2021-04-02 17:20:02 +00:00
|
|
|
<?php
|
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2021-04-02 17:20:02 +00:00
|
|
|
/**
|
|
|
|
* @copyright 2021 Podlibre
|
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace ActivityPub\Models;
|
|
|
|
|
|
|
|
use ActivityPub\Activities\AnnounceActivity;
|
|
|
|
use ActivityPub\Activities\CreateActivity;
|
|
|
|
use ActivityPub\Activities\DeleteActivity;
|
|
|
|
use ActivityPub\Activities\UndoActivity;
|
2021-05-19 16:35:13 +00:00
|
|
|
use ActivityPub\Entities\Actor;
|
|
|
|
use ActivityPub\Entities\Note;
|
2021-04-02 17:20:02 +00:00
|
|
|
use ActivityPub\Objects\TombstoneObject;
|
2021-05-12 14:00:25 +00:00
|
|
|
use CodeIgniter\Database\BaseResult;
|
2021-05-19 16:35:13 +00:00
|
|
|
use CodeIgniter\Database\Query;
|
2021-04-02 17:20:02 +00:00
|
|
|
use CodeIgniter\Events\Events;
|
|
|
|
use CodeIgniter\HTTP\URI;
|
|
|
|
use CodeIgniter\I18n\Time;
|
2021-05-19 16:35:13 +00:00
|
|
|
use Exception;
|
2021-04-22 17:20:28 +00:00
|
|
|
use Michalsn\Uuid\UuidModel;
|
2021-04-02 17:20:02 +00:00
|
|
|
|
|
|
|
class NoteModel extends UuidModel
|
|
|
|
{
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2021-04-02 17:20:02 +00:00
|
|
|
protected $table = 'activitypub_notes';
|
2021-05-19 16:35:13 +00:00
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2021-04-02 17:20:02 +00:00
|
|
|
protected $primaryKey = 'id';
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2021-04-02 17:20:02 +00:00
|
|
|
protected $uuidFields = ['id', 'in_reply_to_id', 'reblog_of_id'];
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2021-04-02 17:20:02 +00:00
|
|
|
protected $allowedFields = [
|
|
|
|
'id',
|
|
|
|
'uri',
|
|
|
|
'actor_id',
|
|
|
|
'in_reply_to_id',
|
|
|
|
'reblog_of_id',
|
|
|
|
'message',
|
|
|
|
'message_html',
|
|
|
|
'favourites_count',
|
|
|
|
'reblogs_count',
|
|
|
|
'replies_count',
|
|
|
|
'published_at',
|
|
|
|
];
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $returnType = Note::class;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
2021-04-02 17:20:02 +00:00
|
|
|
protected $useSoftDeletes = false;
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
2021-04-02 17:20:02 +00:00
|
|
|
protected $useTimestamps = true;
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
protected $updatedField;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array<string, string>
|
|
|
|
*/
|
2021-04-02 17:20:02 +00:00
|
|
|
protected $validationRules = [
|
|
|
|
'actor_id' => 'required',
|
|
|
|
'message_html' => 'required_without[reblog_of_id]|max_length[500]',
|
|
|
|
];
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2021-04-02 17:20:02 +00:00
|
|
|
protected $beforeInsert = ['setNoteId'];
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
public function getNoteById(string $noteId): ?Note
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2021-05-19 16:35:13 +00:00
|
|
|
$cacheName = config('ActivityPub')
|
|
|
|
->cachePrefix . "note#{$noteId}";
|
|
|
|
if (! ($found = cache($cacheName))) {
|
2021-04-22 17:20:28 +00:00
|
|
|
$found = $this->find($noteId);
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
cache()
|
|
|
|
->save($cacheName, $found, DECADE);
|
2021-04-22 17:20:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $found;
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
public function getNoteByUri(string $noteUri): ?Note
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2021-04-22 17:20:28 +00:00
|
|
|
$hashedNoteUri = md5($noteUri);
|
|
|
|
$cacheName =
|
2021-05-19 16:35:13 +00:00
|
|
|
config('ActivityPub')
|
|
|
|
->cachePrefix . "note-{$hashedNoteUri}";
|
|
|
|
if (! ($found = cache($cacheName))) {
|
|
|
|
$found = $this->where('uri', $noteUri)
|
|
|
|
->first();
|
2021-04-22 17:20:28 +00:00
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
cache()
|
|
|
|
->save($cacheName, $found, DECADE);
|
2021-04-22 17:20:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $found;
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves all published notes for a given actor ordered by publication date
|
|
|
|
*
|
2021-05-06 14:00:48 +00:00
|
|
|
* @return Note[]
|
2021-04-02 17:20:02 +00:00
|
|
|
*/
|
2021-05-14 17:59:35 +00:00
|
|
|
public function getActorPublishedNotes(int $actorId): array
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2021-04-22 17:20:28 +00:00
|
|
|
$cacheName =
|
2021-05-19 16:35:13 +00:00
|
|
|
config('ActivityPub')
|
|
|
|
->cachePrefix .
|
2021-04-22 17:20:28 +00:00
|
|
|
"actor#{$actorId}_published_notes";
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! ($found = cache($cacheName))) {
|
2021-04-22 17:20:28 +00:00
|
|
|
$found = $this->where([
|
|
|
|
'actor_id' => $actorId,
|
|
|
|
'in_reply_to_id' => null,
|
|
|
|
])
|
|
|
|
->where('`published_at` <= NOW()', null, false)
|
|
|
|
->orderBy('published_at', 'DESC')
|
|
|
|
->findAll();
|
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
$secondsToNextUnpublishedNote = $this->getSecondsToNextUnpublishedNote($actorId);
|
2021-05-25 18:00:09 +00:00
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
cache()
|
2021-06-08 09:52:11 +00:00
|
|
|
->save($cacheName, $found, $secondsToNextUnpublishedNote ? $secondsToNextUnpublishedNote : DECADE);
|
2021-04-22 17:20:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $found;
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
2021-05-25 18:00:09 +00:00
|
|
|
/**
|
|
|
|
* Returns the timestamp difference in seconds between the next note to publish and the current timestamp. Returns
|
|
|
|
* false if there's no note to publish
|
|
|
|
*/
|
|
|
|
public function getSecondsToNextUnpublishedNote(int $actorId): int | false
|
|
|
|
{
|
2021-06-09 12:40:22 +00:00
|
|
|
$result = $this->select('TIMESTAMPDIFF(SECOND, NOW(), `published_at`) as timestamp_diff')
|
2021-05-25 18:00:09 +00:00
|
|
|
->where([
|
|
|
|
'actor_id' => $actorId,
|
|
|
|
])
|
|
|
|
->where('`published_at` > NOW()', null, false)
|
|
|
|
->orderBy('published_at', 'asc')
|
|
|
|
->get()
|
|
|
|
->getResultArray();
|
|
|
|
|
|
|
|
return count($result) !== 0
|
|
|
|
? (int) $result[0]['timestamp_diff']
|
|
|
|
: false;
|
|
|
|
}
|
|
|
|
|
2021-04-02 17:20:02 +00:00
|
|
|
/**
|
2021-05-19 16:35:13 +00:00
|
|
|
* Retrieves all published replies for a given note. By default, it does not get replies from blocked actors.
|
2021-04-02 17:20:02 +00:00
|
|
|
*
|
2021-05-06 14:00:48 +00:00
|
|
|
* @return Note[]
|
2021-04-02 17:20:02 +00:00
|
|
|
*/
|
2021-05-19 16:35:13 +00:00
|
|
|
public function getNoteReplies(string $noteId, bool $withBlocked = false): array
|
|
|
|
{
|
2021-04-22 17:20:28 +00:00
|
|
|
$cacheName =
|
2021-05-19 16:35:13 +00:00
|
|
|
config('ActivityPub')
|
|
|
|
->cachePrefix .
|
2021-04-22 17:20:28 +00:00
|
|
|
"note#{$noteId}_replies" .
|
|
|
|
($withBlocked ? '_withBlocked' : '');
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! ($found = cache($cacheName))) {
|
|
|
|
if (! $withBlocked) {
|
2021-04-22 17:20:28 +00:00
|
|
|
$this->select('activitypub_notes.*')
|
2021-06-09 12:40:22 +00:00
|
|
|
->join('activitypub_actors', 'activitypub_actors.id = activitypub_notes.actor_id', 'inner')
|
2021-04-22 17:20:28 +00:00
|
|
|
->where('activitypub_actors.is_blocked', 0);
|
|
|
|
}
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-06-09 12:40:22 +00:00
|
|
|
$this->where('in_reply_to_id', $this->uuid->fromString($noteId) ->getBytes())
|
2021-04-22 17:20:28 +00:00
|
|
|
->where('`published_at` <= NOW()', null, false)
|
|
|
|
->orderBy('published_at', 'ASC');
|
|
|
|
$found = $this->findAll();
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
cache()
|
|
|
|
->save($cacheName, $found, DECADE);
|
2021-04-22 17:20:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $found;
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves all published reblogs for a given note
|
2021-05-19 16:35:13 +00:00
|
|
|
*
|
2021-05-14 17:59:35 +00:00
|
|
|
* @return Note[]
|
2021-04-02 17:20:02 +00:00
|
|
|
*/
|
2021-05-14 17:59:35 +00:00
|
|
|
public function getNoteReblogs(string $noteId): array
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2021-04-22 17:20:28 +00:00
|
|
|
$cacheName =
|
2021-05-19 16:35:13 +00:00
|
|
|
config('ActivityPub')
|
|
|
|
->cachePrefix . "note#{$noteId}_reblogs";
|
2021-04-22 17:20:28 +00:00
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! ($found = cache($cacheName))) {
|
2021-06-09 12:40:22 +00:00
|
|
|
$found = $this->where('reblog_of_id', $this->uuid->fromString($noteId) ->getBytes())
|
2021-04-22 17:20:28 +00:00
|
|
|
->where('`published_at` <= NOW()', null, false)
|
|
|
|
->orderBy('published_at', 'ASC')
|
|
|
|
->findAll();
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
cache()
|
|
|
|
->save($cacheName, $found, DECADE);
|
2021-04-22 17:20:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $found;
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
public function addPreviewCard(string $noteId, int $previewCardId): Query | bool
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2021-05-19 16:35:13 +00:00
|
|
|
return $this->db->table('activitypub_notes_preview_cards')
|
|
|
|
->insert([
|
|
|
|
'note_id' => $this->uuid->fromString($noteId)
|
|
|
|
->getBytes(),
|
|
|
|
'preview_card_id' => $previewCardId,
|
|
|
|
]);
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds note in database along preview card if relevant
|
|
|
|
*
|
|
|
|
* @return string|false returns the new note id if success or false otherwise
|
|
|
|
*/
|
|
|
|
public function addNote(
|
2021-05-06 14:00:48 +00:00
|
|
|
Note $note,
|
|
|
|
bool $createPreviewCard = true,
|
|
|
|
bool $registerActivity = true
|
2021-05-19 16:35:13 +00:00
|
|
|
): string | false {
|
2021-04-02 17:20:02 +00:00
|
|
|
helper('activitypub');
|
|
|
|
|
|
|
|
$this->db->transStart();
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! ($newNoteId = $this->insert($note, true))) {
|
2021-04-02 17:20:02 +00:00
|
|
|
$this->db->transRollback();
|
|
|
|
|
|
|
|
// Couldn't insert note
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($createPreviewCard) {
|
|
|
|
// parse message
|
|
|
|
$messageUrls = extract_urls_from_message($note->message);
|
|
|
|
|
|
|
|
if (
|
2021-05-18 17:16:36 +00:00
|
|
|
$messageUrls !== [] &&
|
2021-06-09 12:40:22 +00:00
|
|
|
($previewCard = get_or_create_preview_card_from_url(new URI($messageUrls[0]))) &&
|
2021-05-19 16:35:13 +00:00
|
|
|
! $this->addPreviewCard($newNoteId, $previewCard->id)
|
2021-04-02 17:20:02 +00:00
|
|
|
) {
|
2021-05-06 14:00:48 +00:00
|
|
|
$this->db->transRollback();
|
|
|
|
// problem when linking note to preview card
|
|
|
|
return false;
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
model('ActorModel')
|
|
|
|
->where('id', $note->actor_id)
|
|
|
|
->increment('notes_count');
|
|
|
|
|
|
|
|
if ($registerActivity) {
|
|
|
|
// set note id and uri to construct NoteObject
|
2021-04-22 17:20:28 +00:00
|
|
|
$note->id = $newNoteId;
|
2021-06-08 09:52:11 +00:00
|
|
|
$note->uri = base_url(route_to('note', $note->actor->username, $newNoteId));
|
2021-04-02 17:20:02 +00:00
|
|
|
|
|
|
|
$createActivity = new CreateActivity();
|
2021-05-19 16:35:13 +00:00
|
|
|
$noteObjectClass = config('ActivityPub')
|
|
|
|
->noteObject;
|
2021-04-02 17:20:02 +00:00
|
|
|
$createActivity
|
|
|
|
->set('actor', $note->actor->uri)
|
|
|
|
->set('object', new $noteObjectClass($note));
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
$activityId = model('ActivityModel')
|
|
|
|
->newActivity(
|
|
|
|
'Create',
|
|
|
|
$note->actor_id,
|
|
|
|
null,
|
|
|
|
$newNoteId,
|
|
|
|
$createActivity->toJSON(),
|
|
|
|
$note->published_at,
|
|
|
|
'queued',
|
|
|
|
);
|
|
|
|
|
2021-06-09 12:40:22 +00:00
|
|
|
$createActivity->set('id', base_url(route_to('activity', $note->actor->username, $activityId)));
|
2021-05-19 16:35:13 +00:00
|
|
|
|
|
|
|
model('ActivityModel')
|
|
|
|
->update($activityId, [
|
|
|
|
'payload' => $createActivity->toJSON(),
|
|
|
|
]);
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
Events::trigger('on_note_add', $note);
|
|
|
|
|
|
|
|
$this->clearCache($note);
|
|
|
|
|
2021-04-02 17:20:02 +00:00
|
|
|
$this->db->transComplete();
|
|
|
|
|
|
|
|
return $newNoteId;
|
|
|
|
}
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
public function editNote(Note $updatedNote): bool
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
|
|
|
$this->db->transStart();
|
|
|
|
|
|
|
|
// update note create activity schedule in database
|
|
|
|
$scheduledActivity = model('ActivityModel')
|
|
|
|
->where([
|
|
|
|
'type' => 'Create',
|
2021-04-22 17:20:28 +00:00
|
|
|
'note_id' => $this->uuid
|
2021-04-02 17:20:02 +00:00
|
|
|
->fromString($updatedNote->id)
|
|
|
|
->getBytes(),
|
|
|
|
])
|
|
|
|
->first();
|
|
|
|
|
|
|
|
// update published date in payload
|
|
|
|
$newPayload = $scheduledActivity->payload;
|
2021-06-08 09:52:11 +00:00
|
|
|
$newPayload->object->published = $updatedNote->published_at->format(DATE_W3C);
|
2021-05-19 16:35:13 +00:00
|
|
|
model('ActivityModel')
|
|
|
|
->update($scheduledActivity->id, [
|
|
|
|
'payload' => json_encode($newPayload, JSON_THROW_ON_ERROR),
|
|
|
|
'scheduled_at' => $updatedNote->published_at,
|
|
|
|
]);
|
2021-04-02 17:20:02 +00:00
|
|
|
|
|
|
|
// update note
|
|
|
|
$updateResult = $this->update($updatedNote->id, $updatedNote);
|
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
Events::trigger('on_note_edit', $updatedNote);
|
|
|
|
|
|
|
|
$this->clearCache($updatedNote);
|
2021-04-22 17:20:28 +00:00
|
|
|
|
2021-04-02 17:20:02 +00:00
|
|
|
$this->db->transComplete();
|
|
|
|
|
|
|
|
return $updateResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a note from the database and decrements meta data
|
|
|
|
*/
|
2021-05-19 16:35:13 +00:00
|
|
|
public function removeNote(Note $note, bool $registerActivity = true): BaseResult | bool
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
|
|
|
$this->db->transStart();
|
|
|
|
|
|
|
|
model('ActorModel')
|
|
|
|
->where('id', $note->actor_id)
|
|
|
|
->decrement('notes_count');
|
|
|
|
|
2021-06-09 12:40:22 +00:00
|
|
|
if ($note->in_reply_to_id !== null) {
|
2021-04-02 17:20:02 +00:00
|
|
|
// Note to remove is a reply
|
|
|
|
model('NoteModel')
|
2021-06-09 12:40:22 +00:00
|
|
|
->where('id', $this->uuid->fromString($note->in_reply_to_id) ->getBytes())
|
2021-04-02 17:20:02 +00:00
|
|
|
->decrement('replies_count');
|
2021-04-22 17:20:28 +00:00
|
|
|
|
|
|
|
Events::trigger('on_reply_remove', $note);
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
2021-04-22 17:20:28 +00:00
|
|
|
// remove all note reblogs
|
2021-04-02 17:20:02 +00:00
|
|
|
foreach ($note->reblogs as $reblog) {
|
2021-06-09 12:40:22 +00:00
|
|
|
// FIXME: issue when actor is not local, can't get actor information
|
2021-04-02 17:20:02 +00:00
|
|
|
$this->removeNote($reblog);
|
|
|
|
}
|
|
|
|
|
2021-04-22 17:20:28 +00:00
|
|
|
// remove all note replies
|
2021-04-02 17:20:02 +00:00
|
|
|
foreach ($note->replies as $reply) {
|
|
|
|
$this->removeNote($reply);
|
|
|
|
}
|
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
// check that preview card is no longer used elsewhere before deleting it
|
2021-05-06 14:00:48 +00:00
|
|
|
if (
|
|
|
|
$note->preview_card &&
|
|
|
|
$this->db
|
|
|
|
->table('activitypub_notes_preview_cards')
|
|
|
|
->where('preview_card_id', $note->preview_card->id)
|
|
|
|
->countAll() <= 1
|
|
|
|
) {
|
2021-06-08 09:52:11 +00:00
|
|
|
model('PreviewCardModel')->deletePreviewCard($note->preview_card->id, $note->preview_card->url);
|
2021-04-22 17:20:28 +00:00
|
|
|
}
|
|
|
|
|
2021-04-02 17:20:02 +00:00
|
|
|
if ($registerActivity) {
|
|
|
|
$deleteActivity = new DeleteActivity();
|
|
|
|
$tombstoneObject = new TombstoneObject();
|
|
|
|
$tombstoneObject->set('id', $note->uri);
|
|
|
|
$deleteActivity
|
|
|
|
->set('actor', $note->actor->uri)
|
|
|
|
->set('object', $tombstoneObject);
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
$activityId = model('ActivityModel')
|
|
|
|
->newActivity(
|
|
|
|
'Delete',
|
|
|
|
$note->actor_id,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
$deleteActivity->toJSON(),
|
|
|
|
Time::now(),
|
|
|
|
'queued',
|
|
|
|
);
|
|
|
|
|
2021-06-09 12:40:22 +00:00
|
|
|
$deleteActivity->set('id', base_url(route_to('activity', $note->actor->username, $activityId)));
|
2021-05-19 16:35:13 +00:00
|
|
|
|
|
|
|
model('ActivityModel')
|
|
|
|
->update($activityId, [
|
|
|
|
'payload' => $deleteActivity->toJSON(),
|
|
|
|
]);
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
$result = model('NoteModel', false)
|
|
|
|
->delete($note->id);
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
Events::trigger('on_note_remove', $note);
|
|
|
|
|
|
|
|
$this->clearCache($note);
|
|
|
|
|
2021-04-02 17:20:02 +00:00
|
|
|
$this->db->transComplete();
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function addReply(
|
2021-05-14 17:59:35 +00:00
|
|
|
Note $reply,
|
|
|
|
bool $createPreviewCard = true,
|
|
|
|
bool $registerActivity = true
|
2021-05-19 16:35:13 +00:00
|
|
|
): string | false {
|
|
|
|
if (! $reply->in_reply_to_id) {
|
2021-05-06 14:00:48 +00:00
|
|
|
throw new Exception('Passed note is not a reply!');
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->db->transStart();
|
|
|
|
|
|
|
|
$noteId = $this->addNote($reply, $createPreviewCard, $registerActivity);
|
|
|
|
|
|
|
|
model('NoteModel')
|
2021-06-09 12:40:22 +00:00
|
|
|
->where('id', $this->uuid->fromString($reply->in_reply_to_id) ->getBytes())
|
2021-04-02 17:20:02 +00:00
|
|
|
->increment('replies_count');
|
|
|
|
|
|
|
|
Events::trigger('on_note_reply', $reply);
|
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
$this->clearCache($reply);
|
|
|
|
|
2021-04-02 17:20:02 +00:00
|
|
|
$this->db->transComplete();
|
|
|
|
|
|
|
|
return $noteId;
|
|
|
|
}
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
public function reblog(Actor $actor, Note $note, bool $registerActivity = true): string | false
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
|
|
|
$this->db->transStart();
|
|
|
|
|
|
|
|
$reblog = new Note([
|
|
|
|
'actor_id' => $actor->id,
|
|
|
|
'reblog_of_id' => $note->id,
|
|
|
|
'published_at' => Time::now(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// add reblog
|
2021-05-14 17:59:35 +00:00
|
|
|
$reblogId = $this->insert($reblog);
|
2021-04-02 17:20:02 +00:00
|
|
|
|
|
|
|
model('ActorModel')
|
|
|
|
->where('id', $actor->id)
|
|
|
|
->increment('notes_count');
|
|
|
|
|
|
|
|
model('NoteModel')
|
2021-04-22 17:20:28 +00:00
|
|
|
->where('id', $this->uuid->fromString($note->id)->getBytes())
|
2021-04-02 17:20:02 +00:00
|
|
|
->increment('reblogs_count');
|
|
|
|
|
|
|
|
if ($registerActivity) {
|
|
|
|
$announceActivity = new AnnounceActivity($reblog);
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
$activityId = model('ActivityModel')
|
|
|
|
->newActivity(
|
|
|
|
'Announce',
|
|
|
|
$actor->id,
|
|
|
|
null,
|
|
|
|
$note->id,
|
|
|
|
$announceActivity->toJSON(),
|
|
|
|
$reblog->published_at,
|
|
|
|
'queued',
|
|
|
|
);
|
|
|
|
|
2021-06-09 12:40:22 +00:00
|
|
|
$announceActivity->set('id', base_url(route_to('activity', $note->actor->username, $activityId)));
|
2021-05-19 16:35:13 +00:00
|
|
|
|
|
|
|
model('ActivityModel')
|
|
|
|
->update($activityId, [
|
|
|
|
'payload' => $announceActivity->toJSON(),
|
|
|
|
]);
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
Events::trigger('on_note_reblog', $actor, $note);
|
|
|
|
|
|
|
|
$this->clearCache($note);
|
|
|
|
|
2021-04-02 17:20:02 +00:00
|
|
|
$this->db->transComplete();
|
|
|
|
|
|
|
|
return $reblogId;
|
|
|
|
}
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
public function undoReblog(Note $reblogNote, bool $registerActivity = true): BaseResult | bool
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
|
|
|
$this->db->transStart();
|
|
|
|
|
|
|
|
model('ActorModel')
|
|
|
|
->where('id', $reblogNote->actor_id)
|
|
|
|
->decrement('notes_count');
|
|
|
|
|
|
|
|
model('NoteModel')
|
2021-06-09 12:40:22 +00:00
|
|
|
->where('id', $this->uuid->fromString($reblogNote->reblog_of_id) ->getBytes())
|
2021-04-02 17:20:02 +00:00
|
|
|
->decrement('reblogs_count');
|
|
|
|
|
|
|
|
if ($registerActivity) {
|
|
|
|
$undoActivity = new UndoActivity();
|
|
|
|
// get like activity
|
|
|
|
$activity = model('ActivityModel')
|
|
|
|
->where([
|
|
|
|
'type' => 'Announce',
|
|
|
|
'actor_id' => $reblogNote->actor_id,
|
2021-04-22 17:20:28 +00:00
|
|
|
'note_id' => $this->uuid
|
2021-04-02 17:20:02 +00:00
|
|
|
->fromString($reblogNote->reblog_of_id)
|
|
|
|
->getBytes(),
|
|
|
|
])
|
|
|
|
->first();
|
|
|
|
|
|
|
|
$announceActivity = new AnnounceActivity($reblogNote);
|
|
|
|
$announceActivity->set(
|
|
|
|
'id',
|
2021-06-09 12:40:22 +00:00
|
|
|
base_url(route_to('activity', $reblogNote->actor->username, $activity->id)),
|
2021-04-02 17:20:02 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$undoActivity
|
|
|
|
->set('actor', $reblogNote->actor->uri)
|
|
|
|
->set('object', $announceActivity);
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
$activityId = model('ActivityModel')
|
|
|
|
->newActivity(
|
|
|
|
'Undo',
|
|
|
|
$reblogNote->actor_id,
|
|
|
|
null,
|
|
|
|
$reblogNote->reblog_of_id,
|
|
|
|
$undoActivity->toJSON(),
|
|
|
|
Time::now(),
|
|
|
|
'queued',
|
|
|
|
);
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-06-09 12:40:22 +00:00
|
|
|
$undoActivity->set('id', base_url(route_to('activity', $reblogNote->actor->username, $activityId)));
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
model('ActivityModel')
|
|
|
|
->update($activityId, [
|
|
|
|
'payload' => $undoActivity->toJSON(),
|
|
|
|
]);
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
$result = model('NoteModel', false)
|
|
|
|
->delete($reblogNote->id);
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
Events::trigger('on_note_undo_reblog', $reblogNote);
|
|
|
|
|
|
|
|
$this->clearCache($reblogNote);
|
|
|
|
|
2021-04-02 17:20:02 +00:00
|
|
|
$this->db->transComplete();
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
public function toggleReblog(Actor $actor, Note $note): void
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
|
|
|
if (
|
2021-05-19 16:35:13 +00:00
|
|
|
! ($reblogNote = $this->where([
|
2021-04-02 17:20:02 +00:00
|
|
|
'actor_id' => $actor->id,
|
2021-04-22 17:20:28 +00:00
|
|
|
'reblog_of_id' => $this->uuid
|
2021-04-02 17:20:02 +00:00
|
|
|
->fromString($note->id)
|
|
|
|
->getBytes(),
|
|
|
|
])->first())
|
|
|
|
) {
|
|
|
|
$this->reblog($actor, $note);
|
|
|
|
} else {
|
|
|
|
$this->undoReblog($reblogNote);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
public function clearCache(Note $note): void
|
|
|
|
{
|
|
|
|
$cachePrefix = config('ActivityPub')
|
|
|
|
->cachePrefix;
|
|
|
|
|
|
|
|
$hashedNoteUri = md5($note->uri);
|
|
|
|
|
|
|
|
model('ActorModel')
|
|
|
|
->clearCache($note->actor);
|
|
|
|
cache()
|
|
|
|
->deleteMatching($cachePrefix . "note#{$note->id}*");
|
|
|
|
cache()
|
|
|
|
->deleteMatching($cachePrefix . "note-{$hashedNoteUri}*");
|
|
|
|
|
2021-06-09 12:40:22 +00:00
|
|
|
if ($note->in_reply_to_id !== null) {
|
2021-06-08 09:52:11 +00:00
|
|
|
$this->clearCache($note->reply_to_note);
|
|
|
|
}
|
|
|
|
|
2021-06-09 12:40:22 +00:00
|
|
|
if ($note->reblog_of_id !== null) {
|
2021-06-08 09:52:11 +00:00
|
|
|
$this->clearCache($note->reblog_of_note);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
/**
|
2021-05-14 17:59:35 +00:00
|
|
|
* @param array<string, array<string|int, mixed>> $data
|
|
|
|
* @return array<string, array<string|int, mixed>>
|
|
|
|
*/
|
|
|
|
protected function setNoteId(array $data): array
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2021-04-22 17:20:28 +00:00
|
|
|
$uuid4 = $this->uuid->{$this->uuidVersion}();
|
|
|
|
$data['data']['id'] = $uuid4->toString();
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! isset($data['data']['uri'])) {
|
|
|
|
$actor = model('ActorModel')
|
2021-06-09 12:40:22 +00:00
|
|
|
->getActorById((int) $data['data']['actor_id']);
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
$data['data']['uri'] = base_url(route_to('note', $actor->username, $uuid4->toString()));
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
}
|