-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprofile.js
372 lines (336 loc) · 11.4 KB
/
profile.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
module.exports = function (feedId) {
const ssbSingleton = require('ssb-browser-core/ssb-singleton')
let initialState = function(self) {
return {
componentStillLoaded: false,
isSelf: false,
following: false,
blocking: false,
name: '',
image: 'noavatar.svg',
imageBlobId: '',
showFriends: true,
showBlocked: false,
showFollowers: false,
showBlockingUs: false,
friends: [],
followers: [],
blocked: [],
blockingUs: [],
waitingForBlobURLs: 0,
}
}
return {
template: `
<div id="app">
<div id="profile">
<span v-if="isSelf">
<div class="avatar">
<img :src='image'><br>
<input type="file" v-on:change="onFileSelect"><br>
</div>
<div class="description">
<div class="feedId">{{ feedId }}</div>
Name: <input id="name" type="text" v-model="name" placeholder="(Your name / nickname)">
</div>
</span>
<span v-else>
<div v-bind:class="{ avatar: true, blockedAvatar: blocking }">
<img :src='image'><br>
<span v-if="blocking" class="blockedSymbol">🚫</span>
</div>
<div class="description">
<div class="feedId">{{ feedId }}</div>
<h2 class="profileName">{{ name }}</h2>
</div>
</span>
<div class="profileButtons" v-if="isSelf">
<button class="clickButton" v-on:click="saveProfile">Save profile</button>
</div>
<div class="profileButtons" v-else>
<button class="clickButton" v-on:click="changeFollowStatus">{{ followText }}</button>
<button class="clickButton" v-on:click="changeBlockStatus">{{ blockText }}</button>
<br><br>
</div>
<h2 v-if="friends">
<a href="javascript:void(0)" @click="showFriends=!showFriends">
<span v-if="showFriends">▼</span>
<span v-else>►</span>
Following ({{ friends.length }})
</a>
</h2>
<div v-if="showFriends" id="follows">
<div v-for="friend in friends">
<ssb-profile-link :feed="friend"></ssb-profile-link>
</div>
</div>
<h2 v-if="blocked">
<a href="javascript:void(0)" @click="showBlocked=!showBlocked">
<span v-if="showBlocked">▼</span>
<span v-else>►</span>
Blocking ({{ blocked.length }})
</a>
</h2>
<div v-if="showBlocked" id="blocked">
<div v-for="block in blocked">
<ssb-profile-link :feed="block"></ssb-profile-link>
</div>
</div>
<h2 v-if="followers && followers.length > 0">
<a href="javascript:void(0)" @click="showFollowers=!showFollowers">
<span v-if="showFollowers">▼</span>
<span v-else>►</span>
Followers ({{ followers.length }})
</a>
</h2>
<div v-if="followers && followers.length > 0 && showFollowers" id="followers">
<div v-for="friend in followers">
<ssb-profile-link :feed="friend"></ssb-profile-link>
</div>
</div>
<h2 v-if="blockingUs && blockingUs.length > 0">
<a href="javascript:void(0)" @click="showBlockingUs=!showBlockingUs">
<span v-if="showBlockingUs">▼</span>
<span v-else>►</span>
Blocking us ({{ blockingUs.length }})
</a>
</h2>
<div v-if="blockingUs && blockingUs.length > 0 && showBlockingUs" id="blockingUs">
<div v-for="friend in blockingUs">
<ssb-profile-link :feed="friend"></ssb-profile-link>
</div>
</div>
<div v-if="blockingUs && blockingUs.length > 0" style="clear: both;"></div>
</div>
</div>`,
data: function() {
return initialState(this)
},
computed: {
followText: function() { return this.following ? 'Unfollow' : 'Follow' },
blockText: function() { return this.blocking ? 'Unblock' : 'Block' },
},
methods: {
cacheImageURLForPreview: function(blobId, cb) {
var self = this;
[ err, SSB ] = ssbSingleton.getSSB()
if (!SSB || !SSB.blobs) {
// Not going to hurt anything to try again later.
setTimeout(function() {
self.cacheImageURLForPreview(blobId, cb)
}, 3000)
return
}
++this.waitingForBlobURLs
SSB.blobs.fsURL(blobId, (err, blobURL) => {
// If this is the last blob we were waiting for, call the callback.
--self.waitingForBlobURLs
if (self.waitingForBlobURLs == 0)
cb(null, true)
})
},
onFileSelect: function(ev) {
const file = ev.target.files[0]
if (!file) return
var self = this;
[ err, SSB ] = ssbSingleton.getSSB()
if (!SSB || !SSB.blobs) {
alert("Can't add file right now. Database couldn't be locked. Please make sure you only have one running instance of ssb-browser.")
return
}
file.arrayBuffer().then(function (buffer) {
SSB.blobs.hash(new Uint8Array(buffer), (err, digest) => {
var blobId = "&" + digest
SSB.blobs.add(blobId, file, (err) => {
if (!err) {
SSB.blobs.push(blobId, (err) => {
SSB.blobs.localGet(blobId, (err, url) => {
if (!err) {
self.image = url
self.imageBlobId = blobId
}
})
})
} else
alert("failed to add img", err)
})
})
})
},
saveProfile: function() {
[ err, SSB ] = ssbSingleton.getSSB()
if (!SSB || !SSB.db) {
alert("Can't save right now. Database couldn't be locked. Please make sure you only have one running instance of ssb-browser.")
return
}
var msg = { type: 'about', about: SSB.id }
if (this.name)
msg.name = this.name
if (this.imageBlobId != '') {
msg.image = {
link: this.imageBlobId
}
}
// Make sure the full post (including headers) is not larger than the 8KiB limit.
if (JSON.stringify(msg).length > 8192) {
alert('Your post is too large. Each post can only be 8KiB. Please shorten your post or split it into multiple posts.')
return
}
SSB.db.publish(msg, (err) => {
if (err) return alert(err)
alert("Saved!")
})
},
changeFollowStatus: function() {
[ err, SSB ] = ssbSingleton.getSSB()
var self = this
if (!SSB || !SSB.db) {
alert("Can't change follow status right now. Database couldn't be locked. Please make sure you only have one running instance of ssb-browser.")
return
}
if (this.following) {
SSB.db.publish({
type: 'contact',
contact: this.feedId,
following: false
}, () => {
alert('Unfollowed') // FIXME: proper UI
})
} else {
SSB.db.publish({
type: 'contact',
contact: this.feedId,
following: true
}, () => {
alert('Followed') // FIXME: proper UI
})
}
},
changeBlockStatus: function() {
[ err, SSB ] = ssbSingleton.getSSB()
if (!SSB || !SSB.db) {
alert("Can't change blocking status right now. Database couldn't be locked. Please make sure you only have one running instance of ssb-browser.")
return
}
var self = this
if (this.blocking) {
SSB.db.publish({
type: 'contact',
contact: this.feedId,
blocking: false
}, () => {
self.blocking = false
alert('Unblocked') // FIXME: proper UI
})
} else {
SSB.db.publish({
type: 'contact',
contact: this.feedId,
blocking: true
}, () => {
SSB.db.deleteFeed(this.feedId, (err) => {
if (err) {
self.blocking = true
alert('Failed to delete messages, but user is blocked.')
} else {
alert('Blocked') // FIXME: proper UI
}
})
})
}
},
updateFollowers: function(err, SSB) {
var self = this
var opts = {
start: this.feedId,
max: 1,
reverse: true
}
SSB.friends.hops(opts, (err, feeds) => {
var newFollowers = []
for(f in feeds) {
if (feeds[f] > 0)
newFollowers.push(f)
}
self.followers = newFollowers
})
},
updateBlockingUs: function(err, SSB) {
var self = this
var opts = {
start: this.feedId,
max: 0,
reverse: true
}
SSB.friends.hops(opts, (err, feeds) => {
var newBlocks = []
for (f in feeds) {
if (Math.round(feeds[f]) == -1)
newBlocks.push(f)
}
self.blockingUs = newBlocks
})
},
renderFollowsCallback: function (err, SSB) {
var self = this
self.isSelf = (SSB.id == this.feedId)
SSB.helpers.getGraphForFeed(self.feedId, (err, graph) => {
self.friends = graph.following
self.blocked = graph.blocking
SSB.friends.isFollowing({ source: SSB.id, dest: self.feedId }, (err, result) => {
self.following = result
})
SSB.friends.isBlocking({ source: SSB.id, dest: self.feedId }, (err, result) => {
self.blocking = result
})
})
this.updateFollowers(err, SSB)
this.updateBlockingUs(err, SSB)
},
renderProfileCallback: function (err, SSB) {
var self = this
const profile = SSB.db.getIndex("aboutSelf").getProfile(self.feedId)
if (profile.name)
self.name = profile.name
if (profile.imageURL) {
self.image = profile.imageURL
self.imageBlobId = profile.image
} else if (profile.image) {
SSB.blobs.localGet(profile.image, (err, url) => {
if (!err) {
self.image = url
self.imageBlobId = profile.image
}
})
}
},
renderProfile() {
var self = this
ssbSingleton.getSimpleSSBEventually(
() => { return self.componentStillLoaded },
self.renderFollowsCallback
)
ssbSingleton.getSSBEventually(
-1,
() => { return self.componentStillLoaded },
(SSB) => {
if (!SSB || !SSB.db) return false
let profile = SSB.db.getIndex("aboutSelf").getProfile(self.feedId)
return Object.keys(profile).length > 0
},
self.renderProfileCallback
)
}
},
created: function () {
this.feedId = feedId
Object.assign(this.$data, initialState(this))
this.componentStillLoaded = true
this.renderProfile()
},
destroyed: function () {
this.componentStillLoaded = false
}
}
}