reaction.go 3.1 KB

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