trac-peer 0.1.33 → 0.1.35
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/api.js +20 -0
- package/src/check.js +27 -1
- package/src/contract.js +1 -1
- package/src/functions.js +20 -2
- package/src/index.js +46 -7
package/package.json
CHANGED
package/src/api.js
CHANGED
|
@@ -79,6 +79,26 @@ export class ProtocolApi{
|
|
|
79
79
|
return res;
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
+
async getPinnedMessageLength(signed = true){
|
|
83
|
+
let res = null;
|
|
84
|
+
if(true === signed) res = await this.peer.protocol_instance.getSigned('pnl');
|
|
85
|
+
if(false === signed) res = await this.peer.protocol_instance.get('pnl');
|
|
86
|
+
res = res !== null ? res : 0;
|
|
87
|
+
console.log(JSON.stringify(res));
|
|
88
|
+
return res;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async getPinnedMessage(index, signed = true){
|
|
92
|
+
let res = null;
|
|
93
|
+
if(true === signed) res = await this.peer.protocol_instance.getSigned('pni/'+parseInt(index));
|
|
94
|
+
if(false === signed) res = await this.peer.protocol_instance.get('pni/'+parseInt(index));
|
|
95
|
+
if(null === res) {
|
|
96
|
+
console.log(JSON.stringify(null));
|
|
97
|
+
return null
|
|
98
|
+
}
|
|
99
|
+
return await this.getMessage(res.msg, signed);
|
|
100
|
+
}
|
|
101
|
+
|
|
82
102
|
async getDeletedMessageLength(signed = true){
|
|
83
103
|
let res = null;
|
|
84
104
|
if(true === signed) res = await this.peer.protocol_instance.getSigned('delml');
|
package/src/check.js
CHANGED
|
@@ -79,6 +79,7 @@ class Check {
|
|
|
79
79
|
this._nick = this.compileNick();
|
|
80
80
|
this._mute = this.compileMute();
|
|
81
81
|
this._delete_message = this.compileDeleteMessage();
|
|
82
|
+
this._pin_message = this.compilePinMessage();
|
|
82
83
|
this._mod = this.compileMod();
|
|
83
84
|
this._whitelist_status = this.compileWhitelistStatus();
|
|
84
85
|
this._enable_whitelist = this.compileEnableWhitelist();
|
|
@@ -174,6 +175,29 @@ class Check {
|
|
|
174
175
|
return res === true;
|
|
175
176
|
}
|
|
176
177
|
|
|
178
|
+
compilePinMessage (){
|
|
179
|
+
const schema = {
|
|
180
|
+
nonce: { type : "string", min : 1, max : 256 },
|
|
181
|
+
hash: { type : "is_hex" },
|
|
182
|
+
value : {
|
|
183
|
+
$$type: "object",
|
|
184
|
+
dispatch : {
|
|
185
|
+
$$type : "object",
|
|
186
|
+
id : { type : "number", integer: true, min : 0, max : 2147483647 },
|
|
187
|
+
pinned : { type : "boolean" },
|
|
188
|
+
type : { type : "string", min : 1, max : 256 },
|
|
189
|
+
address : { type : "is_hex" }
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
return this.validator.compile(schema);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
pinMessage(op){
|
|
197
|
+
const res = this._pin_message(op);
|
|
198
|
+
return res === true;
|
|
199
|
+
}
|
|
200
|
+
|
|
177
201
|
compileMute (){
|
|
178
202
|
const schema = {
|
|
179
203
|
nonce: { type : "string", min : 1, max : 256 },
|
|
@@ -359,7 +383,9 @@ class Check {
|
|
|
359
383
|
type : { type : "string", min : 1, max : 256 },
|
|
360
384
|
address : { type : "is_hex" },
|
|
361
385
|
deleted_by : { type : "is_hex", nullable : true },
|
|
362
|
-
reply_to : { type : "number", integer : true, min : 0, max : 2147483647, nullable : true }
|
|
386
|
+
reply_to : { type : "number", integer : true, min : 0, max : 2147483647, nullable : true },
|
|
387
|
+
pinned : { type : "boolean" },
|
|
388
|
+
pin_id : { type : "number", integer : true, min : 0, max : 2147483647, nullable : true },
|
|
363
389
|
}
|
|
364
390
|
}
|
|
365
391
|
};
|
package/src/contract.js
CHANGED
|
@@ -184,7 +184,7 @@ class Contract {
|
|
|
184
184
|
return key.startsWith('sh/') || key.startsWith('tx/') || key === 'msgl' || key.startsWith('kcin/') || key.startsWith('delm/') ||
|
|
185
185
|
key.startsWith('umsg/') || key.startsWith('umsg/') || key.startsWith('msgl/') || key === 'admin' || key === 'auto_add_writers'
|
|
186
186
|
|| key.startsWith('nick/') || key.startsWith('mod/') || key === 'chat_status' || key.startsWith('mtd/') || key === 'delml' ||
|
|
187
|
-
key === 'wlst' || key === 'txl' || key.startsWith('txi/') || key.startsWith('wl/');
|
|
187
|
+
key === 'wlst' || key === 'txl' || key.startsWith('txi/') || key.startsWith('wl/') || key === 'pnl' || key.startsWith('pni/');
|
|
188
188
|
}
|
|
189
189
|
|
|
190
190
|
async del(key){
|
package/src/functions.js
CHANGED
|
@@ -149,6 +149,22 @@ export async function enableWhitelist(input, peer){
|
|
|
149
149
|
await peer.base.append({type: 'enableWhitelist', value: signature, hash : hash, nonce: nonce });
|
|
150
150
|
}
|
|
151
151
|
|
|
152
|
+
export async function pinMessage(input, peer){
|
|
153
|
+
let address = null;
|
|
154
|
+
const splitted = peer.protocol_instance.parseArgs(input)
|
|
155
|
+
const value = parseInt(splitted.id);
|
|
156
|
+
const pinned = parseInt(splitted.pin) === 1;
|
|
157
|
+
const nonce = Math.random() + '-' + Date.now();
|
|
158
|
+
const signature = { dispatch : {
|
|
159
|
+
type : 'pinMessage',
|
|
160
|
+
id: value,
|
|
161
|
+
pinned : pinned,
|
|
162
|
+
address : peer.wallet.publicKey
|
|
163
|
+
}};
|
|
164
|
+
const hash = peer.wallet.sign(JSON.stringify(signature) + nonce);
|
|
165
|
+
await peer.base.append({type: 'pinMessage', value: signature, hash : hash, nonce: nonce });
|
|
166
|
+
}
|
|
167
|
+
|
|
152
168
|
export async function deleteMessage(input, peer){
|
|
153
169
|
let address = null;
|
|
154
170
|
const splitted = peer.protocol_instance.parseArgs(input)
|
|
@@ -157,7 +173,7 @@ export async function deleteMessage(input, peer){
|
|
|
157
173
|
const signature = { dispatch : {
|
|
158
174
|
type : 'deleteMessage',
|
|
159
175
|
id: value,
|
|
160
|
-
address : peer.wallet.publicKey
|
|
176
|
+
address : peer.wallet.publicKey
|
|
161
177
|
}};
|
|
162
178
|
const hash = peer.wallet.sign(JSON.stringify(signature) + nonce);
|
|
163
179
|
await peer.base.append({type: 'deleteMessage', value: signature, hash : hash, nonce: nonce });
|
|
@@ -236,7 +252,9 @@ export async function postMessage(input, peer){
|
|
|
236
252
|
address : peer.wallet.publicKey,
|
|
237
253
|
attachments : [],
|
|
238
254
|
deleted_by : null,
|
|
239
|
-
reply_to : reply_to
|
|
255
|
+
reply_to : reply_to,
|
|
256
|
+
pinned : false,
|
|
257
|
+
pin_id : null
|
|
240
258
|
}};
|
|
241
259
|
const hash = peer.wallet.sign(JSON.stringify(signature) + nonce);
|
|
242
260
|
await peer.base.append({type: 'msg', value: signature, hash : hash, nonce: nonce });
|
package/src/index.js
CHANGED
|
@@ -13,7 +13,8 @@ const wakeup = new w();
|
|
|
13
13
|
import {
|
|
14
14
|
addWriter, addAdmin, setAutoAddWriters, setChatStatus, setMod, deleteMessage,
|
|
15
15
|
enableWhitelist, postMessage, jsonStringify, visibleLength, setNick,
|
|
16
|
-
muteStatus, setWhitelistStatus, updateAdmin, tx, safeClone, jsonParse
|
|
16
|
+
muteStatus, setWhitelistStatus, updateAdmin, tx, safeClone, jsonParse,
|
|
17
|
+
pinMessage
|
|
17
18
|
} from "./functions.js";
|
|
18
19
|
import Check from "./check.js";
|
|
19
20
|
import DHT from 'hyperdht';
|
|
@@ -53,7 +54,7 @@ export class Peer extends ReadyResource {
|
|
|
53
54
|
this.connectedPeers = new Set();
|
|
54
55
|
this.options = options;
|
|
55
56
|
this.check = new Check();
|
|
56
|
-
this.dhtBootstrap = [
|
|
57
|
+
this.dhtBootstrap = ['116.202.214.143:10001','116.202.214.149:10001', 'node1.hyperdht.org:49737', 'node2.hyperdht.org:49737', 'node3.hyperdht.org:49737'];
|
|
57
58
|
this.dhtNode = null;
|
|
58
59
|
this.seen_auto_add = {};
|
|
59
60
|
this.validator = null;
|
|
@@ -330,11 +331,11 @@ export class Peer extends ReadyResource {
|
|
|
330
331
|
if(false === this.check.mute(op)) continue;
|
|
331
332
|
const admin = await batch.get('admin');
|
|
332
333
|
const str_value = jsonStringify(op.value);
|
|
333
|
-
if(null !==
|
|
334
|
+
if(null !== str_value &&
|
|
334
335
|
null === await batch.get('sh/'+op.hash)){
|
|
335
336
|
const mod = await batch.get('mod/'+op.value.dispatch.address);
|
|
336
337
|
let mod_verified = false;
|
|
337
|
-
if(null !== mod && true === mod.value && admin !== op.value.dispatch.user) {
|
|
338
|
+
if(null !== mod && true === mod.value && admin.value !== op.value.dispatch.user) {
|
|
338
339
|
const target_mod = await batch.get('mod/'+op.value.dispatch.user);
|
|
339
340
|
if(null === target_mod || false === target_mod.value) {
|
|
340
341
|
mod_verified = _this.wallet.verify(op.hash, str_value + op.nonce, op.value.dispatch.address);
|
|
@@ -349,13 +350,13 @@ export class Peer extends ReadyResource {
|
|
|
349
350
|
}
|
|
350
351
|
} else if(op.type === 'deleteMessage') {
|
|
351
352
|
if(false === this.check.deleteMessage(op)) continue;
|
|
352
|
-
const admin = await batch.get('admin');
|
|
353
353
|
const str_value = jsonStringify(op.value);
|
|
354
|
-
if(null !==
|
|
354
|
+
if(null !== str_value &&
|
|
355
355
|
null === await batch.get('sh/'+op.hash)){
|
|
356
356
|
const mod = await batch.get('mod/'+op.value.dispatch.address);
|
|
357
357
|
const message = await batch.get('msg/'+op.value.dispatch.id);
|
|
358
358
|
if(null !== message && null !== message.value && message.value.deleted_by !== undefined && null === message.value.deleted_by) {
|
|
359
|
+
const admin = await batch.get('admin');
|
|
359
360
|
let mod_verified = false;
|
|
360
361
|
if(null !== mod && true === mod.value && message.value.address !== admin.value) {
|
|
361
362
|
mod_verified = _this.wallet.verify(op.hash, str_value + op.nonce, op.value.dispatch.address);
|
|
@@ -376,7 +377,7 @@ export class Peer extends ReadyResource {
|
|
|
376
377
|
} else {
|
|
377
378
|
len = len.value;
|
|
378
379
|
}
|
|
379
|
-
await batch.put('msg/'+op.value.dispatch.id, message);
|
|
380
|
+
await batch.put('msg/'+op.value.dispatch.id, message.value);
|
|
380
381
|
await batch.put('delm/'+len, op.value.dispatch.id);
|
|
381
382
|
await batch.put('delml', len + 1);
|
|
382
383
|
await batch.put('sh/'+op.hash, '');
|
|
@@ -384,6 +385,41 @@ export class Peer extends ReadyResource {
|
|
|
384
385
|
}
|
|
385
386
|
}
|
|
386
387
|
}
|
|
388
|
+
} else if(op.type === 'pinMessage') {
|
|
389
|
+
if(false === this.check.pinMessage(op)) continue;
|
|
390
|
+
const str_value = jsonStringify(op.value);
|
|
391
|
+
if(null !== str_value &&
|
|
392
|
+
null === await batch.get('sh/'+op.hash)){
|
|
393
|
+
const mod = await batch.get('mod/'+op.value.dispatch.address);
|
|
394
|
+
const message = await batch.get('msg/'+op.value.dispatch.id);
|
|
395
|
+
if(null !== message && null !== message.value) {
|
|
396
|
+
const admin = await batch.get('admin');
|
|
397
|
+
let mod_verified = false;
|
|
398
|
+
if(null !== mod && true === mod.value) {
|
|
399
|
+
mod_verified = _this.wallet.verify(op.hash, str_value + op.nonce, op.value.dispatch.address);
|
|
400
|
+
}
|
|
401
|
+
const verified = _this.wallet.verify(op.hash, str_value + op.nonce, admin.value);
|
|
402
|
+
if(true === verified || true === mod_verified) {
|
|
403
|
+
if(null === message.value.pin_id){
|
|
404
|
+
let pin_len = await batch.get('pnl');
|
|
405
|
+
if(null === pin_len) {
|
|
406
|
+
pin_len = 0;
|
|
407
|
+
} else {
|
|
408
|
+
pin_len = pin_len.value;
|
|
409
|
+
}
|
|
410
|
+
message.value.pin_id = pin_len;
|
|
411
|
+
await batch.put('pni/'+pin_len, { msg : op.value.dispatch.id, pinned : op.value.dispatch.pinned });
|
|
412
|
+
await batch.put('pnl', pin_len + 1);
|
|
413
|
+
} else {
|
|
414
|
+
await batch.put('pni/'+message.value.pin_id, { msg : op.value.dispatch.id, pinned : op.value.dispatch.pinned });
|
|
415
|
+
}
|
|
416
|
+
message.value.pinned = op.value.dispatch.pinned;
|
|
417
|
+
await batch.put('msg/'+op.value.dispatch.id, message.value);
|
|
418
|
+
await batch.put('sh/'+op.hash, '');
|
|
419
|
+
console.log(`Pinned message ${op.value.dispatch.id} by ${op.value.dispatch.address}`);
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
}
|
|
387
423
|
} else if(op.type === 'setMod') {
|
|
388
424
|
if(false === this.check.mod(op)) continue;
|
|
389
425
|
console.log('after');
|
|
@@ -774,6 +810,7 @@ export class Peer extends ReadyResource {
|
|
|
774
810
|
console.log('- /mute_status | Only admin and mods. Mute or unmute a user by their address: \'/mute_status --user "<address>" --muted 1\'.');
|
|
775
811
|
console.log('- /set_mod | Only admin. Set a user as mod: \'/set_mod --user "<address>" --mod 1\'.');
|
|
776
812
|
console.log('- /delete_message | Delete a message: \'/delete_message --id 1\'. Chat must be enabled.');
|
|
813
|
+
console.log('- /pin_message | Set the pin status of a message: \'/pin_message --id 1 --pin 1\'. Chat must be enabled.');
|
|
777
814
|
console.log('- /enable_whitelist | Only admin. Enable/disable chat whitelists: \'/enable_whitelist --enabled 1\'.');
|
|
778
815
|
console.log('- /set_whitelist_status | Only admin. Add/remove users to/from the chat whitelist: \'/set_whitelist_status --user "<address>" --status 1\'.');
|
|
779
816
|
console.log(' ');
|
|
@@ -819,6 +856,8 @@ export class Peer extends ReadyResource {
|
|
|
819
856
|
await setNick(input, this);
|
|
820
857
|
} else if (input.startsWith('/mute_status')) {
|
|
821
858
|
await muteStatus(input, this);
|
|
859
|
+
} else if (input.startsWith('/pin_message')) {
|
|
860
|
+
await pinMessage(input, this);
|
|
822
861
|
} else if (input.startsWith('/set_mod')) {
|
|
823
862
|
await setMod(input, this);
|
|
824
863
|
} else if (input.startsWith('/delete_message')) {
|