Conversation.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. namespace Component\Conversation;
  20. use App\Core\Cache;
  21. use App\Core\DB\DB;
  22. use App\Core\Event;
  23. use function App\Core\I18n\_m;
  24. use App\Core\Modules\Component;
  25. use App\Core\Router\RouteLoader;
  26. use App\Core\Router\Router;
  27. use App\Entity\Activity;
  28. use App\Entity\Actor;
  29. use App\Entity\Note;
  30. use App\Util\Common;
  31. use Component\Conversation\Entity\Conversation as ConversationEntity;
  32. use Component\Conversation\Entity\ConversationMute;
  33. use Functional as F;
  34. use Symfony\Component\HttpFoundation\Request;
  35. class Conversation extends Component
  36. {
  37. public function onAddRoute(RouteLoader $r): bool
  38. {
  39. $r->connect('conversation', '/conversation/{conversation_id<\d+>}', [Controller\Conversation::class, 'showConversation']);
  40. $r->connect('conversation_mute', '/conversation/{conversation_id<\d+>}/mute', [Controller\Conversation::class, 'muteConversation']);
  41. $r->connect('conversation_reply_to', '/conversation/reply', [Controller\Conversation::class, 'addReply']);
  42. return Event::next;
  43. }
  44. /**
  45. * **Assigns** the given local Note it's corresponding **Conversation**.
  46. *
  47. * **If a _$parent_id_ is not given**, then the Actor is not attempting a reply,
  48. * therefore, we can assume (for now) that we need to create a new Conversation and assign it
  49. * to the newly created Note (please look at Component\Posting::storeLocalNote, where this function is used)
  50. *
  51. * **On the other hand**, given a _$parent_id_, the Actor is attempting to post a reply. Meaning that,
  52. * this Note conversation_id should be same as the parent Note
  53. *
  54. * @param \App\Entity\Note $current_note Local Note currently being assigned a Conversation
  55. * @param null|int $parent_id If present, it's a reply
  56. */
  57. public static function assignLocalConversation(Note $current_note, ?int $parent_id): void
  58. {
  59. if (!$parent_id) {
  60. // If none found, we don't know yet if it is a reply or root
  61. // Let's assume for now that it's a new conversation and deal with stitching later
  62. $conversation = ConversationEntity::create(['initial_note_id' => $current_note->getId()]);
  63. // We need the Conversation id itself, so a persist is in order
  64. DB::persist($conversation);
  65. // Set current_note's own conversation_id
  66. $current_note->setConversationId($conversation->getId());
  67. } else {
  68. // It's a reply for sure
  69. // Set reply_to property in newly created Note to parent's id
  70. $current_note->setReplyTo($parent_id);
  71. // Parent will have a conversation of its own, the reply should have the same one
  72. $parent_note = Note::getById($parent_id);
  73. $current_note->setConversationId($parent_note->getConversationId());
  74. }
  75. DB::merge($current_note);
  76. }
  77. /**
  78. * HTML rendering event that adds a reply link as a note
  79. * action, if a user is logged in.
  80. *
  81. * @param \App\Entity\Note $note The Note being rendered
  82. * @param array $actions Contains keys 'url' (linking 'conversation_reply_to'
  83. * route), 'title' (used as title for aforementioned url),
  84. * 'classes' (CSS styling classes used to visually inform the user of action context),
  85. * 'id' (HTML markup id used to redirect user to this anchor upon performing the action)
  86. *
  87. * @throws \App\Util\Exception\ServerException
  88. */
  89. public function onAddNoteActions(Request $request, Note $note, array &$actions): bool
  90. {
  91. if (\is_null(Common::user())) {
  92. return Event::next;
  93. }
  94. $from = $request->query->has('from')
  95. ? $request->query->get('from')
  96. : $request->getPathInfo();
  97. $reply_action_url = Router::url(
  98. 'conversation_reply_to',
  99. [
  100. 'reply_to_id' => $note->getId(),
  101. 'from' => $from . '#note-anchor-' . $note->getId(),
  102. ],
  103. Router::ABSOLUTE_PATH,
  104. );
  105. $reply_action = [
  106. 'url' => $reply_action_url,
  107. 'title' => _m('Reply to this note!'),
  108. 'classes' => 'button-container reply-button-container note-actions-unset',
  109. 'id' => 'reply-button-container-' . $note->getId(),
  110. ];
  111. $actions[] = $reply_action;
  112. return Event::next;
  113. }
  114. /**
  115. * Posting event to add extra info to a note
  116. */
  117. public function onPostingModifyData(Request $request, Actor $actor, array &$data): bool
  118. {
  119. $data['reply_to_id'] = $request->get('_route') === 'conversation_reply_to' && $request->query->has('reply_to_id')
  120. ? $request->query->getInt('reply_to_id')
  121. : null;
  122. if (!\is_null($data['reply_to_id'])) {
  123. Note::ensureCanInteract(Note::getById($data['reply_to_id']), $actor);
  124. }
  125. return Event::next;
  126. }
  127. /**
  128. * Append on note information about user actions.
  129. *
  130. * @param array $vars Contains information related to Note currently being rendered
  131. * @param array $result Contains keys 'actors', and 'action'. Needed to construct a string, stating who ($result['actors']), has already performed a reply ($result['action']), in the given Note (vars['note'])
  132. */
  133. public function onAppendCardNote(array $vars, array &$result): bool
  134. {
  135. // The current Note being rendered
  136. $note = $vars['note'];
  137. // Will have actors array, and action string
  138. // Actors are the subjects, action is the verb (in the final phrase)
  139. $reply_actors = F\map(
  140. $note->getReplies(),
  141. fn (Note $reply) => Actor::getByPK($reply->getActorId()),
  142. );
  143. if (empty($reply_actors)) {
  144. return Event::next;
  145. }
  146. // Filter out multiple replies from the same actor
  147. $reply_actors = array_unique($reply_actors, \SORT_REGULAR);
  148. $result[] = ['actors' => $reply_actors, 'action' => 'replied to'];
  149. return Event::next;
  150. }
  151. /**
  152. * Informs **\App\Component\Posting::onAppendRightPostingBlock**, of the **current page context** in which the given
  153. * Actor is in. This is valuable when posting within a group route, allowing \App\Component\Posting to create a
  154. * Note **targeting** that specific Group.
  155. *
  156. * @param \App\Entity\Actor $actor The Actor currently attempting to post a Note
  157. * @param null|\App\Entity\Actor $context_actor The 'owner' of the current route (e.g. Group or Actor), used to target it
  158. */
  159. public function onPostingGetContextActor(Request $request, Actor $actor, ?Actor &$context_actor)
  160. {
  161. $to_note_id = $request->query->get('reply_to_id');
  162. if (!\is_null($to_note_id)) {
  163. // Getting the actor itself
  164. $context_actor = Actor::getById(Note::getById((int) $to_note_id)->getActorId());
  165. return Event::stop;
  166. }
  167. return Event::next;
  168. }
  169. /**
  170. * Event launched when deleting given Note, it's deletion implies further changes to object related to this Note.
  171. * Please note, **replies are NOT deleted**, their reply_to is only set to null since this Note no longer exists.
  172. *
  173. * @param \App\Entity\Note $note Note being deleted
  174. * @param \App\Entity\Actor $actor Actor that performed the delete action
  175. */
  176. public function onNoteDeleteRelated(Note &$note, Actor $actor): bool
  177. {
  178. // Ensure we have the most up to date replies
  179. Cache::delete(Note::cacheKeys($note->getId())['replies']);
  180. DB::wrapInTransaction(fn () => F\each($note->getReplies(), fn (Note $note) => $note->setReplyTo(null)));
  181. Cache::delete(Note::cacheKeys($note->getId())['replies']);
  182. return Event::next;
  183. }
  184. /**
  185. * Adds extra actions related to Conversation Component, that act upon/from the given Note.
  186. *
  187. * @param \App\Entity\Note $note Current Note being rendered
  188. * @param array $actions Containing 'url' (Controller connected route), 'title' (used in anchor link containing the url), ?'classes' (CSS classes required for styling, if needed)
  189. *
  190. * @throws \App\Util\Exception\ServerException
  191. *
  192. * @return bool EventHook
  193. */
  194. public function onAddExtraNoteActions(Request $request, Note $note, array &$actions)
  195. {
  196. if (\is_null($user = Common::user())) {
  197. return Event::next;
  198. }
  199. $from = $request->query->has('from')
  200. ? $request->query->get('from')
  201. : $request->getPathInfo();
  202. $mute_extra_action_url = Router::url(
  203. 'conversation_mute',
  204. [
  205. 'conversation_id' => $note->getConversationId(),
  206. 'from' => $from . '#note-anchor-' . $note->getId(),
  207. ],
  208. Router::ABSOLUTE_PATH,
  209. );
  210. $actions[] = [
  211. 'title' => ConversationMute::isMuted($note, $user) ? _m('Unmute conversation') : _m('Mute conversation'),
  212. 'classes' => '',
  213. 'url' => $mute_extra_action_url,
  214. ];
  215. return Event::next;
  216. }
  217. public function onNewNotificationShould(Activity $activity, Actor $actor)
  218. {
  219. if ($activity->getObjectType() === 'note' && ConversationMute::isMuted($activity, $actor)) {
  220. return Event::stop;
  221. }
  222. return Event::next;
  223. }
  224. }