-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
398 lines (302 loc) · 10.4 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// Bridge between GUI and PuffBall modules
puffworldprops = {
ICX: {
newUser: {
requestedUsername: '',
usernameStatus: '',
usernameMessage: '',
checkingUsername: '',
profilePuff: ''
},
invite: {
questionStatus: false,
questionMessage: '',
answerStatus: false,
answerMessage: ''
},
uniqueConvoIDs: {}
},
clusters: {
filters: true,
publish: true,
view: true,
identity: false,
profile: false,
preferences: false,
about: true,
tools: false
},
menu: {
show: false,
prefs: false,
profile: false,
import: false, // import username
section: false, // the section user is currently working on
popout: false,
user: {
// pick_one: false,
manage: false,
show_bc: false,
show_key: false
}
},
header: {
show: true,
publish: {
show: false
},
identity: {
show: false
},
score: {
show: false
}
},
view: {
icx: { // ICX related
screen: 'init',
toUser: "",
// activeReplies: []
},
mode: 'list',
language : 'en',
query: {
sort: 'DESC' // direction of sort
},
filters: {},
score: {},
slider: {},
// TABLEVIEW
table: {
loaded: 0,
noMorePuffs: false,
column: {
refs: {
show: true,
weight:.3,
},
from: {
show: true,
weight: .7,
},
to: {
show: true,
weight: .7,
},
content: {
show: true,
weight: 3.5,
},
date: {
show: true,
weight: 1,
allowSort: true
},
tags: {
show: false,
weight: 1.5,
},
type: {
show: false,
weight: 1,
allowSort: false
},
filename: {
show: false,
weight: 1,
allowSort: false
},
language: {
show: false,
weight: 1,
allowSort: false
},
},
bar: {
tmp: 1 // to prevent bar from disappearing
},
sort: {
column: 'date',
desc: true
},
format: 'list',
maxRowHeight: 99 // We want to show everything
},
},
reply: {
// show: false,
lastType: false, // type of the last published puff
expand: false,
content: '',
state: {},
privacy: false,
type: false,
replyType: 'message',
caption: ''
},
raw: {
puffs: []
},
prefs: {
reporting: true
},
profile: {
avatarUrl: ''
},
tools: {
users: {
resultstyle: 'raw',
puffstyle: 'raw'
}
}
}
puffworlddefaults = puffworldprops // it's immutable so we don't care
//// event bindings for controlling core behavior from the display ////
Events.sub('prefs/ephemeralKeychain/toggle', function(data, path) {
var new_state = CONFIG.ephemeralKeychain
modConfig('ephemeralKeychain', new_state)
var dir = new_state ? 'on' : 'off'
Events.pub('ui/menu/prefs/ephemeralKeychain/' + dir)
})
///// event bindings that are specific to the GUI //////
Events.sub('ui/*', function(data, path) {
//// rerender on all ui events
// OPT: batch process recent log items on requestAnimationFrame
update_puffworldprops(data) // change props in a persistent fashion
setURLfromViewProps() // set the props in the url and history
updateUI() // then re-render PuffWorld w/ the new props
})
Events.sub("filter/*", function(data, path) {
// side effects: query set to default; view.table.format set to list
data['view.query'] = Boron.shallow_copy(puffworlddefaults.view.query)
data['view.table.format'] = 'list'
// TODO put this in config as default view
if (puffworldprops.view.mode != 'list' ||
puffworldprops.view.mode != 'tableView') {
data['view.mode'] = 'list'
}
Events.pub('ui/query/default', data)
PB.Data.importRemoteShells() // TODO: remove once we upgrade to websockets as our workaround for non-rtc browsers
})
function renderPuffWorld() {
var puffworlddiv = document.getElementById('puffworld') // OPT: cache this for speed
React.renderComponent(ICXWorld(puffworldprops), puffworlddiv)
}
update_puffworldprops = function(data) {
puffworldprops = Boron.persistent_merge(puffworldprops, data)
}
///////// PB.M.Forum Interface ////////////
// NOTE: this has to load last, so keep it at the bottom
window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame
|| window.webkitRequestAnimationFrame || window.msRequestAnimationFrame
|| setTimeout
var updateUI = onceRAF.bind(this, renderPuffWorld) // only update once per rAF
var eatPuffs = function(puffs) {
// if(!Array.isArray(puffs) || !puffs.length) // THINK: this disrupts cold load of contentless shells...
// return false
updateUI()
}
// ISSUE: This handler runs for every NEW puff that the user just received
// and for every existing puff that has just been enboarded into the cache
// Therefore we don't know whether to increment the total count or not
var updateLatestConvo = function(report) {
report.private_promise.then(function(private_report) {
var sigs = private_report.goodsigs
sigs.map(PB.Data.getDecryptedLetterBySig).forEach(function(puff) {
updateCurrentConvos(puff)
})
})
}
////
// START MANUAL FORUM MODULE INIT
// ONLY RECEIVE PRIVATE PUFFS FOR/FROM THE CURRENT USER
CONFIG.noNetwork = true
CONFIG.icxmode = true
PB.addNewPuffHandler(eatPuffs) // register our update function
PB.addNewPuffReportHandler(updateLatestConvo) // conversational update function
// PB.M.Forum.init() // don't call this -- it initializes the P2P network
PB.addRelationshipHandler(PB.M.Forum.addFamilialEdges)
PB.addPreSwitchIdentityHandler(PB.M.Forum.clearPuffContentStash)
// END MANUAL FORUM MODULE INIT
// private puff management for ICX:
setInterval(PB.Data.updatePrivateShells, 60*1000)
PB.addPreSwitchIdentityHandler(PB.Data.removeAllPrivateShells)
// PB.addPostSwitchIdentityHandler(function(username) {
// PB.Data.getMorePrivatePuffs(username, 0, CONFIG.initLoadBatchSize)
// // Events.pub('ui/switchIdentityTo')
// })
// initialize other modules
PB.M.Wardrobe.init() // rehydrate identities and resume last used
// handleImportRedirect() // check if import
// TODO: PB.Users.init()
PB.Users.depersist() // get cached userRecords
setPropsFromURL() // handle pushstate hash
popMods() // deflate any machine prefs
PB.addPayloadModifierHandler(function(payload) {
payload = payload || {}
payload.time = Date.now()
return payload
})
getUniqueConvoKeys()
//// BUILD CRYPTO WORKER
PB.cryptoworker = new Worker("js/cryptoworker.js")
PB.workerqueue = []
PB.workerautoid = 0
PB.workerreceive = function(msg) {
var id = msg.data.id
if(!id) return false // TODO: add onError here
var fun = PB.workerqueue[id]
if(!fun) return false // TODO: add onError here
fun(msg.data.evaluated)
delete PB.workerqueue[id] // THINK: this leaves a sparse array, but is probably faster than splicing
}
PB.workersend = function(funstr, args, resolve, reject) {
PB.workerautoid += 1
PB.workerqueue[PB.workerautoid] = resolve
if(!Array.isArray(args))
args = [args]
PB.cryptoworker.postMessage({fun: funstr, args: args, id: PB.workerautoid})
}
// PB.cryptoworker.addEventListener("message", console.log.bind(console))
PB.cryptoworker.addEventListener("message", PB.workerreceive)
//// END BUILD CRYPTO WORKER
window.addEventListener('resize', function() {
updateUI()
})
window.addEventListener('load', function() {
/// this is cumbersome, but it gets around browser inconsistencies (some fire popstate on page load, others don't)
// via https://code.google.com/p/chromium/issues/detail?id=63040
setTimeout(function() {
window.addEventListener('popstate', function(event) {
if(event.state)
return setPropsFromPushstate(event.state)
puffworldprops = puffworlddefaults
updateUI()
})
}, 0)
})
// Hide slideshow from people with at least one identity
// Make sure not problem if empty
var identityUsernames = PB.getAllIdentityUsernames()
if(identityUsernames.length < 1)
Events.pub( 'ui/slider/close',{ 'view.slider.show': true})
// console.log("hide silder cuz several users")
// TODO: pull out of global, more fineness
ACTIVITY = []
Events.sub('ui/*', function(data) {
if(ACTIVITY.length) {
var last = ACTIVITY[ACTIVITY.length - 1]
if(JSON.stringify(last) == JSON.stringify(data))
return false
}
ACTIVITY.push(data)
// XHR this bad boy!
if(puffworldprops.prefs.reporting)
PB.Net.xhr('https://i.cx/api/events.php', {method: 'POST'}, data)
})
//tells us if the user has any delicious puffs
userHasShells(PB.getCurrentUsername(), function(numShells) {
Events.pub('ui/event',{
'ICX.hasShells': numShells
})
})