diff options
| author | winter | 2025-01-20 18:20:45 +0000 |
|---|---|---|
| committer | winter | 2025-01-20 18:20:45 +0000 |
| commit | b8e259f75dfa52584d7483313b0c6814867d2330 (patch) | |
| tree | 00f84b0f4485889d2278de99389fdced001ba357 /Digitigrade/Model | |
| parent | fb4cbc73bae831e68fe8234811184f46077db5e6 (diff) | |
follow, unfollow, follow request notifications
Diffstat (limited to 'Digitigrade/Model')
| -rw-r--r-- | Digitigrade/Model/Actor.php | 9 | ||||
| -rw-r--r-- | Digitigrade/Model/FollowRelation.php | 37 |
2 files changed, 44 insertions, 2 deletions
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 @@ <?php namespace Digitigrade\Model; +use Digitigrade\Notification\PendingFollowActionedNotif; +use Digitigrade\Notification\UnfollowNotif; use Digitigrade\RpcException; use Digitigrade\RpcReceiver; @@ -118,21 +120,26 @@ class Actor extends PushableModel implements RpcReceiver { switch ($method) { case 'follow': $status = $this->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 |
