2020-07-23 11:57:44 +03:00
|
|
|
import * as Environment from "~/node_common/environment";
|
2020-07-22 08:53:29 +03:00
|
|
|
import * as Data from "~/node_common/data";
|
|
|
|
import * as Utilities from "~/node_common/utilities";
|
2020-07-23 11:57:44 +03:00
|
|
|
import * as Validations from "~/common/validations";
|
2020-09-22 03:36:45 +03:00
|
|
|
import * as Social from "~/node_common/social";
|
2020-10-27 07:41:42 +03:00
|
|
|
import * as ViewerManager from "~/node_common/managers/viewer";
|
2020-11-10 00:20:38 +03:00
|
|
|
import * as SearchManager from "~/node_common/managers/search";
|
2020-07-17 13:24:20 +03:00
|
|
|
|
2020-07-23 11:57:44 +03:00
|
|
|
import BCrypt from "bcrypt";
|
2020-07-17 13:24:20 +03:00
|
|
|
|
|
|
|
export default async (req, res) => {
|
2020-07-22 13:51:40 +03:00
|
|
|
const id = Utilities.getIdFromCookie(req);
|
|
|
|
if (!id) {
|
2020-10-27 07:41:42 +03:00
|
|
|
return res.status(500).send({ decorator: "SERVER_USER_UPDATE", error: true });
|
2020-07-22 08:53:29 +03:00
|
|
|
}
|
|
|
|
|
2020-07-22 13:51:40 +03:00
|
|
|
const user = await Data.getUserById({
|
|
|
|
id,
|
2020-07-22 08:53:29 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!user) {
|
2020-10-27 07:41:42 +03:00
|
|
|
return res.status(404).send({ decorator: "SERVER_USER_UPDATE_USER_NOT_FOUND", error: true });
|
2020-07-22 08:53:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (user.error) {
|
2020-10-27 07:41:42 +03:00
|
|
|
return res.status(500).send({ decorator: "SERVER_USER_UPDATE_USER_NOT_FOUND", error: true });
|
2020-07-22 08:53:29 +03:00
|
|
|
}
|
|
|
|
|
2020-10-27 07:41:42 +03:00
|
|
|
let unsafeResponse;
|
2020-07-22 10:41:29 +03:00
|
|
|
if (req.body.data) {
|
2020-10-27 07:41:42 +03:00
|
|
|
unsafeResponse = await Data.updateUserById({
|
2020-07-22 10:41:29 +03:00
|
|
|
id: user.id,
|
|
|
|
data: { ...user.data, ...req.body.data },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-22 13:51:40 +03:00
|
|
|
if (req.body.username) {
|
|
|
|
const existing = await Data.getUserByUsername({
|
|
|
|
username: req.body.username,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!existing) {
|
2020-10-27 07:41:42 +03:00
|
|
|
unsafeResponse = await Data.updateUserById({
|
2020-07-22 13:51:40 +03:00
|
|
|
id: user.id,
|
|
|
|
username: req.body.username,
|
|
|
|
});
|
2020-10-27 07:41:42 +03:00
|
|
|
} else {
|
|
|
|
return res.status(500).send({ decorator: "SERVER_USERNAME_IS_TAKEN", error: true });
|
2020-09-09 20:56:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 11:57:44 +03:00
|
|
|
if (req.body.type == "CHANGE_PASSWORD") {
|
|
|
|
if (!Validations.password(req.body.password)) {
|
2020-10-27 07:41:42 +03:00
|
|
|
return res.status(500).send({ decorator: "SERVER_INVALID_PASSWORD", error: true });
|
2020-07-23 11:57:44 +03:00
|
|
|
}
|
|
|
|
|
2020-08-11 08:15:39 +03:00
|
|
|
const rounds = Number(Environment.LOCAL_PASSWORD_ROUNDS);
|
|
|
|
const salt = await BCrypt.genSalt(rounds);
|
|
|
|
const hash = await Utilities.encryptPassword(req.body.password, salt);
|
2020-07-23 11:57:44 +03:00
|
|
|
|
2020-10-27 07:41:42 +03:00
|
|
|
unsafeResponse = await Data.updateUserById({
|
2020-07-23 11:57:44 +03:00
|
|
|
id: user.id,
|
|
|
|
salt,
|
2020-08-11 08:15:39 +03:00
|
|
|
password: hash,
|
2020-07-23 11:57:44 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-27 07:41:42 +03:00
|
|
|
if (unsafeResponse) {
|
|
|
|
ViewerManager.hydratePartialViewer(unsafeResponse);
|
|
|
|
}
|
|
|
|
|
2020-11-10 00:20:38 +03:00
|
|
|
if (
|
|
|
|
user.username !== unsafeResponse.username ||
|
|
|
|
user.data.name !== unsafeResponse.data.name ||
|
|
|
|
user.data.photo !== unsafeResponse.data.photo
|
|
|
|
) {
|
|
|
|
SearchManager.updateUser(unsafeResponse, "EDIT");
|
|
|
|
}
|
|
|
|
|
2020-09-03 03:21:29 +03:00
|
|
|
return res.status(200).send({ decorator: "SERVER_USER_UPDATE" });
|
2020-07-17 13:24:20 +03:00
|
|
|
};
|