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-01 10:33:12 +09:00
|
|
|
use App\Core\DB\DB;
|
2020-09-11 05:35:57 +09:00
|
|
|
use App\Core\Event;
|
2021-12-09 07:44:04 +09:00
|
|
|
use App\Core\Modules\Component;
|
|
|
|
use App\Core\Router\RouteLoader;
|
2021-11-17 04:36:17 +09:00
|
|
|
use function App\Core\I18n\_m;
|
2021-11-07 10:32:06 +09:00
|
|
|
use App\Core\Router\Router;
|
|
|
|
use App\Entity\Actor;
|
2021-12-01 10:33:12 +09:00
|
|
|
use App\Entity\Feed;
|
|
|
|
use App\Entity\LocalUser;
|
2020-09-11 05:35:57 +09:00
|
|
|
use App\Entity\Note;
|
|
|
|
use App\Util\Common;
|
2021-11-17 04:36:17 +09:00
|
|
|
use App\Util\Exception\ServerException;
|
2021-11-07 10:32:06 +09:00
|
|
|
use App\Util\Formatting;
|
2021-12-01 10:33:12 +09:00
|
|
|
use App\Util\Nickname;
|
2021-12-09 07:44:04 +09:00
|
|
|
use Component\Conversation\Controller\Reply as ReplyController;
|
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
|
|
|
{
|
2021-12-09 07:44:04 +09:00
|
|
|
|
2020-11-07 04:47:15 +09:00
|
|
|
/**
|
2021-11-07 10:32:06 +09:00
|
|
|
* HTML rendering event that adds the repeat form as a note
|
|
|
|
* action, if a user is logged in
|
2021-09-06 02:25:53 +09:00
|
|
|
*
|
2021-12-09 07:44:04 +09:00
|
|
|
* @param Request $request
|
|
|
|
* @param Note $note
|
|
|
|
* @param array $actions
|
|
|
|
* @return bool
|
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;
|
|
|
|
}
|
|
|
|
|
2021-11-07 10:32:06 +09:00
|
|
|
// Generating URL for repeat action route
|
2021-11-17 04:36:17 +09:00
|
|
|
$args = ['id' => $note->getId()];
|
|
|
|
$type = Router::ABSOLUTE_PATH;
|
2021-11-07 10:32:06 +09:00
|
|
|
$reply_action_url = Router::url('reply_add', $args, $type);
|
|
|
|
|
2021-11-16 04:27:39 +09:00
|
|
|
$query_string = $request->getQueryString();
|
2021-11-07 10:32:06 +09:00
|
|
|
// Concatenating get parameter to redirect the user to where he came from
|
2021-11-16 04:27:39 +09:00
|
|
|
$reply_action_url .= !\is_null($query_string) ? '?from=' . mb_substr($query_string, 2) : '';
|
2021-11-07 10:32:06 +09:00
|
|
|
|
|
|
|
$reply_action = [
|
2021-11-17 04:36:17 +09:00
|
|
|
'url' => $reply_action_url,
|
2021-12-03 00:05:23 +09:00
|
|
|
'title' => '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;
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2021-12-09 07:44:04 +09:00
|
|
|
|
2021-11-17 04:36:17 +09:00
|
|
|
/**
|
|
|
|
* Append on note information about user actions
|
|
|
|
*
|
2021-12-09 07:44:04 +09:00
|
|
|
* @param array $vars
|
|
|
|
* @param array $result
|
2021-12-01 22:42:39 +09:00
|
|
|
* @return bool
|
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-11-07 10:32:06 +09:00
|
|
|
// if note is the original, append on end "user replied to this"
|
|
|
|
// if note is the reply itself: append on end "in response to user in conversation"
|
2021-11-17 04:36:17 +09:00
|
|
|
$check_user = !\is_null(Common::user());
|
|
|
|
$note = $vars['note'];
|
2020-11-07 04:47:15 +09:00
|
|
|
|
2021-11-11 00:44:28 +09:00
|
|
|
$complementary_info = '';
|
2021-11-17 04:36:17 +09:00
|
|
|
$reply_actor = [];
|
2021-12-09 07:44:04 +09:00
|
|
|
$note_replies = $note->getReplies();
|
2021-11-11 00:44:28 +09:00
|
|
|
|
|
|
|
// Get actors who replied
|
|
|
|
foreach ($note_replies as $reply) {
|
2021-12-09 07:44:04 +09:00
|
|
|
$reply_actor[] = Actor::getWithPK(Note::getWithPK($reply)->getActorId());
|
2021-11-11 00:44:28 +09:00
|
|
|
}
|
2021-11-17 04:36:17 +09:00
|
|
|
if (\count($reply_actor) < 1) {
|
|
|
|
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
|
2021-11-17 04:36:17 +09:00
|
|
|
$reply_actor = array_unique($reply_actor, \SORT_REGULAR);
|
2021-11-11 00:44:28 +09:00
|
|
|
|
|
|
|
// Add to complementary info
|
|
|
|
foreach ($reply_actor as $actor) {
|
2021-11-17 04:36:17 +09:00
|
|
|
$reply_actor_url = $actor->getUrl();
|
2021-11-11 00:44:28 +09:00
|
|
|
$reply_actor_nickname = $actor->getNickname();
|
2021-11-17 04:36:17 +09:00
|
|
|
|
|
|
|
if ($check_user && $actor->getId() === (Common::actor())->getId()) {
|
|
|
|
// If the reply is yours
|
|
|
|
try {
|
|
|
|
$you_translation = _m('You');
|
|
|
|
} catch (ServerException $e) {
|
|
|
|
$you_translation = 'You';
|
|
|
|
}
|
|
|
|
|
|
|
|
$prepend = "<a href={$reply_actor_url}>{$you_translation}</a>, " . ($prepend = &$complementary_info);
|
|
|
|
$complementary_info = $prepend;
|
|
|
|
} else {
|
|
|
|
// If the repeat is from someone else
|
|
|
|
$complementary_info .= "<a href={$reply_actor_url}>{$reply_actor_nickname}</a>, ";
|
|
|
|
}
|
2021-11-11 00:44:28 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
$complementary_info = rtrim(trim($complementary_info), ',');
|
|
|
|
$complementary_info .= ' replied to this note.';
|
|
|
|
$result[] = Formatting::twigRenderString($complementary_info, []);
|
|
|
|
|
2021-12-01 22:42:39 +09:00
|
|
|
return Event::next;
|
2021-11-07 10:32:06 +09:00
|
|
|
}
|
|
|
|
|
2021-12-09 07:44:04 +09:00
|
|
|
/**
|
|
|
|
* @param RouteLoader $r
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function onAddRoute(RouteLoader $r)
|
2021-11-07 10:32:06 +09:00
|
|
|
{
|
|
|
|
$r->connect('reply_add', '/object/note/{id<\d+>}/reply', [ReplyController::class, 'replyAddNote']);
|
2021-12-01 10:31:02 +09:00
|
|
|
$r->connect('replies', '/@{nickname<' . Nickname::DISPLAY_FMT . '>}/replies', [ReplyController::class, 'replies']);
|
|
|
|
|
|
|
|
return Event::next;
|
|
|
|
}
|
2021-11-07 10:32:06 +09:00
|
|
|
|
2021-12-01 10:33:12 +09:00
|
|
|
public function onCreateDefaultFeeds(int $actor_id, LocalUser $user, int &$ordering)
|
|
|
|
{
|
|
|
|
DB::persist(Feed::create([
|
|
|
|
'actor_id' => $actor_id,
|
|
|
|
'url' => Router::url($route = 'replies', ['nickname' => $user->getNickname()]),
|
|
|
|
'route' => $route,
|
|
|
|
'title' => _m('Replies'),
|
|
|
|
'ordering' => $ordering++,
|
|
|
|
]));
|
2021-11-07 10:32:06 +09:00
|
|
|
return Event::next;
|
|
|
|
}
|
2020-09-11 05:35:57 +09:00
|
|
|
}
|