diff options
| author | Mistivia <i@mistivia.com> | 2025-11-02 15:27:18 +0800 |
|---|---|---|
| committer | Mistivia <i@mistivia.com> | 2025-11-02 15:27:18 +0800 |
| commit | e9c24f4af7ed56760f6db7941827d09f6db9020b (patch) | |
| tree | 62128c43b883ce5e3148113350978755779bb5de /teleirc/matterbridge/vendor/go.mau.fi/whatsmeow/privacysettings.go | |
| parent | 58d5e7cfda4781d8a57ec52aefd02983835c301a (diff) | |
add matterbridge
Diffstat (limited to 'teleirc/matterbridge/vendor/go.mau.fi/whatsmeow/privacysettings.go')
| -rw-r--r-- | teleirc/matterbridge/vendor/go.mau.fi/whatsmeow/privacysettings.go | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/teleirc/matterbridge/vendor/go.mau.fi/whatsmeow/privacysettings.go b/teleirc/matterbridge/vendor/go.mau.fi/whatsmeow/privacysettings.go new file mode 100644 index 0000000..3aca72d --- /dev/null +++ b/teleirc/matterbridge/vendor/go.mau.fi/whatsmeow/privacysettings.go @@ -0,0 +1,93 @@ +// Copyright (c) 2021 Tulir Asokan +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +package whatsmeow + +import ( + waBinary "go.mau.fi/whatsmeow/binary" + "go.mau.fi/whatsmeow/types" + "go.mau.fi/whatsmeow/types/events" +) + +// TryFetchPrivacySettings will fetch the user's privacy settings, either from the in-memory cache or from the server. +func (cli *Client) TryFetchPrivacySettings(ignoreCache bool) (*types.PrivacySettings, error) { + if val := cli.privacySettingsCache.Load(); val != nil && !ignoreCache { + return val.(*types.PrivacySettings), nil + } + resp, err := cli.sendIQ(infoQuery{ + Namespace: "privacy", + Type: iqGet, + To: types.ServerJID, + Content: []waBinary.Node{{Tag: "privacy"}}, + }) + if err != nil { + return nil, err + } + privacyNode, ok := resp.GetOptionalChildByTag("privacy") + if !ok { + return nil, &ElementMissingError{Tag: "privacy", In: "response to privacy settings query"} + } + var settings types.PrivacySettings + cli.parsePrivacySettings(&privacyNode, &settings) + cli.privacySettingsCache.Store(&settings) + return &settings, nil +} + +// GetPrivacySettings will get the user's privacy settings. If an error occurs while fetching them, the error will be +// logged, but the method will just return an empty struct. +func (cli *Client) GetPrivacySettings() (settings types.PrivacySettings) { + settingsPtr, err := cli.TryFetchPrivacySettings(false) + if err != nil { + cli.Log.Errorf("Failed to fetch privacy settings: %v", err) + } else { + settings = *settingsPtr + } + return +} + +func (cli *Client) parsePrivacySettings(privacyNode *waBinary.Node, settings *types.PrivacySettings) *events.PrivacySettings { + var evt events.PrivacySettings + for _, child := range privacyNode.GetChildren() { + if child.Tag != "category" { + continue + } + ag := child.AttrGetter() + name := ag.String("name") + value := types.PrivacySetting(ag.String("value")) + switch name { + case "groupadd": + settings.GroupAdd = value + evt.GroupAddChanged = true + case "last": + settings.LastSeen = value + evt.LastSeenChanged = true + case "status": + settings.Status = value + evt.StatusChanged = true + case "profile": + settings.Profile = value + evt.ProfileChanged = true + case "readreceipts": + settings.ReadReceipts = value + evt.ReadReceiptsChanged = true + } + } + return &evt +} + +func (cli *Client) handlePrivacySettingsNotification(privacyNode *waBinary.Node) { + cli.Log.Debugf("Parsing privacy settings change notification") + settings, err := cli.TryFetchPrivacySettings(false) + if err != nil { + cli.Log.Errorf("Failed to fetch privacy settings when handling change: %v", err) + } + evt := cli.parsePrivacySettings(privacyNode, settings) + // The data isn't be reliable if the fetch failed, so only cache if it didn't fail + if err == nil { + cli.privacySettingsCache.Store(settings) + } + cli.dispatchEvent(evt) +} |
