ConversationRequestServlet.java

  1. /*
  2.  * Copyright (C) 2020-2024 by Savoir-faire Linux
  3.  *
  4.  * This program is free software; you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation; either version 3 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  16.  */
  17. package net.jami.jams.server.servlets.api.auth.conversations;

  18. import static net.jami.jams.server.Server.dataStore;

  19. import com.google.gson.Gson;

  20. import jakarta.servlet.ServletException;
  21. import jakarta.servlet.annotation.WebServlet;
  22. import jakarta.servlet.http.HttpServlet;
  23. import jakarta.servlet.http.HttpServletRequest;
  24. import jakarta.servlet.http.HttpServletResponse;

  25. import net.jami.jams.common.objects.conversations.Conversation;
  26. import net.jami.jams.common.objects.conversations.ConversationRequest;
  27. import net.jami.jams.common.serialization.adapters.GsonFactory;
  28. import net.jami.jams.common.serialization.tomcat.TomcatCustomErrorHandler;
  29. import net.jami.jams.common.utils.ConversationMerger;

  30. import java.io.IOException;
  31. import java.util.Arrays;
  32. import java.util.List;

  33. @WebServlet("/api/auth/conversationRequests")
  34. public class ConversationRequestServlet extends HttpServlet {
  35.     private static final Gson gson = GsonFactory.createGson();

  36.     /**
  37.      * @apiVersion 1.0.0
  38.      * @api {post} /api/auth/conversationRequests Sync conversationRequest list
  39.      * @apiName postConversationRequests
  40.      * @apiGroup ConversationRequests
  41.      * @apiParam {body} Conversation JSON representation of the conversationRequest object
  42.      * @apiParamExample {json} Request-Example: [
  43.      *     {"conversationId":"1231221","received":1594742298377},
  44.      *     {"conversationId":"213123","declined":1594742298377} ]
  45.      * @apiSuccess (200) {json} ConversationRequest[] user's converationRequests
  46.      * @apiError (500) {null} null contact could not be successfully added
  47.      */
  48.     @Override
  49.     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
  50.             throws ServletException, IOException {
  51.         String owner = req.getAttribute("username").toString();
  52.         addConversationRequests(req, resp, owner);
  53.     }

  54.     public static void addConversationRequests(
  55.             HttpServletRequest req, HttpServletResponse resp, String owner) throws IOException {
  56.         List<ConversationRequest> localList =
  57.                 dataStore.getConversationRequestDao().getByOwner(owner);
  58.         List<Conversation> conversationList = dataStore.getConversationDao().getByOwner(owner);
  59.         List<ConversationRequest> remoteList =
  60.                 Arrays.asList(gson.fromJson(req.getReader(), ConversationRequest[].class));

  61.         remoteList.forEach(conversationRequest -> conversationRequest.setOwner(owner));
  62.         List<ConversationRequest> mergedConversationRequests =
  63.                 ConversationMerger.mergeConversationRequests(localList, remoteList);

  64.         // Conversation requests should be deleted from the database if a conversation
  65.         // is already created
  66.         List<ConversationRequest> filteredConversationRequests =
  67.                 dataStore
  68.                         .getConversationRequestDao()
  69.                         .filterConversationRequests(mergedConversationRequests, conversationList);
  70.         if (filteredConversationRequests.size() > 0
  71.                 && !dataStore
  72.                         .getConversationRequestDao()
  73.                         .storeConversationRequestList(filteredConversationRequests))
  74.             TomcatCustomErrorHandler.sendCustomError(
  75.                     resp, 500, "Could not store conversationRequests!");
  76.         else {
  77.             resp.getOutputStream().write(gson.toJson(filteredConversationRequests).getBytes());
  78.             resp.flushBuffer();
  79.         }
  80.     }
  81. }