on 9d85e83 help sign in
webring/scripts/hallway.js
187 lines 6.2 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 this.el.innerHTML = `
25 <ul id='entries'>
26 ${entries.filter((val, id) => { return id < 50 }).reduce((acc, val, id) => { return acc + this.templateEntry(val) + '\n' }, '')}
27 </ul>
28 <div id='sidebar'>
29 <ul id='channels'>
30 ${Object.keys(channels).reduce((acc, val, id) => { return acc + `<li>${val} <span class='right'>${channels[val]}</span></li>\n` }, '')}
31 </ul>
32 <ul id='users'>
33 ${Object.keys(users).reduce((acc, val, id) => { return acc + `<li>${val} <span class='right'>${users[val]}</span></li>\n` }, '')}
34 </ul>
35 </div>
36 <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>`
37 }
39 // Entries
41 this.findChannels = function (entries) {
42 const channels = {}
43 for (const id in entries) {
44 const entry = entries[id]
45 if (!entry.channel) { continue }
46 channels[entry.channel] = channels[entry.channel] ? channels[entry.channel] + 1 : 1
47 }
48 return channels
49 }
51 this.findUsers = function (entries) {
52 const users = {}
53 for (const id in entries) {
54 const entry = entries[id]
55 users[entry.author] = users[entry.author] ? users[entry.author] + 1 : 1
56 }
57 return users
58 }
60 this.findEntries = function (feeds) {
61 const a = []
62 for (const id in feeds) {
63 for (const i in feeds[id].content) {
64 a.push(feeds[id].content[i])
65 }
66 }
67 return a.sort(compare)
68 }
70 this.templateEntry = function (entry) {
71 const words = entry.body.split(' ')
72 const users = []
73 const channels = []
74 const tags = []
75 const links = []
77 // Channels
78 for (const id in words) {
79 if (words[id].substr(0, 1) === '@') { users.push(words[id]) }
80 if (words[id].substr(0, 1) === '/') { channels.push(words[id]) }
81 if (words[id].substr(0, 1) === '#') { tags.push(words[id]) }
82 if (words[id].substr(0, 4) === 'http') { links.push(words[id]) }
83 }
85 for (const id in users) {
86 entry.body = entry.body.replace(users[id], `<span class='user'>${users[id]}</span>`)
87 }
88 for (const id in channels) {
89 entry.body = entry.body.replace(channels[id], `<span class='user'>${channels[id]}</span>`)
90 }
91 for (const id in tags) {
92 entry.body = entry.body.replace(tags[id], `<span class='tag'>${tags[id]}</span>`)
93 }
94 for (const id in links) {
95 entry.body = entry.body.replace(links[id], `<a href='${links[id]}'>${links[id].replace('https://', '').replace('http://', '')}</a>`)
96 }
98 const filter = window.location.hash.substr(1).replace(/\+/g, ' ').toLowerCase()
99 const highlight = filter === entry.author.toLowerCase()
100 const origin = feeds[entry.author].path
102 return `<li class='entry ${highlight ? 'highlight' : ''}'><span class='date'>${timeAgo(Date.parse(entry.date))}</span> <a class='author' href='${origin}' target='_blank'>${entry.author}</a> <span class='body'>${entry.body}</span></li>`
103 }
105 // Feeds
107 this.findFeeds = function () {
108 console.log('Finding feeds..')
109 for (const id in sites) {
110 const site = sites[id]
111 if (site.feed && site.author) {
112 feeds[site.author] = { path: site.feed }
113 }
114 }
115 console.log(`Found ${Object.keys(feeds).length} feeds.`)
116 }
118 this.fetchFeeds = function () {
119 console.log(`Fetching ${Object.keys(feeds).length} feeds..`)
120 for (const id in feeds) {
121 this.fetchFeed(id, feeds[id])
122 }
123 }
125 this.fetchFeed = function (id, feed) {
126 console.log(`Fetching ${id}(${feed.path})..`)
127 fetch(feed.path, { cache: 'no-store' }).then(x => x.text()).then((content) => {
128 feeds[id].content = parseFeed(id, content)
129 this.refresh(feeds)
130 }).catch((err) => {
131 console.warn(`${id}`, err)
132 })
133 }
135 // Utils
137 function compare (a, b) {
138 return a.offset < b.offset ? -1 : a.offset > b.offset ? 1 : 0
139 }
141 function parseFeed (author, feed) {
142 const lines = feed.split('\n')
143 const entries = []
144 for (const id in lines) {
145 const line = lines[id].trim()
146 if (line === '') { continue }
147 const parts = line.replace(' ', '\t').split('\t')
148 const date = parts[0].trim()
149 const body = escapeHtml(parts[1].trim()).trim()
150 const channel = body.substr(0, 1) === '/' ? body.split(' ')[0].substr(1).toLowerCase() : null
151 const offset = new Date() - new Date(date)
152 entries.push({ date, body, author, offset, channel })
153 }
154 return entries
155 }
157 function timeAgo (dateParam) {
158 const date = typeof dateParam === 'object' ? dateParam : new Date(dateParam)
159 const DAY_IN_MS = 86400000 // 24 * 60 * 60 * 1000
160 const today = new Date()
161 const yesterday = new Date(today - DAY_IN_MS)
162 const seconds = Math.round((today - date) / 1000)
163 const minutes = Math.round(seconds / 60)
164 const isToday = today.toDateString() === date.toDateString()
165 const isYesterday = yesterday.toDateString() === date.toDateString()
167 if (seconds < 5) {
168 return 'now'
169 } else if (seconds < 60) {
170 return `${seconds} seconds ago`
171 } else if (seconds < 90) {
172 return 'about a minute ago'
173 } else if (minutes < 60) {
174 return `${minutes} minutes ago`
175 } else if (isToday) {
176 return `${Math.floor(minutes / 60)} hours ago`
177 } else if (isYesterday) {
178 return 'yesterday'
179 }
181 return `${Math.floor(minutes / 1440)} days ago`
182 }
184 function escapeHtml (unsafe) {
185 return unsafe.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#039;')
186 }