var uri = require('urijs'); var pull = require('pull-stream'); const ipfsAPI = require('ipfs-api'); const isIPFS = require('is-ipfs') var self = module.exports = { extractLinksAndSeed: function(msg, ipfs) { if (typeof msg.value !== 'object') return; if (typeof msg.value.content !== 'object') return; uri.withinString(msg.value.content.text, (ipfsLink) => { let parts = ipfsLink.split('/'); //get multiHash parts = parts.filter((x) => isIPFS.multihash(x)); if (parts.length <= 0) { return; } parts.map((x) => { //console.log(`pin: ipfs://ipfs/${x}\tfrom:${msg.value.author}`); ipfs.pin .add(x, (err) => { if (err) { return console.error(err); } console.log(`pinned: ipfs://ipfs/${x}`); }); }); }); }, getAll: function(sbot, apiURL) { console.log("Looking for ipfs links in all feeds") const ipfs = new ipfsAPI(apiURL); pull( sbot.createLogStream({ reverse: true, limit: -1, live: true }), pull.filter((msg) => { return typeof msg.value == "object" && typeof msg.value.content == "object" && msg.value.content.type == 'post' && (typeof msg.value.content.text) == "string" && ( msg.value.content.text.indexOf("ipfs://") != -1 || msg.value.content.text.indexOf("/ipfs/") != -1 ) }), pull.drain((log) => self.extractLinksAndSeed(log, ipfs)) ) }, messagesFromPeopleIFollow: function(sbot, following, channelSubscriptions, apiURL) { const ipfs = ipfsAPI(apiURL); pull( sbot.createLogStream({ reverse: true, limit: -1, live: true }), pull.filter((msg) => { return !msg.value || ( (msg.value.author in following || msg.value.content.channel in channelSubscriptions) && msg .value.content.type == 'post' && typeof msg.value.content.text == "string" && ( msg.value.content.text.indexOf("ipfs://ipfs") != -1 || msg.value.content.text.indexOf("/ipfs/") != -1 ) ) }), pull.drain((log) => self.extractLinksAndSeed(log, ipfs)) ) }, getFromPeopleIFollow: function(sbot, apiURL) { var following = [] var channelSubscriptions = [] console.log("Looking for ipfs links in people i follow") sbot.whoami((err, feed) => { pull( sbot.createUserStream({ id: feed.id }), pull.filter((msg) => { return !msg.value || msg.value.content.type == 'contact' || (msg.value.content.type == 'channel' && typeof msg.value.content.subscribed != 'undefined') }), pull.collect(function(err, msgs) { msgs.forEach((msg) => { if (msg.value.content.type == 'contact') { if (msg.value.content.following) following[msg.value.content.contact] = 1 else delete following[msg.value.content.contact] } else // channel subscription { if (msg.value.content.subscribed) channelSubscriptions[msg.value.content.channel] = 1 else delete channelSubscriptions[msg.value.content.channel] } }) self.messagesFromPeopleIFollow(sbot, following, channelSubscriptions, apiURL) }) ) }) } }