message.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2020 Tulir Asokan
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package database
  17. import (
  18. "bytes"
  19. "database/sql"
  20. "encoding/json"
  21. "strings"
  22. "github.com/Rhymen/go-whatsapp"
  23. waProto "github.com/Rhymen/go-whatsapp/binary/proto"
  24. log "maunium.net/go/maulogger/v2"
  25. "maunium.net/go/mautrix/id"
  26. )
  27. type MessageQuery struct {
  28. db *Database
  29. log log.Logger
  30. }
  31. func (mq *MessageQuery) New() *Message {
  32. return &Message{
  33. db: mq.db,
  34. log: mq.log,
  35. }
  36. }
  37. func (mq *MessageQuery) GetAll(chat PortalKey) (messages []*Message) {
  38. rows, err := mq.db.Query("SELECT chat_jid, chat_receiver, jid, mxid, sender, timestamp, content FROM message WHERE chat_jid=$1 AND chat_receiver=$2", chat.JID, chat.Receiver)
  39. if err != nil || rows == nil {
  40. return nil
  41. }
  42. defer rows.Close()
  43. for rows.Next() {
  44. messages = append(messages, mq.New().Scan(rows))
  45. }
  46. return
  47. }
  48. func (mq *MessageQuery) GetByJID(chat PortalKey, jid whatsapp.MessageID) *Message {
  49. return mq.get("SELECT chat_jid, chat_receiver, jid, mxid, sender, timestamp, content "+
  50. "FROM message WHERE chat_jid=$1 AND chat_receiver=$2 AND jid=$3", chat.JID, chat.Receiver, jid)
  51. }
  52. func (mq *MessageQuery) GetByMXID(mxid id.EventID) *Message {
  53. return mq.get("SELECT chat_jid, chat_receiver, jid, mxid, sender, timestamp, content "+
  54. "FROM message WHERE mxid=$1", mxid)
  55. }
  56. func (mq *MessageQuery) GetLastInChat(chat PortalKey) *Message {
  57. msg := mq.get("SELECT chat_jid, chat_receiver, jid, mxid, sender, timestamp, content "+
  58. "FROM message WHERE chat_jid=$1 AND chat_receiver=$2 ORDER BY timestamp DESC LIMIT 1", chat.JID, chat.Receiver)
  59. if msg == nil || msg.Timestamp == 0 {
  60. // Old db, we don't know what the last message is.
  61. return nil
  62. }
  63. return msg
  64. }
  65. func (mq *MessageQuery) get(query string, args ...interface{}) *Message {
  66. row := mq.db.QueryRow(query, args...)
  67. if row == nil {
  68. return nil
  69. }
  70. return mq.New().Scan(row)
  71. }
  72. type Message struct {
  73. db *Database
  74. log log.Logger
  75. Chat PortalKey
  76. JID whatsapp.MessageID
  77. MXID id.EventID
  78. Sender whatsapp.JID
  79. Timestamp uint64
  80. Content *waProto.Message
  81. }
  82. func (msg *Message) IsFakeMXID() bool {
  83. return strings.HasPrefix(msg.MXID.String(), "net.maunium.whatsapp.fake::")
  84. }
  85. func (msg *Message) Scan(row Scannable) *Message {
  86. var content []byte
  87. err := row.Scan(&msg.Chat.JID, &msg.Chat.Receiver, &msg.JID, &msg.MXID, &msg.Sender, &msg.Timestamp, &content)
  88. if err != nil {
  89. if err != sql.ErrNoRows {
  90. msg.log.Errorln("Database scan failed:", err)
  91. }
  92. return nil
  93. }
  94. msg.decodeBinaryContent(content)
  95. return msg
  96. }
  97. func (msg *Message) decodeBinaryContent(content []byte) {
  98. msg.Content = &waProto.Message{}
  99. reader := bytes.NewReader(content)
  100. dec := json.NewDecoder(reader)
  101. err := dec.Decode(msg.Content)
  102. if err != nil {
  103. msg.log.Warnln("Failed to decode message content:", err)
  104. }
  105. }
  106. func (msg *Message) encodeBinaryContent() []byte {
  107. var buf bytes.Buffer
  108. enc := json.NewEncoder(&buf)
  109. err := enc.Encode(msg.Content)
  110. if err != nil {
  111. msg.log.Warnln("Failed to encode message content:", err)
  112. }
  113. return buf.Bytes()
  114. }
  115. func (msg *Message) Insert() {
  116. _, err := msg.db.Exec("INSERT INTO message (chat_jid, chat_receiver, jid, mxid, sender, timestamp, content) "+
  117. "VALUES ($1, $2, $3, $4, $5, $6, $7)",
  118. msg.Chat.JID, msg.Chat.Receiver, msg.JID, msg.MXID, msg.Sender, msg.Timestamp, msg.encodeBinaryContent())
  119. if err != nil {
  120. msg.log.Warnfln("Failed to insert %s@%s: %v", msg.Chat, msg.JID, err)
  121. }
  122. }
  123. func (msg *Message) Delete() {
  124. _, err := msg.db.Exec("DELETE FROM message WHERE chat_jid=$1 AND chat_receiver=$2 AND jid=$3", msg.Chat.JID, msg.Chat.Receiver, msg.JID)
  125. if err != nil {
  126. msg.log.Warnfln("Failed to delete %s@%s: %v", msg.Chat, msg.JID, err)
  127. }
  128. }