diff options
Diffstat (limited to 'src/server/api')
| -rw-r--r-- | src/server/api/endpoints/i/update.ts | 33 |
1 files changed, 20 insertions, 13 deletions
diff --git a/src/server/api/endpoints/i/update.ts b/src/server/api/endpoints/i/update.ts index d06ab621c6..8649b59c00 100644 --- a/src/server/api/endpoints/i/update.ts +++ b/src/server/api/endpoints/i/update.ts @@ -13,6 +13,7 @@ import { ApiError } from '../../error'; import { Users, DriveFiles, UserProfiles } from '../../../../models'; import { User } from '../../../../models/entities/user'; import { UserProfile } from '../../../../models/entities/user-profile'; +import { ensure } from '../../../../prelude/ensure'; export const meta = { desc: { @@ -157,22 +158,24 @@ export default define(meta, async (ps, user, app) => { const isSecure = user != null && app == null; const updates = {} as Partial<User>; - const profile = {} as Partial<UserProfile>; + const profileUpdates = {} as Partial<UserProfile>; + + const profile = await UserProfiles.findOne({ userId: user.id }).then(ensure); if (ps.name !== undefined) updates.name = ps.name; - if (ps.description !== undefined) profile.description = ps.description; + if (ps.description !== undefined) profileUpdates.description = ps.description; //if (ps.lang !== undefined) updates.lang = ps.lang; - if (ps.location !== undefined) profile.location = ps.location; - if (ps.birthday !== undefined) profile.birthday = ps.birthday; + if (ps.location !== undefined) profileUpdates.location = ps.location; + if (ps.birthday !== undefined) profileUpdates.birthday = ps.birthday; if (ps.avatarId !== undefined) updates.avatarId = ps.avatarId; if (ps.bannerId !== undefined) updates.bannerId = ps.bannerId; if (typeof ps.isLocked == 'boolean') updates.isLocked = ps.isLocked; if (typeof ps.isBot == 'boolean') updates.isBot = ps.isBot; - if (typeof ps.carefulBot == 'boolean') profile.carefulBot = ps.carefulBot; - if (typeof ps.autoAcceptFollowed == 'boolean') profile.autoAcceptFollowed = ps.autoAcceptFollowed; + if (typeof ps.carefulBot == 'boolean') profileUpdates.carefulBot = ps.carefulBot; + if (typeof ps.autoAcceptFollowed == 'boolean') profileUpdates.autoAcceptFollowed = ps.autoAcceptFollowed; if (typeof ps.isCat == 'boolean') updates.isCat = ps.isCat; - if (typeof ps.autoWatch == 'boolean') profile.autoWatch = ps.autoWatch; - if (typeof ps.alwaysMarkNsfw == 'boolean') profile.alwaysMarkNsfw = ps.alwaysMarkNsfw; + if (typeof ps.autoWatch == 'boolean') profileUpdates.autoWatch = ps.autoWatch; + if (typeof ps.alwaysMarkNsfw == 'boolean') profileUpdates.alwaysMarkNsfw = ps.alwaysMarkNsfw; if (ps.avatarId) { const avatar = await DriveFiles.findOne(ps.avatarId); @@ -201,16 +204,20 @@ export default define(meta, async (ps, user, app) => { } //#region emojis/tags + let emojis = [] as string[]; let tags = [] as string[]; - if (updates.name != null) { - const tokens = parsePlain(updates.name); + const newName = updates.name === undefined ? user.name : updates.name; + const newDescription = profileUpdates.description === undefined ? profile.description : profileUpdates.description; + + if (newName != null) { + const tokens = parsePlain(newName); emojis = emojis.concat(extractEmojis(tokens!)); } - if (profile.description != null) { - const tokens = parse(profile.description); + if (newDescription != null) { + const tokens = parse(newDescription); emojis = emojis.concat(extractEmojis(tokens!)); tags = extractHashtags(tokens!).map(tag => tag.toLowerCase()); } @@ -224,7 +231,7 @@ export default define(meta, async (ps, user, app) => { //#endregion if (Object.keys(updates).length > 0) await Users.update(user.id, updates); - if (Object.keys(profile).length > 0) await UserProfiles.update({ userId: user.id }, profile); + if (Object.keys(profileUpdates).length > 0) await UserProfiles.update({ userId: user.id }, profileUpdates); const iObj = await Users.pack(user.id, user, { detail: true, |