user.go 4.7 KB

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