From c317b04d08d03184f91877f3819e596d1c08635c Mon Sep 17 00:00:00 2001 From: haad Date: Tue, 5 Apr 2016 21:02:18 -0400 Subject: [PATCH] Add new POST: Poll --- src/post/Poll.js | 14 ++++++++++++++ src/post/Post.js | 14 ++++++++------ 2 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 src/post/Poll.js diff --git a/src/post/Poll.js b/src/post/Poll.js new file mode 100644 index 0000000..1485e94 --- /dev/null +++ b/src/post/Poll.js @@ -0,0 +1,14 @@ +'use strict'; + +const Post = require('./BasePost'); + +// A poll / vote +class Poll extends Post { + constructor(question, options) { + super("poll"); + this.question = question; + this.options = options; + } +} + +module.exports = Poll; diff --git a/src/post/Post.js b/src/post/Post.js index 9030ed6..c7c83aa 100644 --- a/src/post/Post.js +++ b/src/post/Post.js @@ -1,14 +1,12 @@ 'use strict'; -const async = require('asyncawait/async'); -const await = require('asyncawait/await'); - const Post = require('./BasePost'); const TextPost = require('./TextPost'); const FilePost = require('./FilePost'); const DirectoryPost = require('./DirectoryPost'); const OrbitDBItem = require('./OrbitDBItem'); const MetaInfo = require('./MetaInfo'); +const Poll = require('./Poll'); const PostTypes = { Message: TextPost, @@ -16,7 +14,8 @@ const PostTypes = { File: FilePost, Directory: DirectoryPost, Link: "link", - OrbitDBItem: OrbitDBItem + OrbitDBItem: OrbitDBItem, + Poll: Poll }; // Factory @@ -33,13 +32,16 @@ class Posts { 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); + } else if(type == PostTypes.Poll) { + post = new PostTypes.Poll(data.question, data.options); } const size = data.size ? data.size : Buffer.byteLength(data, 'utf8'); post.meta = new MetaInfo(post.type, size, new Date().getTime(), data.from); if(post.type) delete post.type; - const res = await(ipfs.object.put(new Buffer(JSON.stringify({ Data: JSON.stringify(post) })), "json")); - resolve({ Post: post, Hash: res.Hash }); + ipfs.object.put(new Buffer(JSON.stringify({ Data: JSON.stringify(post) })), "json") + .then((res) => resolve({ Post: post, Hash: res.Hash })) + .catch(reject); }); }