ConversationServlet.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.serialization.adapters.GsonFactory;
  27. import net.jami.jams.common.serialization.tomcat.TomcatCustomErrorHandler;
  28. import net.jami.jams.common.utils.ConversationMerger;

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

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

  35.     /**
  36.      * @apiVersion 1.0.0
  37.      * @api {post} /api/auth/conversations Sync conversation list
  38.      * @apiName postConversations
  39.      * @apiGroup Conversations
  40.      * @apiParam {body} Conversation JSON representation of the conversation object
  41.      * @apiParamExample {json} Request-Example: [ {"id":"1231221","created":1594742298377},
  42.      *     {"id":"213123","removed":1594742298377} ]
  43.      * @apiSuccess (200) {json} Conversation[] user's conversations
  44.      * @apiError (500) {null} null conversation was unable to be synced
  45.      */
  46.     @Override
  47.     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
  48.             throws ServletException, IOException {
  49.         String owner = req.getAttribute("username").toString();
  50.         addConversations(req, resp, owner);
  51.     }

  52.     public static void addConversations(
  53.             HttpServletRequest req, HttpServletResponse resp, String owner) throws IOException {
  54.         List<Conversation> remoteList =
  55.                 Arrays.asList(gson.fromJson(req.getReader(), Conversation[].class));
  56.         List<Conversation> localList = dataStore.getConversationDao().getByOwner(owner);
  57.         remoteList.forEach(conversation -> conversation.setOwner(owner));
  58.         List<Conversation> result = ConversationMerger.mergeConversations(localList, remoteList);

  59.         if (result.size() > 0 && !dataStore.getConversationDao().storeConversationList(result))
  60.             TomcatCustomErrorHandler.sendCustomError(resp, 500, "Unable to store conversations!");
  61.         else {
  62.             resp.getOutputStream().write(gson.toJson(result).getBytes());
  63.             resp.flushBuffer();
  64.         }
  65.     }
  66. }