2011-06-24 19:59:57 +02:00
|
|
|
<?php
|
|
|
|
|
2014-08-04 16:55:43 +02:00
|
|
|
final class PhabricatorMentionRemarkupRule extends PhutilRemarkupRule {
|
2011-06-24 19:59:57 +02:00
|
|
|
|
2011-11-30 17:45:31 +01:00
|
|
|
const KEY_RULE_MENTION = 'rule.mention';
|
|
|
|
const KEY_RULE_MENTION_ORIGINAL = 'rule.mention.original';
|
|
|
|
|
|
|
|
const KEY_MENTIONED = 'phabricator.mentioned-user-phids';
|
2011-06-24 19:59:57 +02:00
|
|
|
|
|
|
|
|
2012-06-06 16:09:05 +02:00
|
|
|
// NOTE: The negative lookbehind prevents matches like "mail@lists", while
|
|
|
|
// allowing constructs like "@tomo/@mroch". Since we now allow periods in
|
|
|
|
// usernames, we can't resonably distinguish that "@company.com" isn't a
|
|
|
|
// username, so we'll incorrectly pick it up, but there's little to be done
|
|
|
|
// about that. We forbid terminal periods so that we can correctly capture
|
|
|
|
// "@joe" instead of "@joe." in "Hey, @joe.".
|
2014-04-26 21:31:55 +02:00
|
|
|
//
|
|
|
|
// We disallow "@@joe" because it creates a false positive in the common
|
|
|
|
// construction "l@@k", made popular by eBay.
|
|
|
|
const REGEX = '/(?<!\w|@)@([a-zA-Z0-9._-]*[a-zA-Z0-9_-])/';
|
2011-06-24 19:59:57 +02:00
|
|
|
|
2012-03-28 07:23:16 +02:00
|
|
|
public function apply($text) {
|
2013-02-22 01:13:55 +01:00
|
|
|
return preg_replace_callback(
|
2012-03-28 07:23:16 +02:00
|
|
|
self::REGEX,
|
2011-07-27 21:41:21 +02:00
|
|
|
array($this, 'markupMention'),
|
|
|
|
$text);
|
|
|
|
}
|
|
|
|
|
2015-05-04 23:33:00 +02:00
|
|
|
protected function markupMention(array $matches) {
|
2011-07-27 21:41:21 +02:00
|
|
|
$engine = $this->getEngine();
|
2013-03-23 01:33:36 +01:00
|
|
|
|
|
|
|
if ($engine->isTextMode()) {
|
|
|
|
return $engine->storeText($matches[0]);
|
|
|
|
}
|
|
|
|
|
2011-07-27 21:41:21 +02:00
|
|
|
$token = $engine->storeText('');
|
|
|
|
|
2011-11-30 17:45:31 +01:00
|
|
|
// Store the original text exactly so we can preserve casing if it doesn't
|
|
|
|
// resolve into a username.
|
|
|
|
$original_key = self::KEY_RULE_MENTION_ORIGINAL;
|
|
|
|
$original = $engine->getTextMetadata($original_key, array());
|
|
|
|
$original[$token] = $matches[1];
|
|
|
|
$engine->setTextMetadata($original_key, $original);
|
|
|
|
|
2011-07-27 21:41:21 +02:00
|
|
|
$metadata_key = self::KEY_RULE_MENTION;
|
|
|
|
$metadata = $engine->getTextMetadata($metadata_key, array());
|
2011-11-30 17:45:31 +01:00
|
|
|
$username = strtolower($matches[1]);
|
2011-07-27 21:41:21 +02:00
|
|
|
if (empty($metadata[$username])) {
|
|
|
|
$metadata[$username] = array();
|
2011-06-24 19:59:57 +02:00
|
|
|
}
|
2011-07-27 21:41:21 +02:00
|
|
|
$metadata[$username][] = $token;
|
|
|
|
$engine->setTextMetadata($metadata_key, $metadata);
|
|
|
|
|
|
|
|
return $token;
|
|
|
|
}
|
2011-06-24 19:59:57 +02:00
|
|
|
|
2011-07-27 21:41:21 +02:00
|
|
|
public function didMarkupText() {
|
|
|
|
$engine = $this->getEngine();
|
|
|
|
|
|
|
|
$metadata_key = self::KEY_RULE_MENTION;
|
|
|
|
$metadata = $engine->getTextMetadata($metadata_key, array());
|
|
|
|
if (empty($metadata)) {
|
|
|
|
// No mentions, or we already processed them.
|
|
|
|
return;
|
|
|
|
}
|
2011-06-24 19:59:57 +02:00
|
|
|
|
2011-11-30 17:45:31 +01:00
|
|
|
$original_key = self::KEY_RULE_MENTION_ORIGINAL;
|
|
|
|
$original = $engine->getTextMetadata($original_key, array());
|
|
|
|
|
2011-07-27 21:41:21 +02:00
|
|
|
$usernames = array_keys($metadata);
|
2013-04-05 18:17:27 +02:00
|
|
|
|
|
|
|
$users = id(new PhabricatorPeopleQuery())
|
2013-05-31 19:51:20 +02:00
|
|
|
->setViewer($this->getEngine()->getConfig('viewer'))
|
2013-04-05 18:17:27 +02:00
|
|
|
->withUsernames($usernames)
|
2015-05-14 20:15:04 +02:00
|
|
|
->needAvailability(true)
|
2013-04-05 18:17:27 +02:00
|
|
|
->execute();
|
|
|
|
|
2011-07-27 21:41:21 +02:00
|
|
|
$actual_users = array();
|
2011-06-24 20:50:19 +02:00
|
|
|
|
2011-07-27 21:41:21 +02:00
|
|
|
$mentioned_key = self::KEY_MENTIONED;
|
|
|
|
$mentioned = $engine->getTextMetadata($mentioned_key, array());
|
2013-04-05 18:17:27 +02:00
|
|
|
foreach ($users as $row) {
|
|
|
|
$actual_users[strtolower($row->getUserName())] = $row;
|
|
|
|
$mentioned[$row->getPHID()] = $row->getPHID();
|
2011-06-24 19:59:57 +02:00
|
|
|
}
|
|
|
|
|
2011-07-27 21:41:21 +02:00
|
|
|
$engine->setTextMetadata($mentioned_key, $mentioned);
|
2015-01-01 17:05:51 +01:00
|
|
|
$context_object = $engine->getConfig('contextObject');
|
2011-07-27 21:41:21 +02:00
|
|
|
|
|
|
|
foreach ($metadata as $username => $tokens) {
|
|
|
|
$exists = isset($actual_users[$username]);
|
2015-01-01 17:05:51 +01:00
|
|
|
$user_has_no_permission = false;
|
2011-07-27 21:41:21 +02:00
|
|
|
|
|
|
|
if ($exists) {
|
2013-04-05 18:17:27 +02:00
|
|
|
$user = $actual_users[$username];
|
|
|
|
Javelin::initBehavior('phabricator-hovercards');
|
|
|
|
|
2015-01-01 17:05:51 +01:00
|
|
|
// Check if the user has view access to the object she was mentioned in
|
|
|
|
if ($context_object
|
|
|
|
&& $context_object instanceof PhabricatorPolicyInterface) {
|
|
|
|
if (!PhabricatorPolicyFilter::hasCapability(
|
|
|
|
$user,
|
|
|
|
$context_object,
|
|
|
|
PhabricatorPolicyCapability::CAN_VIEW)) {
|
|
|
|
// User mentioned has no permission to this object
|
|
|
|
$user_has_no_permission = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-18 03:26:18 +01:00
|
|
|
$user_href = '/p/'.$user->getUserName().'/';
|
|
|
|
|
|
|
|
if ($engine->isHTMLMailMode()) {
|
|
|
|
$user_href = PhabricatorEnv::getProductionURI($user_href);
|
2015-01-01 17:05:51 +01:00
|
|
|
|
|
|
|
if ($user_has_no_permission) {
|
|
|
|
$colors = '
|
|
|
|
border-color: #92969D;
|
|
|
|
color: #92969D;
|
|
|
|
background-color: #F7F7F7;';
|
|
|
|
} else {
|
|
|
|
$colors = '
|
|
|
|
border-color: #f1f7ff;
|
|
|
|
color: #19558d;
|
|
|
|
background-color: #f1f7ff;';
|
|
|
|
}
|
|
|
|
|
2014-11-18 03:26:18 +01:00
|
|
|
$tag = phutil_tag(
|
|
|
|
'a',
|
|
|
|
array(
|
|
|
|
'href' => $user_href,
|
2015-01-01 17:05:51 +01:00
|
|
|
'style' => $colors.'
|
2014-11-18 03:26:18 +01:00
|
|
|
border: 1px solid transparent;
|
|
|
|
border-radius: 3px;
|
|
|
|
font-weight: bold;
|
|
|
|
padding: 0 4px;',
|
|
|
|
),
|
|
|
|
'@'.$user->getUserName());
|
2013-04-05 18:17:27 +02:00
|
|
|
} else {
|
2014-11-18 03:26:18 +01:00
|
|
|
$tag = id(new PHUITagView())
|
|
|
|
->setType(PHUITagView::TYPE_PERSON)
|
|
|
|
->setPHID($user->getPHID())
|
|
|
|
->setName('@'.$user->getUserName())
|
|
|
|
->setHref($user_href);
|
|
|
|
|
2015-01-01 17:05:51 +01:00
|
|
|
if ($user_has_no_permission) {
|
|
|
|
$tag->addClass('phabricator-remarkup-mention-nopermission');
|
|
|
|
}
|
|
|
|
|
2014-11-18 03:26:18 +01:00
|
|
|
if (!$user->isUserActivated()) {
|
|
|
|
$tag->setDotColor(PHUITagView::COLOR_GREY);
|
|
|
|
} else {
|
2015-05-14 20:15:04 +02:00
|
|
|
if ($user->getAwayUntil()) {
|
|
|
|
$tag->setDotColor(PHUITagView::COLOR_RED);
|
2013-04-05 18:17:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-30 17:45:31 +01:00
|
|
|
foreach ($tokens as $token) {
|
|
|
|
$engine->overwriteStoredText($token, $tag);
|
|
|
|
}
|
2011-07-27 21:41:21 +02:00
|
|
|
} else {
|
2011-11-30 17:45:31 +01:00
|
|
|
// NOTE: The structure here is different from the 'exists' branch,
|
|
|
|
// because we want to preserve the original text capitalization and it
|
|
|
|
// may differ for each token.
|
|
|
|
foreach ($tokens as $token) {
|
2013-01-18 03:43:35 +01:00
|
|
|
$tag = phutil_tag(
|
2011-11-30 17:45:31 +01:00
|
|
|
'span',
|
|
|
|
array(
|
2013-04-05 18:17:27 +02:00
|
|
|
'class' => 'phabricator-remarkup-mention-unknown',
|
2011-11-30 17:45:31 +01:00
|
|
|
),
|
2013-01-18 03:43:35 +01:00
|
|
|
'@'.idx($original, $token, $username));
|
2011-11-30 17:45:31 +01:00
|
|
|
$engine->overwriteStoredText($token, $tag);
|
|
|
|
}
|
2011-07-27 21:41:21 +02:00
|
|
|
}
|
2011-06-24 19:59:57 +02:00
|
|
|
}
|
|
|
|
|
2011-07-27 21:41:21 +02:00
|
|
|
// Don't re-process these mentions.
|
|
|
|
$engine->setTextMetadata($metadata_key, array());
|
2011-06-24 19:59:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|