mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-05-19 13:26: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),
|
||||
get: (key, options) => {
|
||||
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)
|
||||
}
|
||||
|
@ -51,7 +51,7 @@ class OrbitDB {
|
||||
|
||||
if(opts.key) {
|
||||
// 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) {
|
||||
// Greater than case
|
||||
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
|
||||
add(channel, password, data) {
|
||||
let post;
|
||||
if(data instanceof Post) {
|
||||
if(data.Post) {
|
||||
post = data;
|
||||
} else {
|
||||
// Handle everything else as a string
|
||||
@ -84,7 +84,7 @@ class OrbitDB {
|
||||
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) {
|
||||
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';
|
||||
|
||||
const TextPost = require('./TextPost');
|
||||
|
||||
// A reference to a file
|
||||
class FilePost extends TextPost {
|
||||
constructor(content, file, size) {
|
||||
super(content);
|
||||
class FilePost {
|
||||
constructor(name, hash, size) {
|
||||
this.type = "file";
|
||||
this.file = file;
|
||||
this.name = name;
|
||||
this.hash = hash;
|
||||
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 Encryption = require('orbit-common/lib/Encryption');
|
||||
|
||||
const TextPost = require('./TextPost');
|
||||
const FilePost = require('./FilePost');
|
||||
const OrbitDBItem = require('./OrbitDBItem');
|
||||
const MetaInfo = require('./MetaInfo');
|
||||
const TextPost = require('./TextPost');
|
||||
const FilePost = require('./FilePost');
|
||||
const DirectoryPost = require('./DirectoryPost');
|
||||
const OrbitDBItem = require('./OrbitDBItem');
|
||||
const MetaInfo = require('./MetaInfo');
|
||||
|
||||
const PostTypes = {
|
||||
Message: TextPost,
|
||||
Snippet: "snippet",
|
||||
File: FilePost,
|
||||
List: "list",
|
||||
Directory: DirectoryPost,
|
||||
Link: "link",
|
||||
OrbitDBItem: OrbitDBItem
|
||||
};
|
||||
@ -24,17 +25,22 @@ class Posts {
|
||||
static create(ipfs, type, data) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let post;
|
||||
|
||||
if(type === PostTypes.Message) {
|
||||
post = new PostTypes.Message(data);
|
||||
} 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) {
|
||||
post = new PostTypes.OrbitDBItem(data.operation, data.key, data.value, data.meta, data.by);
|
||||
}
|
||||
|
||||
const size = data.size ? data.size : Buffer.byteLength(data, 'utf8');
|
||||
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)));
|
||||
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
|
||||
class TextPost {
|
||||
constructor(content) {
|
||||
this.content = content;
|
||||
this.type = "text";
|
||||
this.content = content;
|
||||
this.ts = new Date().getTime();
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user