Merge pull request 'message' (#15) from message into master

Reviewed-on: #15
This commit is contained in:
v4l3n71n 2023-02-09 21:50:40 +00:00
commit a500acf9bc
10 changed files with 633 additions and 13 deletions

View File

@ -0,0 +1,19 @@
package com.covas.Entity;
import java.time.LocalDateTime;
import java.util.Collection;
import io.quarkus.mongodb.panache.PanacheMongoEntity;
import io.quarkus.mongodb.panache.common.MongoEntity;
@MongoEntity(collection="conversation")
public class ConversationEntity extends PanacheMongoEntity {
public Collection<String> users;
public Short status;
public LocalDateTime created_at;
public LocalDateTime updated_at;
public LocalDateTime deleted_at;
}

View File

@ -0,0 +1,61 @@
package com.covas.Entity;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.CascadeType;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.GenericGenerator;
import io.quarkus.hibernate.orm.panache.PanacheEntityBase;
@Entity
@Table(name = "message")
@NamedQueries({
@NamedQuery(name = "Message.bySearch", query = "from MessageEntity u where u.content like :content"),
})
public class MessageEntity extends PanacheEntityBase implements Serializable {
@Id
@Column(name = "id")
@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
public UUID id;
@Column(columnDefinition="TEXT")
public String content;
@Column(nullable = false)
public LocalDateTime created_at;
@Column(nullable = false)
public LocalDateTime updated_at;
@ColumnDefault("null")
public LocalDateTime deleted_at;
public String conversation;
@ColumnDefault("1")
public Short status;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "users_id", referencedColumnName = "id")
public UsersEntity users;
public static List<MessageEntity> findByUsers(String uuid){
return find("users_id", uuid).list();
}
}

View File

@ -80,6 +80,9 @@ public class UsersEntity extends PanacheEntityBase implements Serializable {
@OneToMany(mappedBy = "users")
public Collection<CommentEntity> comment;
@OneToMany(mappedBy = "users")
public Collection<MessageEntity> message;
public static UsersEntity findByPseudo(String pseudo){
return find("pseudo", pseudo).firstResult();
}
@ -102,8 +105,6 @@ public class UsersEntity extends PanacheEntityBase implements Serializable {
users.created_at = LocalDateTime.now();
users.updated_at = LocalDateTime.now();
users.description = "";
users.persist();
}
}

View File

@ -0,0 +1,22 @@
package com.covas.Json;
import java.util.Collection;
import com.covas.Entity.MessageEntity;
import io.quarkus.runtime.annotations.RegisterForReflection;
@RegisterForReflection
public class MessageByUser {
public final String pseudo;
public final Collection<MessageEntity> message;
public MessageByUser(String pseudo, Collection<MessageEntity> message){
this.pseudo = pseudo;
this.message = message;
}
}

View File

@ -4,8 +4,7 @@ import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
import java.util.ArrayList;
import java.util.Collection;
import javax.annotation.security.RolesAllowed;
@ -27,8 +26,6 @@ import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.covas.Entity.BlacklistEntity;
import com.covas.Entity.FriendEntity;
import com.covas.Entity.GroupEntity;
import com.covas.Entity.UsersEntity;
import io.quarkus.panache.common.Page;

View File

@ -90,7 +90,7 @@ public class CommentRessources {
responseHttp = Response.ok(listComments).build();
if(uuid != null){
CommentEntity commentSingle= PublisherEntity.findById(UUID.fromString(uuid));
CommentEntity commentSingle = CommentEntity.findById(UUID.fromString(uuid));
responseHttp = Response.ok(commentSingle).build();
}

View File

@ -0,0 +1,213 @@
package com.covas.Resources;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
import javax.annotation.security.RolesAllowed;
import javax.inject.Inject;
import javax.transaction.Transactional;
import javax.ws.rs.core.SecurityContext;
import javax.ws.rs.Consumes;
import javax.ws.rs.CookieParam;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.PATCH;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.covas.Entity.ConversationEntity;
import com.covas.Entity.UsersEntity;
import io.quarkus.panache.common.Page;
import org.bson.types.ObjectId;
import org.eclipse.microprofile.jwt.Claims;
import org.eclipse.microprofile.jwt.JsonWebToken;
import org.jboss.logging.Logger;
import org.postgresql.shaded.com.ongres.scram.common.bouncycastle.base64.Base64;
@Produces(MediaType.APPLICATION_JSON)
@Path("conversation")
public class ConversationRessources {
private static final Logger LOGGER = Logger.getLogger(UsersRessources.class);
@Inject
JsonWebToken jwt;
/// Function
private Boolean checkUserCookie(String userCookie, UsersEntity users) {
if ((userCookie == null) || (users == null)) {
return false;
}
String name = new String(Base64.decode(userCookie), StandardCharsets.UTF_8);
if (!name.equals(users.pseudo) && (users.status != 1)) {
return false;
}
return true;
}
private Response.Status getResponseCheck(SecurityContext ctx, String userCookie, UsersEntity users) {
if (!ctx.getUserPrincipal().getName().equals(jwt.getName())) {
return Response.Status.INTERNAL_SERVER_ERROR;
}
if (!checkUserCookie(userCookie, users)) {
return Response.Status.FORBIDDEN;
}
return Response.Status.OK;
}
/// Appel HTTP
/// GET
@GET
@RolesAllowed("Admin")
public Response getFriends(@CookieParam("user") String userCookie, @Context SecurityContext ctx,
@QueryParam("page") Integer page, @QueryParam("nbPages") Integer nbPages) {
if(nbPages == null){
nbPages = 20;
}
if(page == null){
page = 0;
}
UUID kid = UUID.fromString(jwt.getClaim(Claims.kid));
UsersEntity user = UsersEntity.findById(kid);
Response.Status statusHttp = getResponseCheck(ctx, userCookie, user);
Response responseHttp = Response.status(statusHttp).build();
if (statusHttp.equals(Response.Status.OK)) {
List<ConversationEntity> listConversations = ConversationEntity.findAll().page(Page.of(page, nbPages)).list();
responseHttp = Response.ok(listConversations).build();
}
return responseHttp;
}
@GET
@RolesAllowed("Admin")
@Path("count")
public Response getCount(@CookieParam("user") String userCookie, @Context SecurityContext ctx){
UUID kid = UUID.fromString(jwt.getClaim(Claims.kid));
UsersEntity user = UsersEntity.findById(kid);
Response.Status status = getResponseCheck(ctx, userCookie, user);
Response responseHttp = Response.status(status).build();
if (status.equals(Response.Status.OK)){
responseHttp = Response.ok(ConversationEntity.count()).build();
}
return responseHttp;
}
@GET
@RolesAllowed({"Admin", "User"})
@Path("{id}")
public Response getSingleFriend(@PathParam("id") String id, @CookieParam("user") String userCookie,
@Context SecurityContext ctx) {
UUID kid = UUID.fromString(jwt.getClaim(Claims.kid));
UsersEntity user = UsersEntity.findById(kid);
Response.Status status = getResponseCheck(ctx, userCookie, user);
Response responseHttp = Response.status(status).build();
if (status.equals(Response.Status.OK)) {
ConversationEntity conversation = ConversationEntity.findById(new ObjectId(id));
responseHttp = Response.status(Response.Status.NOT_FOUND).build();
if (conversation != null) {
responseHttp = Response.ok(conversation).build();
}
}
return responseHttp;
}
// PUT
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@RolesAllowed({"Admin", "User"})
@Path("{id}")
@Transactional
public Response createFriend(@Context SecurityContext ctx, @CookieParam("user") String userCookie, @PathParam("id") String id, ConversationEntity conversation) {
UUID kid = UUID.fromString(jwt.getClaim(Claims.kid));
UsersEntity user = UsersEntity.findById(kid);
Response.Status status = getResponseCheck(ctx, userCookie, user);
ConversationEntity newConversation = new ConversationEntity();
if (status.equals(Response.Status.OK)) {
newConversation.users = conversation.users;
newConversation.created_at = LocalDateTime.now();
newConversation.updated_at = LocalDateTime.now();
newConversation.persist();
status = Response.Status.CREATED;
}
return Response.status(status).entity(newConversation).build();
}
// DELETE
@DELETE
@Path("{id}")
@RolesAllowed({"Admin", "User"})
@Transactional
public Response changeStatusSingleFriendToDelete(@Context SecurityContext ctx, @CookieParam("user") String userCookie,
@PathParam("id") String id) {
UUID kid = UUID.fromString(jwt.getClaim(Claims.kid));
UsersEntity user = UsersEntity.findById(kid);
Response.Status status = getResponseCheck(ctx, userCookie, user);
if (status.equals(Response.Status.OK)) {
ConversationEntity singleConversation = ConversationEntity.find("id", new ObjectId(id)).firstResult();
if (singleConversation == null) {
status = Response.Status.NOT_FOUND;
} else {
singleConversation.status = -1;
singleConversation.updated_at = LocalDateTime.now();
singleConversation.deleted_at = LocalDateTime.now();
singleConversation.persist();
}
}
return Response.status(status).build();
}
// PATCH
@PATCH
@RolesAllowed({"Admin", "User"})
@Consumes(MediaType.APPLICATION_JSON)
@Transactional
@Path("{id}")
public Response updateFriend(@Context SecurityContext ctx, @CookieParam("user") String userCookie,
ConversationEntity conversation, @PathParam("id") String id) {
UUID kid = UUID.fromString(jwt.getClaim(Claims.kid));
UsersEntity user = UsersEntity.findById(kid);
Response.Status status = getResponseCheck(ctx, userCookie, user);
if (status.equals(Response.Status.OK)) {
ConversationEntity conversationOrig = ConversationEntity.findById(new ObjectId(id));
if (conversationOrig == null) {
status = Response.Status.NOT_FOUND;
} else {
conversationOrig.users = conversation.users;
conversationOrig.updated_at = LocalDateTime.now();
if(conversation.status == 1){
conversationOrig.deleted_at = null;
conversationOrig.status = 1;
}
conversationOrig.persist();
}
}
return Response.status(status).build();
}
}

View File

@ -4,9 +4,6 @@ import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
import java.util.ArrayList;
import java.util.Collection;
import javax.annotation.security.RolesAllowed;
import javax.inject.Inject;
@ -27,7 +24,6 @@ import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.covas.Entity.FriendEntity;
import com.covas.Entity.GroupEntity;
import com.covas.Entity.UsersEntity;
import io.quarkus.panache.common.Page;

View File

@ -4,8 +4,7 @@ import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
import java.util.ArrayList;
import java.util.Collection;
import javax.annotation.security.RolesAllowed;

View File

@ -0,0 +1,312 @@
package com.covas.Resources;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
import javax.annotation.security.RolesAllowed;
import javax.inject.Inject;
import javax.transaction.Transactional;
import javax.ws.rs.core.SecurityContext;
import javax.ws.rs.Consumes;
import javax.ws.rs.CookieParam;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.PATCH;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.covas.Entity.MessageEntity;
import com.covas.Entity.UsersEntity;
import com.covas.Json.MessageByUser;
import io.quarkus.panache.common.Page;
import io.quarkus.panache.common.Parameters;
import org.eclipse.microprofile.jwt.Claims;
import org.eclipse.microprofile.jwt.JsonWebToken;
import org.jboss.logging.Logger;
import org.postgresql.shaded.com.ongres.scram.common.bouncycastle.base64.Base64;
@Produces(MediaType.APPLICATION_JSON)
@Path("message")
public class MessageRessources {
private static final Logger LOGGER = Logger.getLogger(UsersRessources.class);
@Inject
JsonWebToken jwt;
/// Function
private Boolean checkUserCookie(String userCookie, UsersEntity users) {
if ((userCookie == null) || (users == null)) {
return false;
}
String name = new String(Base64.decode(userCookie), StandardCharsets.UTF_8);
if (!name.equals(users.pseudo) && (users.status != 1)) {
return false;
}
return true;
}
private Response.Status getResponseCheck(SecurityContext ctx, String userCookie, UsersEntity users) {
if (!ctx.getUserPrincipal().getName().equals(jwt.getName())) {
return Response.Status.INTERNAL_SERVER_ERROR;
}
if (!checkUserCookie(userCookie, users)) {
return Response.Status.FORBIDDEN;
}
return Response.Status.OK;
}
/// Appel HTTP
/// GET
@GET
@RolesAllowed("Admin")
public Response getMessages(@CookieParam("user") String userCookie, @Context SecurityContext ctx,
@QueryParam("page") Integer page, @QueryParam("nbPages") Integer nbPages,
@QueryParam("status") Short status,
@QueryParam("search") String search,
@QueryParam("uuid") String uuid) {
if(nbPages == null){
nbPages = 20;
}
if(page == null){
page = 0;
}
UUID kid = UUID.fromString(jwt.getClaim(Claims.kid));
UsersEntity user = UsersEntity.findById(kid);
Response.Status statusHttp = getResponseCheck(ctx, userCookie, user);
Response responseHttp = Response.status(statusHttp).build();
if (statusHttp.equals(Response.Status.OK)) {
List<MessageEntity> listMessages = MessageEntity.findAll().page(Page.of(page, nbPages)).list();
responseHttp = Response.ok(listMessages).build();
if(uuid != null){
MessageEntity messageEntity= MessageEntity.findById(UUID.fromString(uuid));
responseHttp = Response.ok(messageEntity).build();
}
if(search != null){
List<MessageEntity> messagesList = MessageEntity.find("#Message.bySearch", Parameters.with("content", search)).page(Page.of(page, nbPages)).list();
responseHttp = Response.ok(messagesList).build();
}
}
return responseHttp;
}
@GET
@RolesAllowed("Admin")
@Path("count")
public Response getCount(@CookieParam("user") String userCookie, @Context SecurityContext ctx){
UUID kid = UUID.fromString(jwt.getClaim(Claims.kid));
UsersEntity user = UsersEntity.findById(kid);
Response.Status status = getResponseCheck(ctx, userCookie, user);
Response responseHttp = Response.status(status).build();
if (status.equals(Response.Status.OK)){
responseHttp = Response.ok(MessageEntity.count()).build();
}
return responseHttp;
}
@GET
@RolesAllowed("Admin")
@Path("{id}")
public Response getSingleMessage(@PathParam("id") String id, @CookieParam("user") String userCookie,
@Context SecurityContext ctx) {
UUID kid = UUID.fromString(jwt.getClaim(Claims.kid));
UsersEntity user = UsersEntity.findById(kid);
Response.Status status = getResponseCheck(ctx, userCookie, user);
Response responseHttp = Response.status(status).build();
if (status.equals(Response.Status.OK)) {
UUID uid = UUID.fromString(id);
MessageEntity message = MessageEntity.findById(uid);
responseHttp = Response.status(Response.Status.NOT_FOUND).build();
if (message != null) {
responseHttp = Response.ok(message).build();
}
}
return responseHttp;
}
@GET
@RolesAllowed("User")
@Path("info")
public Response getInfoMessage(@Context SecurityContext ctx, @CookieParam("user") String userCookie) {
UUID kid = UUID.fromString(jwt.getClaim(Claims.kid));
UsersEntity user = UsersEntity.findById(kid);
Response.Status status = getResponseCheck(ctx, userCookie, user);
if (status.equals(Response.Status.OK)) {
if (user == null) {
status = Response.Status.NOT_FOUND;
}
if (!checkUserCookie(userCookie, user)) {
status = Response.Status.FORBIDDEN;
}
}
Response responseHttp = Response.status(status).build();
if (status.equals(Response.Status.OK)) {
responseHttp = Response.status(status).entity(new MessageByUser(user.pseudo, user.message))
.build();
}
return responseHttp;
}
// PUT
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@RolesAllowed({"Admin", "User"})
@Path("{id}")
@Transactional
public Response createMessage(@Context SecurityContext ctx, @CookieParam("user") String userCookie, @PathParam("id") String id, MessageEntity message) {
UUID kid = UUID.fromString(jwt.getClaim(Claims.kid));
UsersEntity user = UsersEntity.findById(kid);
Response.Status status = getResponseCheck(ctx, userCookie, user);
MessageEntity newMessage = new MessageEntity();
if (status.equals(Response.Status.OK)) {
newMessage.content = message.content;
newMessage.conversation = message.conversation;
newMessage.users = user;
newMessage.created_at = LocalDateTime.now();
newMessage.updated_at = LocalDateTime.now();
newMessage.persist();
if (newMessage.isPersistent()) {
status = Response.Status.CREATED;
} else {
status = Response.Status.NO_CONTENT;
}
}
return Response.status(status).entity(newMessage).build();
}
// DELETE
@DELETE
@Path("{id}")
@RolesAllowed({"Admin", "User"})
@Transactional
public Response changeStatusSingleMessageToDelete(@Context SecurityContext ctx, @CookieParam("user") String userCookie,
@PathParam("id") String id) {
UUID kid = UUID.fromString(jwt.getClaim(Claims.kid));
UsersEntity user = UsersEntity.findById(kid);
Response.Status status = getResponseCheck(ctx, userCookie, user);
if (status.equals(Response.Status.OK)) {
MessageEntity singleMessage= MessageEntity.find("id", UUID.fromString(id)).firstResult();
if (singleMessage == null) {
status = Response.Status.NOT_FOUND;
} else {
singleMessage.status = -1;
singleMessage.updated_at = LocalDateTime.now();
singleMessage.deleted_at = LocalDateTime.now();
singleMessage.persist();
if (!singleMessage.isPersistent()) {
status = Response.Status.NOT_MODIFIED;
}
}
}
return Response.status(status).build();
}
@DELETE
@Path("/disable/{id}")
@RolesAllowed("Admin")
@Transactional
public Response changeStatusSingleMessageToDisable(@Context SecurityContext ctx, @CookieParam("user") String userCookie,
@PathParam("id") String id) {
UUID kid = UUID.fromString(jwt.getClaim(Claims.kid));
UsersEntity user = UsersEntity.findById(kid);
Response.Status status = getResponseCheck(ctx, userCookie, user);
if (status.equals(Response.Status.OK)) {
MessageEntity singleMessage = MessageEntity.find("id", UUID.fromString(id)).firstResult();
if (singleMessage == null) {
status = Response.Status.NOT_FOUND;
} else {
singleMessage.status = 0;
singleMessage.updated_at = LocalDateTime.now();
singleMessage.deleted_at = LocalDateTime.now();
singleMessage.persist();
if (!singleMessage.isPersistent()) {
status = Response.Status.NOT_MODIFIED;
}
}
}
return Response.status(status).build();
}
// PATCH
@PATCH
@RolesAllowed({"Admin", "User"})
@Consumes(MediaType.APPLICATION_JSON)
@Transactional
@Path("{id}")
public Response updateMessageAdmin(@Context SecurityContext ctx, @CookieParam("user") String userCookie,
MessageEntity message, @PathParam("id") String id) {
UUID kid = UUID.fromString(jwt.getClaim(Claims.kid));
UsersEntity user = UsersEntity.findById(kid);
Response.Status status = getResponseCheck(ctx, userCookie, user);
if (status.equals(Response.Status.OK)) {
MessageEntity messageOrig = MessageEntity.findById(UUID.fromString(id));
if (messageOrig == null) {
status = Response.Status.NOT_FOUND;
} else {
messageOrig.content = message.content;
messageOrig.updated_at = LocalDateTime.now();
if(message.status == 1){
messageOrig.deleted_at = null;
messageOrig.status = 1;
}
messageOrig.persist();
if (!messageOrig.isPersistent()) {
status = Response.Status.NOT_MODIFIED;
}
}
}
return Response.status(status).build();
}
@PATCH
@RolesAllowed("Admin")
@Consumes(MediaType.APPLICATION_JSON)
@Transactional
@Path("enable/{id}")
public Response enableCommentAdmin(@Context SecurityContext ctx, @CookieParam("user") String userCookie, @PathParam("id") String id) {
UUID kid = UUID.fromString(jwt.getClaim(Claims.kid));
UsersEntity user = UsersEntity.findById(kid);
Response.Status status = getResponseCheck(ctx, userCookie, user);
if (status.equals(Response.Status.OK)) {
MessageEntity messageOrig = MessageEntity.findById(UUID.fromString(id));
if (messageOrig == null) {
status = Response.Status.NOT_FOUND;
} else {
messageOrig.status = 1;
messageOrig.persist();
if (!messageOrig.isPersistent()) {
status = Response.Status.NOT_MODIFIED;
}
}
}
return Response.status(status).build();
}
}