1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2025-02-20 16:02:40 +00:00
comunicapiv2/src/helpers/UserHelper.ts

140 lines
3.4 KiB
TypeScript
Raw Normal View History

import { User, UserPageStatus } from "../entities/User";
import { DatabaseHelper } from "./DatabaseHelper";
2019-11-23 16:10:51 +01:00
import { AccountImageHelper } from "./AccountImageHelper";
import { FriendsHelper } from "./FriendsHelper";
/**
* User helper
*
* @author Pierre HUBERT
*/
const TABLE_NAME = "utilisateurs";
export class UserHelper {
/**
* Get information a single user
*
* @param id The ID of the user to get
* @returns Information about the user | null if not found
*/
public static async GetUserInfo(id: number) : Promise<User|null> {
const result = await DatabaseHelper.QueryRow({
table: TABLE_NAME,
where: {
ID: id
}
});
if(!result)
return null;
return this.DbToUser(result);
}
2019-11-30 10:11:51 +01:00
/**
* Check out whether a user exists or not
*
* @param id The ID of the user to check
*/
public static async Exists(id: number) : Promise<boolean> {
return await DatabaseHelper.Count({
table: TABLE_NAME,
where: {
ID: id
}
}) > 0;
}
2019-11-30 09:28:50 +01:00
/**
* Search for user in the database
*
* @param query The query
* @param limit Limit for the request (default: 10)
* @returns The list of ids of the user
*/
public static async SearchUser(query: string, limit: number = 10) : Promise<Array<number>> {
query = "%" + query.replace(/\ /g, "%") + "%";
const request = await DatabaseHelper.Query({
fields: ["ID"],
table: TABLE_NAME,
customWhere: "(nom LIKE ?) || (prenom LIKE ?) || (CONCAT(prenom, '%', nom) LIKE ?) || (CONCAT(nom, '%', prenom) LIKE ?)",
customWhereArgs: [query, query, query, query],
limit: limit,
});
return request.map((e) => e.ID);
}
2019-12-26 13:49:17 +01:00
/**
* Search for user by virtual directory
*
* @param dir Target directory
* @returns The ID of the user found / -1 if none found
*/
public static async FindByFolder(dir: string) : Promise<number> {
const result = await DatabaseHelper.QueryRow({
table: TABLE_NAME,
where: {
sous_repertoire: dir
},
fields: ["ID"]
});
return result == null ? -1 : Number(result.ID);
}
/**
* Check out whether a user is allowed to access another
* user's page
*
* @param userID The ID of the user making the request
* @param targetUser The target user page
*/
public static async CanSeeUserPage(userID: number, targetUser: number) : Promise<boolean> {
if(userID == targetUser)
return true;
const visibility = await this.GetVisibility(targetUser);
// Open page = OK
if(visibility == UserPageStatus.OPEN) return true;
// Else the user must be signed in
if(userID <= 0) return false;
// Public page = OK for signed in users
if(visibility == UserPageStatus.PUBLIC) return true;
// Check if the two users are friend
if(!await FriendsHelper.AreFriend(userID, targetUser)) return false;
return true;
}
/**
* Convenience method to get the visibility level of a user
*
* @param userID ID of the target user
*/
private static async GetVisibility(userID: number) : Promise<UserPageStatus> {
return (await this.GetUserInfo(userID)).pageStatus;
}
2019-11-23 16:10:51 +01:00
private static async DbToUser(row: any) : Promise<User> {
return new User({
id: row.ID,
firstName: row.prenom,
lastName: row.nom,
timeCreate: new Date(row.date_creation).getTime()/1000,
virtualDirectory: row.sous_repertoire,
2019-11-23 16:10:51 +01:00
pageStatus: row.pageouverte == 1 ? UserPageStatus.OPEN : (row.public == 1 ? UserPageStatus.PUBLIC : UserPageStatus.PRIVATE),
accountImage: await AccountImageHelper.Get(row.ID)
});
}
}