|
| 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 | +} |
0 commit comments