Files: 1115c71b553c0e514a8b186b8bdd848b71644c16 / lib.js
3610 bytesRaw
1 | var uri = require('urijs'); |
2 | var pull = require('pull-stream'); |
3 | const ipfsAPI = require('ipfs-api'); |
4 | const isIPFS = require('is-ipfs') |
5 | |
6 | var self = module.exports = { |
7 | extractLinksAndSeed: function(msg, ipfs) { |
8 | if (typeof msg.value !== 'object') return; |
9 | if (typeof msg.value.content !== 'object') return; |
10 | uri.withinString(msg.value.content.text, (ipfsLink) => { |
11 | let parts = ipfsLink.split('/'); |
12 | //get multiHash |
13 | parts = parts.filter((x) => isIPFS.multihash(x)); |
14 | if (parts.length <= 0) { |
15 | return; |
16 | } |
17 | parts.map((x) => { |
18 | //console.log(`pin: ipfs://ipfs/${x}\tfrom:${msg.value.author}`); |
19 | ipfs.pin |
20 | .add(x, (err) => { |
21 | if (err) { |
22 | return console.error(err); |
23 | } |
24 | console.log(`pinned: ipfs://ipfs/${x}`); |
25 | }); |
26 | }); |
27 | }); |
28 | }, |
29 | getAll: function(sbot, apiURL) { |
30 | console.log("Looking for ipfs links in all feeds") |
31 | const ipfs = new ipfsAPI(apiURL); |
32 | pull( |
33 | sbot.createLogStream({ |
34 | reverse: true, |
35 | limit: -1, |
36 | live: true |
37 | }), |
38 | pull.filter((msg) => { |
39 | return typeof msg.value == "object" && typeof msg.value.content == |
40 | "object" && |
41 | msg.value.content.type == 'post' && |
42 | (typeof msg.value.content.text) == "string" && |
43 | ( |
44 | msg.value.content.text.indexOf("ipfs://") != -1 || |
45 | msg.value.content.text.indexOf("/ipfs/") != -1 |
46 | ) |
47 | }), |
48 | pull.drain((log) => self.extractLinksAndSeed(log, ipfs)) |
49 | ) |
50 | }, |
51 | |
52 | messagesFromPeopleIFollow: function(sbot, following, |
53 | channelSubscriptions, apiURL) { |
54 | const ipfs = ipfsAPI(apiURL); |
55 | pull( |
56 | sbot.createLogStream({ |
57 | reverse: true, |
58 | limit: -1, |
59 | live: true |
60 | }), |
61 | pull.filter((msg) => { |
62 | return !msg.value || |
63 | ( |
64 | (msg.value.author in following || |
65 | msg.value.content.channel in channelSubscriptions) && msg |
66 | .value.content.type == 'post' && |
67 | typeof msg.value.content.text == "string" && |
68 | ( |
69 | msg.value.content.text.indexOf("ipfs://ipfs") != -1 || |
70 | msg.value.content.text.indexOf("/ipfs/") != -1 |
71 | ) |
72 | ) |
73 | }), |
74 | pull.drain((log) => self.extractLinksAndSeed(log, ipfs)) |
75 | ) |
76 | }, |
77 | |
78 | getFromPeopleIFollow: function(sbot, apiURL) { |
79 | var following = [] |
80 | var channelSubscriptions = [] |
81 | console.log("Looking for ipfs links in people i follow") |
82 | sbot.whoami((err, feed) => { |
83 | pull( |
84 | sbot.createUserStream({ |
85 | id: feed.id |
86 | }), |
87 | pull.filter((msg) => { |
88 | return !msg.value || |
89 | msg.value.content.type == 'contact' || |
90 | (msg.value.content.type == 'channel' && |
91 | typeof msg.value.content.subscribed != 'undefined') |
92 | }), |
93 | pull.collect(function(err, msgs) { |
94 | msgs.forEach((msg) => { |
95 | if (msg.value.content.type == 'contact') { |
96 | if (msg.value.content.following) |
97 | following[msg.value.content.contact] = 1 |
98 | else |
99 | delete following[msg.value.content.contact] |
100 | } else // channel subscription |
101 | { |
102 | if (msg.value.content.subscribed) |
103 | channelSubscriptions[msg.value.content.channel] = |
104 | 1 |
105 | else |
106 | delete channelSubscriptions[msg.value.content.channel] |
107 | } |
108 | }) |
109 | self.messagesFromPeopleIFollow(sbot, following, |
110 | channelSubscriptions, apiURL) |
111 | }) |
112 | ) |
113 | }) |
114 | } |
115 | } |
116 |
Built with git-ssb-web