Allow resyncing groups in provisioning API

This commit is contained in:
Tulir Asokan 2023-05-15 12:30:57 +03:00
parent d6694630af
commit fd9cde29dc
2 changed files with 21 additions and 3 deletions

View file

@ -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",

View file

@ -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 {