2019-01-19 19:07:12 +01:00
|
|
|
import * as mongo from 'mongodb';
|
2019-02-22 03:46:58 +01:00
|
|
|
import Note from '../../../models/note';
|
|
|
|
import User, { isRemoteUser, isLocalUser } from '../../../models/user';
|
|
|
|
import { IdentifiableError } from '../../../misc/identifiable-error';
|
2019-01-19 19:07:12 +01:00
|
|
|
|
|
|
|
/**
|
2019-02-22 06:02:56 +01:00
|
|
|
* Get note for API processing
|
2019-01-19 19:07:12 +01:00
|
|
|
*/
|
2019-02-22 06:02:56 +01:00
|
|
|
export async function getNote(noteId: mongo.ObjectID) {
|
2019-01-19 19:07:12 +01:00
|
|
|
const note = await Note.findOne({
|
|
|
|
_id: noteId,
|
|
|
|
deletedAt: { $exists: false }
|
|
|
|
});
|
|
|
|
|
|
|
|
if (note === null) {
|
2019-02-22 03:46:58 +01:00
|
|
|
throw new IdentifiableError('9725d0ce-ba28-4dde-95a7-2cbb2c15de24', 'No such note.');
|
2019-01-19 19:07:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return note;
|
|
|
|
}
|
2019-01-26 09:53:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get user for API processing
|
|
|
|
*/
|
|
|
|
export async function getUser(userId: mongo.ObjectID) {
|
|
|
|
const user = await User.findOne({
|
2019-02-22 06:02:56 +01:00
|
|
|
_id: userId,
|
2019-02-22 06:09:24 +01:00
|
|
|
$or: [{
|
|
|
|
isDeleted: { $exists: false }
|
|
|
|
}, {
|
|
|
|
isDeleted: false
|
|
|
|
}]
|
2019-02-22 06:02:56 +01:00
|
|
|
}, {
|
|
|
|
fields: {
|
|
|
|
data: false,
|
|
|
|
profile: false,
|
|
|
|
clientSettings: false
|
|
|
|
}
|
2019-01-26 09:53:35 +01:00
|
|
|
});
|
|
|
|
|
2019-02-22 06:02:56 +01:00
|
|
|
if (user === null) {
|
2019-02-22 03:46:58 +01:00
|
|
|
throw new IdentifiableError('15348ddd-432d-49c2-8a5a-8069753becff', 'No such user.');
|
2019-01-26 09:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get remote user for API processing
|
|
|
|
*/
|
|
|
|
export async function getRemoteUser(userId: mongo.ObjectID) {
|
|
|
|
const user = await getUser(userId);
|
|
|
|
|
|
|
|
if (!isRemoteUser(user)) {
|
|
|
|
throw 'user is not a remote user';
|
|
|
|
}
|
|
|
|
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get local user for API processing
|
|
|
|
*/
|
|
|
|
export async function getLocalUser(userId: mongo.ObjectID) {
|
|
|
|
const user = await getUser(userId);
|
|
|
|
|
|
|
|
if (!isLocalUser(user)) {
|
|
|
|
throw 'user is not a local user';
|
|
|
|
}
|
|
|
|
|
|
|
|
return user;
|
|
|
|
}
|