Make better use of Subscription class

removed lib/subs.php as it was essentially only a wrapper for Subscription
This commit is contained in:
Mikael Nordfeldth 2013-09-19 17:20:44 +02:00
parent a35344eb00
commit 93e878d7ca
10 changed files with 61 additions and 88 deletions

View File

@ -122,11 +122,10 @@ class ApiFriendshipsCreateAction extends ApiAuthAction
return; return;
} }
$result = subs_subscribe_to($this->user, $this->other); try {
Subscription::start($this->user->getProfile(), $this->other);
if (is_string($result)) { } catch (Exception $e) {
$this->clientError($result, 403, $this->format); $this->clientError($e->getMessage(), 403, $this->format);
return;
} }
$this->initDocument($this->format); $this->initDocument($this->format);

View File

@ -17,7 +17,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * 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. // @todo XXX: Add documentation.
class InviteAction extends Action class InviteAction extends Action
@ -111,15 +111,22 @@ class InviteAction extends Action
foreach ($addresses as $email) { foreach ($addresses as $email) {
$email = common_canonical_email($email); $email = common_canonical_email($email);
$other = User::getKV('email', $email); try {
if ($other) { // If this user is already registered, subscribe to it!
$other = Profile::getByEmail($email);
if ($user->isSubscribed($other)) { if ($user->isSubscribed($other)) {
$this->already[] = $other; $this->already[] = $other;
} else { } else {
subs_subscribe_to($user, $other); try {
$this->subbed[] = $other; 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->sent[] = $email;
$this->sendInvitation($email, $user, $personal); $this->sendInvitation($email, $user, $personal);
} }

View File

@ -53,11 +53,9 @@ class UnsubscribeAction extends Action
return; return;
} }
$user = common_current_user();
if ($_SERVER['REQUEST_METHOD'] != 'POST') { if ($_SERVER['REQUEST_METHOD'] != 'POST') {
common_redirect(common_local_url('subscriptions', common_redirect(common_local_url('subscriptions',
array('nickname' => $user->nickname))); array('nickname' => $this->scoped->nickname)));
return; return;
} }
@ -82,17 +80,16 @@ class UnsubscribeAction extends Action
$other = Profile::getKV('id', $other_id); $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. // TRANS: Client error displayed when trying to unsubscribe while providing a non-existing profile ID.
$this->clientError(_('No profile with that ID.')); $this->clientError(_('No profile with that ID.'));
return; return;
} }
$result = subs_unsubscribe_to($user, $other); try {
Subscription::cancel($this->scoped, $other);
if (is_string($result)) { } catch (Exception $e) {
$this->clientError($result); $this->clientError($e->getMessage());
return;
} }
if ($this->boolean('ajax')) { if ($this->boolean('ajax')) {
@ -108,7 +105,7 @@ class UnsubscribeAction extends Action
$this->elementEnd('html'); $this->elementEnd('html');
} else { } else {
common_redirect(common_local_url('subscriptions', common_redirect(common_local_url('subscriptions',
array('nickname' => $user->nickname)), array('nickname' => $this->scoped->nickname)),
303); 303);
} }
} }

View File

@ -136,6 +136,11 @@ class Foreign_link extends Managed_DataObject
return User::getKV($this->user_id); 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 // Make sure we only ever delete one record at a time
function safeDelete() function safeDelete()
{ {

View File

@ -82,6 +82,16 @@ class Profile extends Managed_DataObject
/* the code above is auto generated do not remove the tag below */ /* the code above is auto generated do not remove the tag below */
###END_AUTOCODE ###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 protected $_user = -1; // Uninitialized value distinct from null
public function getUser() public function getUser()

View File

@ -475,8 +475,15 @@ class User extends Managed_DataObject
if ($invites->find()) { if ($invites->find()) {
while ($invites->fetch()) { while ($invites->fetch()) {
$other = User::getKV($invites->user_id); try {
subs_subscribe_to($other, $this); $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;
}
} }
} }
} }

View File

@ -137,7 +137,6 @@ define('NICKNAME_FMT', VALIDATE_NUM.VALIDATE_ALPHA_LOWER);
require_once INSTALLDIR.'/lib/util.php'; require_once INSTALLDIR.'/lib/util.php';
require_once INSTALLDIR.'/lib/action.php'; require_once INSTALLDIR.'/lib/action.php';
require_once INSTALLDIR.'/lib/mail.php'; require_once INSTALLDIR.'/lib/mail.php';
require_once INSTALLDIR.'/lib/subs.php';
require_once INSTALLDIR.'/lib/clientexception.php'; require_once INSTALLDIR.'/lib/clientexception.php';
require_once INSTALLDIR.'/lib/serverexception.php'; require_once INSTALLDIR.'/lib/serverexception.php';

View File

@ -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();
}
}

View File

@ -203,7 +203,7 @@ class SyncTwitterFriendsDaemon extends ParallelizingDaemon
return false; return false;
} }
$user = $flink->getUser(); $profile = $flink->getProfile();
foreach ($friends as $friend) { foreach ($friends as $friend) {
@ -228,20 +228,20 @@ class SyncTwitterFriendsDaemon extends ParallelizingDaemon
// Get associated user and subscribe her // 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)) { if ($friend_profile instanceof Profile) {
$result = subs_subscribe_to($user, $friend_user); try {
$other = Profile::getKV('id', $invites->user_id);
if ($result === true) { Subscription::start($profile, $friend_profile);
common_log(LOG_INFO, common_log(LOG_INFO,
$this->name() . ' - Subscribed ' . $this->name() . ' - Subscribed ' .
"$friend_user->nickname to $user->nickname."); "{$friend_profile->nickname} to {$profile->nickname}.");
} else { } catch (Exception $e) {
common_debug($this->name() . common_debug($this->name() .
' - Tried subscribing ' . ' - Tried and failed subscribing ' .
"$friend_user->nickname to $user->nickname - " . "{$friend_profile->nickname} to {$profile->nickname} - " .
$result); $e->getMessage());
} }
} }
} }

View File

@ -235,11 +235,11 @@ function newSub($i)
$to = User::getKV('nickname', $tunic); $to = User::getKV('nickname', $tunic);
if (empty($to)) { if (!($to instanceof User)) {
throw new Exception("Can't find user '$tunic'."); throw new Exception("Can't find user '$tunic'.");
} }
subs_subscribe_to($from, $to); Subscription::start($from->getProfile(), $to->getProfile());
$from->free(); $from->free();
$to->free(); $to->free();