# XXX: do we need to change this for remote users?
- common_save_replies($notice);
+ $notice->saveReplies();
$notice->saveTags();
+ $notice->saveGroups();
# Clear the cache for subscribed users, so they'll update at next request
# XXX: someone clever could prepend instead of clearing the cache
$this->blowRepliesCache($blowLast);
$this->blowPublicCache($blowLast);
$this->blowTagCache($blowLast);
+ $this->blowGroupCache($blowLast);
+ }
+
+ function blowGroupCache($blowLast=false)
+ {
+ $cache = common_memcache();
+ if ($cache) {
+ $group_inbox = new Group_inbox();
+ $group_inbox->notice_id = $this->id;
+ if ($group_inbox->find()) {
+ while ($group_inbox->fetch()) {
+ $cache->delete(common_cache_key('group:notices:'.$group_inbox->group_id));
+ if ($blowLast) {
+ $cache->delete(common_cache_key('group:notices:'.$group_inbox->group_id.';last'));
+ }
+ $member = new Group_member();
+ $member->group_id = $group_inbox->group_id;
+ if ($member->find()) {
+ while ($member->fetch()) {
+ $cache->delete(common_cache_key('user:notices_with_friends:' . $member->profile_id));
+ if ($blowLast) {
+ $cache->delete(common_cache_key('user:notices_with_friends:' . $member->profile_id . ';last'));
+ }
+ }
+ }
+ }
+ }
+ $group_inbox->free();
+ unset($group_inbox);
+ }
}
function blowTagCache($blowLast=false)
# On a cache hit, return a DB-object-like wrapper
if ($notices !== false) {
- $wrapper = new NoticeWrapper(array_slice($notices, $offset, $limit));
+ $wrapper = new ArrayWrapper(array_slice($notices, $offset, $limit));
return $wrapper;
}
# return a wrapper of the array for use now
- return new NoticeWrapper(array_slice($notices, $offset, $limit));
+ return new ArrayWrapper(array_slice($notices, $offset, $limit));
}
}
# return a wrapper of the array for use now
- $wrapper = new NoticeWrapper(array_slice($notices, $offset, $limit));
+ $wrapper = new ArrayWrapper(array_slice($notices, $offset, $limit));
return $wrapper;
}
return;
}
-}
+ function saveGroups()
+ {
+ $enabled = common_config('inboxes', 'enabled');
+ if ($enabled !== true && $enabled !== 'transitional') {
+ return;
+ }
+
+ /* extract all !group */
+ $count = preg_match_all('/(?:^|\s)!([A-Za-z0-9]{1,64})/',
+ strtolower($this->content),
+ $match);
+ if (!$count) {
+ return true;
+ }
+
+ $profile = $this->getProfile();
+
+ /* Add them to the database */
+
+ foreach (array_unique($match[1]) as $nickname) {
+ /* XXX: remote groups. */
+ $group = User_group::staticGet('nickname', $nickname);
+
+ if (!$group) {
+ continue;
+ }
+
+ if ($profile->isMember($group)) {
+
+ $gi = new Group_inbox();
+
+ $gi->group_id = $group->id;
+ $gi->notice_id = $this->id;
+ $gi->created = common_sql_now();
+ $result = $gi->insert();
+
+ if (!$result) {
+ common_log_db_error($gi, 'INSERT', __FILE__);
+ }
+
+ // FIXME: do this in an offline daemon
+
+ $inbox = new Notice_inbox();
+ $qry = 'INSERT INTO notice_inbox (user_id, notice_id, created, source) ' .
+ 'SELECT user.id, ' . $this->id . ', "' . $this->created . '", 2 ' .
+ 'FROM user JOIN group_member ON user.id = group_member.profile_id ' .
+ 'WHERE group_member.group_id = ' . $group->id . ' ' .
+ 'AND NOT EXISTS (SELECT user_id, notice_id ' .
+ 'FROM notice_inbox ' .
+ 'WHERE user_id = user.id ' .
+ 'AND notice_id = ' . $this->id . ' )';
+ if ($enabled === 'transitional') {
+ $qry .= ' AND user.inboxed = 1';
+ }
+ $result = $inbox->query($qry);
+ }
+ }
+ }
+
+ function saveReplies()
+ {
+ // Alternative reply format
+ $tname = false;
+ if (preg_match('/^T ([A-Z0-9]{1,64}) /', $this->content, $match)) {
+ $tname = $match[1];
+ }
+ // extract all @messages
+ $cnt = preg_match_all('/(?:^|\s)@([a-z0-9]{1,64})/', $this->content, $match);
+
+ $names = array();
+
+ if ($cnt || $tname) {
+ // XXX: is there another way to make an array copy?
+ $names = ($tname) ? array_unique(array_merge(array(strtolower($tname)), $match[1])) : array_unique($match[1]);
+ }
+
+ $sender = Profile::staticGet($this->profile_id);
+
+ $replied = array();
+
+ // store replied only for first @ (what user/notice what the reply directed,
+ // we assume first @ is it)
+
+ for ($i=0; $i<count($names); $i++) {
+ $nickname = $names[$i];
+ $recipient = common_relative_profile($sender, $nickname, $this->created);
+ if (!$recipient) {
+ continue;
+ }
+ if ($i == 0 && ($recipient->id != $sender->id) && !$this->reply_to) { // Don't save reply to self
+ $reply_for = $recipient;
+ $recipient_notice = $reply_for->getCurrentNotice();
+ if ($recipient_notice) {
+ $orig = clone($this);
+ $this->reply_to = $recipient_notice->id;
+ $this->update($orig);
+ }
+ }
+ // Don't save replies from blocked profile to local user
+ $recipient_user = User::staticGet('id', $recipient->id);
+ if ($recipient_user && $recipient_user->hasBlocked($sender)) {
+ continue;
+ }
+ $reply = new Reply();
+ $reply->notice_id = $this->id;
+ $reply->profile_id = $recipient->id;
+ $id = $reply->insert();
+ if (!$id) {
+ $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
+ common_log(LOG_ERR, 'DB error inserting reply: ' . $last_error->message);
+ common_server_error(sprintf(_('DB error inserting reply: %s'), $last_error->message));
+ return;
+ } else {
+ $replied[$recipient->id] = 1;
+ }
+ }
+
+ // Hash format replies, too
+ $cnt = preg_match_all('/(?:^|\s)@#([a-z0-9]{1,64})/', $this->content, $match);
+ if ($cnt) {
+ foreach ($match[1] as $tag) {
+ $tagged = Profile_tag::getTagged($sender->id, $tag);
+ foreach ($tagged as $t) {
+ if (!$replied[$t->id]) {
+ // Don't save replies from blocked profile to local user
+ $t_user = User::staticGet('id', $t->id);
+ if ($t_user && $t_user->hasBlocked($sender)) {
+ continue;
+ }
+ $reply = new Reply();
+ $reply->notice_id = $this->id;
+ $reply->profile_id = $t->id;
+ $id = $reply->insert();
+ if (!$id) {
+ common_log_db_error($reply, 'INSERT', __FILE__);
+ return;
+ }
+ }
+ }
+ }
+ }
+ }
+}