2016-07-01 01:55:44 +08:00
|
|
|
import { ajax } from 'discourse/lib/ajax';
|
2017-03-29 00:16:58 +08:00
|
|
|
import { userPath } from 'discourse/lib/url';
|
2016-10-24 18:46:22 +08:00
|
|
|
|
2015-11-30 14:03:47 +08:00
|
|
|
function replaceSpan($e, username, opts) {
|
2016-11-15 11:03:16 +08:00
|
|
|
let extra = "";
|
|
|
|
let extraClass = "";
|
|
|
|
|
2015-11-30 14:03:47 +08:00
|
|
|
if (opts && opts.group) {
|
2015-12-04 10:40:38 +08:00
|
|
|
if (opts.mentionable) {
|
2016-10-24 18:46:22 +08:00
|
|
|
extra = `data-name='${username}' data-mentionable-user-count='${opts.mentionable.user_count}'`;
|
|
|
|
extraClass = "notify";
|
2015-12-04 10:40:38 +08:00
|
|
|
}
|
2016-10-24 18:46:22 +08:00
|
|
|
$e.replaceWith(`<a href='${Discourse.getURL("/groups/") + username}' class='mention-group ${extraClass}' ${extra}>@${username}</a>`);
|
2015-11-30 14:03:47 +08:00
|
|
|
} else {
|
2016-11-15 11:03:16 +08:00
|
|
|
if (opts && opts.cannot_see) {
|
|
|
|
extra = `data-name='${username}'`;
|
|
|
|
extraClass = "cannot-see";
|
|
|
|
}
|
2017-03-29 00:16:58 +08:00
|
|
|
$e.replaceWith(`<a href='${userPath(username.toLowerCase())}' class='mention ${extraClass}' ${extra}>@${username}</a>`);
|
2015-11-30 14:03:47 +08:00
|
|
|
}
|
2015-06-11 22:31:43 +08:00
|
|
|
}
|
|
|
|
|
2016-10-24 18:46:22 +08:00
|
|
|
const found = {};
|
|
|
|
const foundGroups = {};
|
|
|
|
const mentionableGroups = {};
|
|
|
|
const checked = {};
|
2016-11-15 11:03:16 +08:00
|
|
|
const cannotSee = [];
|
2015-06-11 22:31:43 +08:00
|
|
|
|
|
|
|
function updateFound($mentions, usernames) {
|
|
|
|
Ember.run.scheduleOnce('afterRender', function() {
|
|
|
|
$mentions.each((i, e) => {
|
|
|
|
const $e = $(e);
|
|
|
|
const username = usernames[i];
|
2016-10-24 18:46:22 +08:00
|
|
|
if (found[username.toLowerCase()]) {
|
2016-11-15 11:03:16 +08:00
|
|
|
replaceSpan($e, username, { cannot_see: cannotSee[username] });
|
2016-10-24 18:46:22 +08:00
|
|
|
} else if (foundGroups[username]) {
|
|
|
|
replaceSpan($e, username, { group: true, mentionable: mentionableGroups[username] });
|
|
|
|
} else if (checked[username]) {
|
2015-06-12 03:12:16 +08:00
|
|
|
$e.addClass('mention-tested');
|
2015-06-11 22:31:43 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-06-12 03:12:16 +08:00
|
|
|
export function linkSeenMentions($elem, siteSettings) {
|
|
|
|
const $mentions = $('span.mention:not(.mention-tested)', $elem);
|
|
|
|
if ($mentions.length) {
|
2015-06-15 22:57:41 +08:00
|
|
|
const usernames = $mentions.map((_, e) => $(e).text().substr(1));
|
2015-06-12 03:12:16 +08:00
|
|
|
updateFound($mentions, usernames);
|
2016-10-24 18:46:22 +08:00
|
|
|
return _.uniq(usernames).filter(u => !checked[u] && u.length >= siteSettings.min_username_length);
|
2015-06-12 03:12:16 +08:00
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
2015-06-11 22:31:43 +08:00
|
|
|
|
2016-11-16 19:26:36 +08:00
|
|
|
// 'Create a New Topic' scenario is not supported (per conversation with codinghorror)
|
|
|
|
// https://meta.discourse.org/t/taking-another-1-7-release-task/51986/7
|
2016-11-15 11:03:16 +08:00
|
|
|
export function fetchUnseenMentions(usernames, topic_id) {
|
2017-03-29 00:16:58 +08:00
|
|
|
return ajax(userPath("is_local_username"), { data: { usernames, topic_id } }).then(r => {
|
2016-10-24 18:46:22 +08:00
|
|
|
r.valid.forEach(v => found[v] = true);
|
|
|
|
r.valid_groups.forEach(vg => foundGroups[vg] = true);
|
2016-11-15 11:03:16 +08:00
|
|
|
r.mentionable_groups.forEach(mg => mentionableGroups[mg.name] = mg);
|
|
|
|
r.cannot_see.forEach(cs => cannotSee[cs] = true);
|
2016-10-24 18:46:22 +08:00
|
|
|
usernames.forEach(u => checked[u] = true);
|
2015-12-04 10:40:38 +08:00
|
|
|
return r;
|
2015-06-12 03:12:16 +08:00
|
|
|
});
|
2015-06-11 22:31:43 +08:00
|
|
|
}
|