diff --git a/actions/apifriendshipscreate.php b/actions/apifriendshipscreate.php index acafbb964b..9c410f379a 100644 --- a/actions/apifriendshipscreate.php +++ b/actions/apifriendshipscreate.php @@ -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); diff --git a/actions/invite.php b/actions/invite.php index 6d2605d054..f99dd4d783 100644 --- a/actions/invite.php +++ b/actions/invite.php @@ -17,7 +17,7 @@ * along with this program. If not, see . */ -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); } diff --git a/actions/unsubscribe.php b/actions/unsubscribe.php index 8adfff9cf6..8679ea6256 100644 --- a/actions/unsubscribe.php +++ b/actions/unsubscribe.php @@ -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); } } diff --git a/classes/Foreign_link.php b/classes/Foreign_link.php index 39bd5f862a..a964d87fdd 100644 --- a/classes/Foreign_link.php +++ b/classes/Foreign_link.php @@ -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() { diff --git a/classes/Profile.php b/classes/Profile.php index 51b1fb72a8..f4ac30cb03 100644 --- a/classes/Profile.php +++ b/classes/Profile.php @@ -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() diff --git a/classes/User.php b/classes/User.php index cc6e55d1bd..c3e1d29827 100644 --- a/classes/User.php +++ b/classes/User.php @@ -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; + } } } } diff --git a/lib/framework.php b/lib/framework.php index 99955b040c..50a04d4fd3 100644 --- a/lib/framework.php +++ b/lib/framework.php @@ -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 index 6ac448b954..0000000000 --- a/lib/subs.php +++ /dev/null @@ -1,51 +0,0 @@ -. - */ - -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(); - } -} diff --git a/plugins/TwitterBridge/daemons/synctwitterfriends.php b/plugins/TwitterBridge/daemons/synctwitterfriends.php index b20d8691a3..3f1a8af6fb 100755 --- a/plugins/TwitterBridge/daemons/synctwitterfriends.php +++ b/plugins/TwitterBridge/daemons/synctwitterfriends.php @@ -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()); } } } diff --git a/scripts/createsim.php b/scripts/createsim.php index 9de855ee54..78bc5a1f89 100644 --- a/scripts/createsim.php +++ b/scripts/createsim.php @@ -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();