-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtextOutput.ts
More file actions
64 lines (60 loc) · 1.78 KB
/
textOutput.ts
File metadata and controls
64 lines (60 loc) · 1.78 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
import { Message, MessageProcessor } from "./slack.ts";
import { Timestamp } from "./timestamp.ts";
export type MessageRecord = {
thread: string;
ts: string;
user: string;
text: string;
};
export function msgToRecord(msg: Message, p: MessageProcessor): MessageRecord {
const { ts, user, text, ...rest } = msg;
const threadMark = msg.reply_count ? "+" : msg.parent_user_id ? ">" : "";
return {
thread: threadMark,
ts: ts!,
user: p.username(user) || rest.username || "",
text: p.readable(text) || rest.attachments?.[0]?.fallback || "",
};
}
export function recordsToJson(
channels: { name: string; records: MessageRecord[] }[],
): string {
const output = channels.map(({ name, records }) => ({
channel: name,
messages: records.map((r) => ({
thread: r.thread,
datetime: Timestamp.fromSlack(r.ts)!.toISOString(),
user: r.user,
text: r.text,
})),
}));
return JSON.stringify(output, null, 2);
}
export function recordsToMarkdown(
channels: { name: string; records: MessageRecord[] }[],
tz: string,
): string {
const lines: string[] = [];
for (const { name, records } of channels) {
if (records.length === 0) continue;
lines.push(`# #${name}`);
lines.push("");
let lastDate = "";
for (const r of records) {
const ts = Timestamp.fromSlack(r.ts)!;
const date = ts.date(tz);
if (date !== lastDate) {
if (lastDate !== "") lines.push("");
lines.push(`## ${date}`);
lines.push("");
lastDate = date;
}
const time = ts.hourMin(tz);
const indent = r.thread === ">" ? " " : "";
const prefix = r.thread ? `${r.thread} ` : "";
lines.push(`${indent}${time} ${prefix}${r.user}: ${r.text}`);
}
lines.push("");
}
return lines.join("\n");
}