mirror of
https://gitlab.com/comunic/comunicapiv2
synced 2024-11-21 21:09:22 +00:00
Moved some requests
This commit is contained in:
parent
088deb5b9d
commit
fc5de4c73e
@ -4,6 +4,9 @@
|
||||
* @author Pierre Hubert
|
||||
*/
|
||||
|
||||
import { UserHelper } from "../helpers/UserHelper";
|
||||
import { removeHTMLNodes, checkMail } from "../utils/StringUtils";
|
||||
|
||||
export abstract class BaseRequestsHandler {
|
||||
|
||||
protected abstract getPostParam(name : string) : any;
|
||||
@ -12,6 +15,16 @@ export abstract class BaseRequestsHandler {
|
||||
public abstract success(message: string) : void;
|
||||
public abstract send(data: any): void;
|
||||
|
||||
/**
|
||||
* Check out whether a POST string is present in the request or not
|
||||
*
|
||||
* @param name The name of the POST field to check
|
||||
* @param minLength Minimal length of the parameter
|
||||
*/
|
||||
public hasPostString(name: string, minLength: number = 0) : boolean {
|
||||
return this.hasPostParameter(name) && this.getPostParam(name).length >= minLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a String from the request
|
||||
*
|
||||
@ -35,4 +48,150 @@ export abstract class BaseRequestsHandler {
|
||||
|
||||
return param;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get some content for post and satinize it (remove HTML nodes)
|
||||
*
|
||||
* @param name The name of the POST field
|
||||
* @param minLength Optionnal minimal length for the post
|
||||
*/
|
||||
public postContent(name: string, minLength ?: number) : string {
|
||||
const content = this.postString(name, minLength);
|
||||
|
||||
if(content.match(/data:image/))
|
||||
this.error(401, "Please do not include inline images!");
|
||||
|
||||
return removeHTMLNodes(content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an email address included in a post request
|
||||
*
|
||||
* @param name The name of the POST filed
|
||||
*/
|
||||
public postEmail(name: string) : string {
|
||||
const email = this.postString(name, 3);
|
||||
|
||||
if(!checkMail(email))
|
||||
this.error(400, email + " is not a valid email address!");
|
||||
|
||||
return email;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an integer included in the request
|
||||
*
|
||||
* @param name Name of POST field
|
||||
* @param fallback Fallback value (if none, throw an error)
|
||||
* @returns The number (throws in case of error)
|
||||
*/
|
||||
public postInt(name: string, fallback ?: number) : number {
|
||||
const param = this.getPostParam(name);
|
||||
|
||||
if(param == undefined) {
|
||||
if(fallback == undefined)
|
||||
this.error(400, "Missing integer '"+name+"' in the request!");
|
||||
return fallback;
|
||||
}
|
||||
|
||||
// Check number
|
||||
if(Number.parseInt(param).toString() !== param.toString())
|
||||
this.error(400, "'"+name+"' is an invalid integer!");
|
||||
|
||||
return Number.parseInt(param);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a list of integeres included in the request
|
||||
*
|
||||
* @param name The name of the post field
|
||||
* @param minEntries Specify the minimum number of entries required
|
||||
*/
|
||||
public postNumbersList(name: string, minEntries : number = 1) : Array<number> {
|
||||
const param = this.postString(name, minEntries < 1 ? 0 : minEntries, minEntries > 0);
|
||||
let list = [];
|
||||
for (const el of param.split(",")) {
|
||||
|
||||
if(el == "")
|
||||
continue;
|
||||
|
||||
if(Number.parseInt(el).toString() != el)
|
||||
this.error(400, "Invalid number detected in '"+name+"'!");
|
||||
|
||||
list.push(Number.parseInt(el));
|
||||
|
||||
}
|
||||
|
||||
if(list.length < minEntries)
|
||||
this.error(400, "Not enough entries in '" + name + "'!")
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn a list of string into a Set object
|
||||
*
|
||||
* @param name Name of POST field
|
||||
* @param minEntries Minimum number of entries to specify
|
||||
*/
|
||||
public postNumbersSet(name : string, minEntries : number = 1) : Set<number> {
|
||||
return new Set(this.postNumbersList(name, minEntries));
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to decode JSON included in a POST request
|
||||
*
|
||||
* @param name Name of POST field
|
||||
*/
|
||||
public postJSON(name: string) : any {
|
||||
const src = this.getPostParam(name);
|
||||
|
||||
if(src == undefined)
|
||||
this.error(400, "Missing JSON '" + name + "' in the request!");
|
||||
|
||||
try {
|
||||
const response = JSON.parse(src);
|
||||
return response;
|
||||
} catch(e) {
|
||||
this.error(500, "'" + name + "' is not a valid JSON !");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a boolean included in the request
|
||||
*
|
||||
* @param name The name of the POST field
|
||||
* @param fallback Fallback value to use if the value is not
|
||||
* found in the request
|
||||
*/
|
||||
public postBool(name: string, fallback ?: boolean) : boolean {
|
||||
const param = this.getPostParam(name);
|
||||
|
||||
if(param == undefined) {
|
||||
if(fallback != undefined)
|
||||
return fallback;
|
||||
|
||||
this.error(400, "Missing boolean '" + name + "' in the request!");
|
||||
}
|
||||
|
||||
return param === "true" || param === true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the ID of a user specified in a POST request
|
||||
*
|
||||
* @param name Name of the POST field
|
||||
*/
|
||||
public async postUserId(name: string) : Promise<number> {
|
||||
const userID = this.postInt(name);
|
||||
|
||||
if(userID < 1)
|
||||
this.error(400, "Invalid user ID specified in '" + name +"'!");
|
||||
|
||||
if(!await UserHelper.Exists(userID))
|
||||
this.error(404, "User with ID " + userID + " not found!");
|
||||
|
||||
return userID;
|
||||
}
|
||||
}
|
@ -65,162 +65,6 @@ export class RequestHandler extends BaseRequestsHandler {
|
||||
return this.getPostParam(name) != undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check out whether a POST string is present in the request or not
|
||||
*
|
||||
* @param name The name of the POST field to check
|
||||
* @param minLength Minimal length of the parameter
|
||||
*/
|
||||
public hasPostString(name: string, minLength: number = 0) : boolean {
|
||||
return this.hasPostParameter(name) && this.getPostParam(name).length >= minLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get some content for post and satinize it (remove HTML nodes)
|
||||
*
|
||||
* @param name The name of the POST field
|
||||
* @param minLength Optionnal minimal length for the post
|
||||
*/
|
||||
public postContent(name: string, minLength ?: number) : string {
|
||||
const content = this.postString(name, minLength);
|
||||
|
||||
if(content.match(/data:image/))
|
||||
this.error(401, "Please do not include inline images!");
|
||||
|
||||
return removeHTMLNodes(content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an email address included in a post request
|
||||
*
|
||||
* @param name The name of the POST filed
|
||||
*/
|
||||
public postEmail(name: string) : string {
|
||||
const email = this.postString(name, 3);
|
||||
|
||||
if(!checkMail(email))
|
||||
this.error(400, email + " is not a valid email address!");
|
||||
|
||||
return email;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an integer included in the request
|
||||
*
|
||||
* @param name Name of POST field
|
||||
* @param fallback Fallback value (if none, throw an error)
|
||||
* @returns The number (throws in case of error)
|
||||
*/
|
||||
public postInt(name: string, fallback ?: number) : number {
|
||||
const param = this.getPostParam(name);
|
||||
|
||||
if(param == undefined) {
|
||||
if(fallback == undefined)
|
||||
this.error(400, "Missing integer '"+name+"' in the request!");
|
||||
return fallback;
|
||||
}
|
||||
|
||||
// Check number
|
||||
if(Number.parseInt(param).toString() !== param.toString())
|
||||
this.error(400, "'"+name+"' is an invalid integer!");
|
||||
|
||||
return Number.parseInt(param);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a list of integeres included in the request
|
||||
*
|
||||
* @param name The name of the post field
|
||||
* @param minEntries Specify the minimum number of entries required
|
||||
*/
|
||||
public postNumbersList(name: string, minEntries : number = 1) : Array<number> {
|
||||
const param = this.postString(name, minEntries < 1 ? 0 : minEntries, minEntries > 0);
|
||||
let list = [];
|
||||
for (const el of param.split(",")) {
|
||||
|
||||
if(el == "")
|
||||
continue;
|
||||
|
||||
if(Number.parseInt(el).toString() != el)
|
||||
this.error(400, "Invalid number detected in '"+name+"'!");
|
||||
|
||||
list.push(Number.parseInt(el));
|
||||
|
||||
}
|
||||
|
||||
if(list.length < minEntries)
|
||||
this.error(400, "Not enough entries in '" + name + "'!")
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn a list of string into a Set object
|
||||
*
|
||||
* @param name Name of POST field
|
||||
* @param minEntries Minimum number of entries to specify
|
||||
*/
|
||||
public postNumbersSet(name : string, minEntries : number = 1) : Set<number> {
|
||||
return new Set(this.postNumbersList(name, minEntries));
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to decode JSON included in a POST request
|
||||
*
|
||||
* @param name Name of POST field
|
||||
*/
|
||||
public postJSON(name: string) : any {
|
||||
const src = this.getPostParam(name);
|
||||
|
||||
if(src == undefined)
|
||||
this.error(400, "Missing JSON '" + name + "' in the request!");
|
||||
|
||||
try {
|
||||
const response = JSON.parse(src);
|
||||
return response;
|
||||
} catch(e) {
|
||||
this.error(500, "'" + name + "' is not a valid JSON !");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a boolean included in the request
|
||||
*
|
||||
* @param name The name of the POST field
|
||||
* @param fallback Fallback value to use if the value is not
|
||||
* found in the request
|
||||
*/
|
||||
public postBool(name: string, fallback ?: boolean) : boolean {
|
||||
const param = this.getPostParam(name);
|
||||
|
||||
if(param == undefined) {
|
||||
if(fallback != undefined)
|
||||
return fallback;
|
||||
|
||||
this.error(400, "Missing boolean '" + name + "' in the request!");
|
||||
}
|
||||
|
||||
return param === "true" || param === true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the ID of a user specified in a POST request
|
||||
*
|
||||
* @param name Name of the POST field
|
||||
*/
|
||||
public async postUserId(name: string) : Promise<number> {
|
||||
const userID = this.postInt(name);
|
||||
|
||||
if(userID < 1)
|
||||
this.error(400, "Invalid user ID specified in '" + name +"'!");
|
||||
|
||||
if(!await UserHelper.Exists(userID))
|
||||
this.error(404, "User with ID " + userID + " not found!");
|
||||
|
||||
return userID;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the ID of a friend included in a POST request
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user