user.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. log "maunium.net/go/maulogger/v2"
  20. "maunium.net/go/mautrix/id"
  21. "go.mau.fi/whatsmeow/types"
  22. )
  23. type UserQuery struct {
  24. db *Database
  25. log log.Logger
  26. }
  27. func (uq *UserQuery) New() *User {
  28. return &User{
  29. db: uq.db,
  30. log: uq.log,
  31. }
  32. }
  33. func (uq *UserQuery) GetAll() (users []*User) {
  34. rows, err := uq.db.Query(`SELECT mxid, username, agent, device, management_room FROM "user"`)
  35. if err != nil || rows == nil {
  36. return nil
  37. }
  38. defer rows.Close()
  39. for rows.Next() {
  40. users = append(users, uq.New().Scan(rows))
  41. }
  42. return
  43. }
  44. func (uq *UserQuery) GetByMXID(userID id.UserID) *User {
  45. row := uq.db.QueryRow(`SELECT mxid, username, agent, device, management_room FROM "user" WHERE mxid=$1`, userID)
  46. if row == nil {
  47. return nil
  48. }
  49. return uq.New().Scan(row)
  50. }
  51. func (uq *UserQuery) GetByUsername(username string) *User {
  52. row := uq.db.QueryRow(`SELECT mxid, username, agent, device, management_room FROM "user" WHERE username=$1`, username)
  53. if row == nil {
  54. return nil
  55. }
  56. return uq.New().Scan(row)
  57. }
  58. type User struct {
  59. db *Database
  60. log log.Logger
  61. MXID id.UserID
  62. JID types.JID
  63. ManagementRoom id.RoomID
  64. }
  65. func (user *User) Scan(row Scannable) *User {
  66. var username sql.NullString
  67. var device, agent sql.NullByte
  68. err := row.Scan(&user.MXID, &username, &agent, &device, &user.ManagementRoom)
  69. if err != nil {
  70. if err != sql.ErrNoRows {
  71. user.log.Errorln("Database scan failed:", err)
  72. }
  73. return nil
  74. }
  75. if len(username.String) > 0 {
  76. user.JID = types.NewADJID(username.String, agent.Byte, device.Byte)
  77. }
  78. return user
  79. }
  80. func (user *User) usernamePtr() *string {
  81. if !user.JID.IsEmpty() {
  82. return &user.JID.User
  83. }
  84. return nil
  85. }
  86. func (user *User) agentPtr() *uint8 {
  87. if !user.JID.IsEmpty() {
  88. return &user.JID.Agent
  89. }
  90. return nil
  91. }
  92. func (user *User) devicePtr() *uint8 {
  93. if !user.JID.IsEmpty() {
  94. return &user.JID.Device
  95. }
  96. return nil
  97. }
  98. func (user *User) Insert() {
  99. _, err := user.db.Exec(`INSERT INTO "user" (mxid, username, agent, device, management_room) VALUES ($1, $2, $3, $4, $5)`,
  100. user.MXID, user.usernamePtr(), user.agentPtr(), user.devicePtr(), user.ManagementRoom)
  101. if err != nil {
  102. user.log.Warnfln("Failed to insert %s: %v", user.MXID, err)
  103. }
  104. }
  105. func (user *User) Update() {
  106. _, err := user.db.Exec(`UPDATE "user" SET username=$1, agent=$2, device=$3, management_room=$4 WHERE mxid=$5`,
  107. user.usernamePtr(), user.agentPtr(), user.devicePtr(), user.ManagementRoom, user.MXID)
  108. if err != nil {
  109. user.log.Warnfln("Failed to update %s: %v", user.MXID, err)
  110. }
  111. }
  112. //type PortalKeyWithMeta struct {
  113. // PortalKey
  114. // InCommunity bool
  115. //}
  116. //
  117. //func (user *User) SetPortalKeys(newKeys []PortalKeyWithMeta) error {
  118. // tx, err := user.db.Begin()
  119. // if err != nil {
  120. // return err
  121. // }
  122. // _, err = tx.Exec("DELETE FROM user_portal WHERE user_jid=$1", user.jidPtr())
  123. // if err != nil {
  124. // _ = tx.Rollback()
  125. // return err
  126. // }
  127. // valueStrings := make([]string, len(newKeys))
  128. // values := make([]interface{}, len(newKeys)*4)
  129. // for i, key := range newKeys {
  130. // pos := i * 4
  131. // valueStrings[i] = fmt.Sprintf("($%d, $%d, $%d, $%d)", pos+1, pos+2, pos+3, pos+4)
  132. // values[pos] = user.jidPtr()
  133. // values[pos+1] = key.JID
  134. // values[pos+2] = key.Receiver
  135. // values[pos+3] = key.InCommunity
  136. // }
  137. // query := fmt.Sprintf("INSERT INTO user_portal (user_jid, portal_jid, portal_receiver, in_community) VALUES %s",
  138. // strings.Join(valueStrings, ", "))
  139. // _, err = tx.Exec(query, values...)
  140. // if err != nil {
  141. // _ = tx.Rollback()
  142. // return err
  143. // }
  144. // return tx.Commit()
  145. //}
  146. //
  147. //func (user *User) IsInPortal(key PortalKey) bool {
  148. // row := user.db.QueryRow(`SELECT EXISTS(SELECT 1 FROM user_portal WHERE user_jid=$1 AND portal_jid=$2 AND portal_receiver=$3)`, user.jidPtr(), &key.JID, &key.Receiver)
  149. // var exists bool
  150. // _ = row.Scan(&exists)
  151. // return exists
  152. //}
  153. //
  154. //func (user *User) GetPortalKeys() []PortalKey {
  155. // rows, err := user.db.Query(`SELECT portal_jid, portal_receiver FROM user_portal WHERE user_jid=$1`, user.jidPtr())
  156. // if err != nil {
  157. // user.log.Warnln("Failed to get user portal keys:", err)
  158. // return nil
  159. // }
  160. // var keys []PortalKey
  161. // for rows.Next() {
  162. // var key PortalKey
  163. // err = rows.Scan(&key.JID, &key.Receiver)
  164. // if err != nil {
  165. // user.log.Warnln("Failed to scan row:", err)
  166. // continue
  167. // }
  168. // keys = append(keys, key)
  169. // }
  170. // return keys
  171. //}
  172. //
  173. //func (user *User) GetInCommunityMap() map[PortalKey]bool {
  174. // rows, err := user.db.Query(`SELECT portal_jid, portal_receiver, in_community FROM user_portal WHERE user_jid=$1`, user.jidPtr())
  175. // if err != nil {
  176. // user.log.Warnln("Failed to get user portal keys:", err)
  177. // return nil
  178. // }
  179. // keys := make(map[PortalKey]bool)
  180. // for rows.Next() {
  181. // var key PortalKey
  182. // var inCommunity bool
  183. // err = rows.Scan(&key.JID, &key.Receiver, &inCommunity)
  184. // if err != nil {
  185. // user.log.Warnln("Failed to scan row:", err)
  186. // continue
  187. // }
  188. // keys[key] = inCommunity
  189. // }
  190. // return keys
  191. //}
  192. //
  193. //func (user *User) CreateUserPortal(newKey PortalKeyWithMeta) {
  194. // user.log.Debugfln("Creating new portal %s for %s", newKey.PortalKey.JID, newKey.PortalKey.Receiver)
  195. // _, err := user.db.Exec(`INSERT INTO user_portal (user_jid, portal_jid, portal_receiver, in_community) VALUES ($1, $2, $3, $4)`,
  196. // user.jidPtr(),
  197. // newKey.PortalKey.JID, newKey.PortalKey.Receiver,
  198. // newKey.InCommunity)
  199. // if err != nil {
  200. // user.log.Warnfln("Failed to insert %s: %v", user.MXID, err)
  201. // }
  202. //}