feat(email validation): email requirement and validation + better importer

This commit is contained in:
Nicolai Van der Storm
2022-06-13 14:21:05 +02:00
parent cc69f66ba9
commit d835336d33
6 changed files with 78 additions and 59 deletions

View File

@@ -1,6 +1,6 @@
import { Router } from 'express';
import gravatarUrl from 'gravatar-url';
import { findIndex, sortBy } from 'lodash';
import { findIndex, forEach, sortBy } from 'lodash';
import { getRepository, In, Not } from 'typeorm';
import JellyfinAPI from '../../api/jellyfin';
import PlexTvAPI from '../../api/plextv';
@@ -492,62 +492,44 @@ router.post(
);
jellyfinClient.setUserId(admin.jellyfinUserId ?? '');
const jellyfinUsersResponse = await jellyfinClient.getUsers();
//const jellyfinUsersResponse = await jellyfinClient.getUsers();
const createdUsers: User[] = [];
const { hostname, externalHostname } = getSettings().jellyfin;
const jellyfinHost =
externalHostname && externalHostname.length > 0
? externalHostname
: hostname;
for (const account of jellyfinUsersResponse.users) {
if (account.Name) {
const user = await userRepository
.createQueryBuilder('user')
.where('user.jellyfinUserId = :id', { id: account.Id })
.orWhere('user.email = :email', {
email: account.Name,
})
.getOne();
const avatar = account.PrimaryImageTag
? `${jellyfinHost}/Users/${account.Id}/Images/Primary/?tag=${account.PrimaryImageTag}&quality=90`
: '/os_logo_square.png';
forEach(body.jellyfinUserIds, async (jellyfinUserId) => {
jellyfinClient.setUserId(jellyfinUserId);
const jellyfinUser = await jellyfinClient.getUser();
if (user) {
// Update the user's avatar with their Jellyfin thumbnail, in case it changed
user.avatar = avatar;
user.email = account.Name;
user.jellyfinUsername = account.Name;
const user = await userRepository.findOne({
select: ['id', 'jellyfinUserId'],
where: { jellyfinUserId: jellyfinUserId },
});
// In case the user was previously a local account
if (user.userType === UserType.LOCAL) {
user.userType = UserType.JELLYFIN;
user.jellyfinUserId = account.Id;
}
await userRepository.save(user);
} else if (!body || body.jellyfinUserIds.includes(account.Id)) {
// logger.error('CREATED USER', {
// label: 'API',
// });
if (!user) {
const newUser = new User({
jellyfinUsername: jellyfinUser.Name,
jellyfinUserId: jellyfinUser.Id,
jellyfinDeviceId: Buffer.from(
`BOT_jellyseerr_${jellyfinUser.Name ?? ''}`
).toString('base64'),
email: jellyfinUser.Name,
permissions: settings.main.defaultPermissions,
avatar: jellyfinUser.PrimaryImageTag
? `${jellyfinHost}/Users/${jellyfinUser.Id}/Images/Primary/?tag=${jellyfinUser.PrimaryImageTag}&quality=90`
: '/os_logo_square.png',
userType: UserType.JELLYFIN,
});
const newUser = new User({
jellyfinUsername: account.Name,
jellyfinUserId: account.Id,
jellyfinDeviceId: Buffer.from(
`BOT_overseerr_${account.Name ?? ''}`
).toString('base64'),
email: account.Name,
permissions: settings.main.defaultPermissions,
avatar,
userType: UserType.JELLYFIN,
});
await userRepository.save(newUser);
createdUsers.push(newUser);
}
await userRepository.save(newUser);
createdUsers.push(newUser);
}
}
return res.status(201).json(User.filterMany(createdUsers));
return res.status(201).json(User.filterMany(createdUsers));
});
} catch (e) {
next({ status: 500, message: e.message });
}