From 285aa36556b4f28a4181e3ffc9dc7ed3471020ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Wed, 31 Mar 2021 13:20:35 -0400 Subject: [PATCH] feat: allow exists methods to work with arrays and single id --- src/categories/index.js | 9 ++++----- src/posts/index.js | 7 +++---- src/topics/index.js | 6 ++++-- src/user/index.js | 8 ++++++-- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/categories/index.js b/src/categories/index.js index c7a1748507..81f98e2009 100644 --- a/src/categories/index.js +++ b/src/categories/index.js @@ -23,11 +23,10 @@ require('./update')(Categories); require('./watch')(Categories); require('./search')(Categories); -Categories.exists = async function (cid) { - if (Array.isArray(cid)) { - return await db.exists(cid.map(cid => `category:${cid}`)); - } - return await db.exists(`category:${cid}`); +Categories.exists = async function (cids) { + return await db.exists( + Array.isArray(cids) ? cids.map(cid => `category:${cid}`) : `category:${cids}` + ); }; Categories.getCategoryById = async function (data) { diff --git a/src/posts/index.js b/src/posts/index.js index aed0d85c90..43dd6674a6 100644 --- a/src/posts/index.js +++ b/src/posts/index.js @@ -28,10 +28,9 @@ require('./diffs')(Posts); require('./uploads')(Posts); Posts.exists = async function (pids) { - const isArray = Array.isArray(pids); - pids = isArray ? pids : [pids]; - const exists = await db.exists(pids.map(pid => `post:${pid}`)); - return isArray ? exists : exists[0]; + return await db.exists( + Array.isArray(pids) ? pids.map(pid => `post:${pid}`) : `post:${pids}` + ); }; Posts.getPidsFromSet = async function (set, start, stop, reverse) { diff --git a/src/topics/index.js b/src/topics/index.js index 2108eaeb6a..6c49995203 100644 --- a/src/topics/index.js +++ b/src/topics/index.js @@ -35,8 +35,10 @@ require('./bookmarks')(Topics); require('./merge')(Topics); Topics.events = require('./events'); -Topics.exists = async function (tid) { - return await db.exists(`topic:${tid}`); +Topics.exists = async function (tids) { + return await db.exists( + Array.isArray(tids) ? tids.map(tid => `topic:${tid}`) : `topic:${tids}` + ); }; Topics.getTopicsFromSet = async function (set, uid, start, stop) { diff --git a/src/user/index.js b/src/user/index.js index 70cbb4fa79..e2c807ed04 100644 --- a/src/user/index.js +++ b/src/user/index.js @@ -40,8 +40,12 @@ require('./online')(User); require('./blocks')(User); require('./uploads')(User); -User.exists = async function (uid) { - return await db.isSortedSetMember('users:joindate', uid); +User.exists = async function (uids) { + return await ( + Array.isArray(uids) ? + db.isSortedSetMembers('users:joindate', uids) : + db.isSortedSetMember('users:joindate', uids) + ); }; User.existsBySlug = async function (userslug) {