Free and Open-source video streaming suite. https://miracle-tv.live
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

98 lines
3.0 KiB

  1. import ramda from 'ramda';
  2. import uuidv4 from 'uuid/v4';
  3. import md5 from 'js-md5';
  4. import db from 'server/db';
  5. import IUser, { IUserCreation, IUserSafe } from 'miracle-tv-shared/src/types/api/v1/interfaces/IUser';
  6. import { hashPassword } from 'server/lib/crypto';
  7. export interface IUserSafeMap {
  8. [key: string]: IUserSafe;
  9. }
  10. class Users {
  11. sanitzieUser = (user: IUser): IUserSafe => {
  12. if (user) {
  13. const {
  14. passwordHash,
  15. passwordSalt,
  16. email,
  17. ...userSafe
  18. } = user;
  19. return {
  20. emailHash: md5(email),
  21. ...userSafe,
  22. };
  23. }
  24. return null;
  25. }
  26. getUserSafeMap = (users: IUserSafe[]): IUserSafeMap =>
  27. ramda.reduce<IUserSafe, IUserSafeMap>(
  28. (acc, user) => ({ ...acc, [user.id]: user }), {}, users,
  29. )
  30. getUserIds = (users: IUserSafe[] | IUser[]): string[] =>
  31. ramda.map<IUser | IUserSafe, string>(ramda.path(['id']))(users)
  32. checkIfUserExists = (username: string, email: string): PromiseLike<boolean> => {
  33. return db('users').where(function () {
  34. return this.where({ username }).orWhere({ email });
  35. }).count().get(0).then(ramda.pipe(ramda.pathOr(0, ['count']), ramda.lt(0)));
  36. }
  37. getV1Users = (): PromiseLike<IUser[]> =>
  38. db('users').select('*')
  39. .then((users: IUser[]) => users.map((user: any) => user as IUser))
  40. getUsersByIds = (ids: string[]): PromiseLike<IUser[]> =>
  41. db('users').select('*').whereIn('id', ids)
  42. .then((users: IUser[]) => users.map((user: any) => user as IUser))
  43. getUsersByIdsSafe = (ids: string[]): PromiseLike<IUserSafe[]> =>
  44. db('users').select('*').whereIn('id', ids)
  45. .then((users: IUser[]) => users
  46. .map((user: any) => user as IUser)
  47. .map(this.sanitzieUser))
  48. getV1UsersSafe = (): PromiseLike<IUserSafe[]> =>
  49. this.getV1Users().then(users => users.map(this.sanitzieUser))
  50. getV1UserSafe = (id: string): PromiseLike<IUserSafe> =>
  51. this.getV1User(id).then(this.sanitzieUser)
  52. getV1User = (id: string): PromiseLike<IUser> =>
  53. db('users').select('*').where({ id }).get(0)
  54. getV1UserByUsername = (username: string): PromiseLike<IUser> =>
  55. db('users').select('*').where({ username }).get(0)
  56. getV1UserByEmail = (email: string): PromiseLike<IUser> =>
  57. db('users').select('*').where({ email }).get(0)
  58. createV1User = (user: IUserCreation): PromiseLike<IUserSafe> => {
  59. const { password, ...sanitizedUser } = user;
  60. const { passwordSalt, passwordHash } = hashPassword(password);
  61. const userObject = {
  62. passwordSalt,
  63. passwordHash,
  64. id: uuidv4(),
  65. ...sanitizedUser,
  66. singleChannelMode: true,
  67. role: 'user',
  68. };
  69. return db('users').insert(userObject).returning('*').get(0).then(this.sanitzieUser);
  70. }
  71. updateV1User = (id: string, user: IUserSafe): PromiseLike<IUserSafe> => {
  72. return db('users').where({ id }).update(user, '*').get(0).then(this.sanitzieUser);
  73. }
  74. deleteV1User = (id: string): PromiseLike<Boolean> =>
  75. db('users').where({ id }).delete().then()
  76. }
  77. export default new Users;