Абстрактный класс для автоответчика

Чтобы можно было реализовать автоответчики для комментариев и прочего
This commit is contained in:
Mark Struchkov 2019-02-07 02:26:39 +03:00
parent 85a9849c0a
commit 809d17715e
8 changed files with 46 additions and 34 deletions

View File

@ -4,7 +4,7 @@ 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 org.sadtech.vkbot.core.service.RawEventService;
import java.util.HashMap;
import java.util.Map;
@ -13,18 +13,18 @@ public class EventDistributorVK implements EventDistributable, Runnable {
public static final Logger log = Logger.getLogger(EventDistributorVK.class);
private EventService eventService;
private RawEventService rawEventService;
private Map<String, EventSubscribe> eventDistributionMap = new HashMap<>();
public EventDistributorVK(EventService eventService) {
this.eventService = eventService;
public EventDistributorVK(RawEventService rawEventService) {
this.rawEventService = rawEventService;
}
@Override
public void distribute() {
while (true) {
if (eventService.getJsonObjects().peek() != null) {
JsonObject event = eventService.getJsonObjects().poll();
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"));

View File

@ -12,8 +12,8 @@ import com.vk.api.sdk.objects.groups.responses.GetLongPollServerResponse;
import org.apache.log4j.Logger;
import org.sadtech.vkbot.core.VkConnect;
import org.sadtech.vkbot.core.repository.impl.EventRepositoryQueue;
import org.sadtech.vkbot.core.service.EventService;
import org.sadtech.vkbot.core.service.impl.EventServiceImpl;
import org.sadtech.vkbot.core.service.RawEventService;
import org.sadtech.vkbot.core.service.impl.RawEventServiceImpl;
public class EventListenerVk implements EventListener, Runnable {
@ -22,7 +22,7 @@ public class EventListenerVk implements EventListener, Runnable {
private VkApiClient vk;
private GroupActor actor;
private EventService eventService;
private RawEventService rawEventService;
private GetLongPollEventsQuery longPollEventsQuery;
private LongPoll longPoll;
@ -31,19 +31,19 @@ public class EventListenerVk implements EventListener, Runnable {
public EventListenerVk(VkConnect vkConnect) {
vk = vkConnect.getVkApiClient();
actor = vkConnect.getGroupActor();
eventService = new EventServiceImpl(new EventRepositoryQueue());
rawEventService = new RawEventServiceImpl(new EventRepositoryQueue());
longPoll = new LongPoll(vk);
}
public EventListenerVk(VkConnect vkConnect, EventService eventService) {
public EventListenerVk(VkConnect vkConnect, RawEventService rawEventService) {
this.vk = vkConnect.getVkApiClient();
this.actor = vkConnect.getGroupActor();
this.eventService = eventService;
this.rawEventService = rawEventService;
longPoll = new LongPoll(vk);
}
public EventService getEventService() {
return eventService;
public RawEventService getRawEventService() {
return rawEventService;
}
public void listen() throws ClientException, ApiException {
@ -56,7 +56,7 @@ public class EventListenerVk implements EventListener, Runnable {
log.info("Полученно событие от ВК");
log.info(eventsResponse.getUpdates());
for (JsonObject update : eventsResponse.getUpdates()) {
eventService.add(update);
rawEventService.add(update);
}
}
longPollEventsQuery = longPoll.getEvents(server.getServer(), server.getKey(), eventsResponse.getTs()).waitTime(20);

View File

@ -5,7 +5,7 @@ import org.sadtech.vkbot.core.repository.EventRepository;
import java.util.Queue;
public interface EventService {
public interface RawEventService {
void cleanAll();

View File

@ -4,7 +4,7 @@ import com.vk.api.sdk.objects.messages.Message;
import org.sadtech.vkbot.core.entity.Mail;
import org.sadtech.vkbot.core.service.distribution.impl.EventService;
public interface MailService extends EventService<Mail>, SortEventService<Mail> {
public interface MailService extends EventService<Mail> {
void add(Message message);

View File

@ -1,13 +0,0 @@
package org.sadtech.vkbot.core.service.distribution;
import java.util.List;
public interface SortEventService<T> {
List<T> getEvent(Integer timeFrom, Integer timeTo);
List<T> getFirstMailByTime(Integer timeFrom, Integer timeTo);
List<T> getLastMailByTime(Integer timeFrom, Integer timeTo);
}

View File

@ -4,6 +4,8 @@ import org.sadtech.vkbot.core.entity.Comment;
import org.sadtech.vkbot.core.repository.CommentRepository;
import org.sadtech.vkbot.core.service.distribution.CommentService;
import java.util.List;
public class CommentServiceImpl implements CommentService {
private CommentRepository commentRepository;
@ -16,4 +18,19 @@ public class CommentServiceImpl implements CommentService {
public void add(Comment comment) {
commentRepository.add(comment);
}
@Override
public List<Comment> getEvent(Integer timeFrom, Integer timeTo) {
return null;
}
@Override
public List<Comment> getFirstMailByTime(Integer timeFrom, Integer timeTo) {
return null;
}
@Override
public List<Comment> getLastMailByTime(Integer timeFrom, Integer timeTo) {
return null;
}
}

View File

@ -1,7 +1,15 @@
package org.sadtech.vkbot.core.service.distribution.impl;
import java.util.List;
public interface EventService<T> {
void add(T event);
List<T> getEvent(Integer timeFrom, Integer timeTo);
List<T> getFirstMailByTime(Integer timeFrom, Integer timeTo);
List<T> getLastMailByTime(Integer timeFrom, Integer timeTo);
}

View File

@ -3,17 +3,17 @@ package org.sadtech.vkbot.core.service.impl;
import com.google.gson.JsonObject;
import org.apache.log4j.Logger;
import org.sadtech.vkbot.core.repository.EventRepository;
import org.sadtech.vkbot.core.service.EventService;
import org.sadtech.vkbot.core.service.RawEventService;
import java.util.Queue;
public class EventServiceImpl implements EventService {
public class RawEventServiceImpl implements RawEventService {
public static final Logger log = Logger.getLogger(EventServiceImpl.class);
public static final Logger log = Logger.getLogger(RawEventServiceImpl.class);
private EventRepository eventRepository;
public EventServiceImpl(EventRepository eventRepository) {
public RawEventServiceImpl(EventRepository eventRepository) {
this.eventRepository = eventRepository;
}