From e2373db508a28c7884f18d8c565ae08fb7222055 Mon Sep 17 00:00:00 2001 From: ccd0 Date: Wed, 17 Jul 2019 10:25:59 -0700 Subject: [PATCH] Multifile support in Filter. #2171 --- src/Filtering/Filter.coffee | 96 ++++++++++++++++++++----------------- src/General/Index.coffee | 2 +- src/General/Test.coffee | 6 +-- src/Menu/ArchiveLink.coffee | 2 +- 4 files changed, 56 insertions(+), 50 deletions(-) diff --git a/src/Filtering/Filter.coffee b/src/Filtering/Filter.coffee index e4af3055a..0075c907e 100644 --- a/src/Filtering/Filter.coffee +++ b/src/Filtering/Filter.coffee @@ -127,25 +127,25 @@ Filter = mask = (mask | (if post.file then 4 else 8)) board = "#{post.siteID}/#{post.boardID}" site = "#{post.siteID}/*" - for key of Filter.filters when ((value = Filter.value key, post)?) - # Continue if there's nothing to filter (no tripcode for example). - for filter in Filter.filters[key] - continue if ( - (filter.boards and !(filter.boards[board] or filter.boards[site] )) or - (filter.excludes and (filter.excludes[board] or filter.excludes[site])) or - (filter.mask & mask) or - (if filter.isstring then (filter.regexp isnt value) else !filter.regexp.test(value)) - ) - if filter.hide - if hideable - hide = true - stub and= filter.stub - else - unless hl and filter.hl in hl - (hl or= []).push filter.hl - top or= filter.top - if filter.noti - noti = true + for key of Filter.filters + for value in Filter.values(key, post) + for filter in Filter.filters[key] + continue if ( + (filter.boards and !(filter.boards[board] or filter.boards[site] )) or + (filter.excludes and (filter.excludes[board] or filter.excludes[site])) or + (filter.mask & mask) or + (if filter.isstring then (filter.regexp isnt value) else !filter.regexp.test(value)) + ) + if filter.hide + if hideable + hide = true + stub and= filter.stub + else + unless hl and filter.hl in hl + (hl or= []).push filter.hl + top or= filter.top + if filter.noti + noti = true if hide {hide, stub} else @@ -170,26 +170,31 @@ Filter = !!Filter.test(post).hide valueF: - postID: (post) -> "#{post.ID}" - name: (post) -> post.info.name - uniqueID: (post) -> post.info.uniqueID or '' - tripcode: (post) -> post.info.tripcode - capcode: (post) -> post.info.capcode - pass: (post) -> post.info.pass - email: (post) -> post.info.email - subject: (post) -> post.info.subject or (if post.isReply then undefined else '') - comment: (post) -> (post.info.comment ?= g.sites[post.siteID]?.Build?.parseComment?(post.info.commentHTML.innerHTML)) - flag: (post) -> post.info.flag - filename: (post) -> post.file?.name - dimensions: (post) -> post.file?.dimensions - filesize: (post) -> post.file?.size - MD5: (post) -> post.file?.MD5 + postID: (post) -> ["#{post.ID}"] + name: (post) -> [post.info.name] + uniqueID: (post) -> [post.info.uniqueID or ''] + tripcode: (post) -> [post.info.tripcode] + capcode: (post) -> [post.info.capcode] + pass: (post) -> [post.info.pass] + email: (post) -> [post.info.email] + subject: (post) -> [post.info.subject or (if post.isReply then undefined else '')] + comment: (post) -> [(post.info.comment ?= g.sites[post.siteID]?.Build?.parseComment?(post.info.commentHTML.innerHTML))] + flag: (post) -> [post.info.flag] + filename: (post) -> post.files.map((f) -> f.name) + dimensions: (post) -> post.files.map((f) -> f.dimensions) + filesize: (post) -> post.files.map((f) -> f.size) + MD5: (post) -> post.files.map((f) -> f.MD5) - value: (key, post) -> + values: (key, post) -> if key of Filter.valueF - Filter.valueF[key](post) + Filter.valueF[key](post).filter((v) -> v?) else - key.split('+').map((k) -> Filter.valueF[k]?(post) or '').join('\n') + [key.split('+').map((k) -> + if (f=Filter.valueF[k]) + f(post).map((v) -> v or '').join('\n') + else + '' + ).join('\n')] addFilter: (type, re, cb) -> $.get type, Conf[type], (item) -> @@ -286,21 +291,22 @@ Filter = return { el: el open: (post) -> - value = Filter.value type, post - value? + Filter.values(type, post).length } makeFilter: -> {type} = @dataset # Convert value -> regexp, unless type is MD5 - value = Filter.value type, Filter.menu.post - re = if type in ['uniqueID', 'MD5'] then value else Filter.escape(value) - re = if type in ['uniqueID', 'MD5'] - "/#{re}/" - else - "/^#{re}$/" + values = Filter.values type, Filter.menu.post + res = values.map((value) -> + re = if type in ['uniqueID', 'MD5'] then value else Filter.escape(value) + if type in ['uniqueID', 'MD5'] + "/#{re}/" + else + "/^#{re}$/" + ).join('\n') - Filter.addFilter type, re, -> + Filter.addFilter type, res, -> # Open the settings and display & focus the relevant filter textarea. Settings.open 'Filter' section = $ '.section-container' diff --git a/src/General/Index.coffee b/src/General/Index.coffee index 6e1c7a54e..4399a841e 100644 --- a/src/General/Index.coffee +++ b/src/General/Index.coffee @@ -944,7 +944,7 @@ Index = catch return [] return Index.sortedThreadIDs.filter (ID) -> - regexp.test(Filter.value(match[1], Index.parsedThreads[ID]) or '') + regexp.test(Filter.values(match[1], Index.parsedThreads[ID]).join('\n')) return if not (keywords = query.toLowerCase().match /\S+/g) Index.sortedThreadIDs.filter (ID) -> Index.searchMatch Index.parsedThreads[ID], keywords diff --git a/src/General/Test.coffee b/src/General/Test.coffee index d62d94b20..a60c14f3a 100644 --- a/src/General/Test.coffee +++ b/src/General/Test.coffee @@ -91,9 +91,9 @@ Test = c.log y.outerHTML for key of Config.filter when not key is 'General' and not (key is 'MD5' and post.board.ID is 'f') - val1 = Filter.value key, obj - val2 = Filter.value key, post2 - if val1 isnt val2 + val1 = Filter.values key, obj + val2 = Filter.values key, post2 + unless val1.length is val2.length and val1.every((x, i) -> x is val2[i]) fail = true c.log "#{post.fullID} has filter bug in #{key}" c.log val1 diff --git a/src/Menu/ArchiveLink.coffee b/src/Menu/ArchiveLink.coffee index e45080ca7..393e08836 100644 --- a/src/Menu/ArchiveLink.coffee +++ b/src/Menu/ArchiveLink.coffee @@ -45,7 +45,7 @@ ArchiveLink = value = if type is 'country' post.info.flagCode or post.info.flagCodeTroll else - Filter.value type, post + Filter.values(type, post)[0] # We want to parse the exact same stuff as the filter does already. return false unless value el.href = Redirect.to 'search',