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