feat(timezone): Adds timezone as option
This commit is contained in:
parent
0b9089ffae
commit
b852c06f83
11 changed files with 278 additions and 22 deletions
|
|
@ -8,6 +8,7 @@ export enum TransformerType {
|
|||
PermissionBoolean,
|
||||
String,
|
||||
Paragraph,
|
||||
Timezone
|
||||
}
|
||||
|
||||
type ConfigurationTransformerItem = {
|
||||
|
|
@ -35,6 +36,7 @@ export class ConfigurationTransformer {
|
|||
return <ChannelId>configValue.value;
|
||||
case TransformerType.PermissionBoolean:
|
||||
return configValue.value === '1';
|
||||
case TransformerType.Timezone:
|
||||
case TransformerType.Paragraph:
|
||||
case TransformerType.String:
|
||||
return configValue.value;
|
||||
|
|
|
|||
|
|
@ -11,7 +11,8 @@ import {ConfigurationTransformer, TransformerType} from "../ConfigurationTransfo
|
|||
export type RuntimeGroupConfiguration = {
|
||||
channels: Nullable<ChannelRuntimeGroupConfiguration>,
|
||||
permissions: PermissionRuntimeGroupConfiguration,
|
||||
calendar: CalendarRuntimeGroupConfiguration
|
||||
calendar: CalendarRuntimeGroupConfiguration,
|
||||
timezone: string|null
|
||||
};
|
||||
|
||||
export type ChannelRuntimeGroupConfiguration = {
|
||||
|
|
@ -49,7 +50,8 @@ export class GroupConfigurationProvider implements ConfigurationProvider<
|
|||
title: null,
|
||||
description: null,
|
||||
location: null
|
||||
}
|
||||
},
|
||||
timezone: null
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -97,6 +99,10 @@ export class GroupConfigurationProvider implements ConfigurationProvider<
|
|||
{
|
||||
path: ['calendar', 'location'],
|
||||
type: TransformerType.String
|
||||
},
|
||||
{
|
||||
path: ['timezone'],
|
||||
type: TransformerType.Timezone
|
||||
}
|
||||
]
|
||||
)
|
||||
|
|
|
|||
|
|
@ -8,7 +8,8 @@ import { Nullable } from "../../types/Nullable";
|
|||
import {ConfigurationTransformer, TransformerType} from "../ConfigurationTransformer";
|
||||
|
||||
export type RuntimeServerConfiguration = {
|
||||
permissions: PermissionRuntimeServerConfiguration
|
||||
permissions: PermissionRuntimeServerConfiguration,
|
||||
timezone: string
|
||||
}
|
||||
|
||||
export type PermissionRuntimeServerConfiguration = {
|
||||
|
|
@ -35,7 +36,8 @@ export class ServerConfigurationProvider implements ConfigurationProvider<
|
|||
groupCreation: {
|
||||
allowEveryone: false
|
||||
}
|
||||
}
|
||||
},
|
||||
timezone: '',
|
||||
}
|
||||
}
|
||||
get(path: string): Nullable<ServerConfigurationModel> {
|
||||
|
|
@ -62,6 +64,10 @@ export class ServerConfigurationProvider implements ConfigurationProvider<
|
|||
{
|
||||
path: ['permissions', 'groupCreation', 'allowEveryone'],
|
||||
type: TransformerType.PermissionBoolean
|
||||
},
|
||||
{
|
||||
path: ['timezone'],
|
||||
type: TransformerType.Timezone
|
||||
}
|
||||
]
|
||||
)
|
||||
|
|
|
|||
93
source/Configuration/TimezoneHandler.ts
Normal file
93
source/Configuration/TimezoneHandler.ts
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
import {GroupModel} from "../Database/Models/GroupModel";
|
||||
import {ConfigurationHandler, PathConfigurationFrom} from "./ConfigurationHandler";
|
||||
import {GroupConfigurationProvider} from "./Groups/GroupConfigurationProvider";
|
||||
import {Container} from "../Container/Container";
|
||||
import {GroupConfigurationRepository} from "../Database/Repositories/GroupConfigurationRepository";
|
||||
import {ServerConfigurationProvider} from "./Server/ServerConfigurationProvider";
|
||||
import {Snowflake, time} from "discord.js";
|
||||
import {ServerConfigurationRepository} from "../Database/Repositories/ServerConfigurationRepository";
|
||||
import tzdata from 'tzdata';
|
||||
import {Nullable} from "../types/Nullable";
|
||||
|
||||
export type Timezone = {
|
||||
zone: string,
|
||||
gmt: string,
|
||||
name: string,
|
||||
}
|
||||
|
||||
export enum TimezoneSaveTarget {
|
||||
Server,
|
||||
Group
|
||||
}
|
||||
|
||||
export class TimezoneHandler {
|
||||
public static ALL_TIMEZONES: string[] = Object.keys(tzdata.zones)
|
||||
|
||||
constructor(
|
||||
private readonly serverid: Snowflake,
|
||||
private readonly group: GroupModel|null = null
|
||||
) {
|
||||
}
|
||||
|
||||
public use<TReturn>(callback: () => TReturn): TReturn {
|
||||
const previousTZ = process.env.TZ;
|
||||
|
||||
process.env.TZ = this.getCurrentTimezone();
|
||||
const result = callback();
|
||||
process.env.TZ = previousTZ;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public getCurrentTimezone(): string {
|
||||
const configs = [
|
||||
this.getGroupConfiguration(),
|
||||
this.getServerConfiguration()
|
||||
];
|
||||
|
||||
for (const config of configs) {
|
||||
if (!config) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const timezone = config.getConfigurationByPath('timezone');
|
||||
if (timezone.from === PathConfigurationFrom.Default) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return <string>timezone.value;
|
||||
}
|
||||
|
||||
return process.env.TZ ?? "Europe/London";
|
||||
}
|
||||
|
||||
public save(timezone: string, target: TimezoneSaveTarget) {
|
||||
const config = target === TimezoneSaveTarget.Server ? this.getServerConfiguration() : this.getGroupConfiguration();
|
||||
if (!config) {
|
||||
return;
|
||||
}
|
||||
|
||||
config.save('timezone', timezone);
|
||||
}
|
||||
|
||||
private getGroupConfiguration(): Nullable<ConfigurationHandler> {
|
||||
if (!this.group) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new ConfigurationHandler(
|
||||
new GroupConfigurationProvider(
|
||||
Container.get<GroupConfigurationRepository>(GroupConfigurationRepository.name),
|
||||
this.group
|
||||
)
|
||||
);
|
||||
}
|
||||
private getServerConfiguration(): ConfigurationHandler {
|
||||
return new ConfigurationHandler(
|
||||
new ServerConfigurationProvider(
|
||||
Container.get<ServerConfigurationRepository>(ServerConfigurationRepository.name),
|
||||
this.serverid
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue