Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow private notes on games in kid mode #16769

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 34 additions & 35 deletions app/controllers/Round.scala
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ final class Round(
private[controllers] def getWatcherChat(
game: GameModel
)(using ctx: Context): Fu[Option[lila.chat.UserChat.Mine]] = {
ctx.kid.no && (ctx.noBot || ctx.userId.exists(game.userIds.has)) && ctx.me.fold(
(ctx.noBot || ctx.userId.exists(game.userIds.has)) && ctx.me.fold(
HTTPRequest.isHuman(ctx.req)
)(env.chat.panic.allowed(_)) && {
game.finishedOrAborted || !ctx.userId.exists(game.userIds.has)
Expand All @@ -211,41 +211,40 @@ final class Round(
private[controllers] def getPlayerChat(game: GameModel, tour: Option[Tour])(using
ctx: Context
): Fu[Option[Chat.GameOrEvent]] =
ctx.kid.no.so:
def toEventChat(resource: String)(c: lila.chat.UserChat.Mine) =
Chat
.GameOrEvent:
Right:
(c.truncate(100), lila.chat.Chat.ResourceId(resource))
.some
(game.tournamentId, game.simulId, game.swissId) match
case (Some(tid), _, _) =>
val hasChat = ctx.isAuth && tour.forall(tournamentC.canHaveChat(_, none))
hasChat.so(
def toEventChat(resource: String)(c: lila.chat.UserChat.Mine) =
Chat
.GameOrEvent:
Right:
(c.truncate(100), lila.chat.Chat.ResourceId(resource))
.some
(game.tournamentId, game.simulId, game.swissId) match
case (Some(tid), _, _) =>
val hasChat = ctx.isAuth && tour.forall(tournamentC.canHaveChat(_, none))
hasChat.so(
env.chat.api.userChat.cached
.findMine(tid.into(ChatId))
.dmap(toEventChat(s"tournament/$tid"))
)
case (_, Some(sid), _) =>
env.chat.api.userChat.cached.findMine(sid.into(ChatId)).dmap(toEventChat(s"simul/$sid"))
case (_, _, Some(sid)) =>
env.swiss.api
.roundInfo(sid)
.flatMapz(swissC.canHaveChat)
.flatMapz:
env.chat.api.userChat.cached
.findMine(tid.into(ChatId))
.dmap(toEventChat(s"tournament/$tid"))
)
case (_, Some(sid), _) =>
env.chat.api.userChat.cached.findMine(sid.into(ChatId)).dmap(toEventChat(s"simul/$sid"))
case (_, _, Some(sid)) =>
env.swiss.api
.roundInfo(sid)
.flatMapz(swissC.canHaveChat)
.flatMapz:
env.chat.api.userChat.cached
.findMine(sid.into(ChatId))
.dmap(toEventChat(s"swiss/$sid"))
case _ =>
game.hasChat.so:
for
chat <- env.chat.api.playerChat.findIf(game.id.into(ChatId), !game.justCreated)
lines <- lila.chat.JsonView.asyncLines(chat)
yield Chat
.GameOrEvent:
Left:
Chat.Restricted(chat, lines, restricted = game.sourceIs(_.Lobby) && ctx.isAnon)
.some
.findMine(sid.into(ChatId))
.dmap(toEventChat(s"swiss/$sid"))
case _ =>
game.hasChat.so:
for
chat <- env.chat.api.playerChat.findIf(game.id.into(ChatId), !game.justCreated)
lines <- lila.chat.JsonView.asyncLines(chat)
yield Chat
.GameOrEvent:
Left:
Chat.Restricted(chat, lines, restricted = game.sourceIs(_.Lobby) && ctx.isAnon)
.some

def sides(gameId: GameId, color: Color) = Open:
FoundSnip(env.round.proxyRepo.pov(gameId, color)): pov =>
Expand Down
70 changes: 39 additions & 31 deletions modules/chat/src/main/ChatUi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -64,34 +64,42 @@ final class ChatUi(helpers: Helpers):
palantir: Boolean = false,
hostIds: List[UserId] = Nil
)(using ctx: Context): JsObject =
Json
.obj(
"data" -> Json
.obj(
"id" -> chat.id,
"name" -> name,
"lines" -> lines,
"resourceId" -> resourceId.value
)
.add("hostIds" -> hostIds.some.filter(_.nonEmpty))
.add("userId" -> ctx.userId)
.add("loginRequired" -> chat.loginRequired)
.add("restricted" -> restricted)
.add("palantir" -> (palantir && ctx.isAuth)),
"writeable" -> writeable,
"public" -> public,
"permissions" -> Json
.obj("local" -> (public && localMod))
.add("broadcast" -> (public && broadcastMod))
.add("timeout" -> (public && Granter.opt(_.ChatTimeout)))
.add("shadowban" -> (public && Granter.opt(_.Shadowban)))
)
.add("kobold" -> ctx.troll)
.add("blind" -> ctx.blind)
.add("timeout" -> timeout)
.add("noteId" -> (withNoteAge.isDefined && ctx.noBlind).option(chat.id.value.take(8)))
.add("noteAge" -> withNoteAge)
.add(
"timeoutReasons" -> (!localMod && (Granter.opt(_.ChatTimeout) || Granter.opt(_.BroadcastTimeout)))
.option(JsonView.timeoutReasons)
)
if ctx.kid.yes then
Json
.obj("data" -> false)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this makes the TS types lie

data: ChatData;

.add("kidMode" -> true)
.add("noteId" -> (withNoteAge.isDefined && ctx.noBlind).option(chat.id.value.take(8)))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code duplication with line 99

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

.add("noteAge" -> withNoteAge)
else
Json
.obj(
"data" -> Json
.obj(
"id" -> chat.id,
"name" -> name,
"lines" -> lines,
"resourceId" -> resourceId.value
)
.add("hostIds" -> hostIds.some.filter(_.nonEmpty))
.add("userId" -> ctx.userId)
.add("loginRequired" -> chat.loginRequired)
.add("restricted" -> restricted)
.add("palantir" -> (palantir && ctx.isAuth)),
"writeable" -> writeable,
"public" -> public,
"permissions" -> Json
.obj("local" -> (public && localMod))
.add("broadcast" -> (public && broadcastMod))
.add("timeout" -> (public && Granter.opt(_.ChatTimeout)))
.add("shadowban" -> (public && Granter.opt(_.Shadowban)))
)
.add("kidMode" -> ctx.kid.yes)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be omitted since we know that ctx.kid.no

Copy link
Contributor Author

@yafred yafred Jan 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done
I am not sure how it works though
absent means false (on the typescript side) ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lila usually omits false or missing values altogether.

.add("kobold" -> ctx.troll)
.add("blind" -> ctx.blind)
.add("timeout" -> timeout)
.add("noteId" -> (withNoteAge.isDefined && ctx.noBlind).option(chat.id.value.take(8)))
.add("noteAge" -> withNoteAge)
.add(
"timeoutReasons" -> (!localMod && (Granter.opt(_.ChatTimeout) || Granter.opt(_.BroadcastTimeout)))
.option(JsonView.timeoutReasons)
)
9 changes: 6 additions & 3 deletions ui/chat/src/ctrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default class ChatCtrl {
private maxLinesDrop = 50; // how many lines to drop at once
private subs: [PubsubEvent, PubsubCallback][];

allTabs: Tab[] = ['discussion'];
allTabs: Tab[] = [];
palantir: ChatPalantir;
tabStorage: LichessStorage = storage.make('chat.tab');
storedTab: string | null = this.tabStorage.get();
Expand All @@ -39,12 +39,13 @@ export default class ChatCtrl {
readonly redraw: Redraw,
) {
this.data = opts.data;
if (!opts.kidMode) this.allTabs.push('discussion');
if (opts.noteId) this.allTabs.push('note');
if (opts.plugin) this.allTabs.push(opts.plugin.tab.key);
if (!opts.kidMode && opts.plugin) this.allTabs.push(opts.plugin.tab.key);
this.palantir = {
instance: undefined,
loaded: false,
enabled: prop(!!this.data.palantir),
enabled: prop(!!this.data.palantir && !opts.kidMode),
};
const noChat = storage.get('nochat');
this.vm = {
Expand All @@ -71,6 +72,8 @@ export default class ChatCtrl {
redraw: this.redraw,
});

if (opts.kidMode) return;

/* If discussion is disabled, and we have another chat tab,
* then select that tab over discussion */
if (this.allTabs.length > 1 && this.vm.tab === 'discussion' && noChat) this.vm.tab = this.allTabs[1];
Expand Down
1 change: 1 addition & 0 deletions ui/chat/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface ChatOpts {
noteText?: string;
plugin?: ChatPlugin;
alwaysEnabled: boolean;
kidMode: boolean;
}

export interface ChatPlugin {
Expand Down
Loading