function common_at_hash_link($sender_id, $tag) { $user = User::staticGet($sender_id); if (!$user) { return $tag; } $tagged = Profile_tag::getTagged($user->id, common_canonical_tag($tag)); if ($tagged) { $url = common_local_url('subscriptions', array('nickname' => $user->nickname, 'tag' => $tag)); $xs = new XMLStringer(); $xs->elementStart('span', 'tag'); $xs->element('a', array('href' => $url, 'rel' => $tag), $tag); $xs->elementEnd('span'); return $xs->getString(); } else { return $tag; } }
function common_find_mentions($text, $notice) { $mentions = array(); $sender = Profile::staticGet('id', $notice->profile_id); if (empty($sender)) { return $mentions; } if (Event::handle('StartFindMentions', array($sender, $text, &$mentions))) { // Get the context of the original notice, if any $originalAuthor = null; $originalNotice = null; $originalMentions = array(); // Is it a reply? if (!empty($notice) && !empty($notice->reply_to)) { $originalNotice = Notice::staticGet('id', $notice->reply_to); if (!empty($originalNotice)) { $originalAuthor = Profile::staticGet('id', $originalNotice->profile_id); $ids = $originalNotice->getReplies(); foreach ($ids as $id) { $repliedTo = Profile::staticGet('id', $id); if (!empty($repliedTo)) { $originalMentions[$repliedTo->nickname] = $repliedTo; } } } } preg_match_all('/^T ([A-Z0-9]{1,64}) /u', $text, $tmatches, PREG_OFFSET_CAPTURE); preg_match_all('/(?:^|\\s+)@([' . NICKNAME_FMT . ']{1,64})/', $text, $atmatches, PREG_OFFSET_CAPTURE); $matches = array_merge($tmatches[1], $atmatches[1]); foreach ($matches as $match) { $nickname = common_canonical_nickname($match[0]); // Try to get a profile for this nickname. // Start with conversation context, then go to // sender context. if (!empty($originalAuthor) && $originalAuthor->nickname == $nickname) { $mentioned = $originalAuthor; } else { if (!empty($originalMentions) && array_key_exists($nickname, $originalMentions)) { $mentioned = $originalMentions[$nickname]; } else { $mentioned = common_relative_profile($sender, $nickname); } } if (!empty($mentioned)) { $user = User::staticGet('id', $mentioned->id); if ($user) { $url = common_local_url('userbyid', array('id' => $user->id)); } else { $url = $mentioned->profileurl; } $mention = array('mentioned' => array($mentioned), 'text' => $match[0], 'position' => $match[1], 'url' => $url); if (!empty($mentioned->fullname)) { $mention['title'] = $mentioned->fullname; } $mentions[] = $mention; } } // @#tag => mention of all subscriptions tagged 'tag' preg_match_all('/(?:^|[\\s\\.\\,\\:\\;]+)@#([\\pL\\pN_\\-\\.]{1,64})/u', $text, $hmatches, PREG_OFFSET_CAPTURE); foreach ($hmatches[1] as $hmatch) { $tag = common_canonical_tag($hmatch[0]); $tagged = Profile_tag::getTagged($sender->id, $tag); $url = common_local_url('subscriptions', array('nickname' => $sender->nickname, 'tag' => $tag)); $mentions[] = array('mentioned' => $tagged, 'text' => $hmatch[0], 'position' => $hmatch[1], 'url' => $url); } Event::handle('EndFindMentions', array($sender, $text, &$mentions)); } return $mentions; }
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; } else { $replied[$recipient->id] = 1; } } } } } foreach (array_keys($replied) as $recipient) { $user = User::staticGet('id', $recipient); if ($user) { mail_notify_attn($user, $this); } } }