You are reading content from Scuttlebutt
@farewellutopia %ld3BapFVbbeQQA5dfzIlwqmkVlm/TOua4En32oLJoC4=.sha256

I was wondering if there is a method combining createUserStream and server.query.read? I want to retrieve particular messages by the current user and it feels somehow awkward to first request the FeedID with whoami just to send it back immediately as part of the query.

Is #ssb-server the right channel to ask API questions?

@cel-desktop %HrVCz6QbFAUEUJ2JgHiiatLYVOyUlxQFSKTZETchsK0=.sha256

@farewellutopia
What query are you trying to make? To get messages by the current user, with either createUserStream or server.query.read you will have to specify the user id. You can make a whoami call at the start of your application and keep the id in memory. Or you can use the sbot.id value which is the id of the ssb-client's key (usually the same as the server's id but may be different if you are using a different master key to connect, or possibly connecting via noauth socket.

@farewellutopia %OoP0fXRT1ZbYbtpWRPZXoPQFPAhg+tqjnxpiGm0XoFE=.sha256

Thanks, @cel-desktop . I didn't realize that createUserStream needs the user id as well. I'm trying to get the last vote the current user submitted for a particular message. As the whoami+caching approach seems to be the more generic, I'll use that.

@cel-desktop %SMAwXhY60dP+beXKWiQPEPtjePETtdLlwu7+kiKU+yM=.sha256

@farewellutopia
Here is an example function to get the latest vote by a user for a target id.
This will use the most efficient RPC API if available, and fall back to others. (TODO: support go-sbot [%7QnBSs0...]) For the sake of simplicity, it uses pull.filter/take/map in all cases, even though that could be omitted or simplified if using the ssb-query or ssb-backlinks method. Public domain.

// Get latest vote by feed for target.
// Calls back:
//   > 0 (usually 1): upvote
//   0: unvote (retracted/reset vote)
//   null: no vote
//   < 0 (usually -1): downvote (rare)
//   other: invalid/unknown vote
// Note: vote expression is omitted
function getVote(sbot, feed, target, cb) {
  pull(
    // most efficient: use ssb-backlinks, returning one message
    sbot.backlinks ? sbot.backlinks.read({
      reverse: true,
      limit: 1,
      query: [{$filter: {
        dest: target,
        value: {
          author: feed,
          content: {
            type: 'vote',
            vote: {
              link: target
            }
          }
        }
      }}]
    }) :
    // sbot.links: returns all the user's votes for the target.
    // usually the user has few votes, so this is okay.
    sbot.links ? sbot.links({
      source: feed,
      dest: target,
      rel: 'vote',
      values: true,
      reverse: true,
    }) :
    // ssb-query: scan through user's vote messages
    sbot.query ? sbot.query.read({
      reverse: true,
      limit: 1,
      query: [{$filter: {
        value: {
          author: feed,
          content: {
            type: 'vote',
            vote: {
              link: target
            }
          }
        }
      }}]
    }) :
    // scan through user's feed
    sbot.createUserStream ? sbot.createUserStream({
      id: feed,
      reverse: true
    }) : pull.error('Unable to get votes'),
    // verify vote message schema
    // needed for use with sbot.links and createUserStream above
    pull.filter(function (msg) {
      var c = msg && msg.value && msg.value.content
      return c && c.type === 'vote'
        && c.vote && c.vote.link === target
    }),
    pull.take(1),
    pull.map(function (msg) {
      var c = msg && msg.value && msg.value.content
      return c && c.vote && c.vote.value
    }),
    pull.collect(function (err, msgs) {
      if (err) return cb(err)
      if (!msgs.length) return cb(null, null)
      cb(null, msgs[0])
    })
  )
}
@farewellutopia %oIKq/B9kpQ10ERVng+RbSqSii9ZobFX9O/qFWVv2DNY=.sha256

Thanks @cel-desktop for this very helpful example! It helps me learn about the sbot ass well as the pull-stream API.

The client often fails to connect to the server (Error: could not connect to sbot), on the server I see this log:

server error, from net:127.0.0.1:47778~shs:
Error: stream ended with:0 but wanted:64
    at drain (/usr/local/lib/node_modules/ssb-server/node_modules/pull-reader/index.js:43:26)
    at /usr/local/lib/node_modules/ssb-server/node_modules/pull-reader/index.js:63:18
    at /usr/local/lib/node_modules/ssb-server/node_modules/pull-reader/index.js:20:7
    at drain (/usr/local/lib/node_modules/ssb-server/node_modules/stream-to-pull-stream/index.js:126:18)
    at Socket.<anonymous> (/usr/local/lib/node_modules/ssb-server/node_modules/stream-to-pull-stream/index.js:147:5)
    at Socket.emit (events.js:228:7)
    at TCP.<anonymous> (net.js:664:12)

I understand that I'm connecting unnecessarily often and that I could instead just connect once and keep a reference to the server. I'm still curious about what exactly is causing this error.

@cel-desktop %v0Djsw1fpsQ7wVmxCIXGCkc2gKAJsvSGgtbEkfSSHPY=.sha256

@farewellutopia
You're welcome.

If that error is accompanied with a few seconds delay on the client side before the connection fails, it could be that the server is busy/overloaded and doesn't respond to the connection request in time. (Or the client could be busy/overloaded, but that is less common).

Join Scuttlebutt now