2021-12-10 02:45:31 +00:00
|
|
|
<?php
|
|
|
|
|
2021-12-28 22:52:15 +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/>.
|
|
|
|
// }}}
|
|
|
|
|
2022-01-27 00:54:27 +00:00
|
|
|
/**
|
|
|
|
* @author Eliseu Amaro <mail@eliseuama.ro>
|
|
|
|
* @author Diogo Peralta Cordeiro <@diogo.site>
|
|
|
|
* @copyright 2021-2022 Free Software Foundation, Inc http://www.fsf.org
|
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
|
|
|
*/
|
|
|
|
|
2021-12-10 02:45:31 +00:00
|
|
|
namespace Plugin\RepeatNote;
|
|
|
|
|
2022-01-08 17:15:00 +00:00
|
|
|
use App\Core\Cache;
|
2022-03-27 15:19:09 +01:00
|
|
|
use App\Core\DB;
|
2021-12-10 02:45:31 +00:00
|
|
|
use App\Core\Event;
|
2022-01-04 22:04:23 +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\Note;
|
|
|
|
use App\Util\Common;
|
2021-12-28 22:52:15 +00:00
|
|
|
use App\Util\Exception\ClientException;
|
|
|
|
use App\Util\Exception\DuplicateFoundException;
|
2021-12-10 02:45:31 +00:00
|
|
|
use App\Util\Exception\ServerException;
|
2021-12-26 09:48:16 +00:00
|
|
|
use Component\Language\Entity\Language;
|
2022-03-13 18:23:19 +00:00
|
|
|
use Component\Notification\Entity\Attention;
|
2021-12-10 02:45:31 +00:00
|
|
|
use Component\Posting\Posting;
|
2021-12-16 11:14:34 +00:00
|
|
|
use DateTime;
|
2022-01-02 16:17:11 +00:00
|
|
|
use Plugin\RepeatNote\Entity\NoteRepeat as RepeatEntity;
|
|
|
|
use const SORT_REGULAR;
|
2022-01-04 22:04:23 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2021-12-10 02:45:31 +00:00
|
|
|
|
|
|
|
class RepeatNote extends NoteHandlerPlugin
|
|
|
|
{
|
2021-12-28 21:49:22 +00:00
|
|
|
/**
|
|
|
|
* **Repeats a Note**
|
|
|
|
*
|
|
|
|
* This means the current Actor creates a new Note, cloning the contents of
|
|
|
|
* the original Note provided as an argument.
|
|
|
|
*
|
|
|
|
* Bear in mind that, if it's a repeat, the **reply_to** should be to the
|
|
|
|
* original, and **conversation** ought to be the same.
|
|
|
|
*
|
|
|
|
* In the end, the Activity is created, and a new notification for the
|
|
|
|
* repeat Activity created
|
|
|
|
*
|
2022-01-27 00:54:27 +00:00
|
|
|
* @param Note $note Note being repeated
|
|
|
|
* @param int $actor_id Actor id of whom is performing the repeat Activity
|
|
|
|
*
|
2021-12-28 22:52:15 +00:00
|
|
|
* @throws ClientException
|
|
|
|
* @throws DuplicateFoundException
|
|
|
|
* @throws ServerException
|
2021-12-28 21:49:22 +00:00
|
|
|
*/
|
2021-12-28 16:07:35 +00:00
|
|
|
public static function repeatNote(Note $note, int $actor_id, string $source = 'web'): ?Activity
|
2021-12-10 02:45:31 +00:00
|
|
|
{
|
2022-01-17 20:56:14 +00:00
|
|
|
$note_repeat = RepeatEntity::getNoteActorRepeat($note, $actor_id);
|
2022-01-08 17:15:00 +00:00
|
|
|
if (!\is_null($note_repeat)) {
|
2021-12-28 16:07:35 +00:00
|
|
|
return null;
|
2021-12-10 04:03:19 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 20:56:14 +00:00
|
|
|
$original_note_id = $note->getId();
|
2021-12-26 18:40:54 +00:00
|
|
|
|
2021-12-10 04:03:19 +00:00
|
|
|
// Create a new note with the same content as the original
|
2022-03-28 20:52:12 +01:00
|
|
|
[, $repeat, ] = Posting::storeLocalNote(
|
2022-03-13 18:23:19 +00:00
|
|
|
actor: $actor = Actor::getById($actor_id),
|
2021-12-10 04:03:19 +00:00
|
|
|
content: $note->getContent(),
|
|
|
|
content_type: $note->getContentType(),
|
2022-01-04 22:04:23 +00:00
|
|
|
locale: \is_null($lang_id = $note->getLanguageId()) ? null : Language::getById($lang_id)->getLocale(),
|
2022-01-17 20:56:14 +00:00
|
|
|
// If it's a repeat, the reply_to should be to the original, conversation ought to be the same
|
2022-02-16 06:56:59 +00:00
|
|
|
reply_to: $note->getReplyTo(),
|
2022-03-07 17:08:55 +00:00
|
|
|
processed_attachments: $processed_attachments = $note->getAttachmentsWithTitle(),
|
2022-02-16 19:35:27 +00:00
|
|
|
flush_and_notify: false,
|
2022-01-17 20:56:14 +00:00
|
|
|
rendered: $note->getRendered(),
|
2021-12-10 04:03:19 +00:00
|
|
|
);
|
|
|
|
|
2022-03-07 17:08:55 +00:00
|
|
|
// Increment attachment lives, posting already handled actor_to_attachment relations for us,
|
|
|
|
// and it obviously handled the attachment_to_note
|
|
|
|
foreach ($processed_attachments as [$a, $fname]) {
|
|
|
|
// We usually don't have to increment lives manually when storing an attachment, but we aren't re-storing it
|
|
|
|
$a->livesIncrementAndGet();
|
|
|
|
}
|
|
|
|
|
2022-01-02 16:17:11 +00:00
|
|
|
DB::persist(RepeatEntity::create([
|
2022-01-04 22:04:23 +00:00
|
|
|
'note_id' => $repeat->getId(),
|
|
|
|
'actor_id' => $actor_id,
|
2022-01-02 16:17:11 +00:00
|
|
|
'repeat_of' => $original_note_id,
|
2021-12-26 22:17:26 +00:00
|
|
|
]));
|
2022-01-17 20:56:14 +00:00
|
|
|
Cache::delete(RepeatEntity::cacheKeys($note->getId(), $actor_id)['actor_repeat']);
|
|
|
|
Cache::delete(RepeatEntity::cacheKeys($note->getId())['repeats']);
|
2021-12-10 02:45:31 +00:00
|
|
|
|
|
|
|
// Log an activity
|
|
|
|
$repeat_activity = Activity::create([
|
2022-01-04 22:04:23 +00:00
|
|
|
'actor_id' => $actor_id,
|
|
|
|
'verb' => 'repeat',
|
2021-12-10 02:45:31 +00:00
|
|
|
'object_type' => 'note',
|
2022-01-04 22:04:23 +00:00
|
|
|
'object_id' => $note->getId(),
|
|
|
|
'source' => $source,
|
2021-12-10 02:45:31 +00:00
|
|
|
]);
|
|
|
|
DB::persist($repeat_activity);
|
2022-03-13 18:23:19 +00:00
|
|
|
DB::persist(Attention::create(['object_type' => Activity::schemaName(), 'object_id' => $repeat_activity->getId(), 'target_id' => $note->getActorId()]));
|
|
|
|
|
|
|
|
foreach ($actor->getSubscribers() as $subscriber) {
|
|
|
|
$target_id = $subscriber->getId();
|
|
|
|
DB::persist(Attention::create(['object_type' => Activity::schemaName(), 'object_id' => $repeat_activity->getId(), 'target_id' => $target_id]));
|
|
|
|
$effective_attentions[$target_id] = $subscriber;
|
|
|
|
}
|
2021-12-10 04:03:19 +00:00
|
|
|
|
2021-12-10 02:45:31 +00:00
|
|
|
return $repeat_activity;
|
|
|
|
}
|
|
|
|
|
2021-12-28 21:49:22 +00:00
|
|
|
/**
|
|
|
|
* **Undoes a Repeat**
|
|
|
|
*
|
|
|
|
* Removes the Repeat from NoteRepeat table, and the deletes the Note
|
|
|
|
* clone.
|
|
|
|
*
|
|
|
|
* Finally, creates a new Activity, undoing the repeat, and the respective
|
|
|
|
* Notification is handled.
|
|
|
|
*
|
2022-01-27 00:54:27 +00:00
|
|
|
* @param int $note_id Note id being unrepeated
|
|
|
|
* @param int $actor_id Actor undoing repeat Activity
|
|
|
|
*
|
|
|
|
* @throws \App\Util\Exception\NotFoundException
|
|
|
|
* @throws DuplicateFoundException
|
2021-12-28 22:52:15 +00:00
|
|
|
* @throws ServerException
|
2021-12-28 21:49:22 +00:00
|
|
|
*/
|
2021-12-10 02:45:31 +00:00
|
|
|
public static function unrepeatNote(int $note_id, int $actor_id, string $source = 'web'): ?Activity
|
|
|
|
{
|
2022-01-17 20:56:14 +00:00
|
|
|
$note_repeat = RepeatEntity::getNoteActorRepeat($note_id, $actor_id);
|
2021-12-10 02:45:31 +00:00
|
|
|
|
2022-01-08 17:15:00 +00:00
|
|
|
if (!\is_null($note_repeat)) { // If it was repeated, then we can undo it
|
2021-12-10 02:45:31 +00:00
|
|
|
// Find previous repeat activity
|
2022-01-08 17:15:00 +00:00
|
|
|
$already_repeated_activity = DB::findOneBy(Activity::class, [
|
2022-01-04 22:04:23 +00:00
|
|
|
'actor_id' => $actor_id,
|
|
|
|
'verb' => 'repeat',
|
|
|
|
'object_type' => 'note',
|
2022-01-08 17:15:00 +00:00
|
|
|
'object_id' => $note_repeat->getRepeatOf(),
|
|
|
|
], return_null: true);
|
2021-12-10 02:45:31 +00:00
|
|
|
|
|
|
|
// Remove the clone note
|
2022-01-08 17:15:00 +00:00
|
|
|
DB::findOneBy(Note::class, ['id' => $note_repeat->getNoteId()])->delete(actor: Actor::getById($actor_id));
|
2021-12-10 02:45:31 +00:00
|
|
|
|
|
|
|
// Remove from the note_repeat table
|
2022-01-08 17:15:00 +00:00
|
|
|
DB::removeBy(RepeatEntity::class, ['note_id' => $note_repeat->getNoteId()]);
|
2022-01-17 20:56:14 +00:00
|
|
|
Cache::delete(RepeatEntity::cacheKeys($note_id, $actor_id)['actor_repeat']);
|
|
|
|
Cache::delete(RepeatEntity::cacheKeys($note_repeat->getNoteId())['repeats']);
|
2021-12-10 02:45:31 +00:00
|
|
|
|
|
|
|
// Log an activity
|
|
|
|
$undo_repeat_activity = Activity::create([
|
2022-01-04 22:04:23 +00:00
|
|
|
'actor_id' => $actor_id,
|
|
|
|
'verb' => 'undo',
|
2021-12-10 02:45:31 +00:00
|
|
|
'object_type' => 'activity',
|
2022-01-04 22:04:23 +00:00
|
|
|
'object_id' => $already_repeated_activity->getId(),
|
|
|
|
'source' => $source,
|
2021-12-10 02:45:31 +00:00
|
|
|
]);
|
|
|
|
DB::persist($undo_repeat_activity);
|
2022-03-13 18:23:19 +00:00
|
|
|
DB::persist(Attention::create(['object_type' => Activity::schemaName(), 'object_id' => $undo_repeat_activity->getId(), 'target_id' => Note::getById($note_id)->getActorId()]));
|
|
|
|
|
|
|
|
foreach (Actor::getById($actor_id)->getSubscribers() as $subscriber) {
|
|
|
|
$target_id = $subscriber->getId();
|
|
|
|
DB::persist(Attention::create(['object_type' => Activity::schemaName(), 'object_id' => $undo_repeat_activity->getId(), 'target_id' => $target_id]));
|
|
|
|
$effective_attentions[$target_id] = $subscriber;
|
|
|
|
}
|
2021-12-10 04:03:19 +00:00
|
|
|
|
2021-12-10 02:45:31 +00:00
|
|
|
return $undo_repeat_activity;
|
|
|
|
} else {
|
|
|
|
// Either was undoed already
|
2022-03-13 18:23:19 +00:00
|
|
|
if (!\is_null($already_repeated_activity = DB::findOneBy(Activity::class, [
|
2022-01-04 22:04:23 +00:00
|
|
|
'actor_id' => $actor_id,
|
|
|
|
'verb' => 'repeat',
|
|
|
|
'object_type' => 'note',
|
|
|
|
'object_id' => $note_id,
|
2022-01-08 17:15:00 +00:00
|
|
|
], return_null: true))) {
|
2022-03-13 18:23:19 +00:00
|
|
|
return DB::findOneBy(Activity::class, [
|
2022-01-04 22:04:23 +00:00
|
|
|
'actor_id' => $actor_id,
|
|
|
|
'verb' => 'undo',
|
|
|
|
'object_type' => 'activity',
|
|
|
|
'object_id' => $already_repeated_activity->getId(),
|
2022-01-08 17:15:00 +00:00
|
|
|
], return_null: true); // null if not undoed
|
2021-12-10 02:45:31 +00:00
|
|
|
} else {
|
|
|
|
// or it's an attempt to undo something that wasn't repeated in the first place,
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-28 17:28:44 +00:00
|
|
|
/**
|
2021-12-28 21:49:22 +00:00
|
|
|
* Filters repeats out of Conversations, and replaces a repeat with the
|
|
|
|
* original Note on Actor feed
|
2022-01-27 00:54:27 +00:00
|
|
|
*
|
|
|
|
* @param array $notes List of Notes to be filtered
|
|
|
|
*
|
|
|
|
* @return bool Event hook, Event::next (true) is returned to allow Event to be handled by other handlers
|
2021-12-28 17:28:44 +00:00
|
|
|
*/
|
2021-12-28 21:49:22 +00:00
|
|
|
public function onFilterNoteList(?Actor $actor, array &$notes, Request $request): bool
|
2021-12-28 17:28:44 +00:00
|
|
|
{
|
|
|
|
// Replaces repeat with original note on Actor feed
|
|
|
|
// it's pretty cool
|
|
|
|
if (str_starts_with($request->get('_route'), 'actor_view_')) {
|
|
|
|
$notes = array_map(
|
2022-01-27 00:54:27 +00:00
|
|
|
static fn (Note $note) => RepeatEntity::isNoteRepeat($note)
|
2022-01-02 16:17:11 +00:00
|
|
|
? Note::getById(RepeatEntity::getByPK($note->getId())->getRepeatOf())
|
2021-12-28 21:49:22 +00:00
|
|
|
: $note,
|
2021-12-28 17:28:44 +00:00
|
|
|
$notes,
|
|
|
|
);
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter out repeats altogether
|
2022-01-27 00:54:27 +00:00
|
|
|
$notes = array_filter($notes, static fn (Note $note) => !RepeatEntity::isNoteRepeat($note));
|
2021-12-28 17:28:44 +00:00
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2021-12-10 02:45:31 +00:00
|
|
|
/**
|
|
|
|
* HTML rendering event that adds the repeat form as a note
|
|
|
|
* action, if a user is logged in
|
|
|
|
*
|
2022-01-27 00:54:27 +00:00
|
|
|
* @return bool Event hook, Event::next (true) is returned to allow Event to be handled by other handlers
|
2021-12-10 02:45:31 +00:00
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-01-17 20:56:14 +00:00
|
|
|
$note_repeat = RepeatEntity::getNoteActorRepeat($note, $user->getId());
|
2022-01-08 17:15:00 +00:00
|
|
|
|
2021-12-10 02:45:31 +00:00
|
|
|
// If note is repeated, "is_repeated" is 1, 0 otherwise.
|
2022-01-08 17:15:00 +00:00
|
|
|
$is_repeat = !\is_null($note_repeat);
|
2021-12-10 02:45:31 +00:00
|
|
|
|
|
|
|
// Generating URL for repeat action route
|
2022-01-08 17:15:00 +00:00
|
|
|
$args = ['note_id' => !$is_repeat ? $note->getId() : $note_repeat->getRepeatOf()];
|
2022-01-04 22:04:23 +00:00
|
|
|
$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);
|
|
|
|
|
|
|
|
// Concatenating get parameter to redirect the user to where he came from
|
2021-12-28 17:49:46 +00:00
|
|
|
$repeat_action_url .= '?from=' . urlencode($request->getRequestUri());
|
2021-12-10 02:45:31 +00:00
|
|
|
|
|
|
|
$extra_classes = $is_repeat ? 'note-actions-set' : 'note-actions-unset';
|
|
|
|
$repeat_action = [
|
2022-01-04 22:04:23 +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-26 18:40:54 +00:00
|
|
|
'note_id' => 'repeat-button-container-' . $note->getId(),
|
2021-12-10 02:45:31 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
$actions[] = $repeat_action;
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-12-28 21:49:22 +00:00
|
|
|
* Appends in Note information stating who and what user actions were
|
|
|
|
* performed.
|
|
|
|
*
|
|
|
|
* @param array $vars Contains the Note currently being rendered
|
|
|
|
* @param array $result Rendered String containing anchors for Actors that
|
|
|
|
* repeated the Note
|
2021-12-10 02:45:31 +00:00
|
|
|
*
|
2022-01-27 00:54:27 +00:00
|
|
|
* @return bool Event hook, Event::next (true) is returned to allow Event to be handled by other handlers
|
2021-12-10 02:45:31 +00:00
|
|
|
*/
|
|
|
|
public function onAppendCardNote(array $vars, array &$result)
|
|
|
|
{
|
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-12-16 11:14:34 +00:00
|
|
|
$check_user = !\is_null(Common::user());
|
2021-12-10 02:45:31 +00:00
|
|
|
|
2021-12-29 17:54:02 +00:00
|
|
|
// The current Note being rendered
|
2022-01-04 22:04:23 +00:00
|
|
|
$note = $vars['note'];
|
2021-12-10 02:45:31 +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-02 16:17:11 +00:00
|
|
|
$repeat_actors = [];
|
2022-01-04 22:04:23 +00:00
|
|
|
$note_repeats = RepeatEntity::getNoteRepeats($note);
|
2021-12-10 02:45:31 +00:00
|
|
|
|
2021-12-29 17:54:02 +00:00
|
|
|
// Get actors who repeated the note
|
|
|
|
foreach ($note_repeats as $repeat) {
|
|
|
|
$repeat_actors[] = Actor::getByPK($repeat->getActorId());
|
2021-12-10 02:45:31 +00:00
|
|
|
}
|
2021-12-29 17:54:02 +00:00
|
|
|
if (\count($repeat_actors) < 1) {
|
2021-12-10 02:45:31 +00:00
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter out multiple replies from the same actor
|
2021-12-29 17:54:02 +00:00
|
|
|
$repeat_actors = array_unique($repeat_actors, SORT_REGULAR);
|
2022-01-04 22:04:23 +00:00
|
|
|
$result[] = ['actors' => $repeat_actors, 'action' => 'repeated'];
|
2021-12-10 02:45:31 +00:00
|
|
|
|
2021-12-29 17:54:02 +00:00
|
|
|
return Event::next;
|
2021-12-10 02:45:31 +00:00
|
|
|
}
|
|
|
|
|
2022-01-02 16:17:11 +00:00
|
|
|
/**
|
|
|
|
* Deletes every repeat entity that is related to a deleted Note in its
|
|
|
|
* respective table
|
2022-01-27 00:54:27 +00:00
|
|
|
*
|
|
|
|
* @param Note $note Note to be deleted
|
|
|
|
* @param Actor $actor Who performed the Delete action
|
|
|
|
*
|
|
|
|
* @return bool Event hook, Event::next (true) is returned to allow Event to be handled by other handlers
|
2022-01-02 16:17:11 +00:00
|
|
|
*/
|
|
|
|
public function onNoteDeleteRelated(Note &$note, Actor $actor): bool
|
|
|
|
{
|
|
|
|
$note_repeats_list = RepeatEntity::getNoteRepeats($note);
|
2022-01-08 17:15:00 +00:00
|
|
|
foreach ($note_repeats_list as $note_repeat) {
|
|
|
|
DB::remove($note_repeat);
|
2022-01-02 16:17:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2021-12-28 21:49:22 +00:00
|
|
|
/**
|
|
|
|
* Connects the following Routes to their respective Controllers:
|
|
|
|
*
|
|
|
|
* - **repeat_add**
|
|
|
|
* page containing the Note the user wishes to Repeat and a button to
|
|
|
|
* confirm it wishes to perform the action
|
|
|
|
*
|
|
|
|
* - **repeat_remove**
|
|
|
|
* same as above, except that it undoes the aforementioned action
|
|
|
|
*
|
2022-01-27 00:54:27 +00:00
|
|
|
* @return bool Event hook, Event::next (true) is returned to allow Event to be handled by other handlers
|
2021-12-28 21:49:22 +00:00
|
|
|
*/
|
2021-12-10 02:45:31 +00:00
|
|
|
public function onAddRoute(RouteLoader $r): bool
|
|
|
|
{
|
|
|
|
// Add/remove note to/from repeats
|
2021-12-26 18:40:54 +00:00
|
|
|
$r->connect(id: 'repeat_add', uri_path: '/object/note/{note_id<\d+>}/repeat', target: [Controller\Repeat::class, 'repeatAddNote']);
|
|
|
|
$r->connect(id: 'repeat_remove', uri_path: '/object/note/{note_id<\d+>}/unrepeat', target: [Controller\Repeat::class, 'repeatRemoveNote']);
|
2021-12-10 02:45:31 +00:00
|
|
|
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2021-12-28 22:52:15 +00:00
|
|
|
// ActivityPub handling and processing for Repeats is below
|
2021-12-10 02:45:31 +00:00
|
|
|
|
2021-12-28 21:49:22 +00:00
|
|
|
/**
|
2021-12-28 22:52:15 +00:00
|
|
|
* ActivityPub Inbox handler for Announces and Undo Announce activities
|
2021-12-28 21:49:22 +00:00
|
|
|
*
|
2021-12-28 22:52:15 +00:00
|
|
|
* @param Actor $actor Actor who authored the activity
|
|
|
|
* @param \ActivityPhp\Type\AbstractObject $type_activity Activity Streams 2.0 Activity
|
|
|
|
* @param mixed $type_object Activity's Object
|
|
|
|
* @param null|\Plugin\ActivityPub\Entity\ActivitypubActivity $ap_act Resulting ActivitypubActivity
|
2021-12-28 21:49:22 +00:00
|
|
|
*
|
2022-01-27 00:54:27 +00:00
|
|
|
* @throws \App\Util\Exception\NoSuchActorException
|
|
|
|
* @throws \App\Util\Exception\NotFoundException
|
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
|
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
|
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
|
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
|
|
|
|
* @throws ClientException
|
|
|
|
* @throws DuplicateFoundException
|
|
|
|
* @throws ServerException
|
|
|
|
*
|
2021-12-28 22:52:15 +00:00
|
|
|
* @return bool Returns `Event::stop` if handled, `Event::next` otherwise
|
2021-12-28 21:49:22 +00:00
|
|
|
*/
|
2021-12-28 22:52:15 +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-10 04:03:19 +00:00
|
|
|
{
|
2022-02-12 04:34:10 +00:00
|
|
|
if (!\in_array($type_activity->get('type'), ['Announce', 'Undo']) || !$actor->isPerson()) {
|
2021-12-10 04:03:19 +00:00
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
if ($type_activity->get('type') === 'Announce') { // Repeat
|
2021-12-28 22:52:15 +00:00
|
|
|
if ($type_object instanceof \ActivityPhp\Type\AbstractObject) {
|
2022-03-28 20:52:12 +01:00
|
|
|
if ($type_object->get('type') === 'Note' || $type_object->get('type') === 'ChatMessage' || $type_object->get('type') === 'Article' || $type_object->get('type') === 'Page') {
|
2021-12-28 22:52:15 +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) {
|
2021-12-28 22:52:15 +00:00
|
|
|
$note = $type_object;
|
2021-12-28 17:49:46 +00:00
|
|
|
$note_id = $note->getId();
|
2021-12-10 04:03:19 +00:00
|
|
|
} else {
|
|
|
|
return Event::next;
|
|
|
|
}
|
2022-03-13 18:23:19 +00:00
|
|
|
$activity = self::repeatNote($note, $actor->getId(), source: 'ActivityPub');
|
2022-03-10 00:40:54 +00:00
|
|
|
} elseif ($type_activity->get('type') === 'Undo') { // Undo Repeat
|
2021-12-28 22:52:15 +00:00
|
|
|
if ($type_object instanceof \ActivityPhp\Type\AbstractObject) {
|
2021-12-10 04:03:19 +00:00
|
|
|
$ap_prev_repeat_act = \Plugin\ActivityPub\Util\Model\Activity::fromJson($type_object);
|
2021-12-28 22:52:15 +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;
|
|
|
|
}
|
2022-02-18 21:12:14 +00:00
|
|
|
$activity = self::unrepeatNote($note_id, $actor->getId(), source: 'ActivityPub');
|
2022-03-10 00:40:54 +00:00
|
|
|
} else {
|
|
|
|
return Event::next;
|
2021-12-28 16:07:35 +00:00
|
|
|
}
|
2022-03-10 00:40:54 +00:00
|
|
|
|
2021-12-28 16:07:35 +00:00
|
|
|
if (!\is_null($activity)) {
|
|
|
|
// Store ActivityPub Activity
|
2021-12-28 22:52:15 +00:00
|
|
|
$ap_act = \Plugin\ActivityPub\Entity\ActivitypubActivity::create([
|
|
|
|
'activity_id' => $activity->getId(),
|
2021-12-28 16:07:35 +00:00
|
|
|
'activity_uri' => $type_activity->get('id'),
|
2021-12-28 22:52:15 +00:00
|
|
|
'created' => new DateTime($type_activity->get('published') ?? 'now'),
|
|
|
|
'modified' => new DateTime(),
|
2021-12-28 16:07:35 +00:00
|
|
|
]);
|
|
|
|
DB::persist($ap_act);
|
2021-12-10 04:03:19 +00:00
|
|
|
}
|
|
|
|
return Event::stop;
|
|
|
|
}
|
|
|
|
|
2022-03-13 18:23:19 +00:00
|
|
|
public function onActivityPubNewNotification(Actor $sender, Activity $activity, array $targets, ?string $reason = null): bool
|
2022-02-18 21:12:14 +00:00
|
|
|
{
|
|
|
|
switch ($activity->getVerb()) {
|
|
|
|
case 'repeat':
|
2022-03-13 18:23:19 +00:00
|
|
|
Event::handle('NewNotification', [$sender, $activity, $targets, _m('{actor_id} repeated note {note_id}.', ['{actor_id}' => $sender->getId(), '{note_id}' => $activity->getObjectId()])]);
|
2022-02-18 21:12:14 +00:00
|
|
|
return Event::stop;
|
|
|
|
case 'undo':
|
|
|
|
if ($activity->getObjectType() === 'activity') {
|
|
|
|
$undone_repeat = $activity->getObject();
|
|
|
|
if ($undone_repeat->getVerb() === 'repeat') {
|
2022-03-13 18:23:19 +00:00
|
|
|
Event::handle('NewNotification', [$sender, $activity, $targets, _m('{actor_id} unrepeated note {note_id}.', ['{actor_id}' => $sender->getId(), '{note_id}' => $undone_repeat->getObjectId()])]);
|
2022-02-18 21:12:14 +00:00
|
|
|
return Event::stop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2021-12-28 22:52:15 +00:00
|
|
|
/**
|
|
|
|
* Convert an Activity Streams 2.0 Announce or Undo Announce into the appropriate Repeat and Undo Repeat entities
|
|
|
|
*
|
|
|
|
* @param Actor $actor Actor who authored the activity
|
|
|
|
* @param \ActivityPhp\Type\AbstractObject $type_activity Activity Streams 2.0 Activity
|
|
|
|
* @param \ActivityPhp\Type\AbstractObject $type_object Activity Streams 2.0 Object
|
|
|
|
* @param null|\Plugin\ActivityPub\Entity\ActivitypubActivity $ap_act Resulting ActivitypubActivity
|
|
|
|
*
|
2022-01-27 00:54:27 +00:00
|
|
|
* @throws \App\Util\Exception\NoSuchActorException
|
|
|
|
* @throws \App\Util\Exception\NotFoundException
|
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
|
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
|
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
|
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
|
|
|
|
* @throws ClientException
|
|
|
|
* @throws DuplicateFoundException
|
|
|
|
* @throws ServerException
|
|
|
|
*
|
2021-12-28 22:52:15 +00:00
|
|
|
* @return bool Returns `Event::stop` if handled, `Event::next` otherwise
|
|
|
|
*/
|
|
|
|
public function onNewActivityPubActivity(Actor $actor, \ActivityPhp\Type\AbstractObject $type_activity, \ActivityPhp\Type\AbstractObject $type_object, ?\Plugin\ActivityPub\Entity\ActivitypubActivity &$ap_act): bool
|
2021-12-10 04:03:19 +00:00
|
|
|
{
|
|
|
|
return $this->activitypub_handler($actor, $type_activity, $type_object, $ap_act);
|
|
|
|
}
|
|
|
|
|
2021-12-28 22:52:15 +00:00
|
|
|
/**
|
|
|
|
* Convert an Activity Streams 2.0 formatted activity with a known object into Entities
|
|
|
|
*
|
|
|
|
* @param Actor $actor Actor who authored the activity
|
|
|
|
* @param \ActivityPhp\Type\AbstractObject $type_activity Activity Streams 2.0 Activity
|
|
|
|
* @param mixed $type_object Object
|
|
|
|
* @param null|\Plugin\ActivityPub\Entity\ActivitypubActivity $ap_act Resulting ActivitypubActivity
|
|
|
|
*
|
2022-01-27 00:54:27 +00:00
|
|
|
* @throws \App\Util\Exception\NoSuchActorException
|
|
|
|
* @throws \App\Util\Exception\NotFoundException
|
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
|
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
|
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
|
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
|
|
|
|
* @throws ClientException
|
|
|
|
* @throws DuplicateFoundException
|
|
|
|
* @throws ServerException
|
|
|
|
*
|
2021-12-28 22:52:15 +00:00
|
|
|
* @return bool Returns `Event::stop` if handled, `Event::next` otherwise
|
|
|
|
*/
|
|
|
|
public function onNewActivityPubActivityWithObject(Actor $actor, \ActivityPhp\Type\AbstractObject $type_activity, mixed $type_object, ?\Plugin\ActivityPub\Entity\ActivitypubActivity &$ap_act): bool
|
2021-12-10 04:03:19 +00:00
|
|
|
{
|
|
|
|
return $this->activitypub_handler($actor, $type_activity, $type_object, $ap_act);
|
|
|
|
}
|
2021-12-12 06:49:14 +00:00
|
|
|
|
2021-12-28 22:52:15 +00:00
|
|
|
/**
|
|
|
|
* Translate GNU social internal verb 'repeat' to Activity Streams 2.0 'Announce'
|
|
|
|
*
|
|
|
|
* @param string $verb GNU social's internal verb
|
|
|
|
* @param null|string $gs_verb_to_activity_stream_two_verb Resulting Activity Streams 2.0 verb
|
|
|
|
*
|
|
|
|
* @return bool Returns `Event::stop` if handled, `Event::next` otherwise
|
|
|
|
*/
|
2021-12-12 06:49:14 +00:00
|
|
|
public function onGSVerbToActivityStreamsTwoActivityType(string $verb, ?string &$gs_verb_to_activity_stream_two_verb): bool
|
|
|
|
{
|
|
|
|
if ($verb === 'repeat') {
|
|
|
|
$gs_verb_to_activity_stream_two_verb = 'Announce';
|
|
|
|
return Event::stop;
|
|
|
|
}
|
|
|
|
return Event::next;
|
|
|
|
}
|
2021-12-10 02:45:31 +00:00
|
|
|
}
|