-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert bookmarks.js to ts with require promise errors
- Loading branch information
Showing
2 changed files
with
159 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<unknown>; | ||
unbookmark: (pid: string, uid: string) => Promise<unknown> | ||
getPostFields: (pid: string, fields: string[]) => PostData; | ||
hasBookmarked: (pid: string, uid: string) => Promise<unknown>; | ||
setPostField: (pid: string, field: string, bookmarks: string[]) => Promise<unknown>; | ||
} | ||
|
||
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; | ||
}; | ||
}; |