on 45c2363 help sign in
webring/scripts/hallway.js
182 lines 5.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 this.findFeeds()
12 }
14 this.start = function () {
15 this.el.innerHTML = 'hello'
16 this.fetchFeeds()
17 }
19 this.refresh = function (feeds) {
20 const entries = this.findEntries(feeds)
21 const channels = this.findChannels(entries)
22 const users = this.findUsers(entries)
24 let _entries = entries.reduce((acc, val, id) => { return acc + this.templateEntry(val) + '\n' }, '')
25 let _channels = Object.keys(channels).reduce((acc, val, id) => { return acc + `<li>${val} <span class='right'>${channels[val]}</span></li>\n` }, '')
26 let _users = Object.keys(users).reduce((acc, val, id) => { return acc + `<li>${val} <span class='right'>${users[val]}</span></li>\n` }, '')
28 this.el.innerHTML = `
29 <ul id='entries'>
30 ${_entries}
31 </ul>
32 <div id='sidebar'>
33 <ul id='channels'>
34 ${_channels}
35 </ul>
36 <ul id='users'>
37 ${_users}
38 </ul>
39 </div>
40 ${this._footer()}`
41 }
43 // Entries
45 this.findChannels = function (entries) {
46 const channels = {}
47 for (const id in entries) {
48 const entry = entries[id]
49 if (!entry.channel) { continue }
50 channels[entry.channel] = channels[entry.channel] ? channels[entry.channel] + 1 : 1
51 }
52 return channels
53 }
55 this.findUsers = function (entries) {
56 const users = {}
57 for (const id in entries) {
58 const entry = entries[id]
59 users[entry.author] = users[entry.author] ? users[entry.author] + 1 : 1
60 }
61 return users
62 }
64 this.findEntries = function (feeds) {
65 const a = []
66 for (const id in feeds) {
67 const feed = feeds[id]
68 for (const i in feed.content) {
69 const entry = feed.content[i]
70 a.push(entry)
71 }
72 }
73 return a.sort(compare)
74 }
76 this.templateEntry = function (entry) {
77 // Find mention
78 if (entry.body.indexOf('@<') > -1) {
79 const data = entry.body.split('@<').pop().split('>')[0]
80 const mention = data.split(' ')
81 const name = mention[0]
82 const path = mention[1]
83 entry.body = entry.body.replace(`@<${data}>`, `<a href='${path}'>@${name}</a>`)
84 }
86 if (entry.body.substr(0, 1) === '/') {
87 const channel = entry.body.split(' ')[0]
88 entry.body = entry.body.replace(channel, `<span class='channel'>${channel}</span>`)
89 }
91 const filter = window.location.hash.substr(1).replace(/\+/g, ' ').toLowerCase()
92 const highlight = filter == entry.author.toLowerCase()
94 return `<li class='entry ${highlight ? 'highlight' : ''}'><span class='date'>${timeAgo(Date.parse(entry.date))}</span> <a class='author' href=''>${entry.author}</a> <span class='body'>${entry.body}</span></li>`
95 }
97 // Feeds
99 this.findFeeds = function () {
100 console.log('Finding feeds..')
101 for (const id in sites) {
102 const site = sites[id]
103 if (site.feed && site.author) {
104 feeds[site.author] = { path: site.feed }
105 }
106 }
107 console.log(`Found ${Object.keys(feeds).length} feeds.`)
108 }
110 this.fetchFeeds = function () {
111 console.log(`Fetching ${Object.keys(feeds).length} feeds..`)
112 for (const id in feeds) {
113 this.fetchFeed(id, feeds[id])
114 }
115 }
117 this.fetchFeed = function (id, feed) {
118 console.log(`Fetching ${id}(${feed.path})..`)
120 fetch(feed.path, { cache: 'no-store' }).then(x => x.text()).then((content) => {
121 feeds[id].content = parseFeed(id, content)
122 this.refresh(feeds)
123 }).catch((err) => {
124 console.warn(`${id}`, err)
125 })
126 }
128 // Extras
130 this._footer = function () {
131 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 <a href="index.html">webring</a>.</p>'
132 }
134 // Utils
136 function compare (a, b) {
137 return a.offset < b.offset ? -1 : a.offset > b.offset ? 1 : 0
138 }
140 function parseFeed (author, feed) {
141 const lines = feed.split('\n')
142 const entries = []
143 for (const id in lines) {
144 const line = lines[id].trim()
145 if (line === '') { continue }
146 const parts = line.replace(' ', '\t').split('\t')
147 const date = parts[0].trim()
148 const body = parts[1].trim()
149 const channel = body.substr(0, 1) === '/' ? body.split(' ')[0].substr(1).toLowerCase() : null
150 const offset = new Date() - new Date(date)
151 entries.push({ date, body, author, offset, channel })
152 }
153 return entries
154 }
156 function timeAgo (dateParam) {
157 const date = typeof dateParam === 'object' ? dateParam : new Date(dateParam)
158 const DAY_IN_MS = 86400000 // 24 * 60 * 60 * 1000
159 const today = new Date()
160 const yesterday = new Date(today - DAY_IN_MS)
161 const seconds = Math.round((today - date) / 1000)
162 const minutes = Math.round(seconds / 60)
163 const isToday = today.toDateString() === date.toDateString()
164 const isYesterday = yesterday.toDateString() === date.toDateString()
166 if (seconds < 5) {
167 return 'now'
168 } else if (seconds < 60) {
169 return `${seconds} seconds ago`
170 } else if (seconds < 90) {
171 return 'about a minute ago'
172 } else if (minutes < 60) {
173 return `${minutes} minutes ago`
174 } else if (isToday) {
175 return `${Math.floor(minutes / 60)} hours ago`
176 } else if (isYesterday) {
177 return 'yesterday'
178 }
180 return `${Math.floor(minutes / 1440)} days ago`
181 }