Skip to content

Commit

Permalink
Add ignore and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
stevencrader committed Dec 5, 2022
1 parent 97a4038 commit efc8d34
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 52 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
max_line_length = 120
tab_width = 4
ij_continuation_indent_size = 8
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
# podping-hivewatcher-js

A watcher script for the hive backed podping network. (Javascript)

Uses [@hiveio/dhive](https://yarnpkg.com/package/@hiveio/dhive) to communicate with the Hive blockchain.

# Use

1. Add `hive-watcher.js` and `index.html` to a web server
1. Ex using node:
1. Run `npm install http-server -g`
2. Run `http-server`
1. Will start a server at `http://localhost:8080/` by default
2. Ex using Python:
1. Run `python -m http.server`
2. Will start a server at `http://localhost:8000` by default
2. Open `index.html` in a web browser. Podpings will be added to list as they are received.
82 changes: 42 additions & 40 deletions hive-watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ const addressList = [
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,
}
addressList, {
// reducing timeout and threshold makes fall over to other API address happen faster
timeout: 6000, // in ms
failoverThreshold: 1,
}
);

let validAccounts = ['podping']
Expand Down Expand Up @@ -155,15 +155,17 @@ function handlePodpingPost(post, timestamp, blockNumber, transactionId) {
let updateReason = postJson.reason || postJson.r || postJson.type
let medium = postJson.medium

if (version === "1.0") {
if (!(PodpingReason.includes(updateReason) && PodpingMedium.includes(medium))) {
return
}
} else {
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 || []
Expand Down Expand Up @@ -222,21 +224,21 @@ function isAccountAllowed(required_posting_auths) {
}

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
)
.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) {
Expand All @@ -247,20 +249,20 @@ function startStream(blockNumber = undefined) {
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);
}
);
.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);
}
);

}
24 changes: 12 additions & 12 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<head>
<meta charset="utf-8"/>

<title>Hive Watcher</title>
<title>Hive Watcher</title>

<script src="https://unpkg.com/@hiveio/dhive@0.14.12/dist/dhive.js"></script>
<script src="hive-watcher.js"></script>
</head>
<body>
<noscript>This site requires JavaScript to run.</noscript>
<h1>Hive Posts</h1>
<p>Parsing block #<span id="block_num">0</span></p>
<ol id="posts"></ol>
</body>
<script src="https://unpkg.com/@hiveio/dhive@1.2.4/dist/dhive.js"></script>
<script src="hive-watcher.js"></script>
</head>
<body>
<noscript>This site requires JavaScript to run.</noscript>
<h1>Hive Posts</h1>
<p>Parsing block #<span id="block_num">0</span></p>
<ol id="posts"></ol>
</body>
</html>

0 comments on commit efc8d34

Please sign in to comment.