Files: 88efa57edc6f7800ae827d2a28a7aab3065c1159 / app / html / context.js
5804 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 | 'app.html.scroller': 'first', |
12 | 'about.html.avatar': 'first', |
13 | 'about.obs.name': 'first', |
14 | 'feed.pull.private': 'first', |
15 | 'feed.pull.rollup': 'first', |
16 | 'keys.sync.id': 'first', |
17 | 'history.sync.push': 'first', |
18 | 'message.html.subject': 'first', |
19 | 'sbot.obs.localPeers': 'first', |
20 | 'translations.sync.strings': 'first', |
21 | }) |
22 | |
23 | |
24 | exports.create = (api) => { |
25 | return nest('app.html.context', (location) => { |
26 | |
27 | const strings = api.translations.sync.strings() |
28 | const myKey = api.keys.sync.id() |
29 | |
30 | var nearby = api.sbot.obs.localPeers() |
31 | var recentMsgLog = Dict () |
32 | function updateRecentMsgLog (msg) { |
33 | const { author, timestamp } = msg.value |
34 | |
35 | if (!recentMsgLog.has(author)) { |
36 | recentMsgLog.put(author, msg) |
37 | return |
38 | } |
39 | |
40 | const currentWinner = recentMsgLog.get(author) |
41 | if (timestamp > currentWinner.value.timestamp) { |
42 | recentMsgLog.put(author, msg) |
43 | } |
44 | } |
45 | function isLatestMsg (msg) { |
46 | const { author, timestamp } = msg.value |
47 | return recentMsgLog.get(author).value.timestamp === timestamp |
48 | } |
49 | |
50 | return h('Context -feed', [ |
51 | LevelOneContext(), |
52 | LevelTwoContext() |
53 | ]) |
54 | |
55 | function LevelOneContext () { |
56 | function isDiscoverContext (loc) { |
57 | const PAGES_UNDER_DISCOVER = ['blogIndex', 'blogShow', 'home'] |
58 | |
59 | return PAGES_UNDER_DISCOVER.includes(location.page) |
60 | || get(location, 'value.private') === undefined |
61 | } |
62 | |
63 | const prepend = [ |
64 | // Nearby |
65 | computed(nearby, n => !isEmpty(n) ? h('header', strings.peopleNearby) : null), |
66 | map(nearby, feedId => Option({ |
67 | notifications: Math.random() > 0.7 ? Math.floor(Math.random()*9+1) : 0, // TODO |
68 | imageEl: api.about.html.avatar(feedId, 'small'), |
69 | label: api.about.obs.name(feedId), |
70 | selected: location.feed === feedId, |
71 | location: computed(recentMsgLog, recent => { |
72 | const lastMsg = recent[feedId] |
73 | return lastMsg |
74 | ? Object.assign(lastMsg, { feed: feedId }) |
75 | : { page: 'threadNew', feed: feedId } |
76 | }), |
77 | }), { comparer: (a, b) => a === b }), |
78 | |
79 | // --------------------- |
80 | computed(nearby, n => !isEmpty(n) ? h('hr') : null), |
81 | |
82 | // Discover |
83 | Option({ |
84 | notifications: Math.floor(Math.random()*5+1), |
85 | imageEl: h('i.fa.fa-binoculars'), |
86 | label: strings.blogIndex.title, |
87 | selected: isDiscoverContext(location), |
88 | location: { page: 'blogIndex' }, |
89 | }) |
90 | ] |
91 | |
92 | return api.app.html.scroller({ |
93 | classList: [ 'level', '-one' ], |
94 | prepend, |
95 | stream: api.feed.pull.private, |
96 | filter: () => pull( |
97 | pull.filter(msg => msg.value.content.type === 'post'), // TODO is this the best way to protect against votes? |
98 | pull.filter(msg => msg.value.author != myKey), |
99 | pull.filter(msg => msg.value.content.recps), |
100 | pull.through(updateRecentMsgLog), |
101 | pull.filter(isLatestMsg) |
102 | //pull.through( // trim exisiting from content up Top case) // do this with new updateTop in mutant-scroll |
103 | ), |
104 | render: (msg) => { |
105 | const { author } = msg.value |
106 | if (nearby.has(author)) return |
107 | |
108 | return Option({ |
109 | notifications: Math.random() > 0.7 ? Math.floor(Math.random()*9+1) : 0, // TODO |
110 | imageEl: api.about.html.avatar(author), |
111 | label: api.about.obs.name(author), |
112 | selected: location.feed === author, |
113 | location: Object.assign({}, msg, { feed: author }) // TODO make obs? |
114 | }) |
115 | } |
116 | }) |
117 | } |
118 | |
119 | function LevelTwoContext () { |
120 | const { key, value, feed: targetUser, page } = location |
121 | const root = get(value, 'content.root', key) |
122 | if (!targetUser) return |
123 | |
124 | var threads = MutantArray() |
125 | |
126 | const prepend = Option({ |
127 | selected: page === 'threadNew', |
128 | location: {page: 'threadNew', feed: targetUser}, |
129 | label: h('Button', strings.threadNew.action.new), |
130 | }) |
131 | |
132 | return api.app.html.scroller({ |
133 | classList: [ 'level', '-two' ], |
134 | prepend, |
135 | stream: api.feed.pull.private, |
136 | filter: () => pull( |
137 | pull.filter(msg => msg.value.content.recps), |
138 | pull.filter(msg => msg.value.content.recps |
139 | .map(recp => typeof recp === 'object' ? recp.link : recp) |
140 | .some(recp => recp === targetUser) |
141 | ), |
142 | api.feed.pull.rollup() // TODO - not technically a filter ...? |
143 | ), |
144 | render: (thread) => { |
145 | return Option({ |
146 | label: api.message.html.subject(thread), |
147 | selected: thread.key === root, |
148 | location: Object.assign(thread, { feed: targetUser }), |
149 | }) |
150 | } |
151 | }) |
152 | } |
153 | |
154 | function Option ({ notifications = 0, imageEl, label, location, selected }) { |
155 | const className = selected ? '-selected' : '' |
156 | const goToLocation = (e) => { |
157 | e.preventDefault() |
158 | e.stopPropagation() |
159 | api.history.sync.push(resolve(location)) |
160 | } |
161 | |
162 | if (!imageEl) { |
163 | return h('Option', { className, 'ev-click': goToLocation }, [ |
164 | h('div.label', label) |
165 | ]) |
166 | } |
167 | |
168 | return h('Option', { className }, [ |
169 | h('div.circle', [ |
170 | when(notifications, h('div.alert', notifications)), |
171 | imageEl |
172 | ]), |
173 | h('div.label', { 'ev-click': goToLocation }, label) |
174 | ]) |
175 | } |
176 | }) |
177 | } |
178 | |
179 |
Built with git-ssb-web