-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhive-watcher.js
268 lines (236 loc) · 7.79 KB
/
hive-watcher.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/**
* Example way to use the dhive library to watch for podpings on the Hive blockchain.
*/
MEDIUM_PODCAST = "podcast"
MEDIUM_AUDIOBOOK = "audiobook"
MEDIUM_BLOG = "blog"
MEDIUM_FILM = "film"
MEDIUM_MUSIC = "music"
MEDIUM_NEWSLETTER = "newsletter"
MEDIUM_VIDEO = "video"
PodpingMedium = [
MEDIUM_PODCAST,
MEDIUM_AUDIOBOOK,
MEDIUM_BLOG,
MEDIUM_FILM,
MEDIUM_MUSIC,
MEDIUM_NEWSLETTER,
MEDIUM_VIDEO,
]
REASON_LIVE = "live"
REASON_LIVE_END = "liveEnd"
REASON_UPDATE = "update"
PodpingReason = [
REASON_LIVE,
REASON_LIVE_END,
REASON_UPDATE,
]
let lastBlockNumber = undefined
/**
* Shuffles items in an array. Shuffles in place
*
* from https://stackoverflow.com/a/12646864
*
* @param array list of items to shuffle
*/
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
const addressList = [
"https://api.openhive.network",
"https://api.hive.blog",
"https://anyx.io",
"https://api.deathwing.me",
]
// shuffle list to try and spread the load across different endpoints since the first item is always used.
shuffleArray(addressList)
console.log(`Using API address "${addressList[0]}"`)
const client = new dhive.Client(
addressList, {
// reducing timeout and threshold makes fall over to other API address happen faster
timeout: 6000, // in ms
failoverThreshold: 1,
}
);
let validAccounts = ['podping']
/**
* Handle new block received
*
* @param block received block
*/
function handleBlock(block) {
try {
let timestamp = block.timestamp
for (let transaction of block.transactions) {
handleTransaction(transaction, timestamp)
}
} catch (error) {
console.error("error handling block")
console.error(error)
}
}
/**
* Handle transaction included in block
*
* @param transaction received transaction
* @param timestamp timestamp for block transaction is in
*/
function handleTransaction(transaction, timestamp) {
let blockNumber = transaction.block_num
let transactionId = transaction.transaction_id
lastBlockNumber = blockNumber
const block_num_span = document.getElementById("block_num");
block_num_span.innerText = blockNumber.toLocaleString()
for (let operation of transaction.operations) {
handleOperation(operation, timestamp, blockNumber, transactionId)
}
}
/**
* Handle operation included in transaction
*
* @param operation received operation
* @param timestamp timestamp for block operation is in
* @param blockNumber block number operation is in
* @param transactionId transaction operation is in
*/
function handleOperation(operation, timestamp, blockNumber, transactionId) {
let operationType = operation[0]
if (operationType === "custom_json") {
let post = operation[1]
handleCustomJsonPost(post, timestamp, blockNumber, transactionId)
}
}
/**
* Handle custom JSON post and check if desired type
*
* @param post received post in operation
* @param timestamp timestamp for block post is in
* @param blockNumber block number post is in
* @param transactionId transaction identifier post is in
*/
function handleCustomJsonPost(post, timestamp, blockNumber, transactionId) {
if (post.id === "podping" || post.id.startsWith("pp_")) {
handlePodpingPost(post, timestamp, blockNumber, transactionId)
}
// To include test posts
// else if (post.id === "podpingtest" || post.id.startsWith("pplt_")) {
// handlePodpingPost(post, timestamp, blockNumber, transactionId)
// }
}
/**
* Handle PodPing post
*
* @param post received post in operation
* @param timestamp timestamp for block post is in
* @param blockNumber block number post is in
* @param transactionId transaction identifier post is in
*/
function handlePodpingPost(post, timestamp, blockNumber, transactionId) {
if (!this.isAccountAllowed(post.required_posting_auths))
return
let postJson = JSON.parse(post.json)
let version = postJson.version || postJson.v
let updateReason = postJson.reason || postJson.r || postJson.type
let medium = postJson.medium
let versionValue = parseFloat(version)
if (isNaN(versionValue)) {
// fallback to any possible
// old posts didn't include an update type so still accept them
if (updateReason !== undefined && updateReason !== "feed_update" && updateReason !== 1)
return
} else {
// handle version 1.0 and newer
if (!(PodpingReason.includes(updateReason) && PodpingMedium.includes(medium))) {
return
}
}
let iris = postJson.iris || []
let urls = postJson.urls || []
if (urls) {
iris = iris.concat(urls)
}
if (postJson.url) {
iris = [postJson.url]
}
const list = document.getElementById("posts");
// add ping
const transactionMessage = `Feed updated(s) - ${timestamp} - ${blockNumber} - ${transactionId} - ${updateReason} - ${medium}`;
console.log(transactionMessage)
const item = document.createElement("li");
item.appendChild(document.createTextNode(transactionMessage))
list.appendChild(item)
const subList = document.createElement("ul")
item.appendChild(subList)
for (let iri of iris) {
console.log(` - ${iri}`)
const subItem = document.createElement("li");
const linkItem = document.createElement("a")
linkItem.href = iri
linkItem.target = "_blank"
linkItem.appendChild(document.createTextNode(iri))
subItem.appendChild(linkItem)
subList.appendChild(subItem)
}
}
/**
* Checks if account making PodPing is allowed
*
* @param required_posting_auths account used to make PodPing post
* @returns true if account is valid
*/
function isAccountAllowed(required_posting_auths) {
// check if valid user
let postingAuths = new Set(required_posting_auths)
let accounts = new Set(validAccounts)
let intersect = new Set()
for (let x of accounts) {
if (postingAuths.has(x))
intersect.add(x)
}
// if accounts don't overlap, skip post
return intersect.size !== 0
}
client.database.call('get_following', [validAccounts[0], null, 'blog', 100])
.then(
/**
* Get all accounts that are accepted as a valid PodPing poster
*
* @param followers list of follower objects
*/
function (followers) {
for (let follower of followers) {
validAccounts = validAccounts.concat(follower.following)
}
}
)
.then(
startStream
)
;
function startStream(blockNumber = undefined) {
// can pass the block number to start searching from. By default, uses the current block
// note: using mode BlockchainMode.Latest does not seem to return data using `getOperationsStream` so
// `getBlockStream` is used instead and transactions are parsed by block
client.blockchain.getBlockStream({
from: blockNumber,
mode: dhive.BlockchainMode.Latest
})
.on('data', handleBlock)
.on('error',
function (error) {
console.error('Error occurred parsing stream')
console.error(error)
// Note: when an error occurs, the `end` event is emitted (see below)
}
)
.on('end',
function () {
console.log('Reached end of stream')
// Note: this restart the stream
startStream(lastBlockNumber);
}
);
}