portal.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 PortalKey struct {
  24. JID types.JID
  25. Receiver types.JID
  26. }
  27. func NewPortalKey(jid, receiver types.JID) PortalKey {
  28. if jid.Server == types.GroupServer {
  29. receiver = jid
  30. } else if jid.Server == types.LegacyUserServer {
  31. jid.Server = types.DefaultUserServer
  32. }
  33. return PortalKey{
  34. JID: jid.ToNonAD(),
  35. Receiver: receiver.ToNonAD(),
  36. }
  37. }
  38. func (key PortalKey) String() string {
  39. if key.Receiver == key.JID {
  40. return key.JID.String()
  41. }
  42. return key.JID.String() + "-" + key.Receiver.String()
  43. }
  44. type PortalQuery struct {
  45. db *Database
  46. log log.Logger
  47. }
  48. func (pq *PortalQuery) New() *Portal {
  49. return &Portal{
  50. db: pq.db,
  51. log: pq.log,
  52. }
  53. }
  54. func (pq *PortalQuery) GetAll() []*Portal {
  55. return pq.getAll("SELECT * FROM portal")
  56. }
  57. func (pq *PortalQuery) GetByJID(key PortalKey) *Portal {
  58. return pq.get("SELECT * FROM portal WHERE jid=$1 AND receiver=$2", key.JID, key.Receiver)
  59. }
  60. func (pq *PortalQuery) GetByMXID(mxid id.RoomID) *Portal {
  61. return pq.get("SELECT * FROM portal WHERE mxid=$1", mxid)
  62. }
  63. func (pq *PortalQuery) GetAllByJID(jid types.JID) []*Portal {
  64. return pq.getAll("SELECT * FROM portal WHERE jid=$1", jid.ToNonAD())
  65. }
  66. func (pq *PortalQuery) FindPrivateChats(receiver types.JID) []*Portal {
  67. return pq.getAll("SELECT * FROM portal WHERE receiver=$1 AND jid LIKE '%@s.whatsapp.net'", receiver.ToNonAD())
  68. }
  69. func (pq *PortalQuery) FindPrivateChatsNotInSpace(receiver types.JID) (keys []PortalKey) {
  70. receiver = receiver.ToNonAD()
  71. rows, err := pq.db.Query(`
  72. SELECT jid FROM portal
  73. LEFT JOIN user_portal ON portal.jid=user_portal.portal_jid AND portal.receiver=user_portal.portal_receiver
  74. WHERE mxid<>'' AND receiver=$1 AND (in_space=false OR in_space IS NULL)
  75. `, receiver)
  76. if err != nil || rows == nil {
  77. return
  78. }
  79. for rows.Next() {
  80. var key PortalKey
  81. key.Receiver = receiver
  82. err = rows.Scan(&key.JID)
  83. if err == nil {
  84. keys = append(keys, key)
  85. }
  86. }
  87. return
  88. }
  89. func (pq *PortalQuery) getAll(query string, args ...interface{}) (portals []*Portal) {
  90. rows, err := pq.db.Query(query, args...)
  91. if err != nil || rows == nil {
  92. return nil
  93. }
  94. defer rows.Close()
  95. for rows.Next() {
  96. portals = append(portals, pq.New().Scan(rows))
  97. }
  98. return
  99. }
  100. func (pq *PortalQuery) get(query string, args ...interface{}) *Portal {
  101. row := pq.db.QueryRow(query, args...)
  102. if row == nil {
  103. return nil
  104. }
  105. return pq.New().Scan(row)
  106. }
  107. type Portal struct {
  108. db *Database
  109. log log.Logger
  110. Key PortalKey
  111. MXID id.RoomID
  112. Name string
  113. Topic string
  114. Avatar string
  115. AvatarURL id.ContentURI
  116. Encrypted bool
  117. FirstEventID id.EventID
  118. NextBatchID id.BatchID
  119. RelayUserID id.UserID
  120. ExpirationTime uint32
  121. }
  122. func (portal *Portal) Scan(row Scannable) *Portal {
  123. var mxid, avatarURL, firstEventID, nextBatchID, relayUserID sql.NullString
  124. err := row.Scan(&portal.Key.JID, &portal.Key.Receiver, &mxid, &portal.Name, &portal.Topic, &portal.Avatar, &avatarURL, &portal.Encrypted, &firstEventID, &nextBatchID, &relayUserID, &portal.ExpirationTime)
  125. if err != nil {
  126. if err != sql.ErrNoRows {
  127. portal.log.Errorln("Database scan failed:", err)
  128. }
  129. return nil
  130. }
  131. portal.MXID = id.RoomID(mxid.String)
  132. portal.AvatarURL, _ = id.ParseContentURI(avatarURL.String)
  133. portal.FirstEventID = id.EventID(firstEventID.String)
  134. portal.NextBatchID = id.BatchID(nextBatchID.String)
  135. portal.RelayUserID = id.UserID(relayUserID.String)
  136. return portal
  137. }
  138. func (portal *Portal) mxidPtr() *id.RoomID {
  139. if len(portal.MXID) > 0 {
  140. return &portal.MXID
  141. }
  142. return nil
  143. }
  144. func (portal *Portal) relayUserPtr() *id.UserID {
  145. if len(portal.RelayUserID) > 0 {
  146. return &portal.RelayUserID
  147. }
  148. return nil
  149. }
  150. func (portal *Portal) Insert() {
  151. _, err := portal.db.Exec("INSERT INTO portal (jid, receiver, mxid, name, topic, avatar, avatar_url, encrypted, first_event_id, next_batch_id, relay_user_id, expiration_time) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)",
  152. portal.Key.JID, portal.Key.Receiver, portal.mxidPtr(), portal.Name, portal.Topic, portal.Avatar, portal.AvatarURL.String(), portal.Encrypted, portal.FirstEventID.String(), portal.NextBatchID.String(), portal.relayUserPtr(), portal.ExpirationTime)
  153. if err != nil {
  154. portal.log.Warnfln("Failed to insert %s: %v", portal.Key, err)
  155. }
  156. }
  157. func (portal *Portal) Update() {
  158. _, err := portal.db.Exec("UPDATE portal SET mxid=$1, name=$2, topic=$3, avatar=$4, avatar_url=$5, encrypted=$6, first_event_id=$7, next_batch_id=$8, relay_user_id=$9, expiration_time=$10 WHERE jid=$11 AND receiver=$12",
  159. portal.mxidPtr(), portal.Name, portal.Topic, portal.Avatar, portal.AvatarURL.String(), portal.Encrypted, portal.FirstEventID.String(), portal.NextBatchID.String(), portal.relayUserPtr(), portal.ExpirationTime, portal.Key.JID, portal.Key.Receiver)
  160. if err != nil {
  161. portal.log.Warnfln("Failed to update %s: %v", portal.Key, err)
  162. }
  163. }
  164. func (portal *Portal) Delete() {
  165. _, err := portal.db.Exec("DELETE FROM portal WHERE jid=$1 AND receiver=$2", portal.Key.JID, portal.Key.Receiver)
  166. if err != nil {
  167. portal.log.Warnfln("Failed to delete %s: %v", portal.Key, err)
  168. }
  169. }