message.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. "database/sql"
  19. "strings"
  20. "time"
  21. "github.com/Rhymen/go-whatsapp"
  22. log "maunium.net/go/maulogger/v2"
  23. "maunium.net/go/mautrix/id"
  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 whatsapp.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().Unix()+60)
  56. }
  57. func (mq *MessageQuery) GetLastInChatBefore(chat PortalKey, maxTimestamp int64) *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)
  61. if msg == nil || msg.Timestamp == 0 {
  62. // Old db, we don't know what the last message is.
  63. return nil
  64. }
  65. return msg
  66. }
  67. func (mq *MessageQuery) get(query string, args ...interface{}) *Message {
  68. row := mq.db.QueryRow(query, args...)
  69. if row == nil {
  70. return nil
  71. }
  72. return mq.New().Scan(row)
  73. }
  74. type Message struct {
  75. db *Database
  76. log log.Logger
  77. Chat PortalKey
  78. JID whatsapp.MessageID
  79. MXID id.EventID
  80. Sender whatsapp.JID
  81. Timestamp int64
  82. Sent bool
  83. }
  84. func (msg *Message) IsFakeMXID() bool {
  85. return strings.HasPrefix(msg.MXID.String(), "net.maunium.whatsapp.fake::")
  86. }
  87. func (msg *Message) Scan(row Scannable) *Message {
  88. err := row.Scan(&msg.Chat.JID, &msg.Chat.Receiver, &msg.JID, &msg.MXID, &msg.Sender, &msg.Timestamp, &msg.Sent)
  89. if err != nil {
  90. if err != sql.ErrNoRows {
  91. msg.log.Errorln("Database scan failed:", err)
  92. }
  93. return nil
  94. }
  95. return msg
  96. }
  97. func (msg *Message) Insert() {
  98. _, err := msg.db.Exec(`INSERT INTO message
  99. (chat_jid, chat_receiver, jid, mxid, sender, timestamp, sent)
  100. VALUES ($1, $2, $3, $4, $5, $6, $7)`,
  101. msg.Chat.JID, msg.Chat.Receiver, msg.JID, msg.MXID, msg.Sender, msg.Timestamp, msg.Sent)
  102. if err != nil {
  103. msg.log.Warnfln("Failed to insert %s@%s: %v", msg.Chat, msg.JID, err)
  104. }
  105. }
  106. func (msg *Message) MarkSent() {
  107. msg.Sent = true
  108. _, 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)
  109. if err != nil {
  110. msg.log.Warnfln("Failed to update %s@%s: %v", msg.Chat, msg.JID, err)
  111. }
  112. }
  113. func (msg *Message) Delete() {
  114. _, 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)
  115. if err != nil {
  116. msg.log.Warnfln("Failed to delete %s@%s: %v", msg.Chat, msg.JID, err)
  117. }
  118. }