From 9e3e88b8deafd74b1800c591367529a835493b0a Mon Sep 17 00:00:00 2001 From: Anna Zhang Date: Sat, 20 Jan 2024 01:40:27 -0500 Subject: [PATCH] Convert bookmarks.js to ts with require promise errors --- src/posts/bookmarks.js | 136 ++++++++++++++++++++++------------------- src/posts/bookmarks.ts | 85 ++++++++++++++++++++++++++ 2 files changed, 159 insertions(+), 62 deletions(-) create mode 100644 src/posts/bookmarks.ts diff --git a/src/posts/bookmarks.js b/src/posts/bookmarks.js index 9924664bce..1b6ab5a5c9 100644 --- a/src/posts/bookmarks.js +++ b/src/posts/bookmarks.js @@ -1,68 +1,80 @@ -'use strict'; - -const db = require('../database'); -const plugins = require('../plugins'); - +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const db = require("../database"); +const plugins = require("../plugins"); module.exports = function (Posts) { - Posts.bookmark = async function (pid, uid) { - return await toggleBookmark('bookmark', pid, uid); + function toggleBookmark(type, pid, uid) { + return __awaiter(this, void 0, void 0, function* () { + if (parseInt(uid, 10) <= 0) { + throw new Error('[[error:not-logged-in]]'); + } + const isBookmarking = type === 'bookmark'; + const [postData, hasBookmarked] = yield Promise.all([ + Posts.getPostFields(pid, ['pid', 'uid']), + Posts.hasBookmarked(pid, uid), + ]); + if (isBookmarking && hasBookmarked) { + throw new Error('[[error:already-bookmarked]]'); + } + if (!isBookmarking && !hasBookmarked) { + throw new Error('[[error:already-unbookmarked]]'); + } + if (isBookmarking) { + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + yield db.sortedSetAdd(`uid:${uid}:bookmarks`, Date.now(), pid); + } + else { + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + yield db.sortedSetRemove(`uid:${uid}:bookmarks`, pid); + } + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + yield db[isBookmarking ? 'setAdd' : 'setRemove'](`pid:${pid}:users_bookmarked`, uid); + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + postData.bookmarks = (yield db.setCount(`pid:${pid}:users_bookmarked`)); + yield Posts.setPostField(pid, 'bookmarks', postData.bookmarks); + plugins.hooks.fire(`action:post.${type}`, { + pid: pid, + uid: uid, + owner: postData.uid, + current: hasBookmarked ? 'bookmarked' : 'unbookmarked', + }); + return { + post: postData, + isBookmarked: isBookmarking, + }; + }); + } + Posts.bookmark = function (pid, uid) { + return __awaiter(this, void 0, void 0, function* () { + return yield toggleBookmark('bookmark', pid, uid); + }); }; - - Posts.unbookmark = async function (pid, uid) { - return await toggleBookmark('unbookmark', pid, uid); + Posts.unbookmark = function (pid, uid) { + return __awaiter(this, void 0, void 0, function* () { + return yield toggleBookmark('unbookmark', pid, uid); + }); }; - - async function toggleBookmark(type, pid, uid) { - if (parseInt(uid, 10) <= 0) { - throw new Error('[[error:not-logged-in]]'); - } - - const isBookmarking = type === 'bookmark'; - - const [postData, hasBookmarked] = await Promise.all([ - Posts.getPostFields(pid, ['pid', 'uid']), - Posts.hasBookmarked(pid, uid), - ]); - - if (isBookmarking && hasBookmarked) { - throw new Error('[[error:already-bookmarked]]'); - } - - if (!isBookmarking && !hasBookmarked) { - throw new Error('[[error:already-unbookmarked]]'); - } - - if (isBookmarking) { - await db.sortedSetAdd(`uid:${uid}:bookmarks`, Date.now(), pid); - } else { - await db.sortedSetRemove(`uid:${uid}:bookmarks`, pid); - } - await db[isBookmarking ? 'setAdd' : 'setRemove'](`pid:${pid}:users_bookmarked`, uid); - postData.bookmarks = await db.setCount(`pid:${pid}:users_bookmarked`); - await Posts.setPostField(pid, 'bookmarks', postData.bookmarks); - - plugins.hooks.fire(`action:post.${type}`, { - pid: pid, - uid: uid, - owner: postData.uid, - current: hasBookmarked ? 'bookmarked' : 'unbookmarked', + Posts.hasBookmarked = function (pid, uid) { + return __awaiter(this, void 0, void 0, function* () { + if (parseInt(uid, 10) <= 0) { + return Array.isArray(pid) ? pid.map(() => false) : false; + } + if (Array.isArray(pid)) { + const sets = pid.map(pid => `pid:${pid}:users_bookmarked`); + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + return yield db.isMemberOfSets(sets, uid); + } + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + return yield db.isSetMember(`pid:${pid}:users_bookmarked`, uid); }); - - return { - post: postData, - isBookmarked: isBookmarking, - }; - } - - Posts.hasBookmarked = async function (pid, uid) { - if (parseInt(uid, 10) <= 0) { - return Array.isArray(pid) ? pid.map(() => false) : false; - } - - if (Array.isArray(pid)) { - const sets = pid.map(pid => `pid:${pid}:users_bookmarked`); - return await db.isMemberOfSets(sets, uid); - } - return await db.isSetMember(`pid:${pid}:users_bookmarked`, uid); }; }; diff --git a/src/posts/bookmarks.ts b/src/posts/bookmarks.ts new file mode 100644 index 0000000000..cec897707d --- /dev/null +++ b/src/posts/bookmarks.ts @@ -0,0 +1,85 @@ +import db = require('../database'); +import plugins = require('../plugins'); + +type PostData = { + uid: string; + bookmarks: string[]; +} + +type Post = { + bookmark: (pid: string, uid: string) => Promise; + unbookmark: (pid: string, uid: string) => Promise + getPostFields: (pid: string, fields: string[]) => PostData; + hasBookmarked: (pid: string, uid: string) => Promise; + setPostField: (pid: string, field: string, bookmarks: string[]) => Promise; +} + +module.exports = function (Posts: Post) { + async function toggleBookmark(type: string, pid: string, uid: string) { + if (parseInt(uid, 10) <= 0) { + throw new Error('[[error:not-logged-in]]'); + } + + const isBookmarking = type === 'bookmark'; + + const [postData, hasBookmarked] = await Promise.all([ + Posts.getPostFields(pid, ['pid', 'uid']), + Posts.hasBookmarked(pid, uid), + ]); + + if (isBookmarking && hasBookmarked) { + throw new Error('[[error:already-bookmarked]]'); + } + + if (!isBookmarking && !hasBookmarked) { + throw new Error('[[error:already-unbookmarked]]'); + } + + if (isBookmarking) { + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + await db.sortedSetAdd(`uid:${uid}:bookmarks`, Date.now(), pid); + } else { + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + await db.sortedSetRemove(`uid:${uid}:bookmarks`, pid); + } + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + await db[isBookmarking ? 'setAdd' : 'setRemove'](`pid:${pid}:users_bookmarked`, uid); + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + postData.bookmarks = await db.setCount(`pid:${pid}:users_bookmarked`) as string[]; + await Posts.setPostField(pid, 'bookmarks', postData.bookmarks); + + plugins.hooks.fire(`action:post.${type}`, { + pid: pid, + uid: uid, + owner: postData.uid, + current: hasBookmarked ? 'bookmarked' : 'unbookmarked', + }) as void; + + return { + post: postData, + isBookmarked: isBookmarking, + }; + } + + Posts.bookmark = async function (pid: string, uid: string) { + return await toggleBookmark('bookmark', pid, uid); + }; + + Posts.unbookmark = async function (pid: string, uid: string) { + return await toggleBookmark('unbookmark', pid, uid); + }; + + Posts.hasBookmarked = async function (pid: string, uid: string) { + if (parseInt(uid, 10) <= 0) { + return Array.isArray(pid) ? pid.map(() => false) : false; + } + + if (Array.isArray(pid)) { + const sets = pid.map(pid => `pid:${pid as string}:users_bookmarked`); + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + return await db.isMemberOfSets(sets, uid) as boolean; + } + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + return await db.isSetMember(`pid:${pid}:users_bookmarked`, uid) as boolean; + }; +};