forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIpRender.scala
40 lines (29 loc) · 1.08 KB
/
IpRender.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
34
35
36
37
38
39
40
package lila.mod
import com.github.blemale.scaffeine.LoadingCache
import scala.concurrent.duration.*
import scala.jdk.CollectionConverters.*
import lila.common.CuteNameGenerator
import lila.common.IpAddress
import lila.memo.CacheApi
import lila.security.Granter
import lila.user.Holder
import lila.common.ThreadLocalRandom
object IpRender:
type Raw = String
type Rendered = String
type RenderIp = IpAddress => Rendered
final class IpRender:
import IpRender.*
def apply(mod: Holder): RenderIp = if (Granter.is(_.Admin)(mod)) visible else encrypted
val visible = (ip: IpAddress) => ip.value
val encrypted = (ip: IpAddress) => cache get ip
def decrypt(str: String): Option[IpAddress] = IpAddress.from(str) orElse
cache.underlying.asMap.asScala.collectFirst {
case (ip, encrypted) if encrypted == str =>
ip
}
private val cache: LoadingCache[IpAddress, Rendered] = CacheApi.scaffeineNoScheduler
.expireAfterAccess(30 minutes)
.build((_: IpAddress) =>
s"NoIP:${~CuteNameGenerator.make(maxSize = 30)}-${ThreadLocalRandom.nextString(3)}"
)