Files: 1de8481b3ecc82fb5558ec44c90f9731df320b05 / about / async / suggest.js
2611 bytesRaw
1 | var nest = require('depnest') |
2 | var { Struct, map, concat, dictToCollection, computed, lookup, watch, keys, resolve } = require('mutant') |
3 | |
4 | exports.gives = nest('about.async.suggest') |
5 | |
6 | exports.needs = nest({ |
7 | 'about.obs': { |
8 | name: 'first', |
9 | names: 'first', |
10 | imageUrl: 'first' |
11 | }, |
12 | 'contact.obs.following': 'first', |
13 | 'feed.obs.recent': 'first', |
14 | 'keys.sync.id': 'first' |
15 | }) |
16 | |
17 | exports.create = function (api) { |
18 | var suggestions = null |
19 | var recentSuggestions = null |
20 | |
21 | return nest('about.async.suggest', suggest) |
22 | |
23 | function suggest () { |
24 | loadSuggestions() |
25 | return function (word) { |
26 | if (!word) { |
27 | return recentSuggestions() |
28 | } else { |
29 | return suggestions().filter((item) => { |
30 | return item.title.toLowerCase().startsWith(word.toLowerCase()) |
31 | }) |
32 | } |
33 | } |
34 | } |
35 | |
36 | function loadSuggestions () { |
37 | if (suggestions) return |
38 | |
39 | var id = api.keys.sync.id() |
40 | var following = api.contact.obs.following(id) |
41 | var recentlyUpdated = api.feed.obs.recent() |
42 | var contacts = computed([following, recentlyUpdated], (a, b) => { |
43 | var result = Array.from(a) |
44 | b.forEach(item => { |
45 | if (!result.includes(item)) { |
46 | result.push(item) |
47 | } |
48 | }) |
49 | return result |
50 | }) |
51 | |
52 | recentSuggestions = map( |
53 | computed(recentlyUpdated, (items) => Array.from(items).slice(0, 10)), |
54 | suggestion, |
55 | {idle: true} |
56 | ) |
57 | |
58 | const suggestionsRecord = lookup(contacts, contact => { |
59 | return [contact, keys(api.about.obs.names(contact))] |
60 | }) |
61 | |
62 | suggestions = concat( |
63 | map(dictToCollection(suggestionsRecord), pluralSuggestions, {idle: true}) |
64 | ) |
65 | |
66 | watch(recentSuggestions) |
67 | watch(suggestions) |
68 | } |
69 | |
70 | function pluralSuggestions (item) { |
71 | const id = resolve(item.key) |
72 | return map(item.value, name => { |
73 | return Struct({ |
74 | id, |
75 | title: name, |
76 | subtitle: subtitle(id, name), |
77 | value: computed([name, id], mention), |
78 | image: api.about.obs.imageUrl(id), |
79 | showBoth: true |
80 | }) |
81 | }) |
82 | } |
83 | |
84 | function suggestion (id) { |
85 | var name = api.about.obs.name(id) |
86 | return Struct({ |
87 | title: name, |
88 | id, |
89 | subtitle: id.substring(0, 10), |
90 | value: computed([name, id], mention), |
91 | image: api.about.obs.imageUrl(id), |
92 | showBoth: true |
93 | }) |
94 | } |
95 | |
96 | function subtitle (id, name) { |
97 | return computed([api.about.obs.name(id)], commonName => { |
98 | return name.toLowerCase() === commonName.toLowerCase() |
99 | ? id.substring(0, 10) |
100 | : `${commonName} ${id.substring(0, 10)}` |
101 | }) |
102 | } |
103 | } |
104 | |
105 | function mention (name, id) { |
106 | return `[@${name}](${id})` |
107 | } |
108 |
Built with git-ssb-web