ChatController.coffee 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. define [
  2. "base"
  3. ], (App) ->
  4. App.controller "ChatController", ["$scope", ($scope) ->
  5. james = {
  6. id: $scope.user.id
  7. first_name: "James"
  8. last_name: "Allen"
  9. email: "james@sharelatex.com"
  10. }
  11. james2 = {
  12. id: "james2-id"
  13. first_name: "James"
  14. last_name: "Allen"
  15. email: "jamesallen0108@gmail.com"
  16. }
  17. henry = {
  18. id: "henry-id"
  19. first_name: "Henry"
  20. last_name: "Oswald"
  21. email: "henry.oswald@sharelatex.com"
  22. }
  23. $scope.chat.messages = [
  24. { content: "Hello world", timestamp: Date.now() - 2000, user: james2 }
  25. { content: "Hello, this is the new chat", timestamp: Date.now() - 4000, user: james }
  26. { content: "Here are some longer messages to show what it looks like when I say a lot!", timestamp: Date.now() - 20000, user: henry }
  27. { content: "What about some maths? $x^2 = 1$?", timestamp: Date.now() - 22000, user: james2 }
  28. { content: "Nope, that doesn't work yet!", timestamp: Date.now() - 45000, user: henry }
  29. { content: "I'm running out of things to say.", timestamp: Date.now() - 56000, user: henry }
  30. { content: "Yep, me too", timestamp: Date.now() - 100000, user: james }
  31. { content: "Hmm, looks like we've had this conversation backwards", timestamp: Date.now() - 120000, user: james }
  32. { content: "Hello world", timestamp: Date.now() - 202000, user: james2 }
  33. { content: "Hello, this is the new chat", timestamp: Date.now() - 204000, user: james }
  34. { content: "Here are some longer messages to show what it looks like when I say a lot!", timestamp: Date.now() - 2020000, user: henry }
  35. { content: "What about some maths? $x^2 = 1$?", timestamp: Date.now() - 12022000, user: james2 }
  36. { content: "Nope, that doesn't work yet!", timestamp: Date.now() - 12045000, user: henry }
  37. { content: "I'm running out of things to say.", timestamp: Date.now() - 22056000, user: henry }
  38. { content: "Yep, me too", timestamp: Date.now() - 220100000, user: james }
  39. { content: "Hmm, looks like we've had this conversation backwards", timestamp: Date.now() - 520120000, user: james }
  40. ].reverse()
  41. $scope.$watch "chat.messages", (messages) ->
  42. if messages?
  43. $scope.chat.groupedMessages = groupMessages(messages)
  44. TIMESTAMP_GROUP_SIZE = 5 * 60 * 1000 # 5 minutes
  45. groupMessages = (messages) ->
  46. previousMessage = null
  47. groupedMessages = []
  48. for message in messages
  49. shouldGroup = previousMessage? and
  50. previousMessage.user == message.user and
  51. message.timestamp - previousMessage.timestamp < TIMESTAMP_GROUP_SIZE
  52. if shouldGroup
  53. previousMessage.timestamp = message.timestamp
  54. previousMessage.contents.push message.content
  55. else
  56. groupedMessages.push(previousMessage = {
  57. user: message.user
  58. timestamp: message.timestamp
  59. contents: [message.content]
  60. })
  61. return groupedMessages
  62. ]