diff --git a/src/entities/Friend.ts b/src/entities/Friend.ts new file mode 100644 index 0000000..e721baa --- /dev/null +++ b/src/entities/Friend.ts @@ -0,0 +1,27 @@ +/** + * Friend information + * + * @author Pierre HUBERT + */ + +export interface FriendBuilder { + friendID: number, + accepted: boolean, + following: boolean, + lastActivityTime: number, + canPostTexts: boolean +} + +export class Friend implements FriendBuilder { + friendID: number; accepted: boolean; + following: boolean; + lastActivityTime: number; + canPostTexts: boolean; + + public constructor(info: FriendBuilder) { + for (const key in info) { + if (info.hasOwnProperty(key)) + this[key] = info[key]; + } + } +} \ No newline at end of file diff --git a/src/helpers/AccountHelper.ts b/src/helpers/AccountHelper.ts index 32054d6..2a34c36 100644 --- a/src/helpers/AccountHelper.ts +++ b/src/helpers/AccountHelper.ts @@ -10,7 +10,7 @@ import { UserHelper } from "./UserHelper"; * @author Pierre HUBERT */ -const USER_TABLE = "utilisateurs"; +export const USER_TABLE = "utilisateurs"; const USERS_TOKENS_TABLE = "comunic_api_users_tokens"; export class AccountHelper { diff --git a/src/helpers/FriendsHelper.ts b/src/helpers/FriendsHelper.ts index ccbf866..51ce6cb 100644 --- a/src/helpers/FriendsHelper.ts +++ b/src/helpers/FriendsHelper.ts @@ -1,4 +1,6 @@ import { DatabaseHelper } from "./DatabaseHelper"; +import { USER_TABLE } from "./AccountHelper"; +import { Friend } from "../entities/Friend"; /** * Friends helper @@ -10,6 +12,35 @@ const FRIENDS_TABLE = "amis"; export class FriendsHelper { + /** + * Get the list of friends of the user + * + * @param userID The ID of the user + */ + public static async GetList(userID: number) : Promise> { + const results = await DatabaseHelper.Query({ + table: FRIENDS_TABLE + " f", + where: { + ID_personne: userID, + }, + joins: [ + { + table: USER_TABLE + " u", + condition: "f.ID_amis = u.ID" + } + ], + fields: [ + "u.last_activity", + "f.ID_amis", + "f.actif", + "f.abonnement", + "f.autoriser_post_page" + ] + }); + + return results.map((r) => this.DbToFriend(r)); + } + /** * Count the number of friendship requests a user * received @@ -80,4 +111,19 @@ export class FriendsHelper { return result["autoriser_post_page"] == 1; } + + /** + * Turn a database entry into a {Friend} object + * + * @param row The row to transform + */ + private static DbToFriend(row: any) : Friend { + return new Friend({ + friendID: row.ID_amis, + accepted: row.actif == 1, + following: row.abonnement == 1, + lastActivityTime: row.last_activity, + canPostTexts: row.autoriser_post_page == 1 + }); + } } \ No newline at end of file