updates for invitations

Add the code to registration to handle invitation codes.

Some edge cases on invitations: is the user already subbed to this
person? Tell them. Is the person already on the system? Sub the user
to them, then, and tell the user.

Add some code to User to auto-sub invitees whenever the email address
changes. Call it from a new registration with an invite code, and also
from confirmaddress.

Some whitespace cleanup in the files touched.

darcs-hash:20080827001927-84dde-b50e5d921ca3f2fb894821730ff93cac09d2ba66.gz
This commit is contained in:
Evan Prodromou
2008-08-26 20:19:27 -04:00
parent c2839a80f0
commit e248066b74
6 changed files with 175 additions and 118 deletions

View File

@@ -121,94 +121,51 @@ class User extends DB_DataObject
}
return $profile->getCurrentNotice($dt);
}
function getCarrier() {
return Sms_carrier::staticGet($this->carrier);
}
function subscribeTo($other) {
$sub = new Subscription();
$sub->subscriber = $this->id;
$sub->subscribed = $other->id;
$sub->created = DB_DataObject_Cast::dateTime(); # current time
$sub->created = common_sql_now(); # current time
if (!$sub->insert()) {
return false;
}
return true;
}
function noticesWithFriends($offset=0, $limit=20) {
# We clearly need a more elegant way to make this work.
if (common_config('memcached', 'enabled')) {
if ($offset + $limit <= WITHFRIENDS_CACHE_WINDOW) {
$cached = $this->noticesWithFriendsWindow();
$wrapper = new NoticeWrapper(array_slice($cached, $offset, $limit));
return $wrapper;
}
}
$notice = new Notice();
$notice->query('SELECT notice.* ' .
'FROM notice JOIN subscription on notice.profile_id = subscription.subscribed ' .
'WHERE subscription.subscriber = ' . $this->id . ' ' .
'ORDER BY created DESC, notice.id DESC ' .
'LIMIT ' . $offset . ', ' . $limit);
return $notice;
}
function noticesWithFriendsWindow() {
$cache = new Memcache();
$res = $cache->connect(common_config('memcached', 'server'), common_config('memcached', 'port'));
if (!$res) {
return NULL;
}
$notices = $cache->get(common_cache_key('user:notices_with_friends:' . $this->id));
if ($notices) {
return $notices;
}
$notice = new Notice();
$notice->query('SELECT notice.* ' .
'FROM notice JOIN subscription on notice.profile_id = subscription.subscribed ' .
'WHERE subscription.subscriber = ' . $this->id . ' ' .
'ORDER BY created DESC, notice.id DESC ' .
'LIMIT 0, ' . WITHFRIENDS_CACHE_WINDOW);
$notices = array();
while ($notice->fetch()) {
$notices[] = clone($notice);
}
$cache->set(common_cache_key('user:notices_with_friends:' . $this->id), $notices);
return $notices;
}
static function register($fields) {
# MAGICALLY put fields into current scope
extract($fields);
$profile = new Profile();
$profile->query('BEGIN');
$profile->nickname = $nickname;
$profile->profileurl = common_profile_url($nickname);
if ($fullname) {
$profile->fullname = $fullname;
}
@@ -221,25 +178,34 @@ class User extends DB_DataObject
if ($location) {
$profile->location = $location;
}
$profile->created = common_sql_now();
$id = $profile->insert();
if (!$id) {
common_log_db_error($profile, 'INSERT', __FILE__);
return FALSE;
}
$user = new User();
$user->id = $id;
$user->nickname = $nickname;
if ($password) { # may not have a password for OpenID users
$user->password = common_munge_password($password, $id);
}
# Users who respond to invite email have proven their ownership of that address
if ($code) {
$invite = Invite::staticGet($code);
if ($invite && $invite->address && $invite->address_type == 'email') {
$user->email = $invite->address;
}
}
$user->created = common_sql_now();
$user->uri = common_user_uri($user);
@@ -256,15 +222,15 @@ class User extends DB_DataObject
$subscription->subscriber = $user->id;
$subscription->subscribed = $user->id;
$subscription->created = $user->created;
$result = $subscription->insert();
if (!$result) {
common_log_db_error($subscription, 'INSERT', __FILE__);
return FALSE;
}
if ($email) {
if ($email && !$code) {
$confirm = new Confirm_address();
$confirm->code = common_confirmation_code(128);
@@ -279,9 +245,13 @@ class User extends DB_DataObject
}
}
if ($code && $user->email) {
$user->emailChanged();
}
$profile->query('COMMIT');
if ($email) {
if ($email && !$code) {
mail_confirm_address($confirm->code,
$profile->nickname,
$email);
@@ -289,4 +259,20 @@ class User extends DB_DataObject
return $user;
}
# Things we do when the email changes
function emailChanged() {
$invites = new Invitation();
$invites->address = $user->email;
$invites->address_type = 'email';
if ($invites->find()) {
while ($invites->fetch()) {
$other = User::staticGet($invites->user_id);
subs_subscribe_to($other, $this);
}
}
}
}