Improved contact-id detection

This commit is contained in:
Michael 2022-08-18 07:48:39 +00:00
parent 037b4b7928
commit a4f1df68e4
2 changed files with 48 additions and 29 deletions

View File

@ -329,12 +329,13 @@ class Contact
* *
* @param int $cid Either public contact id or user's contact id * @param int $cid Either public contact id or user's contact id
* @param int $uid User ID * @param int $uid User ID
* @param bool $strict If "true" then contact mustn't be set to pending or readonly
* *
* @return boolean is the contact id a follower? * @return boolean is the contact id a follower?
* @throws HTTPException\InternalServerErrorException * @throws HTTPException\InternalServerErrorException
* @throws \ImagickException * @throws \ImagickException
*/ */
public static function isFollower(int $cid, int $uid): bool public static function isFollower(int $cid, int $uid, bool $strict = false): bool
{ {
if (Contact\User::isBlocked($cid, $uid)) { if (Contact\User::isBlocked($cid, $uid)) {
return false; return false;
@ -346,6 +347,9 @@ class Contact
} }
$condition = ['id' => $cdata['user'], 'rel' => [self::FOLLOWER, self::FRIEND]]; $condition = ['id' => $cdata['user'], 'rel' => [self::FOLLOWER, self::FRIEND]];
if ($strict) {
$condition = array_merge($condition, ['pending' => false, 'readonly' => false, 'blocked' => false]);
}
return DBA::exists('contact', $condition); return DBA::exists('contact', $condition);
} }
@ -354,12 +358,13 @@ class Contact
* *
* @param string $url Contact URL * @param string $url Contact URL
* @param int $uid User ID * @param int $uid User ID
* @param bool $strict If "true" then contact mustn't be set to pending or readonly
* *
* @return boolean is the contact id a follower? * @return boolean is the contact id a follower?
* @throws HTTPException\InternalServerErrorException * @throws HTTPException\InternalServerErrorException
* @throws \ImagickException * @throws \ImagickException
*/ */
public static function isFollowerByURL(string $url, int $uid): bool public static function isFollowerByURL(string $url, int $uid, bool $strict = false): bool
{ {
$cid = self::getIdForURL($url, $uid); $cid = self::getIdForURL($url, $uid);
@ -367,7 +372,7 @@ class Contact
return false; return false;
} }
return self::isFollower($cid, $uid); return self::isFollower($cid, $uid, $strict);
} }
/** /**
@ -375,12 +380,13 @@ class Contact
* *
* @param int $cid Either public contact id or user's contact id * @param int $cid Either public contact id or user's contact id
* @param int $uid User ID * @param int $uid User ID
* @param bool $strict If "true" then contact mustn't be set to pending or readonly
* *
* @return boolean is the contact sharing with given user? * @return boolean is the contact sharing with given user?
* @throws HTTPException\InternalServerErrorException * @throws HTTPException\InternalServerErrorException
* @throws \ImagickException * @throws \ImagickException
*/ */
public static function isSharing(int $cid, int $uid): bool public static function isSharing(int $cid, int $uid, bool $strict = false): bool
{ {
if (Contact\User::isBlocked($cid, $uid)) { if (Contact\User::isBlocked($cid, $uid)) {
return false; return false;
@ -392,6 +398,9 @@ class Contact
} }
$condition = ['id' => $cdata['user'], 'rel' => [self::SHARING, self::FRIEND]]; $condition = ['id' => $cdata['user'], 'rel' => [self::SHARING, self::FRIEND]];
if ($strict) {
$condition = array_merge($condition, ['pending' => false, 'readonly' => false, 'blocked' => false]);
}
return DBA::exists('contact', $condition); return DBA::exists('contact', $condition);
} }
@ -400,12 +409,13 @@ class Contact
* *
* @param string $url Contact URL * @param string $url Contact URL
* @param int $uid User ID * @param int $uid User ID
* @param bool $strict If "true" then contact mustn't be set to pending or readonly
* *
* @return boolean is the contact url being followed? * @return boolean is the contact url being followed?
* @throws HTTPException\InternalServerErrorException * @throws HTTPException\InternalServerErrorException
* @throws \ImagickException * @throws \ImagickException
*/ */
public static function isSharingByURL(string $url, int $uid): bool public static function isSharingByURL(string $url, int $uid, bool $strict = false): bool
{ {
$cid = self::getIdForURL($url, $uid); $cid = self::getIdForURL($url, $uid);
@ -413,7 +423,7 @@ class Contact
return false; return false;
} }
return self::isSharing($cid, $uid); return self::isSharing($cid, $uid, $strict);
} }
/** /**

View File

@ -1660,25 +1660,34 @@ class Item
$item['origin'] = 0; $item['origin'] = 0;
$item['wall'] = 0; $item['wall'] = 0;
if ($item['gravity'] == GRAVITY_PARENT) { if (!empty($item['causer-id']) && Contact::isSharing($item['causer-id'], $uid, true)) {
$contact = Contact::getByURLForUser($item['owner-link'], $uid, false, ['id']); $cdata = Contact::getPublicAndUserContactID($item['causer-id'], $uid);
} else { $contact_id = $cdata['user'] ?? 0;
$contact = Contact::getByURLForUser($item['author-link'], $uid, false, ['id']);
} }
if (!empty($contact['id'])) { if (empty($contact_id)) {
$item['contact-id'] = $contact['id']; if ($item['gravity'] == GRAVITY_PARENT) {
if (Contact::isSharingByURL($item['owner-link'], $uid, true)) {
$contact_id = Contact::getIdForURL($item['owner-link'], $uid);
} else { } else {
// Shouldn't happen at all $contact_id = Contact::getIdForURL($item['owner-link']);
Logger::warning('contact-id could not be fetched', ['uid' => $uid, 'item' => $item]);
$self = DBA::selectFirst('contact', ['id'], ['self' => true, 'uid' => $uid]);
if (!DBA::isResult($self)) {
// Shouldn't happen even less
Logger::warning('self contact could not be fetched', ['uid' => $uid, 'item' => $item]);
return 0;
} }
$item['contact-id'] = $self['id']; } else {
if (Contact::isSharingByURL($item['author-link'], $uid, true)) {
$contact_id = Contact::getIdForURL($item['author-link'], $uid);
} else {
$contact_id = Contact::getIdForURL($item['author-link']);
} }
}
}
if (empty($contact_id)) {
Logger::warning('contact-id could not be fetched, using self contact instead.', ['uid' => $uid, 'item' => $item]);
$self = Contact::selectFirst(['id'], ['self' => true, 'uid' => $uid]);
$contact_id = $self['id'];
}
$item['contact-id'] = $contact_id;
$notify = false; $notify = false;
if ($item['gravity'] == GRAVITY_PARENT) { if ($item['gravity'] == GRAVITY_PARENT) {