From e95a3cdb1131fdab0b246c27fed22117f9c7a7f5 Mon Sep 17 00:00:00 2001 From: Lorenzo Manacorda Date: Mon, 13 Jan 2025 16:44:02 +0100 Subject: [PATCH] fix query in unfollow --- message_handlers.go | 14 +++++++++++- message_handlers_test.go | 47 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/message_handlers.go b/message_handlers.go index aa6bb4c..09183d9 100644 --- a/message_handlers.go +++ b/message_handlers.go @@ -179,7 +179,19 @@ func handleFollowUnfollow(msg string, evt *event.Event) { } if un != "" { - if _, err := clients.db.Exec("DELETE FROM subscriptions WHERE mxid = ? AND attr_path IN ?", evt.Sender, aps); err != nil { + // Create the right number of placeholders: "(?,?,?)" + qmarks := make([]string, len(aps)) + args := make([]any, len(aps)) + for i, v := range aps { + qmarks[i] = "?" + args[i] = v + } + placeholders := strings.Join(qmarks, ",") + + query := fmt.Sprintf("DELETE FROM subscriptions WHERE mxid = ? AND attr_path IN (%s)", placeholders) + // We need to stash evt.Sender in args, because we can onlu pass two arguments to db.Exec + args = append([]any{evt.Sender}, args...) + if _, err := clients.db.Exec(query, args...); err != nil { panic(err) } } else { diff --git a/message_handlers_test.go b/message_handlers_test.go index 763b715..8dd605d 100644 --- a/message_handlers_test.go +++ b/message_handlers_test.go @@ -341,7 +341,7 @@ func TestCheckIfSubExists(t *testing.T) { } } -func TestFollowUnfollow(t *testing.T) { +func TestFollow(t *testing.T) { h = handlers{ packagesJSONFetcher: stubJSONFetcher, logFetcher: func(string) (string, bool) { @@ -431,6 +431,51 @@ func TestFollowUnfollow(t *testing.T) { }) } +func TestUnfollow(t *testing.T) { + h = handlers{ + packagesJSONFetcher: stubJSONFetcher, + logFetcher: func(string) (string, bool) { + return "1999", false + }, + sender: testSender, + } + + if err := setupDB(ctx, ":memory:"); err != nil { + panic(err) + } + + mps := []string{ + "btrbk", + "btrfs-list", + "diceware", + "python312Packages.diceware", + } + + all := append(mps, "foo") + + addPackages(all...) + + sub("foo") + + // First follow... + fillEventContent(evt, fmt.Sprintf("follow %s", "asymmetric")) + handleMessage(ctx, evt) + + // Then unfollow + fillEventContent(evt, fmt.Sprintf("unfollow %s", "asymmetric")) + handleMessage(ctx, evt) + + for _, p := range mps { + if exists, _ := checkIfSubExists(p, evt.RoomID.String()); exists { + t.Errorf("should not be subscribed to %s", p) + } + } + + if exists, _ := checkIfSubExists("foo", evt.RoomID.String()); !exists { + t.Errorf("should be subscribed to %s", "foo") + } +} + func TestFindPackagesForHandle(t *testing.T) { if err := setupDB(ctx, ":memory:"); err != nil { panic(err)