Performance improvements when displaying local posts
[friendica.git/.git] / src / Model / Post.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2024, 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\Model;
23
24 use BadMethodCallException;
25 use Friendica\Core\Logger;
26 use Friendica\Database\Database;
27 use Friendica\Database\DBA;
28 use Friendica\DI;
29 use Friendica\Protocol\Activity;
30
31 class Post
32 {
33         /**
34          * Insert a new post entry
35          *
36          * @param integer $uri_id
37          * @param array   $fields
38          * @return bool   Success of the insert process
39          * @throws \Exception
40          */
41         public static function insert(int $uri_id, array $data = []): bool
42         {
43                 if (empty($uri_id)) {
44                         throw new BadMethodCallException('Empty URI_id');
45                 }
46
47                 $fields = DI::dbaDefinition()->truncateFieldsForTable('post', $data);
48
49                 // Additionally assign the key fields
50                 $fields['uri-id'] = $uri_id;
51
52                 return DBA::insert('post', $fields, Database::INSERT_IGNORE);
53         }
54
55         /**
56          * Fetch a single post row
57          *
58          * @param mixed $stmt statement object
59          * @return array|false current row or false
60          * @throws \Exception
61          */
62         public static function fetch($stmt)
63         {
64                 $row = DBA::fetch($stmt);
65
66                 if (!is_array($row)) {
67                         return $row;
68                 }
69
70                 if (array_key_exists('verb', $row)) {
71                         if (in_array($row['verb'], Item::ACTIVITIES)) {
72                                 if (array_key_exists('title', $row)) {
73                                         $row['title'] = '';
74                                 }
75                                 if (array_key_exists('body', $row) && empty($row['body'])) {
76                                         $row['body'] = $row['verb'];
77                                 }
78                                 if (array_key_exists('object', $row)) {
79                                         $row['object'] = '';
80                                 }
81                                 if (array_key_exists('object-type', $row)) {
82                                         $row['object-type'] = Activity\ObjectType::NOTE;
83                                 }
84                         } elseif (in_array($row['verb'], ['', Activity::POST, Activity::SHARE])) {
85                                 // Posts don't have a target - but having tags or files.
86                                 if (array_key_exists('target', $row)) {
87                                         $row['target'] = '';
88                                 }
89                         }
90                 }
91
92                 if (array_key_exists('extid', $row) && is_null($row['extid'])) {
93                         $row['extid'] = '';
94                 }
95
96                 return $row;
97         }
98
99         /**
100          * Fills an array with data from a post query
101          *
102          * @param object|bool $stmt Return value from Database->select
103          * @return array Data array
104          * @throws \Exception
105          */
106         public static function toArray($stmt): array
107         {
108                 if (is_bool($stmt)) {
109                         return [];
110                 }
111
112                 $data = [];
113                 while ($row = self::fetch($stmt)) {
114                         $data[] = $row;
115                 }
116
117                 DBA::close($stmt);
118
119                 return $data;
120         }
121
122         /**
123          * Check if post-user-view records exists
124          *
125          * @param array $condition array of fields for condition
126          *
127          * @return boolean Are there rows for that condition?
128          * @throws \Exception
129          */
130         public static function exists(array $condition): bool
131         {
132                 return DBA::exists('post-user-view', $condition);
133         }
134
135         /**
136          * Counts the post-user-view records satisfying the provided condition
137          *
138          * @param array        $condition array of fields for condition
139          * @param array        $params    Array of several parameters
140          *
141          * @return int
142          *
143          * Example:
144          * $condition = ["uid" => 1, "network" => 'dspr'];
145          * or:
146          * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
147          *
148          * $count = Post::count($condition);
149          * @throws \Exception
150          */
151         public static function count(array $condition = [], array $params = []): int
152         {
153                 return DBA::count('post-user-view', $condition, $params);
154         }
155
156         /**
157          * Counts the post-thread-user-view records satisfying the provided condition
158          *
159          * @param array        $condition array of fields for condition
160          * @param array        $params    Array of several parameters
161          *
162          * @return int
163          *
164          * Example:
165          * $condition = ["uid" => 1, "network" => 'dspr'];
166          * or:
167          * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
168          *
169          * $count = Post::count($condition);
170          * @throws \Exception
171          */
172         public static function countThread(array $condition = [], array $params = []): int
173         {
174                 return DBA::count('post-thread-user-view', $condition, $params);
175         }
176
177         /**
178          * Counts the post-view records satisfying the provided condition
179          *
180          * @param array        $condition array of fields for condition
181          * @param array        $params    Array of several parameters
182          *
183          * @return int
184          *
185          * Example:
186          * $condition = ["network" => 'dspr'];
187          * or:
188          * $condition = ["`network` IN (?, ?)", 1, 'dfrn', 'dspr'];
189          *
190          * $count = Post::count($condition);
191          * @throws \Exception
192          */
193         public static function countPosts(array $condition = [], array $params = []): int
194         {
195                 return DBA::count('post-view', $condition, $params);
196         }
197
198         /**
199          * Retrieve a single record from the post-user-view view and returns it in an associative array
200          *
201          * @param array $fields
202          * @param array $condition
203          * @param array $params
204          * @param bool  $user_mode true = post-user-view, false = post-view
205          * @return bool|array
206          * @throws \Exception
207          * @see   DBA::select
208          */
209         public static function selectFirst(array $fields = [], array $condition = [], array $params = [])
210         {
211                 $params['limit'] = 1;
212
213                 $result = self::select($fields, $condition, $params);
214
215                 if (is_bool($result)) {
216                         return $result;
217                 } else {
218                         $row = self::fetch($result);
219                         DBA::close($result);
220                         return $row;
221                 }
222         }
223
224         /**
225          * Retrieve a single record from the post-user-view view and returns it in an associative array
226          * When the requested record is a reshare activity, the system fetches the reshared original post.
227          * Otherwise the function reacts similar to selectFirst
228          *
229          * @param array $fields
230          * @param array $condition
231          * @param array $params
232          * @param bool  $user_mode true = post-user-view, false = post-view
233          * @return bool|array
234          * @throws \Exception
235          * @see   DBA::select
236          */
237         public static function selectOriginal(array $fields = [], array $condition = [], array $params = [])
238         {
239                 $original_fields = $fields;
240                 $remove = [];
241                 if (!empty($fields)) {
242                         foreach (['gravity', 'verb', 'thr-parent-id', 'uid'] as $field) {
243                                 if (!in_array($field, $fields)) {
244                                         $fields[] = $field;
245                                         $remove[] = $field;
246                                 }
247                         }
248                 }
249                 $result = self::selectFirst($fields, $condition, $params);
250                 if (empty($result)) {
251                         return $result;
252                 }
253
254                 if (($result['gravity'] != Item::GRAVITY_ACTIVITY) || ($result['verb'] != Activity::ANNOUNCE)) {
255                         foreach ($remove as $field) {
256                                 unset($result[$field]);
257                         }
258                         return $result;
259                 }
260
261                 return self::selectFirst($original_fields, ['uri-id' => $result['thr-parent-id'], 'uid' => [0, $result['uid']]], $params);
262         }
263
264         /**
265          * Retrieve a single record from the post-view view and returns it in an associative array
266          *
267          * @param array $fields
268          * @param array $condition
269          * @param array $params
270          * @return bool|array
271          * @throws \Exception
272          * @see   DBA::select
273          */
274         public static function selectFirstPost(array $fields = [], array $condition = [], array $params = [])
275         {
276                 $params['limit'] = 1;
277
278                 $result = self::selectPosts($fields, $condition, $params);
279
280                 if (is_bool($result)) {
281                         return $result;
282                 } else {
283                         $row = self::fetch($result);
284                         DBA::close($result);
285                         return $row;
286                 }
287         }
288
289         /**
290          * Retrieve a single record from the post-thread-user-view view and returns it in an associative array
291          *
292          * @param array $fields
293          * @param array $condition
294          * @param array $params
295          * @return bool|array
296          * @throws \Exception
297          * @see   DBA::select
298          */
299         public static function selectFirstThread(array $fields = [], array $condition = [], array $params = [])
300         {
301                 $params['limit'] = 1;
302
303                 $result = self::selectThread($fields, $condition, $params);
304
305                 if (is_bool($result)) {
306                         return $result;
307                 } else {
308                         $row = self::fetch($result);
309                         DBA::close($result);
310                         return $row;
311                 }
312         }
313
314         /**
315          * Select rows from the post-user-view view and returns them as an array
316          *
317          * @param array $selected  Array of selected fields, empty for all
318          * @param array $condition Array of fields for condition
319          * @param array $params    Array of several parameters
320          *
321          * @return array
322          * @throws \Exception
323          */
324         public static function selectToArray(array $fields = [], array $condition = [], array $params = [])
325         {
326                 $result = self::select($fields, $condition, $params);
327
328                 if (is_bool($result)) {
329                         return [];
330                 }
331
332                 $data = [];
333                 while ($row = self::fetch($result)) {
334                         $data[] = $row;
335                 }
336                 DBA::close($result);
337
338                 return $data;
339         }
340
341         /**
342          * Select rows from the given view
343          *
344          * @param string $view      View (post-user-view or post-thread-user-view)
345          * @param array  $selected  Array of selected fields, empty for all
346          * @param array  $condition Array of fields for condition
347          * @param array  $params    Array of several parameters
348          *
349          * @return boolean|object
350          * @throws \Exception
351          */
352         private static function selectView(string $view, array $selected = [], array $condition = [], array $params = [])
353         {
354                 if (empty($selected)) {
355                         $selected = array_merge(Item::DISPLAY_FIELDLIST, Item::ITEM_FIELDLIST);
356
357                         if ($view == 'post-thread-user-view') {
358                                 $selected = array_merge($selected, ['ignored']);
359                         }
360                 }
361
362                 $selected = array_unique($selected);
363
364                 return DBA::select($view, $selected, $condition, $params);
365         }
366
367         /**
368          * Select rows from the post-user-view view
369          *
370          * @param array $selected  Array of selected fields, empty for all
371          * @param array $condition Array of fields for condition
372          * @param array $params    Array of several parameters
373          *
374          * @return boolean|object
375          * @throws \Exception
376          */
377         public static function select(array $selected = [], array $condition = [], array $params = [])
378         {
379                 return self::selectView('post-user-view', $selected, $condition, $params);
380         }
381
382         /**
383          * Select rows from the post-origin-view view
384          *
385          * @param array $selected  Array of selected fields, empty for all
386          * @param array $condition Array of fields for condition
387          * @param array $params    Array of several parameters
388          *
389          * @return boolean|object
390          * @throws \Exception
391          */
392         public static function selectOrigin(array $selected = [], array $condition = [], array $params = [])
393         {
394                 return self::selectView('post-origin-view', $selected, $condition, $params);
395         }
396
397         /**
398          * Select rows from the post-view view
399          *
400          * @param array $selected  Array of selected fields, empty for all
401          * @param array $condition Array of fields for condition
402          * @param array $params    Array of several parameters
403          *
404          * @return boolean|object
405          * @throws \Exception
406          */
407         public static function selectPosts(array $selected = [], array $condition = [], array $params = [])
408         {
409                 return self::selectView('post-view', $selected, $condition, $params);
410         }
411
412         /**
413          * Select rows from the post-thread-user-view view
414          *
415          * @param array $selected  Array of selected fields, empty for all
416          * @param array $condition Array of fields for condition
417          * @param array $params    Array of several parameters
418          *
419          * @return boolean|object
420          * @throws \Exception
421          */
422         public static function selectThread(array $selected = [], array $condition = [], array $params = [])
423         {
424                 return self::selectView('post-thread-user-view', $selected, $condition, $params);
425         }
426
427         /**
428          * Select rows from the post-thread-view view
429          *
430          * @param array $selected  Array of selected fields, empty for all
431          * @param array $condition Array of fields for condition
432          * @param array $params    Array of several parameters
433          *
434          * @return boolean|object
435          * @throws \Exception
436          */
437         public static function selectPostThread(array $selected = [], array $condition = [], array $params = [])
438         {
439                 return self::selectView('post-thread-view', $selected, $condition, $params);
440         }
441
442         /**
443          * Select rows from the post-thread-origin-view view
444          *
445          * @param array $selected  Array of selected fields, empty for all
446          * @param array $condition Array of fields for condition
447          * @param array $params    Array of several parameters
448          *
449          * @return boolean|object
450          * @throws \Exception
451          */
452         public static function selectOriginThread(array $selected = [], array $condition = [], array $params = [])
453         {
454                 return self::selectView('post-thread-origin-view', $selected, $condition, $params);
455         }
456
457         /**
458          * Select rows from the given view for a given user
459          *
460          * @param string  $view      View (post-user-view or post-thread-user-view)
461          * @param integer $uid       User ID
462          * @param array   $selected  Array of selected fields, empty for all
463          * @param array   $condition Array of fields for condition
464          * @param array   $params    Array of several parameters
465          *
466          * @return boolean|object
467          * @throws \Exception
468          */
469         private static function selectViewForUser(string $view, int $uid, array $selected = [], array $condition = [], array $params = [])
470         {
471                 if (empty($selected)) {
472                         $selected = Item::DISPLAY_FIELDLIST;
473                 }
474
475                 $condition = DBA::mergeConditions($condition,
476                         ["`visible` AND NOT `deleted`
477                         AND NOT `author-blocked` AND NOT `owner-blocked`
478                         AND (NOT `causer-blocked` OR `causer-id` = ? OR `causer-id` IS NULL) AND NOT `contact-blocked`
479                         AND ((NOT `contact-readonly` AND NOT `contact-pending` AND (`contact-rel` IN (?, ?)))
480                                 OR `self` OR `contact-uid` = ?)
481                         AND NOT EXISTS(SELECT `uri-id` FROM `post-user`    WHERE `uid` = ? AND `uri-id` = " . DBA::quoteIdentifier($view) . ".`uri-id` AND `hidden`)
482                         AND NOT EXISTS(SELECT `cid`    FROM `user-contact` WHERE `uid` = ? AND `cid` IN (`author-id`, `owner-id`) AND (`blocked` OR `ignored`))
483                         AND NOT EXISTS(SELECT `gsid`   FROM `user-gserver` WHERE `uid` = ? AND `gsid` IN (`author-gsid`, `owner-gsid`, `causer-gsid`) AND `ignored`)",
484                                 0, Contact::SHARING, Contact::FRIEND, 0, $uid, $uid, $uid]);
485
486                 $select_string = implode(', ', array_map([DBA::class, 'quoteIdentifier'], $selected));
487
488                 $condition_string = DBA::buildCondition($condition);
489                 $param_string     = DBA::buildParameter($params);
490
491                 $sql = "SELECT " . $select_string . " FROM `" . $view . "` " . $condition_string . $param_string;
492                 $sql = DBA::cleanQuery($sql);
493
494                 return DBA::p($sql, $condition);
495         }
496
497         /**
498          * Select rows from the post-user-view view for a given user
499          *
500          * @param integer $uid       User ID
501          * @param array   $selected  Array of selected fields, empty for all
502          * @param array   $condition Array of fields for condition
503          * @param array   $params    Array of several parameters
504          *
505          * @return boolean|object
506          * @throws \Exception
507          */
508         public static function selectForUser(int $uid, array $selected = [], array $condition = [], array $params = [])
509         {
510                 return self::selectViewForUser('post-user-view', $uid, $selected, $condition, $params);
511         }
512
513         /**
514          * Select rows from the post-view view for a given user
515          *
516          * @param integer $uid       User ID
517          * @param array   $selected  Array of selected fields, empty for all
518          * @param array   $condition Array of fields for condition
519          * @param array   $params    Array of several parameters
520          *
521          * @return boolean|object
522          * @throws \Exception
523          */
524         public static function selectPostsForUser(int $uid, array $selected = [], array $condition = [], array $params = [])
525         {
526                 return self::selectViewForUser('post-view', $uid, $selected, $condition, $params);
527         }
528
529         /**
530          * Select rows from the post-timeline-view view for a given user
531          * This function is used for API calls.
532          *
533          * @param integer $uid       User ID
534          * @param array   $selected  Array of selected fields, empty for all
535          * @param array   $condition Array of fields for condition
536          * @param array   $params    Array of several parameters
537          *
538          * @return boolean|object
539          * @throws \Exception
540          */
541         public static function selectTimelineForUser(int $uid, array $selected = [], array $condition = [], array $params = [])
542         {
543                 return self::selectViewForUser('post-timeline-view', $uid, $selected, $condition, $params);
544         }
545
546         public static function selectLocalTimelineForUser(int $uid, array $selected = [], array $condition = [], array $params = [])
547         {
548                 return self::selectViewForUser('post-timeline-origin-view', $uid, $selected, $condition, $params);
549         }
550
551         /**
552          * Select rows from the post-thread-user-view view for a given user
553          *
554          * @param integer $uid       User ID
555          * @param array   $selected  Array of selected fields, empty for all
556          * @param array   $condition Array of fields for condition
557          * @param array   $params    Array of several parameters
558          *
559          * @return boolean|object
560          * @throws \Exception
561          */
562         public static function selectThreadForUser(int $uid, array $selected = [], array $condition = [], array $params = [])
563         {
564                 return self::selectViewForUser('post-thread-user-view', $uid, $selected, $condition, $params);
565         }
566
567         /**
568          * Retrieve a single record from the post-user-view view for a given user and returns it in an associative array
569          *
570          * @param integer $uid User ID
571          * @param array   $selected
572          * @param array   $condition
573          * @param array   $params
574          * @return bool|array
575          * @throws \Exception
576          * @see   DBA::select
577          */
578         public static function selectFirstForUser(int $uid, array $selected = [], array $condition = [], array $params = [])
579         {
580                 $params['limit'] = 1;
581
582                 $result = self::selectForUser($uid, $selected, $condition, $params);
583
584                 if (is_bool($result)) {
585                         return $result;
586                 } else {
587                         $row = self::fetch($result);
588                         DBA::close($result);
589                         return $row;
590                 }
591         }
592
593         /**
594          * Retrieve a single record from the post-user-view view for a given user and returns it in an associative array
595          * When the requested record is a reshare activity, the system fetches the reshared original post.
596          * Otherwise the function reacts similar to selectFirstForUser
597          *
598          * @param integer $uid User ID
599          * @param array   $selected
600          * @param array   $condition
601          * @param array   $params
602          * @return bool|array
603          * @throws \Exception
604          * @see   DBA::select
605          */
606         public static function selectOriginalForUser(int $uid, array $selected = [], array $condition = [], array $params = [])
607         {
608                 $original_selected = $selected;
609                 $remove = [];
610                 if (!empty($selected)) {
611                         foreach (['gravity', 'verb', 'thr-parent-id'] as $field) {
612                                 if (!in_array($field, $selected)) {
613                                         $selected[] = $field;
614                                         $remove[]   = $field;
615                                 }
616                         }
617                 }
618                 $result = self::selectFirstForUser($uid, $selected, $condition, $params);
619                 if (empty($result)) {
620                         return $result;
621                 }
622
623                 if (($result['gravity'] != Item::GRAVITY_ACTIVITY) || ($result['verb'] != Activity::ANNOUNCE)) {
624                         foreach ($remove as $field) {
625                                 unset($result[$field]);
626                         }
627                         return $result;
628                 }
629
630                 return self::selectFirstForUser($uid, $original_selected, ['uri-id' => $result['thr-parent-id'], 'uid' => [0, $uid]], $params);
631         }
632
633         /**
634          * Update existing post entries
635          *
636          * @param array $fields    The fields that are to be changed
637          * @param array $condition The condition for finding the item entries
638          *
639          * A return value of "0" doesn't mean an error - but that 0 rows had been changed.
640          *
641          * @return integer|boolean number of affected rows - or "false" if there was an error
642          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
643          */
644         public static function update(array $fields, array $condition)
645         {
646                 $affected = 0;
647
648                 Logger::info('Start Update', ['fields' => $fields, 'condition' => $condition, 'uid' => DI::userSession()->getLocalUserId()]);
649
650                 // Don't allow changes to fields that are responsible for the relation between the records
651                 unset($fields['id']);
652                 unset($fields['parent']);
653                 unset($fields['uid']);
654                 unset($fields['uri']);
655                 unset($fields['uri-id']);
656                 unset($fields['thr-parent']);
657                 unset($fields['thr-parent-id']);
658                 unset($fields['parent-uri']);
659                 unset($fields['parent-uri-id']);
660
661                 $thread_condition = DBA::mergeConditions($condition, ['gravity' => Item::GRAVITY_PARENT]);
662
663                 // To ensure the data integrity we do it in an transaction
664                 DBA::transaction();
665
666                 $update_fields = DI::dbaDefinition()->truncateFieldsForTable('post-user', $fields);
667                 if (!empty($update_fields)) {
668                         $affected_count = 0;
669                         $posts          = DBA::select('post-user-view', ['post-user-id'], $condition);
670                         while ($rows = DBA::toArray($posts, false, 100)) {
671                                 $puids = array_column($rows, 'post-user-id');
672                                 if (!DBA::update('post-user', $update_fields, ['id' => $puids])) {
673                                         DBA::rollback();
674                                         Logger::warning('Updating post-user failed', ['fields' => $update_fields, 'condition' => $condition]);
675                                         return false;
676                                 }
677                                 $affected_count += DBA::affectedRows();
678                         }
679                         DBA::close($posts);
680                         $affected = $affected_count;
681                 }
682
683                 $update_fields = DI::dbaDefinition()->truncateFieldsForTable('post-content', $fields);
684                 if (!empty($update_fields)) {
685                         $affected_count = 0;
686                         $posts          = DBA::select('post-user-view', ['uri-id'], $condition, ['group_by' => ['uri-id']]);
687                         while ($rows = DBA::toArray($posts, false, 100)) {
688                                 $uriids = array_column($rows, 'uri-id');
689                                 if (!DBA::update('post-content', $update_fields, ['uri-id' => $uriids])) {
690                                         DBA::rollback();
691                                         Logger::warning('Updating post-content failed', ['fields' => $update_fields, 'condition' => $condition]);
692                                         return false;
693                                 }
694                                 $affected_count += DBA::affectedRows();
695                         }
696                         DBA::close($posts);
697                         $affected = max($affected, $affected_count);
698                 }
699
700                 $update_fields = DI::dbaDefinition()->truncateFieldsForTable('post', $fields);
701                 if (!empty($update_fields)) {
702                         $affected_count = 0;
703                         $posts          = DBA::select('post-user-view', ['uri-id'], $condition, ['group_by' => ['uri-id']]);
704                         while ($rows = DBA::toArray($posts, false, 100)) {
705                                 $uriids = array_column($rows, 'uri-id');
706
707                                 // Only delete the "post" entry when all "post-user" entries are deleted
708                                 if (!empty($update_fields['deleted']) && DBA::exists('post-user', ['uri-id' => $uriids, 'deleted' => false])) {
709                                         unset($update_fields['deleted']);
710                                 }
711
712                                 if (!DBA::update('post', $update_fields, ['uri-id' => $uriids])) {
713                                         DBA::rollback();
714                                         Logger::warning('Updating post failed', ['fields' => $update_fields, 'condition' => $condition]);
715                                         return false;
716                                 }
717                                 $affected_count += DBA::affectedRows();
718                         }
719                         DBA::close($posts);
720                         $affected = max($affected, $affected_count);
721                 }
722
723                 $update_fields = Post\DeliveryData::extractFields($fields);
724                 if (!empty($update_fields)) {
725                         $affected_count = 0;
726                         $posts          = DBA::select('post-user-view', ['uri-id'], $condition, ['group_by' => ['uri-id']]);
727                         while ($rows = DBA::toArray($posts, false, 100)) {
728                                 $uriids = array_column($rows, 'uri-id');
729                                 if (!DBA::update('post-delivery-data', $update_fields, ['uri-id' => $uriids])) {
730                                         DBA::rollback();
731                                         Logger::warning('Updating post-delivery-data failed', ['fields' => $update_fields, 'condition' => $condition]);
732                                         return false;
733                                 }
734                                 $affected_count += DBA::affectedRows();
735                         }
736                         DBA::close($posts);
737                         $affected = max($affected, $affected_count);
738                 }
739
740                 $update_fields = DI::dbaDefinition()->truncateFieldsForTable('post-thread', $fields);
741                 if (!empty($update_fields)) {
742                         $affected_count = 0;
743                         $posts          = DBA::select('post-user-view', ['uri-id'], $thread_condition, ['group_by' => ['uri-id']]);
744                         while ($rows = DBA::toArray($posts, false, 100)) {
745                                 $uriids = array_column($rows, 'uri-id');
746                                 if (!DBA::update('post-thread', $update_fields, ['uri-id' => $uriids])) {
747                                         DBA::rollback();
748                                         Logger::warning('Updating post-thread failed', ['fields' => $update_fields, 'condition' => $condition]);
749                                         return false;
750                                 }
751                                 $affected_count += DBA::affectedRows();
752                         }
753                         DBA::close($posts);
754                         $affected = max($affected, $affected_count);
755                 }
756
757                 $update_fields = DI::dbaDefinition()->truncateFieldsForTable('post-thread-user', $fields);
758                 if (!empty($update_fields)) {
759                         $affected_count = 0;
760                         $posts          = DBA::select('post-user-view', ['post-user-id'], $thread_condition);
761                         while ($rows = DBA::toArray($posts, false, 100)) {
762                                 $thread_puids = array_column($rows, 'post-user-id');
763                                 if (!DBA::update('post-thread-user', $update_fields, ['post-user-id' => $thread_puids])) {
764                                         DBA::rollback();
765                                         Logger::warning('Updating post-thread-user failed', ['fields' => $update_fields, 'condition' => $condition]);
766                                         return false;
767                                 }
768                                 $affected_count += DBA::affectedRows();
769                         }
770                         DBA::close($posts);
771                         $affected = max($affected, $affected_count);
772                 }
773
774                 DBA::commit();
775
776                 Logger::info('Updated posts', ['rows' => $affected]);
777                 return $affected;
778         }
779
780         /**
781          * Delete a row from the post table
782          *
783          * @param array        $conditions Field condition(s)
784          * @param array        $options
785          *                           - cascade: If true we delete records in other tables that depend on the one we're deleting through
786          *                           relations (default: true)
787          *
788          * @return boolean was the delete successful?
789          * @throws \Exception
790          */
791         public static function delete(array $conditions, array $options = []): bool
792         {
793                 return DBA::delete('post', $conditions, $options);
794         }
795 }