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\Protocol\Activity;
31 use Friendica\Util\DateTimeFormat;
32 use Friendica\Worker\Delivery;
35 * Class to handle private messages
40 * Insert received private message
43 * @return int|boolean Message ID or false on error
45 public static function insert($msg)
47 $user = User::getById($msg['uid']);
49 if (!isset($msg['reply'])) {
50 $msg['reply'] = DBA::exists('mail', ['parent-uri' => $msg['parent-uri']]);
53 if (empty($msg['convid'])) {
54 $mail = DBA::selectFirst('mail', ['convid'], ["`convid` != 0 AND `parent-uri` = ?", $msg['parent-uri']]);
55 if (DBA::isResult($mail)) {
56 $msg['convid'] = $mail['convid'];
60 if (empty($msg['guid'])) {
61 $host = parse_url($msg['from-url'], PHP_URL_HOST);
62 $msg['guid'] = Item::guidFromUri($msg['uri'], $host);
65 $msg['created'] = (!empty($msg['created']) ? DateTimeFormat::utc($msg['created']) : DateTimeFormat::utcNow());
69 if (DBA::exists('mail', ['uri' => $msg['uri'], 'uid' => $msg['uid']])) {
71 Logger::info('duplicate message already delivered.');
75 DBA::insert('mail', $msg);
77 $msg['id'] = DBA::lastInsertId();
81 if (!empty($msg['convid'])) {
82 DBA::update('conv', ['updated' => DateTimeFormat::utcNow()], ['id' => $msg['convid']]);
85 // send notifications.
88 'otype' => Notify\ObjectType::MAIL,
89 'verb' => Activity::POST,
90 'uid' => $user['uid'],
91 'cid' => $msg['contact-id'],
92 'link' => DI::baseUrl() . '/message/' . $msg['id'],
95 notification($notif_params);
97 Logger::info('Mail is processed, notification was sent.', ['id' => $msg['id'], 'uri' => $msg['uri']]);
103 * Send private message
105 * @param integer $recipient recipient id, default 0
106 * @param string $body message body, default empty
107 * @param string $subject message subject, default empty
108 * @param string $replyto reply to
110 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
112 public static function send($recipient = 0, $body = '', $subject = '', $replyto = '')
120 if (!strlen($subject)) {
121 $subject = DI::l10n()->t('[no subject]');
124 $me = DBA::selectFirst('contact', [], ['uid' => local_user(), 'self' => true]);
125 if (!DBA::isResult($me)) {
129 $contact = DBA::selectFirst('contact', [], ['id' => $recipient, 'uid' => local_user()]);
130 if (!DBA::isResult($contact)) {
134 Photo::setPermissionFromBody($body, local_user(), $me['id'], '<' . $contact['id'] . '>', '', '', '');
136 $guid = System::createUUID();
137 $uri = Item::newURI(local_user(), $guid);
142 // look for any existing conversation structure
144 if (strlen($replyto)) {
146 $condition = ["`uid` = ? AND (`uri` = ? OR `parent-uri` = ?)",
147 local_user(), $replyto, $replyto];
148 $mail = DBA::selectFirst('mail', ['convid'], $condition);
149 if (DBA::isResult($mail)) {
150 $convid = $mail['convid'];
156 // create a new conversation
157 $recip_host = substr($contact['url'], strpos($contact['url'], '://') + 3);
158 $recip_host = substr($recip_host, 0, strpos($recip_host, '/'));
160 $recip_handle = (($contact['addr']) ? $contact['addr'] : $contact['nick'] . '@' . $recip_host);
161 $sender_handle = $a->user['nickname'] . '@' . substr(DI::baseUrl(), strpos(DI::baseUrl(), '://') + 3);
163 $conv_guid = System::createUUID();
164 $convuri = $recip_handle . ':' . $conv_guid;
166 $handles = $recip_handle . ';' . $sender_handle;
168 $fields = ['uid' => local_user(), 'guid' => $conv_guid, 'creator' => $sender_handle,
169 'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
170 'subject' => $subject, 'recips' => $handles];
171 if (DBA::insert('conv', $fields)) {
172 $convid = DBA::lastInsertId();
177 Logger::log('send message: conversation not found.');
181 if (!strlen($replyto)) {
186 $success = DBA::insert(
189 'uid' => local_user(),
192 'from-name' => $me['name'],
193 'from-photo' => $me['thumb'],
194 'from-url' => $me['url'],
195 'contact-id' => $recipient,
202 'parent-uri' => $replyto,
203 'created' => DateTimeFormat::utcNow()
208 $post_id = DBA::lastInsertId();
213 * When a photo was uploaded into the message using the (profile wall) ajax
214 * uploader, The permissions are initially set to disallow anybody but the
215 * owner from seeing it. This is because the permissions may not yet have been
216 * set for the post. If it's private, the photo permissions should be set
217 * appropriately. But we didn't know the final permissions on the post until
218 * now. So now we'll look for links of uploaded messages that are in the
219 * post and set them to the same permissions as the post itself.
223 if (preg_match_all("/\[img\](.*?)\[\/img\]/", $body, $match)) {
225 if (count($images)) {
226 foreach ($images as $image) {
227 $image_rid = Photo::ridFromURI($image);
228 if (!empty($image_rid)) {
229 Photo::update(['allow-cid' => '<' . $recipient . '>'], ['resource-id' => $image_rid, 'album' => 'Wall Photos', 'uid' => local_user()]);
236 Worker::add(PRIORITY_HIGH, "Notifier", Delivery::MAIL, $post_id);
237 return intval($post_id);
244 * @param array $recipient recipient, default empty
245 * @param string $body message body, default empty
246 * @param string $subject message subject, default empty
247 * @param string $replyto reply to, default empty
249 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
250 * @throws \ImagickException
252 public static function sendWall(array $recipient = [], $body = '', $subject = '', $replyto = '')
258 if (!strlen($subject)) {
259 $subject = DI::l10n()->t('[no subject]');
262 $guid = System::createUUID();
263 $uri = Item::newURI(local_user(), $guid);
265 $me = Contact::getByURL($replyto);
270 $conv_guid = System::createUUID();
272 $recip_handle = $recipient['nickname'] . '@' . substr(DI::baseUrl(), strpos(DI::baseUrl(), '://') + 3);
274 $sender_handle = $me['addr'];
276 $handles = $recip_handle . ';' . $sender_handle;
279 $fields = ['uid' => $recipient['uid'], 'guid' => $conv_guid, 'creator' => $sender_handle,
280 'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
281 'subject' => $subject, 'recips' => $handles];
282 if (DBA::insert('conv', $fields)) {
283 $convid = DBA::lastInsertId();
287 Logger::log('send message: conversation not found.');
294 'uid' => $recipient['uid'],
297 'from-name' => $me['name'],
298 'from-photo' => $me['photo'],
299 'from-url' => $me['url'],
307 'parent-uri' => $me['url'],
308 'created' => DateTimeFormat::utcNow(),