First version of POST

This commit is contained in:
haad 2016-03-09 17:14:31 +01:00
parent c92fd08369
commit ed74645d12
8 changed files with 98 additions and 53 deletions

View File

@ -6,8 +6,8 @@ const async = require('asyncawait/async');
const await = require('asyncawait/await');
const OrbitList = require('./list/OrbitList');
const Operation = require('./db/Operation');
const OpTypes = require('./db/OpTypes');
const Post = require('./db/Post');
const OpTypes = require('./db/OpTypes'); // TODO: move to Operation.Types
const Post = require('./post/Post');
class OrbitDB {
constructor(ipfs) {
@ -69,14 +69,19 @@ class OrbitDB {
// Adds an event to the log
add(channel, password, data) {
const post = await(Post.publish(this._ipfs, data));
const key = post.Hash;
return await(this._write(channel, password, OpTypes.Add, key, post.Hash, data));
let post;
if(data instanceof Post) {
post = data;
} else {
// Handle everything else as a string
post = await(Post.create(this._ipfs, Post.Types.Message, data));
}
return await(this._write(channel, password, OpTypes.Add, post.Hash, post.Hash));
}
// Sets a key-value pair
put(channel, password, key, data) {
const post = await(Post.publish(this._ipfs, data));
const post = await(Post.create(this._ipfs, Post.Types.Message, data));
return await(this._write(channel, password, OpTypes.Put, key, post.Hash));
}
@ -114,7 +119,7 @@ class OrbitDB {
}
// Write an op to the db
_write(channel, password, operation, key, value, data) {
_write(channel, password, operation, key, value) {
const hash = await(Operation.create(this._ipfs, this._logs[channel], this.user, operation, key, value));
this.events[channel].emit('write', channel, hash);
return key;

View File

@ -1,11 +0,0 @@
'use strict';
const ItemTypes = {
Message: "text",
Snippet: "snippet",
File: "file",
List: "list",
Link: "link"
};
module.exports = ItemTypes;

View File

@ -3,8 +3,8 @@
const async = require('asyncawait/async');
const await = require('asyncawait/await');
const ipfsAPI = require('orbit-common/lib/ipfs-api-promised');
const OrbitDBItem = require('./OrbitDBItem');
const ItemTypes = require('./ItemTypes');
const OrbitDBItem = require('../post/OrbitDBItem');
const Post = require('../post/Post');
const MetaInfo = require('./MetaInfo');
class Operation {
@ -24,10 +24,15 @@ class Operation {
var createOperationAsync = async(() => {
return new Promise(async((resolve, reject) => {
const size = -1;
const meta = new MetaInfo(ItemTypes.Message, size, user.username, new Date().getTime());
const item = new OrbitDBItem(operation, key, value, meta);
const data = await (ipfsAPI.putObject(ipfs, JSON.stringify(item)));
resolve(data.Hash);
const meta = new MetaInfo("orbitdb-op", size, user.username, new Date().getTime());
const data = {
operation: operation,
key: key,
value: value,
meta: meta
};
const op = await(Post.create(ipfs, Post.Types.OrbitDBItem, data));
resolve(op.Hash);
}));
})
return await(createOperationAsync());

View File

@ -1,27 +0,0 @@
'use strict';
const async = require('asyncawait/async');
const await = require('asyncawait/await');
const ipfsAPI = require('orbit-common/lib/ipfs-api-promised');
const Encryption = require('orbit-common/lib/Encryption');
class Post {
constructor(content) {
this.content = content;
this.ts = new Date().getTime();
}
encrypt(privkey, pubkey) {
this.content = Encryption.encrypt(this.content, privkey, pubkey);
}
static publish(ipfs, data) {
return new Promise((resolve, reject) => {
let post = new Post(data);
const res = await (ipfsAPI.putObject(ipfs, JSON.stringify(post)));
resolve(res);
})
}
}
module.exports = Post;

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

@ -0,0 +1,14 @@
'use strict';
const TextPost = require('./TextPost');
// A reference to a file
class FilePost extends TextPost {
constructor(content, file, size) {
super(content);
this.file = file;
this.size = size;
}
}
module.exports = FilePost;

View File

@ -1,7 +1,5 @@
'use strict';
// const Encryption = require('orbit-common/lib/Encryption');
class OrbitDBItem {
constructor(operation, key, value, metaInfo) {
this.op = operation;

43
src/post/Post.js Normal file
View File

@ -0,0 +1,43 @@
'use strict';
const async = require('asyncawait/async');
const await = require('asyncawait/await');
const ipfsAPI = require('orbit-common/lib/ipfs-api-promised');
const Encryption = require('orbit-common/lib/Encryption');
const TextPost = require('./TextPost');
const FilePost = require('./FilePost');
const OrbitDBItem = require('./OrbitDBItem');
const PostTypes = {
Message: TextPost,
Snippet: "snippet",
File: FilePost,
List: "list",
Link: "link",
OrbitDBItem: OrbitDBItem
};
// Factory
class Posts {
static create(ipfs, type, data) {
return new Promise((resolve, reject) => {
let post;
if(type === PostTypes.Message) {
post = new PostTypes.Message(data);
} else if(type === PostTypes.File) {
post = new PostTypes.File(data.content, data.file, data.size);
} else if(type == PostTypes.OrbitDBItem) {
post = new PostTypes.OrbitDBItem(data.operation, data.key, data.value, data.meta);
}
const res = await (ipfsAPI.putObject(ipfs, JSON.stringify(post)));
resolve(res);
})
}
static get Types() {
return PostTypes;
}
}
module.exports = Posts;

18
src/post/TextPost.js Normal file
View File

@ -0,0 +1,18 @@
'use strict';
const Encryption = require('orbit-common/lib/Encryption');
// Simplest type of post: a string
class TextPost {
constructor(content) {
this.content = content;
this.ts = new Date().getTime();
}
// TODO: make sure this works
encrypt(privkey, pubkey) {
this.content = Encryption.encrypt(this.content, privkey, pubkey);
}
}
module.exports = TextPost;