git ssb

2+

arj / dat-share



Commit 842b574ea8edb049a6403e9b4434d95823a39caa

Add option to only seed for people/channels I follow

Anders Rune Jensen committed on 9/19/2017, 1:18:08 PM
Parent: 1b9093405deadf26da6cab9aefb5cdf7ec500421

Files changed

index.jschanged
index.jsView
@@ -6,43 +6,109 @@
66 var pull = require('pull-stream')
77
88 program
99 .option('-f, --folder [value]', 'Folder for sharing')
10 + .option('-i, --only-people-i-follow', 'Only seed urls from people or channels I follow')
1011 .parse(process.argv);
1112
12-require('ssb-client')((err, sbot) => {
13 +function extractLinksAndSeed(err, logs) {
1314 if (err) throw err;
1415
15- console.log("Looking for dat links")
16 + console.log("Found " + logs.length)
1617
18 + logs.forEach(msg => {
19 + uri.withinString(msg.value.content.text, (datLink) => {
20 + if (!datLink.startsWith("dat://")) return
21 +
22 + console.log("Saving to:", program.folder + "/" + datLink.substring(6))
23 +
24 + Dat(program.folder + "/" + datLink.substring(6), {
25 + key: datLink
26 + }, function (err, dat) {
27 + if (err) throw err
28 +
29 + console.log("sharing:", datLink)
30 + dat.joinNetwork()
31 + })
32 + })
33 + })
34 +}
35 +
36 +function getAll(sbot) {
37 + console.log("Looking for dat links in all feeds")
38 +
1739 pull(
1840 sbot.createLogStream({ reverse: true, limit: 10000 }),
1941 pull.filter((msg) => {
2042 return !msg.value ||
2143 msg.value.content.type == 'post' &&
2244 typeof msg.value.content.text == "string" &&
2345 msg.value.content.text.indexOf("dat://") != -1
2446 }),
25- pull.collect((err, log) => {
26- if (err) throw err;
47 + pull.collect(extractLinksAndSeed)
48 + )
49 +}
2750
28- console.log("Found " + log.length)
51 +function messagesFromPeopleIFollow(sbot, following, channelSubscriptions) {
52 + console.log("users:", following)
53 + console.log("channels:", channelSubscriptions)
54 + pull(
55 + sbot.createLogStream({ reverse: true, limit: 10000 }),
56 + pull.filter((msg) => {
57 + return !msg.value ||
58 + ((msg.value.author in following ||
59 + msg.value.content.channel in channelSubscriptions)
60 + && msg.value.content.type == 'post' &&
61 + typeof msg.value.content.text == "string" &&
62 + msg.value.content.text.indexOf("dat://") != -1)
63 + }),
64 + pull.collect(extractLinksAndSeed)
65 + )
66 +}
2967
30- log.forEach(msg => {
31- uri.withinString(msg.value.content.text, (datLink) => {
32- if (!datLink.startsWith("dat://")) return
68 +function getFromPeopleIFollow(sbot) {
69 + var following = []
70 + var channelSubscriptions = []
3371
34- console.log("Saving to:", program.folder + "/" + datLink.substring(6))
72 + console.log("Looking for dat links in people i follow")
3573
36- Dat(program.folder + "/" + datLink.substring(6), {
37- key: datLink
38- }, function (err, dat) {
39- if (err) throw err
40-
41- console.log("sharing:", datLink)
42- dat.joinNetwork()
43- })
44- })
74 + sbot.whoami((err, feed) => {
75 + pull(
76 + sbot.createUserStream({ id: feed.id }),
77 + pull.filter((msg) => {
78 + return !msg.value ||
79 + msg.value.content.type == 'contact' ||
80 + (msg.value.content.type == 'channel' &&
81 + typeof msg.value.content.subscribed != 'undefined')
82 + }),
83 + pull.collect(function (err, msgs) {
84 + msgs.forEach((msg) => {
85 + if (msg.value.content.type == 'contact')
86 + {
87 + if (msg.value.content.following)
88 + following[msg.value.content.contact] = 1
89 + else
90 + delete following[msg.value.content.contact]
91 + }
92 + else // channel subscription
93 + {
94 + if (msg.value.content.subscribed)
95 + channelSubscriptions[msg.value.content.channel] = 1
96 + else
97 + delete channelSubscriptions[msg.value.content.channel]
98 + }
99 + })
100 +
101 + messagesFromPeopleIFollow(sbot, following, channelSubscriptions)
45102 })
46- })
47- )
103 + )
104 + })
105 +}
106 +
107 +require('ssb-client')((err, sbot) => {
108 + if (err) throw err;
109 +
110 + if (program.onlyPeopleIFollow)
111 + getFromPeopleIFollow(sbot)
112 + else
113 + getAll(sbot)
48114 })

Built with git-ssb-web