From 79c9738b96629b49f445d81c02997c6f37666fcd Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Wed, 8 Dec 2021 21:50:35 -0600 Subject: [PATCH] Fix various bugs --- src/hmndata/snippet_helper.go | 5 ++++ src/templates/src/project_homepage.html | 2 +- src/website/podcast.go | 36 +++++-------------------- src/website/projects.go | 16 +++++------ src/website/routes.go | 9 ++++--- 5 files changed, 24 insertions(+), 44 deletions(-) diff --git a/src/hmndata/snippet_helper.go b/src/hmndata/snippet_helper.go index 0fa1acb4..33e6bdaa 100644 --- a/src/hmndata/snippet_helper.go +++ b/src/hmndata/snippet_helper.go @@ -62,6 +62,11 @@ func FetchSnippets( } iSnippetIDs := itSnippetIDs.ToSlice() + // special early-out: no snippets found for these tags at all + if len(iSnippetIDs) == 0 { + return nil, nil + } + q.IDs = make([]int, len(iSnippetIDs)) for i := range iSnippetIDs { q.IDs[i] = iSnippetIDs[i].(*snippetIDRow).SnippetID diff --git a/src/templates/src/project_homepage.html b/src/templates/src/project_homepage.html index 58745e86..54a9d860 100644 --- a/src/templates/src/project_homepage.html +++ b/src/templates/src/project_homepage.html @@ -18,7 +18,7 @@ {{ .Project.Name }} {{ else }}
-
{{ .Project.Name }}
+
{{ .Project.Name }}
{{ end }} diff --git a/src/website/podcast.go b/src/website/podcast.go index 50e2cbf5..e7152c3d 100644 --- a/src/website/podcast.go +++ b/src/website/podcast.go @@ -39,10 +39,7 @@ func PodcastIndex(c *RequestContext) ResponseData { return FourOhFour(c) } - canEdit, err := CanEditProject(c, c.CurrentUser, podcastResult.Podcast.ProjectID) - if err != nil { - return c.ErrorResponse(http.StatusInternalServerError, err) - } + canEdit := c.CurrentUserCanEditCurrentProject baseData := getBaseDataAutocrumb(c, podcastResult.Podcast.Title) @@ -78,10 +75,7 @@ func PodcastEdit(c *RequestContext) ResponseData { return c.ErrorResponse(http.StatusInternalServerError, err) } - canEdit, err := CanEditProject(c, c.CurrentUser, podcastResult.Podcast.ProjectID) - if err != nil { - return c.ErrorResponse(http.StatusInternalServerError, err) - } + canEdit := c.CurrentUserCanEditCurrentProject if podcastResult.Podcast == nil || !canEdit { return FourOhFour(c) @@ -112,10 +106,7 @@ func PodcastEditSubmit(c *RequestContext) ResponseData { return c.ErrorResponse(http.StatusInternalServerError, err) } - canEdit, err := CanEditProject(c, c.CurrentUser, podcastResult.Podcast.ProjectID) - if err != nil { - return c.ErrorResponse(http.StatusInternalServerError, err) - } + canEdit := c.CurrentUserCanEditCurrentProject if podcastResult.Podcast == nil || !canEdit { return FourOhFour(c) @@ -218,11 +209,7 @@ func PodcastEpisode(c *RequestContext) ResponseData { return FourOhFour(c) } - canEdit, err := CanEditProject(c, c.CurrentUser, podcastResult.Podcast.ProjectID) - if err != nil { - c.Logger.Error().Err(err).Msg("Failed to check if user can edit podcast. Assuming they can't.") // NOTE(asaf): No need to return an error response here if it failed. - canEdit = false - } + canEdit := c.CurrentUserCanEditCurrentProject editUrl := "" if canEdit { @@ -268,10 +255,7 @@ func PodcastEpisodeNew(c *RequestContext) ResponseData { return c.ErrorResponse(http.StatusInternalServerError, err) } - canEdit, err := CanEditProject(c, c.CurrentUser, podcastResult.Podcast.ProjectID) - if err != nil { - return c.ErrorResponse(http.StatusInternalServerError, err) - } + canEdit := c.CurrentUserCanEditCurrentProject if podcastResult.Podcast == nil || !canEdit { return FourOhFour(c) @@ -311,10 +295,7 @@ func PodcastEpisodeEdit(c *RequestContext) ResponseData { return c.ErrorResponse(http.StatusInternalServerError, err) } - canEdit, err := CanEditProject(c, c.CurrentUser, podcastResult.Podcast.ProjectID) - if err != nil { - return c.ErrorResponse(http.StatusInternalServerError, err) - } + canEdit := c.CurrentUserCanEditCurrentProject if podcastResult.Podcast == nil || len(podcastResult.Episodes) == 0 || !canEdit { return FourOhFour(c) @@ -360,10 +341,7 @@ func PodcastEpisodeSubmit(c *RequestContext) ResponseData { return c.ErrorResponse(http.StatusInternalServerError, err) } - canEdit, err := CanEditProject(c, c.CurrentUser, podcastResult.Podcast.ProjectID) - if err != nil { - return c.ErrorResponse(http.StatusInternalServerError, err) - } + canEdit := c.CurrentUserCanEditCurrentProject if podcastResult.Podcast == nil || (isEdit && len(podcastResult.Episodes) == 0) || !canEdit { return FourOhFour(c) diff --git a/src/website/projects.go b/src/website/projects.go index f8395e6a..e8397ebd 100644 --- a/src/website/projects.go +++ b/src/website/projects.go @@ -692,7 +692,7 @@ func updateProject(ctx context.Context, tx pgx.Tx, user *models.User, payload *P blurb = $?, description = $?, descparsed = $?, - lifecycle = $?, + lifecycle = $? `, payload.Name, payload.Blurb, @@ -701,7 +701,7 @@ func updateProject(ctx context.Context, tx pgx.Tx, user *models.User, payload *P payload.Lifecycle, ) if user.IsStaff { - qb.Add(`hidden = $?`, payload.Hidden) + qb.Add(`, hidden = $?`, payload.Hidden) } qb.Add(`WHERE id = $?`, payload.ProjectID) @@ -857,21 +857,17 @@ func GetFormImage(c *RequestContext, fieldName string) (FormImage, error) { return res, nil } -func CanEditProject(c *RequestContext, user *models.User, projectId int) (bool, error) { +func CanEditProject(user *models.User, owners []*models.User) bool { if user != nil { if user.IsStaff { - return true, nil + return true } else { - owners, err := hmndata.FetchProjectOwners(c.Context(), c.Conn, projectId) - if err != nil { - return false, err - } for _, owner := range owners { if owner.ID == user.ID { - return true, nil + return true } } } } - return false, nil + return false } diff --git a/src/website/routes.go b/src/website/routes.go index 16da39b5..fd5ba7fc 100644 --- a/src/website/routes.go +++ b/src/website/routes.go @@ -311,6 +311,7 @@ func NewWebsiteRoutes(longRequestContext context.Context, conn *pgxpool.Pool) ht c.CurrentProject = &p.Project c.UrlContext = hmndata.UrlContextForProject(c.CurrentProject) + c.CurrentUserCanEditCurrentProject = CanEditProject(c.CurrentUser, p.Owners) if !p.Project.Personal { return c.Redirect(c.UrlContext.RewriteProjectUrl(c.URL()), http.StatusSeeOther) @@ -485,7 +486,6 @@ func LoadCommonWebsiteData(c *RequestContext) (bool, ResponseData) { panic(oops.New(err, "failed to fetch HMN project")) } c.CurrentProject = &dbProject.Project - owners = dbProject.Owners } if c.CurrentProject == nil { @@ -494,11 +494,12 @@ func LoadCommonWebsiteData(c *RequestContext) (bool, ResponseData) { canEditProject := false if c.CurrentUser != nil { - canEditProject = c.CurrentUser.IsStaff - if !canEditProject { + if c.CurrentUser.IsStaff { + canEditProject = true + } else { for _, o := range owners { if o.ID == c.CurrentUser.ID { - c.CurrentUserCanEditCurrentProject = true + canEditProject = true break } }