polloption.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. "fmt"
  19. "strings"
  20. "github.com/lib/pq"
  21. "maunium.net/go/mautrix/util/dbutil"
  22. )
  23. func scanPollOptionMapping(rows dbutil.Rows) (id string, hashArr [32]byte, err error) {
  24. var hash []byte
  25. err = rows.Scan(&id, &hash)
  26. if err != nil {
  27. // return below
  28. } else if len(hash) != 32 {
  29. err = fmt.Errorf("unexpected hash length %d", len(hash))
  30. } else {
  31. hashArr = *(*[32]byte)(hash)
  32. }
  33. return
  34. }
  35. func (msg *Message) PutPollOptions(opts map[[32]byte]string) {
  36. query := "INSERT INTO poll_option_id (msg_mxid, opt_id, opt_hash) VALUES ($1, $2, $3)"
  37. args := make([]any, len(opts)*2+1)
  38. placeholders := make([]string, len(opts))
  39. args[0] = msg.MXID
  40. i := 0
  41. for hash, id := range opts {
  42. args[i*2+1] = id
  43. hashCopy := hash
  44. args[i*2+2] = hashCopy[:]
  45. placeholders[i] = fmt.Sprintf("($1, $%d, $%d)", i*2+2, i*2+3)
  46. i++
  47. }
  48. query = strings.ReplaceAll(query, "($1, $2, $3)", strings.Join(placeholders, ","))
  49. _, err := msg.db.Exec(query, args...)
  50. if err != nil {
  51. msg.log.Errorfln("Failed to save poll options for %s: %v", msg.MXID, err)
  52. }
  53. }
  54. func (msg *Message) GetPollOptionIDs(hashes [][]byte) map[[32]byte]string {
  55. query := "SELECT opt_id, opt_hash FROM poll_option_id WHERE msg_mxid=$1 AND opt_hash = ANY($2)"
  56. var args []any
  57. if msg.db.Dialect == dbutil.Postgres {
  58. args = []any{msg.MXID, pq.Array(hashes)}
  59. } else {
  60. query = strings.ReplaceAll(query, " = ANY($2)", fmt.Sprintf(" IN (%s)", strings.TrimSuffix(strings.Repeat("?,", len(hashes)), ",")))
  61. args = make([]any, len(hashes)+1)
  62. args[0] = msg.MXID
  63. for i, hash := range hashes {
  64. args[i+1] = hash
  65. }
  66. }
  67. ids := make(map[[32]byte]string, len(hashes))
  68. rows, err := msg.db.Query(query, args...)
  69. if err != nil {
  70. msg.log.Errorfln("Failed to query poll option IDs for %s: %v", msg.MXID, err)
  71. } else {
  72. for rows.Next() {
  73. id, hash, err := scanPollOptionMapping(rows)
  74. if err != nil {
  75. msg.log.Errorfln("Failed to scan poll option ID for %s: %v", msg.MXID, err)
  76. break
  77. }
  78. ids[hash] = id
  79. }
  80. }
  81. return ids
  82. }
  83. func (msg *Message) GetPollOptionHashes(ids []string) map[string][32]byte {
  84. query := "SELECT opt_id, opt_hash FROM poll_option_id WHERE msg_mxid=$1 AND opt_id = ANY($2)"
  85. var args []any
  86. if msg.db.Dialect == dbutil.Postgres {
  87. args = []any{msg.MXID, pq.Array(ids)}
  88. } else {
  89. query = strings.ReplaceAll(query, " = ANY($2)", fmt.Sprintf(" IN (%s)", strings.TrimSuffix(strings.Repeat("?,", len(ids)), ",")))
  90. args = make([]any, len(ids)+1)
  91. args[0] = msg.MXID
  92. for i, id := range ids {
  93. args[i+1] = id
  94. }
  95. }
  96. hashes := make(map[string][32]byte, len(ids))
  97. rows, err := msg.db.Query(query, args...)
  98. if err != nil {
  99. msg.log.Errorfln("Failed to query poll option hashes for %s: %v", msg.MXID, err)
  100. } else {
  101. for rows.Next() {
  102. id, hash, err := scanPollOptionMapping(rows)
  103. if err != nil {
  104. msg.log.Errorfln("Failed to scan poll option hash for %s: %v", msg.MXID, err)
  105. break
  106. }
  107. hashes[id] = hash
  108. }
  109. }
  110. return hashes
  111. }