about summary refs log tree commit diff
diff options
context:
space:
mode:
authorabcang <abcang1015@gmail.com>2018-04-17 20:49:09 +0900
committerEugen Rochko <eugen@zeonfederated.com>2018-04-17 13:49:09 +0200
commit609bf9302976768c56116f9d920f34a430d538b5 (patch)
treeeeb502c55b21a0164c79598676bda8d7f712997f
parent3c722fe687de0c6e6ba144a8c87c34465fca3af9 (diff)
Perform processing that does not use the database before connecting to the database (#7168)
-rw-r--r--streaming/index.js81
1 files changed, 41 insertions, 40 deletions
diff --git a/streaming/index.js b/streaming/index.js
index a42a91242..1b4f8596c 100644
--- a/streaming/index.js
+++ b/streaming/index.js
@@ -329,52 +329,53 @@ const startWorker = (workerId) => {
 
       // Only messages that may require filtering are statuses, since notifications
       // are already personalized and deletes do not matter
-      if (needsFiltering && event === 'update') {
-        pgPool.connect((err, client, done) => {
-          if (err) {
-            log.error(err);
-            return;
-          }
+      if (!needsFiltering || event !== 'update') {
+        transmit();
+        return;
+      }
 
-          const unpackedPayload  = payload;
-          const targetAccountIds = [unpackedPayload.account.id].concat(unpackedPayload.mentions.map(item => item.id));
-          const accountDomain    = unpackedPayload.account.acct.split('@')[1];
+      const unpackedPayload  = payload;
+      const targetAccountIds = [unpackedPayload.account.id].concat(unpackedPayload.mentions.map(item => item.id));
+      const accountDomain    = unpackedPayload.account.acct.split('@')[1];
 
-          if (Array.isArray(req.filteredLanguages) && req.filteredLanguages.indexOf(unpackedPayload.language) !== -1) {
-            log.silly(req.requestId, `Message ${unpackedPayload.id} filtered by language (${unpackedPayload.language})`);
-            done();
+      if (Array.isArray(req.filteredLanguages) && req.filteredLanguages.indexOf(unpackedPayload.language) !== -1) {
+        log.silly(req.requestId, `Message ${unpackedPayload.id} filtered by language (${unpackedPayload.language})`);
+        return;
+      }
+
+      // When the account is not logged in, it is not necessary to confirm the block or mute
+      if (!req.accountId) {
+        transmit();
+        return;
+      }
+
+      pgPool.connect((err, client, done) => {
+        if (err) {
+          log.error(err);
+          return;
+        }
+
+        const queries = [
+          client.query(`SELECT 1 FROM blocks WHERE (account_id = $1 AND target_account_id IN (${placeholders(targetAccountIds, 2)})) OR (account_id = $2 AND target_account_id = $1) UNION SELECT 1 FROM mutes WHERE account_id = $1 AND target_account_id IN (${placeholders(targetAccountIds, 2)})`, [req.accountId, unpackedPayload.account.id].concat(targetAccountIds)),
+        ];
+
+        if (accountDomain) {
+          queries.push(client.query('SELECT 1 FROM account_domain_blocks WHERE account_id = $1 AND domain = $2', [req.accountId, accountDomain]));
+        }
+
+        Promise.all(queries).then(values => {
+          done();
+
+          if (values[0].rows.length > 0 || (values.length > 1 && values[1].rows.length > 0)) {
             return;
           }
 
-          if (req.accountId) {
-            const queries = [
-              client.query(`SELECT 1 FROM blocks WHERE (account_id = $1 AND target_account_id IN (${placeholders(targetAccountIds, 2)})) OR (account_id = $2 AND target_account_id = $1) UNION SELECT 1 FROM mutes WHERE account_id = $1 AND target_account_id IN (${placeholders(targetAccountIds, 2)})`, [req.accountId, unpackedPayload.account.id].concat(targetAccountIds)),
-            ];
-
-            if (accountDomain) {
-              queries.push(client.query('SELECT 1 FROM account_domain_blocks WHERE account_id = $1 AND domain = $2', [req.accountId, accountDomain]));
-            }
-
-            Promise.all(queries).then(values => {
-              done();
-
-              if (values[0].rows.length > 0 || (values.length > 1 && values[1].rows.length > 0)) {
-                return;
-              }
-
-              transmit();
-            }).catch(err => {
-              done();
-              log.error(err);
-            });
-          } else {
-            done();
-            transmit();
-          }
+          transmit();
+        }).catch(err => {
+          done();
+          log.error(err);
         });
-      } else {
-        transmit();
-      }
+      });
     };
 
     subscribe(`${redisPrefix}${id}`, listener);