git ssb

10+

Matt McKegg / patchwork



Tree: 271cbdd2add56b26f6e997f982fb76db39f59d06

Files: 271cbdd2add56b26f6e997f982fb76db39f59d06 / modules / channel / obs / suggest.js

1523 bytesRaw
1var nest = require('depnest')
2var {Struct, map, computed, watch} = require('mutant')
3
4exports.needs = nest({
5 'channel.obs.recent': 'first',
6 'channel.obs.subscribed': 'first',
7 'keys.sync.id': 'first'
8})
9
10exports.gives = nest('channel.async.suggest')
11
12exports.create = function (api) {
13 var suggestions = null
14 var subscribed = null
15
16 return nest('channel.async.suggest', function () {
17 loadSuggestions()
18 return function (word) {
19 if (!word) {
20 return suggestions().slice(0, 200)
21 } else {
22 return suggestions().filter((item) => {
23 return item.title.toLowerCase().startsWith(word.toLowerCase())
24 })
25 }
26 }
27 })
28
29 function loadSuggestions () {
30 if (!suggestions) {
31 var id = api.keys.sync.id()
32 subscribed = api.channel.obs.subscribed(id)
33 var recentlyUpdated = api.channel.obs.recent()
34 var contacts = computed([subscribed, recentlyUpdated], function (a, b) {
35 var result = Array.from(a)
36 b.forEach((item, i) => {
37 if (!result.includes(item)) {
38 result.push(item)
39 }
40 })
41 return result
42 })
43
44 suggestions = map(contacts, suggestion, {idle: true})
45 watch(suggestions)
46 }
47 }
48
49 function suggestion (id) {
50 return Struct({
51 title: id,
52 id: `#${id}`,
53 subtitle: computed([id, subscribed], subscribedCaption),
54 value: `#${id}`
55 })
56 }
57}
58
59function subscribedCaption (id, subscribed) {
60 if (subscribed.has(id)) {
61 return 'subscribed'
62 }
63}
64

Built with git-ssb-web