-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotification.inc
More file actions
276 lines (262 loc) · 8.64 KB
/
Copy pathnotification.inc
File metadata and controls
276 lines (262 loc) · 8.64 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
// This file is part of Music Match.
// Copyright (C) 2022 David P. Anderson
//
// Music Match is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// Music Match is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Music Match. If not, see <http://www.gnu.org/licenses/>.
// --------------------------------------------------------------------
// stuff related to notifications.
require_once("../inc/mm.inc");
require_once("../inc/mm_db.inc");
require_once("../inc/forum_db.inc");
require_once("../inc/user.inc");
define('NOTIFY_SEARCH_NRESULTS', 10);
// notify the top N search results that user looked for them
define('NOTIFY_NEVER', 0);
define('NOTIFY_DAILY', 1);
define('NOTIFY_WEEKLY', 2);
define('NOTIFY_IMMEDIATE', 3);
define('NOTIFY_LIST', array(
NOTIFY_DAILY => 'Daily',
NOTIFY_WEEKLY => 'Weekly',
NOTIFY_IMMEDIATE => 'Immediately',
NOTIFY_NEVER => 'Never'
));
// return a string describing the notification.
// If $web is true, it's for a web page, so include <a> links where relevant.
// Otherwise it's for a (non-HTML) email.
//
function notification_string($n, $web) {
switch($n->type) {
case NOTIFY_PM:
$pm = BoincPrivateMessage::lookup_id($n->opaque);
if (!$pm) return null;
$u = BoincUser::lookup_id($pm->senderid);
if (!$u) return null;
if ($web) {
return sprintf(
'You received <a href=%s>a message from %s</a>.',
'pm.php?action=inbox', $u->name
);
} else {
return sprintf('You received a message from %s.', $u->name);
}
case NOTIFY_SEARCH:
if ($web) {
return "One of your <a href=search_list.php?new=1>previous searches</a> has a new result.";
} else {
return "One of your previous searches has a new result.";
}
case NOTIFY_ENS_DEL:
$ens = Ensemble::lookup_id($n->opaque);
if (!$ens) return null;
return sprintf('The ensemble %s was removed by its founder.', $ens->name);
case NOTIFY_FOLLOW:
$u = BoincUser::lookup_id($n->opaque);
if (!$u) return null;
if ($web) {
return sprintf("<a href=user.php?user_id=%d>%s</a> is following you.",
$u->id, $u->name
);
} else {
return "$u->name is following you.";
}
case NOTIFY_FRIEND_ACCEPT:
return null;
case NOTIFY_SUBSCRIBED_POST:
if ($web) {
return subscribed_post_web_line($n);
} else {
return subscribed_post_email_line($n);
}
case NOTIFY_PROFILE:
$user = BoincUser::lookup_id($n->opaque);
if (!$user) return null;
if ($web) {
return sprintf("<a href=user.php?user_id=%d>%s</a> changed their %s profile.",
$user->id, $user->name, role_name($n->id2)
);
} else {
return sprintf("%s changed their %s profile.",
$user->name, role_name($n->id2)
);
}
case NOTIFY_ENS_JOIN_REQ:
$ens = Ensemble::lookup_id($n->opaque);
if (!$ens) return null;
$u = BoincUser::lookup_id($n->id2);
if (!$u) return null;
if ($web) {
return sprintf(
'%s asked to join %s. <a href=%s&ens_id=%d>Accept or decline</a>.',
$u->name, $ens->name,
'ensemble_join.php?action=review', $ens->id
);
} else {
return sprintf('%s asked to join %s.', $u->name, $ens->name);
}
case NOTIFY_ENS_JOIN_REPLY:
$ens = Ensemble::lookup_id($n->opaque);
if (!$ens) return null;
if ($web) {
return sprintf(
'Your request to join <a href=ensemble.php?ens_id=%d>%s</a> was %s.',
$ens->id, $ens->name, $n->id2?"accepted":"declined"
);
} else {
return sprintf(
'Your request to join %s was %s.',
$ens->name, $n->id2?"accepted":"declined"
);
}
case NOTIFY_ENS_REMOVE:
$ens = Ensemble::lookup_id($n->opaque);
if (!$ens) return null;
return sprintf(
'You were removed from <a href=ensemble.php?ens_id=%d>%s</a>.',
$ens->id, $ens->name
);
case NOTIFY_ENS_QUIT:
$ens = Ensemble::lookup_id($n->opaque);
if (!$ens) return null;
$u = BoincUser::lookup_id($n->id2);
if ($web) {
return sprintf('<a href=%s>%s</a> resigned from <a href=%s>%s</a>.',
"user.php?user_id=$u->id",
$u->name,
"ensemble.php?ens_id=$ens->id",
$ens->name
);
} else {
return sprintf('%s resigned from %s.', $u->name, $ens->name);
}
break;
case NOTIFY_LOOKING_FOR_YOU:
$u = BoincUser::lookup_id($n->id2);
if ($web) {
return sprintf('<a href=%s>%s</a> searched for %ss like you.',
"user.php?user_id=$u->id",
$u->name,
role_dir($n->opaque)
);
} else {
return sprintf('%s searched for %ss like you',
$u->name, role_dir($n->opaque)
);
}
break;
default:
return "unknown type $n->type";
}
}
// show notifications (on home page)
//
function show_notifications($user) {
$t = time() - 7*86400;
$ns = BoincNotify::enum("userid = $user->id and create_time>$t order by id desc");
if (!$ns) {
row2('No recent notifications', '');
return;
}
foreach ($ns as $n) {
$s = notification_string($n, true);
if (!$s) continue;
row2(time_str($n->create_time), $s);
$n->update(sprintf('last_view=%d', time()));
}
}
// send notifications not previously emailed
// return true if anything was sent
//
//
function send_notify_email($user) {
$now = time();
$ns = BoincNotify::enum("userid=$user->id and sent_by_email=0 order by create_time desc");
if (!$ns) {
return false;
}
$x = [];
foreach ($ns as $n) {
$x[] = notification_string($n, false);
$n->update("sent_by_email=$now");
}
if ($x) {
$subject = "Music Match notifications";
$body = sprintf(
'%s
For details, or to change email settings, visit Music Match:
https://music-match.org/home.php
Spread the word about Music Match: visit
https://music-match.org/email.php
',
implode("\n\n", $x)
);
$body_html = sprintf(
'%s
<p><p>
For details, or to change email settings, <a href=%s>visit Music Match</a>.
<p><p>
<a href=https://music-match.org/email.php>Spread the word</a> about Music Match.
',
implode("<p><p>", $x),
'https://music-match.org/home.php'
);
send_email($user, $subject, $body, null);
}
return true;
}
// user has a new notification.
// If their email setting is "immediate", send them an email
//
function email_if_immediate($user) {
if ($user && $user->send_email == NOTIFY_IMMEDIATE) {
send_notify_email($user);
}
}
// The given user X just did a search.
// Notify the top N users that X is looking for someone like them.
//
function notify_search_results($user, $role, $profiles) {
$n = 0;
$now = time();
foreach ($profiles as $p) {
BoincNotify::replace(
sprintf("userid=%d, create_time=%d, type=%d, opaque=%d, id2=%d, sent_by_email=0",
$p->user->id, $now, NOTIFY_LOOKING_FOR_YOU, $role, $user->id
)
);
email_if_immediate($p->user);
$n++;
if ($n == NOTIFY_SEARCH_NRESULTS) {
break;
}
}
}
// The user has modified their given profile.
// Notify the people who follow them
//
function notify_profile_change($user, $role) {
$friends = BoincFriend::enum("user_dest = $user->id");
$now = time();
foreach ($friends as $friend) {
$user2 = BoincUser::lookup_id($friend->user_src);
if (!$user2) continue;
BoincNotify::replace(
sprintf("userid=%d, create_time=%d, type=%d, opaque=%d, id2=%d, sent_by_email=0",
$user2->id, $now, NOTIFY_PROFILE, $user->id, $role
)
);
email_if_immediate($user2);
}
}
?>