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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
<?php
namespace Forum\Service;
use Forum\Enum\SubscriptionNotificationType;
use Icon;
use PersonalNotifications;
use Forum\Discussion;
use Forum\Posting;
use Forum\Subscription;
use Forum\Topic;
use URLHelper;
class PostingNotification
{
protected Posting $posting;
protected Discussion $discussion;
protected Topic $topic;
public function __construct(Posting $posting)
{
$this->posting = $posting;
$this->topic = $posting->discussion->topic;
$this->discussion = $posting->discussion;
}
public function notifySubscribers(): void
{
$excludeUserId = null;
if ($this->posting->parent_id) {
$subscriber = $this->notifyParentPostAuthor();
if ($subscriber) {
$excludeUserId = $subscriber->user_id;
}
}
$subscribers = $this->getSubscribers($excludeUserId);
foreach ($subscribers as $subscriber) {
if ($subscriber->user_id === $this->posting->user_id || $subscriber->notification_type !== SubscriptionNotificationType::All->value) {
continue;
}
$this->sendNotifications($subscriber);
}
}
protected function getSubscribers($excludeUserId = null): array
{
$query = [
"range_id = :range_id AND subject_id IN (:subject_ids)",
[
'range_id' => $this->posting->range_id,
'subject_ids' => [$this->discussion->discussion_id, $this->topic->topic_id]
]
];
if ($excludeUserId) {
$query[0] .= " AND user_id != :user_id";
$query[1]['user_id'] = $excludeUserId;
}
$subscriptions = Subscription::findBySQL(...$query);
/**
* Allow only one subscription per user.
* 'discussion' subscription has priority over 'topic' subscription
*/
$filteredSubscriptions = [];
foreach ($subscriptions as $subscription) {
$userId = $subscription->user_id;
if (isset($filtered[$userId])) {
if ($filteredSubscriptions[$userId]->subject === 'discussion') {
continue;
}
// If current subscription is discussion, replace it with topic
if ($subscription->subject === 'discussion') {
$filteredSubscriptions[$userId] = $subscription;
}
continue;
}
$filteredSubscriptions[$userId] = $subscription;
}
return array_values($filteredSubscriptions);
}
protected function sendNotifications(Subscription $subscriber): void
{
$url = URLHelper::getURL('dispatch.php/course/forum/discussions/show/'.$this->discussion->discussion_id, ['cid' => $this->topic->range_id], true)."#post_" . $this->posting->posting_id;
$message = sprintf(
_('Es gibt einen neuen Beitrag zur Diskussion ā%sā.'),
$this->discussion->title
);
PersonalNotifications::add(
$subscriber->user_id,
$url,
$message,
"post_" . $this->posting->posting_id,
Icon::create('reply')
);
}
protected function notifyParentPostAuthor(): ?Subscription
{
$parent = $this->posting->posting;
$subscriber = Subscription::findOneBySQL(
"range_id = :range_id AND subject_id IN (:subject_ids) AND user_id = :user_id AND notification_type != :notification_type ORDER BY subject",
[
'range_id' => $parent->range_id,
'subject_ids' => [$this->discussion->discussion_id, $this->topic->topic_id],
'user_id' => $parent->user_id,
'notification_type' => SubscriptionNotificationType::None->value
]
);
if ($subscriber && $subscriber->user_id !== $this->posting->user_id) {
\PersonalNotifications::add(
$subscriber->user_id,
\URLHelper::getURL('dispatch.php/course/forum/discussions/show/'.$this->posting->discussion_id, ['cid' => $this->topic->range_id], true)."#post_" . $this->posting->posting_id,
sprintf(
_('%s hat ihren Beitrag zur Diskussion ā%sā zitiert.'),
$this->posting->anonymous ? _('Jemand') : $this->posting->user->getFullName(),
$this->discussion->title
),
"post_" . $this->posting->posting_id,
\Icon::create('quote')
);
}
return $subscriber;
}
}
|