diff options
| author | s-ol <s+removethis@s-ol.nu> | 2022-01-13 17:28:04 +0000 |
|---|---|---|
| committer | s-ol <s+removethis@s-ol.nu> | 2022-01-13 17:28:04 +0000 |
| commit | ad6f77fcadd3968d9a9edb840aacad1e7efec8ea (patch) | |
| tree | ea17d08d54ce88135fe35e637e947db8434ea666 /main.ts | |
| parent | better auth, list (diff) | |
| download | fedidag-ad6f77fcadd3968d9a9edb840aacad1e7efec8ea.tar.gz fedidag-ad6f77fcadd3968d9a9edb840aacad1e7efec8ea.zip | |
move everything into server/
Diffstat (limited to 'main.ts')
| -rw-r--r-- | main.ts | 195 |
1 files changed, 0 insertions, 195 deletions
diff --git a/main.ts b/main.ts deleted file mode 100644 index 0bb805d..0000000 --- a/main.ts +++ /dev/null @@ -1,195 +0,0 @@ -import { URLSearchParams } from "url"; -import express from "express"; -import cors from "cors"; -import fetch from "node-fetch"; -import cookieParser from "cookie-parser"; -import * as cheerio from "cheerio"; - -import { wrap } from "./async"; - -const PORT = 3000; -const DISCDAG_URL = 'http://solipsys.co.uk/cgi-bin/DiscDAG.py'; -const PUBLIC_URL = 'http://localhost:3000'; - -const app = express(); - -app.use(cors({ origin: 'http://localhost:8080', credentials: true })); -app.use(express.json()); -app.use(cookieParser()); - -const discdag = express() - .post('/login', wrap(async ($req, $res) => { - const { username, password } = $req.body; - if (!username || !password) - throw new Error("no credentials"); - - const res = await fetch(DISCDAG_URL, { - method: 'POST', - headers: { - 'Content-Type': 'application/x-www-form-urlencoded', - }, - body: new URLSearchParams({ - Action: 'Login', - Username: username, - Password: password, - }).toString(), - }); - - if (res.status !== 200) - throw new Error("bad response"); - - const cookiestr = res.headers.get('set-cookie') ?? ''; - const match = cookiestr.match(/digest=([^ ]+);/); - if (!match || !match[1].length) throw new Error("wrong credentials"); - - const doc = cheerio.load(await res.text()); - const name = doc("font b").text().match(/browsing as (.*)$/)![1]; - const user = `${PUBLIC_URL}/discdag/user/${name}`; - - return $res - .cookie('digest', match[1]) - .json({ - '@context': 'https://www.w3.org/ns/activitystreams', - id: user, - type: 'User', - name, - }); - })) - - .get('/list', wrap(async ($req, $res) => { - const { digest } = $req.cookies; - - const res = await fetch(DISCDAG_URL + '?Action=ListDiscussions', { - method: 'POST', - headers: { - 'Content-Type': 'application/x-www-form-urlencoded', - 'Cookie': `digest=${digest}`, - }, - }); - - const doc = cheerio.load(await res.text()); - const name = doc("font b").text().match(/browsing as (.*)$/)![1]; - const user = `${PUBLIC_URL}/discdag/user/${name}`; - - const discussions = doc("ul > li").map(function(this: any) { - const writeAccess = this.children[0].data.indexOf('(W)') > -1; - const href = doc("a", this).attr("href")!; - const id = href.match(/cgi-bin\/DiscDAG.py\?DiscussionID=(.+)/)![1]; - - return { - id: `${PUBLIC_URL}/discdag/${id}`, - type: 'Document', - name: id, - url: { - type: 'Link', - href, - }, - attributedTo: writeAccess ? user : null, - audience: user, - }; - }).toArray(); - - return $res.json({ discussions }); - })) - - .get('/user/:name', wrap(async ($req, $res) => { - const { name } = $req.params; - - return $res.json({ - '@context': 'https://www.w3.org/ns/activitystreams', - id: `${PUBLIC_URL}/discdag/user/${name}`, - type: 'User', - name, - }); - })) - - .get('/:discussion', wrap(async ($req, $res) => { - const { discussion } = $req.params; - const { digest } = $req.cookies; - - const res = await fetch(DISCDAG_URL, { - headers: { - 'Cookie': `DiscussionID=${discussion}; digest=${digest}`, - } - }); - - if (res.status !== 200) - throw new Error("bad response"); - - const doc = cheerio.load(await res.text()); - - type Note = { - type: 'Note'; - published: string; - attributedTo: string | { - id: string; - type: 'Person', - name: string; - }; - inReplyTo: string[]; - replies: string[]; - content: string; - }; - - let first = null; - const items: Record<string, Note> = {}; - for (const node of doc('g.node', 'svg')) { - const nodeId = doc('title', node).text(); - const [_, time, author] = nodeId.split('_'); - - const id = `${PUBLIC_URL}/discdag/${discussion}/${nodeId}`; - first = first ?? id; - - const content = doc('g:first text', node) - .slice(3) - .map(function(this: any) { return doc(this).text(); }) - .toArray() - .join(' ').trim(); - - items[id] = { - type: 'Note', - published: time.replace(/^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)[a-z]+/, '$1-$2-$3T$4:$5:$6Z'), - attributedTo: `${PUBLIC_URL}/discdag/user/${author}`, - inReplyTo: [], - replies: [], - content, - }; - } - - for (const node of doc('g.edge', 'svg')) { - let [frm, to] = doc('title', node).text().split('->'); - frm = `${PUBLIC_URL}/discdag/${discussion}/${frm}`; - to = `${PUBLIC_URL}/discdag/${discussion}/${to}`; - - items[frm].replies.push(to); - items[to].inReplyTo.push(frm); - } - - return $res.json({ - '@context': [ - 'https://www.w3.org/ns/activitystreams', - { - items: { - '@id': 'as:items', - '@type': '@id', - '@container': '@id' - }, - }, - ], - id: `${PUBLIC_URL}/discdag/${discussion}`, - type: 'Document', - name: discussion, - first, - items, - }); - })); - -app.use('/discdag', discdag) - -try { - app.listen(PORT, (): void => { - console.log(`Connected successfully on port ${PORT}`); - }); -} catch (error) { - console.error(`Error occured: ${error}`); -} |
