on 39efe03 help sign in
webring/scripts/hallway.js
100 lines 2.6 kB history raw
1'use strict'
3function Hallway (sites) {
4 const feeds = {}
5 this.sites = sites
6 this.el = document.createElement('div')
7 this.el.id = 'hallway'
9 this.install = function (host) {
10 host.appendChild(this.el)
11 // Find feeds
12 this.findFeeds()
13 }
15 this.start = function () {
16 this.el.innerHTML = 'hello'
17 this.fetchFeeds()
18 // this.fetch('https://wiki.xxiivv.com/twtxt.txt')
19 }
21 this.refresh = function () {
22 const entries = this.sortEntries()
23 let html = ''
24 for (const id in entries) {
25 html += `${this.templateEntry(entries[id])}\n`
26 }
27 this.el.innerHTML = `<ul>${html}</ul>${this._footer()}`
28 }
30 // Entries
32 this.sortEntries = function () {
33 const a = []
34 for (const id in feeds) {
35 const feed = feeds[id]
36 for (const i in feed.content) {
37 const entry = feed.content[i]
38 a.push(entry)
39 }
40 }
41 return a
42 }
44 this.templateEntry = function (entry) {
45 const now = new Date()
46 const then = Date.parse(entry.date)
47 const diff = (now - then)
48 const date = Math.floor(diff / 86400000)
50 return `<li class='entry'><span class='date'>${date}</span> <span class='author'>${entry.author}</span> <span class='body'>${entry.body}</span></li>`
51 }
53 // Feeds
55 this.findFeeds = function () {
56 console.log('Finding feeds..')
57 for (const id in sites) {
58 const site = sites[id]
59 if (site.feed && site.author) {
60 feeds[site.author] = { path: site.feed }
61 }
62 }
63 console.log(`Found ${Object.keys(feeds).length} feeds.`)
64 }
66 this.fetchFeeds = function () {
67 console.log(`Fetching ${Object.keys(feeds).length} feeds..`)
68 for (const id in feeds) {
69 this.fetchFeed(id, feeds[id])
70 }
71 }
73 this.fetchFeed = function (id, feed) {
74 console.log(`Fetching ${id}(${feed.path})..`)
75 Promise.all([ fetch(feed.path).then(x => x.text()) ]).then(([content]) => {
76 feeds[id].content = parseFeed(id, content)
77 this.refresh()
78 })
79 }
81 // Extras
83 this._footer = function () {
84 return '<p>The <b>Hallway</b> is a decentralized forum, to join the conversation, simply create yourself a <a href="https://twtxt.readthedocs.io/en/stable/user/twtxtfile.html">twtxt</a> feed and add it to your entry in the webring.</p>'
85 }
87 // Utils
89 function parseFeed (author, feed) {
90 const lines = feed.split('\n')
91 const entries = []
92 for (const id in lines) {
93 const line = lines[id]
94 const date = line.substr(0, 25).trim()
95 const body = line.substr(26).trim()
96 entries.push({ date, body, author })
97 }
98 return entries
99 }