]> git.mxchange.org Git - friendica.git/commitdiff
New function to add group members from a forum
authorMichael <heluecht@pirati.ca>
Wed, 9 Feb 2022 21:34:25 +0000 (21:34 +0000)
committerMichael <heluecht@pirati.ca>
Wed, 9 Feb 2022 21:34:25 +0000 (21:34 +0000)
src/Model/Group.php

index 17e0a18e2e49dd0c8019f2779dcb2b541898e5c8..390ed532e3e0135286a5f5761b4b0a54b01fd41b 100644 (file)
@@ -29,6 +29,7 @@ use Friendica\Database\Database;
 use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Network\HTTPException;
+use Friendica\Protocol\ActivityPub;
 
 /**
  * functions for interacting with the group database table
@@ -519,4 +520,53 @@ class Group
 
                return $o;
        }
+
+       /**
+        * Fetch the followers of a given contact id and store them as group members
+        *
+        * @param integer $id Contact ID
+        */
+       public static function getMembersForForum(int $id) {
+               $contact = Contact::getById($id, ['uid', 'url', 'name']);
+               if (empty($contact)) {
+                       return;
+               }
+       
+               $apcontact = APContact::getByURL($contact['url']);
+               if (empty($apcontact['followers'])) {
+                       return;
+               }
+       
+               $group = DBA::selectFirst('group', ['id'], ['uid' => $contact['uid'], 'cid' => $id]);
+               if (empty($group)) {
+                       $fields = [
+                               'uid'  => $contact['uid'],
+                               'name' => $contact['name'],
+                               'cid'  => $id,
+                       ];
+                       DBA::insert('group', $fields);
+                       $gid = DBA::lastInsertId();
+               } else {
+                       $gid = $group['id'];
+               }
+       
+               $group_members = DBA::selectToArray('group_member', ['contact-id'], ['gid' => $gid]);
+               if (!empty($group_members)) {
+                       $current = array_unique(array_column($group_members, 'contact-id'));
+               } else {
+                       $current = [];
+               }
+       
+               foreach (ActivityPub::fetchItems($apcontact['followers']) as $follower) {
+                       $id = Contact::getIdForURL($follower);
+                       if (!in_array($id, $current)) {
+                               DBA::insert('group_member', ['gid' => $gid, 'contact-id' => $id]);
+                       } else {
+                               $key = array_search($id, $current);
+                               unset($current[$key]);
+                       }
+               }
+       
+               DBA::delete('group_member', ['gid' => $gid, 'contact-id' => $current]);
+       }
 }