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