mirror of
				https://gitlab.com/comunic/comunicapiv2
				synced 2025-11-03 19:14:03 +00:00 
			
		
		
		
	Can trigger API
This commit is contained in:
		@@ -7,6 +7,8 @@
 | 
			
		||||
 * @author Pierre HUBERT
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
import { time } from "../utils/DateUtils";
 | 
			
		||||
 | 
			
		||||
// Different supported actions
 | 
			
		||||
export enum Action {
 | 
			
		||||
	LOGIN_FAILED = "login_failed",
 | 
			
		||||
@@ -16,7 +18,7 @@ export enum Action {
 | 
			
		||||
/**
 | 
			
		||||
 * The duration entries will be kept in the table
 | 
			
		||||
 */
 | 
			
		||||
const MAX_TIME = 3600; // 1 hour
 | 
			
		||||
const MAX_TIME = 20;//3600; // 1 hour
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Information about a specific IP & action
 | 
			
		||||
@@ -33,6 +35,18 @@ const list : Array<CountInfo> = [];
 | 
			
		||||
 | 
			
		||||
export class APILimitHelper {
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Find an entry in the list
 | 
			
		||||
	 * 
 | 
			
		||||
	 * If none is found, returns undefined if none found
 | 
			
		||||
	 * 
 | 
			
		||||
	 * @param ip The target IP address
 | 
			
		||||
	 * @param action Target action
 | 
			
		||||
	 */
 | 
			
		||||
	private static FindEntry(ip: string, action: Action) : CountInfo {
 | 
			
		||||
		return list.find((f) => f.ip == ip && f.action == action);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Trigger the counter (increase it by one)
 | 
			
		||||
	 * 
 | 
			
		||||
@@ -40,7 +54,19 @@ export class APILimitHelper {
 | 
			
		||||
	 * @param action The action to check
 | 
			
		||||
	 */
 | 
			
		||||
	public static async Trigger(ip: string, action: Action) {
 | 
			
		||||
		// TODO : trigger counter
 | 
			
		||||
		const entry = this.FindEntry(ip, action);
 | 
			
		||||
 | 
			
		||||
		// Check if it is the first time the API is triggered
 | 
			
		||||
		if(entry == undefined)
 | 
			
		||||
			list.push({
 | 
			
		||||
				ip: ip,
 | 
			
		||||
				action: action,
 | 
			
		||||
				time: time(),
 | 
			
		||||
				count: 1
 | 
			
		||||
			})
 | 
			
		||||
		
 | 
			
		||||
		else
 | 
			
		||||
			entry.count++
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
@@ -50,8 +76,9 @@ export class APILimitHelper {
 | 
			
		||||
	 * @param action The action to check
 | 
			
		||||
	 */
 | 
			
		||||
	public static async Count(ip: string, action: Action) : Promise<number> {
 | 
			
		||||
		// TODO : return count
 | 
			
		||||
		return 0;
 | 
			
		||||
		const entry = this.FindEntry(ip, action);
 | 
			
		||||
		console.log(entry)
 | 
			
		||||
		return entry == undefined ? 0 : entry.count;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user