Browse Source

bot: do not announce new threads (#38)

Joystream Stats 4 years ago
parent
commit
89f7abb0fd

+ 4 - 19
community-contributions/joystreamtelegrambot/src/bot.ts

@@ -37,7 +37,7 @@ const main = async () => {
   await api.isReady;
 
   const [chain, node, version] = await Promise.all([
-    api.rpc.system.chain(),
+    String(await api.rpc.system.chain()),
     api.rpc.system.name(),
     api.rpc.system.version(),
   ]);
@@ -160,27 +160,12 @@ const main = async () => {
 
       if (opts.forum) {
         cats[1] = await get.currentCategoryId(api);
+        cats[0] = await announce.categories(api, cats, sendMessage);
         posts[1] = await get.currentPostId(api);
-        threads[1] = await get.currentThreadId(api);
-
-        if (cats[1] > cats[0])
-          cats[0] = await announce.categories(api, cats, sendMessage);
-
-        if (posts[1] > posts[0])
-          posts[0] = await announce.posts(api, posts, sendMessage);
-
-        if (threads[1] > threads[0])
-          threads[0] = await announce.threads(api, threads, sendMessage);
+        posts[0] = await announce.posts(api, posts, sendMessage);
       }
 
-      printStatus(opts, {
-        block: id,
-        cats,
-        chain: String(chain),
-        posts,
-        proposals,
-        threads,
-      });
+      printStatus(opts, { block: id, cats, chain, posts, proposals });
     }
   );
 };

+ 2 - 26
community-contributions/joystreamtelegrambot/src/lib/announcements.ts

@@ -114,6 +114,7 @@ export const categories = async (
   category: number[],
   sendMessage: (msg: string) => void
 ): Promise<number> => {
+  if (category[0] === category[1]) return category[0];
   const messages: string[] = [];
 
   for (let id: number = +category[0] + 1; id <= category[1]; id++) {
@@ -133,6 +134,7 @@ export const posts = async (
   sendMessage: (msg: string) => void
 ): Promise<number> => {
   const [last, current] = posts;
+  if (current === last) return last;
   const messages: string[] = [];
 
   for (let id: number = +last + 1; id <= current; id++) {
@@ -159,32 +161,6 @@ export const posts = async (
   return current;
 };
 
-// announce latest threads
-export const threads = async (
-  api: Api,
-  threads: number[],
-  sendMessage: (msg: string) => void
-): Promise<number> => {
-  const [last, current] = threads;
-  const messages: string[] = [];
-
-  for (let id: number = +last + 1; id <= current; id++) {
-    const thread: Thread = await query("title", () =>
-      api.query.forum.threadById(id)
-    );
-    const { title, author_id } = thread;
-    const handle: string = await memberHandleByAccount(api, author_id.toJSON());
-    const category: Category = await query("title", () =>
-      categoryById(api, thread.category_id.toNumber())
-    );
-    const msg = `Thread ${id}: <a href="${domain}/#/forum/threads/${id}">"${title}"</a> by <a href="${domain}/#/members/${handle}">${handle}</a> in category "<a href="${domain}/#/forum/categories/${category.id}">${category.title}</a>" `;
-    messages.push(msg);
-  }
-
-  sendMessage(messages.join("\r\n\r\n"));
-  return current;
-};
-
 // announce latest proposals
 export const proposals = async (
   api: Api,

+ 4 - 6
community-contributions/joystreamtelegrambot/src/lib/util.ts

@@ -3,7 +3,7 @@ import moment from "moment";
 
 export const parseArgs = (args: string[]): Options => {
   const inArgs = (term: string): boolean => {
-    return args.find(a => a.search(term) > -1) ? true : false;
+    return args.find((a) => a.search(term) > -1) ? true : false;
   };
 
   const options: Options = {
@@ -11,7 +11,7 @@ export const parseArgs = (args: string[]): Options => {
     channel: inArgs("--channel"),
     council: inArgs("--council"),
     forum: inArgs("--forum"),
-    proposals: inArgs("--proposals")
+    proposals: inArgs("--proposals"),
   };
 
   if (options.verbose > 1) console.debug("args", args, "\noptions", options);
@@ -26,17 +26,15 @@ export const printStatus = (
     chain: string;
     posts: number[];
     proposals: Proposals;
-    threads: number[];
   }
 ): void => {
   if (opts.verbose < 1) return;
 
-  const { block, chain, proposals, cats, posts, threads } = data;
+  const { block, chain, proposals, cats, posts } = data;
   const date = formatTime();
   let message = `[${date}] Chain:${chain} Block:${block} `;
 
-  if (opts.forum)
-    message += `Post:${posts[1]} Cat:${cats[1]} Thread:${threads[1]} `;
+  if (opts.forum) message += `Post:${posts[1]} Cat:${cats[1]} `;
 
   if (opts.proposals)
     message += `Proposals:${proposals.current} (Active:${proposals.active.length} Pending:${proposals.executing.length}) `;

+ 0 - 9
community-contributions/joystreamtelegrambot/src/tests.ts

@@ -36,7 +36,6 @@ const main = async () => {
   };
   let categories = [0, 0];
   let posts = [0, 0];
-  let threads = [0, 0];
   let channels = [0, 0];
 
   const unsubscribe = await api.rpc.chain.subscribeNewHeads(
@@ -73,14 +72,6 @@ const main = async () => {
       posts[0] = posts[1] - 1;
       announce.posts(api, posts, sendMessage);
 
-      log("first thread");
-      announce.threads(api, threads, sendMessage);
-
-      log("last thread");
-      threads[1] = await get.currentThreadId(api);
-      threads[0] = threads[1] - 1;
-      announce.threads(api, threads, sendMessage);
-
       log("first channel");
       announce.channels(api, channels, sendMessage);