puppet.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2021 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. "time"
  20. log "maunium.net/go/maulogger/v2"
  21. "maunium.net/go/mautrix/id"
  22. "maunium.net/go/mautrix/util/dbutil"
  23. "go.mau.fi/whatsmeow/types"
  24. )
  25. type PuppetQuery struct {
  26. db *Database
  27. log log.Logger
  28. }
  29. func (pq *PuppetQuery) New() *Puppet {
  30. return &Puppet{
  31. db: pq.db,
  32. log: pq.log,
  33. EnablePresence: true,
  34. EnableReceipts: true,
  35. }
  36. }
  37. func (pq *PuppetQuery) GetAll() (puppets []*Puppet) {
  38. rows, err := pq.db.Query("SELECT username, avatar, avatar_url, displayname, name_quality, name_set, avatar_set, last_sync, custom_mxid, access_token, next_batch, enable_presence, enable_receipts FROM puppet")
  39. if err != nil || rows == nil {
  40. return nil
  41. }
  42. defer rows.Close()
  43. for rows.Next() {
  44. puppets = append(puppets, pq.New().Scan(rows))
  45. }
  46. return
  47. }
  48. func (pq *PuppetQuery) Get(jid types.JID) *Puppet {
  49. row := pq.db.QueryRow("SELECT username, avatar, avatar_url, displayname, name_quality, name_set, avatar_set, last_sync, custom_mxid, access_token, next_batch, enable_presence, enable_receipts FROM puppet WHERE username=$1", jid.User)
  50. if row == nil {
  51. return nil
  52. }
  53. return pq.New().Scan(row)
  54. }
  55. func (pq *PuppetQuery) GetByCustomMXID(mxid id.UserID) *Puppet {
  56. row := pq.db.QueryRow("SELECT username, avatar, avatar_url, displayname, name_quality, name_set, avatar_set, last_sync, custom_mxid, access_token, next_batch, enable_presence, enable_receipts FROM puppet WHERE custom_mxid=$1", mxid)
  57. if row == nil {
  58. return nil
  59. }
  60. return pq.New().Scan(row)
  61. }
  62. func (pq *PuppetQuery) GetAllWithCustomMXID() (puppets []*Puppet) {
  63. rows, err := pq.db.Query("SELECT username, avatar, avatar_url, displayname, name_quality, name_set, avatar_set, last_sync, custom_mxid, access_token, next_batch, enable_presence, enable_receipts FROM puppet WHERE custom_mxid<>''")
  64. if err != nil || rows == nil {
  65. return nil
  66. }
  67. defer rows.Close()
  68. for rows.Next() {
  69. puppets = append(puppets, pq.New().Scan(rows))
  70. }
  71. return
  72. }
  73. type Puppet struct {
  74. db *Database
  75. log log.Logger
  76. JID types.JID
  77. Avatar string
  78. AvatarURL id.ContentURI
  79. AvatarSet bool
  80. Displayname string
  81. NameQuality int8
  82. NameSet bool
  83. LastSync time.Time
  84. CustomMXID id.UserID
  85. AccessToken string
  86. NextBatch string
  87. EnablePresence bool
  88. EnableReceipts bool
  89. }
  90. func (puppet *Puppet) Scan(row dbutil.Scannable) *Puppet {
  91. var displayname, avatar, avatarURL, customMXID, accessToken, nextBatch sql.NullString
  92. var quality, lastSync sql.NullInt64
  93. var enablePresence, enableReceipts, nameSet, avatarSet sql.NullBool
  94. var username string
  95. err := row.Scan(&username, &avatar, &avatarURL, &displayname, &quality, &nameSet, &avatarSet, &lastSync, &customMXID, &accessToken, &nextBatch, &enablePresence, &enableReceipts)
  96. if err != nil {
  97. if err != sql.ErrNoRows {
  98. puppet.log.Errorln("Database scan failed:", err)
  99. }
  100. return nil
  101. }
  102. puppet.JID = types.NewJID(username, types.DefaultUserServer)
  103. puppet.Displayname = displayname.String
  104. puppet.Avatar = avatar.String
  105. puppet.AvatarURL, _ = id.ParseContentURI(avatarURL.String)
  106. puppet.NameQuality = int8(quality.Int64)
  107. puppet.NameSet = nameSet.Bool
  108. puppet.AvatarSet = avatarSet.Bool
  109. if lastSync.Int64 > 0 {
  110. puppet.LastSync = time.Unix(lastSync.Int64, 0)
  111. }
  112. puppet.CustomMXID = id.UserID(customMXID.String)
  113. puppet.AccessToken = accessToken.String
  114. puppet.NextBatch = nextBatch.String
  115. puppet.EnablePresence = enablePresence.Bool
  116. puppet.EnableReceipts = enableReceipts.Bool
  117. return puppet
  118. }
  119. func (puppet *Puppet) Insert() {
  120. if puppet.JID.Server != types.DefaultUserServer {
  121. puppet.log.Warnfln("Not inserting %s: not a user", puppet.JID)
  122. return
  123. }
  124. var lastSyncTs int64
  125. if !puppet.LastSync.IsZero() {
  126. lastSyncTs = puppet.LastSync.Unix()
  127. }
  128. _, err := puppet.db.Exec(`
  129. INSERT INTO puppet (username, avatar, avatar_url, avatar_set, displayname, name_quality, name_set, last_sync,
  130. custom_mxid, access_token, next_batch, enable_presence, enable_receipts)
  131. VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
  132. `, puppet.JID.User, puppet.Avatar, puppet.AvatarURL.String(), puppet.AvatarSet, puppet.Displayname,
  133. puppet.NameQuality, puppet.NameSet, lastSyncTs, puppet.CustomMXID, puppet.AccessToken, puppet.NextBatch,
  134. puppet.EnablePresence, puppet.EnableReceipts,
  135. )
  136. if err != nil {
  137. puppet.log.Warnfln("Failed to insert %s: %v", puppet.JID, err)
  138. }
  139. }
  140. func (puppet *Puppet) Update() {
  141. var lastSyncTs int64
  142. if !puppet.LastSync.IsZero() {
  143. lastSyncTs = puppet.LastSync.Unix()
  144. }
  145. _, err := puppet.db.Exec(`
  146. UPDATE puppet
  147. SET displayname=$1, name_quality=$2, name_set=$3, avatar=$4, avatar_url=$5, avatar_set=$6, last_sync=$7,
  148. custom_mxid=$8, access_token=$9, next_batch=$10, enable_presence=$11, enable_receipts=$12
  149. WHERE username=$13
  150. `, puppet.Displayname, puppet.NameQuality, puppet.NameSet, puppet.Avatar, puppet.AvatarURL.String(), puppet.AvatarSet,
  151. lastSyncTs, puppet.CustomMXID, puppet.AccessToken, puppet.NextBatch, puppet.EnablePresence, puppet.EnableReceipts,
  152. puppet.JID.User)
  153. if err != nil {
  154. puppet.log.Warnfln("Failed to update %s: %v", puppet.JID, err)
  155. }
  156. }