[PLUGIN][Reply] WIP. Note complementary info now shows who has replied on the original note!

This commit is contained in:
Eliseu Amaro 2021-11-10 15:44:28 +00:00
parent f2f1bdc145
commit d0f9fde7c2
Signed by: eliseuamaro
GPG Key ID: 96DA09D4B97BC2D5
6 changed files with 60 additions and 45 deletions

View File

@ -96,7 +96,7 @@ class Reply extends Controller
// Add it to note_repeat table
if (!is_null($reply_id)) {
DB::persist(NoteReply::create([
'id' => $reply_id,
'note_id' => $reply_id,
'actor_id' => $actor_id,
'reply_to' => $og_id
]));

View File

@ -40,19 +40,19 @@ use function PHPUnit\Framework\isEmpty;
*/
class NoteReply extends Entity
{
private int $id;
private int $note_id;
private int $actor_id;
private int $reply_to;
public function setId(int $id): self
public function setNoteId(int $note_id): self
{
$this->id = $id;
$this->note_id = $note_id;
return $this;
}
public function getId(): int
public function getNoteId(): int
{
return $this->id;
return $this->note_id;
}
public function setActorId(int $actor_id): self
@ -72,22 +72,29 @@ class NoteReply extends Entity
return $this;
}
public function getReplyTo(): self
public function getReplyTo(): int
{
return $this->reply_to;
}
public static function getNoteReplies(Note $note): array
{
return DB::sql("select n.id from note n cross join note_reply nr where n.id = :note_id",
['n' => 'App\Entity\Note', 'note_id' => $note->getId()],
return DB::sql(
<<<'EOF'
select {select} from note n
inner join note_reply nr
on nr.note_id = n.id
where reply_to = :note_id
order by n.created DESC
EOF,
['note_id' => $note->getId()]
);
}
public static function getReplyToNote(Note $note): ?int
{
$result = DB::dql('select nr.reply_to from note_reply nr '
. 'where nr.id = :note_id', ['note_id' => $note->getId()]);
. 'where nr.note_id = :note_id', ['note_id' => $note->getId()]);
if (!isEmpty($result)) {
return $result['reply_to'];
@ -101,17 +108,18 @@ class NoteReply extends Entity
return [
'name' => 'note_reply',
'fields' => [
'id' => ['type' => 'int', 'not null' => true, 'description' => 'The id of the reply itself'],
'note_id' => ['type' => 'int', 'not null' => true, 'foreign key' => true, 'target' => 'Note.id', 'multiplicity' => 'one to one', 'description' => 'The id of the reply itself'],
'actor_id' => ['type' => 'int', 'not null' => true, 'foreign key' => true, 'target' => 'Actor.id', 'multiplicity' => 'one to one', 'description' => 'Who made this reply'],
'reply_to' => ['type' => 'int', 'not null' => true, 'foreign key' => true, 'target' => 'Note.id', 'multiplicity' => 'one to one', 'description' => 'Note this is a reply of'],
],
'primary key' => ['id'],
'primary key' => ['note_id'],
'foreign keys' => [
'note_id_to_id_fkey' => ['note', ['note_id' => 'id']],
'note_reply_to_id_fkey' => ['note', ['reply_to' => 'id']],
'actor_reply_to_id_fkey' => ['actor', ['actor_id' => 'id']],
],
'indexes' => [
'note_reply_to_idx' => ['reply_to'],
'note_reply_to_idx' => ['reply_to'],
],
];
}

View File

@ -79,40 +79,35 @@ class Reply extends NoteHandlerPlugin
public function onAppendCardNote(array $vars, array &$result) {
// 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"
$actor = $vars['actor'];
$note = $vars['note'];
if ($actor !== null) {
try {
try {
$complementary_info = '';
$note_replies[] = NoteReply::getNoteReplies($note);
$complementary_info = '';
$reply_actor = [];
$note_replies = NoteReply::getNoteReplies($note);
if (isEmpty($note_replies)) {
return Event::next;
}
dd($note_replies);
foreach ($note_replies as $reply) {
$reply_actor = Actor::getWithPK($reply['actor_id']);
$reply_actor_url = $reply_actor->getUrl();
$reply_actor_nickname = $reply_actor->getNickname();
$complementary_info .= "<a href={$reply_actor_url}>{$reply_actor_nickname}</a>, ";
}
$complementary_info = rtrim(trim($complementary_info), ',');
$complementary_info .= ' replied to this note.';
$result[] = Formatting::twigRenderString($complementary_info, []);
} catch (NotFoundException $e) {
return Event::next;
}
} catch (NotFoundException $e) {
return Event::next;
}
// Get actors who replied
foreach ($note_replies as $reply) {
$reply_actor[] = Actor::getWithPK($reply->getActorId());
}
if (count($reply_actor) < 1) {
return null;
}
return Event::next;
// Filter out multiple replies from the same actor
$reply_actor = array_unique($reply_actor, SORT_REGULAR);
// Add to complementary info
foreach ($reply_actor as $actor) {
$reply_actor_url = $actor->getUrl();
$reply_actor_nickname = $actor->getNickname();
$complementary_info .= "<a href={$reply_actor_url}>{$reply_actor_nickname}</a>, ";
}
$complementary_info = rtrim(trim($complementary_info), ',');
$complementary_info .= ' replied to this note.';
$result[] = Formatting::twigRenderString($complementary_info, []);
return $result;
}
public function onAddRoute($r)

View File

@ -272,7 +272,8 @@ embed header {
-webkit-border-radius: 0 0 var(--smaller) var(--smaller);
-moz-border-radius: 0 0 var(--smaller) var(--smaller);
border-radius: 0 0 var(--smaller) var(--smaller);
padding: var(--default)
padding-top: var(--default);
padding-bottom: var(--default);
}
.note-text {
@ -315,3 +316,15 @@ embed header {
margin-bottom: var(--smaller)
}
.note-complementary {
border-left: 2px solid var(--accent);
padding-left: var(--smaller);
padding-top: 5px;
padding-bottom: 5px;
margin-bottom: var(--smaller);
background: var(--gradient) !important;
}
.note-complementary a {
font-weight: bold;
}

View File

@ -324,7 +324,6 @@ class Note extends Entity
'note_actor_created_idx' => ['actor_id', 'created'],
'note_is_local_created_actor_idx' => ['is_local', 'created', 'actor_id'],
'note_conversation_created_idx' => ['conversation', 'created'],
'note_reply_to_idx' => ['reply_to'],
],
'fulltext indexes' => ['notice_fulltext_idx' => ['content']], // TODO make this configurable
];

View File

@ -96,7 +96,7 @@
{{ block('note_links') }}
</section>
{% for block in handle_event('AppendCardNote', {'note': note, 'actor': actor}) %}
{% for block in handle_event('AppendCardNote', {'note': note, 'actor': note.getActor() }) %}
<aside class="note-complementary">
{{ block | raw }}
</aside>