ReplyResource.java

/*
 * Copyright (C) 2000 - 2026 Silverpeas
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * As a special exception to the terms and conditions of version 3.0 of
 * the GPL, you may redistribute this Program in connection with Free/Libre
 * Open Source Software ("FLOSS") applications as described in Silverpeas's
 * FLOSS exception. You should have received a copy of the text describing
 * the FLOSS exception, and it is also available here:
 * "http://www.silverpeas.org/docs/core/legal/floss_exception.html"
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 */
package org.silverpeas.components.questionreply.web;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;

import jakarta.inject.Inject;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import org.silverpeas.components.questionreply.model.Question;
import org.silverpeas.components.questionreply.model.Reply;
import org.silverpeas.core.admin.user.model.SilverpeasRole;
import org.silverpeas.core.annotation.WebService;
import org.silverpeas.core.contribution.attachment.AttachmentService;
import org.silverpeas.core.contribution.attachment.model.SimpleDocument;
import org.silverpeas.core.web.rs.annotation.Authorized;
import org.silverpeas.core.rs.doc.NotFound;

import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * A REST Web resource representing a given reply to a question. It is a web service that provides
 * access to a reply referenced by its URL.
 */
@WebService
@Path(QuestionReplyBaseWebService.PATH + "/{componentId}/replies")
@Authorized
public class ReplyResource extends QuestionReplyBaseWebService {

  @PathParam("componentId")
  protected String componentId;

  @Inject
  private AttachmentService attachmentService;

  @Override
  public String getComponentId() {
    return this.componentId;
  }

  /**
   * Gets the JSON representation of the replies to the specified question. If the question doesn't
   * exist in the FAQ, a 404 HTTP code is returned. If the user isn't authentified, a 401 HTTP code
   * is returned. If the user isn't authorized to access the question, a 403 is returned. If a
   * problem occurs when processing the request, a 503 HTTP code is returned.
   *
   * @param onQuestionId the unique identifier of the question.
   * @return the response to the HTTP GET request with the JSON representation of the asked question
   * replies.
   */
  @Operation(summary = "Gets the replies to the given question.",
      description = "Only the replies the user is allowed to see are returned. The public " +
          "replies are visible to everybody whereas a private one is visible only to the " +
          "managers of the application and to the author of the question.")
  @ApiResponse(responseCode = "200", description = "The replies the user can see.",
      content = @Content(array = @ArraySchema(schema = @Schema(implementation = ReplyEntity.class))))
  @NotFound
  @GET
  @Path("question/{questionId}")
  @Produces(MediaType.APPLICATION_JSON)
  public List<ReplyEntity> getAllRepliesForQuestion(@PathParam("questionId") String onQuestionId) {
    try {
      Question question = getQuestionInComponent(onQuestionId);
      List<Reply> replies =
          getQuestionManager().getAllReplies(Long.parseLong(onQuestionId), componentId);
      return asWebEntities(extractVisibleReplies(question, replies), getUserProfile());
    } catch (Exception ex) {
      throw encapsulateException(ex);
    }
  }

  /**
   * Gets the public replies to the specified question.
   *
   * @param onQuestionId the unique identifier of the question.
   * @return a list with the public replies to the question.
   */
  @Operation(summary = "Gets only the public replies to the given question.")
  @ApiResponse(responseCode = "200", description = "The public replies to the question.",
      content = @Content(array = @ArraySchema(schema = @Schema(implementation = ReplyEntity.class))))
  @NotFound
  @GET
  @Produces(MediaType.APPLICATION_JSON)
  @Path("public/question/{questionId}")
  public List<ReplyEntity> getPublicRepliesForQuestion(@PathParam("questionId") String onQuestionId) {
    try {
      getQuestionInComponent(onQuestionId);
      List<Reply> replies = getQuestionManager().getQuestionPublicReplies(
          Long.parseLong(onQuestionId), componentId);
      return asWebEntities(replies, getUserProfile());
    } catch (Exception ex) {
      throw encapsulateException(ex);
    }
  }

  protected URI identifiedBy(URI uri) {
    return uri;
  }

  /**
   * Converts the specified list of replies into their corresponding web entities.
   *
   * @param replies the replies to convert.
   * @param profile the profile of the user.
   * @return a list with the corresponding reply entities.
   */
  protected List<ReplyEntity> asWebEntities(List<Reply> replies, SilverpeasRole profile) {
    return replies.stream()
        .map(r -> {
          URI commentURI = getUri().getRequestUriBuilder().path(r.getId()).build();
          return asWebEntity(r, identifiedBy(commentURI), profile);
        })
        .collect(Collectors.toList());
  }

  /**
   * Converts the reply into its corresponding web entity.
   *
   * @param reply the reply to convert.
   * @param replyURI the URI of the reply.
   * @param profile the profile of the user.
   * @return the corresponding reply entity.
   */
  protected ReplyEntity asWebEntity(final Reply reply, URI replyURI, SilverpeasRole profile) {
    ReplyEntity entity = ReplyEntity.fromReply(reply, getUserPreferences().getLanguage()).withURI(
        replyURI).withProfile(profile);
    Collection<SimpleDocument> attachments =
        attachmentService.listDocumentsByForeignKey(reply.toResourceReference(),
            entity.getLanguage());
    entity.withAttachments(attachments);
    AuthorEntity author = AuthorEntity.fromUser(reply.readAuthor());
    author.setAvatar(getHttpServletRequest().getContextPath() + author.getAvatar());
    return entity;
  }

  /**
   * Private replies should be visible to writers or publishers that have asked the question.
   *
   * @param questionAuthor the unique identifier of the question's author
   * @param reply the reply to the question
   * @param role the role the requester plays
   * @param userId the unique identifier of the requester
   * @return true if the reply is visible to the requester (the current user).
   */
  boolean isReplyVisible(String questionAuthor, Reply reply, SilverpeasRole role,
      String userId) {
    boolean isPrivate = reply.getPublicReply() <= 0;
    if (isPrivate) {
      boolean isAuthor = questionAuthor.equals(userId);
      return SilverpeasRole.USER != role && (SilverpeasRole.PUBLISHER != role || isAuthor);
    }
    return true;
  }

  List<Reply> extractVisibleReplies(Question question, List<Reply> replies) {
    String authorId = question.getCreatorId();
    SilverpeasRole profile = getUserProfile();
    String userid = getUser().getId();
    return replies.stream()
        .filter(reply -> isReplyVisible(authorId, reply, profile, userid))
        .collect(Collectors.toCollection(() -> new ArrayList<>(replies.size())));
  }
}