puppet.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. log "maunium.net/go/maulogger/v2"
  20. "maunium.net/go/mautrix-whatsapp/types"
  21. "maunium.net/go/mautrix/id"
  22. )
  23. type PuppetQuery struct {
  24. db *Database
  25. log log.Logger
  26. }
  27. func (pq *PuppetQuery) New() *Puppet {
  28. return &Puppet{
  29. db: pq.db,
  30. log: pq.log,
  31. }
  32. }
  33. func (pq *PuppetQuery) GetAll() (puppets []*Puppet) {
  34. rows, err := pq.db.Query("SELECT jid, avatar, avatar_url, displayname, name_quality, custom_mxid, access_token, next_batch FROM puppet")
  35. if err != nil || rows == nil {
  36. return nil
  37. }
  38. defer rows.Close()
  39. for rows.Next() {
  40. puppets = append(puppets, pq.New().Scan(rows))
  41. }
  42. return
  43. }
  44. func (pq *PuppetQuery) Get(jid types.WhatsAppID) *Puppet {
  45. row := pq.db.QueryRow("SELECT jid, avatar, avatar_url, displayname, name_quality, custom_mxid, access_token, next_batch FROM puppet WHERE jid=$1", jid)
  46. if row == nil {
  47. return nil
  48. }
  49. return pq.New().Scan(row)
  50. }
  51. func (pq *PuppetQuery) GetByCustomMXID(mxid id.UserID) *Puppet {
  52. row := pq.db.QueryRow("SELECT jid, avatar, avatar_url, displayname, name_quality, custom_mxid, access_token, next_batch FROM puppet WHERE custom_mxid=$1", mxid)
  53. if row == nil {
  54. return nil
  55. }
  56. return pq.New().Scan(row)
  57. }
  58. func (pq *PuppetQuery) GetAllWithCustomMXID() (puppets []*Puppet) {
  59. rows, err := pq.db.Query("SELECT jid, avatar, avatar_url, displayname, name_quality, custom_mxid, access_token, next_batch FROM puppet WHERE custom_mxid<>''")
  60. if err != nil || rows == nil {
  61. return nil
  62. }
  63. defer rows.Close()
  64. for rows.Next() {
  65. puppets = append(puppets, pq.New().Scan(rows))
  66. }
  67. return
  68. }
  69. type Puppet struct {
  70. db *Database
  71. log log.Logger
  72. JID types.WhatsAppID
  73. Avatar string
  74. AvatarURL id.ContentURI
  75. Displayname string
  76. NameQuality int8
  77. CustomMXID id.UserID
  78. AccessToken string
  79. NextBatch string
  80. }
  81. func (puppet *Puppet) Scan(row Scannable) *Puppet {
  82. var displayname, avatar, avatarURL, customMXID, accessToken, nextBatch sql.NullString
  83. var quality sql.NullInt64
  84. err := row.Scan(&puppet.JID, &avatar, &avatarURL, &displayname, &quality, &customMXID, &accessToken, &nextBatch)
  85. if err != nil {
  86. if err != sql.ErrNoRows {
  87. puppet.log.Errorln("Database scan failed:", err)
  88. }
  89. return nil
  90. }
  91. puppet.Displayname = displayname.String
  92. puppet.Avatar = avatar.String
  93. puppet.AvatarURL, _ = id.ParseContentURI(avatarURL.String)
  94. puppet.NameQuality = int8(quality.Int64)
  95. puppet.CustomMXID = id.UserID(customMXID.String)
  96. puppet.AccessToken = accessToken.String
  97. puppet.NextBatch = nextBatch.String
  98. return puppet
  99. }
  100. func (puppet *Puppet) Insert() {
  101. _, err := puppet.db.Exec("INSERT INTO puppet (jid, avatar, avatar_url, displayname, name_quality, custom_mxid, access_token, next_batch) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)",
  102. puppet.JID, puppet.Avatar, puppet.AvatarURL.String(), puppet.Displayname, puppet.NameQuality, puppet.CustomMXID, puppet.AccessToken, puppet.NextBatch)
  103. if err != nil {
  104. puppet.log.Warnfln("Failed to insert %s: %v", puppet.JID, err)
  105. }
  106. }
  107. func (puppet *Puppet) Update() {
  108. _, err := puppet.db.Exec("UPDATE puppet SET displayname=$1, name_quality=$2, avatar=$3, avatar_url=$4, custom_mxid=$5, access_token=$6, next_batch=$7 WHERE jid=$8",
  109. puppet.Displayname, puppet.NameQuality, puppet.Avatar, puppet.AvatarURL.String(), puppet.CustomMXID, puppet.AccessToken, puppet.NextBatch, puppet.JID)
  110. if err != nil {
  111. puppet.log.Warnfln("Failed to update %s->%s: %v", puppet.JID, err)
  112. }
  113. }