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 ) ;
2023-10-04 20:54:45 -05:00
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 ) ;
2023-09-27 08:30:39 -05:00
} ,
getApplication : ( client _id ) => {
return db . prepare ( "SELECT id, client_id, client_secret, redirect_uri, scopes, website FROM applications WHERE client_id = ?" ) . get ( client _id ) ;
} ,
2023-09-27 17:31:59 -05:00
getApplicationById : ( id ) => {
return db . prepare ( "SELECT id, client_id, client_secret, redirect_uri, scopes, website FROM applications WHERE id = ?" ) . get ( id ) ;
} ,
2023-09-27 08:30:39 -05:00
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 ) ;
2023-09-27 17:31:59 -05:00
} ,
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 ) ;
2023-09-30 16:46:07 -05:00
} ,
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
} ,
2023-10-01 19:45:45 -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 ) => {
2023-10-01 19:45:45 -05:00
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 ) => {
2023-10-04 00:42:53 -05:00
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 ) ;
2023-10-05 23:07:56 -05:00
} ,
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 : ( ) => {
2023-10-06 22:00:20 -05:00
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 ) {
2023-10-06 22:00:20 -05:00
return { public : vapidPublic , private : vapidPrivate } ;
2023-10-05 23:07:56 -05:00
}
2023-10-08 23:14:04 -05:00
return null ;
2023-10-05 23:07:56 -05:00
} ,
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 ) ;
2023-10-08 22:02:20 -05:00
} ,
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
} ,
} ;