X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FModel%2FContact%2FUser.php;h=c86b1c6bea8d1aba1037a96ed35528a04cfbe4c8;hb=f56054ecc140d2f76ea0cb7ea42f94e3fb90cbf6;hp=be60c119bf69d45a8d52a844ba254ecdb6fa0c43;hpb=122ad0af14f046c2462a03fe33967dc41abfc8b5;p=friendica.git diff --git a/src/Model/Contact/User.php b/src/Model/Contact/User.php index be60c119bf..c86b1c6bea 100644 --- a/src/Model/Contact/User.php +++ b/src/Model/Contact/User.php @@ -1,6 +1,6 @@ $contact]); + return false; + } + + if (empty($contact['uri-id'])) { + $contact['uri-id'] = ItemURI::getIdByURI($contact['url']); + } + + $pcontact = Contact::selectFirst(['id'], ['uri-id' => $contact['uri-id'], 'uid' => 0]); + if (!empty($contact['uri-id']) && DBA::isResult($pcontact)) { + $pcid = $pcontact['id']; + } elseif (empty($contact['url']) || !($pcid = Contact::getIdForURL($contact['url'], 0, false))) { + Logger::info('Public contact for user not found', ['uri-id' => $contact['uri-id'], 'uid' => $contact['uid']]); + return false; + } + + $fields = self::preparedFields($contact); + $fields['cid'] = $pcid; + $fields['uid'] = $contact['uid']; + $fields['uri-id'] = $contact['uri-id']; + + $ret = DBA::insert('user-contact', $fields, Database::INSERT_UPDATE); + + Logger::info('Inserted user contact', ['uid' => $contact['uid'], 'cid' => $pcid, 'uri-id' => $contact['uri-id'], 'ret' => $ret]); + + return $ret; + } + + /** + * Apply changes from contact update data to user-contact table + * + * @param array $fields + * @param array $condition + * @return void + * @throws PDOException + * @throws Exception + */ + public static function updateByContactUpdate(array $fields, array $condition) + { + DBA::transaction(); + + $update_fields = self::preparedFields($fields); + if (!empty($update_fields)) { + $contacts = DBA::select('account-user-view', ['pid', 'uri-id', 'uid'], $condition); + while ($contact = DBA::fetch($contacts)) { + if (empty($contact['uri-id']) || empty($contact['uid'])) { + continue; + } + $update_fields['cid'] = $contact['pid']; + $ret = DBA::update('user-contact', $update_fields, ['uri-id' => $contact['uri-id'], 'uid' => $contact['uid']], true); + Logger::info('Updated user contact', ['uid' => $contact['uid'], 'id' => $contact['pid'], 'uri-id' => $contact['uri-id'], 'ret' => $ret]); + } + + DBA::close($contacts); + } + + DBA::commit(); + } + + /** + * Prepare field data for update/insert + * + * @param array $fields + * @return array prepared fields + */ + private static function preparedFields(array $fields): array + { + unset($fields['uid']); + unset($fields['cid']); + unset($fields['uri-id']); + + if (isset($fields['readonly'])) { + $fields['ignored'] = $fields['readonly']; + } + + if (!empty($fields['self'])) { + $fields['rel'] = Contact::SELF; + } + + return DI::dbaDefinition()->truncateFieldsForTable('user-contact', $fields); + } + /** * Block contact id for user id * * @param int $cid Either public contact id or user's contact id * @param int $uid User ID * @param boolean $blocked Is the contact blocked or unblocked? + * @return void * @throws \Exception */ - public static function setBlocked($cid, $uid, $blocked) + public static function setBlocked(int $cid, int $uid, bool $blocked) { - $cdata = Contact::getPublicAndUserContacID($cid, $uid); + $cdata = Contact::getPublicAndUserContactID($cid, $uid); if (empty($cdata)) { return; } + $contact = Contact::getById($cdata['public']); + if ($blocked) { + Protocol::block($contact, $uid); + } else { + Protocol::unblock($contact, $uid); + } + if ($cdata['user'] != 0) { DBA::update('contact', ['blocked' => $blocked], ['id' => $cdata['user'], 'pending' => false]); } @@ -60,9 +175,9 @@ class User * @return boolean is the contact id blocked for the given user? * @throws \Exception */ - public static function isBlocked($cid, $uid) + public static function isBlocked(int $cid, int $uid): bool { - $cdata = Contact::getPublicAndUserContacID($cid, $uid); + $cdata = Contact::getPublicAndUserContactID($cid, $uid); if (empty($cdata)) { return false; } @@ -72,7 +187,7 @@ class User if (!empty($cdata['public'])) { $public_contact = DBA::selectFirst('user-contact', ['blocked'], ['cid' => $cdata['public'], 'uid' => $uid]); if (DBA::isResult($public_contact)) { - $public_blocked = $public_contact['blocked']; + $public_blocked = (bool) $public_contact['blocked']; } } @@ -81,7 +196,7 @@ class User if (!empty($cdata['user'])) { $user_contact = DBA::selectFirst('contact', ['blocked'], ['id' => $cdata['user'], 'pending' => false]); if (DBA::isResult($user_contact)) { - $user_blocked = $user_contact['blocked']; + $user_blocked = (bool) $user_contact['blocked']; } } @@ -98,11 +213,12 @@ class User * @param int $cid Either public contact id or user's contact id * @param int $uid User ID * @param boolean $ignored Is the contact ignored or unignored? + * @return void * @throws \Exception */ - public static function setIgnored($cid, $uid, $ignored) + public static function setIgnored(int $cid, int $uid, bool $ignored) { - $cdata = Contact::getPublicAndUserContacID($cid, $uid); + $cdata = Contact::getPublicAndUserContactID($cid, $uid); if (empty($cdata)) { return; } @@ -119,13 +235,12 @@ class User * * @param int $cid Either public contact id or user's contact id * @param int $uid User ID - * * @return boolean is the contact id ignored for the given user? * @throws \Exception */ - public static function isIgnored($cid, $uid) + public static function isIgnored(int $cid, int $uid): bool { - $cdata = Contact::getPublicAndUserContacID($cid, $uid); + $cdata = Contact::getPublicAndUserContactID($cid, $uid); if (empty($cdata)) { return false; } @@ -135,7 +250,7 @@ class User if (!empty($cdata['public'])) { $public_contact = DBA::selectFirst('user-contact', ['ignored'], ['cid' => $cdata['public'], 'uid' => $uid]); if (DBA::isResult($public_contact)) { - $public_ignored = $public_contact['ignored']; + $public_ignored = (bool) $public_contact['ignored']; } } @@ -144,7 +259,7 @@ class User if (!empty($cdata['user'])) { $user_contact = DBA::selectFirst('contact', ['readonly'], ['id' => $cdata['user'], 'pending' => false]); if (DBA::isResult($user_contact)) { - $user_ignored = $user_contact['readonly']; + $user_ignored = (bool) $user_contact['readonly']; } } @@ -161,11 +276,12 @@ class User * @param int $cid Either public contact id or user's contact id * @param int $uid User ID * @param boolean $collapsed are the contact's posts collapsed or uncollapsed? + * @return void * @throws \Exception */ - public static function setCollapsed($cid, $uid, $collapsed) + public static function setCollapsed(int $cid, int $uid, bool $collapsed) { - $cdata = Contact::getPublicAndUserContacID($cid, $uid); + $cdata = Contact::getPublicAndUserContactID($cid, $uid); if (empty($cdata)) { return; } @@ -178,16 +294,15 @@ class User * * @param int $cid Either public contact id or user's contact id * @param int $uid User ID - * * @return boolean is the contact id blocked for the given user? * @throws HTTPException\InternalServerErrorException * @throws \ImagickException */ - public static function isCollapsed($cid, $uid) + public static function isCollapsed(int $cid, int $uid): bool { - $cdata = Contact::getPublicAndUserContacID($cid, $uid); + $cdata = Contact::getPublicAndUserContactID($cid, $uid); if (empty($cdata)) { - return; + return false; } $collapsed = false; @@ -195,10 +310,101 @@ class User if (!empty($cdata['public'])) { $public_contact = DBA::selectFirst('user-contact', ['collapsed'], ['cid' => $cdata['public'], 'uid' => $uid]); if (DBA::isResult($public_contact)) { - $collapsed = $public_contact['collapsed']; + $collapsed = (bool) $public_contact['collapsed']; } } return $collapsed; } + + /** + * Set the channel post frequency for contact id and user id + * + * @param int $cid Either public contact id or user's contact id + * @param int $uid User ID + * @param int $frequency Type of post frequency in channels + * @return void + * @throws \Exception + */ + public static function setChannelFrequency(int $cid, int $uid, int $frequency) + { + $cdata = Contact::getPublicAndUserContactID($cid, $uid); + if (empty($cdata)) { + return; + } + + DBA::update('user-contact', ['channel-frequency' => $frequency], ['cid' => $cdata['public'], 'uid' => $uid], true); + } + + /** + * Returns the channel frequency state for contact id and user id + * + * @param int $cid Either public contact id or user's contact id + * @param int $uid User ID + * @return int Type of post frequency in channels + * @throws HTTPException\InternalServerErrorException + * @throws \ImagickException + */ + public static function getChannelFrequency(int $cid, int $uid): int + { + $cdata = Contact::getPublicAndUserContactID($cid, $uid); + if (empty($cdata)) { + return false; + } + + $frequency = self::FREQUENCY_DEFAULT; + + if (!empty($cdata['public'])) { + $public_contact = DBA::selectFirst('user-contact', ['channel-frequency'], ['cid' => $cdata['public'], 'uid' => $uid]); + if (DBA::isResult($public_contact)) { + $frequency = $public_contact['channel-frequency'] ?? self::FREQUENCY_DEFAULT; + } + } + + return $frequency; + } + + /** + * Set/Release that the user is blocked by the contact + * + * @param int $cid Either public contact id or user's contact id + * @param int $uid User ID + * @param boolean $blocked Is the user blocked or unblocked by the contact? + * @return void + * @throws \Exception + */ + public static function setIsBlocked(int $cid, int $uid, bool $blocked) + { + $cdata = Contact::getPublicAndUserContactID($cid, $uid); + if (empty($cdata)) { + return; + } + + DBA::update('user-contact', ['is-blocked' => $blocked], ['cid' => $cdata['public'], 'uid' => $uid], true); + } + + /** + * Returns if the user is blocked by the contact + * + * @param int $cid Either public contact id or user's contact id + * @param int $uid User ID + * @return boolean Is the user blocked or unblocked by the contact? + * @throws \Exception + */ + public static function isIsBlocked(int $cid, int $uid): bool + { + $cdata = Contact::getPublicAndUserContactID($cid, $uid); + if (empty($cdata)) { + return false; + } + + if (!empty($cdata['public'])) { + $public_contact = DBA::selectFirst('user-contact', ['is-blocked'], ['cid' => $cdata['public'], 'uid' => $uid]); + if (DBA::isResult($public_contact)) { + return $public_contact['is-blocked']; + } + } + + return false; + } }