Skip to content

Commit

Permalink
Merge pull request #16769 from yafred/issue-16715-notes-in-kid-mode-t…
Browse files Browse the repository at this point in the history
…ake2

Allow private notes on games in kid mode
  • Loading branch information
ornicar authored Jan 21, 2025
2 parents c6d7b20 + 35c1e91 commit f528bb9
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 69 deletions.
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
69 changes: 38 additions & 31 deletions modules/chat/src/main/ChatUi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -64,34 +64,41 @@ 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)
)
val noteId = (withNoteAge.isDefined && ctx.noBlind).option(chat.id.value.take(8))
if ctx.kid.yes then
Json
.obj("kidMode" -> true)
.add("noteId" -> noteId)
.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("kobold" -> ctx.troll)
.add("blind" -> ctx.blind)
.add("timeout" -> timeout)
.add("noteId" -> noteId)
.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(!opts.kidMode && !!this.data.palantir),
};
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

0 comments on commit f528bb9

Please sign in to comment.