reaction.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2022 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. "errors"
  20. "go.mau.fi/util/dbutil"
  21. "go.mau.fi/whatsmeow/types"
  22. log "maunium.net/go/maulogger/v2"
  23. "maunium.net/go/mautrix/id"
  24. )
  25. type ReactionQuery struct {
  26. db *Database
  27. log log.Logger
  28. }
  29. func (rq *ReactionQuery) New() *Reaction {
  30. return &Reaction{
  31. db: rq.db,
  32. log: rq.log,
  33. }
  34. }
  35. const (
  36. getReactionByTargetJIDQuery = `
  37. SELECT chat_jid, chat_receiver, target_jid, sender, mxid, jid FROM reaction
  38. WHERE chat_jid=$1 AND chat_receiver=$2 AND target_jid=$3 AND sender=$4
  39. `
  40. getReactionByMXIDQuery = `
  41. SELECT chat_jid, chat_receiver, target_jid, sender, mxid, jid FROM reaction
  42. WHERE mxid=$1
  43. `
  44. upsertReactionQuery = `
  45. INSERT INTO reaction (chat_jid, chat_receiver, target_jid, sender, mxid, jid)
  46. VALUES ($1, $2, $3, $4, $5, $6)
  47. ON CONFLICT (chat_jid, chat_receiver, target_jid, sender)
  48. DO UPDATE SET mxid=excluded.mxid, jid=excluded.jid
  49. `
  50. deleteReactionQuery = `
  51. DELETE FROM reaction WHERE chat_jid=$1 AND chat_receiver=$2 AND target_jid=$3 AND sender=$4 AND mxid=$5
  52. `
  53. )
  54. func (rq *ReactionQuery) GetByTargetJID(chat PortalKey, jid types.MessageID, sender types.JID) *Reaction {
  55. return rq.maybeScan(rq.db.QueryRow(getReactionByTargetJIDQuery, chat.JID, chat.Receiver, jid, sender.ToNonAD()))
  56. }
  57. func (rq *ReactionQuery) GetByMXID(mxid id.EventID) *Reaction {
  58. return rq.maybeScan(rq.db.QueryRow(getReactionByMXIDQuery, mxid))
  59. }
  60. func (rq *ReactionQuery) maybeScan(row *sql.Row) *Reaction {
  61. if row == nil {
  62. return nil
  63. }
  64. return rq.New().Scan(row)
  65. }
  66. type Reaction struct {
  67. db *Database
  68. log log.Logger
  69. Chat PortalKey
  70. TargetJID types.MessageID
  71. Sender types.JID
  72. MXID id.EventID
  73. JID types.MessageID
  74. }
  75. func (reaction *Reaction) Scan(row dbutil.Scannable) *Reaction {
  76. err := row.Scan(&reaction.Chat.JID, &reaction.Chat.Receiver, &reaction.TargetJID, &reaction.Sender, &reaction.MXID, &reaction.JID)
  77. if err != nil {
  78. if !errors.Is(err, sql.ErrNoRows) {
  79. reaction.log.Errorln("Database scan failed:", err)
  80. }
  81. return nil
  82. }
  83. return reaction
  84. }
  85. func (reaction *Reaction) Upsert(txn dbutil.Execable) {
  86. reaction.Sender = reaction.Sender.ToNonAD()
  87. if txn == nil {
  88. txn = reaction.db
  89. }
  90. _, err := txn.Exec(upsertReactionQuery, reaction.Chat.JID, reaction.Chat.Receiver, reaction.TargetJID, reaction.Sender, reaction.MXID, reaction.JID)
  91. if err != nil {
  92. reaction.log.Warnfln("Failed to upsert reaction to %s@%s by %s: %v", reaction.Chat, reaction.TargetJID, reaction.Sender, err)
  93. }
  94. }
  95. func (reaction *Reaction) GetTarget() *Message {
  96. return reaction.db.Message.GetByJID(reaction.Chat, reaction.TargetJID)
  97. }
  98. func (reaction *Reaction) Delete() {
  99. _, err := reaction.db.Exec(deleteReactionQuery, reaction.Chat.JID, reaction.Chat.Receiver, reaction.TargetJID, reaction.Sender, reaction.MXID)
  100. if err != nil {
  101. reaction.log.Warnfln("Failed to delete reaction %s: %v", reaction.MXID, err)
  102. }
  103. }