+ $condition = DBA::mergeConditions($condition,
+ ["`visible` AND NOT `deleted`
+ AND NOT `author-blocked` AND NOT `owner-blocked`
+ AND (NOT `causer-blocked` OR `causer-id` = ? OR `causer-id` IS NULL) AND NOT `contact-blocked`
+ AND ((NOT `contact-readonly` AND NOT `contact-pending` AND (`contact-rel` IN (?, ?)))
+ OR `self` OR `contact-uid` = ?)
+ AND NOT EXISTS(SELECT `uri-id` FROM `post-user` WHERE `uid` = ? AND `uri-id` = " . DBA::quoteIdentifier($view) . ".`uri-id` AND `hidden`)
+ AND NOT EXISTS(SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `cid` IN (`author-id`, `owner-id`) AND (`blocked` OR `ignored`))
+ AND NOT EXISTS(SELECT `gsid` FROM `user-gserver` WHERE `uid` = ? AND `gsid` IN (`author-gsid`, `owner-gsid`, `causer-gsid`) AND `ignored`)",
+ 0, Contact::SHARING, Contact::FRIEND, 0, $uid, $uid, $uid]);
+
+ $select_string = implode(', ', array_map([DBA::class, 'quoteIdentifier'], $selected));
+
+ $condition_string = DBA::buildCondition($condition);
+ $param_string = DBA::buildParameter($params);
+
+ $sql = "SELECT " . $select_string . " FROM `" . $view . "` " . $condition_string . $param_string;
+ $sql = DBA::cleanQuery($sql);
+
+ return DBA::p($sql, $condition);
+ }
+
+ /**
+ * Select rows from the post-user-view view for a given user
+ *
+ * @param integer $uid User ID
+ * @param array $selected Array of selected fields, empty for all
+ * @param array $condition Array of fields for condition
+ * @param array $params Array of several parameters
+ *
+ * @return boolean|object
+ * @throws \Exception
+ */
+ public static function selectForUser(int $uid, array $selected = [], array $condition = [], array $params = [])
+ {
+ return self::selectViewForUser('post-user-view', $uid, $selected, $condition, $params);
+ }
+
+ /**
+ * Select rows from the post-view view for a given user
+ *
+ * @param integer $uid User ID
+ * @param array $selected Array of selected fields, empty for all
+ * @param array $condition Array of fields for condition
+ * @param array $params Array of several parameters
+ *
+ * @return boolean|object
+ * @throws \Exception
+ */
+ public static function selectPostsForUser(int $uid, array $selected = [], array $condition = [], array $params = [])
+ {
+ return self::selectViewForUser('post-view', $uid, $selected, $condition, $params);
+ }
+
+ /**
+ * Select rows from the post-timeline-view view for a given user
+ * This function is used for API calls.
+ *
+ * @param integer $uid User ID
+ * @param array $selected Array of selected fields, empty for all
+ * @param array $condition Array of fields for condition
+ * @param array $params Array of several parameters
+ *
+ * @return boolean|object
+ * @throws \Exception
+ */
+ public static function selectTimelineForUser(int $uid, array $selected = [], array $condition = [], array $params = [])
+ {
+ return self::selectViewForUser('post-timeline-view', $uid, $selected, $condition, $params);
+ }
+
+ /**
+ * Select rows from the post-thread-user-view view for a given user
+ *
+ * @param integer $uid User ID
+ * @param array $selected Array of selected fields, empty for all
+ * @param array $condition Array of fields for condition
+ * @param array $params Array of several parameters
+ *
+ * @return boolean|object
+ * @throws \Exception
+ */
+ public static function selectThreadForUser(int $uid, array $selected = [], array $condition = [], array $params = [])
+ {
+ return self::selectViewForUser('post-thread-user-view', $uid, $selected, $condition, $params);
+ }
+
+ /**
+ * Retrieve a single record from the post-user-view view for a given user and returns it in an associative array
+ *
+ * @param integer $uid User ID
+ * @param array $selected
+ * @param array $condition
+ * @param array $params
+ * @return bool|array
+ * @throws \Exception
+ * @see DBA::select
+ */
+ public static function selectFirstForUser(int $uid, array $selected = [], array $condition = [], array $params = [])
+ {
+ $params['limit'] = 1;
+
+ $result = self::selectForUser($uid, $selected, $condition, $params);
+
+ if (is_bool($result)) {
+ return $result;
+ } else {
+ $row = self::fetch($result);
+ DBA::close($result);
+ return $row;
+ }
+ }
+
+ /**
+ * Retrieve a single record from the post-user-view view for a given user and returns it in an associative array
+ * When the requested record is a reshare activity, the system fetches the reshared original post.
+ * Otherwise the function reacts similar to selectFirstForUser
+ *
+ * @param integer $uid User ID
+ * @param array $selected
+ * @param array $condition
+ * @param array $params
+ * @return bool|array
+ * @throws \Exception
+ * @see DBA::select
+ */
+ public static function selectOriginalForUser(int $uid, array $selected = [], array $condition = [], array $params = [])
+ {
+ $original_selected = $selected;
+ $remove = [];
+ if (!empty($selected)) {
+ foreach (['gravity', 'verb', 'thr-parent-id'] as $field) {
+ if (!in_array($field, $selected)) {
+ $selected[] = $field;
+ $remove[] = $field;
+ }
+ }
+ }
+ $result = self::selectFirstForUser($uid, $selected, $condition, $params);
+ if (empty($result)) {
+ return $result;
+ }
+
+ if (($result['gravity'] != Item::GRAVITY_ACTIVITY) || ($result['verb'] != Activity::ANNOUNCE)) {
+ foreach ($remove as $field) {
+ unset($result[$field]);
+ }
+ return $result;
+ }
+
+ return self::selectFirstForUser($uid, $original_selected, ['uri-id' => $result['thr-parent-id'], 'uid' => [0, $uid]], $params);
+ }
+
+ /**
+ * Update existing post entries
+ *
+ * @param array $fields The fields that are to be changed
+ * @param array $condition The condition for finding the item entries
+ *
+ * A return value of "0" doesn't mean an error - but that 0 rows had been changed.
+ *
+ * @return integer|boolean number of affected rows - or "false" if there was an error
+ * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+ */
+ public static function update(array $fields, array $condition)
+ {
+ $affected = 0;
+
+ Logger::info('Start Update', ['fields' => $fields, 'condition' => $condition, 'uid' => DI::userSession()->getLocalUserId()]);
+
+ // Don't allow changes to fields that are responsible for the relation between the records
+ unset($fields['id']);
+ unset($fields['parent']);
+ unset($fields['uid']);
+ unset($fields['uri']);
+ unset($fields['uri-id']);
+ unset($fields['thr-parent']);
+ unset($fields['thr-parent-id']);
+ unset($fields['parent-uri']);
+ unset($fields['parent-uri-id']);