portal.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2018 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 main
  17. import (
  18. "maunium.net/go/mautrix-whatsapp/database"
  19. log "maunium.net/go/maulogger"
  20. "fmt"
  21. "maunium.net/go/mautrix-whatsapp/types"
  22. )
  23. func (user *User) GetPortalByMXID(mxid types.MatrixRoomID) *Portal {
  24. portal, ok := user.portalsByMXID[mxid]
  25. if !ok {
  26. dbPortal := user.bridge.DB.Portal.GetByMXID(mxid)
  27. if dbPortal == nil || dbPortal.Owner != user.UserID {
  28. return nil
  29. }
  30. portal = user.NewPortal(dbPortal)
  31. user.portalsByJID[portal.JID] = portal
  32. if len(portal.MXID) > 0 {
  33. user.portalsByMXID[portal.MXID] = portal
  34. }
  35. }
  36. return portal
  37. }
  38. func (user *User) GetPortalByJID(jid types.WhatsAppID) *Portal {
  39. portal, ok := user.portalsByJID[jid]
  40. if !ok {
  41. dbPortal := user.bridge.DB.Portal.GetByJID(user.UserID, jid)
  42. if dbPortal == nil {
  43. return nil
  44. }
  45. portal = user.NewPortal(dbPortal)
  46. user.portalsByJID[portal.JID] = portal
  47. if len(portal.MXID) > 0 {
  48. user.portalsByMXID[portal.MXID] = portal
  49. }
  50. }
  51. return portal
  52. }
  53. func (user *User) GetAllPortals() []*Portal {
  54. dbPortals := user.bridge.DB.Portal.GetAll(user.UserID)
  55. output := make([]*Portal, len(dbPortals))
  56. for index, dbPortal := range dbPortals {
  57. portal, ok := user.portalsByJID[dbPortal.JID]
  58. if !ok {
  59. portal = user.NewPortal(dbPortal)
  60. user.portalsByJID[dbPortal.JID] = portal
  61. if len(dbPortal.MXID) > 0 {
  62. user.portalsByMXID[dbPortal.MXID] = portal
  63. }
  64. }
  65. output[index] = portal
  66. }
  67. return output
  68. }
  69. func (user *User) NewPortal(dbPortal *database.Portal) *Portal {
  70. return &Portal{
  71. Portal: dbPortal,
  72. user: user,
  73. bridge: user.bridge,
  74. log: user.log.Sub(fmt.Sprintf("Portal/%s", dbPortal.JID)),
  75. }
  76. }
  77. type Portal struct {
  78. *database.Portal
  79. user *User
  80. bridge *Bridge
  81. log log.Logger
  82. }