Skip to content

Commit

Permalink
fix query in unfollow
Browse files Browse the repository at this point in the history
  • Loading branch information
asymmetric committed Jan 13, 2025
1 parent 7444f2c commit e95a3cd
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
14 changes: 13 additions & 1 deletion message_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
47 changes: 46 additions & 1 deletion message_handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit e95a3cd

Please sign in to comment.