This commit is contained in:
syuilo 2017-03-03 08:24:48 +09:00
parent 67f9c158d7
commit 2a9cba25a8
9 changed files with 77 additions and 126 deletions

View File

@ -3,7 +3,7 @@
/** /**
* Module dependencies * Module dependencies
*/ */
import * as mongo from 'mongodb'; import it from '../../it';
import History from '../../models/messaging-history'; import History from '../../models/messaging-history';
import serialize from '../../serializers/messaging-message'; import serialize from '../../serializers/messaging-message';
@ -18,17 +18,8 @@ module.exports = (params, user) =>
new Promise(async (res, rej) => new Promise(async (res, rej) =>
{ {
// Get 'limit' parameter // Get 'limit' parameter
let limit = params.limit; const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed();
if (limit !== undefined && limit !== null) { if (limitErr) return rej('invalid limit param');
limit = parseInt(limit, 10);
// From 1 to 100
if (!(1 <= limit && limit <= 100)) {
return rej('invalid limit range');
}
} else {
limit = 10;
}
// Get history // Get history
const history = await History const history = await History

View File

@ -3,7 +3,7 @@
/** /**
* Module dependencies * Module dependencies
*/ */
import * as mongo from 'mongodb'; import it from '../../it';
import Message from '../../models/messaging-message'; import Message from '../../models/messaging-message';
import User from '../../models/user'; import User from '../../models/user';
import serialize from '../../serializers/messaging-message'; import serialize from '../../serializers/messaging-message';
@ -21,10 +21,12 @@ module.exports = (params, user) =>
new Promise(async (res, rej) => new Promise(async (res, rej) =>
{ {
// Get 'user_id' parameter // Get 'user_id' parameter
let recipient = params.user_id; const [recipientId, recipientIdErr] = it(params.user_id).expect.id().required().qed();
if (recipient !== undefined && recipient !== null) { if (recipientIdErr) return rej('invalid user_id param');
recipient = await User.findOne({
_id: new mongo.ObjectID(recipient) // Fetch recipient
const recipient = await User.findOne({
_id: recipientId
}, { }, {
fields: { fields: {
_id: true _id: true
@ -34,34 +36,25 @@ module.exports = (params, user) =>
if (recipient === null) { if (recipient === null) {
return rej('user not found'); return rej('user not found');
} }
} else {
return rej('user_id is required');
}
// Get 'mark_as_read' parameter // Get 'mark_as_read' parameter
let markAsRead = params.mark_as_read; const [markAsRead, markAsReadErr] = it(params.mark_as_read).expect.boolean().default(true).qed();
if (markAsRead == null) { if (markAsReadErr) return rej('invalid mark_as_read param');
markAsRead = true;
}
// Get 'limit' parameter // Get 'limit' parameter
let limit = params.limit; const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed();
if (limit !== undefined && limit !== null) { if (limitErr) return rej('invalid limit param');
limit = parseInt(limit, 10);
// From 1 to 100 // Get 'since_id' parameter
if (!(1 <= limit && limit <= 100)) { const [sinceId, sinceIdErr] = it(params.since_id).expect.id().qed();
return rej('invalid limit range'); if (sinceIdErr) return rej('invalid since_id param');
}
} else {
limit = 10;
}
const since = params.since_id || null; // Get 'max_id' parameter
const max = params.max_id || null; const [maxId, maxIdErr] = it(params.max_id).expect.id().qed();
if (maxIdErr) return rej('invalid max_id param');
// Check if both of since_id and max_id is specified // Check if both of since_id and max_id is specified
if (since !== null && max !== null) { if (sinceId !== null && maxId !== null) {
return rej('cannot set since_id and max_id'); return rej('cannot set since_id and max_id');
} }
@ -73,20 +66,20 @@ module.exports = (params, user) =>
user_id: recipient._id, user_id: recipient._id,
recipient_id: user._id recipient_id: user._id
}] }]
}; } as any;
const sort = { const sort = {
_id: -1 _id: -1
}; };
if (since !== null) { if (sinceId) {
sort._id = 1; sort._id = 1;
query._id = { query._id = {
$gt: new mongo.ObjectID(since) $gt: sinceId
}; };
} else if (max !== null) { } else if (maxId) {
query._id = { query._id = {
$lt: new mongo.ObjectID(max) $lt: maxId
}; };
} }

View File

@ -3,8 +3,9 @@
/** /**
* Module dependencies * Module dependencies
*/ */
import * as mongo from 'mongodb'; import it from '../../../it';
import Message from '../../../models/messaging-message'; import Message from '../../../models/messaging-message';
import { isValidText } from '../../../models/messaging-message';
import History from '../../../models/messaging-history'; import History from '../../../models/messaging-history';
import User from '../../../models/user'; import User from '../../../models/user';
import DriveFile from '../../../models/drive-file'; import DriveFile from '../../../models/drive-file';
@ -13,11 +14,6 @@ import publishUserStream from '../../../event';
import { publishMessagingStream } from '../../../event'; import { publishMessagingStream } from '../../../event';
import config from '../../../../conf'; import config from '../../../../conf';
/**
*
*/
const maxTextLength = 500;
/** /**
* Create a message * Create a message
* *
@ -29,24 +25,17 @@ module.exports = (params, user) =>
new Promise(async (res, rej) => new Promise(async (res, rej) =>
{ {
// Get 'user_id' parameter // Get 'user_id' parameter
let recipient = params.user_id; const [recipientId, recipientIdErr] = it(params.user_id).expect.id().required().qed();
if (recipient !== undefined && recipient !== null) { if (recipientIdErr) return rej('invalid user_id param');
if (typeof recipient != 'string') {
return rej('user_id must be a string');
}
// Validate id
if (!mongo.ObjectID.isValid(recipient)) {
return rej('incorrect user_id');
}
// Myself // Myself
if (new mongo.ObjectID(recipient).equals(user._id)) { if (recipientId.equals(user._id)) {
return rej('cannot send message to myself'); return rej('cannot send message to myself');
} }
recipient = await User.findOne({ // Fetch recipient
_id: new mongo.ObjectID(recipient) const recipient = await User.findOne({
_id: recipientId
}, { }, {
fields: { fields: {
_id: true _id: true
@ -56,28 +45,19 @@ module.exports = (params, user) =>
if (recipient === null) { if (recipient === null) {
return rej('user not found'); return rej('user not found');
} }
} else {
return rej('user_id is required');
}
// Get 'text' parameter // Get 'text' parameter
let text = params.text; const [text, textErr] = it(params.text).expect.string().validate(isValidText).qed();
if (text !== undefined && text !== null) { if (textErr) return rej('invalid text');
text = text.trim();
if (text.length === 0) {
text = null;
} else if (text.length > maxTextLength) {
return rej('too long text');
}
} else {
text = null;
}
// Get 'file_id' parameter // Get 'file_id' parameter
let file = params.file_id; const [fileId, fileIdErr] = it(params.file_id).expect.id().qed();
if (file !== undefined && file !== null) { if (fileIdErr) return rej('invalid file_id param');
let file = null;
if (fileId !== null) {
file = await DriveFile.findOne({ file = await DriveFile.findOne({
_id: new mongo.ObjectID(file), _id: fileId,
user_id: user._id user_id: user._id
}, { }, {
data: false data: false
@ -86,8 +66,6 @@ module.exports = (params, user) =>
if (file === null) { if (file === null) {
return rej('file not found'); return rej('file not found');
} }
} else {
file = null;
} }
// テキストが無いかつ添付ファイルも無かったらエラー // テキストが無いかつ添付ファイルも無かったらエラー

View File

@ -3,7 +3,7 @@
/** /**
* Module dependencies * Module dependencies
*/ */
import * as mongo from 'mongodb'; import it from '../../it';
import App from '../../models/app'; import App from '../../models/app';
import serialize from '../../serializers/app'; import serialize from '../../serializers/app';
@ -18,25 +18,12 @@ module.exports = (params, user) =>
new Promise(async (res, rej) => new Promise(async (res, rej) =>
{ {
// Get 'limit' parameter // Get 'limit' parameter
let limit = params.limit; const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed();
if (limit !== undefined && limit !== null) { if (limitErr) return rej('invalid limit param');
limit = parseInt(limit, 10);
// From 1 to 100
if (!(1 <= limit && limit <= 100)) {
return rej('invalid limit range');
}
} else {
limit = 10;
}
// Get 'offset' parameter // Get 'offset' parameter
let offset = params.offset; const [offset, offsetErr] = it(params.offset).expect.number().min(0).default(0).qed();
if (offset !== undefined && offset !== null) { if (offsetErr) return rej('invalid offset param');
offset = parseInt(offset, 10);
} else {
offset = 0;
}
const query = { const query = {
user_id: user._id user_id: user._id

View File

@ -3,10 +3,10 @@
/** /**
* Module dependencies * Module dependencies
*/ */
import * as mongo from 'mongodb'; import it from '../../it';
import Notification from '../../../models/notification'; import Notification from '../../models/notification';
import serialize from '../../../serializers/notification'; import serialize from '../../serializers/notification';
import event from '../../../event'; import event from '../../event';
/** /**
* Mark as read a notification * Mark as read a notification
@ -17,16 +17,13 @@ import event from '../../../event';
*/ */
module.exports = (params, user) => module.exports = (params, user) =>
new Promise(async (res, rej) => { new Promise(async (res, rej) => {
const notificationId = params.notification; const [notificationId, notificationIdErr] = it(params.notification_id).expect.id().required().qed();
if (notificationIdErr) return rej('invalid notification_id param');
if (notificationId === undefined || notificationId === null) {
return rej('notification is required');
}
// Get notification // Get notification
const notification = await Notification const notification = await Notification
.findOne({ .findOne({
_id: new mongo.ObjectID(notificationId), _id: notificationId,
i: user._id i: user._id
}); });

View File

@ -424,7 +424,7 @@ class IdQuery extends QueryCore {
/** /**
* *
*/ */
qed(): [any[], Error] { qed(): [mongo.ObjectID, Error] {
return super.qed(); return super.qed();
} }
@ -433,7 +433,7 @@ class IdQuery extends QueryCore {
* false * false
* @param validator * @param validator
*/ */
validate(validator: Validator<any[]>) { validate(validator: Validator<mongo.ObjectID>) {
return super.validate(validator); return super.validate(validator);
} }
} }

View File

@ -1,3 +1,8 @@
import db from '../../db/mongodb'; import db from '../../db/mongodb';
export default db.get('messaging_messages') as any; // fuck type definition export default db.get('messaging_messages') as any; // fuck type definition
export function isValidText(text: string): boolean {
return text.length <= 1000 && text.trim() != '';
}

View File

@ -19,7 +19,7 @@ import deepcopy = require('deepcopy');
*/ */
export default ( export default (
message: any, message: any,
me: any, me?: any,
options?: { options?: {
populateRecipient: boolean populateRecipient: boolean
} }