Небольшие доработки обработки сообщений
This commit is contained in:
parent
5177fee391
commit
e88563b94e
@ -14,6 +14,7 @@ public class Message {
|
||||
private String text;
|
||||
private Long date;
|
||||
private String sourceMessage;
|
||||
private String sex;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "idUser", nullable = false)
|
||||
|
@ -3,8 +3,7 @@ package org.sadtech.consultant.database.entity;
|
||||
import lombok.Data;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
@Entity
|
||||
@Data
|
||||
@ -30,4 +29,7 @@ public class Person {
|
||||
@MapKeyColumn(name="KeysSocialNetworks", length = 20)
|
||||
private Map<String, Integer> socialNetworks = new HashMap<>();
|
||||
|
||||
// @OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL, mappedBy="idMess")
|
||||
// private List<Message> messages;
|
||||
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import org.sadtech.consultant.database.repository.MessageRepository;
|
||||
import org.sadtech.consultant.database.service.MessageService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
@ -27,9 +27,7 @@ public class MessageHandler {
|
||||
while (true) {
|
||||
List<Message> messages = messageLogicService.getMessageRange(data);
|
||||
for (Message message : messages) {
|
||||
System.out.println(message);
|
||||
|
||||
|
||||
log.info(message);
|
||||
data = message.getDate();
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package org.sadtech.consultant.processing;
|
||||
|
||||
import org.sadtech.consultant.database.entity.Message;
|
||||
import org.sadtech.consultant.database.entity.Person;
|
||||
import org.sadtech.consultant.database.service.MessageService;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
|
@ -1,9 +1,11 @@
|
||||
package org.sadtech.consultant.processing;
|
||||
|
||||
import lombok.extern.log4j.Log4j;
|
||||
import org.sadtech.consultant.database.entity.Person;
|
||||
import org.sadtech.consultant.database.service.PersonService;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Log4j
|
||||
@Component
|
||||
public class PersonLogicService {
|
||||
|
||||
@ -13,9 +15,30 @@ public class PersonLogicService {
|
||||
this.personService = userService;
|
||||
}
|
||||
|
||||
public boolean addUser(Person user) {
|
||||
public void addUser(Person user) {
|
||||
personService.addUser(user);
|
||||
return true;
|
||||
log.info("Пользователь добавлен в бд");
|
||||
}
|
||||
|
||||
public boolean checkPersonBySocialNetworksId(String socType, Integer socId) {
|
||||
Person checkPerson = personService.getUserBySocialNetworksId(socType, socId);
|
||||
if (checkPerson != null) {
|
||||
log.info("Пользователь уже есть в бд");
|
||||
return true;
|
||||
}
|
||||
log.info("Пользователя нет в бд");
|
||||
return false;
|
||||
}
|
||||
|
||||
public Person getPersonBySocialNetworksId(Person person) {
|
||||
for (String key : person.getSocialNetworks().keySet()) {
|
||||
Person getPerson = personService.getUserBySocialNetworksId(key, person.getSocialNetworks().get(key));
|
||||
if (getPerson != null) {
|
||||
log.info("Пользователь уже есть в бд");
|
||||
return getPerson;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Person getUserById(Long id) {
|
||||
|
@ -16,8 +16,6 @@ public class Main {
|
||||
}
|
||||
|
||||
public void run() throws Exception {
|
||||
log.info("\n\n\n\n=== Запуск прогарммы ===\n\n");
|
||||
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfigVk.class);
|
||||
EventListenable eventListener = context.getBean(EventListenable.class);
|
||||
eventListener.listen();
|
||||
@ -25,7 +23,5 @@ public class Main {
|
||||
dispetcherHandler.sortAndSend();
|
||||
// MessageHandler messageHandler = context.getBean(MessageHandler.class);
|
||||
// messageHandler.processing();
|
||||
|
||||
log.info("\n\n=== Конец программы ===\n\n");
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,16 @@
|
||||
package org.sadtech.vkbot;
|
||||
|
||||
public enum SourceMessage {
|
||||
VK, FB
|
||||
VK("VK"),
|
||||
FB("FB");
|
||||
|
||||
private final String value;
|
||||
|
||||
private SourceMessage(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return this.value;
|
||||
}
|
||||
}
|
||||
|
@ -6,12 +6,16 @@ import com.vk.api.sdk.client.actors.GroupActor;
|
||||
import com.vk.api.sdk.exceptions.ApiException;
|
||||
import com.vk.api.sdk.exceptions.ClientException;
|
||||
import com.vk.api.sdk.objects.users.User;
|
||||
import com.vk.api.sdk.objects.users.UserFull;
|
||||
import com.vk.api.sdk.objects.users.UserMin;
|
||||
import com.vk.api.sdk.objects.users.UserXtrCounters;
|
||||
import com.vk.api.sdk.queries.users.UserField;
|
||||
import lombok.extern.log4j.Log4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Log4j
|
||||
@Component
|
||||
@ -33,15 +37,14 @@ public class VkApi {
|
||||
}
|
||||
}
|
||||
|
||||
public static String getUserName(Integer id) {
|
||||
List<UserXtrCounters> user = null;
|
||||
UserFull userFull = null;
|
||||
public static UserXtrCounters getUserVk(Integer id) {
|
||||
List<UserXtrCounters> temp = null;
|
||||
try {
|
||||
user = vk.users().get(actor).userIds(String.valueOf(id)).execute();
|
||||
temp = vk.users().get(actor).userIds(String.valueOf(id)).execute();
|
||||
} catch (ApiException | ClientException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return user.get(0).getLastName() + " " + user.get(0).getFirstName();
|
||||
return temp.get(0);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -2,26 +2,34 @@ package org.sadtech.vkbot.handlers.impl;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.vk.api.sdk.objects.users.UserXtrCounters;
|
||||
import com.vk.api.sdk.queries.users.UserField;
|
||||
import lombok.extern.log4j.Log4j;
|
||||
import org.sadtech.consultant.database.entity.Message;
|
||||
import org.sadtech.consultant.database.entity.Person;
|
||||
import org.sadtech.consultant.processing.MessageLogicService;
|
||||
import org.sadtech.consultant.processing.PersonLogicService;
|
||||
import org.sadtech.consultant.database.entity.Message;
|
||||
import org.sadtech.vkbot.SourceMessage;
|
||||
import org.sadtech.vkbot.VkApi;
|
||||
import org.sadtech.vkbot.listener.Observable;
|
||||
import org.sadtech.vkbot.listener.Observer;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
@Log4j
|
||||
@Component
|
||||
public class MessageHandlerVk implements Observer {
|
||||
|
||||
private MessageLogicService messageLogicService;
|
||||
private PersonLogicService userLogicService;
|
||||
private PersonLogicService personLogicService;
|
||||
|
||||
public MessageHandlerVk(Observable dispetcherHandler, PersonLogicService userLogicService, MessageLogicService messageLogicService) {
|
||||
this.userLogicService = userLogicService;
|
||||
public MessageHandlerVk(Observable dispetcherHandler, PersonLogicService personLogicService, MessageLogicService messageLogicService) {
|
||||
this.messageLogicService = messageLogicService;
|
||||
this.personLogicService = personLogicService;
|
||||
dispetcherHandler.registerObserver(this);
|
||||
}
|
||||
|
||||
@ -29,23 +37,29 @@ public class MessageHandlerVk implements Observer {
|
||||
public void update(JsonObject object) {
|
||||
if (object.get("type").toString().equals("\"message_new\"")) {
|
||||
Gson gson = new Gson();
|
||||
log.info(object.getAsJsonObject("object"));
|
||||
com.vk.api.sdk.objects.messages.Message message = gson.fromJson(object.getAsJsonObject("object"), com.vk.api.sdk.objects.messages.Message.class);
|
||||
sendProcessing(message);
|
||||
}
|
||||
}
|
||||
|
||||
private void sendProcessing(com.vk.api.sdk.objects.messages.Message userMessage) {
|
||||
log.info(userMessage.getBody());
|
||||
Message message = new Message();
|
||||
// Person user = userLogicService.getUserBySocialId(SourceMessage.VK.name(), Long.valueOf(userMessage.getUserId()));
|
||||
Person user;
|
||||
user = new Person();
|
||||
user.getSocialNetworks().put(SourceMessage.VK.name(), userMessage.getUserId());
|
||||
user.setName("Name");
|
||||
user.setLastName("LastName");
|
||||
user.setCity("City");
|
||||
userLogicService.addUser(user);
|
||||
log.info(user);
|
||||
Integer userVkId = userMessage.getUserId();
|
||||
log.info(VkApi.getUserVk(userVkId));
|
||||
if (personLogicService.checkPersonBySocialNetworksId(SourceMessage.VK.name(), userVkId)) {
|
||||
user = personLogicService.getUserBySocialId(SourceMessage.VK.name(), userMessage.getUserId());
|
||||
} else {
|
||||
user = new Person();
|
||||
UserXtrCounters userXtrCounters = VkApi.getUserVk(userVkId);
|
||||
user.setCity("ыыыы");
|
||||
user.setName(userXtrCounters.getFirstName());
|
||||
user.setLastName(userXtrCounters.getLastName());
|
||||
user.getSocialNetworks().put(String.valueOf(SourceMessage.VK), userMessage.getUserId());
|
||||
personLogicService.addUser(user);
|
||||
}
|
||||
|
||||
Message message = new Message();
|
||||
message.setUser(user);
|
||||
message.setText(userMessage.getBody());
|
||||
message.setDate(Long.valueOf(userMessage.getDate()));
|
||||
|
Loading…
Reference in New Issue
Block a user