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) {
// 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));
}

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';
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();
}
}

View File

@ -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();
}
}

View File

@ -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 || '';
}
}

View File

@ -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;
}
}

View File

@ -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 });

View File

@ -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