Skip to content

Commit 0341bf6

Browse files
committed
refactoring nodejs-ssb
* Extracted pipelines * Extracted caches * Extracted job queues still works. related to #105
1 parent fa21bff commit 0341bf6

File tree

12 files changed

+308
-317
lines changed

12 files changed

+308
-317
lines changed

.jshintrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
"quotmark": "double",
44
"camelcase": true,
55
"browser": true,
6+
"semi": false,
67
"globals": {
78
"Dexie": true,
89
"_": true
910
}
10-
}
11+
}

src/core/components/ui/Card.svelte

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const MessageDropdown = require("../MessageDropdown.svelte")
33
const MessageRaw = require("../MessageRaw.svelte")
44
const AvatarTile = require("../AvatarTile.svelte")
5-
const { isMessageBlured } = require("../../platforms/nodejs-ssb/abusePrevention.js")
5+
const { isMessageBlured } = require("../../platforms/common/abusePrevention.js")
66
77
export let msg
88
export let showRaw = false

src/core/core.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const kernel = require("./kernel/kernel.js")
22
const runtimes = require("./runtimes/runtimes.js")
33
const queryString = require("query-string")
4-
const { isMessageHidden } = require("./platforms/nodejs-ssb/abusePrevention.js")
4+
const { isMessageHidden } = require("./platforms/common/abusePrevention.js")
55

66

77
if (window) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
const { getPref, setPref } = require("../../kernel/prefs.js")
2+
const _ = require("lodash")
3+
4+
const getFilters = () => getPref("filters", [])
5+
6+
const addFilter = filter => {
7+
let currentFilters = getFilters()
8+
9+
filter.action = filter.action || "hide"
10+
filter.feed = filter.feed || false
11+
filter.channel = filter.channel || false
12+
filter.keywords = filter.keywords || false
13+
filter.expires = filter.expires || false
14+
15+
currentFilters.push(filter)
16+
17+
setPref("filters", currentFilters)
18+
}
19+
20+
const deleteFilter = filter => {
21+
let currentFilters = getFilters()
22+
23+
setPref(
24+
"filters",
25+
currentFilters.filter(f => {
26+
let keys = Object.keys(filter)
27+
let include = false
28+
keys.forEach(k => {
29+
if (filter[k] !== f[k]) include = true
30+
})
31+
return include
32+
})
33+
)
34+
}
35+
36+
const isMessageBlured = msg => {
37+
let currentFilters = getFilters().filter(f => f.action == "blur")
38+
if (currentFilters.length > 0) {
39+
let res = currentFilters.map(f => isMessageFiltered(msg, f, "blur"))
40+
return !res.some(r => r)
41+
} else {
42+
return false
43+
}
44+
}
45+
46+
const isChannelFiltered = channel => {
47+
let currentFilters = getFilters().filter(f => f.action == "hide")
48+
if (currentFilters.length > 0) {
49+
let res = currentFilters.map(filter => {
50+
if (channel && channel.startsWith("#")) {
51+
channel = channel.slice(1)
52+
}
53+
54+
if (filter.expires) {
55+
let expirationDate = new Date(filter.expires)
56+
let today = new Date()
57+
58+
if (today > expirationDate) {
59+
return true
60+
}
61+
}
62+
63+
if (channel == filter.channel) {
64+
return false
65+
}
66+
67+
return true
68+
})
69+
70+
return !res.some(r => r)
71+
} else {
72+
return false
73+
}
74+
}
75+
76+
const isMessageHidden = msg => {
77+
let currentFilters = getFilters().filter(f => f.action == "hide")
78+
if (currentFilters.length > 0) {
79+
let res = currentFilters.map(f => isMessageFiltered(msg, f, "hide"))
80+
res = !res.some(r => !r)
81+
return res
82+
} else {
83+
return true // true because it is used by a pull.filter()
84+
}
85+
}
86+
87+
const isMessageFiltered = (msg, filter, action) => {
88+
let filterResults = []
89+
if (filter.action !== action) {
90+
return true
91+
}
92+
93+
if (filter.expires) {
94+
let expirationDate = new Date(filter.expires)
95+
let today = new Date()
96+
97+
if (today > expirationDate) {
98+
return true
99+
}
100+
}
101+
102+
if (filter.feed) {
103+
if (filter.feed == msg.value.author) {
104+
filterResults.push(true)
105+
} else {
106+
filterResults.push(false)
107+
}
108+
}
109+
110+
if (filter.channel) {
111+
if (msg.value.content.channel && filter.channel == msg.value.content.channel) {
112+
filterResults.push(true)
113+
} else {
114+
filterResults.push(false)
115+
}
116+
}
117+
118+
if (filter.keywords.length > 0 && msg.value.content.type == "post" && msg.value.content.text) {
119+
let keywords = filter.keywords
120+
let content = msg.value.content.text.toLowerCase()
121+
122+
let res = keywords.map(k => content.includes(k.toLowerCase())).some(r => r)
123+
if (res) console.log("filtered due to keywords")
124+
filterResults.push(res)
125+
}
126+
127+
return !filterResults.some(n => n == true)
128+
}
129+
130+
module.exports = {
131+
getFilters,
132+
isMessageBlured,
133+
isMessageFiltered,
134+
isMessageHidden,
135+
isChannelFiltered,
136+
addFilter,
137+
deleteFilter,
138+
}

src/core/platforms/common/cache.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
const caches = {}
3+
const cacheResult = (kind, msgId, value) => {
4+
let key = `cache-${kind}-${msgId}`
5+
caches[key] = {
6+
time: Date.now(),
7+
value,
8+
}
9+
}
10+
const invalidateCacheResult = (kind, msgId) => {
11+
let key = `cache-${kind}-${msgId}`
12+
delete caches[key]
13+
}
14+
15+
const resultFromCache = (kind, msgId, falseIfOlderThan) => {
16+
let key = `cache-${kind}-${msgId}`
17+
let currentDate = Date.now()
18+
if (caches.hasOwnProperty(key)) {
19+
let expiryDate = caches[key].time + falseIfOlderThan * 1000
20+
if (expiryDate > currentDate) {
21+
return caches[key].value
22+
}
23+
}
24+
// console.log("no cached result for", key)
25+
return false
26+
}
27+
28+
29+
const getMsgCache = (id) => {
30+
let data = sessionStorage.getItem(id)
31+
if (data) {
32+
try {
33+
return JSON.parse(data)
34+
} catch (n) {
35+
sessionStorage.removeItem(id)
36+
return false
37+
}
38+
} else {
39+
return false
40+
}
41+
}
42+
43+
const setMsgCache = (id, data) => {
44+
sessionStorage.setItem(id, JSON.stringify(data))
45+
}
46+
47+
48+
module.exports = {
49+
caches,
50+
cacheResult,
51+
resultFromCache,
52+
invalidateCacheResult,
53+
getMsgCache,
54+
setMsgCache
55+
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
const pipelines = {
3+
threadPipes: new Set(),
4+
messagePipes: new Set(),
5+
thread: {
6+
use: (func) => pipelines.threadPipes.add(func),
7+
get: () => [...pipelines.threadPipes].map((p) => p.apply(p)),
8+
},
9+
message: {
10+
use: (func) => pipelines.messagePipes.add(func),
11+
get: () => [...pipelines.messagePipes].map((p) => p.apply(p)),
12+
},
13+
}
14+
15+
module.exports = pipelines

src/core/platforms/common/queues.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const { resultFromCache, cacheResult } = require("./cache.js")
2+
3+
const workQueue = {}
4+
5+
let parallelJobs = 0
6+
7+
const maxParallelJobs = 5
8+
9+
const enqueue = (kind, msgId, falseIfOlderThan, work, cb) => {
10+
let key = `queue-${kind}-${msgId}`
11+
let possibleResult = resultFromCache(kind, msgId, falseIfOlderThan)
12+
13+
if (possibleResult) {
14+
cb(possibleResult)
15+
} else {
16+
if (workQueue.hasOwnProperty(key)) {
17+
// is in the queue, should wait for result.
18+
setTimeout(() => {
19+
enqueue(kind, msgId, falseIfOlderThan, work, cb)
20+
}, (falseIfOlderThan * 1000) / 2)
21+
} else {
22+
// not in the queue, should enqueue and block for result.
23+
workQueue[key] = { work, cb, kind, msgId }
24+
}
25+
processQueue()
26+
}
27+
}
28+
29+
const processQueue = () => {
30+
if (Object.keys(workQueue).length > 0 && parallelJobs < maxParallelJobs) {
31+
// entries in the queue.
32+
let jobs = Object.keys(workQueue).slice(0, maxParallelJobs)
33+
parallelJobs += jobs.length
34+
35+
jobs.forEach(async (job) => {
36+
let work = workQueue[job]
37+
try {
38+
// console.log("starting job...", job)
39+
let res = await work.work()
40+
cacheResult(work.kind, work.msgId, res)
41+
delete workQueue[job]
42+
work.cb(res)
43+
console.log("remaining jobs", Object.keys(workQueue).length)
44+
setTimeout(processQueue, 10)
45+
} catch (n) {
46+
console.log("work", work)
47+
console.error("error with work", n)
48+
}
49+
parallelJobs = parallelJobs >= 0 ? parallelJobs - 1 : 0
50+
})
51+
}
52+
}
53+
54+
module.exports = {
55+
enqueue,
56+
processQueue
57+
}

0 commit comments

Comments
 (0)