message.go 4.2 KB

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