6 Commits 0d3866097c ... 89303564d9

Autor SHA1 Mensagem Data
  edmilson-dk 89303564d9 feat: created get user method 3 anos atrás
  edmilson-dk 3256117460 feat: created reset posts tables 3 anos atrás
  edmilson-dk c4ca3d2f25 feat: added method of reset posts table in 1 day 3 anos atrás
  edmilson-dk fd218fa037 refactro: removed command validate of view chat command 3 anos atrás
  edmilson-dk 5fafc34088 feat: added environment variables of example 3 anos atrás
  edmilson-dk 020ef9d8e9 feat: added environment variables of exemple 3 anos atrás

+ 0 - 3
.env.example

@@ -1,11 +1,8 @@
 BOT_TOKEN=
 BOT_USERNAME=
 BOT_CHAT=
-
 BOT_INTERVAL_SEND_POSTS=
 BOT_INTERVAL_GET_POSTS=
-BOT_COUNT_POSTS=
-
 DB_CLIENT=
 DATABASE=
 DB_USERNAME=

+ 0 - 5
src/commands/index.js

@@ -128,11 +128,6 @@ module.exports = bot => {
 
   async function viewChatCommand(ctx) {
     const userId = String(ctx.message.from.id);
-
-    if (!(await validateCommand(userId))) {
-      ctx.reply(command_error.message);
-      return;
-    }
     const chatTitle = removeCommand(ctx.message.text,'/view_chat');
    
     if (!chatTitle) {

+ 17 - 20
src/core/index.js

@@ -1,4 +1,5 @@
 const RssParser = require('../drivers/rss-parser');
+const { strToHex } = require('../helpers/features_helpers');
 
 const ChatRepository = require('../infra/repositories/chat_repository');
 const FeedRepository = require('../infra/repositories/feed_repository');
@@ -27,7 +28,13 @@ async function getChatsData() {
 
   async function fetchData(feed) {
     const items = await rssParser.getFeeds(feed.rss_url);
-    return { data: items, chat_id: feed.chat_id, title: feed.title, hashtags: feed.hashtag };
+    
+    return { 
+      data: items, 
+      chat_id: feed.chat_id, 
+      title: feed.title, 
+      hashtags: feed.hashtag 
+    };
   }
   
   const items = data.map(feed => {
@@ -41,9 +48,11 @@ async function getChatsData() {
   return feeds;
 }
 
-function sendMessages({ feed, data, bot }) {
+function sendFeedPosts({ feed, data, bot }) {
   data.forEach(async (item, index) => {
-    const defaultObject = { title: item.title, chat_id: feed.chat_id };
+    const defaultObject = { 
+      title: item.title.split(' ').join(''), 
+      chat_id: feed.chat_id };
     
     if (!(await postRepository.existsPost(defaultObject))) {
       await postRepository.addPost(defaultObject);
@@ -57,27 +66,15 @@ function sendMessages({ feed, data, bot }) {
   });
 }
 
-function isUTCMidNight() {
-  const utcHours = time.getUTCHours() === 0;
-  const utcMinutes = time.getUTCMinutes() <= 58;
-  const utcSeconds = time.getUTCSeconds() <= 58;
-
-  return (utcHours && utcMinutes && utcSeconds) ? true : false;
-}
-
 async function start(bot) {
   const data = await getChatsData();
-  
-  if (isUTCMidNight()) await postRepository.dropAllPosts();
+
+  await postRepository.dropAllPosts();
+
   if (data.length === 0) return data;
   
-  data.forEach(async feed => {
-    const { count } = await postRepository.getPostsCount({ chat_id: feed.chat_id});
-   
-    feed.data.splice(0, count);
-    const items = feed.data.slice(0, process.env.BOT_COUNT_POSTS);
-
-    sendMessages({ feed, data: items, bot });
+  data.forEach(feed => {
+    sendFeedPosts({ feed, data: feed.data, bot });
   });
 }
 

+ 3 - 3
src/infra/repositories/post_repository.js

@@ -21,11 +21,11 @@ class PostRepository {
     return count[0];
   }
 
-
   async dropAllPosts() {
     await knex('posts')
-      .del();
-    
+      .where(knex.raw(`created_at <= NOW() - '1 day'::INTERVAL`))
+      .del()
+  
     return;
   }
 }

+ 0 - 1
src/infra/repositories/user_repository.js

@@ -30,7 +30,6 @@ class UserRepository {
 
   async getUser(user_id) {
     const row = await knex('users')
-      .select()
       .where({ id: user_id });
 
     return row;