git ssb

1+

bencevans / ssb-news



Tree: 42db44acea925a145748bffb1196e0ec554485c2

Files: 42db44acea925a145748bffb1196e0ec554485c2 / src / server.js

4679 bytesRaw
1const SSBNews = require('.')
2const getUserAbout = require('./helpers/get-user-about')
3const getDigsFor = require('./helpers/get-digs-for')
4const getCommentsFor = require('./helpers/get-comments-for')
5const path = require('path')
6const url = require('url')
7const express = require('express')
8const sAgo = require('s-ago').default
9const util = require('util')
10const bodyParser = require('body-parser')
11
12module.exports = function createServer (sbot) {
13
14 if (!sbot.links) {
15 throw new Error('links sbot plugin required: sbot plugins.install ssb-links')
16 }
17
18 if (!sbot.backlinks) {
19 throw new Error('backlinks sbot plugin required: sbot plugins.install ssb-backlinks')
20 }
21
22
23 const news = new SSBNews(sbot)
24 const server = express()
25
26 server.set('view engine', 'pug')
27 server.set('views', path.resolve(__dirname, 'views'))
28
29 server.get('/', async (req, res, next) => {
30 const rawStories = await util.promisify(news.recent.bind(news))()
31 const stories = await Promise.all(rawStories.map(async (story) => {
32 const about = await util.promisify(getUserAbout)(sbot, story.value.author)
33 const comments = await util.promisify(getCommentsFor)(sbot, story.key)
34 const digs = await util.promisify(getDigsFor)(sbot, story.key)
35 return {
36 id: story.key,
37 title: story.value.content.title,
38 domain: url.parse(story.value.content.link).host,
39 link: story.value.content.link,
40 href: '/story/' + encodeURIComponent(story.key),
41 authorId: story.value.author,
42 authorHref: '/user/' + encodeURIComponent(story.value.author),
43 authorName: about.name,
44 timestamp: story.value.timestamp,
45 ago: sAgo(new Date(story.value.timestamp)),
46 comments,
47 digs
48 }
49 }))
50
51 res.render('index', {
52 title: 'recent',
53 stories: stories
54 })
55 })
56
57 server.get('/recent', (req, res) => {
58 res.redirect('/')
59 })
60
61 server.get('/story/:id', async (req, res, next) => {
62 const story = await util.promisify(news.story.bind(news))(req.params.id)
63 const about = await util.promisify(getUserAbout)(sbot, story.author)
64 const comments = await util.promisify(getCommentsFor)(sbot, req.params.id)
65 const digs = await util.promisify(getDigsFor)(sbot, req.params.id)
66
67 res.render('story', {
68 story: {
69 id: req.params.id,
70 title: story.content.title,
71 domain: url.parse(story.content.link).host,
72 link: story.content.link,
73 href: '/story/' + encodeURIComponent(req.params.id),
74 authorId: story.author,
75 authorHref: '/user/' + encodeURIComponent(story.author),
76 authorName: about.name,
77 timestamp: story.timestamp,
78 ago: sAgo(new Date(story.timestamp)),
79 comments,
80 digs
81 }
82 })
83 })
84
85 server.get('/user/:id', (req, res, next) => {
86 getUserAbout(sbot, req.params.id, (err, about) => {
87 if (err) return next(err)
88 res.render('user', {
89 title: about.name,
90 about
91 })
92 })
93 })
94
95 server.get('/me', (req, res) => {
96 res.redirect('/user/' + sbot.id)
97 })
98
99 server.get('/new', (req, res) => {
100 res.render('new', {})
101 })
102
103 server.get('/best', async (req, res) => {
104 const rawStories = await util.promisify(news.recent.bind(news))()
105 const stories = (await Promise.all(rawStories.map(async (story) => {
106 const about = await util.promisify(getUserAbout)(sbot, story.value.author)
107 const comments = await util.promisify(getCommentsFor)(sbot, story.key)
108 const digs = await util.promisify(getDigsFor)(sbot, story.key)
109 return {
110 id: story.key,
111 title: story.value.content.title,
112 domain: url.parse(story.value.content.link).host,
113 link: story.value.content.link,
114 href: '/story/' + encodeURIComponent(story.key),
115 authorId: story.value.author,
116 authorHref: '/user/' + encodeURIComponent(story.value.author),
117 authorName: about.name,
118 timestamp: story.value.timestamp,
119 ago: sAgo(new Date(story.value.timestamp)),
120 comments,
121 digs
122 }
123 }))).sort((a, b) => {
124 a.digs.length > b.digs.length
125 })
126
127 res.render('index', {
128 title: 'best',
129 stories: stories
130 })
131 })
132
133 server.get('/dig/:id', (req, res) => {
134
135 })
136
137 server.post('/new', bodyParser.urlencoded({extended: false}), (req, res) => {
138 const {title, link, body} = req.body
139 news.publish({
140 title, link, body
141 }, (err, data) => {
142 console.log(err)
143 if (err) return next(err)
144 res.redirect('/story/' + encodeURIComponent(data.key))
145 })
146 })
147
148 server.listen(3000)
149 console.log('Listening on http://127.0.0.1:3000')
150}
151

Built with git-ssb-web