-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcron.js
More file actions
114 lines (97 loc) · 3.47 KB
/
Copy pathcron.js
File metadata and controls
114 lines (97 loc) · 3.47 KB
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
import timer from 'node-schedule';
import LdapClient from "ldapjs-client";
import { db } from './index.js';
import { sendNewWeeklyMessage } from './commands/weeklyMessage.js';
import { sendReminderToAllUsers } from './commands/users.js';
export default class Cron {
constructor(config) {
this.config = config;
this.announcementJob = null;
this.reminderJob = null;
this.ldapSyncCron = null;
if (config.ldap && config.ldap.updateCron) {
this.syncLdap.bind(this)();
this.ldapSyncCron = timer.scheduleJob(config.ldap.updateCron, this.syncLdap.bind(this));
}
this.updateAnnouncementJob();
this.updateReminderJob();
}
updateAnnouncementJob() {
let reminderTime = db.getWeeklyMessageTime();
let reminderWeekday = db.getWeeklyMessageWeekday();
if (this.announcementJob != null) {
if (reminderTime != undefined && reminderWeekday != undefined) {
this.announcementJob.reschedule({dayOfWeek: reminderWeekday, hour: reminderTime.hour, minute: reminderTime.minute});
} else {
this.announcementJob.cancel();
this.announcementJob = null;
}
} else {
if (reminderTime != undefined && reminderWeekday != undefined) {
this.announcementJob = timer.scheduleJob({dayOfWeek: reminderWeekday, hour: reminderTime.hour, minute: reminderTime.minute}, sendNewWeeklyMessage);
}
}
}
updateReminderJob() {
let weeklyTime = db.getReminderMessageTime();
let weeklyWeekday = db.getReminderMessageWeekday();
if (this.reminderJob != null) {
if (weeklyTime != undefined && weeklyWeekday != undefined) {
this.reminderJob.reschedule({dayOfWeek: weeklyWeekday, hour: weeklyTime.hour, minute: weeklyTime.minute});
} else {
this.reminderJob.cancel();
this.reminderJob = null;
}
} else {
if (weeklyTime != undefined && weeklyWeekday != undefined) {
this.reminderJob = timer.scheduleJob({dayOfWeek: weeklyWeekday, hour: weeklyTime.hour, minute: weeklyTime.minute}, sendReminderToAllUsers);
}
}
}
async syncLdap() {
console.log(`Connecting to LDAP server at ${this.config.ldap.url}...`);
this.client = new LdapClient({ url: this.config.ldap.url, timeout: this.config.ldap.timeout });
await this.client.bind(this.config.ldap.username, this.config.ldap.password);
let updatedUsers = await this.client.search(this.config.ldap.userDn, {
scope: "sub",
filter: "(&(!(nsAccountLock=*))(hasKey=TRUE))",
attributes: [
"cn",
"telegramID",
"telegramnickname",
],
});
let existingUsers = db.getUsers();
const validUpdatedUsers = updatedUsers.filter(user => user.telegramID != null && user.telegramID !== "");
const existingById = new Map(existingUsers.map(user => [user.id, user]));
const updatedById = new Map(validUpdatedUsers.map(user => [user.telegramID, user]));
for (const updated of validUpdatedUsers) {
const id = updated.telegramID;
const existing = existingById.get(id);
const normalizedUser = {
id,
username: updated.telegramnickname ?? updated.cn ?? "",
name: updated.cn ?? ""
};
if (!existing) {
await db.addUser(normalizedUser);
continue;
}
const changes = {};
if (existing.username !== normalizedUser.username) {
changes.username = normalizedUser.username;
}
if (existing.name !== normalizedUser.name) {
changes.name = normalizedUser.name;
}
if (Object.keys(changes).length > 0) {
await db.updateUser(id, changes);
}
}
for (const existing of existingUsers) {
if (!updatedById.has(existing.id)) {
await db.removeUser(existing.id);
}
}
}
}