Skip to content

feat(coderd): allow workspace owners to mark workspaces as favorite #11791

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 24, 2024

Conversation

johnstcn
Copy link
Member

@johnstcn johnstcn commented Jan 24, 2024

Part of #7912

Continues from #11673 and rebased on latest main

Notable changes:

  • favorite now a boolean
  • Workspaces can only be marked as 'favorite' by their owner. This can be migrated to its own table later if we need to allow favoriting other users' workspaces, but I suspect the rule of YAGNI may apply here. Migrating this data to a separate table in future should be a simple and reversible operation.

Comment on lines +7777 to +7782
if strings.Compare(preloadedUsers[w1.ID].Username, preloadedUsers[w2.ID].Username) < 0 {
return true
}

// Order by: workspace names
return sort.StringsAreSorted([]string{w1.Name, w2.Name})
return strings.Compare(w1.Name, w2.Name) < 0
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

review: fixes pre-existing ordering issue in dbmem that diverges from postgres behaviour

Comment on lines 1051 to 1053
httpapi.Write(ctx, rw, http.StatusForbidden, codersdk.Response{
Message: "You can only favorite workspaces that you own",
})
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

review: open to changing this response code if forbidden is not what we want here

Comment on lines 162 to 164
// To show the user's favorite workspaces first, we pass their userID and compare it to
// column favorite_of when ordering the rows.
filter.OrderByFavorite = apiKey.UserID
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

review: this is part of how we ensure that 'favorite' workspaces only come up first for their respective owners.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the naming is a bit confusing when read in query/code. I.e. "order by favorite" being the users ID.

How about something like (Prioritize|Order)FavoritesForUser?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about just RequesterID?

@johnstcn johnstcn marked this pull request as ready for review January 24, 2024 09:43
@johnstcn johnstcn requested review from mafredri and mtojek January 24, 2024 09:46
@johnstcn johnstcn requested a review from Emyrk January 24, 2024 10:58
Copy link
Member

@mtojek mtojek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comments or questions

Comment on lines +953 to +954
r.Put("/favorite", api.putFavoriteWorkspace)
r.Delete("/favorite", api.deleteFavoriteWorkspace)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: as we modify a single record in workspaces, I would consider single PATCH operation. This form is also correct but longer.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What you say is correct. One can also make an argument for the simplicity of a handler that does one thing only instead of a complex handler that maybe does many things. I don't feel particularly strongly either way; the separate handler approach seems to be more in line with existing conventions.

@@ -1366,7 +1471,7 @@ func (api *API) workspaceData(ctx context.Context, workspaces []database.Workspa
}, nil
}

func convertWorkspaces(workspaces []database.Workspace, data workspaceData) ([]codersdk.Workspace, error) {
func convertWorkspaces(requesterID uuid.UUID, workspaces []database.Workspace, data workspaceData) ([]codersdk.Workspace, error) {
buildByWorkspaceID := map[uuid.UUID]codersdk.WorkspaceBuild{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about adding a precondition here to fail fast if uuid.UUID is empty/null/zero?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be the purpose of this?

Copy link
Member

@mtojek mtojek Jan 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To catch misuse of convertWorkspaces where it is called without user context.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will address in a follow-up PR.

Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer this approach to the previous one, nice! A few small comments, and I wonder if we really need an own endpoint for the favorite, vs patching the workspace 🤔. Not a deal-breaker by any means.

@@ -1315,6 +1316,25 @@ func (*FakeQuerier) DeleteTailnetTunnel(_ context.Context, arg database.DeleteTa
return database.DeleteTailnetTunnelRow{}, ErrUnimplemented
}

func (q *FakeQuerier) FavoriteWorkspace(_ context.Context, arg uuid.UUID) error {
err := validateDatabaseType(arg)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this added by code gen? I thought we only add it for when arg is a struct (i.e. database.*Params). If it works, I don't see any harm though, it's more complete with the check in-place.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you're correct, it's auto-added by codegen.

@@ -7713,7 +7753,15 @@ func (q *FakeQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg database.
w1 := workspaces[i]
w2 := workspaces[j]

// Order by: running first
// Order by: favorite first
if arg.OrderByFavorite == w1.OwnerID && w1.Favorite {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If both are favorite, this might not produce the desired order. 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's possible, but as it's just dbmem I'm going to defer caring about it until it makes a test fail ;-)

Comment on lines 162 to 164
// To show the user's favorite workspaces first, we pass their userID and compare it to
// column favorite_of when ordering the rows.
filter.OrderByFavorite = apiKey.UserID
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the naming is a bit confusing when read in query/code. I.e. "order by favorite" being the users ID.

How about something like (Prioritize|Order)FavoritesForUser?

@johnstcn johnstcn merged commit f92336c into main Jan 24, 2024
@johnstcn johnstcn deleted the cj/favorite-workspaces-redux branch January 24, 2024 13:39
@github-actions github-actions bot locked and limited conversation to collaborators Jan 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants