This commit is contained in:
@@ -6,6 +6,7 @@ import org.hibernate.Session;
|
||||
|
||||
import im.rosetta.database.HibernateUtil;
|
||||
import im.rosetta.database.Repository;
|
||||
import im.rosetta.database.entity.Device;
|
||||
import im.rosetta.database.entity.PushToken;
|
||||
|
||||
public class PushTokenRepository extends Repository<PushToken> {
|
||||
@@ -40,4 +41,19 @@ public class PushTokenRepository extends Repository<PushToken> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Найти токен для заданного устройства и токена
|
||||
* @param device устройство, которому принадлежит токен
|
||||
* @param token токен, который нужно найти
|
||||
* @return токен для заданного устройства и токена, или null, если такой токен не найден
|
||||
*/
|
||||
public PushToken findToken(Device device, String token){
|
||||
try (Session session = HibernateUtil.getSessionFactory().openSession()) {
|
||||
return session.createQuery("SELECT pt FROM PushToken pt WHERE pt.device_id = :device AND pt.token = :token", PushToken.class)
|
||||
.setParameter("device", device.getDeviceId())
|
||||
.setParameter("token", token)
|
||||
.uniqueResult();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import im.rosetta.client.tags.ECIAuthentificate;
|
||||
import im.rosetta.database.entity.Device;
|
||||
import im.rosetta.database.entity.PushToken;
|
||||
import im.rosetta.database.repository.DeviceRepository;
|
||||
import im.rosetta.database.repository.PushTokenRepository;
|
||||
import im.rosetta.packet.Packet16PushNotification;
|
||||
import im.rosetta.packet.runtime.NetworkNotificationAction;
|
||||
import im.rosetta.service.services.DeviceService;
|
||||
@@ -19,6 +20,7 @@ public class Executor16PushNotification extends PacketExecutor<Packet16PushNotif
|
||||
|
||||
private final DeviceRepository deviceRepository = new DeviceRepository();
|
||||
private final DeviceService deviceService = new DeviceService(deviceRepository);
|
||||
private final PushTokenRepository pushTokenRepository = new PushTokenRepository();
|
||||
|
||||
@Override
|
||||
public void onPacketReceived(Packet16PushNotification packet, Client client) throws Exception, ProtocolException {
|
||||
@@ -46,7 +48,7 @@ public class Executor16PushNotification extends PacketExecutor<Packet16PushNotif
|
||||
client.disconnect();
|
||||
return;
|
||||
}
|
||||
PushToken pushToken = this.findToken(device, notificationToken);
|
||||
PushToken pushToken = this.pushTokenRepository.findToken(device, notificationToken);
|
||||
|
||||
if(packet.getAction() == NetworkNotificationAction.SUBSCRIBE && pushToken == null){
|
||||
/**
|
||||
@@ -68,16 +70,6 @@ public class Executor16PushNotification extends PacketExecutor<Packet16PushNotif
|
||||
}
|
||||
}
|
||||
|
||||
private PushToken findToken(Device device, String token) {
|
||||
List<PushToken> tokens = device.getTokens();
|
||||
for(PushToken pushToken : tokens){
|
||||
if(pushToken.getToken().equals(token)){
|
||||
return pushToken;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Device findDevice(String publicKey, String deviceId) {
|
||||
List<Device> devices = this.deviceService.getDevicesByPK(publicKey);
|
||||
if(devices.size() == 0){
|
||||
|
||||
Reference in New Issue
Block a user