Refactor POSTs

This commit is contained in:
haad 2016-03-12 10:55:49 +01:00
parent 9447112378
commit a4dc49b83b
8 changed files with 38 additions and 27 deletions

View File

@ -51,16 +51,13 @@ 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) result = this._read(operations.reverse(), opts.key, 1, true).map((f) => f.value.content);
.map((f) => f.value["content"]);
} 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)
// .map((f) => { return { key: f.key, value: f.value["content"] } });
} else { } else {
// Lower than and lastN case, search latest first by reversing the sequence // 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() 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(); if(opts.reverse) result.reverse();
@ -76,14 +73,14 @@ class OrbitDB {
post = data; post = data;
} else { } else {
// Handle everything else as a string // 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)); return await(this._write(channel, password, DBOperation.Types.Add, post.Hash, post.Hash));
} }
// Sets a key-value pair // Sets a key-value pair
put(channel, password, key, data) { 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)); return await(this._write(channel, password, DBOperation.Types.Put, key, post.Hash));
} }

10
src/post/BasePost.js Normal file
View File

@ -0,0 +1,10 @@
'use strict';
// Base class
class Post {
constructor(type) {
this.type = type;
}
}
module.exports = Post;

View File

@ -1,13 +1,14 @@
'use strict'; 'use strict';
const Post = require('./BasePost');
// A reference to a file // A reference to a file
class DirectoryPost { class DirectoryPost extends Post {
constructor(name, hash, size) { constructor(name, hash, size) {
this.type = "directory"; super("directory");
this.name = name; this.name = name;
this.hash = hash; this.hash = hash;
this.size = size; this.size = size;
this.ts = new Date().getTime();
} }
} }

View File

@ -1,13 +1,14 @@
'use strict'; 'use strict';
const Post = require('./BasePost');
// A reference to a file // A reference to a file
class FilePost { class FilePost extends Post {
constructor(name, hash, size) { constructor(name, hash, size) {
this.type = "file"; super("file");
this.name = name; this.name = name;
this.hash = hash; this.hash = hash;
this.size = size; this.size = size;
this.ts = new Date().getTime();
} }
} }

View File

@ -1,10 +1,11 @@
'use strict'; 'use strict';
class MetaInfo { class MetaInfo {
constructor(type, size, ts) { constructor(type, size, ts, from) {
this.type = type; this.type = type;
this.size = size; this.size = size;
this.ts = ts; this.ts = ts;
this.from = from || '';
} }
} }

View File

@ -1,13 +1,13 @@
'use strict'; 'use strict';
class OrbitDBItem { const Post = require('./BasePost');
constructor(operation, key, value, metaInfo, by) {
this.type = "orbit-db-op"; class OrbitDBItem extends Post {
constructor(operation, key, value) {
super("orbit-db-op");
this.op = operation; this.op = operation;
this.key = key; this.key = key;
this.value = value; this.value = value;
this.meta = metaInfo;
this.by = by;
} }
} }

View File

@ -5,6 +5,7 @@ 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 Post = require('./BasePost');
const TextPost = require('./TextPost'); const TextPost = require('./TextPost');
const FilePost = require('./FilePost'); const FilePost = require('./FilePost');
const DirectoryPost = require('./DirectoryPost'); const DirectoryPost = require('./DirectoryPost');
@ -27,17 +28,17 @@ class Posts {
let post; let post;
if(type === PostTypes.Message) { if(type === PostTypes.Message) {
post = new PostTypes.Message(data); post = new PostTypes.Message(data.content);
} else if(type === PostTypes.File) { } else if(type === PostTypes.File) {
post = new PostTypes.File(data.name, data.hash, data.size); post = new PostTypes.File(data.name, data.hash, data.size);
} else if(type == PostTypes.Directory) { } else if(type == PostTypes.Directory) {
post = new PostTypes.Directory(data.name, data.hash, data.size); 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);
} }
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(), data.from);
if(post.type) delete post.type; 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({ Post: post, Hash: res.Hash }); resolve({ Post: post, Hash: res.Hash });

View File

@ -1,13 +1,13 @@
'use strict'; 'use strict';
const Post = require('./BasePost');
const Encryption = require('orbit-common/lib/Encryption'); const Encryption = require('orbit-common/lib/Encryption');
// Simplest type of post: a string // Simplest type of post: a string
class TextPost { class TextPost extends Post {
constructor(content) { constructor(content) {
this.type = "text"; super("text");
this.content = content; this.content = content;
this.ts = new Date().getTime();
} }
// TODO: make sure this works // TODO: make sure this works