Files: 5b028e25819da101de6c6a94f246cdb26c15ae2e / mentions.js
2424 bytesRaw
1 | var mentions = require('ssb-mentions') |
2 | |
3 | /* |
4 | when javascript is turned off we can't use auto predict. |
5 | this method lets you just enter the names as raw mentions, |
6 | @{name} and then looks up the names you might use. |
7 | * if it's unambigious it uses that key. |
8 | * if there is more than one with that name, but only |
9 | one that you call that name (other peers can use the same name) |
10 | then it chooses the one you use. |
11 | * if there are more than one feed you call that name, |
12 | make a dropdown select which one you will mention. |
13 | |
14 | */ |
15 | |
16 | function simpleAt (text) { |
17 | var rx = /(?:^|\s)(@[\w\-_\d\!]+)/g |
18 | var a = [] |
19 | var match = rx.exec(text) |
20 | while(match) { |
21 | a.push({name:match[1].substring(1), link: false}) |
22 | match = rx.exec(text) |
23 | } |
24 | return a |
25 | |
26 | } |
27 | |
28 | function _mentions (text, cb) { |
29 | var m = mentions(text) |
30 | simpleAt(text).forEach(function (e) { |
31 | for(var i = 0; i < m.length; i++) |
32 | if(m[i].name == e.name) return |
33 | m.push(e) |
34 | }) |
35 | return m |
36 | } |
37 | |
38 | function lookup (sbot, name, cb) { |
39 | var mention = {name: name, link: null} |
40 | sbot.names.getSignifies(name, function (err, names) { |
41 | if(names && names.length) { |
42 | names = names.filter(function (e) { |
43 | return e.name == name |
44 | }) |
45 | var first = names[0] |
46 | names = names.filter(function (e, i) { |
47 | return !i || first.id !== e.id |
48 | }) |
49 | if(names.length) { |
50 | var _names = names.filter(function (e) { |
51 | return e.named === name |
52 | }) |
53 | if(_names.length === 1) |
54 | return cb(null, _names[0].id) |
55 | } |
56 | if(names.length === 1) |
57 | cb(null, first.id) |
58 | else |
59 | cb(null, null, names) |
60 | cb(null, mention) |
61 | } |
62 | }) |
63 | } |
64 | |
65 | module.exports = function (sbot, text, cb) { |
66 | var m = _mentions(text), n = 1, ambigious = [] |
67 | m.forEach(function (mention, i) { |
68 | if(mention.name && mention.link === false) { |
69 | n++ |
70 | lookup(sbot, mention.name, function (err, link, names) { |
71 | if(err) next(err) |
72 | if(link) mention.link = link |
73 | else ambigious.push(names) |
74 | next() |
75 | }) |
76 | } |
77 | }) |
78 | |
79 | next() |
80 | |
81 | function next (err) { |
82 | if(n>=0 && err) { |
83 | n = -1 |
84 | cb(err) |
85 | } |
86 | if(--n) return |
87 | cb(null, m, ambigious) |
88 | } |
89 | } |
90 | |
91 | if(!module.parent) |
92 | require('ssb-client')(function (err, sbot) { |
93 | if(err) throw err |
94 | module.exports(sbot, process.argv[2], function (err, data) { |
95 | console.log(JSON.stringify(data, null, 2)) |
96 | sbot.close() |
97 | }) |
98 | }) |
99 | |
100 | |
101 |
Built with git-ssb-web