portal.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2020 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. "strings"
  20. log "maunium.net/go/maulogger/v2"
  21. "maunium.net/go/mautrix/id"
  22. "maunium.net/go/mautrix-whatsapp/types"
  23. )
  24. type PortalKey struct {
  25. JID types.WhatsAppID
  26. Receiver types.WhatsAppID
  27. }
  28. func GroupPortalKey(jid types.WhatsAppID) PortalKey {
  29. return PortalKey{
  30. JID: jid,
  31. Receiver: jid,
  32. }
  33. }
  34. func NewPortalKey(jid, receiver types.WhatsAppID) PortalKey {
  35. if strings.HasSuffix(jid, "@g.us") {
  36. receiver = jid
  37. }
  38. return PortalKey{
  39. JID: jid,
  40. Receiver: receiver,
  41. }
  42. }
  43. func (key PortalKey) String() string {
  44. if key.Receiver == key.JID {
  45. return key.JID
  46. }
  47. return key.JID + "-" + key.Receiver
  48. }
  49. type PortalQuery struct {
  50. db *Database
  51. log log.Logger
  52. }
  53. func (pq *PortalQuery) New() *Portal {
  54. return &Portal{
  55. db: pq.db,
  56. log: pq.log,
  57. }
  58. }
  59. func (pq *PortalQuery) GetAll() []*Portal {
  60. return pq.getAll("SELECT * FROM portal")
  61. }
  62. func (pq *PortalQuery) GetByJID(key PortalKey) *Portal {
  63. return pq.get("SELECT * FROM portal WHERE jid=$1 AND receiver=$2", key.JID, key.Receiver)
  64. }
  65. func (pq *PortalQuery) GetByMXID(mxid id.RoomID) *Portal {
  66. return pq.get("SELECT * FROM portal WHERE mxid=$1", mxid)
  67. }
  68. func (pq *PortalQuery) GetAllByJID(jid types.WhatsAppID) []*Portal {
  69. return pq.getAll("SELECT * FROM portal WHERE jid=$1", jid)
  70. }
  71. func (pq *PortalQuery) getAll(query string, args ...interface{}) (portals []*Portal) {
  72. rows, err := pq.db.Query(query, args...)
  73. if err != nil || rows == nil {
  74. return nil
  75. }
  76. defer rows.Close()
  77. for rows.Next() {
  78. portals = append(portals, pq.New().Scan(rows))
  79. }
  80. return
  81. }
  82. func (pq *PortalQuery) get(query string, args ...interface{}) *Portal {
  83. row := pq.db.QueryRow(query, args...)
  84. if row == nil {
  85. return nil
  86. }
  87. return pq.New().Scan(row)
  88. }
  89. type Portal struct {
  90. db *Database
  91. log log.Logger
  92. Key PortalKey
  93. MXID id.RoomID
  94. Name string
  95. Topic string
  96. Avatar string
  97. AvatarURL id.ContentURI
  98. Encrypted bool
  99. }
  100. func (portal *Portal) Scan(row Scannable) *Portal {
  101. var mxid, avatarURL sql.NullString
  102. err := row.Scan(&portal.Key.JID, &portal.Key.Receiver, &mxid, &portal.Name, &portal.Topic, &portal.Avatar, &avatarURL, &portal.Encrypted)
  103. if err != nil {
  104. if err != sql.ErrNoRows {
  105. portal.log.Errorln("Database scan failed:", err)
  106. }
  107. return nil
  108. }
  109. portal.MXID = id.RoomID(mxid.String)
  110. portal.AvatarURL, _ = id.ParseContentURI(avatarURL.String)
  111. return portal
  112. }
  113. func (portal *Portal) mxidPtr() *id.RoomID {
  114. if len(portal.MXID) > 0 {
  115. return &portal.MXID
  116. }
  117. return nil
  118. }
  119. func (portal *Portal) Insert() {
  120. _, err := portal.db.Exec("INSERT INTO portal (jid, receiver, mxid, name, topic, avatar, avatar_url, encrypted) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)",
  121. portal.Key.JID, portal.Key.Receiver, portal.mxidPtr(), portal.Name, portal.Topic, portal.Avatar, portal.AvatarURL.String(), portal.Encrypted)
  122. if err != nil {
  123. portal.log.Warnfln("Failed to insert %s: %v", portal.Key, err)
  124. }
  125. }
  126. func (portal *Portal) Update() {
  127. var mxid *id.RoomID
  128. if len(portal.MXID) > 0 {
  129. mxid = &portal.MXID
  130. }
  131. _, err := portal.db.Exec("UPDATE portal SET mxid=$1, name=$2, topic=$3, avatar=$4, avatar_url=$5, encrypted=$6 WHERE jid=$7 AND receiver=$8",
  132. mxid, portal.Name, portal.Topic, portal.Avatar, portal.AvatarURL.String(), portal.Encrypted, portal.Key.JID, portal.Key.Receiver)
  133. if err != nil {
  134. portal.log.Warnfln("Failed to update %s: %v", portal.Key, err)
  135. }
  136. }
  137. func (portal *Portal) Delete() {
  138. _, err := portal.db.Exec("DELETE FROM portal WHERE jid=$1 AND receiver=$2", portal.Key.JID, portal.Key.Receiver)
  139. if err != nil {
  140. portal.log.Warnfln("Failed to delete %s: %v", portal.Key, err)
  141. }
  142. }
  143. func (portal *Portal) GetUserIDs() []id.UserID {
  144. rows, err := portal.db.Query(`SELECT "user".mxid FROM "user", user_portal
  145. WHERE "user".jid=user_portal.user_jid
  146. AND user_portal.portal_jid=$1
  147. AND user_portal.portal_receiver=$2`,
  148. portal.Key.JID, portal.Key.Receiver)
  149. if err != nil {
  150. portal.log.Debugln("Failed to get portal user ids:", err)
  151. return nil
  152. }
  153. var userIDs []id.UserID
  154. for rows.Next() {
  155. var userID id.UserID
  156. err = rows.Scan(&userID)
  157. if err != nil {
  158. portal.log.Warnln("Failed to scan row:", err)
  159. continue
  160. }
  161. userIDs = append(userIDs, userID)
  162. }
  163. return userIDs
  164. }