broadcast.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright (c) 2022 Tulir Asokan
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. package whatsmeow
  7. import (
  8. "context"
  9. "errors"
  10. "fmt"
  11. waBinary "go.mau.fi/whatsmeow/binary"
  12. "go.mau.fi/whatsmeow/types"
  13. )
  14. func (cli *Client) getBroadcastListParticipants(ctx context.Context, jid types.JID) ([]types.JID, error) {
  15. var list []types.JID
  16. var err error
  17. if jid == types.StatusBroadcastJID {
  18. list, err = cli.getStatusBroadcastRecipients(ctx)
  19. } else {
  20. return nil, ErrBroadcastListUnsupported
  21. }
  22. if err != nil {
  23. return nil, err
  24. }
  25. ownID := cli.getOwnID().ToNonAD()
  26. if ownID.IsEmpty() {
  27. return nil, ErrNotLoggedIn
  28. }
  29. selfIndex := -1
  30. for i, participant := range list {
  31. if participant.User == ownID.User {
  32. selfIndex = i
  33. break
  34. }
  35. }
  36. if selfIndex < 0 {
  37. list = append(list, ownID)
  38. }
  39. return list, nil
  40. }
  41. func (cli *Client) getStatusBroadcastRecipients(ctx context.Context) ([]types.JID, error) {
  42. statusPrivacyOptions, err := cli.GetStatusPrivacy(ctx)
  43. if err != nil {
  44. return nil, fmt.Errorf("failed to get status privacy: %w", err)
  45. }
  46. statusPrivacy := statusPrivacyOptions[0]
  47. if statusPrivacy.Type == types.StatusPrivacyTypeWhitelist {
  48. // Whitelist mode, just return the list
  49. return statusPrivacy.List, nil
  50. }
  51. // Blacklist or all contacts mode. Find all contacts from database, then filter them appropriately.
  52. contacts, err := cli.Store.Contacts.GetAllContacts(ctx)
  53. if err != nil {
  54. return nil, fmt.Errorf("failed to get contact list from db: %w", err)
  55. }
  56. blacklist := make(map[types.JID]struct{})
  57. if statusPrivacy.Type == types.StatusPrivacyTypeBlacklist {
  58. for _, jid := range statusPrivacy.List {
  59. blacklist[jid] = struct{}{}
  60. }
  61. }
  62. var contactsArray []types.JID
  63. for jid, contact := range contacts {
  64. _, isBlacklisted := blacklist[jid]
  65. if isBlacklisted {
  66. continue
  67. }
  68. // TODO should there be a better way to separate contacts and found push names in the db?
  69. if len(contact.FullName) > 0 {
  70. contactsArray = append(contactsArray, jid)
  71. }
  72. }
  73. return contactsArray, nil
  74. }
  75. var DefaultStatusPrivacy = []types.StatusPrivacy{{
  76. Type: types.StatusPrivacyTypeContacts,
  77. IsDefault: true,
  78. }}
  79. // GetStatusPrivacy gets the user's status privacy settings (who to send status broadcasts to).
  80. //
  81. // There can be multiple different stored settings, the first one is always the default.
  82. func (cli *Client) GetStatusPrivacy(ctx context.Context) ([]types.StatusPrivacy, error) {
  83. resp, err := cli.sendIQ(ctx, infoQuery{
  84. Namespace: "status",
  85. Type: iqGet,
  86. To: types.ServerJID,
  87. Content: []waBinary.Node{{
  88. Tag: "privacy",
  89. }},
  90. })
  91. if err != nil {
  92. if errors.Is(err, ErrIQNotFound) {
  93. return DefaultStatusPrivacy, nil
  94. }
  95. return nil, err
  96. }
  97. privacyLists := resp.GetChildByTag("privacy")
  98. var outputs []types.StatusPrivacy
  99. for _, list := range privacyLists.GetChildren() {
  100. if list.Tag != "list" {
  101. continue
  102. }
  103. ag := list.AttrGetter()
  104. var out types.StatusPrivacy
  105. out.IsDefault = ag.OptionalBool("default")
  106. out.Type = types.StatusPrivacyType(ag.String("type"))
  107. children := list.GetChildren()
  108. if len(children) > 0 {
  109. out.List = make([]types.JID, 0, len(children))
  110. for _, child := range children {
  111. jid, ok := child.Attrs["jid"].(types.JID)
  112. if child.Tag == "user" && ok {
  113. out.List = append(out.List, jid)
  114. }
  115. }
  116. }
  117. outputs = append(outputs, out)
  118. if out.IsDefault {
  119. // Move default to always be first in the list
  120. outputs[len(outputs)-1] = outputs[0]
  121. outputs[0] = out
  122. }
  123. if len(ag.Errors) > 0 {
  124. return nil, ag.Error()
  125. }
  126. }
  127. if len(outputs) == 0 {
  128. return DefaultStatusPrivacy, nil
  129. }
  130. return outputs, nil
  131. }