2024-05-02 14:22:11 +00:00
//
// Form validation
//
// Preform sanity checks on content
// Format given data in an accessible way
//
2023-11-15 18:49:09 +00:00
const core = require ( "./core/core" ) ;
2024-05-02 14:22:11 +00:00
// Make sure the user registration data is safe and valid.
function newUser ( { username , password } = { } ) {
if ( ! username ) return _r ( false , "No username provided" ) ;
if ( ! password ) return _r ( false , "No password provided" ) ;
if ( password . length < core . settings [ "USER_MINIMUM_PASSWORD_LENGTH" ] ) return _r ( false , ` Password is not long enough. Minimum length is ${ core . settings [ "USER_MINIMUM_PASSWORD_LENGTH" ] } ` ) ;
2023-11-15 18:49:09 +00:00
2024-05-02 14:22:11 +00:00
// TODO: Method to block special characters
if ( ! _isUrlSafe ( username ) ) return _r ( false , "Invalid Username. Please only use a-z A-Z 0-9" ) ;
2023-11-15 18:49:09 +00:00
2024-05-02 14:22:11 +00:00
return _r ( true ) ;
2023-11-15 18:49:09 +00:00
}
2024-05-02 14:22:11 +00:00
function patchPost ( post _content , user , post ) {
let post _formatted = { } ; // The formatted post content object that will be returned upon success
let publish _date ; // Time and date the post should be made public
let tags = [ ] ; // An array of tags for the post
if ( ! user . success ) return _r ( false , "User not found" ) ;
if ( ! post . success ) return _r ( false , "Post not found" ) ;
2023-11-15 18:49:09 +00:00
// Get the publish date in a standard format
2024-05-02 14:22:11 +00:00
const [ year , month , day ] = post _content . date . split ( "-" ) ;
const [ hour , minute ] = post _content . time . split ( ":" ) ;
publish _date = new Date ( year , month - 1 , day , hour , minute ) ;
2023-11-15 18:49:09 +00:00
2024-05-02 14:22:11 +00:00
// Go though tags list, and turn into a pretty array
post _content . tags . forEach ( ( tag ) => {
2024-03-22 09:24:19 +00:00
// Trimmed
tag = tag . trim ( ) ;
// Lowercase
tag = tag . toLowerCase ( ) ;
// Non-empty
2024-05-02 14:22:11 +00:00
if ( tag . length !== 0 ) tags . push ( tag ) ;
2024-03-22 09:24:19 +00:00
} ) ;
2024-05-02 14:22:11 +00:00
delete post _content . date ;
delete post _content . time ;
// Format the post content
post _formatted = {
// Autofill the given data
... post _content ,
// Update tags to our pretty array
tags : tags ,
// Update date
2023-11-15 18:49:09 +00:00
publish _date : publish _date ,
} ;
2024-05-02 14:22:11 +00:00
return _r ( true , null , { post _formatted : post _formatted , user : user . data , post : post . data } ) ;
2023-11-15 18:49:09 +00:00
}
2024-05-02 14:22:11 +00:00
function patchBiography ( biography _content , user , biography ) {
if ( ! user . success ) return _r ( false , "User not found" ) ;
if ( ! biography . success ) return _r ( false , "Post not found" ) ;
return _r ( true , null , { biography _content : biography _content , user : user . data , biography : biography . data } ) ;
}
// Helper functions --------------------
2023-11-15 18:49:09 +00:00
function _isUrlSafe ( str ) {
const pattern = /^[A-Za-z0-9\-_.~]+$/ ;
return pattern . test ( str ) ;
}
2024-05-02 14:22:11 +00:00
function _r ( s , m , d ) {
return { success : s , message : m ? m || "Unknown error" : undefined , data : d } ;
}
2023-11-15 18:49:09 +00:00
2024-05-02 14:22:11 +00:00
module . exports = { newUser , patchPost , patchBiography } ;