user.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. "sync"
  20. "time"
  21. "go.mau.fi/util/dbutil"
  22. "go.mau.fi/whatsmeow/types"
  23. log "maunium.net/go/maulogger/v2"
  24. "maunium.net/go/mautrix/id"
  25. )
  26. type UserQuery struct {
  27. db *Database
  28. log log.Logger
  29. }
  30. func (uq *UserQuery) New() *User {
  31. return &User{
  32. db: uq.db,
  33. log: uq.log,
  34. lastReadCache: make(map[PortalKey]time.Time),
  35. inSpaceCache: make(map[PortalKey]bool),
  36. }
  37. }
  38. func (uq *UserQuery) GetAll() (users []*User) {
  39. rows, err := uq.db.Query(`SELECT mxid, username, agent, device, management_room, space_room, phone_last_seen, phone_last_pinged, timezone FROM "user"`)
  40. if err != nil || rows == nil {
  41. return nil
  42. }
  43. defer rows.Close()
  44. for rows.Next() {
  45. users = append(users, uq.New().Scan(rows))
  46. }
  47. return
  48. }
  49. func (uq *UserQuery) GetByMXID(userID id.UserID) *User {
  50. row := uq.db.QueryRow(`SELECT mxid, username, agent, device, management_room, space_room, phone_last_seen, phone_last_pinged, timezone FROM "user" WHERE mxid=$1`, userID)
  51. if row == nil {
  52. return nil
  53. }
  54. return uq.New().Scan(row)
  55. }
  56. func (uq *UserQuery) GetByUsername(username string) *User {
  57. row := uq.db.QueryRow(`SELECT mxid, username, agent, device, management_room, space_room, phone_last_seen, phone_last_pinged, timezone FROM "user" WHERE username=$1`, username)
  58. if row == nil {
  59. return nil
  60. }
  61. return uq.New().Scan(row)
  62. }
  63. type User struct {
  64. db *Database
  65. log log.Logger
  66. MXID id.UserID
  67. JID types.JID
  68. ManagementRoom id.RoomID
  69. SpaceRoom id.RoomID
  70. PhoneLastSeen time.Time
  71. PhoneLastPinged time.Time
  72. Timezone string
  73. lastReadCache map[PortalKey]time.Time
  74. lastReadCacheLock sync.Mutex
  75. inSpaceCache map[PortalKey]bool
  76. inSpaceCacheLock sync.Mutex
  77. }
  78. func (user *User) Scan(row dbutil.Scannable) *User {
  79. var username, timezone sql.NullString
  80. var device, agent sql.NullByte
  81. var phoneLastSeen, phoneLastPinged sql.NullInt64
  82. err := row.Scan(&user.MXID, &username, &agent, &device, &user.ManagementRoom, &user.SpaceRoom, &phoneLastSeen, &phoneLastPinged, &timezone)
  83. if err != nil {
  84. if err != sql.ErrNoRows {
  85. user.log.Errorln("Database scan failed:", err)
  86. }
  87. return nil
  88. }
  89. user.Timezone = timezone.String
  90. if len(username.String) > 0 {
  91. user.JID = types.NewADJID(username.String, agent.Byte, device.Byte)
  92. }
  93. if phoneLastSeen.Valid {
  94. user.PhoneLastSeen = time.Unix(phoneLastSeen.Int64, 0)
  95. }
  96. if phoneLastPinged.Valid {
  97. user.PhoneLastPinged = time.Unix(phoneLastPinged.Int64, 0)
  98. }
  99. return user
  100. }
  101. func (user *User) usernamePtr() *string {
  102. if !user.JID.IsEmpty() {
  103. return &user.JID.User
  104. }
  105. return nil
  106. }
  107. func (user *User) agentPtr() *uint8 {
  108. if !user.JID.IsEmpty() {
  109. zero := uint8(0)
  110. return &zero
  111. }
  112. return nil
  113. }
  114. func (user *User) devicePtr() *uint8 {
  115. if !user.JID.IsEmpty() {
  116. device := uint8(user.JID.Device)
  117. return &device
  118. }
  119. return nil
  120. }
  121. func (user *User) phoneLastSeenPtr() *int64 {
  122. if user.PhoneLastSeen.IsZero() {
  123. return nil
  124. }
  125. ts := user.PhoneLastSeen.Unix()
  126. return &ts
  127. }
  128. func (user *User) phoneLastPingedPtr() *int64 {
  129. if user.PhoneLastPinged.IsZero() {
  130. return nil
  131. }
  132. ts := user.PhoneLastPinged.Unix()
  133. return &ts
  134. }
  135. func (user *User) Insert() {
  136. _, err := user.db.Exec(`INSERT INTO "user" (mxid, username, agent, device, management_room, space_room, phone_last_seen, phone_last_pinged, timezone) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)`,
  137. user.MXID, user.usernamePtr(), user.agentPtr(), user.devicePtr(), user.ManagementRoom, user.SpaceRoom, user.phoneLastSeenPtr(), user.phoneLastPingedPtr(), user.Timezone)
  138. if err != nil {
  139. user.log.Warnfln("Failed to insert %s: %v", user.MXID, err)
  140. }
  141. }
  142. func (user *User) Update() {
  143. _, err := user.db.Exec(`UPDATE "user" SET username=$1, agent=$2, device=$3, management_room=$4, space_room=$5, phone_last_seen=$6, phone_last_pinged=$7, timezone=$8 WHERE mxid=$9`,
  144. user.usernamePtr(), user.agentPtr(), user.devicePtr(), user.ManagementRoom, user.SpaceRoom, user.phoneLastSeenPtr(), user.phoneLastPingedPtr(), user.Timezone, user.MXID)
  145. if err != nil {
  146. user.log.Warnfln("Failed to update %s: %v", user.MXID, err)
  147. }
  148. }
  149. func (user *User) GetLastAppStateKeyID() ([]byte, error) {
  150. var keyID []byte
  151. err := user.db.QueryRow("SELECT key_id FROM whatsmeow_app_state_sync_keys ORDER BY timestamp DESC LIMIT 1").Scan(&keyID)
  152. return keyID, err
  153. }