2020-01-03 09:17:34 +00:00
|
|
|
import { RequestHandler } from "../entities/RequestHandler";
|
|
|
|
import { UserHelper } from "../helpers/UserHelper";
|
|
|
|
import { PostsHelper } from "../helpers/PostsHelper";
|
2020-01-03 15:38:44 +00:00
|
|
|
import { Post, PostVisibilityLevel, PostKind, PostAccessLevel } from "../entities/Post";
|
2020-01-03 09:24:05 +00:00
|
|
|
import { MoviesController } from "./MoviesController";
|
|
|
|
import { MoviesHelper } from "../helpers/MoviesHelper";
|
2020-01-03 13:37:54 +00:00
|
|
|
import { SurveyHelper } from "../helpers/SurveyHelper";
|
|
|
|
import { SurveyController } from "./SurveyController";
|
2020-01-03 13:49:56 +00:00
|
|
|
import { LikesHelper, LikesType } from "../helpers/LikesHelper";
|
2020-01-03 09:17:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Posts controller
|
|
|
|
*
|
|
|
|
* @author Pierre HUBERT
|
|
|
|
*/
|
|
|
|
|
|
|
|
const VISIBILITY_LEVELS_API = {};
|
|
|
|
VISIBILITY_LEVELS_API[PostVisibilityLevel.VISIBILITY_PUBLIC] = "public";
|
|
|
|
VISIBILITY_LEVELS_API[PostVisibilityLevel.VISIBILITY_FRIENDS] = "friends";
|
|
|
|
VISIBILITY_LEVELS_API[PostVisibilityLevel.VISIBILITY_USER] = "private";
|
|
|
|
VISIBILITY_LEVELS_API[PostVisibilityLevel.VISIBILITY_GROUP_MEMBERS] = "members";
|
|
|
|
|
2020-01-03 15:38:44 +00:00
|
|
|
|
|
|
|
const ACCESS_LEVELS_API = {};
|
|
|
|
ACCESS_LEVELS_API[PostAccessLevel.NO_ACCESS] = "no-access";
|
|
|
|
ACCESS_LEVELS_API[PostAccessLevel.BASIC_ACCESS] = "basic";
|
|
|
|
ACCESS_LEVELS_API[PostAccessLevel.INTERMEDIATE_ACCESS] = "intermediate";
|
|
|
|
ACCESS_LEVELS_API[PostAccessLevel.FULL_ACCESS] = "full";
|
|
|
|
|
|
|
|
|
2020-01-03 09:17:34 +00:00
|
|
|
export class PostsController {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the posts of a user
|
|
|
|
*
|
|
|
|
* @param h Request handler
|
|
|
|
*/
|
|
|
|
public static async GetListUser(h: RequestHandler) {
|
|
|
|
const userID = await h.postUserId("userID");
|
|
|
|
const startFrom = h.postInt("startFrom", 0);
|
|
|
|
|
|
|
|
if(!await UserHelper.CanSeeUserPage(h.optionnalUserID, userID))
|
|
|
|
h.error(401, "You are not allowed to access this user posts !");
|
|
|
|
|
|
|
|
const posts = await PostsHelper.GetUserPosts(h.optionnalUserID, userID, startFrom);
|
|
|
|
|
|
|
|
let list = [];
|
|
|
|
for (const p of posts) {
|
2020-01-03 13:37:54 +00:00
|
|
|
list.push(await this.PostToAPI(h, p));
|
2020-01-03 09:17:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
h.send(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Turn a post object into an API entry
|
|
|
|
*
|
2020-01-03 13:37:54 +00:00
|
|
|
* @param h Request handler
|
2020-01-03 09:17:34 +00:00
|
|
|
* @param p The post
|
|
|
|
*/
|
2020-01-03 13:37:54 +00:00
|
|
|
public static async PostToAPI(h: RequestHandler, p: Post) : Promise<any> {
|
2020-01-03 09:17:34 +00:00
|
|
|
let data : any = {
|
|
|
|
ID: p.id,
|
2020-01-03 09:32:44 +00:00
|
|
|
userID: p.userID,
|
2020-01-03 09:17:34 +00:00
|
|
|
user_page_id: p.userPageID,
|
|
|
|
group_id: p.groupID,
|
|
|
|
post_time: p.timeCreate,
|
|
|
|
content: p.hasContent ? p.content : null,
|
|
|
|
visibility_level: VISIBILITY_LEVELS_API[p.visibilityLevel],
|
|
|
|
kind: p.kind,
|
|
|
|
|
2020-01-03 09:30:37 +00:00
|
|
|
|
2020-01-03 09:17:34 +00:00
|
|
|
// File specific
|
|
|
|
file_size: !p.hasFile ? null : p.file.size,
|
|
|
|
file_type: !p.hasFile ? null : p.file.type,
|
|
|
|
file_path: !p.hasFile ? null : p.file.path,
|
2020-01-03 09:24:05 +00:00
|
|
|
file_url: !p.hasFile ? null : p.file.url,
|
|
|
|
|
|
|
|
|
|
|
|
// Movie specific
|
2020-01-03 09:30:37 +00:00
|
|
|
video_id: p.hasMovie ? p.movieID : null,
|
|
|
|
video_info: p.hasMovie ?
|
|
|
|
MoviesController.MovieToAPI(await MoviesHelper.GetInfo(p.movieID)) : null,
|
|
|
|
|
|
|
|
|
|
|
|
// Countdown timer specific
|
|
|
|
time_end: p.hasTimeEnd ? p.timeEnd : null,
|
|
|
|
|
|
|
|
// Weblink specific
|
|
|
|
link_url: !p.hasLink ? null : p.link.url,
|
|
|
|
link_title: !p.hasLink ? null : p.link.title,
|
|
|
|
link_description: !p.hasLink ? null : p.link.description,
|
2020-01-03 13:37:54 +00:00
|
|
|
link_image: !p.hasLink ? null : p.link.image,
|
|
|
|
|
|
|
|
|
|
|
|
// Survey specific
|
|
|
|
data_survey: !p.hasSurvey ? null : await SurveyController.SurveyToAPI(h, await SurveyHelper.GetInfo(p.id)),
|
2020-01-03 13:49:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Likes information
|
|
|
|
likes: await LikesHelper.Count(p.id, LikesType.POST),
|
|
|
|
userlike: h.signedIn ? await LikesHelper.IsLiking(h.getUserId(), p.id, LikesType.POST) : false,
|
2020-01-03 15:38:44 +00:00
|
|
|
|
|
|
|
// Determine user access level
|
|
|
|
user_access: ACCESS_LEVELS_API[await PostsHelper.GetAccessLevel(h.optionnalUserID, p)],
|
|
|
|
|
|
|
|
// TODO : add comments
|
2020-01-03 09:17:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
}
|