Skip to content

Commit ccc3f2e

Browse files
committed
fixes based on copilot comments
- add request validation - fix docstrings
1 parent 1bd5f55 commit ccc3f2e

3 files changed

Lines changed: 36 additions & 3 deletions

File tree

db/crud.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ func (ch ChannelHistoryResp) AddChannelHistoryEntry(name string, seq uint64) {
237237
// from channel name to the sequences at which the document was removed from that channel.
238238
// It collects revocation sequences from the active Channels map, the ChannelSet, and the
239239
// ChannelSetHistory (overflow). Only channels that have been revoked at least once appear in
240-
// the result; channels the document is currently assigned to are excluded.
240+
// the result; active memberships with no revocation history are omitted, even though a currently
241+
// assigned channel can still appear if it was revoked and later re-added.
241242
func (c *DatabaseCollection) GetDocChannelHistory(ctx context.Context, docid string) (map[string][]uint64, error) {
242243

243244
chanHistory := make(ChannelHistoryResp)

rest/api_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4141,6 +4141,28 @@ func TestDocumentChannelHistoryCompact(t *testing.T) {
41414141

41424142
assert.Equal(t, expectedOutput, chanOutput)
41434143
})
4144+
4145+
t.Run("empty doc_ids returns 400", func(t *testing.T) {
4146+
req := CompactDocChannelHistoryRequest{
4147+
DocIds: []string{},
4148+
Seq: 1,
4149+
}
4150+
bodyBytes, err := base.JSONMarshal(req)
4151+
require.NoError(t, err)
4152+
resp := rt.SendAdminRequest("POST", "/{{.keyspace}}/_channel_history/_compact", string(bodyBytes))
4153+
RequireStatus(t, resp, http.StatusBadRequest)
4154+
})
4155+
4156+
t.Run("seq zero returns 400", func(t *testing.T) {
4157+
req := CompactDocChannelHistoryRequest{
4158+
DocIds: []string{"doc1"},
4159+
Seq: 0,
4160+
}
4161+
bodyBytes, err := base.JSONMarshal(req)
4162+
require.NoError(t, err)
4163+
resp := rt.SendAdminRequest("POST", "/{{.keyspace}}/_channel_history/_compact", string(bodyBytes))
4164+
RequireStatus(t, resp, http.StatusBadRequest)
4165+
})
41444166
}
41454167

41464168
// TestCompactNonImportedDocWithAutoImport verifies that when CompactDocChannelHistory is called

rest/doc_api.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ type CompactDocChannelHistoryRequest struct {
949949
Seq uint64 `json:"seq"`
950950
}
951951

952-
// handleCompactDocChannelHistory handles POST /{keyspace}/_compact_channel_history.
952+
// handleCompactDocChannelHistory handles POST /{keyspace}/_channel_history/_compact.
953953
// It accepts a JSON body with a list of doc IDs and a sequence number, and removes channel
954954
// history entries that ended at or before that sequence for each document, returning the
955955
// list of compacted channel names per doc ID.
@@ -962,13 +962,23 @@ func (h *handler) handleCompactDocChannelHistory() error {
962962
return base.HTTPErrorf(http.StatusBadRequest, "invalid JSON: %v", err)
963963
}
964964

965+
if len(req.DocIds) == 0 {
966+
return base.HTTPErrorf(http.StatusBadRequest, "missing doc ids")
967+
}
968+
969+
if req.Seq == 0 {
970+
return base.HTTPErrorf(http.StatusBadRequest, "missing seq")
971+
}
972+
965973
res := make(map[string][]string)
966974
for _, id := range req.DocIds {
967975
channels, err := h.collection.CompactDocChannelHistory(h.ctx(), id, req.Seq)
968976
if err != nil && !base.IsDocNotFoundError(err) {
969977
return err
970978
}
971-
res[id] = channels
979+
if _, ok := res[id]; !ok {
980+
res[id] = channels
981+
}
972982
}
973983

974984
h.writeJSON(res)

0 commit comments

Comments
 (0)