|
| 1 | +import * as _ from 'lodash' |
| 2 | +import { lorem, random, name, internet } from 'faker' |
| 3 | +import { IMessage, IUser, IChat, UserStatus, getTimestamp, getRandomDates } from '.' |
| 4 | + |
| 5 | +export interface ChatOptions { |
| 6 | + userCount?: number |
| 7 | + msgCount?: number |
| 8 | +} |
| 9 | + |
| 10 | +const userStatuses: UserStatus[] = ['Available', 'Away', 'DoNotDisturb', 'Offline'] |
| 11 | + |
| 12 | +class ChatMock { |
| 13 | + private static readonly minUserCount = 2 |
| 14 | + private static readonly defaultCount = 10 |
| 15 | + private static readonly daysAgo = 40 |
| 16 | + private static readonly defaultChatTitle = 'Test Chat' |
| 17 | + |
| 18 | + private userIds: string[] = [] |
| 19 | + private usersMap: Map<string, IUser> = new Map() |
| 20 | + private chatMessages: IMessage[] = [] |
| 21 | + public chat: IChat |
| 22 | + |
| 23 | + constructor( |
| 24 | + private options: ChatOptions = { |
| 25 | + userCount: ChatMock.defaultCount, |
| 26 | + msgCount: ChatMock.defaultCount, |
| 27 | + }, |
| 28 | + ) { |
| 29 | + if (this.options.userCount < ChatMock.minUserCount) { |
| 30 | + throw `[ChatMock]: A chat has a minimum of ${ChatMock.minUserCount} users` |
| 31 | + } |
| 32 | + |
| 33 | + this.userIds = _.times(this.options.userCount, random.uuid) |
| 34 | + |
| 35 | + this.userIds.forEach(id => { |
| 36 | + const firstName = name.firstName() |
| 37 | + const lastName = name.lastName() |
| 38 | + const user: IUser = { |
| 39 | + id, |
| 40 | + firstName, |
| 41 | + lastName, |
| 42 | + avatar: internet.avatar(), |
| 43 | + displayName: internet.userName(firstName, lastName), |
| 44 | + email: internet.email(firstName, lastName), |
| 45 | + status: userStatuses[_.random(userStatuses.length - 1)], |
| 46 | + } |
| 47 | + |
| 48 | + this.usersMap.set(id, user) |
| 49 | + }) |
| 50 | + |
| 51 | + const currentUser = this.usersMap.get(this.userIds[0]) |
| 52 | + const dates = getRandomDates(this.options.msgCount, ChatMock.daysAgo) |
| 53 | + |
| 54 | + this.chatMessages = _.times(this.options.msgCount, id => { |
| 55 | + const mine = random.boolean() |
| 56 | + const from = (mine ? currentUser : this.getRandomUser()).id |
| 57 | + const date = dates[id] |
| 58 | + const timestamp = getTimestamp(date) |
| 59 | + |
| 60 | + const message: IMessage = { |
| 61 | + id, |
| 62 | + from, |
| 63 | + mine, |
| 64 | + date, |
| 65 | + content: lorem.sentences(_.random(1, 5, false)), |
| 66 | + timestamp: timestamp.short, |
| 67 | + timestampLong: timestamp.long, |
| 68 | + isImportant: random.boolean(), |
| 69 | + } |
| 70 | + |
| 71 | + return message |
| 72 | + }).sort((a, b) => a.date - b.date) |
| 73 | + |
| 74 | + this.chat = { |
| 75 | + id: random.uuid(), |
| 76 | + currentUser, |
| 77 | + isOneOnOne: this.usersMap.size === ChatMock.minUserCount, |
| 78 | + messages: this.chatMessages, |
| 79 | + members: this.usersMap, |
| 80 | + title: ChatMock.defaultChatTitle, |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private getRandomUser(max: number = this.usersMap.size - 1): IUser { |
| 85 | + return this.usersMap.get(this.userIds[random.number({ max, precision: 1 })]) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +export const getChatMock = (options?: ChatOptions) => new ChatMock(options) |
0 commit comments