Fixed timeline query for user+project combo

This commit is contained in:
Asaf Gartner 2024-06-27 23:13:22 +03:00
parent f9ada49278
commit a602e02c9c
1 changed files with 33 additions and 13 deletions

View File

@ -129,10 +129,12 @@ func FetchTimeline(
WHERE TRUE WHERE TRUE
`, `,
) )
if len(q.ProjectIDs) > 0 { if len(q.ProjectIDs)+len(q.OwnerIDs) > 0 {
qb.Add( qb.Add(`AND (`)
` if len(q.ProjectIDs) > 0 {
AND ( qb.Add(
`
(
SELECT count(*) SELECT count(*)
FROM snippet_project FROM snippet_project
WHERE WHERE
@ -140,9 +142,19 @@ func FetchTimeline(
AND AND
snippet_project.project_id = ANY($?) snippet_project.project_id = ANY($?)
) > 0 ) > 0
`, `,
q.ProjectIDs, q.ProjectIDs,
) )
} else {
qb.Add("FALSE")
}
qb.Add(" OR ")
if len(q.OwnerIDs) > 0 {
qb.Add(`owner_id = ANY($?)`, q.OwnerIDs)
} else {
qb.Add("FALSE")
}
qb.Add(`)`)
} }
qb.Add( qb.Add(
` `
@ -169,8 +181,20 @@ func FetchTimeline(
WHERE post.deleted = false AND thread.deleted = false WHERE post.deleted = false AND thread.deleted = false
`, `,
) )
if len(q.ProjectIDs) > 0 { if len(q.OwnerIDs)+len(q.ProjectIDs) > 0 {
qb.Add(`AND post.project_id = ANY($?)`, q.ProjectIDs) qb.Add(`AND (`)
if len(q.ProjectIDs) > 0 {
qb.Add(`post.project_id = ANY($?)`, q.ProjectIDs)
} else {
qb.Add("FALSE")
}
qb.Add(" OR ")
if len(q.OwnerIDs) > 0 {
qb.Add(`post.author_id = ANY($?)`, q.OwnerIDs)
} else {
qb.Add("FALSE")
}
qb.Add(`)`)
} }
qb.Add( qb.Add(
` `
@ -192,10 +216,6 @@ func FetchTimeline(
`, `,
) )
if len(q.OwnerIDs) > 0 {
qb.Add(`AND owner_id = ANY($?)`, q.OwnerIDs)
}
if currentUser == nil { if currentUser == nil {
qb.Add( qb.Add(
`AND owner.status = $? -- snippet owner is Approved`, `AND owner.status = $? -- snippet owner is Approved`,