-
Notifications
You must be signed in to change notification settings - Fork 2
Orderunterstützung für Veranstaltungsauswahl, Integration von myHAW Scraper #177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
52 commits
Select commit
Hold shift + click to select a range
f7853e1
feature(events): extended event list to support directories, save eve…
Malex14 3dfe1d5
fixed module load hanging
Malex14 a841a1b
handle non-existing directories better
Malex14 6259f1d
added log messages when directory gets (re)loaded
Malex14 7c76653
use generic function to extract typed keys
Malex14 5f913e4
improved readability of string interpolation
Malex14 bc4f2fa
added typedEntries function
Malex14 04539f1
removed Events type
Malex14 43bb683
use fewer empty lines
Malex14 369aaec
Make records in EventDirectory readonly
Malex14 897c716
unified types of js helpers
Malex14 c0fd0fb
use even fewer blank lines
Malex14 851bab9
improved comment explaining session fields
Malex14 9a5d210
Specify encoding
Malex14 7fbaefe
Removed empty line
Malex14 ce6d364
cleanedup comment
Malex14 920ae7b
comment EventDirectory fields
Malex14 b4be207
made empty dir logic clearer
Malex14 532f45f
cleanedup session fields
Malex14 75ebc27
changed resolvePath to getSubdirectory and simplified logic
Malex14 6d40243
made eventPath in session non-optional
Malex14 9200175
added git-support for eventfiles
Malex14 e059872
fixed directory updating
Malex14 10f0a23
refactored filter button text logic
Malex14 4701568
refactor(admin): simplify filter logic
EdJoPaTo f4a441b
refactor(git): simplify imports
EdJoPaTo 042540f
refactor: EventDirectory is already Readonly by itself
EdJoPaTo 7e4d6dd
refactor(all-events): stricter type for namesOfEvents
EdJoPaTo b5bbf17
refactor: use typedKeys/typedEntries to prevent type issues
EdJoPaTo 824a27e
Merge branch 'main' into malex-main
EdJoPaTo e1bbb1c
fix(events): correctly display non existing event id
EdJoPaTo 22c0a88
refactor: use same method name as before
EdJoPaTo 646aba0
store events in subdirectory
Malex14 1ae8294
fixup: store events in subdirectory
Malex14 f157e8f
make xo happy
Malex14 42256b9
chore: adapt init-debug-environment.sh
EdJoPaTo 7652dd3
fixup! chore: adapt init-debug-environment.sh
EdJoPaTo a22f6c9
refactor: always Partial EventDirectory
EdJoPaTo c9e375c
feat(event-add): improve filter and path handling
EdJoPaTo e6d4af0
fix(event-add): prevent clicking non existing subdirectories
EdJoPaTo 52ec85e
refactor(events): easier to read find function
EdJoPaTo b49dd5e
fix(events): sort event buttons by name
EdJoPaTo 57425b1
refactor: use in keyword over typedKeys helper
EdJoPaTo 479d14d
refactor: replace file watcher with interval
EdJoPaTo 6483e2d
refactor: import from ts extension
EdJoPaTo 4f5be80
fixup! refactor: import from ts extension
EdJoPaTo 8a7f954
refactor: remove async from not anymore async methods
EdJoPaTo ff51132
refactor: move more often changing argument to the right
EdJoPaTo 2cdbf1f
refactor(events): reuse exists for nonExisting
EdJoPaTo 153770e
refactor: inline single use constants
EdJoPaTo 6f98bb7
refactor: move folder handling to their respective modules
EdJoPaTo d9348ab
refactor: inline DIRECTORY constant
EdJoPaTo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,40 +1,113 @@ | ||
| import {readFile} from 'node:fs/promises'; | ||
| import {pull} from './git.ts'; | ||
| import {typedEntries} from './javascript-helper.ts'; | ||
| import type {EventDirectory, EventEntry, EventId} from './types.ts'; | ||
|
|
||
| async function getAll(): Promise<string[]> { | ||
| const data = await readFile('eventfiles/all.txt', 'utf8'); | ||
| const list = data.split('\n').filter(element => element !== ''); | ||
| return list; | ||
| } | ||
| let directory: EventDirectory = {}; | ||
| let namesOfEvents: Readonly<Record<EventId, string>> = {}; | ||
|
|
||
| setInterval(async () => update(), 1000 * 60 * 30); // Every 30 minutes | ||
| await update(); | ||
| console.log(new Date(), 'eventfiles loaded'); | ||
|
|
||
| export async function count(): Promise<number> { | ||
| const allEvents = await getAll(); | ||
| return allEvents.length; | ||
| async function update() { | ||
| await pull( | ||
| 'eventfiles', | ||
| 'https://github.com/HAWHHCalendarBot/eventfiles.git', | ||
| ); | ||
| const directoryString = await readFile('eventfiles/directory.json', 'utf8'); | ||
| directory = JSON.parse(directoryString) as EventDirectory; | ||
| namesOfEvents = await generateMapping(); | ||
| } | ||
|
|
||
| export async function exists(name: string): Promise<boolean> { | ||
| const allEvents = await getAll(); | ||
| return allEvents.includes(name); | ||
| async function generateMapping(): Promise<Readonly<Record<EventId, string>>> { | ||
| const namesOfEvents: Record<EventId, string> = {}; | ||
|
|
||
| function collect(directory: EventDirectory) { | ||
| for (const subDirectory of Object.values(directory.subDirectories ?? {})) { | ||
| collect(subDirectory); | ||
| } | ||
|
|
||
| Object.assign(namesOfEvents, directory.events ?? {}); | ||
| } | ||
|
|
||
| collect(directory); | ||
| return namesOfEvents; | ||
| } | ||
|
|
||
| export async function nonExisting(names: readonly string[]): Promise<string[]> { | ||
| const allEvents = new Set(await getAll()); | ||
| const result: string[] = []; | ||
| for (const event of names) { | ||
| if (!allEvents.has(event)) { | ||
| result.push(event); | ||
| function getSubdirectory(path: string[]): EventDirectory | undefined { | ||
| let resolvedDirectory = directory; | ||
|
|
||
| for (const part of path) { | ||
| const subDirectory = resolvedDirectory.subDirectories?.[part]; | ||
| if (subDirectory === undefined) { | ||
| return undefined; | ||
| } | ||
|
|
||
| resolvedDirectory = subDirectory; | ||
| } | ||
|
|
||
| return resolvedDirectory; | ||
| } | ||
|
|
||
| export function directoryHasContent(directory: EventDirectory): boolean { | ||
| const events = Object.keys(directory.events ?? {}).length; | ||
| const subDirectories = Object.keys(directory.subDirectories ?? {}).length; | ||
| return events > 0 || subDirectories > 0; | ||
| } | ||
|
|
||
| export function directoryExists(path: string[]): boolean { | ||
| if (path.length === 0) { | ||
| // Toplevel always exists | ||
| return true; | ||
| } | ||
|
|
||
| return result; | ||
| const directory = getSubdirectory(path); | ||
| return Boolean(directory && directoryHasContent(directory)); | ||
| } | ||
|
|
||
| export async function find( | ||
| pattern: string | RegExp, | ||
| ignore: readonly string[] = [], | ||
| ): Promise<readonly string[]> { | ||
| const allEvents = await getAll(); | ||
| export function getEventName(id: EventId): string { | ||
| return namesOfEvents[id] ?? id; | ||
| } | ||
|
|
||
| export function count(): number { | ||
| return Object.keys(namesOfEvents).length; | ||
| } | ||
|
|
||
| export function exists(id: EventId): boolean { | ||
| return id in namesOfEvents; | ||
| } | ||
|
|
||
| export function find( | ||
| path: string[], | ||
| pattern: string | RegExp | undefined, | ||
| ): EventDirectory { | ||
| if (!pattern) { | ||
| return getSubdirectory(path) ?? {}; | ||
| } | ||
|
|
||
| const regex = new RegExp(pattern, 'i'); | ||
| const filtered = allEvents.filter(event => | ||
| regex.test(event) && !ignore.includes(event)); | ||
| return filtered; | ||
| const accumulator: Record<EventId, string> = {}; | ||
|
|
||
| function collect(directory: EventDirectory) { | ||
| for (const [eventId, name] of typedEntries(directory.events ?? {})) { | ||
| if (regex.test(name)) { | ||
| accumulator[eventId] = name; | ||
| } | ||
| } | ||
|
|
||
| for (const subDirectory of Object.values(directory.subDirectories ?? {})) { | ||
| collect(subDirectory); | ||
| } | ||
| } | ||
|
|
||
| collect(getSubdirectory(path) ?? {}); | ||
| return { | ||
| events: Object.fromEntries(typedEntries(accumulator).sort((a, b) => a[1].localeCompare(b[1]))), | ||
| }; | ||
| } | ||
|
|
||
| export async function loadEvents(eventId: EventId): Promise<EventEntry[]> { | ||
| const content = await readFile(`eventfiles/events/${eventId}.json`, 'utf8'); | ||
| return JSON.parse(content) as EventEntry[]; | ||
| } |
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import {exec} from 'node:child_process'; | ||
| import {existsSync} from 'node:fs'; | ||
| import {promisify} from 'node:util'; | ||
|
|
||
| const run = promisify(exec); | ||
|
|
||
| export async function pull( | ||
| directory: string, | ||
| remoteUrl: string, | ||
| ): Promise<void> { | ||
| try { | ||
| await (existsSync(`${directory}/.git`) | ||
| ? run(`git -C ${directory} pull`) | ||
| : run(`git clone -q --depth 1 ${remoteUrl} ${directory}`)); | ||
| } catch {} | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| export function typedKeys<K extends keyof any>(record: Readonly<Partial<Record<K, unknown>>>): K[] { | ||
| return Object.keys(record) as K[]; | ||
| } | ||
|
|
||
| export function typedEntries<K extends keyof any, V>(record: Readonly<Partial<Record<K, V>>>): Array<[K, V]> { | ||
| if (!record) { | ||
| return []; | ||
| } | ||
|
|
||
| return (Object.entries(record) as unknown[]) as Array<[K, V]>; | ||
| } |
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.