1
0

fix(subscriptions): cleanup and simplify fetching subscribers for tasks and projects logic

Vikunja now uses one recursive CTE and a few optimizations to fetch all subscribers for a task or project. This makes the relevant code easier to maintain and more performant.

(cherry picked from commit 4ff8815fe1bfe72e02c10f6a6877c93a630f36a4)
This commit is contained in:
kolaente
2024-09-04 19:54:22 +02:00
parent 7646c7f0c9
commit 8b8ec19bb3
7 changed files with 330 additions and 265 deletions

View File

@ -297,10 +297,13 @@ func (p *Project) ReadOne(s *xorm.Session, a web.Auth) (err error) {
return
}
p.Subscription, err = GetSubscription(s, SubscriptionEntityProject, p.ID, a)
subs, err := GetSubscriptionForUser(s, SubscriptionEntityProject, p.ID, a)
if err != nil && IsErrProjectDoesNotExist(err) && isFilter {
return nil
}
if subs != nil {
p.Subscription = &subs.Subscription
}
p.Views, err = getViewsForProject(s, p.ID)
return
@ -629,10 +632,23 @@ func addProjectDetails(s *xorm.Session, projects []*Project, a web.Auth) (err er
return err
}
subscriptions, err := GetSubscriptionsForProjects(s, projects, a)
if err != nil {
log.Errorf("An error occurred while getting project subscriptions for a project: %s", err.Error())
subscriptions = make(map[int64][]*Subscription)
var subscriptions = make(map[int64][]*Subscription)
u, is := a.(*user.User)
if is {
subscriptionsWithUser, err := GetSubscriptionsForEntitiesAndUser(s, SubscriptionEntityProject, projectIDs, u)
if err != nil {
log.Errorf("An error occurred while getting project subscriptions for a project: %s", err.Error())
}
if err == nil {
for pID, subs := range subscriptionsWithUser {
for _, sub := range subs {
if _, has := subscriptions[pID]; !has {
subscriptions[pID] = []*Subscription{}
}
subscriptions[pID] = append(subscriptions[pID], &sub.Subscription)
}
}
}
}
views := []*ProjectView{}