forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserSearch.scala
33 lines (26 loc) · 1.23 KB
/
UserSearch.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package lila.mod
import lila.common.{ EmailAddress, IpAddress }
import lila.user.{ User, UserRepo }
final class UserSearch(
securityApi: lila.security.SecurityApi,
emailValidator: lila.security.EmailAddressValidator
) {
def apply(query: String): Fu[List[User]] =
if (query.isEmpty) fuccess(Nil)
else EmailAddress.from(query).map(searchEmail) orElse
IpAddress.from(query).map(searchIp) getOrElse
(searchUsername(query) zip searchFingerHash(query) map Function.tupled(_ ++ _)) // list concatenation, in case a fingerhash is also someone's username
private def searchIp(ip: IpAddress) =
securityApi recentUserIdsByIp ip map (_.reverse) flatMap UserRepo.usersFromSecondary
private def searchFingerHash(fh: String): Fu[List[User]] =
(fh.size == 8) ?? {
securityApi recentUserIdsByFingerHash lila.security.FingerHash(fh) map (_.reverse) flatMap UserRepo.usersFromSecondary
}
private def searchUsername(username: String) = UserRepo named username map (_.toList)
private def searchEmail(email: EmailAddress): Fu[List[User]] =
emailValidator.validate(email) ?? { fixed =>
UserRepo.byEmail(fixed) flatMap { current =>
UserRepo.byPrevEmail(fixed) map current.toList.:::
}
}
}