diff --git a/pom.xml b/pom.xml
index af7e00f..ce1a15b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
org.sadtech.vkbot
vkbot-core
- 0.1.0-RELEASE
+ 0.2.0-RELEASE
jar
@@ -23,6 +23,7 @@
+ 0.6.0-RELEASE
0.5.13-SNAPSHOT
1.2.17
@@ -43,8 +44,9 @@
org.sadtech.bot
bot-core
- 0.4.3-SNAPSHOT
+ ${bot.core.ver}
+
diff --git a/src/main/java/org/sadtech/vkbot/core/VkApi.java b/src/main/java/org/sadtech/vkbot/core/VkApi.java
index c4255b2..eebb050 100644
--- a/src/main/java/org/sadtech/vkbot/core/VkApi.java
+++ b/src/main/java/org/sadtech/vkbot/core/VkApi.java
@@ -7,9 +7,9 @@ import com.vk.api.sdk.client.VkApiClient;
import com.vk.api.sdk.client.actors.GroupActor;
import com.vk.api.sdk.exceptions.ApiException;
import com.vk.api.sdk.exceptions.ClientException;
+import com.vk.api.sdk.objects.users.Fields;
import com.vk.api.sdk.objects.users.UserMin;
import com.vk.api.sdk.objects.users.UserXtrCounters;
-import com.vk.api.sdk.queries.users.UserField;
import org.apache.log4j.Logger;
import java.util.List;
@@ -32,9 +32,7 @@ public class VkApi {
try {
List temp = vk.users().get(actor).userIds(String.valueOf(id)).execute();
JsonParser parser = new JsonParser();
- JsonObject object = parser.parse(temp.get(0).toString().substring(15)).getAsJsonObject();
- object.add("last_name", object.get("lastName"));
- object.add("first_name", object.get("firstName"));
+ JsonObject object = parser.parse(temp.get(0).toString()).getAsJsonObject();
userMin = gson.fromJson(object, UserMin.class);
} catch (ApiException | ClientException e) {
log.error(e);
@@ -45,7 +43,7 @@ public class VkApi {
public String getUserUniver(Integer id) {
List temp = null;
try {
- temp = vk.users().get(actor).userIds(String.valueOf(id)).fields(UserField.UNIVERSITIES).execute();
+ temp = vk.users().get(actor).userIds(String.valueOf(id)).fields(Fields.UNIVERSITIES).execute();
} catch (ApiException | ClientException e) {
log.error(e);
}
@@ -55,7 +53,7 @@ public class VkApi {
public String getUserCity(Integer id) {
List temp = null;
try {
- temp = vk.users().get(actor).userIds(String.valueOf(id)).fields(UserField.CITY).execute();
+ temp = vk.users().get(actor).userIds(String.valueOf(id)).fields(Fields.CITY).execute();
} catch (ApiException | ClientException e) {
log.error(e);
}
diff --git a/src/main/java/org/sadtech/vkbot/core/convert/KeyBoardConvert.java b/src/main/java/org/sadtech/vkbot/core/convert/KeyBoardConvert.java
new file mode 100644
index 0000000..69a9677
--- /dev/null
+++ b/src/main/java/org/sadtech/vkbot/core/convert/KeyBoardConvert.java
@@ -0,0 +1,70 @@
+package org.sadtech.vkbot.core.convert;
+
+import com.vk.api.sdk.objects.messages.*;
+import org.sadtech.bot.core.domain.keyboard.ButtonColor;
+import org.sadtech.bot.core.domain.keyboard.KeyBoard;
+import org.sadtech.bot.core.domain.keyboard.KeyBoardButton;
+import org.sadtech.bot.core.domain.keyboard.KeyBoardLine;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public final class KeyBoardConvert {
+
+ private KeyBoardConvert() {
+
+ }
+
+ public static Keyboard convertKeyboard(KeyBoard keyboard) {
+ Keyboard keyboardVk = new Keyboard();
+ keyboardVk.setOneTime(keyboard.isOneTime());
+
+ List> listButton = new ArrayList<>();
+ for (KeyBoardLine keyBoardLine : keyboard.getKeyBoardLines()) {
+ List buttonList = new ArrayList<>();
+ for (KeyBoardButton button : keyBoardLine.getKeyBoardButtons()) {
+ buttonList.add(convertButton(button));
+ }
+ listButton.add(buttonList);
+ }
+ keyboardVk.setButtons(listButton);
+
+ return keyboardVk;
+ }
+
+ private static KeyboardButton convertButton(KeyBoardButton button) {
+ KeyboardButton buttonVk = new KeyboardButton();
+ buttonVk.setColor(convertColor(button.getColor()));
+
+ KeyboardButtonAction buttonActionVk = new KeyboardButtonAction();
+ buttonActionVk.setLabel(button.getLabel());
+ buttonActionVk.setType(KeyboardButtonActionType.TEXT);
+ buttonActionVk.setPayload(button.getPayload());
+
+ buttonVk.setAction(buttonActionVk);
+ return buttonVk;
+ }
+
+
+ private static KeyboardButtonColor convertColor(ButtonColor color) {
+ KeyboardButtonColor buttonColorVk;
+ switch (color) {
+ case DEFAULT:
+ buttonColorVk = KeyboardButtonColor.DEFAULT;
+ break;
+ case PRIMARY:
+ buttonColorVk = KeyboardButtonColor.PRIMARY;
+ break;
+ case NEGATIVE:
+ buttonColorVk = KeyboardButtonColor.NEGATIVE;
+ break;
+ case POSITIVE:
+ buttonColorVk = KeyboardButtonColor.POSITIVE;
+ break;
+ default:
+ buttonColorVk = KeyboardButtonColor.DEFAULT;
+ }
+ return buttonColorVk;
+ }
+
+}
diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/MailSubscriber.java b/src/main/java/org/sadtech/vkbot/core/distribution/MailSubscriber.java
index f66f387..b4aeadf 100644
--- a/src/main/java/org/sadtech/vkbot/core/distribution/MailSubscriber.java
+++ b/src/main/java/org/sadtech/vkbot/core/distribution/MailSubscriber.java
@@ -3,14 +3,20 @@ package org.sadtech.vkbot.core.distribution;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.vk.api.sdk.objects.messages.Message;
+import com.vk.api.sdk.objects.messages.MessageAttachment;
+import com.vk.api.sdk.objects.messages.MessageAttachmentType;
import org.apache.log4j.Logger;
import org.sadtech.bot.core.domain.Mail;
-import org.sadtech.vkbot.core.service.distribution.MailService;
+import org.sadtech.bot.core.domain.attachment.Attachment;
+import org.sadtech.bot.core.domain.attachment.AudioMessage;
+import org.sadtech.bot.core.service.MailService;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.time.Instant;
+import java.time.LocalDateTime;
+import java.util.*;
public class MailSubscriber implements EventSubscribe {
@@ -37,12 +43,14 @@ public class MailSubscriber implements EventSubscribe {
log.info("Дистрибьютор получил событие - сообщение");
Gson gson = new Gson();
Message userMessage = gson.fromJson(object, Message.class);
+ log.info(userMessage);
+
if (userMessage.getPeerId() > 2000000000) {
if (eventDistributionMap.containsKey("chat")) {
eventDistributionMap.get("chat").update(userMessage);
}
} else {
- if (admins.contains(userMessage.getUserId()) && eventDistributionMap.containsKey("terminal")) {
+ if (admins.contains(userMessage.getPeerId()) && eventDistributionMap.containsKey("terminal")) {
log.info("Сообщение отправлено в репозиторий команд");
eventDistributionMap.get("terminal").update(userMessage);
} else {
@@ -52,12 +60,34 @@ public class MailSubscriber implements EventSubscribe {
}
}
+ public byte[] getBytes(InputStream inputStream) throws IOException {
+ ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
+ int bufferSize = 1024;
+ byte[] buffer = new byte[bufferSize];
+
+ int len = 0;
+ while ((len = inputStream.read(buffer)) != -1) {
+ byteBuffer.write(buffer, 0, len);
+ }
+ return byteBuffer.toByteArray();
+ }
+
private Mail createMaail(Message message) {
Mail mail = new Mail();
- mail.setMessage(message.getBody());
- mail.setDate(message.getDate());
+ mail.setMessage(message.getText());
+ mail.setDate(LocalDateTime.ofInstant(Instant.ofEpochSecond(message.getDate()), TimeZone.getDefault().toZoneId()));
mail.setId(message.getId());
mail.setPersonId(message.getPeerId());
+
+ List attachments = new ArrayList<>();
+ for (MessageAttachment attachment : message.getAttachments()) {
+ if (MessageAttachmentType.AUDIO_MESSAGE.equals(attachment.getType())) {
+ AudioMessage audioMessage = new AudioMessage();
+ audioMessage.setLinkOdd(attachment.getAudioMessage().getLinkOgg());
+ attachments.add(audioMessage);
+ }
+ }
+ mail.setAttachments(attachments);
return mail;
}
diff --git a/src/main/java/org/sadtech/vkbot/core/keyboard/ButtonKeyBoard.java b/src/main/java/org/sadtech/vkbot/core/keyboard/ButtonKeyBoard.java
deleted file mode 100644
index 58802e4..0000000
--- a/src/main/java/org/sadtech/vkbot/core/keyboard/ButtonKeyBoard.java
+++ /dev/null
@@ -1,67 +0,0 @@
-package org.sadtech.vkbot.core.keyboard;
-
-import com.google.gson.JsonObject;
-
-public class ButtonKeyBoard {
-
- private String type;
- private String payload;
- private String label;
- private ColorButton color;
-
- private ButtonKeyBoard() {
- throw new IllegalStateException();
- }
-
- public JsonObject getButton() {
- JsonObject newButton = new JsonObject();
- newButton.addProperty("color", color.toString().toLowerCase());
- newButton.add("action", generateAction());
- return newButton;
- }
-
- private JsonObject generateAction() {
- JsonObject action = new JsonObject();
- action.addProperty("type", type);
- action.addProperty("payload", payload);
- action.addProperty("label", label);
- return action;
- }
-
- public static Builder builder() {
- return new ButtonKeyBoard().new Builder();
- }
-
- public class Builder {
-
- private Builder() {
-
- }
-
- public Builder color(ColorButton color) {
- ButtonKeyBoard.this.color = color;
- return this;
- }
-
- public Builder label(String label) {
- ButtonKeyBoard.this.label = label;
- return this;
- }
-
- public Builder payload(String payload) {
- ButtonKeyBoard.this.payload = payload;
- return this;
- }
-
- public Builder type(String type) {
- ButtonKeyBoard.this.type = type;
- return this;
- }
-
- public ButtonKeyBoard build() {
- return ButtonKeyBoard.this;
- }
-
- }
-
-}
diff --git a/src/main/java/org/sadtech/vkbot/core/keyboard/ColorButton.java b/src/main/java/org/sadtech/vkbot/core/keyboard/ColorButton.java
deleted file mode 100644
index 6eb856b..0000000
--- a/src/main/java/org/sadtech/vkbot/core/keyboard/ColorButton.java
+++ /dev/null
@@ -1,7 +0,0 @@
-package org.sadtech.vkbot.core.keyboard;
-
-public enum ColorButton {
-
- PRIMARY, DEFAULT, NEGATIVE, POSITIVE
-
-}
diff --git a/src/main/java/org/sadtech/vkbot/core/keyboard/KeyBoard.java b/src/main/java/org/sadtech/vkbot/core/keyboard/KeyBoard.java
deleted file mode 100644
index 537b05e..0000000
--- a/src/main/java/org/sadtech/vkbot/core/keyboard/KeyBoard.java
+++ /dev/null
@@ -1,61 +0,0 @@
-package org.sadtech.vkbot.core.keyboard;
-
-import com.google.gson.JsonArray;
-import com.google.gson.JsonObject;
-
-import java.util.ArrayList;
-import java.util.List;
-
-public class KeyBoard {
-
- private List lineKeyBoards;
- private boolean oneTime;
-
- private KeyBoard() {
- lineKeyBoards = new ArrayList<>();
- throw new IllegalStateException();
- }
-
- public JsonObject getKeyboard() {
- JsonObject keyboard = new JsonObject();
- keyboard.addProperty("one_time", oneTime);
-
- JsonArray menuLine = new JsonArray();
- for (LineKeyBoard lineKeyboard : lineKeyBoards) {
- menuLine.add(lineKeyboard.getLine());
- }
-
- keyboard.add("buttons", menuLine);
- return keyboard;
- }
-
- public static Builder builder() {
- return new KeyBoard().new Builder();
- }
-
- public void addLine(LineKeyBoard lineKeyBoard) {
- lineKeyBoards.add(lineKeyBoard);
- }
-
- public class Builder {
-
- private Builder() {
-
- }
-
- public Builder lineKeyBoard(LineKeyBoard lineKeyBoard) {
- KeyBoard.this.lineKeyBoards.add(lineKeyBoard);
- return this;
- }
-
- public Builder oneTime(boolean oneTime) {
- KeyBoard.this.oneTime = oneTime;
- return this;
- }
-
- public KeyBoard build() {
- return KeyBoard.this;
- }
-
- }
-}
diff --git a/src/main/java/org/sadtech/vkbot/core/keyboard/KeyBoardService.java b/src/main/java/org/sadtech/vkbot/core/keyboard/KeyBoardService.java
deleted file mode 100644
index 11f70b1..0000000
--- a/src/main/java/org/sadtech/vkbot/core/keyboard/KeyBoardService.java
+++ /dev/null
@@ -1,34 +0,0 @@
-package org.sadtech.vkbot.core.keyboard;
-
-import java.util.List;
-
-public class KeyBoardService {
-
- private KeyBoardService() {
- throw new IllegalStateException();
- }
-
- public static KeyBoard keyBoardYesNo() {
- ButtonKeyBoard yesButton = ButtonKeyBoard.builder().color(ColorButton.POSITIVE).label("Да").payload("{\"button\": \"yes\"}").build();
- ButtonKeyBoard noButton = ButtonKeyBoard.builder().color(ColorButton.NEGATIVE).label("Нет").payload("{\"button\": \"no\"}").build();
- LineKeyBoard lineKeyBoard = LineKeyBoard.builder().setButtonKeyBoard(yesButton).setButtonKeyBoard(noButton).build();
- return KeyBoard.builder().lineKeyBoard(lineKeyBoard).oneTime(true).build();
- }
-
- public static KeyBoard verticalMenuString(List labelButtons) {
- KeyBoard keyBoard = KeyBoard.builder().oneTime(true).build();
- for (String labelButton : labelButtons) {
- ButtonKeyBoard buttonKeyBoard = ButtonKeyBoard.builder().label(labelButton).type("text").payload("{\"button\": \"" + labelButton + "\"}").build();
- keyBoard.addLine(LineKeyBoard.builder().setButtonKeyBoard(buttonKeyBoard).build());
- }
- return keyBoard;
- }
-
- public static KeyBoard verticalMenuButton(List buttonKeyBoards) {
- KeyBoard keyBoard = KeyBoard.builder().oneTime(true).build();
- for (ButtonKeyBoard buttonKeyBoard : buttonKeyBoards) {
- keyBoard.addLine(LineKeyBoard.builder().setButtonKeyBoard(buttonKeyBoard).build());
- }
- return keyBoard;
- }
-}
diff --git a/src/main/java/org/sadtech/vkbot/core/keyboard/LineKeyBoard.java b/src/main/java/org/sadtech/vkbot/core/keyboard/LineKeyBoard.java
deleted file mode 100644
index 6b28849..0000000
--- a/src/main/java/org/sadtech/vkbot/core/keyboard/LineKeyBoard.java
+++ /dev/null
@@ -1,47 +0,0 @@
-package org.sadtech.vkbot.core.keyboard;
-
-import com.google.gson.JsonArray;
-
-import java.util.ArrayList;
-import java.util.List;
-
-public class LineKeyBoard {
-
- private List buttonKeyBoards = new ArrayList<>();
-
- public LineKeyBoard() {
-
- }
-
- public LineKeyBoard(List buttonKeyBoards) {
- this.buttonKeyBoards = buttonKeyBoards;
- }
-
- public JsonArray getLine() {
- JsonArray line = new JsonArray();
- for (ButtonKeyBoard buttonKeyBoard : buttonKeyBoards) {
- line.add(buttonKeyBoard.getButton());
- }
- return line;
- }
-
- public static Builder builder() {
- return new LineKeyBoard().new Builder();
- }
-
- public class Builder {
-
- private Builder() {
-
- }
-
- public Builder setButtonKeyBoard(ButtonKeyBoard buttonKeyBoard) {
- LineKeyBoard.this.buttonKeyBoards.add(buttonKeyBoard);
- return this;
- }
-
- public LineKeyBoard build() {
- return LineKeyBoard.this;
- }
- }
-}
diff --git a/src/main/java/org/sadtech/vkbot/core/listener/EventListenerVk.java b/src/main/java/org/sadtech/vkbot/core/listener/EventListenerVk.java
index 57c86cb..1f923fc 100644
--- a/src/main/java/org/sadtech/vkbot/core/listener/EventListenerVk.java
+++ b/src/main/java/org/sadtech/vkbot/core/listener/EventListenerVk.java
@@ -7,7 +7,7 @@ import com.vk.api.sdk.client.actors.GroupActor;
import com.vk.api.sdk.exceptions.ApiException;
import com.vk.api.sdk.exceptions.ClientException;
import com.vk.api.sdk.exceptions.LongPollServerKeyExpiredException;
-import com.vk.api.sdk.objects.groups.responses.GetLongPollServerResponse;
+import com.vk.api.sdk.objects.groups.LongPollServer;
import org.apache.log4j.Logger;
import org.sadtech.bot.core.repository.impl.EventRepositoryQueue;
import org.sadtech.bot.core.service.RawEventService;
@@ -42,7 +42,7 @@ public class EventListenerVk implements Runnable {
}
public void listen() throws ClientException, ApiException {
- GetLongPollServerResponse longPollServer = getLongPollServer();
+ LongPollServer longPollServer = getLongPollServer();
int lastTimeStamp = longPollServer.getTs();
while (true) {
try {
@@ -62,10 +62,10 @@ public class EventListenerVk implements Runnable {
}
}
- private GetLongPollServerResponse getLongPollServer() throws ClientException, ApiException {
+ private LongPollServer getLongPollServer() throws ClientException, ApiException {
log.info("LongPoll сервер инициализирован");
if (actor != null) {
- return vk.groups().getLongPollServer(actor).execute();
+ return vk.groups().getLongPollServer(actor, actor.getGroupId()).execute();
} else {
throw new NullPointerException("Group actor");
}
diff --git a/src/main/java/org/sadtech/vkbot/core/sender/MailSenderVk.java b/src/main/java/org/sadtech/vkbot/core/sender/MailSenderVk.java
index bf76e32..6b3e8d9 100644
--- a/src/main/java/org/sadtech/vkbot/core/sender/MailSenderVk.java
+++ b/src/main/java/org/sadtech/vkbot/core/sender/MailSenderVk.java
@@ -1,15 +1,21 @@
package org.sadtech.vkbot.core.sender;
+import com.google.gson.Gson;
import com.vk.api.sdk.client.VkApiClient;
import com.vk.api.sdk.client.actors.GroupActor;
import com.vk.api.sdk.exceptions.ApiException;
import com.vk.api.sdk.exceptions.ClientException;
+import com.vk.api.sdk.objects.messages.Keyboard;
import com.vk.api.sdk.queries.messages.MessagesSendQuery;
import org.apache.log4j.Logger;
import org.sadtech.bot.core.domain.BoxAnswer;
import org.sadtech.bot.core.sender.Sent;
import org.sadtech.vkbot.core.VkConnect;
import org.sadtech.vkbot.core.VkInsertData;
+import org.sadtech.vkbot.core.convert.KeyBoardConvert;
+
+import java.util.Collections;
+import java.util.concurrent.ThreadLocalRandom;
public class MailSenderVk implements Sent {
@@ -19,6 +25,7 @@ public class MailSenderVk implements Sent {
private final GroupActor groupActor;
private final VkInsertData vkInsertData;
+ private final Gson gson = new Gson();
public MailSenderVk(VkConnect vkConnect) {
this.vkApiClient = vkConnect.getVkApiClient();
@@ -28,7 +35,7 @@ public class MailSenderVk implements Sent {
@Override
public void send(Integer idPerson, String message) {
- sendMessage(vkApiClient.messages().send(groupActor).peerId(idPerson).message(message));
+ sendMessage(vkApiClient.messages().send(groupActor).peerId(idPerson).message(message).randomId(ThreadLocalRandom.current().nextInt(0, Integer.MAX_VALUE)));
}
@Override
@@ -38,11 +45,14 @@ public class MailSenderVk implements Sent {
}
private MessagesSendQuery createMessage(BoxAnswer boxAnswer, Integer peerId) {
- MessagesSendQuery messages = vkApiClient.messages().send(groupActor).peerId(peerId).message(vkInsertData.insertWords(boxAnswer.getMessage(), peerId));
+ MessagesSendQuery messages = vkApiClient.messages().send(groupActor).peerId(peerId).message(vkInsertData.insertWords(boxAnswer.getMessage(), peerId)).randomId(ThreadLocalRandom.current().nextInt(0, Integer.MAX_VALUE));
if (boxAnswer.getKeyboard() != null) {
- messages.keyboard(boxAnswer.getKeyboard());
+ messages.keyboard(KeyBoardConvert.convertKeyboard(boxAnswer.getKeyboard()));
} else {
- messages.keyboard("{\"buttons\":[],\"one_time\":true}");
+ Keyboard keyboard = new Keyboard();
+ keyboard.setOneTime(true);
+ keyboard.setButtons(Collections.EMPTY_LIST);
+ messages.keyboard(keyboard);
}
if (boxAnswer.getLat() != null && boxAnswer.getaLong() != null) {
messages.lat(boxAnswer.getLat()).lng(boxAnswer.getaLong());
@@ -57,6 +67,9 @@ public class MailSenderVk implements Sent {
return messages;
}
+
+
+
private void sendMessage(MessagesSendQuery messages) {
try {
messages.execute();
@@ -64,4 +77,6 @@ public class MailSenderVk implements Sent {
log.error(e);
}
}
+
+
}
diff --git a/src/main/java/org/sadtech/vkbot/core/service/distribution/CommentService.java b/src/main/java/org/sadtech/vkbot/core/service/distribution/CommentService.java
deleted file mode 100644
index 6830e21..0000000
--- a/src/main/java/org/sadtech/vkbot/core/service/distribution/CommentService.java
+++ /dev/null
@@ -1,10 +0,0 @@
-package org.sadtech.vkbot.core.service.distribution;
-
-import org.sadtech.bot.core.domain.Comment;
-import org.sadtech.bot.core.service.EventService;
-
-//@TODO: Дописать класс
-public interface CommentService extends EventService {
-
-
-}
diff --git a/src/main/java/org/sadtech/vkbot/core/service/distribution/MailService.java b/src/main/java/org/sadtech/vkbot/core/service/distribution/MailService.java
deleted file mode 100644
index 5d1bbe7..0000000
--- a/src/main/java/org/sadtech/vkbot/core/service/distribution/MailService.java
+++ /dev/null
@@ -1,8 +0,0 @@
-package org.sadtech.vkbot.core.service.distribution;
-
-import org.sadtech.bot.core.domain.Mail;
-import org.sadtech.bot.core.service.EventService;
-
-public interface MailService extends EventService {
-
-}
diff --git a/src/main/java/org/sadtech/vkbot/core/service/distribution/impl/MailServiceImpl.java b/src/main/java/org/sadtech/vkbot/core/service/distribution/impl/MailServiceImpl.java
deleted file mode 100644
index ebbef13..0000000
--- a/src/main/java/org/sadtech/vkbot/core/service/distribution/impl/MailServiceImpl.java
+++ /dev/null
@@ -1,70 +0,0 @@
-package org.sadtech.vkbot.core.service.distribution.impl;
-
-import org.apache.log4j.Logger;
-import org.sadtech.bot.core.domain.Mail;
-import org.sadtech.bot.core.repository.MailRepository;
-import org.sadtech.bot.core.repository.impl.MailRepositoryList;
-import org.sadtech.vkbot.core.service.distribution.MailService;
-
-import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-
-public class MailServiceImpl implements MailService {
-
- private static final Logger log = Logger.getLogger(MailServiceImpl.class);
-
- private final MailRepository mailRepository;
-
- public MailServiceImpl() {
- this.mailRepository = new MailRepositoryList();
- }
-
- public MailServiceImpl(MailRepository mailRepository) {
- this.mailRepository = mailRepository;
- }
-
- @Override
- public void add(Mail mail) {
- mailRepository.add(mail);
- log.info("Сообщение добавлено в репозиторий");
- log.info(mail);
- }
-
- @Override
- public List getFirstEventByTime(Integer timeFrom, Integer timeTo) {
- log.info("Запрошены сообщения " + timeFrom + "-" + timeTo);
- List mails = mailRepository.getMailByTime(timeFrom, timeTo);
- Set people = new HashSet<>();
- List returnMails = new ArrayList<>();
- for (int i = mails.size() - 1; i >= 0; i--) {
- if (!people.contains(mails.get(i).getPersonId())) {
- returnMails.add(mails.get(i));
- people.add(mails.get(i).getPersonId());
- }
- }
- return returnMails;
- }
-
- @Override
- public List getLastEventByTime(Integer timeFrom, Integer timeTo) {
- List mails = mailRepository.getMailByTime(timeFrom, timeTo);
- Set people = new HashSet<>();
- List returnMails = new ArrayList<>();
- for (Mail mail : mails) {
- if (!people.contains(mail.getPersonId())) {
- returnMails.add(mail);
- people.add(mail.getPersonId());
- }
- }
- return returnMails;
- }
-
- @Override
- public List getEvent(Integer timeFrom, Integer timeTo) {
- log.info("Запрос на получение сообщений в интервале от " + timeFrom + " до " + timeTo);
- return mailRepository.getMailByTime(timeFrom, timeTo);
- }
-
-}