From 92d6a31aa97ec45e64da1599ec0d69db68b59f9c Mon Sep 17 00:00:00 2001 From: Asaf Gartner Date: Mon, 31 Jan 2022 10:22:25 +0200 Subject: [PATCH] Fixed tag removal issue. --- src/discord/message_handling.go | 70 ++++++++++++++++----------------- src/hmndata/project_helper.go | 2 +- src/website/projects.go | 5 ++- 3 files changed, 39 insertions(+), 38 deletions(-) diff --git a/src/discord/message_handling.go b/src/discord/message_handling.go index 719df531..0522197a 100644 --- a/src/discord/message_handling.go +++ b/src/discord/message_handling.go @@ -798,45 +798,43 @@ func HandleSnippetForInternedMessage(ctx context.Context, dbConn db.ConnOrTx, in var desiredTags []int var allTags []int - if len(messageTags) > 0 { - // Fetch projects so we know what tags the user can apply to their snippet. - projects, err := hmndata.FetchProjects(ctx, tx, interned.HMNUser, hmndata.ProjectsQuery{ - OwnerIDs: []int{interned.HMNUser.ID}, - }) - if err != nil { - return oops.New(err, "failed to look up user projects") - } + // Fetch projects so we know what tags the user can apply to their snippet. + projects, err := hmndata.FetchProjects(ctx, tx, interned.HMNUser, hmndata.ProjectsQuery{ + OwnerIDs: []int{interned.HMNUser.ID}, + }) + if err != nil { + return oops.New(err, "failed to look up user projects") + } - projectIDs := make([]int, len(projects)) - for i, p := range projects { - projectIDs[i] = p.Project.ID - } + projectIDs := make([]int, len(projects)) + for i, p := range projects { + projectIDs[i] = p.Project.ID + } - type tagsRow struct { - Tag models.Tag `db:"tags"` - } - iUserTags, err := db.Query(ctx, tx, tagsRow{}, - ` - SELECT $columns - FROM - tags - JOIN handmade_project AS project ON project.tag = tags.id - WHERE - project.id = ANY ($1) - `, - projectIDs, - ) - if err != nil { - return oops.New(err, "failed to fetch tags for user projects") - } + type tagsRow struct { + Tag models.Tag `db:"tags"` + } + iUserTags, err := db.Query(ctx, tx, tagsRow{}, + ` + SELECT $columns + FROM + tags + JOIN handmade_project AS project ON project.tag = tags.id + WHERE + project.id = ANY ($1) + `, + projectIDs, + ) + if err != nil { + return oops.New(err, "failed to fetch tags for user projects") + } - for _, itag := range iUserTags { - tag := itag.(*tagsRow).Tag - for _, messageTag := range messageTags { - allTags = append(allTags, tag.ID) - if strings.EqualFold(tag.Text, messageTag) { - desiredTags = append(desiredTags, tag.ID) - } + for _, itag := range iUserTags { + tag := itag.(*tagsRow).Tag + allTags = append(allTags, tag.ID) + for _, messageTag := range messageTags { + if strings.EqualFold(tag.Text, messageTag) { + desiredTags = append(desiredTags, tag.ID) } } } diff --git a/src/hmndata/project_helper.go b/src/hmndata/project_helper.go index b2edbc05..c8eb0deb 100644 --- a/src/hmndata/project_helper.go +++ b/src/hmndata/project_helper.go @@ -463,7 +463,7 @@ func SetProjectTag( IncludeHidden: true, }) if err != nil { - return nil, err + return nil, oops.New(err, "Failed to fetch project") } var resultTag *models.Tag diff --git a/src/website/projects.go b/src/website/projects.go index 3488854e..efe9613a 100644 --- a/src/website/projects.go +++ b/src/website/projects.go @@ -760,7 +760,10 @@ func updateProject(ctx context.Context, tx pgx.Tx, user *models.User, payload *P return oops.New(err, "Failed to update project") } - hmndata.SetProjectTag(ctx, tx, payload.ProjectID, payload.Tag) + _, err = hmndata.SetProjectTag(ctx, tx, payload.ProjectID, payload.Tag) + if err != nil { + return err + } if user.IsStaff { _, err = tx.Exec(ctx,