summaryrefslogtreecommitdiff
path: root/classes/Inbox.php
diff options
context:
space:
mode:
authorEvan Prodromou <evan@status.net>2010-01-09 10:01:07 -0800
committerEvan Prodromou <evan@status.net>2010-01-09 10:02:07 -0800
commit920c878221cf87c1cf74baabbf0636337550eeab (patch)
treef29a5caa20ca7ddd7470cb71fd027441e31c6d1d /classes/Inbox.php
parent2272438698f5a0a71d098b0de4f4e184033b1034 (diff)
initialize an Inbox from Notice_inbox records
Diffstat (limited to 'classes/Inbox.php')
-rw-r--r--classes/Inbox.php75
1 files changed, 68 insertions, 7 deletions
diff --git a/classes/Inbox.php b/classes/Inbox.php
index 610b5fceb..de52e1e0c 100644
--- a/classes/Inbox.php
+++ b/classes/Inbox.php
@@ -51,15 +51,73 @@ class Inbox extends Memcached_DataObject
return array(false, false, false);
}
- static function insertNotice($user_id, $notice_id)
+ /**
+ * Create a new inbox from existing Notice_inbox stuff
+ */
+
+ static function initialize($user_id)
{
+ $ids = array();
+
+ $ni = new Notice_inbox();
+
+ $ni->user_id = $user_id;
+ $ni->selectAdd();
+ $ni->selectAdd('notice_id');
+ $ni->orderBy('notice_id DESC');
+ $ni->limit(0, 1024);
+
+ if ($ni->find()) {
+ while($ni->fetch()) {
+ $ids[] = $ni->notice_id;
+ }
+ }
+
+ $ni->free();
+ unset($ni);
+
$inbox = new Inbox();
- $inbox->query(sprintf('UPDATE inbox '.
- 'set notice_ids = concat(cast(0x%08x as binary(4)), '.
- 'substr(notice_ids, 1, 4092)) '.
- 'WHERE user_id = %d',
- $notice_id, $user_id));
+ $inbox->user_id = $user_id;
+ $inbox->notice_ids = pack('N*', $ids);
+
+ $result = $inbox->insert();
+
+ if (!$result) {
+ common_log_db_error($inbox, 'INSERT', __FILE__);
+ return null;
+ }
+
+ return $inbox;
+ }
+
+ static function insertNotice($user_id, $notice_id)
+ {
+ $inbox = Inbox::staticGet('user_id', $user_id);
+
+ if (empty($inbox)) {
+ $inbox = Inbox::initialize($user_id);
+ }
+
+ if (empty($inbox)) {
+ return false;
+ }
+
+ $result = $inbox->query(sprintf('UPDATE inbox '.
+ 'set notice_ids = concat(cast(0x%08x as binary(4)), '.
+ 'substr(notice_ids, 1, 4092)) '.
+ 'WHERE user_id = %d',
+ $notice_id, $user_id));
+
+ if ($result) {
+ $c = $this->memcache();
+
+ if (!empty($c)) {
+ $c->delete($this->cacheKey($this->tableName(), 'user_id', $user_id));
+ }
+ }
+
+ return $result;
}
static function bulkInsert($notice_id, $user_ids)
@@ -91,7 +149,10 @@ class Inbox extends Memcached_DataObject
$inbox = Inbox::staticGet('user_id', $user_id);
if (empty($inbox)) {
- return array();
+ $inbox = Inbox::initialize($user_id);
+ if (empty($inbox)) {
+ return array();
+ }
}
$ids = unpack('N*', $inbox->notice_ids);