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