1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2025-07-13 05:08:05 +00:00

Start implementation of post system

This commit is contained in:
2020-01-03 10:17:34 +01:00
parent 899ec40bd7
commit 87daf8acbe
4 changed files with 387 additions and 0 deletions

@ -0,0 +1,68 @@
import { RequestHandler } from "../entities/RequestHandler";
import { UserHelper } from "../helpers/UserHelper";
import { PostsHelper } from "../helpers/PostsHelper";
import { Post, PostVisibilityLevel } from "../entities/Post";
/**
* 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";
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) {
list.push(await this.PostToAPI(p));
}
h.send(list);
}
/**
* Turn a post object into an API entry
*
* @param p The post
*/
public static async PostToAPI(p: Post) : Promise<any> {
let data : any = {
ID: p.id,
useriD: p.userID,
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,
// 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,
file_url: !p.hasFile ? null : p.file.url
};
return data;
}
}

@ -11,6 +11,7 @@ import { WebAppControllers } from "./WebAppController";
import { CallsController } from "./CallsController";
import { FriendsController } from "./FriendsController";
import { MoviesController } from "./MoviesController";
import { PostsController } from "./PostsController";
/**
* Controllers routes
@ -182,6 +183,11 @@ export const Routes : Route[] = [
// Posts controller
{path: "/posts/get_user", cb: (h) => PostsController.GetListUser(h), needLogin: false},
// Notifications controller
{path: "/notifications/count_unread", cb: (h) => NotificationsController.CountUnread(h)},