Fix various bugs

This commit is contained in:
Ben Visness 2021-12-08 21:50:35 -06:00
parent 40cd19c5f0
commit 79c9738b96
5 changed files with 24 additions and 44 deletions

View File

@ -62,6 +62,11 @@ func FetchSnippets(
} }
iSnippetIDs := itSnippetIDs.ToSlice() 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)) q.IDs = make([]int, len(iSnippetIDs))
for i := range iSnippetIDs { for i := range iSnippetIDs {
q.IDs[i] = iSnippetIDs[i].(*snippetIDRow).SnippetID q.IDs[i] = iSnippetIDs[i].(*snippetIDRow).SnippetID

View File

@ -18,7 +18,7 @@
<img alt="{{ .Project.Name }}" src="{{ .Project.Logo }}"> <img alt="{{ .Project.Name }}" src="{{ .Project.Logo }}">
{{ else }} {{ else }}
<div class="bg--dim w-100 aspect-ratio--1x1 relative"> <div class="bg--dim w-100 aspect-ratio--1x1 relative">
<div class="aspect-ratio--object flex justify-center items-center f3 b c--dimmest">{{ .Project.Name }}</div> <div class="aspect-ratio--object flex justify-center items-center f3 b c--dimmest tc">{{ .Project.Name }}</div>
</div> </div>
{{ end }} {{ end }}
</div> </div>

View File

@ -39,10 +39,7 @@ func PodcastIndex(c *RequestContext) ResponseData {
return FourOhFour(c) return FourOhFour(c)
} }
canEdit, err := CanEditProject(c, c.CurrentUser, podcastResult.Podcast.ProjectID) canEdit := c.CurrentUserCanEditCurrentProject
if err != nil {
return c.ErrorResponse(http.StatusInternalServerError, err)
}
baseData := getBaseDataAutocrumb(c, podcastResult.Podcast.Title) baseData := getBaseDataAutocrumb(c, podcastResult.Podcast.Title)
@ -78,10 +75,7 @@ func PodcastEdit(c *RequestContext) ResponseData {
return c.ErrorResponse(http.StatusInternalServerError, err) return c.ErrorResponse(http.StatusInternalServerError, err)
} }
canEdit, err := CanEditProject(c, c.CurrentUser, podcastResult.Podcast.ProjectID) canEdit := c.CurrentUserCanEditCurrentProject
if err != nil {
return c.ErrorResponse(http.StatusInternalServerError, err)
}
if podcastResult.Podcast == nil || !canEdit { if podcastResult.Podcast == nil || !canEdit {
return FourOhFour(c) return FourOhFour(c)
@ -112,10 +106,7 @@ func PodcastEditSubmit(c *RequestContext) ResponseData {
return c.ErrorResponse(http.StatusInternalServerError, err) return c.ErrorResponse(http.StatusInternalServerError, err)
} }
canEdit, err := CanEditProject(c, c.CurrentUser, podcastResult.Podcast.ProjectID) canEdit := c.CurrentUserCanEditCurrentProject
if err != nil {
return c.ErrorResponse(http.StatusInternalServerError, err)
}
if podcastResult.Podcast == nil || !canEdit { if podcastResult.Podcast == nil || !canEdit {
return FourOhFour(c) return FourOhFour(c)
@ -218,11 +209,7 @@ func PodcastEpisode(c *RequestContext) ResponseData {
return FourOhFour(c) return FourOhFour(c)
} }
canEdit, err := CanEditProject(c, c.CurrentUser, podcastResult.Podcast.ProjectID) canEdit := c.CurrentUserCanEditCurrentProject
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
}
editUrl := "" editUrl := ""
if canEdit { if canEdit {
@ -268,10 +255,7 @@ func PodcastEpisodeNew(c *RequestContext) ResponseData {
return c.ErrorResponse(http.StatusInternalServerError, err) return c.ErrorResponse(http.StatusInternalServerError, err)
} }
canEdit, err := CanEditProject(c, c.CurrentUser, podcastResult.Podcast.ProjectID) canEdit := c.CurrentUserCanEditCurrentProject
if err != nil {
return c.ErrorResponse(http.StatusInternalServerError, err)
}
if podcastResult.Podcast == nil || !canEdit { if podcastResult.Podcast == nil || !canEdit {
return FourOhFour(c) return FourOhFour(c)
@ -311,10 +295,7 @@ func PodcastEpisodeEdit(c *RequestContext) ResponseData {
return c.ErrorResponse(http.StatusInternalServerError, err) return c.ErrorResponse(http.StatusInternalServerError, err)
} }
canEdit, err := CanEditProject(c, c.CurrentUser, podcastResult.Podcast.ProjectID) canEdit := c.CurrentUserCanEditCurrentProject
if err != nil {
return c.ErrorResponse(http.StatusInternalServerError, err)
}
if podcastResult.Podcast == nil || len(podcastResult.Episodes) == 0 || !canEdit { if podcastResult.Podcast == nil || len(podcastResult.Episodes) == 0 || !canEdit {
return FourOhFour(c) return FourOhFour(c)
@ -360,10 +341,7 @@ func PodcastEpisodeSubmit(c *RequestContext) ResponseData {
return c.ErrorResponse(http.StatusInternalServerError, err) return c.ErrorResponse(http.StatusInternalServerError, err)
} }
canEdit, err := CanEditProject(c, c.CurrentUser, podcastResult.Podcast.ProjectID) canEdit := c.CurrentUserCanEditCurrentProject
if err != nil {
return c.ErrorResponse(http.StatusInternalServerError, err)
}
if podcastResult.Podcast == nil || (isEdit && len(podcastResult.Episodes) == 0) || !canEdit { if podcastResult.Podcast == nil || (isEdit && len(podcastResult.Episodes) == 0) || !canEdit {
return FourOhFour(c) return FourOhFour(c)

View File

@ -692,7 +692,7 @@ func updateProject(ctx context.Context, tx pgx.Tx, user *models.User, payload *P
blurb = $?, blurb = $?,
description = $?, description = $?,
descparsed = $?, descparsed = $?,
lifecycle = $?, lifecycle = $?
`, `,
payload.Name, payload.Name,
payload.Blurb, payload.Blurb,
@ -701,7 +701,7 @@ func updateProject(ctx context.Context, tx pgx.Tx, user *models.User, payload *P
payload.Lifecycle, payload.Lifecycle,
) )
if user.IsStaff { if user.IsStaff {
qb.Add(`hidden = $?`, payload.Hidden) qb.Add(`, hidden = $?`, payload.Hidden)
} }
qb.Add(`WHERE id = $?`, payload.ProjectID) qb.Add(`WHERE id = $?`, payload.ProjectID)
@ -857,21 +857,17 @@ func GetFormImage(c *RequestContext, fieldName string) (FormImage, error) {
return res, nil 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 != nil {
if user.IsStaff { if user.IsStaff {
return true, nil return true
} else { } else {
owners, err := hmndata.FetchProjectOwners(c.Context(), c.Conn, projectId)
if err != nil {
return false, err
}
for _, owner := range owners { for _, owner := range owners {
if owner.ID == user.ID { if owner.ID == user.ID {
return true, nil return true
} }
} }
} }
} }
return false, nil return false
} }

View File

@ -311,6 +311,7 @@ func NewWebsiteRoutes(longRequestContext context.Context, conn *pgxpool.Pool) ht
c.CurrentProject = &p.Project c.CurrentProject = &p.Project
c.UrlContext = hmndata.UrlContextForProject(c.CurrentProject) c.UrlContext = hmndata.UrlContextForProject(c.CurrentProject)
c.CurrentUserCanEditCurrentProject = CanEditProject(c.CurrentUser, p.Owners)
if !p.Project.Personal { if !p.Project.Personal {
return c.Redirect(c.UrlContext.RewriteProjectUrl(c.URL()), http.StatusSeeOther) 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")) panic(oops.New(err, "failed to fetch HMN project"))
} }
c.CurrentProject = &dbProject.Project c.CurrentProject = &dbProject.Project
owners = dbProject.Owners
} }
if c.CurrentProject == nil { if c.CurrentProject == nil {
@ -494,11 +494,12 @@ func LoadCommonWebsiteData(c *RequestContext) (bool, ResponseData) {
canEditProject := false canEditProject := false
if c.CurrentUser != nil { if c.CurrentUser != nil {
canEditProject = c.CurrentUser.IsStaff if c.CurrentUser.IsStaff {
if !canEditProject { canEditProject = true
} else {
for _, o := range owners { for _, o := range owners {
if o.ID == c.CurrentUser.ID { if o.ID == c.CurrentUser.ID {
c.CurrentUserCanEditCurrentProject = true canEditProject = true
break break
} }
} }