2021-12-10 02:45:31 +00:00
|
|
|
<?php
|
|
|
|
|
2021-12-16 11:14:34 +00:00
|
|
|
declare(strict_types = 1);
|
2021-12-10 02:45:31 +00:00
|
|
|
|
|
|
|
// {{{ License
|
|
|
|
// 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/>.
|
|
|
|
// }}}
|
|
|
|
|
|
|
|
namespace Plugin\RepeatNote;
|
|
|
|
|
|
|
|
use App\Core\DB\DB;
|
|
|
|
use App\Core\Event;
|
2021-12-16 11:14:34 +00:00
|
|
|
use function App\Core\I18n\_m;
|
2021-12-10 02:45:31 +00:00
|
|
|
use App\Core\Modules\NoteHandlerPlugin;
|
|
|
|
use App\Core\Router\RouteLoader;
|
|
|
|
use App\Core\Router\Router;
|
|
|
|
use App\Entity\Activity;
|
|
|
|
use App\Entity\Actor;
|
|
|
|
use App\Entity\Language;
|
|
|
|
use App\Entity\Note;
|
|
|
|
use App\Util\Common;
|
2021-12-10 04:03:19 +00:00
|
|
|
use App\Util\Exception\DuplicateFoundException;
|
|
|
|
use App\Util\Exception\NotFoundException;
|
2021-12-10 02:45:31 +00:00
|
|
|
use App\Util\Exception\ServerException;
|
|
|
|
use App\Util\Formatting;
|
|
|
|
use Component\Posting\Posting;
|
2021-12-16 11:14:34 +00:00
|
|
|
use DateTime;
|
2021-12-10 02:45:31 +00:00
|
|
|
use Plugin\RepeatNote\Entity\NoteRepeat;
|
|
|
|
use const SORT_REGULAR;
|
2021-12-16 11:14:34 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2021-12-10 02:45:31 +00:00
|
|
|
|
|
|
|
class RepeatNote extends NoteHandlerPlugin
|
|
|
|
{
|
|
|
|
public static function repeatNote(Note $note, int $actor_id, string $source = 'web'): Activity
|
|
|
|
{
|
|
|
|
$repeat_entity = DB::findBy('note_repeat', [
|
|
|
|
'actor_id' => $actor_id,
|
2021-12-16 11:14:34 +00:00
|
|
|
'note_id' => $note->getId(),
|
2021-12-10 02:45:31 +00:00
|
|
|
])[0] ?? null;
|
|
|
|
|
2021-12-16 11:14:34 +00:00
|
|
|
if (!\is_null($repeat_entity)) {
|
2021-12-10 02:45:31 +00:00
|
|
|
return DB::findBy('activity', [
|
2021-12-16 11:14:34 +00:00
|
|
|
'actor_id' => $actor_id,
|
|
|
|
'verb' => 'repeat',
|
|
|
|
'object_type' => 'note',
|
|
|
|
'object_id' => $note->getId(),
|
|
|
|
], order_by: ['created' => 'dsc'])[0];
|
2021-12-10 04:03:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new note with the same content as the original
|
|
|
|
$repeat = Posting::storeLocalNote(
|
|
|
|
actor: Actor::getById($actor_id),
|
|
|
|
content: $note->getContent(),
|
|
|
|
content_type: $note->getContentType(),
|
2021-12-16 11:14:34 +00:00
|
|
|
language: \is_null($lang_id = $note->getLanguageId()) ? null : Language::getById($lang_id)->getLocale(),
|
2021-12-10 04:03:19 +00:00
|
|
|
processed_attachments: $note->getAttachmentsWithTitle(),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Find the id of the note we just created
|
|
|
|
$repeat_id = $repeat?->getId();
|
2021-12-16 11:14:34 +00:00
|
|
|
$og_id = $note->getId();
|
2021-12-10 04:03:19 +00:00
|
|
|
|
|
|
|
// Add it to note_repeat table
|
2021-12-16 11:14:34 +00:00
|
|
|
if (!\is_null($repeat_id)) {
|
2021-12-10 04:03:19 +00:00
|
|
|
DB::persist(NoteRepeat::create([
|
2021-12-16 11:14:34 +00:00
|
|
|
'note_id' => $repeat_id,
|
|
|
|
'actor_id' => $actor_id,
|
2021-12-10 04:03:19 +00:00
|
|
|
'repeat_of' => $og_id,
|
|
|
|
]));
|
2021-12-10 02:45:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Log an activity
|
|
|
|
$repeat_activity = Activity::create([
|
2021-12-16 11:14:34 +00:00
|
|
|
'actor_id' => $actor_id,
|
|
|
|
'verb' => 'repeat',
|
2021-12-10 02:45:31 +00:00
|
|
|
'object_type' => 'note',
|
2021-12-16 11:14:34 +00:00
|
|
|
'object_id' => $note->getId(),
|
|
|
|
'source' => $source,
|
2021-12-10 02:45:31 +00:00
|
|
|
]);
|
|
|
|
DB::persist($repeat_activity);
|
2021-12-10 04:03:19 +00:00
|
|
|
|
|
|
|
Event::handle('NewNotification', [$actor = Actor::getById($actor_id), $repeat_activity, [], "{$actor->getNickname()} repeated note {$note->getUrl()}"]);
|
|
|
|
|
2021-12-10 02:45:31 +00:00
|
|
|
return $repeat_activity;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function unrepeatNote(int $note_id, int $actor_id, string $source = 'web'): ?Activity
|
|
|
|
{
|
|
|
|
$already_repeated = DB::findBy('note_repeat', ['actor_id' => $actor_id, 'repeat_of' => $note_id])[0] ?? null;
|
|
|
|
|
2021-12-16 11:14:34 +00:00
|
|
|
if (!\is_null($already_repeated)) { // If it was repeated, then we can undo it
|
2021-12-10 02:45:31 +00:00
|
|
|
// Find previous repeat activity
|
|
|
|
$already_repeated_activity = DB::findBy('activity', [
|
2021-12-16 11:14:34 +00:00
|
|
|
'actor_id' => $actor_id,
|
|
|
|
'verb' => 'repeat',
|
2021-12-10 02:45:31 +00:00
|
|
|
'object_type' => 'note',
|
2021-12-16 11:14:34 +00:00
|
|
|
'object_id' => $already_repeated->getRepeatOf(),
|
2021-12-10 02:45:31 +00:00
|
|
|
])[0] ?? null;
|
|
|
|
|
|
|
|
// Remove the clone note
|
|
|
|
DB::findBy('note', ['id' => $already_repeated->getNoteId()])[0]->delete();
|
|
|
|
|
|
|
|
// Remove from the note_repeat table
|
|
|
|
DB::remove(DB::findBy('note_repeat', ['note_id' => $already_repeated->getNoteId()])[0]);
|
|
|
|
|
|
|
|
// Log an activity
|
|
|
|
$undo_repeat_activity = Activity::create([
|
2021-12-16 11:14:34 +00:00
|
|
|
'actor_id' => $actor_id,
|
|
|
|
'verb' => 'undo',
|
2021-12-10 02:45:31 +00:00
|
|
|
'object_type' => 'activity',
|
2021-12-16 11:14:34 +00:00
|
|
|
'object_id' => $already_repeated_activity->getId(),
|
|
|
|
'source' => $source,
|
2021-12-10 02:45:31 +00:00
|
|
|
]);
|
|
|
|
DB::persist($undo_repeat_activity);
|
2021-12-10 04:03:19 +00:00
|
|
|
|
|
|
|
Event::handle('NewNotification', [$actor = Actor::getById($actor_id), $undo_repeat_activity, [], "{$actor->getNickname()} unrepeated note {$note_id}"]);
|
|
|
|
|
2021-12-10 02:45:31 +00:00
|
|
|
return $undo_repeat_activity;
|
|
|
|
} else {
|
|
|
|
// Either was undoed already
|
2021-12-16 11:14:34 +00:00
|
|
|
if (!\is_null($already_repeated_activity = DB::findBy('activity', [
|
2021-12-10 02:45:31 +00:00
|
|
|
'actor_id' => $actor_id,
|
|
|
|
'verb' => 'repeat',
|
|
|
|
'object_type' => 'note',
|
|
|
|
'object_id' => $note_id,
|
|
|
|
])[0] ?? null)) {
|
|
|
|
return DB::findBy('activity', [
|
2021-12-16 11:14:34 +00:00
|
|
|
'actor_id' => $actor_id,
|
|
|
|
'verb' => 'undo',
|
2021-12-10 02:45:31 +00:00
|
|
|
'object_type' => 'activity',
|
2021-12-16 11:14:34 +00:00
|
|
|
'object_id' => $already_repeated_activity->getId(),
|
2021-12-10 02:45:31 +00:00
|
|
|
])[0] ?? null; // null if not undoed
|
|
|
|
} else {
|
|
|
|
// or it's an attempt to undo something that wasn't repeated in the first place,
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* HTML rendering event that adds the repeat form as a note
|
|
|
|
* action, if a user is logged in
|
|
|
|
*
|
|
|
|
* @return bool Event hook
|
|
|
|
*/
|
|
|
|
public function onAddNoteActions(Request $request, Note $note, array &$actions): bool
|
|
|
|
{
|
|
|
|
// Only logged users can repeat notes
|
2021-12-16 11:14:34 +00:00
|
|
|
if (\is_null($user = Common::user())) {
|
2021-12-10 02:45:31 +00:00
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If note is repeated, "is_repeated" is 1, 0 otherwise.
|
|
|
|
$is_repeat = ($note_repeat = DB::findBy('note_repeat', [
|
|
|
|
'actor_id' => $user->getId(),
|
2021-12-16 11:14:34 +00:00
|
|
|
'note_id' => $note->getId(),
|
2021-12-10 02:45:31 +00:00
|
|
|
])) !== [] ? 1 : 0;
|
|
|
|
|
|
|
|
// If note was already repeated, do not add the action
|
|
|
|
try {
|
|
|
|
if (DB::findOneBy('note_repeat', [
|
|
|
|
'repeat_of' => $note->getId(),
|
2021-12-16 11:14:34 +00:00
|
|
|
'actor_id' => $user->getId(),
|
2021-12-10 02:45:31 +00:00
|
|
|
])) {
|
|
|
|
return Event::next;
|
|
|
|
}
|
2021-12-10 04:03:19 +00:00
|
|
|
} catch (DuplicateFoundException|NotFoundException) {
|
2021-12-10 02:45:31 +00:00
|
|
|
// It's okay
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generating URL for repeat action route
|
2021-12-16 11:14:34 +00:00
|
|
|
$args = ['id' => $is_repeat === 0 ? $note->getId() : $note_repeat[0]->getRepeatOf()];
|
|
|
|
$type = Router::ABSOLUTE_PATH;
|
2021-12-10 02:45:31 +00:00
|
|
|
$repeat_action_url = $is_repeat
|
|
|
|
? Router::url('repeat_remove', $args, $type)
|
|
|
|
: Router::url('repeat_add', $args, $type);
|
|
|
|
|
|
|
|
// TODO clean this up
|
|
|
|
// SECURITY: open redirect?
|
|
|
|
$query_string = $request->getQueryString();
|
|
|
|
// Concatenating get parameter to redirect the user to where he came from
|
2021-12-16 11:14:34 +00:00
|
|
|
$repeat_action_url .= !\is_null($query_string) ? '?from=' . mb_substr($query_string, 2) : '';
|
2021-12-10 02:45:31 +00:00
|
|
|
|
|
|
|
$extra_classes = $is_repeat ? 'note-actions-set' : 'note-actions-unset';
|
|
|
|
$repeat_action = [
|
2021-12-16 11:14:34 +00:00
|
|
|
'url' => $repeat_action_url,
|
|
|
|
'title' => $is_repeat ? 'Remove this repeat' : 'Repeat this note!',
|
2021-12-10 02:45:31 +00:00
|
|
|
'classes' => "button-container repeat-button-container {$extra_classes}",
|
2021-12-16 11:14:34 +00:00
|
|
|
'id' => 'repeat-button-container-' . $note->getId(),
|
2021-12-10 02:45:31 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
$actions[] = $repeat_action;
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Append on note information about user actions.
|
|
|
|
*
|
|
|
|
* @return array|bool
|
|
|
|
*/
|
|
|
|
public function onAppendCardNote(array $vars, array &$result)
|
|
|
|
{
|
|
|
|
// 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-12-16 11:14:34 +00:00
|
|
|
$check_user = !\is_null(Common::user());
|
2021-12-10 02:45:31 +00:00
|
|
|
|
|
|
|
$note = $vars['note'];
|
|
|
|
|
|
|
|
$complementary_info = '';
|
2021-12-16 11:14:34 +00:00
|
|
|
$repeat_actor = [];
|
|
|
|
$note_repeats = NoteRepeat::getNoteRepeats($note);
|
2021-12-10 02:45:31 +00:00
|
|
|
|
|
|
|
// Get actors who replied
|
|
|
|
foreach ($note_repeats as $reply) {
|
2021-12-16 11:14:34 +00:00
|
|
|
$repeat_actor[] = Actor::getByPK($reply->getActorId());
|
2021-12-10 02:45:31 +00:00
|
|
|
}
|
2021-12-16 11:14:34 +00:00
|
|
|
if (\count($repeat_actor) < 1) {
|
2021-12-10 02:45:31 +00:00
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter out multiple replies from the same actor
|
|
|
|
$repeat_actor = array_unique($repeat_actor, SORT_REGULAR);
|
|
|
|
|
|
|
|
// Add to complementary info
|
|
|
|
foreach ($repeat_actor as $actor) {
|
2021-12-16 11:14:34 +00:00
|
|
|
$repeat_actor_url = $actor->getUrl();
|
2021-12-10 02:45:31 +00:00
|
|
|
$repeat_actor_nickname = $actor->getNickname();
|
|
|
|
|
|
|
|
if ($check_user && $actor->getId() === (Common::actor())->getId()) {
|
|
|
|
// If the repeat is yours
|
|
|
|
try {
|
|
|
|
$you_translation = _m('You');
|
|
|
|
} catch (ServerException $e) {
|
|
|
|
$you_translation = 'You';
|
|
|
|
}
|
|
|
|
|
2021-12-16 11:14:34 +00:00
|
|
|
$prepend = "<a href={$repeat_actor_url}>{$you_translation}</a>, " . ($prepend = &$complementary_info);
|
2021-12-10 02:45:31 +00:00
|
|
|
$complementary_info = $prepend;
|
|
|
|
} else {
|
|
|
|
// If the repeat is from someone else
|
|
|
|
$complementary_info .= "<a href={$repeat_actor_url}>{$repeat_actor_nickname}</a>, ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$complementary_info = rtrim(trim($complementary_info), ',');
|
|
|
|
$complementary_info .= ' repeated this note.';
|
|
|
|
$result[] = Formatting::twigRenderString($complementary_info, []);
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onAddRoute(RouteLoader $r): bool
|
|
|
|
{
|
|
|
|
// Add/remove note to/from repeats
|
|
|
|
$r->connect(id: 'repeat_add', uri_path: '/object/note/{id<\d+>}/repeat', target: [Controller\Repeat::class, 'repeatAddNote']);
|
|
|
|
$r->connect(id: 'repeat_remove', uri_path: '/object/note/{id<\d+>}/unrepeat', target: [Controller\Repeat::class, 'repeatRemoveNote']);
|
|
|
|
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ActivityPub
|
|
|
|
|
2021-12-10 04:03:19 +00:00
|
|
|
private function activitypub_handler(Actor $actor, \ActivityPhp\Type\AbstractObject $type_activity, mixed $type_object, ?\Plugin\ActivityPub\Entity\ActivitypubActivity &$ap_act): bool
|
|
|
|
{
|
2021-12-16 11:14:34 +00:00
|
|
|
if (!\in_array($type_activity->get('type'), ['Announce', 'Undo'])) {
|
2021-12-10 04:03:19 +00:00
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
if ($type_activity->get('type') === 'Announce') { // Repeat
|
|
|
|
if ($type_object instanceof \ActivityPhp\Type\AbstractObject) {
|
|
|
|
if ($type_object->get('type') === 'Note') {
|
2021-12-16 11:14:34 +00:00
|
|
|
$note = \Plugin\ActivityPub\Util\Model\Note::fromJson($type_object);
|
2021-12-10 04:03:19 +00:00
|
|
|
$note_id = $note->getId();
|
|
|
|
} else {
|
|
|
|
return Event::next;
|
|
|
|
}
|
2021-12-16 11:14:34 +00:00
|
|
|
} elseif ($type_object instanceof Note) {
|
|
|
|
$note = $type_object;
|
|
|
|
$note_id = ${$note}->getId();
|
2021-12-10 04:03:19 +00:00
|
|
|
} else {
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
} else { // Undo Repeat
|
|
|
|
if ($type_object instanceof \ActivityPhp\Type\AbstractObject) {
|
|
|
|
$ap_prev_repeat_act = \Plugin\ActivityPub\Util\Model\Activity::fromJson($type_object);
|
2021-12-16 11:14:34 +00:00
|
|
|
$prev_repeat_act = $ap_prev_repeat_act->getActivity();
|
2021-12-10 04:03:19 +00:00
|
|
|
if ($prev_repeat_act->getVerb() === 'repeat' && $prev_repeat_act->getObjectType() === 'note') {
|
|
|
|
$note_id = $prev_repeat_act->getObjectId();
|
|
|
|
} else {
|
|
|
|
return Event::next;
|
|
|
|
}
|
2021-12-16 11:14:34 +00:00
|
|
|
} elseif ($type_object instanceof Activity) {
|
2021-12-10 04:03:19 +00:00
|
|
|
if ($type_object->getVerb() === 'repeat' && $type_object->getObjectType() === 'note') {
|
|
|
|
$note_id = $type_object->getObjectId();
|
|
|
|
} else {
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($type_activity->get('type') === 'Announce') {
|
|
|
|
$act = self::repeatNote($note ?? Note::getById($note_id), $actor->getId(), source: 'ActivityPub');
|
|
|
|
} else {
|
|
|
|
$act = self::unrepeatNote($note_id, $actor->getId(), source: 'ActivityPub');
|
|
|
|
}
|
|
|
|
// Store ActivityPub Activity
|
|
|
|
$ap_act = \Plugin\ActivityPub\Entity\ActivitypubActivity::create([
|
2021-12-16 11:14:34 +00:00
|
|
|
'activity_id' => $act->getId(),
|
2021-12-10 04:03:19 +00:00
|
|
|
'activity_uri' => $type_activity->get('id'),
|
2021-12-16 11:14:34 +00:00
|
|
|
'created' => new DateTime($type_activity->get('published') ?? 'now'),
|
|
|
|
'modified' => new DateTime(),
|
2021-12-10 04:03:19 +00:00
|
|
|
]);
|
|
|
|
DB::persist($ap_act);
|
|
|
|
return Event::stop;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onNewActivityPubActivity(Actor $actor, \ActivityPhp\Type\AbstractObject $type_activity, \ActivityPhp\Type\AbstractObject $type_object, ?\Plugin\ActivityPub\Entity\ActivitypubActivity &$ap_act): bool
|
|
|
|
{
|
|
|
|
return $this->activitypub_handler($actor, $type_activity, $type_object, $ap_act);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onNewActivityPubActivityWithObject(Actor $actor, \ActivityPhp\Type\AbstractObject $type_activity, mixed $type_object, ?\Plugin\ActivityPub\Entity\ActivitypubActivity &$ap_act): bool
|
|
|
|
{
|
|
|
|
return $this->activitypub_handler($actor, $type_activity, $type_object, $ap_act);
|
|
|
|
}
|
2021-12-10 02:45:31 +00:00
|
|
|
}
|