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-25 18:08:43 +00:00
|
|
|
import { UserLike } from "../entities/UserLike";
|
2020-03-28 13:28:37 +00:00
|
|
|
import { NotificationsHelper } from "../helpers/NotificationsHelper";
|
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;
|
|
|
|
|
2020-03-28 13:28:37 +00:00
|
|
|
// Delete any notification targetting this user about the post
|
|
|
|
await NotificationsHelper.DeleteAllPostsNotificationsTargetingUser(h.getUserId(), id);
|
2020-03-21 13:45:03 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-03-25 18:08:43 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
}
|
|
|
|
}
|
2020-03-21 11:07:52 +00:00
|
|
|
}
|