-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.ts
152 lines (129 loc) · 3.3 KB
/
routes.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { RouteHandler } from 'fastify';
import { eq } from 'drizzle-orm';
import findFeed from 'feedrat';
import { promisify } from 'util';
import { extract } from '@extractus/feed-extractor';
const asyncFindFeed = promisify(findFeed);
import { db } from './src/db';
import {
feeds,
globalMetadata,
entries as entriesTable,
} from './src/db/schema';
interface DefaultChannelBody {
default_channel: string;
}
export const putDefaultChannel: RouteHandler<{ Body: DefaultChannelBody }> = (
request,
response
) => {
const defaultChannel = request.body?.default_channel;
console.log(`Saving ${defaultChannel} as the default channel...`);
if (!defaultChannel) {
response.status(400);
return { message: 'default_channel is required' };
}
const { newDefaultChannel } = db
.insert(globalMetadata)
.values({ id: 1, default_channel: defaultChannel })
.onConflictDoUpdate({
target: globalMetadata.id,
set: { default_channel: defaultChannel },
where: eq(globalMetadata.id, 1),
})
.returning({ newDefaultChannel: globalMetadata.default_channel })
.get();
console.log(`${newDefaultChannel} is now the new default channel.`);
return { message: 'success', default_channel: newDefaultChannel };
};
interface SubscribeBody {
url: string;
channel?: string;
}
export const postSubscribe: RouteHandler<{ Body: SubscribeBody }> = async (
request,
response
) => {
const url = request.body?.url;
const channel = request.body?.channel;
if (!url) {
response.status(400);
return { message: 'url is required' };
}
let feedUrl: string;
try {
const listOfUrls = await asyncFindFeed(url);
if (listOfUrls.length > 1) {
response.status(400);
return {
message: 'Multiple feeds found; Please choose one.',
feeds: listOfUrls,
};
}
feedUrl = listOfUrls[0];
} catch (err) {
response.status(400);
return { message: err };
}
const existingSub = db
.select()
.from(feeds)
.where(eq(feeds.feed_url, feedUrl))
.get();
if (existingSub) {
response.status(409);
return { message: 'A subscription for this feed already exists.' };
}
const feed_results = await extract(feedUrl);
const { title, description, link, entries } = feed_results;
if (!entries || entries.length === 0) {
db.insert(feeds)
.values({
feed_url: feedUrl,
page_url: url,
title,
description,
link,
channel,
})
.get();
response.status(204);
return {
message: "The provided feed exists, but it doesn't contain any items.",
};
}
const { id } = db
.insert(feeds)
.values({
feed_url: feedUrl,
page_url: url,
title,
description,
link,
})
.returning({ id: feeds.id })
.get();
await db.transaction(async (tx) => {
entries.forEach((entry) => {
tx.insert(entriesTable)
.values({
entry_id: entry.id,
feed: id,
})
.run();
});
});
return {
lastEntry: entries[0],
};
};
export const getList: RouteHandler = async (request, response) => {
try {
const subscriptions = db.select().from(feeds).all();
response.status(200);
return { subscriptions };
} catch (e) {
response.status(200);
return { subscriptions: [] };
}
};