on master help sign in
add in the ability to filter out messages that hold external references to non-hallway members
commitfb660b0c24bacd569e55fa6898aaaa040bf25b33 authorckipp01 <ckipp@pm.me> dateSat 3 Aug 2019, 11:49 (7 years ago) parentb057ef0
1 file changed+31−23
scripts/hallway.js+31−23
scripts/hallway.js+31−23
@@ -1,9 +1,9 @@
11'use strict'
22
3−const re_channel = /(\s|^)\/([a-zA-Z0-9]+)/g
4−const re_user = /((\s|^)@&lt;[a-zA-Z0-9./:\-_\+~#= ]*&gt;)/g
5−const re_tag = /([^&]|^)\#([a-zA-Z0-9]+)/g
6−const re_url = /((https?):\/\/(([-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b)([-a-zA-Z0-9@:%_\+.~#?&//=]*)))/g
3+const reChannel = /(\s|^)\/([a-zA-Z0-9]+)/g
4+const reUser = /((\s|^)@&lt;[a-zA-Z0-9./:\-_+~#= ]*&gt;)/g
5+const reTag = /([^&]|^)#([a-zA-Z0-9]+)/g
6+const reUrl = /((https?):\/\/(([-a-zA-Z0-9j:%._+~#=]{2,256}\.[a-z]{2,6}\b)([-a-zA-Z0-9@:%_+.~#?&//=]*)))/g
77
88function Hallway (sites) {
99 const feeds = {}
@@ -37,14 +37,15 @@ function Hallway (sites) {
3737
3838 this.refresh = function (feeds = this.cache) {
3939 const entries = this.findEntries(feeds)
40− const channels = this.find(entries, 'channel')
41− const users = this.find(entries, 'author')
42− const tags = this.findTags(entries)
43− const relevantEntries = entries.filter(val => !this.finder.filter || (val.author === this.finder.filter || val.channel === this.finder.filter || val.tags.includes(this.finder.filter)))
40+ const localEntries = entries.filter(entry => this.filterExternals(entry))
41+ const channels = this.find(localEntries, 'channel')
42+ const users = this.find(localEntries, 'author')
43+ const tags = this.findTags(localEntries)
44+ const relevantEntries = localEntries.filter(val => !this.finder.filter || (val.author === this.finder.filter || val.channel === this.finder.filter || val.tags.includes(this.finder.filter)))
4445
4546 this._entries.innerHTML = `
4647 <ul>
47− ${entries.filter((val) => !this.finder.filter || (val.author === this.finder.filter || val.channel === this.finder.filter || val.tags.includes(this.finder.filter))).filter((_, id) => id < Number(this.finder.page) * 20 && id >= (Number(this.finder.page) - 1) * 20).reduce((acc, val) => acc + this.templateEntry(val) + '\n', '')}
48+ ${localEntries.filter((val) => !this.finder.filter || (val.author === this.finder.filter || val.channel === this.finder.filter || val.tags.includes(this.finder.filter))).filter((_, id) => id < Number(this.finder.page) * 20 && id >= (Number(this.finder.page) - 1) * 20).reduce((acc, val) => acc + this.templateEntry(val) + '\n', '')}
4849 </ul>
4950 <div id='pagination'>
5051 ${[...Array(Math.ceil(relevantEntries.length / 20)).keys()].reduce((acc, num) => `${acc}<span class='${Number(hallway.finder.page) === num + 1 ? 'selected' : ''}' onclick='filter("${this.finder.filter}^${num + 1}")'>${num + 1}</span>`, '')}
@@ -54,7 +55,7 @@ function Hallway (sites) {
5455 this._sidebar.innerHTML = `
5556 <a id='hidebar' onclick="toggleVisibility('sidebar');"></a>
5657 <ul id='channels'>
57− <li onclick='filter("")' class='${hallway.finder.filter === '' ? 'selected' : ''}'><a href='#'>hallway <span class='right'>${entries.length}</span></a></li>
58+ <li onclick='filter("")' class='${hallway.finder.filter === '' ? 'selected' : ''}'><a href='#'>hallway <span class='right'>${localEntries.length}</span></a></li>
5859 ${Object.keys(channels).slice(0, 15).reduce((acc, val) => acc + `<li onclick='filter("${val}")' class='${hallway.finder.filter === val ? 'selected' : ''}'><a href='#${val}'>${val} <span class='right'>${channels[val]}</span></a></li>\n`, '')}
5960 </ul>
6061 <ul id='users'>
@@ -71,18 +72,25 @@ function Hallway (sites) {
7172
7273 // Entries
7374
74− this.find = function (entries, p) {
75+ this.filterExternals = function (entry) {
76+ const matches = entry.body.match(reUser)
77+ const locals = Object.keys(feeds)
78+ return matches ? matches.some(match => locals.some(local => match.indexOf(feeds[local].path) !== -1)) : true
79+ }
80+
81+ this.find = function (entries, key) {
7582 const h = {}
76− for (const id in entries) {
77− if (entries[id] && entries[id][p]) { h[entries[id][p]] = h[entries[id][p]] ? h[entries[id][p]] + 1 : 1 }
83+ for (const entry of entries) {
84+ if (entry && entry[key]) {
85+ h[entry[key]] = h[entry[key]] ? h[entry[key]] + 1 : 1
86+ }
7887 }
7988 return h
8089 }
8190
8291 this.findTags = function (entries) {
8392 const tags = {}
84− for (const id in entries) {
85− const entry = entries[id]
93+ for (const entry of entries) {
8694 entry.tags.map(tag => {
8795 tags[tag] = tags[tag] ? tags[tag] + 1 : 1
8896 })
@@ -102,15 +110,15 @@ function Hallway (sites) {
102110
103111 this.findMention = function (found) {
104112 const mention = Object.keys(feeds).filter(user => found.indexOf(feeds[user].path) > -1)
105− return mention.length === 1 ? ` <span class='user local'>${mention[0]}</span>` : ` <span class='user external'>${found.substr(5).split(' ')[0]}</span>`
113+ return ` <span class='user local'>${mention[0]}</span>`
106114 }
107115
108116 this.templateEntry = function (entry) {
109117 entry.html = entry.body
110− .replace(re_channel, `$1<span class='channel'>/$2</span>`)
111− .replace(re_user, this.findMention)
112− .replace(re_tag, `$1<span class='tag'>#$2</span>`)
113− .replace(re_url, `<a target="_blank" href='$1'>$1</a>`)
118+ .replace(reChannel, `$1<span class='channel'>/$2</span>`)
119+ .replace(reUser, this.findMention)
120+ .replace(reTag, `$1<span class='tag'>#$2</span>`)
121+ .replace(reUrl, `<a target="_blank" href='$1'>$1</a>`)
114122
115123 const filter = window.location.hash.substr(1).replace(/\+/g, ' ').toLowerCase()
116124 const highlight = filter === entry.author.toLowerCase()
@@ -160,7 +168,7 @@ function Hallway (sites) {
160168 const date = parts[0].trim()
161169 const body = escapeHtml(parts[1].trim()).trim()
162170 const channel = body.substr(0, 1) === '/' ? body.split(' ')[0].substr(1).toLowerCase() : body.substr(0, 1) === '@' ? 'veranda' : 'lobby'
163− const tags = (body.match(re_tag) || []).map(a => a.substr(a.indexOf('#') + 1).toLowerCase())
171+ const tags = (body.match(reTag) || []).map(a => a.substr(a.indexOf('#') + 1).toLowerCase())
164172 const offset = new Date() - new Date(date)
165173 entries.push({ date, body, author, offset, channel, tags })
166174 }
@@ -195,8 +203,8 @@ function Hallway (sites) {
195203}
196204
197205function toggleVisibility (id) {
198− var e = document.getElementById(id)
199− if (e.style.display == 'block') { e.style.display = 'none' } else { e.style.display = 'block' }
206+ const e = document.getElementById(id)
207+ if (e.style.display === 'block') { e.style.display = 'none' } else { e.style.display = 'block' }
200208}
201209
202210function escapeHtml (unsafe) {