feat(imports): Access Pass products through the importer's signed-in account (#20)
Co-authored-by: dhkatz <[email protected]>
This commit was merged in pull request #20.
This commit is contained in:
1 parent
56d25f7507
commit
cc6959082c
12 files changed
+4871
-51
No files matched your search
+151
-24
@@ -7,8 +7,22 @@ import { ImvuUnavailable, NoSuchImvuUser, type ImvuClient, type ImvuUser } from
|
||||
import type { Signer } from "./keys";
|
||||
import { BlockedUpload, checkFiles, newProductID, ProductError, type Catalog } from "./products";
|
||||
|
||||
const { catalog, catalogChange, fileBlock, hosting, imvuLink, imvuProductInfo, importGrant, importJob, importRequest, library, product, productRevision, user } =
|
||||
schema;
|
||||
const {
|
||||
catalog,
|
||||
catalogChange,
|
||||
fileBlock,
|
||||
hosting,
|
||||
imvuLink,
|
||||
imvuProductInfo,
|
||||
importerAccount,
|
||||
importGrant,
|
||||
importJob,
|
||||
importRequest,
|
||||
library,
|
||||
product,
|
||||
productRevision,
|
||||
user,
|
||||
} = schema;
|
||||
|
||||
export type Grant = typeof importGrant.$inferSelect;
|
||||
export type Job = typeof importJob.$inferSelect;
|
||||
@@ -59,6 +73,23 @@ const imvuNameOf = sql<string | null>`coalesce(${imvuLink.username}, (select u.d
|
||||
export const unclaimedUsername = (imvuId: number) => `imvu_${imvuId}`;
|
||||
|
||||
/** Whether a grant covers an IMVU product (by its IMVU creator): active, the creator's, and in its scope. */
|
||||
/** A product as an importer's IMVU account sees it (docs/imports.md, "Access Pass products"). */
|
||||
export type SeenProduct = { id: number; name: string; rating: string; creatorId: number; creatorName: string; purchasable: boolean };
|
||||
|
||||
/** Checks what an importer says it saw: null if it isn't a product. */
|
||||
function checkSeen(p: SeenProduct): SeenProduct | null {
|
||||
if (!Number.isSafeInteger(p.id) || p.id <= 0 || !Number.isSafeInteger(p.creatorId) || p.creatorId <= 0) return null;
|
||||
if (p.rating !== "general" && p.rating !== "adult") return null;
|
||||
return {
|
||||
id: p.id,
|
||||
name: String(p.name ?? "").trim().slice(0, 200),
|
||||
rating: p.rating,
|
||||
creatorId: p.creatorId,
|
||||
creatorName: String(p.creatorName ?? "").trim().slice(0, 64),
|
||||
purchasable: p.purchasable !== false,
|
||||
};
|
||||
}
|
||||
|
||||
export function covers(g: Grant, imvuProduct: number, creatorId: number): boolean {
|
||||
if (g.status !== "active" || creatorId !== g.imvuId) return false;
|
||||
return g.scope === "shop" ? !g.exclude.includes(imvuProduct) : g.products.includes(imvuProduct);
|
||||
@@ -545,17 +576,24 @@ export class Imports {
|
||||
listed = []; // the account is gone from IMVU: nothing to list (rechecks withdraw what was imported)
|
||||
}
|
||||
const now = new Date();
|
||||
for (let i = 0; i < listed.length; i += 500) {
|
||||
const rows = listed.slice(i, i + 500).map((p) => ({
|
||||
id: p.id,
|
||||
found: true,
|
||||
name: p.name,
|
||||
creatorId: p.creatorId,
|
||||
creatorName: p.creatorName,
|
||||
rating: p.adult ? "adult" : "general",
|
||||
purchasable: p.purchasable,
|
||||
checkedAt: now,
|
||||
}));
|
||||
await this.keepSeen(
|
||||
listed.map((p) => ({ id: p.id, name: p.name, rating: p.adult ? "adult" : "general", creatorId: p.creatorId, creatorName: p.creatorName, purchasable: p.purchasable })),
|
||||
now,
|
||||
);
|
||||
const [updated] = await this.db
|
||||
.update(importGrant)
|
||||
.set({ shopListedAt: now, shopListed: listed.length, shopListedSignedIn: false })
|
||||
.where(eq(importGrant.id, g.id))
|
||||
.returning();
|
||||
const queued = updated?.status === "active" && updated.scope === "shop" ? await this.queueCovered(updated) : 0;
|
||||
if (updated) await this.queueListing(updated);
|
||||
return { listed: listed.length, queued };
|
||||
}
|
||||
|
||||
/** Keeps products' info as seen (by the hub's listing, or an importer's account), in batches. */
|
||||
private async keepSeen(seen: SeenProduct[], now: Date) {
|
||||
for (let i = 0; i < seen.length; i += 500) {
|
||||
const rows = seen.slice(i, i + 500).map((p) => ({ ...p, found: true, checkedAt: now }));
|
||||
await this.db
|
||||
.insert(imvuProductInfo)
|
||||
.values(rows)
|
||||
@@ -572,13 +610,97 @@ export class Imports {
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Queues a signed-in listing of a whole-shop grant's shop for its catalog's importer, if one of the importer's IMVU
|
||||
* accounts signed in with Access Pass: IMVU lists Access Pass products only to them.
|
||||
*/
|
||||
async queueListing(g: Grant): Promise<boolean> {
|
||||
if (g.status !== "active" || g.scope !== "shop") return false;
|
||||
const [ap] = await this.db
|
||||
.select({ id: importerAccount.id })
|
||||
.from(importerAccount)
|
||||
.where(and(eq(importerAccount.catalogId, g.catalogId), eq(importerAccount.state, "ok"), eq(importerAccount.accessPass, true)))
|
||||
.limit(1);
|
||||
if (!ap) return false;
|
||||
const [j] = await this.db
|
||||
.insert(importJob)
|
||||
.values({ id: newProductID(), catalogId: g.catalogId, grantId: g.id, imvuProduct: g.imvuId, kind: "list" })
|
||||
.onConflictDoNothing()
|
||||
.returning();
|
||||
return j !== undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* The importer lists a whole shop signed in (a "list" job): the products it saw are kept (only the grant's IMVU
|
||||
* account's: an importer can't vouch for another creator's), and those not imported yet are queued.
|
||||
*/
|
||||
async reportListing(c: Catalog, jobId: string, products: SeenProduct[]): Promise<{ listed: number; queued: number }> {
|
||||
const job = await this.claimedJob(c, jobId);
|
||||
if (job.kind !== "list") throw new ProductError(409, "not_list", "That job isn't a shop listing.");
|
||||
const g = await this.grant(job.grantId);
|
||||
const seen = new Map<number, SeenProduct>();
|
||||
for (const p of products) {
|
||||
const ok = checkSeen(p);
|
||||
if (ok && ok.creatorId === g.imvuId) seen.set(ok.id, ok);
|
||||
}
|
||||
const now = new Date();
|
||||
await this.keepSeen([...seen.values()], now);
|
||||
await this.db.update(importJob).set({ status: "done", reason: "", claimedAt: null }).where(eq(importJob.id, job.id));
|
||||
const [updated] = await this.db
|
||||
.update(importGrant)
|
||||
.set({ shopListedAt: now, shopListed: listed.length })
|
||||
.where(eq(importGrant.id, g.id))
|
||||
.set({ shopListedAt: now, shopListed: seen.size, shopListedSignedIn: true })
|
||||
.where(and(eq(importGrant.id, g.id), eq(importGrant.status, "active"), eq(importGrant.scope, "shop")))
|
||||
.returning();
|
||||
const queued = updated?.status === "active" && updated.scope === "shop" ? await this.queueCovered(updated) : 0;
|
||||
return { listed: listed.length, queued };
|
||||
const queued = updated ? await this.queueCovered(updated) : 0;
|
||||
return { listed: seen.size, queued };
|
||||
}
|
||||
|
||||
/**
|
||||
* The importer says what its IMVU account sees of a claimed job's product, which IMVU hides from the hub (Access
|
||||
* Pass products): kept, so the job can go on. What IMVU shows the hub publicly wins.
|
||||
*/
|
||||
async reportInfo(c: Catalog, jobId: string, seen: SeenProduct & { found: boolean }): Promise<{ rating: string | null; covered: boolean }> {
|
||||
const job = await this.claimedJob(c, jobId);
|
||||
if (job.kind === "list") throw new ProductError(409, "not_product", "That job is a shop listing.");
|
||||
const g = await this.grant(job.grantId);
|
||||
const answer = this.imvu ? await this.imvu.lookup(job.imvuProduct) : "hidden";
|
||||
let info: ImvuInfo | undefined;
|
||||
if (typeof answer !== "string") info = await this.info(job.imvuProduct, true);
|
||||
else if (!seen.found) {
|
||||
// Gone, as the importer's account sees it too: rechecks withdraw it.
|
||||
const now = new Date();
|
||||
await this.db
|
||||
.insert(imvuProductInfo)
|
||||
.values({ id: job.imvuProduct, found: false, checkedAt: now })
|
||||
.onConflictDoUpdate({ target: imvuProductInfo.id, set: { found: false, checkedAt: now } });
|
||||
} else {
|
||||
const ok = checkSeen({ ...seen, id: job.imvuProduct });
|
||||
if (!ok) throw new ProductError(400, "invalid", "That isn't a product's info.");
|
||||
await this.keepSeen([ok], new Date());
|
||||
[info] = await this.db.select().from(imvuProductInfo).where(eq(imvuProductInfo.id, job.imvuProduct));
|
||||
}
|
||||
return { rating: info?.rating ?? null, covered: !!info && covers(g, job.imvuProduct, info.creatorId) };
|
||||
}
|
||||
|
||||
/** Queues signed-in listings of a catalog's whole-shop grants (once its importer has an Access Pass account). */
|
||||
async queueListings(catalogId: string): Promise<number> {
|
||||
const grants = await this.db
|
||||
.select()
|
||||
.from(importGrant)
|
||||
.where(and(eq(importGrant.catalogId, catalogId), eq(importGrant.status, "active"), eq(importGrant.scope, "shop")));
|
||||
let n = 0;
|
||||
for (const g of grants) if (await this.queueListing(g)) n++;
|
||||
return n;
|
||||
}
|
||||
|
||||
/** A job of this catalog's that its importer has claimed. */
|
||||
private async claimedJob(c: Catalog, jobId: string): Promise<Job> {
|
||||
const [job] = await this.db.select().from(importJob).where(and(eq(importJob.id, jobId), eq(importJob.catalogId, c.id)));
|
||||
if (!job) throw new ProductError(404, "not_found", "No such job.");
|
||||
if (job.status !== "claimed") throw new ProductError(409, "not_claimed", "That job isn't claimed.");
|
||||
return job;
|
||||
}
|
||||
|
||||
/** Lists the whole-shop grants not listed in a day, never-listed ones first. */
|
||||
@@ -829,13 +951,17 @@ export class Imports {
|
||||
.from(imvuProductInfo)
|
||||
.where(and(inArray(imvuProductInfo.id, jobs.map((j) => j.imvuProduct)), eq(imvuProductInfo.found, true)))
|
||||
: [];
|
||||
return jobs.map((j) => ({
|
||||
id: j.id,
|
||||
kind: j.kind,
|
||||
imvuProduct: j.imvuProduct,
|
||||
lastRevision: last.find((l) => l.imvuProduct === j.imvuProduct)?.imvuRevision ?? 0,
|
||||
rating: infos.find((i) => i.id === j.imvuProduct)?.rating ?? null,
|
||||
}));
|
||||
return jobs.map((j) =>
|
||||
j.kind === "list"
|
||||
? { id: j.id, kind: j.kind, imvuProduct: 0, imvuCreator: j.imvuProduct, lastRevision: 0, rating: null }
|
||||
: {
|
||||
id: j.id,
|
||||
kind: j.kind,
|
||||
imvuProduct: j.imvuProduct,
|
||||
lastRevision: last.find((l) => l.imvuProduct === j.imvuProduct)?.imvuRevision ?? 0,
|
||||
rating: infos.find((i) => i.id === j.imvuProduct)?.rating ?? null,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/** A claimed job of this catalog's, whose grant still covers its product. */
|
||||
@@ -843,6 +969,7 @@ export class Imports {
|
||||
const [job] = await this.db.select().from(importJob).where(and(eq(importJob.id, jobId), eq(importJob.catalogId, c.id)));
|
||||
if (!job) throw new ProductError(404, "not_found", "No such job.");
|
||||
if (job.status !== "claimed") throw new ProductError(409, "not_claimed", "That job isn't claimed.");
|
||||
if (job.kind === "list") throw new ProductError(409, "not_product", "That job is a shop listing.");
|
||||
const g = await this.grant(job.grantId);
|
||||
const info = await this.info(job.imvuProduct);
|
||||
if (!info || !covers(g, job.imvuProduct, info.creatorId)) throw new ProductError(409, "not_covered", "The grant no longer covers this product.");
|
||||
|
||||
Reference in new issue
Block a user