This repository has been archived by the owner on Mar 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathlib.overlay.js
230 lines (214 loc) · 8.71 KB
/
lib.overlay.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
// even though we want to conform to the dialect API
// we need to amend data_access and dispatcher with our own models and api
//
// best we have a single entry point for all our common dialect to reduce set up in them
const bb = require('bytebuffer');
const libsignal = require('libsignal');
const storage = require('./storage');
const config = require('./lib.config');
const logic = require('./logic');
const dialect = require('./lib.dialect');
const loki_crypt = require('./lib.loki_crypt');
const platformConfigUtil = require('./server/lib/lib.config');
// used for creating a default token for user 1
const ADN_SCOPES = 'basic stream write_post follow messages update_profile files export';
// Look for a config file
const disk_config = config.getDiskConfig();
preflight = false;
const setup = (cache, dispatcher) => {
// I guess we're do preflight checks here...
const dataAccess = cache;
// preflight checks
const addChannelNote = (channelId) => {
var defaultObj = {"name":"Your Public Chat","description":"Your public chat room","avatar":"images/group_default.png"};
dataAccess.addAnnotation('channel', channelId, 'net.patter-app.settings', defaultObj, function(err, rec, meta) {
if (err) console.error('err', err);
if (!rec) {
console.warn('annotation', JSON.parse(JSON.stringify(rec)), 'meta', meta);
}
});
}
const addChannelMessage = (privKey, channelId) => {
return new Promise(resolve => {
dataAccess.addMessage({
channel_id: channelId,
text: 'system generated initial message',
machine_only: 0,
thread_id: 0,
userid: 1,
reply_to: 0,
is_deleted: 0,
created_at: new Date
}, async (err, msg) =>{
if (err) console.error('addChannelMessage err', err);
// console.log('addChannelMessage msg', JSON.parse(JSON.stringify(msg)));
if (msg.id) {
var defaultObj = {
timestamp: parseInt(Date.now() / 1000),
};
/*
const sigData = getSigData(1, defaultObj, {
text: msg.text
});
const sig = await libsignal.curve.calculateSignature(privKey, sigData);
defaultObj.sigver = 1;
defaultObj.sig = sig.toString('hex');
*/
defaultObj.sig = await loki_crypt.getSigData(1, privKey, defaultObj, {
text: msg.text
});
defaultObj.sigver = 1;
dataAccess.addAnnotation('message', msg.id, 'network.loki.messenger.publicChat', defaultObj, function(err, rec, meta) {
// , JSON.parse(JSON.stringify(rec))
console.log('created initial message for mobile');
resolve(err, msg);
});
}
});
});
}
// only do this once on startup...
if (!preflight) {
preflight = true
config.setup({ cache, storage });
logic.setup({ storage, cache, config });
dialect.setup({ dispatcher });
storage.start(disk_config);
// only set up a channels, if channels enabled (open group mode)
if (platformConfigUtil.moduleEnabled('channels')) {
console.log('Open group mode detected')
dataAccess.getChannel(1, {}, async (err, chnl, meta) => {
if (err) console.error('channel 1 get err', err);
if (chnl && chnl.id) {
const configWhitelistEnabled = !!disk_config.whitelist;
// do read permissions match?
// write shouldn't matter, if you can't get a token/user, you can't write
const channelWhitelistEnabled = chnl.reader !== 0;
console.log('configWhitelistEnabled', configWhitelistEnabled);
console.log('channelWhitelistEnabled', channelWhitelistEnabled);
if (configWhitelistEnabled != channelWhitelistEnabled) {
console.log('Need to fix up channel permissions');
// this will disable public reading of the channel
// would this work with proxy-admin system?
// 0 = public, 1 = any user (has token)
dataAccess.updateChannel(1, { reader: configWhitelistEnabled ? 1 : 0 }, function(err, channel) {
if (err) console.error('overlay updateChannel err', err);
else console.log('updated channel permissions', channel);
});
}
if (configWhitelistEnabled) {
// just make sure our owner is whitelisted for proxy mod actions
console.log('checking', chnl.ownerid);
if (chnl.ownerid) {
const alreadyWhitelisted = await storage.isWhitelisted(chnl.ownerid);
if (!alreadyWhitelisted) {
console.log('whitelisting channel owner, userid:', chnl.ownerid);
logic.whitelistUserForServer(chnl.ownerid);
}
}
}
return;
}
console.log('need to create initial channel');
// FIXME: user token_helpers's findOrCreateUser?
dataAccess.getUser(1, async (err2, user, meta2) => {
if (err2) console.error('get user 1 err', err2);
// if no user, create the user...
// user === null when D.N.E.
// console.log('user', user);
var privKey, pubKey;
if (!user || !user.length) {
console.log('need to create initial user');
// block until this is complete
user = await new Promise((resolve, rej) => {
const ourKey = libsignal.curve.generateKeyPair();
privKey = ourKey.privKey;
pubKey = ourKey.pubKey;
var pubKeyhex = bb.wrap(ourKey.pubKey).toString('hex')
dataAccess.addUser(pubKeyhex, '', async function(err4, user, meta4) {
if (err4) console.error('add user 1 err', err4);
// maybe some annotation to set the profile name...
// maybe a session icon?
// console.log('schemaType', storage.schemaType)
if (storage.schemaType === 'memory') {
// lets prompt him to mod too...
console.log('Giving temp mod to', user.id)
config.addTempModerator(user.id)
if (config.inWhiteListMode()) {
// add them to the white list...
const result = await logic.whitelistUserForServer(user.id);
if (!result) {
console.warn('could not whitelist!')
}
}
// generate a token for server/tests
cache.createOrFindUserToken(user.id, 'messenger', ADN_SCOPES, function(err5, token) {
if (err5) console.error('add user 1 token err', err5);
console.log('generated token', JSON.parse(JSON.stringify(token)));
})
}
resolve(user);
});
});
console.log('user', user.id, 'created!');
}
// no channel, so we need to create this public channel
dataAccess.addChannel(1, {
type: 'network.loki.messenger.chat.public',
reader: 0,
writer: 1,
readedit: 1,
writeedit: 1,
editedit: 1,
readers: [],
writers: [],
editors: [],
}, (err3, chnl, meta3) => {
if (err3) console.error('addChannel err', err3);
if (chnl && chnl.id) {
console.log('channel', chnl.id, 'created');
addChannelNote(chnl.id);
// only can do this if we just created the userid 1
if (privKey) {
//console.log('need to create message 1!')
addChannelMessage(privKey, chnl.id);
}
} else {
console.error('Unable to set up channel')
}
});
});
});
// the race was causing this to create a duplicate annotation
/*
dataAccess.getAnnotations('channel', 1, (notes, err, meta) => {
if (err) console.error('getAnnotations channel err', err);
//console.log('notes', notes);
if (!notes || !notes.length) {
console.log('adding note')
addChannelNote(1);
}
});
*/
} else {
console.log('File server mode detected')
}
}
return { storage, logic, config, dialect, cache };
}
const getUserAccess = async (userid) => {
const globMod = await storage.isGlobalModerator(userid);
if (globMod) return true;
// just get a list a channels I'm a mod for...
const channels = await storage.getChannelModerator(userid);
if (channels.length) {
return channels.join(',');
}
// finally check local disk config
const configVal = config.globalAllow(userid);
return configVal ? configVal : false;
}
module.exports = {
setup,
getUserAccess
};