chat-messages.js 823 B

12345678910111213141516171819202122232425262728293031323334
  1. const ONE_MINUTE = 60 * 1000
  2. const user = {
  3. id: 'fake_user',
  4. first_name: 'mortimer',
  5. email: 'fake@example.com',
  6. }
  7. const user2 = {
  8. id: 'another_fake_user',
  9. first_name: 'leopold',
  10. email: 'another_fake@example.com',
  11. }
  12. let nextMessageId = 1
  13. export function generateMessages(count) {
  14. const messages = []
  15. let timestamp = new Date().getTime() // newest message goes first
  16. for (let i = 0; i <= count; i++) {
  17. const author = Math.random() > 0.5 ? user : user2
  18. // modify the timestamp so the previous message has 70% chances to be within 5 minutes from
  19. // the current one, for grouping purposes
  20. timestamp -= (4.3 + Math.random()) * ONE_MINUTE
  21. messages.push({
  22. id: '' + nextMessageId++,
  23. content: `message #${i}`,
  24. user: author,
  25. timestamp,
  26. })
  27. }
  28. return messages
  29. }