]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
enable block API
authorEvan Prodromou <evan@controlyourself.ca>
Mon, 8 Dec 2008 18:57:28 +0000 (13:57 -0500)
committerEvan Prodromou <evan@controlyourself.ca>
Mon, 8 Dec 2008 18:57:28 +0000 (13:57 -0500)
darcs-hash:20081208185728-5ed1f-8d5f6be6decfbb50deb4ca50bee13404d0c51b72.gz

actions/block.php
actions/twitapiblocks.php
actions/unblock.php
classes/User.php

index fb553da6e957d557f9801f1ed159065f62998817..e6d2b7e49e96bc685b8751c6ea454350dc47619c 100644 (file)
@@ -118,41 +118,13 @@ class BlockAction extends Action {
             return;
         }
 
-        # Add a new block record
-
-        $block = new Profile_block();
-
-        # Begin a transaction
-
-        $block->query('BEGIN');
-
-        $block->blocker = $cur->id;
-        $block->blocked = $this->profile->id;
-
-        $result = $block->insert();
+        $result = $cur->block($this->profile);
 
         if (!$result) {
-            common_log_db_error($block, 'INSERT', __FILE__);
-            $this->server_error(_('Could not save new block record.'));
+            $this->server_error(_('Failed to save block information.'));
             return;
         }
 
-        # Cancel their subscription, if it exists
-
-               $sub = Subscription::pkeyGet(array('subscriber' => $this->profile->id,
-                                                                                  'subscribed' => $cur->id));
-
-        if ($sub) {
-            $result = $sub->delete();
-            if (!$result) {
-                common_log_db_error($sub, 'DELETE', __FILE__);
-                $this->server_error(_('Could not delete subscription.'));
-                return;
-            }
-        }
-
-        $block->query('COMMIT');
-
         # Now, gotta figure where we go back to
 
         foreach ($this->args as $k => $v) {
index 21d620dd3a82bb0f072c80ab82041e0aa5319a62..6bfdec0d0008a578c94d391e0ceff1530325b220 100644 (file)
@@ -24,13 +24,40 @@ require_once(INSTALLDIR.'/lib/twitterapi.php');
 class TwitapiblocksAction extends TwitterapiAction {
 
        function create($args, $apidata) {
+
                parent::handle($args);
-               common_server_error(_('API method under construction.'), $code=501);
+
+               $blockee = $this->get_user($apidata['api_arg'], $apidata);
+
+        if (!$blockee) {
+                       $this->client_error('Not Found', 404, $apidata['content-type']);
+                       return;
+        }
+
+        $user = $apidata['user'];
+
+        if ($user->hasBlocked($blockee) || $user->block($blockee)) {
+            $this->show_profile($blockee, $apidata['content-type']);
+        } else {
+                       common_server_error(_('Block user failed.'));
+        }
        }
 
        function destroy($args, $apidata) {
                parent::handle($args);
-               common_server_error(_('API method under construction.'), $code=501);
-       }
+               $blockee = $this->get_user($apidata['api_arg'], $apidata);
+
+        if (!$blockee) {
+                       $this->client_error('Not Found', 404, $apidata['content-type']);
+                       return;
+        }
 
+        $user = $apidata['user'];
+
+        if (!$user->hasBlocked($blockee) || $user->unblock($blockee)) {
+            $this->show_profile($blockee, $apidata['content-type']);
+        } else {
+                       common_server_error(_('Unblock user failed.'));
+        }
+       }
 }
\ No newline at end of file
index 192beaab5b61eeade016745ca5b89a5457e65ac1..d60cc7088595de49380f17a09635b989f58b2815 100644 (file)
@@ -67,20 +67,10 @@ class UnblockAction extends Action {
 
         $cur = common_current_user();
 
-        # Get the block record
-
-        $block = Profile_block::get($cur->id, $this->profile->id);
-
-        if (!$block) {
-            $this->client_error(_('That user is not blocked!'));
-            return;
-        }
-
-        $result = $block->delete();
+        $result = $cur->unblock($this->profile);
 
         if (!$result) {
-            common_log_db_error($block, 'DELETE', __FILE__);
-            $this->server_error(_('Could not delete block record.'));
+            $this->server_error(_('Error removing the block.'));
             return;
         }
 
index 10c6d6b2476dc936e004577f36e367b81b86ab7a..04558d43a1efaeeed8d84fbcca12c98e94ba2a6c 100644 (file)
@@ -422,4 +422,63 @@ class User extends Memcached_DataObject
        function setSelfTags($newtags) {
                return Profile_tag::setTags($this->id, $this->id, $newtags);
        }
+
+    function block($other) {
+
+        # Add a new block record
+
+        $block = new Profile_block();
+
+        # Begin a transaction
+
+        $block->query('BEGIN');
+
+        $block->blocker = $this->id;
+        $block->blocked = $other->id;
+
+        $result = $block->insert();
+
+        if (!$result) {
+            common_log_db_error($block, 'INSERT', __FILE__);
+            return false;
+        }
+
+        # Cancel their subscription, if it exists
+
+               $sub = Subscription::pkeyGet(array('subscriber' => $other->id,
+                                                                                  'subscribed' => $this->id));
+
+        if ($sub) {
+            $result = $sub->delete();
+            if (!$result) {
+                common_log_db_error($sub, 'DELETE', __FILE__);
+                return false;
+            }
+        }
+
+        $block->query('COMMIT');
+
+        return true;
+    }
+
+    function unblock($other) {
+
+        # Get the block record
+
+        $block = Profile_block::get($this->id, $other->id);
+
+        if (!$block) {
+            return false;
+        }
+
+        $result = $block->delete();
+
+        if (!$result) {
+            common_log_db_error($block, 'DELETE', __FILE__);
+            return false;
+        }
+
+        return true;
+    }
+
 }