Skip to content

Commit 5dc858f

Browse files
authored
Matrix bot: add labels to generated notes. (#210)
* Add label to the notes generated by matrix-bot. * Allow using major version in labels string.
1 parent 083a0f0 commit 5dc858f

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

tools/matrix-bot/convert-to-notes.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ type Message = {
1818
msg: string;
1919
};
2020

21-
export async function convertToNotes(meta: Metadata, file: string, outputFile?: string) {
21+
export function saveNotes(notes: INotesEnvelopeV3, outputFilename: string) {
22+
fs.writeFileSync(path.resolve(outputFilename), JSON.stringify(notes));
23+
console.info(`💾 Saved ${notes.notes.length} notes to ${outputFilename}.`);
24+
}
25+
26+
export async function convertToNotes(meta: Metadata, file: string, labels?: string[]) {
2227
const data = JSON.parse(readFileSync(path.resolve(file), "utf-8")) as Message[];
2328

2429
const notes = new Map<string, INoteV3>();
@@ -46,7 +51,7 @@ export async function convertToNotes(meta: Metadata, file: string, outputFile?:
4651
date,
4752
author: msg.sender,
4853
...linkData,
49-
labels: [],
54+
labels: labels ?? [],
5055
});
5156
}
5257
}
@@ -56,12 +61,8 @@ export async function convertToNotes(meta: Metadata, file: string, outputFile?:
5661
version: 3,
5762
notes: notesArray,
5863
};
59-
if (outputFile) {
60-
fs.writeFileSync(path.resolve(outputFile), JSON.stringify(envelope));
61-
console.info(`💾 Saved ${notesArray.length} notes to ${outputFile}.`);
62-
} else {
63-
console.info(JSON.stringify(envelope, null, 2));
64-
}
64+
65+
return envelope;
6566
}
6667

6768
function findAndParseLink(content: string, meta: Metadata) {
@@ -81,7 +82,8 @@ function findAndParseLink(content: string, meta: Metadata) {
8182

8283
async function main(inputFilename: string, outputFilename: string) {
8384
const meta = await fetchMetadata();
84-
await convertToNotes(meta, inputFilename, outputFilename);
85+
const notes = await convertToNotes(meta, inputFilename);
86+
saveNotes(notes, outputFilename);
8587
}
8688

8789
if (require.main === module) {

tools/matrix-bot/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ dotenv.config();
66
const homeserverUrl = "https://matrix.org";
77
const accessToken = process.env.ACCESS_TOKEN;
88
const userId = process.env.USER_ID;
9+
const notesLabel = process.env.NOTES_LABEL;
910
const roomId = "!ddsEwXlCWnreEGuqXZ:polkadot.io";
1011

1112
if (!accessToken || !userId) {
1213
throw new Error("Provide .env file or ENV variables `ACCESS_TOKEN` and `USER_ID`");
1314
}
1415

15-
async function main(accessToken: string, userId: string) {
16-
const logger = new MessagesLogger(roomId);
16+
async function main(accessToken: string, userId: string, notesLabel?: string) {
17+
const logger = new MessagesLogger(roomId, notesLabel ? [notesLabel] : []);
1718
const client = await listenToMessages(homeserverUrl, accessToken, userId, roomId, logger);
1819

1920
const cleanup = () => {
@@ -23,4 +24,4 @@ async function main(accessToken: string, userId: string) {
2324
process.once("SIGINT", cleanup);
2425
}
2526

26-
main(accessToken, userId);
27+
main(accessToken, userId, notesLabel);

tools/matrix-bot/logger.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { readFileSync, writeFileSync } from "node:fs";
22
import path from "node:path";
33
import { fetchMetadata, findLinkToLatestVersion } from "@fluffylabs/links-metadata";
4-
import { convertToNotes } from "./convert-to-notes";
4+
import { convertToNotes, saveNotes } from "./convert-to-notes";
55

66
export class MessagesLogger {
7-
constructor(private readonly roomId: string) {}
7+
constructor(
8+
private readonly roomId: string,
9+
private readonly notesLabels: string[],
10+
) {}
811

912
private generatePermalink(eventId: string): string {
1013
return `https://matrix.to/#/${this.roomId}/${eventId}`;
@@ -26,6 +29,8 @@ export class MessagesLogger {
2629

2730
const majorVersion = `${versionName.split(".").slice(0, 2).join(".")}.x`;
2831
const outputFilename = `output/messages-${majorVersion}.json`;
32+
const notesOutputFilename = `output/notes-${majorVersion}.json`;
33+
const labels = this.notesLabels.map((x) => x.replace("$MAJOR_VERSION", majorVersion));
2934

3035
const link = this.generatePermalink(eventId);
3136
const newMessage = {
@@ -36,15 +41,18 @@ export class MessagesLogger {
3641
};
3742

3843
let messages = [];
44+
// read previous log file
3945
try {
4046
messages = JSON.parse(readFileSync(path.resolve(outputFilename), "utf-8"));
4147
} catch (e) {}
48+
49+
// append the new message and save.
4250
messages.push(newMessage);
4351
writeFileSync(outputFilename, JSON.stringify(messages));
4452

45-
const notesFilename = `output/notes-${majorVersion}.json`;
4653
try {
47-
convertToNotes(meta, outputFilename, notesFilename);
54+
const notes = await convertToNotes(meta, outputFilename, labels);
55+
saveNotes(notes, notesOutputFilename);
4856
} catch (e) {
4957
console.error(e);
5058
}

0 commit comments

Comments
 (0)