Update mongodb

This commit is contained in:
syuilo 2018-10-16 11:38:09 +09:00
parent 7b94a46f51
commit 3b54711a87
31 changed files with 83 additions and 52 deletions

View File

@ -160,7 +160,7 @@
"mkdirp": "0.5.1", "mkdirp": "0.5.1",
"mocha": "5.2.0", "mocha": "5.2.0",
"moji": "0.5.1", "moji": "0.5.1",
"mongodb": "3.1.1", "mongodb": "3.1.8",
"monk": "6.0.6", "monk": "6.0.6",
"ms": "2.1.1", "ms": "2.1.1",
"nan": "2.11.1", "nan": "2.11.1",

View File

@ -1,5 +1,6 @@
import * as mongo from 'mongodb'; import * as mongo from 'mongodb';
import { Context } from 'cafy'; import { Context } from 'cafy';
import isObjectId from './is-objectid';
export const isAnId = (x: any) => mongo.ObjectID.isValid(x); export const isAnId = (x: any) => mongo.ObjectID.isValid(x);
export const isNotAnId = (x: any) => !isAnId(x); export const isNotAnId = (x: any) => !isAnId(x);
@ -12,7 +13,7 @@ export default class ID extends Context<mongo.ObjectID> {
super(); super();
this.transform = v => { this.transform = v => {
if (isAnId(v) && !mongo.ObjectID.prototype.isPrototypeOf(v)) { if (isAnId(v) && !isObjectId(v)) {
return new mongo.ObjectID(v); return new mongo.ObjectID(v);
} else { } else {
return v; return v;
@ -20,7 +21,7 @@ export default class ID extends Context<mongo.ObjectID> {
}; };
this.push(v => { this.push(v => {
if (!mongo.ObjectID.prototype.isPrototypeOf(v) && isNotAnId(v)) { if (!isObjectId(v) && isNotAnId(v)) {
return new Error('must-be-an-id'); return new Error('must-be-an-id');
} }
return true; return true;

3
src/misc/is-objectid.ts Normal file
View File

@ -0,0 +1,3 @@
export default function(x: any): boolean {
return x.hasOwnProperty('toHexString') || x.hasOwnProperty('_bsontype');
}

View File

@ -1,7 +1,8 @@
import * as mongo from 'mongodb'; import * as mongo from 'mongodb';
import isObjectId from './is-objectid';
function toString(id: any) { 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 { export default function(note: any, mutedUserIds: string[]): boolean {

View File

@ -1,5 +1,6 @@
import * as mongo from 'mongodb'; import * as mongo from 'mongodb';
import db from '../db/mongodb'; import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
const AccessToken = db.get<IAccessToken>('accessTokens'); const AccessToken = db.get<IAccessToken>('accessTokens');
AccessToken.createIndex('token'); AccessToken.createIndex('token');
@ -22,7 +23,7 @@ export async function deleteAccessToken(accessToken: string | mongo.ObjectID | I
let a: IAccessToken; let a: IAccessToken;
// Populate // Populate
if (mongo.ObjectID.prototype.isPrototypeOf(accessToken)) { if (isObjectId(accessToken)) {
a = await AccessToken.findOne({ a = await AccessToken.findOne({
_id: accessToken _id: accessToken
}); });

View File

@ -2,6 +2,7 @@ import * as mongo from 'mongodb';
const deepcopy = require('deepcopy'); const deepcopy = require('deepcopy');
import AccessToken from './access-token'; import AccessToken from './access-token';
import db from '../db/mongodb'; import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
import config from '../config'; import config from '../config';
const App = db.get<IApp>('apps'); const App = db.get<IApp>('apps');
@ -43,7 +44,7 @@ export const pack = (
let _app: any; let _app: any;
// Populate the app if 'app' is ID // Populate the app if 'app' is ID
if (mongo.ObjectID.prototype.isPrototypeOf(app)) { if (isObjectId(app)) {
_app = await App.findOne({ _app = await App.findOne({
_id: app _id: app
}); });
@ -56,7 +57,7 @@ export const pack = (
} }
// Me // Me
if (me && !mongo.ObjectID.prototype.isPrototypeOf(me)) { if (me && !isObjectId(me)) {
if (typeof me === 'string') { if (typeof me === 'string') {
me = new mongo.ObjectID(me); me = new mongo.ObjectID(me);
} else { } else {

View File

@ -1,6 +1,7 @@
import * as mongo from 'mongodb'; import * as mongo from 'mongodb';
const deepcopy = require('deepcopy'); const deepcopy = require('deepcopy');
import db from '../db/mongodb'; import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
import { pack as packApp } from './app'; import { pack as packApp } from './app';
const AuthSession = db.get<IAuthSession>('authSessions'); const AuthSession = db.get<IAuthSession>('authSessions');
@ -31,7 +32,7 @@ export const pack = (
_session = deepcopy(session); _session = deepcopy(session);
// Me // Me
if (me && !mongo.ObjectID.prototype.isPrototypeOf(me)) { if (me && !isObjectId(me)) {
if (typeof me === 'string') { if (typeof me === 'string') {
me = new mongo.ObjectID(me); me = new mongo.ObjectID(me);
} else { } else {

View File

@ -1,5 +1,6 @@
import * as mongo from 'mongodb'; import * as mongo from 'mongodb';
import monkDb, { nativeDbConn } from '../db/mongodb'; import monkDb, { nativeDbConn } from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
const DriveFileThumbnail = monkDb.get<IDriveFileThumbnail>('driveFileThumbnails.files'); const DriveFileThumbnail = monkDb.get<IDriveFileThumbnail>('driveFileThumbnails.files');
DriveFileThumbnail.createIndex('metadata.originalId', { sparse: true, unique: true }); DriveFileThumbnail.createIndex('metadata.originalId', { sparse: true, unique: true });
@ -35,7 +36,7 @@ export async function deleteDriveFileThumbnail(driveFile: string | mongo.ObjectI
let d: IDriveFileThumbnail; let d: IDriveFileThumbnail;
// Populate // Populate
if (mongo.ObjectID.prototype.isPrototypeOf(driveFile)) { if (isObjectId(driveFile)) {
d = await DriveFileThumbnail.findOne({ d = await DriveFileThumbnail.findOne({
_id: driveFile _id: driveFile
}); });

View File

@ -3,6 +3,7 @@ const deepcopy = require('deepcopy');
import { pack as packFolder } from './drive-folder'; import { pack as packFolder } from './drive-folder';
import config from '../config'; import config from '../config';
import monkDb, { nativeDbConn } from '../db/mongodb'; import monkDb, { nativeDbConn } from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
import Note, { deleteNote } from './note'; import Note, { deleteNote } from './note';
import MessagingMessage, { deleteMessagingMessage } from './messaging-message'; import MessagingMessage, { deleteMessagingMessage } from './messaging-message';
import User from './user'; import User from './user';
@ -78,7 +79,7 @@ export async function deleteDriveFile(driveFile: string | mongo.ObjectID | IDriv
let d: IDriveFile; let d: IDriveFile;
// Populate // Populate
if (mongo.ObjectID.prototype.isPrototypeOf(driveFile)) { if (isObjectId(driveFile)) {
d = await DriveFile.findOne({ d = await DriveFile.findOne({
_id: driveFile _id: driveFile
}); });
@ -154,7 +155,7 @@ export const pack = (
let _file: any; let _file: any;
// Populate the file if 'file' is ID // Populate the file if 'file' is ID
if (mongo.ObjectID.prototype.isPrototypeOf(file)) { if (isObjectId(file)) {
_file = await DriveFile.findOne({ _file = await DriveFile.findOne({
_id: file _id: file
}); });

View File

@ -1,6 +1,7 @@
import * as mongo from 'mongodb'; import * as mongo from 'mongodb';
const deepcopy = require('deepcopy'); const deepcopy = require('deepcopy');
import db from '../db/mongodb'; import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
import DriveFile from './drive-file'; import DriveFile from './drive-file';
const DriveFolder = db.get<IDriveFolder>('driveFolders'); const DriveFolder = db.get<IDriveFolder>('driveFolders');
@ -29,7 +30,7 @@ export async function deleteDriveFolder(driveFolder: string | mongo.ObjectID | I
let d: IDriveFolder; let d: IDriveFolder;
// Populate // Populate
if (mongo.ObjectID.prototype.isPrototypeOf(driveFolder)) { if (isObjectId(driveFolder)) {
d = await DriveFolder.findOne({ d = await DriveFolder.findOne({
_id: driveFolder _id: driveFolder
}); });
@ -83,7 +84,7 @@ export const pack = (
let _folder: any; let _folder: any;
// Populate the folder if 'folder' is ID // Populate the folder if 'folder' is ID
if (mongo.ObjectID.prototype.isPrototypeOf(folder)) { if (isObjectId(folder)) {
_folder = await DriveFolder.findOne({ _id: folder }); _folder = await DriveFolder.findOne({ _id: folder });
} else if (typeof folder === 'string') { } else if (typeof folder === 'string') {
_folder = await DriveFolder.findOne({ _id: new mongo.ObjectID(folder) }); _folder = await DriveFolder.findOne({ _id: new mongo.ObjectID(folder) });

View File

@ -1,6 +1,7 @@
import * as mongo from 'mongodb'; import * as mongo from 'mongodb';
const deepcopy = require('deepcopy'); const deepcopy = require('deepcopy');
import db from '../db/mongodb'; import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
import { pack as packNote } from './note'; import { pack as packNote } from './note';
const Favorite = db.get<IFavorite>('favorites'); const Favorite = db.get<IFavorite>('favorites');
@ -21,7 +22,7 @@ export async function deleteFavorite(favorite: string | mongo.ObjectID | IFavori
let f: IFavorite; let f: IFavorite;
// Populate // Populate
if (mongo.ObjectID.prototype.isPrototypeOf(favorite)) { if (isObjectId(favorite)) {
f = await Favorite.findOne({ f = await Favorite.findOne({
_id: favorite _id: favorite
}); });
@ -58,7 +59,7 @@ export const pack = (
let _favorite: any; let _favorite: any;
// Populate the favorite if 'favorite' is ID // Populate the favorite if 'favorite' is ID
if (mongo.ObjectID.prototype.isPrototypeOf(favorite)) { if (isObjectId(favorite)) {
_favorite = await Favorite.findOne({ _favorite = await Favorite.findOne({
_id: favorite _id: favorite
}); });

View File

@ -1,6 +1,7 @@
import * as mongo from 'mongodb'; import * as mongo from 'mongodb';
const deepcopy = require('deepcopy'); const deepcopy = require('deepcopy');
import db from '../db/mongodb'; import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
import { pack as packUser } from './user'; import { pack as packUser } from './user';
const FollowRequest = db.get<IFollowRequest>('followRequests'); const FollowRequest = db.get<IFollowRequest>('followRequests');
@ -34,7 +35,7 @@ export async function deleteFollowRequest(followRequest: string | mongo.ObjectID
let f: IFollowRequest; let f: IFollowRequest;
// Populate // Populate
if (mongo.ObjectID.prototype.isPrototypeOf(followRequest)) { if (isObjectId(followRequest)) {
f = await FollowRequest.findOne({ f = await FollowRequest.findOne({
_id: followRequest _id: followRequest
}); });
@ -64,7 +65,7 @@ export const pack = (
let _request: any; let _request: any;
// Populate the request if 'request' is ID // Populate the request if 'request' is ID
if (mongo.ObjectID.prototype.isPrototypeOf(request)) { if (isObjectId(request)) {
_request = await FollowRequest.findOne({ _request = await FollowRequest.findOne({
_id: request _id: request
}); });

View File

@ -1,5 +1,6 @@
import * as mongo from 'mongodb'; import * as mongo from 'mongodb';
import db from '../db/mongodb'; import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
const FollowedLog = db.get<IFollowedLog>('followedLogs'); const FollowedLog = db.get<IFollowedLog>('followedLogs');
export default FollowedLog; export default FollowedLog;
@ -18,7 +19,7 @@ export async function deleteFollowedLog(followedLog: string | mongo.ObjectID | I
let f: IFollowedLog; let f: IFollowedLog;
// Populate // Populate
if (mongo.ObjectID.prototype.isPrototypeOf(followedLog)) { if (isObjectId(followedLog)) {
f = await FollowedLog.findOne({ f = await FollowedLog.findOne({
_id: followedLog _id: followedLog
}); });

View File

@ -1,5 +1,6 @@
import * as mongo from 'mongodb'; import * as mongo from 'mongodb';
import db from '../db/mongodb'; import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
const FollowingLog = db.get<IFollowingLog>('followingLogs'); const FollowingLog = db.get<IFollowingLog>('followingLogs');
export default FollowingLog; export default FollowingLog;
@ -18,7 +19,7 @@ export async function deleteFollowingLog(followingLog: string | mongo.ObjectID |
let f: IFollowingLog; let f: IFollowingLog;
// Populate // Populate
if (mongo.ObjectID.prototype.isPrototypeOf(followingLog)) { if (isObjectId(followingLog)) {
f = await FollowingLog.findOne({ f = await FollowingLog.findOne({
_id: followingLog _id: followingLog
}); });

View File

@ -1,5 +1,6 @@
import * as mongo from 'mongodb'; import * as mongo from 'mongodb';
import db from '../db/mongodb'; import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
const Following = db.get<IFollowing>('following'); const Following = db.get<IFollowing>('following');
Following.createIndex(['followerId', 'followeeId'], { unique: true }); Following.createIndex(['followerId', 'followeeId'], { unique: true });
@ -32,7 +33,7 @@ export async function deleteFollowing(following: string | mongo.ObjectID | IFoll
let f: IFollowing; let f: IFollowing;
// Populate // Populate
if (mongo.ObjectID.prototype.isPrototypeOf(following)) { if (isObjectId(following)) {
f = await Following.findOne({ f = await Following.findOne({
_id: following _id: following
}); });

View File

@ -1,6 +1,7 @@
import * as mongo from 'mongodb'; import * as mongo from 'mongodb';
const deepcopy = require('deepcopy'); const deepcopy = require('deepcopy');
import db from '../../../db/mongodb'; import db from '../../../db/mongodb';
import isObjectId from '../../../misc/is-objectid';
import { IUser, pack as packUser } from '../../user'; import { IUser, pack as packUser } from '../../user';
const ReversiGame = db.get<IReversiGame>('reversiGames'); const ReversiGame = db.get<IReversiGame>('reversiGames');
@ -62,7 +63,7 @@ export const pack = (
let _game: any; let _game: any;
// Populate the game if 'game' is ID // Populate the game if 'game' is ID
if (mongo.ObjectID.prototype.isPrototypeOf(game)) { if (isObjectId(game)) {
_game = await ReversiGame.findOne({ _game = await ReversiGame.findOne({
_id: game _id: game
}); });
@ -76,7 +77,7 @@ export const pack = (
// Me // Me
const meId: mongo.ObjectID = me const meId: mongo.ObjectID = me
? mongo.ObjectID.prototype.isPrototypeOf(me) ? isObjectId(me)
? me as mongo.ObjectID ? me as mongo.ObjectID
: typeof me === 'string' : typeof me === 'string'
? new mongo.ObjectID(me) ? new mongo.ObjectID(me)

View File

@ -1,6 +1,7 @@
import * as mongo from 'mongodb'; import * as mongo from 'mongodb';
const deepcopy = require('deepcopy'); const deepcopy = require('deepcopy');
import db from '../../../db/mongodb'; import db from '../../../db/mongodb';
import isObjectId from '../../../misc/is-objectid';
import { IUser, pack as packUser } from '../../user'; import { IUser, pack as packUser } from '../../user';
const Matching = db.get<IMatching>('reversiMatchings'); const Matching = db.get<IMatching>('reversiMatchings');
@ -23,7 +24,7 @@ export const pack = (
// Me // Me
const meId: mongo.ObjectID = me const meId: mongo.ObjectID = me
? mongo.ObjectID.prototype.isPrototypeOf(me) ? isObjectId(me)
? me as mongo.ObjectID ? me as mongo.ObjectID
: typeof me === 'string' : typeof me === 'string'
? new mongo.ObjectID(me) ? new mongo.ObjectID(me)

View File

@ -1,5 +1,6 @@
import * as mongo from 'mongodb'; import * as mongo from 'mongodb';
import db from '../db/mongodb'; import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
const MessagingHistory = db.get<IMessagingHistory>('messagingHistories'); const MessagingHistory = db.get<IMessagingHistory>('messagingHistories');
export default MessagingHistory; export default MessagingHistory;
@ -19,7 +20,7 @@ export async function deleteMessagingHistory(messagingHistory: string | mongo.Ob
let m: IMessagingHistory; let m: IMessagingHistory;
// Populate // Populate
if (mongo.ObjectID.prototype.isPrototypeOf(messagingHistory)) { if (isObjectId(messagingHistory)) {
m = await MessagingHistory.findOne({ m = await MessagingHistory.findOne({
_id: messagingHistory _id: messagingHistory
}); });

View File

@ -3,6 +3,7 @@ const deepcopy = require('deepcopy');
import { pack as packUser } from './user'; import { pack as packUser } from './user';
import { pack as packFile } from './drive-file'; import { pack as packFile } from './drive-file';
import db from '../db/mongodb'; import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
import MessagingHistory, { deleteMessagingHistory } from './messaging-history'; import MessagingHistory, { deleteMessagingHistory } from './messaging-history';
import { length } from 'stringz'; import { length } from 'stringz';
@ -30,7 +31,7 @@ export async function deleteMessagingMessage(messagingMessage: string | mongo.Ob
let m: IMessagingMessage; let m: IMessagingMessage;
// Populate // Populate
if (mongo.ObjectID.prototype.isPrototypeOf(messagingMessage)) { if (isObjectId(messagingMessage)) {
m = await MessagingMessage.findOne({ m = await MessagingMessage.findOne({
_id: messagingMessage _id: messagingMessage
}); });
@ -72,7 +73,7 @@ export const pack = (
let _message: any; let _message: any;
// Populate the message if 'message' is ID // Populate the message if 'message' is ID
if (mongo.ObjectID.prototype.isPrototypeOf(message)) { if (isObjectId(message)) {
_message = await MessagingMessage.findOne({ _message = await MessagingMessage.findOne({
_id: message _id: message
}); });

View File

@ -1,5 +1,6 @@
import * as mongo from 'mongodb'; import * as mongo from 'mongodb';
import db from '../db/mongodb'; import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
const Mute = db.get<IMute>('mute'); const Mute = db.get<IMute>('mute');
Mute.createIndex(['muterId', 'muteeId'], { unique: true }); Mute.createIndex(['muterId', 'muteeId'], { unique: true });
@ -19,7 +20,7 @@ export async function deleteMute(mute: string | mongo.ObjectID | IMute) {
let m: IMute; let m: IMute;
// Populate // Populate
if (mongo.ObjectID.prototype.isPrototypeOf(mute)) { if (isObjectId(mute)) {
m = await Mute.findOne({ m = await Mute.findOne({
_id: mute _id: mute
}); });

View File

@ -2,6 +2,7 @@ import * as mongo from 'mongodb';
import $ from 'cafy'; import $ from 'cafy';
const deepcopy = require('deepcopy'); const deepcopy = require('deepcopy');
import db from '../db/mongodb'; import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
import Reaction from './note-reaction'; import Reaction from './note-reaction';
import { pack as packUser } from './user'; import { pack as packUser } from './user';
@ -37,7 +38,7 @@ export async function deleteNoteReaction(noteReaction: string | mongo.ObjectID |
let n: INoteReaction; let n: INoteReaction;
// Populate // Populate
if (mongo.ObjectID.prototype.isPrototypeOf(noteReaction)) { if (isObjectId(noteReaction)) {
n = await NoteReaction.findOne({ n = await NoteReaction.findOne({
_id: noteReaction _id: noteReaction
}); });
@ -67,7 +68,7 @@ export const pack = (
let _reaction: any; let _reaction: any;
// Populate the reaction if 'reaction' is ID // Populate the reaction if 'reaction' is ID
if (mongo.ObjectID.prototype.isPrototypeOf(reaction)) { if (isObjectId(reaction)) {
_reaction = await Reaction.findOne({ _reaction = await Reaction.findOne({
_id: reaction _id: reaction
}); });

View File

@ -1,5 +1,6 @@
import * as mongo from 'mongodb'; import * as mongo from 'mongodb';
import db from '../db/mongodb'; import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
const NoteWatching = db.get<INoteWatching>('noteWatching'); const NoteWatching = db.get<INoteWatching>('noteWatching');
NoteWatching.createIndex(['userId', 'noteId'], { unique: true }); NoteWatching.createIndex(['userId', 'noteId'], { unique: true });
@ -19,7 +20,7 @@ export async function deleteNoteWatching(noteWatching: string | mongo.ObjectID |
let n: INoteWatching; let n: INoteWatching;
// Populate // Populate
if (mongo.ObjectID.prototype.isPrototypeOf(noteWatching)) { if (isObjectId(noteWatching)) {
n = await NoteWatching.findOne({ n = await NoteWatching.findOne({
_id: noteWatching _id: noteWatching
}); });

View File

@ -2,6 +2,7 @@ import * as mongo from 'mongodb';
const deepcopy = require('deepcopy'); const deepcopy = require('deepcopy');
import rap from '@prezzemolo/rap'; import rap from '@prezzemolo/rap';
import db from '../db/mongodb'; import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
import { length } from 'stringz'; import { length } from 'stringz';
import { IUser, pack as packUser } from './user'; import { IUser, pack as packUser } from './user';
import { pack as packApp } from './app'; import { pack as packApp } from './app';
@ -107,7 +108,7 @@ export async function deleteNote(note: string | mongo.ObjectID | INote) {
let n: INote; let n: INote;
// Populate // Populate
if (mongo.ObjectID.prototype.isPrototypeOf(note)) { if (isObjectId(note)) {
n = await Note.findOne({ n = await Note.findOne({
_id: note _id: note
}); });
@ -259,7 +260,7 @@ export const pack = async (
// Me // Me
const meId: mongo.ObjectID = me const meId: mongo.ObjectID = me
? mongo.ObjectID.prototype.isPrototypeOf(me) ? isObjectId(me)
? me as mongo.ObjectID ? me as mongo.ObjectID
: typeof me === 'string' : typeof me === 'string'
? new mongo.ObjectID(me) ? new mongo.ObjectID(me)
@ -269,7 +270,7 @@ export const pack = async (
let _note: any; let _note: any;
// Populate the note if 'note' is ID // Populate the note if 'note' is ID
if (mongo.ObjectID.prototype.isPrototypeOf(note)) { if (isObjectId(note)) {
_note = await Note.findOne({ _note = await Note.findOne({
_id: note _id: note
}); });

View File

@ -1,6 +1,7 @@
import * as mongo from 'mongodb'; import * as mongo from 'mongodb';
const deepcopy = require('deepcopy'); const deepcopy = require('deepcopy');
import db from '../db/mongodb'; import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
import { IUser, pack as packUser } from './user'; import { IUser, pack as packUser } from './user';
import { pack as packNote } from './note'; import { pack as packNote } from './note';
@ -57,7 +58,7 @@ export async function deleteNotification(notification: string | mongo.ObjectID |
let n: INotification; let n: INotification;
// Populate // Populate
if (mongo.ObjectID.prototype.isPrototypeOf(notification)) { if (isObjectId(notification)) {
n = await Notification.findOne({ n = await Notification.findOne({
_id: notification _id: notification
}); });
@ -90,7 +91,7 @@ export const pack = (notification: any) => new Promise<any>(async (resolve, reje
let _notification: any; let _notification: any;
// Populate the notification if 'notification' is ID // Populate the notification if 'notification' is ID
if (mongo.ObjectID.prototype.isPrototypeOf(notification)) { if (isObjectId(notification)) {
_notification = await Notification.findOne({ _notification = await Notification.findOne({
_id: notification _id: notification
}); });

View File

@ -1,5 +1,6 @@
import * as mongo from 'mongodb'; import * as mongo from 'mongodb';
import db from '../db/mongodb'; import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
const PollVote = db.get<IPollVote>('pollVotes'); const PollVote = db.get<IPollVote>('pollVotes');
export default PollVote; export default PollVote;
@ -19,7 +20,7 @@ export async function deletePollVote(pollVote: string | mongo.ObjectID | IPollVo
let p: IPollVote; let p: IPollVote;
// Populate // Populate
if (mongo.ObjectID.prototype.isPrototypeOf(pollVote)) { if (isObjectId(pollVote)) {
p = await PollVote.findOne({ p = await PollVote.findOne({
_id: pollVote _id: pollVote
}); });

View File

@ -1,5 +1,6 @@
import * as mongo from 'mongodb'; import * as mongo from 'mongodb';
import db from '../db/mongodb'; import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
const SwSubscription = db.get<ISwSubscription>('swSubscriptions'); const SwSubscription = db.get<ISwSubscription>('swSubscriptions');
export default SwSubscription; export default SwSubscription;
@ -19,7 +20,7 @@ export async function deleteSwSubscription(swSubscription: string | mongo.Object
let s: ISwSubscription; let s: ISwSubscription;
// Populate // Populate
if (mongo.ObjectID.prototype.isPrototypeOf(swSubscription)) { if (isObjectId(swSubscription)) {
s = await SwSubscription.findOne({ s = await SwSubscription.findOne({
_id: swSubscription _id: swSubscription
}); });

View File

@ -1,6 +1,7 @@
import * as mongo from 'mongodb'; import * as mongo from 'mongodb';
const deepcopy = require('deepcopy'); const deepcopy = require('deepcopy');
import db from '../db/mongodb'; import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
const UserList = db.get<IUserList>('userList'); const UserList = db.get<IUserList>('userList');
export default UserList; export default UserList;
@ -20,7 +21,7 @@ export async function deleteUserList(userList: string | mongo.ObjectID | IUserLi
let u: IUserList; let u: IUserList;
// Populate // Populate
if (mongo.ObjectID.prototype.isPrototypeOf(userList)) { if (isObjectId(userList)) {
u = await UserList.findOne({ u = await UserList.findOne({
_id: userList _id: userList
}); });
@ -45,7 +46,7 @@ export const pack = (
) => new Promise<any>(async (resolve, reject) => { ) => new Promise<any>(async (resolve, reject) => {
let _userList: any; let _userList: any;
if (mongo.ObjectID.prototype.isPrototypeOf(userList)) { if (isObjectId(userList)) {
_userList = await UserList.findOne({ _userList = await UserList.findOne({
_id: userList _id: userList
}); });

View File

@ -3,6 +3,7 @@ const deepcopy = require('deepcopy');
const sequential = require('promise-sequential'); const sequential = require('promise-sequential');
import rap from '@prezzemolo/rap'; import rap from '@prezzemolo/rap';
import db from '../db/mongodb'; import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
import Note, { packMany as packNoteMany, deleteNote } from './note'; import Note, { packMany as packNoteMany, deleteNote } from './note';
import Following, { deleteFollowing } from './following'; import Following, { deleteFollowing } from './following';
import Mute, { deleteMute } from './mute'; import Mute, { deleteMute } from './mute';
@ -175,7 +176,7 @@ export async function deleteUser(user: string | mongo.ObjectID | IUser) {
let u: IUser; let u: IUser;
// Populate // Populate
if (mongo.ObjectID.prototype.isPrototypeOf(user)) { if (isObjectId(user)) {
u = await User.findOne({ u = await User.findOne({
_id: user _id: user
}); });
@ -340,7 +341,6 @@ export const pack = (
includeHasUnreadNotes?: boolean includeHasUnreadNotes?: boolean
} }
) => new Promise<any>(async (resolve, reject) => { ) => new Promise<any>(async (resolve, reject) => {
const opts = Object.assign({ const opts = Object.assign({
detail: false, detail: false,
includeSecrets: false includeSecrets: false
@ -358,7 +358,7 @@ export const pack = (
}; };
// Populate the user if 'user' is ID // Populate the user if 'user' is ID
if (mongo.ObjectID.prototype.isPrototypeOf(user)) { if (isObjectId(user)) {
_user = await User.findOne({ _user = await User.findOne({
_id: user _id: user
}, { fields }); }, { fields });
@ -378,7 +378,7 @@ export const pack = (
// Me // Me
const meId: mongo.ObjectID = me const meId: mongo.ObjectID = me
? mongo.ObjectID.prototype.isPrototypeOf(me) ? isObjectId(me)
? me as mongo.ObjectID ? me as mongo.ObjectID
: typeof me === 'string' : typeof me === 'string'
? new mongo.ObjectID(me) ? new mongo.ObjectID(me)

View File

@ -1,4 +1,5 @@
import * as mongo from 'mongodb'; import * as mongo from 'mongodb';
import isObjectId from '../../../misc/is-objectid';
import Message from '../../../models/messaging-message'; import Message from '../../../models/messaging-message';
import { IMessagingMessage as IMessage } from '../../../models/messaging-message'; import { IMessagingMessage as IMessage } from '../../../models/messaging-message';
import { publishMainStream } from '../../../stream'; import { publishMainStream } from '../../../stream';
@ -15,21 +16,21 @@ export default (
message: string | string[] | IMessage | IMessage[] | mongo.ObjectID | mongo.ObjectID[] message: string | string[] | IMessage | IMessage[] | mongo.ObjectID | mongo.ObjectID[]
) => new Promise<any>(async (resolve, reject) => { ) => new Promise<any>(async (resolve, reject) => {
const userId = mongo.ObjectID.prototype.isPrototypeOf(user) const userId = isObjectId(user)
? user ? user
: new mongo.ObjectID(user); : new mongo.ObjectID(user);
const otherpartyId = mongo.ObjectID.prototype.isPrototypeOf(otherparty) const otherpartyId = isObjectId(otherparty)
? otherparty ? otherparty
: new mongo.ObjectID(otherparty); : new mongo.ObjectID(otherparty);
const ids: mongo.ObjectID[] = Array.isArray(message) const ids: mongo.ObjectID[] = Array.isArray(message)
? mongo.ObjectID.prototype.isPrototypeOf(message[0]) ? isObjectId(message[0])
? (message as mongo.ObjectID[]) ? (message as mongo.ObjectID[])
: typeof message[0] === 'string' : typeof message[0] === 'string'
? (message as string[]).map(m => new mongo.ObjectID(m)) ? (message as string[]).map(m => new mongo.ObjectID(m))
: (message as IMessage[]).map(m => m._id) : (message as IMessage[]).map(m => m._id)
: mongo.ObjectID.prototype.isPrototypeOf(message) : isObjectId(message)
? [(message as mongo.ObjectID)] ? [(message as mongo.ObjectID)]
: typeof message === 'string' : typeof message === 'string'
? [new mongo.ObjectID(message)] ? [new mongo.ObjectID(message)]

View File

@ -1,4 +1,5 @@
import * as mongo from 'mongodb'; import * as mongo from 'mongodb';
import isObjectId from '../../../misc/is-objectid';
import { default as Notification, INotification } from '../../../models/notification'; import { default as Notification, INotification } from '../../../models/notification';
import { publishMainStream } from '../../../stream'; import { publishMainStream } from '../../../stream';
import Mute from '../../../models/mute'; import Mute from '../../../models/mute';
@ -12,17 +13,17 @@ export default (
message: string | string[] | INotification | INotification[] | mongo.ObjectID | mongo.ObjectID[] message: string | string[] | INotification | INotification[] | mongo.ObjectID | mongo.ObjectID[]
) => new Promise<any>(async (resolve, reject) => { ) => new Promise<any>(async (resolve, reject) => {
const userId = mongo.ObjectID.prototype.isPrototypeOf(user) const userId = isObjectId(user)
? user ? user
: new mongo.ObjectID(user); : new mongo.ObjectID(user);
const ids: mongo.ObjectID[] = Array.isArray(message) const ids: mongo.ObjectID[] = Array.isArray(message)
? mongo.ObjectID.prototype.isPrototypeOf(message[0]) ? isObjectId(message[0])
? (message as mongo.ObjectID[]) ? (message as mongo.ObjectID[])
: typeof message[0] === 'string' : typeof message[0] === 'string'
? (message as string[]).map(m => new mongo.ObjectID(m)) ? (message as string[]).map(m => new mongo.ObjectID(m))
: (message as INotification[]).map(m => m._id) : (message as INotification[]).map(m => m._id)
: mongo.ObjectID.prototype.isPrototypeOf(message) : isObjectId(message)
? [(message as mongo.ObjectID)] ? [(message as mongo.ObjectID)]
: typeof message === 'string' : typeof message === 'string'
? [new mongo.ObjectID(message)] ? [new mongo.ObjectID(message)]

View File

@ -1,4 +1,5 @@
import * as mongo from 'mongodb'; import * as mongo from 'mongodb';
import isObjectId from '../../misc/is-objectid';
import { publishMainStream } from '../../stream'; import { publishMainStream } from '../../stream';
import User from '../../models/user'; import User from '../../models/user';
import NoteUnread from '../../models/note-unread'; import NoteUnread from '../../models/note-unread';
@ -11,11 +12,11 @@ export default (
note: string | mongo.ObjectID note: string | mongo.ObjectID
) => new Promise<any>(async (resolve, reject) => { ) => new Promise<any>(async (resolve, reject) => {
const userId: mongo.ObjectID = mongo.ObjectID.prototype.isPrototypeOf(user) const userId: mongo.ObjectID = isObjectId(user)
? user as mongo.ObjectID ? user as mongo.ObjectID
: new mongo.ObjectID(user); : new mongo.ObjectID(user);
const noteId: mongo.ObjectID = mongo.ObjectID.prototype.isPrototypeOf(note) const noteId: mongo.ObjectID = isObjectId(note)
? note as mongo.ObjectID ? note as mongo.ObjectID
: new mongo.ObjectID(note); : new mongo.ObjectID(note);