]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Contact/User.php
spelling: activity
[friendica.git] / src / Model / Contact / User.php
index eb0b5af096aa73aed342bcb51783703fbc0b082b..a3091f7d836871c9a77a2c907442a4549c5afee5 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2021, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -23,10 +23,12 @@ 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\DI;
 use Friendica\Model\Contact;
 use Friendica\Model\ItemURI;
 use PDOException;
@@ -81,11 +83,11 @@ class User
        /**
         * Apply changes from contact update data to user-contact table
         *
-        * @param array $fields 
-        * @param array $condition 
-        * @return void 
-        * @throws PDOException 
-        * @throws Exception 
+        * @param array $fields
+        * @param array $condition
+        * @return void
+        * @throws PDOException
+        * @throws Exception
         */
        public static function updateByContactUpdate(array $fields, array $condition)
        {
@@ -94,17 +96,18 @@ class User
                $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'])) {
+                       while ($contact = DBA::fetch($contacts)) {
+                               if (empty($contact['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]);
+                               $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();  
+
+               DBA::commit();
        }
 
        /**
@@ -127,7 +130,7 @@ class User
                        $fields['rel'] = Contact::SELF;
                }
 
-               return DBStructure::getFieldsForTable('user-contact', $fields);
+               return DI::dbaDefinition()->truncateFieldsForTable('user-contact', $fields);
        }
 
        /**
@@ -136,15 +139,23 @@ class User
         * @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::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]);
                }
@@ -161,7 +172,7 @@ 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::getPublicAndUserContactID($cid, $uid);
                if (empty($cdata)) {
@@ -173,7 +184,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'];
                        }
                }
 
@@ -182,7 +193,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'];
                        }
                }
 
@@ -199,9 +210,10 @@ 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::getPublicAndUserContactID($cid, $uid);
                if (empty($cdata)) {
@@ -220,11 +232,10 @@ 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::getPublicAndUserContactID($cid, $uid);
                if (empty($cdata)) {
@@ -236,7 +247,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'];
                        }
                }
 
@@ -245,7 +256,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'];
                        }
                }
 
@@ -262,9 +273,10 @@ 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::getPublicAndUserContactID($cid, $uid);
                if (empty($cdata)) {
@@ -279,16 +291,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::getPublicAndUserContactID($cid, $uid);
                if (empty($cdata)) {
-                       return;
+                       return false;
                }
 
                $collapsed = false;
@@ -296,10 +307,54 @@ 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/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;
+       }
 }