3 * @copyright Copyright (C) 2010-2022, 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\ACL;
25 use Friendica\Core\Logger;
26 use Friendica\Core\System;
27 use Friendica\Core\Worker;
28 use Friendica\Database\DBA;
30 use Friendica\Protocol\Activity;
31 use Friendica\Util\DateTimeFormat;
32 use Friendica\Worker\Delivery;
35 * Class to handle private messages
40 * Insert private message
43 * @param bool $notification
44 * @return int|boolean Message ID or false on error
45 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
46 * @throws \ImagickException
48 public static function insert($msg, $notification = true)
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());
68 $msg['author-id'] = Contact::getIdForURL($msg['from-url'], 0, false);
69 $msg['uri-id'] = ItemURI::insert(['uri' => $msg['uri'], 'guid' => $msg['guid']]);
70 $msg['parent-uri-id'] = ItemURI::getIdByURI($msg['parent-uri']);
74 if (DBA::exists('mail', ['uri' => $msg['uri'], 'uid' => $msg['uid']])) {
76 Logger::info('duplicate message already delivered.');
80 if ($msg['reply'] && DBA::isResult($reply = DBA::selectFirst('mail', ['uri', 'uri-id'], ['parent-uri' => $msg['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']]);
99 $user = User::getById($msg['uid']);
100 // send notifications.
102 'type' => Notification\Type::MAIL,
103 'otype' => Notification\ObjectType::MAIL,
104 'verb' => Activity::POST,
105 'uid' => $user['uid'],
106 'cid' => $msg['contact-id'],
107 'link' => DI::baseUrl() . '/message/' . $msg['id'],
110 DI::notify()->createFromArray($notif_params);
112 Logger::info('Mail is processed, notification was sent.', ['id' => $msg['id'], 'uri' => $msg['uri']]);
119 * Send private message
121 * @param integer $recipient recipient id, default 0
122 * @param string $body message body, default empty
123 * @param string $subject message subject, default empty
124 * @param string $replyto reply to
126 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
128 public static function send($recipient = 0, $body = '', $subject = '', $replyto = '')
136 if (!strlen($subject)) {
137 $subject = DI::l10n()->t('[no subject]');
140 $me = DBA::selectFirst('contact', [], ['uid' => local_user(), 'self' => true]);
141 if (!DBA::isResult($me)) {
145 $contacts = ACL::getValidMessageRecipientsForUser(local_user());
147 $contactIndex = array_search($recipient, array_column($contacts, 'id'));
148 if ($contactIndex === false) {
152 $contact = $contacts[$contactIndex];
154 Photo::setPermissionFromBody($body, local_user(), $me['id'], '<' . $contact['id'] . '>', '', '', '');
156 $guid = System::createUUID();
157 $uri = Item::newURI(local_user(), $guid);
162 // look for any existing conversation structure
164 if (strlen($replyto)) {
166 $condition = ["`uid` = ? AND (`uri` = ? OR `parent-uri` = ?)",
167 local_user(), $replyto, $replyto];
168 $mail = DBA::selectFirst('mail', ['convid'], $condition);
169 if (DBA::isResult($mail)) {
170 $convid = $mail['convid'];
176 // create a new conversation
177 $conv_guid = System::createUUID();
178 $convuri = $contact['addr'] . ':' . $conv_guid;
180 $fields = ['uid' => local_user(), 'guid' => $conv_guid, 'creator' => $me['addr'],
181 'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
182 'subject' => $subject, 'recips' => $contact['addr'] . ';' . $me['addr']];
183 if (DBA::insert('conv', $fields)) {
184 $convid = DBA::lastInsertId();
189 Logger::notice('send message: conversation not found.');
193 if (!strlen($replyto)) {
197 $post_id = self::insert(
199 'uid' => local_user(),
202 'from-name' => $me['name'],
203 'from-photo' => $me['thumb'],
204 'from-url' => $me['url'],
205 'contact-id' => $recipient,
212 'parent-uri' => $replyto,
213 'created' => DateTimeFormat::utcNow()
219 * When a photo was uploaded into the message using the (profile wall) ajax
220 * uploader, The permissions are initially set to disallow anybody but the
221 * owner from seeing it. This is because the permissions may not yet have been
222 * set for the post. If it's private, the photo permissions should be set
223 * appropriately. But we didn't know the final permissions on the post until
224 * now. So now we'll look for links of uploaded messages that are in the
225 * post and set them to the same permissions as the post itself.
229 if (preg_match_all("/\[img\](.*?)\[\/img\]/", $body, $match)) {
231 if (count($images)) {
232 foreach ($images as $image) {
233 $image_rid = Photo::ridFromURI($image);
234 if (!empty($image_rid)) {
235 Photo::update(['allow-cid' => '<' . $recipient . '>'], ['resource-id' => $image_rid, 'album' => 'Wall Photos', 'uid' => local_user()]);
242 Worker::add(PRIORITY_HIGH, "Notifier", Delivery::MAIL, $post_id);
243 return intval($post_id);
250 * @param array $recipient recipient, default empty
251 * @param string $body message body, default empty
252 * @param string $subject message subject, default empty
253 * @param string $replyto reply to, default empty
255 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
256 * @throws \ImagickException
258 public static function sendWall(array $recipient = [], $body = '', $subject = '', $replyto = '')
264 if (!strlen($subject)) {
265 $subject = DI::l10n()->t('[no subject]');
268 $guid = System::createUUID();
269 $uri = Item::newURI(local_user(), $guid);
271 $me = Contact::getByURL($replyto);
276 $conv_guid = System::createUUID();
278 $recip_handle = $recipient['nickname'] . '@' . substr(DI::baseUrl(), strpos(DI::baseUrl(), '://') + 3);
280 $sender_handle = $me['addr'];
282 $handles = $recip_handle . ';' . $sender_handle;
285 $fields = ['uid' => $recipient['uid'], 'guid' => $conv_guid, 'creator' => $sender_handle,
286 'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
287 'subject' => $subject, 'recips' => $handles];
288 if (DBA::insert('conv', $fields)) {
289 $convid = DBA::lastInsertId();
293 Logger::notice('send message: conversation not found.');
299 'uid' => $recipient['uid'],
302 'from-name' => $me['name'],
303 'from-photo' => $me['photo'],
304 'from-url' => $me['url'],
312 'parent-uri' => $me['url'],
313 'created' => DateTimeFormat::utcNow(),