mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-10-07 22:57:07 +00:00
First version of POST
This commit is contained in:
parent
c92fd08369
commit
ed74645d12
@ -6,8 +6,8 @@ const async = require('asyncawait/async');
|
|||||||
const await = require('asyncawait/await');
|
const await = require('asyncawait/await');
|
||||||
const OrbitList = require('./list/OrbitList');
|
const OrbitList = require('./list/OrbitList');
|
||||||
const Operation = require('./db/Operation');
|
const Operation = require('./db/Operation');
|
||||||
const OpTypes = require('./db/OpTypes');
|
const OpTypes = require('./db/OpTypes'); // TODO: move to Operation.Types
|
||||||
const Post = require('./db/Post');
|
const Post = require('./post/Post');
|
||||||
|
|
||||||
class OrbitDB {
|
class OrbitDB {
|
||||||
constructor(ipfs) {
|
constructor(ipfs) {
|
||||||
@ -69,14 +69,19 @@ class OrbitDB {
|
|||||||
|
|
||||||
// Adds an event to the log
|
// Adds an event to the log
|
||||||
add(channel, password, data) {
|
add(channel, password, data) {
|
||||||
const post = await(Post.publish(this._ipfs, data));
|
let post;
|
||||||
const key = post.Hash;
|
if(data instanceof Post) {
|
||||||
return await(this._write(channel, password, OpTypes.Add, key, post.Hash, data));
|
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
|
// Sets a key-value pair
|
||||||
put(channel, password, key, data) {
|
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));
|
return await(this._write(channel, password, OpTypes.Put, key, post.Hash));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,7 +119,7 @@ class OrbitDB {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Write an op to the db
|
// 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));
|
const hash = await(Operation.create(this._ipfs, this._logs[channel], this.user, operation, key, value));
|
||||||
this.events[channel].emit('write', channel, hash);
|
this.events[channel].emit('write', channel, hash);
|
||||||
return key;
|
return key;
|
||||||
|
@ -1,11 +0,0 @@
|
|||||||
'use strict';
|
|
||||||
|
|
||||||
const ItemTypes = {
|
|
||||||
Message: "text",
|
|
||||||
Snippet: "snippet",
|
|
||||||
File: "file",
|
|
||||||
List: "list",
|
|
||||||
Link: "link"
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = ItemTypes;
|
|
@ -3,8 +3,8 @@
|
|||||||
const async = require('asyncawait/async');
|
const async = require('asyncawait/async');
|
||||||
const await = require('asyncawait/await');
|
const await = require('asyncawait/await');
|
||||||
const ipfsAPI = require('orbit-common/lib/ipfs-api-promised');
|
const ipfsAPI = require('orbit-common/lib/ipfs-api-promised');
|
||||||
const OrbitDBItem = require('./OrbitDBItem');
|
const OrbitDBItem = require('../post/OrbitDBItem');
|
||||||
const ItemTypes = require('./ItemTypes');
|
const Post = require('../post/Post');
|
||||||
const MetaInfo = require('./MetaInfo');
|
const MetaInfo = require('./MetaInfo');
|
||||||
|
|
||||||
class Operation {
|
class Operation {
|
||||||
@ -24,10 +24,15 @@ class Operation {
|
|||||||
var createOperationAsync = async(() => {
|
var createOperationAsync = async(() => {
|
||||||
return new Promise(async((resolve, reject) => {
|
return new Promise(async((resolve, reject) => {
|
||||||
const size = -1;
|
const size = -1;
|
||||||
const meta = new MetaInfo(ItemTypes.Message, size, user.username, new Date().getTime());
|
const meta = new MetaInfo("orbitdb-op", size, user.username, new Date().getTime());
|
||||||
const item = new OrbitDBItem(operation, key, value, meta);
|
const data = {
|
||||||
const data = await (ipfsAPI.putObject(ipfs, JSON.stringify(item)));
|
operation: operation,
|
||||||
resolve(data.Hash);
|
key: key,
|
||||||
|
value: value,
|
||||||
|
meta: meta
|
||||||
|
};
|
||||||
|
const op = await(Post.create(ipfs, Post.Types.OrbitDBItem, data));
|
||||||
|
resolve(op.Hash);
|
||||||
}));
|
}));
|
||||||
})
|
})
|
||||||
return await(createOperationAsync());
|
return await(createOperationAsync());
|
||||||
|
@ -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
14
src/post/FilePost.js
Normal 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;
|
@ -1,7 +1,5 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
// const Encryption = require('orbit-common/lib/Encryption');
|
|
||||||
|
|
||||||
class OrbitDBItem {
|
class OrbitDBItem {
|
||||||
constructor(operation, key, value, metaInfo) {
|
constructor(operation, key, value, metaInfo) {
|
||||||
this.op = operation;
|
this.op = operation;
|
43
src/post/Post.js
Normal file
43
src/post/Post.js
Normal 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
18
src/post/TextPost.js
Normal 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;
|
Loading…
x
Reference in New Issue
Block a user