on 111d52e help sign in
webring/scripts/hallway.js
119 lines 3.4 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 // Find mention
51 if (entry.body.indexOf('@<') > -1) {
52 const data = entry.body.split('@<').pop().split('>')[0]
53 const mention = data.split(' ')
54 const name = mention[0]
55 const path = mention[1]
56 entry.body = entry.body.replace(`@<${data}>`, `<a href='${path}'>${name}</a>`)
57 }
59 return `<li class='entry'><span class='date'>${ago(date)}</span> <span class='author'>${entry.author}</span> <span class='body'>${entry.body}</span></li>`
60 }
62 // Feeds
64 this.findFeeds = function () {
65 console.log('Finding feeds..')
66 for (const id in sites) {
67 const site = sites[id]
68 if (site.feed && site.author) {
69 feeds[site.author] = { path: site.feed }
70 }
71 }
72 console.log(`Found ${Object.keys(feeds).length} feeds.`)
73 }
75 this.fetchFeeds = function () {
76 console.log(`Fetching ${Object.keys(feeds).length} feeds..`)
77 for (const id in feeds) {
78 this.fetchFeed(id, feeds[id])
79 }
80 }
82 this.fetchFeed = function (id, feed) {
83 console.log(`Fetching ${id}(${feed.path})..`)
84 Promise.all([ fetch(feed.path, { cache: 'no-store' }).then(x => x.text()) ]).then(([content]) => {
85 feeds[id].content = parseFeed(id, content)
86 this.refresh()
87 })
88 }
90 // Extras
92 this._footer = function () {
93 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 <a href="https://github.com/XXIIVV/Webring/">add it</a> to your entry in the webring.</p>'
94 }
96 // Utils
98 function parseFeed (author, feed) {
99 const lines = feed.split('\n')
100 const entries = []
101 for (const id in lines) {
102 const line = lines[id]
103 const date = line.substr(0, 25).trim()
104 const body = line.substr(26).trim()
105 entries.push({ date, body, author })
106 }
107 return entries
108 }
110 function ago (days, cap = 9999) {
111 if (-days > cap) { return `${this.toString(true)}` }
112 if (days === -1) { return `yesterday` }
113 if (days === 1) { return 'tomorrow' }
114 if (days === 0) { return 'today' }
115 if (days < -365) { return `${Math.floor(days / -365)} years ago` }
116 if (days < 1) { return `${days * -1} days ago` }
117 return `in ${days} days`
118 }