Skip to content

Commit 0c027bf

Browse files
authored
Merge pull request #268 from DestinyItemManager/loadout-share
2 parents 4db8e8d + 1fbca18 commit 0c027bf

File tree

4 files changed

+27
-16
lines changed

4 files changed

+27
-16
lines changed

api/routes/import.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,12 @@ export async function statelyImport(
8989
triumphs: ExportResponse['triumphs'],
9090
searches: ExportResponse['searches'],
9191
itemHashTags: ItemHashTag[],
92-
deleteExisting = true,
9392
): Promise<number> {
9493
// TODO: what we should do, is map all these to items, and then we can just do
9594
// batch puts, 25 at a time.
9695

9796
let numTriumphs = 0;
98-
if (deleteExisting) {
99-
await deleteAllDataForUser(bungieMembershipId, platformMembershipIds);
100-
}
97+
await deleteAllDataForUser(bungieMembershipId, platformMembershipIds);
10198

10299
const settingsItem = convertToStatelyItem(
103100
{ ...defaultSettings, ...settings },

api/routes/update.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,14 @@ function validateUpdates(
167167
};
168168
}
169169
if (result.status !== 'Success') {
170+
const dimVersion = req.headers['x-dim-version']?.[0];
170171
captureMessage(`update ${update.action} failed validation: ${result.message}`, {
171172
extra: {
172173
update,
173174
result,
174175
platformMembershipId,
175176
appId,
176-
dimVersion: req.headers['x-dim-version']?.[0],
177+
dimVersion: `v${dimVersion?.replaceAll('.', '_') ?? 'Unknown'}`,
177178
},
178179
});
179180
console.log('Stately failed update', update.action, result, appId);

api/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ const errorHandler: ErrorRequestHandler = (err, req, res, _next) => {
162162
tags: {
163163
dimApp: dimApp?.id,
164164
user: user?.bungieMembershipId,
165-
dimVersion: `v${dimVersion ?? 'Unknown'}`,
165+
dimVersion: `v${dimVersion?.replaceAll('.', '_') ?? 'Unknown'}`,
166166
},
167167
});
168168
// Allow any origin to see the response

api/stately/loadout-share-queries.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { keyPath, StatelyError, WithPutOptions } from '@stately-cloud/client';
22
import { Loadout } from '../shapes/loadouts.js';
3+
import { delay } from '../utils.js';
34
import { client } from './client.js';
45
import { LoadoutShare as StatelyLoadoutShare } from './generated/index.js';
56
import {
@@ -82,18 +83,30 @@ export async function addLoadoutSharesForMigration(
8283
* Touch the last_accessed_at and visits fields to keep track of access.
8384
*/
8485
export async function recordAccess(shareId: string): Promise<void> {
85-
// Hmm this is probably pretty expensive. Should I store the view count in a
86-
// separate item? It'd also be nice to have an Update API.
87-
await client.transaction(async (txn) => {
88-
const loadoutShare = await txn.get('LoadoutShare', keyFor(shareId));
89-
if (!loadoutShare) {
90-
throw new Error("somehow this loadout share doesn't exist");
91-
}
86+
for (let attempts = 0; attempts < 3; attempts++) {
87+
try {
88+
// Hmm this is probably pretty expensive. Should I store the view count in a
89+
// separate item? It'd also be nice to have an Update API.
90+
await client.transaction(async (txn) => {
91+
const loadoutShare = await txn.get('LoadoutShare', keyFor(shareId));
92+
if (!loadoutShare) {
93+
throw new Error("somehow this loadout share doesn't exist");
94+
}
9295

93-
loadoutShare.viewCount++;
96+
loadoutShare.viewCount++;
9497

95-
await txn.put(loadoutShare);
96-
});
98+
await txn.put(loadoutShare);
99+
});
100+
return;
101+
} catch (e) {
102+
if (e instanceof StatelyError && e.statelyCode === 'ConcurrentModification') {
103+
// try again after a delay
104+
await delay(100 * Math.random() + 100);
105+
} else {
106+
throw e;
107+
}
108+
}
109+
}
97110
}
98111

99112
export async function getLoadoutShareByShareId(shareId: string): Promise<Loadout | undefined> {

0 commit comments

Comments
 (0)