Files: 18c1a9e02d04c233f4fbf7182d7079ac6419e0e9 / app / html / context.js
5218 bytesRaw
1 | const nest = require('depnest') |
2 | const { h, computed, map, when, Dict, dictToCollection, Array: MutantArray, resolve } = require('mutant') |
3 | const pull = require('pull-stream') |
4 | const next = require('pull-next-step') |
5 | const get = require('lodash/get') |
6 | const isEmpty = require('lodash/isEmpty') |
7 | |
8 | exports.gives = nest('app.html.context') |
9 | |
10 | exports.needs = nest({ |
11 | 'about.html.avatar': 'first', |
12 | 'about.obs.name': 'first', |
13 | 'feed.pull.private': 'first', |
14 | 'feed.pull.rollup': 'first', |
15 | 'keys.sync.id': 'first', |
16 | 'history.sync.push': 'first', |
17 | 'message.html.subject': 'first', |
18 | 'sbot.obs.localPeers': 'first', |
19 | 'translations.sync.strings': 'first', |
20 | }) |
21 | |
22 | |
23 | exports.create = (api) => { |
24 | return nest('app.html.context', (location) => { |
25 | |
26 | const strings = api.translations.sync.strings() |
27 | const myKey = api.keys.sync.id() |
28 | |
29 | var nearby = api.sbot.obs.localPeers() |
30 | var recentPeersContacted = Dict() |
31 | // TODO - extract as contact.obs.recentPrivate or something |
32 | |
33 | pull( |
34 | next(api.feed.pull.private, {reverse: true, limit: 100, live: false}, ['value', 'timestamp']), |
35 | pull.filter(msg => msg.value.content.type === 'post'), // TODO is this the best way to protect against votes? |
36 | pull.filter(msg => msg.value.content.recps), |
37 | pull.drain(msg => { |
38 | msg.value.content.recps |
39 | .map(recp => typeof recp === 'object' ? recp.link : recp) |
40 | .filter(recp => recp != myKey) |
41 | .forEach(recp => { |
42 | if (recentPeersContacted.has(recp)) return |
43 | |
44 | recentPeersContacted.put(recp, msg) |
45 | }) |
46 | }) |
47 | ) |
48 | |
49 | return h('Context -feed', [ |
50 | LevelOneContext(), |
51 | LevelTwoContext() |
52 | ]) |
53 | |
54 | function LevelOneContext () { |
55 | const PAGES_UNDER_DISCOVER = ['blogIndex', 'blogShow', 'home'] |
56 | |
57 | return h('div.level.-one', [ |
58 | // Nearby |
59 | computed(nearby, n => !isEmpty(n) ? h('header', strings.peopleNearby) : null), |
60 | map(nearby, feedId => Option({ |
61 | notifications: Math.random() > 0.7 ? Math.floor(Math.random()*9+1) : 0, // TODO |
62 | imageEl: api.about.html.avatar(feedId), |
63 | label: api.about.obs.name(feedId), |
64 | selected: location.feed === feedId, |
65 | location: computed(recentPeersContacted, recent => { |
66 | const lastMsg = recent[feedId] |
67 | return lastMsg |
68 | ? Object.assign(lastMsg, { feed: feedId }) |
69 | : { page: 'threadNew', feed: feedId } |
70 | }), |
71 | })), |
72 | computed(nearby, n => !isEmpty(n) ? h('hr') : null), |
73 | |
74 | // Discover |
75 | Option({ |
76 | notifications: Math.floor(Math.random()*5+1), |
77 | imageEl: h('i.fa.fa-binoculars'), |
78 | label: strings.blogIndex.title, |
79 | selected: PAGES_UNDER_DISCOVER.includes(location.page), |
80 | location: { page: 'blogIndex' }, |
81 | }), |
82 | |
83 | // Recent Messages |
84 | map(dictToCollection(recentPeersContacted), ({ key, value }) => { |
85 | const feedId = key() |
86 | const lastMsg = value() |
87 | if (nearby.has(feedId)) return |
88 | |
89 | return Option({ |
90 | notifications: Math.random() > 0.7 ? Math.floor(Math.random()*9+1) : 0, // TODO |
91 | imageEl: api.about.html.avatar(feedId), |
92 | label: api.about.obs.name(feedId), |
93 | selected: location.feed === feedId, |
94 | location: Object.assign({}, lastMsg, { feed: feedId }) // TODO make obs? |
95 | }) |
96 | }) |
97 | ]) |
98 | } |
99 | |
100 | function LevelTwoContext () { |
101 | const { key, value, feed: targetUser, page } = location |
102 | const root = get(value, 'content.root', key) |
103 | if (!targetUser) return |
104 | |
105 | var threads = MutantArray() |
106 | |
107 | pull( |
108 | next(api.feed.pull.private, {reverse: true, limit: 100, live: false}, ['value', 'timestamp']), |
109 | pull.filter(msg => msg.value.content.recps), |
110 | pull.filter(msg => msg.value.content.recps |
111 | .map(recp => typeof recp === 'object' ? recp.link : recp) |
112 | .some(recp => recp === targetUser) |
113 | ), |
114 | api.feed.pull.rollup(), |
115 | pull.drain(thread => threads.push(thread)) |
116 | ) |
117 | |
118 | return h('div.level.-two', [ |
119 | Option({ |
120 | selected: page === 'threadNew', |
121 | location: {page: 'threadNew', feed: targetUser}, |
122 | label: h('Button', strings.threadNew.action.new), |
123 | }), |
124 | map(threads, thread => { |
125 | return Option({ |
126 | label: api.message.html.subject(thread), |
127 | selected: thread.key === root, |
128 | location: Object.assign(thread, { feed: targetUser }), |
129 | }) |
130 | }) |
131 | ]) |
132 | } |
133 | |
134 | function Option ({ notifications = 0, imageEl, label, location, selected }) { |
135 | const className = selected ? '-selected' : '' |
136 | const goToLocation = (e) => { |
137 | e.preventDefault() |
138 | e.stopPropagation() |
139 | api.history.sync.push(resolve(location)) |
140 | } |
141 | |
142 | if (!imageEl) { |
143 | return h('Option', { className, 'ev-click': goToLocation }, [ |
144 | h('div.label', label) |
145 | ]) |
146 | } |
147 | |
148 | return h('Option', { className }, [ |
149 | h('div.circle', [ |
150 | when(notifications, h('div.alert', notifications)), |
151 | imageEl |
152 | ]), |
153 | h('div.label', { 'ev-click': goToLocation }, label) |
154 | ]) |
155 | } |
156 | }) |
157 | } |
158 | |
159 |
Built with git-ssb-web