Архитектурные изменения

This commit is contained in:
Mark Struchkov 2019-02-05 21:52:26 +03:00
parent ccdc44d689
commit f6e69a374c
9 changed files with 113 additions and 39 deletions

View File

@ -26,8 +26,7 @@ public class EventDistributorVK implements EventDistributable, Runnable {
if (eventService.getJsonObjects().peek() != null) { if (eventService.getJsonObjects().peek() != null) {
JsonObject event = eventService.getJsonObjects().poll(); JsonObject event = eventService.getJsonObjects().poll();
log.info("Главный дистрибьютор отправил событие дальше"); log.info("Главный дистрибьютор отправил событие дальше");
eventDistributionMap.get(event.get("type").toString()).update(event); eventDistributionMap.get(event.get("type").getAsString()).update(event.getAsJsonObject("object"));
} }
} }
} }

View File

@ -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<JsonObject> {
@Override
public void update(JsonObject object) {
}
}

View File

@ -18,13 +18,12 @@ public class MailSubscriber implements EventSubscribe<JsonObject>, EventDistribu
public static final Logger log = Logger.getLogger(MailSubscriber.class); public static final Logger log = Logger.getLogger(MailSubscriber.class);
private MailService mailService; private MailService mailService;
private Set<Integer> admins = new HashSet<>(); private Set<Integer> admins = new HashSet<>();
private Map<String, EventSubscribe<Message>> eventDistributionMap = new HashMap<>(); private Map<String, EventSubscribe<Message>> eventDistributionMap = new HashMap<>();
public MailSubscriber(EventDistributable eventDistributable, MailService mailService) { public MailSubscriber(EventDistributable eventDistributable, MailService mailService) {
this.mailService = mailService; this.mailService = mailService;
eventDistributable.registerSubscriber("\"message_new\"", this); eventDistributable.registerSubscriber("message_new", this);
} }
public MailService getMailService() { public MailService getMailService() {
@ -45,16 +44,23 @@ public class MailSubscriber implements EventSubscribe<JsonObject>, EventDistribu
@Override @Override
public void update(JsonObject object) { public void update(JsonObject object) {
log.info("Дистрибьютор получил событие - сообщение от пользователя"); log.info("Дистрибьютор получил событие - сообщение");
Gson gson = new Gson(); Gson gson = new Gson();
Message userMessage = gson.fromJson(object.getAsJsonObject("object"), Message.class); Message userMessage = gson.fromJson(object, Message.class);
if (admins.contains(userMessage.getUserId())) { // if (userMessage.getPeerId()>200000000) {
log.info("Сообщение отправлено в репозиторий команд"); // if (eventDistributionMap.containsKey("chat")) {
eventDistributionMap.get("terminal").update(userMessage); // eventDistributionMap.get("chat").update(userMessage);
} else { // }
log.info("Сообщение отправленно на добавление в репозиторий"); // } else {
mailService.add(userMessage); if (admins.contains(userMessage.getUserId())) {
} log.info("Сообщение отправлено в репозиторий команд");
eventDistributionMap.get("terminal").update(userMessage);
} else {
log.info("Сообщение отправленно на добавление в репозиторий");
mailService.add(userMessage);
}
// }
} }

View File

@ -7,6 +7,7 @@ public class Mail {
private Integer id; private Integer id;
private Integer date; private Integer date;
private Person person; private Person person;
private Integer peerId;
private String body; private String body;
public Mail() { public Mail() {
@ -52,6 +53,14 @@ public class Mail {
this.body = body; this.body = body;
} }
public Integer getPeerId() {
return peerId;
}
public void setPeerId(Integer peerId) {
this.peerId = peerId;
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;

View File

@ -0,0 +1,7 @@
package org.sadtech.vkbot.core.insert;
public interface Insert {
String insertWords();
}

View File

@ -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 org.sadtech.vkbot.core.entity.MailSend;
import java.util.regex.Matcher; import java.util.regex.Matcher;

View File

@ -7,7 +7,7 @@ import com.vk.api.sdk.exceptions.ClientException;
import com.vk.api.sdk.queries.messages.MessagesSendQuery; import com.vk.api.sdk.queries.messages.MessagesSendQuery;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.sadtech.vkbot.core.VkConnect; 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.entity.MailSend;
import org.sadtech.vkbot.core.insert.InsertWords; import org.sadtech.vkbot.core.insert.InsertWords;
@ -28,8 +28,8 @@ public class MailSanderVk implements MailSandler {
this.vkInsertData = new VkInsertData(vkConnect); this.vkInsertData = new VkInsertData(vkConnect);
} }
private MessagesSendQuery createMessage (MailSend mailSend, Integer idUser) { private MessagesSendQuery createMessage (MailSend mailSend, Integer peerId) {
MessagesSendQuery messages = vkApiClient.messages().send(groupActor).peerId(idUser); MessagesSendQuery messages = vkApiClient.messages().send(groupActor).peerId(peerId);
if (mailSend.getKeyboard() != null) { if (mailSend.getKeyboard() != null) {
messages.keyboard(mailSend.getKeyboard()); messages.keyboard(mailSend.getKeyboard());
} else { } else {
@ -40,7 +40,7 @@ public class MailSanderVk implements MailSandler {
} }
if (mailSend.getStickerId() != null) { if (mailSend.getStickerId() != null) {
try { 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) { } catch (ApiException | ClientException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -49,19 +49,40 @@ public class MailSanderVk implements MailSandler {
} }
@Override @Override
public void send(MailSend mailSend, Integer idUser) { public void send(MailSend mailSend, Integer peerId) {
MessagesSendQuery messages = createMessage(mailSend, idUser); MessagesSendQuery messages = createMessage(mailSend, peerId);
if (mailSend.getMessage() != null) { if (mailSend.getMessage() != null) {
messages.message(vkInsertData.insertWords(mailSend, idUser)); messages.message(mailSend.getMessage());
} }
sendMessage(messages); sendMessage(messages);
} }
public void send(MailSend mailSend, Integer idUser, List<String> insertWords) { @Override
MessagesSendQuery messages = createMessage(mailSend, idUser); 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<String> insertWords) {
MessagesSendQuery messages = createMessage(mailSend, peerId);
if (mailSend.getMessage() != null) { if (mailSend.getMessage() != null) {
InsertWords insert = new InsertWords(); 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<String> insertWords) {
MessagesSendQuery messages = createMessage(mailSend, peerId);
if (mailSend.getMessage() != null) {
InsertWords insert = new InsertWords();
insert.setInText(vkInsertData.insertWords(mailSend, peerId));
insert.insert(insertWords); insert.insert(insertWords);
messages.message(insert.getOutText()); messages.message(insert.getOutText());
} }

View File

@ -6,8 +6,12 @@ import java.util.List;
public interface MailSandler { public interface MailSandler {
void send(MailSend mailSend, Integer idUser); void send(MailSend mailSend, Integer peerId);
void send(MailSend mailSend, Integer idUser, List<String> insertWords); void send(MailSend mailSend, Integer peerId, Integer userId);
void send(MailSend mailSend, Integer peerId, List<String> insertWords);
void send(MailSend mailSend, Integer peerId, Integer userId, List<String> insertWords);
} }

View File

@ -5,6 +5,7 @@ import org.apache.log4j.Logger;
import org.sadtech.vkbot.core.entity.Mail; import org.sadtech.vkbot.core.entity.Mail;
import org.sadtech.vkbot.core.entity.Person; import org.sadtech.vkbot.core.entity.Person;
import org.sadtech.vkbot.core.repository.MailRepository; 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.PersonService;
import org.sadtech.vkbot.core.service.handlers.MailService; 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); public static final Logger log = Logger.getLogger(MailServiceImpl.class);
private MailRepository messageRepository; private MailRepository mailRepository;
private PersonService personService; private PersonService personService;
public MailServiceImpl(MailRepository messageRepository, PersonService personService) { public MailServiceImpl(PersonService personService) {
this.messageRepository = messageRepository; this.mailRepository = new MailRepositoryList();
this.personService = personService;
}
public MailServiceImpl(MailRepository mailRepository, PersonService personService) {
this.mailRepository = mailRepository;
this.personService = personService; this.personService = personService;
} }
@Override @Override
public void add(Mail mail) { public void add(Mail mail) {
messageRepository.add(mail); mailRepository.add(mail);
log.info("Сообщение добавлено в репозиторий"); log.info("Сообщение добавлено в репозиторий");
} }
@ -35,20 +41,29 @@ public class MailServiceImpl implements MailService {
public void add(Message message) { public void add(Message message) {
Mail mail = new Mail(); Mail mail = new Mail();
mail.setDate(message.getDate()); mail.setDate(message.getDate());
mail.setBody(message.getBody()); if (!message.getFromId().equals(message.getPeerId())) {
mail.setId(message.getId()); mail.setBody(message.getBody().replaceFirst("\\[club([0-9]+)\\|\\@\\w+] ", ""));
if (!personService.checkPerson(message.getUserId())) { } else {
personService.add(personService.createPerson(message.getUserId())); 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("Сообщение добавлено в репозиторий"); log.info("Сообщение добавлено в репозиторий");
messageRepository.add(mail); mailRepository.add(mail);
}
private void clean() {
} }
@Override @Override
public List<Mail> getFirstMailByTime(Integer timeFrom, Integer timeTo) { public List<Mail> getFirstMailByTime(Integer timeFrom, Integer timeTo) {
log.info("Запрошены сообщения от " + timeFrom + " до " + timeTo); log.info("Запрошены сообщения от " + timeFrom + " до " + timeTo);
List<Mail> mails = messageRepository.getMailByTime(timeFrom, timeTo); List<Mail> mails = mailRepository.getMailByTime(timeFrom, timeTo);
Set<Person> people = new HashSet<>(); Set<Person> people = new HashSet<>();
List<Mail> returnMails = new ArrayList<>(); List<Mail> returnMails = new ArrayList<>();
for (int i = mails.size() - 1; i >= 0; i--) { for (int i = mails.size() - 1; i >= 0; i--) {
@ -62,7 +77,7 @@ public class MailServiceImpl implements MailService {
@Override @Override
public List<Mail> getLastMailByTime(Integer timeFrom, Integer timeTo) { public List<Mail> getLastMailByTime(Integer timeFrom, Integer timeTo) {
List<Mail> mails = messageRepository.getMailByTime(timeFrom, timeTo); List<Mail> mails = mailRepository.getMailByTime(timeFrom, timeTo);
Set<Person> people = new HashSet<>(); Set<Person> people = new HashSet<>();
List<Mail> returnMails = new ArrayList<>(); List<Mail> returnMails = new ArrayList<>();
for (Mail mail : mails) { for (Mail mail : mails) {
@ -77,7 +92,7 @@ public class MailServiceImpl implements MailService {
@Override @Override
public List<Mail> getEvent(Integer timeFrom, Integer timeTo) { public List<Mail> getEvent(Integer timeFrom, Integer timeTo) {
log.info("Запрос на получение сообщений в интервале от " + timeFrom + " до " + timeTo); log.info("Запрос на получение сообщений в интервале от " + timeFrom + " до " + timeTo);
return messageRepository.getMailByTime(timeFrom, timeTo); return mailRepository.getMailByTime(timeFrom, timeTo);
} }
} }