From 8f5242c48f7aaac801b1b102d29f8a40c6e84e5e Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Sat, 17 Aug 2019 11:39:06 +0300 Subject: [PATCH] =?UTF-8?q?=D0=90=D0=BA=D1=82=D1=83=D0=B0=D0=BB=D0=B8?= =?UTF-8?q?=D0=B7=D0=B0=D1=86=D0=B8=D1=8F=20=D1=81=20socail-utils=20=D0=A0?= =?UTF-8?q?=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE=D1=80=D0=B8=D0=BD=D0=B3=20?= =?UTF-8?q?lombok=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20=D0=B2?= =?UTF-8?q?=D0=BB=D0=BE=D0=B6=D0=B5=D0=BD=D0=BD=D1=8B=D0=B5=20=D1=81=D0=BE?= =?UTF-8?q?=D0=BE=D0=B1=D1=89=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 7 --- .../core/convert/MessageMailConvert.java | 63 +++++++++++++++++-- .../subscriber/AccountSubscribe.java | 21 +++---- .../vkbot/core/domain/jpa/JsonObjectId.java | 2 +- .../vkbot/core/listener/EventListenerVk.java | 2 +- .../impl/RawEventRepositorySet.java | 5 +- .../core/sender/BoardCommentSenderVk.java | 10 ++- .../vkbot/core/sender/MailSenderVk.java | 17 +++-- .../org/sadtech/vkbot/core/utils/VkApi.java | 11 ++-- 9 files changed, 95 insertions(+), 43 deletions(-) diff --git a/pom.xml b/pom.xml index c078dc8..9474dd4 100644 --- a/pom.xml +++ b/pom.xml @@ -26,7 +26,6 @@ 0.6.3-SNAPSHOT 0.5.13-FORK - 1.2.17 @@ -36,12 +35,6 @@ ${vksdk.ver} - - log4j - log4j - ${log4j.ver} - - org.sadtech.social social-core diff --git a/src/main/java/org/sadtech/vkbot/core/convert/MessageMailConvert.java b/src/main/java/org/sadtech/vkbot/core/convert/MessageMailConvert.java index 1cd3d60..ef928a3 100644 --- a/src/main/java/org/sadtech/vkbot/core/convert/MessageMailConvert.java +++ b/src/main/java/org/sadtech/vkbot/core/convert/MessageMailConvert.java @@ -1,5 +1,6 @@ package org.sadtech.vkbot.core.convert; +import com.vk.api.sdk.objects.messages.ForeignMessage; import com.vk.api.sdk.objects.messages.Message; import com.vk.api.sdk.objects.messages.MessageAttachment; import org.sadtech.social.core.domain.content.Mail; @@ -9,6 +10,7 @@ import org.sadtech.social.core.domain.content.attachment.Geo; import java.time.Instant; import java.time.LocalDateTime; +import java.util.List; import java.util.TimeZone; import java.util.stream.Collectors; @@ -21,13 +23,62 @@ public class MessageMailConvert implements Convert { mail.setCreateDate(LocalDateTime.ofInstant(Instant.ofEpochSecond(message.getDate()), TimeZone.getDefault().toZoneId())); mail.setId(message.getId()); mail.setPersonId(message.getPeerId()); - mail.setAttachments(message.getAttachments() - .stream() - .map(this::convertAttachment) - .collect(Collectors.toList())); - if (message.getGeo() != null) { - mail.getAttachments().add(convertGeo(message.getGeo())); + List attachments = message.getAttachments(); + + if (attachments != null && !attachments.isEmpty()) { + mail.setAttachments( + attachments.stream() + .map(this::convertAttachment) + .collect(Collectors.toList()) + ); } + + com.vk.api.sdk.objects.base.Geo messageGeo = message.getGeo(); + if (messageGeo != null) { + mail.getAttachments().add(convertGeo(messageGeo)); + } + + List fwdMessages = message.getFwdMessages(); + if (fwdMessages != null && !fwdMessages.isEmpty()) { + mail.setForwardMail( + fwdMessages.stream() + .map(this::convertFwdMessage) + .collect(Collectors.toList()) + ); + } + mail.setAddDate(LocalDateTime.now()); + return mail; + } + + private Mail convertFwdMessage(ForeignMessage foreignMessage) { + Mail mail = new Mail(); + mail.setText(foreignMessage.getText()); + mail.setCreateDate(LocalDateTime.ofInstant(Instant.ofEpochSecond(foreignMessage.getDate()), TimeZone.getDefault().toZoneId())); + mail.setId(foreignMessage.getId()); + mail.setPersonId(foreignMessage.getPeerId()); + + List attachments = foreignMessage.getAttachments(); + if (attachments != null && !attachments.isEmpty()) { + mail.setAttachments( + attachments.stream() + .map(this::convertAttachment) + .collect(Collectors.toList()) + ); + } + + com.vk.api.sdk.objects.base.Geo messageGeo = foreignMessage.getGeo(); + if (messageGeo != null) { + mail.getAttachments().add(convertGeo(messageGeo)); + } + + List fwdMessages = foreignMessage.getFwdMessages(); + if (fwdMessages != null && !fwdMessages.isEmpty()) + mail.setForwardMail( + fwdMessages.stream() + .map(this::convertFwdMessage) + .collect(Collectors.toList()) + ); + mail.setAddDate(LocalDateTime.now()); return mail; } diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/subscriber/AccountSubscribe.java b/src/main/java/org/sadtech/vkbot/core/distribution/subscriber/AccountSubscribe.java index de6b979..53f6521 100644 --- a/src/main/java/org/sadtech/vkbot/core/distribution/subscriber/AccountSubscribe.java +++ b/src/main/java/org/sadtech/vkbot/core/distribution/subscriber/AccountSubscribe.java @@ -2,26 +2,21 @@ package org.sadtech.vkbot.core.distribution.subscriber; import com.vk.api.sdk.objects.messages.Message; import com.vk.api.sdk.objects.messages.MessageAttachmentType; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.sadtech.social.core.domain.BoxAnswer; import org.sadtech.social.core.exception.PaymentException; import org.sadtech.social.core.service.AccountService; -import org.sadtech.social.core.service.sender.Sent; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.sadtech.social.core.service.sender.Sending; +@Slf4j +@RequiredArgsConstructor public class AccountSubscribe extends AbstractBasketSubscribe { - private static final Logger log = LoggerFactory.getLogger(AccountSubscribe.class); - private final AccountService accountService; - private final Sent sent; + private final Sending sending; private BoxAnswer answerSuccessfulPayment; - public AccountSubscribe(AccountService accountService, Sent sent) { - this.accountService = accountService; - this.sent = sent; - } - @Override public boolean check(Message userMessage) { return userMessage.getAttachments().size() > 0 @@ -35,11 +30,11 @@ public class AccountSubscribe extends AbstractBasketSubscribe try { Integer valueSum = Integer.valueOf(message.getAttachments().get(0).getLink().getTitle().split(" ")[0]); if (accountService.pay(Integer.valueOf(message.getText()), message.getPeerId(), valueSum) && answerSuccessfulPayment != null) { - sent.send(message.getPeerId(), answerSuccessfulPayment); + sending.send(message.getPeerId(), answerSuccessfulPayment); } } catch (PaymentException e) { log.error(e.getMessage()); - sent.send(message.getPeerId(), BoxAnswer.builder().message(e.getDescription()).build()); + sending.send(message.getPeerId(), BoxAnswer.builder().message(e.getDescription()).build()); } } } diff --git a/src/main/java/org/sadtech/vkbot/core/domain/jpa/JsonObjectId.java b/src/main/java/org/sadtech/vkbot/core/domain/jpa/JsonObjectId.java index 5a22708..0adbae7 100644 --- a/src/main/java/org/sadtech/vkbot/core/domain/jpa/JsonObjectId.java +++ b/src/main/java/org/sadtech/vkbot/core/domain/jpa/JsonObjectId.java @@ -21,7 +21,7 @@ import javax.persistence.Table; @NoArgsConstructor public class JsonObjectId extends BasicEntity { - @Column(name = "json", length = 500) + @Column(name = "json", length = 3000) private String json; public JsonObjectId(String json) { 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 f5abb29..5499c96 100644 --- a/src/main/java/org/sadtech/vkbot/core/listener/EventListenerVk.java +++ b/src/main/java/org/sadtech/vkbot/core/listener/EventListenerVk.java @@ -50,7 +50,7 @@ public class EventListenerVk implements Runnable { } private LongPollServer getLongPollServer() throws ClientException, ApiException { - log.info("LongPoll сервер инициализирован"); + log.debug("LongPoll сервер инициализирован"); if (actor != null) { return vk.groups().getLongPollServer(actor, actor.getGroupId()).execute(); } else { diff --git a/src/main/java/org/sadtech/vkbot/core/repository/impl/RawEventRepositorySet.java b/src/main/java/org/sadtech/vkbot/core/repository/impl/RawEventRepositorySet.java index 41b0f6b..a3d271a 100644 --- a/src/main/java/org/sadtech/vkbot/core/repository/impl/RawEventRepositorySet.java +++ b/src/main/java/org/sadtech/vkbot/core/repository/impl/RawEventRepositorySet.java @@ -20,7 +20,10 @@ public class RawEventRepositorySet implements RawEventRepository { jsonObjects.clear(); } + @Override public Set findNewEvent() { - return jsonObjects; + Set copy = new HashSet<>(jsonObjects); + jsonObjects.removeAll(copy); + return copy; } } diff --git a/src/main/java/org/sadtech/vkbot/core/sender/BoardCommentSenderVk.java b/src/main/java/org/sadtech/vkbot/core/sender/BoardCommentSenderVk.java index e615ea2..078450f 100644 --- a/src/main/java/org/sadtech/vkbot/core/sender/BoardCommentSenderVk.java +++ b/src/main/java/org/sadtech/vkbot/core/sender/BoardCommentSenderVk.java @@ -10,11 +10,12 @@ import org.sadtech.social.core.domain.keyboard.KeyBoardButton; import org.sadtech.social.core.domain.keyboard.KeyBoardLine; import org.sadtech.social.core.domain.keyboard.button.KeyBoardButtonText; import org.sadtech.social.core.exception.MailSendException; -import org.sadtech.social.core.service.sender.Sent; +import org.sadtech.social.core.service.sender.SendType; +import org.sadtech.social.core.service.sender.Sending; import org.sadtech.vkbot.core.config.VkConnect; import org.sadtech.vkbot.core.utils.VkInsertData; -public class BoardCommentSenderVk implements Sent { +public class BoardCommentSenderVk implements Sending { private final VkApiClient vkApiClient; private final GroupActor groupActor; @@ -56,4 +57,9 @@ public class BoardCommentSenderVk implements Sent { e.printStackTrace(); } } + + @Override + public SendType getType() { + return SendType.PUBLIC; + } } 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 c99b130..cf975ab 100644 --- a/src/main/java/org/sadtech/vkbot/core/sender/MailSenderVk.java +++ b/src/main/java/org/sadtech/vkbot/core/sender/MailSenderVk.java @@ -6,10 +6,11 @@ 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 lombok.extern.slf4j.Slf4j; import org.sadtech.social.core.domain.BoxAnswer; import org.sadtech.social.core.domain.keyboard.KeyBoard; -import org.sadtech.social.core.service.sender.Sent; +import org.sadtech.social.core.service.sender.SendType; +import org.sadtech.social.core.service.sender.Sending; import org.sadtech.vkbot.core.config.VkConnect; import org.sadtech.vkbot.core.convert.KeyBoardConvert; import org.sadtech.vkbot.core.utils.VkInsertData; @@ -17,9 +18,8 @@ import org.sadtech.vkbot.core.utils.VkInsertData; import java.util.Collections; import java.util.concurrent.ThreadLocalRandom; -public class MailSenderVk implements Sent { - - private static final Logger log = Logger.getLogger(MailSenderVk.class); +@Slf4j +public class MailSenderVk implements Sending { private final VkApiClient vkApiClient; private final GroupActor groupActor; @@ -75,7 +75,7 @@ public class MailSenderVk implements Sent { try { messages.execute(); } catch (ApiException | ClientException e) { - log.error(e); + log.error(e.getMessage()); } } @@ -84,4 +84,9 @@ public class MailSenderVk implements Sent { } + @Override + public SendType getType() { + return SendType.PRIVATE; + } + } diff --git a/src/main/java/org/sadtech/vkbot/core/utils/VkApi.java b/src/main/java/org/sadtech/vkbot/core/utils/VkApi.java index 19a4423..1549c90 100644 --- a/src/main/java/org/sadtech/vkbot/core/utils/VkApi.java +++ b/src/main/java/org/sadtech/vkbot/core/utils/VkApi.java @@ -10,15 +10,14 @@ 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 org.apache.log4j.Logger; +import lombok.extern.slf4j.Slf4j; import org.sadtech.vkbot.core.config.VkConnect; import java.util.List; +@Slf4j public class VkApi { - private static final Logger log = Logger.getLogger(String.valueOf(VkApi.class)); - private final VkApiClient vk; private final ServiceActor actor; @@ -36,7 +35,7 @@ public class VkApi { JsonObject object = parser.parse(temp.get(0).toString()).getAsJsonObject(); userMin = gson.fromJson(object, UserMin.class); } catch (ApiException | ClientException e) { - log.error(e); + log.error(e.getMessage()); } return userMin; } @@ -46,7 +45,7 @@ public class VkApi { try { temp = vk.users().get(actor).userIds(String.valueOf(id)).fields(Fields.UNIVERSITIES).execute(); } catch (ApiException | ClientException e) { - log.error(e); + log.error(e.getMessage()); } return temp != null ? temp.get(0).getUniversities().get(0).getName() : null; } @@ -56,7 +55,7 @@ public class VkApi { try { temp = vk.users().get(actor).userIds(String.valueOf(id)).fields(Fields.CITY).execute(); } catch (ApiException | ClientException e) { - log.error(e); + log.error(e.getMessage()); } if (temp != null && checkCity(temp)) { return temp.get(0).getCity().getTitle();