From 93db24ff8b6685f236e3f13031fb23602a4ada46 Mon Sep 17 00:00:00 2001 From: cel Date: Thu, 27 Apr 2017 11:18:19 -1000 Subject: Add advanced search --- lib/app.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'lib/app.js') diff --git a/lib/app.js b/lib/app.js index b64ec2a..6653c1d 100644 --- a/lib/app.js +++ b/lib/app.js @@ -99,6 +99,49 @@ App.prototype.search = function (opts) { return search(opts) } +App.prototype.advancedSearch = function (opts) { + return pull( + opts.dest ? + this.sbot.links({ + values: true, + dest: opts.dest, + source: opts.source || undefined, + reverse: true, + }) + : opts.source ? + this.sbot.createUserStream({ + reverse: true, + id: opts.source + }) + : + this.sbot.createFeedStream({ + reverse: true, + }), + opts.text && pull.filter(filterByText(opts.text)) + ) +} + +function forSome(each) { + return function some(obj) { + if (obj == null) return false + if (typeof obj === 'string') return each(obj) + if (Array.isArray(obj)) return obj.some(some) + if (typeof obj === 'object') + for (var k in obj) if (some(obj[k])) return true + return false + } +} + +function filterByText(str) { + if (!str) return function () { return true } + var search = new RegExp(str, 'i') + var matches = forSome(search.test.bind(search)) + return function (msg) { + var c = msg.value.content + return c && matches(c) + } +} + App.prototype.getMsgDecrypted = function (key, cb) { var self = this this.getMsg(key, function (err, msg) { -- cgit v1.2.3