Merge remote-tracking branch 'upstream/develop' into contact-media

This commit is contained in:
Michael 2021-10-02 15:13:55 +00:00
commit a44d200c21
35 changed files with 702 additions and 433 deletions

View file

@ -133,6 +133,7 @@ class Contact
const FOLLOWER = 1;
const SHARING = 2;
const FRIEND = 3;
const SELF = 4;
/**
* @}
*/
@ -175,7 +176,7 @@ class Contact
* @param array $fields field array
* @param int $duplicate_mode Do an update on a duplicate entry
*
* @return boolean was the insert successful?
* @return int id of the created contact
* @throws \Exception
*/
public static function insert(array $fields, int $duplicate_mode = Database::INSERT_DEFAULT)
@ -190,15 +191,40 @@ class Contact
$fields['created'] = DateTimeFormat::utcNow();
}
$ret = DBA::insert('contact', $fields, $duplicate_mode);
$contact = DBA::selectFirst('contact', ['nurl', 'uid'], ['id' => DBA::lastInsertId()]);
DBA::insert('contact', $fields, $duplicate_mode);
$contact = DBA::selectFirst('contact', [], ['id' => DBA::lastInsertId()]);
if (!DBA::isResult($contact)) {
// Shouldn't happen
return $ret;
Logger::warning('Created contact could not be found', ['fields' => $fields]);
return 0;
}
Contact\User::insertForContactArray($contact);
// Search for duplicated contacts and get rid of them
self::removeDuplicates($contact['nurl'], $contact['uid']);
if (!$contact['self']) {
self::removeDuplicates($contact['nurl'], $contact['uid']);
}
return $contact['id'];
}
/**
* Updates rows in the contact table
*
* @param array $fields contains the fields that are updated
* @param array $condition condition array with the key values
* @param array|boolean $old_fields array with the old field values that are about to be replaced (true = update on duplicate, false = don't update identical fields)
*
* @return boolean was the update successfull?
* @throws \Exception
*/
public static function update(array $fields, array $condition, $old_fields = [])
{
$ret = DBA::update('contact', $fields, $condition, $old_fields);
// Apply changes to the "user-contact" table on dedicated fields
Contact\User::updateByContactUpdate($fields, $condition);
return $ret;
}
@ -650,7 +676,7 @@ class Contact
// Only create the entry if it doesn't exist yet
if (!DBA::exists('contact', ['uid' => $uid, 'self' => true])) {
$return = DBA::insert('contact', $contact);
$return = (bool)self::insert($contact);
}
// Create the public contact
@ -659,7 +685,7 @@ class Contact
$contact['uid'] = 0;
$contact['prvkey'] = null;
DBA::insert('contact', $contact, Database::INSERT_IGNORE);
self::insert($contact, Database::INSERT_IGNORE);
}
return $return;
@ -760,12 +786,12 @@ class Contact
$fields['name-date'] = DateTimeFormat::utcNow();
}
$fields['updated'] = DateTimeFormat::utcNow();
DBA::update('contact', $fields, ['id' => $self['id']]);
self::update($fields, ['id' => $self['id']]);
// Update the public contact as well
$fields['prvkey'] = null;
$fields['self'] = false;
DBA::update('contact', $fields, ['uid' => 0, 'nurl' => $self['nurl']]);
self::update($fields, ['uid' => 0, 'nurl' => $self['nurl']]);
// Update the profile
$fields = [
@ -783,7 +809,6 @@ class Contact
* Marks a contact for removal
*
* @param int $id contact id
* @return null
* @throws HTTPException\InternalServerErrorException
*/
public static function remove($id)
@ -795,63 +820,33 @@ class Contact
}
// Archive the contact
DBA::update('contact', ['archive' => true, 'network' => Protocol::PHANTOM, 'deleted' => true], ['id' => $id]);
self::update(['archive' => true, 'network' => Protocol::PHANTOM, 'deleted' => true], ['id' => $id]);
// Delete it in the background
Worker::add(PRIORITY_MEDIUM, 'RemoveContact', $id);
}
/**
* Sends an unfriend message. Does not remove the contact
* Sends an unfriend message. Removes the contact for two-way unfriending or sharing only protocols (feed an mail)
*
* @param array $user User unfriending
* @param array $contact Contact unfriended
* @param boolean $dissolve Remove the contact on the remote side
* @return void
* @param array $user User unfriending
* @param array $contact Contact unfriended
* @param boolean $two_way Revoke eventual inbound follow as well
* @return bool|null true if successful, false if not, null if no action was performed
* @throws HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
public static function terminateFriendship(array $user, array $contact, $dissolve = false)
public static function terminateFriendship(array $user, array $contact, bool $two_way = false): bool
{
if (empty($contact['network'])) {
return;
}
$result = Protocol::terminateFriendship($user, $contact, $two_way);
$protocol = $contact['network'];
if (($protocol == Protocol::DFRN) && !empty($contact['protocol'])) {
$protocol = $contact['protocol'];
}
if (in_array($protocol, [Protocol::OSTATUS, Protocol::DFRN])) {
// create an unfollow slap
$item = [];
$item['verb'] = Activity::O_UNFOLLOW;
$item['gravity'] = GRAVITY_ACTIVITY;
$item['follow'] = $contact["url"];
$item['body'] = '';
$item['title'] = '';
$item['guid'] = '';
$item['uri-id'] = 0;
$slap = OStatus::salmon($item, $user);
if (!empty($contact['notify'])) {
Salmon::slapper($user, $contact['notify'], $slap);
}
} elseif ($protocol == Protocol::DIASPORA) {
Diaspora::sendUnshare($user, $contact);
} elseif ($protocol == Protocol::ACTIVITYPUB) {
ActivityPub\Transmitter::sendContactUndo($contact['url'], $contact['id'], $user['uid']);
if ($dissolve) {
ActivityPub\Transmitter::sendContactReject($contact['url'], $contact['hub-verify'], $user['uid']);
}
if ($two_way || in_array($contact['network'], [Protocol::FEED, Protocol::MAIL])) {
self::remove($contact['id']);
} else {
$hook_data = [
'contact' => $contact,
'dissolve' => $dissolve,
];
Hook::callAll('unfollow', $hook_data);
self::update(['rel' => Contact::FOLLOWER], ['id' => $contact['id']]);
}
return $result;
}
/**
@ -887,8 +882,8 @@ class Contact
}
if ($contact['term-date'] <= DBA::NULL_DATETIME) {
DBA::update('contact', ['term-date' => DateTimeFormat::utcNow()], ['id' => $contact['id']]);
DBA::update('contact', ['term-date' => DateTimeFormat::utcNow()], ['`nurl` = ? AND `term-date` <= ? AND NOT `self`', Strings::normaliseLink($contact['url']), DBA::NULL_DATETIME]);
self::update(['term-date' => DateTimeFormat::utcNow()], ['id' => $contact['id']]);
self::update(['term-date' => DateTimeFormat::utcNow()], ['`nurl` = ? AND `term-date` <= ? AND NOT `self`', Strings::normaliseLink($contact['url']), DBA::NULL_DATETIME]);
} else {
/* @todo
* We really should send a notification to the owner after 2-3 weeks
@ -905,8 +900,8 @@ class Contact
* delete, though if the owner tries to unarchive them we'll start
* the whole process over again.
*/
DBA::update('contact', ['archive' => true], ['id' => $contact['id']]);
DBA::update('contact', ['archive' => true], ['nurl' => Strings::normaliseLink($contact['url']), 'self' => false]);
self::update(['archive' => true], ['id' => $contact['id']]);
self::update(['archive' => true], ['nurl' => Strings::normaliseLink($contact['url']), 'self' => false]);
}
}
}
@ -927,7 +922,7 @@ class Contact
$fields = ['failed' => false, 'term-date' => DBA::NULL_DATETIME, 'archive' => false];
$condition = ['uid' => 0, 'network' => Protocol::FEDERATED, 'batch' => $contact['batch'], 'contact-type' => self::TYPE_RELAY];
if (!DBA::exists('contact', array_merge($condition, $fields))) {
DBA::update('contact', $fields, $condition);
self::update($fields, $condition);
}
}
@ -951,8 +946,8 @@ class Contact
// It's a miracle. Our dead contact has inexplicably come back to life.
$fields = ['failed' => false, 'term-date' => DBA::NULL_DATETIME, 'archive' => false];
DBA::update('contact', $fields, ['id' => $contact['id']]);
DBA::update('contact', $fields, ['nurl' => Strings::normaliseLink($contact['url']), 'self' => false]);
self::update($fields, ['id' => $contact['id']]);
self::update($fields, ['nurl' => Strings::normaliseLink($contact['url']), 'self' => false]);
}
/**
@ -1218,8 +1213,7 @@ class Contact
$contact_id = $contact['id'];
Logger::notice('Contact had been created (shortly) before', ['id' => $contact_id, 'url' => $url, 'uid' => $uid]);
} else {
DBA::insert('contact', $fields);
$contact_id = DBA::lastInsertId();
$contact_id = self::insert($fields);
if ($contact_id) {
Logger::info('Contact inserted', ['id' => $contact_id, 'url' => $url, 'uid' => $uid]);
}
@ -1508,7 +1502,7 @@ class Contact
*/
public static function block($cid, $reason = null)
{
$return = DBA::update('contact', ['blocked' => true, 'block_reason' => $reason], ['id' => $cid]);
$return = self::update(['blocked' => true, 'block_reason' => $reason], ['id' => $cid]);
return $return;
}
@ -1522,7 +1516,7 @@ class Contact
*/
public static function unblock($cid)
{
$return = DBA::update('contact', ['blocked' => false, 'block_reason' => null], ['id' => $cid]);
$return = self::update(['blocked' => false, 'block_reason' => null], ['id' => $cid]);
return $return;
}
@ -1821,7 +1815,7 @@ class Contact
// Only update the cached photo links of public contacts when they already are cached
if (($uid == 0) && !$force && empty($contact['thumb']) && empty($contact['micro']) && !$create_cache) {
if ($contact['avatar'] != $avatar) {
DBA::update('contact', ['avatar' => $avatar], ['id' => $cid]);
self::update(['avatar' => $avatar], ['id' => $cid]);
Logger::info('Only update the avatar', ['id' => $cid, 'avatar' => $avatar, 'contact' => $contact]);
}
return;
@ -1918,7 +1912,7 @@ class Contact
$cids[] = $cid;
$uids[] = $uid;
Logger::info('Updating cached contact avatars', ['cid' => $cids, 'uid' => $uids, 'fields' => $fields]);
DBA::update('contact', $fields, ['id' => $cids]);
self::update($fields, ['id' => $cids]);
}
public static function deleteContactByUrl(string $url)
@ -1945,7 +1939,7 @@ class Contact
*/
private static function updateContact(int $id, int $uid, string $old_url, string $new_url, array $fields)
{
if (!DBA::update('contact', $fields, ['id' => $id])) {
if (!self::update($fields, ['id' => $id])) {
Logger::info('Couldn\'t update contact.', ['id' => $id, 'fields' => $fields]);
return;
}
@ -1978,7 +1972,7 @@ class Contact
$condition = ['self' => false, 'nurl' => Strings::normaliseLink($old_url)];
$condition['network'] = [Protocol::DFRN, Protocol::DIASPORA, Protocol::ACTIVITYPUB];
DBA::update('contact', $fields, $condition);
self::update($fields, $condition);
// We mustn't set the update fields for OStatus contacts since they are updated in OnePoll
$condition['network'] = Protocol::OSTATUS;
@ -1994,7 +1988,7 @@ class Contact
return;
}
DBA::update('contact', $fields, $condition);
self::update($fields, $condition);
}
/**
@ -2007,7 +2001,7 @@ class Contact
*/
public static function removeDuplicates(string $nurl, int $uid)
{
$condition = ['nurl' => $nurl, 'uid' => $uid, 'deleted' => false, 'network' => Protocol::FEDERATED];
$condition = ['nurl' => $nurl, 'uid' => $uid, 'self' => false, 'deleted' => false, 'network' => Protocol::FEDERATED];
$count = DBA::count('contact', $condition);
if ($count <= 1) {
return false;
@ -2268,7 +2262,7 @@ class Contact
}
}
if (!empty($fields)) {
DBA::update('contact', $fields, ['id' => $id, 'self' => false]);
self::update($fields, ['id' => $id, 'self' => false]);
Logger::info('Updating local contact', ['id' => $id]);
}
}
@ -2446,7 +2440,7 @@ class Contact
$new_relation = (($contact['rel'] == self::FOLLOWER) ? self::FRIEND : self::SHARING);
$fields = ['rel' => $new_relation, 'subhub' => $subhub, 'readonly' => false];
DBA::update('contact', $fields, ['id' => $contact['id']]);
self::update($fields, ['id' => $contact['id']]);
} else {
$new_relation = (in_array($protocol, [Protocol::MAIL]) ? self::FRIEND : self::SHARING);
@ -2579,7 +2573,7 @@ class Contact
$fields = ['url' => $contact['url'], 'request' => $contact['request'],
'notify' => $contact['notify'], 'poll' => $contact['poll'],
'confirm' => $contact['confirm'], 'poco' => $contact['poco']];
DBA::update('contact', $fields, ['id' => $contact['id']]);
self::update($fields, ['id' => $contact['id']]);
}
return $contact;
@ -2682,7 +2676,7 @@ class Contact
if (($contact['rel'] == self::SHARING)
|| ($sharing && $contact['rel'] == self::FOLLOWER)) {
DBA::update('contact', ['rel' => self::FRIEND, 'writable' => true, 'pending' => false],
self::update(['rel' => self::FRIEND, 'writable' => true, 'pending' => false],
['id' => $contact['id'], 'uid' => $importer['uid']]);
}
@ -2700,7 +2694,7 @@ class Contact
}
// create contact record
DBA::insert('contact', [
$contact_id = self::insert([
'uid' => $importer['uid'],
'created' => DateTimeFormat::utcNow(),
'url' => $url,
@ -2715,8 +2709,6 @@ class Contact
'writable' => 1,
]);
$contact_id = DBA::lastInsertId();
// Ensure to always have the correct network type, independent from the connection request method
self::updateFromProbe($contact_id);
@ -2764,7 +2756,7 @@ class Contact
$fields['rel'] = self::FRIEND;
}
DBA::update('contact', $fields, $condition);
self::update($fields, $condition);
return true;
}
@ -2787,7 +2779,7 @@ class Contact
public static function removeSharer($importer, $contact)
{
if (($contact['rel'] == self::FRIEND) || ($contact['rel'] == self::FOLLOWER)) {
DBA::update('contact', ['rel' => self::FOLLOWER], ['id' => $contact['id']]);
self::update(['rel' => self::FOLLOWER], ['id' => $contact['id']]);
} else {
self::remove($contact['id']);
}

View file

@ -114,7 +114,7 @@ class Relation
}
if (empty($followers) && empty($followings)) {
DBA::update('contact', ['last-discovery' => DateTimeFormat::utcNow()], ['id' => $contact['id']]);
Contact::update(['last-discovery' => DateTimeFormat::utcNow()], ['id' => $contact['id']]);
Logger::info('The contact does not offer discoverable data', ['id' => $contact['id'], 'url' => $url, 'network' => $contact['network']]);
return;
}
@ -162,7 +162,7 @@ class Relation
DBA::delete('contact-relation', ['cid' => $target, 'follows' => false, 'last-interaction' => DBA::NULL_DATETIME]);
}
DBA::update('contact', ['last-discovery' => DateTimeFormat::utcNow()], ['id' => $target]);
Contact::update(['last-discovery' => DateTimeFormat::utcNow()], ['id' => $target]);
Logger::info('Contacts discovery finished', ['id' => $target, 'url' => $url, 'follower' => $follower_counter, 'following' => $following_counter]);
return;
}

View file

@ -21,14 +21,115 @@
namespace Friendica\Model\Contact;
use Exception;
use Friendica\Core\Logger;
use Friendica\Core\System;
use Friendica\Database\Database;
use Friendica\Database\DBA;
use Friendica\Database\DBStructure;
use Friendica\Model\Contact;
use Friendica\Model\ItemURI;
use PDOException;
/**
* This class provides information about user related contacts based on the "user-contact" table.
*/
class User
{
/**
* Insert a user-contact for a given contact array
*
* @param array $contact
* @return void
*/
public static function insertForContactArray(array $contact)
{
if (empty($contact['uid'])) {
// We don't create entries for the public user - by now
return false;
}
if (empty($contact['uri-id']) && empty($contact['url'])) {
Logger::info('Missing contact details', ['contact' => $contact, 'callstack' => System::callstack(20)]);
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('contact', ['uri-id', 'uid'], $condition);
while ($row = DBA::fetch($contacts)) {
if (empty($row['uri-id']) || empty($contact['uid'])) {
continue;
}
$ret = DBA::update('user-contact', $update_fields, ['uri-id' => $row['uri-id'], 'uid' => $row['uid']]);
Logger::info('Updated user contact', ['uid' => $row['uid'], 'uri-id' => $row['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 DBStructure::getFieldsForTable('user-contact', $fields);
}
/**
* Block contact id for user id
*

View file

@ -110,7 +110,7 @@ class Introduction extends BaseModel
'hidden' => $hidden ?? $contact['hidden'],
'rel' => $newRelation,
];
$this->dba->update('contact', $fields, ['id' => $contact['id']]);
Contact::update($fields, ['id' => $contact['id']]);
array_merge($contact, $fields);
@ -159,7 +159,7 @@ class Introduction extends BaseModel
if ($this->dba->exists('contact', $condition)) {
Contact::remove($this->{'contact-id'});
} else {
$this->dba->update('contact', ['pending' => false], ['id' => $this->{'contact-id'}]);
Contact::update(['pending' => false], ['id' => $this->{'contact-id'}]);
}
}

View file

@ -1766,15 +1766,15 @@ class Item
} else {
$condition = ['id' => $arr['contact-id'], 'self' => false];
}
DBA::update('contact', ['failed' => false, 'success_update' => $arr['received'], 'last-item' => $arr['received']], $condition);
Contact::update(['failed' => false, 'success_update' => $arr['received'], 'last-item' => $arr['received']], $condition);
}
// Now do the same for the system wide contacts with uid=0
if ($arr['private'] != self::PRIVATE) {
DBA::update('contact', ['failed' => false, 'success_update' => $arr['received'], 'last-item' => $arr['received']],
Contact::update(['failed' => false, 'success_update' => $arr['received'], 'last-item' => $arr['received']],
['id' => $arr['owner-id']]);
if ($arr['owner-id'] != $arr['author-id']) {
DBA::update('contact', ['failed' => false, 'success_update' => $arr['received'], 'last-item' => $arr['received']],
Contact::update(['failed' => false, 'success_update' => $arr['received'], 'last-item' => $arr['received']],
['id' => $arr['author-id']]);
}
}

View file

@ -234,7 +234,7 @@ class User
$system['closeness'] = 0;
$system['baseurl'] = DI::baseUrl();
$system['gsid'] = GServer::getID($system['baseurl']);
DBA::insert('contact', $system);
Contact::insert($system);
}
/**