3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Model;
24 use Friendica\Core\Logger;
25 use Friendica\Core\System;
26 use Friendica\Core\Worker;
28 use Friendica\Database\DBA;
29 use Friendica\Model\Notify\Type;
30 use Friendica\Network\Probe;
31 use Friendica\Protocol\Activity;
32 use Friendica\Util\DateTimeFormat;
33 use Friendica\Worker\Delivery;
36 * Class to handle private messages
41 * Insert received private message
44 * @return int|boolean Message ID or false on error
46 public static function insert($msg)
48 $user = User::getById($msg['uid']);
50 if (!isset($msg['reply'])) {
51 $msg['reply'] = DBA::exists('mail', ['parent-uri' => $msg['parent-uri']]);
54 if (empty($msg['convid'])) {
55 $mail = DBA::selectFirst('mail', ['convid'], ["`convid` != 0 AND `parent-uri` = ?", $msg['parent-uri']]);
56 if (DBA::isResult($mail)) {
57 $msg['convid'] = $mail['convid'];
61 if (empty($msg['guid'])) {
62 $host = parse_url($msg['from-url'], PHP_URL_HOST);
63 $msg['guid'] = Item::guidFromUri($msg['uri'], $host);
66 $msg['created'] = (!empty($msg['created']) ? DateTimeFormat::utc($msg['created']) : DateTimeFormat::utcNow());
70 if (DBA::exists('mail', ['uri' => $msg['uri'], 'uid' => $msg['uid']])) {
72 Logger::info('duplicate message already delivered.');
76 DBA::insert('mail', $msg);
78 $msg['id'] = DBA::lastInsertId();
82 if (!empty($msg['convid'])) {
83 DBA::update('conv', ['updated' => DateTimeFormat::utcNow()], ['id' => $msg['convid']]);
86 // send notifications.
89 'notify_flags' => $user['notify-flags'],
90 'language' => $user['language'],
91 'to_name' => $user['username'],
92 'to_email' => $user['email'],
93 'uid' => $user['uid'],
95 'parent' => $msg['id'],
96 'source_name' => $msg['from-name'],
97 'source_link' => $msg['from-url'],
98 'source_photo' => $msg['from-photo'],
99 'verb' => Activity::POST,
103 notification($notif_params);
105 Logger::info('Mail is processed, notification was sent.', ['id' => $msg['id'], 'uri' => $msg['uri']]);
111 * Send private message
113 * @param integer $recipient recipient id, default 0
114 * @param string $body message body, default empty
115 * @param string $subject message subject, default empty
116 * @param string $replyto reply to
118 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
120 public static function send($recipient = 0, $body = '', $subject = '', $replyto = '')
128 if (!strlen($subject)) {
129 $subject = DI::l10n()->t('[no subject]');
132 $me = DBA::selectFirst('contact', [], ['uid' => local_user(), 'self' => true]);
133 $contact = DBA::selectFirst('contact', [], ['id' => $recipient, 'uid' => local_user()]);
135 if (!(count($me) && (count($contact)))) {
139 Photo::setPermissionFromBody($body, local_user(), $me['id'], '<' . $contact['id'] . '>', '', '', '');
141 $guid = System::createUUID();
142 $uri = Item::newURI(local_user(), $guid);
147 // look for any existing conversation structure
149 if (strlen($replyto)) {
151 $condition = ["`uid` = ? AND (`uri` = ? OR `parent-uri` = ?)",
152 local_user(), $replyto, $replyto];
153 $mail = DBA::selectFirst('mail', ['convid'], $condition);
154 if (DBA::isResult($mail)) {
155 $convid = $mail['convid'];
161 // create a new conversation
162 $recip_host = substr($contact['url'], strpos($contact['url'], '://') + 3);
163 $recip_host = substr($recip_host, 0, strpos($recip_host, '/'));
165 $recip_handle = (($contact['addr']) ? $contact['addr'] : $contact['nick'] . '@' . $recip_host);
166 $sender_handle = $a->user['nickname'] . '@' . substr(DI::baseUrl(), strpos(DI::baseUrl(), '://') + 3);
168 $conv_guid = System::createUUID();
169 $convuri = $recip_handle . ':' . $conv_guid;
171 $handles = $recip_handle . ';' . $sender_handle;
173 $fields = ['uid' => local_user(), 'guid' => $conv_guid, 'creator' => $sender_handle,
174 'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
175 'subject' => $subject, 'recips' => $handles];
176 if (DBA::insert('conv', $fields)) {
177 $convid = DBA::lastInsertId();
182 Logger::log('send message: conversation not found.');
186 if (!strlen($replyto)) {
191 $success = DBA::insert(
194 'uid' => local_user(),
197 'from-name' => $me['name'],
198 'from-photo' => $me['thumb'],
199 'from-url' => $me['url'],
200 'contact-id' => $recipient,
207 'parent-uri' => $replyto,
208 'created' => DateTimeFormat::utcNow()
213 $post_id = DBA::lastInsertId();
218 * When a photo was uploaded into the message using the (profile wall) ajax
219 * uploader, The permissions are initially set to disallow anybody but the
220 * owner from seeing it. This is because the permissions may not yet have been
221 * set for the post. If it's private, the photo permissions should be set
222 * appropriately. But we didn't know the final permissions on the post until
223 * now. So now we'll look for links of uploaded messages that are in the
224 * post and set them to the same permissions as the post itself.
228 if (preg_match_all("/\[img\](.*?)\[\/img\]/", $body, $match)) {
230 if (count($images)) {
231 foreach ($images as $image) {
232 $image_rid = Photo::ridFromURI($image);
233 if (!empty($image_rid)) {
234 Photo::update(['allow-cid' => '<' . $recipient . '>'], ['resource-id' => $image_rid, 'album' => 'Wall Photos', 'uid' => local_user()]);
241 Worker::add(PRIORITY_HIGH, "Notifier", Delivery::MAIL, $post_id);
242 return intval($post_id);
249 * @param array $recipient recipient, default empty
250 * @param string $body message body, default empty
251 * @param string $subject message subject, default empty
252 * @param string $replyto reply to, default empty
254 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
255 * @throws \ImagickException
257 public static function sendWall(array $recipient = [], $body = '', $subject = '', $replyto = '')
263 if (!strlen($subject)) {
264 $subject = DI::l10n()->t('[no subject]');
267 $guid = System::createUUID();
268 $uri = Item::newURI(local_user(), $guid);
270 $me = Probe::uri($replyto);
276 $conv_guid = System::createUUID();
278 $recip_handle = $recipient['nickname'] . '@' . substr(DI::baseUrl(), strpos(DI::baseUrl(), '://') + 3);
280 $sender_nick = basename($replyto);
281 $sender_host = substr($replyto, strpos($replyto, '://') + 3);
282 $sender_host = substr($sender_host, 0, strpos($sender_host, '/'));
283 $sender_handle = $sender_nick . '@' . $sender_host;
285 $handles = $recip_handle . ';' . $sender_handle;
288 $fields = ['uid' => $recipient['uid'], 'guid' => $conv_guid, 'creator' => $sender_handle,
289 'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
290 'subject' => $subject, 'recips' => $handles];
291 if (DBA::insert('conv', $fields)) {
292 $convid = DBA::lastInsertId();
296 Logger::log('send message: conversation not found.');
303 'uid' => $recipient['uid'],
306 'from-name' => $me['name'],
307 'from-photo' => $me['photo'],
308 'from-url' => $me['url'],
316 'parent-uri' => $replyto,
317 'created' => DateTimeFormat::utcNow(),