diff --git a/app/controllers/Team.scala b/app/controllers/Team.scala index 375dd93bf3..9ed057f8ca 100644 --- a/app/controllers/Team.scala +++ b/app/controllers/Team.scala @@ -283,11 +283,11 @@ final class Team( def subscribe(teamId: String) = { def doSub(req: Request[_], me: UserModel) = - Form(single("v" -> boolean)) + Form(single("subscribe" -> optional(boolean))) .bindFromRequest()(req) - .fold(_ => funit, v => api.subscribe(teamId, me.id, v)) + .fold(_ => funit, v => api.subscribe(teamId, me.id, ~v)) AuthOrScopedBody(_.Team.Write)( - auth = ctx => me => doSub(ctx.body, me) inject Redirect(routes.Team.show(teamId)), + auth = ctx => me => doSub(ctx.body, me) inject jsonOkResult, scoped = req => me => doSub(req, me) inject jsonOkResult ) } diff --git a/app/views/team/show.scala b/app/views/team/show.scala index d15fac186c..ea5c1acd39 100644 --- a/app/views/team/show.scala +++ b/app/views/team/show.scala @@ -38,8 +38,8 @@ object show { v <- socketVersion chat <- chatOption } yield frag( - jsModule("chat"), - embedJsUnsafeLoadThen(s"""const cfg=${safeJsonValue( + jsModule("team"), + embedJsUnsafeLoadThen(s"""teamStart(${safeJsonValue( Json.obj( "id" -> t.id, "socketVersion" -> v.value, @@ -52,15 +52,7 @@ object show { localMod = ctx.userId exists t.leaders.contains ) ) - )}; -lichess.socket = new lichess.StrongSocket('/team/${t.id}',${v.value}); -cfg.chat && lichess.makeChat(cfg.chat); -$$('#team-subscribe').on('change', function() { - const v = this.checked; - $$(this).parents('form').each(function() { - $$.post(this.action, { v }); - }); -})""") + )})""") ) ) { val enabledOrLeader = t.enabled || info.ledByMe || isGranted(_.Admin) diff --git a/ui/@types/lichess/index.d.ts b/ui/@types/lichess/index.d.ts index f45125934c..63231930a5 100644 --- a/ui/@types/lichess/index.d.ts +++ b/ui/@types/lichess/index.d.ts @@ -51,7 +51,7 @@ interface Lichess { // socket.js StrongSocket: { - new(url: string, version: number | false, cfg: any): any; + new(url: string, version: number | false, cfg?: any): any; firstConnect: Promise<(tpe: string, data: any) => void> } diff --git a/ui/build b/ui/build index 39a693d7d5..4983a2389b 100755 --- a/ui/build +++ b/ui/build @@ -16,7 +16,7 @@ mkdir -p public/compiled apps1="common" apps2="chess ceval game tree chat nvui" apps3="site swiss msg chat cli challenge notify learn insight editor puzzle round analyse lobby tournament tournamentSchedule tournamentCalendar simul dasher speech palantir serviceWorker" -site_plugins="tvEmbed puzzleEmbed analyseEmbed user modUser clas coordinate captcha expandText" +site_plugins="tvEmbed puzzleEmbed analyseEmbed user modUser clas coordinate captcha expandText team" round_plugins="keyboardMove nvui" analyse_plugins="nvui" diff --git a/ui/site/rollup.config.js b/ui/site/rollup.config.js index 231cb53f84..26b3ff3563 100644 --- a/ui/site/rollup.config.js +++ b/ui/site/rollup.config.js @@ -113,4 +113,9 @@ export default rollupProject({ input: 'src/expand-text.ts', output: 'expand-text', }, + team: { + input: 'src/team.ts', + output: 'team', + name: 'teamStart' + } }); diff --git a/ui/site/src/team.ts b/ui/site/src/team.ts new file mode 100644 index 0000000000..a6074ed42f --- /dev/null +++ b/ui/site/src/team.ts @@ -0,0 +1,25 @@ +import * as xhr from 'common/xhr'; +import LichessChat from 'chat'; + +window.LichessChat = LichessChat; + +interface TeamOpts { + id: string; + socketVersion: number; + chat?: any; +} + +export default function(opts: TeamOpts) { + + const li = window.lichess; + + li.socket = new li.StrongSocket('/team/' + opts.id, opts.socketVersion); + + if (opts.chat) window.lichess.makeChat(opts.chat); + + $('#team-subscribe').on('change', function(this: HTMLInputElement) { + $(this).parents('form').each(function(this: HTMLFormElement) { + xhr.formToXhr(this); + }); + }); +}