summaryrefslogtreecommitdiff
path: root/classes
diff options
context:
space:
mode:
Diffstat (limited to 'classes')
-rw-r--r--classes/Subscription.php64
-rw-r--r--classes/User.php54
2 files changed, 50 insertions, 68 deletions
diff --git a/classes/Subscription.php b/classes/Subscription.php
index 5ac95f922..60c12cccc 100644
--- a/classes/Subscription.php
+++ b/classes/Subscription.php
@@ -62,6 +62,14 @@ class Subscription extends Memcached_DataObject
static function start($subscriber, $other)
{
+ // @fixme should we enforce this as profiles in callers instead?
+ if ($subscriber instanceof User) {
+ $subscriber = $subscriber->getProfile();
+ }
+ if ($other instanceof User) {
+ $other = $other->getProfile();
+ }
+
if (!$subscriber->hasRight(Right::SUBSCRIBE)) {
throw new Exception(_('You have been banned from subscribing.'));
}
@@ -75,20 +83,7 @@ class Subscription extends Memcached_DataObject
}
if (Event::handle('StartSubscribe', array($subscriber, $other))) {
-
- $sub = new Subscription();
-
- $sub->subscriber = $subscriber->id;
- $sub->subscribed = $other->id;
- $sub->created = common_sql_now();
-
- $result = $sub->insert();
-
- if (!$result) {
- common_log_db_error($sub, 'INSERT', __FILE__);
- throw new Exception(_('Could not save subscription.'));
- }
-
+ $sub = self::saveNew($subscriber->id, $other->id);
$sub->notify();
self::blow('user:notices_with_friends:%d', $subscriber->id);
@@ -103,20 +98,11 @@ class Subscription extends Memcached_DataObject
!self::exists($other, $subscriber) &&
!$subscriber->hasBlocked($other)) {
- $auto = new Subscription();
-
- $auto->subscriber = $other->id;
- $auto->subscribed = $subscriber->id;
- $auto->created = common_sql_now();
-
- $result = $auto->insert();
-
- if (!$result) {
- common_log_db_error($auto, 'INSERT', __FILE__);
- throw new Exception(_('Could not save subscription.'));
+ try {
+ self::start($other, $subscriber);
+ } catch (Exception $e) {
+ common_log(LOG_ERR, "Exception during autosubscribe of {$other->nickname} to profile {$subscriber->id}: {$e->getMessage()}");
}
-
- $auto->notify();
}
Event::handle('EndSubscribe', array($subscriber, $other));
@@ -125,6 +111,30 @@ class Subscription extends Memcached_DataObject
return true;
}
+ /**
+ * Low-level subscription save.
+ * Outside callers should use Subscription::start()
+ */
+ protected function saveNew($subscriber_id, $other_id)
+ {
+ $sub = new Subscription();
+
+ $sub->subscriber = $subscriber_id;
+ $sub->subscribed = $other_id;
+ $sub->jabber = 1;
+ $sub->sms = 1;
+ $sub->created = common_sql_now();
+
+ $result = $sub->insert();
+
+ if (!$result) {
+ common_log_db_error($sub, 'INSERT', __FILE__);
+ throw new Exception(_('Could not save subscription.'));
+ }
+
+ return $sub;
+ }
+
function notify()
{
# XXX: add other notifications (Jabber, SMS) here
diff --git a/classes/User.php b/classes/User.php
index ee1006ee1..8ad2ec63d 100644
--- a/classes/User.php
+++ b/classes/User.php
@@ -75,7 +75,11 @@ class User extends Memcached_DataObject
function getProfile()
{
- return Profile::staticGet('id', $this->id);
+ $profile = Profile::staticGet('id', $this->id);
+ if (empty($profile)) {
+ throw new UserNoProfileException($this);
+ }
+ return $profile;
}
function isSubscribed($other)
@@ -141,9 +145,6 @@ class User extends Memcached_DataObject
function getCurrentNotice()
{
$profile = $this->getProfile();
- if (!$profile) {
- return null;
- }
return $profile->getCurrentNotice();
}
@@ -152,19 +153,12 @@ class User extends Memcached_DataObject
return Sms_carrier::staticGet('id', $this->carrier);
}
+ /**
+ * @deprecated use Subscription::start($sub, $other);
+ */
function subscribeTo($other)
{
- $sub = new Subscription();
- $sub->subscriber = $this->id;
- $sub->subscribed = $other->id;
-
- $sub->created = common_sql_now(); // current time
-
- if (!$sub->insert()) {
- return false;
- }
-
- return true;
+ return Subscription::start($this->getProfile(), $other);
}
function hasBlocked($other)
@@ -345,17 +339,7 @@ class User extends Memcached_DataObject
common_log(LOG_WARNING, sprintf("Default user %s does not exist.", $defnick),
__FILE__);
} else {
- $defsub = new Subscription();
- $defsub->subscriber = $user->id;
- $defsub->subscribed = $defuser->id;
- $defsub->created = $user->created;
-
- $result = $defsub->insert();
-
- if (!$result) {
- common_log_db_error($defsub, 'INSERT', __FILE__);
- return false;
- }
+ Subscription::start($user, $defuser);
}
}
@@ -471,21 +455,13 @@ class User extends Memcached_DataObject
function getTaggedNotices($tag, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) {
$profile = $this->getProfile();
- if (!$profile) {
- return null;
- } else {
- return $profile->getTaggedNotices($tag, $offset, $limit, $since_id, $before_id);
- }
+ return $profile->getTaggedNotices($tag, $offset, $limit, $since_id, $before_id);
}
function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
{
$profile = $this->getProfile();
- if (!$profile) {
- return null;
- } else {
- return $profile->getNotices($offset, $limit, $since_id, $before_id);
- }
+ return $profile->getNotices($offset, $limit, $since_id, $before_id);
}
function favoriteNotices($offset=0, $limit=NOTICES_PER_PAGE, $own=false)
@@ -626,14 +602,12 @@ class User extends Memcached_DataObject
function getSubscriptions($offset=0, $limit=null)
{
$profile = $this->getProfile();
- assert(!empty($profile));
return $profile->getSubscriptions($offset, $limit);
}
function getSubscribers($offset=0, $limit=null)
{
$profile = $this->getProfile();
- assert(!empty($profile));
return $profile->getSubscribers($offset, $limit);
}
@@ -697,9 +671,7 @@ class User extends Memcached_DataObject
function delete()
{
$profile = $this->getProfile();
- if ($profile) {
- $profile->delete();
- }
+ $profile->delete();
$related = array('Fave',
'Confirm_address',