feat: recorded import grants, unclaimed creators and claiming (#15)
Co-authored-by: dhkatz <[email protected]>
This commit was merged in pull request #15.
This commit is contained in:
1 parent
e6a97be894
commit
8f6dacba10
18 files changed
+5222
-26
No files matched your search
+154
-5
@@ -3,11 +3,11 @@
|
||||
// registers the revisions as an ordinary product owned by the creator. The routes are in app.ts.
|
||||
import { createHash, timingSafeEqual } from "node:crypto";
|
||||
import { and, asc, desc, eq, inArray, lt, ne, sql, schema, type Database, type FileEntry } from "@t5de/db";
|
||||
import { ImvuUnavailable, type ImvuClient } from "./imvu";
|
||||
import { ImvuUnavailable, type ImvuClient, type ImvuUser } from "./imvu";
|
||||
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 } =
|
||||
const { catalog, catalogChange, fileBlock, hosting, imvuLink, imvuProductInfo, importGrant, importJob, importRequest, library, product, productRevision, user } =
|
||||
schema;
|
||||
|
||||
export type Grant = typeof importGrant.$inferSelect;
|
||||
@@ -50,6 +50,12 @@ const productIDs = (v: unknown): number[] | undefined => {
|
||||
return [...out].sort((a, b) => a - b);
|
||||
};
|
||||
|
||||
/** A grant's IMVU account's name: its link's, or its unclaimed creator's. */
|
||||
const imvuNameOf = sql<string | null>`coalesce(${imvuLink.username}, (select u.display_username from "user" u where u.unclaimed_imvu_id = ${importGrant.imvuId}))`;
|
||||
|
||||
/** An unclaimed creator's username: reserved (people can't sign up with the prefix). */
|
||||
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. */
|
||||
export function covers(g: Grant, imvuProduct: number, creatorId: number): boolean {
|
||||
if (g.status !== "active" || creatorId !== g.imvuId) return false;
|
||||
@@ -191,6 +197,141 @@ export class Imports {
|
||||
return g!;
|
||||
}
|
||||
|
||||
// ---- recorded grants and unclaimed creators (docs/imports.md "Unclaimed creators and claiming") ----
|
||||
|
||||
/** The unclaimed creator standing in for an IMVU account, made if there's none yet. */
|
||||
private async placeholderFor(imvu: ImvuUser): Promise<string> {
|
||||
const [known] = await this.db.select({ id: user.id }).from(user).where(eq(user.unclaimedImvuId, imvu.id));
|
||||
if (known) return known.id;
|
||||
const id = crypto.randomUUID().replaceAll("-", "");
|
||||
await this.db.insert(user).values({
|
||||
id,
|
||||
name: `${imvu.username}, on IMVU`,
|
||||
// Never delivered (.invalid); there's no password, so nobody signs in as it.
|
||||
email: `imvu-${imvu.id}@unclaimed.invalid`,
|
||||
username: unclaimedUsername(imvu.id),
|
||||
displayUsername: imvu.username,
|
||||
unclaimedImvuId: imvu.id,
|
||||
});
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* An admin records a creator's consent (they won't make an account): a grant for an unclaimed creator standing in
|
||||
* for their IMVU account. Pending until the catalog's team accepts, like any grant.
|
||||
*/
|
||||
async recordGrant(
|
||||
byId: string,
|
||||
imvu: ImvuUser,
|
||||
input: { catalog: string; scope: string; products?: unknown; exclude?: unknown; listed?: boolean; evidence: string },
|
||||
): Promise<Grant> {
|
||||
const evidence = input.evidence.trim();
|
||||
if (!evidence) throw new ProductError(400, "invalid", "Say how they agreed (where and when).");
|
||||
const [link] = await this.db.select().from(imvuLink).where(eq(imvuLink.imvuId, imvu.id));
|
||||
if (link)
|
||||
throw new ProductError(409, "linked", `${imvu.username} is on T5DE: they bring their products themselves, from their account.`);
|
||||
const [c] = await this.db.select().from(catalog).where(eq(catalog.id, input.catalog));
|
||||
if (!c || c.status !== "approved" || c.importer === "off") throw new ProductError(404, "not_found", "That catalog doesn't take imports.");
|
||||
const scope = this.checkScope(input.scope, input.products, input.exclude);
|
||||
const [open] = await this.db
|
||||
.select({ id: importGrant.id })
|
||||
.from(importGrant)
|
||||
.where(and(eq(importGrant.catalogId, c.id), eq(importGrant.imvuId, imvu.id), inArray(importGrant.status, ["pending", "active", "suspended"])));
|
||||
if (open) throw new ProductError(409, "exists", "That IMVU account already has a grant with this catalog. Change that one instead.");
|
||||
const creatorId = await this.placeholderFor(imvu);
|
||||
const [g] = await this.db
|
||||
.insert(importGrant)
|
||||
.values({
|
||||
id: newProductID(),
|
||||
catalogId: c.id,
|
||||
creatorId,
|
||||
imvuId: imvu.id,
|
||||
...scope,
|
||||
listed: input.listed !== false,
|
||||
consentVersion: consent.version,
|
||||
consentKind: "recorded",
|
||||
evidence: evidence.slice(0, 1000),
|
||||
recordedBy: byId,
|
||||
})
|
||||
.returning();
|
||||
return g!;
|
||||
}
|
||||
|
||||
/** Grants admins recorded, newest first: their catalog, IMVU name, who recorded them, and whether claimed. */
|
||||
async recordedGrants() {
|
||||
const recorder = sql<string | null>`(select u.username from "user" u where u.id = ${importGrant.recordedBy})`;
|
||||
return this.db
|
||||
.select({
|
||||
grant: importGrant,
|
||||
catalogName: catalog.name,
|
||||
imvuName: imvuNameOf,
|
||||
recordedByName: recorder,
|
||||
claimed: sql<boolean>`not exists (select 1 from "user" u where u.id = ${importGrant.creatorId} and u.unclaimed_imvu_id is not null)`,
|
||||
})
|
||||
.from(importGrant)
|
||||
.innerJoin(catalog, eq(catalog.id, importGrant.catalogId))
|
||||
.leftJoin(imvuLink, eq(imvuLink.imvuId, importGrant.imvuId))
|
||||
.where(eq(importGrant.consentKind, "recorded"))
|
||||
.orderBy(desc(importGrant.createdAt));
|
||||
}
|
||||
|
||||
/** Whether a grant's creator is still an unclaimed placeholder. */
|
||||
async unclaimed(g: Grant): Promise<boolean> {
|
||||
const [u] = await this.db.select({ imvu: user.unclaimedImvuId }).from(user).where(eq(user.id, g.creatorId));
|
||||
return u?.imvu != null;
|
||||
}
|
||||
|
||||
/** The code for a recorded grant's proof (made once): the creator puts it in their IMVU tagline. */
|
||||
async proofCode(id: string, code: string): Promise<string> {
|
||||
const g = await this.grant(id);
|
||||
if (g.consentKind !== "recorded") throw new ProductError(409, "not_recorded", "Only recorded grants need proof.");
|
||||
if (g.proofCode) return g.proofCode;
|
||||
await this.db.update(importGrant).set({ proofCode: code }).where(eq(importGrant.id, g.id));
|
||||
return code;
|
||||
}
|
||||
|
||||
/** Checks a recorded grant's proof against the IMVU account's tagline. */
|
||||
async checkProof(id: string, tagline: string): Promise<Grant> {
|
||||
const g = await this.grant(id);
|
||||
if (!g.proofCode) throw new ProductError(409, "no_code", "Make a code first, and send it to the creator.");
|
||||
if (!tagline.includes(g.proofCode))
|
||||
throw new ProductError(409, "not_found", `The code isn't in that IMVU account's tagline yet. It must say ${g.proofCode}.`);
|
||||
const [updated] = await this.db.update(importGrant).set({ proofCheckedAt: new Date() }).where(eq(importGrant.id, g.id)).returning();
|
||||
return updated!;
|
||||
}
|
||||
|
||||
/** What a person could claim: unclaimed creators for IMVU accounts they've linked, with their products and grants. */
|
||||
async claimable(personId: string) {
|
||||
return this.db
|
||||
.select({
|
||||
imvuId: user.unclaimedImvuId,
|
||||
username: user.displayUsername,
|
||||
products: sql<number>`(select count(*)::int from ${product} p where p.creator_id = ${user.id} and p.removed_at is null)`,
|
||||
grants: sql<number>`(select count(*)::int from ${importGrant} g where g.creator_id = ${user.id} and g.status in ('pending', 'active', 'suspended'))`,
|
||||
})
|
||||
.from(user)
|
||||
.innerJoin(imvuLink, eq(imvuLink.imvuId, user.unclaimedImvuId))
|
||||
.where(eq(imvuLink.userId, personId));
|
||||
}
|
||||
|
||||
/**
|
||||
* Someone who linked an IMVU account claims its unclaimed creator: its products and grants become theirs, and the
|
||||
* placeholder is deleted.
|
||||
*/
|
||||
async claimCreator(personId: string, imvuId: number): Promise<{ products: number; grants: number }> {
|
||||
const [link] = await this.db.select().from(imvuLink).where(eq(imvuLink.imvuId, imvuId));
|
||||
if (!link || link.userId !== personId) throw new ProductError(403, "not_linked", "Link that IMVU account first.");
|
||||
const [placeholder] = await this.db.select().from(user).where(eq(user.unclaimedImvuId, imvuId));
|
||||
if (!placeholder) throw new ProductError(404, "not_found", "Nothing waits for that IMVU account.");
|
||||
return this.db.transaction(async (tx) => {
|
||||
const products = await tx.update(product).set({ creatorId: personId }).where(eq(product.creatorId, placeholder.id)).returning({ id: product.id });
|
||||
await tx.update(productRevision).set({ uploadedBy: personId }).where(eq(productRevision.uploadedBy, placeholder.id));
|
||||
const grants = await tx.update(importGrant).set({ creatorId: personId }).where(eq(importGrant.creatorId, placeholder.id)).returning({ id: importGrant.id });
|
||||
await tx.delete(user).where(eq(user.id, placeholder.id));
|
||||
return { products: products.length, grants: grants.length };
|
||||
});
|
||||
}
|
||||
|
||||
private checkScope(scope: string, products: unknown, exclude: unknown) {
|
||||
if (scope !== "shop" && scope !== "products") throw new ProductError(400, "invalid", "Choose the whole shop or listed products.");
|
||||
const p = productIDs(products);
|
||||
@@ -203,7 +344,7 @@ export class Imports {
|
||||
/** A person's grants (as the creator), newest first, with their catalogs' names. */
|
||||
async grantsOf(personId: string) {
|
||||
return this.db
|
||||
.select({ grant: importGrant, catalogName: catalog.name, imvuName: imvuLink.username })
|
||||
.select({ grant: importGrant, catalogName: catalog.name, imvuName: imvuNameOf })
|
||||
.from(importGrant)
|
||||
.innerJoin(catalog, eq(catalog.id, importGrant.catalogId))
|
||||
.leftJoin(imvuLink, eq(imvuLink.imvuId, importGrant.imvuId))
|
||||
@@ -214,7 +355,7 @@ export class Imports {
|
||||
/** A catalog's grants, pending first. */
|
||||
async grantsFor(catalogId: string) {
|
||||
return this.db
|
||||
.select({ grant: importGrant, catalogName: catalog.name, imvuName: imvuLink.username })
|
||||
.select({ grant: importGrant, catalogName: catalog.name, imvuName: imvuNameOf })
|
||||
.from(importGrant)
|
||||
.innerJoin(catalog, eq(catalog.id, importGrant.catalogId))
|
||||
.leftJoin(imvuLink, eq(imvuLink.imvuId, importGrant.imvuId))
|
||||
@@ -272,7 +413,14 @@ export class Imports {
|
||||
const gs = await this.db
|
||||
.update(importGrant)
|
||||
.set({ status: "suspended", statusReason: reason })
|
||||
.where(and(eq(importGrant.imvuId, imvuId), inArray(importGrant.status, ["pending", "active"])))
|
||||
.where(
|
||||
and(
|
||||
eq(importGrant.imvuId, imvuId),
|
||||
inArray(importGrant.status, ["pending", "active"]),
|
||||
// An unclaimed creator's grants stand on the consent an admin recorded, whoever links the account.
|
||||
sql`not exists (select 1 from "user" u where u.id = ${importGrant.creatorId} and u.unclaimed_imvu_id is not null)`,
|
||||
),
|
||||
)
|
||||
.returning();
|
||||
for (const g of gs) {
|
||||
await this.db.update(importJob).set({ status: "failed", reason }).where(and(eq(importJob.grantId, g.id), inArray(importJob.status, ["queued", "claimed"])));
|
||||
@@ -407,6 +555,7 @@ export class Imports {
|
||||
return this.db
|
||||
.select({
|
||||
imvuCreator: importRequest.imvuCreator,
|
||||
name: sql<string>`coalesce((select max(i.creator_name) from ${imvuProductInfo} i where i.creator_id = ${importRequest.imvuCreator}), '')`,
|
||||
people: sql<number>`count(distinct ${importRequest.userId})::int`,
|
||||
products: sql<number>`count(distinct ${importRequest.imvuProduct}) filter (where ${importRequest.imvuProduct} <> 0)::int`,
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user