community.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2019 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. "fmt"
  19. "net/http"
  20. "maunium.net/go/mautrix"
  21. appservice "maunium.net/go/mautrix-appservice"
  22. )
  23. func (user *User) inviteToCommunity() {
  24. url := user.bridge.Bot.BuildURL("groups", user.CommunityID, "admin", "users", "invite", user.MXID)
  25. reqBody := map[string]interface{}{}
  26. _, err := user.bridge.Bot.MakeRequest(http.MethodPut, url, &reqBody, nil)
  27. if err != nil {
  28. user.log.Warnln("Failed to invite user to personal filtering community %s: %v", user.CommunityID, err)
  29. }
  30. }
  31. func (user *User) updateCommunityProfile() {
  32. url := user.bridge.Bot.BuildURL("groups", user.CommunityID, "profile")
  33. profileReq := struct {
  34. Name string `json:"name"`
  35. AvatarURL string `json:"avatar_url"`
  36. ShortDescription string `json:"short_description"`
  37. }{"WhatsApp", user.bridge.Config.AppService.Bot.Avatar, "Your WhatsApp bridged chats"}
  38. _, err := user.bridge.Bot.MakeRequest(http.MethodPost, url, &profileReq, nil)
  39. if err != nil {
  40. user.log.Warnln("Failed to update metadata of %s: %v", user.CommunityID, err)
  41. }
  42. }
  43. func (user *User) createCommunity() {
  44. if !user.bridge.Config.Bridge.EnableCommunities() {
  45. return
  46. }
  47. localpart, server := appservice.ParseUserID(user.MXID)
  48. community := user.bridge.Config.Bridge.FormatCommunity(localpart, server)
  49. user.log.Debugln("Creating personal filtering community", community)
  50. bot := user.bridge.Bot
  51. req := struct {
  52. Localpart string `json:"localpart"`
  53. }{community}
  54. resp := struct {
  55. GroupID string `json:"group_id"`
  56. }{}
  57. _, err := bot.MakeRequest(http.MethodPost, bot.BuildURL("create_group"), &req, &resp)
  58. if err != nil {
  59. if httpErr, ok := err.(mautrix.HTTPError); ok {
  60. if httpErr.RespError.Err != "Group already exists" {
  61. user.log.Warnln("Server responded with error creating personal filtering community:", err)
  62. return
  63. } else {
  64. user.log.Debugln("Personal filtering community", resp.GroupID, "already existed")
  65. user.CommunityID = fmt.Sprintf("+%s:%s", req.Localpart, user.bridge.Config.Homeserver.Domain)
  66. }
  67. } else {
  68. user.log.Warnln("Unknown error creating personal filtering community:", err)
  69. return
  70. }
  71. } else {
  72. user.log.Infoln("Created personal filtering community %s", resp.GroupID)
  73. user.CommunityID = resp.GroupID
  74. user.inviteToCommunity()
  75. user.updateCommunityProfile()
  76. }
  77. }
  78. func (user *User) addPortalToCommunity(portal *Portal) bool {
  79. if len(user.CommunityID) == 0 {
  80. return false
  81. }
  82. bot := user.bridge.Bot
  83. url := bot.BuildURL("groups", user.CommunityID, "admin", "rooms", portal.MXID)
  84. reqBody := map[string]interface{}{}
  85. _, err := bot.MakeRequest(http.MethodPut, url, &reqBody, nil)
  86. if err != nil {
  87. user.log.Warnln("Failed to add %s to %s: %v", portal.MXID, user.CommunityID, err)
  88. return false
  89. }
  90. user.log.Debugln("Added", portal.MXID, "to", user.CommunityID)
  91. return true
  92. }