Retrieve all groups for peers and restrict groups for regular users

Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com>
This commit is contained in:
bcmmbaga 2024-11-28 15:01:44 +03:00
parent f87bc601c6
commit 1ba6eb62a6
No known key found for this signature in database
GPG Key ID: 511EED5C928AD547

View File

@ -62,12 +62,8 @@ func (h *PeersHandler) getPeer(ctx context.Context, accountID, peerID, userID st
} }
dnsDomain := h.accountManager.GetDNSDomain() dnsDomain := h.accountManager.GetDNSDomain()
peerGroups, err := h.accountManager.GetPeerGroups(ctx, accountID, peer.ID) groups, _ := h.accountManager.GetAllGroups(ctx, accountID, userID)
if err != nil { groupsInfo := toGroupsInfo(groups, peerID)
util.WriteError(ctx, err, w)
return
}
groupsInfo := toGroupsInfo(peerGroups)
validPeers, err := h.accountManager.GetValidatedPeers(ctx, accountID) validPeers, err := h.accountManager.GetValidatedPeers(ctx, accountID)
if err != nil { if err != nil {
@ -116,7 +112,7 @@ func (h *PeersHandler) updatePeer(ctx context.Context, accountID, userID, peerID
util.WriteError(ctx, err, w) util.WriteError(ctx, err, w)
return return
} }
groupMinimumInfo := toGroupsInfo(peerGroups) groupMinimumInfo := toGroupsInfo(peerGroups, peer.ID)
validPeers, err := h.accountManager.GetValidatedPeers(ctx, accountID) validPeers, err := h.accountManager.GetValidatedPeers(ctx, accountID)
if err != nil { if err != nil {
@ -187,6 +183,8 @@ func (h *PeersHandler) GetAllPeers(w http.ResponseWriter, r *http.Request) {
dnsDomain := h.accountManager.GetDNSDomain() dnsDomain := h.accountManager.GetDNSDomain()
groups, _ := h.accountManager.GetAllGroups(r.Context(), accountID, userID)
respBody := make([]*api.PeerBatch, 0, len(peers)) respBody := make([]*api.PeerBatch, 0, len(peers))
for _, peer := range peers { for _, peer := range peers {
peerToReturn, err := h.checkPeerStatus(peer) peerToReturn, err := h.checkPeerStatus(peer)
@ -195,12 +193,7 @@ func (h *PeersHandler) GetAllPeers(w http.ResponseWriter, r *http.Request) {
return return
} }
peerGroups, err := h.accountManager.GetPeerGroups(r.Context(), accountID, peer.ID) groupMinimumInfo := toGroupsInfo(groups, peer.ID)
if err != nil {
util.WriteError(r.Context(), err, w)
return
}
groupMinimumInfo := toGroupsInfo(peerGroups)
respBody = append(respBody, toPeerListItemResponse(peerToReturn, groupMinimumInfo, dnsDomain, 0)) respBody = append(respBody, toPeerListItemResponse(peerToReturn, groupMinimumInfo, dnsDomain, 0))
} }
@ -312,14 +305,28 @@ func peerToAccessiblePeer(peer *nbpeer.Peer, dnsDomain string) api.AccessiblePee
} }
} }
func toGroupsInfo(groups []*nbgroup.Group) []api.GroupMinimum { func toGroupsInfo(groups []*nbgroup.Group, peerID string) []api.GroupMinimum {
groupsInfo := make([]api.GroupMinimum, 0, len(groups)) groupsInfo := []api.GroupMinimum{}
groupsChecked := make(map[string]struct{})
for _, group := range groups { for _, group := range groups {
groupsInfo = append(groupsInfo, api.GroupMinimum{ _, ok := groupsChecked[group.ID]
Id: group.ID, if ok {
Name: group.Name, continue
PeersCount: len(group.Peers), }
})
groupsChecked[group.ID] = struct{}{}
for _, pk := range group.Peers {
if pk == peerID {
info := api.GroupMinimum{
Id: group.ID,
Name: group.Name,
PeersCount: len(group.Peers),
}
groupsInfo = append(groupsInfo, info)
break
}
}
} }
return groupsInfo return groupsInfo
} }