-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
174 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
178 changes: 93 additions & 85 deletions
178
src/modules/handlers/screen/poster/gewis/gewis-poster-screen-controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,103 +1,111 @@ | ||
import {BasePosterResponse, BasePosterScreenController, BorrelModeParams} from "../base-poster-screen-controller"; | ||
import HandlerManager from "../../../../root/handler-manager"; | ||
import {Screen} from "../../../../root/entities"; | ||
import {GewisPosterScreenHandler} from "../../index"; | ||
import {Body, Get, Post, Put, Query, Request, Route, Security, Tags} from "tsoa"; | ||
import {SecurityNames} from "../../../../../helpers/security"; | ||
import {securityGroups} from "../../../../../helpers/security-groups"; | ||
import logger from "../../../../../logger"; | ||
import {Request as ExpressRequest} from "express"; | ||
import {TrainResponse} from "../ns-trains-service"; | ||
import GEWISPosterService, {GEWISPhotoAlbumParams} from "./gewis-poster-service"; | ||
import OlympicsService from "../olympics-service"; | ||
import {FeatureEnabled} from "../../../../server-settings"; | ||
import { | ||
BasePosterResponse, | ||
BasePosterScreenController, | ||
BorrelModeParams, | ||
} from '../base-poster-screen-controller'; | ||
import HandlerManager from '../../../../root/handler-manager'; | ||
import { Screen } from '../../../../root/entities'; | ||
import { GewisPosterScreenHandler } from '../../index'; | ||
import { Body, Get, Post, Put, Query, Request, Route, Security, Tags } from 'tsoa'; | ||
import { SecurityNames } from '../../../../../helpers/security'; | ||
import { securityGroups } from '../../../../../helpers/security-groups'; | ||
import logger from '../../../../../logger'; | ||
import { Request as ExpressRequest } from 'express'; | ||
import { TrainResponse } from '../ns-trains-service'; | ||
import GEWISPosterService, { GEWISPhotoAlbumParams } from './gewis-poster-service'; | ||
import OlympicsService from '../olympics-service'; | ||
import { FeatureEnabled } from '../../../../server-settings'; | ||
|
||
interface GewisPosterResponse extends BasePosterResponse { | ||
borrelMode: boolean; | ||
borrelMode: boolean; | ||
} | ||
|
||
@Route('handler/screen/gewis-poster') | ||
@Tags('Handlers') | ||
@FeatureEnabled('GewisPosterScreenHandler') | ||
export class GewisPosterScreenController extends BasePosterScreenController { | ||
protected screenHandler: GewisPosterScreenHandler; | ||
protected screenHandler: GewisPosterScreenHandler; | ||
|
||
constructor() { | ||
super(); | ||
this.screenHandler = HandlerManager.getInstance() | ||
.getHandlers(Screen) | ||
.filter((h) => h.constructor.name === GewisPosterScreenHandler.name)[0] as GewisPosterScreenHandler; | ||
} | ||
constructor() { | ||
super(); | ||
this.screenHandler = HandlerManager.getInstance() | ||
.getHandlers(Screen) | ||
.filter( | ||
(h) => h.constructor.name === GewisPosterScreenHandler.name, | ||
)[0] as GewisPosterScreenHandler; | ||
} | ||
|
||
@Security(SecurityNames.LOCAL, securityGroups.poster.base) | ||
@Get('') | ||
public async getGewisPosters(@Query() alwaysReturnBorrelPosters?: boolean): Promise<GewisPosterResponse> { | ||
if (!this.screenHandler.posterManager.posters) { | ||
try { | ||
await this.screenHandler.posterManager.fetchPosters(); | ||
} catch (e) { | ||
logger.error(e); | ||
} | ||
} | ||
const posters = this.screenHandler.posterManager.posters ?? []; | ||
if (alwaysReturnBorrelPosters || this.screenHandler.borrelMode) { | ||
return { | ||
posters, | ||
borrelMode: this.screenHandler.borrelMode, | ||
}; | ||
} | ||
return { | ||
posters: posters.filter((p) => !p.borrelMode), | ||
borrelMode: false, | ||
}; | ||
@Security(SecurityNames.LOCAL, securityGroups.poster.base) | ||
@Get('') | ||
public async getGewisPosters( | ||
@Query() alwaysReturnBorrelPosters?: boolean, | ||
): Promise<GewisPosterResponse> { | ||
if (!this.screenHandler.posterManager.posters) { | ||
try { | ||
await this.screenHandler.posterManager.fetchPosters(); | ||
} catch (e) { | ||
logger.error(e); | ||
} | ||
} | ||
|
||
@Security(SecurityNames.LOCAL, securityGroups.poster.privileged) | ||
@Post('force-update') | ||
public async forceUpdateGewisPosters(@Request() req: ExpressRequest): Promise<void> { | ||
super.forceUpdatePosters(req); | ||
const posters = this.screenHandler.posterManager.posters ?? []; | ||
if (alwaysReturnBorrelPosters || this.screenHandler.borrelMode) { | ||
return { | ||
posters, | ||
borrelMode: this.screenHandler.borrelMode, | ||
}; | ||
} | ||
return { | ||
posters: posters.filter((p) => !p.borrelMode), | ||
borrelMode: false, | ||
}; | ||
} | ||
|
||
@Security(SecurityNames.LOCAL, securityGroups.poster.base) | ||
@Get('borrel-mode') | ||
public async getPosterBorrelMode(): Promise<BorrelModeParams> { | ||
return { enabled: this.screenHandler.borrelMode }; | ||
} | ||
@Security(SecurityNames.LOCAL, securityGroups.poster.privileged) | ||
@Post('force-update') | ||
public async forceUpdateGewisPosters(@Request() req: ExpressRequest): Promise<void> { | ||
super.forceUpdatePosters(req); | ||
} | ||
|
||
@Security(SecurityNames.LOCAL, securityGroups.poster.base) | ||
@Put('borrel-mode') | ||
public async setPosterBorrelMode( | ||
@Request() req: ExpressRequest, | ||
@Body() body: BorrelModeParams, | ||
): Promise<void> { | ||
logger.audit( | ||
req.user, | ||
`Set poster screen borrel mode to "${body.enabled ? 'true' : 'false'}".`, | ||
); | ||
this.screenHandler.setBorrelModeEnabled(body.enabled); | ||
} | ||
@Security(SecurityNames.LOCAL, securityGroups.poster.base) | ||
@Get('borrel-mode') | ||
public async getPosterBorrelMode(): Promise<BorrelModeParams> { | ||
return { enabled: this.screenHandler.borrelMode }; | ||
} | ||
|
||
@Security(SecurityNames.LOCAL, securityGroups.poster.base) | ||
@Get('train-departures') | ||
public async getTrains(): Promise<TrainResponse[]> { | ||
return super.getTrains(); | ||
} | ||
@Security(SecurityNames.LOCAL, securityGroups.poster.base) | ||
@Put('borrel-mode') | ||
public async setPosterBorrelMode( | ||
@Request() req: ExpressRequest, | ||
@Body() body: BorrelModeParams, | ||
): Promise<void> { | ||
logger.audit( | ||
req.user, | ||
`Set poster screen borrel mode to "${body.enabled ? 'true' : 'false'}".`, | ||
); | ||
this.screenHandler.setBorrelModeEnabled(body.enabled); | ||
} | ||
|
||
@Security(SecurityNames.LOCAL, securityGroups.poster.base) | ||
@Post('photo') | ||
public async getPhoto(@Body() params: GEWISPhotoAlbumParams) { | ||
return new GEWISPosterService().getPhoto(params); | ||
} | ||
@Security(SecurityNames.LOCAL, securityGroups.poster.base) | ||
@Get('train-departures') | ||
public async getTrains(): Promise<TrainResponse[]> { | ||
return super.getTrains(); | ||
} | ||
|
||
@Security(SecurityNames.LOCAL, securityGroups.poster.base) | ||
@Get('olympics/medal-table') | ||
public async getOlympicsMedalTable() { | ||
return super.getOlympicsMedalTable(); | ||
} | ||
@Security(SecurityNames.LOCAL, securityGroups.poster.base) | ||
@Post('photo') | ||
public async getPhoto(@Body() params: GEWISPhotoAlbumParams) { | ||
return new GEWISPosterService().getPhoto(params); | ||
} | ||
|
||
@Security(SecurityNames.LOCAL, securityGroups.poster.base) | ||
@Get('olympics/country-medals') | ||
public async getDutchOlympicMedals() { | ||
return new OlympicsService().getDutchMedals(); | ||
} | ||
} | ||
@Security(SecurityNames.LOCAL, securityGroups.poster.base) | ||
@Get('olympics/medal-table') | ||
public async getOlympicsMedalTable() { | ||
return super.getOlympicsMedalTable(); | ||
} | ||
|
||
@Security(SecurityNames.LOCAL, securityGroups.poster.base) | ||
@Get('olympics/country-medals') | ||
public async getDutchOlympicMedals() { | ||
return new OlympicsService().getDutchMedals(); | ||
} | ||
} |
75 changes: 37 additions & 38 deletions
75
src/modules/handlers/screen/poster/gewis/gewis-poster-screen-handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,49 @@ | ||
import BasePosterScreenHandler from "../base-poster-screen-handler"; | ||
import { Namespace } from "socket.io"; | ||
import { FeatureEnabled } from "../../../../server-settings"; | ||
import BasePosterScreenHandler from '../base-poster-screen-handler'; | ||
import { Namespace } from 'socket.io'; | ||
import { FeatureEnabled } from '../../../../server-settings'; | ||
|
||
@FeatureEnabled('GewisPosterScreenHandler') | ||
export default class GewisPosterScreenHandler extends BasePosterScreenHandler { | ||
private borrelModeDay: number | undefined; | ||
private borrelModeDay: number | undefined; | ||
|
||
public borrelModeInterval: NodeJS.Timeout; | ||
public borrelModeInterval: NodeJS.Timeout; | ||
|
||
constructor(socket: Namespace) { | ||
super(socket); | ||
constructor(socket: Namespace) { | ||
super(socket); | ||
|
||
// Check whether we need to enable/disable borrel mode | ||
this.borrelModeInterval = setInterval(this.checkBorrelMode.bind(this), 60 * 1000); | ||
} | ||
// Check whether we need to enable/disable borrel mode | ||
this.borrelModeInterval = setInterval(this.checkBorrelMode.bind(this), 60 * 1000); | ||
} | ||
|
||
public get borrelMode() { | ||
return this.borrelModeDay !== undefined; | ||
} | ||
|
||
public get borrelMode() { | ||
return this.borrelModeDay !== undefined; | ||
} | ||
private checkBorrelMode() { | ||
const now = new Date(); | ||
|
||
private checkBorrelMode() { | ||
const now = new Date(); | ||
|
||
// If borrelmode is enabled, but we arrive at the next day, disable borrelmode again | ||
if (this.borrelMode && this.borrelModeDay !== now.getDay()) { | ||
this.borrelModeDay = undefined; | ||
return; | ||
} | ||
// if borrelmode is enabled, we do not have to check whether we can enable it | ||
if (this.borrelMode) return; | ||
|
||
// By default, enable borrelmode on Thursdays 16:30 local time | ||
if ( | ||
now.getDay() === 4 && | ||
((now.getHours() === 16 && now.getMinutes() >= 30) || now.getHours() >= 17) | ||
) { | ||
this.borrelModeDay = now.getDay(); | ||
} | ||
// If borrelmode is enabled, but we arrive at the next day, disable borrelmode again | ||
if (this.borrelMode && this.borrelModeDay !== now.getDay()) { | ||
this.borrelModeDay = undefined; | ||
return; | ||
} | ||
// if borrelmode is enabled, we do not have to check whether we can enable it | ||
if (this.borrelMode) return; | ||
|
||
// By default, enable borrelmode on Thursdays 16:30 local time | ||
if ( | ||
now.getDay() === 4 && | ||
((now.getHours() === 16 && now.getMinutes() >= 30) || now.getHours() >= 17) | ||
) { | ||
this.borrelModeDay = now.getDay(); | ||
} | ||
} | ||
|
||
public setBorrelModeEnabled(enabled: boolean) { | ||
if (enabled) { | ||
this.borrelModeDay = new Date().getDay(); | ||
} else { | ||
this.borrelModeDay = undefined; | ||
} | ||
public setBorrelModeEnabled(enabled: boolean) { | ||
if (enabled) { | ||
this.borrelModeDay = new Date().getDay(); | ||
} else { | ||
this.borrelModeDay = undefined; | ||
} | ||
} | ||
} | ||
} |
56 changes: 29 additions & 27 deletions
56
src/modules/handlers/screen/poster/hubble/hubble-poster-screen-controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,36 @@ | ||
import {BasePosterResponse, BasePosterScreenController} from "../base-poster-screen-controller"; | ||
import HandlerManager from "../../../../root/handler-manager"; | ||
import {Screen} from "../../../../root/entities"; | ||
import BasePosterScreenHandler from "../base-poster-screen-handler"; | ||
import {HubblePosterScreenHandler} from "../../index"; | ||
import {Get, Post, Request, Route, Security, Tags} from "tsoa"; | ||
import {SecurityNames} from "../../../../../helpers/security"; | ||
import {securityGroups} from "../../../../../helpers/security-groups"; | ||
import {Request as ExpressRequest} from "express"; | ||
import {FeatureEnabled} from "../../../../server-settings"; | ||
import { BasePosterResponse, BasePosterScreenController } from '../base-poster-screen-controller'; | ||
import HandlerManager from '../../../../root/handler-manager'; | ||
import { Screen } from '../../../../root/entities'; | ||
import BasePosterScreenHandler from '../base-poster-screen-handler'; | ||
import { HubblePosterScreenHandler } from '../../index'; | ||
import { Get, Post, Request, Route, Security, Tags } from 'tsoa'; | ||
import { SecurityNames } from '../../../../../helpers/security'; | ||
import { securityGroups } from '../../../../../helpers/security-groups'; | ||
import { Request as ExpressRequest } from 'express'; | ||
import { FeatureEnabled } from '../../../../server-settings'; | ||
|
||
@Route('handler/screen/hubble-poster') | ||
@Tags('Handlers') | ||
@FeatureEnabled('HubblePosterScreenHandler') | ||
export class HubblePosterScreenController extends BasePosterScreenController { | ||
constructor() { | ||
super(); | ||
this.screenHandler = HandlerManager.getInstance() | ||
.getHandlers(Screen) | ||
.filter((h) => h.constructor.name === HubblePosterScreenHandler.name)[0] as BasePosterScreenHandler; | ||
} | ||
constructor() { | ||
super(); | ||
this.screenHandler = HandlerManager.getInstance() | ||
.getHandlers(Screen) | ||
.filter( | ||
(h) => h.constructor.name === HubblePosterScreenHandler.name, | ||
)[0] as BasePosterScreenHandler; | ||
} | ||
|
||
@Security(SecurityNames.LOCAL, securityGroups.poster.base) | ||
@Get('') | ||
public async getHubblePosters(): Promise<BasePosterResponse> { | ||
return super.getPosters(); | ||
} | ||
@Security(SecurityNames.LOCAL, securityGroups.poster.base) | ||
@Get('') | ||
public async getHubblePosters(): Promise<BasePosterResponse> { | ||
return super.getPosters(); | ||
} | ||
|
||
@Security(SecurityNames.LOCAL, securityGroups.poster.privileged) | ||
@Post('force-update') | ||
public async forceUpdateHubblePosters(@Request() req: ExpressRequest): Promise<void> { | ||
super.forceUpdatePosters(req); | ||
} | ||
} | ||
@Security(SecurityNames.LOCAL, securityGroups.poster.privileged) | ||
@Post('force-update') | ||
public async forceUpdateHubblePosters(@Request() req: ExpressRequest): Promise<void> { | ||
super.forceUpdatePosters(req); | ||
} | ||
} |
14 changes: 7 additions & 7 deletions
14
src/modules/handlers/screen/poster/hubble/hubble-poster-screen-handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import BasePosterScreenHandler from "../base-poster-screen-handler"; | ||
import { Namespace } from "socket.io"; | ||
import {FeatureEnabled} from "../../../../server-settings"; | ||
import BasePosterScreenHandler from '../base-poster-screen-handler'; | ||
import { Namespace } from 'socket.io'; | ||
import { FeatureEnabled } from '../../../../server-settings'; | ||
|
||
@FeatureEnabled('HubblePosterScreenHandler') | ||
export default class HubblePosterScreenHandler extends BasePosterScreenHandler { | ||
constructor(socket: Namespace) { | ||
super(socket); | ||
} | ||
} | ||
constructor(socket: Namespace) { | ||
super(socket); | ||
} | ||
} |
Oops, something went wrong.