[PLUGIN][Repeat] onAppendCardNote added. getNoteRepeats implemented.

This commit is contained in:
Eliseu Amaro 2021-11-15 17:11:27 +00:00 committed by Hugo Sales
parent 774eb49af4
commit b71e869843
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
3 changed files with 68 additions and 11 deletions

View File

@ -97,7 +97,7 @@ class Repeat extends Controller
// Add it to note_repeat table // Add it to note_repeat table
if (!is_null($repeat_id)) { if (!is_null($repeat_id)) {
DB::persist(NoteRepeat::create([ DB::persist(NoteRepeat::create([
'id' => $repeat_id, 'note_id' => $repeat_id,
'actor_id' => $actor_id, 'actor_id' => $actor_id,
'repeat_of' => $og_id 'repeat_of' => $og_id
])); ]));

View File

@ -23,7 +23,9 @@ declare(strict_types=1);
namespace Plugin\Repeat\Entity; namespace Plugin\Repeat\Entity;
use App\Core\DB\DB;
use App\Core\Entity; use App\Core\Entity;
use App\Entity\Note;
/** /**
* Entity for notices * Entity for notices
@ -37,19 +39,19 @@ use App\Core\Entity;
*/ */
class NoteRepeat extends Entity class NoteRepeat extends Entity
{ {
private int $id; private int $note_id;
private int $actor_id; private int $actor_id;
private int $repeat_of; private int $repeat_of;
public function setId(int $id): self public function setNoteId(int $note_id): self
{ {
$this->id = $id; $this->note_id = $note_id;
return $this; return $this;
} }
public function getId(): int public function getNoteId(): int
{ {
return $this->id; return $this->note_id;
} }
public function setActorId(int $actor_id): self public function setActorId(int $actor_id): self
@ -74,18 +76,37 @@ class NoteRepeat extends Entity
return $this->repeat_of; return $this->repeat_of;
} }
public static function getNoteRepeats(Note $note): array
{
return DB::sql(
<<<'EOF'
select {select} from note n
inner join note_repeat nr
on nr.note_id = n.id
where repeat_of = :note_id
order by n.created DESC
EOF,
['note_id' => $note->getId()]
);
}
public static function schemaDef(): array public static function schemaDef(): array
{ {
return [ return [
'name' => 'note_repeat', 'name' => 'note_repeat',
'fields' => [ 'fields' => [
'id' => ['type' => 'int', 'not null' => true, 'description' => 'The id of the repeat itself'], 'note_id' => ['type' => 'int', 'not null' => true, 'foreign key' => true, 'target' => 'Note.id', 'multiplicity' => 'one to one', 'description' => 'The id of the repeat itself'],
'actor_id' => ['type' => 'int', 'not null' => true, 'foreign key' => true, 'target' => 'Actor.id', 'multiplicity' => 'one to one', 'description' => 'Who made this repeat'], 'actor_id' => ['type' => 'int', 'not null' => true, 'foreign key' => true, 'target' => 'Actor.id', 'multiplicity' => 'one to one', 'description' => 'Who made this repeat'],
'repeat_of' => ['type' => 'int', 'not null' => true, 'foreign key' => true, 'target' => 'Note.id', 'multiplicity' => 'one to one', 'description' => 'Note this is a repeat of'], 'repeat_of' => ['type' => 'int', 'not null' => true, 'foreign key' => true, 'target' => 'Note.id', 'multiplicity' => 'one to one', 'description' => 'Note this is a repeat of'],
], ],
'primary key' => ['id'], 'primary key' => ['note_id'],
'foreign keys' => [ 'foreign keys' => [
'note_id_to_id_fkey' => ['note', ['note_id' => 'id']],
'note_repeat_of_id_fkey' => ['note', ['repeat_of' => 'id']], 'note_repeat_of_id_fkey' => ['note', ['repeat_of' => 'id']],
'actor_reply_to_id_fkey' => ['actor', ['actor_id' => 'id']],
],
'indexes' => [
'note_repeat_of_idx' => ['repeat_of'],
], ],
]; ];
} }

View File

@ -34,6 +34,8 @@ use App\Util\Exception\InvalidFormException;
use App\Util\Exception\NoSuchNoteException; use App\Util\Exception\NoSuchNoteException;
use App\Util\Exception\NotFoundException; use App\Util\Exception\NotFoundException;
use App\Util\Exception\RedirectException; use App\Util\Exception\RedirectException;
use App\Util\Formatting;
use Plugin\Repeat\Entity\NoteRepeat;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
class Repeat extends NoteHandlerPlugin class Repeat extends NoteHandlerPlugin
@ -64,7 +66,7 @@ class Repeat extends NoteHandlerPlugin
} catch (NotFoundException $e) { } catch (NotFoundException $e) {
} }
$is_repeat = DB::count('note_repeat', ['id' => $note->getId()]) >= 1; $is_repeat = DB::count('note_repeat', ['note_id' => $note->getId()]) >= 1;
// Generating URL for repeat action route // Generating URL for repeat action route
$args = ['id' => $note->getId()]; $args = ['id' => $note->getId()];
@ -93,10 +95,44 @@ class Repeat extends NoteHandlerPlugin
public function onAppendCardNote(array $vars, array &$result) { 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 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?" // if user is the one who repeated, append on end "you repeated this, remove repeat?"
$actor = $vars['actor']; $current_actor_id = (Common::actor())->getId();
$note = $vars['note']; $note = $vars['note'];
return Event::next; $complementary_info = '';
$repeat_actor = [];
$note_repeats = NoteRepeat::getNoteRepeats($note);
// Get actors who replied
foreach ($note_repeats as $reply) {
$repeat_actor[] = Actor::getWithPK($reply->getActorId());
}
if (count($repeat_actor) < 1) {
return null;
}
// 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) {
$repeat_actor_url = $actor->getUrl();
$repeat_actor_nickname = $actor->getNickname();
if ($actor->getId() === $current_actor_id) {
// If the repeat is yours
$prepend = "<a href={$repeat_actor_url}>You</a>, " . ($prepend = &$complementary_info);
$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 public function onAddRoute(RouteLoader $r): bool