Browse Source

update webapp on master branch.

Jason Schwarzenberger 8 months ago
parent
commit
359496d035

+ 1 - 1
readerserver

@@ -1 +1 @@
-Subproject commit 8ef1d418dc797869370097ff4d77208f5182f635
+Subproject commit fa017a33fef0d1e05956468db998b25f1401eb99

+ 15 - 3
webapp/src/routes/[id].json.js

@@ -1,19 +1,31 @@
 import fetch from 'isomorphic-fetch';
 
+import { keyStories, keyStory } from './_key';
 import { purify, purifyArray } from './_purify';
 import { samesite } from './_samesite';
 
-const API_URL = process.env.API_URL || 'http://localhost:33842';
+import sources from '../sources.json';
 
 export async function get(req, res) {
-	const response = await fetch(`${API_URL}/api/${req.params.id}`);
+	const key = req.params.key;
+	const id = req.params.id;
+	const source = sources[key];
+	if (!source) {
+		res.writeHead(404);
+		return res.end();
+	}
+	console.error(`${source.url}/api/${id}`);
+	const response = await fetch(`${source.url}/api/${id}`);
 	res.writeHead(response.status, { 'Content-Type': response.headers.get('Content-Type') });
 	if (!response.ok) {
 		return res.end(await response.text());
 	}
 	const data = await response.json();
+	data.story = keyStory(data.story, key);
+	data.related = keyStories(data.related, key);
+	data.links = keyStories(data.links, key);
 	data.story = purify(data.story);
 	data.story = samesite(data.story, data.links);
 	data.related = purifyArray(data.related);
 	res.end(JSON.stringify(data));
-}
+}

+ 3 - 1
webapp/src/routes/[id].svelte

@@ -1,6 +1,6 @@
 <script context="module">
   export async function preload({ params }) {
-    const res = await this.fetch(`${params.id}.json`);
+    const res = await this.fetch(`${params.key}-${params.id}.json`);
     const data = await res.json();
 
     if (res.status === 200) {
@@ -18,6 +18,8 @@
   export let story;
   export let related;
 
+  related = related || [];
+
   let others = related.filter(
     (r) => r.id !== story.id && Number(r.num_comments)
   );

+ 13 - 0
webapp/src/routes/_key.js

@@ -0,0 +1,13 @@
+export const keyStory = (story, key) => {
+	if (story.id) {
+		story.id = `${key}-${story.id}`;
+	}
+	return story;
+};
+
+export const keyStories = (array, key) => {
+	if (array instanceof Array) {
+		return array.map(story => keyStory(story, key));
+	}
+	return array;
+};

+ 40 - 10
webapp/src/routes/index.json.js

@@ -1,20 +1,50 @@
 import fetch from 'isomorphic-fetch';
 
 import { purifyArray } from './_purify';
-
-const API_URL = process.env.API_URL || 'http://localhost:33842';
+import { keyStories } from './_key';
+import sources from '../sources.json';
 
 export async function get(req, res) {
 	const { skip, limit } = {
 		skip: req.query.skip || 0,
 		limit: req.query.limit || 20,
 	};
-	const response = await fetch(`${API_URL}/api?skip=${skip}&limit=${limit}`);
-	res.writeHead(response.status, { 'Content-Type': response.headers.get('Content-Type') });
-	if (!response.ok) {
-		return res.end(await response.text());
+	let stories = [];
+	for (const key in sources) {
+		const source = sources[key];
+		if (!source.enabled) {
+			continue;
+		}
+		const response = await fetch(`${source.url}/api?skip=${skip}&limit=${limit}`);
+		if (!response.ok) {
+			continue;
+		}
+
+		const data = await response.json();
+		data.stories.sort((a, b) => b.date - a.date);
+		if (!source.paging) {
+			data.stories = data.stories.slice(skip, limit);
+		}
+		stories = stories.concat(keyStories(data.stories, key));
 	}
-	const data = await response.json();
-	data.stories = purifyArray(data.stories);
-	res.end(JSON.stringify(data));
-}
+
+	stories.sort((a, b) => b.date - a.date);
+	stories = purifyArray(stories);
+	res.writeHead(200, { 'Content-Type': 'application/json' });
+	res.end(JSON.stringify({ stories }));
+}
+
+// export async function get(req, res) {
+// 	const { skip, limit } = {
+// 		skip: req.query.skip || 0,
+// 		limit: req.query.limit || 20,
+// 	};
+// 	const response = await fetch(`${API_URL}/api?skip=${skip}&limit=${limit}`);
+// 	res.writeHead(response.status, { 'Content-Type': response.headers.get('Content-Type') });
+// 	if (!response.ok) {
+// 		return res.end(await response.text());
+// 	}
+// 	const data = await response.json();
+// 	data.stories = purifyArray(data.stories);
+// 	res.end(JSON.stringify(data));
+// }

+ 40 - 10
webapp/src/routes/search.json.js

@@ -1,20 +1,50 @@
 import fetch from 'isomorphic-fetch';
 
 import { purifyArray } from './_purify';
-
-const API_URL = process.env.API_URL || 'http://localhost:33842';
+import { keyStories, keyStory } from './_key';
+import sources from '../sources.json';
 
 export async function get(req, res) {
 	const { skip, limit } = {
 		skip: req.query.skip || 0,
 		limit: req.query.limit || 20,
 	};
-	const response = await fetch(`${API_URL}/api/search?q=${req.query.q}&skip=${skip}&limit=${limit}`);
-	res.writeHead(response.status, { 'Content-Type': response.headers.get('Content-Type') });
-	if (!response.ok) {
-		return res.end(await response.text());
+	let results = [];
+	for (const key in sources) {
+		const source = sources[key];
+		if (!source.enabled) {
+			continue;
+		}
+		const response = await fetch(`${source.url}/api/search?q=${req.query.q}&skip=${skip}&limit=${limit}`);
+		if (!response.ok) {
+			continue;
+		}
+
+		const data = await response.json();
+		data.results.sort((a, b) => b.date - a.date);
+		if (!source.paging) {
+			data.results = data.results.slice(skip, limit);
+		}
+		results = results.concat(keyStories(data.results, key));
 	}
-	const data = await response.json();
-	data.results = purifyArray(data.results);
-	res.end(JSON.stringify(data));
-}
+
+	results.sort((a, b) => b.date - a.date);
+	results = purifyArray(results);
+	res.writeHead(200, { 'Content-Type': 'application/json' });
+	res.end(JSON.stringify({ results }));
+}
+
+// export async function get(req, res) {
+// 	const { skip, limit } = {
+// 		skip: req.query.skip || 0,
+// 		limit: req.query.limit || 20,
+// 	};
+// 	const response = await fetch(`${API_URL}/api/search?q=${req.query.q}&skip=${skip}&limit=${limit}`);
+// 	res.writeHead(response.status, { 'Content-Type': response.headers.get('Content-Type') });
+// 	if (!response.ok) {
+// 		return res.end(await response.text());
+// 	}
+// 	const data = await response.json();
+// 	data.results = purifyArray(data.results);
+// 	res.end(JSON.stringify(data));
+// }

+ 5 - 3
webapp/src/routes/submit.json.js

@@ -2,15 +2,17 @@ import FormData from 'form-data';
 import fetch from 'isomorphic-fetch';
 import redirect from '@polka/redirect';
 
-const API_URL = process.env.API_URL || 'http://localhost:33842';
+import sources from '../sources.json';
+
+const key = Object.keys(sources).find(k => sources[k].enabled && sources[k].primary);
 
 export async function post(req, res) {
 	const body = new FormData();
 	body.append('url', req.body.url);
-	const response = await fetch(`${API_URL}/api/submit`, { method: "POST", body });
+	const response = await fetch(`${sources[key].url}/api/submit`, { method: "POST", body });
 	if (req.body.redirect) {
 		const { nid } = await response.json();
-		return redirect(res, 302, `/${nid}`);
+		return redirect(res, 302, `/${key}-${nid}`);
 	}
 	res.writeHead(response.status, { 'Content-Type': response.headers.get('Content-Type') });
 	res.end(await response.text());

+ 17 - 0
webapp/src/sources.example.json

@@ -0,0 +1,17 @@
+{
+	"0": {
+		"url": "http://localhost:33842",
+		"primary": true,
+		"paging": true,
+		"enabled": false
+	},
+	"1j": {
+		"url": "https://qotnews.1j.nz",
+		"paging": true,
+		"enabled": true
+	},
+	"t0": {
+		"url": "https://news.t0.vc",
+		"enabled": true
+	}
+}

+ 20 - 0
webapp/src/sources.json

@@ -0,0 +1,20 @@
+{
+	"0": {
+		"url": "http://localhost:33842",
+		"primary": true,
+		"paging": true,
+		"enabled": false
+	},
+	"1j": {
+		"url": "https://qotnews.1j.nz",
+		"primary": true,
+		"paging": true,
+		"enabled": true
+	},
+	"t0": {
+		"url": "https://news.t0.vc",
+		"primary": false,
+		"paging": false,
+		"enabled": true
+	}
+}