Browse Source

bot config: add update delay for finalized proposals

Joystream Stats 4 years ago
parent
commit
437a68a88f

+ 4 - 2
community-contributions/joystreamtelegrambot/config.ts

@@ -5,11 +5,13 @@ export const domain = "https://testnet.joystream.org";
 export const wsLocation = "wss://rome-rpc-endpoint.joystream.org:9944/";
 
 // telegram bot token
-export const token: string = "";
+export const token = "";
 
 // telegram chat ID
-export const chatid: string = "";
+export const chatid = "";
 
 // time between heartbeat announcement in milliseconds
 export const heartbeat = 60000 * 60 * 24;
 
+// minutes between checking for proposal updates
+export const proposalDelay = 15

+ 7 - 5
community-contributions/joystreamtelegrambot/src/bot.ts

@@ -1,5 +1,5 @@
 import TelegramBot from "node-telegram-bot-api";
-import { token, chatid, heartbeat, wsLocation } from "../config";
+import { token, chatid, heartbeat, proposalDelay, wsLocation } from "../config";
 
 // types
 import { Block, Council, Options, Proposals } from "./types";
@@ -66,6 +66,7 @@ const main = async () => {
   const posts: number[] = [0, 0];
   const threads: number[] = [0, 0];
   let proposals: Proposals = { last: 0, current: 0, active: [], executing: [] };
+  let lastProposalUpdate = 0;
 
   if (opts.channel) channels[0] = await get.currentChannelId(api);
 
@@ -149,11 +150,12 @@ const main = async () => {
         proposals.current = await get.proposalCount(api);
         if (
           proposals.current > proposals.last ||
-          proposals.active ||
-          proposals.executing
-        )
-          // TODO do not refetch each active/executing proposal on every block
+          (timestamp > lastProposalUpdate + 60000 * proposalDelay &&
+            (proposals.active || proposals.executing))
+        ) {
           proposals = await announce.proposals(api, proposals, id, sendMessage);
+          lastProposalUpdate = timestamp;
+        }
       }
 
       if (opts.forum) {

+ 4 - 4
community-contributions/joystreamtelegrambot/src/tests.ts

@@ -22,7 +22,7 @@ const main = async () => {
   const [chain, node, version] = await Promise.all([
     api.rpc.system.chain(),
     api.rpc.system.name(),
-    api.rpc.system.version()
+    api.rpc.system.version(),
   ]);
   log(`Connected to ${chain} on ${node} v${version}`);
 
@@ -32,7 +32,7 @@ const main = async () => {
     last: 1,
     current: 2,
     active: [],
-    executing: []
+    executing: [],
   };
   let categories = [0, 0];
   let posts = [0, 0];
@@ -50,12 +50,12 @@ const main = async () => {
       lastBlock = currentBlock;
 
       log("first proposal");
-      announce.proposals(api, proposals, sendMessage);
+      announce.proposals(api, proposals, lastBlock, sendMessage);
 
       log("last proposal");
       proposals.current = await get.proposalCount(api);
       proposals.last = proposals.current - 1;
-      announce.proposals(api, proposals, sendMessage);
+      announce.proposals(api, proposals, lastBlock, sendMessage);
 
       log("first category");
       announce.categories(api, categories, sendMessage);