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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
<?php
/**
* * Name: HQ Messages
* * Description: Quick access to messages, direct messages, starred messages (if enabled) and notifications
* * Author: Mario Vavti
* * Requires: hq
*/
namespace Zotlabs\Widget;
use App;
use Zotlabs\Lib\IConfig;
class Messages {
public static function widget($arr) {
if (!local_channel())
return EMPTY_STR;
$page = self::get_messages_page([]);
$_SESSION['messages_loadtime'] = datetime_convert();
$r = q("SELECT DISTINCT(term) FROM term WHERE uid = %d AND ttype = %d ORDER BY term",
intval(local_channel()),
intval(TERM_FILE)
);
if ($r) {
foreach($r as $rr) {
$file_tags[] = $rr['term'];
}
}
$tpl = get_markup_template('messages_widget.tpl');
$o = replace_macros($tpl, [
'$entries' => $page['entries'] ?? [],
'$offset' => $page['offset'] ?? 0,
'$feature_star' => feature_enabled(local_channel(), 'star_posts'),
'$feature_file' => feature_enabled(local_channel(), 'filing'),
'$file_tags' => $file_tags,
'$strings' => [
'messages_title' => t('Public and restricted messages'),
'direct_messages_title' => t('Direct messages'),
'starred_messages_title' => t('Starred messages'),
'filed_messages_title' => t('Filed messages'),
'notice_messages_title' => t('Notices'),
'loading' => t('Loading'),
'empty' => t('No messages'),
'unseen_count' => t('Unseen'),
'filter' => t('Filter by name or address'),
'file_filter' => t('Filter by file name')
]
]);
return $o;
}
public static function get_messages_page($options) {
if (!local_channel())
return;
$offset = $options['offset'] ?? 0;
$type = $options['type'] ?? '';
$author = $options['author'] ?? '';
$file = $options['file'] ?? '';
if ($offset == -1) {
return;
}
if ($type == 'notification') {
return self::get_notices_page($options);
}
$channel = App::get_channel();
$item_normal = item_normal();
// Filter internal follow activities and strerams add/remove activities
$item_normal .= " and item.verb not in ('Add', 'Remove', 'Follow', 'Ignore', '" . ACTIVITY_FOLLOW . "') ";
$item_normal_i = str_replace('item.', 'i.', $item_normal);
$item_normal_c = str_replace('item.', 'c.', $item_normal);
$entries = [];
$limit = 30;
$order_sql = 'i.created DESC';
$dummy_order_sql = '';
$filter_sql = '';
$loadtime = (($offset) ? $_SESSION['messages_loadtime'] : datetime_convert());
$vnotify = get_pconfig(local_channel(), 'system', 'vnotify', -1);
$vnotify_sql_c = '';
$vnotify_sql_i = '';
if (!($vnotify & VNOTIFY_LIKE)) {
$vnotify_sql_c = " AND c.verb NOT IN ('Like', 'Dislike', '" . dbesc(ACTIVITY_LIKE) . "', '" . dbesc(ACTIVITY_DISLIKE) . "') ";
$vnotify_sql_i = " AND i.verb NOT IN ('Like', 'Dislike', '" . dbesc(ACTIVITY_LIKE) . "', '" . dbesc(ACTIVITY_DISLIKE) . "') ";
}
elseif (!feature_enabled(local_channel(), 'dislike')) {
$vnotify_sql_c = " AND c.verb NOT IN ('Dislike', '" . dbesc(ACTIVITY_DISLIKE) . "') ";
$vnotify_sql_i = " AND i.verb NOT IN ('Dislike', '" . dbesc(ACTIVITY_DISLIKE) . "') ";
}
if($type !== 'filed' && $author) {
$filter_sql = " AND (i.owner_xchan = '" . protect_sprintf(dbesc($author)) . "') ";
}
if($type === 'filed' && $file) {
$filed_filter_sql = " AND (term.term = '" . protect_sprintf(dbesc($file)) . "') ";
}
switch($type) {
case 'direct':
$type_sql = ' AND i.item_private = 2 AND i.item_thread_top = 1 ';
// $dummy_order_sql has no other meaning but to trick
// some mysql backends into using the right index.
$dummy_order_sql = ', i.received DESC ';
break;
case 'starred':
$type_sql = ' AND i.item_starred = 1 AND i.item_thread_top = 1 ';
break;
case 'filed':
$type_sql = ' AND i.id IN (SELECT term.oid FROM term WHERE term.ttype = ' . TERM_FILE . ' AND term.uid = i.uid ' . $filed_filter_sql . ')';
break;
default:
$type_sql = ' AND i.item_private IN (0, 1) AND i.item_thread_top = 1 ';
}
$items = q("SELECT *,
(SELECT count(*) FROM item c WHERE c.uid = %d AND c.parent = i.parent AND c.item_unseen = 1 AND c.item_thread_top = 0 $item_normal_c $vnotify_sql_c) AS unseen_count
FROM item i
WHERE i.uid = %d
AND i.created <= '%s'
$type_sql
$filter_sql
$item_normal_i
ORDER BY $order_sql $dummy_order_sql
LIMIT $limit OFFSET $offset",
intval(local_channel()),
intval(local_channel()),
dbescdate($loadtime)
);
if ($type === 'filed') {
$items = fetch_post_tags($items);
}
xchan_query($items, false);
$i = 0;
$entries = [];
$ids = [];
foreach($items as $item) {
$hook_data = [
'uid' => $item['uid'],
'owner_xchan' => $item['owner_xchan'],
'author_xchan' => $item['author_xchan'],
'cancel' => false
];
call_hooks('messages_widget', $hook_data);
if ($hook_data['cancel']) {
continue;
}
$info = '';
if ($type == 'direct') {
$info .= self::get_dm_recipients($channel, $item);
}
if($item['owner_xchan'] !== $item['author_xchan']) {
$info .= t('via') . ' ' . $item['owner']['xchan_name'];
}
elseif($item['verb'] === 'Announce' && isset($item['source'])) {
$info .= t('via') . ' ' . $item['source']['xchan_name'];
}
if ($type == 'filed') {
$info = '';
foreach ($item['term'] as $t) {
if ($t['ttype'] !== TERM_FILE) {
continue;
}
$info .= '<span class="badge rounded-pill bg-danger me-1"><i class="bi bi-folder"></i> ' . $t['term'] . '</span>';
}
}
$summary = $item['title'];
if (!$summary) {
$summary = $item['summary'];
}
if (!$summary) {
$summary = html2plain(bbcode($item['body'], ['drop_media' => true, 'tryoembed' => false]), 75, true);
if ($summary) {
$summary = htmlentities($summary, ENT_QUOTES, 'UTF-8', false);
}
}
if (!$summary) {
$summary = '...';
}
else {
$summary = substr_words($summary, 140);
}
switch(intval($item['item_private'])) {
case 1:
$icon = '<i class="bi bi-lock"></i>';
break;
case 2:
$icon = '<i class="bi bi-envelope"></i>';
break;
default:
$icon = '';
}
$entries[$i]['author_name'] = $item['author']['xchan_name'];
$entries[$i]['author_addr'] = (($item['author']['xchan_addr']) ? $item['author']['xchan_addr'] : $item['author']['xchan_url']);
$entries[$i]['author_img'] = $item['author']['xchan_photo_s'];
$entries[$i]['info'] = $info;
$entries[$i]['created'] = datetime_convert('UTC', date_default_timezone_get(), $item['created']);
$entries[$i]['summary'] = $summary;
//$entries[$i]['b64mid'] = gen_link_id($item['mid']);
$entries[$i]['b64mid'] = $item['uuid'];
$entries[$i]['href'] = z_root() . '/hq/' . $item['uuid'];
$entries[$i]['icon'] = $icon;
$entries[$i]['unseen_count'] = (($item['unseen_count']) ? $item['unseen_count'] : (($item['item_unseen']) ? ' ' : ''));
$entries[$i]['unseen_class'] = (($item['item_unseen']) ? 'primary' : 'secondary');
$i++;
}
$result = [
'offset' => ((count($entries) < $limit) ? -1 : intval($offset + $limit)),
'entries' => $entries
];
return $result;
}
public static function get_dm_recipients($channel, $item) {
if($channel['channel_hash'] === $item['owner']['xchan_hash']) {
// we are the owner, get the recipients from the item
$recips = expand_acl($item['allow_cid']);
if (is_array($recips)) {
array_unshift($recips, $item['owner']['xchan_hash']);
$column = 'xchan_hash';
}
}
else {
$recips = IConfig::Get($item, 'activitypub', 'recips');
if (isset($recips['to']) && is_array($recips['to'])) {
$recips = $recips['to'];
array_unshift($recips, $item['owner']['xchan_url']);
$column = 'xchan_url';
}
else {
$hookinfo = [
'item' => $item,
'recips' => null,
'column' => ''
];
call_hooks('direct_message_recipients', $hookinfo);
$recips = $hookinfo['recips'];
$column = $hookinfo['column'];
}
}
$recipients = '';
if(is_array($recips)) {
stringify_array_elms($recips, true);
$query_str = implode(',', $recips);
$xchans = dbq("SELECT DISTINCT xchan_name FROM xchan WHERE $column IN ($query_str) AND xchan_deleted = 0");
foreach($xchans as $xchan) {
$recipients .= $xchan['xchan_name'] . ', ';
}
}
return trim($recipients, ', ');
}
public static function get_notices_page($options) {
if (!local_channel())
return;
$limit = 30;
$offset = 0;
if ($options['offset']) {
$offset = intval($options['offset']);
}
$author_url = $options['author'] ?? '';
$author_sql = '';
if($author_url) {
$author_sql = " AND url = '" . protect_sprintf(dbesc($author_url)) . "' ";
}
$notices = q("SELECT * FROM notify WHERE uid = %d $author_sql
ORDER BY created DESC LIMIT $limit OFFSET $offset",
intval(local_channel())
);
$i = 0;
$entries = [];
foreach($notices as $notice) {
$summary = trim(strip_tags(bbcode($notice['msg'])));
if(strpos($summary, $notice['xname']) === 0) {
$summary = substr($summary, strlen($notice['xname']) + 1);
}
$entries[$i]['author_name'] = $notice['xname'];
$entries[$i]['author_addr'] = $notice['url'];
$entries[$i]['author_img'] = $notice['photo'];// $item['author']['xchan_photo_s'];
$entries[$i]['info'] = '';
$entries[$i]['created'] = datetime_convert('UTC', date_default_timezone_get(), $notice['created']);
$entries[$i]['summary'] = $summary;
$entries[$i]['b64mid'] = (($notice['ntype'] & NOTIFY_INTRO) ? '' : ((str_contains($notice['hash'], '-')) ? $notice['hash'] : basename($notice['link'])));
$entries[$i]['href'] = (($notice['ntype'] & NOTIFY_INTRO) ? $notice['link'] : z_root() . '/hq/' . ((str_contains($notice['hash'], '-')) ? $notice['hash'] : basename($notice['link'])));
$entries[$i]['icon'] = (($notice['ntype'] & NOTIFY_INTRO) ? '<i class="bi bi-person-plus"></i>' : '');
$i++;
}
$result = [
'offset' => ((count($entries) < $limit) ? -1 : intval($offset + $limit)),
'entries' => $entries
];
return $result;
}
}
|