aboutsummaryrefslogtreecommitdiffhomepage
path: root/Digitigrade/Model/BlockRelation.php
diff options
context:
space:
mode:
Diffstat (limited to 'Digitigrade/Model/BlockRelation.php')
-rw-r--r--Digitigrade/Model/BlockRelation.php82
1 files changed, 82 insertions, 0 deletions
diff --git a/Digitigrade/Model/BlockRelation.php b/Digitigrade/Model/BlockRelation.php
new file mode 100644
index 0000000..ca7166c
--- /dev/null
+++ b/Digitigrade/Model/BlockRelation.php
@@ -0,0 +1,82 @@
+<?php
+namespace Digitigrade\Model;
+
+use Digitigrade\Model;
+use Digitigrade\Notification\Notifyable;
+
+class BlockRelation extends Model implements Notifyable {
+ public Actor $subject;
+ public Actor $object;
+
+ /**
+ * Creates and saves a new blocking relationship
+ * @param Actor $subject the actor who doesn't want to see the other one
+ * @param Actor $object the actor being blocked
+ * @return self
+ */
+ public static function create(Actor $subject, Actor $object): self {
+ $rel = new self();
+ $rel->subject = $subject;
+ $rel->object = $object;
+ $rel->save();
+ return $rel;
+ }
+
+ protected function getUpdateWhereClause(\PDO $db): ?string {
+ if (self::findWhere('subject = ? and object = ?', [$this->subject->id, $this->object->id]) != null) {
+ return 'subject = ' . $this->subject->id . ' and object = ' . $this->object->id;
+ }
+ return null;
+ }
+
+ public static function findByActors(Actor $subject, Actor $object): ?self {
+ return self::findWhere('subject = ? and object = ?', [$subject->id, $object->id]);
+ }
+
+ /**
+ * @return self[]
+ */
+ public static function findAllWithSubject(Actor $subject): array {
+ return self::findAllWhere('subject = ?', [$subject->id]);
+ }
+
+ /**
+ * @return self[]
+ */
+ 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 {
+ return sprintf(__("notifications.block"), $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