low-level management of blocks

darcs-hash:20081208031008-5ed1f-c96006b5c05fa0e68f9adaacd0518016aedfee2a.gz
This commit is contained in:
Evan Prodromou 2008-12-07 22:10:08 -05:00
parent 665a3325b3
commit 81a81baf83
3 changed files with 81 additions and 48 deletions

View File

@ -4,7 +4,7 @@
*/ */
require_once 'classes/Memcached_DataObject'; require_once 'classes/Memcached_DataObject';
class Profile_block extends Memcached_DataObject class Profile_block extends Memcached_DataObject
{ {
###START_AUTOCODE ###START_AUTOCODE
/* the code below is auto generated do not remove the above tag */ /* the code below is auto generated do not remove the above tag */
@ -19,4 +19,9 @@ class Profile_block extends Memcached_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
static function get($blocker, $blocked) {
return Profile_block::pkeyGet(array('blocker' => $blocker,
'blocked' => $blocked));
}
} }

View File

@ -25,7 +25,7 @@ if (!defined('LACONICA')) { exit(1); }
require_once INSTALLDIR.'/classes/Memcached_DataObject.php'; require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
require_once 'Validate.php'; require_once 'Validate.php';
class User extends Memcached_DataObject class User extends Memcached_DataObject
{ {
###START_AUTOCODE ###START_AUTOCODE
/* the code below is auto generated do not remove the above tag */ /* the code below is auto generated do not remove the above tag */
@ -33,7 +33,7 @@ class User extends Memcached_DataObject
public $__table = 'user'; // table name public $__table = 'user'; // table name
public $id; // int(4) primary_key not_null public $id; // int(4) primary_key not_null
public $nickname; // varchar(64) unique_key public $nickname; // varchar(64) unique_key
public $password; // varchar(255) public $password; // varchar(255)
public $email; // varchar(255) unique_key public $email; // varchar(255) unique_key
public $incomingemail; // varchar(255) unique_key public $incomingemail; // varchar(255) unique_key
public $emailnotifysub; // tinyint(1) default_1 public $emailnotifysub; // tinyint(1) default_1
@ -41,23 +41,23 @@ class User extends Memcached_DataObject
public $emailnotifynudge; // tinyint(1) default_1 public $emailnotifynudge; // tinyint(1) default_1
public $emailnotifymsg; // tinyint(1) default_1 public $emailnotifymsg; // tinyint(1) default_1
public $emailmicroid; // tinyint(1) default_1 public $emailmicroid; // tinyint(1) default_1
public $language; // varchar(50) public $language; // varchar(50)
public $timezone; // varchar(50) public $timezone; // varchar(50)
public $emailpost; // tinyint(1) default_1 public $emailpost; // tinyint(1) default_1
public $jabber; // varchar(255) unique_key public $jabber; // varchar(255) unique_key
public $jabbernotify; // tinyint(1) public $jabbernotify; // tinyint(1)
public $jabberreplies; // tinyint(1) public $jabberreplies; // tinyint(1)
public $jabbermicroid; // tinyint(1) default_1 public $jabbermicroid; // tinyint(1) default_1
public $updatefrompresence; // tinyint(1) public $updatefrompresence; // tinyint(1)
public $sms; // varchar(64) unique_key public $sms; // varchar(64) unique_key
public $carrier; // int(4) public $carrier; // int(4)
public $smsnotify; // tinyint(1) public $smsnotify; // tinyint(1)
public $smsreplies; // tinyint(1) public $smsreplies; // tinyint(1)
public $smsemail; // varchar(255) public $smsemail; // varchar(255)
public $uri; // varchar(255) unique_key public $uri; // varchar(255) unique_key
public $autosubscribe; // tinyint(1) public $autosubscribe; // tinyint(1)
public $urlshorteningservice; // varchar(50) default_ur1.ca public $urlshorteningservice; // varchar(50) default_ur1.ca
public $inboxed; // tinyint(1) public $inboxed; // tinyint(1)
public $created; // datetime() not_null public $created; // datetime() not_null
public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
@ -144,6 +144,19 @@ class User extends Memcached_DataObject
return true; return true;
} }
function hasBlocked($other) {
$block = Profile_block::get($this->id, $other->id);
if (is_null($block)) {
$result = false;
} else {
$result = true;
$block->free();
}
return $result;
}
static function register($fields) { static function register($fields) {
# MAGICALLY put fields into current scope # MAGICALLY put fields into current scope
@ -198,11 +211,11 @@ class User extends Memcached_DataObject
} }
$inboxes = common_config('inboxes', 'enabled'); $inboxes = common_config('inboxes', 'enabled');
if ($inboxes === true || $inboxes == 'transitional') { if ($inboxes === true || $inboxes == 'transitional') {
$user->inboxed = 1; $user->inboxed = 1;
} }
$user->created = common_sql_now(); $user->created = common_sql_now();
$user->uri = common_user_uri($user); $user->uri = common_user_uri($user);
@ -277,13 +290,13 @@ class User extends Memcached_DataObject
$cache = common_memcache(); $cache = common_memcache();
# XXX: Kind of a hack. # XXX: Kind of a hack.
if ($cache) { if ($cache) {
# This is the stream of favorite notices, in rev chron # This is the stream of favorite notices, in rev chron
# order. This forces it into cache. # order. This forces it into cache.
$faves = $this->favoriteNotices(0, NOTICE_CACHE_WINDOW); $faves = $this->favoriteNotices(0, NOTICE_CACHE_WINDOW);
$cnt = 0; $cnt = 0;
while ($faves->fetch()) { while ($faves->fetch()) {
if ($faves->id < $notice->id) { if ($faves->id < $notice->id) {
# If we passed it, it's not a fave # If we passed it, it's not a fave
@ -303,27 +316,27 @@ class User extends Memcached_DataObject
# Otherwise, cache doesn't have all faves; # Otherwise, cache doesn't have all faves;
# fall through to the default # fall through to the default
} }
$fave = Fave::pkeyGet(array('user_id' => $this->id, $fave = Fave::pkeyGet(array('user_id' => $this->id,
'notice_id' => $notice->id)); 'notice_id' => $notice->id));
return ((is_null($fave)) ? false : true); return ((is_null($fave)) ? false : true);
} }
function mutuallySubscribed($other) { function mutuallySubscribed($other) {
return $this->isSubscribed($other) && return $this->isSubscribed($other) &&
$other->isSubscribed($this); $other->isSubscribed($this);
} }
function mutuallySubscribedUsers() { function mutuallySubscribedUsers() {
# 3-way join; probably should get cached # 3-way join; probably should get cached
$qry = 'SELECT user.* ' . $qry = 'SELECT user.* ' .
'FROM subscription sub1 JOIN user ON sub1.subscribed = user.id ' . 'FROM subscription sub1 JOIN user ON sub1.subscribed = user.id ' .
'JOIN subscription sub2 ON user.id = sub2.subscriber ' . 'JOIN subscription sub2 ON user.id = sub2.subscriber ' .
'WHERE sub1.subscriber = %d and sub2.subscribed = %d ' . 'WHERE sub1.subscriber = %d and sub2.subscribed = %d ' .
'ORDER BY user.nickname'; 'ORDER BY user.nickname';
$user = new User(); $user = new User();
$user->query(sprintf($qry, $this->id, $this->id)); $user->query(sprintf($qry, $this->id, $this->id));
@ -335,39 +348,41 @@ class User extends Memcached_DataObject
'SELECT notice.* ' . 'SELECT notice.* ' .
'FROM notice JOIN reply ON notice.id = reply.notice_id ' . 'FROM notice JOIN reply ON notice.id = reply.notice_id ' .
'WHERE reply.profile_id = %d '; 'WHERE reply.profile_id = %d ';
return Notice::getStream(sprintf($qry, $this->id), return Notice::getStream(sprintf($qry, $this->id),
'user:replies:'.$this->id, 'user:replies:'.$this->id,
$offset, $limit, $since_id, $before_id); $offset, $limit, $since_id, $before_id);
} }
function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) { function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) {
$profile = $this->getProfile(); $qry =
if (!$profile) { 'SELECT * ' .
return NULL; 'FROM notice ' .
} else { 'WHERE profile_id = %d ';
return $profile->getNotices($offset, $limit, $since_id, $before_id);
} return Notice::getStream(sprintf($qry, $this->id),
'user:notices:'.$this->id,
$offset, $limit, $since_id, $before_id);
} }
function favoriteNotices($offset=0, $limit=NOTICES_PER_PAGE) { function favoriteNotices($offset=0, $limit=NOTICES_PER_PAGE) {
$qry = $qry =
'SELECT notice.* ' . 'SELECT notice.* ' .
'FROM notice JOIN fave ON notice.id = fave.notice_id ' . 'FROM notice JOIN fave ON notice.id = fave.notice_id ' .
'WHERE fave.user_id = %d '; 'WHERE fave.user_id = %d ';
return Notice::getStream(sprintf($qry, $this->id), return Notice::getStream(sprintf($qry, $this->id),
'user:faves:'.$this->id, 'user:faves:'.$this->id,
$offset, $limit); $offset, $limit);
} }
function noticesWithFriends($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) { function noticesWithFriends($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) {
$enabled = common_config('inboxes', 'enabled'); $enabled = common_config('inboxes', 'enabled');
# Complicated code, depending on whether we support inboxes yet # Complicated code, depending on whether we support inboxes yet
# XXX: make this go away when inboxes become mandatory # XXX: make this go away when inboxes become mandatory
if ($enabled === false || if ($enabled === false ||
($enabled == 'transitional' && $this->inboxed == 0)) { ($enabled == 'transitional' && $this->inboxed == 0)) {
$qry = $qry =
'SELECT notice.* ' . 'SELECT notice.* ' .
@ -375,7 +390,7 @@ class User extends Memcached_DataObject
'WHERE subscription.subscriber = %d '; 'WHERE subscription.subscriber = %d ';
$order = NULL; $order = NULL;
} else if ($enabled === true || } else if ($enabled === true ||
($enabled == 'transitional' && $this->inboxed == 1)) { ($enabled == 'transitional' && $this->inboxed == 1)) {
$qry = $qry =
'SELECT notice.* ' . 'SELECT notice.* ' .
'FROM notice JOIN notice_inbox ON notice.id = notice_inbox.notice_id ' . 'FROM notice JOIN notice_inbox ON notice.id = notice_inbox.notice_id ' .
@ -383,13 +398,13 @@ class User extends Memcached_DataObject
# NOTE: we override ORDER # NOTE: we override ORDER
$order = 'ORDER BY notice_inbox.created DESC, notice_inbox.notice_id DESC '; $order = 'ORDER BY notice_inbox.created DESC, notice_inbox.notice_id DESC ';
} }
return Notice::getStream(sprintf($qry, $this->id), return Notice::getStream(sprintf($qry, $this->id),
'user:notices_with_friends:' . $this->id, 'user:notices_with_friends:' . $this->id,
$offset, $limit, $since_id, $before_id, $offset, $limit, $since_id, $before_id,
$order); $order);
} }
function blowFavesCache() { function blowFavesCache() {
$cache = common_memcache(); $cache = common_memcache();
if ($cache) { if ($cache) {
@ -399,11 +414,11 @@ class User extends Memcached_DataObject
$cache->delete(common_cache_key('user:faves:'.$this->id).';last'); $cache->delete(common_cache_key('user:faves:'.$this->id).';last');
} }
} }
function getSelfTags() { function getSelfTags() {
return Profile_tag::getTags($this->id, $this->id); return Profile_tag::getTags($this->id, $this->id);
} }
function setSelfTags($newtags) { function setSelfTags($newtags) {
return Profile_tag::setTags($this->id, $this->id, $newtags); return Profile_tag::setTags($this->id, $this->id, $newtags);
} }

View File

@ -25,7 +25,7 @@ require_once('XMPPHP/XMPP.php');
Returns true or an error message. Returns true or an error message.
*/ */
function subs_subscribe_user($user,$other_nickname) { function subs_subscribe_user($user, $other_nickname) {
$other = User::staticGet('nickname', $other_nickname); $other = User::staticGet('nickname', $other_nickname);
@ -36,18 +36,27 @@ function subs_subscribe_user($user,$other_nickname) {
return subs_subscribe_to($user, $other); return subs_subscribe_to($user, $other);
} }
/* 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) { function subs_subscribe_to($user, $other) {
if ($user->isSubscribed($other)) { if ($user->isSubscribed($other)) {
return _('Already subscribed!.'); return _('Already subscribed!.');
} }
if ($other->hasBlocked($user)) {
return _('User has blocked you.');
}
if (!$user->subscribeTo($other)) { if (!$user->subscribeTo($other)) {
return _('Could not subscribe.'); return _('Could not subscribe.');
return; return;
} }
subs_notify($other, $user); subs_notify($other, $user);
if (common_config('memcached', 'enabled')) { if (common_config('memcached', 'enabled')) {
$cache = new Memcache(); $cache = new Memcache();
@ -55,8 +64,8 @@ function subs_subscribe_to($user, $other) {
$cache->delete(common_cache_key('user:notices_with_friends:' . $user->id)); $cache->delete(common_cache_key('user:notices_with_friends:' . $user->id));
} }
} }
if ($other->autosubscribe && !$other->isSubscribed($user)) { if ($other->autosubscribe && !$other->isSubscribed($user) && !$user->hasBlocked($other)) {
if (!$other->subscribeTo($user)) { if (!$other->subscribeTo($user)) {
return _('Could not subscribe other to you.'); return _('Could not subscribe other to you.');
} }
@ -66,7 +75,7 @@ function subs_subscribe_to($user, $other) {
$cache->delete(common_cache_key('user:notices_with_friends:' . $other->id)); $cache->delete(common_cache_key('user:notices_with_friends:' . $other->id));
} }
} }
subs_notify($user, $other); subs_notify($user, $other);
} }
@ -87,6 +96,7 @@ function subs_notify_email($listenee, $listener) {
/* Unsubscribe $user from nickname $other_nickname /* Unsubscribe $user from nickname $other_nickname
Returns true or an error message. Returns true or an error message.
*/ */
function subs_unsubscribe_user($user, $other_nickname) { function subs_unsubscribe_user($user, $other_nickname) {
$other = User::staticGet('nickname', $other_nickname); $other = User::staticGet('nickname', $other_nickname);
@ -95,9 +105,12 @@ function subs_unsubscribe_user($user, $other_nickname) {
return _('No such user.'); return _('No such user.');
} }
return subs_unsubscribe_to($user, $other); return subs_unsubscribe_to($user, $other->getProfile());
} }
/* Unsubscribe user $user from profile $other
* NB: other can be a remote user. */
function subs_unsubscribe_to($user, $other) { function subs_unsubscribe_to($user, $other) {
if (!$user->isSubscribed($other)) if (!$user->isSubscribed($other))
@ -121,7 +134,7 @@ function subs_unsubscribe_to($user, $other) {
$cache->delete(common_cache_key('user:notices_with_friends:' . $user->id)); $cache->delete(common_cache_key('user:notices_with_friends:' . $user->id));
} }
} }
return true; return true;
} }