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;
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 DBA::insert('mail', $msg);
76 $msg['id'] = DBA::lastInsertId();
80 if (!empty($msg['convid'])) {
81 DBA::update('conv', ['updated' => DateTimeFormat::utcNow()], ['id' => $msg['convid']]);
84 // send notifications.
86 'type' => Notification\Type::MAIL,
87 'otype' => Notification\ObjectType::MAIL,
88 'verb' => Activity::POST,
89 'uid' => $user['uid'],
90 'cid' => $msg['contact-id'],
91 'link' => DI::baseUrl() . '/message/' . $msg['id'],
94 notification($notif_params);
96 Logger::info('Mail is processed, notification was sent.', ['id' => $msg['id'], 'uri' => $msg['uri']]);
102 * Send private message
104 * @param integer $recipient recipient id, default 0
105 * @param string $body message body, default empty
106 * @param string $subject message subject, default empty
107 * @param string $replyto reply to
109 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
111 public static function send($recipient = 0, $body = '', $subject = '', $replyto = '')
119 if (!strlen($subject)) {
120 $subject = DI::l10n()->t('[no subject]');
123 $me = DBA::selectFirst('contact', [], ['uid' => local_user(), 'self' => true]);
124 if (!DBA::isResult($me)) {
128 $contact = DBA::selectFirst('contact', [], ['id' => $recipient, 'uid' => local_user()]);
129 if (!DBA::isResult($contact)) {
133 Photo::setPermissionFromBody($body, local_user(), $me['id'], '<' . $contact['id'] . '>', '', '', '');
135 $guid = System::createUUID();
136 $uri = Item::newURI(local_user(), $guid);
141 // look for any existing conversation structure
143 if (strlen($replyto)) {
145 $condition = ["`uid` = ? AND (`uri` = ? OR `parent-uri` = ?)",
146 local_user(), $replyto, $replyto];
147 $mail = DBA::selectFirst('mail', ['convid'], $condition);
148 if (DBA::isResult($mail)) {
149 $convid = $mail['convid'];
155 // create a new conversation
156 $recip_host = substr($contact['url'], strpos($contact['url'], '://') + 3);
157 $recip_host = substr($recip_host, 0, strpos($recip_host, '/'));
159 $recip_handle = (($contact['addr']) ? $contact['addr'] : $contact['nick'] . '@' . $recip_host);
160 $sender_handle = $a->user['nickname'] . '@' . substr(DI::baseUrl(), strpos(DI::baseUrl(), '://') + 3);
162 $conv_guid = System::createUUID();
163 $convuri = $recip_handle . ':' . $conv_guid;
165 $handles = $recip_handle . ';' . $sender_handle;
167 $fields = ['uid' => local_user(), 'guid' => $conv_guid, 'creator' => $sender_handle,
168 'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
169 'subject' => $subject, 'recips' => $handles];
170 if (DBA::insert('conv', $fields)) {
171 $convid = DBA::lastInsertId();
176 Logger::log('send message: conversation not found.');
180 if (!strlen($replyto)) {
185 $success = DBA::insert(
188 'uid' => local_user(),
191 'from-name' => $me['name'],
192 'from-photo' => $me['thumb'],
193 'from-url' => $me['url'],
194 'contact-id' => $recipient,
201 'parent-uri' => $replyto,
202 'created' => DateTimeFormat::utcNow()
207 $post_id = DBA::lastInsertId();
212 * When a photo was uploaded into the message using the (profile wall) ajax
213 * uploader, The permissions are initially set to disallow anybody but the
214 * owner from seeing it. This is because the permissions may not yet have been
215 * set for the post. If it's private, the photo permissions should be set
216 * appropriately. But we didn't know the final permissions on the post until
217 * now. So now we'll look for links of uploaded messages that are in the
218 * post and set them to the same permissions as the post itself.
222 if (preg_match_all("/\[img\](.*?)\[\/img\]/", $body, $match)) {
224 if (count($images)) {
225 foreach ($images as $image) {
226 $image_rid = Photo::ridFromURI($image);
227 if (!empty($image_rid)) {
228 Photo::update(['allow-cid' => '<' . $recipient . '>'], ['resource-id' => $image_rid, 'album' => 'Wall Photos', 'uid' => local_user()]);
235 Worker::add(PRIORITY_HIGH, "Notifier", Delivery::MAIL, $post_id);
236 return intval($post_id);
243 * @param array $recipient recipient, default empty
244 * @param string $body message body, default empty
245 * @param string $subject message subject, default empty
246 * @param string $replyto reply to, default empty
248 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
249 * @throws \ImagickException
251 public static function sendWall(array $recipient = [], $body = '', $subject = '', $replyto = '')
257 if (!strlen($subject)) {
258 $subject = DI::l10n()->t('[no subject]');
261 $guid = System::createUUID();
262 $uri = Item::newURI(local_user(), $guid);
264 $me = Contact::getByURL($replyto);
269 $conv_guid = System::createUUID();
271 $recip_handle = $recipient['nickname'] . '@' . substr(DI::baseUrl(), strpos(DI::baseUrl(), '://') + 3);
273 $sender_handle = $me['addr'];
275 $handles = $recip_handle . ';' . $sender_handle;
278 $fields = ['uid' => $recipient['uid'], 'guid' => $conv_guid, 'creator' => $sender_handle,
279 'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
280 'subject' => $subject, 'recips' => $handles];
281 if (DBA::insert('conv', $fields)) {
282 $convid = DBA::lastInsertId();
286 Logger::log('send message: conversation not found.');
293 'uid' => $recipient['uid'],
296 'from-name' => $me['name'],
297 'from-photo' => $me['photo'],
298 'from-url' => $me['url'],
306 'parent-uri' => $me['url'],
307 'created' => DateTimeFormat::utcNow(),