Skip to content

Commit db41f30

Browse files
authored
Merge pull request #257 from DestinyItemManager/search-group
Further consolidate search deletes with other search commands
2 parents 509e2fe + 25dac9b commit db41f30

File tree

4 files changed

+57
-19
lines changed

4 files changed

+57
-19
lines changed

api/routes/update.ts

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { captureMessage } from '@sentry/node';
2-
import { chunk, groupBy, sortBy } from 'es-toolkit';
2+
import { chunk, groupBy, partition, sortBy } from 'es-toolkit';
33
import { isEmpty } from 'es-toolkit/compat';
44
import express from 'express';
55
import asyncHandler from 'express-async-handler';
@@ -286,8 +286,9 @@ async function statelyUpdate(
286286
platformMembershipId: string | undefined,
287287
destinyVersion: DestinyVersion,
288288
) {
289-
// We want to group save search and search updates together
290-
const actionKey = (u: ProfileUpdate) => (u.action === 'save_search' ? 'search' : u.action);
289+
// We want to group save/delete search and search updates together
290+
const actionKey = (u: ProfileUpdate) =>
291+
u.action === 'save_search' || u.action === 'delete_search' ? 'search' : u.action;
291292

292293
const sortedUpdates = sortBy(updates, [actionKey]).flatMap((u): ProfileUpdate[] => {
293294
// Separate out tag_cleanup updates into individual updates
@@ -382,20 +383,22 @@ async function statelyUpdate(
382383
// saved searches and used searches are collectively "searches"
383384
case 'search': {
384385
const searchUpdates = consolidateSearchUpdates(
385-
group as (UsedSearchUpdate | SavedSearchUpdate)[],
386+
group as (UsedSearchUpdate | SavedSearchUpdate | DeleteSearchUpdate)[],
386387
);
387-
await updateSearches(txn, platformMembershipId!, destinyVersion, searchUpdates);
388+
const [deletes, updates] = partition(searchUpdates, (u) => u.deleted);
389+
if (deletes.length) {
390+
await deleteSearchInStately(
391+
txn,
392+
platformMembershipId!,
393+
destinyVersion,
394+
deletes.map((u) => u.query),
395+
);
396+
}
397+
if (updates.length) {
398+
await updateSearches(txn, platformMembershipId!, destinyVersion, updates);
399+
}
388400
break;
389401
}
390-
391-
case 'delete_search':
392-
await deleteSearchInStately(
393-
txn,
394-
platformMembershipId!,
395-
destinyVersion,
396-
(group as DeleteSearchUpdate[]).map((u) => u.payload.query),
397-
);
398-
break;
399402
}
400403
}
401404
});
@@ -815,18 +818,26 @@ async function updateItemHashTag(
815818
metrics.timing('update.updateItemHashTag', start);
816819
}
817820

818-
function consolidateSearchUpdates(updates: (UsedSearchUpdate | SavedSearchUpdate)[]) {
821+
function consolidateSearchUpdates(
822+
updates: (UsedSearchUpdate | SavedSearchUpdate | DeleteSearchUpdate)[],
823+
) {
819824
const updatesByQuery = groupBy(updates, (u) => u.payload.query);
820825
return Object.values(updatesByQuery).map((group) => {
821826
const u: UpdateSearch = {
822827
query: group[0].payload.query,
823828
type: group[0].payload.type ?? SearchType.Item,
824829
incrementUsed: 0,
830+
deleted: false,
825831
};
826832
for (const update of group) {
827833
if (update.action === 'save_search') {
834+
u.deleted = false;
828835
u.saved = update.payload.saved;
836+
} else if (update.action === 'delete_search') {
837+
u.deleted = true;
838+
u.incrementUsed = 0;
829839
} else {
840+
u.deleted = false;
830841
u.incrementUsed++;
831842
}
832843
}

api/stately/bulk-queries.test.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,22 @@ describe('deleteAllDataForUser', () => {
3838
});
3939
await updateLoadout(txn, platformMembershipId, 2, [loadout]);
4040
await updateSearches(txn, platformMembershipId, 1, [
41-
{ query: 'is:handcannon', type: SearchType.Item, incrementUsed: 1, saved: false },
41+
{
42+
query: 'is:handcannon',
43+
type: SearchType.Item,
44+
incrementUsed: 1,
45+
saved: false,
46+
deleted: false,
47+
},
4248
]);
4349
await updateSearches(txn, platformMembershipId, 2, [
44-
{ query: 'tag:junk', type: SearchType.Item, incrementUsed: 1, saved: false },
50+
{
51+
query: 'tag:junk',
52+
type: SearchType.Item,
53+
incrementUsed: 1,
54+
saved: false,
55+
deleted: false,
56+
},
4557
]);
4658
await trackUntrackTriumphs(txn, platformMembershipId, [
4759
{ recordHash: 3851137658, tracked: true },
@@ -92,10 +104,22 @@ describe('exportDataForUser', () => {
92104
});
93105
await updateLoadout(txn, platformMembershipId, 2, [loadout]);
94106
await updateSearches(txn, platformMembershipId, 1, [
95-
{ query: 'is:handcannon', type: SearchType.Item, incrementUsed: 1, saved: false },
107+
{
108+
query: 'is:handcannon',
109+
type: SearchType.Item,
110+
incrementUsed: 1,
111+
saved: false,
112+
deleted: false,
113+
},
96114
]);
97115
await updateSearches(txn, platformMembershipId, 2, [
98-
{ query: 'tag:junk', type: SearchType.Item, incrementUsed: 1, saved: false },
116+
{
117+
query: 'tag:junk',
118+
type: SearchType.Item,
119+
incrementUsed: 1,
120+
saved: false,
121+
deleted: false,
122+
},
99123
]);
100124
await trackUntrackTriumphs(txn, platformMembershipId, [
101125
{ recordHash: 3851137658, tracked: true },

api/stately/searches-queries.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ async function updateUsedSearch(
2626
type,
2727
saved: false,
2828
incrementUsed: 1,
29+
deleted: false,
2930
},
3031
]);
3132
});
@@ -45,6 +46,7 @@ async function saveSearch(
4546
type,
4647
saved,
4748
incrementUsed: 0,
49+
deleted: false,
4850
},
4951
]);
5052
});

api/stately/searches-queries.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ export interface UpdateSearch {
125125
saved?: boolean;
126126
/** How much to increment the used count by. */
127127
incrementUsed: number;
128+
deleted: boolean;
128129
}
129130

130131
/**

0 commit comments

Comments
 (0)