]> git.mxchange.org Git - friendica.git/blob - src/Worker/Delivery.php
Reversed annouce check to have priority for AP
[friendica.git] / src / Worker / Delivery.php
1 <?php
2 /**
3  * @file src/Worker/Delivery.php
4  */
5 namespace Friendica\Worker;
6
7 use Friendica\BaseObject;
8 use Friendica\Core\Config;
9 use Friendica\Core\L10n;
10 use Friendica\Core\Logger;
11 use Friendica\Core\Protocol;
12 use Friendica\Core\System;
13 use Friendica\Database\DBA;
14 use Friendica\Model;
15 use Friendica\Protocol\DFRN;
16 use Friendica\Protocol\Diaspora;
17 use Friendica\Protocol\Email;
18 use Friendica\Protocol\Activity;
19 use Friendica\Util\Strings;
20 use Friendica\Util\Network;
21 use Friendica\Core\Worker;
22
23 class Delivery extends BaseObject
24 {
25         const MAIL          = 'mail';
26         const SUGGESTION    = 'suggest';
27         const RELOCATION    = 'relocate';
28         const DELETION      = 'drop';
29         const POST          = 'wall-new';
30         const POKE          = 'poke';
31         const UPLINK        = 'uplink';
32         const REMOVAL       = 'removeme';
33         const PROFILEUPDATE = 'profileupdate';
34
35         public static function execute($cmd, $target_id, $contact_id)
36         {
37                 Logger::info('Invoked', ['cmd' => $cmd, 'target' => $target_id, 'contact' => $contact_id]);
38
39                 $top_level = false;
40                 $followup = false;
41                 $public_message = false;
42
43                 $items = [];
44                 if ($cmd == self::MAIL) {
45                         $target_item = DBA::selectFirst('mail', [], ['id' => $target_id]);
46                         if (!DBA::isResult($target_item)) {
47                                 self::setFailedQueue($cmd, $target_id);
48                                 return;
49                         }
50                         $uid = $target_item['uid'];
51                 } elseif ($cmd == self::SUGGESTION) {
52                         $target_item = DBA::selectFirst('fsuggest', [], ['id' => $target_id]);
53                         if (!DBA::isResult($target_item)) {
54                                 self::setFailedQueue($cmd, $target_id);
55                                 return;
56                         }
57                         $uid = $target_item['uid'];
58                 } elseif ($cmd == self::RELOCATION) {
59                         $uid = $target_id;
60                         $target_item = [];
61                 } else {
62                         $item = Model\Item::selectFirst(['parent'], ['id' => $target_id]);
63                         if (!DBA::isResult($item) || empty($item['parent'])) {
64                                 self::setFailedQueue($cmd, $target_id);
65                                 return;
66                         }
67                         $parent_id = intval($item['parent']);
68
69                         $condition = ['id' => [$target_id, $parent_id], 'visible' => true, 'moderated' => false];
70                         $params = ['order' => ['id']];
71                         $itemdata = Model\Item::select([], $condition, $params);
72
73                         while ($item = Model\Item::fetch($itemdata)) {
74                                 if ($item['id'] == $parent_id) {
75                                         $parent = $item;
76                                 }
77                                 if ($item['id'] == $target_id) {
78                                         $target_item = $item;
79                                 }
80                                 $items[] = $item;
81                         }
82                         DBA::close($itemdata);
83
84                         if (empty($target_item)) {
85                                 Logger::log('Item ' . $target_id . "wasn't found. Quitting here.");
86                                 self::setFailedQueue($cmd, $target_id);
87                                 return;
88                         }
89
90                         if (empty($parent)) {
91                                 Logger::log('Parent ' . $parent_id . ' for item ' . $target_id . "wasn't found. Quitting here.");
92                                 self::setFailedQueue($cmd, $target_id);
93                                 return;
94                         }
95
96                         if (!empty($target_item['contact-uid'])) {
97                                 $uid = $target_item['contact-uid'];
98                         } elseif (!empty($target_item['uid'])) {
99                                 $uid = $target_item['uid'];
100                         } else {
101                                 Logger::log('Only public users for item ' . $target_id, Logger::DEBUG);
102                                 self::setFailedQueue($cmd, $target_id);
103                                 return;
104                         }
105
106                         $condition = ['uri' => $target_item['thr-parent'], 'uid' => $target_item['uid']];
107                         $thr_parent = Model\Item::selectFirst(['network', 'object'], $condition);
108                         if (!DBA::isResult($thr_parent)) {
109                                 // Shouldn't happen. But when this does, we just take the parent as thread parent.
110                                 // That's totally okay for what we use this variable here.
111                                 $thr_parent = $parent;
112                         }
113
114                         if (!empty($contact_id) && Model\Contact::isArchived($contact_id)) {
115                                 Logger::info('Contact is archived', ['id' => $contact_id, 'cmd' => $cmd, 'item' => $target_item['id']]);
116                                 self::setFailedQueue($cmd, $target_id);
117                                 return;
118                         }
119
120                         // avoid race condition with deleting entries
121                         if ($items[0]['deleted']) {
122                                 foreach ($items as $item) {
123                                         $item['deleted'] = 1;
124                                 }
125                         }
126
127                         // When commenting too fast after delivery, a post wasn't recognized as top level post.
128                         // The count then showed more than one entry. The additional check should help.
129                         // The check for the "count" should be superfluous, but I'm not totally sure by now, so we keep it.
130                         if ((($parent['id'] == $target_id) || (count($items) == 1)) && ($parent['uri'] === $parent['parent-uri'])) {
131                                 Logger::log('Top level post');
132                                 $top_level = true;
133                         }
134
135                         // This is IMPORTANT!!!!
136
137                         // We will only send a "notify owner to relay" or followup message if the referenced post
138                         // originated on our system by virtue of having our hostname somewhere
139                         // in the URI, AND it was a comment (not top_level) AND the parent originated elsewhere.
140                         // if $parent['wall'] == 1 we will already have the parent message in our array
141                         // and we will relay the whole lot.
142
143                         $localhost = self::getApp()->getHostName();
144                         if (strpos($localhost, ':')) {
145                                 $localhost = substr($localhost, 0, strpos($localhost, ':'));
146                         }
147                         /**
148                          *
149                          * Be VERY CAREFUL if you make any changes to the following line. Seemingly innocuous changes
150                          * have been known to cause runaway conditions which affected several servers, along with
151                          * permissions issues.
152                          *
153                          */
154
155                         if (!$top_level && ($parent['wall'] == 0) && stristr($target_item['uri'], $localhost)) {
156                                 Logger::log('Followup ' . $target_item["guid"], Logger::DEBUG);
157                                 // local followup to remote post
158                                 $followup = true;
159                         }
160
161                         if (empty($parent['allow_cid'])
162                                 && empty($parent['allow_gid'])
163                                 && empty($parent['deny_cid'])
164                                 && empty($parent['deny_gid'])
165                                 && !$parent["private"]) {
166                                 $public_message = true;
167                         }
168                 }
169
170                 if (empty($items)) {
171                         Logger::log('No delivery data for  ' . $cmd . ' - Item ID: ' .$target_id . ' - Contact ID: ' . $contact_id);
172                 }
173
174                 $owner = Model\User::getOwnerDataById($uid);
175                 if (!DBA::isResult($owner)) {
176                         self::setFailedQueue($cmd, $target_id);
177                         return;
178                 }
179
180                 // We don't deliver our items to blocked or pending contacts, and not to ourselves either
181                 $contact = DBA::selectFirst('contact', [],
182                         ['id' => $contact_id, 'blocked' => false, 'pending' => false, 'self' => false]
183                 );
184                 if (!DBA::isResult($contact)) {
185                         self::setFailedQueue($cmd, $target_id);
186                         return;
187                 }
188
189                 if (Network::isUrlBlocked($contact['url'])) {
190                         self::setFailedQueue($cmd, $target_id);
191                         return;
192                 }
193
194                 // Transmit via Diaspora if the thread had started as Diaspora post.
195                 // Also transmit via Diaspora if this is a direct answer to a Diaspora comment.
196                 // This is done since the uri wouldn't match (Diaspora doesn't transmit it)
197                 if (!empty($parent) && !empty($thr_parent) && in_array(Protocol::DIASPORA, [$parent['network'], $thr_parent['network']])) {
198                         $contact['network'] = Protocol::DIASPORA;
199                 }
200
201                 Logger::notice('Delivering', ['cmd' => $cmd, 'target' => $target_id, 'followup' => $followup, 'network' => $contact['network']]);
202
203                 switch ($contact['network']) {
204                         case Protocol::DFRN:
205                                 self::deliverDFRN($cmd, $contact, $owner, $items, $target_item, $public_message, $top_level, $followup);
206                                 break;
207
208                         case Protocol::DIASPORA:
209                                 self::deliverDiaspora($cmd, $contact, $owner, $items, $target_item, $public_message, $top_level, $followup);
210                                 break;
211
212                         case Protocol::MAIL:
213                                 self::deliverMail($cmd, $contact, $owner, $target_item, $thr_parent);
214                                 break;
215
216                         default:
217                                 break;
218                 }
219
220                 return;
221         }
222
223         /**
224          * Increased the "failed" counter in the item delivery data
225          *
226          * @param string  $cmd Command
227          * @param integer $id  Item id
228          */
229         private static function setFailedQueue(string $cmd, int $id)
230         {
231                 if (!in_array($cmd, [Delivery::POST, Delivery::POKE])) {
232                         return;
233                 }
234
235                 Model\ItemDeliveryData::incrementQueueFailed($id);
236         }
237
238         /**
239          * @brief Deliver content via DFRN
240          *
241          * @param string  $cmd            Command
242          * @param array   $contact        Contact record of the receiver
243          * @param array   $owner          Owner record of the sender
244          * @param array   $items          Item record of the content and the parent
245          * @param array   $target_item    Item record of the content
246          * @param boolean $public_message Is the content public?
247          * @param boolean $top_level      Is it a thread starter?
248          * @param boolean $followup       Is it an answer to a remote post?
249          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
250          * @throws \ImagickException
251          */
252         private static function deliverDFRN($cmd, $contact, $owner, $items, $target_item, $public_message, $top_level, $followup)
253         {
254                 // Skip transmitting the announce via DFRN when it had already been done via AP
255                 if (ActivityPub\Transmitter::isAnnounce($target_item) && !empty(Model\APContact::getByURL($contact['url'], false))) {
256                         Logger::info('Announce had already been transmitted', ['url' => $contact['url'], 'guid' => ($target_item['guid'] ?? '') ?: $target_item['id']]);
257                         return;
258                 }
259
260                 // Transmit Diaspora reshares via Diaspora if the Friendica contact support Diaspora
261                 if (Diaspora::isReshare($target_item['body']) && !empty(Diaspora::personByHandle(contact['addr'], false))) {
262                         Logger::info('Reshare will be transmitted via Diaspora', ['url' => $contact['url'], 'guid' => ($target_item['guid'] ?? '') ?: $target_item['id']]);
263                         self::deliverDiaspora($cmd, $contact, $owner, $items, $target_item, $public_message, $top_level, $followup);
264                         return;
265                 }
266
267                 Logger::info('Deliver ' . (($target_item['guid'] ?? '') ?: $target_item['id']) . ' via DFRN to ' . (($contact['addr'] ?? '') ?: $contact['url']));
268
269                 if ($cmd == self::MAIL) {
270                         $item = $target_item;
271                         $item['body'] = Model\Item::fixPrivatePhotos($item['body'], $owner['uid'], null, $item['contact-id']);
272                         $atom = DFRN::mail($item, $owner);
273                 } elseif ($cmd == self::SUGGESTION) {
274                         $item = $target_item;
275                         $atom = DFRN::fsuggest($item, $owner);
276                         DBA::delete('fsuggest', ['id' => $item['id']]);
277                 } elseif ($cmd == self::RELOCATION) {
278                         $atom = DFRN::relocate($owner, $owner['uid']);
279                 } elseif ($followup) {
280                         $msgitems = [$target_item];
281                         $atom = DFRN::entries($msgitems, $owner);
282                 } else {
283                         $msgitems = [];
284                         foreach ($items as $item) {
285                                 // Only add the parent when we don't delete other items.
286                                 if (($target_item['id'] == $item['id']) || ($cmd != self::DELETION)) {
287                                         $item["entry:comment-allow"] = true;
288                                         $item["entry:cid"] = ($top_level ? $contact['id'] : 0);
289                                         $msgitems[] = $item;
290                                 }
291                         }
292                         $atom = DFRN::entries($msgitems, $owner);
293                 }
294
295                 Logger::debug('Notifier entry: ' . $contact["url"] . ' ' . (($target_item['guid'] ?? '') ?: $target_item['id']) . ' entry: ' . $atom);
296
297                 $basepath =  implode('/', array_slice(explode('/', $contact['url']), 0, 3));
298
299                 // perform local delivery if we are on the same site
300
301                 if (Strings::compareLink($basepath, System::baseUrl())) {
302                         $condition = ['nurl' => Strings::normaliseLink($contact['url']), 'self' => true];
303                         $target_self = DBA::selectFirst('contact', ['uid'], $condition);
304                         if (!DBA::isResult($target_self)) {
305                                 return;
306                         }
307                         $target_uid = $target_self['uid'];
308
309                         // Check if the user has got this contact
310                         $cid = Model\Contact::getIdForURL($owner['url'], $target_uid);
311                         if (!$cid) {
312                                 // Otherwise there should be a public contact
313                                 $cid = Model\Contact::getIdForURL($owner['url']);
314                                 if (!$cid) {
315                                         return;
316                                 }
317                         }
318
319                         $target_importer = DFRN::getImporter($cid, $target_uid);
320                         if (empty($target_importer)) {
321                                 // This should never happen
322                                 return;
323                         }
324
325                         DFRN::import($atom, $target_importer);
326
327                         if (in_array($cmd, [Delivery::POST, Delivery::POKE])) {
328                                 Model\ItemDeliveryData::incrementQueueDone($target_item['id'], Model\ItemDeliveryData::DFRN);
329                         }
330
331                         return;
332                 }
333
334                 $protocol = Model\ItemDeliveryData::DFRN;
335
336                 // We don't have a relationship with contacts on a public post.
337                 // Se we transmit with the new method and via Diaspora as a fallback
338                 if (!empty($items) && (($items[0]['uid'] == 0) || ($contact['uid'] == 0))) {
339                         // Transmit in public if it's a relay post
340                         $public_dfrn = ($contact['contact-type'] == Model\Contact::TYPE_RELAY);
341
342                         $deliver_status = DFRN::transmit($owner, $contact, $atom, $public_dfrn);
343
344                         // We never spool failed relay deliveries
345                         if ($public_dfrn) {
346                                 Logger::info('Relay delivery to ' . $contact["url"] . ' with guid ' . $target_item["guid"] . ' returns ' . $deliver_status);
347
348                                 if (in_array($cmd, [Delivery::POST, Delivery::POKE])) {
349                                         if (($deliver_status >= 200) && ($deliver_status <= 299)) {
350                                                 Model\ItemDeliveryData::incrementQueueDone($target_item['id'], $protocol);
351                                         } else {
352                                                 Model\ItemDeliveryData::incrementQueueFailed($target_item['id']);
353                                         }
354                                 }
355                                 return;
356                         }
357
358                         if (($deliver_status < 200) || ($deliver_status > 299)) {
359                                 // Transmit via Diaspora if not possible via Friendica
360                                 self::deliverDiaspora($cmd, $contact, $owner, $items, $target_item, $public_message, $top_level, $followup);
361                                 return;
362                         }
363                 } elseif ($cmd != self::RELOCATION) {
364                         // DFRN payload over Diaspora transport layer
365                         $deliver_status = DFRN::transmit($owner, $contact, $atom);
366                         if ($deliver_status < 200) {
367                                 // Legacy DFRN
368                                 $deliver_status = DFRN::deliver($owner, $contact, $atom);
369                                 $protocol = Model\ItemDeliveryData::LEGACY_DFRN;
370                         }
371                 } else {
372                         $deliver_status = DFRN::deliver($owner, $contact, $atom);
373                         $protocol = Model\ItemDeliveryData::LEGACY_DFRN;
374                 }
375
376                 Logger::info('DFRN Delivery', ['cmd' => $cmd, 'url' => $contact['url'], 'guid' => ($target_item['guid'] ?? '') ?: $target_item['id'], 'return' => $deliver_status]);
377
378                 if (($deliver_status >= 200) && ($deliver_status <= 299)) {
379                         // We successfully delivered a message, the contact is alive
380                         Model\Contact::unmarkForArchival($contact);
381
382                         if (in_array($cmd, [Delivery::POST, Delivery::POKE])) {
383                                 Model\ItemDeliveryData::incrementQueueDone($target_item['id'], $protocol);
384                         }
385                 } else {
386                         // The message could not be delivered. We mark the contact as "dead"
387                         Model\Contact::markForArchival($contact);
388
389                         Logger::info('Delivery failed: defer message', ['id' => ($target_item['guid'] ?? '') ?: $target_item['id']]);
390                         if (!Worker::defer() && in_array($cmd, [Delivery::POST, Delivery::POKE])) {
391                                 Model\ItemDeliveryData::incrementQueueFailed($target_item['id']);
392                         }
393                 }
394         }
395
396         /**
397          * @brief Deliver content via Diaspora
398          *
399          * @param string  $cmd            Command
400          * @param array   $contact        Contact record of the receiver
401          * @param array   $owner          Owner record of the sender
402          * @param array   $items          Item record of the content and the parent
403          * @param array   $target_item    Item record of the content
404          * @param boolean $public_message Is the content public?
405          * @param boolean $top_level      Is it a thread starter?
406          * @param boolean $followup       Is it an answer to a remote post?
407          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
408          * @throws \ImagickException
409          */
410         private static function deliverDiaspora($cmd, $contact, $owner, $items, $target_item, $public_message, $top_level, $followup)
411         {
412                 // We don't treat Forum posts as "wall-to-wall" to be able to post them via Diaspora
413                 $walltowall = $top_level && ($owner['id'] != $items[0]['contact-id']) & ($owner['account-type'] != Model\User::ACCOUNT_TYPE_COMMUNITY);
414
415                 if ($public_message) {
416                         $loc = 'public batch ' . $contact['batch'];
417                 } else {
418                         $loc = $contact['addr'];
419                 }
420
421                 Logger::notice('Deliver via Diaspora', ['target' => $target_item['id'], 'guid' => $target_item['guid'], 'to' => $loc]);
422
423                 if (Config::get('system', 'dfrn_only') || !Config::get('system', 'diaspora_enabled')) {
424                         return;
425                 }
426
427                 if ($cmd == self::MAIL) {
428                         Diaspora::sendMail($target_item, $owner, $contact);
429                         return;
430                 }
431
432                 if ($cmd == self::SUGGESTION) {
433                         return;
434                 }
435
436                 if (!$contact['pubkey'] && !$public_message) {
437                         return;
438                 }
439
440                 if ($cmd == self::RELOCATION) {
441                         $deliver_status = Diaspora::sendAccountMigration($owner, $contact, $owner['uid']);
442                 } elseif ($target_item['deleted'] && (($target_item['uri'] === $target_item['parent-uri']) || $followup)) {
443                         // top-level retraction
444                         Logger::log('diaspora retract: ' . $loc);
445                         $deliver_status = Diaspora::sendRetraction($target_item, $owner, $contact, $public_message);
446                 } elseif ($followup) {
447                         // send comments and likes to owner to relay
448                         Logger::log('diaspora followup: ' . $loc);
449                         $deliver_status = Diaspora::sendFollowup($target_item, $owner, $contact, $public_message);
450                 } elseif ($target_item['uri'] !== $target_item['parent-uri']) {
451                         // we are the relay - send comments, likes and relayable_retractions to our conversants
452                         Logger::log('diaspora relay: ' . $loc);
453                         $deliver_status = Diaspora::sendRelay($target_item, $owner, $contact, $public_message);
454                 } elseif ($top_level && !$walltowall) {
455                         // currently no workable solution for sending walltowall
456                         Logger::log('diaspora status: ' . $loc);
457                         $deliver_status = Diaspora::sendStatus($target_item, $owner, $contact, $public_message);
458                 } else {
459                         Logger::log('Unknown mode ' . $cmd . ' for ' . $loc);
460                         return;
461                 }
462
463                 if (($deliver_status >= 200) && ($deliver_status <= 299)) {
464                         // We successfully delivered a message, the contact is alive
465                         Model\Contact::unmarkForArchival($contact);
466
467                         if (in_array($cmd, [Delivery::POST, Delivery::POKE])) {
468                                 Model\ItemDeliveryData::incrementQueueDone($target_item['id'], Model\ItemDeliveryData::DIASPORA);
469                         }
470                 } else {
471                         // The message could not be delivered. We mark the contact as "dead"
472                         Model\Contact::markForArchival($contact);
473
474                         // When it is delivered to the public endpoint, we do mark the relay contact for archival as well
475                         if ($public_message) {
476                                 Diaspora::markRelayForArchival($contact);
477                         }
478
479                         if (empty($contact['contact-type']) || ($contact['contact-type'] != Model\Contact::TYPE_RELAY)) {
480                                 Logger::info('Delivery failed: defer message', ['id' => ($target_item['guid'] ?? '') ?: $target_item['id']]);
481                                 // defer message for redelivery
482                                 if (!Worker::defer() && in_array($cmd, [Delivery::POST, Delivery::POKE])) {
483                                         Model\ItemDeliveryData::incrementQueueFailed($target_item['id']);
484                                 }
485                         } elseif (in_array($cmd, [Delivery::POST, Delivery::POKE])) {
486                                 Model\ItemDeliveryData::incrementQueueFailed($target_item['id']);
487                         }
488                 }
489         }
490
491         /**
492          * @brief Deliver content via mail
493          *
494          * @param string $cmd         Command
495          * @param array  $contact     Contact record of the receiver
496          * @param array  $owner       Owner record of the sender
497          * @param array  $target_item Item record of the content
498          * @param array  $thr_parent  Item record of the direct parent in the thread
499          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
500          * @throws \ImagickException
501          */
502         private static function deliverMail($cmd, $contact, $owner, $target_item, $thr_parent)
503         {
504                 if (Config::get('system','dfrn_only')) {
505                         return;
506                 }
507
508                 $addr = $contact['addr'];
509                 if (!strlen($addr)) {
510                         return;
511                 }
512
513                 if (!in_array($cmd, [self::POST, self::POKE])) {
514                         return;
515                 }
516
517                 if ($target_item['verb'] != Activity::POST) {
518                         return;
519                 }
520
521                 if (!empty($thr_parent['object'])) {
522                         $data = json_decode($thr_parent['object'], true);
523                         if (!empty($data['reply_to'])) {
524                                 $addr = $data['reply_to'][0]['mailbox'] . '@' . $data['reply_to'][0]['host'];
525                                 Logger::info('Use "reply-to" address of the thread parent', ['addr' => $addr]);
526                         } elseif (!empty($data['from'])) {
527                                 $addr = $data['from'][0]['mailbox'] . '@' . $data['from'][0]['host'];
528                                 Logger::info('Use "from" address of the thread parent', ['addr' => $addr]);
529                         }
530                 }
531
532                 $local_user = DBA::selectFirst('user', [], ['uid' => $owner['uid']]);
533                 if (!DBA::isResult($local_user)) {
534                         return;
535                 }
536
537                 Logger::info('About to deliver via mail', ['guid' => $target_item['guid'], 'to' => $addr]);
538
539                 $reply_to = '';
540                 $mailacct = DBA::selectFirst('mailacct', ['reply_to'], ['uid' => $owner['uid']]);
541                 if (DBA::isResult($mailacct) && !empty($mailacct['reply_to'])) {
542                         $reply_to = $mailacct['reply_to'];
543                 }
544
545                 $subject  = ($target_item['title'] ? Email::encodeHeader($target_item['title'], 'UTF-8') : L10n::t("\x28no subject\x29"));
546
547                 // only expose our real email address to true friends
548
549                 if (($contact['rel'] == Model\Contact::FRIEND) && !$contact['blocked']) {
550                         if ($reply_to) {
551                                 $headers  = 'From: ' . Email::encodeHeader($local_user['username'],'UTF-8') . ' <' . $reply_to . '>' . "\n";
552                                 $headers .= 'Sender: ' . $local_user['email'] . "\n";
553                         } else {
554                                 $headers  = 'From: ' . Email::encodeHeader($local_user['username'],'UTF-8') . ' <' . $local_user['email'] . '>' . "\n";
555                         }
556                 } else {
557                         $headers  = 'From: '. Email::encodeHeader($local_user['username'], 'UTF-8') . ' <noreply@' . self::getApp()->getHostName() . '>' . "\n";
558                 }
559
560                 $headers .= 'Message-Id: <' . Email::iri2msgid($target_item['uri']) . '>' . "\n";
561
562                 if ($target_item['uri'] !== $target_item['parent-uri']) {
563                         $headers .= 'References: <' . Email::iri2msgid($target_item['parent-uri']) . '>';
564
565                         // Export more references on deeper nested threads
566                         if (($target_item['thr-parent'] != '') && ($target_item['thr-parent'] != $target_item['parent-uri'])) {
567                                 $headers .= ' <' . Email::iri2msgid($target_item['thr-parent']) . '>';
568                         }
569
570                         $headers .= "\n";
571
572                         if (empty($target_item['title'])) {
573                                 $condition = ['uri' => $target_item['parent-uri'], 'uid' => $owner['uid']];
574                                 $title = Model\Item::selectFirst(['title'], $condition);
575
576                                 if (DBA::isResult($title) && ($title['title'] != '')) {
577                                         $subject = $title['title'];
578                                 } else {
579                                         $condition = ['parent-uri' => $target_item['parent-uri'], 'uid' => $owner['uid']];
580                                         $title = Model\Item::selectFirst(['title'], $condition);
581
582                                         if (DBA::isResult($title) && ($title['title'] != '')) {
583                                                 $subject = $title['title'];
584                                         }
585                                 }
586                         }
587
588                         if (strncasecmp($subject, 'RE:', 3)) {
589                                 $subject = 'Re: ' . $subject;
590                         }
591                 }
592
593                 Email::send($addr, $subject, $headers, $target_item);
594
595                 Model\ItemDeliveryData::incrementQueueDone($target_item['id'], Model\ItemDeliveryData::MAIL);
596
597                 Logger::info('Delivered via mail', ['guid' => $target_item['guid'], 'to' => $addr, 'subject' => $subject]);
598         }
599 }