mirror of
https://github.com/burrow-dev/burrow-server.git
synced 2026-11-18 17:27: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
@@ -330,6 +330,11 @@ module.exports = {
|
||||
// how often at most the packs with custom items in are made again after
|
||||
// one changes (lib/defpacks.js clientPack); the tests use 0, made at once.
|
||||
packRemakeMs: env.AJ_PACK_REMAKE_MS === undefined ? 15000 : Number(env.AJ_PACK_REMAKE_MS),
|
||||
// masterpieces (lib/masterpieces.js): seconds an account must have had
|
||||
// before it paints one (AJ_MASTERPIECE_MIN_AGE; the tests use 0), and new
|
||||
// pictures an account may post in any 24 hours.
|
||||
masterpieceMinAccountAge: env.AJ_MASTERPIECE_MIN_AGE === undefined ? 86400 : Number(env.AJ_MASTERPIECE_MIN_AGE),
|
||||
masterpiecesPerDay: 10,
|
||||
maxConnections: 2000,
|
||||
maxHttpConnections: 2000, // open at once, in all
|
||||
maxHttpConnectionsPerAddress: 40, // the game opens a good many at once while it loads
|
||||
|
||||
+5
-1
@@ -1060,7 +1060,11 @@ const handle = async (req, res) => {
|
||||
return;
|
||||
}
|
||||
const parts = formParts(req.headers["content-type"], await readBody(req, masterpieces.MAX_IMAGE_BYTES + 16 * 1024));
|
||||
const uuid = await masterpieces.upload(String(parts.get("authToken") || ""), parts.get("image"));
|
||||
const {uuid, refusal, maker} = await masterpieces.upload(String(parts.get("authToken") || ""), parts.get("image"));
|
||||
// the game only says it could not be sent: HQ says why
|
||||
if (refusal && maker) {
|
||||
world.tellAccount(maker, refusal);
|
||||
}
|
||||
send(res, 200, uuid ? {success: true, uuid} : {success: false});
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,212 @@
|
||||
"use strict";
|
||||
|
||||
// a picture's fingerprint, to know a masterpiece again when it comes back
|
||||
// re-encoded (lib/masterpieces.js): the Art Studio paints a saved .ajart
|
||||
// again a little differently, so the JPEG's bytes differ while every 8 by 8
|
||||
// block's average stays within a fraction of a step.
|
||||
//
|
||||
// the fingerprint is the average of each block, per component, read from
|
||||
// the JPEG's DC coefficients alone (no inverse DCT): a baseline JPEG (what
|
||||
// the Art Studio writes) with no subsampling and no restart markers. any
|
||||
// other JPEG has none, and only its exact bytes are known again.
|
||||
//
|
||||
// layout: [width (uint16), height (uint16), components (uint8)], then a byte
|
||||
// per block and component, block by block in rows.
|
||||
|
||||
const HEADER = 5;
|
||||
const SAME_WITHIN = 2; // of 255, per block: a mark of a few pixels moves its block by more
|
||||
const THUMB = 16; // the thumbnail's side, for finding candidates quickly
|
||||
|
||||
// a Huffman table as JPEG decoders keep it: per code length the smallest
|
||||
// and largest code and where its values start.
|
||||
const huffmanTable = (counts, values) => {
|
||||
const minCode = new Int32Array(17);
|
||||
const maxCode = new Int32Array(18).fill(-1);
|
||||
const valPtr = new Int32Array(17);
|
||||
let code = 0;
|
||||
let k = 0;
|
||||
for (let length = 1; length <= 16; length++) {
|
||||
valPtr[length] = k;
|
||||
minCode[length] = code;
|
||||
code += counts[length - 1];
|
||||
k += counts[length - 1];
|
||||
maxCode[length] = counts[length - 1] ? code - 1 : -1;
|
||||
code <<= 1;
|
||||
}
|
||||
maxCode[17] = 0x7FFFFFFF;
|
||||
return {minCode, maxCode, valPtr, values};
|
||||
};
|
||||
|
||||
// the fingerprint of a JPEG, or null.
|
||||
const fingerprint = buf => {
|
||||
try {
|
||||
return read(buf);
|
||||
}
|
||||
catch (err) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
const read = buf => {
|
||||
const tables = {};
|
||||
const quant = {};
|
||||
let frame = null;
|
||||
let scan = null;
|
||||
let at = 2;
|
||||
while (!scan) {
|
||||
if (at + 4 > buf.length || buf[at] !== 0xFF) {
|
||||
return null;
|
||||
}
|
||||
const marker = buf[at + 1];
|
||||
const length = buf.readUInt16BE(at + 2);
|
||||
const body = buf.subarray(at + 4, at + 2 + length);
|
||||
if (marker === 0xDB) {
|
||||
for (let p = 0; p + 65 <= body.length; p += 65) {
|
||||
if (body[p] >> 4) {
|
||||
return null; // 16-bit tables: not the Art Studio's
|
||||
}
|
||||
quant[body[p] & 15] = body[p + 1];
|
||||
}
|
||||
}
|
||||
else if (marker === 0xC4) {
|
||||
for (let p = 0; p + 17 <= body.length;) {
|
||||
const counts = body.subarray(p + 1, p + 17);
|
||||
const total = counts.reduce((sum, count) => sum + count, 0);
|
||||
tables[(body[p] >> 4 ? "ac" : "dc") + (body[p] & 15)] = huffmanTable(counts, body.subarray(p + 17, p + 17 + total));
|
||||
p += 17 + total;
|
||||
}
|
||||
}
|
||||
else if (marker === 0xC0 || marker === 0xC1) {
|
||||
const comps = body[5];
|
||||
frame = {height: body.readUInt16BE(1), width: body.readUInt16BE(3), comps: []};
|
||||
for (let c = 0; c < comps; c++) {
|
||||
if (body[7 + c * 3] !== 0x11) {
|
||||
return null; // subsampled
|
||||
}
|
||||
frame.comps.push(body[8 + c * 3]);
|
||||
}
|
||||
}
|
||||
else if (marker === 0xDD || (marker >= 0xC2 && marker <= 0xCF && marker !== 0xC4 && marker !== 0xC8 && marker !== 0xCC)) {
|
||||
return null; // restart markers, progressive, lossless or arithmetic
|
||||
}
|
||||
else if (marker === 0xDA) {
|
||||
scan = [];
|
||||
for (let c = 0; c < body[0]; c++) {
|
||||
scan.push({dc: tables[`dc${body[2 + c * 2] >> 4}`], ac: tables[`ac${body[2 + c * 2] & 15}`]});
|
||||
}
|
||||
}
|
||||
at += 2 + length;
|
||||
}
|
||||
if (!frame || frame.width < 1 || frame.height < 1 || scan.length !== frame.comps.length || scan.length < 1 || scan.length > 3
|
||||
|| scan.some(table => !table.dc || !table.ac) || frame.comps.some(id => quant[id] === undefined)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// the entropy-coded data, its 0xFF bytes followed by a stuffed 0x00
|
||||
let pos = at;
|
||||
let bits = 0;
|
||||
let left = 0;
|
||||
const bit = () => {
|
||||
if (left === 0) {
|
||||
if (pos >= buf.length) {
|
||||
throw new Error("ran out");
|
||||
}
|
||||
bits = buf[pos++];
|
||||
if (bits === 0xFF) {
|
||||
if (buf[pos] !== 0) {
|
||||
throw new Error("marker in the data");
|
||||
}
|
||||
pos++;
|
||||
}
|
||||
left = 8;
|
||||
}
|
||||
left--;
|
||||
return (bits >> left) & 1;
|
||||
};
|
||||
const decode = table => {
|
||||
let code = bit();
|
||||
let length = 1;
|
||||
while (code > table.maxCode[length]) {
|
||||
code = (code << 1) | bit();
|
||||
if (++length > 16) {
|
||||
throw new Error("bad code");
|
||||
}
|
||||
}
|
||||
return table.values[table.valPtr[length] + code - table.minCode[length]];
|
||||
};
|
||||
const receive = size => {
|
||||
let value = 0;
|
||||
for (let i = 0; i < size; i++) {
|
||||
value = (value << 1) | bit();
|
||||
}
|
||||
return value < 1 << (size - 1) ? value - (1 << size) + 1 : value;
|
||||
};
|
||||
|
||||
const across = Math.ceil(frame.width / 8);
|
||||
const down = Math.ceil(frame.height / 8);
|
||||
const comps = scan.length;
|
||||
const out = Buffer.alloc(HEADER + across * down * comps);
|
||||
out.writeUInt16BE(frame.width, 0);
|
||||
out.writeUInt16BE(frame.height, 2);
|
||||
out[4] = comps;
|
||||
const predicted = new Int32Array(comps);
|
||||
for (let block = 0; block < across * down; block++) {
|
||||
for (let c = 0; c < comps; c++) {
|
||||
const size = decode(scan[c].dc);
|
||||
predicted[c] += size ? receive(size) : 0;
|
||||
// the DC coefficient is eight times the block's average, less 128
|
||||
const average = Math.round(predicted[c] * quant[frame.comps[c]] / 8 + 128);
|
||||
out[HEADER + block * comps + c] = Math.max(0, Math.min(255, average));
|
||||
for (let k = 1; k < 64;) {
|
||||
const runSize = decode(scan[c].ac);
|
||||
if ((runSize & 15) === 0) {
|
||||
if (runSize >> 4 !== 15) {
|
||||
break; // end of block
|
||||
}
|
||||
k += 16;
|
||||
continue;
|
||||
}
|
||||
k += runSize >> 4;
|
||||
receive(runSize & 15);
|
||||
k++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return out;
|
||||
};
|
||||
|
||||
// the first component (brightness) in THUMB by THUMB, to find candidates
|
||||
// among many fingerprints before comparing whole ones.
|
||||
const thumbnail = print => {
|
||||
const across = Math.ceil(print.readUInt16BE(0) / 8);
|
||||
const down = Math.ceil(print.readUInt16BE(2) / 8);
|
||||
const comps = print[4];
|
||||
const sums = new Float64Array(THUMB * THUMB);
|
||||
const counts = new Float64Array(THUMB * THUMB);
|
||||
for (let y = 0; y < down; y++) {
|
||||
for (let x = 0; x < across; x++) {
|
||||
const cell = Math.floor(y * THUMB / down) * THUMB + Math.floor(x * THUMB / across);
|
||||
sums[cell] += print[HEADER + (y * across + x) * comps];
|
||||
counts[cell]++;
|
||||
}
|
||||
}
|
||||
return Buffer.from(sums.map((sum, cell) => (counts[cell] ? Math.round(sum / counts[cell]) : 0)));
|
||||
};
|
||||
|
||||
// could these two be the same picture? (thumbnails: a cheap first look)
|
||||
const thumbnailsClose = (one, other) => one.length === other.length && one.every((value, i) => Math.abs(value - other[i]) <= SAME_WITHIN);
|
||||
|
||||
// the same picture: the same size, and no block further apart than a step or two.
|
||||
const samePicture = (one, other) => {
|
||||
if (!one || !other || one.length !== other.length || one.compare(other, 0, HEADER, 0, HEADER) !== 0) {
|
||||
return false;
|
||||
}
|
||||
for (let i = HEADER; i < one.length; i++) {
|
||||
if (Math.abs(one[i] - other[i]) > SAME_WITHIN) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
module.exports = {fingerprint, thumbnail, thumbnailsClose, samePicture};
|
||||
+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,
|
||||
};
|
||||
+8
-1
@@ -464,8 +464,15 @@ module.exports = ({tell, leaveRoom, findOnline, blocks, watchers}) => {
|
||||
},
|
||||
};
|
||||
|
||||
// refused to an account under a day old: the game shows its "could not
|
||||
// send" popup, and a plain message box ("xtl") says why.
|
||||
const uploadToken = (session, game, [, purpose], reply) => {
|
||||
reply("tkn", 1, 0, masterpieces.issueToken(session.account), 0, purpose === undefined ? 1 : Number(purpose) || 1);
|
||||
const token = masterpieces.issueToken(session.account);
|
||||
if (!token) {
|
||||
const why = masterpieces.tooNew(session.account) || "Your Masterpiece could not be sent. Try again later.";
|
||||
session.sendXt("xtl", -1, why.replace(/%/g, " percent"));
|
||||
}
|
||||
reply("tkn", token ? 1 : 0, 0, token || "", 0, purpose === undefined ? 1 : Number(purpose) || 1);
|
||||
};
|
||||
|
||||
// what a game asks of the server while it runs, by game class and then by
|
||||
|
||||
+13
-12
@@ -2013,20 +2013,21 @@ module.exports = {
|
||||
// the panel's Masterpieces page. A painting waiting for review is an
|
||||
// item its maker cannot use yet; approving it tells them so, by a
|
||||
// Jam-A-Gram from Animal Jam HQ and at once if they are playing. `by`
|
||||
// is who approved it: a moderator's username, or "admin".
|
||||
// is who approved it: a moderator's username, or "admin". the same
|
||||
// picture waiting elsewhere is approved with it, and its makers told too.
|
||||
approveMasterpiece(id, by) {
|
||||
const maker = masterpieces.creatorOf(id);
|
||||
if (!masterpieces.approve(id, by)) {
|
||||
return false;
|
||||
}
|
||||
if (maker) {
|
||||
ecards.noteFromHq(maker, "Your Masterpiece has been approved! It is with your den items.");
|
||||
const session = findOnline(maker.username);
|
||||
if (session) {
|
||||
dens.sendDenItems(session);
|
||||
const approved = masterpieces.approve(id, by);
|
||||
for (const painting of approved) {
|
||||
const maker = masterpieces.creatorOf(painting.id);
|
||||
if (maker) {
|
||||
ecards.noteFromHq(maker, "Your Masterpiece has been approved! It is with your den items.");
|
||||
const session = findOnline(maker.username);
|
||||
if (session) {
|
||||
dens.sendDenItems(session);
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
return approved.length > 0;
|
||||
},
|
||||
// takes a painting away for good, from whoever has it by now: one that
|
||||
// was not approved, or one that should not have been. its maker gets a
|
||||
@@ -2051,7 +2052,7 @@ module.exports = {
|
||||
account.denItems = account.denItems.filter(item => item.image !== painting.id);
|
||||
account.tradeList = account.tradeList.filter(listed => !(listed.type === 1 && held.some(item => item.inv === listed.inv)));
|
||||
}
|
||||
masterpieces.forget(painting.id);
|
||||
masterpieces.forget(painting.id, true);
|
||||
if (maker) {
|
||||
dens.giveDenItem(maker, masterpieces.TOKEN_DEN_ITEM);
|
||||
ecards.noteFromHq(maker, "Your Masterpiece was not approved. Here is a Masterpiece Token to frame another one with.");
|
||||
|
||||
+2
-1
@@ -168,7 +168,8 @@ const withServer = async (name, httpPort, gamePort, run, {dataDir: keptDir = nul
|
||||
// and in the one world
|
||||
AJ_WORLDS: process.env.AJ_WORLDS || "1",
|
||||
// the packs made again at once after a custom item changes
|
||||
AJ_PACK_REMAKE_MS: process.env.AJ_PACK_REMAKE_MS || "0"},
|
||||
AJ_PACK_REMAKE_MS: process.env.AJ_PACK_REMAKE_MS || "0",
|
||||
AJ_MASTERPIECE_MIN_AGE: process.env.AJ_MASTERPIECE_MIN_AGE || "0"},
|
||||
stdio: ["ignore", "pipe", "inherit"],
|
||||
});
|
||||
let output = "";
|
||||
|
||||
Reference in new issue
Block a user