user.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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, timezone 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, timezone 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, timezone 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. Timezone string
  72. lastReadCache map[PortalKey]time.Time
  73. lastReadCacheLock sync.Mutex
  74. inSpaceCache map[PortalKey]bool
  75. inSpaceCacheLock sync.Mutex
  76. }
  77. func (user *User) Scan(row Scannable) *User {
  78. var username sql.NullString
  79. var device, agent sql.NullByte
  80. var phoneLastSeen, phoneLastPinged sql.NullInt64
  81. err := row.Scan(&user.MXID, &username, &agent, &device, &user.ManagementRoom, &user.SpaceRoom, &phoneLastSeen, &phoneLastPinged, &user.Timezone)
  82. if err != nil {
  83. if err != sql.ErrNoRows {
  84. user.log.Errorln("Database scan failed:", err)
  85. }
  86. return nil
  87. }
  88. if len(username.String) > 0 {
  89. user.JID = types.NewADJID(username.String, agent.Byte, device.Byte)
  90. }
  91. if phoneLastSeen.Valid {
  92. user.PhoneLastSeen = time.Unix(phoneLastSeen.Int64, 0)
  93. }
  94. if phoneLastPinged.Valid {
  95. user.PhoneLastPinged = time.Unix(phoneLastPinged.Int64, 0)
  96. }
  97. return user
  98. }
  99. func (user *User) usernamePtr() *string {
  100. if !user.JID.IsEmpty() {
  101. return &user.JID.User
  102. }
  103. return nil
  104. }
  105. func (user *User) agentPtr() *uint8 {
  106. if !user.JID.IsEmpty() {
  107. return &user.JID.Agent
  108. }
  109. return nil
  110. }
  111. func (user *User) devicePtr() *uint8 {
  112. if !user.JID.IsEmpty() {
  113. return &user.JID.Device
  114. }
  115. return nil
  116. }
  117. func (user *User) phoneLastSeenPtr() *int64 {
  118. if user.PhoneLastSeen.IsZero() {
  119. return nil
  120. }
  121. ts := user.PhoneLastSeen.Unix()
  122. return &ts
  123. }
  124. func (user *User) phoneLastPingedPtr() *int64 {
  125. if user.PhoneLastPinged.IsZero() {
  126. return nil
  127. }
  128. ts := user.PhoneLastPinged.Unix()
  129. return &ts
  130. }
  131. func (user *User) Insert() {
  132. _, 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)`,
  133. user.MXID, user.usernamePtr(), user.agentPtr(), user.devicePtr(), user.ManagementRoom, user.SpaceRoom, user.phoneLastSeenPtr(), user.phoneLastPingedPtr(), user.Timezone)
  134. if err != nil {
  135. user.log.Warnfln("Failed to insert %s: %v", user.MXID, err)
  136. }
  137. }
  138. func (user *User) Update() {
  139. _, 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`,
  140. user.usernamePtr(), user.agentPtr(), user.devicePtr(), user.ManagementRoom, user.SpaceRoom, user.phoneLastSeenPtr(), user.phoneLastPingedPtr(), user.Timezone, user.MXID)
  141. if err != nil {
  142. user.log.Warnfln("Failed to update %s: %v", user.MXID, err)
  143. }
  144. }