user.go 4.5 KB

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