From 9427a756c9e4a1d95210ccfca56fdb67d62183ec Mon Sep 17 00:00:00 2001 From: syuilo Date: Tue, 16 Oct 2018 11:38:09 +0900 Subject: [PATCH] Update mongodb --- package.json | 2 +- src/misc/cafy-id.ts | 5 +++-- src/misc/is-objectid.ts | 3 +++ src/misc/should-mute-this-note.ts | 3 ++- src/models/access-token.ts | 3 ++- src/models/app.ts | 5 +++-- src/models/auth-session.ts | 3 ++- src/models/drive-file-thumbnail.ts | 3 ++- src/models/drive-file.ts | 5 +++-- src/models/drive-folder.ts | 5 +++-- src/models/favorite.ts | 5 +++-- src/models/follow-request.ts | 5 +++-- src/models/followed-log.ts | 3 ++- src/models/following-log.ts | 3 ++- src/models/following.ts | 3 ++- src/models/games/reversi/game.ts | 5 +++-- src/models/games/reversi/matching.ts | 3 ++- src/models/messaging-history.ts | 3 ++- src/models/messaging-message.ts | 5 +++-- src/models/mute.ts | 3 ++- src/models/note-reaction.ts | 5 +++-- src/models/note-watching.ts | 3 ++- src/models/note.ts | 7 ++++--- src/models/notification.ts | 5 +++-- src/models/poll-vote.ts | 3 ++- src/models/sw-subscription.ts | 3 ++- src/models/user-list.ts | 5 +++-- src/models/user.ts | 8 ++++---- src/server/api/common/read-messaging-message.ts | 9 +++++---- src/server/api/common/read-notification.ts | 7 ++++--- src/services/note/read.ts | 5 +++-- 31 files changed, 83 insertions(+), 52 deletions(-) create mode 100644 src/misc/is-objectid.ts diff --git a/package.json b/package.json index cae719d02..19bdc049f 100644 --- a/package.json +++ b/package.json @@ -160,7 +160,7 @@ "mkdirp": "0.5.1", "mocha": "5.2.0", "moji": "0.5.1", - "mongodb": "3.1.1", + "mongodb": "3.1.8", "monk": "6.0.6", "ms": "2.1.1", "nan": "2.11.1", diff --git a/src/misc/cafy-id.ts b/src/misc/cafy-id.ts index f3e1f5251..3880f0bd0 100644 --- a/src/misc/cafy-id.ts +++ b/src/misc/cafy-id.ts @@ -1,5 +1,6 @@ import * as mongo from 'mongodb'; import { Context } from 'cafy'; +import isObjectId from './is-objectid'; export const isAnId = (x: any) => mongo.ObjectID.isValid(x); export const isNotAnId = (x: any) => !isAnId(x); @@ -12,7 +13,7 @@ export default class ID extends Context { super(); this.transform = v => { - if (isAnId(v) && !mongo.ObjectID.prototype.isPrototypeOf(v)) { + if (isAnId(v) && !isObjectId(v)) { return new mongo.ObjectID(v); } else { return v; @@ -20,7 +21,7 @@ export default class ID extends Context { }; this.push(v => { - if (!mongo.ObjectID.prototype.isPrototypeOf(v) && isNotAnId(v)) { + if (!isObjectId(v) && isNotAnId(v)) { return new Error('must-be-an-id'); } return true; diff --git a/src/misc/is-objectid.ts b/src/misc/is-objectid.ts new file mode 100644 index 000000000..8c1aabd56 --- /dev/null +++ b/src/misc/is-objectid.ts @@ -0,0 +1,3 @@ +export default function(x: any): boolean { + return x.hasOwnProperty('toHexString') || x.hasOwnProperty('_bsontype'); +} diff --git a/src/misc/should-mute-this-note.ts b/src/misc/should-mute-this-note.ts index 663e60af6..b1d29c6a2 100644 --- a/src/misc/should-mute-this-note.ts +++ b/src/misc/should-mute-this-note.ts @@ -1,7 +1,8 @@ import * as mongo from 'mongodb'; +import isObjectId from './is-objectid'; function toString(id: any) { - return mongo.ObjectID.prototype.isPrototypeOf(id) ? (id as mongo.ObjectID).toHexString() : id; + return isObjectId(id) ? (id as mongo.ObjectID).toHexString() : id; } export default function(note: any, mutedUserIds: string[]): boolean { diff --git a/src/models/access-token.ts b/src/models/access-token.ts index 9909ea01a..e9cbec706 100644 --- a/src/models/access-token.ts +++ b/src/models/access-token.ts @@ -1,5 +1,6 @@ import * as mongo from 'mongodb'; import db from '../db/mongodb'; +import isObjectId from '../misc/is-objectid'; const AccessToken = db.get('accessTokens'); AccessToken.createIndex('token'); @@ -22,7 +23,7 @@ export async function deleteAccessToken(accessToken: string | mongo.ObjectID | I let a: IAccessToken; // Populate - if (mongo.ObjectID.prototype.isPrototypeOf(accessToken)) { + if (isObjectId(accessToken)) { a = await AccessToken.findOne({ _id: accessToken }); diff --git a/src/models/app.ts b/src/models/app.ts index c0b2b5a0f..45686fe40 100644 --- a/src/models/app.ts +++ b/src/models/app.ts @@ -2,6 +2,7 @@ import * as mongo from 'mongodb'; const deepcopy = require('deepcopy'); import AccessToken from './access-token'; import db from '../db/mongodb'; +import isObjectId from '../misc/is-objectid'; import config from '../config'; const App = db.get('apps'); @@ -43,7 +44,7 @@ export const pack = ( let _app: any; // Populate the app if 'app' is ID - if (mongo.ObjectID.prototype.isPrototypeOf(app)) { + if (isObjectId(app)) { _app = await App.findOne({ _id: app }); @@ -56,7 +57,7 @@ export const pack = ( } // Me - if (me && !mongo.ObjectID.prototype.isPrototypeOf(me)) { + if (me && !isObjectId(me)) { if (typeof me === 'string') { me = new mongo.ObjectID(me); } else { diff --git a/src/models/auth-session.ts b/src/models/auth-session.ts index 3d2c9ee3c..3458d5675 100644 --- a/src/models/auth-session.ts +++ b/src/models/auth-session.ts @@ -1,6 +1,7 @@ import * as mongo from 'mongodb'; const deepcopy = require('deepcopy'); import db from '../db/mongodb'; +import isObjectId from '../misc/is-objectid'; import { pack as packApp } from './app'; const AuthSession = db.get('authSessions'); @@ -31,7 +32,7 @@ export const pack = ( _session = deepcopy(session); // Me - if (me && !mongo.ObjectID.prototype.isPrototypeOf(me)) { + if (me && !isObjectId(me)) { if (typeof me === 'string') { me = new mongo.ObjectID(me); } else { diff --git a/src/models/drive-file-thumbnail.ts b/src/models/drive-file-thumbnail.ts index 46de24379..5864b8d32 100644 --- a/src/models/drive-file-thumbnail.ts +++ b/src/models/drive-file-thumbnail.ts @@ -1,5 +1,6 @@ import * as mongo from 'mongodb'; import monkDb, { nativeDbConn } from '../db/mongodb'; +import isObjectId from '../misc/is-objectid'; const DriveFileThumbnail = monkDb.get('driveFileThumbnails.files'); DriveFileThumbnail.createIndex('metadata.originalId', { sparse: true, unique: true }); @@ -35,7 +36,7 @@ export async function deleteDriveFileThumbnail(driveFile: string | mongo.ObjectI let d: IDriveFileThumbnail; // Populate - if (mongo.ObjectID.prototype.isPrototypeOf(driveFile)) { + if (isObjectId(driveFile)) { d = await DriveFileThumbnail.findOne({ _id: driveFile }); diff --git a/src/models/drive-file.ts b/src/models/drive-file.ts index fad6a3ef7..0f8101445 100644 --- a/src/models/drive-file.ts +++ b/src/models/drive-file.ts @@ -3,6 +3,7 @@ const deepcopy = require('deepcopy'); import { pack as packFolder } from './drive-folder'; import config from '../config'; import monkDb, { nativeDbConn } from '../db/mongodb'; +import isObjectId from '../misc/is-objectid'; import Note, { deleteNote } from './note'; import MessagingMessage, { deleteMessagingMessage } from './messaging-message'; import User from './user'; @@ -78,7 +79,7 @@ export async function deleteDriveFile(driveFile: string | mongo.ObjectID | IDriv let d: IDriveFile; // Populate - if (mongo.ObjectID.prototype.isPrototypeOf(driveFile)) { + if (isObjectId(driveFile)) { d = await DriveFile.findOne({ _id: driveFile }); @@ -154,7 +155,7 @@ export const pack = ( let _file: any; // Populate the file if 'file' is ID - if (mongo.ObjectID.prototype.isPrototypeOf(file)) { + if (isObjectId(file)) { _file = await DriveFile.findOne({ _id: file }); diff --git a/src/models/drive-folder.ts b/src/models/drive-folder.ts index 427015769..e826d7840 100644 --- a/src/models/drive-folder.ts +++ b/src/models/drive-folder.ts @@ -1,6 +1,7 @@ import * as mongo from 'mongodb'; const deepcopy = require('deepcopy'); import db from '../db/mongodb'; +import isObjectId from '../misc/is-objectid'; import DriveFile from './drive-file'; const DriveFolder = db.get('driveFolders'); @@ -29,7 +30,7 @@ export async function deleteDriveFolder(driveFolder: string | mongo.ObjectID | I let d: IDriveFolder; // Populate - if (mongo.ObjectID.prototype.isPrototypeOf(driveFolder)) { + if (isObjectId(driveFolder)) { d = await DriveFolder.findOne({ _id: driveFolder }); @@ -83,7 +84,7 @@ export const pack = ( let _folder: any; // Populate the folder if 'folder' is ID - if (mongo.ObjectID.prototype.isPrototypeOf(folder)) { + if (isObjectId(folder)) { _folder = await DriveFolder.findOne({ _id: folder }); } else if (typeof folder === 'string') { _folder = await DriveFolder.findOne({ _id: new mongo.ObjectID(folder) }); diff --git a/src/models/favorite.ts b/src/models/favorite.ts index 9acaec5c5..9a01d3a99 100644 --- a/src/models/favorite.ts +++ b/src/models/favorite.ts @@ -1,6 +1,7 @@ import * as mongo from 'mongodb'; const deepcopy = require('deepcopy'); import db from '../db/mongodb'; +import isObjectId from '../misc/is-objectid'; import { pack as packNote } from './note'; const Favorite = db.get('favorites'); @@ -21,7 +22,7 @@ export async function deleteFavorite(favorite: string | mongo.ObjectID | IFavori let f: IFavorite; // Populate - if (mongo.ObjectID.prototype.isPrototypeOf(favorite)) { + if (isObjectId(favorite)) { f = await Favorite.findOne({ _id: favorite }); @@ -58,7 +59,7 @@ export const pack = ( let _favorite: any; // Populate the favorite if 'favorite' is ID - if (mongo.ObjectID.prototype.isPrototypeOf(favorite)) { + if (isObjectId(favorite)) { _favorite = await Favorite.findOne({ _id: favorite }); diff --git a/src/models/follow-request.ts b/src/models/follow-request.ts index b84fc5f5f..01d4b8ce6 100644 --- a/src/models/follow-request.ts +++ b/src/models/follow-request.ts @@ -1,6 +1,7 @@ import * as mongo from 'mongodb'; const deepcopy = require('deepcopy'); import db from '../db/mongodb'; +import isObjectId from '../misc/is-objectid'; import { pack as packUser } from './user'; const FollowRequest = db.get('followRequests'); @@ -34,7 +35,7 @@ export async function deleteFollowRequest(followRequest: string | mongo.ObjectID let f: IFollowRequest; // Populate - if (mongo.ObjectID.prototype.isPrototypeOf(followRequest)) { + if (isObjectId(followRequest)) { f = await FollowRequest.findOne({ _id: followRequest }); @@ -64,7 +65,7 @@ export const pack = ( let _request: any; // Populate the request if 'request' is ID - if (mongo.ObjectID.prototype.isPrototypeOf(request)) { + if (isObjectId(request)) { _request = await FollowRequest.findOne({ _id: request }); diff --git a/src/models/followed-log.ts b/src/models/followed-log.ts index 7d488b9cd..0c8c15f11 100644 --- a/src/models/followed-log.ts +++ b/src/models/followed-log.ts @@ -1,5 +1,6 @@ import * as mongo from 'mongodb'; import db from '../db/mongodb'; +import isObjectId from '../misc/is-objectid'; const FollowedLog = db.get('followedLogs'); export default FollowedLog; @@ -18,7 +19,7 @@ export async function deleteFollowedLog(followedLog: string | mongo.ObjectID | I let f: IFollowedLog; // Populate - if (mongo.ObjectID.prototype.isPrototypeOf(followedLog)) { + if (isObjectId(followedLog)) { f = await FollowedLog.findOne({ _id: followedLog }); diff --git a/src/models/following-log.ts b/src/models/following-log.ts index c06a337fd..d2efb45b3 100644 --- a/src/models/following-log.ts +++ b/src/models/following-log.ts @@ -1,5 +1,6 @@ import * as mongo from 'mongodb'; import db from '../db/mongodb'; +import isObjectId from '../misc/is-objectid'; const FollowingLog = db.get('followingLogs'); export default FollowingLog; @@ -18,7 +19,7 @@ export async function deleteFollowingLog(followingLog: string | mongo.ObjectID | let f: IFollowingLog; // Populate - if (mongo.ObjectID.prototype.isPrototypeOf(followingLog)) { + if (isObjectId(followingLog)) { f = await FollowingLog.findOne({ _id: followingLog }); diff --git a/src/models/following.ts b/src/models/following.ts index 8aa588f55..2d55ce361 100644 --- a/src/models/following.ts +++ b/src/models/following.ts @@ -1,5 +1,6 @@ import * as mongo from 'mongodb'; import db from '../db/mongodb'; +import isObjectId from '../misc/is-objectid'; const Following = db.get('following'); Following.createIndex(['followerId', 'followeeId'], { unique: true }); @@ -32,7 +33,7 @@ export async function deleteFollowing(following: string | mongo.ObjectID | IFoll let f: IFollowing; // Populate - if (mongo.ObjectID.prototype.isPrototypeOf(following)) { + if (isObjectId(following)) { f = await Following.findOne({ _id: following }); diff --git a/src/models/games/reversi/game.ts b/src/models/games/reversi/game.ts index 6a6c6463d..3393932af 100644 --- a/src/models/games/reversi/game.ts +++ b/src/models/games/reversi/game.ts @@ -1,6 +1,7 @@ import * as mongo from 'mongodb'; const deepcopy = require('deepcopy'); import db from '../../../db/mongodb'; +import isObjectId from '../../../misc/is-objectid'; import { IUser, pack as packUser } from '../../user'; const ReversiGame = db.get('reversiGames'); @@ -62,7 +63,7 @@ export const pack = ( let _game: any; // Populate the game if 'game' is ID - if (mongo.ObjectID.prototype.isPrototypeOf(game)) { + if (isObjectId(game)) { _game = await ReversiGame.findOne({ _id: game }); @@ -76,7 +77,7 @@ export const pack = ( // Me const meId: mongo.ObjectID = me - ? mongo.ObjectID.prototype.isPrototypeOf(me) + ? isObjectId(me) ? me as mongo.ObjectID : typeof me === 'string' ? new mongo.ObjectID(me) diff --git a/src/models/games/reversi/matching.ts b/src/models/games/reversi/matching.ts index e94b832cb..981665ca2 100644 --- a/src/models/games/reversi/matching.ts +++ b/src/models/games/reversi/matching.ts @@ -1,6 +1,7 @@ import * as mongo from 'mongodb'; const deepcopy = require('deepcopy'); import db from '../../../db/mongodb'; +import isObjectId from '../../../misc/is-objectid'; import { IUser, pack as packUser } from '../../user'; const Matching = db.get('reversiMatchings'); @@ -23,7 +24,7 @@ export const pack = ( // Me const meId: mongo.ObjectID = me - ? mongo.ObjectID.prototype.isPrototypeOf(me) + ? isObjectId(me) ? me as mongo.ObjectID : typeof me === 'string' ? new mongo.ObjectID(me) diff --git a/src/models/messaging-history.ts b/src/models/messaging-history.ts index 5367f8141..4d7db5617 100644 --- a/src/models/messaging-history.ts +++ b/src/models/messaging-history.ts @@ -1,5 +1,6 @@ import * as mongo from 'mongodb'; import db from '../db/mongodb'; +import isObjectId from '../misc/is-objectid'; const MessagingHistory = db.get('messagingHistories'); export default MessagingHistory; @@ -19,7 +20,7 @@ export async function deleteMessagingHistory(messagingHistory: string | mongo.Ob let m: IMessagingHistory; // Populate - if (mongo.ObjectID.prototype.isPrototypeOf(messagingHistory)) { + if (isObjectId(messagingHistory)) { m = await MessagingHistory.findOne({ _id: messagingHistory }); diff --git a/src/models/messaging-message.ts b/src/models/messaging-message.ts index d778164de..7e94205ca 100644 --- a/src/models/messaging-message.ts +++ b/src/models/messaging-message.ts @@ -3,6 +3,7 @@ const deepcopy = require('deepcopy'); import { pack as packUser } from './user'; import { pack as packFile } from './drive-file'; import db from '../db/mongodb'; +import isObjectId from '../misc/is-objectid'; import MessagingHistory, { deleteMessagingHistory } from './messaging-history'; import { length } from 'stringz'; @@ -30,7 +31,7 @@ export async function deleteMessagingMessage(messagingMessage: string | mongo.Ob let m: IMessagingMessage; // Populate - if (mongo.ObjectID.prototype.isPrototypeOf(messagingMessage)) { + if (isObjectId(messagingMessage)) { m = await MessagingMessage.findOne({ _id: messagingMessage }); @@ -72,7 +73,7 @@ export const pack = ( let _message: any; // Populate the message if 'message' is ID - if (mongo.ObjectID.prototype.isPrototypeOf(message)) { + if (isObjectId(message)) { _message = await MessagingMessage.findOne({ _id: message }); diff --git a/src/models/mute.ts b/src/models/mute.ts index 8fe4eb2ee..adcaf04b3 100644 --- a/src/models/mute.ts +++ b/src/models/mute.ts @@ -1,5 +1,6 @@ import * as mongo from 'mongodb'; import db from '../db/mongodb'; +import isObjectId from '../misc/is-objectid'; const Mute = db.get('mute'); Mute.createIndex(['muterId', 'muteeId'], { unique: true }); @@ -19,7 +20,7 @@ export async function deleteMute(mute: string | mongo.ObjectID | IMute) { let m: IMute; // Populate - if (mongo.ObjectID.prototype.isPrototypeOf(mute)) { + if (isObjectId(mute)) { m = await Mute.findOne({ _id: mute }); diff --git a/src/models/note-reaction.ts b/src/models/note-reaction.ts index a710fef36..cdc859b5a 100644 --- a/src/models/note-reaction.ts +++ b/src/models/note-reaction.ts @@ -2,6 +2,7 @@ import * as mongo from 'mongodb'; import $ from 'cafy'; const deepcopy = require('deepcopy'); import db from '../db/mongodb'; +import isObjectId from '../misc/is-objectid'; import Reaction from './note-reaction'; import { pack as packUser } from './user'; @@ -37,7 +38,7 @@ export async function deleteNoteReaction(noteReaction: string | mongo.ObjectID | let n: INoteReaction; // Populate - if (mongo.ObjectID.prototype.isPrototypeOf(noteReaction)) { + if (isObjectId(noteReaction)) { n = await NoteReaction.findOne({ _id: noteReaction }); @@ -67,7 +68,7 @@ export const pack = ( let _reaction: any; // Populate the reaction if 'reaction' is ID - if (mongo.ObjectID.prototype.isPrototypeOf(reaction)) { + if (isObjectId(reaction)) { _reaction = await Reaction.findOne({ _id: reaction }); diff --git a/src/models/note-watching.ts b/src/models/note-watching.ts index 479f92dd4..ae576907b 100644 --- a/src/models/note-watching.ts +++ b/src/models/note-watching.ts @@ -1,5 +1,6 @@ import * as mongo from 'mongodb'; import db from '../db/mongodb'; +import isObjectId from '../misc/is-objectid'; const NoteWatching = db.get('noteWatching'); NoteWatching.createIndex(['userId', 'noteId'], { unique: true }); @@ -19,7 +20,7 @@ export async function deleteNoteWatching(noteWatching: string | mongo.ObjectID | let n: INoteWatching; // Populate - if (mongo.ObjectID.prototype.isPrototypeOf(noteWatching)) { + if (isObjectId(noteWatching)) { n = await NoteWatching.findOne({ _id: noteWatching }); diff --git a/src/models/note.ts b/src/models/note.ts index e6bdbe0b8..aeec07563 100644 --- a/src/models/note.ts +++ b/src/models/note.ts @@ -2,6 +2,7 @@ import * as mongo from 'mongodb'; const deepcopy = require('deepcopy'); import rap from '@prezzemolo/rap'; import db from '../db/mongodb'; +import isObjectId from '../misc/is-objectid'; import { length } from 'stringz'; import { IUser, pack as packUser } from './user'; import { pack as packApp } from './app'; @@ -107,7 +108,7 @@ export async function deleteNote(note: string | mongo.ObjectID | INote) { let n: INote; // Populate - if (mongo.ObjectID.prototype.isPrototypeOf(note)) { + if (isObjectId(note)) { n = await Note.findOne({ _id: note }); @@ -259,7 +260,7 @@ export const pack = async ( // Me const meId: mongo.ObjectID = me - ? mongo.ObjectID.prototype.isPrototypeOf(me) + ? isObjectId(me) ? me as mongo.ObjectID : typeof me === 'string' ? new mongo.ObjectID(me) @@ -269,7 +270,7 @@ export const pack = async ( let _note: any; // Populate the note if 'note' is ID - if (mongo.ObjectID.prototype.isPrototypeOf(note)) { + if (isObjectId(note)) { _note = await Note.findOne({ _id: note }); diff --git a/src/models/notification.ts b/src/models/notification.ts index 9e700104a..b385a1ed7 100644 --- a/src/models/notification.ts +++ b/src/models/notification.ts @@ -1,6 +1,7 @@ import * as mongo from 'mongodb'; const deepcopy = require('deepcopy'); import db from '../db/mongodb'; +import isObjectId from '../misc/is-objectid'; import { IUser, pack as packUser } from './user'; import { pack as packNote } from './note'; @@ -57,7 +58,7 @@ export async function deleteNotification(notification: string | mongo.ObjectID | let n: INotification; // Populate - if (mongo.ObjectID.prototype.isPrototypeOf(notification)) { + if (isObjectId(notification)) { n = await Notification.findOne({ _id: notification }); @@ -90,7 +91,7 @@ export const pack = (notification: any) => new Promise(async (resolve, reje let _notification: any; // Populate the notification if 'notification' is ID - if (mongo.ObjectID.prototype.isPrototypeOf(notification)) { + if (isObjectId(notification)) { _notification = await Notification.findOne({ _id: notification }); diff --git a/src/models/poll-vote.ts b/src/models/poll-vote.ts index 85c8454dd..f9faa8124 100644 --- a/src/models/poll-vote.ts +++ b/src/models/poll-vote.ts @@ -1,5 +1,6 @@ import * as mongo from 'mongodb'; import db from '../db/mongodb'; +import isObjectId from '../misc/is-objectid'; const PollVote = db.get('pollVotes'); export default PollVote; @@ -19,7 +20,7 @@ export async function deletePollVote(pollVote: string | mongo.ObjectID | IPollVo let p: IPollVote; // Populate - if (mongo.ObjectID.prototype.isPrototypeOf(pollVote)) { + if (isObjectId(pollVote)) { p = await PollVote.findOne({ _id: pollVote }); diff --git a/src/models/sw-subscription.ts b/src/models/sw-subscription.ts index a38edd3a5..baeccc28d 100644 --- a/src/models/sw-subscription.ts +++ b/src/models/sw-subscription.ts @@ -1,5 +1,6 @@ import * as mongo from 'mongodb'; import db from '../db/mongodb'; +import isObjectId from '../misc/is-objectid'; const SwSubscription = db.get('swSubscriptions'); export default SwSubscription; @@ -19,7 +20,7 @@ export async function deleteSwSubscription(swSubscription: string | mongo.Object let s: ISwSubscription; // Populate - if (mongo.ObjectID.prototype.isPrototypeOf(swSubscription)) { + if (isObjectId(swSubscription)) { s = await SwSubscription.findOne({ _id: swSubscription }); diff --git a/src/models/user-list.ts b/src/models/user-list.ts index 5cfa7e4df..9e0be6a94 100644 --- a/src/models/user-list.ts +++ b/src/models/user-list.ts @@ -1,6 +1,7 @@ import * as mongo from 'mongodb'; const deepcopy = require('deepcopy'); import db from '../db/mongodb'; +import isObjectId from '../misc/is-objectid'; const UserList = db.get('userList'); export default UserList; @@ -20,7 +21,7 @@ export async function deleteUserList(userList: string | mongo.ObjectID | IUserLi let u: IUserList; // Populate - if (mongo.ObjectID.prototype.isPrototypeOf(userList)) { + if (isObjectId(userList)) { u = await UserList.findOne({ _id: userList }); @@ -45,7 +46,7 @@ export const pack = ( ) => new Promise(async (resolve, reject) => { let _userList: any; - if (mongo.ObjectID.prototype.isPrototypeOf(userList)) { + if (isObjectId(userList)) { _userList = await UserList.findOne({ _id: userList }); diff --git a/src/models/user.ts b/src/models/user.ts index 6ca09ca16..f2afe00d1 100644 --- a/src/models/user.ts +++ b/src/models/user.ts @@ -3,6 +3,7 @@ const deepcopy = require('deepcopy'); const sequential = require('promise-sequential'); import rap from '@prezzemolo/rap'; import db from '../db/mongodb'; +import isObjectId from '../misc/is-objectid'; import Note, { packMany as packNoteMany, deleteNote } from './note'; import Following, { deleteFollowing } from './following'; import Mute, { deleteMute } from './mute'; @@ -175,7 +176,7 @@ export async function deleteUser(user: string | mongo.ObjectID | IUser) { let u: IUser; // Populate - if (mongo.ObjectID.prototype.isPrototypeOf(user)) { + if (isObjectId(user)) { u = await User.findOne({ _id: user }); @@ -340,7 +341,6 @@ export const pack = ( includeHasUnreadNotes?: boolean } ) => new Promise(async (resolve, reject) => { - const opts = Object.assign({ detail: false, includeSecrets: false @@ -358,7 +358,7 @@ export const pack = ( }; // Populate the user if 'user' is ID - if (mongo.ObjectID.prototype.isPrototypeOf(user)) { + if (isObjectId(user)) { _user = await User.findOne({ _id: user }, { fields }); @@ -378,7 +378,7 @@ export const pack = ( // Me const meId: mongo.ObjectID = me - ? mongo.ObjectID.prototype.isPrototypeOf(me) + ? isObjectId(me) ? me as mongo.ObjectID : typeof me === 'string' ? new mongo.ObjectID(me) diff --git a/src/server/api/common/read-messaging-message.ts b/src/server/api/common/read-messaging-message.ts index 075e36983..63080d22a 100644 --- a/src/server/api/common/read-messaging-message.ts +++ b/src/server/api/common/read-messaging-message.ts @@ -1,4 +1,5 @@ import * as mongo from 'mongodb'; +import isObjectId from '../../../misc/is-objectid'; import Message from '../../../models/messaging-message'; import { IMessagingMessage as IMessage } from '../../../models/messaging-message'; import { publishMainStream } from '../../../stream'; @@ -15,21 +16,21 @@ export default ( message: string | string[] | IMessage | IMessage[] | mongo.ObjectID | mongo.ObjectID[] ) => new Promise(async (resolve, reject) => { - const userId = mongo.ObjectID.prototype.isPrototypeOf(user) + const userId = isObjectId(user) ? user : new mongo.ObjectID(user); - const otherpartyId = mongo.ObjectID.prototype.isPrototypeOf(otherparty) + const otherpartyId = isObjectId(otherparty) ? otherparty : new mongo.ObjectID(otherparty); const ids: mongo.ObjectID[] = Array.isArray(message) - ? mongo.ObjectID.prototype.isPrototypeOf(message[0]) + ? isObjectId(message[0]) ? (message as mongo.ObjectID[]) : typeof message[0] === 'string' ? (message as string[]).map(m => new mongo.ObjectID(m)) : (message as IMessage[]).map(m => m._id) - : mongo.ObjectID.prototype.isPrototypeOf(message) + : isObjectId(message) ? [(message as mongo.ObjectID)] : typeof message === 'string' ? [new mongo.ObjectID(message)] diff --git a/src/server/api/common/read-notification.ts b/src/server/api/common/read-notification.ts index 2d58ada4c..27d3f1be3 100644 --- a/src/server/api/common/read-notification.ts +++ b/src/server/api/common/read-notification.ts @@ -1,4 +1,5 @@ import * as mongo from 'mongodb'; +import isObjectId from '../../../misc/is-objectid'; import { default as Notification, INotification } from '../../../models/notification'; import { publishMainStream } from '../../../stream'; import Mute from '../../../models/mute'; @@ -12,17 +13,17 @@ export default ( message: string | string[] | INotification | INotification[] | mongo.ObjectID | mongo.ObjectID[] ) => new Promise(async (resolve, reject) => { - const userId = mongo.ObjectID.prototype.isPrototypeOf(user) + const userId = isObjectId(user) ? user : new mongo.ObjectID(user); const ids: mongo.ObjectID[] = Array.isArray(message) - ? mongo.ObjectID.prototype.isPrototypeOf(message[0]) + ? isObjectId(message[0]) ? (message as mongo.ObjectID[]) : typeof message[0] === 'string' ? (message as string[]).map(m => new mongo.ObjectID(m)) : (message as INotification[]).map(m => m._id) - : mongo.ObjectID.prototype.isPrototypeOf(message) + : isObjectId(message) ? [(message as mongo.ObjectID)] : typeof message === 'string' ? [new mongo.ObjectID(message)] diff --git a/src/services/note/read.ts b/src/services/note/read.ts index caf5cf318..f2c121336 100644 --- a/src/services/note/read.ts +++ b/src/services/note/read.ts @@ -1,4 +1,5 @@ import * as mongo from 'mongodb'; +import isObjectId from '../../misc/is-objectid'; import { publishMainStream } from '../../stream'; import User from '../../models/user'; import NoteUnread from '../../models/note-unread'; @@ -11,11 +12,11 @@ export default ( note: string | mongo.ObjectID ) => new Promise(async (resolve, reject) => { - const userId: mongo.ObjectID = mongo.ObjectID.prototype.isPrototypeOf(user) + const userId: mongo.ObjectID = isObjectId(user) ? user as mongo.ObjectID : new mongo.ObjectID(user); - const noteId: mongo.ObjectID = mongo.ObjectID.prototype.isPrototypeOf(note) + const noteId: mongo.ObjectID = isObjectId(note) ? note as mongo.ObjectID : new mongo.ObjectID(note);