From fd9cde29dcfa4e6b4e0c38c1f49b299902af4716 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Mon, 15 May 2023 12:30:57 +0300 Subject: [PATCH] Allow resyncing groups in provisioning API --- provisioning.go | 20 +++++++++++++++++--- user.go | 4 ++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/provisioning.go b/provisioning.go index 97832ca..84dc2f8 100644 --- a/provisioning.go +++ b/provisioning.go @@ -63,7 +63,7 @@ func (prov *ProvisioningAPI) Init() { r.HandleFunc("/v1/debug/appstate/{name}", prov.SyncAppState).Methods(http.MethodPost) r.HandleFunc("/v1/debug/retry", prov.SendRetryReceipt).Methods(http.MethodPost) r.HandleFunc("/v1/contacts", prov.ListContacts).Methods(http.MethodGet) - r.HandleFunc("/v1/groups", prov.ListGroups).Methods(http.MethodGet) + r.HandleFunc("/v1/groups", prov.ListGroups).Methods(http.MethodGet, http.MethodPost) r.HandleFunc("/v1/resolve_identifier/{number}", prov.ResolveIdentifier).Methods(http.MethodGet) r.HandleFunc("/v1/bulk_resolve_identifier", prov.BulkResolveIdentifier).Methods(http.MethodPost) r.HandleFunc("/v1/pm/{number}", prov.StartPM).Methods(http.MethodPost) @@ -315,12 +315,26 @@ func (prov *ProvisioningAPI) ListContacts(w http.ResponseWriter, r *http.Request } func (prov *ProvisioningAPI) ListGroups(w http.ResponseWriter, r *http.Request) { - if user := r.Context().Value("user").(*User); user.Session == nil { + user := r.Context().Value("user").(*User) + if user.Session == nil { jsonResponse(w, http.StatusBadRequest, Error{ Error: "User is not logged into WhatsApp", ErrCode: "no session", }) - } else if groups, err := user.getCachedGroupList(); err != nil { + return + } + if r.Method == http.MethodPost { + err := user.ResyncGroups(r.URL.Query().Get("create_portals") == "true") + if err != nil { + prov.log.Errorfln("Failed to resync %s's groups: %v", user.MXID, err) + jsonResponse(w, http.StatusInternalServerError, Error{ + Error: "Internal server error while resyncing groups", + ErrCode: "failed to sync groups", + }) + return + } + } + if groups, err := user.getCachedGroupList(); err != nil { prov.log.Errorfln("Failed to fetch %s's groups: %v", user.MXID, err) jsonResponse(w, http.StatusInternalServerError, Error{ Error: "Internal server error while fetching group list", diff --git a/user.go b/user.go index f1f5829..68447a9 100644 --- a/user.go +++ b/user.go @@ -1191,6 +1191,10 @@ func (user *User) ResyncGroups(createPortals bool) error { if err != nil { return fmt.Errorf("failed to get group list from server: %w", err) } + user.groupListCacheLock.Lock() + user.groupListCache = groups + user.groupListCacheTime = time.Now() + user.groupListCacheLock.Unlock() for _, group := range groups { portal := user.GetPortalByJID(group.JID) if len(portal.MXID) == 0 {