]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Contact/User.php
Changed to null-coalscing style (??) as sugguested by @MrPetovan
[friendica.git] / src / Model / Contact / User.php
index 0283fae7209c90a03cf37009b69ccd3129bd1eb2..665b95624dd477dc7c84eee76a300f341d396c87 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2021, the Friendica project
+ * @copyright Copyright (C) 2010-2022, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
 
 namespace Friendica\Model\Contact;
 
+use Exception;
 use Friendica\Core\Logger;
+use Friendica\Core\Protocol;
 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.
@@ -41,7 +45,12 @@ class User
         */
        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;
                }
@@ -51,26 +60,78 @@ class User
                }
 
                $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 = [
-                       'cid'     => $pcontact['id'],
-                       'uid'     => $contact['uid'],
-                       'uri-id'  => $contact['uri-id'],
-                       'blocked' => $contact['blocked'] ?? false,
-                       'ignored' => $contact['readonly'] ?? 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_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;
        }
 
+       /**
+        * 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 ($contact = DBA::fetch($contacts)) {
+                               if (empty($contact['uri-id']) || empty($contact['uid'])) {
+                                       continue;
+                               }
+                               $ret = DBA::update('user-contact', $update_fields, ['uri-id' => $contact['uri-id'], 'uid' => $contact['uid']]);
+                               Logger::info('Updated user contact', ['uid' => $contact['uid'], '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 DBStructure::getFieldsForTable('user-contact', $fields);
+       }
+
        /**
         * Block contact id for user id
         *
@@ -86,6 +147,13 @@ class User
                        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]);
                }
@@ -243,4 +311,48 @@ class User
 
                return $collapsed;
        }
+
+       /**
+        * 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?
+        * @throws \Exception
+        */
+       public static function setIsBlocked($cid, $uid, $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($cid, $uid)
+       {
+               $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;
+       }
 }