aboutsummaryrefslogtreecommitdiff
path: root/src/routes/feeds/activity-notifications/+server.ts
blob: 145e236e1490376599b32bd9b3416a5bf9953deb (plain) (blame)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import {
	type Notification,
	notifications,
} from "$lib/Data/AniList/notifications";
import { refreshAniListToken } from "$lib/Utility/anilistOauth";
import { siteUrl } from "$lib/Utility/appOrigin";
import { decryptFeedToken } from "$lib/Utility/feedToken";

const htmlEncode = (input: string) => {
	return input.replace(/[\u00A0-\u9999<>&]/g, (i) => `&#${i.charCodeAt(0)};`);
};

const channel = (items: string) => `<?xml version="1.0" encoding="UTF-8" ?>
<rss
	version="2.0"
	xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:snf="http://www.smartnews.be/snf"
xmlns:media="http://search.yahoo.com/mrss/">
  <channel>
    <atom:link href="${siteUrl("/feeds/activity-notifications")}" rel="self" type="application/rss+xml" />
    <title>AniList Notifications • due.moe</title>
    <link>${siteUrl("/")}</link>
    <description>Instantly view your AniList notifications via RSS!</description>
    <pubDate>${new Date().toUTCString()}</pubDate>
    <lastBuildDate>${new Date().toUTCString()}</lastBuildDate>
    <language>en-US</language>
		<snf:logo><url>${siteUrl("/favicon-196x196.png")}</url></snf:logo>
    ${items}
  </channel>
</rss>
`;

const render = (posts: Notification[] = []) =>
	channel(
		posts
			.filter((notification: Notification) => notification.type !== undefined)
			.map((notification: Notification) => {
				let title = `${notification.user.name}${notification.context}`;
				let link = `https://anilist.co/user/${notification.user.name}`;
				const prettyType = notification.type
					.toString()
					.replace(/_/g, " ")
					.toLowerCase()
					.replace(/\w\S*/g, (text) => {
						return text.charAt(0).toUpperCase() + text.substr(1).toLowerCase();
					});

				try {
					if (
						!["FOLLOWING", "ACTIVITY_MESSAGE"].includes(
							notification.type.toString(),
						) &&
						!notification.type.toString().includes("THREAD")
					) {
						link = `https://anilist.co/activity/${notification.activity.id}`;
					} else if (notification.type.toString().includes("THREAD")) {
						title += `${notification.thread.title}`;
						link = `https://anilist.co/forum/thread/${notification.thread.id}`;
					}
				} catch {
					return "";
				}

				return `<item>
<guid isPermaLink="false">${notification.id}</guid>
<title>${htmlEncode(title)}</title>
<link>${link}</link>
<author>${notification.user.name}</author>
<media:thumbnail url="${notification.user.avatar.large}" />
<category>${prettyType}</category>
<pubDate>${new Date(notification.createdAt * 1000).toUTCString()}</pubDate>
</item>`;
			})
			.join(""),
	);

// Old feed URLs carried the tokens in clear (?token=&refresh=); they no longer
// resolve, so degrade to a single item nudging the user to re-copy the link
// instead of silently going empty.
const staleNotice = () =>
	channel(`<item>
<guid isPermaLink="false">due-moe-feed-url-outdated</guid>
<title>${htmlEncode("Your due.moe RSS feed URL is outdated — re-copy it from Settings")}</title>
<link>${siteUrl("/settings#feeds")}</link>
<description>${htmlEncode("This feed link no longer works. Open due.moe → Settings → RSS Feeds and copy the new URL into your reader.")}</description>
<pubDate>${new Date().toUTCString()}</pubDate>
</item>`);

const feed = (body: string) =>
	new Response(body, {
		headers: {
			"Cache-Control": "max-age=0",
			"Content-Type": "application/xml",
		},
	});

export const GET = async ({ url }) => {
	const sealed = url.searchParams.get("feed");
	const refreshToken = sealed ? await decryptFeedToken(sealed) : null;

	if (!refreshToken) return feed(staleNotice());

	const accessToken = await refreshAniListToken(refreshToken);

	if (!accessToken) return feed(render());

	const notification = await notifications(accessToken);

	return feed(render(notification || []));
};