conversation.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package types
  2. import "time"
  3. // ConversationData is a conversation event payload: an operation and the
  4. // conversations it applies to.
  5. type ConversationData struct {
  6. Operation string `json:"operation" xml:"operation"`
  7. Conversations []ConversationEntryData `json:"conversations" xml:"conversations>conversation"`
  8. }
  9. // ConversationEntryData is one conversation in the client's list.
  10. type ConversationEntryData struct {
  11. AimID string `json:"aimId" xml:"aimId"`
  12. // Active is always sent, zero included, because the client reads it
  13. // unconditionally.
  14. Active int `json:"active" xml:"active"`
  15. UnreadCount int `json:"unreadCount" xml:"unreadCount"`
  16. // DisplayID is omitted when empty rather than sent blank: the client falls
  17. // back to the name it already has for aimID, whereas any value present here
  18. // replaces it.
  19. DisplayID string `json:"displayId,omitempty" xml:"displayId,omitempty"`
  20. LastIM *LastIM `json:"lastIM,omitempty" xml:"lastIM,omitempty"`
  21. }
  22. // LastIM is the most recent message in a conversation.
  23. //
  24. // Timestamp is a float because AMF3 encodes whole numbers in 29 bits, which a
  25. // Unix timestamp overflows.
  26. type LastIM struct {
  27. Message string `json:"message" xml:"message"`
  28. MsgID string `json:"msgId" xml:"msgId"`
  29. Sender string `json:"sender" xml:"sender"`
  30. Sent bool `json:"sent" xml:"sent"`
  31. Timestamp float64 `json:"timestamp" xml:"timestamp"`
  32. }
  33. // ConversationEventData builds a conversation fetchEvents payload.
  34. func ConversationEventData(operation string, conversations []ConversationEntryData) *ConversationData {
  35. if conversations == nil {
  36. conversations = []ConversationEntryData{}
  37. }
  38. return &ConversationData{
  39. Operation: operation,
  40. Conversations: conversations,
  41. }
  42. }
  43. // ConversationEntry builds one conversation object for the Web AIM client.
  44. func ConversationEntry(aimID, displayID, message, msgID, sender string, sent bool, unread int) ConversationEntryData {
  45. entry := ConversationEntryData{
  46. AimID: aimID,
  47. Active: 0,
  48. UnreadCount: unread,
  49. DisplayID: displayID,
  50. }
  51. if message != "" {
  52. entry.LastIM = &LastIM{
  53. Message: message,
  54. MsgID: msgID,
  55. Sender: sender,
  56. Sent: sent,
  57. Timestamp: float64(time.Now().Unix()),
  58. }
  59. }
  60. return entry
  61. }