Files: d221f528353915ced2efa52aed3c925af62ddb9a / views.js
5911 bytesRaw
1 | var pull = require('pull-stream') |
2 | var sbot = require('./scuttlebot') |
3 | var hyperscroll = require('hyperscroll') |
4 | var More = require('pull-more') |
5 | var stream = require('hyperloadmore/stream') |
6 | var h = require('hyperscript') |
7 | var render = require('./render') |
8 | var ref = require('ssb-ref') |
9 | |
10 | var config = require('./config')() |
11 | |
12 | var id = require('./keys').id |
13 | |
14 | var fs = require('fs') |
15 | |
16 | var compose = require('./compose') |
17 | |
18 | var about = function () { |
19 | var screen = document.getElementById('screen') |
20 | |
21 | var about = require('./about') |
22 | |
23 | var content = h('div.content', about) |
24 | |
25 | screen.appendChild(hyperscroll(content)) |
26 | } |
27 | |
28 | var edit = function() { |
29 | var content = h('div.content') |
30 | |
31 | var screen = document.getElementById('screen') |
32 | |
33 | screen.appendChild(hyperscroll(content)) |
34 | |
35 | var nameInput = h('input', {placeholder: 'New name'}) |
36 | |
37 | var locInput = h('input', {placeholder: 'New location'}) |
38 | |
39 | var descInput = h('textarea', {placeholder: 'New description'}) |
40 | |
41 | var editor = h('div.message', |
42 | h('h1', 'Edit profile'), |
43 | nameInput, |
44 | h('button.btn.btn-primary', 'Preview', {onclick: function () { |
45 | if(nameInput.value) { |
46 | api.message_confirm({ |
47 | type: 'about', |
48 | about: id, |
49 | name: nameInput.value || undefined |
50 | }) |
51 | } |
52 | }}), |
53 | h('hr'), |
54 | locInput, |
55 | h('button.btn.btn-primary', 'Preview', {onclick: function () { |
56 | if(locInput.value) { |
57 | api.message_confirm({ |
58 | type: 'loc', |
59 | about: id, |
60 | loc: locInput.value || undefined |
61 | }) |
62 | } |
63 | }}), |
64 | h('hr'), |
65 | descInput, |
66 | h('button.btn.btn-primary', 'Preview', {onclick: function (){ |
67 | if(descInput.value) { |
68 | api.message_confirm({ |
69 | type: 'description', |
70 | about: id, |
71 | description: descInput.value || undefined |
72 | }) |
73 | } |
74 | }}), |
75 | h('hr') |
76 | ) |
77 | |
78 | content.appendChild(editor) |
79 | } |
80 | |
81 | var mentionsStream = function () { |
82 | var content = h('div.content') |
83 | |
84 | var screen = document.getElementById('screen') |
85 | |
86 | screen.appendChild(hyperscroll(content)) |
87 | |
88 | function createStream (opts) { |
89 | return pull( |
90 | sbot.backlinks({query: [{$filter: {dest: id}}], reverse: true}), |
91 | pull.map(function (msg) { |
92 | if (msg.value.private == true) |
93 | return 'ignoring private message' |
94 | else |
95 | return render(msg) |
96 | }) |
97 | ) |
98 | } |
99 | |
100 | pull( |
101 | createStream({reverse: true, limit: 10}), |
102 | stream.bottom(content) |
103 | ) |
104 | } |
105 | |
106 | var logStream = function () { |
107 | var content = h('div.content') |
108 | var screen = document.getElementById('screen') |
109 | screen.appendChild(hyperscroll(content)) |
110 | |
111 | function createStream (opts) { |
112 | return pull( |
113 | More(sbot.createLogStream, opts), |
114 | pull.map(function (msg) { |
115 | return render(msg) |
116 | }) |
117 | |
118 | ) |
119 | } |
120 | |
121 | pull( |
122 | createStream({old: false, limit: 10}), |
123 | stream.top(content) |
124 | ) |
125 | |
126 | pull( |
127 | createStream({reverse: true, live: false, limit: 10}), |
128 | stream.bottom(content) |
129 | ) |
130 | } |
131 | |
132 | var userStream = function (src) { |
133 | var content = h('div.content') |
134 | var screen = document.getElementById('screen') |
135 | screen.appendChild(hyperscroll(content)) |
136 | function createStream (opts) { |
137 | return pull( |
138 | More(sbot.userStream, opts, ['value', 'sequence']), |
139 | pull.map(function (msg) { |
140 | return render(msg) |
141 | }) |
142 | ) |
143 | } |
144 | |
145 | pull( |
146 | createStream({old: false, limit: 10, id: src}), |
147 | stream.top(content) |
148 | ) |
149 | |
150 | pull( |
151 | createStream({reverse: true, live: false, limit: 10, id: src}), |
152 | stream.bottom(content) |
153 | ) |
154 | |
155 | } |
156 | |
157 | var msgThread = function(src) { |
158 | var content = h('div.content') |
159 | var screen = document.getElementById('screen') |
160 | screen.appendChild(hyperscroll(content)) |
161 | sbot.get(src, function (err, data) { |
162 | if (err) {console.log('could not find message') } |
163 | data.value = data |
164 | var root = src |
165 | if (data.value.content.root) |
166 | root = data.value.content.root |
167 | sbot.get(root, function (err, data) { |
168 | if (err) { console.log('could not find root')} |
169 | data.value = data |
170 | data.key = root |
171 | content.appendChild(render(data)) |
172 | pull( |
173 | sbot.links({rel: 'root', dest: root, values: true, keys: true, live: true}), |
174 | pull.drain(function (msg) { |
175 | if (msg.value) |
176 | content.appendChild(render(msg)) |
177 | }) |
178 | ) |
179 | }) |
180 | }) |
181 | } |
182 | |
183 | var keyPage = function () { |
184 | var screen = document.getElementById('screen') |
185 | |
186 | var importKey = h('textarea.import', {placeholder: 'Import a new public/private key', name: 'textarea', style: 'width: 97%; height: 100px;'}) |
187 | |
188 | var content = h('div.content', |
189 | h('div.message#key', |
190 | h('h1', 'Your Key'), |
191 | h('p', {innerHTML: 'Your public/private key is: <pre><code>' + localStorage[config.caps.shs + '/secret'] + '</code></pre>'}, |
192 | h('button.btn', {onclick: function (e){ |
193 | localStorage[config.caps.shs +'/secret'] = '' |
194 | alert('Your public/private key has been deleted') |
195 | e.preventDefault() |
196 | location.hash = "" |
197 | location.reload() |
198 | }}, 'Delete Key') |
199 | ), |
200 | h('hr'), |
201 | h('form', |
202 | importKey, |
203 | h('button.btn', {onclick: function (e){ |
204 | if(importKey.value) { |
205 | localStorage[config.caps.shs + '/secret'] = importKey.value.replace(/\s+/g, ' ') |
206 | e.preventDefault() |
207 | alert('Your public/private key has been updated') |
208 | } |
209 | location.hash = "" |
210 | location.reload() |
211 | }}, 'Import key'), |
212 | ) |
213 | ) |
214 | ) |
215 | |
216 | screen.appendChild(hyperscroll(content)) |
217 | } |
218 | |
219 | |
220 | function hash () { |
221 | return window.location.hash.substring(1) |
222 | } |
223 | |
224 | module.exports = function () { |
225 | var src = hash() |
226 | |
227 | if (ref.isFeed(src)) { |
228 | userStream(src) |
229 | } else if (ref.isMsg(src)) { |
230 | msgThread(src) |
231 | } else if (src == 'mentions') { |
232 | mentionsStream() |
233 | } else if (src == 'about') { |
234 | about() |
235 | } else if (src == 'edit') { |
236 | edit() |
237 | } else if (src == 'key') { |
238 | keyPage() |
239 | } else { |
240 | logStream() |
241 | } |
242 | } |
243 |
Built with git-ssb-web