Introduce processor
This commit is contained in:
37
src/server/api/endpoints/i/2fa/done.ts
Normal file
37
src/server/api/endpoints/i/2fa/done.ts
Normal file
@ -0,0 +1,37 @@
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
import $ from 'cafy';
|
||||
import * as speakeasy from 'speakeasy';
|
||||
import User from '../../../models/user';
|
||||
|
||||
module.exports = async (params, user) => new Promise(async (res, rej) => {
|
||||
// Get 'token' parameter
|
||||
const [token, tokenErr] = $(params.token).string().$;
|
||||
if (tokenErr) return rej('invalid token param');
|
||||
|
||||
const _token = token.replace(/\s/g, '');
|
||||
|
||||
if (user.two_factor_temp_secret == null) {
|
||||
return rej('二段階認証の設定が開始されていません');
|
||||
}
|
||||
|
||||
const verified = (speakeasy as any).totp.verify({
|
||||
secret: user.two_factor_temp_secret,
|
||||
encoding: 'base32',
|
||||
token: _token
|
||||
});
|
||||
|
||||
if (!verified) {
|
||||
return rej('not verified');
|
||||
}
|
||||
|
||||
await User.update(user._id, {
|
||||
$set: {
|
||||
'account.two_factor_secret': user.two_factor_temp_secret,
|
||||
'account.two_factor_enabled': true
|
||||
}
|
||||
});
|
||||
|
||||
res();
|
||||
});
|
48
src/server/api/endpoints/i/2fa/register.ts
Normal file
48
src/server/api/endpoints/i/2fa/register.ts
Normal file
@ -0,0 +1,48 @@
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
import $ from 'cafy';
|
||||
import * as bcrypt from 'bcryptjs';
|
||||
import * as speakeasy from 'speakeasy';
|
||||
import * as QRCode from 'qrcode';
|
||||
import User from '../../../models/user';
|
||||
import config from '../../../../../conf';
|
||||
|
||||
module.exports = async (params, user) => new Promise(async (res, rej) => {
|
||||
// Get 'password' parameter
|
||||
const [password, passwordErr] = $(params.password).string().$;
|
||||
if (passwordErr) return rej('invalid password param');
|
||||
|
||||
// Compare password
|
||||
const same = await bcrypt.compare(password, user.account.password);
|
||||
|
||||
if (!same) {
|
||||
return rej('incorrect password');
|
||||
}
|
||||
|
||||
// Generate user's secret key
|
||||
const secret = speakeasy.generateSecret({
|
||||
length: 32
|
||||
});
|
||||
|
||||
await User.update(user._id, {
|
||||
$set: {
|
||||
two_factor_temp_secret: secret.base32
|
||||
}
|
||||
});
|
||||
|
||||
// Get the data URL of the authenticator URL
|
||||
QRCode.toDataURL(speakeasy.otpauthURL({
|
||||
secret: secret.base32,
|
||||
encoding: 'base32',
|
||||
label: user.username,
|
||||
issuer: config.host
|
||||
}), (err, data_url) => {
|
||||
res({
|
||||
qr: data_url,
|
||||
secret: secret.base32,
|
||||
label: user.username,
|
||||
issuer: config.host
|
||||
});
|
||||
});
|
||||
});
|
28
src/server/api/endpoints/i/2fa/unregister.ts
Normal file
28
src/server/api/endpoints/i/2fa/unregister.ts
Normal file
@ -0,0 +1,28 @@
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
import $ from 'cafy';
|
||||
import * as bcrypt from 'bcryptjs';
|
||||
import User from '../../../models/user';
|
||||
|
||||
module.exports = async (params, user) => new Promise(async (res, rej) => {
|
||||
// Get 'password' parameter
|
||||
const [password, passwordErr] = $(params.password).string().$;
|
||||
if (passwordErr) return rej('invalid password param');
|
||||
|
||||
// Compare password
|
||||
const same = await bcrypt.compare(password, user.account.password);
|
||||
|
||||
if (!same) {
|
||||
return rej('incorrect password');
|
||||
}
|
||||
|
||||
await User.update(user._id, {
|
||||
$set: {
|
||||
'account.two_factor_secret': null,
|
||||
'account.two_factor_enabled': false
|
||||
}
|
||||
});
|
||||
|
||||
res();
|
||||
});
|
39
src/server/api/endpoints/i/appdata/get.ts
Normal file
39
src/server/api/endpoints/i/appdata/get.ts
Normal file
@ -0,0 +1,39 @@
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
import $ from 'cafy';
|
||||
import Appdata from '../../../models/appdata';
|
||||
|
||||
/**
|
||||
* Get app data
|
||||
*
|
||||
* @param {any} params
|
||||
* @param {any} user
|
||||
* @param {any} app
|
||||
* @param {Boolean} isSecure
|
||||
* @return {Promise<any>}
|
||||
*/
|
||||
module.exports = (params, user, app) => new Promise(async (res, rej) => {
|
||||
if (app == null) return rej('このAPIはサードパーティAppからのみ利用できます');
|
||||
|
||||
// Get 'key' parameter
|
||||
const [key = null, keyError] = $(params.key).optional.nullable.string().match(/[a-z_]+/).$;
|
||||
if (keyError) return rej('invalid key param');
|
||||
|
||||
const select = {};
|
||||
if (key !== null) {
|
||||
select[`data.${key}`] = true;
|
||||
}
|
||||
const appdata = await Appdata.findOne({
|
||||
app_id: app._id,
|
||||
user_id: user._id
|
||||
}, {
|
||||
fields: select
|
||||
});
|
||||
|
||||
if (appdata) {
|
||||
res(appdata.data);
|
||||
} else {
|
||||
res();
|
||||
}
|
||||
});
|
58
src/server/api/endpoints/i/appdata/set.ts
Normal file
58
src/server/api/endpoints/i/appdata/set.ts
Normal file
@ -0,0 +1,58 @@
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
import $ from 'cafy';
|
||||
import Appdata from '../../../models/appdata';
|
||||
|
||||
/**
|
||||
* Set app data
|
||||
*
|
||||
* @param {any} params
|
||||
* @param {any} user
|
||||
* @param {any} app
|
||||
* @param {Boolean} isSecure
|
||||
* @return {Promise<any>}
|
||||
*/
|
||||
module.exports = (params, user, app) => new Promise(async (res, rej) => {
|
||||
if (app == null) return rej('このAPIはサードパーティAppからのみ利用できます');
|
||||
|
||||
// Get 'data' parameter
|
||||
const [data, dataError] = $(params.data).optional.object()
|
||||
.pipe(obj => {
|
||||
const hasInvalidData = Object.entries(obj).some(([k, v]) =>
|
||||
$(k).string().match(/^[a-z_]+$/).nok() && $(v).string().nok());
|
||||
return !hasInvalidData;
|
||||
}).$;
|
||||
if (dataError) return rej('invalid data param');
|
||||
|
||||
// Get 'key' parameter
|
||||
const [key, keyError] = $(params.key).optional.string().match(/[a-z_]+/).$;
|
||||
if (keyError) return rej('invalid key param');
|
||||
|
||||
// Get 'value' parameter
|
||||
const [value, valueError] = $(params.value).optional.string().$;
|
||||
if (valueError) return rej('invalid value param');
|
||||
|
||||
const set = {};
|
||||
if (data) {
|
||||
Object.entries(data).forEach(([k, v]) => {
|
||||
set[`data.${k}`] = v;
|
||||
});
|
||||
} else {
|
||||
set[`data.${key}`] = value;
|
||||
}
|
||||
|
||||
await Appdata.update({
|
||||
app_id: app._id,
|
||||
user_id: user._id
|
||||
}, Object.assign({
|
||||
app_id: app._id,
|
||||
user_id: user._id
|
||||
}, {
|
||||
$set: set
|
||||
}), {
|
||||
upsert: true
|
||||
});
|
||||
|
||||
res(204);
|
||||
});
|
43
src/server/api/endpoints/i/authorized_apps.ts
Normal file
43
src/server/api/endpoints/i/authorized_apps.ts
Normal file
@ -0,0 +1,43 @@
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
import $ from 'cafy';
|
||||
import AccessToken from '../../models/access-token';
|
||||
import { pack } from '../../models/app';
|
||||
|
||||
/**
|
||||
* Get authorized apps of my account
|
||||
*
|
||||
* @param {any} params
|
||||
* @param {any} user
|
||||
* @return {Promise<any>}
|
||||
*/
|
||||
module.exports = (params, user) => new Promise(async (res, rej) => {
|
||||
// Get 'limit' parameter
|
||||
const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$;
|
||||
if (limitErr) return rej('invalid limit param');
|
||||
|
||||
// Get 'offset' parameter
|
||||
const [offset = 0, offsetErr] = $(params.offset).optional.number().min(0).$;
|
||||
if (offsetErr) return rej('invalid offset param');
|
||||
|
||||
// Get 'sort' parameter
|
||||
const [sort = 'desc', sortError] = $(params.sort).optional.string().or('desc asc').$;
|
||||
if (sortError) return rej('invalid sort param');
|
||||
|
||||
// Get tokens
|
||||
const tokens = await AccessToken
|
||||
.find({
|
||||
user_id: user._id
|
||||
}, {
|
||||
limit: limit,
|
||||
skip: offset,
|
||||
sort: {
|
||||
_id: sort == 'asc' ? 1 : -1
|
||||
}
|
||||
});
|
||||
|
||||
// Serialize
|
||||
res(await Promise.all(tokens.map(async token =>
|
||||
await pack(token.app_id))));
|
||||
});
|
42
src/server/api/endpoints/i/change_password.ts
Normal file
42
src/server/api/endpoints/i/change_password.ts
Normal file
@ -0,0 +1,42 @@
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
import $ from 'cafy';
|
||||
import * as bcrypt from 'bcryptjs';
|
||||
import User from '../../models/user';
|
||||
|
||||
/**
|
||||
* Change password
|
||||
*
|
||||
* @param {any} params
|
||||
* @param {any} user
|
||||
* @return {Promise<any>}
|
||||
*/
|
||||
module.exports = async (params, user) => new Promise(async (res, rej) => {
|
||||
// Get 'current_password' parameter
|
||||
const [currentPassword, currentPasswordErr] = $(params.current_password).string().$;
|
||||
if (currentPasswordErr) return rej('invalid current_password param');
|
||||
|
||||
// Get 'new_password' parameter
|
||||
const [newPassword, newPasswordErr] = $(params.new_password).string().$;
|
||||
if (newPasswordErr) return rej('invalid new_password param');
|
||||
|
||||
// Compare password
|
||||
const same = await bcrypt.compare(currentPassword, user.account.password);
|
||||
|
||||
if (!same) {
|
||||
return rej('incorrect password');
|
||||
}
|
||||
|
||||
// Generate hash of password
|
||||
const salt = await bcrypt.genSalt(8);
|
||||
const hash = await bcrypt.hash(newPassword, salt);
|
||||
|
||||
await User.update(user._id, {
|
||||
$set: {
|
||||
'account.password': hash
|
||||
}
|
||||
});
|
||||
|
||||
res();
|
||||
});
|
44
src/server/api/endpoints/i/favorites.ts
Normal file
44
src/server/api/endpoints/i/favorites.ts
Normal file
@ -0,0 +1,44 @@
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
import $ from 'cafy';
|
||||
import Favorite from '../../models/favorite';
|
||||
import { pack } from '../../models/post';
|
||||
|
||||
/**
|
||||
* Get followers of a user
|
||||
*
|
||||
* @param {any} params
|
||||
* @param {any} user
|
||||
* @return {Promise<any>}
|
||||
*/
|
||||
module.exports = (params, user) => new Promise(async (res, rej) => {
|
||||
// Get 'limit' parameter
|
||||
const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$;
|
||||
if (limitErr) return rej('invalid limit param');
|
||||
|
||||
// Get 'offset' parameter
|
||||
const [offset = 0, offsetErr] = $(params.offset).optional.number().min(0).$;
|
||||
if (offsetErr) return rej('invalid offset param');
|
||||
|
||||
// Get 'sort' parameter
|
||||
const [sort = 'desc', sortError] = $(params.sort).optional.string().or('desc asc').$;
|
||||
if (sortError) return rej('invalid sort param');
|
||||
|
||||
// Get favorites
|
||||
const favorites = await Favorite
|
||||
.find({
|
||||
user_id: user._id
|
||||
}, {
|
||||
limit: limit,
|
||||
skip: offset,
|
||||
sort: {
|
||||
_id: sort == 'asc' ? 1 : -1
|
||||
}
|
||||
});
|
||||
|
||||
// Serialize
|
||||
res(await Promise.all(favorites.map(async favorite =>
|
||||
await pack(favorite.post)
|
||||
)));
|
||||
});
|
110
src/server/api/endpoints/i/notifications.ts
Normal file
110
src/server/api/endpoints/i/notifications.ts
Normal file
@ -0,0 +1,110 @@
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
import $ from 'cafy';
|
||||
import Notification from '../../models/notification';
|
||||
import Mute from '../../models/mute';
|
||||
import { pack } from '../../models/notification';
|
||||
import getFriends from '../../common/get-friends';
|
||||
import read from '../../common/read-notification';
|
||||
|
||||
/**
|
||||
* Get notifications
|
||||
*
|
||||
* @param {any} params
|
||||
* @param {any} user
|
||||
* @return {Promise<any>}
|
||||
*/
|
||||
module.exports = (params, user) => new Promise(async (res, rej) => {
|
||||
// Get 'following' parameter
|
||||
const [following = false, followingError] =
|
||||
$(params.following).optional.boolean().$;
|
||||
if (followingError) return rej('invalid following param');
|
||||
|
||||
// Get 'mark_as_read' parameter
|
||||
const [markAsRead = true, markAsReadErr] = $(params.mark_as_read).optional.boolean().$;
|
||||
if (markAsReadErr) return rej('invalid mark_as_read param');
|
||||
|
||||
// Get 'type' parameter
|
||||
const [type, typeErr] = $(params.type).optional.array('string').unique().$;
|
||||
if (typeErr) return rej('invalid type param');
|
||||
|
||||
// Get 'limit' parameter
|
||||
const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$;
|
||||
if (limitErr) return rej('invalid limit param');
|
||||
|
||||
// Get 'since_id' parameter
|
||||
const [sinceId, sinceIdErr] = $(params.since_id).optional.id().$;
|
||||
if (sinceIdErr) return rej('invalid since_id param');
|
||||
|
||||
// Get 'until_id' parameter
|
||||
const [untilId, untilIdErr] = $(params.until_id).optional.id().$;
|
||||
if (untilIdErr) return rej('invalid until_id param');
|
||||
|
||||
// Check if both of since_id and until_id is specified
|
||||
if (sinceId && untilId) {
|
||||
return rej('cannot set since_id and until_id');
|
||||
}
|
||||
|
||||
const mute = await Mute.find({
|
||||
muter_id: user._id,
|
||||
deleted_at: { $exists: false }
|
||||
});
|
||||
|
||||
const query = {
|
||||
notifiee_id: user._id,
|
||||
$and: [{
|
||||
notifier_id: {
|
||||
$nin: mute.map(m => m.mutee_id)
|
||||
}
|
||||
}]
|
||||
} as any;
|
||||
|
||||
const sort = {
|
||||
_id: -1
|
||||
};
|
||||
|
||||
if (following) {
|
||||
// ID list of the user itself and other users who the user follows
|
||||
const followingIds = await getFriends(user._id);
|
||||
|
||||
query.$and.push({
|
||||
notifier_id: {
|
||||
$in: followingIds
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (type) {
|
||||
query.type = {
|
||||
$in: type
|
||||
};
|
||||
}
|
||||
|
||||
if (sinceId) {
|
||||
sort._id = 1;
|
||||
query._id = {
|
||||
$gt: sinceId
|
||||
};
|
||||
} else if (untilId) {
|
||||
query._id = {
|
||||
$lt: untilId
|
||||
};
|
||||
}
|
||||
|
||||
// Issue query
|
||||
const notifications = await Notification
|
||||
.find(query, {
|
||||
limit: limit,
|
||||
sort: sort
|
||||
});
|
||||
|
||||
// Serialize
|
||||
res(await Promise.all(notifications.map(async notification =>
|
||||
await pack(notification))));
|
||||
|
||||
// Mark as read all
|
||||
if (notifications.length > 0 && markAsRead) {
|
||||
read(user._id, notifications);
|
||||
}
|
||||
});
|
44
src/server/api/endpoints/i/pin.ts
Normal file
44
src/server/api/endpoints/i/pin.ts
Normal file
@ -0,0 +1,44 @@
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
import $ from 'cafy';
|
||||
import User from '../../models/user';
|
||||
import Post from '../../models/post';
|
||||
import { pack } from '../../models/user';
|
||||
|
||||
/**
|
||||
* Pin post
|
||||
*
|
||||
* @param {any} params
|
||||
* @param {any} user
|
||||
* @return {Promise<any>}
|
||||
*/
|
||||
module.exports = async (params, user) => new Promise(async (res, rej) => {
|
||||
// Get 'post_id' parameter
|
||||
const [postId, postIdErr] = $(params.post_id).id().$;
|
||||
if (postIdErr) return rej('invalid post_id param');
|
||||
|
||||
// Fetch pinee
|
||||
const post = await Post.findOne({
|
||||
_id: postId,
|
||||
user_id: user._id
|
||||
});
|
||||
|
||||
if (post === null) {
|
||||
return rej('post not found');
|
||||
}
|
||||
|
||||
await User.update(user._id, {
|
||||
$set: {
|
||||
pinned_post_id: post._id
|
||||
}
|
||||
});
|
||||
|
||||
// Serialize
|
||||
const iObj = await pack(user, user, {
|
||||
detail: true
|
||||
});
|
||||
|
||||
// Send response
|
||||
res(iObj);
|
||||
});
|
42
src/server/api/endpoints/i/regenerate_token.ts
Normal file
42
src/server/api/endpoints/i/regenerate_token.ts
Normal file
@ -0,0 +1,42 @@
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
import $ from 'cafy';
|
||||
import * as bcrypt from 'bcryptjs';
|
||||
import User from '../../models/user';
|
||||
import event from '../../event';
|
||||
import generateUserToken from '../../common/generate-native-user-token';
|
||||
|
||||
/**
|
||||
* Regenerate native token
|
||||
*
|
||||
* @param {any} params
|
||||
* @param {any} user
|
||||
* @return {Promise<any>}
|
||||
*/
|
||||
module.exports = async (params, user) => new Promise(async (res, rej) => {
|
||||
// Get 'password' parameter
|
||||
const [password, passwordErr] = $(params.password).string().$;
|
||||
if (passwordErr) return rej('invalid password param');
|
||||
|
||||
// Compare password
|
||||
const same = await bcrypt.compare(password, user.account.password);
|
||||
|
||||
if (!same) {
|
||||
return rej('incorrect password');
|
||||
}
|
||||
|
||||
// Generate secret
|
||||
const secret = generateUserToken();
|
||||
|
||||
await User.update(user._id, {
|
||||
$set: {
|
||||
'account.token': secret
|
||||
}
|
||||
});
|
||||
|
||||
res();
|
||||
|
||||
// Publish event
|
||||
event(user._id, 'my_token_regenerated');
|
||||
});
|
61
src/server/api/endpoints/i/signin_history.ts
Normal file
61
src/server/api/endpoints/i/signin_history.ts
Normal file
@ -0,0 +1,61 @@
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
import $ from 'cafy';
|
||||
import Signin, { pack } from '../../models/signin';
|
||||
|
||||
/**
|
||||
* Get signin history of my account
|
||||
*
|
||||
* @param {any} params
|
||||
* @param {any} user
|
||||
* @return {Promise<any>}
|
||||
*/
|
||||
module.exports = (params, user) => new Promise(async (res, rej) => {
|
||||
// Get 'limit' parameter
|
||||
const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$;
|
||||
if (limitErr) return rej('invalid limit param');
|
||||
|
||||
// Get 'since_id' parameter
|
||||
const [sinceId, sinceIdErr] = $(params.since_id).optional.id().$;
|
||||
if (sinceIdErr) return rej('invalid since_id param');
|
||||
|
||||
// Get 'until_id' parameter
|
||||
const [untilId, untilIdErr] = $(params.until_id).optional.id().$;
|
||||
if (untilIdErr) return rej('invalid until_id param');
|
||||
|
||||
// Check if both of since_id and until_id is specified
|
||||
if (sinceId && untilId) {
|
||||
return rej('cannot set since_id and until_id');
|
||||
}
|
||||
|
||||
const query = {
|
||||
user_id: user._id
|
||||
} as any;
|
||||
|
||||
const sort = {
|
||||
_id: -1
|
||||
};
|
||||
|
||||
if (sinceId) {
|
||||
sort._id = 1;
|
||||
query._id = {
|
||||
$gt: sinceId
|
||||
};
|
||||
} else if (untilId) {
|
||||
query._id = {
|
||||
$lt: untilId
|
||||
};
|
||||
}
|
||||
|
||||
// Issue query
|
||||
const history = await Signin
|
||||
.find(query, {
|
||||
limit: limit,
|
||||
sort: sort
|
||||
});
|
||||
|
||||
// Serialize
|
||||
res(await Promise.all(history.map(async record =>
|
||||
await pack(record))));
|
||||
});
|
97
src/server/api/endpoints/i/update.ts
Normal file
97
src/server/api/endpoints/i/update.ts
Normal file
@ -0,0 +1,97 @@
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
import $ from 'cafy';
|
||||
import User, { isValidName, isValidDescription, isValidLocation, isValidBirthday, pack } from '../../models/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] = $(params.name).optional.string().pipe(isValidName).$;
|
||||
if (nameErr) return rej('invalid name param');
|
||||
if (name) user.name = name;
|
||||
|
||||
// Get 'description' parameter
|
||||
const [description, descriptionErr] = $(params.description).optional.nullable.string().pipe(isValidDescription).$;
|
||||
if (descriptionErr) return rej('invalid description param');
|
||||
if (description !== undefined) user.description = description;
|
||||
|
||||
// Get 'location' parameter
|
||||
const [location, locationErr] = $(params.location).optional.nullable.string().pipe(isValidLocation).$;
|
||||
if (locationErr) return rej('invalid location param');
|
||||
if (location !== undefined) user.account.profile.location = location;
|
||||
|
||||
// Get 'birthday' parameter
|
||||
const [birthday, birthdayErr] = $(params.birthday).optional.nullable.string().pipe(isValidBirthday).$;
|
||||
if (birthdayErr) return rej('invalid birthday param');
|
||||
if (birthday !== undefined) user.account.profile.birthday = birthday;
|
||||
|
||||
// Get 'avatar_id' parameter
|
||||
const [avatarId, avatarIdErr] = $(params.avatar_id).optional.id().$;
|
||||
if (avatarIdErr) return rej('invalid avatar_id param');
|
||||
if (avatarId) user.avatar_id = avatarId;
|
||||
|
||||
// Get 'banner_id' parameter
|
||||
const [bannerId, bannerIdErr] = $(params.banner_id).optional.id().$;
|
||||
if (bannerIdErr) return rej('invalid banner_id param');
|
||||
if (bannerId) user.banner_id = bannerId;
|
||||
|
||||
// Get 'is_bot' parameter
|
||||
const [isBot, isBotErr] = $(params.is_bot).optional.boolean().$;
|
||||
if (isBotErr) return rej('invalid is_bot param');
|
||||
if (isBot != null) user.account.is_bot = isBot;
|
||||
|
||||
// Get 'auto_watch' parameter
|
||||
const [autoWatch, autoWatchErr] = $(params.auto_watch).optional.boolean().$;
|
||||
if (autoWatchErr) return rej('invalid auto_watch param');
|
||||
if (autoWatch != null) user.account.settings.auto_watch = autoWatch;
|
||||
|
||||
await User.update(user._id, {
|
||||
$set: {
|
||||
name: user.name,
|
||||
description: user.description,
|
||||
avatar_id: user.avatar_id,
|
||||
banner_id: user.banner_id,
|
||||
'account.profile': user.account.profile,
|
||||
'account.is_bot': user.account.is_bot,
|
||||
'account.settings': user.account.settings
|
||||
}
|
||||
});
|
||||
|
||||
// Serialize
|
||||
const iObj = await pack(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
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
43
src/server/api/endpoints/i/update_client_setting.ts
Normal file
43
src/server/api/endpoints/i/update_client_setting.ts
Normal file
@ -0,0 +1,43 @@
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
import $ from 'cafy';
|
||||
import User, { pack } from '../../models/user';
|
||||
import event from '../../event';
|
||||
|
||||
/**
|
||||
* Update myself
|
||||
*
|
||||
* @param {any} params
|
||||
* @param {any} user
|
||||
* @return {Promise<any>}
|
||||
*/
|
||||
module.exports = async (params, user) => new Promise(async (res, rej) => {
|
||||
// Get 'name' parameter
|
||||
const [name, nameErr] = $(params.name).string().$;
|
||||
if (nameErr) return rej('invalid name param');
|
||||
|
||||
// Get 'value' parameter
|
||||
const [value, valueErr] = $(params.value).nullable.any().$;
|
||||
if (valueErr) return rej('invalid value param');
|
||||
|
||||
const x = {};
|
||||
x[`account.client_settings.${name}`] = value;
|
||||
|
||||
await User.update(user._id, {
|
||||
$set: x
|
||||
});
|
||||
|
||||
// Serialize
|
||||
user.account.client_settings[name] = value;
|
||||
const iObj = await pack(user, user, {
|
||||
detail: true,
|
||||
includeSecrets: true
|
||||
});
|
||||
|
||||
// Send response
|
||||
res(iObj);
|
||||
|
||||
// Publish i updated event
|
||||
event(user._id, 'i_updated', iObj);
|
||||
});
|
60
src/server/api/endpoints/i/update_home.ts
Normal file
60
src/server/api/endpoints/i/update_home.ts
Normal file
@ -0,0 +1,60 @@
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
import $ from 'cafy';
|
||||
import User from '../../models/user';
|
||||
import event from '../../event';
|
||||
|
||||
module.exports = async (params, user) => new Promise(async (res, rej) => {
|
||||
// Get 'home' parameter
|
||||
const [home, homeErr] = $(params.home).optional.array().each(
|
||||
$().strict.object()
|
||||
.have('name', $().string())
|
||||
.have('id', $().string())
|
||||
.have('place', $().string())
|
||||
.have('data', $().object())).$;
|
||||
if (homeErr) return rej('invalid home param');
|
||||
|
||||
// Get 'id' parameter
|
||||
const [id, idErr] = $(params.id).optional.string().$;
|
||||
if (idErr) return rej('invalid id param');
|
||||
|
||||
// Get 'data' parameter
|
||||
const [data, dataErr] = $(params.data).optional.object().$;
|
||||
if (dataErr) return rej('invalid data param');
|
||||
|
||||
if (home) {
|
||||
await User.update(user._id, {
|
||||
$set: {
|
||||
'account.client_settings.home': home
|
||||
}
|
||||
});
|
||||
|
||||
res();
|
||||
|
||||
event(user._id, 'home_updated', {
|
||||
home
|
||||
});
|
||||
} else {
|
||||
if (id == null && data == null) return rej('you need to set id and data params if home param unset');
|
||||
|
||||
const _home = user.account.client_settings.home;
|
||||
const widget = _home.find(w => w.id == id);
|
||||
|
||||
if (widget == null) return rej('widget not found');
|
||||
|
||||
widget.data = data;
|
||||
|
||||
await User.update(user._id, {
|
||||
$set: {
|
||||
'account.client_settings.home': _home
|
||||
}
|
||||
});
|
||||
|
||||
res();
|
||||
|
||||
event(user._id, 'home_updated', {
|
||||
id, data
|
||||
});
|
||||
}
|
||||
});
|
59
src/server/api/endpoints/i/update_mobile_home.ts
Normal file
59
src/server/api/endpoints/i/update_mobile_home.ts
Normal file
@ -0,0 +1,59 @@
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
import $ from 'cafy';
|
||||
import User from '../../models/user';
|
||||
import event from '../../event';
|
||||
|
||||
module.exports = async (params, user) => new Promise(async (res, rej) => {
|
||||
// Get 'home' parameter
|
||||
const [home, homeErr] = $(params.home).optional.array().each(
|
||||
$().strict.object()
|
||||
.have('name', $().string())
|
||||
.have('id', $().string())
|
||||
.have('data', $().object())).$;
|
||||
if (homeErr) return rej('invalid home param');
|
||||
|
||||
// Get 'id' parameter
|
||||
const [id, idErr] = $(params.id).optional.string().$;
|
||||
if (idErr) return rej('invalid id param');
|
||||
|
||||
// Get 'data' parameter
|
||||
const [data, dataErr] = $(params.data).optional.object().$;
|
||||
if (dataErr) return rej('invalid data param');
|
||||
|
||||
if (home) {
|
||||
await User.update(user._id, {
|
||||
$set: {
|
||||
'account.client_settings.mobile_home': home
|
||||
}
|
||||
});
|
||||
|
||||
res();
|
||||
|
||||
event(user._id, 'mobile_home_updated', {
|
||||
home
|
||||
});
|
||||
} else {
|
||||
if (id == null && data == null) return rej('you need to set id and data params if home param unset');
|
||||
|
||||
const _home = user.account.client_settings.mobile_home || [];
|
||||
const widget = _home.find(w => w.id == id);
|
||||
|
||||
if (widget == null) return rej('widget not found');
|
||||
|
||||
widget.data = data;
|
||||
|
||||
await User.update(user._id, {
|
||||
$set: {
|
||||
'account.client_settings.mobile_home': _home
|
||||
}
|
||||
});
|
||||
|
||||
res();
|
||||
|
||||
event(user._id, 'mobile_home_updated', {
|
||||
id, data
|
||||
});
|
||||
}
|
||||
});
|
Reference in New Issue
Block a user