aboutsummaryrefslogtreecommitdiffhomepage
path: root/Digitigrade/Model/BlockRelation.php
blob: ca7166cea978e1661facc7c1e2eaa69d798fb31a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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();
    }
}