mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-10-07 22:57:07 +00:00
Remove post/ prototypes
This commit is contained in:
@@ -1,10 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
// Base class
|
||||
class Post {
|
||||
constructor(type) {
|
||||
this.type = type;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Post;
|
||||
@@ -1,15 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
const Post = require('./BasePost');
|
||||
|
||||
// A reference to a file
|
||||
class DirectoryPost extends Post {
|
||||
constructor(name, hash, size) {
|
||||
super("directory");
|
||||
this.name = name;
|
||||
this.hash = hash;
|
||||
this.size = size;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = DirectoryPost;
|
||||
@@ -1,15 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
const Post = require('./BasePost');
|
||||
|
||||
// A reference to a file
|
||||
class FilePost extends Post {
|
||||
constructor(name, hash, size) {
|
||||
super("file");
|
||||
this.name = name;
|
||||
this.hash = hash;
|
||||
this.size = size;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = FilePost;
|
||||
@@ -1,12 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
class MetaInfo {
|
||||
constructor(type, size, ts, from) {
|
||||
this.type = type;
|
||||
this.size = size;
|
||||
this.ts = ts;
|
||||
this.from = from || '';
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MetaInfo;
|
||||
@@ -1,67 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
class HashCacheItem {
|
||||
constructor(operation, key, sequenceNumber, targetHash, metaInfo, next) {
|
||||
this.op = operation;
|
||||
this.seq = sequenceNumber;
|
||||
this.key = key;
|
||||
this.target = targetHash;
|
||||
this.meta = metaInfo;
|
||||
this.next = next;
|
||||
}
|
||||
}
|
||||
|
||||
class EncryptedHashCacheItem extends HashCacheItem {
|
||||
constructor(operation, key, sequenceNumber, targetHash, metaInfo, next, publicKey, privateKey, salt) {
|
||||
if(key)
|
||||
key = Encryption.encrypt(key, privateKey, publicKey);
|
||||
|
||||
super(operation, key, sequenceNumber, targetHash, metaInfo, next);
|
||||
|
||||
try {
|
||||
this.pubkey = publicKey;
|
||||
this.target = Encryption.encrypt(targetHash, privateKey, publicKey);
|
||||
this.meta = Encryption.encrypt(JSON.stringify(metaInfo), privateKey, publicKey);
|
||||
this.sig = Encryption.sign(this.target, privateKey, this.seq, salt || "");
|
||||
} catch(e) {
|
||||
console.log("Failed to create HashCacheItem:", e);
|
||||
}
|
||||
}
|
||||
|
||||
static fromEncrypted(encryptedItem, publicKey, privateKey, salt) {
|
||||
let data = JSON.parse(encryptedItem.Data);
|
||||
|
||||
// verify signature
|
||||
const verified = Encryption.verify(data.target, data.pubkey, data.sig, data.seq, salt);
|
||||
if(!verified) throw "Invalid signature"
|
||||
|
||||
// link to the next item
|
||||
const next = encryptedItem.Links[0] ? encryptedItem.Links[0].Hash : null;
|
||||
|
||||
// decrypt data structure
|
||||
const targetDec = Encryption.decrypt(data.target, privateKey, 'TODO: pubkey');
|
||||
const metaDec = Encryption.decrypt(data.meta, privateKey, 'TODO: pubkey');
|
||||
data.target = targetDec;
|
||||
data.meta = JSON.parse(metaDec);
|
||||
|
||||
if(data.key)
|
||||
data.key = Encryption.decrypt(data.key, privateKey, 'TODO: pubkey');
|
||||
|
||||
const item = new HashCacheItem(data.op, data.key, data.seq, data.target, data.meta, next, publicKey, privateKey, salt);
|
||||
return item;
|
||||
}
|
||||
}
|
||||
*/
|
||||
module.exports = OrbitDBItem;
|
||||
@@ -1,14 +0,0 @@
|
||||
'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;
|
||||
@@ -1,54 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
const Buffer = require('buffer').Buffer;
|
||||
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,
|
||||
Snippet: "snippet",
|
||||
File: FilePost,
|
||||
Directory: DirectoryPost,
|
||||
Link: "link",
|
||||
OrbitDBItem: OrbitDBItem,
|
||||
Poll: Poll
|
||||
};
|
||||
|
||||
// Factory
|
||||
class Posts {
|
||||
static create(ipfs, type, data) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let post;
|
||||
|
||||
if(type === PostTypes.Message) {
|
||||
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);
|
||||
} 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;
|
||||
ipfs.object.put(new Buffer(JSON.stringify({ Data: JSON.stringify(post) })), "json")
|
||||
.then((res) => resolve({ Post: post, Hash: res.Hash }))
|
||||
.catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
static get Types() {
|
||||
return PostTypes;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Posts;
|
||||
@@ -1,18 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
const Post = require('./BasePost');
|
||||
// const Encryption = require('orbit-common/lib/Encryption');
|
||||
|
||||
// Simplest type of post: a string
|
||||
class TextPost extends Post {
|
||||
constructor(content) {
|
||||
super("text");
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
// encrypt(privkey, pubkey) {
|
||||
// this.content = Encryption.encrypt(this.content, privkey, pubkey);
|
||||
// }
|
||||
}
|
||||
|
||||
module.exports = TextPost;
|
||||
Reference in New Issue
Block a user