2019-11-23 15:11:33 +01:00
|
|
|
import { User, UserPageStatus } from "../entities/User";
|
|
|
|
import { DatabaseHelper } from "./DatabaseHelper";
|
2019-11-23 16:10:51 +01:00
|
|
|
import { AccountImageHelper } from "./AccountImageHelper";
|
2019-12-28 13:38:17 +01:00
|
|
|
import { FriendsHelper } from "./FriendsHelper";
|
2019-11-23 15:11:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2019-12-28 13:38:17 +01:00
|
|
|
/**
|
|
|
|
* 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 15:11:33 +01:00
|
|
|
|
2019-11-23 16:10:51 +01:00
|
|
|
private static async DbToUser(row: any) : Promise<User> {
|
2019-11-23 15:11:33 +01:00
|
|
|
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)
|
2019-11-23 15:11:33 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|