forked from arj03/ssb-browser-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssb-db.js
144 lines (115 loc) · 3.42 KB
/
ssb-db.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
// name is blank as in ssb-db to merge into global namespace
// most of this stuff is from ssb-db
const validate = require('ssb-validate')
const keys = require('ssb-keys')
const pull = require('pull-stream')
var Obv = require('obv')
exports.manifest = {
createHistoryStream: 'source',
partialReplication: {
partialReplication: 'source'
}
}
exports.permissions = {
anonymous: {allow: ['createHistoryStream'], deny: null}
}
exports.init = function (sbot, config) {
// ebt stuff
sbot.createHistoryStream = function() {
return pull.empty()
}
sbot.post = Obv()
sbot.getVectorClock = function (_, cb) {
if (!cb) cb = _
function getClock()
{
var last = SSB.db.last.get()
var clock = {}
for (var k in last) {
clock[k] = last[k].sequence
}
cb(null, clock)
}
SSB.events.on('SSB: loaded', getClock)
}
function isString (s) {
return typeof s === 'string'
}
function originalValue(value) {
var copy = {}
for (let key in value) {
if (key !== 'meta' && key !== 'cyphertext' && key !== 'private' && key !== 'unbox') {
copy[key] = value[key]
}
}
if (value.meta && value.meta.original) {
for (let key in value.meta.original) {
copy[key] = value.meta.original[key]
}
}
return copy
}
function originalData(data) {
data.value = originalValue(data.value)
return data
}
sbot.getAtSequence = function (seqid, cb) {
// will NOT expose private plaintext
SSB.db.clock.get(isString(seqid) ? seqid.split(':') : seqid, function (err, value) {
if (err) cb(err)
else cb(null, originalData(value))
})
}
function decryptMessage(msg) {
return keys.unbox(msg.content, SSB.net.config.keys.private)
}
const hmac_key = null
function updateProfile(msg) {
if (!SSB.profiles)
SSB.profiles = {}
if (!SSB.profiles[msg.author])
SSB.profiles[msg.author] = {}
if (msg.content.name)
SSB.profiles[msg.author].name = msg.content.name
if (msg.content.description)
SSB.profiles[msg.author].description = msg.content.description
if (msg.content.image && typeof msg.content.image.link === 'string')
SSB.profiles[msg.author].image = msg.content.image.link
else if (typeof msg.content.image === 'string')
SSB.profiles[msg.author].image = msg.content.image
}
sbot.add = function(msg, cb) {
if (!(msg.author in SSB.state.feeds))
SSB.state = validate.appendOOO(SSB.state, hmac_key, msg)
else
SSB.state = validate.append(SSB.state, hmac_key, msg)
if (SSB.state.error)
return cb(SSB.state.error)
var last = SSB.db.last.update(msg)
var ok = true
if (last.partial) {
var isPrivate = (typeof (msg.content) === 'string')
if (isPrivate && !SSB.privateMessages) {
ok = false
} else if (!isPrivate && msg.content.type == 'about' && msg.content.about == msg.author) {
ok = true
updateProfile(msg)
} else if (!isPrivate && !SSB.validMessageTypes.includes(msg.content.type)) {
ok = false
} else if (isPrivate) {
var decrypted = decryptMessage(msg)
if (!decrypted) // not for us
ok = false
}
}
else if (msg.content.type == 'about' && msg.content.about == msg.author)
updateProfile(msg)
if (ok)
SSB.db.add(msg, cb)
else {
SSB.db.last.setPartialLogState(msg.author, true)
cb()
}
}
return {}
}