Return all subscriptions for namespaces when getting all namespaces

This commit is contained in:
kolaente 2021-02-14 18:29:51 +01:00
parent 9d27da9c47
commit 41b6fcedf4
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
1 changed files with 19 additions and 3 deletions

View File

@ -181,10 +181,11 @@ type NamespaceWithLists struct {
Lists []*List `xorm:"-" json:"lists"`
}
func makeNamespaceSliceFromMap(namespaces map[int64]*NamespaceWithLists, userMap map[int64]*user.User) []*NamespaceWithLists {
func makeNamespaceSliceFromMap(namespaces map[int64]*NamespaceWithLists, userMap map[int64]*user.User, subscriptions map[int64]*Subscription) []*NamespaceWithLists {
all := make([]*NamespaceWithLists, 0, len(namespaces))
for _, n := range namespaces {
n.Owner = userMap[n.OwnerID]
n.Subscription = subscriptions[n.ID]
all = append(all, n)
}
sort.Slice(all, func(i, j int) bool {
@ -295,6 +296,21 @@ func (n *Namespace) ReadAll(s *xorm.Session, a web.Auth, search string, page int
userIDs = append(userIDs, nsp.OwnerID)
}
// Get all subscriptions
subscriptions := []*Subscription{}
err = s.
Where("entity_type = ? AND user_id = ?", SubscriptionEntityNamespace, a.GetID()).
In("entity_id", namespaceids).
Find(&subscriptions)
if err != nil {
return nil, 0, 0, err
}
subscriptionsMap := make(map[int64]*Subscription)
for _, sub := range subscriptions {
sub.Entity = sub.EntityType.String()
subscriptionsMap[sub.EntityID] = sub
}
// Get all owners
userMap := make(map[int64]*user.User)
err = s.In("id", userIDs).Find(&userMap)
@ -303,7 +319,7 @@ func (n *Namespace) ReadAll(s *xorm.Session, a web.Auth, search string, page int
}
if n.NamespacesOnly {
all := makeNamespaceSliceFromMap(namespaces, userMap)
all := makeNamespaceSliceFromMap(namespaces, userMap, subscriptionsMap)
return all, len(all), numberOfTotalItems, nil
}
@ -449,7 +465,7 @@ func (n *Namespace) ReadAll(s *xorm.Session, a web.Auth, search string, page int
//////////////////////
// Put it all together (and sort it)
all := makeNamespaceSliceFromMap(namespaces, userMap)
all := makeNamespaceSliceFromMap(namespaces, userMap, subscriptionsMap)
return all, len(all), numberOfTotalItems, nil
}