From 41b6fcedf429a216e62becd79aaafe984f251c05 Mon Sep 17 00:00:00 2001 From: kolaente Date: Sun, 14 Feb 2021 18:29:51 +0100 Subject: [PATCH] Return all subscriptions for namespaces when getting all namespaces --- pkg/models/namespace.go | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/pkg/models/namespace.go b/pkg/models/namespace.go index 32181905b..ad3a70d39 100644 --- a/pkg/models/namespace.go +++ b/pkg/models/namespace.go @@ -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 }