]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/command.php
replace calls to subs_(un)subscribe_user with Subscription methods
[quix0rs-gnu-social.git] / lib / command.php
index ad2e0bb975d999d806ec41f4a8c492b2a9f95c3b..ea7b60372d96542dd134069d7a94b67b4d40756c 100644 (file)
@@ -222,18 +222,15 @@ class JoinCommand extends Command
             return;
         }
 
-        $member = new Group_member();
-
-        $member->group_id   = $group->id;
-        $member->profile_id = $cur->id;
-        $member->created    = common_sql_now();
-
-        $result = $member->insert();
-        if (!$result) {
-          common_log_db_error($member, 'INSERT', __FILE__);
-          $channel->error($cur, sprintf(_('Could not join user %s to group %s'),
-                                       $cur->nickname, $group->nickname));
-          return;
+        try {
+            if (Event::handle('StartJoinGroup', array($group, $cur))) {
+                Group_member::join($group->id, $cur->id);
+                Event::handle('EndJoinGroup', array($group, $cur));
+            }
+        } catch (Exception $e) {
+            $channel->error($cur, sprintf(_('Could not join user %s to group %s'),
+                                          $cur->nickname, $group->nickname));
+            return;
         }
 
         $channel->output($cur, sprintf(_('%s joined group %s'),
@@ -269,21 +266,15 @@ class DropCommand extends Command
             return;
         }
 
-        $member = new Group_member();
-
-        $member->group_id   = $group->id;
-        $member->profile_id = $cur->id;
-
-        if (!$member->find(true)) {
-          $channel->error($cur,_('Could not find membership record.'));
-          return;
-        }
-        $result = $member->delete();
-        if (!$result) {
-          common_log_db_error($member, 'INSERT', __FILE__);
-          $channel->error($cur, sprintf(_('Could not remove user %s to group %s'),
-                                       $cur->nickname, $group->nickname));
-          return;
+        try {
+            if (Event::handle('StartLeaveGroup', array($group, $cur))) {
+                Group_member::leave($group->id, $cur->id);
+                Event::handle('EndLeaveGroup', array($group, $cur));
+            }
+        } catch (Exception $e) {
+            $channel->error($cur, sprintf(_('Could not remove user %s to group %s'),
+                                          $cur->nickname, $group->nickname));
+            return;
         }
 
         $channel->output($cur, sprintf(_('%s left group %s'),
@@ -431,7 +422,7 @@ class RepeatCommand extends Command
         $repeat = $notice->repeat($this->user->id, $channel->source);
 
         if ($repeat) {
-            common_broadcast_notice($repeat);
+
             $channel->output($this->user, sprintf(_('Notice from %s repeated'), $recipient->nickname));
         } else {
             $channel->error($this->user, _('Error repeating notice.'));
@@ -501,7 +492,7 @@ class ReplyCommand extends Command
         } else {
             $channel->error($this->user, _('Error saving notice.'));
         }
-        common_broadcast_notice($notice);
+
     }
 }
 
@@ -557,12 +548,19 @@ class SubCommand extends Command
             return;
         }
 
-        $result = subs_subscribe_user($this->user, $this->other);
+        $otherUser = User::staticGet('nickname', $this->other);
 
-        if ($result == 'true') {
+        if (empty($otherUser)) {
+            $channel->error($this->user, _('No such user'));
+            return;
+        }
+
+        try {
+            Subscription::start($this->user->getProfile(),
+                                $otherUser->getProfile());
             $channel->output($this->user, sprintf(_('Subscribed to %s'), $this->other));
-        } else {
-            $channel->error($this->user, $result);
+        } catch (Exception $e) {
+            $channel->error($this->user, $e->getMessage());
         }
     }
 }
@@ -585,12 +583,18 @@ class UnsubCommand extends Command
             return;
         }
 
-        $result=subs_unsubscribe_user($this->user, $this->other);
+        $otherUser = User::staticGet('nickname', $this->other);
 
-        if ($result) {
+        if (empty($otherUser)) {
+            $channel->error($this->user, _('No such user'));
+        }
+
+        try {
+            Subscription::cancel($this->user->getProfile(),
+                                 $otherUser->getProfile());
             $channel->output($this->user, sprintf(_('Unsubscribed from %s'), $this->other));
-        } else {
-            $channel->error($this->user, $result);
+        } catch (Exception $e) {
+            $channel->error($this->user, $e->getMessage());
         }
     }
 }
@@ -650,25 +654,17 @@ class LoginCommand extends Command
             $channel->error($this->user, _('Login command is disabled'));
             return;
         }
-        $login_token = Login_token::staticGet('user_id',$this->user->id);
-        if($login_token){
-            $login_token->delete();
-        }
-        $login_token = new Login_token();
-        $login_token->user_id = $this->user->id;
-        $login_token->token = common_good_rand(16);
-        $login_token->created = common_sql_now();
-        $result = $login_token->insert();
-        if (!$result) {
-          common_log_db_error($login_token, 'INSERT', __FILE__);
-          $channel->error($this->user, sprintf(_('Could not create login token for %s'),
-                                       $this->user->nickname));
-          return;
+
+        try {
+            $login_token = Login_token::makeNew($this->user);
+        } catch (Exception $e) {
+            $channel->error($this->user, $e->getMessage());
         }
+
         $channel->output($this->user,
             sprintf(_('This link is useable only once, and is good for only 2 minutes: %s'),
-                    common_local_url('login',
-                        array('user_id'=>$login_token->user_id, 'token'=>$login_token->token))));
+                    common_local_url('otp',
+                        array('user_id' => $login_token->user_id, 'token' => $login_token->token))));
     }
 }
 
@@ -742,42 +738,42 @@ class HelpCommand extends Command
     function execute($channel)
     {
         $channel->output($this->user,
-                         _("Commands:")."\n".
-                         _("on - turn on notifications")."\n".
-                         _("off - turn off notifications")."\n".
-                         _("help - show this help")."\n".
-                         _("follow <nickname> - subscribe to user")."\n".
-                         _("groups - lists the groups you have joined")."\n".
-                         _("subscriptions - list the people you follow")."\n".
-                         _("subscribers - list the people that follow you")."\n".
-                         _("leave <nickname> - unsubscribe from user")."\n".
-                         _("d <nickname> <text> - direct message to user")."\n".
-                         _("get <nickname> - get last notice from user")."\n".
-                         _("whois <nickname> - get profile info on user")."\n".
-                         _("fav <nickname> - add user's last notice as a 'fave'")."\n".
-                         _("fav #<notice_id> - add notice with the given id as a 'fave'")."\n".
-                         _("repeat #<notice_id> - repeat a notice with a given id")."\n".
-                         _("repeat <nickname> - repeat the last notice from user")."\n".
-                         _("reply #<notice_id> - reply to notice with a given id")."\n".
-                         _("reply <nickname> - reply to the last notice from user")."\n".
-                         _("join <group> - join group")."\n".
-                         #_("login - Get a link to login to the web interface")."\n".
-                         _("drop <group> - leave group")."\n".
-                         _("stats - get your stats")."\n".
-                         _("stop - same as 'off'")."\n".
-                         _("quit - same as 'off'")."\n".
-                         _("sub <nickname> - same as 'follow'")."\n".
-                         _("unsub <nickname> - same as 'leave'")."\n".
-                         _("last <nickname> - same as 'get'")."\n".
-                         #_("on <nickname> - not yet implemented.")."\n".
-                         #_("off <nickname> - not yet implemented.")."\n".
-                         _("nudge <nickname> - remind a user to update.")."\n");
-                         #_("invite <phone number> - not yet implemented.")."\n".
-                         #_("track <word> - not yet implemented.")."\n".
-                         #_("untrack <word> - not yet implemented.")."\n".
-                         #_("track off - not yet implemented.")."\n".
-                         #_("untrack all - not yet implemented.")."\n".
-                         #_("tracks - not yet implemented.")."\n".
-                         #_("tracking - not yet implemented.")."\n"
+                         _("Commands:\n".
+                           "on - turn on notifications\n".
+                           "off - turn off notifications\n".
+                           "help - show this help\n".
+                           "follow <nickname> - subscribe to user\n".
+                           "groups - lists the groups you have joined\n".
+                           "subscriptions - list the people you follow\n".
+                           "subscribers - list the people that follow you\n".
+                           "leave <nickname> - unsubscribe from user\n".
+                           "d <nickname> <text> - direct message to user\n".
+                           "get <nickname> - get last notice from user\n".
+                           "whois <nickname> - get profile info on user\n".
+                           "fav <nickname> - add user's last notice as a 'fave'\n".
+                           "fav #<notice_id> - add notice with the given id as a 'fave'\n".
+                           "repeat #<notice_id> - repeat a notice with a given id\n".
+                           "repeat <nickname> - repeat the last notice from user\n".
+                           "reply #<notice_id> - reply to notice with a given id\n".
+                           "reply <nickname> - reply to the last notice from user\n".
+                           "join <group> - join group\n".
+                           "login - Get a link to login to the web interface\n".
+                           "drop <group> - leave group\n".
+                           "stats - get your stats\n".
+                           "stop - same as 'off'\n".
+                           "quit - same as 'off'\n".
+                           "sub <nickname> - same as 'follow'\n".
+                           "unsub <nickname> - same as 'leave'\n".
+                           "last <nickname> - same as 'get'\n".
+                           "on <nickname> - not yet implemented.\n".
+                           "off <nickname> - not yet implemented.\n".
+                           "nudge <nickname> - remind a user to update.\n".
+                           "invite <phone number> - not yet implemented.\n".
+                           "track <word> - not yet implemented.\n".
+                           "untrack <word> - not yet implemented.\n".
+                           "track off - not yet implemented.\n".
+                           "untrack all - not yet implemented.\n".
+                           "tracks - not yet implemented.\n".
+                           "tracking - not yet implemented.\n"));
     }
 }