2020-03-21 11:07:52 +00:00
|
|
|
import { RequestHandler } from "../entities/RequestHandler";
|
2020-03-21 13:45:03 +00:00
|
|
|
import { LikesType, LikesHelper } from "../helpers/LikesHelper";
|
|
|
|
import { UserHelper } from "../helpers/UserHelper";
|
|
|
|
import { GroupsAccessLevel } from "../entities/Group";
|
2020-03-21 11:07:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Likes controller
|
|
|
|
*
|
|
|
|
* @author Pierre HUBERT
|
|
|
|
*/
|
|
|
|
export class LikesController {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update like status over an element
|
|
|
|
*
|
|
|
|
* @param h Request handler
|
|
|
|
*/
|
|
|
|
public static async Update(h: RequestHandler) {
|
2020-03-21 13:45:03 +00:00
|
|
|
const req_type = h.postString("type");
|
|
|
|
const is_linking = h.postBool("like");
|
|
|
|
|
|
|
|
let id: number;
|
|
|
|
let type: LikesType;
|
|
|
|
|
|
|
|
switch(req_type) {
|
|
|
|
|
|
|
|
// In case of user
|
|
|
|
case "user":
|
|
|
|
id = await h.postUserId("id");
|
|
|
|
type = LikesType.USER;
|
|
|
|
|
|
|
|
if(!await UserHelper.CanSeeUserPage(h.getUserId(), id))
|
|
|
|
h.error(401, "You cannot access this user information!")
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
// In case of post
|
|
|
|
case "post":
|
|
|
|
id = await h.postPostIDWithAccess("id");
|
|
|
|
type = LikesType.POST;
|
|
|
|
|
|
|
|
// TODO : delete any notification targetting this user about the post
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
// In case of comment
|
|
|
|
case "comment":
|
|
|
|
id = await h.postCommentIDWithAccess("id");
|
|
|
|
type = LikesType.COMMENT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
// In case of group
|
|
|
|
case "group":
|
|
|
|
id = await h.postGroupIDWithAccess("id", GroupsAccessLevel.VIEW_ACCESS);
|
|
|
|
type = LikesType.GROUP;
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
// Default case : throw an error
|
|
|
|
default:
|
|
|
|
throw Error("Unsupported like type : "+req_type+" !");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Update like status
|
|
|
|
await LikesHelper.Update(h.getUserId(), is_linking, id, type);
|
|
|
|
|
|
|
|
|
|
|
|
h.success();
|
|
|
|
|
2020-03-21 11:07:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|