nodebb-plugin-mentions 4.8.0 → 4.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/library.js CHANGED
@@ -360,11 +360,17 @@ Mentions.getMatches = async (content) => {
360
360
  // Exported method only accepts markdown, also filters out dupes and matches to ensure slugs exist
361
361
  let { matches } = await getMatches(content, true);
362
362
  matches = await filterMatches(matches);
363
+ if (!matches.length) {
364
+ return new Set();
365
+ }
366
+
363
367
  const normalized = matches.map(match => match.slice(1).toLowerCase());
364
- const [uids, cids] = await Promise.all([
368
+ let [uids, localCids, remoteCids] = await Promise.all([
365
369
  User.getUidsByUserslugs(normalized),
366
370
  db.sortedSetScores('categoryhandle:cid', normalized),
371
+ db.getObjectFields('handle:cid', normalized),
367
372
  ]);
373
+ remoteCids = Object.values(remoteCids);
368
374
 
369
375
  matches = matches.map((slug, idx) => {
370
376
  if (uids[idx]) {
@@ -373,10 +379,10 @@ Mentions.getMatches = async (content) => {
373
379
  id: uids[idx],
374
380
  slug,
375
381
  };
376
- } else if (cids[idx]) {
382
+ } else if (localCids[idx] || remoteCids[idx]) {
377
383
  return {
378
384
  type: 'cid',
379
- id: cids[idx],
385
+ id: localCids[idx] || remoteCids[idx],
380
386
  slug,
381
387
  };
382
388
  }
@@ -389,9 +395,11 @@ Mentions.getMatches = async (content) => {
389
395
 
390
396
  async function filterMatches(matches) {
391
397
  matches = Array.from(new Set(matches));
392
- const exists = await meta.userOrGroupExists(matches.map(match => match.slice(1)));
398
+ const slugs = matches.map(match => match.slice(1));
399
+ const exists = await meta.slugTaken(slugs);
400
+ const remoteCidExists = await db.isObjectFields('handle:cid', slugs);
393
401
 
394
- return matches.filter((m, i) => exists[[i]]);
402
+ return matches.filter((m, i) => exists[i] || remoteCidExists[i]);
395
403
  }
396
404
 
397
405
  Mentions.parseRaw = async (content, type = 'default') => {
package/package-lock.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "nodebb-plugin-mentions",
3
- "version": "4.8.0",
3
+ "version": "4.8.1",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "nodebb-plugin-mentions",
9
- "version": "4.8.0",
9
+ "version": "4.8.1",
10
10
  "license": "MIT",
11
11
  "dependencies": {
12
12
  "html-entities": "^2.3.2",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodebb-plugin-mentions",
3
- "version": "4.8.0",
3
+ "version": "4.8.1",
4
4
  "description": "NodeBB Plugin that allows users to mention other users by prepending an '@' sign to their username",
5
5
  "main": "library.js",
6
6
  "repository": {
@@ -6,6 +6,7 @@ $(document).ready(function () {
6
6
  let categoryList;
7
7
  const categorySlugMap = new Map();
8
8
  let localUserList = [];
9
+ let helpers;
9
10
 
10
11
  $(window).on('composer:autocomplete:init chat:autocomplete:init', function (ev, data) {
11
12
  loadTopicUsers(data.element);
@@ -22,11 +23,11 @@ $(document).ready(function () {
22
23
  const strategy = {
23
24
  match: /\B@([^\s\n]*)?$/,
24
25
  search: function (term, callback) {
25
- require(['composer', 'helpers', 'slugify'], function (composer, helpers, _slugify) {
26
+ require(['composer', 'helpers', 'slugify'], function (composer, _helpers, _slugify) {
26
27
  slugify = _slugify;
27
- let mentions = [];
28
+ helpers = _helpers;
28
29
  if (!term) {
29
- mentions = entriesToMentions(sortEntries(localUserList), helpers);
30
+ const mentions = entriesToMentions(sortEntries(localUserList), helpers);
30
31
  return callback(mentions);
31
32
  }
32
33
 
@@ -56,7 +57,7 @@ $(document).ready(function () {
56
57
  // remove local matches from search results, add category matches
57
58
  users = users.filter(u => !localMatches.find(lu => lu.uid === u.uid));
58
59
  users = sortEntries(localMatches).concat(sortEntries([...users, ...categoryMatches]));
59
- mentions = entriesToMentions(users, helpers);
60
+ // mentions = entriesToMentions(users, helpers);
60
61
 
61
62
  // Add groups that start with the search term
62
63
  const groupMentions = groupList.filter(function (groupName) {
@@ -66,27 +67,20 @@ $(document).ready(function () {
66
67
  });
67
68
 
68
69
  // Add group mentions at the bottom of dropdown
69
- mentions = mentions.concat(groupMentions);
70
+ // mentions = mentions.concat(groupMentions);
70
71
 
71
- callback(mentions);
72
+ callback([...users, ...groupMentions]);
72
73
  });
73
74
  });
74
75
  },
75
76
  index: 1,
77
+ template: entryToMention,
76
78
  replace: function (mention) {
77
- // Strip (fullname) part from mentions
78
- mention = mention.replace(/ \(.+\)$/, '');
79
- mention = $('<div/>').html(mention);
80
- // Strip letter avatar
81
- mention.find('span').remove();
82
-
83
- const text = mention.text().trim();
84
- const categoryHandle = categorySlugMap.get(text.toLowerCase());
85
- if (categoryHandle) {
86
- return `@${categoryHandle}`;
79
+ if (mention.uid) {
80
+ return `@${mention.userslug}`;
81
+ } else if (mention.cid) {
82
+ return `@${utils.isNumber(mention.cid) ? mention.handle : mention.slug}`;
87
83
  }
88
-
89
- return '@' + slugify(text, true) + ' ';
90
84
  },
91
85
  cache: true,
92
86
  };
@@ -107,6 +101,25 @@ $(document).ready(function () {
107
101
  });
108
102
  }
109
103
 
104
+ function entryToMention(entry) {
105
+ // Format suggestions as 'avatar username/name (fullname/slug)'
106
+ switch(true) {
107
+ case entry.hasOwnProperty('uid'): {
108
+ const avatar = helpers.buildAvatar(entry, '24px', true);
109
+ const fullname = entry.fullname ? `(${entry.fullname})` : '';
110
+ return `${avatar} ${entry.username || entry.name} ${helpers.escape(fullname)}`;
111
+ }
112
+
113
+ case entry.hasOwnProperty('cid'): {
114
+ const avatar = helpers.buildCategoryIcon(entry, '24px', 'rounded-circle');
115
+ return `${avatar} ${entry.name}${!utils.isNumber(entry.cid) ? ` (${entry.slug})` : ''}`;
116
+ }
117
+
118
+ default:
119
+ return entry.name;
120
+ }
121
+ }
122
+
110
123
  function entriesToMentions(entries, helpers) {
111
124
  return entries.reduce(function (carry, entry) {
112
125
  // Don't add current user to suggestions
@@ -114,25 +127,7 @@ $(document).ready(function () {
114
127
  return carry;
115
128
  }
116
129
 
117
- // Format suggestions as 'avatar username/name (fullname/slug)'
118
- switch(true) {
119
- case entry.hasOwnProperty('uid'): {
120
- const avatar = helpers.buildAvatar(entry, '24px', true);
121
- const fullname = entry.fullname ? `(${entry.fullname})` : '';
122
- carry.push(`${avatar} ${entry.username || entry.name} ${helpers.escape(fullname)}`);
123
- break;
124
- }
125
-
126
- case entry.hasOwnProperty('cid'): {
127
- const avatar = helpers.buildCategoryIcon(entry, '24px', 'rounded-circle');
128
- carry.push(`${avatar} ${entry.name}${!utils.isNumber(entry.cid) ? ` (${entry.slug})` : ''}`);
129
- break;
130
- }
131
-
132
- default:
133
- carry.push(entry.name);
134
- }
135
-
130
+ carry.push(entryToMention(entry));
136
131
  return carry;
137
132
  }, []);
138
133
  }