diff --git a/src/OrbitDB.js b/src/OrbitDB.js index 7520653..53a85ec 100644 --- a/src/OrbitDB.js +++ b/src/OrbitDB.js @@ -51,16 +51,13 @@ class OrbitDB { if(opts.key) { // Key-Value, search latest key first - result = this._read(operations.reverse(), opts.key, 1, true) - .map((f) => f.value["content"]); + result = this._read(operations.reverse(), opts.key, 1, true).map((f) => f.value.content); } 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) - // .map((f) => { return { key: f.key, value: f.value["content"] } }); } else { // Lower than and lastN case, search latest first by reversing the sequence result = this._read(operations.reverse(), opts.lt ? opts.lt : opts.lte, amount, opts.lte || !opts.lt).reverse() - // .map((f) => { return { key: f.key, value: f.value["content"] } }); } if(opts.reverse) result.reverse(); @@ -76,14 +73,14 @@ class OrbitDB { post = data; } else { // Handle everything else as a string - post = await(Post.create(this._ipfs, Post.Types.Message, data)); + post = await(Post.create(this._ipfs, Post.Types.Message, { content: data })); } return await(this._write(channel, password, DBOperation.Types.Add, post.Hash, post.Hash)); } // Sets a key-value pair put(channel, password, key, data) { - const post = await(Post.create(this._ipfs, Post.Types.Message, data)); + const post = await(Post.create(this._ipfs, Post.Types.Message, { content: data })); return await(this._write(channel, password, DBOperation.Types.Put, key, post.Hash)); } diff --git a/src/post/BasePost.js b/src/post/BasePost.js new file mode 100644 index 0000000..1d72a94 --- /dev/null +++ b/src/post/BasePost.js @@ -0,0 +1,10 @@ +'use strict'; + +// Base class +class Post { + constructor(type) { + this.type = type; + } +} + +module.exports = Post; diff --git a/src/post/DirectoryPost.js b/src/post/DirectoryPost.js index b65bfb5..7902f3e 100644 --- a/src/post/DirectoryPost.js +++ b/src/post/DirectoryPost.js @@ -1,13 +1,14 @@ 'use strict'; +const Post = require('./BasePost'); + // A reference to a file -class DirectoryPost { +class DirectoryPost extends Post { constructor(name, hash, size) { - this.type = "directory"; + super("directory"); this.name = name; this.hash = hash; this.size = size; - this.ts = new Date().getTime(); } } diff --git a/src/post/FilePost.js b/src/post/FilePost.js index 5e00e54..5f4638c 100644 --- a/src/post/FilePost.js +++ b/src/post/FilePost.js @@ -1,13 +1,14 @@ 'use strict'; +const Post = require('./BasePost'); + // A reference to a file -class FilePost { +class FilePost extends Post { constructor(name, hash, size) { - this.type = "file"; + super("file"); this.name = name; this.hash = hash; this.size = size; - this.ts = new Date().getTime(); } } diff --git a/src/post/MetaInfo.js b/src/post/MetaInfo.js index f68f953..997a0cc 100644 --- a/src/post/MetaInfo.js +++ b/src/post/MetaInfo.js @@ -1,10 +1,11 @@ 'use strict'; class MetaInfo { - constructor(type, size, ts) { + constructor(type, size, ts, from) { this.type = type; this.size = size; - this.ts = ts; + this.ts = ts; + this.from = from || ''; } } diff --git a/src/post/OrbitDBItem.js b/src/post/OrbitDBItem.js index 29a1c35..e13f144 100644 --- a/src/post/OrbitDBItem.js +++ b/src/post/OrbitDBItem.js @@ -1,13 +1,13 @@ 'use strict'; -class OrbitDBItem { - constructor(operation, key, value, metaInfo, by) { - this.type = "orbit-db-op"; - this.op = operation; - this.key = key; +const Post = require('./BasePost'); + +class OrbitDBItem extends Post { + constructor(operation, key, value) { + super("orbit-db-op"); + this.op = operation; + this.key = key; this.value = value; - this.meta = metaInfo; - this.by = by; } } diff --git a/src/post/Post.js b/src/post/Post.js index 5975a00..a416c7b 100644 --- a/src/post/Post.js +++ b/src/post/Post.js @@ -5,6 +5,7 @@ const await = require('asyncawait/await'); const ipfsAPI = require('orbit-common/lib/ipfs-api-promised'); const Encryption = require('orbit-common/lib/Encryption'); +const Post = require('./BasePost'); const TextPost = require('./TextPost'); const FilePost = require('./FilePost'); const DirectoryPost = require('./DirectoryPost'); @@ -27,17 +28,17 @@ class Posts { let post; if(type === PostTypes.Message) { - post = new PostTypes.Message(data); + post = new PostTypes.Message(data.content); } else if(type === PostTypes.File) { 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); + post = new PostTypes.OrbitDBItem(data.operation, data.key, data.value); } 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(), data.from); if(post.type) delete post.type; const res = await (ipfsAPI.putObject(ipfs, JSON.stringify(post))); resolve({ Post: post, Hash: res.Hash }); diff --git a/src/post/TextPost.js b/src/post/TextPost.js index a8ff2d9..734bc36 100644 --- a/src/post/TextPost.js +++ b/src/post/TextPost.js @@ -1,13 +1,13 @@ 'use strict'; +const Post = require('./BasePost'); const Encryption = require('orbit-common/lib/Encryption'); // Simplest type of post: a string -class TextPost { +class TextPost extends Post { constructor(content) { - this.type = "text"; + super("text"); this.content = content; - this.ts = new Date().getTime(); } // TODO: make sure this works