mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-05-19 21:36:37 +00:00
Refactor POSTs, add DirectoryPost.
This commit is contained in:
parent
b6c355cdbc
commit
7e6d137fe6
@ -36,7 +36,7 @@ class Client {
|
|||||||
put: (key, data) => this.db.put(channel, password, key, data),
|
put: (key, data) => this.db.put(channel, password, key, data),
|
||||||
get: (key, options) => {
|
get: (key, options) => {
|
||||||
const items = this._iterator(channel, password, { key: key }).collect();
|
const items = this._iterator(channel, password, { key: key }).collect();
|
||||||
return items[0] ? items[0].content : null;
|
return items[0] ? items[0].content : null; // TODO: use KeyValuePost, currently .content is from TextPost
|
||||||
},
|
},
|
||||||
close: () => this._pubsub.unsubscribe(channel)
|
close: () => this._pubsub.unsubscribe(channel)
|
||||||
}
|
}
|
||||||
|
@ -51,7 +51,7 @@ class OrbitDB {
|
|||||||
|
|
||||||
if(opts.key) {
|
if(opts.key) {
|
||||||
// Key-Value, search latest key first
|
// Key-Value, search latest key first
|
||||||
result = this._read(operations.reverse(), opts.key, 1, true).map((f) => f.value);
|
result = this._read(operations.reverse(), opts.key, 1, true).map((f) => f.value); // TODO: use KeyValuePost
|
||||||
} else if(opts.gt || opts.gte) {
|
} else if(opts.gt || opts.gte) {
|
||||||
// Greater than case
|
// Greater than case
|
||||||
result = this._read(operations, opts.gt ? opts.gt : opts.gte, amount, opts.gte ? opts.gte : false);
|
result = this._read(operations, opts.gt ? opts.gt : opts.gte, amount, opts.gte ? opts.gte : false);
|
||||||
@ -69,7 +69,7 @@ class OrbitDB {
|
|||||||
// Adds an event to the log
|
// Adds an event to the log
|
||||||
add(channel, password, data) {
|
add(channel, password, data) {
|
||||||
let post;
|
let post;
|
||||||
if(data instanceof Post) {
|
if(data.Post) {
|
||||||
post = data;
|
post = data;
|
||||||
} else {
|
} else {
|
||||||
// Handle everything else as a string
|
// Handle everything else as a string
|
||||||
@ -84,7 +84,7 @@ class OrbitDB {
|
|||||||
return await(this._write(channel, password, DBOperation.Types.Put, key, post.Hash));
|
return await(this._write(channel, password, DBOperation.Types.Put, key, post.Hash));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deletes an event based on hash (of the operation)
|
// Deletes an event based on hash (of the operation) or 'key' of a key/val pair
|
||||||
del(channel, password, hash) {
|
del(channel, password, hash) {
|
||||||
return await(this._write(channel, password, DBOperation.Types.Delete, hash, null));
|
return await(this._write(channel, password, DBOperation.Types.Delete, hash, null));
|
||||||
}
|
}
|
||||||
|
14
src/post/DirectoryPost.js
Normal file
14
src/post/DirectoryPost.js
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
// A reference to a file
|
||||||
|
class DirectoryPost {
|
||||||
|
constructor(name, hash, size) {
|
||||||
|
this.type = "directory";
|
||||||
|
this.name = name;
|
||||||
|
this.hash = hash;
|
||||||
|
this.size = size;
|
||||||
|
this.ts = new Date().getTime();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = DirectoryPost;
|
@ -1,14 +1,13 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const TextPost = require('./TextPost');
|
|
||||||
|
|
||||||
// A reference to a file
|
// A reference to a file
|
||||||
class FilePost extends TextPost {
|
class FilePost {
|
||||||
constructor(content, file, size) {
|
constructor(name, hash, size) {
|
||||||
super(content);
|
|
||||||
this.type = "file";
|
this.type = "file";
|
||||||
this.file = file;
|
this.name = name;
|
||||||
|
this.hash = hash;
|
||||||
this.size = size;
|
this.size = size;
|
||||||
|
this.ts = new Date().getTime();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,16 +5,17 @@ const await = require('asyncawait/await');
|
|||||||
const ipfsAPI = require('orbit-common/lib/ipfs-api-promised');
|
const ipfsAPI = require('orbit-common/lib/ipfs-api-promised');
|
||||||
const Encryption = require('orbit-common/lib/Encryption');
|
const Encryption = require('orbit-common/lib/Encryption');
|
||||||
|
|
||||||
const TextPost = require('./TextPost');
|
const TextPost = require('./TextPost');
|
||||||
const FilePost = require('./FilePost');
|
const FilePost = require('./FilePost');
|
||||||
const OrbitDBItem = require('./OrbitDBItem');
|
const DirectoryPost = require('./DirectoryPost');
|
||||||
const MetaInfo = require('./MetaInfo');
|
const OrbitDBItem = require('./OrbitDBItem');
|
||||||
|
const MetaInfo = require('./MetaInfo');
|
||||||
|
|
||||||
const PostTypes = {
|
const PostTypes = {
|
||||||
Message: TextPost,
|
Message: TextPost,
|
||||||
Snippet: "snippet",
|
Snippet: "snippet",
|
||||||
File: FilePost,
|
File: FilePost,
|
||||||
List: "list",
|
Directory: DirectoryPost,
|
||||||
Link: "link",
|
Link: "link",
|
||||||
OrbitDBItem: OrbitDBItem
|
OrbitDBItem: OrbitDBItem
|
||||||
};
|
};
|
||||||
@ -24,17 +25,22 @@ class Posts {
|
|||||||
static create(ipfs, type, data) {
|
static create(ipfs, type, data) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let post;
|
let post;
|
||||||
|
|
||||||
if(type === PostTypes.Message) {
|
if(type === PostTypes.Message) {
|
||||||
post = new PostTypes.Message(data);
|
post = new PostTypes.Message(data);
|
||||||
} else if(type === PostTypes.File) {
|
} else if(type === PostTypes.File) {
|
||||||
post = new PostTypes.File(data.content, data.file, data.size);
|
post = new PostTypes.File(data.name, data.hash, data.size);
|
||||||
|
} else if(type == PostTypes.Directory) {
|
||||||
|
post = new PostTypes.Directory(data.name, data.hash, data.size);
|
||||||
} else if(type == PostTypes.OrbitDBItem) {
|
} else if(type == PostTypes.OrbitDBItem) {
|
||||||
post = new PostTypes.OrbitDBItem(data.operation, data.key, data.value, data.meta, data.by);
|
post = new PostTypes.OrbitDBItem(data.operation, data.key, data.value, data.meta, data.by);
|
||||||
}
|
}
|
||||||
|
|
||||||
const size = data.size ? data.size : Buffer.byteLength(data, 'utf8');
|
const size = data.size ? data.size : Buffer.byteLength(data, 'utf8');
|
||||||
post.meta = new MetaInfo(post.type, size, new Date().getTime());
|
post.meta = new MetaInfo(post.type, size, new Date().getTime());
|
||||||
|
if(post.type) delete post.type;
|
||||||
const res = await (ipfsAPI.putObject(ipfs, JSON.stringify(post)));
|
const res = await (ipfsAPI.putObject(ipfs, JSON.stringify(post)));
|
||||||
resolve(res);
|
resolve({ Post: post, Hash: res.Hash });
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,8 +5,8 @@ const Encryption = require('orbit-common/lib/Encryption');
|
|||||||
// Simplest type of post: a string
|
// Simplest type of post: a string
|
||||||
class TextPost {
|
class TextPost {
|
||||||
constructor(content) {
|
constructor(content) {
|
||||||
this.content = content;
|
|
||||||
this.type = "text";
|
this.type = "text";
|
||||||
|
this.content = content;
|
||||||
this.ts = new Date().getTime();
|
this.ts = new Date().getTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user