1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
import fs from "fs";
import toml from "toml";
import { exitWithError } from "./process.js";
import { Response } from "express";
import { markdownToSanitizedHTML } from "../util/markdown.js";
import i18next from "i18next";
interface StaticPage {
title: string;
path: string;
filename: string;
}
export interface GathioConfig {
general: {
domain: string;
port: string;
email: string;
site_name: string;
delete_after_days: number;
is_federated: boolean;
email_logo_url: string;
show_kofi: boolean;
show_public_event_list: boolean;
mail_service: "nodemailer" | "sendgrid" | "none";
creator_email_addresses: string[];
};
database: {
mongodb_url: string;
};
nodemailer?: {
smtp_url?: string;
smtp_server: string;
smtp_port: string;
smtp_username: string;
smtp_password: string;
};
sendgrid?: {
api_key: string;
};
static_pages?: StaticPage[];
}
interface FrontendConfig {
domain: string;
siteName: string;
isFederated: boolean;
emailLogoUrl: string;
showKofi: boolean;
showPublicEventList: boolean;
showInstanceInformation: boolean;
staticPages?: StaticPage[];
version: string;
}
const defaultConfig: GathioConfig = {
general: {
domain: "localhost:3000",
email: "contact@example.com",
port: "3000",
site_name: "gathio",
is_federated: true,
delete_after_days: 7,
email_logo_url: "",
show_public_event_list: false,
show_kofi: false,
mail_service: "none",
creator_email_addresses: [],
},
database: {
mongodb_url: "mongodb://localhost:27017/gathio",
},
};
export const frontendConfig = (res: Response): FrontendConfig => {
const config = res.locals.config;
if (!config) {
return {
domain: defaultConfig.general.domain,
siteName: defaultConfig.general.site_name,
isFederated: defaultConfig.general.is_federated,
emailLogoUrl: defaultConfig.general.email_logo_url,
showPublicEventList: defaultConfig.general.show_public_event_list,
showKofi: defaultConfig.general.show_kofi,
showInstanceInformation: false,
staticPages: [],
version: process.env.npm_package_version || "unknown",
};
}
return {
domain: config.general.domain,
siteName: config.general.site_name,
isFederated: !!config.general.is_federated,
emailLogoUrl: config.general.email_logo_url,
showPublicEventList: !!config.general.show_public_event_list,
showKofi: !!config.general.show_kofi,
showInstanceInformation: !!config.static_pages?.length,
staticPages: config.static_pages,
version: process.env.npm_package_version || "unknown",
};
};
interface InstanceRule {
icon: string;
text: string;
}
export const instanceRules = (): InstanceRule[] => {
const config = getConfig();
const rules = [];
rules.push(
config.general.show_public_event_list
? {
text: i18next.t("config.instancerule.showpubliceventlist-true"),
icon: "fas fa-eye",
}
: {
text: i18next.t("config.instancerule.showpubliceventlist-false"),
icon: "fas fa-eye-slash",
},
);
rules.push(
config.general.creator_email_addresses?.length
? {
text: i18next.t("config.instancerule.creatoremail-true"),
icon: "fas fa-user-check",
}
: {
text: i18next.t("config.instancerule.creatoremail-false"),
icon: "fas fa-users",
},
);
rules.push(
config.general.delete_after_days > 0
? {
text: i18next.t("config.instancerule.deleteafterdays-true", { days: config.general.delete_after_days } ),
icon: "far fa-calendar-times",
}
: {
text: i18next.t("config.instancerule.deleteafterdays-false"),
icon: "far fa-calendar-check",
},
);
rules.push(
config.general.is_federated
? {
text: i18next.t("config.instancerule.isfederated-true"),
icon: "fas fa-globe",
}
: {
text: i18next.t("config.instancerule.isfederated-false"),
icon: "fas fa-globe",
},
);
return rules;
};
export const instanceDescription = (): string => {
const config = getConfig();
const defaultInstanceDescription =
i18next.t("config.defaultinstancedesc");
let instanceDescription = defaultInstanceDescription;
let instancedescfile = "./static/instance-description-" + i18next.language + ".md";
try {
if (fs.existsSync(instancedescfile)) {
const fileBody = fs.readFileSync(
instancedescfile,
"utf-8",
);
instanceDescription = markdownToSanitizedHTML(fileBody);
}
// Replace {{siteName}} with the instance name
instanceDescription = instanceDescription.replace(
/\{\{ ?siteName ?\}\}/g,
config?.general.site_name,
);
return instanceDescription;
} catch (err) {
console.log(err);
return defaultInstanceDescription;
}
};
let _resolvedConfig: GathioConfig | null = null;
// Attempt to load our global config. Will stop the app if the config file
// cannot be read (there's no point trying to continue!)
export const getConfig = (): GathioConfig => {
if (_resolvedConfig) {
return _resolvedConfig;
}
try {
const config = toml.parse(
fs.readFileSync("./config/config.toml", "utf-8"),
) as GathioConfig;
const resolvedConfig = {
...defaultConfig,
...config,
}
if (process.env.CYPRESS || process.env.CI) {
config.general.mail_service = "none";
console.log(
"Running in Cypress or CI, not initializing email service.",
);
} else if (config.general.mail_service === "none") {
console.warn(
"You have not configured this Gathio instance to send emails! This means that event creators will not receive emails when their events are created, which means they may end up locked out of editing events. Consider setting up an email service.",
);
}
_resolvedConfig = resolvedConfig;
return resolvedConfig;
} catch {
exitWithError(
"Configuration file not found! Have you renamed './config/config-example.toml' to './config/config.toml'?",
);
return process.exit(1);
}
};
export default getConfig;
|