aboutsummaryrefslogtreecommitdiff
path: root/src/routes/feeds/activity-notifications/+server.ts
blob: db2836132a70706e0c1d75dbb6b17cc0dcf48204 (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
import { notifications, type Notification } from '$lib/AniList/notifications';

export const GET = async ({ url }) => {
	const data = await notifications(url.searchParams.get('token') || 'null');
	const render = (posts: Notification[]) => `<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <atom:link href="https://due.moe/feeds/activity-notifications" rel="self" type="application/rss+xml" />
    <title>期限 | AniList Notifications</title>
    <link>https://due.moe</link>
    <description>Instantly view your AniList notifications via RSS!</description>
    <pubDate>${new Date().toUTCString()}</pubDate>
    <lastBuildDate>${new Date().toUTCString()}</lastBuildDate>
    <language>en-US</language>
    <image>
      <url>https://due.moe/favicon-192x192.png</url>
      <link>https://due.moe</link>
    	<title>期限 | AniList Notifications</title>
    </image>
    ${posts
			.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();
					});

				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}`;
				}

				return `<item>
<guid isPermaLink="false">${notification.id}</guid>
<title>${title}</title>
<link>${link}</link>
<author>${notification.user.name}</author>
<enclosure url="${notification.user.avatar.large}" type="image/png" />
<category>${prettyType}</category>
<pubDate>${new Date(
					notification.createdAt * 1000 + new Date().getTimezoneOffset()
				).toUTCString()}</pubDate>
</item>`;
			})
			.join('')}
  </channel>
</rss>
`;

	return new Response(render(data), {
		headers: {
			'Cache-Control': `max-age=0, s-max-age=${60}`,
			'Content-Type': 'application/xml'
		}
	});
};