preapring for rls
This commit is contained in:
52
scripts/create-admin.mjs
Normal file
52
scripts/create-admin.mjs
Normal file
@@ -0,0 +1,52 @@
|
||||
import db from "../src/inc/sql.mjs";
|
||||
import lib from "../src/inc/lib.mjs";
|
||||
import cfg from "../src/inc/config.mjs";
|
||||
import { getDefaultLayout } from "../src/inc/settings.mjs";
|
||||
|
||||
const [username, password] = process.argv.slice(2);
|
||||
|
||||
if (!username || !password) {
|
||||
console.error("Usage: node scripts/create-admin.mjs <username> <password>");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (password.length < 20) {
|
||||
console.error("Error: Password must be at least 20 characters long to meet system security requirements.");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
async function createAdmin() {
|
||||
console.log(`--- Creating Admin User: ${username} ---`);
|
||||
|
||||
// Check if user exists
|
||||
const existing = await db`select id from "user" where "login" = ${username.toLowerCase()} or "user" = ${username}`;
|
||||
if (existing.length > 0) {
|
||||
console.error("Error: Username already taken.");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const hash = await lib.hash(password);
|
||||
const ts = ~~(Date.now() / 1e3);
|
||||
|
||||
try {
|
||||
const newUser = await db`
|
||||
insert into "user" ("login", "password", "user", "created_at", "admin", "is_moderator", "activated")
|
||||
values (${username.toLowerCase()}, ${hash}, ${username}, to_timestamp(${ts}), true, true, true)
|
||||
returning id
|
||||
`;
|
||||
const userId = newUser[0].id;
|
||||
|
||||
await db`
|
||||
insert into user_options (user_id, mode, theme, fullscreen, avatar, avatar_file, use_new_layout, disable_autoplay, disable_swiping)
|
||||
values (${userId}, 3, 'amoled', 0, null, 'default.png', ${getDefaultLayout() === 'modern'}, false, false)
|
||||
`;
|
||||
|
||||
console.log(`--- Admin User ${username} Created Successfully ---`);
|
||||
process.exit(0);
|
||||
} catch (err) {
|
||||
console.error("Error creating admin user:", err);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
createAdmin();
|
||||
65
scripts/seed.mjs
Normal file
65
scripts/seed.mjs
Normal file
@@ -0,0 +1,65 @@
|
||||
import db from "../src/inc/sql.mjs";
|
||||
|
||||
const SETTINGS = [
|
||||
{ key: 'motd', value: 'Hello World!' },
|
||||
{ key: 'manual_approval', value: 'true' },
|
||||
{ key: 'min_tags', value: '3' },
|
||||
{ key: 'registration_open', value: 'true' },
|
||||
{ key: 'trusted_uploads', value: '0' },
|
||||
{ key: 'about_text', value: 'Check the README.md for more information.' },
|
||||
{ key: 'rules_text', value: '' },
|
||||
{ key: 'terms_text', value: '' }
|
||||
];
|
||||
|
||||
const TAGS = [
|
||||
{ id: 1, tag: 'sfw', normalized: 'sfw' },
|
||||
{ id: 2, tag: 'nsfw', normalized: 'nsfw' },
|
||||
{ id: 3, tag: 'nsfp', normalized: 'nsfp' },
|
||||
{ id: 4, tag: 'nsfl', normalized: 'nsfl' }
|
||||
];
|
||||
|
||||
async function seed() {
|
||||
console.log('--- Starting Database Seed ---');
|
||||
|
||||
// Seed Site Settings
|
||||
console.log('Seeding site_settings...');
|
||||
for (const setting of SETTINGS) {
|
||||
await db`
|
||||
INSERT INTO site_settings (key, value)
|
||||
VALUES (${setting.key}, ${setting.value})
|
||||
ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value
|
||||
`;
|
||||
console.log(` Set ${setting.key} = ${setting.value.substring(0, 30)}${setting.value.length > 30 ? '...' : ''}`);
|
||||
}
|
||||
|
||||
// Seed Tags
|
||||
console.log('Seeding tags...');
|
||||
for (const tag of TAGS) {
|
||||
if (tag.id) {
|
||||
// For protected tags with specific IDs, we use the ID
|
||||
await db`
|
||||
INSERT INTO tags (id, tag, normalized)
|
||||
VALUES (${tag.id}, ${tag.tag}, ${tag.normalized})
|
||||
ON CONFLICT (id) DO UPDATE SET tag = EXCLUDED.tag, normalized = EXCLUDED.normalized
|
||||
`;
|
||||
// Also ensure sequence is updated if we inserted specific IDs
|
||||
await db`SELECT setval('tags_id_seq', (SELECT MAX(id) FROM tags))`;
|
||||
} else {
|
||||
await db`
|
||||
INSERT INTO tags (tag, normalized)
|
||||
VALUES (${tag.tag}, ${tag.normalized})
|
||||
ON CONFLICT (tag) DO NOTHING
|
||||
`;
|
||||
}
|
||||
console.log(` Tag: ${tag.tag}`);
|
||||
}
|
||||
|
||||
console.log('--- Seed Completed Successfully ---');
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
seed().catch(err => {
|
||||
console.error('--- Seed Failed ---');
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user