group.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 whatsappExt
  17. import (
  18. "encoding/json"
  19. "fmt"
  20. "maunium.net/go/mautrix-whatsapp/types"
  21. )
  22. type CreateGroupResponse struct {
  23. Status int `json:"status"`
  24. GroupID types.WhatsAppID `json:"gid"`
  25. Participants map[types.WhatsAppID]struct {
  26. Code string `json:"code"`
  27. } `json:"participants"`
  28. Source string `json:"-"`
  29. }
  30. type actualCreateGroupResponse struct {
  31. Status int `json:"status"`
  32. GroupID types.WhatsAppID `json:"gid"`
  33. Participants []map[types.WhatsAppID]struct {
  34. Code string `json:"code"`
  35. } `json:"participants"`
  36. }
  37. func (ext *ExtendedConn) CreateGroup(subject string, participants []types.WhatsAppID) (*CreateGroupResponse, error) {
  38. respChan, err := ext.Conn.CreateGroup(subject, participants)
  39. if err != nil {
  40. return nil, err
  41. }
  42. var resp CreateGroupResponse
  43. var actualResp actualCreateGroupResponse
  44. resp.Source = <-respChan
  45. fmt.Println(">>>>>>", resp.Source)
  46. err = json.Unmarshal([]byte(resp.Source), &actualResp)
  47. if err != nil {
  48. return nil, err
  49. }
  50. resp.Status = actualResp.Status
  51. resp.GroupID = actualResp.GroupID
  52. resp.Participants = make(map[types.WhatsAppID]struct {
  53. Code string `json:"code"`
  54. })
  55. for _, participantMap := range actualResp.Participants {
  56. for jid, status := range participantMap {
  57. resp.Participants[jid] = status
  58. }
  59. }
  60. return &resp, nil
  61. }