aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Widget/Messages.php
blob: ed666bfaafff799af94b81453d269bc13ccc4857 (plain) (blame)
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
<?php

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($options);

		$tpl = get_markup_template('messages_widget.tpl');
		$o = replace_macros($tpl, [
			'$entries' => $page['entries'],
			'$offset' => $page['offset'],
			'$feature_star' => feature_enabled(local_channel(), 'star_posts'),
			'$strings' => [
				'messages_title' => t('Public and restricted messages'),
				'direct_messages_title' => t('Direct messages'),
				'starred_messages_title' => t('Starred messages'),
				'loading' => t('Loading'),
				'empty' => t('No messages')
			]
		]);

		return $o;
	}

	public static function get_messages_page($options) {
		if (!local_channel())
			return;

		if ($options['offset'] == -1) {
			return;
		}

		$channel = App::get_channel();
		$item_normal = item_normal();
		$entries = [];
		$limit = 30;
		$dummy_sql = '';

		$offset = 0;
		if ($options['offset']) {
			$offset = intval($options['offset']);
		}

		$loadtime = (($offset) ? $_SESSION['page_loadtime'] : datetime_convert());

		switch($options['type']) {
			case 'direct':
				$type_sql = ' AND item_private = 2 ';
				// $dummy_order_sql has no other meaning but trick
				// some mysql backends into using the right index.
				$dummy_order_sql = ', received DESC ';
				break;
			case 'starred':
				$type_sql = ' AND item_starred = 1 ';
				break;
			default:
				$type_sql = ' AND item_private IN (0, 1) ';
		}

		$items = q("SELECT * FROM item WHERE uid = %d
			AND created <= '%s'
			$type_sql
			AND item_thread_top = 1
			$item_normal
			ORDER BY created DESC $dummy_order_sql
			LIMIT $limit OFFSET $offset",
			intval(local_channel()),
			dbescdate($loadtime)
		);

		xchan_query($items, false);

		$i = 0;

		foreach($items as $item) {

			$info = '';
			if ($options['type'] == 'direct') {
				$info .= self::get_dm_recipients($channel, $item);
			}

			if($item['owner_xchan'] !== $item['author_xchan']) {
				$info .= t('via') . ' ' . $item['owner']['xchan_name'];
			}

			$summary = $item['title'];
			if (!$summary) {
				$summary = $item['summary'];
			}
			if (!$summary) {
				$summary = htmlentities(html2plain(bbcode($item['body'], ['drop_media' => true]), 75, true), ENT_QUOTES, 'UTF-8', false);
			}
			if (!$summary) {
				$summary = '...';
			}
			$summary = substr_words($summary, 68);

			switch(intval($item['item_private'])) {
				case 1:
					$icon = '<i class="fa fa-lock"></i>';
					break;
				case 2:
					$icon = '<i class="fa fa-envelope-o"></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]['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]['href'] = z_root() . '/hq/' . gen_link_id($item['mid']);
			$entries[$i]['icon'] = $icon;

			$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'];
			}
		}

		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)");

			foreach($xchans as $xchan) {
				$recipients .= $xchan['xchan_name'] . ', ';
			}
		}

		return trim($recipients, ', ');
	}

}