]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Make better use of Subscription class
authorMikael Nordfeldth <mmn@hethane.se>
Thu, 19 Sep 2013 15:20:44 +0000 (17:20 +0200)
committerMikael Nordfeldth <mmn@hethane.se>
Thu, 19 Sep 2013 15:29:05 +0000 (17:29 +0200)
removed lib/subs.php as it was essentially only a wrapper for Subscription

actions/apifriendshipscreate.php
actions/invite.php
actions/unsubscribe.php
classes/Foreign_link.php
classes/Profile.php
classes/User.php
lib/framework.php
lib/subs.php [deleted file]
plugins/TwitterBridge/daemons/synctwitterfriends.php
scripts/createsim.php

index acafbb964b372a86b595bbcba8758b76aeb2c8ba..9c410f379a08f648beb098ceb7e88564b0418989 100644 (file)
@@ -122,11 +122,10 @@ class ApiFriendshipsCreateAction extends ApiAuthAction
             return;
         }
 
-        $result = subs_subscribe_to($this->user, $this->other);
-
-        if (is_string($result)) {
-            $this->clientError($result, 403, $this->format);
-            return;
+        try {
+            Subscription::start($this->user->getProfile(), $this->other);
+        } catch (Exception $e) {
+            $this->clientError($e->getMessage(), 403, $this->format);
         }
 
         $this->initDocument($this->format);
index 6d2605d05457b0769279cf8d728067c0a8f1d4c0..f99dd4d783497a0cd76f8f170de7254362cd4e32 100644 (file)
@@ -17,7 +17,7 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
+if (!defined('GNUSOCIAL')) { exit(1); }
 
 // @todo XXX: Add documentation.
 class InviteAction extends Action
@@ -111,15 +111,22 @@ class InviteAction extends Action
 
             foreach ($addresses as $email) {
                 $email = common_canonical_email($email);
-                $other = User::getKV('email', $email);
-                if ($other) {
+                try {
+                    // If this user is already registered, subscribe to it!
+                    $other = Profile::getByEmail($email);
                     if ($user->isSubscribed($other)) {
                         $this->already[] = $other;
                     } else {
-                        subs_subscribe_to($user, $other);
-                        $this->subbed[] = $other;
+                        try {
+                            Subscription::start($profile, $other);
+                            $this->subbed[] = $other;
+                        } catch (Exception $e) {
+                            // subscription failed, but keep working
+                            common_debug('Invitation-based subscription failed: '.$e->getMessage());
+                        }
                     }
-                } else {
+                } catch (NoSuchUserException $e) {
+                    // If email was not known, let's send an invite!
                     $this->sent[] = $email;
                     $this->sendInvitation($email, $user, $personal);
                 }
index 8adfff9cf63ffeb450ec955a8742e28c33b9aa62..8679ea6256bbf6a4b0b16b030398d7f1503b2d27 100644 (file)
@@ -53,11 +53,9 @@ class UnsubscribeAction extends Action
             return;
         }
 
-        $user = common_current_user();
-
         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
             common_redirect(common_local_url('subscriptions',
-                                             array('nickname' => $user->nickname)));
+                                             array('nickname' => $this->scoped->nickname)));
             return;
         }
 
@@ -82,17 +80,16 @@ class UnsubscribeAction extends Action
 
         $other = Profile::getKV('id', $other_id);
 
-        if (!$other) {
+        if (!($other instanceof Profile)) {
             // TRANS: Client error displayed when trying to unsubscribe while providing a non-existing profile ID.
             $this->clientError(_('No profile with that ID.'));
             return;
         }
 
-        $result = subs_unsubscribe_to($user, $other);
-
-        if (is_string($result)) {
-            $this->clientError($result);
-            return;
+        try {
+            Subscription::cancel($this->scoped, $other);
+        } catch (Exception $e) {
+            $this->clientError($e->getMessage());
         }
 
         if ($this->boolean('ajax')) {
@@ -108,7 +105,7 @@ class UnsubscribeAction extends Action
             $this->elementEnd('html');
         } else {
             common_redirect(common_local_url('subscriptions',
-                                             array('nickname' => $user->nickname)),
+                                             array('nickname' => $this->scoped->nickname)),
                             303);
         }
     }
index 39bd5f862a0308ce666c771f361f006e639ec5c1..a964d87fdd9baa19e2a1fd024a74f9d3c5745c6c 100644 (file)
@@ -136,6 +136,11 @@ class Foreign_link extends Managed_DataObject
         return User::getKV($this->user_id);
     }
 
+    function getProfile()
+    {
+        return Profile::getKV('id', $this->user_id);
+    }
+
     // Make sure we only ever delete one record at a time
     function safeDelete()
     {
index 51b1fb72a87500911a895eebf3f25c102e977f43..f4ac30cb035e379b37f3b77288766b9a11fa3df7 100644 (file)
@@ -82,6 +82,16 @@ class Profile extends Managed_DataObject
     /* the code above is auto generated do not remove the tag below */
     ###END_AUTOCODE
 
+    public static function getByEmail($email)
+    {
+        // in the future, profiles should have emails stored...
+        $user = User::getKV('email', $email);
+        if (!($user instanceof User)) {
+            throw new NoSuchUserException(array('email'=>$email));
+        }
+        return $user->getProfile();
+    } 
+
     protected $_user = -1;  // Uninitialized value distinct from null
 
     public function getUser()
index cc6e55d1bde3ab936829102e19560b08024c6ed8..c3e1d29827e245240989eef0123a7fa66c4507bb 100644 (file)
@@ -475,8 +475,15 @@ class User extends Managed_DataObject
 
         if ($invites->find()) {
             while ($invites->fetch()) {
-                $other = User::getKV($invites->user_id);
-                subs_subscribe_to($other, $this);
+                try {
+                    $other = Profile::getKV('id', $invites->user_id);
+                    if (!($other instanceof Profile)) {    // remove when getKV throws exceptions
+                        continue;
+                    }
+                    Subscription::start($other, $this->getProfile());
+                } catch (Exception $e) {
+                    continue;
+                }
             }
         }
     }
index 99955b040c137ac25857e3a25b308ed90ff9b250..50a04d4fd39c1645476725e2a36fde212cd55d7d 100644 (file)
@@ -137,7 +137,6 @@ define('NICKNAME_FMT', VALIDATE_NUM.VALIDATE_ALPHA_LOWER);
 require_once INSTALLDIR.'/lib/util.php';
 require_once INSTALLDIR.'/lib/action.php';
 require_once INSTALLDIR.'/lib/mail.php';
-require_once INSTALLDIR.'/lib/subs.php';
 
 require_once INSTALLDIR.'/lib/clientexception.php';
 require_once INSTALLDIR.'/lib/serverexception.php';
diff --git a/lib/subs.php b/lib/subs.php
deleted file mode 100644 (file)
index 6ac448b..0000000
+++ /dev/null
@@ -1,51 +0,0 @@
-<?php
-/*
- * StatusNet - the distributed open-source microblogging tool
- * Copyright (C) 2008, 2009, StatusNet, Inc.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
-
-/* Subscribe user $user to other user $other.
- * Note: $other must be a local user, not a remote profile.
- * Because the other way is quite a bit more complicated.
- */
-
-function subs_subscribe_to($user, $other)
-{
-    if (is_a($other, 'User')) {
-        $other = $other->getProfile();
-    }
-    try {
-        Subscription::start($user->getProfile(), $other);
-        return true;
-    } catch (Exception $e) {
-        return $e->getMessage();
-    }
-}
-
-function subs_unsubscribe_to($user, $other)
-{
-    if (is_a($other, 'User')) {
-        $other = $other->getProfile();
-    }
-    try {
-        Subscription::cancel($user->getProfile(), $other);
-        return true;
-    } catch (Exception $e) {
-        return $e->getMessage();
-    }
-}
index b20d8691a3ba2d20a6112e30f325eecab5eeb47d..3f1a8af6fb2ae915761f42596efb4494934f0421 100755 (executable)
@@ -203,7 +203,7 @@ class SyncTwitterFriendsDaemon extends ParallelizingDaemon
             return false;
         }
 
-        $user = $flink->getUser();
+        $profile = $flink->getProfile();
 
         foreach ($friends as $friend) {
 
@@ -228,20 +228,20 @@ class SyncTwitterFriendsDaemon extends ParallelizingDaemon
 
                 // Get associated user and subscribe her
 
-                $friend_user = User::getKV('id', $friend_flink->user_id);
+                $friend_profile = Profile::getKV('id', $friend_flink->user_id);
 
-                if (!empty($friend_user)) {
-                    $result = subs_subscribe_to($user, $friend_user);
-
-                    if ($result === true) {
+                if ($friend_profile instanceof Profile) {
+                    try {
+                        $other = Profile::getKV('id', $invites->user_id);
+                        Subscription::start($profile, $friend_profile);
                         common_log(LOG_INFO,
                                    $this->name() . ' - Subscribed ' .
-                                   "$friend_user->nickname to $user->nickname.");
-                    } else {
+                                   "{$friend_profile->nickname} to {$profile->nickname}.");
+                    } catch (Exception $e) {
                         common_debug($this->name() .
-                                     ' - Tried subscribing ' .
-                                     "$friend_user->nickname to $user->nickname - " .
-                                     $result);
+                                     ' - Tried and failed subscribing ' .
+                                     "{$friend_profile->nickname} to {$profile->nickname} - " .
+                                     $e->getMessage());
                     }
                 }
             }
index 9de855ee54cdd8b731f7f02703697332f2f1c3ac..78bc5a1f8987aaf7608194ed016fbb22af10e259 100644 (file)
@@ -235,11 +235,11 @@ function newSub($i)
 
     $to = User::getKV('nickname', $tunic);
 
-    if (empty($to)) {
+    if (!($to instanceof User)) {
         throw new Exception("Can't find user '$tunic'.");
     }
 
-    subs_subscribe_to($from, $to);
+    Subscription::start($from->getProfile(), $to->getProfile());
 
     $from->free();
     $to->free();