diff --git a/src/controllers/AccountController.ts b/src/controllers/AccountController.ts index 1491c1c..077f40b 100644 --- a/src/controllers/AccountController.ts +++ b/src/controllers/AccountController.ts @@ -8,6 +8,7 @@ import { Action } from "../helpers/APILimitsHelper"; import { UserController } from "./UserController"; import { PostsController } from "./PostsController"; import { CommentsController } from "./CommentsController"; +import { LikesController } from "./LikesController"; /** * Account controller @@ -237,6 +238,9 @@ export class AccountController { // User comments comments: await CommentsController.CommentsToAPI(h, data.comments), + + // User likes + likes: data.likes.map(LikesController.UserLikeToAPI) }; diff --git a/src/controllers/LikesController.ts b/src/controllers/LikesController.ts index da119ec..12e131f 100644 --- a/src/controllers/LikesController.ts +++ b/src/controllers/LikesController.ts @@ -2,6 +2,7 @@ import { RequestHandler } from "../entities/RequestHandler"; import { LikesType, LikesHelper } from "../helpers/LikesHelper"; import { UserHelper } from "../helpers/UserHelper"; import { GroupsAccessLevel } from "../entities/Group"; +import { UserLike } from "../entities/UserLike"; /** * Likes controller @@ -74,4 +75,18 @@ export class LikesController { } + /** + * Turn a user like entry into an API entry + * + * @param l The like entry + */ + public static UserLikeToAPI(l: UserLike) : any { + return { + id: l.id, + userID: l.userID, + time_sent: l.timeSent, + elem_type: l.elemType, + elem_id: l.elemId + } + } } \ No newline at end of file diff --git a/src/entities/AccountExport.ts b/src/entities/AccountExport.ts index 32ee09d..1a20ecd 100644 --- a/src/entities/AccountExport.ts +++ b/src/entities/AccountExport.ts @@ -7,12 +7,14 @@ import { User } from "./User"; import { Post } from "./Post"; import { Comment } from "./Comment"; +import { UserLike } from "./UserLike"; export interface AccountExportBuilder { userID: number; userInfo: User; postsList: Post[]; comments: Comment[]; + likes: UserLike[]; } export class AccountExport implements AccountExportBuilder { @@ -20,6 +22,7 @@ export class AccountExport implements AccountExportBuilder { userInfo: User; postsList: Post[]; comments: Comment[]; + likes: UserLike[]; public constructor(info: AccountExportBuilder) { for (const key in info) { diff --git a/src/entities/UserLike.ts b/src/entities/UserLike.ts new file mode 100644 index 0000000..3ae7704 --- /dev/null +++ b/src/entities/UserLike.ts @@ -0,0 +1,28 @@ +/** + * User like + * + * @author Pierre Hubert + */ + +export interface UserLikeBuilder { + id: number, + userID: number, + timeSent: number, + elemType: string, + elemId: string +} + +export class UserLike implements UserLikeBuilder { + id: number; + userID: number; + timeSent: number; + elemType: string; + elemId: string; + + public constructor(info: UserLikeBuilder) { + 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 b49ed1d..597de09 100644 --- a/src/helpers/AccountHelper.ts +++ b/src/helpers/AccountHelper.ts @@ -9,6 +9,7 @@ import { GeneralSettings, UserPageStatus, LangSettings, SecuritySettings } from import { AccountExport } from "../entities/AccountExport"; import { PostsHelper } from "./PostsHelper"; import { CommentsHelper } from "./CommentsHelper"; +import { LikesHelper } from "./LikesHelper"; /** * Account helper @@ -393,7 +394,10 @@ export class AccountHelper { postsList: await PostsHelper.ExportAllPostsUser(userID), // Export the list of comments - comments: await CommentsHelper.ExportAllUser(userID) + comments: await CommentsHelper.ExportAllUser(userID), + + // Export user likes + likes: await LikesHelper.ExportAllUser(userID), }) diff --git a/src/helpers/LikesHelper.ts b/src/helpers/LikesHelper.ts index b8506df..e031923 100644 --- a/src/helpers/LikesHelper.ts +++ b/src/helpers/LikesHelper.ts @@ -1,5 +1,6 @@ import { DatabaseHelper } from "./DatabaseHelper"; import { mysql_date } from "../utils/DateUtils"; +import { UserLike } from "../entities/UserLike"; /** * Likes helper @@ -119,4 +120,33 @@ export class LikesHelper { type: LikesKindsDB[type] }); } + + /** + * Get all the likes of a user + * + * @param userID Target user ID + */ + public static async ExportAllUser(userID: number) { + return (await DatabaseHelper.Query({ + table: LIKES_TABLE, + where: { + ID_personne: userID + } + })).map(this.DBToUserLike); + } + + /** + * Turn a database entry into a like entry + * + * @param row Target database row + */ + private static DBToUserLike(row: any) : UserLike { + return new UserLike({ + id: row.ID, + userID: row.ID_personne, + timeSent: new Date(row.Date_envoi).getTime()/1000, + elemId: row.ID_type, + elemType: row.type + }) + } } \ No newline at end of file