summaryrefslogtreecommitdiff
path: root/src/api/endpoints/i
diff options
context:
space:
mode:
authorsyuilo⭐️ <Syuilotan@yahoo.co.jp>2017-03-03 19:54:40 +0900
committerGitHub <noreply@github.com>2017-03-03 19:54:40 +0900
commit3ce6601f0436da23589384990dfb6c12cec5a5b4 (patch)
treeb7b9cc14d9787f06c72d013bc25690a9470e6bbe /src/api/endpoints/i
parentfix(package): update whatwg-fetch to version 2.0.3 (diff)
parentdone (diff)
downloadmisskey-3ce6601f0436da23589384990dfb6c12cec5a5b4.tar.gz
misskey-3ce6601f0436da23589384990dfb6c12cec5a5b4.tar.bz2
misskey-3ce6601f0436da23589384990dfb6c12cec5a5b4.zip
Merge pull request #232 from syuilo/#226
#226、あとTypeScriptにした
Diffstat (limited to 'src/api/endpoints/i')
-rw-r--r--src/api/endpoints/i/appdata/get.ts (renamed from src/api/endpoints/i/appdata/get.js)0
-rw-r--r--src/api/endpoints/i/appdata/set.ts (renamed from src/api/endpoints/i/appdata/set.js)0
-rw-r--r--src/api/endpoints/i/authorized_apps.ts (renamed from src/api/endpoints/i/authorized_apps.js)26
-rw-r--r--src/api/endpoints/i/favorites.ts (renamed from src/api/endpoints/i/favorites.js)31
-rw-r--r--src/api/endpoints/i/notifications.ts (renamed from src/api/endpoints/i/notifications.js)50
-rw-r--r--src/api/endpoints/i/signin_history.ts (renamed from src/api/endpoints/i/signin_history.js)34
-rw-r--r--src/api/endpoints/i/update.js119
-rw-r--r--src/api/endpoints/i/update.ts91
8 files changed, 145 insertions, 206 deletions
diff --git a/src/api/endpoints/i/appdata/get.js b/src/api/endpoints/i/appdata/get.ts
index 7f1bdf0713..7f1bdf0713 100644
--- a/src/api/endpoints/i/appdata/get.js
+++ b/src/api/endpoints/i/appdata/get.ts
diff --git a/src/api/endpoints/i/appdata/set.js b/src/api/endpoints/i/appdata/set.ts
index 57001f4e8b..57001f4e8b 100644
--- a/src/api/endpoints/i/appdata/set.js
+++ b/src/api/endpoints/i/appdata/set.ts
diff --git a/src/api/endpoints/i/authorized_apps.js b/src/api/endpoints/i/authorized_apps.ts
index 3c0cf75057..fb56a107e7 100644
--- a/src/api/endpoints/i/authorized_apps.js
+++ b/src/api/endpoints/i/authorized_apps.ts
@@ -3,7 +3,7 @@
/**
* Module dependencies
*/
-import * as mongo from 'mongodb';
+import it from '../../it';
import AccessToken from '../../models/access-token';
import serialize from '../../serializers/app';
@@ -18,28 +18,16 @@ module.exports = (params, user) =>
new Promise(async (res, rej) =>
{
// Get 'limit' parameter
- let limit = params.limit;
- if (limit !== undefined && limit !== null) {
- limit = parseInt(limit, 10);
-
- // From 1 to 100
- if (!(1 <= limit && limit <= 100)) {
- return rej('invalid limit range');
- }
- } else {
- limit = 10;
- }
+ const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed();
+ if (limitErr) return rej('invalid limit param');
// Get 'offset' parameter
- let offset = params.offset;
- if (offset !== undefined && offset !== null) {
- offset = parseInt(offset, 10);
- } else {
- offset = 0;
- }
+ const [offset, offsetErr] = it(params.offset).expect.number().min(0).default(0).qed();
+ if (offsetErr) return rej('invalid offset param');
// Get 'sort' parameter
- let sort = params.sort || 'desc';
+ const [sort, sortError] = it(params.sort).expect.string().or('desc asc').default('desc').qed();
+ if (sortError) return rej('invalid sort param');
// Get tokens
const tokens = await AccessToken
diff --git a/src/api/endpoints/i/favorites.js b/src/api/endpoints/i/favorites.ts
index 28e402e366..c04d318379 100644
--- a/src/api/endpoints/i/favorites.js
+++ b/src/api/endpoints/i/favorites.ts
@@ -3,7 +3,7 @@
/**
* Module dependencies
*/
-import * as mongo from 'mongodb';
+import it from '../../it';
import Favorite from '../../models/favorite';
import serialize from '../../serializers/post';
@@ -11,37 +11,26 @@ import serialize from '../../serializers/post';
* Get followers of a user
*
* @param {any} params
+ * @param {any} user
* @return {Promise<any>}
*/
-module.exports = (params) =>
+module.exports = (params, user) =>
new Promise(async (res, rej) =>
{
// Get 'limit' parameter
- let limit = params.limit;
- if (limit !== undefined && limit !== null) {
- limit = parseInt(limit, 10);
-
- // From 1 to 100
- if (!(1 <= limit && limit <= 100)) {
- return rej('invalid limit range');
- }
- } else {
- limit = 10;
- }
+ const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed();
+ if (limitErr) return rej('invalid limit param');
// Get 'offset' parameter
- let offset = params.offset;
- if (offset !== undefined && offset !== null) {
- offset = parseInt(offset, 10);
- } else {
- offset = 0;
- }
+ const [offset, offsetErr] = it(params.offset).expect.number().min(0).default(0).qed();
+ if (offsetErr) return rej('invalid offset param');
// Get 'sort' parameter
- let sort = params.sort || 'desc';
+ const [sort, sortError] = it(params.sort).expect.string().or('desc asc').default('desc').qed();
+ if (sortError) return rej('invalid sort param');
// Get favorites
- const favorites = await Favorites
+ const favorites = await Favorite
.find({
user_id: user._id
}, {
diff --git a/src/api/endpoints/i/notifications.js b/src/api/endpoints/i/notifications.ts
index d5174439e2..21537ea799 100644
--- a/src/api/endpoints/i/notifications.js
+++ b/src/api/endpoints/i/notifications.ts
@@ -3,7 +3,7 @@
/**
* Module dependencies
*/
-import * as mongo from 'mongodb';
+import it from '../../it';
import Notification from '../../models/notification';
import serialize from '../../serializers/notification';
import getFriends from '../../common/get-friends';
@@ -19,44 +19,38 @@ module.exports = (params, user) =>
new Promise(async (res, rej) =>
{
// Get 'following' parameter
- const following = params.following;
+ const [following, followingError] =
+ it(params.following).expect.boolean().default(false).qed();
+ if (followingError) return rej('invalid following param');
// Get 'mark_as_read' parameter
- let markAsRead = params.mark_as_read;
- if (markAsRead == null) {
- markAsRead = true;
- }
+ const [markAsRead, markAsReadErr] = it(params.mark_as_read).expect.boolean().default(true).qed();
+ if (markAsReadErr) return rej('invalid mark_as_read param');
// Get 'type' parameter
- let type = params.type;
- if (type !== undefined && type !== null) {
- type = type.split(',').map(x => x.trim());
- }
+ const [type, typeErr] = it(params.type).expect.array().unique().allString().qed();
+ if (typeErr) return rej('invalid type param');
// Get 'limit' parameter
- let limit = params.limit;
- if (limit !== undefined && limit !== null) {
- limit = parseInt(limit, 10);
+ const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed();
+ if (limitErr) return rej('invalid limit param');
- // From 1 to 100
- if (!(1 <= limit && limit <= 100)) {
- return rej('invalid limit range');
- }
- } else {
- limit = 10;
- }
+ // Get 'since_id' parameter
+ const [sinceId, sinceIdErr] = it(params.since_id).expect.id().qed();
+ if (sinceIdErr) return rej('invalid since_id param');
- const since = params.since_id || null;
- const max = params.max_id || null;
+ // Get 'max_id' parameter
+ 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
- if (since !== null && max !== null) {
+ if (sinceId !== null && maxId !== null) {
return rej('cannot set since_id and max_id');
}
const query = {
notifiee_id: user._id
- };
+ } as any;
const sort = {
_id: -1
@@ -77,14 +71,14 @@ module.exports = (params, user) =>
};
}
- if (since !== null) {
+ if (sinceId) {
sort._id = 1;
query._id = {
- $gt: new mongo.ObjectID(since)
+ $gt: sinceId
};
- } else if (max !== null) {
+ } else if (maxId) {
query._id = {
- $lt: new mongo.ObjectID(max)
+ $lt: maxId
};
}
diff --git a/src/api/endpoints/i/signin_history.js b/src/api/endpoints/i/signin_history.ts
index ede821e3cf..db36438bfe 100644
--- a/src/api/endpoints/i/signin_history.js
+++ b/src/api/endpoints/i/signin_history.ts
@@ -3,7 +3,7 @@
/**
* Module dependencies
*/
-import * as mongo from 'mongodb';
+import it from '../../it';
import Signin from '../../models/signin';
import serialize from '../../serializers/signin';
@@ -18,42 +18,38 @@ module.exports = (params, user) =>
new Promise(async (res, rej) =>
{
// Get 'limit' parameter
- let limit = params.limit;
- if (limit !== undefined && limit !== null) {
- limit = parseInt(limit, 10);
+ const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed();
+ if (limitErr) return rej('invalid limit param');
- // From 1 to 100
- if (!(1 <= limit && limit <= 100)) {
- return rej('invalid limit range');
- }
- } else {
- limit = 10;
- }
+ // Get 'since_id' parameter
+ const [sinceId, sinceIdErr] = it(params.since_id).expect.id().qed();
+ if (sinceIdErr) return rej('invalid since_id param');
- const since = params.since_id || null;
- const max = params.max_id || null;
+ // Get 'max_id' parameter
+ 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
- if (since !== null && max !== null) {
+ if (sinceId !== null && maxId !== null) {
return rej('cannot set since_id and max_id');
}
const query = {
user_id: user._id
- };
+ } as any;
const sort = {
_id: -1
};
- if (since !== null) {
+ if (sinceId) {
sort._id = 1;
query._id = {
- $gt: new mongo.ObjectID(since)
+ $gt: sinceId
};
- } else if (max !== null) {
+ } else if (maxId) {
query._id = {
- $lt: new mongo.ObjectID(max)
+ $lt: maxId
};
}
diff --git a/src/api/endpoints/i/update.js b/src/api/endpoints/i/update.js
deleted file mode 100644
index 4abb4fcb7b..0000000000
--- a/src/api/endpoints/i/update.js
+++ /dev/null
@@ -1,119 +0,0 @@
-'use strict';
-
-/**
- * Module dependencies
- */
-import * as mongo from 'mongodb';
-import User from '../../models/user';
-import { isValidName, isValidBirthday } from '../../models/user';
-import serialize from '../../serializers/user';
-import event from '../../event';
-import config from '../../../conf';
-
-/**
- * Update myself
- *
- * @param {any} params
- * @param {any} user
- * @param {any} _
- * @param {boolean} isSecure
- * @return {Promise<any>}
- */
-module.exports = async (params, user, _, isSecure) =>
- new Promise(async (res, rej) =>
-{
- // Get 'name' parameter
- const name = params.name;
- if (name !== undefined && name !== null) {
- if (typeof name != 'string') {
- return rej('name must be a string');
- }
-
- if (!isValidName(name)) {
- return rej('invalid name');
- }
-
- user.name = name;
- }
-
- // Get 'description' parameter
- const description = params.description;
- if (description !== undefined && description !== null) {
- if (description.length > 500) {
- return rej('too long description');
- }
-
- user.description = description;
- }
-
- // Get 'location' parameter
- const location = params.location;
- if (location !== undefined && location !== null) {
- if (location.length > 50) {
- return rej('too long location');
- }
-
- user.profile.location = location;
- }
-
- // Get 'birthday' parameter
- const birthday = params.birthday;
- if (birthday != null) {
- if (!isValidBirthday(birthday)) {
- return rej('invalid birthday');
- }
-
- user.profile.birthday = birthday;
- } else {
- user.profile.birthday = null;
- }
-
- // Get 'avatar_id' parameter
- const avatar = params.avatar_id;
- if (avatar !== undefined && avatar !== null) {
- user.avatar_id = new mongo.ObjectID(avatar);
- }
-
- // Get 'banner_id' parameter
- const banner = params.banner_id;
- if (banner !== undefined && banner !== null) {
- user.banner_id = new mongo.ObjectID(banner);
- }
-
- await User.update(user._id, {
- $set: {
- name: user.name,
- description: user.description,
- avatar_id: user.avatar_id,
- banner_id: user.banner_id,
- profile: user.profile
- }
- });
-
- // Serialize
- const iObj = await serialize(user, user, {
- detail: true,
- includeSecrets: isSecure
- });
-
- // Send response
- res(iObj);
-
- // Publish i updated event
- event(user._id, 'i_updated', iObj);
-
- // Update search index
- if (config.elasticsearch.enable) {
- const es = require('../../../db/elasticsearch');
-
- es.index({
- index: 'misskey',
- type: 'user',
- id: user._id.toString(),
- body: {
- name: user.name,
- bio: user.bio
- }
- });
- }
-});
diff --git a/src/api/endpoints/i/update.ts b/src/api/endpoints/i/update.ts
new file mode 100644
index 0000000000..a5f1538610
--- /dev/null
+++ b/src/api/endpoints/i/update.ts
@@ -0,0 +1,91 @@
+'use strict';
+
+/**
+ * Module dependencies
+ */
+import it from '../../it';
+import User from '../../models/user';
+import { isValidName, isValidDescription, isValidLocation, isValidBirthday } from '../../models/user';
+import serialize from '../../serializers/user';
+import event from '../../event';
+import config from '../../../conf';
+
+/**
+ * Update myself
+ *
+ * @param {any} params
+ * @param {any} user
+ * @param {any} _
+ * @param {boolean} isSecure
+ * @return {Promise<any>}
+ */
+module.exports = async (params, user, _, isSecure) =>
+ new Promise(async (res, rej) =>
+{
+ // Get 'name' parameter
+ const [name, nameErr] = it(params.name).expect.string().validate(isValidName).qed();
+ if (nameErr) return rej('invalid name param');
+ if (name) user.name = name;
+
+ // Get 'description' parameter
+ const [description, descriptionErr] = it(params.description).expect.nullable.string().validate(isValidDescription).qed();
+ if (descriptionErr) return rej('invalid description param');
+ if (description !== undefined) user.description = description;
+
+ // Get 'location' parameter
+ const [location, locationErr] = it(params.location).expect.nullable.string().validate(isValidLocation).qed();
+ if (locationErr) return rej('invalid location param');
+ if (location !== undefined) user.location = location;
+
+ // Get 'birthday' parameter
+ const [birthday, birthdayErr] = it(params.birthday).expect.nullable.string().validate(isValidBirthday).qed();
+ if (birthdayErr) return rej('invalid birthday param');
+ if (birthday !== undefined) user.birthday = birthday;
+
+ // Get 'avatar_id' parameter
+ const [avatarId, avatarIdErr] = it(params.avatar_id).expect.id().notNull().qed();
+ if (avatarIdErr) return rej('invalid avatar_id param');
+ if (avatarId) user.avatar_id = avatarId;
+
+ // Get 'banner_id' parameter
+ const [bannerId, bannerIdErr] = it(params.banner_id).expect.id().notNull().qed();
+ if (bannerIdErr) return rej('invalid banner_id param');
+ if (bannerId) user.banner_id = bannerId;
+
+ await User.update(user._id, {
+ $set: {
+ name: user.name,
+ description: user.description,
+ avatar_id: user.avatar_id,
+ banner_id: user.banner_id,
+ profile: user.profile
+ }
+ });
+
+ // Serialize
+ const iObj = await serialize(user, user, {
+ detail: true,
+ includeSecrets: isSecure
+ });
+
+ // Send response
+ res(iObj);
+
+ // Publish i updated event
+ event(user._id, 'i_updated', iObj);
+
+ // Update search index
+ if (config.elasticsearch.enable) {
+ const es = require('../../../db/elasticsearch');
+
+ es.index({
+ index: 'misskey',
+ type: 'user',
+ id: user._id.toString(),
+ body: {
+ name: user.name,
+ bio: user.bio
+ }
+ });
+ }
+});