2020-09-11 05:35:57 +09:00
|
|
|
<?php
|
|
|
|
|
2021-10-10 17:26:18 +09:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
2020-09-11 05:35:57 +09:00
|
|
|
// {{{ License
|
2020-10-11 05:05:16 +09:00
|
|
|
|
2020-09-11 05:35:57 +09: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-11 05:05:16 +09:00
|
|
|
|
2020-09-11 05:35:57 +09:00
|
|
|
// }}}
|
|
|
|
|
2021-12-09 07:44:04 +09:00
|
|
|
namespace Component\Conversation;
|
2020-09-11 05:35:57 +09:00
|
|
|
|
2021-12-28 15:18:59 +09:00
|
|
|
use App\Core\Cache;
|
2021-12-01 10:33:12 +09:00
|
|
|
use App\Core\DB\DB;
|
2020-09-11 05:35:57 +09:00
|
|
|
use App\Core\Event;
|
2022-01-01 19:31:10 +09:00
|
|
|
use function App\Core\I18n\_m;
|
2021-12-09 07:44:04 +09:00
|
|
|
use App\Core\Modules\Component;
|
|
|
|
use App\Core\Router\RouteLoader;
|
2021-11-07 10:32:06 +09:00
|
|
|
use App\Core\Router\Router;
|
2022-01-01 19:31:10 +09:00
|
|
|
use App\Entity\Activity;
|
2021-11-07 10:32:06 +09:00
|
|
|
use App\Entity\Actor;
|
2020-09-11 05:35:57 +09:00
|
|
|
use App\Entity\Note;
|
|
|
|
use App\Util\Common;
|
2021-12-20 02:43:43 +09:00
|
|
|
use Component\Conversation\Entity\Conversation as ConversationEntity;
|
2022-01-04 05:38:45 +09:00
|
|
|
use Component\Conversation\Entity\ConversationMute;
|
|
|
|
use Functional as F;
|
2020-09-11 05:35:57 +09:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
|
2021-12-09 07:44:04 +09:00
|
|
|
class Conversation extends Component
|
2020-09-11 05:35:57 +09:00
|
|
|
{
|
2022-01-04 05:38:45 +09:00
|
|
|
public function onAddRoute(RouteLoader $r): bool
|
|
|
|
{
|
|
|
|
$r->connect('conversation', '/conversation/{conversation_id<\d+>}', [Controller\Conversation::class, 'showConversation']);
|
|
|
|
$r->connect('conversation_mute', '/conversation/{conversation_id<\d+>}/mute', [Controller\Conversation::class, 'muteConversation']);
|
|
|
|
$r->connect('conversation_reply_to', '/conversation/reply', [Controller\Conversation::class, 'addReply']);
|
|
|
|
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2021-12-20 02:43:43 +09:00
|
|
|
/**
|
2022-01-04 03:50:23 +09:00
|
|
|
* **Assigns** the given local Note it's corresponding **Conversation**.
|
2021-12-20 02:43:43 +09:00
|
|
|
*
|
|
|
|
* **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
|
2022-01-04 03:50:23 +09:00
|
|
|
*
|
|
|
|
* @param \App\Entity\Note $current_note Local Note currently being assigned a Conversation
|
|
|
|
* @param null|int $parent_id If present, it's a reply
|
2021-12-20 02:43:43 +09:00
|
|
|
*/
|
|
|
|
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-24 01:20:52 +09:00
|
|
|
// Set current_note's own conversation_id
|
2021-12-20 02:43:43 +09: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-07 04:47:15 +09:00
|
|
|
/**
|
2021-12-11 08:09:39 +09:00
|
|
|
* HTML rendering event that adds a reply link as a note
|
2022-01-04 03:50:23 +09:00
|
|
|
* action, if a user is logged in.
|
|
|
|
*
|
|
|
|
* @param \App\Entity\Note $note The Note being rendered
|
|
|
|
* @param array $actions Contains keys 'url' (linking 'conversation_reply_to'
|
|
|
|
* route), 'title' (used as title for aforementioned url),
|
|
|
|
* 'classes' (CSS styling classes used to visually inform the user of action context),
|
|
|
|
* 'id' (HTML markup id used to redirect user to this anchor upon performing the action)
|
|
|
|
*
|
|
|
|
* @throws \App\Util\Exception\ServerException
|
2020-11-07 04:47:15 +09:00
|
|
|
*/
|
2021-11-07 10:32:06 +09:00
|
|
|
public function onAddNoteActions(Request $request, Note $note, array &$actions): bool
|
2020-09-11 05:35:57 +09:00
|
|
|
{
|
2021-11-17 04:36:17 +09:00
|
|
|
if (\is_null(Common::user())) {
|
2020-11-07 04:47:15 +09:00
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2022-01-04 05:38:45 +09:00
|
|
|
$from = $request->query->has('from')
|
|
|
|
? $request->query->get('from')
|
|
|
|
: $request->getPathInfo();
|
2021-11-07 10:32:06 +09:00
|
|
|
|
2022-01-04 05:38:45 +09:00
|
|
|
$reply_action_url = Router::url(
|
|
|
|
'conversation_reply_to',
|
|
|
|
[
|
|
|
|
'reply_to_id' => $note->getId(),
|
|
|
|
'from' => $from . '#note-anchor-' . $note->getId(),
|
|
|
|
],
|
|
|
|
Router::ABSOLUTE_PATH,
|
|
|
|
);
|
2021-11-07 10:32:06 +09:00
|
|
|
|
|
|
|
$reply_action = [
|
2021-11-17 04:36:17 +09:00
|
|
|
'url' => $reply_action_url,
|
2021-12-24 01:20:52 +09:00
|
|
|
'title' => _m('Reply to this note!'),
|
2021-11-17 04:36:17 +09:00
|
|
|
'classes' => 'button-container reply-button-container note-actions-unset',
|
|
|
|
'id' => 'reply-button-container-' . $note->getId(),
|
2021-11-07 10:32:06 +09:00
|
|
|
];
|
|
|
|
|
|
|
|
$actions[] = $reply_action;
|
2022-01-04 03:50:23 +09:00
|
|
|
|
2021-11-07 10:32:06 +09:00
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2022-01-04 05:38:45 +09:00
|
|
|
/**
|
|
|
|
* Posting event to add extra info to a note
|
|
|
|
*/
|
|
|
|
public function onPostingModifyData(Request $request, Actor $actor, array &$data): bool
|
2021-12-20 02:43:43 +09:00
|
|
|
{
|
2022-01-04 05:38:45 +09:00
|
|
|
$data['reply_to_id'] = $request->get('_route') === 'conversation_reply_to' && $request->query->has('reply_to_id')
|
|
|
|
? $request->query->getInt('reply_to_id')
|
|
|
|
: null;
|
2022-01-04 03:50:23 +09:00
|
|
|
|
2022-01-04 05:38:45 +09:00
|
|
|
if (!\is_null($data['reply_to_id'])) {
|
|
|
|
Note::ensureCanInteract(Note::getById($data['reply_to_id']), $actor);
|
|
|
|
}
|
2021-12-20 02:43:43 +09:00
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2021-11-17 04:36:17 +09:00
|
|
|
/**
|
2022-01-04 03:50:23 +09:00
|
|
|
* Append on note information about user actions.
|
|
|
|
*
|
|
|
|
* @param array $vars Contains information related to Note currently being rendered
|
|
|
|
* @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'])
|
2021-11-17 04:36:17 +09:00
|
|
|
*/
|
2021-12-01 22:42:39 +09:00
|
|
|
public function onAppendCardNote(array $vars, array &$result): bool
|
2021-11-17 04:36:17 +09:00
|
|
|
{
|
2021-12-30 02:54:02 +09:00
|
|
|
// The current Note being rendered
|
|
|
|
$note = $vars['note'];
|
2021-11-11 00:44:28 +09:00
|
|
|
|
2021-12-30 02:54:02 +09:00
|
|
|
// Will have actors array, and action string
|
|
|
|
// Actors are the subjects, action is the verb (in the final phrase)
|
2022-01-04 05:38:45 +09:00
|
|
|
$reply_actors = F\map(
|
|
|
|
$note->getReplies(),
|
|
|
|
fn (Note $reply) => Actor::getByPK($reply->getActorId()),
|
|
|
|
);
|
2021-12-30 02:54:02 +09:00
|
|
|
|
2022-01-04 05:38:45 +09:00
|
|
|
if (empty($reply_actors)) {
|
2021-11-17 04:36:17 +09:00
|
|
|
return Event::next;
|
2020-09-11 05:35:57 +09:00
|
|
|
}
|
2021-11-07 10:32:06 +09:00
|
|
|
|
2021-11-11 00:44:28 +09:00
|
|
|
// Filter out multiple replies from the same actor
|
2022-01-01 19:31:10 +09:00
|
|
|
$reply_actors = array_unique($reply_actors, \SORT_REGULAR);
|
|
|
|
$result[] = ['actors' => $reply_actors, 'action' => 'replied to'];
|
2021-11-11 00:44:28 +09:00
|
|
|
|
2021-12-01 22:42:39 +09:00
|
|
|
return Event::next;
|
2021-11-07 10:32:06 +09:00
|
|
|
}
|
|
|
|
|
2022-01-04 03:50:23 +09:00
|
|
|
/**
|
|
|
|
* Informs **\App\Component\Posting::onAppendRightPostingBlock**, of the **current page context** in which the given
|
|
|
|
* Actor is in. This is valuable when posting within a group route, allowing \App\Component\Posting to create a
|
|
|
|
* Note **targeting** that specific Group.
|
|
|
|
*
|
|
|
|
* @param \App\Entity\Actor $actor The Actor currently attempting to post a Note
|
|
|
|
* @param null|\App\Entity\Actor $context_actor The 'owner' of the current route (e.g. Group or Actor), used to target it
|
|
|
|
*/
|
2022-01-04 05:38:45 +09:00
|
|
|
public function onPostingGetContextActor(Request $request, Actor $actor, ?Actor &$context_actor)
|
2021-12-25 20:23:25 +09:00
|
|
|
{
|
2022-01-04 05:38:45 +09:00
|
|
|
$to_note_id = $request->query->get('reply_to_id');
|
|
|
|
if (!\is_null($to_note_id)) {
|
2021-12-25 20:23:25 +09:00
|
|
|
// Getting the actor itself
|
2022-01-04 05:38:45 +09:00
|
|
|
$context_actor = Actor::getById(Note::getById((int) $to_note_id)->getActorId());
|
2021-12-25 20:23:25 +09:00
|
|
|
return Event::stop;
|
2022-01-04 05:38:45 +09:00
|
|
|
}
|
2021-12-25 20:23:25 +09:00
|
|
|
return Event::next;
|
|
|
|
}
|
2021-12-28 15:18:59 +09:00
|
|
|
|
2022-01-04 03:50:23 +09:00
|
|
|
/**
|
|
|
|
* Event launched when deleting given Note, it's deletion implies further changes to object related to this Note.
|
|
|
|
* Please note, **replies are NOT deleted**, their reply_to is only set to null since this Note no longer exists.
|
|
|
|
*
|
|
|
|
* @param \App\Entity\Note $note Note being deleted
|
|
|
|
* @param \App\Entity\Actor $actor Actor that performed the delete action
|
|
|
|
*/
|
2021-12-28 15:18:59 +09:00
|
|
|
public function onNoteDeleteRelated(Note &$note, Actor $actor): bool
|
|
|
|
{
|
2022-01-04 05:38:45 +09:00
|
|
|
// Ensure we have the most up to date replies
|
|
|
|
Cache::delete(Note::cacheKeys($note->getId())['replies']);
|
|
|
|
DB::wrapInTransaction(fn () => F\each($note->getReplies(), fn (Note $note) => $note->setReplyTo(null)));
|
|
|
|
Cache::delete(Note::cacheKeys($note->getId())['replies']);
|
2021-12-28 15:18:59 +09:00
|
|
|
return Event::next;
|
|
|
|
}
|
2022-01-01 19:31:10 +09:00
|
|
|
|
2022-01-04 03:50:23 +09:00
|
|
|
/**
|
|
|
|
* Adds extra actions related to Conversation Component, that act upon/from the given Note.
|
|
|
|
*
|
|
|
|
* @param \App\Entity\Note $note Current Note being rendered
|
|
|
|
* @param array $actions Containing 'url' (Controller connected route), 'title' (used in anchor link containing the url), ?'classes' (CSS classes required for styling, if needed)
|
|
|
|
*
|
|
|
|
* @throws \App\Util\Exception\ServerException
|
|
|
|
*
|
|
|
|
* @return bool EventHook
|
|
|
|
*/
|
2022-01-01 19:31:10 +09:00
|
|
|
public function onAddExtraNoteActions(Request $request, Note $note, array &$actions)
|
|
|
|
{
|
2022-01-04 05:38:45 +09:00
|
|
|
if (\is_null($user = Common::user())) {
|
2022-01-01 19:31:10 +09:00
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2022-01-14 02:47:47 +09:00
|
|
|
$from = $request->query->has('from')
|
|
|
|
? $request->query->get('from')
|
|
|
|
: $request->getPathInfo();
|
|
|
|
|
|
|
|
$mute_extra_action_url = Router::url(
|
|
|
|
'conversation_mute',
|
|
|
|
[
|
|
|
|
'conversation_id' => $note->getConversationId(),
|
|
|
|
'from' => $from . '#note-anchor-' . $note->getId(),
|
|
|
|
],
|
|
|
|
Router::ABSOLUTE_PATH,
|
|
|
|
);
|
|
|
|
|
2022-01-01 19:31:10 +09:00
|
|
|
$actions[] = [
|
2022-01-08 06:14:13 +09:00
|
|
|
'title' => ConversationMute::isMuted($note, $user) ? _m('Unmute conversation') : _m('Mute conversation'),
|
2022-01-01 19:31:10 +09:00
|
|
|
'classes' => '',
|
2022-01-14 02:47:47 +09:00
|
|
|
'url' => $mute_extra_action_url,
|
2022-01-01 19:31:10 +09:00
|
|
|
];
|
|
|
|
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onNewNotificationShould(Activity $activity, Actor $actor)
|
|
|
|
{
|
2022-01-04 05:38:45 +09:00
|
|
|
if ($activity->getObjectType() === 'note' && ConversationMute::isMuted($activity, $actor)) {
|
|
|
|
return Event::stop;
|
2022-01-01 19:31:10 +09:00
|
|
|
}
|
2022-01-04 05:38:45 +09:00
|
|
|
return Event::next;
|
2022-01-01 19:31:10 +09:00
|
|
|
}
|
2020-09-11 05:35:57 +09:00
|
|
|
}
|