From b8e259f75dfa52584d7483313b0c6814867d2330 Mon Sep 17 00:00:00 2001 From: winter Date: Mon, 20 Jan 2025 18:20:45 +0000 Subject: follow, unfollow, follow request notifications --- Digitigrade/Model/Actor.php | 9 ++++++++- Digitigrade/Model/FollowRelation.php | 37 +++++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) (limited to 'Digitigrade/Model') diff --git a/Digitigrade/Model/Actor.php b/Digitigrade/Model/Actor.php index d04c841..b4e9392 100644 --- a/Digitigrade/Model/Actor.php +++ b/Digitigrade/Model/Actor.php @@ -1,6 +1,8 @@ requestToFollow ? FollowRelationStatus::PENDING : FollowRelationStatus::ACTIVE; - FollowRelation::create($args[0], $this, $status); + $rel = FollowRelation::create($args[0], $this, $status); + $rel->processNotifications(); return $status; case 'unfollow': FollowRelation::findByActors($args[0], $this)->remove(); + (new UnfollowNotif($args[0], $this))->processNotifications(); return; case 'acceptedFollow': $rel = FollowRelation::findByActors($this, $args[0]); $rel->status = FollowRelationStatus::ACTIVE; $rel->save(); + $rel->processNotifications(); + (new PendingFollowActionedNotif($args[0], $this, 'accept'))->processNotifications(); return; case 'rejectedFollow': FollowRelation::findByActors($this, $args[0])->remove(); + (new PendingFollowActionedNotif($args[0], $this, 'reject'))->processNotifications(); return; default: diff --git a/Digitigrade/Model/FollowRelation.php b/Digitigrade/Model/FollowRelation.php index 0af9da5..7cab761 100644 --- a/Digitigrade/Model/FollowRelation.php +++ b/Digitigrade/Model/FollowRelation.php @@ -2,8 +2,9 @@ namespace Digitigrade\Model; use Digitigrade\Model; +use Digitigrade\Notification\Notifyable; -class FollowRelation extends Model { +class FollowRelation extends Model implements Notifyable { public Actor $subject; public Actor $object; public FollowRelationStatus $status; @@ -48,4 +49,38 @@ class FollowRelation extends Model { public static function findAllWithObject(Actor $object): array { return self::findAllWhere('object = ?', [$object->id]); } + + public function toJsonReference(): mixed { + return [$this->subject->id, $this->object->id]; + } + + public static function fromJsonReference(mixed $reference): self { + return self::findByActors( + Actor::find($reference[0]), + Actor::find($reference[1]) + ); + } + + public function getNotificationTitle(): string { + $status = $this->status->value; + return sprintf(__("notifications.follow.$status"), $this->subject->displayName); + } + public function getNotificationTitleLink(): ?string { + return '/@/' . $this->subject->getFullHandle(); + } + public function getNotificationImageUrl(): ?string { + return $this->subject->avatar ?? '/static/default-avatar.png'; + } + public function getNotificationImageLink(): ?string { + return $this->getNotificationTitleLink(); + } + public function getNotificationBody(): ?string { + return null; + } + + public function processNotifications() { + if (!$this->object->isLocal) + return; + Notification::fromNotifyable($this, UserAccount::findByLinkedActor($this->object))->send(); + } } \ No newline at end of file -- cgit v1.3