-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Patchwork integration #54
base: master
Are you sure you want to change the base?
Changes from 43 commits
69cf20e
bcf159d
98883aa
82c9c2b
8f26469
68b2d13
dc88834
a3b0106
1d832f3
e45cf26
ddc9f58
f526c63
068db25
ed9b841
ccc662a
192cd0d
27d3018
61a7283
7ec787b
877e0a6
5ba42f3
1f3399f
6a3e3fd
45eac4f
10f7157
77ff194
ae0369f
20d6041
e564791
d6d9d7a
a59d724
2d6e381
771c7ce
f5c494e
c854420
2857197
ad55b44
b359959
f2d21ce
a3bbb62
a603a0e
6b39cfa
ece1f79
0f45ea3
5f8466e
61901a6
3d65df0
7a6b394
8bcfe87
49325a7
4ba4640
817c6e9
dd0c68c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const { isForwardRequest, errorParser } = require('ssb-dark-crystal-schema') | ||
|
||
module.exports = function (server) { | ||
return function buildForwardRequest ({ secretOwner, recp }, callback) { | ||
var content = { | ||
type: 'dark-crystal/forward-request', | ||
version: '1.0.0', | ||
secretOwner, | ||
recps: [recp, server.id] | ||
} | ||
|
||
// TODO: check that secretOwner !== recp or sever.id | ||
|
||
if (!isForwardRequest(content)) return callback(isForwardRequest.errors) | ||
|
||
callback(null, content) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const pull = require('pull-stream') | ||
const { isMsg, isFeed } = require('ssb-ref') | ||
|
||
const buildForwardRequest = require('../async/build') | ||
const publish = require('../../lib/publish-msg') | ||
|
||
module.exports = function (server) { | ||
return function publishAll ({ secretOwner, recps }, callback) { | ||
let feedIds = recps | ||
.map(recp => typeof recp === 'string' ? recp : recp.link) | ||
.filter(Boolean) | ||
.filter(isFeed) | ||
|
||
if (feedIds.length !== recps.length) return callback(new Error('forwardRequests publishAll: all recps must be valid feedIds'), recps) | ||
if (!validRecps(feedIds)) return callback(new Error('forwardRequests publishAll: all recps must be valid feedIds', feedIds)) | ||
if (!isFeed(secretOwner)) return callback(new Error('forwardRequests publishAll: invalid feedId', secretOwner)) | ||
|
||
pull( | ||
pull.values(feedIds.map(recp => ({ secretOwner, recp }))), | ||
pull.asyncMap(buildForwardRequest(server)), | ||
pull.collect((err, forwardRequests) => { | ||
if (err) return callback(err) | ||
|
||
publishAll(forwardRequests) | ||
}) | ||
) | ||
|
||
function publishAll (forwardRequests) { | ||
pull( | ||
pull.values(forwardRequests), | ||
pull.asyncMap(publish(server)), | ||
pull.collect(callback) | ||
) | ||
} | ||
} | ||
} | ||
|
||
function validRecps (recps) { | ||
return recps.every(isFeed) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const pull = require('pull-stream') | ||
const next = require('pull-next-query') | ||
const { isForwardRequest } = require('ssb-dark-crystal-schema') | ||
const { isFeedId } = require('ssb-ref') | ||
|
||
module.exports = function (server) { | ||
return function bySecretOwner (secretOwner, opts = {}) { | ||
assert(isFeedId(secretOwner), 'secretOwner must be a valid feedId') | ||
const query = [{ | ||
$filter: { | ||
value: { | ||
timestamp: { $gt: 0 }, // needed for pull-next-query to stepOn on published timestamp | ||
content: { type: 'dark-crystal/forward-request' } | ||
} | ||
} | ||
}, { | ||
$filter: { | ||
value: { | ||
content: { secretOwner } | ||
} | ||
} | ||
}] | ||
|
||
const _opts = Object.assign({}, { query, limit: 100 }, opts) | ||
|
||
return pull( | ||
next(server.query.read, _opts), | ||
pull.filter(isForwardRequest) | ||
) | ||
} | ||
} | ||
|
||
function assert (test, message) { | ||
if (!test) throw new Error(message || 'AssertionError') | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const pull = require('pull-stream') | ||
// const next = require('pull-next-query') | ||
const ShardsFromOthers = require('../../shard/pull/from-others') | ||
const forwardRequestBySecretOwner = require('./by-secret-owner') | ||
const { get } = require('lodash') | ||
|
||
module.exports = function (server) { | ||
const shardsFromOthers = ShardsFromOthers(server) | ||
|
||
return function forOwnShards (filter, opts = {}) { | ||
if ((typeof filter === 'object') && (opts === null)) { | ||
opts = filter | ||
filter = null | ||
} | ||
if (!filter) filter = () => { return true } // don't filter anything | ||
|
||
return pull( | ||
shardsFromOthers(), | ||
// pull.filter(filter), | ||
pull.unique(s => get(s, 'value.author')), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what exactly this is this doing? |
||
pull.asyncMap((shard, cb) => { | ||
pull( | ||
forwardRequestBySecretOwner(get(shard, 'value.author')), | ||
pull.collect((err, forwards) => { | ||
if (err) cb(err) | ||
cb(null, forwards) | ||
}) | ||
) | ||
}) | ||
) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const pull = require('pull-stream') | ||
const next = require('pull-next-query') | ||
const { isForwardRequest } = require('ssb-dark-crystal-schema') | ||
|
||
module.exports = function (server) { | ||
return function fromOthers (opts = {}) { | ||
const query = [{ | ||
$filter: { | ||
value: { | ||
timestamp: { $gt: 0 }, // needed for pull-next-query to stepOn on published timestamp | ||
content: { type: 'dark-crystal/forward-request' } | ||
} | ||
} | ||
}, { | ||
$filter: { | ||
value: { | ||
author: { $ne: server.id } | ||
} | ||
} | ||
}] | ||
|
||
const _opts = Object.assign({}, { query, limit: 100 }, opts) | ||
|
||
return pull( | ||
next(server.query.read, _opts), | ||
pull.filter(isForwardRequest) | ||
) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const pull = require('pull-stream') | ||
const next = require('pull-next-query') | ||
const { isForwardRequest } = require('ssb-dark-crystal-schema') | ||
|
||
module.exports = function (server) { | ||
return function fromSelf (opts = {}) { | ||
const query = [{ | ||
$filter: { | ||
value: { | ||
timestamp: { $gt: 0 }, // needed for pull-next-query to stepOn on published timestamp | ||
content: { type: 'dark-crystal/forward-request' } | ||
} | ||
} | ||
}, { | ||
$filter: { | ||
value: { | ||
author: server.id | ||
} | ||
} | ||
}] | ||
|
||
const _opts = Object.assign({}, { query, limit: 100 }, opts) | ||
|
||
return pull( | ||
next(server.query.read, _opts), | ||
pull.filter(isForwardRequest) | ||
) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,9 +4,11 @@ const get = require('lodash.get') | |
const PullShardsByRoot = require('../../shard/pull/by-root') | ||
const buildForward = require('./build') | ||
const publish = require('../../lib/publish-msg') | ||
const PullForwardRequestsSecretOwner = require('../../forward-request/pull/by-secret-owner') | ||
|
||
module.exports = function (server) { | ||
const pullShardsByRoot = PullShardsByRoot(server) | ||
const pullForwardRequestsSecretOwner = PullForwardRequestsSecretOwner(server) | ||
return function publishForward (root, recp, callback) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should be noted too that this breaks the pattern we use in the other publish functions, which expect a single |
||
if (!ref.isMsgId(root)) return callback(new Error('Invalid rootId')) | ||
pull( | ||
|
@@ -25,16 +27,26 @@ module.exports = function (server) { | |
if (get(shards[0], 'value.author') === recp) { | ||
return callback(new Error('You may not forward a shard to its author. Use reply instead.')) | ||
} | ||
|
||
const shareVersion = get(shards[0], 'value.content.version') | ||
|
||
const shard = get(shards[0], 'value.content.shard') | ||
|
||
buildForward(server)({ root, shard, shareVersion, recp }, (err, content) => { | ||
if (err) return callback(err) | ||
|
||
publish(server)(content, callback) | ||
}) | ||
pull( | ||
pullForwardRequestsSecretOwner(get(shards[0], 'value.author')), | ||
pull.collect((err, forwardRequests) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change doesn't belong here, since its being used here in patchbay-darl-crystal - https://github.com/blockades/patchbay-dark-crystal/blob/master/views/forward/new.js#L99 - where we're not dealing with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ameba23 unless this accounts for its use elsewhere? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've changed the Patchwork code to pass in only the |
||
var requestId = null | ||
if (err) return callback(err) | ||
if (forwardRequests.length > 0) { | ||
requestId = forwardRequests[0] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔥 If Bob has got a forward-request from Alice2 and Claire, both claiming to be Alice, and Claire's arrived first, we send it to Alice2 (by passing in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should instead be passing in the |
||
} | ||
|
||
const shareVersion = get(shards[0], 'value.content.version') | ||
const shardId = get(shards[0], 'key') | ||
const shard = get(shards[0], 'value.content.shard') | ||
|
||
buildForward(server)({ root, shard, shardId, requestId, shareVersion, recp }, (err, content) => { | ||
if (err) return callback(err) | ||
|
||
publish(server)(content, callback) | ||
}) | ||
}) | ||
) | ||
}) | ||
) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = function assert (test, message) { | ||
if (!test) throw new Error(message || 'AssertionError') | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const assert = require('./assert') | ||
const { isBlobId } = require('ssb-ref') | ||
|
||
module.exports = function unpackLink (link) { | ||
const index = link.indexOf('?') | ||
const blobId = link.substring(0, index) | ||
const blobKey = link.substring(index + 1) | ||
assert(((index > 0) && (blobKey.length)), 'Blob not encrypted') | ||
// TODO: test for well formed blob key | ||
assert((isBlobId(blobId)), 'attachment contains invalid blob reference') | ||
return { blobId, blobKey } | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is
forSecretOwner
rather thanbySecretOwner
, because we're looking for all requests that have been created by person A, who may or may not be person B, and as person C, its a big semantic difference.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
im happy to change it. i was imaging its short for 'indexed by secret owner', like we've got a query for shards indexed 'by root'. but if you are seeing it as 'forward requests authored by secret owner' then thats not what we want.