API: Fix case with abandoned partial paste

It is possible to abandon a paste while editing it on the web UI,
causing the database to have a state that the API was not expecting.
This filters out such pastes.

When we rewrite the paste.sr.ht frontend to work via GraphQL, we should
change the column to non-nullable and remove this where clause.
This commit is contained in:
Drew DeVault 2021-09-22 09:20:29 +02:00
parent de46f494b0
commit f9ccb57940
2 changed files with 10 additions and 4 deletions

View File

@ -302,7 +302,12 @@ func (r *queryResolver) Pastes(ctx context.Context, cursor *coremodel.Cursor) (*
query := database.
Select(ctx, paste).
From(`paste`).
Where(`paste.user_id = ?`, auth.ForContext(ctx).UserID)
Where(sq.And{
// TODO: Remove this and set the column to non-nullable after
// refactoring the frontend to create new pastes via GQL
sq.Expr(`paste.sha IS NOT NULL`),
sq.Expr(`paste.user_id = ?`, auth.ForContext(ctx).UserID),
})
pastes, cursor = paste.QueryWithCursor(ctx, tx, query, cursor)
return nil
}); err != nil {
@ -331,6 +336,7 @@ func (r *userResolver) Pastes(ctx context.Context, obj *model.User, cursor *core
Select(ctx, paste).
From(`paste`).
Where(sq.And{
sq.Expr(`paste.sha IS NOT NULL`),
sq.Expr(`paste.user_id = ?`, obj.ID),
sq.Or{
sq.Expr(`paste.user_id = ?`, auth.ForContext(ctx).UserID),

View File

@ -7,11 +7,11 @@ query {
me {
# Grab their canonical name:
canonicalName
# And a list of repositories:
# And a list of pastes:
pastes {
# This resource is paginated, so it has a cursor. If you pass this value
# into repositories(cursor:"...") in a subsequent request, you'll get the
# next page.
# into pastes(cursor:"...") in a subsequent request, you'll get the next
# page.
cursor
# These are the actual results. Grab the id, name, and updated fields
# from each repository.