message.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright (c) 2021 Tulir Asokan
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. package types
  7. import (
  8. "fmt"
  9. "time"
  10. )
  11. type AddressingMode string
  12. const (
  13. AddressingModePN AddressingMode = "pn"
  14. AddressingModeLID AddressingMode = "lid"
  15. )
  16. type BroadcastRecipient struct {
  17. LID JID
  18. PN JID
  19. }
  20. // MessageSource contains basic sender and chat information about a message.
  21. type MessageSource struct {
  22. Chat JID // The chat where the message was sent.
  23. Sender JID // The user who sent the message.
  24. IsFromMe bool // Whether the message was sent by the current user instead of someone else.
  25. IsGroup bool // Whether the chat is a group chat or broadcast list.
  26. AddressingMode AddressingMode // The addressing mode of the message (phone number or LID)
  27. SenderAlt JID // The alternative address of the user who sent the message
  28. RecipientAlt JID // The alternative address of the recipient of the message for DMs.
  29. // When sending a read receipt to a broadcast list message, the Chat is the broadcast list
  30. // and Sender is you, so this field contains the recipient of the read receipt.
  31. BroadcastListOwner JID
  32. BroadcastRecipients []BroadcastRecipient
  33. }
  34. // IsIncomingBroadcast returns true if the message was sent to a broadcast list instead of directly to the user.
  35. //
  36. // If this is true, it means the message shows up in the direct chat with the Sender.
  37. func (ms *MessageSource) IsIncomingBroadcast() bool {
  38. return (!ms.IsFromMe || !ms.BroadcastListOwner.IsEmpty()) && ms.Chat.IsBroadcastList()
  39. }
  40. // DeviceSentMeta contains metadata from messages sent by another one of the user's own devices.
  41. type DeviceSentMeta struct {
  42. DestinationJID string // The destination user. This should match the MessageInfo.Recipient field.
  43. Phash string
  44. }
  45. type EditAttribute string
  46. const (
  47. EditAttributeEmpty EditAttribute = ""
  48. EditAttributeMessageEdit EditAttribute = "1"
  49. EditAttributePinInChat EditAttribute = "2"
  50. EditAttributeAdminEdit EditAttribute = "3" // only used in newsletters
  51. EditAttributeSenderRevoke EditAttribute = "7"
  52. EditAttributeAdminRevoke EditAttribute = "8"
  53. )
  54. type BotEditType string
  55. const (
  56. EditTypeFirst BotEditType = "first"
  57. EditTypeInner BotEditType = "inner"
  58. EditTypeLast BotEditType = "last"
  59. )
  60. // MsgBotInfo targets <bot>
  61. type MsgBotInfo struct {
  62. EditType BotEditType
  63. EditTargetID MessageID
  64. EditSenderTimestampMS time.Time
  65. }
  66. // MsgMetaInfo targets <meta>
  67. type MsgMetaInfo struct {
  68. // Bot things
  69. TargetID MessageID
  70. TargetSender JID
  71. TargetChat JID
  72. DeprecatedLIDSession *bool
  73. ThreadMessageID MessageID
  74. ThreadMessageSenderJID JID
  75. }
  76. // MessageInfo contains metadata about an incoming message.
  77. type MessageInfo struct {
  78. MessageSource
  79. ID MessageID
  80. ServerID MessageServerID
  81. Type string
  82. PushName string
  83. Timestamp time.Time
  84. Category string
  85. Multicast bool
  86. MediaType string
  87. Edit EditAttribute
  88. MsgBotInfo MsgBotInfo
  89. MsgMetaInfo MsgMetaInfo
  90. VerifiedName *VerifiedName
  91. DeviceSentMeta *DeviceSentMeta // Metadata for direct messages sent from another one of the user's own devices.
  92. }
  93. // SourceString returns a log-friendly representation of who sent the message and where.
  94. func (ms *MessageSource) SourceString() string {
  95. if ms.Sender != ms.Chat {
  96. return fmt.Sprintf("%s in %s", ms.Sender, ms.Chat)
  97. } else {
  98. return ms.Chat.String()
  99. }
  100. }