diff --git a/pom.xml b/pom.xml index ce1a15b..b0b0bd0 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.sadtech.vkbot vkbot-core - 0.2.0-RELEASE + 0.2.1-SNAPSHOT jar @@ -23,7 +23,7 @@ - 0.6.0-RELEASE + 0.6.1-SNAPSHOT 0.5.13-SNAPSHOT 1.2.17 diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/AbstractBasketSubscribe.java b/src/main/java/org/sadtech/vkbot/core/distribution/AbstractBasketSubscribe.java new file mode 100644 index 0000000..30947fc --- /dev/null +++ b/src/main/java/org/sadtech/vkbot/core/distribution/AbstractBasketSubscribe.java @@ -0,0 +1,53 @@ +package org.sadtech.vkbot.core.distribution; + +import java.util.Set; + +public abstract class AbstractBasketSubscribe implements EventSubscribe { + + private Set basketSubscribes; + private AbstractBasketSubscribe prioritySubscribe; + + public Set getBasketSubscribes() { + return basketSubscribes; + } + + public void setBasketSubscribes(Set basketSubscribes) { + this.basketSubscribes = basketSubscribes; + } + + public AbstractBasketSubscribe getPrioritySubscribe() { + return prioritySubscribe; + } + + public void setPrioritySubscribe(AbstractBasketSubscribe prioritySubscribe) { + this.prioritySubscribe = prioritySubscribe; + } + + protected abstract boolean check(S object); + + private boolean goNextSubscribe(S object) { + boolean flag = false; + if (prioritySubscribe != null && prioritySubscribe.check(object)) { + prioritySubscribe.update(object); + flag = true; + } else if (basketSubscribes != null) { + for (AbstractBasketSubscribe basketSubscribe : basketSubscribes) { + if (basketSubscribe.check(object)) { + basketSubscribe.update(object); + } else { + flag = true; + } + } + } + return flag; + } + + @Override + public void update(S object) { + if (!goNextSubscribe(object)) { + processing(object); + } + } + + public abstract void processing(S object); +} diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/AccountSubscribe.java b/src/main/java/org/sadtech/vkbot/core/distribution/AccountSubscribe.java new file mode 100644 index 0000000..790b61f --- /dev/null +++ b/src/main/java/org/sadtech/vkbot/core/distribution/AccountSubscribe.java @@ -0,0 +1,23 @@ +package org.sadtech.vkbot.core.distribution; + +import com.vk.api.sdk.objects.messages.Message; +import org.sadtech.bot.core.service.AccountService; + +public class AccountSubscribe extends AbstractBasketSubscribe { + + private final AccountService accountService; + + public AccountSubscribe(AccountService accountService) { + this.accountService = accountService; + } + + @Override + protected boolean check(Message userMessage) { + return userMessage.getAttachments().size() > 0 && "Денежный перевод".equals(userMessage.getAttachments().get(0).getLink().getCaption()); + } + + @Override + public void processing(Message object) { + accountService.pay(0); + } +} diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/EventDistributor.java b/src/main/java/org/sadtech/vkbot/core/distribution/EventDistributor.java index 051f3bb..5fbc61b 100644 --- a/src/main/java/org/sadtech/vkbot/core/distribution/EventDistributor.java +++ b/src/main/java/org/sadtech/vkbot/core/distribution/EventDistributor.java @@ -4,15 +4,11 @@ import com.google.gson.JsonObject; import org.apache.log4j.Logger; import org.sadtech.bot.core.service.RawEventService; -import java.util.HashMap; -import java.util.Map; - -public class EventDistributor implements Runnable { +public class EventDistributor extends AbstractBasketSubscribe implements Runnable { private static final Logger log = Logger.getLogger(EventDistributor.class); private final RawEventService rawEventService; - private final Map eventDistributionMap = new HashMap<>(); public EventDistributor(RawEventService rawEventService) { this.rawEventService = rawEventService; @@ -21,28 +17,27 @@ public class EventDistributor implements Runnable { public void update() { while (true) { - try { - if (rawEventService.getJsonObjects().peek() != null) { - JsonObject event = rawEventService.getJsonObjects().poll(); - log.info("Главный дистрибьютор отправил событие дальше"); - if (eventDistributionMap.containsKey(event.get("type").getAsString())) { - eventDistributionMap.get(event.get("type").getAsString()).update(event.getAsJsonObject("object")); - } - } - } catch (Exception e) { - log.error(e.getStackTrace()); - break; + if (rawEventService.getJsonObjects().peek() != null) { + JsonObject event = rawEventService.getJsonObjects().poll(); + log.info("Главный дистрибьютор отправил событие дальше"); + super.update(event); } } } - public void setEventDistributionMap(String key, EventSubscribe eventSubscribe) { - this.eventDistributionMap.put(key, eventSubscribe); - } - @Override public void run() { log.info("EventDistributor запущен"); update(); } + + @Override + protected boolean check(JsonObject object) { + return false; + } + + @Override + public void processing(JsonObject object) { + + } } 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 b4aeadf..c582499 100644 --- a/src/main/java/org/sadtech/vkbot/core/distribution/MailSubscriber.java +++ b/src/main/java/org/sadtech/vkbot/core/distribution/MailSubscriber.java @@ -11,68 +11,37 @@ 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.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; import java.time.Instant; import java.time.LocalDateTime; -import java.util.*; +import java.util.ArrayList; +import java.util.List; +import java.util.TimeZone; -public class MailSubscriber implements EventSubscribe { +public class MailSubscriber extends AbstractBasketSubscribe { private static final Logger log = Logger.getLogger(MailSubscriber.class); private final MailService mailService; - private Set admins = new HashSet<>(); - private final Map> eventDistributionMap = new HashMap<>(); public MailSubscriber(MailService mailService) { this.mailService = mailService; } - public void setAdmins(Set admins) { - this.admins = admins; - } - - public Set getAdmins() { - return admins; + @Override + protected boolean check(JsonObject object) { + String type = object.get("type").getAsString(); + return "message_new".equals(type); } @Override - public void update(JsonObject object) { - log.info("Дистрибьютор получил событие - сообщение"); + public void processing(JsonObject object) { Gson gson = new Gson(); - Message userMessage = gson.fromJson(object, Message.class); + Message userMessage = gson.fromJson(object.getAsJsonObject("object"), Message.class); log.info(userMessage); - - if (userMessage.getPeerId() > 2000000000) { - if (eventDistributionMap.containsKey("chat")) { - eventDistributionMap.get("chat").update(userMessage); - } - } else { - if (admins.contains(userMessage.getPeerId()) && eventDistributionMap.containsKey("terminal")) { - log.info("Сообщение отправлено в репозиторий команд"); - eventDistributionMap.get("terminal").update(userMessage); - } else { - log.info("Сообщение отправленно на добавление в репозиторий"); - mailService.add(createMaail(userMessage)); - } - } + mailService.add(createMail(userMessage)); } - 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) { + private Mail createMail(Message message) { Mail mail = new Mail(); mail.setMessage(message.getText()); mail.setDate(LocalDateTime.ofInstant(Instant.ofEpochSecond(message.getDate()), TimeZone.getDefault().toZoneId())); 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 6b3e8d9..09bb80b 100644 --- a/src/main/java/org/sadtech/vkbot/core/sender/MailSenderVk.java +++ b/src/main/java/org/sadtech/vkbot/core/sender/MailSenderVk.java @@ -9,7 +9,7 @@ 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.bot.core.service.sender.Sent; import org.sadtech.vkbot.core.VkConnect; import org.sadtech.vkbot.core.VkInsertData; import org.sadtech.vkbot.core.convert.KeyBoardConvert; @@ -68,8 +68,6 @@ public class MailSenderVk implements Sent { } - - private void sendMessage(MessagesSendQuery messages) { try { messages.execute();