63 lines
2.5 KiB
Java
63 lines
2.5 KiB
Java
package im.rosetta.service.services;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
|
|
import im.rosetta.client.tags.ECIAuthentificate;
|
|
import im.rosetta.database.QuerySession;
|
|
import im.rosetta.database.entity.User;
|
|
import im.rosetta.database.repository.UserRepository;
|
|
import im.rosetta.service.Service;
|
|
|
|
import io.orprotocol.client.Client;
|
|
|
|
public class UserService extends Service<UserRepository> {
|
|
|
|
public UserService(UserRepository repository) {
|
|
super(repository);
|
|
}
|
|
|
|
/**
|
|
* Поиск пользователей по части имени пользователя и публичному ключу.
|
|
* @param query часть имени пользователя
|
|
* @param take сколько пользователей отдать
|
|
* @return список пользователей, соответствующих запросу
|
|
*/
|
|
public List<User> searchUsers(String query, int take) {
|
|
String hql = "FROM User WHERE username LIKE :query OR publicKey = :queryExact ORDER BY verified ASC";
|
|
HashMap<String, Object> parameters = new HashMap<>();
|
|
parameters.put("query", "%" + query + "%");
|
|
parameters.put("queryExact", query);
|
|
try(QuerySession<User> querySession = this.getRepository().buildQuery(hql, parameters)){
|
|
return querySession.getQuery().setMaxResults(take).list();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Получает User из клиента, так же на всякий случай проверяется авторизован ли пользователь,
|
|
* если нет то User не будет найден
|
|
* @param client сетевой клиент
|
|
* @return пользователь
|
|
*/
|
|
public User fromClient(Client client) {
|
|
ECIAuthentificate eciAuthentificate = client.getTag(ECIAuthentificate.class);
|
|
if(eciAuthentificate == null){
|
|
return null;
|
|
}
|
|
if(!eciAuthentificate.hasAuthorized()){
|
|
return null;
|
|
}
|
|
return this.getRepository().findByField("publicKey", eciAuthentificate.getPublicKey());
|
|
}
|
|
|
|
/**
|
|
* Проверяет занятость имени пользователя
|
|
* @param username имя пользователя
|
|
* @return true если имя занято, иначе false
|
|
*/
|
|
public boolean isUsernameTaken(String username) {
|
|
User user = this.getRepository().findByField("username", username);
|
|
return user != null;
|
|
}
|
|
}
|