Add owners to seeded projects

This commit is contained in:
Ben Visness 2022-05-11 23:39:43 -05:00
parent 196eda8185
commit 3aa16c6d12
4 changed files with 40 additions and 14 deletions

View File

@ -150,7 +150,7 @@ func FetchProjects(
for i, p := range projectRows { for i, p := range projectRows {
projectIds[i] = p.Project.ID projectIds[i] = p.Project.ID
} }
projectOwners, err := FetchMultipleProjectsOwners(ctx, tx, currentUser, projectIds) projectOwners, err := FetchMultipleProjectsOwners(ctx, tx, projectIds)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -316,7 +316,6 @@ type ProjectOwners struct {
func FetchMultipleProjectsOwners( func FetchMultipleProjectsOwners(
ctx context.Context, ctx context.Context,
dbConn db.ConnOrTx, dbConn db.ConnOrTx,
currentUser *models.User,
projectIds []int, projectIds []int,
) ([]ProjectOwners, error) { ) ([]ProjectOwners, error) {
perf := perf.ExtractPerf(ctx) perf := perf.ExtractPerf(ctx)
@ -359,8 +358,9 @@ func FetchMultipleProjectsOwners(
userIds = append(userIds, userProject.UserID) userIds = append(userIds, userProject.UserID)
} }
} }
users, err := FetchUsers(ctx, tx, currentUser, UsersQuery{ users, err := FetchUsers(ctx, tx, nil, UsersQuery{
UserIDs: userIds, UserIDs: userIds,
AnyStatus: true,
}) })
if err != nil { if err != nil {
return nil, oops.New(err, "failed to fetch users for projects") return nil, oops.New(err, "failed to fetch users for projects")
@ -408,14 +408,13 @@ func FetchMultipleProjectsOwners(
func FetchProjectOwners( func FetchProjectOwners(
ctx context.Context, ctx context.Context,
dbConn db.ConnOrTx, dbConn db.ConnOrTx,
currentUser *models.User,
projectId int, projectId int,
) ([]*models.User, error) { ) ([]*models.User, error) {
perf := perf.ExtractPerf(ctx) perf := perf.ExtractPerf(ctx)
perf.StartBlock("SQL", "Fetch owners for project") perf.StartBlock("SQL", "Fetch owners for project")
defer perf.EndBlock() defer perf.EndBlock()
projectOwners, err := FetchMultipleProjectsOwners(ctx, dbConn, currentUser, []int{projectId}) projectOwners, err := FetchMultipleProjectsOwners(ctx, dbConn, []int{projectId})
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -62,7 +62,7 @@ func BareMinimumSeed() *models.Project {
defer tx.Rollback(ctx) defer tx.Rollback(ctx)
fmt.Println("Creating HMN project...") fmt.Println("Creating HMN project...")
hmn := seedProject(ctx, tx, seedHMN) hmn := seedProject(ctx, tx, seedHMN, nil)
utils.Must0(tx.Commit(ctx)) utils.Must0(tx.Commit(ctx))
@ -101,11 +101,21 @@ func SampleSeed() {
users := []*models.User{alice, bob, charlie, spammer} users := []*models.User{alice, bob, charlie, spammer}
fmt.Println("Creating starter projects...") fmt.Println("Creating starter projects...")
hero := seedProject(ctx, tx, seedHandmadeHero) hero := seedProject(ctx, tx, seedHandmadeHero, []*models.User{admin})
fourcoder := seedProject(ctx, tx, seed4coder) fourcoder := seedProject(ctx, tx, seed4coder, []*models.User{bob})
for i := 0; i < 3; i++ { for i := 0; i < 5; i++ {
name := fmt.Sprintf("%s %s", lorem.Word(1, 10), lorem.Word(1, 10)) name := fmt.Sprintf("%s %s", lorem.Word(1, 10), lorem.Word(1, 10))
slug := strings.ReplaceAll(strings.ToLower(name), " ", "-") slug := strings.ReplaceAll(strings.ToLower(name), " ", "-")
possibleOwners := []*models.User{alice, bob, charlie}
var owners []*models.User
for ownerIdx, owner := range possibleOwners {
mask := (i % ((1 << len(possibleOwners)) - 1)) + 1
if (1<<ownerIdx)&mask != 0 {
owners = append(owners, owner)
}
}
seedProject(ctx, tx, models.Project{ seedProject(ctx, tx, models.Project{
Slug: slug, Slug: slug,
Name: name, Name: name,
@ -113,8 +123,17 @@ func SampleSeed() {
Description: lorem.Paragraph(3, 5), Description: lorem.Paragraph(3, 5),
Personal: true, Personal: true,
}) }, owners)
} }
// spam project!
seedProject(ctx, tx, models.Project{
Slug: "spam",
Name: "Cheap abstraction enhancers",
Blurb: "Get higher than ever before...up the ladder of abstraction.",
Description: "Tired of boring details like the actual problem assigned to you? The sky's the limit with these abstraction enhancers, guaranteed to sweep away all those pesky details so you can focus on what matters: \"architecture\".",
Personal: true,
}, []*models.User{spammer})
fmt.Println("Creating some forum threads...") fmt.Println("Creating some forum threads...")
for i := 0; i < 5; i++ { for i := 0; i < 5; i++ {
@ -246,7 +265,7 @@ func populateThread(ctx context.Context, tx pgx.Tx, thread *models.Thread, users
var latestProjectId int var latestProjectId int
func seedProject(ctx context.Context, tx pgx.Tx, input models.Project) *models.Project { func seedProject(ctx context.Context, tx pgx.Tx, input models.Project, owners []*models.User) *models.Project {
project := db.MustQueryOne[models.Project](ctx, tx, project := db.MustQueryOne[models.Project](ctx, tx,
` `
INSERT INTO project ( INSERT INTO project (
@ -303,6 +322,14 @@ func seedProject(ctx context.Context, tx pgx.Tx, input models.Project) *models.P
)) ))
project.ForumID = &forum.ID project.ForumID = &forum.ID
// Add project owners
for _, owner := range owners {
utils.Must1(tx.Exec(ctx,
`INSERT INTO user_project (user_id, project_id) VALUES ($1, $2)`,
owner.ID, project.ID,
))
}
return project return project
} }

View File

@ -77,7 +77,7 @@ func BlogIndex(c *RequestContext) ResponseData {
canCreate := false canCreate := false
if c.CurrentProject.HasBlog() && c.CurrentUser != nil { if c.CurrentProject.HasBlog() && c.CurrentUser != nil {
isProjectOwner := false isProjectOwner := false
owners, err := hmndata.FetchProjectOwners(c.Context(), c.Conn, c.CurrentUser, c.CurrentProject.ID) owners, err := hmndata.FetchProjectOwners(c.Context(), c.Conn, c.CurrentProject.ID)
if err != nil { if err != nil {
return c.ErrorResponse(http.StatusInternalServerError, oops.New(err, "failed to fetch project owners")) return c.ErrorResponse(http.StatusInternalServerError, oops.New(err, "failed to fetch project owners"))
} }

View File

@ -181,7 +181,7 @@ func ProjectHomepage(c *RequestContext) ResponseData {
// There are no further permission checks to do, because permissions are // There are no further permission checks to do, because permissions are
// checked whatever way we fetch the project. // checked whatever way we fetch the project.
owners, err := hmndata.FetchProjectOwners(c.Context(), c.Conn, c.CurrentUser, c.CurrentProject.ID) owners, err := hmndata.FetchProjectOwners(c.Context(), c.Conn, c.CurrentProject.ID)
if err != nil { if err != nil {
return c.ErrorResponse(http.StatusInternalServerError, err) return c.ErrorResponse(http.StatusInternalServerError, err)
} }