Introduce account document to user document

An account document is attached to a user document if an account of the
user is on the server. It may be missing if the user is on a remote server.
This commit is contained in:
Akihiko Odaki 2018-03-26 00:19:07 +09:00
parent a633f184ab
commit 19b9cb105d
70 changed files with 355 additions and 280 deletions

View file

@ -28,8 +28,8 @@ module.exports = async (params, user) => new Promise(async (res, rej) => {
await User.update(user._id, {
$set: {
two_factor_secret: user.two_factor_temp_secret,
two_factor_enabled: true
'account.two_factor_secret': user.two_factor_temp_secret,
'account.two_factor_enabled': true
}
});

View file

@ -14,7 +14,7 @@ module.exports = async (params, user) => new Promise(async (res, rej) => {
if (passwordErr) return rej('invalid password param');
// Compare password
const same = await bcrypt.compare(password, user.password);
const same = await bcrypt.compare(password, user.account.password);
if (!same) {
return rej('incorrect password');

View file

@ -11,7 +11,7 @@ module.exports = async (params, user) => new Promise(async (res, rej) => {
if (passwordErr) return rej('invalid password param');
// Compare password
const same = await bcrypt.compare(password, user.password);
const same = await bcrypt.compare(password, user.account.password);
if (!same) {
return rej('incorrect password');
@ -19,8 +19,8 @@ module.exports = async (params, user) => new Promise(async (res, rej) => {
await User.update(user._id, {
$set: {
two_factor_secret: null,
two_factor_enabled: false
'account.two_factor_secret': null,
'account.two_factor_enabled': false
}
});

View file

@ -22,7 +22,7 @@ module.exports = async (params, user) => new Promise(async (res, rej) => {
if (newPasswordErr) return rej('invalid new_password param');
// Compare password
const same = await bcrypt.compare(currentPassword, user.password);
const same = await bcrypt.compare(currentPassword, user.account.password);
if (!same) {
return rej('incorrect password');
@ -34,7 +34,7 @@ module.exports = async (params, user) => new Promise(async (res, rej) => {
await User.update(user._id, {
$set: {
password: hash
'account.password': hash
}
});

View file

@ -20,7 +20,7 @@ module.exports = async (params, user) => new Promise(async (res, rej) => {
if (passwordErr) return rej('invalid password param');
// Compare password
const same = await bcrypt.compare(password, user.password);
const same = await bcrypt.compare(password, user.account.password);
if (!same) {
return rej('incorrect password');
@ -31,7 +31,7 @@ module.exports = async (params, user) => new Promise(async (res, rej) => {
await User.update(user._id, {
$set: {
token: secret
'account.token': secret
}
});

View file

@ -29,12 +29,12 @@ module.exports = async (params, user, _, isSecure) => new Promise(async (res, re
// Get 'location' parameter
const [location, locationErr] = $(params.location).optional.nullable.string().pipe(isValidLocation).$;
if (locationErr) return rej('invalid location param');
if (location !== undefined) user.profile.location = location;
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.profile.birthday = birthday;
if (birthday !== undefined) user.account.profile.birthday = birthday;
// Get 'avatar_id' parameter
const [avatarId, avatarIdErr] = $(params.avatar_id).optional.id().$;
@ -49,12 +49,12 @@ module.exports = async (params, user, _, isSecure) => new Promise(async (res, re
// Get 'is_bot' parameter
const [isBot, isBotErr] = $(params.is_bot).optional.boolean().$;
if (isBotErr) return rej('invalid is_bot param');
if (isBot != null) user.is_bot = isBot;
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.settings.auto_watch = autoWatch;
if (autoWatch != null) user.account.settings.auto_watch = autoWatch;
await User.update(user._id, {
$set: {
@ -62,9 +62,9 @@ module.exports = async (params, user, _, isSecure) => new Promise(async (res, re
description: user.description,
avatar_id: user.avatar_id,
banner_id: user.banner_id,
profile: user.profile,
is_bot: user.is_bot,
settings: user.settings
'account.profile': user.account.profile,
'account.is_bot': user.account.is_bot,
'account.settings': user.account.settings
}
});

View file

@ -22,14 +22,14 @@ module.exports = async (params, user) => new Promise(async (res, rej) => {
if (valueErr) return rej('invalid value param');
const x = {};
x[`client_settings.${name}`] = value;
x[`account.client_settings.${name}`] = value;
await User.update(user._id, {
$set: x
});
// Serialize
user.client_settings[name] = value;
user.account.client_settings[name] = value;
const iObj = await pack(user, user, {
detail: true,
includeSecrets: true

View file

@ -26,7 +26,7 @@ module.exports = async (params, user) => new Promise(async (res, rej) => {
if (home) {
await User.update(user._id, {
$set: {
'client_settings.home': home
'account.client_settings.home': home
}
});
@ -38,7 +38,7 @@ module.exports = async (params, user) => new Promise(async (res, rej) => {
} else {
if (id == null && data == null) return rej('you need to set id and data params if home param unset');
const _home = user.client_settings.home;
const _home = user.account.client_settings.home;
const widget = _home.find(w => w.id == id);
if (widget == null) return rej('widget not found');
@ -47,7 +47,7 @@ module.exports = async (params, user) => new Promise(async (res, rej) => {
await User.update(user._id, {
$set: {
'client_settings.home': _home
'account.client_settings.home': _home
}
});

View file

@ -25,7 +25,7 @@ module.exports = async (params, user) => new Promise(async (res, rej) => {
if (home) {
await User.update(user._id, {
$set: {
'client_settings.mobile_home': home
'account.client_settings.mobile_home': home
}
});
@ -37,7 +37,7 @@ module.exports = async (params, user) => new Promise(async (res, rej) => {
} else {
if (id == null && data == null) return rej('you need to set id and data params if home param unset');
const _home = user.client_settings.mobile_home || [];
const _home = user.account.client_settings.mobile_home || [];
const widget = _home.find(w => w.id == id);
if (widget == null) return rej('widget not found');
@ -46,7 +46,7 @@ module.exports = async (params, user) => new Promise(async (res, rej) => {
await User.update(user._id, {
$set: {
'client_settings.mobile_home': _home
'account.client_settings.mobile_home': _home
}
});