-
Notifications
You must be signed in to change notification settings - Fork 0
/
track_formatter.ts
79 lines (72 loc) · 1.91 KB
/
track_formatter.ts
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
import { MrkdwnElement, SlackSchema, TrackSchema } from "./types.ts";
function simplePluralize(count: number, noun: string, suffix = "s"): string {
return `${count} ${noun}${count !== 1 ? suffix : ""}`;
}
function lastPlayed(lastPlayed: string): MrkdwnElement {
return {
type: "mrkdwn",
text: `*Last Played:* ${lastPlayed}`,
};
}
function formatSlack(track: TrackSchema): SlackSchema {
const metrics: MrkdwnElement[] = [
{
type: "mrkdwn",
text: `*Rating:* ${track?.metrics.votes}`,
},
{
type: "mrkdwn",
text: `*Voted by:* ${simplePluralize(track?.metrics.votes || 0, "user")}`,
},
{
type: "mrkdwn",
text: `*Played:* ${simplePluralize(track?.metrics.plays || 0, "time")}`,
},
];
if (track?.last_played) metrics.push(lastPlayed(track.last_played));
return {
blocks: [
{ type: "section", text: { type: "mrkdwn", text: "Now Playing:" } },
{ type: "divider" },
{
type: "section",
block_id: "section1",
text: {
type: "mrkdwn",
text: `*<${track?.spotify}|${track?.title}>* \n ${track
?.artist} \n ${track?.album} (${track?.year})`,
},
accessory: {
type: "image",
image_url: track?.image,
alt_text: track?.artist,
},
},
{ type: "divider" },
{
type: "section",
block_id: "section2",
fields: metrics,
},
{
type: "context",
elements: [
{
type: "mrkdwn",
text: `Added by ${track?.added_by} ${track?.added_at}`,
},
],
},
],
};
}
const FormatTrack = (track: TrackSchema) => {
return {
to: (format: string) => {
if (format == "JSON") return track;
if (format == "SLACK") return formatSlack(track);
throw new Error(`Unknown format: ${format}!`);
},
};
};
export default FormatTrack;