Большие архитектурные изменения
* Добавлен новый класс, для хранения терминальных команд от администратора бота * Некоторые интерфесы переписаны под дженерики * Архитектура стала более логичной и продуманной * Рефакторинг
This commit is contained in:
parent
ac50183423
commit
c50ce8cd40
5
pom.xml
5
pom.xml
@ -56,6 +56,11 @@
|
|||||||
<artifactId>json</artifactId>
|
<artifactId>json</artifactId>
|
||||||
<version>${json.ver}</version>
|
<version>${json.ver}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.12</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,15 +1,21 @@
|
|||||||
package org.sadtech.vkbot.core;
|
package org.sadtech.vkbot.core;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import com.google.gson.JsonParser;
|
||||||
import com.vk.api.sdk.client.VkApiClient;
|
import com.vk.api.sdk.client.VkApiClient;
|
||||||
import com.vk.api.sdk.client.actors.GroupActor;
|
import com.vk.api.sdk.client.actors.GroupActor;
|
||||||
import com.vk.api.sdk.exceptions.ApiException;
|
import com.vk.api.sdk.exceptions.ApiException;
|
||||||
import com.vk.api.sdk.exceptions.ClientException;
|
import com.vk.api.sdk.exceptions.ClientException;
|
||||||
|
import com.vk.api.sdk.objects.users.UserMin;
|
||||||
import com.vk.api.sdk.objects.users.UserXtrCounters;
|
import com.vk.api.sdk.objects.users.UserXtrCounters;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class VkApi {
|
public class VkApi {
|
||||||
|
|
||||||
|
// public static final Logger log = Logger.getLogger(String.valueOf(VkApi.class));
|
||||||
|
|
||||||
private VkApiClient vk;
|
private VkApiClient vk;
|
||||||
private GroupActor actor;
|
private GroupActor actor;
|
||||||
|
|
||||||
@ -18,14 +24,20 @@ public class VkApi {
|
|||||||
actor = vkConnect.getGroupActor();
|
actor = vkConnect.getGroupActor();
|
||||||
}
|
}
|
||||||
|
|
||||||
public UserXtrCounters getUserVk(Integer id) {
|
public UserMin getUserMini(Integer id) {
|
||||||
List<UserXtrCounters> temp = null;
|
UserMin userMin = null;
|
||||||
|
Gson gson = new Gson();
|
||||||
try {
|
try {
|
||||||
temp = vk.users().get(actor).userIds(String.valueOf(id)).execute();
|
List<UserXtrCounters> 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"));
|
||||||
|
userMin = gson.fromJson(object, UserMin.class);
|
||||||
} catch (ApiException | ClientException e) {
|
} catch (ApiException | ClientException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
return temp.get(0);
|
return userMin;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,9 @@
|
|||||||
|
package org.sadtech.vkbot.core.distribution;
|
||||||
|
|
||||||
|
public interface EventDistributable {
|
||||||
|
|
||||||
|
void distribute() throws Exception;
|
||||||
|
|
||||||
|
void registerSubscriber(String key, EventSubscribe o);
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package org.sadtech.vkbot.core.distribution;
|
||||||
|
|
||||||
|
public interface EventSubscribe<T> {
|
||||||
|
|
||||||
|
void update(T object);
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
package org.sadtech.vkbot.core.distribution.impl;
|
||||||
|
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
import org.sadtech.vkbot.core.distribution.EventDistributable;
|
||||||
|
import org.sadtech.vkbot.core.distribution.EventSubscribe;
|
||||||
|
import org.sadtech.vkbot.core.service.EventService;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class EventDistributorVK implements EventDistributable, Runnable {
|
||||||
|
|
||||||
|
public static final Logger log = Logger.getLogger(EventDistributorVK.class);
|
||||||
|
|
||||||
|
private EventService eventService;
|
||||||
|
private Map<String, EventSubscribe> eventDistributionMap = new HashMap<>();
|
||||||
|
|
||||||
|
public EventDistributorVK(EventService eventService) {
|
||||||
|
this.eventService = eventService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void distribute() {
|
||||||
|
while (true) {
|
||||||
|
if (eventService.getJsonObjects().peek() != null) {
|
||||||
|
JsonObject event = eventService.getJsonObjects().poll();
|
||||||
|
log.info("Главный дистрибьютор отправил событие дальше");
|
||||||
|
eventDistributionMap.get(event.get("type").toString()).update(event);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void registerSubscriber(String key, EventSubscribe o) {
|
||||||
|
eventDistributionMap.put(key, o);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
distribute();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,71 @@
|
|||||||
|
package org.sadtech.vkbot.core.distribution.impl;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import com.vk.api.sdk.objects.messages.Message;
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
import org.sadtech.vkbot.core.distribution.EventDistributable;
|
||||||
|
import org.sadtech.vkbot.core.distribution.EventSubscribe;
|
||||||
|
import org.sadtech.vkbot.core.service.handlers.MailService;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class MailSubscriber implements EventSubscribe<JsonObject>, EventDistributable {
|
||||||
|
|
||||||
|
public static final Logger log = Logger.getLogger(MailSubscriber.class);
|
||||||
|
|
||||||
|
private MailService mailService;
|
||||||
|
|
||||||
|
private Set<Integer> admins = new HashSet<>();
|
||||||
|
private Map<String, EventSubscribe> eventDistributionMap = new HashMap<>();
|
||||||
|
|
||||||
|
public MailSubscriber(EventDistributable eventDistributable, MailService mailService) {
|
||||||
|
this.mailService = mailService;
|
||||||
|
eventDistributable.registerSubscriber("\"message_new\"", this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public MailService getMailService() {
|
||||||
|
return mailService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMailService(MailService mailService) {
|
||||||
|
this.mailService = mailService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAdmins(Set<Integer> admins) {
|
||||||
|
this.admins = admins;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<Integer> getAdmins() {
|
||||||
|
return admins;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void update(JsonObject object) {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void distribute() throws Exception {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void registerSubscriber(String key, EventSubscribe o) {
|
||||||
|
eventDistributionMap.put(key, o);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
package org.sadtech.vkbot.core.distribution.impl;
|
||||||
|
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
import org.sadtech.vkbot.core.distribution.EventSubscribe;
|
||||||
|
import org.sadtech.vkbot.core.entity.Mail;
|
||||||
|
import org.sadtech.vkbot.core.service.handlers.MailService;
|
||||||
|
|
||||||
|
public class TerminalSubscriber implements EventSubscribe<Mail> {
|
||||||
|
|
||||||
|
public static final Logger log = Logger.getLogger(TerminalSubscriber.class);
|
||||||
|
|
||||||
|
private MailService mailService;
|
||||||
|
|
||||||
|
public TerminalSubscriber(MailSubscriber mailSubscriber, MailService mailService) {
|
||||||
|
this.mailService = mailService;
|
||||||
|
mailSubscriber.registerSubscriber("terminal", this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public MailService getMailService() {
|
||||||
|
return mailService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMailService(MailService mailService) {
|
||||||
|
this.mailService = mailService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void update(Mail object) {
|
||||||
|
log.info("Сообщение отправленно на добавление в репозиторий команд");
|
||||||
|
mailService.add(object);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package org.sadtech.vkbot.core.sender;
|
package org.sadtech.vkbot.core.entity;
|
||||||
|
|
||||||
public class MailSend {
|
public class MailSend {
|
||||||
|
|
@ -1,13 +0,0 @@
|
|||||||
package org.sadtech.vkbot.core.handlers;
|
|
||||||
|
|
||||||
import org.sadtech.vkbot.core.service.EventService;
|
|
||||||
|
|
||||||
public interface Handled extends Runnable {
|
|
||||||
|
|
||||||
void sortAndSend() throws Exception;
|
|
||||||
|
|
||||||
void registerObserver(String key, ProcessableEvent o);
|
|
||||||
|
|
||||||
EventService getResponsibleService();
|
|
||||||
|
|
||||||
}
|
|
@ -1,12 +0,0 @@
|
|||||||
package org.sadtech.vkbot.core.handlers;
|
|
||||||
|
|
||||||
import com.google.gson.JsonObject;
|
|
||||||
import org.sadtech.vkbot.core.service.handlers.SortEventService;
|
|
||||||
|
|
||||||
public interface ProcessableEvent {
|
|
||||||
|
|
||||||
void update(JsonObject object);
|
|
||||||
|
|
||||||
SortEventService getServiceEventData();
|
|
||||||
|
|
||||||
}
|
|
@ -1,44 +0,0 @@
|
|||||||
package org.sadtech.vkbot.core.handlers.impl;
|
|
||||||
|
|
||||||
import com.google.gson.JsonObject;
|
|
||||||
import org.sadtech.vkbot.core.handlers.Handled;
|
|
||||||
import org.sadtech.vkbot.core.handlers.ProcessableEvent;
|
|
||||||
import org.sadtech.vkbot.core.service.EventService;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class DispatcherHandlerVk implements Handled, Runnable {
|
|
||||||
|
|
||||||
private EventService responsibleService;
|
|
||||||
private Map<String, ProcessableEvent> processableEventMap = new HashMap<>();
|
|
||||||
|
|
||||||
public DispatcherHandlerVk(EventService responsibleService) {
|
|
||||||
this.responsibleService = responsibleService;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void sortAndSend() {
|
|
||||||
while (true) {
|
|
||||||
if (responsibleService.getJsonObjects().peek() != null) {
|
|
||||||
JsonObject event = responsibleService.getJsonObjects().poll();
|
|
||||||
processableEventMap.get(event.get("type").toString()).update(event);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void registerObserver(String key, ProcessableEvent o) {
|
|
||||||
processableEventMap.put(key, o);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public EventService getResponsibleService() {
|
|
||||||
return responsibleService;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
sortAndSend();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,83 +0,0 @@
|
|||||||
package org.sadtech.vkbot.core.handlers.impl;
|
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
|
||||||
import com.google.gson.JsonObject;
|
|
||||||
import com.vk.api.sdk.objects.messages.Message;
|
|
||||||
import org.apache.log4j.Logger;
|
|
||||||
import org.sadtech.vkbot.core.entity.Mail;
|
|
||||||
import org.sadtech.vkbot.core.entity.Person;
|
|
||||||
import org.sadtech.vkbot.core.handlers.Handled;
|
|
||||||
import org.sadtech.vkbot.core.handlers.ProcessableEvent;
|
|
||||||
import org.sadtech.vkbot.core.service.PersonService;
|
|
||||||
import org.sadtech.vkbot.core.service.handlers.MailService;
|
|
||||||
import org.sadtech.vkbot.core.service.handlers.SortEventService;
|
|
||||||
import org.sadtech.vkbot.core.service.handlers.impl.MailServiceImpl;
|
|
||||||
import org.sadtech.vkbot.core.service.impl.PersonServiceImpl;
|
|
||||||
|
|
||||||
public class MailHandlerVk implements ProcessableEvent {
|
|
||||||
|
|
||||||
public static final Logger log = Logger.getLogger(MailHandlerVk.class);
|
|
||||||
|
|
||||||
private PersonService personService;
|
|
||||||
private MailService mailService;
|
|
||||||
|
|
||||||
public MailHandlerVk(Handled dispatcherHandler) {
|
|
||||||
personService = new PersonServiceImpl();
|
|
||||||
mailService = new MailServiceImpl();
|
|
||||||
dispatcherHandler.registerObserver("\"message_new\"", this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public MailHandlerVk(Handled dispatcherHandler, PersonService personService, MailService mailService) {
|
|
||||||
this.personService = personService;
|
|
||||||
this.mailService = mailService;
|
|
||||||
dispatcherHandler.registerObserver("\"message_new\"", this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public PersonService getPersonService() {
|
|
||||||
return personService;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPersonService(PersonService personService) {
|
|
||||||
this.personService = personService;
|
|
||||||
}
|
|
||||||
|
|
||||||
public MailService getMailService() {
|
|
||||||
return mailService;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setMailService(MailService mailService) {
|
|
||||||
this.mailService = mailService;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void update(JsonObject object) {
|
|
||||||
Gson gson = new Gson();
|
|
||||||
Message userMessage = gson.fromJson(object.getAsJsonObject("object"), Message.class);
|
|
||||||
if (!personService.checkPerson(userMessage.getUserId())) {
|
|
||||||
Person person = new Person();
|
|
||||||
person.setId(userMessage.getUserId());
|
|
||||||
log.info("Пользователь отправлен на добавление в репозиторий");
|
|
||||||
personService.add(person);
|
|
||||||
}
|
|
||||||
log.info("Сообщение отправленно на добавление в репозиторий");
|
|
||||||
mailService.add(createMail(userMessage));
|
|
||||||
}
|
|
||||||
|
|
||||||
private Mail createMail(Message message) {
|
|
||||||
Mail mail = new Mail();
|
|
||||||
mail.setDate(message.getDate());
|
|
||||||
mail.setBody(message.getBody());
|
|
||||||
mail.setId(message.getId());
|
|
||||||
if (personService.checkPerson(message.getUserId())) {
|
|
||||||
mail.setPerson(personService.get(message.getUserId()));
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return mail;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public SortEventService getServiceEventData() {
|
|
||||||
return mailService;
|
|
||||||
}
|
|
||||||
}
|
|
@ -11,7 +11,6 @@ import com.vk.api.sdk.exceptions.ClientException;
|
|||||||
import com.vk.api.sdk.objects.groups.responses.GetLongPollServerResponse;
|
import com.vk.api.sdk.objects.groups.responses.GetLongPollServerResponse;
|
||||||
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.repository.EventRepository;
|
|
||||||
import org.sadtech.vkbot.core.repository.impl.EventRepositoryQueue;
|
import org.sadtech.vkbot.core.repository.impl.EventRepositoryQueue;
|
||||||
import org.sadtech.vkbot.core.service.EventService;
|
import org.sadtech.vkbot.core.service.EventService;
|
||||||
import org.sadtech.vkbot.core.service.impl.EventServiceImpl;
|
import org.sadtech.vkbot.core.service.impl.EventServiceImpl;
|
||||||
@ -36,10 +35,10 @@ public class EventListenerVk implements EventListener, Runnable {
|
|||||||
longPoll = new LongPoll(vk);
|
longPoll = new LongPoll(vk);
|
||||||
}
|
}
|
||||||
|
|
||||||
public EventListenerVk(VkConnect vkConnect, EventRepository eventRepository) {
|
public EventListenerVk(VkConnect vkConnect, EventService eventService) {
|
||||||
this.vk = vkConnect.getVkApiClient();
|
this.vk = vkConnect.getVkApiClient();
|
||||||
this.actor = vkConnect.getGroupActor();
|
this.actor = vkConnect.getGroupActor();
|
||||||
this.eventService = new EventServiceImpl(eventRepository);
|
this.eventService = eventService;
|
||||||
longPoll = new LongPoll(vk);
|
longPoll = new LongPoll(vk);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,17 +1,13 @@
|
|||||||
package org.sadtech.vkbot.core.repository;
|
package org.sadtech.vkbot.core.repository;
|
||||||
|
|
||||||
import com.google.gson.JsonObject;
|
|
||||||
|
|
||||||
import java.util.Queue;
|
import java.util.Queue;
|
||||||
|
|
||||||
public interface EventRepository {
|
public interface EventRepository<T> {
|
||||||
|
|
||||||
void add(JsonObject jsonObject);
|
void add(T dataObject);
|
||||||
|
|
||||||
void remove(int id);
|
|
||||||
|
|
||||||
void cleanAll();
|
void cleanAll();
|
||||||
|
|
||||||
Queue<JsonObject> getJsonObjects();
|
Queue<T> getEventQueue();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ import java.util.List;
|
|||||||
|
|
||||||
public interface MailRepository {
|
public interface MailRepository {
|
||||||
|
|
||||||
void add(Mail message);
|
void add(Mail mail);
|
||||||
|
|
||||||
List<Mail> getMailByTime(Integer timeFrom, Integer timeTo);
|
List<Mail> getMailByTime(Integer timeFrom, Integer timeTo);
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@ import org.sadtech.vkbot.core.repository.EventRepository;
|
|||||||
import java.util.Queue;
|
import java.util.Queue;
|
||||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
|
|
||||||
public class EventRepositoryQueue implements EventRepository {
|
public class EventRepositoryQueue implements EventRepository<JsonObject> {
|
||||||
|
|
||||||
private Queue<JsonObject> jsonObjects = new ConcurrentLinkedQueue<>();
|
private Queue<JsonObject> jsonObjects = new ConcurrentLinkedQueue<>();
|
||||||
|
|
||||||
@ -15,17 +15,12 @@ public class EventRepositoryQueue implements EventRepository {
|
|||||||
jsonObjects.offer(jsonObject);
|
jsonObjects.offer(jsonObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void remove(int id) {
|
|
||||||
jsonObjects.remove(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void cleanAll() {
|
public void cleanAll() {
|
||||||
jsonObjects.clear();
|
jsonObjects.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Queue<JsonObject> getJsonObjects() {
|
public Queue<JsonObject> getEventQueue() {
|
||||||
return jsonObjects;
|
return jsonObjects;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,15 @@
|
|||||||
package org.sadtech.vkbot.core.repository.impl;
|
package org.sadtech.vkbot.core.repository.impl;
|
||||||
|
|
||||||
import org.sadtech.vkbot.core.entity.Mail;
|
import org.sadtech.vkbot.core.entity.Mail;
|
||||||
|
import org.sadtech.vkbot.core.repository.EventRepository;
|
||||||
import org.sadtech.vkbot.core.repository.MailRepository;
|
import org.sadtech.vkbot.core.repository.MailRepository;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Queue;
|
||||||
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
|
|
||||||
public class MailRepositoryList implements MailRepository {
|
public class MailRepositoryList implements EventRepository<Mail>, MailRepository {
|
||||||
|
|
||||||
private List<Mail> messages = new ArrayList<>();
|
private List<Mail> messages = new ArrayList<>();
|
||||||
|
|
||||||
@ -16,6 +19,15 @@ public class MailRepositoryList implements MailRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
public void cleanAll() {
|
||||||
|
messages.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Queue<Mail> getEventQueue() {
|
||||||
|
return new ConcurrentLinkedQueue<>(messages);
|
||||||
|
}
|
||||||
|
|
||||||
public List<Mail> getMailByTime(Integer timeFrom, Integer timeTo) {
|
public List<Mail> getMailByTime(Integer timeFrom, Integer timeTo) {
|
||||||
ArrayList<Mail> mails = new ArrayList<>();
|
ArrayList<Mail> mails = new ArrayList<>();
|
||||||
for (int i = messages.size() - 1; i >= 0; i--) {
|
for (int i = messages.size() - 1; i >= 0; i--) {
|
||||||
|
@ -1,12 +1,15 @@
|
|||||||
package org.sadtech.vkbot.core.repository.impl;
|
package org.sadtech.vkbot.core.repository.impl;
|
||||||
|
|
||||||
import org.sadtech.vkbot.core.entity.Person;
|
import org.sadtech.vkbot.core.entity.Person;
|
||||||
|
import org.sadtech.vkbot.core.repository.EventRepository;
|
||||||
import org.sadtech.vkbot.core.repository.PersonRepository;
|
import org.sadtech.vkbot.core.repository.PersonRepository;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Queue;
|
||||||
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
|
|
||||||
public class PersonRepositoryMap implements PersonRepository {
|
public class PersonRepositoryMap implements EventRepository<Person>, PersonRepository {
|
||||||
|
|
||||||
private Map<Integer, Person> personMap = new HashMap<>();
|
private Map<Integer, Person> personMap = new HashMap<>();
|
||||||
|
|
||||||
@ -15,6 +18,16 @@ public class PersonRepositoryMap implements PersonRepository {
|
|||||||
personMap.put(person.getId(), person);
|
personMap.put(person.getId(), person);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void cleanAll() {
|
||||||
|
personMap.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Queue<Person> getEventQueue() {
|
||||||
|
return new ConcurrentLinkedQueue<>(personMap.values());
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Person get(Integer id) {
|
public Person get(Integer id) {
|
||||||
return personMap.get(id);
|
return personMap.get(id);
|
||||||
|
@ -0,0 +1,27 @@
|
|||||||
|
package org.sadtech.vkbot.core.repository.impl;
|
||||||
|
|
||||||
|
import org.sadtech.vkbot.core.entity.Mail;
|
||||||
|
import org.sadtech.vkbot.core.repository.EventRepository;
|
||||||
|
|
||||||
|
import java.util.Queue;
|
||||||
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
|
|
||||||
|
public class TerminalComandRepositoryQueue implements EventRepository<Mail> {
|
||||||
|
|
||||||
|
private Queue<Mail> mailQueue = new ConcurrentLinkedQueue<>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void add(Mail dataObject) {
|
||||||
|
mailQueue.offer(dataObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void cleanAll() {
|
||||||
|
mailQueue.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Queue<Mail> getEventQueue() {
|
||||||
|
return mailQueue;
|
||||||
|
}
|
||||||
|
}
|
@ -7,6 +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.entity.MailSend;
|
||||||
|
|
||||||
public class MailSanderVk implements MailSandler {
|
public class MailSanderVk implements MailSandler {
|
||||||
|
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package org.sadtech.vkbot.core.sender;
|
package org.sadtech.vkbot.core.sender;
|
||||||
|
|
||||||
|
import org.sadtech.vkbot.core.entity.MailSend;
|
||||||
|
|
||||||
public interface MailSandler {
|
public interface MailSandler {
|
||||||
|
|
||||||
void send(MailSend mailSend);
|
void send(MailSend mailSend);
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package org.sadtech.vkbot.core.service;
|
package org.sadtech.vkbot.core.service;
|
||||||
|
|
||||||
import com.vk.api.sdk.objects.users.User;
|
import com.vk.api.sdk.objects.users.User;
|
||||||
|
import com.vk.api.sdk.objects.users.UserMin;
|
||||||
import org.sadtech.vkbot.core.entity.Person;
|
import org.sadtech.vkbot.core.entity.Person;
|
||||||
|
|
||||||
public interface PersonService {
|
public interface PersonService {
|
||||||
@ -9,8 +10,11 @@ public interface PersonService {
|
|||||||
|
|
||||||
void add(Person person);
|
void add(Person person);
|
||||||
|
|
||||||
|
void add(UserMin userMin);
|
||||||
|
|
||||||
Person get(Integer id);
|
Person get(Integer id);
|
||||||
|
|
||||||
boolean checkPerson(Integer idPerson);
|
boolean checkPerson(Integer idPerson);
|
||||||
|
|
||||||
|
Person createPerson(Integer userId);
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
package org.sadtech.vkbot.core.service.handlers;
|
package org.sadtech.vkbot.core.service.handlers;
|
||||||
|
|
||||||
|
import com.vk.api.sdk.objects.messages.Message;
|
||||||
import org.sadtech.vkbot.core.entity.Mail;
|
import org.sadtech.vkbot.core.entity.Mail;
|
||||||
|
|
||||||
public interface MailService extends SortEventService<Mail> {
|
public interface MailService extends SortEventService<Mail> {
|
||||||
|
|
||||||
void add(Mail mail);
|
void add(Mail mail);
|
||||||
|
|
||||||
|
void add(Message message);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
package org.sadtech.vkbot.core.service.handlers.impl;
|
package org.sadtech.vkbot.core.service.handlers.impl;
|
||||||
|
|
||||||
|
import com.vk.api.sdk.objects.messages.Message;
|
||||||
import org.apache.log4j.Logger;
|
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.handlers.MailService;
|
import org.sadtech.vkbot.core.service.handlers.MailService;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -17,18 +18,31 @@ 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 messageRepository;
|
||||||
|
private PersonService personService;
|
||||||
|
|
||||||
public MailServiceImpl() {
|
public MailServiceImpl(MailRepository messageRepository, PersonService personService) {
|
||||||
this.messageRepository = new MailRepositoryList();
|
|
||||||
}
|
|
||||||
|
|
||||||
public MailServiceImpl(MailRepository messageRepository) {
|
|
||||||
this.messageRepository = messageRepository;
|
this.messageRepository = messageRepository;
|
||||||
|
this.personService = personService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void add(Mail mail) {
|
public void add(Mail mail) {
|
||||||
messageRepository.add(mail);
|
messageRepository.add(mail);
|
||||||
|
log.info("Сообщение добавлено в репозиторий");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
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()));
|
||||||
|
}
|
||||||
|
mail.setPerson(personService.get(message.getUserId()));
|
||||||
|
log.info("Сообщение добавлено в репозиторий");
|
||||||
|
messageRepository.add(mail);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -65,4 +79,5 @@ public class MailServiceImpl implements MailService {
|
|||||||
log.info("Запрос на получение сообщений в интервале от " + timeFrom + " до " + timeTo);
|
log.info("Запрос на получение сообщений в интервале от " + timeFrom + " до " + timeTo);
|
||||||
return messageRepository.getMailByTime(timeFrom, timeTo);
|
return messageRepository.getMailByTime(timeFrom, timeTo);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,7 @@ public class EventServiceImpl implements EventService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Queue<JsonObject> getJsonObjects() {
|
public Queue<JsonObject> getJsonObjects() {
|
||||||
return eventRepository.getJsonObjects();
|
return eventRepository.getEventQueue();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
package org.sadtech.vkbot.core.service.impl;
|
package org.sadtech.vkbot.core.service.impl;
|
||||||
|
|
||||||
import com.vk.api.sdk.objects.users.User;
|
import com.vk.api.sdk.objects.users.User;
|
||||||
|
import com.vk.api.sdk.objects.users.UserMin;
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
|
import org.sadtech.vkbot.core.VkApi;
|
||||||
|
import org.sadtech.vkbot.core.VkConnect;
|
||||||
import org.sadtech.vkbot.core.entity.Person;
|
import org.sadtech.vkbot.core.entity.Person;
|
||||||
import org.sadtech.vkbot.core.repository.PersonRepository;
|
import org.sadtech.vkbot.core.repository.PersonRepository;
|
||||||
import org.sadtech.vkbot.core.repository.impl.PersonRepositoryMap;
|
import org.sadtech.vkbot.core.repository.impl.PersonRepositoryMap;
|
||||||
@ -12,13 +15,16 @@ public class PersonServiceImpl implements PersonService {
|
|||||||
public static final Logger log = Logger.getLogger(PersonServiceImpl.class);
|
public static final Logger log = Logger.getLogger(PersonServiceImpl.class);
|
||||||
|
|
||||||
private PersonRepository personRepository;
|
private PersonRepository personRepository;
|
||||||
|
private VkApi vkApi;
|
||||||
|
|
||||||
public PersonServiceImpl() {
|
public PersonServiceImpl(VkConnect vkConnect) {
|
||||||
this.personRepository = new PersonRepositoryMap();
|
this.personRepository = new PersonRepositoryMap();
|
||||||
|
vkApi = new VkApi(vkConnect);
|
||||||
}
|
}
|
||||||
|
|
||||||
public PersonServiceImpl(PersonRepository personRepository) {
|
public PersonServiceImpl(PersonRepository personRepository, VkConnect vkConnect) {
|
||||||
this.personRepository = personRepository;
|
this.personRepository = personRepository;
|
||||||
|
vkApi = new VkApi(vkConnect);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -37,6 +43,15 @@ public class PersonServiceImpl implements PersonService {
|
|||||||
log.info("Пользователь добавлен в репозиторий");
|
log.info("Пользователь добавлен в репозиторий");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void add(UserMin userMin) {
|
||||||
|
Person person = new Person();
|
||||||
|
person.setId(userMin.getId());
|
||||||
|
person.setFirstName(userMin.getFirstName());
|
||||||
|
person.setLastName(userMin.getLastName());
|
||||||
|
personRepository.add(person);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Person get(Integer id) {
|
public Person get(Integer id) {
|
||||||
return personRepository.get(id);
|
return personRepository.get(id);
|
||||||
@ -47,4 +62,14 @@ public class PersonServiceImpl implements PersonService {
|
|||||||
log.info("Проверка наличия пользователя в репозитории");
|
log.info("Проверка наличия пользователя в репозитории");
|
||||||
return get(idPerson) != null;
|
return get(idPerson) != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Person createPerson(Integer userId) {
|
||||||
|
Person person = new Person();
|
||||||
|
UserMin userMin = vkApi.getUserMini(userId);
|
||||||
|
person.setId(userMin.getId());
|
||||||
|
person.setLastName(userMin.getLastName());
|
||||||
|
person.setFirstName(userMin.getFirstName());
|
||||||
|
return person;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user