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

View File

@ -62,7 +62,7 @@ func BareMinimumSeed() *models.Project {
defer tx.Rollback(ctx)
fmt.Println("Creating HMN project...")
hmn := seedProject(ctx, tx, seedHMN)
hmn := seedProject(ctx, tx, seedHMN, nil)
utils.Must0(tx.Commit(ctx))
@ -101,11 +101,21 @@ func SampleSeed() {
users := []*models.User{alice, bob, charlie, spammer}
fmt.Println("Creating starter projects...")
hero := seedProject(ctx, tx, seedHandmadeHero)
fourcoder := seedProject(ctx, tx, seed4coder)
for i := 0; i < 3; i++ {
hero := seedProject(ctx, tx, seedHandmadeHero, []*models.User{admin})
fourcoder := seedProject(ctx, tx, seed4coder, []*models.User{bob})
for i := 0; i < 5; i++ {
name := fmt.Sprintf("%s %s", lorem.Word(1, 10), lorem.Word(1, 10))
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{
Slug: slug,
Name: name,
@ -113,8 +123,17 @@ func SampleSeed() {
Description: lorem.Paragraph(3, 5),
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...")
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
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,
`
INSERT INTO project (
@ -303,6 +322,14 @@ func seedProject(ctx context.Context, tx pgx.Tx, input models.Project) *models.P
))
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
}

View File

@ -77,7 +77,7 @@ func BlogIndex(c *RequestContext) ResponseData {
canCreate := false
if c.CurrentProject.HasBlog() && c.CurrentUser != nil {
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 {
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
// 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 {
return c.ErrorResponse(http.StatusInternalServerError, err)
}