brainz-social-old/lib/database-handler.js

148 lines
7 KiB
JavaScript
Raw Normal View History

2023-09-28 18:54:38 -05:00
const db = require("better-sqlite3")("brainz-social.db");
2023-10-08 23:14:04 -05:00
2023-09-28 18:54:38 -05:00
db.pragma("journal_mode = WAL");
2023-09-27 07:03:36 -05:00
2023-09-28 18:54:38 -05:00
module.exports = {
2023-09-27 07:03:36 -05:00
db,
getConfig: (key) => {
2023-09-28 18:54:38 -05:00
const row = db.prepare("SELECT * FROM config WHERE key = ?").get(key);
return row ? row.value : null;
2023-09-27 07:03:36 -05:00
},
setConfig: (key, value) => {
db.prepare("INSERT OR REPLACE INTO config (key, value) VALUES(?, ?);").run(key, value);
},
createApplication: (client_name, redirect_uri, scopes, website, client_id, client_secret) => {
db.prepare("INSERT INTO applications (client_name, redirect_uri, scopes, website, client_id, client_secret) VALUES (?, ?, ?, ?, ?, ?);").run(client_name, redirect_uri, scopes, website, client_id, client_secret);
},
getApplication: (client_id) => {
return db.prepare("SELECT id, client_id, client_secret, redirect_uri, scopes, website FROM applications WHERE client_id = ?").get(client_id);
},
getApplicationById: (id) => {
return db.prepare("SELECT id, client_id, client_secret, redirect_uri, scopes, website FROM applications WHERE id = ?").get(id);
},
createToken: (token, scope, application_id, user_id, created_at) => {
2023-10-05 20:16:00 -05:00
db.prepare("INSERT INTO oauth_tokens (token, scopes, application_id, user_id, created_at, revoked) VALUES (?, ?, ?, ?, ?, false)").run(token, scope, application_id, user_id, created_at);
},
getTokenData: (token) => {
return db.prepare("SELECT application_id, user_id, created_at, revoked FROM oauth_tokens WHERE token = ?").get(token);
2023-09-27 19:29:55 -05:00
},
revokeToken: (token) => {
db.prepare("UPDATE oauth_tokens SET revoked = true WHERE token = ?").run(token);
2023-09-28 18:36:35 -05:00
},
getAccountByUsername: (username) => {
return db.prepare("SELECT id, username, email, password_hash, account_tier FROM accounts WHERE username = ?").get(username);
},
createAccount: (username, email, password_hash) => {
db.prepare("INSERT INTO accounts (username, email, password_hash, account_tier) VALUES (?, ?, ?, 0)").run(username, email, password_hash);
},
checkAuthCookie: (cookie_value) => {
return db.prepare("SELECT id, cookie_value, created_at, user_id, revoked FROM cookies WHERE cookie_value = ?").get(cookie_value);
},
revokeAuthCookie: (cookie_value) => {
db.prepare("UPDATE cookies SET revoked = true WHERE cookie_value = ?").run(cookie_value);
},
createAuthCookie: (cookie_value, created_at, user_id) => {
db.prepare("INSERT INTO cookies (cookie_value, created_at, user_id, revoked) VALUES (?, ?, ?, false)").run(cookie_value, created_at, user_id);
2023-10-01 16:02:20 -05:00
},
createCsrfToken: (url, created_at, cookie_value) => {
const db_row_id = db.prepare("INSERT INTO csrf_token (url, created_at, cookie_value) VALUES (?, ?, ?)").run(url, created_at, cookie_value).lastInsertRowid;
2023-10-01 16:02:20 -05:00
return db.prepare("SELECT id FROM csrf_token WHERE rowid = ?").get(db_row_id);
},
createCsrfTokenAssociation: (...ids) => {
2023-10-08 23:14:04 -05:00
for (const source_id in ids) {
if (Number.parseInt(ids[source_id], 10) === ids[source_id]) {
for (const destination_id in ids) {
if (Number.parseInt(ids[destination_id], 10) === ids[destination_id]) {
db.prepare("INSERT INTO csrf_token_relations (source_id, destination_id) VALUES (?, ?)").run(ids[source_id], ids[destination_id]);
}
}
2023-10-01 16:02:20 -05:00
}
}
},
removeAssociatedCsrfTokens: (id) => {
db.prepare("DELETE FROM csrf_token WHERE id IN (SELECT destination_id AS id FROM csrf_token_relations WHERE source_id = ?)").run(id);
},
getCsrfToken: (url) => {
return db.prepare("SELECT id, url, created_at, cookie_value FROM csrf_token WHERE url = ?").get(url);
2023-10-02 19:38:21 -05:00
},
createOauthCode: (code, application_id, user_id, scopes, created_at) => {
db.prepare("INSERT INTO oauth_code (code, application_id, user_id, scopes, created_at, revoked) VALUES (?, ?, ?, ?, ?, false)").run(code, application_id, user_id, scopes, created_at);
},
getOauthCode: (code) => {
return db.prepare("SELECT code, application_id, user_id, scopes, created_at, revoked FROM oauth_code WHERE code = ?").get(code);
},
revokeOauthCode: (code) => {
db.prepare("UPDATE oauth_code SET revoked = true WHERE code = ?").run(code);
},
selectApplicationByAuthToken: (token) => {
return db.prepare("SELECT id, client_id, client_secret, redirect_uri, scopes, website FROM applications WHERE id in (SELECT application_id as id FROM oauth_tokens WHERE token = ?);").get(token);
},
getVapidKey: () => {
const vapidPublic = db.prepare("SELECT value FROM config WHERE key = vapid_key_public").get();
const vapidPrivate = db.prepare("SELECT value FROM config WHERE key = vapid_key_private").get();
2023-10-08 23:14:04 -05:00
if (vapidPublic.value && vapidPrivate.value) {
return {public: vapidPublic, private: vapidPrivate};
}
2023-10-08 23:14:04 -05:00
return null;
},
setVapidKey: (publicKey, privateKey) => {
db.prepare("INSERT INTO config (key, value) VALUES (vapid_key_public, ?)").run(publicKey);
db.prepare("INSERT INTO config (key, value) VALUES (vapid_key_private, ?)").run(privateKey);
2023-10-07 14:36:11 -05:00
},
getJsonldSchemaCache: (url) => {
2023-10-08 23:14:04 -05:00
return db.prepare("SELECT schema FROM jsonld_schema_cache WHERE schema_uri = ? AND expires > ?").get(url, Math.floor(Date.now() / 1000));
2023-10-07 14:36:11 -05:00
},
storeJsonldSchemaCache: (url, schema, expiry) => {
db.prepare("INSERT INTO jsonld_schema_cache (schema, schema_uri, expires) VALUES (?, ?, ?)").run(schema, url, expiry);
},
getAccountByToken: (token) => {
return db.prepare("SELECT id, username, email, password_hash, account_tier FROM accounts WHERE id in (SELECT user_id AS id FROM tokens WHERE token = ?)").get(token);
},
getAccountActivityByAccount: (user_id) => {
return db.prepare("SELECT id, object, type, local, uri_id, owner FROM activity_objects WHERE (type = 'https://www.w3.org/ns/activitystreams#Person' OR type = 'https://www.w3.org/ns/activitystreams#Service') AND local = true AND owner in (SELECT username AS owner WHERE id = ?)").get(user_id);
},
addActivity: (object, type, local, uri_id, owner) => {
db.prepare("INSERT INTO activity_objects (object, type, local, uri_id, owner) VALUES (?, ?, ?, ?, ?)").run(object, type, local, uri_id, owner);
},
getLastStatus: (owner) => {
return db.prepare("SELECT created_at FROM activity_objects WHERE type = 'https://www.w3.org/ns/activitystreams#Note' AND owner = ? ORDER BY created_at DESC").get(owner);
},
getStatusCount: (owner) => {
return db.prepare("SELECT COUNT(*) AS count FROM activity_objects WHERE type = 'https://www.w3.org/ns/activitystreams#Note' AND owner = ?").get(owner);
},
storeW3idSecurityKey: (key_uri, publicKey, privateKey, expires) => {
db.prepare("INSERT INTO w3id_security_keys (key_uri, public_key, private_key) VALUES (?, ?, ?)").run(key_uri, publicKey, privateKey, expires);
2023-10-08 23:14:04 -05:00
},
};