git ssb

0+

clemo / ssb-ipfs-share



forked from arj / dat-share

Tree: 8d17a41209cdd6c812ab9a2b84b33e948725194a

Files: 8d17a41209cdd6c812ab9a2b84b33e948725194a / lib.js

3661 bytesRaw
1var Dat = require('dat-node')
2var uri = require('urijs')
3var pull = require('pull-stream')
4
5var self = module.exports = {
6 extractLinksAndSeed: function(err, logs, shareFolder, useTemp) {
7 if (err) throw err;
8
9 console.log("Found " + logs.length)
10
11 logs.forEach(msg => {
12 uri.withinString(msg.value.content.text, (datLink) => {
13 if (!datLink.startsWith("dat://")) return
14
15 // issue #360 in uri.js
16 var bracket = datLink.indexOf("]")
17
18 if (bracket != -1)
19 datLink = datLink.substring(0, bracket)
20
21 if (!useTemp)
22 console.log("Saving to:", shareFolder + "/" + datLink.substring(6))
23
24 Dat(shareFolder + "/" + datLink.substring(6), {
25 key: datLink,
26 temp: useTemp
27 }, function (err, dat) {
28 if (err) {
29 console.log(err);
30 return
31 }
32
33 console.log("sharing:", datLink)
34 dat.joinNetwork()
35 })
36 })
37 })
38 },
39
40 getAll: function(sbot, shareFolder, useTemp) {
41 console.log("Looking for dat links in all feeds")
42
43 pull(
44 sbot.createLogStream({ reverse: true, limit: 15000 }),
45 pull.filter((msg) => {
46 return !msg.value ||
47 msg.value.content.type == 'post' &&
48 typeof msg.value.content.text == "string" &&
49 msg.value.content.text.indexOf("dat://") != -1
50 }),
51 pull.collect((err, logs) => self.extractLinksAndSeed(err, logs,
52 shareFolder,
53 useTemp))
54 )
55 },
56
57 messagesFromPeopleIFollow: function(sbot, following,
58 channelSubscriptions,
59 shareFolder, useTemp) {
60 console.log("users:", following)
61 console.log("channels:", channelSubscriptions)
62 pull(
63 sbot.createLogStream({ reverse: true, limit: 15000 }),
64 pull.filter((msg) => {
65 return !msg.value ||
66 ((msg.value.author in following ||
67 msg.value.content.channel in channelSubscriptions)
68 && msg.value.content.type == 'post' &&
69 typeof msg.value.content.text == "string" &&
70 msg.value.content.text.indexOf("dat://") != -1)
71 }),
72 pull.collect((err, logs) => self.extractLinksAndSeed(err, logs,
73 shareFolder,
74 useTemp))
75 )
76 },
77
78 getFromPeopleIFollow: function(sbot, shareFolder, useTemp) {
79 var following = []
80 var channelSubscriptions = []
81
82 console.log("Looking for dat links in people i follow")
83
84 sbot.whoami((err, feed) => {
85 pull(
86 sbot.createUserStream({ id: feed.id }),
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 {
97 if (msg.value.content.following)
98 following[msg.value.content.contact] = 1
99 else
100 delete following[msg.value.content.contact]
101 }
102 else // channel subscription
103 {
104 if (msg.value.content.subscribed)
105 channelSubscriptions[msg.value.content.channel] = 1
106 else
107 delete channelSubscriptions[msg.value.content.channel]
108 }
109 })
110
111 self.messagesFromPeopleIFollow(sbot, following,
112 channelSubscriptions,
113 shareFolder, useTemp)
114 })
115 )
116 })
117 }
118}
119

Built with git-ssb-web