portal.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. "maunium.net/go/gomatrix"
  23. "strings"
  24. "maunium.net/go/mautrix-appservice"
  25. )
  26. func (user *User) GetPortalByMXID(mxid types.MatrixRoomID) *Portal {
  27. portal, ok := user.portalsByMXID[mxid]
  28. if !ok {
  29. dbPortal := user.bridge.DB.Portal.GetByMXID(mxid)
  30. if dbPortal == nil || dbPortal.Owner != user.ID {
  31. return nil
  32. }
  33. portal = user.NewPortal(dbPortal)
  34. user.portalsByJID[portal.JID] = portal
  35. if len(portal.MXID) > 0 {
  36. user.portalsByMXID[portal.MXID] = portal
  37. }
  38. }
  39. return portal
  40. }
  41. func (user *User) GetPortalByJID(jid types.WhatsAppID) *Portal {
  42. portal, ok := user.portalsByJID[jid]
  43. if !ok {
  44. dbPortal := user.bridge.DB.Portal.GetByJID(user.ID, jid)
  45. if dbPortal == nil {
  46. dbPortal = user.bridge.DB.Portal.New()
  47. dbPortal.JID = jid
  48. dbPortal.Owner = user.ID
  49. dbPortal.Insert()
  50. }
  51. portal = user.NewPortal(dbPortal)
  52. user.portalsByJID[portal.JID] = portal
  53. if len(portal.MXID) > 0 {
  54. user.portalsByMXID[portal.MXID] = portal
  55. }
  56. }
  57. return portal
  58. }
  59. func (user *User) GetAllPortals() []*Portal {
  60. dbPortals := user.bridge.DB.Portal.GetAll(user.ID)
  61. output := make([]*Portal, len(dbPortals))
  62. for index, dbPortal := range dbPortals {
  63. portal, ok := user.portalsByJID[dbPortal.JID]
  64. if !ok {
  65. portal = user.NewPortal(dbPortal)
  66. user.portalsByJID[dbPortal.JID] = portal
  67. if len(dbPortal.MXID) > 0 {
  68. user.portalsByMXID[dbPortal.MXID] = portal
  69. }
  70. }
  71. output[index] = portal
  72. }
  73. return output
  74. }
  75. func (user *User) NewPortal(dbPortal *database.Portal) *Portal {
  76. return &Portal{
  77. Portal: dbPortal,
  78. user: user,
  79. bridge: user.bridge,
  80. log: user.log.Sub(fmt.Sprintf("Portal/%s", dbPortal.JID)),
  81. }
  82. }
  83. type Portal struct {
  84. *database.Portal
  85. user *User
  86. bridge *Bridge
  87. log log.Logger
  88. }
  89. func (portal *Portal) CreateMatrixRoom() error {
  90. if len(portal.MXID) > 0 {
  91. return nil
  92. }
  93. name := portal.Name
  94. topic := ""
  95. isPrivateChat := false
  96. if strings.HasSuffix(portal.JID, "s.whatsapp.net") {
  97. puppet := portal.user.GetPuppetByJID(portal.JID)
  98. name = puppet.Displayname
  99. topic = "WhatsApp private chat"
  100. isPrivateChat = true
  101. }
  102. resp, err := portal.MainIntent().CreateRoom(&gomatrix.ReqCreateRoom{
  103. Visibility: "private",
  104. Name: name,
  105. Topic: topic,
  106. Invite: []string{portal.user.ID},
  107. Preset: "private_chat",
  108. IsDirect: isPrivateChat,
  109. })
  110. if err != nil {
  111. return err
  112. }
  113. portal.MXID = resp.RoomID
  114. portal.Update()
  115. return nil
  116. }
  117. func (portal *Portal) IsPrivateChat() bool {
  118. return strings.HasSuffix(portal.JID, puppetJIDStrippedSuffix)
  119. }
  120. func (portal *Portal) MainIntent() *appservice.IntentAPI {
  121. if portal.IsPrivateChat() {
  122. return portal.user.GetPuppetByJID(portal.JID).Intent()
  123. }
  124. return portal.bridge.AppService.BotIntent()
  125. }
  126. func (portal *Portal) HandleMessage(evt *gomatrix.Event) {
  127. portal.log.Debugln("Received event:", evt)
  128. }