-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
151 lines (143 loc) · 4.46 KB
/
api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
var Hashes = require('jshashes')
var ee = require('event-emitter')
var API = {
Emitter: ee({}),
Listener: null,
NewHash: function(article, signature, time) {
if (signature) article.signature = signature
if (time) article.time = time
article._id = new Hashes.MD5().hex(JSON.stringify(article))
return article
},
// Hash: function(title, content) {
// var hash = new Hashes.MD5().hex(title+JSON.stringify(content))
// return {
// _id: hash,
// title: title,
// content: content
// }
// },
// HashTime: function(title, content, time) {
// var hash = new Hashes.MD5().hex(title+JSON.stringify(content)+JSON.stringify(time))
// return {
// _id: hash,
// title: title,
// content: content,
// time: time
// }
// },
Get: function(title, cb) {
Waka.db.Articles.findOne({'info.title': title},{},function(match) {
if (!match) { cb('o Not found', null); return;}
cb(null, match)
})
},
Set: function(article, options, cb) {
Waka.api.Get(article.info.title, function(e, match) {
// ensuring uniqueness of title
if (match) {
// maybe add to variants in memory
Waka.db.Articles.remove(match._id)
}
// options to sign a content with a previously generated keyPair
var signature = null
if (options.signKeyPair) {
signature = {
pubKey: options.signKeyPair.publicKeyBase64,
base64: Waka.crypto.GetSignature(article, options.signKeyPair.secretKey)
}
}
// option for secure timestamp to verify the date at which a content existed
if (options.timestampAuthority) {
// first stamp the hash on the timestamp authority
Waka.api.Stamp(article, signature, options.timestampAuthority, function(timestamp) {
var article = Waka.api.NewHash(article, signature, timestamp)
Waka.api.Save(article, function(e,r) {
if (r) cb(null, {match: match, article: article})
})
})
} else {
var validArticle = Waka.api.NewHash(article, signature)
Waka.api.Save(validArticle, function(e,r) {
if (r) cb(null, {match: match, article: article})
})
}
})
},
Save: function(article, cb) {
if (!article || !article._id || !article.info || !article.info.title || !article.content) {
cb('invalid article')
return
}
Waka.db.Articles.upsert(article, function(article) {
// broadcasting our new hash for this article
if (Waka.c) Waka.c.broadcast({
c: 'indexchange',
data: {_id: article._id, info: article.info}
})
cb(null, true);
})
},
Stamp: function(article, signature, timestampAuthority, cb) {
var hash = Waka.api.NewHash(article, signature)._id;
var url = "http://steemwhales.com:6060/time/request";
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var result = JSON.parse(this.responseText)
result.method = 'STEEM'
cb(result)
}
};
xhttp.open("POST", url, true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("hash="+hash);
},
Search: function(title) {
console.log('Searching',title)
// deletes all previous searches for this user? why ??
Waka.mem.Search.find({origin: Waka.c.id},{}).fetch(function(s) {
for (var i = 0; i < s.length; i++) {
Waka.mem.Search.remove(s[i]._id)
}
})
var search = {
info: {title: title},
origin: Waka.c.id,
echo: 2
}
Waka.mem.Search.upsert(search)
Waka.c.broadcast({
c:'search',
data: search
})
},
Index: function(cb) {
Waka.db.Articles.find({},{fields: {_id:1, info: 1}}).fetch(function(res){
cb(null, res)
})
},
DeleteFieldsWithDots: function(object) {
for (var key in object) {
if (key.indexOf('.') > -1) {
delete object[key]
continue
}
if (typeof object[key] === 'object') {
object[key] = this.DeleteFieldsWithDots(object[key])
}
}
return object
}
};
// Events
// API.Emitter.on('connected', API.Listener = function(){
// console.log('connected');
// })
// API.Emitter.on('peerchange', API.Listener = function(){
// console.log('peerchange');
// })
// API.Emitter.on('newshare', API.Listener = function(args){
// console.log('newshare',args);
// })
var exports = module.exports = API;