-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathssb-profile-link.js
136 lines (113 loc) · 3.61 KB
/
ssb-profile-link.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
const ssbSingleton = require('ssb-browser-core/ssb-singleton')
const pull = require('pull-stream')
const feedToMainCache = {}
Vue.component('ssb-profile-link', {
template: `
<div class="avatarcontainer">
<div class="avatarlink" v-on:click="openProfile()">
<img class='img' :src='imgURL' :title="feed" />
<span v-if="isBlocked" class="blockedSymbol">🚫</span>
<span class="name">{{ name }}</span>
</div>
</div>`,
props: ['feed'],
data: function() {
return {
componentStillLoaded: false,
imgURL: 'noavatar.svg',
isBlocked: false,
name: '',
mainfeed: ''
}
},
methods: {
renderProfileCallback: function (err, SSB, existingProfile) {
const self = this
const profile = existingProfile || SSB.db.getIndex("aboutSelf").getProfile(self.mainfeed)
if (self.mainfeed == SSB.id)
self.name = 'You'
else if (profile.name !== '')
self.name = profile.name
if (profile.imageURL) self.imgURL = profile.imageURL
else if (profile.image) {
SSB.blobs.localProfileGet(profile.image, (err, url) => {
if (err) return console.error("failed to get img", err)
profile.imageURL = self.imgURL = url
})
}
},
openProfile: function() {
const profile = require('./profile')
new Vue(profile(this.feed)).$mount("#app")
},
loadBlocking: function (err, SSB) {
const self = this
SSB.friends.isBlocking({ source: SSB.id, dest: self.mainfeed }, (err, result) => {
if (!err) self.isBlocked = result
})
},
load: function() {
const self = this
ssbSingleton.getSimpleSSBEventually(
() => { return self.componentStillLoaded },
self.loadBlocking
)
ssbSingleton.getSimpleSSBEventually(
() => { return self.componentStillLoaded },
self.renderProfileCallback
)
},
refresh: function() {
const self = this
// convert feed to main feed
if (feedToMainCache[self.feed]) {
self.mainfeed = feedToMainCache[self.feed]
self.load()
} else {
ssbSingleton.getSimpleSSBEventually(
() => { return self.componentStillLoaded },
() => {
const { where, author, slowEqual, toPullStream } = SSB.db.operators
pull(
SSB.db.query(
where(slowEqual('value.content.subfeed', self.feed)),
toPullStream()
),
pull.collect((err, messages) => {
if (err) return console.error(err)
if (messages.length === 0) return
const { metafeed } = messages[0].value.content
pull(
SSB.db.query(
where(author(metafeed)),
toPullStream()
),
pull.filter((msg) => msg.value.content.feedpurpose === 'main'),
pull.collect((err, messages) => {
if (err || messages.length == 0) return console.error(err)
self.mainfeed = messages[0].value.content.subfeed
feedToMainCache[self.feed] = self.mainfeed
self.load()
})
)
})
)
}
)
}
}
},
created: function() {
this.name = this.feed.substr(0,5)
this.componentStillLoaded = true
this.refresh()
},
destroyed: function() {
this.componentStillLoaded = false
},
watch: {
feed: function (oldValue, newValue) {
this.refresh()
}
}
})