68 Commits
Author SHA1 Message Date
ntr 623d0f3938 bundle offers grouped 2022-10-23 15:22:15 +11:00
ntr 7946259550 add logrocket 2022-10-04 22:59:02 +11:00
ntr 015a2fd66c fix fetching login routes stuff 2022-10-04 22:41:49 +11:00
ntr 77428aa7de various styles 2022-09-20 21:25:48 +10:00
ntr 2761456009 correect nginx conf for redirs 2022-09-19 19:56:26 +10:00
ntr 6b88740595 extra info 2022-09-19 19:55:47 +10:00
ntr 9fad9a69ec nginx conf 2022-09-19 19:46:52 +10:00
ntr 9f0bb35d09 don't upstream client in prod nginx 2022-09-19 19:45:10 +10:00
ntr e792951d24 no client in prod 2022-09-19 19:44:33 +10:00
ntr ef33baacba static 2022-09-19 19:41:47 +10:00
ntr 9b8676809d email subject 2022-09-19 19:37:47 +10:00
ntr e45b2ca9cd styles and login return 2022-09-19 19:36:02 +10:00
ntr 1e368913ab info page 2022-09-19 19:34:04 +10:00
ntr f6f03730e3 login check effect 2022-09-18 21:04:01 +10:00
ntr 3bd8691ebf sync 2022-09-18 20:32:42 +10:00
ntr 470270954f styles n stuff 2022-09-18 20:26:57 +10:00
ntr 6b8ebe3f35 rn home offers 2022-09-16 21:48:26 +10:00
ntr acf7e54be9 remove unused api methods 2022-09-16 21:48:14 +10:00
ntr 73248f7d9f big websocket change 2022-09-16 19:21:11 +10:00
ntr 0e3c272adc fix user rerender race condition 2022-09-15 23:47:20 +10:00
ntr d2ec08159d ws url connect 2022-09-15 23:02:33 +10:00
ntr 12c3da9f5e wants list redux and removal 2022-09-15 23:00:35 +10:00
ntr 70a67b0c53 startup sync 2022-09-15 22:20:48 +10:00
ntr 644398319d prod nginx changes and remove want on offer complete 2022-09-15 22:14:54 +10:00
ntr 2c66c47163 rpc 2022-09-11 21:16:33 +10:00
ntr 111f690482 remove preact prerender from build 2022-09-10 19:51:49 +10:00
ntr b0572cb63d based redux 2022-09-10 18:54:01 +10:00
ntr 5f853696ab offers changes and select status 2022-09-10 17:47:18 +10:00
ntr bbc3fe6b48 singularise listing fetching 2022-09-05 21:21:34 +10:00
ntr f2294def05 this is podracing 2022-09-04 20:36:42 +10:00
ntr 65d37eeb61 ws 2022-09-04 18:47:51 +10:00
ntr 0bf75d7faa nearly 2022-09-03 15:03:16 +10:00
ntr cf01670ee6 unistore wip 2022-09-03 14:26:41 +10:00
ntr a43d4f32e4 simplify and automate offer creation 2022-09-02 00:06:51 +10:00
ntr 7ea2c37116 logout and info 2022-08-30 23:02:34 +10:00
ntr 4041d9827d autofocus 2022-08-30 20:34:57 +10:00
ntr 94dfe7c082 use correct nginx conf 2022-08-30 19:45:01 +10:00
ntr 20a587600e add 433 to prod nginx 2022-08-30 19:42:32 +10:00
ntr 106921a0da binary targets 2022-08-30 19:38:08 +10:00
ntr 2804477601 compose and nginx 2022-08-30 19:25:36 +10:00
ntr 44c83d3b33 moar logs 2022-08-30 18:39:22 +10:00
ntr 70cb07ee27 size plugin thing 2022-08-25 23:22:37 +10:00
ntr 9d21ee63d3 maybe i don't need tsconfig 2022-08-25 23:21:59 +10:00
ntr 82c905650f can't get build to ignore ts errors 2022-08-25 23:05:20 +10:00
ntr 047f606e83 acp stuff 2022-08-23 23:35:25 +10:00
ntr deb9eb3fc2 offers wip 2022-08-23 00:02:50 +10:00
ntr 453c2da045 offers page 2022-08-20 22:36:17 +10:00
ntr 323c7877b1 yeh boi 2022-08-18 00:34:46 +10:00
ntr 30bf7426e4 want events 2022-08-17 23:22:48 +10:00
ntr f5e75f4fe6 wants wip" 2022-08-16 10:09:36 +10:00
ntr c99f6fd2b6 error msg is back 2022-08-14 23:17:58 +10:00
ntr 12f00eb639 api response 2022-08-14 23:04:56 +10:00
ntr 7121b2f4cd interceptor error types needed 2022-08-14 22:56:46 +10:00
ntr b0bca74ee2 stylez 2022-08-14 22:17:42 +10:00
ntr 9a42c290ae css and some nav 2022-08-10 23:47:33 +10:00
ntr a8e5e5d7cd more auth stuff 2022-08-08 22:58:19 +10:00
ntr 65e603f222 cookies working for some raeson now 2022-08-08 21:36:46 +10:00
ntr 413aeedadf auth init 2022-08-08 19:51:17 +10:00
ntr faa2e70e08 search 2022-08-08 11:22:05 +10:00
ntr bd32044427 move to koa and add user pages 2022-08-07 14:27:13 +10:00
ntr b4114a2440 non discogs users 2022-08-06 21:49:40 +10:00
ntr 66dcdaeb70 rename tables 2022-08-06 18:18:24 +10:00
ntr b622d2cdbd ships 2022-08-04 21:47:02 +10:00
ntr 413c1f4840 nice bundles markup 2022-08-03 00:05:06 +10:00
ntr 02aadcf2db unistore sucks i think 2022-08-01 18:29:23 +10:00
ntr baeaead650 client init 2022-08-01 00:05:55 +10:00
ntr 8700b15f39 bundle it up 2022-07-28 20:15:27 +10:00
ntr adcc19564f prisma pretty noice 2022-07-28 00:18:11 +10:00
74 changed files with 47478 additions and 3804 deletions

No files matched your search

+14
View File
@@ -0,0 +1,14 @@
# TODO
status stepper
queue up wantlist adds to gracefully handle rate limiting
push offers
cover off admin routes and commands
listen / notify
on offers
notify admins
View File
File renamed without changes.
+8
View File
@@ -0,0 +1,8 @@
{
"extension": ["ts"],
"spec": "src/**/*.test.ts",
"require": "ts-node/register",
"watch-files": "**/*.ts",
"watch-ignore": ["db"]
}
View File
File renamed without changes.
+16
View File
@@ -0,0 +1,16 @@
// global type aliases and whatnot
type UUID = string;
type Email = string;
type DiscogsName = string;
type DiscogsListing = number;
type DiscogsRelease = number;
type DiscogsSeller = number;
type BundleCandidate = {
seller: DiscogsSeller,
count: number,
listings: DiscogsListing[],
}
+37
View File
@@ -0,0 +1,37 @@
import createServices from './src/services';
import createLog from './src/log';
import { Events } from './src/events';
async function start() {
const log = createLog();
// process.on('uncaughtException', (err, origin) => {
// log.error({ err, origin }, 'uncaughtException');
// process.exit(1);
// });
// process.on('unhandledRejection', (reason, promise) => {
// log.error({ reason, promise }, 'unhandledRejection');
// process.exit(1);
// });
process.on('SIGTERM', () => {
log.debug('SIGTERM received, exiting');
process.exit(0);
});
const {
prisma,
pg,
discogs,
api,
events,
} = createServices(log);
events.emit(Events.StartupSync);
// events.emit(Events.ListingsUpdated);
api.listen();
}
start();
+6454
View File
File diff suppressed because it is too large. Load diff
+50
View File
@@ -0,0 +1,50 @@
{
"name": "mates-crates",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha",
"test:watch": "mocha --watch",
"migrate": "knex migrate:latest",
"migrate:make": "prisma migrate dev --name",
"start": "ts-node-dev --respawn --transpile-only index.ts | bunyan -o short"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@types/bcrypt": "^5.0.0",
"@types/bluebird": "^3.5.36",
"@types/koa": "^2.13.5",
"@types/lodash": "^4.14.182",
"@types/mocha": "^9.1.1",
"axios": "^0.27.2",
"mocha": "^10.0.0",
"node-dev": "^7.4.3",
"prisma": "^4.5.0",
"ts-node-dev": "^2.0.0",
"typescript": "^4.7.4"
},
"dependencies": {
"@koa/cors": "^3.3.0",
"@koa/router": "^12.0.0",
"@prisma/client": "^4.5.0",
"bcrypt": "^5.0.1",
"bluebird": "^3.7.2",
"bunyan": "^1.8.15",
"cheerio": "^1.0.0-rc.12",
"koa": "^2.13.4",
"koa-body": "^5.0.0",
"koa-passport": "^5.0.0",
"koa-session": "^6.2.0",
"koa-websocket": "^7.0.0",
"koa-ws": "^1.0.1",
"lodash": "^4.17.21",
"passport-local": "^1.0.0"
},
"overrides": {
"koa-passport": {
"passport": "^0.5"
}
}
}
+89
View File
@@ -0,0 +1,89 @@
generator client {
provider = "prisma-client-js"
// include for node docker image
binaryTargets = ["native", "debian-openssl-1.1.x"]
previewFeatures = ["interactiveTransactions"]
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model Listing {
id Int @id
seller Int
data Json
wantId String @db.Uuid
want Want @relation(fields: [wantId], references: [id], onDelete: Cascade, onUpdate: Cascade)
offers Offer[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@index([seller])
@@map("listings")
}
model User {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
email String @unique
password String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
wants Want[]
offers Offer[]
@@map("users")
}
model Want {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
release Int
data Json
userId String @db.Uuid
user User @relation(fields: [userId], references: [id], onDelete: Cascade, onUpdate: Cascade)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
listings Listing[]
offers Offer[]
@@unique([userId, release])
@@index([release])
@@map("wants")
}
model Offer {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
status String
condition String @default("")
item Decimal @db.Money
// markup Decimal @db.Money
shipping Decimal @db.Money
total Decimal @db.Money
comparison Decimal @db.Money
listingId Int
listing Listing @relation(fields: [listingId], references: [id])
userId String @db.Uuid
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
wantId String @db.Uuid
want Want @relation(fields: [wantId], references: [id], onDelete: Cascade)
// only one offer per want for a user
// @@unique([userId, wantId])
@@unique([listingId])
@@map("offers")
}
+59
View File
@@ -0,0 +1,59 @@
import assert from 'assert/strict';
import axios from 'axios';
import createServices from './services';
import createLog from './log';
const log = createLog();
log.level('debug');
const services = createServices(log);
const api = axios.create({
baseURL: 'http://localhost:41337',
withCredentials: true
});
let server;
before(function setup() {
server = services.api.listen();
});
after(function teardown(done) {
server.close(done)
});
describe('api', function apiTests() {
describe.skip('auth', function authTests() {
it('listens', async function listenTest() {
const res = await api.get('/health');
assert.equal(res.status, 200);
});
it('register', async function register() {
const body = {
email: `test+${Date.now()}@user.com`,
password: 'woaaah theres no limit',
};
const { data } = await api.post('/auth/register', body);
assert.equal(data.email, body.email);
});
it('login', async function login() {
const body = {
email: `test+${Date.now()}@user.com`,
password: 'woaaah theres no limit',
};
await api.post('/auth/register', body);
const res = await api.post('/auth/login', body);
assert(res.headers['set-cookie']);
})
});
describe('offers', function offerTests() {
it('create offers', async function createOffersTest() {
const { data: bundles } = await api.get('/bundles');
const listingIds = bundles[0].items.map(i => i.id);
const { data } = await api.post('/offers', { listingIds, shipping: 50.0, baseShipping: 15.0 });
});
});
});
+146
View File
@@ -0,0 +1,146 @@
import http from 'http';
import koa from 'koa';
import koaWebsocket from 'koa-websocket';
import bodyParser from 'koa-body';
import session from 'koa-session';
import koaRouter from '@koa/router'
import cors from '@koa/cors';
import { groupBy, sortBy } from 'lodash';
import Bluebird from 'bluebird';
import type Log from 'bunyan';
import type { Listing, User, Want, Prisma } from '@prisma/client';
import { Pg } from './pg';
import routerAuth from './routers/router.auth';
import { Events } from './events';
import createRpc, { RpcMessageType } from './rpc';
export default function createApi(log: Log, pg: Pg, discogs, offers, events) {
const PORT = 41337;
const WS_PORT = 40055;
const SECRET = 'ohja4Eif9eidah3aj5ta2haa4iemooth';
const app = koaWebsocket(new koa());
app.use(async (ctx, next) => {
try {
await next();
} catch (err) {
log.error(err);
ctx.status = err.statusCode || err.status || 500;
ctx.body = {
error: err.message
};
}
});
const router = koaRouter();
function listen() {
const server = app.listen(PORT);
log.info({ PORT }, 'listening');
}
app.use(cors({ credentials: true }));
app.use(bodyParser())
app.keys = [SECRET];
app.use(session(app));
const { authRouter, passport } = routerAuth(log, pg);
app.use(passport.initialize());
app.use(passport.session())
app.use(authRouter.routes());
router.get('/health', async function health(ctx) {
ctx.status = 200;
});
router.get('/user/offers', async function userOffersGet(ctx, next) {
ctx.assert(ctx.isAuthenticated(), 401, 'log in required');
const user = ctx.state.user;
const offers = await pg.userOffers(user);
ctx.response.body = offers;
});
router.post('/want', async function wantPost(ctx, next) {
ctx.assert(ctx.isAuthenticated(), 401, 'log in required');
const { releaseId } = ctx.request.body;
const user = await pg.userFindId(ctx.state.user.id);
const release = await discogs.release(releaseId);
const want = await pg.wantCreate(release, user);
events.emit(Events.WantsAdded, [want]);
ctx.response.body = [want];
});
router.post('/want/discogs', async function wantListPost(ctx, next) {
ctx.assert(ctx.isAuthenticated(), 401, 'log in required');
const { username } = ctx.request.body;
const user = await pg.userFindId(ctx.state.user.id);
const wants = await discogs.wantListSync(username, user);
events.emit(Events.WantsAdded, [wants]);
ctx.response.body = wants;
});
router.get('/want/all', async function wantGet(ctx, next) {
ctx.assert(ctx.isAuthenticated(), 401, 'log in required');
const user = ctx.state.user;
const wants = await pg.wantList(user);
ctx.response.body = wants;
});
router.post('/want/search', async function wantPost(ctx, next) {
const { artist, title } = ctx.request.body;
const results = await discogs.search(artist, title);
ctx.response.body = results;
});
router.post('/auth/logout', async (ctx, next) => {
ctx.assert(ctx.isAuthenticated(), 401, 'log in required');
ctx.logout();
ctx.status = 200;
ctx.body = { success: true };
});
app.use(router.routes());
app.ws.use(passport.initialize());
app.ws.use(passport.session());
app.ws.use(async (ctx, next) => {
log.info('new ws connection', { user: ctx.state.user });
// log.child...
const ws = ctx.websocket;
const rpc = createRpc(log, pg, ctx.state.user, ws, discogs, offers);
if (ctx.state.user) events.emit(Events.Connection, { ws, id: ctx.state.user.id });
ws.on('message', async function incoming(message) {
try {
const parsed = JSON.parse(message.toString());
const response = await rpc.receive(parsed);
rpc.send(response);
} catch (e) {
log.warn(e);
log.warn(message.toString());
rpc.send([RpcMessageType.Error, e.message || e.msg]);
}
return true;
});
ws.on('close', function onClose() {
log.info('ws disconnected', { user: ctx.state.user });
});
await rpc.onConnection();
});
return {
app,
listen,
};
}
+115
View File
@@ -0,0 +1,115 @@
import fs from 'fs/promises';
import axios from 'axios';
import cheerio from 'cheerio';
import Bluebird from 'bluebird';
import type Log from 'bunyan';
import type { PrismaClient, Want, User, Listing } from '@prisma/client';
import { logAxiosError, wantDetails } from './utils';
import { Pg } from './pg';
// margh
// vanderbabe
// jmcdermid1
export default function createDiscogs(log: Log, pg: Pg) {
const TOKEN = 'Discogs token=wRTXuYZGOPsIQgKRGKCAkUkidGtvpSfQgPWkYIpD';
const apiBase = 'https://api.discogs.com'
const discogsBase = 'https://www.discogs.com'
const api = axios.create({
headers: {
'Authorization': TOKEN,
// discogs only returns the first page without a desktop u/a
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0',
},
});
async function handleApiError(e) {
logAxiosError(e);
if (e.response.status === 429) {
throw new Error('rate limited');
}
return null;
}
async function wantListSync(username: string, user: User): Promise<Want[]> {
const { data: { wants, pagination }} = await api.get(`${apiBase}/users/${username}/wants`);
const releases = await Promise.all(wants.map(want => release(want.id)));
log.info({ wants: wants.length, user: username, userId: user.id }, 'fetched discogs wantlist');
// TODO filter releases to be vinyl format only
// convert want object to actual release and ignore duplicate errors
const results = await Promise.allSettled(releases.map(release => pg.wantCreate(release, user)));
// @ts-ignore
const additions = results.filter(r => r.status === 'fulfilled').map(r => r.value);
log.info(additions);
return additions;
}
async function release(id: DiscogsRelease): Promise<any> {
const { data } = await api.get(`${apiBase}/releases/${id}`);
return data;
}
async function search(artist: string, title: string) {
const url = new URL(`${apiBase}/database/search`);
url.searchParams.append('artist', artist);
url.searchParams.append('title', title);
url.searchParams.append('type', 'release');
url.searchParams.append('format', 'vinyl');
const { data: { results } } = await api.get(url.href);
return results;
}
async function inventory(user: string) {
const res = await api.get(`${apiBase}/users/${user}/inventory`);
return res.data.listings;
}
async function listings(want: Want) {
const res = await api.get(`https://www.discogs.com/sell/release/${want.release}?limit=250`);
const $ = cheerio.load(res.data);
const ids = $('.item_description_title[href]')
.map(function getListingId() {
const id = Number.parseInt(this.attribs.href.replace('/sell/item/', ''), 10);
return id;
})
.get();
const unlisted = await pg.listingRmUnlisted(want, ids);
if (unlisted) log.info({ unlisted: unlisted, ...wantDetails(want) }, 'missing listings removed');
// preferentially take listings from sellers if we hit req limits
return Bluebird.mapSeries(ids, async id => listing(id, want));
}
async function listing(id, want) {
const listing = await pg.listingFind(id);
if (listing) return listing;
try {
log.debug('fetching listing', id);
const res = await api.get(`${apiBase}/marketplace/listings/${id}`);
log.debug(res.data);
return pg.listingUpsert(res.data, want);
} catch (e) {
return handleApiError(e);
}
}
return {
wantListSync,
inventory,
listings,
listing,
release,
search,
};
}
+82
View File
@@ -0,0 +1,82 @@
import { EventEmitter } from 'node:events';
import { setTimeout } from 'timers/promises';
import type Log from 'bunyan';
import type { Want } from '@prisma/client';
import { Pg } from './pg';
import { wantDetails } from './utils';
import routerAuth from './routers/router.auth';
export enum Events {
WantsAdded = 'WantsAdded',
StartupSync = 'StartupSync',
ListingsUpdated = 'ListingsUpdated',
Connection = 'Connection',
}
export default function createEvents(log: Log, pg: Pg, discogs, offers) {
const events = new EventEmitter({ captureRejections: true });
const connections = new Map();
async function populateListings(want: Want) {
try {
const listings = await discogs.listings(want);
return listings;
} catch (e) {
if (e.message === 'rate limited') {
log.warn('rate limited, trying again in 60s');
await setTimeout(60 * 1000);
return populateListings(want);
}
throw e;
}
}
events.on('error', function eventError(e) {
log.error('event error', e);
});
async function updateWantListings(wants: Want[]) {
for (const want of wants) {
const listings = await populateListings(want);
log.info({ listings: listings.length, ...wantDetails(want) }, 'listings added');
events.emit(Events.ListingsUpdated);
}
log.info({ wants: wants.length }, 'finished syncing');
}
events.on(Events.WantsAdded, async function wantsAdded(wants: Want[]) {
log.info({ event: Events.WantsAdded, wants: wants.map(w => wantDetails(w)) }, 'starting listings population');
await updateWantListings(wants);
log.info({ event: Events.WantsAdded, wants: wants.map(w => wantDetails(w)) }, 'finished populating listings');
});
events.on(Events.StartupSync, async function startupSync() {
const wants = await pg.wantsToSync();
log.info({ event: Events.StartupSync, wants: wants.length });
await updateWantListings(wants);
events.emit(Events.ListingsUpdated);
});
events.on(Events.ListingsUpdated, async function listingsUpdated() {
const bundles = await offers.bundles();
log.info({ event: Events.ListingsUpdated, bundles: bundles.length });
for (const bundle of bundles) {
log.info({ seller: bundle.seller, items: bundle.items.length }, 'generating offers...');
const listingIds = bundle.items.map(i => i.id);
const updatedOffers = await offers.generate(listingIds, 100, 30)
log.info({ offers: updatedOffers.length }, 'offers set');
}
log.info('offers up to date');
});
events.on(Events.Connection, async function onConnection({ ws, id }) {
connections.set(id, ws);
})
return events;
}
+10
View File
@@ -0,0 +1,10 @@
import bunyan from 'bunyan';
export default function createLog() {
const log = bunyan.createLogger({
name: 'mcrates',
stream: process.stdout
});
return log;
}
+163
View File
@@ -0,0 +1,163 @@
import type Log from 'bunyan';
import type { PrismaClient, Want, User, Listing, Offer, } from '@prisma/client';
import assert from 'assert/strict';
import groupBy from 'lodash/groupBy';
import sortBy from 'lodash/sortBy';
import { Context } from 'koa';
import { logAxiosError, wantDetails } from './utils';
import { Pg } from './pg';
export enum Status {
Complete = 'Complete',
Confirmed = 'Confirmed',
Accepted = 'Accepted',
Pending = 'Pending',
Declined = 'Declined',
}
function sortOffers(o) {
return Object.values(Status).indexOf(o.status);
}
export default function createOffers(log: Log, pg: Pg) {
async function generate(listingIds: DiscogsListing[], bulkShipping: number, baseShipping: number) {
log.info('generateOffers', { listingIds, bulkShipping, baseShipping });
const listings = await pg.listingByIds(listingIds);
// log.info({ listings }, 'creating offers for listings')
const shipping = 7.5; // yolt
// const shipping = bulkShipping / listings.length;
// how to deal with users who don't want all items in a bundle...
const offers = listings.map(l => ({
userId: l.want.user.id,
listingId: l.id,
wantId: l.want.id,
// sellerId: l.seller,
status: Status.Pending,
condition: l.data.condition,
item: l.data.price.value,
shipping,
total: l.data.price.value + shipping,
// without mcrates...
comparison: l.data.price.value + baseShipping,
}))
// we must sort the offers by price,
// sometimes a seller has multiples of a want
// to deal with this we just ignore duplicates on insert
// and ensure the cheapest one goes in first
const sorted = sortBy(offers, ['item']);
const inserts = await pg.offersCreate(sorted);
return inserts;
}
async function accept(ctx: Context) {
const { id } = ctx.request.body;
ctx.assert(id, 401, 'no id supplied');
// check the user actually has this offer
const offer = await pg.userOfferFind(ctx.state.user, id);
return pg.offerStatusSet(offer.id, Status.Accepted);
}
async function statusSet(user: User, id: UUID, status: Status) {
log.info({ status, id }, 'setting offer status');
const offer = await pg.offerFindId(id);
const update = await pg.offerStatusSet(offer.id, status);
if (status === Status.Complete) {
log.info({ id, status }, 'completed offer, removing want...');
await pg.wantRm(offer.wantId);
}
return update;
}
async function bundles() {
const bundles = await pg.bundleCandidates();
interface ListingData extends Listing {
data: any,
want: { user: User },
};
return bundles.map((b: ListingData[]) => {
const total = b.reduce((acc, l) => acc + l.data.price.value, 0);
const items = b.map(l => ({
id: l.data.id,
description: l.data.release.description,
originalPrice: l.data.original_price.value,
originalCurrency: l.data.original_price.curr_abbr,
price: l.data.price.value,
currency: l.data.price.currency,
condition: l.data.condition,
uri: l.data.uri,
user: l.want.user,
}));
const shipping = b[0]?.data.seller.shipping;
const bundle = {
username: b[0]?.data.seller.username,
seller: b[0]?.data.seller.id,
shipsFrom: b[0]?.data.ships_from,
total,
currency: b[0]?.data.price.currency,
shipping: '...',
items,
mentionsAustralia: shipping.toLowerCase().includes('australia'),
mentionsWorldwide: shipping.toLowerCase().includes('world'),
}
return bundle;
});
}
async function acpOffers() {
const offers = await pg.offersAdmin();
const grouped = groupBy(offers, o => o.listing.data.seller.id);
const sellerOffers = sortBy(Object.values(grouped).map(offers => {
const shipping = offers[0]?.listing.data.seller.shipping;
const bundle = {
username: offers[0]?.listing.data.seller.username,
seller: offers[0]?.listing.data.seller.id,
shipsFrom: offers[0]?.listing.data.ships_from,
shipping,
offers: sortBy(offers, sortOffers),
mentionsAustralia: shipping.toLowerCase().includes('australia'),
mentionsWorldwide: shipping.toLowerCase().includes('world'),
}
return bundle;
}), bundle => -bundle.offers.length);
return sellerOffers;
}
async function userOffers(user: User) {
const offers = await pg.userOffers(user);
return groupBy(offers, (o) => o.wantId);
}
return {
generate,
accept,
bundles,
statusSet,
acpOffers,
userOffers,
};
}
+326
View File
@@ -0,0 +1,326 @@
import bcrypt from 'bcrypt';
import type Log from 'bunyan';
import type { PrismaClient, Want, User, Listing, Offer, Prisma } from '@prisma/client';
import * as Bluebird from 'bluebird';
import _ from 'lodash';
import { Status } from './offers';
export interface UserCreateParams {
email: string,
password: string,
}
export interface Pg {
userFindId: (id: UUID) => Promise<User>,
userFindEmail: (email: Email) => Promise<User>,
userCreate: (params: UserCreateParams) => Promise<User>,
userWants: (user: User) => Promise<Want[]>,
userOffers: (user: User) => Promise<Offer[]>,
userOfferFind: (user: User, id: UUID) => Promise<Offer>,
listingUpsert: (listing: Listing, want: Want) => Promise<Listing>,
listingFind: (id: DiscogsListing) => Promise<Listing>,
listingByIds: (ids: DiscogsListing[]) => Promise<Listing[]>,
listingRmUnlisted: (want: Want, ids: DiscogsListing[]) => Promise<number>,
wantCreate: (discogsWant: any, user: User) => Promise<Want>,
wantList: (user: User) => Promise<Want[]>,
wantsToSync: () => Promise<Want[]>,
wantRm: (id: UUID) => Promise<void>,
bundleCandidates: () => Promise<Listing[][]>,
offersCreate: (offers: Offer[]) => Promise<Offer[]>,
offerStatusSet: (id: UUID, status: Status) => Promise<Offer>,
offersAdmin: () => Promise<Offer[]>,
offerFindId: (id: UUID) => Promise<Offer>,
}
export default function createPg(log: Log, prisma: PrismaClient): Pg {
async function userFindId(id: UUID) {
return prisma.user.findUnique({ where: { id }});
}
async function userFindEmail(email: Email) {
return prisma.user.findUnique({ where: { email }});
}
async function userWants(user: User) {
return prisma.want.findMany({ where: { userId: user.id }});
}
async function userOffers(user: User) {
return prisma.offer.findMany({
where: {
userId: user.id,
status: { not: Status.Declined }
},
include: {
want: true,
},
orderBy: {
total: 'asc',
},
});
}
async function offerFindId(id: UUID) {
return prisma.offer.findFirstOrThrow({
where: { id },
include: {
want: true,
}
});
}
async function userOfferFind(user: User, id: UUID) {
return prisma.offer.findFirstOrThrow({
where: {
userId: user.id,
id,
},
include: {
want: true,
}
});
}
async function userCreate(params: UserCreateParams) {
const hashRounds = 10;
const password = await bcrypt.hash(params.password, hashRounds);
const data = {
email: params.email,
password,
};
return prisma.user.create({
data,
});
}
// userUpdate({ email: 'jen@vdk.com', password: 'jen' });
// userUpdate({ email: 'james@mcd', password: 'james' });
async function userUpdate(params: UserCreateParams) {
const hashRounds = 10;
const password = await bcrypt.hash(params.password, hashRounds);
const data = {
email: params.email,
password,
};
return prisma.user.update({
where: { email: params.email },
data,
});
}
async function wantList(user: User) {
return prisma.want.findMany({
where: { userId: user.id },
});
}
async function wantsToSync() {
return prisma.want.findMany({});
}
async function wantRm(id: UUID) {
await prisma.want.delete({
where: { id },
});
}
async function wantCreate(discogsRelease, user) {
const data: { userId: UUID, release: DiscogsListing, data: any } = {
data: discogsRelease,
release: discogsRelease.id,
userId: user.id,
};
return prisma.want.create({
data,
});
}
async function listingRmUnlisted(want: Want, currentListingIds: DiscogsListing[]) {
return prisma.$transaction(async (prisma) => {
log.debug({ currentListingIds });
const protectedOfferStatuses = [Status.Confirmed, Status.Complete];
// can't delete a listing with an open offer
// so delete those offers first
const { count: offersRemoved } = await prisma.offer.deleteMany({
where: {
wantId: want.id,
listingId: {
notIn: currentListingIds,
},
status: {
notIn: protectedOfferStatuses,
},
}
});
if (offersRemoved > 0) log.info({ offersRemoved }, 'open offers removed');
// now we have to filter listingIds with confirmed offers
// and get their listingIds to filter out
const confirmedListingIds = (await prisma.offer.findMany({
where: {
wantId: want.id,
status: {
in: protectedOfferStatuses,
}
},
select: {
listingId: true,
}
})).map(r => r.listingId);
const removalIds = _.without(currentListingIds, ...confirmedListingIds);
// finally we delete listings that aren't related
// to a historical offer
const { count } = await prisma.listing.deleteMany({
where: {
wantId: want.id,
id: {
notIn: currentListingIds,
}
}
});
return count;
});
}
async function listingUpsert(listing, want) {
const values = {
id: listing.id,
wantId: want.id,
seller: listing.seller.id,
data: listing,
};
return prisma.listing.upsert({
where: {
id: listing.id
},
update: values,
create: values,
});
}
async function listingFind(id: DiscogsListing) {
return prisma.listing.findUnique({
where: {
id,
},
include: {
want: {
include: { user: true },
}
}
});
}
async function listingByIds(ids: DiscogsListing[]) {
return prisma.listing.findMany({
where: {
id: { in: ids },
},
include: {
want: {
include: { user: true },
}
}
});
}
async function bundleCandidates(): Promise<Listing[][]> {
const candidates = await prisma.$queryRaw`
select
seller, count(distinct("wantId")), array_agg(id) as listings
from
listings
where
seller not in (3709203)
group by
seller
order by
count desc
limit 5;
` as BundleCandidate[];
return Promise.all(candidates.map(async c => prisma.listing.findMany({
where: {
id: { in: c.listings }
},
include: {
want: {
include: { user: true },
}
},
distinct: ['wantId'],
})));
}
async function offersCreate(offers: Offer[]) {
return await prisma.$transaction(
offers.map(offer => prisma.offer.upsert({
where: {
listingId: offer.listingId,
},
create: offer,
update: _.omit(offer, ['status']),
}))
);
}
async function offerStatusSet(id: UUID, status: Status) {
return prisma.offer.update({
where: { id: id },
data: { status },
});
}
async function offersAdmin() {
return prisma.offer.findMany({
include: {
listing: true,
user: true,
want: true,
},
});
}
return {
userFindId,
userFindEmail,
userCreate,
userWants,
userOffers,
userOfferFind,
wantCreate,
wantList,
wantsToSync,
wantRm,
listingUpsert,
listingFind,
listingRmUnlisted,
listingByIds,
bundleCandidates,
offersCreate,
offerStatusSet,
offersAdmin,
offerFindId,
};
}
+75
View File
@@ -0,0 +1,75 @@
import bcrypt from 'bcrypt';
import { KoaPassport } from 'koa-passport';
import { Strategy as LocalStrategy } from 'passport-local';
import Router from '@koa/router'
import type { Pg } from './../pg';
export default function authRouter(log, pg: Pg) {
const passport = new KoaPassport();
async function verifyPassword(user, password) {
const match = await bcrypt.compare(password, user.password);
return match;
}
passport.use(new LocalStrategy(
{ usernameField: 'email' },
function localStrategy(email, password, done) {
pg.userFindEmail(email)
.then(async user => {
if (!user) return done(null, false);
const passwordMatch = await verifyPassword(user, password);
if (!passwordMatch) return done(null, false);
return done(null, user);
})
.catch(e => done(e, null));
}
));
passport.serializeUser((user, done) => done(null, user.id));
passport.deserializeUser((id, done) => {
pg.userFindId(id)
.then(u => done(null, u))
.catch(e => done(e, null));
})
const authRouter = new Router({ prefix: '/auth' });
authRouter.post('/login', async (ctx, next) => {
await passport.authenticate('local', (err, user, info, status) => {
ctx.assert(user, 401, 'incorrect login');
ctx.login(user);
ctx.status = 200;
ctx.body = {
email: user.email,
};
})(ctx, next);
});
authRouter.get('/login', async (ctx, next) => {
ctx.assert(ctx.isAuthenticated(), 401, 'log in required');
const user = ctx.state.user;
ctx.status = 200;
ctx.body = {
id: user.id,
email: user.email,
};
});
authRouter.post('/register', async (ctx, next) => {
const { discogs, email, password } = ctx.request.body;
// fetch discogs data if they have it
const params = { discogs, email, password, data: {} };
const user = await pg.userCreate(params)
ctx.login(user);
ctx.status = 200;
ctx.body = {
id: user.id,
email: user.email,
};
});
return { authRouter, passport };
}
+196
View File
@@ -0,0 +1,196 @@
import { EventEmitter } from 'node:events';
import { setTimeout } from 'timers/promises';
import assert from 'assert/strict';
import type Log from 'bunyan';
import type { Want, Offer, User } from '@prisma/client';
import { Pg } from './pg';
import { Status } from './offers';
import { isAdmin } from './utils';
function checkMsgType(msgType: RpcMessageType): msgType is RpcMessageType {
if (RpcMessageType[msgType]) {
return true;
}
return false;
}
// rpc is created per connection from the api
export default function createRpc(log: Log, pg: Pg, user: User, ws, discogs, offers) {
async function ping(): Promise<PongMessage> {
return [RpcMessageType.Pong, []];
}
async function userOffers(): Promise<OffersMessage> {
return [RpcMessageType.Offers, await offers.userOffers(user)];
}
async function userWants(): Promise<WantsMessage> {
return [RpcMessageType.Wants, await pg.userWants(user)];
}
async function offerStatusSet(params: OfferStatusSetParams): Promise<OffersMessage> {
const { id, status } = params;
assert(id, 'no id supplied');
assert(Status[status], 'unknown status');
const offer = await offers.statusSet(user, id, status);
const userOffers = await offers.userOffers(user);
return [RpcMessageType.Offers, userOffers]
}
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 acpOfferStatusSet(params: OfferStatusSetParams): Promise<AcpOffersMessage> {
const { id, status } = params;
assert(isAdmin(user), 'not authorised');
assert(id, 'no id supplied');
assert(Status[status], 'unknown status');
const offer = await offers.statusSet(user, id, status);
return [RpcMessageType.AcpOffers, await offers.acpOffers()]
}
async function acpOffers(): Promise<AcpOffersMessage> {
assert(isAdmin(user), 'not authorised');
return [RpcMessageType.AcpOffers, await offers.acpOffers()];
}
async function receive(message: any): Promise<RpcMessage> {
const [msgType, params] = message;
if (!user) {
log.debug('anon rpc call');
return [RpcMessageType.WhoDis, []];
}
if (msgType !== RpcMessageType.Ping) log.info({ msgType, params }, 'rpc message');
// // this sucks but can't figure out how to convince typescript to agree yet
if (checkMsgType(msgType)) {
switch (msgType) {
case RpcMessageType.Ping:
return ping();
case RpcMessageType.Offers:
return userOffers();
case RpcMessageType.Wants:
return userWants();
case RpcMessageType.OfferStatusSet:
return offerStatusSet(params);
case RpcMessageType.WantRm:
return wantRm(params);
case RpcMessageType.AcpOfferStatusSet:
return acpOfferStatusSet(params);
case RpcMessageType.AcpOffers:
return acpOffers();
default:
throw new Error(`Message type ${msgType} is known but unhandled`);
}
} else {
throw new Error(`Unknown message type ${msgType}`);
}
throw new Error(`Invalid parameters for message type ${msgType}: ${JSON.stringify(message)}`);
}
async function onConnection() {
const userMsg: UserMessage = [RpcMessageType.User, user];
send(userMsg);
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'
? value.toString()
: value // return everything else unchanged
))
}
return {
receive,
send,
onConnection,
}
}
export enum RpcMessageType {
Ping = 'Ping',
Pong = 'Pong',
// points out user is anon
WhoDis = 'WhoDis',
Bundles = 'Bundles',
Offers = 'Offers',
List = 'List',
Wants = 'Wants',
User = 'User',
Error = 'Error',
OfferStatusSet = 'OfferStatusSet',
AcpOfferStatusSet = 'AcpOfferStatusSet',
AcpOffers = 'AcpOffers',
WantRm = 'WantRm',
}
type ErrorMessage = [RpcMessageType.Error, string];
type PongMessage = [RpcMessageType.Pong, []];
type PingMessage = [RpcMessageType.Ping, []];
type WhoDisMessage = [RpcMessageType.WhoDis, []];
type UserMessage = [RpcMessageType.User, User];
type WantsMessage = [RpcMessageType.Wants, Want[]];
type OffersMessage = [RpcMessageType.Offers, Offer[]];
type OfferStatusSetMessage = [RpcMessageType.OfferStatusSet, Offer[]];
type AcpOfferStatusSetMessage = [RpcMessageType.AcpOfferStatusSet, Offer[]];
type AcpOffersMessage = [RpcMessageType.AcpOffers, Offer[]];
export type RpcMessage =
PingMessage |
PongMessage |
WhoDisMessage |
UserMessage |
WantsMessage |
OffersMessage |
OfferStatusSetMessage |
AcpOfferStatusSetMessage |
AcpOffersMessage |
ErrorMessage;
type OfferStatusSetParams = {
id: UUID,
status: Status,
};
type WantRmParams = {
id: UUID,
};
+25
View File
@@ -0,0 +1,25 @@
import { PrismaClient } from '@prisma/client'
import createDiscogs from './discogs';
import createOffers from './offers';
import createLog from './log';
import createApi from './api';
import createEvents from './events';
import createPg from './pg';
export default function creates(log) {
const prisma = new PrismaClient();
const pg = createPg(log, prisma);
const discogs = createDiscogs(log, pg);
const offers = createOffers(log, pg);
const events = createEvents(log, pg, discogs, offers);
const api = createApi(log, pg, discogs, offers, events);
return {
api,
discogs,
events,
pg,
prisma,
};
}
+19
View File
@@ -1,3 +1,5 @@
import type { Want, User } from '@prisma/client';
export function logAxiosError(e) { export function logAxiosError(e) {
console.warn({ console.warn({
status: e.response.status, status: e.response.status,
@@ -11,3 +13,20 @@ export function logAxiosError(e) {
}, },
}); });
} }
export function wantDetails(want: Want) {
const data = want.data as any;
return {
title: data.title,
release: data.id,
artist: data.artists_sort,
id: want.id
}
}
export function isAdmin(user: User) {
return user.email === 'ntr@mnml.gg';
}
export function offerDetails() {}
+11 -3
View File
@@ -27,12 +27,15 @@
/* Modules */ /* Modules */
"module": "commonjs", /* Specify what module code is generated. */ "module": "commonjs", /* Specify what module code is generated. */
// "rootDir": "./", /* Specify the root folder within your source files. */ // "rootDir": "./", /* Specify the root folder within your source files. */
// "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */ "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */ // "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
"types": ["./index.d.ts"], /* Specify type package names to be included without being referenced in a source file. */ "types": [
"./index.d.ts",
"mocha"
], /* Specify type package names to be included without being referenced in a source file. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */ // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
// "resolveJsonModule": true, /* Enable importing .json files. */ // "resolveJsonModule": true, /* Enable importing .json files. */
@@ -99,5 +102,10 @@
/* Completeness */ /* Completeness */
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */ "skipLibCheck": true /* Skip type checking all .d.ts files. */
} },
"ts-node": {
// strict mode gtfo
"transpileOnly": true,
},
} }
+4
View File
@@ -0,0 +1,4 @@
module.exports = {
parserOpts: { strictMode: false },
plugins: ['@babel/plugin-transform-react-jsx-source']
};
+31
View File
@@ -0,0 +1,31 @@
module.exports = {
parser: "@typescript-eslint/parser",
extends: [
"preact",
"plugin:@typescript-eslint/recommended"
],
ignorePatterns: [
"build/"
],
rules: {
// prevents stupid complaints a la
// (req) {
// req.something = x;
// }
'no-param-reassign': [2, { props: false }],
'no-multi-spaces': [0],
'max-len': ['error', 120],
'import/no-extraneous-dependencies': [0],
'prefer-arrow-callback': [0],
'arrow-body-style': [0],
'no-return-assign': 0,
'no-console': [0],
// i like loops
'no-plusplus': [0],
'no-await-in-loop': [0],
indent: ['error', 4],
'keyword-spacing': ['error'],
'key-spacing': ['error'],
},
};
+5
View File
@@ -0,0 +1,5 @@
node_modules
/build
/*.log
size-plugin.json
+19
View File
@@ -0,0 +1,19 @@
# mcrates
## CLI Commands
* `npm install`: Installs dependencies
* `npm run dev`: Run a development, HMR server
* `npm run serve`: Run a production-like server
* `npm run build`: Production-ready build
* `npm run lint`: Pass TypeScript files using ESLint
* `npm run test`: Run Jest and Enzyme with
[`enzyme-adapter-preact-pure`](https://github.com/preactjs/enzyme-adapter-preact-pure) for
your tests
For detailed explanation on how things work, checkout the [CLI Readme](https://github.com/developit/preact-cli/blob/master/README.md).
+2
View File
@@ -0,0 +1,2 @@
This application uses Discogs’ API but is not affiliated with, sponsored or endorsed by Discogs. ‘Discogs’ is a trademark of Zink Media, LLC.
Data provided by Discogs.
+37701
View File
File diff suppressed because it is too large. Load diff
+35
View File
@@ -0,0 +1,35 @@
{
"private": true,
"name": "mcrates-client",
"version": "0.1.0",
"license": "MIT",
"scripts": {
"build": "preact build --no-prerender",
"start": "preact --no-clear watch",
"lint": "eslint src"
},
"dependencies": {
"@reduxjs/toolkit": "^1.8.5",
"lodash": "^4.17.21",
"logrocket": "^3.0.1",
"preact": "^10.10.0",
"preact-render-to-string": "^5.2.1",
"preact-router": "^3.2.1",
"react-feather": "^2.0.10",
"react-redux": "^8.0.2",
"redux": "^4.2.0"
},
"devDependencies": {
"@babel/plugin-transform-react-jsx-source": "^7.18.6",
"@typescript-eslint/eslint-plugin": "^5.30.6",
"@typescript-eslint/parser": "^5.30.6",
"eslint": "^8.20.0",
"eslint-config-preact": "^1.3.0",
"eslint-plugin-jest": "^26.7.0",
"jest": "^28.1.3",
"less": "^4.1.3",
"less-loader": "^7.3.0",
"preact-cli": "^3.4.1",
"typescript": "^4.5.2"
}
}
+24
View File
@@ -0,0 +1,24 @@
import { route } from 'preact-router';
import http from './http';
const actions = store => ({
setWs: function setWs(state, ws) {
return { ws };
},
setUser: function setUser(state, user) {
return { user };
},
setOffers: function setOffers(state, offers) {
return { offers };
},
logout: async function logout() {
await http.postLogout();
route('/login');
return { user: null };
},
});
export default actions;
+64
View File
@@ -0,0 +1,64 @@
import { h } from 'preact';
import { useEffect } from 'preact/hooks';
import { useDispatch, Provider } from 'react-redux'
import { Route, Router, } from 'preact-router';
import LogRocket from 'logrocket';
import createWs from './ws';
import store, { wsState } from './store'
import Nav from './components/nav';
import Offers from './routes/offers';
import Acp from './routes/acp';
import Login from './routes/login';
import Search from './routes/search';
import List from './routes/list';
import Info from './routes/info';
if (window.location.host === 'matescrates.au') LogRocket.init('yh0dy3/mcrates');
const Main = () => {
const dispatch = useDispatch()
// i have no idea why this is needed,
// but it makes sure the user changes actually rerender
useEffect(() => {
const ws = createWs(dispatch);
ws.connect();
dispatch(wsState.actions.set(ws));
}, [dispatch])
function routeChange(e) {
const { ws } = store.getState();
if (!ws) return null;
if (e.url === '/acp') return ws.sendAcpOffers();
}
return (
<main id="app">
<Nav />
<Router onChange={routeChange}>
<Route path="/" component={Offers} />
<Route path="/login" component={Login} mode="login" />
<Route path="/register" component={Login} mode="register" />
<Route path="/search" component={Search} />
<Route path="/list" component={List} />
<Route path="/info" component={Info} />
{/* ACP */}
<Route path="/acp" component={Acp} />
</Router>
</main>
);
};
const App = () => {
return (
<Provider store={store}>
<Main />
</Provider>
);
}
export default App;
Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 626 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.8 KiB

+79
View File
@@ -0,0 +1,79 @@
import { h } from 'preact';
import { route } from 'preact-router';
import { useEffect } from 'preact/hooks';
import { useSelector } from 'react-redux'
import LogRocket from 'logrocket';
import http from './../http';
import { State } from './../store';
function showAdmin(user) {
return user && user.email === 'ntr@mnml.gg';
}
const DEFAULT_ROUTE = '/';
const Nav = () => {
const user = useSelector((state: State) => state.user);
useEffect(() => {
if (!user) {
route('/login');
return;
}
route(DEFAULT_ROUTE)
if (window.location.host === 'matescrates.au') {
LogRocket.identify(user.id, {
name: user.email,
email: user.email,
});
}
return;
}, [user])
async function logoutClick(e) {
e.preventDefault();
await http.postLogout();
window.location.reload();
}
const nav = () => (
<nav>
<a href="/">Offers</a>
<a href='/list'>List</a>
<a href="/search">Search</a>
</nav>
);
const anonNav = () => (
<nav>
<a href="/login">Login</a>
<a href="/register">Register</a>
</nav>
);
const adminNav = () => (
<nav>
<a href='/acp'>Offers ACP</a>
</nav>
)
const userNav = () => (
<nav>
<a href="/info">Info</a>
<a href="/auth/logout" onClick={logoutClick}>Logout</a>
</nav>
)
return (
<header>
{user ? nav() : anonNav()}
{showAdmin(user) ? adminNav() : <div></div>}
{user ? userNav() : null}
</header>
);
}
export default Nav;
+43
View File
@@ -0,0 +1,43 @@
import { h } from 'preact';
import { ExternalLink, Check } from 'react-feather';
import { useState } from 'preact/hooks';
import { useSelector, useDispatch } from 'react-redux'
import { State } from './../store';
import http from '../http';
export default function Offer(offer) {
const ws = useSelector((state: State) => state.ws);
const { want: { data: details } } = offer;
function setStatus(status) {
ws.sendOfferStatusSet(offer.id, status);
}
const accepted = offer.status === 'Accepted';
const classes = `${accepted ? 'accepted' : ''}`;
const btnText = accepted
? offer.status
: 'Accept';
const negBtn = accepted
? <button onClick={() => setStatus('Pending')} class='neg'>Cancel</button>
: <button onClick={() => setStatus('Declined')} class='neg'>Decline</button>;
return (
<div class="offer">
<section>
<h3>{details.artists_sort} - {details.title} <a href={details.uri} target="_blank"><ExternalLink /></a></h3>
<div class='alt-text' >{details.year}</div>
<div class='alt-text'>{details.formats[0].descriptions.join(' ')}</div>
<div class='number'>mcrates price: ${offer.total}</div>
<div class='number'>shipped by itself: ${offer.comparison}</div>
</section>
<img src={details.thumb} />
<button onClick={() => setStatus('Accepted')} disabled={accepted} class={classes} >{btnText}</button>
{negBtn}
</div>
)
}
+75
View File
@@ -0,0 +1,75 @@
import { h } from 'preact';
import { ExternalLink, Check } from 'react-feather';
import { useState } from 'preact/hooks';
import { useSelector, useDispatch } from 'react-redux'
import { State } from './../store';
import http from '../http';
function OfferLine(offer) {
const ws = useSelector((state: State) => state.ws);
function setStatus(status) {
ws.sendOfferStatusSet(offer.id, status);
}
const accepted = offer.status === 'Accepted';
const classes = `${accepted ? 'accepted' : ''}`;
const btnText = accepted
? offer.status
: 'Accept';
const negBtn = accepted
? <button onClick={() => setStatus('Pending')} class='neg'>Cancel</button>
: <button onClick={() => setStatus('Declined')} class='neg'>Decline</button>;
return (
<div class="line">
<span class='number'>${offer.total}</span>
<button onClick={() => setStatus('Accepted')} disabled={accepted} class={classes} >{btnText}</button>
{negBtn}
</div>
)
}
function OfferPriceBtn(offer) {
const ws = useSelector((state: State) => state.ws);
const accepted = offer.status === 'Accepted';
const confirmed = offer.status === 'Confirmed';
function setStatus() {
if (accepted) return ws.sendOfferStatusSet(offer.id, 'Pending')
ws.sendOfferStatusSet(offer.id, 'Accepted');
}
const classes = `${accepted ? 'accepted' : ''} ${confirmed ? 'confirmed' : ''}`;
const btnText = accepted
? offer.status
: 'Accept';
return (
<button disabled={confirmed} onClick={() => setStatus()} class={classes}>${offer.total} {offer.condition}</button>
)
}
export default function WantOffers(wantOffers) {
const ws = useSelector((state: State) => state.ws);
const details = wantOffers[0].want.data;
return (
<div class="offer">
<section>
<h3>{details.artists_sort} - {details.title} <a href={details.uri} target="_blank"><ExternalLink /></a></h3>
<div class='alt-text' >{details.year}</div>
<div class='alt-text'>{details.formats.map(f => f.descriptions.join(' ')).join(' ')}</div>
</section>
<img src={details.thumb} />
<div class="lines">
{wantOffers.map(o => OfferPriceBtn(o))}
</div>
</div>
)
}
+41
View File
@@ -0,0 +1,41 @@
import { h } from 'preact';
import { useState, useCallback } from 'preact/hooks';
import { ExternalLink } from 'react-feather';
import { useSelector } from 'react-redux'
import { State } from './../store'
import http from './../http';
function Want(want) {
const r = want.data;
const ws = useSelector((state: State) => state.ws);
const [confirm, setConfirm] = useState(false);
function onClick() {
if (confirm) return ws.sendWantRm(want.id);
setTimeout(() => setConfirm(false), 2500);
return setConfirm(true);
};
const text = confirm
? 'confirm'
: 'remove';
return (
<article key={r.id}>
<div>
<h3>{r.artists_sort} - {r.title} <a href={r.uri} target="_blank"><ExternalLink /></a></h3>
</div>
<img src={r.thumb} />
<div class='details'>
<div class='alt'>{r.year}</div>
<div class='alt'>{r.formats[0].descriptions.join(' ')}</div>
</div>
<button onClick={onClick} >{text}</button>
</article>
);
}
export default Want;
+4
View File
@@ -0,0 +1,4 @@
declare module "*.less" {
const mapping: Record<string, string>;
export default mapping;
}
+95
View File
@@ -0,0 +1,95 @@
import axios, { AxiosInstance } from 'axios';
type ApiResponse = [string | null, any | null];
declare module 'axios' {
export interface AxiosInstance {
request(config: AxiosRequestConfig): Promise<ApiResponse>;
get(url: string, config?: AxiosRequestConfig): Promise<ApiResponse>;
delete(url: string, config?: AxiosRequestConfig): Promise<ApiResponse>;
head(url: string, config?: AxiosRequestConfig): Promise<ApiResponse>;
post(url: string, data?: any, config?: AxiosRequestConfig): Promise<ApiResponse>;
put(url: string, data?: any, config?: AxiosRequestConfig): Promise<ApiResponse>;
patch(url: string, data?: any, config?: AxiosRequestConfig): Promise<ApiResponse>;
}
}
const api = axios.create({
baseURL: '/api',
withCredentials: true
});
function onSuccess(response) {
return [null, response.data];
}
function onError(error) {
if (error.response) return [error.response.data.error, null];
if (error.request) return ['Network Error', null];
return ['Unknown Error', null];
}
api.interceptors.response.use(onSuccess, onError);
const actions = {
getBundles: async function getBundles() {
return api.get('/acp/bundles');
},
getOffers: async function getOffers() {
return api.get('/user/offers');
},
getOffersAcp: async function getOffersAcp() {
return api.get('/acp/offers');
},
getWants: async function getWants() {
return api.get('/want/all');
},
postWantSearch: async function postWantSearch(artist, title) {
return api.post('/want/search', { artist, title });
},
postWant: async function postWant(releaseId) {
return api.post('/want', { releaseId });
},
postDiscogs: async function postDiscogs(username) {
return api.post('/want/discogs', { username });
},
getLogin: async function getLogin() {
return api.get('/auth/login')
},
postLogin: async function postLogin(email, password) {
return api.post('/auth/login', {
email,
password,
// discogs,
});
},
postLogout: async function postLogout() {
return api.post('/auth/logout', {});
},
postRegister: async function postRegister(email, password) {
return api.post('/auth/register', {
email,
password,
});
},
postOffers: async function postOffers(listingIds, shipping, baseShipping) {
return api.post('/offers', {
listingIds,
shipping,
baseShipping,
});
},
};
export default actions;
+4
View File
@@ -0,0 +1,4 @@
import './style/index.less';
import App from './app';
export default App;
+21
View File
@@ -0,0 +1,21 @@
{
"name": "mcrates",
"short_name": "mcrates",
"start_url": "/",
"display": "standalone",
"orientation": "portrait",
"background_color": "#fff",
"theme_color": "#673ab8",
"icons": [
{
"src": "/assets/icons/android-chrome-192x192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "/assets/icons/android-chrome-512x512.png",
"type": "image/png",
"sizes": "512x512"
}
]
}
+92
View File
@@ -0,0 +1,92 @@
import { h } from 'preact';
import { useState, useEffect } from 'preact/hooks';
import http from './../http';
import { useSelector, useDispatch } from 'react-redux'
import { State } from './../store';
enum Status {
Accepted = 'Accepted',
Pending = 'Pending',
Declined = 'Declined',
Confirmed = 'Confirmed',
Complete = 'Complete',
}
const POSITIVE_STATUS = [Status.Accepted, Status.Confirmed, Status.Complete];
function Item({ item }) {
const ws = useSelector((state: State) => state.ws);
console.log(item);
const statuses = Object.entries(Status).map(([label, value]) => (
<option selected={item.status === value} value={value}>{value}</option>
));
function onChange(e) {
ws.sendAcpOfferStatusSet(item.id, e.target.value);
}
const bold = POSITIVE_STATUS.includes(item.status)
? 'bold'
: '';
return (
<div key={item.id} class={bold}>
<a href={item.listing.data.uri} target="_blank" >
<h4>{item.listing.data.release.description}</h4>
</a>
<h5>{item.user.email}</h5>
<h5 class='alt-text'>{item.total}</h5>
<h5>{item.status}</h5>
<label>Set Status:</label>
<select onChange={onChange}>
{statuses}
</select>
</div>
);
}
function SellerGroup(group) {
const ready = group.offers.filter(item => POSITIVE_STATUS.includes(item.status)).length;
return (
<div class="bundle">
<section>
<h2>{group.username} - {group.seller}</h2>
<h3>{ready} / {group.offers.length} offers</h3>
<h4>Ships: {group.mentionsAustralia && 'AU'} {group.mentionsWorldwide && 'WORLD'}</h4>
</section>
<div class='items'>
{group.offers.map(i => <Item item={i} />)}
</div>
</div>
)
}
function Home() {
const offers = useSelector((state: State) => state.acpOffers);
const ws = useSelector((state: State) => state.ws);
// useEffect(() => {
// ws.sendAcpOffers();
// }, [offers, ws]);
if (!offers.length) {
return (
<div class={'offers'}>
loading...
</div>
);
}
return (
<div class='bundles'>
{offers.map(SellerGroup)}
</div>
);
}
export default Home;
+52
View File
@@ -0,0 +1,52 @@
import { h } from 'preact';
const Info = () => {
return (
<div class='info'>
<div class='words'>
<section>
<h1>Info</h1>
<p> Thanks for signing up! </p>
<p> Mates Crates is a record importing service in alpha. </p>
<p> Its purpose is to reduce the cost of shipping vinyl records to Melbourne, Australia. </p>
</section>
<section>
<h2>How It Works</h2>
<p> First, add some records to your <span class='alt'>List</span> on the <a href='/search'>Search Page</a>. </p>
<p> Mates Crates will then make <span class='alt'>Offers</span> to you for releases you add to your <a href='/list'>List</a>. </p>
<p> Select all <span class='alt'>Offers</span> you are interested in, the more you accept the more likely the item will be <span class='alt-text'>Confirmed</span>.</p>
<p> Check back regularly for updated <span class='alt'>Offers</span>, the global record marketplace is a living thing. </p>
</section>
<section>
<h2>Offers</h2>
<p> Not all items on your <span class='alt'>List</span> will have offers. However, as new users join the site the number of offers we can make increases, so be sure to tell your friends!</p>
<p> Think of offers like an expression of interest, they are not confirmed until they are set to <span class='alt-text'>Confirmed</span> status. </p>
<p> After that they will be shipped to Melbourne and ready for pick up. </p>
</section>
<section>
<h2>Pick Up</h2>
<p> Use the contact options to get in touch. </p>
<p> Payment is cash on delivery. </p>
</section>
<section>
<h2>FAQ</h2>
<h3> Delivery Times </h3>
<p> Records are usually available for pickup within a week. We order in bulk, which often means express, insured shipping to Melbourne. </p>
<h3> Friends </h3>
<p> Please invite other collectors to the site. More users means offers can become cheaper and shipped sooner. </p>
</section>
</div>
<section class='contact'>
<h2>Contact</h2>
<p><a href='mailto:ntr@mnml.gg?subject=Mates%20Crates%20Enquiry'>Email</a></p>
<p><a href='https://discord.gg/QfjY8WNQyx'>Discord</a></p>
</section>
</div>
);
};
export default Info;
+28
View File
@@ -0,0 +1,28 @@
import { h } from 'preact';
import { useSelector } from 'react-redux'
import { State } from './../store'
import Want from './../components/want';
interface Props {
user: any,
wants: any[],
getUserOffers: (userId: string) => void,
}
const List = ({ user }: Props) => {
const wants = useSelector((state: State) => state.wants);
if (!wants.length) {
return <div> loading... </div>;
}
return (
<div class="list">
{wants.map(Want)}
</div>
);
};
export default List;
+109
View File
@@ -0,0 +1,109 @@
import { h } from 'preact';
import { route } from 'preact-router';
import { useState, useEffect, useRef } from 'preact/hooks';
import { useSelector, useDispatch } from 'react-redux'
import http from './../http';
import { State, userState } from './../store';
const Login = ({ mode }) => {
const dispatch = useDispatch()
const user = useSelector((state: State) => state.user);
const ws = useSelector((state: State) => state.ws);
const [email, setEmail] = useState('')
const [password, setPassword] = useState('');
const [confirmPassword, setConfirmPassword] = useState('');
const [error, setError] = useState('');
async function postLogin() {
const [error, user] = await http.postLogin(email, password);
if (error) return setError(error);
route('/');
dispatch(userState.actions.set(user));
// reconnect ws to get logged in methods
ws.connect();
}
async function postRegister() {
const [error, user] = await http.postRegister(email, password);
if (error) return setError(error);
route('/search');
dispatch(userState.actions.set(user));
}
function handleSubmit(e) {
e.preventDefault();
if (mode === 'login') return postLogin()
if (mode === 'register') return postRegister();
}
const emailRef = useRef<HTMLInputElement>()
useEffect(() => {
emailRef.current.focus()
}, [])
const errorEl = error
? <div class='alt-text'>{error}</div>
: null;
if (mode === 'register') return (
<div class="login">
{errorEl}
<form action="">
<div class="item">
<label for="email">email</label>
<input type="email" name="email" id="email" ref={emailRef} value={email} required onInput={(e) => setEmail((e.target as HTMLInputElement).value)} />
</div>
<div class="item">
<label for="password">password</label>
<input type="password" name="password" id="password" value={password} required onInput={(e) => setPassword((e.target as HTMLInputElement).value)} />
</div>
<div class="item">
<label for="password">confirm password</label>
<input type="password" name="password" id="password" value={confirmPassword} required onInput={(e) => setConfirmPassword((e.target as HTMLInputElement).value)} />
</div>
<div class="item">
<input type="submit" value="register" onClick={handleSubmit} />
</div>
</form>
</div>
);
return (
<div class="login">
{errorEl}
<form action="">
<div class="item">
<label for="email">email</label>
<input type="email" name="email" id="email" ref={emailRef} value={email} required onInput={(e) => setEmail((e.target as HTMLInputElement).value)} />
</div>
<div class="item">
<label for="password">password</label>
<input type="password" name="password" id="password" value={password} required onInput={(e) => setPassword((e.target as HTMLInputElement).value)} />
</div>
<div class="item">
<input type="submit" value="login" onClick={handleSubmit} />
</div>
</form>
</div>
);
};
function LoginPage({ mode }) {
return (
<div class="landing">
<Login mode={mode} />
<div></div>
<div>
<p>Mates Crates is a record importing service.</p>
<p>We bundle orders together from thousands of online stores to reduce the cost of shipping vinyl.</p>
<p>Based in Preston.</p>
</div>
</div>
)
}
export default LoginPage;
+30
View File
@@ -0,0 +1,30 @@
import { h } from 'preact';
import { useState, useEffect } from 'preact/hooks';
import { route } from 'preact-router';
import { useSelector, useDispatch } from 'react-redux'
import http from './../http';
import { State, offersState } from './../store';
import WantOffers from './../components/want.offers';
function Home() {
const dispatch = useDispatch()
const offers = useSelector((state: State) => state.offers);
// if (!offers.length) {
// return (
// <div class={'offers'}>
// loading...
// </div>
// );
// }
return (
<div class={'offers'}>
{Object.values(offers).map(WantOffers)}
</div>
);
}
export default Home;
+119
View File
@@ -0,0 +1,119 @@
import { h } from 'preact';
import { useState, useRef, useEffect } from 'preact/hooks';
import { ExternalLink, Check } from 'react-feather';
import http from '../http';
function Result(r) {
const [added, setAdded] = useState(false)
async function onClick() {
const [error, want] = await http.postWant(r.id);
if (error) return false;
setAdded(true);
}
const classes = `${added ? 'added' : ''}`;
const btnText = added
? <Check />
: '+';
return (
<article key={r.id}>
<button onClick={onClick} disabled={added} class={classes} >{btnText}</button>
<div>
<h3>{r.title} <a href={`https://discogs.com${r.uri}`} target="_blank"><ExternalLink /></a></h3>
<h5 class='alt-text' >{r.year}</h5>
<h5 class='alt-text'>{r.country}</h5>
<h5 class='alt-text'>{r.format.join(' ')}</h5>
</div>
<img src={r.thumb} />
</article>
);
}
const User = () => {
const [artist, setArtist] = useState('')
const [title, setTitle] = useState('');
const [username, setUsername] = useState('');
const [results, setResults] = useState([]);
const [infoText, setInfoText] = useState('');
async function fetchResults() {
const [error, results] = await http.postWantSearch(artist, title);
if (results.length === 0) {
setResults([]);
return setInfoText('No results');
}
setInfoText('');
setResults(results);
}
async function submitWantlist() {
const [error, wants] = await http.postDiscogs(username);
if (wants.length === 0) {
setResults([]);
return setInfoText('No new additions');
}
setInfoText(`Added ${wants.length} items to your list.`);
}
const searchRef = useRef<HTMLInputElement>()
useEffect(() => {
searchRef.current.focus()
}, [])
function handleSubmit(e) {
e.preventDefault();
fetchResults();
}
function handleDiscogsSubmit(e) {
e.preventDefault();
submitWantlist();
}
const topEl = results.length
? <button onClick={() => window.scrollTo(0, 0)}> top </button>
: null;
const infoEl = infoText
? <article><div></div><div>{infoText}</div></article>
: null;
return (
<div class='search'>
<div>
<form action="" method="get">
<label for="artist">Artist</label>
<input type="text" name="artist" ref={searchRef} placeholder="Ben Klock" id="artist" value={artist} onChange={e => setArtist((e.target as HTMLInputElement).value) } required />
<label for="title">Title</label>
<input type="text" name="title" placeholder="Subzero" id="title" value={title} onChange={e => setTitle((e.target as HTMLInputElement).value)} required />
<input type="submit" value="Search" onClick={handleSubmit} />
</form>
<hr />
<div>
<form action="" method="get">
<label for="Discogs">Discogs Username</label>
<input type="text" name="username" placeholder="Discogs Username" id="username" value={username} onChange={e => setUsername((e.target as HTMLInputElement).value) } required />
<input type="submit" value="Sync" onClick={handleDiscogsSubmit} />
</form>
</div>
<hr />
<p>Data provided by Discogs.</p>
</div>
<div />
<div>
{infoEl}
{results.map(Result)}
{topEl}
</div>
</div>
);
};
export default User;
+61
View File
@@ -0,0 +1,61 @@
import { createStore, combineReducers } from 'redux'
import { configureStore, createSlice } from '@reduxjs/toolkit'
export const userState = createSlice({
name: 'user',
initialState: null,
reducers: {
set: (state, action) => action.payload
},
});
export const offersState = createSlice({
name: 'offers',
initialState: [],
reducers: {
set: (state, action) => action.payload
},
});
export const acpOffersState = createSlice({
name: 'acpOffers',
initialState: [],
reducers: {
set: (state, action) => action.payload
},
});
export const wantsState = createSlice({
name: 'wants',
initialState: [],
reducers: {
set: (state, action) => action.payload
},
});
export const wsState = createSlice({
name: 'ws',
initialState: null,
reducers: {
set: (state, action) => action.payload
},
});
const reducer = combineReducers({
offers: offersState.reducer,
wants: wantsState.reducer,
ws: wsState.reducer,
user: userState.reducer,
acpOffers: acpOffersState.reducer,
});
const store = createStore(
reducer,
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
);
export default store;
export type State = ReturnType<typeof store.getState>
export type Dispatch = typeof store.dispatch
+446
View File
@@ -0,0 +1,446 @@
// Digital Ocean reset
html {
box-sizing: border-box;
font-size: 16px;
}
*, *:before, *:after {
box-sizing: inherit;
}
body, h4, h5, h6, p, ol, ul {
margin: 0;
padding: 0;
font-weight: normal;
}
ol, ul {
list-style: none;
}
img {
// max-width: 100%;
height: auto;
}
/* latin */
@font-face {
font-family: 'Raleway';
font-style: normal;
font-weight: 400;
src: url(https://fonts.gstatic.com/s/raleway/v28/1Ptxg8zYS_SKggPN4iEgvnHyvveLxVvaorCIPrE.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
@font-face {
font-family: 'Source Code Pro';
font-style: normal;
font-weight: 400;
src: url(https://fonts.gstatic.com/s/sourcecodepro/v21/HI_diYsKILxRpg3hIP6sJ7fM7PqPMcMnZFqUwX28DMyQtMlrTA.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
@font-face {
font-family: 'Noto Sans Mono';
font-style: normal;
font-weight: 400;
font-stretch: normal;
src: url(https://fonts.gstatic.com/s/notosansmono/v20/BngrUXNETWXI6LwhGYvaxZikqZqK6fBq6kPvUce2oAZcdthSBUsYck4-_FNJ093dVQ.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
// stuff from UX laws
html {
color:var(--base-font-color);
font-size:clamp(var(--min-font-size),calc(50% + 1vw),var(--max-font-size));
font-family:var(--base-font-family);
line-height:var(--base-line-height);
-webkit-text-size-adjust:100%;
background:var(--background-0);
box-sizing:border-box
}
// @charset "UTF-8";
:root {
--breakpoint-medium: 35em;
--breakpoint-large: 56em;
--small-font-size: 0.75em;
--base-font-size: 1em;
--smedium-font-size: 1.333em;
--medium-font-size: 1.777em;
--large-font-size: 2.369em;
--xlarge-font-size: 3.157em;
--base-font-family: 'Raleway', Arial, Sans-Serif;
--secondary-font-family: 'Noto Sans Mono', monospace;
--base-font-color: var(--color-off-white);
--secondary-font-color: var(--color-mid-grey);
--base-line-height: 1.4;
--header-line-height: 1.2;
--base-border-color: var(--secondary-font-color);
--base-border: 2px solid var(--base-border-color);
--hover-border: 2px solid var(--base-font-color);
--focus-border: 2px solid var(--base-font-color);
--base-border-radius: .2rem;
--base-focus: 2px solid var(--base-accent-color);
--base-outline-offset: 0.25em;
--min-font-size: var(--base-font-size);
--max-font-size: var(--large-font-size);
--color-dark-blue: #101014;
--color-mid-blue: #16171d;
--color-light-blue: #1f2128;
--color-mid-grey: #9BA0B3;
--color-off-white: #F4F1D0;
--color-accent-blue: #5E9ED6;
--color-blue-gray: #506485;
--color-blue: #475977;
--color-green: #476d4a;
--color-accepted: var(--color-blue);
--color-confirmed: var(--color-green);
--base-accent-color: var(--color-accent-blue);
--fg-color: var(--color-off-white);
--hover-link-color: var(--color-off-white);
--background-0: var(--color-dark-blue);
--background-1: var(--color-mid-blue);
--background-2: var(--color-light-blue);
--spacing: 1rem;
--spacing-half: calc(var(--spacing)/2);
--spacing-double: calc(var(--spacing)*2);
--utility-bar-header-height: calc(var(--spacing) * 1.5);
}
button, input, optgroup, select, textarea {
margin:0;
border:var(--base-border);
border-radius:var(--base-border-radius);
background:0 0;
font-family:inherit;
font-size:100%;
color:inherit;
line-height:var(--base-line-height);
}
input:hover, textarea:hover {
color:var(--base-font-color);
border:var(--hover-border)
}
input:focus, textarea:focus {
border:var(--focus-border);
outline:0
}
button, input {
overflow:visible;
}
button, select {
text-transform: none;
}
button, [type=button], [type=reset], [type=submit] {
-webkit-appearance:button;
border-color: transparent;
cursor:pointer;
font-family: var(--secondary-font-family);
font-size: var(--small-font-size);
font-weight: bold;
text-transform: uppercase;
&:hover {
background: var(--background-2);
border-color: transparent;
}
}
h1, h2, h3, h4, h5, h6 {
margin-top:0;
line-height:var(--header-line-height);
}
h1 {
margin-bottom:var(--spacing);
font-size:var(--large-font-size)
}
h2 {
margin-bottom:var(--spacing);
font-size:var(--medium-font-size);
}
h3 {
margin-bottom:calc(var(--spacing)/2);
font-size:var(--smedium-font-size)
}
h4 {
margin-bottom:calc(var(--spacing)/3);
font-size:var(--base-font-size)
}
p {
margin:0 0 var(--spacing)
}
// ok actual styles
body {
padding-top: calc(var(--spacing)/2);
}
main {
padding: 0 5% 0 5%;
}
button, [type=submit] {
padding: var(--spacing-half);
background: var(--background-1);
}
.landing {
display: grid;
grid-template-columns: 1fr 0.5fr 2fr;
.login {
button {
width: 100%;
}
}
}
.info {
display: grid;
grid-template-columns: 1fr 1fr;
section {
margin-bottom: var(--spacing);
}
.contact {
text-align: center;
}
}
.alt-text, .alt {
font-family: var(--secondary-font-family);
// font-size: var(--small-font-size);
text-transform: uppercase;
}
article {
background-color: var(--background-1);
button {
border-color: var(--background-2);
}
}
.search {
display: grid;
grid-template-columns: 1fr 0.5fr 2fr;
article {
display: grid;
padding: var(--spacing);
margin-bottom: var(--spacing);
grid-template-columns: 1fr 4fr 1fr;
}
button {
margin-right: var(--spacing);
background: var(--background-2);
border-color: var(--background-1);
&.added {
background: var(--color-green);
cursor: auto;
}
}
}
.list {
display: grid;
grid-template-columns:
1fr 1fr 1fr;
grid-row-gap: var(--spacing);
grid-column-gap: var(--spacing);
article {
display: grid;
// grid-template-rows:
// 1fr min-content min-content;
grid-template-areas:
"title img"
"details details"
"remove remove";
button {
margin-top: var(--spacing-half);
grid-area: remove
}
.details {
grid-area: details;
}
padding: var(--spacing);
}
}
form {
margin-bottom: var(--spacing);
label, input {
display: block;
}
input, button {
width: 100%;
}
input {
padding: var(--spacing-half);
margin-bottom: var(--spacing);
}
label {
font-family: var(--secondary-font-family);
font-size: var(--small-font-size);
text-transform: uppercase;
margin-bottom: var(--spacing-half);
}
}
a {
color: var(--secondary-font-color);
font-family: var(--secondary-font-family);
text-transform: uppercase;
&:hover {
color: var(--fg-color);
}
}
nav {
a {
font-size: var(--small-font-size);
margin-right: var(--spacing);
}
}
header {
display: grid;
grid-template-columns: 1fr 1fr min-content;
padding-bottom: var(--spacing);
a {
white-space: nowrap;
}
}
.bundles {
flex-flow: row;
.bundle {
margin-bottom: var(--spacing-double);
section {
margin-bottom: var(--spacing);
}
.items {
display: grid;
grid-template-columns: repeat(5, 1fr);
div {
margin-right: var(--spacing);
margin-bottom: var(--spacing);
display: grid;
&.bold {
font-weight: bold;
background: var(--background-2);
}
}
}
}
}
.offers {
display: grid;
grid-template-columns: 1fr 1fr;
.offer {
display: grid;
grid-template-columns: 1fr min-content;
grid-template-rows: 1fr min-content;
grid-template-areas:
"details img"
"lines lines";
padding: var(--spacing);
margin: var(--spacing-half);
background: var(--background-1);
section {
grid-area: details;
}
img {
grid-area: img;
justify-self: end;
margin-left: var(--spacing-half);
}
.lines {
grid-area: lines;
display: flex;
flex-wrap: wrap;
column-gap: var(--spacing-half);
button {
margin-top: var(--spacing-half);
border-color: var(--background-2);
&.accepted {
background: var(--color-accepted);
}
&.confirmed {
cursor: auto;
background: var(--color-confirmed);
}
&.neg {
margin-left: var(--spacing-half);
}
}
}
}
}
.number {
font-family: var(--secondary-font-family);
text-transform: uppercase;
}
+161
View File
@@ -0,0 +1,161 @@
import { wantsState, wsState, userState, offersState, acpOffersState } from './store'
const SOCKET_URL = `${window.location.protocol === 'https:' ? 'wss://' : 'ws://'}${window.location.host}/api/ws`;
export default function createSocket(dispatch) {
let ws;
// -------------
// Outgoing
// -------------
function send(msg) {
if (msg[0] !== 'Ping') console.log('outgoing msg', msg);
if (ws.readyState !== 1) return false;
try {
ws.send(JSON.stringify(msg));
} catch (e) {
console.warn(e);
}
}
// let ping;
function sendPing() {
// ping = Date.now();
send(['Ping', {}]);
// setTimeout(sendPing, 50);
}
function sendOfferStatusSet(id, status) {
send(['OfferStatusSet', { id, status }]);
}
function sendWantRm(id) {
send(['WantRm', { id }]);
}
function sendOffers() {
send(['Offers', {}]);
}
function sendWants() {
send(['Wants', {}]);
}
function sendAcpOffers() {
send(['AcpOffers', {}]);
}
function sendAcpOfferStatusSet(id, status) {
send(['AcpOfferStatusSet', { id, status }]);
}
let pongTimeout;
function onPong() {
pongTimeout = setTimeout(sendPing, 5000);
}
// -------------
// Incoming
// -------------
function onUser(user) {
dispatch(userState.actions.set(user));
}
function onWants(wants) {
dispatch(wantsState.actions.set(wants));
}
function onOffers(offers) {
dispatch(offersState.actions.set(offers));
}
function onAcpOffers(offers) {
dispatch(acpOffersState.actions.set(offers));
}
// -------------
// Setup
// -------------
// when the server sends a reply it will have one of these message types
// this object wraps the reply types to a function
const handlers = {
Wants: onWants,
Offers: onOffers,
User: onUser,
AcpOffers: onAcpOffers,
Pong: onPong,
Error: errHandler,
};
function errHandler(error) {
// store.dispatch(http.LIBRARY_EVENT, { level: 'error', log: error });
console.error(error);
}
function onMessage(event) {
const res = event.data;
const [msgType, params] = JSON.parse(res);
if (msgType !== 'Pong') {
console.log(msgType, params);
}
// check for error and split into response type and data
if (!handlers[msgType]) return console.error(`${msgType} handler missing`);
return handlers[msgType](params);
}
// Connection opened
function onOpen() {
console.log('websocket connected', SOCKET_URL)
sendPing();
sendAcpOffers();
sendOffers();
sendWants();
}
function onError(event) {
console.error('WebSocket error', event);
}
function onClose(event) {
console.error('WebSocket closed', event);
return setTimeout(connect, 2000);
}
function connect() {
if (ws) {
clearTimeout(pongTimeout);
ws.removeEventListener('open', onOpen);
ws.removeEventListener('message', onMessage);
ws.removeEventListener('error', onError);
ws.removeEventListener('close', onClose);
ws = null;
}
console.log('connecting to websocket', SOCKET_URL);
ws = new WebSocket(SOCKET_URL);
// Listen for messages
ws.addEventListener('open', onOpen);
ws.addEventListener('message', onMessage);
ws.addEventListener('error', onError);
ws.addEventListener('close', onClose);
return ws;
}
return {
connect,
sendOfferStatusSet,
sendWantRm,
sendOffers,
sendAcpOffers,
sendAcpOfferStatusSet,
};
}
+64
View File
@@ -0,0 +1,64 @@
{
"compilerOptions": {
/* Basic Options */
"target": "ES5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'. */
"module": "ESNext", /* Specify module code generation: 'none', commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation: */
"allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
"jsx": "react", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
"jsxFactory": "h", /* Specify the JSX factory function to use when targeting react JSX emit, e.g. React.createElement or h. */
"jsxFragmentFactory": "Fragment", /* Specify the JSX fragment factory function to use when targeting react JSX emit with jsxFactory compiler option is specified, e.g. Fragment. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
// "outDir": "./", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "removeComments": true, /* Do not emit comments to output. */
"noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
/* Strict Type-Checking Options */
"strict": false, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
/* Module Resolution Options */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"esModuleInterop": true, /* */
"baseUrl": "./", /* Base directory to resolve non-absolute module names. */
"paths": { /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
"react": ["./node_modules/preact/compat"],
"react-dom": ["./node_modules/preact/compat"]
},
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
/* Source Map Options */
// "sourceRoot": "./", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "./", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
/* Advanced Options */
"skipLibCheck": true /* Skip type checking of declaration files. */
},
"include": ["src/**/*"]
}
+35
View File
@@ -0,0 +1,35 @@
version: "3.3"
services:
pg:
image: postgres:latest
environment:
POSTGRES_USER: 'mcrates'
POSTGRES_DB: 'mcrates'
POSTGRES_PASSWORD: 'digdeep'
# allow prisma client to push changes from outside docker etc
# ufw blocks this from being publicly accessible
ports:
- '45432:5432'
volumes:
- "./db:/var/lib/postgresql/data"
api:
image: node:16
working_dir: /opt/mcrates/api
entrypoint: npm start
environment:
DATABASE_URL: 'postgresql://mcrates:digdeep@pg:5432/mcrates?schema=public'
volumes:
- "./api:/opt/mcrates/api"
nginx:
image: nginx:latest
volumes:
- './nginx/matescrates.au.nginx.conf:/etc/nginx/conf.d/default.conf'
- './client/build:/srv/matescrates'
- '/etc/letsencrypt:/etc/letsencrypt'
ports:
- '80:80'
- '443:443'
+21
View File
@@ -0,0 +1,21 @@
version: "3.3"
services:
pg:
image: postgres:latest
environment:
POSTGRES_USER: 'mcrates'
POSTGRES_DB: 'test'
POSTGRES_PASSWORD: 'digdeep'
api:
image: node:16
working_dir: /opt/mcrates/api
entrypoint: npm run test:watch
depends_on:
- pg
environment:
DATABASE_URL: "postgresql://mcrates:digdeep@pg:5432/test?schema=public"
volumes:
- "./api:/opt/mcrates/api"
+30
View File
@@ -11,3 +11,33 @@ services:
- '45432:5432' - '45432:5432'
volumes: volumes:
- "./db:/var/lib/postgresql/data" - "./db:/var/lib/postgresql/data"
api:
image: node:16
working_dir: /opt/mcrates/api
depends_on:
- 'pg'
entrypoint: npm start
environment:
DATABASE_URL: 'postgresql://mcrates:digdeep@pg:5432/mcrates?schema=public'
volumes:
- "./api:/opt/mcrates/api"
client:
image: node:16
working_dir: /opt/mcrates/client
entrypoint: npm start
ports:
- '8080:8080' # fwd the hmr websocket
volumes:
- "./client:/opt/mcrates/client"
- "./api:/opt/mcrates/api"
nginx:
image: nginx:latest
depends_on:
- 'api'
volumes:
- './nginx/localhost.nginx.conf:/etc/nginx/conf.d/default.conf'
ports:
- '80:80'
@@ -1,8 +0,0 @@
# #!/bin/bash
# set -e
# psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
# CREATE USER mcrates;
# CREATE DATABASE mcrates;
# GRANT ALL PRIVILEGES ON DATABASE mcrates TO mcrates;
# EOSQL
-93
View File
@@ -1,93 +0,0 @@
import fs from 'fs/promises';
import _ from 'lodash';
import Promise from 'bluebird';
import discogsService from './src/discogs';
import pgService from './src/pg';
import { logAxiosError } from './src/utils';
async function start() {
const pg = pgService();
const discogs = discogsService(pg);
const usernames = [
'margh',
'jmcdermid1',
'vanderbabe',
];
async function fetchListings(w) {
return w.map(discogs.listings)
}
async function populateUserListings(user) {
console.log(user);
const wants = await discogs.wantlist(user);
console.log(user.discogs, 'has', wants.length, 'wants');
for (const want of wants) {
const listings = await discogs.listings(want);
const populated = listings.filter(l => l);
console.log(want.id, 'has', populated.length, '/', listings.length, 'listings');
}
// const listings = await Promise
// .all(releases.map(async want => {
// const listings = (await getListings(want)).filter(l => l);
// const insertions = await Promise.all(listings.map(async l => {
// const listing = await pg.listingUpsert(l, want);
// console.log(listing.id, listing.data.release.description, want.user, listing.seller);
// return listing;
// }));
// console.log(insertions.length, want.id);
// }));
// return listings;
}
return Promise.resolve(usernames)
.map(u => discogs.user(u))
.map(populateUserListings);
// return Promise.map(releases, async w => {
// try {
// const l = await discogs.listings(w.id)
// return l;
// } catch (e) {
// // console.error(e);
// return null;
// }
// })
// .then(listings => _
// .chain(listings)
// .filter(l => l)
// .flatten()
// // .map(l => ({
// // id: l.id,
// // description: l.release.description,
// // seller_id: l.seller.id,
// // seller_name: l.seller.username,
// // }))
// // .groupBy(l => l.seller_id)
// .value()
// );
}
function run() {
start()
.then(r => {
console.log(r);
setTimeout(run, 60 * 1000);
})
.catch(e => {
console.error(e);
if (e.message === 'rate limited') {
return setTimeout(run, 60 * 1000);
}
process.exit(1);
});
}
run();
-17
View File
@@ -1,17 +0,0 @@
module.exports = {
client: 'postgresql',
connection: {
host : '0.0.0.0',
port: 45432,
database: 'mcrates',
user: 'mcrates',
password: 'digdeep',
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
};
-17
View File
@@ -1,17 +0,0 @@
exports.up = async knex => {
await knex.schema.createTable('users', table => {
table.uuid('id')
.primary()
.defaultTo(knex.raw('gen_random_uuid()'));
table.string('discogs')
.unique();
table.jsonb('data')
.notNullable();
table.timestamps(true, true);
});
};
exports.down = async () => true;
-27
View File
@@ -1,27 +0,0 @@
exports.up = async knex => {
await knex.schema.createTable('wants', table => {
table.uuid('id')
.primary()
.defaultTo(knex.raw('gen_random_uuid()'));
table.integer('release')
.notNullable()
.index();
table.uuid('user')
table.foreign('user')
.references('id')
.inTable('users')
.onDelete('CASCADE');
table.jsonb('data')
.notNullable();
// user can only want a release once
table.unique(['user', 'release']);
table.timestamps(true, true);
});
};
exports.down = async () => true;
-23
View File
@@ -1,23 +0,0 @@
exports.up = async knex => {
await knex.schema.createTable('listings', table => {
table.integer('id')
.primary();
table.uuid('want')
table.foreign('want')
.references('id')
.inTable('wants')
.onDelete('CASCADE');
table.integer('seller')
.notNullable()
.index();
table.jsonb('data')
.notNullable();
table.timestamps(true, true);
});
};
exports.down = async () => true;
+34
View File
@@ -0,0 +1,34 @@
upstream mcrates_api {
server api:41337;
}
upstream mcrates_client {
server client:8080;
}
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
location / {
proxy_pass http://mcrates_client;
proxy_read_timeout 600s;
}
location /api/ws {
proxy_pass http://mcrates_api;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
proxy_read_timeout 600s;
}
location /api/ {
# trailing slash means api will receive url WITHOUT /api
proxy_pass http://mcrates_api/;
proxy_read_timeout 600s;
}
}
+54
View File
@@ -0,0 +1,54 @@
upstream mcrates_api {
server api:41337;
}
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
server_name matescrates.au;
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_types text/plain text/css text/xml application/json application/javascript application/xml+rss application/atom+xml image/svg+xml;
location / {
root /srv/matescrates;
index index.html;
try_files $uri $uri/ index.html;
}
location /api/ws {
proxy_pass http://mcrates_api;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
proxy_read_timeout 600s;
}
location /api/ {
# trailing slash means api will receive url WITHOUT /api
proxy_pass http://mcrates_api/;
proxy_read_timeout 600s;
}
error_page 404 /;
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/matescrates.au/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/matescrates.au/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
# http -> https
server {
server_name matescrates.au;
return 301 https://$host$request_uri;
}
-3352
View File
File diff suppressed because it is too large. Load diff
-30
View File
@@ -1,30 +0,0 @@
{
"name": "mates-crates",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"migrate": "knex migrate:latest",
"migrate:make": "prisma migrate dev --name",
"start": "ts-node-dev --respawn --transpile-only index.ts"
},
"author": "",
"license": "ISC",
"devDependencies": {
"node-dev": "^7.4.3",
"prisma": "^4.1.0",
"ts-node-dev": "^2.0.0",
"typescript": "^4.7.4"
},
"dependencies": {
"@prisma/client": "^4.1.0",
"axios": "^0.27.2",
"bluebird": "^3.7.2",
"bunyan": "^1.8.15",
"express": "^4.18.1",
"knex": "^2.2.0",
"lodash": "^4.17.21",
"pg": "^8.7.3"
}
}
@@ -1,13 +0,0 @@
-- CreateTable
CREATE TABLE "User" (
"id" TEXT NOT NULL,
"discogs" TEXT,
"data" JSONB NOT NULL,
"creaedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "User_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "User_discogs_key" ON "User"("discogs");
-3
View File
@@ -1,3 +0,0 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "postgresql"
-20
View File
@@ -1,20 +0,0 @@
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model User {
id String @id @default(uuid())
discogs String? @unique
data Json
creaedAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
-79
View File
@@ -1,79 +0,0 @@
import fs from 'fs/promises';
import axios from 'axios';
import cheerio from 'cheerio';
import { logAxiosError } from './utils';
import { Pg } from './pg';
export default function discogs(pg: Pg) {
const TOKEN = 'wRTXuYZGOPsIQgKRGKCAkUkidGtvpSfQgPWkYIpD';
const apiBase = 'https://api.discogs.com'
const discogsBase = 'https://www.discogs.com'
const api = axios.create({
headers: {
'Authorization': TOKEN,
},
});
function handleApiError(e) {
logAxiosError(e);
if (e.response.status === 429) {
throw new Error('rate limited');
}
}
async function user(discogs: string) {
const user = await pg.userGetName(discogs);
if (user) return user;
console.log('fetching user', discogs);
const res = await api.get(`${apiBase}/users/${discogs}`);
return pg.userUpsert(res.data);
}
async function wantlist(user: User): Promise<Want[]> {
const stored = await pg.userWants(user);
if (stored.length) return stored;
const { data: { wants, pagination }} = await api.get(`${apiBase}/users/${user.data.username}/wants`);
return Promise.all(wants.map(r => pg.wantUpsert(r, user)))
}
async function inventory(user: string) {
const res = await api.get(`${apiBase}/users/${user}/inventory`);
return res.data.listings;
}
async function listings(want: Want): Promise<Listing[]> {
const res = await api.get(`https://www.discogs.com/sell/release/${want.release}`);
const $ = cheerio.load(res.data);
const ids = $('.item_description_title[href]')
.map(function getListingId() {
const id = Number.parseInt(this.attribs.href.replace('/sell/item/', ''), 10);
return id;
})
.get();
return Promise.all(ids.map(id => listing(id, want)));
}
async function listing(id, want) {
const listing = await pg.listingGet(id);
if (listing) return listing;
try {
const res = await api.get(`${apiBase}/marketplace/listings/${id}`);
return pg.listingUpsert(res.data, want);
} catch (e) {
handleApiError(e);
return null;
}
}
return {
user,
wantlist,
inventory,
listings,
listing,
};
}
-21
View File
@@ -1,21 +0,0 @@
type UUID = string;
type User = {
id: UUID,
discogs: string,
data: any,
}
type Listing = {
id: number,
want: UUID,
seller: number,
data: any,
}
type Want = {
id: UUID,
release: number,
user: UUID,
data: any,
}
-98
View File
@@ -1,98 +0,0 @@
import Knex from 'knex';
import config from '../knexfile';
export interface Pg {
userGetId: (id: UUID) => Promise<User>,
userGetName: (name: string) => Promise<User>,
userUpsert: (user: User) => Promise<User>,
userWants: (user: User) => Promise<Want[]>,
wantUpsert: (want: Want, user: User) => Promise<Want>,
listingUpsert: (listing: Listing, want: Want) => Promise<Listing>,
listingGet: (id: number) => Promise<Listing>,
}
export default function createPg(): Pg {
const pg = Knex(config);
async function userGetId(id): Promise<User> {
return pg('users')
.where({ id })
.first();
}
async function userGetName(name): Promise<User> {
return pg('users')
.where({ discogs: name })
.first();
}
async function userWants(user: User): Promise<Want[]> {
return pg('wants')
.where({ user: user.id });
}
async function userUpsert(user): Promise<User> {
const [row] = await pg('users')
.insert({
discogs: user.username,
data: user,
})
.onConflict('id')
.merge()
.onConflict('discogs')
.merge()
.returning('*');
return row;
}
async function wantUpsert(want, user): Promise<Want> {
const [row] = await pg('wants')
.insert({
data: want,
release: want.id,
user: user.id,
})
.onConflict('id')
.merge()
.onConflict(['user', 'release'])
.merge()
.returning('*');
return row;
}
async function listingUpsert(listing, want): Promise<Listing> {
const [row] = await pg('listings')
.insert({
id: listing.id,
want: want.id,
seller: listing.seller.id,
data: listing,
})
.onConflict('id')
.merge()
.returning('*');
return row;
}
async function listingGet(id): Promise<Listing> {
return pg('listings')
.where({ id })
.first();
}
return {
userGetId,
userGetName,
userUpsert,
userWants,
wantUpsert,
listingUpsert,
listingGet,
};
}