-
Notifications
You must be signed in to change notification settings - Fork 56
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
Refactor command-line handling #335
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,13 @@ type Summary = { succeeded: userId[], failed: userId[] }; | |
|
||
// !mjolnir since <date>/<duration> <action> <number> [...rooms] [...reason] | ||
export async function execSinceCommand(destinationRoomId: string, event: any, mjolnir: Mjolnir, lexer: Lexer) { | ||
let result = await execSinceCommandAux(destinationRoomId, event, mjolnir, lexer); | ||
let result; | ||
try { | ||
result = await execSinceCommandAux(destinationRoomId, event, mjolnir, lexer); | ||
} catch (ex) { | ||
result = { error: ex.message }; | ||
console.error("Error executing `since` command", ex); | ||
} | ||
if ("error" in result) { | ||
mjolnir.client.unstableApis.addReactionToEvent(destinationRoomId, event['event_id'], '❌'); | ||
mjolnir.logMessage(LogLevel.WARN, "SinceCommand", result.error); | ||
|
@@ -70,21 +76,16 @@ function formatResult(action: string, targetRoomId: string, recentJoins: Join[], | |
// - in case of success, returns `{ok: undefined}`, in case of error, returns `{error: string}`. | ||
async function execSinceCommandAux(destinationRoomId: string, event: any, mjolnir: Mjolnir, lexer: Lexer): Promise<Result<undefined>> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rather than passing the lexer to commands, wouldn't it be better if commands declared what arguments they are expecting (can be part of the same api to register with the handler)? This means that they don't need to know implementation detail of the lexer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would like to move towards what you suggest. I'm not sure I'll succeed in a single pass, though. |
||
// Attempt to parse `<date/duration>` as a date or duration. | ||
let dateOrDurationToken: Date | number; | ||
try { | ||
dateOrDurationToken = lexer.token("dateOrDuration").value; | ||
} catch (ex) { | ||
return { error: "Invalid <date/duration>" }; | ||
} | ||
console.debug("YORIC", "dateOrDurationToken", dateOrDurationToken); | ||
let dateOrDuration: Date |number = lexer.token("dateOrDuration").value; | ||
console.debug("YORIC", "dateOrDuration", dateOrDuration); | ||
let minDate; | ||
let maxAgeMS; | ||
if (dateOrDurationToken instanceof Date) { | ||
minDate = dateOrDurationToken; | ||
maxAgeMS = Date.now() - dateOrDurationToken.getTime() as number; | ||
if (dateOrDuration instanceof Date) { | ||
minDate = dateOrDuration; | ||
maxAgeMS = Date.now() - dateOrDuration.getTime() as number; | ||
} else { | ||
minDate = new Date(Date.now() - dateOrDurationToken); | ||
maxAgeMS = dateOrDurationToken; | ||
minDate = new Date(Date.now() - dateOrDuration); | ||
maxAgeMS = dateOrDuration; | ||
} | ||
|
||
// Attempt to parse `<action>` as Action. | ||
|
@@ -109,27 +110,29 @@ async function execSinceCommandAux(destinationRoomId: string, event: any, mjolni | |
// Now list affected rooms. | ||
const rooms: Set</* room id */string> = new Set(); | ||
do { | ||
try { | ||
let token = lexer.alternatives( | ||
() => lexer.token("STAR"), | ||
() => lexer.token("roomAliasOrID"), | ||
); | ||
if (token.type == "STAR") { | ||
for (let roomId of Object.keys(mjolnir.protectedRooms)) { | ||
rooms.add(roomId); | ||
} | ||
continue; | ||
} | ||
if (token.type == "roomAliasOrID") { | ||
const roomId = await mjolnir.client.resolveRoom(token.text); | ||
if (!(roomId in mjolnir.protectedRooms)) { | ||
return mjolnir.logMessage(LogLevel.WARN, "SinceCommand", `This room is not protected: ${htmlEscape(roomId)}.`); | ||
} | ||
let token = lexer.alternatives( | ||
() => lexer.token("STAR"), | ||
() => lexer.token("roomAliasOrID"), | ||
); | ||
console.debug("YORIC", "token", token); | ||
if (!token) { | ||
// We have reached the end of rooms. | ||
break; | ||
} | ||
if (token.type === "STAR") { | ||
for (let roomId of Object.keys(mjolnir.protectedRooms)) { | ||
rooms.add(roomId); | ||
continue; | ||
} | ||
} catch (ex) { | ||
// If we're done with rooms, we have entered <reason>. | ||
continue; | ||
} else if (token.type === "roomAliasOrID") { | ||
const roomId = await mjolnir.client.resolveRoom(token.text); | ||
if (!(roomId in mjolnir.protectedRooms)) { | ||
return mjolnir.logMessage(LogLevel.WARN, "SinceCommand", `This room is not protected: ${htmlEscape(roomId)}.`); | ||
} | ||
rooms.add(roomId); | ||
continue; | ||
} | ||
if (token.type == 'EOF') { | ||
break; | ||
} | ||
} while(true); | ||
|
@@ -143,8 +146,8 @@ async function execSinceCommandAux(destinationRoomId: string, event: any, mjolni | |
// Parse everything else as `<reason>`, stripping quotes if any have been added. | ||
const reason = lexer.alternatives( | ||
() => lexer.token("string"), | ||
() => lexer.token("EVERYTHING ELSE") | ||
).text; | ||
() => lexer.token("ETC") | ||
)?.text || ""; | ||
console.debug("YORIC", "reason", reason); | ||
|
||
const progressEventId = await mjolnir.client.unstableApis.addReactionToEvent(destinationRoomId, event['event_id'], '⏳'); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what happens when the lexer fails then? how different is it from what happens now?