namespace Friendica\Model\Contact;
use Exception;
-use Friendica\Collection\Api\Mastodon\Fields;
use Friendica\Core\Logger;
+use Friendica\Core\Protocol;
use Friendica\Core\System;
use Friendica\Database\Database;
use Friendica\Database\DBA;
*/
public static function insertForContactArray(array $contact)
{
- if (!isset($contact['uid']) || (empty($contact['uri-id']) && empty($contact['url']))) {
+ 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;
}
}
$pcontact = Contact::selectFirst(['id'], ['uri-id' => $contact['uri-id'], 'uid' => 0]);
- if (!DBA::isResult($pcontact)) {
- Logger::info('Public contact for user not found', ['uri-id' => $contact['uri-id'], 'uid' => $contact['uid'], 'cid' => $pcontact['id']]);
+ 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 = $contact;
-
- if (isset($fields['readonly'])) {
- $fields['ignored'] = $fields['readonly'];
- }
-
- $fields = DBStructure::getFieldsForTable('user-contact', $fields);
- $fields['cid'] = $pcontact['id'];
+ $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_IGNORE);
+ $ret = DBA::insert('user-contact', $fields, Database::INSERT_UPDATE);
- Logger::info('Inserted user contact', ['uid' => $contact['uid'], 'cid' => $pcontact['id'], 'uri-id' => $contact['uri-id'], 'ret' => $ret]);
+ Logger::info('Inserted user contact', ['uid' => $contact['uid'], 'cid' => $pcid, 'uri-id' => $contact['uri-id'], 'ret' => $ret]);
return $ret;
}
* @throws PDOException
* @throws Exception
*/
- public static function UpdateByContactUpdate(array $fields, array $condition)
+ public static function updateByContactUpdate(array $fields, array $condition)
{
DBA::transaction();
- unset($fields['uid']);
- unset($fields['cid']);
- unset($fields['uri-id']);
-
- if (isset($fields['readonly'])) {
- $fields['ignored'] = $fields['readonly'];
- }
-
- $update_fields = DBStructure::getFieldsForTable('user-contact', $fields);
+ $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'])) {
+ if (empty($row['uri-id']) || empty($contact['uid'])) {
continue;
}
$ret = DBA::update('user-contact', $update_fields, ['uri-id' => $row['uri-id'], 'uid' => $row['uid']]);
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
*
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]);
}