mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-06-27 00:12:29 +00:00
Add setMode() for settings channel read and write (ops) modes.
This commit is contained in:
parent
f831ad34a7
commit
faad4c1c4f
@ -19,6 +19,8 @@ class OrbitClient {
|
||||
constructor(ipfs) {
|
||||
this.sequences = {};
|
||||
this.ipfs = ipfs;
|
||||
this.network = {};
|
||||
this.user = null;
|
||||
}
|
||||
|
||||
channel(hash, password) {
|
||||
@ -29,7 +31,7 @@ class OrbitClient {
|
||||
return this._send(hash, password, text, options);
|
||||
},
|
||||
delete: () => this._delete(hash, password),
|
||||
setMode: (mode) => this._setMode(hash, modes)
|
||||
setMode: (mode) => this._setMode(hash, password, mode)
|
||||
}
|
||||
}
|
||||
|
||||
@ -43,7 +45,7 @@ class OrbitClient {
|
||||
return seq;
|
||||
}
|
||||
|
||||
_iterator(channel, password, options) {
|
||||
_getMessages(channel, password, options) {
|
||||
var currentIndex = 0;
|
||||
var messages = [];
|
||||
|
||||
@ -69,13 +71,13 @@ class OrbitClient {
|
||||
|
||||
if(startFromHash) {
|
||||
// Get messages
|
||||
messages = this._fetchRecursive(startFromHash, password, limit, gte ? gte : gt, 0);
|
||||
messages = this._fetchRecursive(startFromHash, password, limit, gte ? gte : gt);
|
||||
|
||||
// Slice the array
|
||||
var startIndex = 0;
|
||||
var endIndex = messages.length;
|
||||
if(limit > -1) {
|
||||
startIndex = Math.max(messages.length - limit, 0);
|
||||
startIndex = Math.max(0, messages.length - limit);
|
||||
endIndex = messages.length - ((gt || lt) ? 1 : 0);
|
||||
} else if(limit === -1) {
|
||||
endIndex = messages.length - (gt ? 1 : 0);
|
||||
@ -86,6 +88,9 @@ class OrbitClient {
|
||||
|
||||
if(reverse) messages.reverse();
|
||||
|
||||
_iterator(channel, password, options) {
|
||||
var messages = this._getMessages(channel, password, options);
|
||||
|
||||
// Iterator interface implementation
|
||||
let iterator = {
|
||||
[Symbol.iterator]() {
|
||||
@ -130,6 +135,8 @@ class OrbitClient {
|
||||
_fetchRecursive(hash, password, amount, last, currentDepth) {
|
||||
var res = [];
|
||||
|
||||
if(!currentDepth) currentDepth = 0;
|
||||
|
||||
if(!last && amount > -1 && currentDepth >= amount)
|
||||
return res;
|
||||
|
||||
@ -188,12 +195,23 @@ class OrbitClient {
|
||||
return true;
|
||||
}
|
||||
|
||||
_setMode(channel, modes) {
|
||||
/* TODO */
|
||||
_setMode(channel, password, modes) {
|
||||
var m = []
|
||||
if(typeof modes !== 'Array')
|
||||
m.push(modes);
|
||||
else
|
||||
m = modes;
|
||||
return await(this.client.linkedList(channel, password).setMode(m))
|
||||
}
|
||||
|
||||
_connect(host, username, password) {
|
||||
this.client = await(HashCache.connect(host, username, password));
|
||||
this.user = this.client.info.user;
|
||||
this.network = {
|
||||
id: this.client.info.networkId,
|
||||
name: this.client.info.name,
|
||||
config: this.client.info.config
|
||||
};
|
||||
return;
|
||||
}
|
||||
|
||||
|
10
README.md
10
README.md
@ -63,7 +63,15 @@ async(() => {
|
||||
// for(let i of iter)
|
||||
// console.log(i.hash, i.item.Data.seq);
|
||||
|
||||
// delete channel
|
||||
// Set modes
|
||||
var password = 'hello';
|
||||
var channelModes;
|
||||
channelModes = orbit.channel(channel, channelPwd).setMode({ mode: "+r", params: { password: password } }); // { modes: { r: { password: 'hello' } } }
|
||||
channelModes = orbit.channel(channel, password).setMode({ mode: "+w", params: { ops: [orbit.user.id] } }); // { modes: { ... } }
|
||||
channelModes = orbit.channel(channel, password).setMode({ mode: "-r" }); // { modes: { ... } }
|
||||
channelModes = orbit.channel(channel, '').setMode({ mode: "-w" }); // { modes: {} }
|
||||
|
||||
// Delete channel
|
||||
var result = orbit.channel(channelName, channelPwd).delete(); // true | false
|
||||
})();
|
||||
```
|
||||
|
@ -35,7 +35,7 @@ describe('Orbit Client', () => {
|
||||
let head = '';
|
||||
let second = '';
|
||||
let items = [];
|
||||
let channel = 'abc';
|
||||
let channel = 'abc1';
|
||||
|
||||
before(function(done) {
|
||||
// logger.setLevel('ERROR');
|
||||
@ -58,6 +58,7 @@ describe('Orbit Client', () => {
|
||||
// }
|
||||
var start = () => new Promise(async((resolve, reject) => {
|
||||
orbit = OrbitClient.connect(host, username, password);
|
||||
orbit.channel(channel, 'hello').setMode({ mode: "-r" })
|
||||
resolve();
|
||||
}));
|
||||
start().then(done);
|
||||
@ -75,12 +76,17 @@ describe('Orbit Client', () => {
|
||||
it('connects to hash-cache-server', async((done) => {
|
||||
assert.notEqual(orbit, null);
|
||||
assert.notEqual(orbit.client, null);
|
||||
assert.equal(orbit.user.id, 'QmcLzfQBKuvBYLsmgt4nkaUM7i7LNL37dPtnBZWgGpjPRW');
|
||||
assert.equal(orbit.network.id, 'anon-test');
|
||||
assert.equal(orbit.network.name, 'Anonymous Networks TEST');
|
||||
assert.notEqual(orbit.network.config.SupernodeRouting, null);
|
||||
assert.equal(orbit.network.config.Bootstrap.length, 3);
|
||||
done();
|
||||
}));
|
||||
});
|
||||
|
||||
describe('Delete channel', function() {
|
||||
it('deletes a channel', async((done) => {
|
||||
describe('Delete', function() {
|
||||
it('deletes a channel from the database', async((done) => {
|
||||
var result = orbit.channel(channel, '').delete();
|
||||
assert.equal(result, true);
|
||||
var iter = orbit.channel(channel, '').iterator();
|
||||
@ -440,6 +446,81 @@ describe('Orbit Client', () => {
|
||||
|
||||
});
|
||||
|
||||
|
||||
describe('Modes', function() {
|
||||
var password = 'hello';
|
||||
|
||||
it('sets read mode', async((done) => {
|
||||
try {
|
||||
var mode = {
|
||||
mode: "+r",
|
||||
params: {
|
||||
password: password
|
||||
}
|
||||
};
|
||||
var res = orbit.channel(channel, '').setMode(mode)
|
||||
assert.notEqual(res.modes.r, null);
|
||||
assert.equal(res.modes.r.password, password);
|
||||
} catch(e) {
|
||||
assert.equal(e, null);
|
||||
}
|
||||
done();
|
||||
}));
|
||||
|
||||
it('can\'t read with wrong password', async((done) => {
|
||||
try {
|
||||
var res = orbit.channel(channel, '').iterator();
|
||||
assert.equal(true, false);
|
||||
} catch(e) {
|
||||
assert.equal(e, 'Unauthorized');
|
||||
}
|
||||
done();
|
||||
}));
|
||||
|
||||
it('sets write mode', async((done) => {
|
||||
try {
|
||||
var mode = {
|
||||
mode: "+w",
|
||||
params: {
|
||||
ops: [orbit.user.id]
|
||||
}
|
||||
};
|
||||
var res = orbit.channel(channel, password).setMode(mode);
|
||||
assert.notEqual(res.modes.w, null);
|
||||
assert.equal(res.modes.w.ops[0], orbit.user.id);
|
||||
} catch(e) {
|
||||
assert.equal(e, null);
|
||||
}
|
||||
done();
|
||||
}));
|
||||
|
||||
it('can\'t write when user not an op', async((done) => {
|
||||
// TODO
|
||||
done();
|
||||
}));
|
||||
|
||||
it('removes write mode', async((done) => {
|
||||
try {
|
||||
var res = orbit.channel(channel, password).setMode({ mode: "-w" });
|
||||
assert.equal(res.modes.w, null);
|
||||
} catch(e) {
|
||||
assert.equal(e, null);
|
||||
}
|
||||
done();
|
||||
}));
|
||||
|
||||
it('removes read mode', async((done) => {
|
||||
try {
|
||||
var res = orbit.channel(channel, password).setMode({ mode: "-r" });
|
||||
assert.equal(res.modes.r, null);
|
||||
} catch(e) {
|
||||
assert.equal(e, null);
|
||||
}
|
||||
done();
|
||||
}));
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
// let rmDir = function(dirPath) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user