| 
									
										
										
										
											2020-09-10 20:35:57 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-10 09:26:18 +01:00
										 |  |  | declare(strict_types = 1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-10 20:35:57 +00:00
										 |  |  | // {{{ License
 | 
					
						
							| 
									
										
										
										
											2020-10-10 20:05:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-10 20:35:57 +00:00
										 |  |  | // This file is part of GNU social - https://www.gnu.org/software/social
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // GNU social is free software: you can redistribute it and/or modify
 | 
					
						
							|  |  |  | // it under the terms of the GNU Affero General Public License as published by
 | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or
 | 
					
						
							|  |  |  | // (at your option) any later version.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // GNU social is distributed in the hope that it will be useful,
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
					
						
							|  |  |  | // GNU Affero General Public License for more details.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // You should have received a copy of the GNU Affero General Public License
 | 
					
						
							|  |  |  | // along with GNU social.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							| 
									
										
										
										
											2020-10-10 20:05:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-10 20:35:57 +00:00
										 |  |  | // }}}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-08 22:44:04 +00:00
										 |  |  | namespace Component\Conversation; | 
					
						
							| 
									
										
										
										
											2020-09-10 20:35:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-28 06:18:59 +00:00
										 |  |  | use App\Core\Cache; | 
					
						
							| 
									
										
										
										
											2021-12-01 01:33:12 +00:00
										 |  |  | use App\Core\DB\DB; | 
					
						
							| 
									
										
										
										
											2020-09-10 20:35:57 +00:00
										 |  |  | use App\Core\Event; | 
					
						
							| 
									
										
										
										
											2022-01-01 10:31:10 +00:00
										 |  |  | use function App\Core\I18n\_m; | 
					
						
							| 
									
										
										
										
											2021-12-08 22:44:04 +00:00
										 |  |  | use App\Core\Modules\Component; | 
					
						
							|  |  |  | use App\Core\Router\RouteLoader; | 
					
						
							| 
									
										
										
										
											2021-11-07 01:32:06 +00:00
										 |  |  | use App\Core\Router\Router; | 
					
						
							| 
									
										
										
										
											2022-01-01 10:31:10 +00:00
										 |  |  | use App\Entity\Activity; | 
					
						
							| 
									
										
										
										
											2021-11-07 01:32:06 +00:00
										 |  |  | use App\Entity\Actor; | 
					
						
							| 
									
										
										
										
											2020-09-10 20:35:57 +00:00
										 |  |  | use App\Entity\Note; | 
					
						
							|  |  |  | use App\Util\Common; | 
					
						
							| 
									
										
										
										
											2021-12-08 22:44:04 +00:00
										 |  |  | use Component\Conversation\Controller\Reply as ReplyController; | 
					
						
							| 
									
										
										
										
											2021-12-19 17:43:43 +00:00
										 |  |  | use Component\Conversation\Entity\Conversation as ConversationEntity; | 
					
						
							| 
									
										
										
										
											2020-09-10 20:35:57 +00:00
										 |  |  | use Symfony\Component\HttpFoundation\Request; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-08 22:44:04 +00:00
										 |  |  | class Conversation extends Component | 
					
						
							| 
									
										
										
										
											2020-09-10 20:35:57 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-12-19 17:43:43 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * **Assigns** the given local Note it's corresponding **Conversation** | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * **If a _$parent_id_ is not given**, then the Actor is not attempting a reply, | 
					
						
							|  |  |  |      * therefore, we can assume (for now) that we need to create a new Conversation and assign it | 
					
						
							|  |  |  |      * to the newly created Note (please look at Component\Posting::storeLocalNote, where this function is used) | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * **On the other hand**, given a _$parent_id_, the Actor is attempting to post a reply. Meaning that, | 
					
						
							|  |  |  |      * this Note conversation_id should be same as the parent Note | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public static function assignLocalConversation(Note $current_note, ?int $parent_id): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (!$parent_id) { | 
					
						
							|  |  |  |             // If none found, we don't know yet if it is a reply or root
 | 
					
						
							|  |  |  |             // Let's assume for now that it's a new conversation and deal with stitching later
 | 
					
						
							|  |  |  |             $conversation = ConversationEntity::create(['initial_note_id' => $current_note->getId()]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // We need the Conversation id itself, so a persist is in order
 | 
					
						
							|  |  |  |             DB::persist($conversation); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-23 16:20:52 +00:00
										 |  |  |             // Set current_note's own conversation_id
 | 
					
						
							| 
									
										
										
										
											2021-12-19 17:43:43 +00:00
										 |  |  |             $current_note->setConversationId($conversation->getId()); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             // It's a reply for sure
 | 
					
						
							|  |  |  |             // Set reply_to property in newly created Note to parent's id
 | 
					
						
							|  |  |  |             $current_note->setReplyTo($parent_id); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // Parent will have a conversation of its own, the reply should have the same one
 | 
					
						
							|  |  |  |             $parent_note = Note::getById($parent_id); | 
					
						
							|  |  |  |             $current_note->setConversationId($parent_note->getConversationId()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         DB::merge($current_note); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-06 19:47:15 +00:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-12-10 23:09:39 +00:00
										 |  |  |      * HTML rendering event that adds a reply link as a note | 
					
						
							| 
									
										
										
										
											2021-11-07 01:32:06 +00:00
										 |  |  |      * action, if a user is logged in | 
					
						
							| 
									
										
										
										
											2020-11-06 19:47:15 +00:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-11-07 01:32:06 +00:00
										 |  |  |     public function onAddNoteActions(Request $request, Note $note, array &$actions): bool | 
					
						
							| 
									
										
										
										
											2020-09-10 20:35:57 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-11-16 19:36:17 +00:00
										 |  |  |         if (\is_null(Common::user())) { | 
					
						
							| 
									
										
										
										
											2020-11-06 19:47:15 +00:00
										 |  |  |             return Event::next; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-19 17:43:43 +00:00
										 |  |  |         // Generating URL for reply action route
 | 
					
						
							|  |  |  |         $args             = ['note_id' => $note->getId(), 'actor_id' => $note->getActor()->getId()]; | 
					
						
							| 
									
										
										
										
											2021-11-16 19:36:17 +00:00
										 |  |  |         $type             = Router::ABSOLUTE_PATH; | 
					
						
							| 
									
										
										
										
											2021-11-07 01:32:06 +00:00
										 |  |  |         $reply_action_url = Router::url('reply_add', $args, $type); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-15 19:27:39 +00:00
										 |  |  |         $query_string = $request->getQueryString(); | 
					
						
							| 
									
										
										
										
											2021-11-07 01:32:06 +00:00
										 |  |  |         // Concatenating get parameter to redirect the user to where he came from
 | 
					
						
							| 
									
										
										
										
											2021-12-30 18:34:07 +00:00
										 |  |  |         $reply_action_url .= '?from=' . urlencode($request->getRequestUri()) . '#note-anchor-' . $note->getId(); | 
					
						
							| 
									
										
										
										
											2021-11-07 01:32:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $reply_action = [ | 
					
						
							| 
									
										
										
										
											2021-11-16 19:36:17 +00:00
										 |  |  |             'url'     => $reply_action_url, | 
					
						
							| 
									
										
										
										
											2021-12-23 16:20:52 +00:00
										 |  |  |             'title'   => _m('Reply to this note!'), | 
					
						
							| 
									
										
										
										
											2021-11-16 19:36:17 +00:00
										 |  |  |             'classes' => 'button-container reply-button-container note-actions-unset', | 
					
						
							|  |  |  |             'id'      => 'reply-button-container-' . $note->getId(), | 
					
						
							| 
									
										
										
										
											2021-11-07 01:32:06 +00:00
										 |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $actions[] = $reply_action; | 
					
						
							|  |  |  |         return Event::next; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-19 17:43:43 +00:00
										 |  |  |     public function onAddExtraArgsToNoteContent(Request $request, Actor $actor, array $data, array &$extra_args): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // If Actor is adding a reply, get parent's Note id
 | 
					
						
							|  |  |  |         // Else it's null
 | 
					
						
							|  |  |  |         $extra_args['reply_to'] = $request->get('_route') === 'reply_add' ? (int) $request->get('note_id') : null; | 
					
						
							|  |  |  |         return Event::next; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-16 19:36:17 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Append on note information about user actions | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-12-01 13:42:39 +00:00
										 |  |  |     public function onAppendCardNote(array $vars, array &$result): bool | 
					
						
							| 
									
										
										
										
											2021-11-16 19:36:17 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-12-29 17:54:02 +00:00
										 |  |  |         // If note is the original and user isn't the one who repeated, append on end "user repeated this"
 | 
					
						
							|  |  |  |         // If user is the one who repeated, append on end "you repeated this, remove repeat?"
 | 
					
						
							| 
									
										
										
										
											2021-11-16 19:36:17 +00:00
										 |  |  |         $check_user = !\is_null(Common::user()); | 
					
						
							| 
									
										
										
										
											2020-11-06 19:47:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-29 17:54:02 +00:00
										 |  |  |         // The current Note being rendered
 | 
					
						
							|  |  |  |         $note = $vars['note']; | 
					
						
							| 
									
										
										
										
											2021-11-10 15:44:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-29 17:54:02 +00:00
										 |  |  |         // Will have actors array, and action string
 | 
					
						
							|  |  |  |         // Actors are the subjects, action is the verb (in the final phrase)
 | 
					
						
							| 
									
										
										
										
											2022-01-01 10:31:10 +00:00
										 |  |  |         $reply_actors = []; | 
					
						
							|  |  |  |         $note_replies = $note->getReplies(); | 
					
						
							| 
									
										
										
										
											2021-12-29 17:54:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // Get actors who repeated the note
 | 
					
						
							| 
									
										
										
										
											2021-11-10 15:44:28 +00:00
										 |  |  |         foreach ($note_replies as $reply) { | 
					
						
							| 
									
										
										
										
											2021-12-29 17:54:02 +00:00
										 |  |  |             $reply_actors[] = Actor::getByPK($reply->getActorId()); | 
					
						
							| 
									
										
										
										
											2021-11-10 15:44:28 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-12-29 17:54:02 +00:00
										 |  |  |         if (\count($reply_actors) < 1) { | 
					
						
							| 
									
										
										
										
											2021-11-16 19:36:17 +00:00
										 |  |  |             return Event::next; | 
					
						
							| 
									
										
										
										
											2020-09-10 20:35:57 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-11-07 01:32:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-10 15:44:28 +00:00
										 |  |  |         // Filter out multiple replies from the same actor
 | 
					
						
							| 
									
										
										
										
											2022-01-01 10:31:10 +00:00
										 |  |  |         $reply_actors = array_unique($reply_actors, \SORT_REGULAR); | 
					
						
							|  |  |  |         $result[]     = ['actors' => $reply_actors, 'action' => 'replied to']; | 
					
						
							| 
									
										
										
										
											2021-11-10 15:44:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-01 13:42:39 +00:00
										 |  |  |         return Event::next; | 
					
						
							| 
									
										
										
										
											2021-11-07 01:32:06 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-19 17:43:43 +00:00
										 |  |  |     public function onAddRoute(RouteLoader $r): bool | 
					
						
							| 
									
										
										
										
											2021-11-07 01:32:06 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-12-19 17:43:43 +00:00
										 |  |  |         $r->connect('reply_add', '/object/note/new?to={actor_id<\d+>}&reply_to={note_id<\d+>}', [ReplyController::class, 'addReply']); | 
					
						
							|  |  |  |         $r->connect('conversation', '/conversation/{conversation_id<\d+>}', [Controller\Conversation::class, 'showConversation']); | 
					
						
							| 
									
										
										
										
											2022-01-01 10:31:10 +00:00
										 |  |  |         $r->connect('conversation_mute', '/conversation/{conversation_id<\d+>}/mute', [Controller\Conversation::class, 'muteConversation']); | 
					
						
							| 
									
										
										
										
											2021-12-01 01:31:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return Event::next; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-12-25 11:23:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function onPostingGetContextActor(Request $request, Actor $actor, ?Actor $context_actor) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $to_query = $request->get('actor_id'); | 
					
						
							|  |  |  |         if (!\is_null($to_query)) { | 
					
						
							|  |  |  |             // Getting the actor itself
 | 
					
						
							|  |  |  |             $context_actor = Actor::getById((int) $to_query); | 
					
						
							|  |  |  |             return Event::stop; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return Event::next; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-12-28 06:18:59 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function onNoteDeleteRelated(Note &$note, Actor $actor): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         Cache::delete("note-replies-{$note->getId()}"); | 
					
						
							|  |  |  |         DB::wrapInTransaction(function () use ($note) { | 
					
						
							|  |  |  |             foreach ($note->getReplies() as $reply) { | 
					
						
							|  |  |  |                 $reply->setReplyTo(null); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         Cache::delete("note-replies-{$note->getId()}"); | 
					
						
							|  |  |  |         return Event::next; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-01-01 10:31:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function onAddExtraNoteActions(Request $request, Note $note, array &$actions) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (\is_null($actor = Common::actor())) { | 
					
						
							|  |  |  |             return Event::next; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $actions[] = [ | 
					
						
							|  |  |  |             'title'   => _m('Mute conversation'), | 
					
						
							|  |  |  |             'classes' => '', | 
					
						
							|  |  |  |             'url'     => Router::url('conversation_mute', ['conversation_id' => $note->getConversationId()]), | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return Event::next; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function onNewNotificationShould(Activity $activity, Actor $actor) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if ($activity->getObjectType() === 'note') { | 
					
						
							|  |  |  |             $is_blocked = !empty(DB::dql( | 
					
						
							|  |  |  |                 <<<'EOQ' | 
					
						
							|  |  |  |                     select 1 | 
					
						
							|  |  |  |                     from note n | 
					
						
							|  |  |  |                     join conversation_block cb with n.conversation_id = cb.conversation_id | 
					
						
							|  |  |  |                     where n.id = :object_id | 
					
						
							|  |  |  |                     EOQ, | 
					
						
							|  |  |  |                 ['object_id' => $activity->getObjectId()], | 
					
						
							|  |  |  |             )); | 
					
						
							|  |  |  |             if ($is_blocked) { | 
					
						
							|  |  |  |                 return Event::stop; | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 return Event::next; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-09-10 20:35:57 +00:00
										 |  |  | } |