Conversation.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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\DB\DB;
  21. use App\Core\Event;
  22. use function App\Core\I18n\_m;
  23. use App\Core\Modules\Component;
  24. use App\Core\Router\RouteLoader;
  25. use App\Core\Router\Router;
  26. use App\Entity\Actor;
  27. use App\Entity\Note;
  28. use App\Util\Common;
  29. use App\Util\Formatting;
  30. use Component\Conversation\Controller\Reply as ReplyController;
  31. use Component\Conversation\Entity\Conversation as ConversationEntity;
  32. use Symfony\Component\HttpFoundation\Request;
  33. class Conversation extends Component
  34. {
  35. /**
  36. * **Assigns** the given local Note it's corresponding **Conversation**
  37. *
  38. * **If a _$parent_id_ is not given**, then the Actor is not attempting a reply,
  39. * therefore, we can assume (for now) that we need to create a new Conversation and assign it
  40. * to the newly created Note (please look at Component\Posting::storeLocalNote, where this function is used)
  41. *
  42. * **On the other hand**, given a _$parent_id_, the Actor is attempting to post a reply. Meaning that,
  43. * this Note conversation_id should be same as the parent Note
  44. */
  45. public static function assignLocalConversation(Note $current_note, ?int $parent_id): void
  46. {
  47. if (!$parent_id) {
  48. // If none found, we don't know yet if it is a reply or root
  49. // Let's assume for now that it's a new conversation and deal with stitching later
  50. $conversation = ConversationEntity::create(['initial_note_id' => $current_note->getId()]);
  51. // We need the Conversation id itself, so a persist is in order
  52. DB::persist($conversation);
  53. // Set current_note's own conversation_id
  54. $current_note->setConversationId($conversation->getId());
  55. } else {
  56. // It's a reply for sure
  57. // Set reply_to property in newly created Note to parent's id
  58. $current_note->setReplyTo($parent_id);
  59. // Parent will have a conversation of its own, the reply should have the same one
  60. $parent_note = Note::getById($parent_id);
  61. $current_note->setConversationId($parent_note->getConversationId());
  62. }
  63. DB::merge($current_note);
  64. DB::flush();
  65. }
  66. /**
  67. * HTML rendering event that adds a reply link as a note
  68. * action, if a user is logged in
  69. */
  70. public function onAddNoteActions(Request $request, Note $note, array &$actions): bool
  71. {
  72. if (\is_null(Common::user())) {
  73. return Event::next;
  74. }
  75. // Generating URL for reply action route
  76. $args = ['note_id' => $note->getId(), 'actor_id' => $note->getActor()->getId()];
  77. $type = Router::ABSOLUTE_PATH;
  78. $reply_action_url = Router::url('reply_add', $args, $type);
  79. $query_string = $request->getQueryString();
  80. // Concatenating get parameter to redirect the user to where he came from
  81. $reply_action_url .= !\is_null($query_string) ? '?from=' : '&from=';
  82. $reply_action_url .= mb_substr($query_string, 2);
  83. $reply_action = [
  84. 'url' => $reply_action_url,
  85. 'title' => _m('Reply to this note!'),
  86. 'classes' => 'button-container reply-button-container note-actions-unset',
  87. 'id' => 'reply-button-container-' . $note->getId(),
  88. ];
  89. $actions[] = $reply_action;
  90. return Event::next;
  91. }
  92. public function onAddExtraArgsToNoteContent(Request $request, Actor $actor, array $data, array &$extra_args): bool
  93. {
  94. // If Actor is adding a reply, get parent's Note id
  95. // Else it's null
  96. $extra_args['reply_to'] = $request->get('_route') === 'reply_add' ? (int) $request->get('note_id') : null;
  97. return Event::next;
  98. }
  99. /**
  100. * Append on note information about user actions
  101. */
  102. public function onAppendCardNote(array $vars, array &$result): bool
  103. {
  104. // if note is the original, append on end "user replied to this"
  105. // if note is the reply itself: append on end "in response to user in conversation"
  106. $check_user = !\is_null(Common::user());
  107. $note = $vars['note'];
  108. $complementary_info = '';
  109. $reply_actor = [];
  110. $note_replies = $note->getReplies();
  111. // Get actors who replied
  112. foreach ($note_replies as $reply) {
  113. $reply_actor[] = Actor::getByPK($reply->getActorId());
  114. }
  115. if (\count($reply_actor) < 1) {
  116. return Event::next;
  117. }
  118. // Filter out multiple replies from the same actor
  119. $reply_actor = array_unique($reply_actor, \SORT_REGULAR);
  120. // Add to complementary info
  121. foreach ($reply_actor as $actor) {
  122. $reply_actor_url = $actor->getUrl();
  123. $reply_actor_nickname = $actor->getNickname();
  124. if ($check_user && $actor->getId() === (Common::actor())->getId()) {
  125. // If the reply is yours
  126. $you_translation = _m('You');
  127. $prepend = "<a href={$reply_actor_url}>{$you_translation}</a>, " . ($prepend = &$complementary_info);
  128. $complementary_info = $prepend;
  129. } else {
  130. // If the repeat is from someone else
  131. $complementary_info .= "<a href={$reply_actor_url}>{$reply_actor_nickname}</a>, ";
  132. }
  133. }
  134. $complementary_info = rtrim(trim($complementary_info), ',');
  135. $complementary_info .= _m(' replied to this note.');
  136. $result[] = Formatting::twigRenderString($complementary_info, []);
  137. return Event::next;
  138. }
  139. public function onAddRoute(RouteLoader $r): bool
  140. {
  141. $r->connect('reply_add', '/object/note/new?to={actor_id<\d+>}&reply_to={note_id<\d+>}', [ReplyController::class, 'addReply']);
  142. $r->connect('conversation', '/conversation/{conversation_id<\d+>}', [Controller\Conversation::class, 'showConversation']);
  143. return Event::next;
  144. }
  145. }