message.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. waProto "github.com/Rhymen/go-whatsapp/binary/proto"
  22. log "maunium.net/go/maulogger/v2"
  23. "maunium.net/go/mautrix-whatsapp/types"
  24. "maunium.net/go/mautrix/id"
  25. )
  26. type MessageQuery struct {
  27. db *Database
  28. log log.Logger
  29. }
  30. func (mq *MessageQuery) New() *Message {
  31. return &Message{
  32. db: mq.db,
  33. log: mq.log,
  34. }
  35. }
  36. func (mq *MessageQuery) GetAll(chat PortalKey) (messages []*Message) {
  37. 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)
  38. if err != nil || rows == nil {
  39. return nil
  40. }
  41. defer rows.Close()
  42. for rows.Next() {
  43. messages = append(messages, mq.New().Scan(rows))
  44. }
  45. return
  46. }
  47. func (mq *MessageQuery) GetByJID(chat PortalKey, jid types.WhatsAppMessageID) *Message {
  48. return mq.get("SELECT chat_jid, chat_receiver, jid, mxid, sender, timestamp, content " +
  49. "FROM message WHERE chat_jid=$1 AND chat_receiver=$2 AND jid=$3", chat.JID, chat.Receiver, jid)
  50. }
  51. func (mq *MessageQuery) GetByMXID(mxid id.EventID) *Message {
  52. return mq.get("SELECT chat_jid, chat_receiver, jid, mxid, sender, timestamp, content " +
  53. "FROM message WHERE mxid=$1", mxid)
  54. }
  55. func (mq *MessageQuery) GetLastInChat(chat PortalKey) *Message {
  56. msg := mq.get("SELECT chat_jid, chat_receiver, jid, mxid, sender, timestamp, content " +
  57. "FROM message WHERE chat_jid=$1 AND chat_receiver=$2 ORDER BY timestamp DESC LIMIT 1", chat.JID, chat.Receiver)
  58. if msg == nil || msg.Timestamp == 0 {
  59. // Old db, we don't know what the last message is.
  60. return nil
  61. }
  62. return msg
  63. }
  64. func (mq *MessageQuery) get(query string, args ...interface{}) *Message {
  65. row := mq.db.QueryRow(query, args...)
  66. if row == nil {
  67. return nil
  68. }
  69. return mq.New().Scan(row)
  70. }
  71. type Message struct {
  72. db *Database
  73. log log.Logger
  74. Chat PortalKey
  75. JID types.WhatsAppMessageID
  76. MXID id.EventID
  77. Sender types.WhatsAppID
  78. Timestamp uint64
  79. Content *waProto.Message
  80. }
  81. func (msg *Message) Scan(row Scannable) *Message {
  82. var content []byte
  83. err := row.Scan(&msg.Chat.JID, &msg.Chat.Receiver, &msg.JID, &msg.MXID, &msg.Sender, &msg.Timestamp, &content)
  84. if err != nil {
  85. if err != sql.ErrNoRows {
  86. msg.log.Errorln("Database scan failed:", err)
  87. }
  88. return nil
  89. }
  90. msg.decodeBinaryContent(content)
  91. return msg
  92. }
  93. func (msg *Message) decodeBinaryContent(content []byte) {
  94. msg.Content = &waProto.Message{}
  95. reader := bytes.NewReader(content)
  96. dec := json.NewDecoder(reader)
  97. err := dec.Decode(msg.Content)
  98. if err != nil {
  99. msg.log.Warnln("Failed to decode message content:", err)
  100. }
  101. }
  102. func (msg *Message) encodeBinaryContent() []byte {
  103. var buf bytes.Buffer
  104. enc := json.NewEncoder(&buf)
  105. err := enc.Encode(msg.Content)
  106. if err != nil {
  107. msg.log.Warnln("Failed to encode message content:", err)
  108. }
  109. return buf.Bytes()
  110. }
  111. func (msg *Message) Insert() {
  112. _, err := msg.db.Exec("INSERT INTO message (chat_jid, chat_receiver, jid, mxid, sender, timestamp, content) " +
  113. "VALUES ($1, $2, $3, $4, $5, $6, $7)",
  114. msg.Chat.JID, msg.Chat.Receiver, msg.JID, msg.MXID, msg.Sender, msg.Timestamp, msg.encodeBinaryContent())
  115. if err != nil {
  116. msg.log.Warnfln("Failed to insert %s@%s: %v", msg.Chat, msg.JID, err)
  117. }
  118. }
  119. func (msg *Message) Delete() {
  120. _, 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)
  121. if err != nil {
  122. msg.log.Warnfln("Failed to delete %s@%s: %v", msg.Chat, msg.JID, err)
  123. }
  124. }