message.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2021 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. "database/sql"
  19. "strings"
  20. "time"
  21. log "maunium.net/go/maulogger/v2"
  22. "maunium.net/go/mautrix/id"
  23. "go.mau.fi/whatsmeow/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, sent 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.MessageID) *Message {
  47. return mq.get("SELECT chat_jid, chat_receiver, jid, mxid, sender, timestamp, sent "+
  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 id.EventID) *Message {
  51. return mq.get("SELECT chat_jid, chat_receiver, jid, mxid, sender, timestamp, sent "+
  52. "FROM message WHERE mxid=$1", mxid)
  53. }
  54. func (mq *MessageQuery) GetLastInChat(chat PortalKey) *Message {
  55. return mq.GetLastInChatBefore(chat, time.Now().Add(60 * time.Second))
  56. }
  57. func (mq *MessageQuery) GetLastInChatBefore(chat PortalKey, maxTimestamp time.Time) *Message {
  58. msg := mq.get("SELECT chat_jid, chat_receiver, jid, mxid, sender, timestamp, sent "+
  59. "FROM message WHERE chat_jid=$1 AND chat_receiver=$2 AND timestamp<=$3 AND sent=true ORDER BY timestamp DESC LIMIT 1",
  60. chat.JID, chat.Receiver, maxTimestamp.Unix())
  61. if msg == nil || msg.Timestamp.IsZero() {
  62. // Old db, we don't know what the last message is.
  63. return nil
  64. }
  65. return msg
  66. }
  67. func (mq *MessageQuery) GetFirstInChat(chat PortalKey) *Message {
  68. return mq.get("SELECT chat_jid, chat_receiver, jid, mxid, sender, timestamp, sent "+
  69. "FROM message WHERE chat_jid=$1 AND chat_receiver=$2 AND sent=true ORDER BY timestamp ASC LIMIT 1",
  70. chat.JID, chat.Receiver)
  71. }
  72. func (mq *MessageQuery) get(query string, args ...interface{}) *Message {
  73. row := mq.db.QueryRow(query, args...)
  74. if row == nil {
  75. return nil
  76. }
  77. return mq.New().Scan(row)
  78. }
  79. type Message struct {
  80. db *Database
  81. log log.Logger
  82. Chat PortalKey
  83. JID types.MessageID
  84. MXID id.EventID
  85. Sender types.JID
  86. Timestamp time.Time
  87. Sent bool
  88. }
  89. func (msg *Message) IsFakeMXID() bool {
  90. return strings.HasPrefix(msg.MXID.String(), "net.maunium.whatsapp.fake::")
  91. }
  92. func (msg *Message) Scan(row Scannable) *Message {
  93. var ts int64
  94. err := row.Scan(&msg.Chat.JID, &msg.Chat.Receiver, &msg.JID, &msg.MXID, &msg.Sender, &ts, &msg.Sent)
  95. if err != nil {
  96. if err != sql.ErrNoRows {
  97. msg.log.Errorln("Database scan failed:", err)
  98. }
  99. return nil
  100. }
  101. if ts != 0 {
  102. msg.Timestamp = time.Unix(ts, 0)
  103. }
  104. return msg
  105. }
  106. func (msg *Message) Insert() {
  107. var sender interface{} = msg.Sender
  108. // Slightly hacky hack to allow inserting empty senders (used for post-backfill dummy events)
  109. if msg.Sender.IsEmpty() {
  110. sender = ""
  111. }
  112. _, err := msg.db.Exec(`INSERT INTO message
  113. (chat_jid, chat_receiver, jid, mxid, sender, timestamp, sent)
  114. VALUES ($1, $2, $3, $4, $5, $6, $7)`,
  115. msg.Chat.JID, msg.Chat.Receiver, msg.JID, msg.MXID, sender, msg.Timestamp.Unix(), msg.Sent)
  116. if err != nil {
  117. msg.log.Warnfln("Failed to insert %s@%s: %v", msg.Chat, msg.JID, err)
  118. }
  119. }
  120. func (msg *Message) MarkSent() {
  121. msg.Sent = true
  122. _, err := msg.db.Exec("UPDATE message SET sent=true WHERE chat_jid=$1 AND chat_receiver=$2 AND jid=$3", msg.Chat.JID, msg.Chat.Receiver, msg.JID)
  123. if err != nil {
  124. msg.log.Warnfln("Failed to update %s@%s: %v", msg.Chat, msg.JID, err)
  125. }
  126. }
  127. func (msg *Message) Delete() {
  128. _, 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)
  129. if err != nil {
  130. msg.log.Warnfln("Failed to delete %s@%s: %v", msg.Chat, msg.JID, err)
  131. }
  132. }