3 * @copyright Copyright (C) 2010-2021, the Friendica project
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;
27 use Friendica\Database\DBA;
29 use Friendica\Protocol\Activity;
30 use Friendica\Util\DateTimeFormat;
31 use Friendica\Worker\Delivery;
34 * Class to handle private messages
39 * Insert received private message
42 * @return int|boolean Message ID or false on error
44 public static function insert($msg)
46 $user = User::getById($msg['uid']);
48 if (!isset($msg['reply'])) {
49 $msg['reply'] = DBA::exists('mail', ['parent-uri' => $msg['parent-uri']]);
52 if (empty($msg['convid'])) {
53 $mail = DBA::selectFirst('mail', ['convid'], ["`convid` != 0 AND `parent-uri` = ?", $msg['parent-uri']]);
54 if (DBA::isResult($mail)) {
55 $msg['convid'] = $mail['convid'];
59 if (empty($msg['guid'])) {
60 $host = parse_url($msg['from-url'], PHP_URL_HOST);
61 $msg['guid'] = Item::guidFromUri($msg['uri'], $host);
64 $msg['created'] = (!empty($msg['created']) ? DateTimeFormat::utc($msg['created']) : DateTimeFormat::utcNow());
68 if (DBA::exists('mail', ['uri' => $msg['uri'], 'uid' => $msg['uid']])) {
70 Logger::info('duplicate message already delivered.');
74 $msg['author-id'] = Contact::getIdForURL($msg['from-url'], 0, false);
75 $msg['uri-id'] = ItemURI::insert(['uri' => $msg['uri'], 'guid' => $msg['guid']]);
76 $msg['parent-uri-id'] = ItemURI::getIdByURI($msg['parent-uri']);
79 $reply = DBA::selectFirst('mail', ['uri', 'uri-id'], ['parent-uri' => $mail['parent-uri'], 'reply' => false]);
81 $msg['thr-parent'] = $reply['uri'];
82 $msg['thr-parent-id'] = $reply['uri-id'];
84 $msg['thr-parent'] = $msg['uri'];
85 $msg['thr-parent-id'] = $msg['uri-id'];
88 DBA::insert('mail', $msg);
90 $msg['id'] = DBA::lastInsertId();
94 if (!empty($msg['convid'])) {
95 DBA::update('conv', ['updated' => DateTimeFormat::utcNow()], ['id' => $msg['convid']]);
98 // send notifications.
100 'type' => Notification\Type::MAIL,
101 'otype' => Notification\ObjectType::MAIL,
102 'verb' => Activity::POST,
103 'uid' => $user['uid'],
104 'cid' => $msg['contact-id'],
105 'link' => DI::baseUrl() . '/message/' . $msg['id'],
108 notification($notif_params);
110 Logger::info('Mail is processed, notification was sent.', ['id' => $msg['id'], 'uri' => $msg['uri']]);
116 * Send private message
118 * @param integer $recipient recipient id, default 0
119 * @param string $body message body, default empty
120 * @param string $subject message subject, default empty
121 * @param string $replyto reply to
123 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
125 public static function send($recipient = 0, $body = '', $subject = '', $replyto = '')
133 if (!strlen($subject)) {
134 $subject = DI::l10n()->t('[no subject]');
137 $me = DBA::selectFirst('contact', [], ['uid' => local_user(), 'self' => true]);
138 if (!DBA::isResult($me)) {
142 $contact = DBA::selectFirst('contact', [], ['id' => $recipient, 'uid' => local_user()]);
143 if (!DBA::isResult($contact)) {
147 Photo::setPermissionFromBody($body, local_user(), $me['id'], '<' . $contact['id'] . '>', '', '', '');
149 $guid = System::createUUID();
150 $uri = Item::newURI(local_user(), $guid);
155 // look for any existing conversation structure
157 if (strlen($replyto)) {
159 $condition = ["`uid` = ? AND (`uri` = ? OR `parent-uri` = ?)",
160 local_user(), $replyto, $replyto];
161 $mail = DBA::selectFirst('mail', ['convid'], $condition);
162 if (DBA::isResult($mail)) {
163 $convid = $mail['convid'];
169 // create a new conversation
170 $recip_host = substr($contact['url'], strpos($contact['url'], '://') + 3);
171 $recip_host = substr($recip_host, 0, strpos($recip_host, '/'));
173 $recip_handle = (($contact['addr']) ? $contact['addr'] : $contact['nick'] . '@' . $recip_host);
174 $sender_handle = $a->user['nickname'] . '@' . substr(DI::baseUrl(), strpos(DI::baseUrl(), '://') + 3);
176 $conv_guid = System::createUUID();
177 $convuri = $recip_handle . ':' . $conv_guid;
179 $handles = $recip_handle . ';' . $sender_handle;
181 $fields = ['uid' => local_user(), 'guid' => $conv_guid, 'creator' => $sender_handle,
182 'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
183 'subject' => $subject, 'recips' => $handles];
184 if (DBA::insert('conv', $fields)) {
185 $convid = DBA::lastInsertId();
190 Logger::log('send message: conversation not found.');
194 if (!strlen($replyto)) {
199 $success = DBA::insert(
202 'uid' => local_user(),
205 'from-name' => $me['name'],
206 'from-photo' => $me['thumb'],
207 'from-url' => $me['url'],
208 'contact-id' => $recipient,
215 'parent-uri' => $replyto,
216 'created' => DateTimeFormat::utcNow()
221 $post_id = DBA::lastInsertId();
226 * When a photo was uploaded into the message using the (profile wall) ajax
227 * uploader, The permissions are initially set to disallow anybody but the
228 * owner from seeing it. This is because the permissions may not yet have been
229 * set for the post. If it's private, the photo permissions should be set
230 * appropriately. But we didn't know the final permissions on the post until
231 * now. So now we'll look for links of uploaded messages that are in the
232 * post and set them to the same permissions as the post itself.
236 if (preg_match_all("/\[img\](.*?)\[\/img\]/", $body, $match)) {
238 if (count($images)) {
239 foreach ($images as $image) {
240 $image_rid = Photo::ridFromURI($image);
241 if (!empty($image_rid)) {
242 Photo::update(['allow-cid' => '<' . $recipient . '>'], ['resource-id' => $image_rid, 'album' => 'Wall Photos', 'uid' => local_user()]);
249 Worker::add(PRIORITY_HIGH, "Notifier", Delivery::MAIL, $post_id);
250 return intval($post_id);
257 * @param array $recipient recipient, default empty
258 * @param string $body message body, default empty
259 * @param string $subject message subject, default empty
260 * @param string $replyto reply to, default empty
262 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
263 * @throws \ImagickException
265 public static function sendWall(array $recipient = [], $body = '', $subject = '', $replyto = '')
271 if (!strlen($subject)) {
272 $subject = DI::l10n()->t('[no subject]');
275 $guid = System::createUUID();
276 $uri = Item::newURI(local_user(), $guid);
278 $me = Contact::getByURL($replyto);
283 $conv_guid = System::createUUID();
285 $recip_handle = $recipient['nickname'] . '@' . substr(DI::baseUrl(), strpos(DI::baseUrl(), '://') + 3);
287 $sender_handle = $me['addr'];
289 $handles = $recip_handle . ';' . $sender_handle;
292 $fields = ['uid' => $recipient['uid'], 'guid' => $conv_guid, 'creator' => $sender_handle,
293 'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
294 'subject' => $subject, 'recips' => $handles];
295 if (DBA::insert('conv', $fields)) {
296 $convid = DBA::lastInsertId();
300 Logger::log('send message: conversation not found.');
307 'uid' => $recipient['uid'],
310 'from-name' => $me['name'],
311 'from-photo' => $me['photo'],
312 'from-url' => $me['url'],
320 'parent-uri' => $me['url'],
321 'created' => DateTimeFormat::utcNow(),