wants list redux and removal

This commit is contained in:
ntr committed 2022-09-15 23:00:35 +10:00
1 parent 70a67b0c53
commit 12c3da9f5e
8 files changed
+86 -18

No files matched your search

+2
View File
@@ -177,6 +177,8 @@ export default function createApi(log: Log, pg: Pg, discogs, offers, events) {
ws.on('close', function onClose() {
log.info('ws disconnected', { user: ctx.state.user });
});
await rpc.onConnection();
});
return {
+30 -1
View File
@@ -23,7 +23,6 @@ export default function createRpc(log: Log, pg: Pg, user: User, ws, discogs, off
}
async function offerStatusSet(params: OfferStatusSetParams): Promise<OffersMessage> {
log.info(params);
const { id, status } = params;
assert(id, 'no id supplied');
assert(Status[status], 'unknown status');
@@ -32,6 +31,16 @@ export default function createRpc(log: Log, pg: Pg, user: User, ws, discogs, off
return [RpcMessageType.Offers, []]
}
async function wantRm(params: WantRmParams): Promise<WantsMessage> {
const { id } = params;
assert(id, 'no id supplied');
await pg.wantRm(id);
const wants = await pg.userWants(user);
return [RpcMessageType.Wants, wants];
}
async function receive(message: any): Promise<RpcMessage> {
const [msgType, params] = message;
@@ -44,6 +53,8 @@ export default function createRpc(log: Log, pg: Pg, user: User, ws, discogs, off
return ping();
case RpcMessageType.OfferStatusSet:
return offerStatusSet(params);
case RpcMessageType.WantRm:
return wantRm(params);
default:
throw new Error(`Message type ${msgType} is known but unhandled`);
}
@@ -54,6 +65,12 @@ export default function createRpc(log: Log, pg: Pg, user: User, ws, discogs, off
throw new Error(`Invalid parameters for message type ${msgType}: ${JSON.stringify(message)}`);
}
async function onConnection() {
const wants = await pg.userWants(user);
const wantsMsg: WantsMessage = [RpcMessageType.Wants, wants];
send(wantsMsg);
}
function send(msg: RpcMessage) {
ws.send(JSON.stringify(msg, (key, value) =>
typeof value === 'bigint'
@@ -65,6 +82,7 @@ export default function createRpc(log: Log, pg: Pg, user: User, ws, discogs, off
return {
receive,
send,
onConnection,
}
}
@@ -78,17 +96,24 @@ export enum RpcMessageType {
Error = 'Error',
OfferStatusSet = 'OfferStatusSet',
Wants = 'Wants',
WantRm = 'WantRm',
}
type ErrorMessage = [RpcMessageType.Error, string];
type PongMessage = [RpcMessageType.Pong, []];
type PingMessage = [RpcMessageType.Ping, []];
type WantsMessage = [RpcMessageType.Wants, Want[]];
type OffersMessage = [RpcMessageType.Offers, Offer[]];
type OfferStatusSetMessage = [RpcMessageType.OfferStatusSet, Offer[]];
export type RpcMessage =
PingMessage |
PongMessage |
WantsMessage |
OffersMessage |
OfferStatusSetMessage |
ErrorMessage;
@@ -98,3 +123,7 @@ type OfferStatusSetParams = {
id: UUID,
status: Status,
};
type WantRmParams = {
id: UUID,
};