-
-
Notifications
You must be signed in to change notification settings - Fork 708
Expand file tree
/
Copy pathgrainview.js
More file actions
647 lines (564 loc) · 19.1 KB
/
Copy pathgrainview.js
File metadata and controls
647 lines (564 loc) · 19.1 KB
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
let counter = 0;
GrainView = class GrainView {
constructor(grainId, path, token, parentElement) {
// `path` starts with a slash and includes the query and fragment.
//
// Owned grains:
// grainId, path, dep.
// callback sets error, openingSession on failure,
// grainId, sessionId, sessionSub on success.
//
// Sturdyref ApiTokens:
// grainId, path, dep.
// callback sets error, openingSession on failure
// grainId, sessionId, sessionSub on success.
//
// Token-only sessions:
// grainId, token, path, dep
// callback sets error, openingSession on failure
// grainId, sessionId, title, and session Sub on success
this._grainId = grainId;
this._originalPath = path;
this._path = path;
this._token = token;
this._parentElement = parentElement;
this._status = 'closed';
this._dep = new Tracker.Dependency();
this._userIdentityId = new ReactiveVar(undefined);
// `false` means incognito; `undefined` means we still need to decide whether to reveal
// an identity.
if (token) {
if (!Meteor.userId()) {
this.doNotRevealIdentity();
// Suggest to the user that they log in by opening the login menu.
globalTopbar.openPopup('login');
}
} else {
this.revealIdentity();
}
this.enableInlinePowerbox = new ReactiveVar(false);
// We manage our Blaze view directly in order to get more control over when iframes get
// re-rendered. E.g. if we were to instead use a template with {{#each grains}} iterating over
// the list of open grains, all grains might get re-rendered whenever a grain is removed from the
// list, which would reset all the iframe states, making users sad.
this._blazeView = Blaze.renderWithData(Template.grainView, this, parentElement);
this.id = counter++;
}
reset(identityId) {
// TODO(cleanup): This duplicates some code from the GrainView constructor.
this._dep.changed();
this.destroy();
this._hasLoaded = undefined;
this._hostId = undefined;
this._sessionId = null;
this._sessionSalt = null;
if (this._sessionObserver) {
this._sessionObserver.stop();
this._sessionObserver = undefined;
}
if (this._sessionSub) {
this._sessionSub.stop();
this._sessionSub = undefined;
}
this._status = 'closed';
this._userIdentityId = new ReactiveVar(undefined);
this.revealIdentity(identityId);
// We want the iframe to receive the most recently-set path whenever we rerender.
this._originalPath = this._path;
this._blazeView = Blaze.renderWithData(Template.grainView, this, this._parentElement);
}
switchIdentity(identityId) {
check(identityId, String);
const currentIdentityId = this.identityId();
const grainId = this.grainId();
if (currentIdentityId === identityId) return;
const _this = this;
if (this._token) {
_this.reset(identityId);
_this.openSession();
} else if (this.isOwner()) {
Meteor.call('updateGrainPreferredIdentity', grainId, identityId, (err, result) => {
if (err) {
console.log('error:', err);
} else {
_this.reset(identityId);
_this.openSession();
}
});
} else {
if (ApiTokens.findOne({
grainId: grainId,
'owner.user.identityId': identityId,
revoked: {$ne: true},
})) {
// just do the switch
_this.reset(identityId);
_this.openSession();
} else {
// Should we maybe prompt the user first?
// 'That identity does not already have access to this grain. Would you like to share access
// from your current identity? Y/ cancel.'
Meteor.call('newApiToken',
{identityId: currentIdentityId},
grainId,
'direct share',
{allAccess: null},
{user: {identityId: identityId, title: _this.title()}},
(err, result) => {
if (err) {
console.log('error:', err);
} else {
_this.reset(identityId);
_this.openSession();
}
}
);
}
}
}
destroy() {
// This must be called when the GrainView is removed from the list otherwise Blaze will go on
// rendering the iframe forever, even if it is no longer linked into the page DOM.
Blaze.remove(this._blazeView);
if (this._grainSizeSub) this._grainSizeSub.stop();
}
isActive() {
this._dep.depend();
return this._isActive;
}
setActive(isActive) {
this._isActive = isActive;
this._dep.changed();
}
isOldSharingModel() {
this._dep.depend();
const grain = Grains.findOne({_id: this._grainId});
return grain && !grain.private;
}
isOwner() {
this._dep.depend();
// See if this is one of our own grains.
// If we're not logged in, we can't be the owner.
if (!Meteor.userId()) return false;
const grain = Grains.findOne({_id: this._grainId, userId: Meteor.userId()});
return grain != undefined;
}
_isUsingAnonymously() {
this._dep.depend();
if (this.isOldSharingModel()) {
return false;
}
if (!Meteor.userId() && !this._token) {
console.error('should never happen: anonymous, but no token either.');
}
return !!this._token;
}
size() {
const size = GrainSizes.findOne(this._grainId);
return size && size.size;
}
title() {
// Returns the user's name for this grain, not the browser tab title.
// Three cases:
// 1) We own the grain or it is public. Use the value from the Grains collection.
// 2) We own an ApiToken for the grain. Use the value from the ApiTokens collection.
// 3) We are using an ApiToken for the grain. Use the transient value stored in this._title.
this._dep.depend();
if (this.isOwner() || this.isOldSharingModel()) {
// Case 1.
const grain = Grains.findOne({_id: this._grainId});
return grain && grain.title;
} else if (!this._isUsingAnonymously()) {
// Case 2.
const apiToken = ApiTokens.findOne({
grainId: this._grainId,
'owner.user.identityId': this.identityId(),
}, {
sort: {created: 1},
});
return apiToken && apiToken.owner && apiToken.owner.user && apiToken.owner.user.title;
} else {
// Case 3.
return this._title;
}
}
appTitle() {
// Three cases:
// 1) We own the grain. Look up the app title in the package manifest.
// 2) We own an ApiToken for the grain. Use the value from the denormalizedGrainMetadata.
// 3) We are using an ApiToken for the grain (either logged out or incognito). Use the value
// from the TokenInfo pseudocollection.
this._dep.depend();
if (this.isOwner()) {
// Case 1.
const grain = Grains.findOne({_id: this._grainId});
const pkg = grain && Packages.findOne({_id: grain.packageId});
return pkg && pkg.manifest && pkg.manifest.appTitle && pkg.manifest.appTitle.defaultText;
} else if (!this._isUsingAnonymously()) {
// Case 2
const token = ApiTokens.findOne({
grainId: this._grainId,
'owner.user.identityId': this.identityId(),
}, {
sort: {created: 1},
});
return (token && token.owner && token.owner.user && token.owner.user.denormalizedGrainMetadata &&
token.owner.user.denormalizedGrainMetadata.appTitle.defaultText);
// TODO(someday) - shouldn't use defaultText
} else {
// Case 3
const tokenInfo = TokenInfo.findOne({_id: this._token});
const token = ApiTokens.findOne({_id: tokenInfo.apiToken});
return tokenInfo && tokenInfo.grainMetadata && tokenInfo.grainMetadata.appTitle &&
tokenInfo.grainMetadata.appTitle.defaultText;
// TODO(someday) - shouldn't use defaultText
}
}
frameTitle() {
this._dep.depend();
if (this._frameTitle !== undefined) {
return this._frameTitle;
}
const appTitle = this.appTitle();
const grainTitle = this.title();
// Actually set the values
if (appTitle && grainTitle) {
return grainTitle + ' · ' + appTitle + ' · Sandstorm';
} else if (grainTitle) {
return grainTitle + ' · Sandstorm';
} else {
return 'Sandstorm';
}
}
updateDocumentTitle() {
this._dep.depend();
document.title = this.frameTitle();
}
showPowerboxOffer() {
//TODO(now): implement
}
error() {
this._dep.depend();
return this._error;
}
hasLoaded() {
this._dep.depend();
if (this._hasLoaded) {
return true;
}
const session = Sessions.findOne({_id: this._sessionId});
// TODO(soon): this is a hack to cache hasLoaded. Consider moving it to an autorun.
this._hasLoaded = session && session.hasLoaded;
return this._hasLoaded;
}
origin() {
this._dep.depend();
return this._hostId && (window.location.protocol + '//' + makeWildcardHost(this._hostId));
}
viewInfo() {
this._dep.depend();
return this._viewInfo;
}
grainId() {
this._dep.depend();
return this._grainId;
}
sessionId() {
this._dep.depend();
return this._sessionId;
}
setTitle(newTitle) {
this._title = newTitle;
if (this._userIdentityId.get()) {
Meteor.call('updateGrainTitle', this._grainId, newTitle, this._userIdentityId.get());
}
this._dep.changed();
}
setPath(newPath) {
this._path = newPath;
if (this.isActive()) {
window.history.replaceState({}, '', this.route());
}
this._dep.changed();
}
depend() {
this._dep.depend();
}
revealIdentity(identityId) {
if (!Meteor.user()) {
return;
}
const myIdentityIds = SandstormDb.getUserIdentityIds(Meteor.user());
let resultIdentityId = myIdentityIds[0];
const grain = Grains.findOne(this._grainId);
if (identityId && myIdentityIds.indexOf(identityId) != -1) {
resultIdentityId = identityId;
} else if (grain && myIdentityIds.indexOf(grain.identityId) != -1) {
// If we own the grain, open it as the owning identity.
resultIdentityId = grain.identityId;
} else {
const token = ApiTokens.findOne({
grainId: this._grainId,
'owner.user.identityId': {$in: myIdentityIds},
}, {
sort:{'owner.user.lastUsed': -1},
});
if (token) {
resultIdentityId = token.owner.user.identityId;
}
}
this._userIdentityId.set(resultIdentityId);
this._dep.changed();
}
doNotRevealIdentity() {
this._userIdentityId.set(false);
this._dep.changed();
}
identityId() {
this._dep.depend();
const identityId = this._userIdentityId.get();
if (identityId) {
return identityId;
} else {
return null;
}
}
shouldShowInterstitial() {
this._dep.depend();
// We only show the interstitial for /shared/ routes.
if (!this._token) {
return false;
}
// If we have explictly set _userIdentityId, we don't need to show the interstitial.
if (this._userIdentityId.get() !== undefined) {
return false;
}
// If we are not logged in, we don't need to show the interstitial - we'll go incognito by default.
if (!Meteor.userId()) {
return false;
}
// Otherwise, we should show it.
return true;
}
_addSessionObserver(sessionId) {
const _this = this;
_this._sessionSub = Meteor.subscribe('sessions', sessionId);
_this._sessionObserver = Sessions.find({_id: sessionId}).observe({
removed(session) {
_this._sessionSub.stop();
_this._sessionSub = undefined;
_this._status = 'closed';
_this._dep.changed();
if (_this._sessionObserver) {
_this._sessionObserver.stop();
}
Meteor.defer(() => { _this.openSession(); });
},
changed(session) {
_this._viewInfo = session.viewInfo || _this._viewInfo;
_this._permissions = session.permissions || _this._permissions;
_this._dep.changed();
},
added(session) {
_this._viewInfo = session.viewInfo || _this._viewInfo;
_this._permissions = session.permissions || _this._permissions;
_this._status = 'opened';
_this._dep.changed();
},
});
}
_openGrainSession() {
const _this = this;
const identityId = _this.identityId();
Meteor.call('openSession', _this._grainId, identityId, _this._sessionSalt, (error, result) => {
if (error) {
console.error('openSession error', error);
_this._error = error.message;
_this._status = 'error';
_this._dep.changed();
} else {
// result is an object containing sessionId, initial title, and grainId.
if (result.title) {
_this._title = result.title;
}
_this._grainId = result.grainId;
_this._sessionId = result.sessionId;
_this._hostId = result.hostId;
_this._sessionSalt = result.salt;
_this._addSessionObserver(result.sessionId);
if (_this._grainSizeSub) _this._grainSizeSub.stop();
_this._grainSizeSub = Meteor.subscribe('grainSize', result.grainId);
_this._dep.changed();
}
});
}
_openApiTokenSession() {
const _this = this;
const condition = () => {
return _this._userIdentityId.get() !== undefined;
};
onceConditionIsTrue(condition, () => {
const identityId = _this.identityId();
const openSessionArg = {
token: _this._token,
incognito: !identityId,
};
Meteor.call('openSessionFromApiToken',
openSessionArg, identityId, _this._sessionSalt, (error, result) => {
if (error) {
console.log('openSessionFromApiToken error');
_this._error = error.message;
_this._status = 'error';
_this._dep.changed();
} else if (result.redirectToGrain) {
console.log('openSessionFromApiToken redirectToGrain');
_this._grainId = result.redirectToGrain;
_this._dep.changed();
// We should remove this tab from the tab list, since the /grain/<grainId> route
// will set up its own tab for this grain. There could even already be a tab open, if the
// user reuses a /shared/ link.
_this.destroy();
const allGrains = globalGrains.get();
for (let i = 0; i < allGrains.length; i++) {
if (allGrains[i] === _this) {
allGrains.splice(i, 1);
globalGrains.set(allGrains);
}
}
// OK, go to the grain.
return Router.go('/grain/' + result.redirectToGrain + _this._path, {}, {replaceState: true});
} else {
// We are viewing this via just the /shared/ link, either as an anonymous user on in our
// incognito mode (since we'd otherwise have redeemed the token and been redirected).
console.log('openSessionFromApiToken success');
_this._title = result.title;
_this._grainId = result.grainId;
_this._sessionId = result.sessionId;
_this._hostId = result.hostId;
_this._sessionSalt = result.salt;
_this._addSessionObserver(result.sessionId);
_this._dep.changed();
}
}
);
});
}
openSession() {
if (this._status !== 'closed') {
console.error('GrainView: openSession() called but state was ' + this._status);
return;
}
this._status = 'opening';
if (this._token === undefined) {
// Opening a grain session.
this._openGrainSession();
} else {
// Opening an ApiToken session. Only do so if we don't need to show the interstitial first.
this._openApiTokenSession();
}
}
sessionStatus() {
// 'opening', 'opened', 'closed'
this._dep.depend();
return this._status;
}
route() {
this._dep.depend();
if (this._token) {
return '/shared/' + this._token + this._path;
} else {
return '/grain/' + this._grainId + this._path;
}
}
_fallbackIdenticon() {
// identifier is SHA1('');
return Identicon.identiconForApp('da39a3ee5e6b4b0d3255bfef95601890afd80709', 'grain');
}
_urlForAsset(assetId) {
return window.location.protocol + '//' + makeWildcardHost('static') + '/' + assetId;
}
iconSrc() {
// Several options here:
// 1. We own the grain. Look up the icon metadata in the Package manifest (or DevPackage if applicable).
// 2. We own an Api token for the grain. Use the denormalizedGrainMetadata.
// 3. We're using an ApiToken anonymously. Use the data from the TokenInfo pseudocollection.
this._dep.depend();
if (this.isOwner()) {
// Case 1
const grain = Grains.findOne({_id: this._grainId});
if (grain) {
const pkg = DevPackages.findOne({appId: grain.appId}) ||
Packages.findOne({_id: grain.packageId});
if (pkg) return Identicon.iconSrcForPackage(pkg, 'grain', makeWildcardHost('static'));
}
} else if (!this._isUsingAnonymously()) {
// Case 2
const apiToken = ApiTokens.findOne({
grainId: this._grainId,
'owner.user.userId': Meteor.userId(),
}, {
sort: {created: 1},
});
if (apiToken) {
const meta = apiToken.owner.user.denormalizedGrainMetadata;
if (meta && meta.icon && meta.icon.assetId) return this._urlForAsset(meta.icon.assetId);
if (meta && meta.appId) return Identicon.identiconForApp(meta.appId, 'grain');
}
} else {
// Case 3
const tokenInfo = TokenInfo.findOne({_id: this._token});
if (tokenInfo && tokenInfo.grainMetadata) {
const meta = tokenInfo.grainMetadata;
if (meta.icon) return this._urlForAsset(meta.icon.assetId);
if (meta.appId) return Identicon.identiconForApp(meta.appId, 'grain');
}
}
// jscs:disable disallowEmptyBlocks
if (this._token) {
// The TokenInfo collection includes some safe denormalized grain metadata.
} else {
//
}
// jscs:enable disallowEmptyBlocks
// None of our other info sources were available. Weird. Show a fallback identicon.
return this._fallbackIdenticon();
}
setFrameTitle(newFrameTitle) {
this._frameTitle = newFrameTitle;
this._dep.changed();
}
token() {
this._dep.depend();
return this._token;
}
generatedApiToken() {
this._dep.depend();
return this._generatedApiToken;
}
setGeneratedApiToken(newApiToken) {
this._generatedApiToken = newApiToken;
this._dep.changed();
}
startInlinePowerbox(inlinePowerboxState) {
this.inlinePowerboxState = inlinePowerboxState;
if (inlinePowerboxState.isForeground) {
this.enableInlinePowerbox.set(true);
} else {
state.source.postMessage({
rpcId: inlinePowerboxState.rpcId,
error: "Cannot start inline powerbox when app is not in foreground",
}, inlinePowerboxState.origin);
}
}
};
const onceConditionIsTrue = (condition, continuation) => {
Tracker.nonreactive(() => {
Tracker.autorun((handle) => {
if (!condition()) {
return;
}
handle.stop();
Tracker.nonreactive(continuation);
});
});
};