message.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2018 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. log "maunium.net/go/maulogger"
  20. "maunium.net/go/mautrix-whatsapp/types"
  21. )
  22. type MessageQuery struct {
  23. db *Database
  24. log log.Logger
  25. }
  26. func (mq *MessageQuery) CreateTable() error {
  27. _, err := mq.db.Exec(`CREATE TABLE IF NOT EXISTS message (
  28. chat_jid VARCHAR(25) NOT NULL,
  29. chat_receiver VARCHAR(25) NOT NULL,
  30. jid VARCHAR(255) NOT NULL,
  31. mxid VARCHAR(255) NOT NULL UNIQUE,
  32. PRIMARY KEY (chat_jid, jid),
  33. FOREIGN KEY (chat_jid, chat_receiver) REFERENCES portal(jid, receiver)
  34. )`)
  35. return err
  36. }
  37. func (mq *MessageQuery) New() *Message {
  38. return &Message{
  39. db: mq.db,
  40. log: mq.log,
  41. }
  42. }
  43. func (mq *MessageQuery) GetAll(chat PortalKey) (messages []*Message) {
  44. rows, err := mq.db.Query("SELECT * FROM message WHERE chat_jid=? AND chat_receiver=?", chat.JID, chat.Receiver)
  45. if err != nil || rows == nil {
  46. return nil
  47. }
  48. defer rows.Close()
  49. for rows.Next() {
  50. messages = append(messages, mq.New().Scan(rows))
  51. }
  52. return
  53. }
  54. func (mq *MessageQuery) GetByJID(chat PortalKey, jid types.WhatsAppMessageID) *Message {
  55. return mq.get("SELECT * FROM message WHERE chat_jid=? AND chat_receiver=? AND jid=?", chat.JID, chat.Receiver, jid)
  56. }
  57. func (mq *MessageQuery) GetByMXID(mxid types.MatrixEventID) *Message {
  58. return mq.get("SELECT * FROM message WHERE mxid=?", mxid)
  59. }
  60. func (mq *MessageQuery) get(query string, args ...interface{}) *Message {
  61. row := mq.db.QueryRow(query, args...)
  62. if row == nil {
  63. return nil
  64. }
  65. return mq.New().Scan(row)
  66. }
  67. type Message struct {
  68. db *Database
  69. log log.Logger
  70. Chat PortalKey
  71. JID types.WhatsAppMessageID
  72. MXID types.MatrixEventID
  73. }
  74. func (msg *Message) Scan(row Scannable) *Message {
  75. err := row.Scan(&msg.Chat.JID, &msg.Chat.Receiver, &msg.JID, &msg.MXID)
  76. if err != nil {
  77. if err != sql.ErrNoRows {
  78. msg.log.Errorln("Database scan failed:", err)
  79. }
  80. return nil
  81. }
  82. return msg
  83. }
  84. func (msg *Message) Insert() error {
  85. _, err := msg.db.Exec("INSERT INTO message VALUES (?, ?, ?)", msg.Chat.JID, msg.Chat.Receiver, msg.JID, msg.MXID)
  86. if err != nil {
  87. msg.log.Warnfln("Failed to update %s: %v", msg.Chat, msg.JID, err)
  88. }
  89. return err
  90. }
  91. func (msg *Message) Update() error {
  92. _, err := msg.db.Exec("UPDATE portal SET mxid=? WHERE chat_jid=? AND chat_receiver=? AND jid=?", msg.MXID, msg.Chat.JID, msg.Chat.Receiver, msg.JID)
  93. if err != nil {
  94. msg.log.Warnfln("Failed to update %s: %v", msg.Chat, msg.JID, err)
  95. }
  96. return err
  97. }