make spectator system messages volatile, prepare player message expiration

pull/9874/head
Thibault Duplessis 2021-09-23 12:31:48 +02:00
parent f467bac4ab
commit 443e27caef
5 changed files with 31 additions and 21 deletions

View File

@ -110,8 +110,9 @@ object Chat {
def chanOf(id: Chat.Id) = s"chat:$id"
object BSONFields {
val id = "_id"
val lines = "l"
val id = "_id"
val lines = "l"
val expire = "e"
}
import BSONFields._

View File

@ -1,6 +1,7 @@
package lila.chat
import chess.Color
import org.joda.time.DateTime
import reactivemongo.api.ReadPreference
import scala.concurrent.duration._
@ -94,7 +95,7 @@ final class ChatApi(
logger.info(s"Link check rejected $line in $publicSource")
funit
case true =>
(persist ?? persistLine(chatId, line)) >>- {
(persist ?? persistLine(chatId, line, expire = publicSource.isEmpty)) >>- {
if (persist) {
if (publicSource.isDefined) cached invalidate chatId
shutup ! {
@ -121,9 +122,9 @@ final class ChatApi(
def clear(chatId: Chat.Id) = coll.delete.one($id(chatId)).void
def system(chatId: Chat.Id, text: String, busChan: BusChan.Select): Funit = {
def system(chatId: Chat.Id, text: String, busChan: BusChan.Select, expire: Boolean): Funit = {
val line = UserLine(systemUserId, None, false, text, troll = false, deleted = false)
persistLine(chatId, line) >>- {
persistLine(chatId, line, expire) >>- {
cached.invalidate(chatId)
publish(chatId, actorApi.ChatLine(chatId, line), busChan)
}
@ -135,8 +136,15 @@ final class ChatApi(
publish(chatId, actorApi.ChatLine(chatId, line), busChan)
}
def service(chatId: Chat.Id, text: String, busChan: BusChan.Select, isVolatile: Boolean): Unit =
(if (isVolatile) volatile _ else system _)(chatId, text, busChan).unit
def service(
chatId: Chat.Id,
text: String,
busChan: BusChan.Select,
isVolatile: Boolean,
expire: Boolean
): Unit = {
if (isVolatile) volatile(chatId, text, busChan) else system(chatId, text, busChan, expire)
}.unit
def timeout(
chatId: Chat.Id,
@ -281,7 +289,7 @@ final class ChatApi(
def write(chatId: Chat.Id, color: Color, text: String, busChan: BusChan.Select): Funit =
makeLine(chatId, color, text) ?? { line =>
persistLine(chatId, line) >>- {
persistLine(chatId, line, expire = true) >>- {
publish(chatId, actorApi.ChatLine(chatId, line), busChan)
lila.mon.chat.message("anonPlayer", troll = false).increment().unit
}
@ -303,7 +311,7 @@ final class ChatApi(
def removeAll(chatIds: List[Chat.Id]) = coll.delete.one($inIds(chatIds)).void
private def persistLine(chatId: Chat.Id, line: Line): Funit =
private def persistLine(chatId: Chat.Id, line: Line, expire: Boolean): Funit =
coll.update
.one(
$id(chatId),
@ -316,8 +324,13 @@ final class ChatApi(
)
),
upsert = true
) map { res =>
(expire && res.upserted.nonEmpty) ?? coll.updateFieldUnchecked(
$id(chatId),
Chat.BSONFields.expire,
DateTime.now plusMonths 6
)
.void
}
private object Writer {

View File

@ -13,17 +13,12 @@ final class Messenger(api: ChatApi) {
def volatile(game: Game, message: String): Unit =
system(persistent = false)(game, message)
def system(persistent: Boolean)(game: Game, message: String): Unit = {
val apiCall =
if (persistent) api.userChat.system _
else api.userChat.volatile _
apiCall(watcherId(Chat.Id(game.id)), message, _.Round)
if (game.nonAi) apiCall(Chat.Id(game.id), message, _.Round)
def system(persistent: Boolean)(game: Game, message: String): Unit = if (game.nonAi) {
api.userChat.volatile(watcherId(Chat.Id(game.id)), message, _.Round)
if (persistent) api.userChat.system(Chat.Id(game.id), message, _.Round, expire = true)
else api.userChat.volatile(Chat.Id(game.id), message, _.Round)
}.unit
def systemForOwners(chatId: Chat.Id, message: String): Unit =
api.userChat.system(chatId, message, _.Round).unit
def watcher(gameId: Game.Id, userId: User.ID, text: String) =
api.userChat.write(watcherId(gameId), userId, text, PublicSource.Watcher(gameId.value).some, _.Round)

View File

@ -168,7 +168,8 @@ final class StudyApi(
chatApi.userChat.system(
Chat.Id(study.id.value),
s"Cloned from lichess.org/study/${prev.id}",
_.Study
_.Study,
expire = false
) inject study.some
}
}

View File

@ -602,7 +602,7 @@ final class SwissApi(
}
private def systemChat(id: Swiss.Id, text: String, volatile: Boolean = false): Unit =
chatApi.userChat.service(Chat.Id(id.value), text, _.Swiss, volatile)
chatApi.userChat.service(Chat.Id(id.value), text, _.Swiss, isVolatile = volatile, expire = false)
def withdrawAll(user: User, teamIds: List[TeamID]): Funit =
colls.swiss