mirror of
https://github.com/burrow-dev/burrow-server.git
synced 2026-11-19 09:37:50 -05:00
feat(masterpieces): daily limit, a day's wait, the same picture approved again
- accounts under a day old cannot paint for a frame: the easel's token is refused and a message box says how long is left (AJ_MASTERPIECE_MIN_AGE, 86400; the tests use 0). - 10 new pictures an account in any 24 hours (config.masterpiecesPerDay), counted in the database (masterpiece_uploads) so a restart does not forget them; past it HQ says when the next one may go. a picture approved before is not a new one. - a picture a moderator or the admin approved is approved again as it is framed, and the same picture waiting is approved with it, its makers told: the same file (SHA-256), or the same painting saved and painted again, which comes back as other bytes. lib/jpegprint.js reads every 8x8 block's average from a baseline JPEG's DC coefficients (~2.6 ms for the Art Studio's 711x434); the same size with no block more than 2 of 255 apart is the same picture (a saved .ajart painted again: 1). - staff taking a painting back forgets its picture. - paintings from before get their hash and fingerprint a few at a time after start; those a person approved make their pictures approved ones. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
096ceafea7
commit
0c3bfd97b3
7 files changed
+460
-36
No files matched your search
+215
-21
@@ -7,6 +7,7 @@ const config = require("../config.js");
|
||||
const accounts = require("./accounts.js");
|
||||
const limits = require("./limits.js");
|
||||
const imageModeration = require("./image-moderation.js");
|
||||
const jpegPrint = require("./jpegprint.js");
|
||||
|
||||
// masterpieces: paintings from the Art Studio, framed and hung in a den
|
||||
// (client side: game.artStudioPainting.ArtStudioPainting, the same in
|
||||
@@ -45,6 +46,23 @@ const SCHEMA = `
|
||||
likes TEXT NOT NULL,
|
||||
image BLOB NOT NULL
|
||||
);
|
||||
-- every picture taken in, for the daily limit (pictures thrown away
|
||||
-- unframed count too)
|
||||
CREATE TABLE IF NOT EXISTS masterpiece_uploads (
|
||||
creator_id INTEGER NOT NULL,
|
||||
created_at INTEGER NOT NULL
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS masterpiece_uploads_by ON masterpiece_uploads (creator_id, created_at);
|
||||
-- pictures a moderator or the admin approved once: the same picture
|
||||
-- again, the same file (SHA-256) or the same painting saved and painted
|
||||
-- again (its fingerprint, lib/jpegprint.js), is approved as it is framed
|
||||
CREATE TABLE IF NOT EXISTS masterpiece_approved_pictures (
|
||||
hash TEXT PRIMARY KEY,
|
||||
approved_at INTEGER NOT NULL,
|
||||
approved_by TEXT NOT NULL,
|
||||
fingerprint BLOB,
|
||||
thumb BLOB
|
||||
);
|
||||
`;
|
||||
|
||||
const UPLOADED = 0; // posted, not made into an item yet
|
||||
@@ -79,19 +97,35 @@ const fileOf = id => path.join(folder(), `${id}.jpg`);
|
||||
const GALLERY_SPOTS = 12; // PartyManager.setupMasterpiecesInRoom
|
||||
|
||||
const uploadsPerHour = limits.limiter(30, 3600000);
|
||||
const DAY = 86400;
|
||||
const AUTO_APPROVER = "auto"; // reviewedBy of a painting approved for a picture approved before
|
||||
const now = () => Math.floor(Date.now() / 1000);
|
||||
const db = () => accounts.database();
|
||||
|
||||
const known = new Map(); // id -> {id, creator, createdAt, status, frame, reviewedAt, reviewedBy, likes: [account ids]}
|
||||
const known = new Map(); // id -> {id, creator, createdAt, status, frame, reviewedAt, reviewedBy, likes: [account ids], hash}
|
||||
const approvedPictures = new Map(); // hash -> thumbnail of its fingerprint, or null: masterpiece_approved_pictures
|
||||
const tokens = new Map(); // upload token -> {account id, expires (ms)}
|
||||
|
||||
const load = () => {
|
||||
db().exec(SCHEMA);
|
||||
// who approved it: a moderator's username, "admin", or null when nobody
|
||||
// looked (review was off, or it was approved before this was kept).
|
||||
if (!db().prepare("PRAGMA table_info(masterpieces)").all().some(column => column.name === "reviewed_by")) {
|
||||
const columns = db().prepare("PRAGMA table_info(masterpieces)").all().map(column => column.name);
|
||||
if (!columns.includes("reviewed_by")) {
|
||||
db().exec("ALTER TABLE masterpieces ADD COLUMN reviewed_by TEXT");
|
||||
}
|
||||
// the picture's SHA-256 (hashOf) and fingerprint (lib/jpegprint.js), to know it again
|
||||
if (!columns.includes("hash")) {
|
||||
db().exec("ALTER TABLE masterpieces ADD COLUMN hash TEXT");
|
||||
}
|
||||
if (!columns.includes("fingerprint")) {
|
||||
db().exec("ALTER TABLE masterpieces ADD COLUMN fingerprint BLOB");
|
||||
}
|
||||
const pictureColumns = db().prepare("PRAGMA table_info(masterpiece_approved_pictures)").all().map(column => column.name);
|
||||
if (!pictureColumns.includes("fingerprint")) {
|
||||
db().exec("ALTER TABLE masterpiece_approved_pictures ADD COLUMN fingerprint BLOB");
|
||||
db().exec("ALTER TABLE masterpiece_approved_pictures ADD COLUMN thumb BLOB");
|
||||
}
|
||||
fs.mkdirSync(folder(), {recursive: true});
|
||||
// pictures still in the table move out into files, once.
|
||||
for (const row of db().prepare("SELECT id FROM masterpieces WHERE length(image) > 0").all()) {
|
||||
@@ -99,16 +133,138 @@ const load = () => {
|
||||
fs.writeFileSync(fileOf(row.id), Buffer.from(picture));
|
||||
db().prepare("UPDATE masterpieces SET image = ? WHERE id = ?").run(NO_IMAGE, row.id);
|
||||
}
|
||||
for (const row of db().prepare("SELECT id, creator_id, created_at, status, frame, reviewed_at, reviewed_by, likes FROM masterpieces").all()) {
|
||||
for (const row of db().prepare("SELECT id, creator_id, created_at, status, frame, reviewed_at, reviewed_by, likes, hash FROM masterpieces").all()) {
|
||||
known.set(row.id, {id: row.id, creator: row.creator_id, createdAt: row.created_at, status: row.status, frame: row.frame,
|
||||
reviewedAt: row.reviewed_at, reviewedBy: row.reviewed_by, likes: JSON.parse(row.likes)});
|
||||
reviewedAt: row.reviewed_at, reviewedBy: row.reviewed_by, likes: JSON.parse(row.likes), hash: row.hash || null});
|
||||
}
|
||||
for (const row of db().prepare("SELECT hash, thumb FROM masterpiece_approved_pictures").all()) {
|
||||
approvedPictures.set(row.hash, row.thumb ? Buffer.from(row.thumb) : null);
|
||||
}
|
||||
backfill();
|
||||
};
|
||||
|
||||
const hashOf = image => crypto.createHash("sha256").update(image).digest("hex");
|
||||
|
||||
const printOf = painting => {
|
||||
const row = db().prepare("SELECT fingerprint FROM masterpieces WHERE id = ?").get(painting.id);
|
||||
return row && row.fingerprint ? Buffer.from(row.fingerprint) : null;
|
||||
};
|
||||
|
||||
// the approved pictures (their hashes) that this picture is: the same file,
|
||||
// or the same painting by its fingerprint.
|
||||
const approvedMatches = (hash, print) => {
|
||||
const found = approvedPictures.has(hash) ? [hash] : [];
|
||||
if (print) {
|
||||
const thumb = jpegPrint.thumbnail(print);
|
||||
for (const [other, otherThumb] of approvedPictures) {
|
||||
if (other !== hash && otherThumb && jpegPrint.thumbnailsClose(thumb, otherThumb)) {
|
||||
const row = db().prepare("SELECT fingerprint FROM masterpiece_approved_pictures WHERE hash = ?").get(other);
|
||||
if (row && row.fingerprint && jpegPrint.samePicture(print, Buffer.from(row.fingerprint))) {
|
||||
found.push(other);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return found;
|
||||
};
|
||||
|
||||
const pictureApproved = (hash, print) => Boolean(hash) && approvedMatches(hash, print).length > 0;
|
||||
|
||||
const notePictureApproved = (painting, print) => {
|
||||
const thumb = print ? jpegPrint.thumbnail(print) : null;
|
||||
db().prepare("INSERT OR IGNORE INTO masterpiece_approved_pictures (hash, approved_at, approved_by, fingerprint, thumb) VALUES (?, ?, ?, ?, ?)")
|
||||
.run(painting.hash, painting.reviewedAt || now(), painting.reviewedBy, print, thumb);
|
||||
if (!approvedPictures.has(painting.hash)) {
|
||||
approvedPictures.set(painting.hash, thumb);
|
||||
}
|
||||
};
|
||||
|
||||
// paintings from before pictures were known again get their hash and
|
||||
// fingerprint, a few at a time after start (each is a file read and some
|
||||
// milliseconds of decoding, on the thread the game shares); those a person
|
||||
// approved make their pictures approved ones.
|
||||
const BACKFILL_BATCH = 20;
|
||||
const backfill = () => {
|
||||
const missing = db().prepare("SELECT id FROM masterpieces WHERE fingerprint IS NULL OR hash IS NULL").all().map(row => row.id);
|
||||
const notedAgain = db().prepare("SELECT hash FROM masterpiece_approved_pictures WHERE fingerprint IS NULL").all().map(row => row.hash);
|
||||
const step = () => {
|
||||
for (const id of missing.splice(0, BACKFILL_BATCH)) {
|
||||
const painting = known.get(id);
|
||||
let image = null;
|
||||
try {
|
||||
image = painting ? fs.readFileSync(fileOf(id)) : null;
|
||||
}
|
||||
catch (err) {
|
||||
image = null;
|
||||
}
|
||||
if (!image) {
|
||||
continue;
|
||||
}
|
||||
painting.hash = hashOf(image);
|
||||
const print = jpegPrint.fingerprint(image);
|
||||
db().prepare("UPDATE masterpieces SET hash = ?, fingerprint = ? WHERE id = ?").run(painting.hash, print, id);
|
||||
if (painting.status === APPROVED && painting.reviewedBy && painting.reviewedBy !== AUTO_APPROVER) {
|
||||
notePictureApproved(painting, print);
|
||||
}
|
||||
}
|
||||
// pictures approved before fingerprints were kept: from a painting of
|
||||
// theirs, once every painting has its own
|
||||
for (const hash of missing.length ? [] : notedAgain.splice(0, BACKFILL_BATCH)) {
|
||||
const row = db().prepare("SELECT fingerprint FROM masterpieces WHERE hash = ? AND fingerprint IS NOT NULL").get(hash);
|
||||
if (row) {
|
||||
const print = Buffer.from(row.fingerprint);
|
||||
const thumb = jpegPrint.thumbnail(print);
|
||||
db().prepare("UPDATE masterpiece_approved_pictures SET fingerprint = ?, thumb = ? WHERE hash = ?").run(print, thumb, hash);
|
||||
approvedPictures.set(hash, thumb);
|
||||
}
|
||||
}
|
||||
if (missing.length || notedAgain.length) {
|
||||
setTimeout(step, 200).unref();
|
||||
}
|
||||
};
|
||||
if (missing.length || notedAgain.length) {
|
||||
setTimeout(step, 1000).unref();
|
||||
}
|
||||
};
|
||||
|
||||
// why this account may not paint for a frame now, as words for the player,
|
||||
// or null when it may: too new an account (asked at the easel, step 1), or
|
||||
// as many new pictures in the last 24 hours as a day allows (asked of a
|
||||
// picture not approved before, step 2). `whenFree` speaks of the wait.
|
||||
const whenFree = seconds => {
|
||||
const hours = Math.ceil(seconds / 3600);
|
||||
return hours > 1 ? `in ${hours} hours` : `in ${Math.max(1, Math.ceil(seconds / 60))} minutes`;
|
||||
};
|
||||
const tooNew = account => {
|
||||
const age = now() - (Number(account.createdAt) || 0);
|
||||
if (age < config.masterpieceMinAccountAge) {
|
||||
return `Accounts can make Masterpieces once they are a day old. Try again ${whenFree(config.masterpieceMinAccountAge - age)}.`;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
const tooMany = account => {
|
||||
const recent = db().prepare("SELECT created_at FROM masterpiece_uploads WHERE creator_id = ? AND created_at > ? ORDER BY created_at")
|
||||
.all(account.id, now() - DAY);
|
||||
const perDay = config.masterpiecesPerDay;
|
||||
if (recent.length >= perDay) {
|
||||
return `You can make ${perDay} new Masterpieces a day. Try again ${whenFree(recent[recent.length - perDay].created_at + DAY - now())}.`;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
const needsReview = () => accounts.setting(REVIEW_SETTING) !== "0";
|
||||
const setNeedsReview = on => accounts.setSetting(REVIEW_SETTING, on ? "1" : "0");
|
||||
|
||||
const forget = id => {
|
||||
// `unapprove`: it was taken back by staff, so its picture is no longer one
|
||||
// approved before (a painting thrown away unframed leaves it alone).
|
||||
const forget = (id, unapprove = false) => {
|
||||
const painting = known.get(id);
|
||||
if (unapprove && painting && painting.hash) {
|
||||
for (const hash of approvedMatches(painting.hash, printOf(painting))) {
|
||||
db().prepare("DELETE FROM masterpiece_approved_pictures WHERE hash = ?").run(hash);
|
||||
approvedPictures.delete(hash);
|
||||
}
|
||||
}
|
||||
known.delete(id);
|
||||
db().prepare("DELETE FROM masterpieces WHERE id = ?").run(id);
|
||||
fs.rmSync(fileOf(id), {force: true});
|
||||
@@ -126,6 +282,7 @@ const sweep = () => {
|
||||
forget(painting.id);
|
||||
}
|
||||
}
|
||||
db().prepare("DELETE FROM masterpiece_uploads WHERE created_at <= ?").run(now() - DAY);
|
||||
};
|
||||
setInterval(() => {
|
||||
try {
|
||||
@@ -136,8 +293,12 @@ setInterval(() => {
|
||||
}
|
||||
}, 600000).unref();
|
||||
|
||||
// A token for one upload by this account (step 1).
|
||||
// A token for one upload by this account (step 1), or null when it is
|
||||
// `tooNew`, which says why.
|
||||
const issueToken = account => {
|
||||
if (tooNew(account)) {
|
||||
return null;
|
||||
}
|
||||
for (const [token, entry] of tokens) {
|
||||
if (entry.account === account.id) {
|
||||
tokens.delete(token);
|
||||
@@ -189,20 +350,33 @@ const looksLikeJpeg = image => {
|
||||
return false;
|
||||
};
|
||||
|
||||
// step 2. resolves to the new painting's id, or null when the token is not
|
||||
// good, the picture is not a JPEG of a sane size, or the account has posted
|
||||
// too many.
|
||||
// step 2. resolves to {uuid}, the new painting's id, or {uuid: null} when
|
||||
// the token is not good, the picture is not a JPEG of a sane size, or the
|
||||
// account has posted too many; with `refusal` and `maker` when the player
|
||||
// should be told why.
|
||||
const upload = async (token, image) => {
|
||||
const entry = typeof token === "string" ? tokens.get(token) : null;
|
||||
tokens.delete(token);
|
||||
if (!entry || entry.expires < Date.now() || !Buffer.isBuffer(image) || image.length > MAX_IMAGE_BYTES || !looksLikeJpeg(image)
|
||||
|| !uploadsPerHour.allow(entry.account)) {
|
||||
return null;
|
||||
return {uuid: null};
|
||||
}
|
||||
// a picture approved before is not a new one and does not count
|
||||
const hash = hashOf(image);
|
||||
const print = jpegPrint.fingerprint(image);
|
||||
const approvedBefore = pictureApproved(hash, print);
|
||||
const maker = accounts.byId(entry.account);
|
||||
const refusal = maker && !approvedBefore ? tooNew(maker) || tooMany(maker) : null;
|
||||
if (!maker || refusal) {
|
||||
return {uuid: null, refusal, maker};
|
||||
}
|
||||
const safety = await imageModeration.scan(image);
|
||||
if (safety.blocked) {
|
||||
console.log(`[masterpieces] rejected unsafe upload from account ${entry.account}`);
|
||||
return null;
|
||||
return {uuid: null};
|
||||
}
|
||||
if (!approvedBefore) {
|
||||
db().prepare("INSERT INTO masterpiece_uploads (creator_id, created_at) VALUES (?, ?)").run(entry.account, now());
|
||||
}
|
||||
// one painting at a time is between the easel and the frame.
|
||||
for (const painting of [...known.values()]) {
|
||||
@@ -210,12 +384,12 @@ const upload = async (token, image) => {
|
||||
forget(painting.id);
|
||||
}
|
||||
}
|
||||
const painting = {id: crypto.randomUUID(), creator: entry.account, createdAt: now(), status: UPLOADED, frame: 0, reviewedAt: null, reviewedBy: null, likes: []};
|
||||
const painting = {id: crypto.randomUUID(), creator: entry.account, createdAt: now(), status: UPLOADED, frame: 0, reviewedAt: null, reviewedBy: null, likes: [], hash};
|
||||
fs.writeFileSync(fileOf(painting.id), image);
|
||||
db().prepare("INSERT INTO masterpieces (id, creator_id, created_at, status, frame, likes, image) VALUES (?, ?, ?, ?, ?, '[]', ?)")
|
||||
.run(painting.id, painting.creator, painting.createdAt, painting.status, painting.frame, NO_IMAGE);
|
||||
db().prepare("INSERT INTO masterpieces (id, creator_id, created_at, status, frame, likes, image, hash, fingerprint) VALUES (?, ?, ?, ?, ?, '[]', ?, ?, ?)")
|
||||
.run(painting.id, painting.creator, painting.createdAt, painting.status, painting.frame, NO_IMAGE, hash, print);
|
||||
known.set(painting.id, painting);
|
||||
return painting.id;
|
||||
return {uuid: painting.id};
|
||||
};
|
||||
|
||||
// `by`: who approved it, when someone did.
|
||||
@@ -239,11 +413,19 @@ const claimable = (account, id, frame) => {
|
||||
};
|
||||
|
||||
// step 3, paid for: the painting is an item now. true when it is approved
|
||||
// on the spot.
|
||||
// on the spot: review is off, or its picture was approved before.
|
||||
const claim = (id, frame) => {
|
||||
const painting = known.get(String(id));
|
||||
painting.frame = frame;
|
||||
setStatus(painting, needsReview() ? PENDING : APPROVED);
|
||||
if (!needsReview()) {
|
||||
setStatus(painting, APPROVED);
|
||||
}
|
||||
else if (pictureApproved(painting.hash, printOf(painting))) {
|
||||
setStatus(painting, APPROVED, AUTO_APPROVER);
|
||||
}
|
||||
else {
|
||||
setStatus(painting, PENDING);
|
||||
}
|
||||
return painting.status === APPROVED;
|
||||
};
|
||||
|
||||
@@ -278,14 +460,26 @@ const mayView = (id, address, addressOf) => {
|
||||
return painting.status === APPROVED || Boolean(maker && addressOf(maker) && addressOf(maker) === address);
|
||||
};
|
||||
|
||||
// `by`: the moderator's username, or "admin".
|
||||
// `by`: the moderator's username, or "admin". its picture is approved from
|
||||
// now on, and the same picture waiting in other paintings is approved with
|
||||
// it. the paintings approved, none when `id` was not waiting.
|
||||
const approve = (id, by) => {
|
||||
const painting = get(id);
|
||||
if (!painting || painting.status !== PENDING) {
|
||||
return false;
|
||||
return [];
|
||||
}
|
||||
setStatus(painting, APPROVED, by);
|
||||
return true;
|
||||
if (!painting.hash) {
|
||||
return [painting];
|
||||
}
|
||||
const print = printOf(painting);
|
||||
notePictureApproved(painting, print);
|
||||
const twins = [...known.values()].filter(other => other.status === PENDING
|
||||
&& (other.hash === painting.hash || (print && jpegPrint.samePicture(print, printOf(other)))));
|
||||
for (const twin of twins) {
|
||||
setStatus(twin, APPROVED, AUTO_APPROVER);
|
||||
}
|
||||
return [painting, ...twins];
|
||||
};
|
||||
|
||||
// is a den item fit to be shown, traded and recycled? anything that is not
|
||||
@@ -362,7 +556,7 @@ const frameList = listId =>
|
||||
([LIST_FRAMES_OFFERED, LIST_FRAMES_IN_ORDER].includes(Number(listId)) ? FRAMES.flat() : null);
|
||||
|
||||
module.exports = {
|
||||
load, issueToken, upload, claimable, claim, get, image, mayView, approve, forget, itemApproved, itemFields, creatorOf, like, gallery, list, listsFor, recentlyApproved, frameList,
|
||||
load, issueToken, tooNew, upload, claimable, claim, get, image, mayView, approve, forget, itemApproved, itemFields, creatorOf, like, gallery, list, listsFor, recentlyApproved, frameList,
|
||||
needsReview, setNeedsReview, madeBy,
|
||||
PENDING, APPROVED, DEN_ITEM, TOKEN_DEN_ITEM, DIAMOND_DEF, MAX_IMAGE_BYTES,
|
||||
};
|
||||
Reference in new issue
Block a user