diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/impl/EventDistributorVK.java b/src/main/java/org/sadtech/vkbot/core/distribution/impl/EventDistributorVK.java index 948d77e..d8b5d76 100644 --- a/src/main/java/org/sadtech/vkbot/core/distribution/impl/EventDistributorVK.java +++ b/src/main/java/org/sadtech/vkbot/core/distribution/impl/EventDistributorVK.java @@ -26,8 +26,7 @@ public class EventDistributorVK implements EventDistributable, Runnable { if (eventService.getJsonObjects().peek() != null) { JsonObject event = eventService.getJsonObjects().poll(); log.info("Главный дистрибьютор отправил событие дальше"); - eventDistributionMap.get(event.get("type").toString()).update(event); - + eventDistributionMap.get(event.get("type").getAsString()).update(event.getAsJsonObject("object")); } } } diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/impl/MailChatSubscriber.java b/src/main/java/org/sadtech/vkbot/core/distribution/impl/MailChatSubscriber.java new file mode 100644 index 0000000..121d141 --- /dev/null +++ b/src/main/java/org/sadtech/vkbot/core/distribution/impl/MailChatSubscriber.java @@ -0,0 +1,11 @@ +package org.sadtech.vkbot.core.distribution.impl; + +import com.google.gson.JsonObject; +import org.sadtech.vkbot.core.distribution.EventSubscribe; + +public class MailChatSubscriber implements EventSubscribe { + @Override + public void update(JsonObject object) { + + } +} diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/impl/MailSubscriber.java b/src/main/java/org/sadtech/vkbot/core/distribution/impl/MailSubscriber.java index debb99d..ffb2f04 100644 --- a/src/main/java/org/sadtech/vkbot/core/distribution/impl/MailSubscriber.java +++ b/src/main/java/org/sadtech/vkbot/core/distribution/impl/MailSubscriber.java @@ -18,13 +18,12 @@ public class MailSubscriber implements EventSubscribe, EventDistribu public static final Logger log = Logger.getLogger(MailSubscriber.class); private MailService mailService; - private Set admins = new HashSet<>(); private Map> eventDistributionMap = new HashMap<>(); public MailSubscriber(EventDistributable eventDistributable, MailService mailService) { this.mailService = mailService; - eventDistributable.registerSubscriber("\"message_new\"", this); + eventDistributable.registerSubscriber("message_new", this); } public MailService getMailService() { @@ -45,16 +44,23 @@ public class MailSubscriber implements EventSubscribe, EventDistribu @Override public void update(JsonObject object) { - log.info("Дистрибьютор получил событие - сообщение от пользователя"); + log.info("Дистрибьютор получил событие - сообщение"); Gson gson = new Gson(); - Message userMessage = gson.fromJson(object.getAsJsonObject("object"), Message.class); - if (admins.contains(userMessage.getUserId())) { - log.info("Сообщение отправлено в репозиторий команд"); - eventDistributionMap.get("terminal").update(userMessage); - } else { - log.info("Сообщение отправленно на добавление в репозиторий"); - mailService.add(userMessage); - } + Message userMessage = gson.fromJson(object, Message.class); +// if (userMessage.getPeerId()>200000000) { +// if (eventDistributionMap.containsKey("chat")) { +// eventDistributionMap.get("chat").update(userMessage); +// } +// } else { + if (admins.contains(userMessage.getUserId())) { + log.info("Сообщение отправлено в репозиторий команд"); + eventDistributionMap.get("terminal").update(userMessage); + } else { + log.info("Сообщение отправленно на добавление в репозиторий"); + mailService.add(userMessage); + } +// } + } diff --git a/src/main/java/org/sadtech/vkbot/core/entity/Mail.java b/src/main/java/org/sadtech/vkbot/core/entity/Mail.java index 0f5dd73..4a47d0e 100644 --- a/src/main/java/org/sadtech/vkbot/core/entity/Mail.java +++ b/src/main/java/org/sadtech/vkbot/core/entity/Mail.java @@ -7,6 +7,7 @@ public class Mail { private Integer id; private Integer date; private Person person; + private Integer peerId; private String body; public Mail() { @@ -52,6 +53,14 @@ public class Mail { this.body = body; } + public Integer getPeerId() { + return peerId; + } + + public void setPeerId(Integer peerId) { + this.peerId = peerId; + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/src/main/java/org/sadtech/vkbot/core/insert/Insert.java b/src/main/java/org/sadtech/vkbot/core/insert/Insert.java new file mode 100644 index 0000000..173541c --- /dev/null +++ b/src/main/java/org/sadtech/vkbot/core/insert/Insert.java @@ -0,0 +1,7 @@ +package org.sadtech.vkbot.core.insert; + +public interface Insert { + + String insertWords(); + +} diff --git a/src/main/java/org/sadtech/vkbot/core/VkInsertData.java b/src/main/java/org/sadtech/vkbot/core/insert/VkInsertData.java similarity index 90% rename from src/main/java/org/sadtech/vkbot/core/VkInsertData.java rename to src/main/java/org/sadtech/vkbot/core/insert/VkInsertData.java index be36efc..c641171 100644 --- a/src/main/java/org/sadtech/vkbot/core/VkInsertData.java +++ b/src/main/java/org/sadtech/vkbot/core/insert/VkInsertData.java @@ -1,5 +1,7 @@ -package org.sadtech.vkbot.core; +package org.sadtech.vkbot.core.insert; +import org.sadtech.vkbot.core.VkApi; +import org.sadtech.vkbot.core.VkConnect; import org.sadtech.vkbot.core.entity.MailSend; import java.util.regex.Matcher; diff --git a/src/main/java/org/sadtech/vkbot/core/sender/MailSanderVk.java b/src/main/java/org/sadtech/vkbot/core/sender/MailSanderVk.java index 85485fc..aa72851 100644 --- a/src/main/java/org/sadtech/vkbot/core/sender/MailSanderVk.java +++ b/src/main/java/org/sadtech/vkbot/core/sender/MailSanderVk.java @@ -7,7 +7,7 @@ import com.vk.api.sdk.exceptions.ClientException; import com.vk.api.sdk.queries.messages.MessagesSendQuery; import org.apache.log4j.Logger; import org.sadtech.vkbot.core.VkConnect; -import org.sadtech.vkbot.core.VkInsertData; +import org.sadtech.vkbot.core.insert.VkInsertData; import org.sadtech.vkbot.core.entity.MailSend; import org.sadtech.vkbot.core.insert.InsertWords; @@ -28,8 +28,8 @@ public class MailSanderVk implements MailSandler { this.vkInsertData = new VkInsertData(vkConnect); } - private MessagesSendQuery createMessage (MailSend mailSend, Integer idUser) { - MessagesSendQuery messages = vkApiClient.messages().send(groupActor).peerId(idUser); + private MessagesSendQuery createMessage (MailSend mailSend, Integer peerId) { + MessagesSendQuery messages = vkApiClient.messages().send(groupActor).peerId(peerId); if (mailSend.getKeyboard() != null) { messages.keyboard(mailSend.getKeyboard()); } else { @@ -40,7 +40,7 @@ public class MailSanderVk implements MailSandler { } if (mailSend.getStickerId() != null) { try { - vkApiClient.messages().send(groupActor).peerId(idUser).stickerId(mailSend.getStickerId()).execute(); + vkApiClient.messages().send(groupActor).peerId(peerId).stickerId(mailSend.getStickerId()).execute(); } catch (ApiException | ClientException e) { e.printStackTrace(); } @@ -49,19 +49,40 @@ public class MailSanderVk implements MailSandler { } @Override - public void send(MailSend mailSend, Integer idUser) { - MessagesSendQuery messages = createMessage(mailSend, idUser); + public void send(MailSend mailSend, Integer peerId) { + MessagesSendQuery messages = createMessage(mailSend, peerId); if (mailSend.getMessage() != null) { - messages.message(vkInsertData.insertWords(mailSend, idUser)); + messages.message(mailSend.getMessage()); } sendMessage(messages); } - public void send(MailSend mailSend, Integer idUser, List insertWords) { - MessagesSendQuery messages = createMessage(mailSend, idUser); + @Override + public void send(MailSend mailSend, Integer peerId, Integer userId) { + MessagesSendQuery messages = createMessage(mailSend, peerId); + if (mailSend.getMessage() != null) { + messages.message(vkInsertData.insertWords(mailSend, userId)); + } + sendMessage(messages); + } + + public void send(MailSend mailSend, Integer peerId, List insertWords) { + MessagesSendQuery messages = createMessage(mailSend, peerId); if (mailSend.getMessage() != null) { InsertWords insert = new InsertWords(); - insert.setInText(vkInsertData.insertWords(mailSend, idUser)); + insert.setInText(mailSend.getMessage()); + insert.insert(insertWords); + messages.message(insert.getOutText()); + } + sendMessage(messages); + } + + @Override + public void send(MailSend mailSend, Integer peerId, Integer userId, List insertWords) { + MessagesSendQuery messages = createMessage(mailSend, peerId); + if (mailSend.getMessage() != null) { + InsertWords insert = new InsertWords(); + insert.setInText(vkInsertData.insertWords(mailSend, peerId)); insert.insert(insertWords); messages.message(insert.getOutText()); } diff --git a/src/main/java/org/sadtech/vkbot/core/sender/MailSandler.java b/src/main/java/org/sadtech/vkbot/core/sender/MailSandler.java index e2998e1..bdcce23 100644 --- a/src/main/java/org/sadtech/vkbot/core/sender/MailSandler.java +++ b/src/main/java/org/sadtech/vkbot/core/sender/MailSandler.java @@ -6,8 +6,12 @@ import java.util.List; public interface MailSandler { - void send(MailSend mailSend, Integer idUser); + void send(MailSend mailSend, Integer peerId); - void send(MailSend mailSend, Integer idUser, List insertWords); + void send(MailSend mailSend, Integer peerId, Integer userId); + + void send(MailSend mailSend, Integer peerId, List insertWords); + + void send(MailSend mailSend, Integer peerId, Integer userId, List insertWords); } diff --git a/src/main/java/org/sadtech/vkbot/core/service/handlers/impl/MailServiceImpl.java b/src/main/java/org/sadtech/vkbot/core/service/handlers/impl/MailServiceImpl.java index 209aea2..21368c0 100644 --- a/src/main/java/org/sadtech/vkbot/core/service/handlers/impl/MailServiceImpl.java +++ b/src/main/java/org/sadtech/vkbot/core/service/handlers/impl/MailServiceImpl.java @@ -5,6 +5,7 @@ import org.apache.log4j.Logger; import org.sadtech.vkbot.core.entity.Mail; import org.sadtech.vkbot.core.entity.Person; import org.sadtech.vkbot.core.repository.MailRepository; +import org.sadtech.vkbot.core.repository.impl.MailRepositoryList; import org.sadtech.vkbot.core.service.PersonService; import org.sadtech.vkbot.core.service.handlers.MailService; @@ -17,17 +18,22 @@ public class MailServiceImpl implements MailService { public static final Logger log = Logger.getLogger(MailServiceImpl.class); - private MailRepository messageRepository; + private MailRepository mailRepository; private PersonService personService; - public MailServiceImpl(MailRepository messageRepository, PersonService personService) { - this.messageRepository = messageRepository; + public MailServiceImpl(PersonService personService) { + this.mailRepository = new MailRepositoryList(); + this.personService = personService; + } + + public MailServiceImpl(MailRepository mailRepository, PersonService personService) { + this.mailRepository = mailRepository; this.personService = personService; } @Override public void add(Mail mail) { - messageRepository.add(mail); + mailRepository.add(mail); log.info("Сообщение добавлено в репозиторий"); } @@ -35,20 +41,29 @@ public class MailServiceImpl implements MailService { public void add(Message message) { Mail mail = new Mail(); mail.setDate(message.getDate()); - mail.setBody(message.getBody()); - mail.setId(message.getId()); - if (!personService.checkPerson(message.getUserId())) { - personService.add(personService.createPerson(message.getUserId())); + if (!message.getFromId().equals(message.getPeerId())) { + mail.setBody(message.getBody().replaceFirst("\\[club([0-9]+)\\|\\@\\w+] ", "")); + } else { + mail.setBody(message.getBody()); } - mail.setPerson(personService.get(message.getUserId())); + mail.setId(message.getId()); + mail.setPeerId(message.getPeerId()); + if (!personService.checkPerson(message.getFromId())) { + personService.add(personService.createPerson(message.getFromId())); + } + mail.setPerson(personService.get(message.getFromId())); log.info("Сообщение добавлено в репозиторий"); - messageRepository.add(mail); + mailRepository.add(mail); + } + + private void clean() { + } @Override public List getFirstMailByTime(Integer timeFrom, Integer timeTo) { log.info("Запрошены сообщения от " + timeFrom + " до " + timeTo); - List mails = messageRepository.getMailByTime(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--) { @@ -62,7 +77,7 @@ public class MailServiceImpl implements MailService { @Override public List getLastMailByTime(Integer timeFrom, Integer timeTo) { - List mails = messageRepository.getMailByTime(timeFrom, timeTo); + List mails = mailRepository.getMailByTime(timeFrom, timeTo); Set people = new HashSet<>(); List returnMails = new ArrayList<>(); for (Mail mail : mails) { @@ -77,7 +92,7 @@ public class MailServiceImpl implements MailService { @Override public List getEvent(Integer timeFrom, Integer timeTo) { log.info("Запрос на получение сообщений в интервале от " + timeFrom + " до " + timeTo); - return messageRepository.getMailByTime(timeFrom, timeTo); + return mailRepository.getMailByTime(timeFrom, timeTo); } }