diff --git a/package.json b/package.json index cae719d02b..19bdc049f6 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 f3e1f5251b..3880f0bd0c 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 0000000000..8c1aabd568 --- /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 663e60af6d..b1d29c6a28 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 9909ea01ad..e9cbec7061 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 c0b2b5a0f3..45686fe405 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 3d2c9ee3c1..3458d5675f 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 46de24379f..5864b8d321 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 fad6a3ef70..0f81014450 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 4270157694..e826d78403 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 9acaec5c59..9a01d3a990 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 b84fc5f5f5..01d4b8ce6b 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 7d488b9cd3..0c8c15f118 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 c06a337fd4..d2efb45b3d 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 8aa588f557..2d55ce3616 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 6a6c6463d9..3393932af8 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 e94b832cb0..981665ca2c 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 5367f81412..4d7db5617a 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 d778164de0..7e94205ca5 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 8fe4eb2ee9..adcaf04b36 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 a710fef364..cdc859b5a7 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 479f92dd44..ae576907bc 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 e6bdbe0b8b..aeec075636 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 9e700104ab..b385a1ed72 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 85c8454ddc..f9faa8124d 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 a38edd3a50..baeccc28d5 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 5cfa7e4dfc..9e0be6a944 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 6ca09ca168..f2afe00d15 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 075e369832..63080d22a4 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 2d58ada4ce..27d3f1be32 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 caf5cf318f..f2c1213363 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);