]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Switch things from calling Group_member::join & leave & calling events manually to...
authorBrion Vibber <brion@pobox.com>
Mon, 21 Mar 2011 21:35:29 +0000 (14:35 -0700)
committerBrion Vibber <brion@pobox.com>
Mon, 21 Mar 2011 21:35:29 +0000 (14:35 -0700)
17 files changed:
EVENTS.txt
actions/apigroupjoin.php
actions/apigroupleave.php
actions/atompubmembershipfeed.php
actions/atompubshowmembership.php
actions/joingroup.php
actions/leavegroup.php
classes/Group_member.php
classes/Profile.php
classes/User.php
lib/activityimporter.php
lib/activitymover.php
lib/command.php
plugins/ForceGroup/ForceGroupPlugin.php
plugins/OStatus/OStatusPlugin.php
plugins/OStatus/actions/groupsalmon.php
plugins/OStatus/actions/ostatusgroup.php

index 54d06655eecdcbb592ac307579bcd25e547e6e34..1494a9c8904361852a16c28d816859710ca98213 100644 (file)
@@ -742,19 +742,19 @@ EndUnsubscribe: when an unsubscribe is done
 
 StartJoinGroup: when a user is joining a group
 - $group: the group being joined
-- $user: the user joining
+- $profile: the local or remote user joining
 
 EndJoinGroup: when a user finishes joining a group
 - $group: the group being joined
-- $user: the user joining
+- $profile: the local or remote user joining
 
 StartLeaveGroup: when a user is leaving a group
 - $group: the group being left
-- $user: the user leaving
+- $profile: the local or remote user leaving
 
 EndLeaveGroup: when a user has left a group
 - $group: the group being left
-- $user: the user leaving
+- $profile: the local or remote user leaving
 
 StartShowContentLicense: Showing the default license for content
 - $action: the current action
index 2e35cb87de364636e247e95953507703bc7f8069..7124a4b0f07c8304211981106f0b8b7ebe96d3eb 100644 (file)
@@ -126,10 +126,7 @@ class ApiGroupJoinAction extends ApiAuthAction
         }
 
         try {
-            if (Event::handle('StartJoinGroup', array($this->group, $this->user))) {
-                Group_member::join($this->group->id, $this->user->id);
-                Event::handle('EndJoinGroup', array($this->group, $this->user));
-            }
+            $this->user->joinGroup($this->group);
         } catch (Exception $e) {
             // TRANS: Server error displayed when joining a group failed in the database.
             // TRANS: %1$s is the joining user's nickname, $2$s is the group nickname for which the join failed.
index 083ebd890f2c0b38e85e4f927b95e120ed08913c..35a4e04d78889358608f2111f79194729dd9016c 100644 (file)
@@ -117,10 +117,7 @@ class ApiGroupLeaveAction extends ApiAuthAction
         }
 
         try {
-            if (Event::handle('StartLeaveGroup', array($this->group,$this->user))) {
-                Group_member::leave($this->group->id, $this->user->id);
-                Event::handle('EndLeaveGroup', array($this->group, $this->user));
-            }
+            $this->user->leaveGroup($this->group);
         } catch (Exception $e) {
             // TRANS: Server error displayed when leaving a group failed in the database.
             // TRANS: %1$s is the leaving user's nickname, $2$s is the group nickname for which the leave failed.
index b52583314dc6bef2f9a0411b92a02e67fb6cd225..f7882d97d358e9fe1abdd372e727e712d8841c17 100644 (file)
@@ -275,10 +275,7 @@ class AtompubmembershipfeedAction extends ApiAuthAction
                 throw new ClientException(_('Blocked by admin.'));
             }
 
-            if (Event::handle('StartJoinGroup', array($group, $this->auth_user))) {
-                $membership = Group_member::join($group->id, $this->auth_user->id);
-                Event::handle('EndJoinGroup', array($group, $this->auth_user));
-            }
+            $this->auth_user->joinGroup($group);
 
             Event::handle('EndAtomPubNewActivity', array($activity, $membership));
         }
index 098cca8b3eb2dd26bf16d20c4a82432e1c4b0bb3..8bf62912f5268eb8fd3891d859d5fb7dd8c12f0b 100644 (file)
@@ -151,10 +151,7 @@ class AtompubshowmembershipAction extends ApiAuthAction
                                         " membership."), 403);
         }
 
-        if (Event::handle('StartLeaveGroup', array($this->_group, $this->auth_user))) {
-            Group_member::leave($this->_group->id, $this->auth_user->id);
-            Event::handle('EndLeaveGroup', array($this->_group, $this->auth_user));
-        }
+        $this->auth_user->leaveGroup($this->_group);
 
         return;
     }
index 4c45ca8b9ddee3137c61d0564ffc89dc255cc802..8675dbaed3ddcd2f3cd74e493c55bf3976f55a08 100644 (file)
@@ -129,10 +129,7 @@ class JoingroupAction extends Action
         $cur = common_current_user();
 
         try {
-            if (Event::handle('StartJoinGroup', array($this->group, $cur))) {
-                Group_member::join($this->group->id, $cur->id);
-                Event::handle('EndJoinGroup', array($this->group, $cur));
-            }
+            $cur->joinGroup($this->group);
         } catch (Exception $e) {
             // TRANS: Server error displayed when joining a group failed in the database.
             // TRANS: %1$s is the joining user's nickname, $2$s is the group nickname for which the join failed.
index f5d1ccd08ca456e814d108b93958674e2f20eeec..9e560b971721a452b5c7ba668ff682562f8b668b 100644 (file)
@@ -123,10 +123,7 @@ class LeavegroupAction extends Action
         $cur = common_current_user();
 
         try {
-            if (Event::handle('StartLeaveGroup', array($this->group, $cur))) {
-                Group_member::leave($this->group->id, $cur->id);
-                Event::handle('EndLeaveGroup', array($this->group, $cur));
-            }
+            $cur->leaveGroup($this->group);
         } catch (Exception $e) {
             // TRANS: Server error displayed when leaving a group failed in the database.
             // TRANS: %1$s is the leaving user's nickname, $2$s is the group nickname for which the leave failed.
index 2cf31cf123c54b2cd784984c576b3b2e141a229b..30b79bb9318ae1bb3319cf114439866b68ed68d7 100644 (file)
@@ -28,6 +28,7 @@ class Group_member extends Memcached_DataObject
 
     /**
      * Method to add a user to a group.
+     * In most cases, you should call Profile->joinGroup() instead.
      *
      * @param integer $group_id   Group to add to
      * @param integer $profile_id Profile being added
index 88edf5cbb3c1d0912e4cd9b5d86f6b949f768be9..a80f121fc49d67322764266d356165223da1910f 100644 (file)
@@ -339,6 +339,36 @@ class Profile extends Memcached_DataObject
         return $groups;
     }
 
+    /**
+     * Request to join the given group.
+     * May throw exceptions on failure.
+     *
+     * @param User_group $group
+     * @return Group_member
+     */
+    function joinGroup(User_group $group)
+    {
+        $ok = null;
+        if (Event::handle('StartJoinGroup', array($group, $this))) {
+            $ok = Group_member::join($group->id, $this->id);
+            Event::handle('EndJoinGroup', array($group, $this));
+        }
+        return $ok;
+    }
+
+    /**
+     * Leave a group that this profile is a member of.
+     *
+     * @param User_group $group 
+     */
+    function leaveGroup(User_group $group)
+    {
+        if (Event::handle('StartLeaveGroup', array($this->group, $this))) {
+            Group_member::leave($this->group->id, $this->id);
+            Event::handle('EndLeaveGroup', array($this->group, $this));
+        }
+    }
+
     function avatarUrl($size=AVATAR_PROFILE_SIZE)
     {
         $avatar = $this->getAvatar($size);
index 970e167a3bdd6372e745f2bf38b16c9dd0d51da1..31b132d0f33a7fed3b910b35694b71586f82c1db 100644 (file)
@@ -68,6 +68,9 @@ class User extends Memcached_DataObject
     /* the code above is auto generated do not remove the tag below */
     ###END_AUTOCODE
 
+    /**
+     * @return Profile
+     */
     function getProfile()
     {
         $profile = Profile::staticGet('id', $this->id);
@@ -596,6 +599,30 @@ class User extends Memcached_DataObject
         return $profile->getGroups($offset, $limit);
     }
 
+    /**
+     * Request to join the given group.
+     * May throw exceptions on failure.
+     *
+     * @param User_group $group
+     * @return Group_member
+     */
+    function joinGroup(User_group $group)
+    {
+        $profile = $this->getProfile();
+        return $profile->joinGroup($group);
+    }
+
+    /**
+     * Leave a group that this user is a member of.
+     *
+     * @param User_group $group
+     */
+    function leaveGroup(User_group $group)
+    {
+        $profile = $this->getProfile();
+        return $profile->leaveGroup($group);
+    }
+
     function getSubscriptions($offset=0, $limit=null)
     {
         $profile = $this->getProfile();
index aa9b95e084c6b80fb54f717e3ecbde991e90c212..270b285a26abd43581551339dd00a93e7a4532f9 100644 (file)
@@ -163,10 +163,7 @@ class ActivityImporter extends QueueHandler
             throw new ClientException(_("User is already a member of this group."));
         }
 
-        if (Event::handle('StartJoinGroup', array($group, $user))) {
-            Group_member::join($group->id, $user->id);
-            Event::handle('EndJoinGroup', array($group, $user));
-        }
+        $user->joinGroup($group);
     }
 
     // XXX: largely cadged from Ostatus_profile::processNote()
index 495d7b4caa8ea7f4b2e09537583b53aaf3373d71..b308ad5624c1188b230cdbece2e2cfb7f3d74c54 100644 (file)
@@ -116,7 +116,7 @@ class ActivityMover extends QueueHandler
             $sink->postActivity($act);
             $group = User_group::staticGet('uri', $act->objects[0]->id);
             if (!empty($group)) {
-                Group_member::leave($group->id, $user->id);
+                $user->leaveGroup($group);
             }
             break;
         case ActivityVerb::FOLLOW:
index 03baa8212d998a569b1125be893a4b2030d4d771..5b9964c5b1b0a2f878b0b81c39241cb0b8351019 100644 (file)
@@ -352,10 +352,7 @@ class JoinCommand extends Command
         }
 
         try {
-            if (Event::handle('StartJoinGroup', array($group, $cur))) {
-                Group_member::join($group->id, $cur->id);
-                Event::handle('EndJoinGroup', array($group, $cur));
-            }
+            $cur->joinGroup($group);
         } catch (Exception $e) {
             // TRANS: Message given having failed to add a user to a group.
             // TRANS: %1$s is the nickname of the user, %2$s is the nickname of the group.
@@ -400,10 +397,7 @@ class DropCommand extends Command
         }
 
         try {
-            if (Event::handle('StartLeaveGroup', array($group, $cur))) {
-                Group_member::leave($group->id, $cur->id);
-                Event::handle('EndLeaveGroup', array($group, $cur));
-            }
+            $cur->leaveGroup($group);
         } catch (Exception $e) {
             // TRANS: Message given having failed to remove a user from a group.
             // TRANS: %1$s is the nickname of the user, %2$s is the nickname of the group.
index fb98644846b8278922a06748cdfb37ab3696ffc2..5925dcaef04e184ec202a5a459db4606bbc49dfd 100644 (file)
@@ -68,10 +68,7 @@ class ForceGroupPlugin extends Plugin
             $group = User_group::getForNickname($nickname);
             if ($group && !$profile->isMember($group)) {
                 try {
-                    if (Event::handle('StartJoinGroup', array($group, $user))) {
-                        Group_member::join($group->id, $user->id);
-                        Event::handle('EndJoinGroup', array($group, $user));
-                    }
+                    $profile->joinGroup($group);
                 } catch (Exception $e) {
                     // TRANS: Server exception.
                     // TRANS: %1$s is a user nickname, %2$s is a group nickname.
index 738481149c89ee6636408780011ab14aecf49c1a..e75130b9e981f0dfe06083851c3f3fc01c02ff27 100644 (file)
@@ -675,7 +675,7 @@ class OStatusPlugin extends Plugin
      * it'll be left with a stray membership record.
      *
      * @param User_group $group
-     * @param User $user
+     * @param Profile $user
      *
      * @return mixed hook return value
      */
index 024f0cc217a6000cb56cda8526bb601e7bcf90b1..a9838f6e1be48e6c16f20bbccf852a0f99e7be89 100644 (file)
@@ -149,14 +149,7 @@ class GroupsalmonAction extends SalmonAction
         }
 
         try {
-            // @fixme that event currently passes a user from main UI
-            // Event should probably move into Group_member::join
-            // and take a Profile object.
-            //
-            //if (Event::handle('StartJoinGroup', array($this->group, $profile))) {
-                Group_member::join($this->group->id, $profile->id);
-                //Event::handle('EndJoinGroup', array($this->group, $profile));
-            //}
+            $profile->joinGroup($this->group);
         } catch (Exception $e) {
             // TRANS: Server error. %1$s is a profile URI, %2$s is a group nickname.
             $this->serverError(sprintf(_m('Could not join remote user %1$s to group %2$s.'),
@@ -181,11 +174,7 @@ class GroupsalmonAction extends SalmonAction
         $profile = $oprofile->localProfile();
 
         try {
-            // @fixme event needs to be refactored as above
-            //if (Event::handle('StartLeaveGroup', array($this->group, $profile))) {
-                Group_member::leave($this->group->id, $profile->id);
-                //Event::handle('EndLeaveGroup', array($this->group, $profile));
-            //}
+            $profile->leaveGroup($this->group);
         } catch (Exception $e) {
             // TRANS: Server error. %1$s is a profile URI, %2$s is a group nickname.
             $this->serverError(sprintf(_m('Could not remove remote user %1$s from group %2$s.'),
index 24fbaac9ca3ffb532e5de38d01a40f05a48f46d3..245c56a68e327574e6a41d5a565b720ade985334 100644 (file)
@@ -141,18 +141,12 @@ class OStatusGroupAction extends OStatusSubAction
             return;
         }
 
-        if (Event::handle('StartJoinGroup', array($group, $user))) {
-            $ok = Group_member::join($this->oprofile->group_id, $user->id);
-            if ($ok) {
-                Event::handle('EndJoinGroup', array($group, $user));
-                $this->success();
-            } else {
-                // TRANS: OStatus remote group subscription dialog error.
-                $this->showForm(_m('Remote group join failed!'));
-            }
-        } else {
+        try {
+            $user->joinGroup($group);
+        } catch (Exception $e) {
             // TRANS: OStatus remote group subscription dialog error.
-            $this->showForm(_m('Remote group join aborted!'));
+            $this->showForm(_m('Remote group join failed!'));
+            return;
         }
     }