Add new POST: Poll

This commit is contained in:
haad 2016-04-05 21:02:18 -04:00
parent 64a87127a0
commit c317b04d08
2 changed files with 22 additions and 6 deletions

14
src/post/Poll.js Normal file
View File

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

View File

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