Skip to content

Commit

Permalink
Remove unused stuff from the Comment API
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianKniephoff committed Jan 28, 2025
1 parent 347034d commit 3e7f4b9
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 208 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -751,24 +751,11 @@ List<Label> getLabels(long categoryId, Option<Integer> offset, Option<Integer> l
/**
* Get all comments from an annotation
*
* @param annotationId
* the annotation id
* @param replyToId
* id of the comment to ge the replies to
* @param offset
* pagination offset
* @param limit
* limit the result set to the said amount
* @param since
* limit the result set to comments modified since the said date
* @param tagsAnd
* the tags logical AND Map
* @param tagsOr
* the tags logical OR Map
* @param annotationId the annotation id
* @param replyToId id of the comment to ge the replies to
* @return the comment list or an empty list if no comments has been found
*/
List<Comment> getComments(long annotationId, Option<Long> replyToId, Option<Integer> offset, Option<Integer> limit,
Option<Date> since, Option<Map<String, String>> tagsAnd, Option<Map<String, String>> tagsOr);
Stream<Comment> getComments(long annotationId, Option<Long> replyToId);

/**
* Update a comment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import static org.opencast.annotation.endpoint.AbstractExtendedAnnotationsRestService.NO_CONTENT;
import static org.opencast.annotation.endpoint.AbstractExtendedAnnotationsRestService.UNAUTHORIZED;
import static org.opencast.annotation.endpoint.AbstractExtendedAnnotationsRestService.nil;
import static org.opencast.annotation.endpoint.AbstractExtendedAnnotationsRestService.parseDate;
import static org.opencast.annotation.endpoint.AbstractExtendedAnnotationsRestService.parseToJsonMap;
import static org.opencast.annotation.endpoint.AbstractExtendedAnnotationsRestService.run;
import static org.opencastproject.util.UrlSupport.uri;
Expand Down Expand Up @@ -46,7 +45,6 @@
import org.opencastproject.util.data.functions.Functions;

import java.net.URI;
import java.util.Date;
import java.util.Map;

import javax.ws.rs.DELETE;
Expand Down Expand Up @@ -777,27 +775,21 @@ public Response none() {
@Produces(MediaType.APPLICATION_JSON)
@Path("tracks/{trackId}/annotations/{annotationId}/comments")
public Response postComment(@PathParam("trackId") final long trackId,
@PathParam("annotationId") final long annotationId, @FormParam("text") final String text,
@FormParam("tags") final String tags) {
return postCommentResponse(trackId, annotationId, none(), text, tags);
@PathParam("annotationId") final long annotationId, @FormParam("text") final String text) {
return postCommentResponse(trackId, annotationId, none(), text);
}

private Response postCommentResponse(final long trackId, final long annotationId, final Option<Long> replyToId,
final String text, final String tags) {
final String text) {
if (videoOpt.isSome() && eas.getTrack(trackId).isSome()
&& eas.getAnnotation(annotationId).isSome()) {
return run(array(text), new Function0<Response>() {
return run(array(text), new Function0<>() {
@Override
public Response apply() {
final Option<Option<Map<String, String>>> tagsMap = trimToNone(tags).map(parseToJsonMap);
if (tagsMap.isSome() && tagsMap.get().isNone())
return BAD_REQUEST;

Resource resource = eas.createResource(tagsMap.bind(Functions.identity()));
Resource resource = eas.createResource();
final Comment comment = eas.createComment(annotationId, replyToId, text, resource);

return Response.created(commentLocationUri(comment, videoId, trackId))
.entity(CommentDto.toJson.apply(eas, comment).toString()).build();
return Response.created(commentLocationUri(comment, videoId, trackId)).entity(CommentDto.toJson.apply(eas, comment).toString()).build();
}
});
} else {
Expand All @@ -811,46 +803,37 @@ public Response apply() {
@Path("tracks/{trackId}/annotations/{annotationId}/comments/{commentId}")
public Response putComment(@PathParam("trackId") final long trackId,
@PathParam("annotationId") final long annotationId, @PathParam("commentId") final long commentId,
@FormParam("text") final String text, @FormParam("tags") final String tags) {
@FormParam("text") final String text) {
if (videoOpt.isSome() && eas.getTrack(trackId).isSome()
&& eas.getAnnotation(annotationId).isSome()) {
return run(array(text), new Function0<Response>() {
return run(array(text), new Function0<>() {
@Override
public Response apply() {
final Option<Option<Map<String, String>>> tagsMap = trimToNone(tags).map(parseToJsonMap);
if (tagsMap.isSome() && tagsMap.get().isNone())
return BAD_REQUEST;

final Option<Map<String, String>> tags = tagsMap.bind(Functions.identity());

return eas.getComment(commentId).fold(new Option.Match<Comment, Response>() {
return eas.getComment(commentId).fold(new Option.Match<>() {
@Override
public Response some(Comment c) {
if (!eas.hasResourceAccess(c))
if (!eas.hasResourceAccess(c)) {
return UNAUTHORIZED;
Resource resource = eas.updateResource(c, tags);
}
Resource resource = eas.updateResource(c, Option.none());
final Comment updated = new CommentImpl(commentId, annotationId, text, Option.none(), resource);
if (!c.equals(updated)) {
eas.updateComment(updated);
c = updated;
}
return Response.ok(CommentDto.toJson.apply(eas, c).toString())
.header(LOCATION, commentLocationUri(c, videoId, trackId))
.build();
return Response.ok(CommentDto.toJson.apply(eas, c).toString()).header(LOCATION, commentLocationUri(c, videoId, trackId)).build();
}

@Override
public Response none() {
Resource resource = eas.createResource(tags);
Resource resource = eas.createResource();
final Comment comment = eas.createComment(annotationId, Option.none(), text, resource);

return Response.created(commentLocationUri(comment, videoId, trackId))
.entity(CommentDto.toJson.apply(eas, comment).toString()).build();
return Response.created(commentLocationUri(comment, videoId, trackId)).entity(CommentDto.toJson.apply(eas, comment).toString()).build();
}
});
}
});

} else {
// track, video and/or annotation does not exist
return BAD_REQUEST;
Expand All @@ -863,14 +846,15 @@ public Response deleteComment(@PathParam("trackId") final long trackId,
@PathParam("annotationId") final long annotationId, @PathParam("id") final long commentId) {
if (videoOpt.isSome() && eas.getTrack(trackId).isSome()
&& eas.getAnnotation(annotationId).isSome()) {
return run(nil, new Function0<Response>() {
return run(nil, new Function0<>() {
@Override
public Response apply() {
return eas.getComment(commentId).fold(new Option.Match<Comment, Response>() {
return eas.getComment(commentId).fold(new Option.Match<>() {
@Override
public Response some(Comment c) {
if (!eas.hasResourceAccess(c))
if (!eas.hasResourceAccess(c)) {
return UNAUTHORIZED;
}
return eas.deleteComment(c) ? NO_CONTENT : NOT_FOUND;
}

Expand All @@ -894,14 +878,15 @@ public Response getComment(@PathParam("trackId") final long trackId,
@PathParam("annotationId") final long annotationId, @PathParam("id") final long id) {
if (videoOpt.isSome() && eas.getTrack(trackId).isSome()
&& eas.getAnnotation(annotationId).isSome()) {
return run(nil, new Function0<Response>() {
return run(nil, new Function0<>() {
@Override
public Response apply() {
return eas.getComment(id).fold(new Option.Match<Comment, Response>() {
return eas.getComment(id).fold(new Option.Match<>() {
@Override
public Response some(Comment c) {
if (!eas.hasResourceAccess(c))
if (!eas.hasResourceAccess(c)) {
return UNAUTHORIZED;
}
return Response.ok(CommentDto.toJson.apply(eas, c).toString()).build();
}

Expand All @@ -922,36 +907,17 @@ public Response none() {
@Produces(MediaType.APPLICATION_JSON)
@Path("tracks/{trackId}/annotations/{annotationId}/comments")
public Response getComments(@PathParam("trackId") final long trackId,
@PathParam("annotationId") final long annotationId, @QueryParam("limit") final int limit,
@QueryParam("offset") final int offset, @QueryParam("since") final String date,
@QueryParam("tags-and") final String tagsAnd, @QueryParam("tags-or") final String tagsOr) {
return getCommentsResponse(trackId, annotationId, none(), limit, offset, date, tagsAnd, tagsOr);
@PathParam("annotationId") final long annotationId) {
return getCommentsResponse(trackId, annotationId, none());
}

private Response getCommentsResponse(final long trackId, final long annotationId, final Option<Long> replyToId,
final int limit, final int offset, final String date, final String tagsAnd, final String tagsOr) {
private Response getCommentsResponse(final long trackId, final long annotationId, final Option<Long> replyToId) {
if (videoOpt.isSome() && eas.getTrack(trackId).isSome()
&& eas.getAnnotation(annotationId).isSome()) {
return run(nil, new Function0<Response>() {
return run(nil, new Function0<>() {
@Override
public Response apply() {
final Option<Integer> offsetm = offset > 0 ? some(offset) : none();
final Option<Integer> limitm = limit > 0 ? some(limit) : none();
final Option<Option<Date>> datem = trimToNone(date).map(parseDate);
Option<Option<Map<String, String>>> tagsAndArray = trimToNone(tagsAnd).map(parseToJsonMap);
Option<Option<Map<String, String>>> tagsOrArray = trimToNone(tagsOr).map(parseToJsonMap);

if ((datem.isSome() && datem.get().isNone()) || (tagsAndArray.isSome() && tagsAndArray.get().isNone())
|| (tagsOrArray.isSome() && tagsOrArray.get().isNone()))
return BAD_REQUEST;

return Response.ok(CommentDto.toJson(
eas,
offset,
eas.getComments(annotationId, replyToId, offsetm, limitm,
datem.bind(Functions.identity()),
tagsAndArray.bind(Functions.identity()),
tagsOrArray.bind(Functions.identity()))).toString()).build();
return Response.ok(CommentDto.toJson(eas, eas.getComments(annotationId, replyToId)).toString()).build();
}
});
} else {
Expand All @@ -965,28 +931,20 @@ public Response apply() {
@Path("tracks/{trackId}/annotations/{annotationId}/comments/{commentId}/replies")
public Response postReply(@PathParam("trackId") final long trackId,
@PathParam("annotationId") final long annotationId, @PathParam("commentId") final long commentId,
@FormParam("text") final String text, @FormParam("tags") final String tags) {
@FormParam("text") final String text) {
Option<Comment> comment = eas.getComment(commentId);
if (comment.isNone()) return BAD_REQUEST;
return postCommentResponse(trackId, annotationId, Option.some(comment.get().getId()), text, tags);
return postCommentResponse(trackId, annotationId, Option.some(comment.get().getId()), text);
}

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("tracks/{trackId}/annotations/{annotationId}/comments/{commentId}/replies")
public Response getReplies(@PathParam("trackId") final long trackId,
@PathParam("annotationId") final long annotationId, @PathParam("commentId") final long commentId,
@QueryParam("limit") final int limit, @QueryParam("offset") final int offset,
@QueryParam("since") final String date, @QueryParam("tags-and") final String tagsAnd,
@QueryParam("tags-or") final String tagsOr) {
@PathParam("annotationId") final long annotationId, @PathParam("commentId") final long commentId) {
Option<Comment> comment = eas.getComment(commentId);
if (comment.isNone()) return BAD_REQUEST;
return getCommentsResponse(trackId, annotationId, some(commentId), limit, offset, date, tagsAnd,
tagsOr);
}

private URI questionnaireLocationUri(Questionnaire q) {
return uri(host.getEndpointBaseUrl(), "videos", q.getVideoId(), "questionnaire", q.getId());
return getCommentsResponse(trackId, annotationId, some(commentId));
}

private URI trackLocationUri(Track t) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ public final class CommentImpl extends ResourceImpl implements Comment {

public CommentImpl(long id, long annotationId, String text, Option<Long> replyToId, Resource resource) {
super(Option.option(resource.getAccess()), resource.getCreatedBy(), resource.getUpdatedBy(), resource
.getDeletedBy(), resource.getCreatedAt(), resource.getUpdatedAt(), resource.getDeletedAt(), resource
.getTags());
.getDeletedBy(), resource.getCreatedAt(), resource.getUpdatedAt(), resource.getDeletedAt(), null);
this.id = id;
this.annotationId = annotationId;
this.text = text;
Expand All @@ -51,7 +50,7 @@ public long getId() {
}

public Option<Long> getVideo(final ExtendedAnnotationService eas) {
return eas.getAnnotation(annotationId).bind(new Function<Annotation, Option<Long>>() {
return eas.getAnnotation(annotationId).bind(new Function<>() {
@Override
public Option<Long> apply(Annotation annotation) {
return annotation.getVideo(eas);
Expand Down Expand Up @@ -81,12 +80,11 @@ public boolean equals(Object o) {
if (o == null || getClass() != o.getClass())
return false;
Comment comment = (Comment) o;
return id == comment.getId() && annotationId == comment.getAnnotationId() && text.equals(comment.getText())
&& getTags().equals(comment.getTags());
return id == comment.getId() && annotationId == comment.getAnnotationId() && text.equals(comment.getText());
}

@Override
public int hashCode() {
return Objects.hash(id, annotationId, text, getTags());
return Objects.hash(id, annotationId, text);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import static org.opencast.annotation.impl.Jsons.jA;
import static org.opencast.annotation.impl.Jsons.jO;
import static org.opencast.annotation.impl.Jsons.p;
import static org.opencastproject.util.data.Monadics.mlist;
import static org.opencastproject.util.data.Option.option;

import org.opencast.annotation.api.Comment;
Expand All @@ -34,20 +33,14 @@

import org.json.simple.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.MapKeyColumn;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
Expand All @@ -56,14 +49,9 @@
@Entity(name = "Comment")
@Table(name = "xannotations_comment")
@NamedQueries({
@NamedQuery(name = "Comment.findByIdIncludeDeleted", query = "select a from Comment a where a.id = :id"),
@NamedQuery(name = "Comment.findById", query = "select a from Comment a where a.id = :id and a.deletedAt IS NULL"),
@NamedQuery(name = "Comment.findAllOfAnnotation", query = "select a from Comment a where a.annotationId = :id and a.deletedAt IS NULL AND a.replyToId IS NULL"),
@NamedQuery(name = "Comment.findAllOfAnnotationSince", query = "select a from Comment a where a.annotationId = :id AND a.replyToId IS NULL AND a.deletedAt IS NULL AND ((a.updatedAt IS NOT NULL AND a.updatedAt >= :since) OR (a.updatedAt IS NULL AND a.createdAt >= :since))"),
@NamedQuery(name = "Comment.findAllReplies", query = "select a from Comment a where a.replyToId = :id and a.deletedAt IS NULL"),
@NamedQuery(name = "Comment.findAllRepliesSince", query = "select a from Comment a where a.replyToId = :id AND a.deletedAt IS NULL AND ((a.updatedAt IS NOT NULL AND a.updatedAt >= :since) OR (a.updatedAt IS NULL AND a.createdAt >= :since))"),
@NamedQuery(name = "Comment.deleteById", query = "delete from Comment a where a.id = :id"),
@NamedQuery(name = "Comment.count", query = "select count(a) from Comment a where a.deletedAt IS NULL"),
@NamedQuery(name = "Comment.clear", query = "delete from Comment") })
public class CommentDto extends AbstractResourceDto {
@Id
Expand All @@ -82,12 +70,6 @@ public class CommentDto extends AbstractResourceDto {
@Column(name = "reply_to_id")
private Long replyToId;

@ElementCollection
@MapKeyColumn(name = "name")
@Column(name = "value")
@CollectionTable(name = "xannotations_comment_tags", joinColumns = @JoinColumn(name = "comment_id"))
protected Map<String, String> tags = new HashMap<String, String>();

public static CommentDto create(long annotationId, String text, Option<Long> replyToId, Resource resource) {
CommentDto dto = new CommentDto().update(text, resource);
dto.annotationId = annotationId;
Expand All @@ -98,32 +80,30 @@ public static CommentDto create(long annotationId, String text, Option<Long> rep
public CommentDto update(String text, Resource resource) {
super.update(resource);
this.text = text;
if (resource.getTags() != null)
this.tags = resource.getTags();
return this;
}

public Comment toComment() {
return new CommentImpl(id, annotationId, text, option(replyToId), new ResourceImpl(option(access),
option(createdBy), option(updatedBy), option(deletedBy), option(createdAt), option(updatedAt),
option(deletedAt), tags));
option(deletedAt), null));
}

public static final Function<CommentDto, Comment> toComment = new Function<CommentDto, Comment>() {
public static final Function<CommentDto, Comment> toComment = new Function<>() {
@Override
public Comment apply(CommentDto dto) {
return dto.toComment();
}
};

public static final Function2<ExtendedAnnotationService, Comment, JSONObject> toJson = new Function2<ExtendedAnnotationService, Comment, JSONObject>() {
public static final Function2<ExtendedAnnotationService, Comment, JSONObject> toJson = new Function2<>() {
@Override
public JSONObject apply(ExtendedAnnotationService s, Comment c) {
return conc(AbstractResourceDto.toJson.apply(s, c), jO(p("id", c.getId()), p("text", c.getText())));
}
};

public static JSONObject toJson(ExtendedAnnotationService s, int offset, List<Comment> ls) {
return jO(p("offset", offset), p("count", ls.size()), p("comments", jA(mlist(ls).map(toJson.curry(s)))));
public static JSONObject toJson(ExtendedAnnotationService s, Stream<Comment> cs) {
return jO(p("comments", jA(cs.map(c -> toJson.apply(s, c)).toArray())));
}
}
Loading

0 comments on commit 3e7f4b9

Please sign in to comment.