Skip to content

[WIP] Patchql #339

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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions about/obs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const { Value } = require('mutant')
const nest = require('depnest')
const { isFeed } = require('ssb-ref')
const gql = require('graphql-tag').default
// var colorHash = new (require('color-hash'))()
// var fallbackImageUrl = 'data:image/gif;base64,R0lGODlhAQABAPAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='

exports.needs = nest({
'about.sync.shortFeedId': 'first',
'blob.sync.url': 'first',
'graphql.async.query': 'first'
})

exports.gives = nest({
'about.obs.name': true,
'about.obs.imageUrl': true
})

const nameQuery = gql`
query author($feedId: String!) {
author(id: $feedId) {
name
}
}
`

exports.create = function (api) {
var nameCache = {}

return nest({
'about.obs.name': name,
'about.obs.imageUrl': imageUrl
})

// TODO !!!
// add something which watches new about messages coming in and updates any feedId's referenced

function name (id) {
if (!isFeed(id)) throw new Error('about.obs.name requires a feedId, got', id)

if (nameCache[id]) return nameCache[id]

nameCache[id] = Value(api.about.sync.shortFeedId(id))
fetchCurrentName(id, (err, res) => {
if (err) return console.error(JSON.stringify(err, null, 2))

nameCache[id].set(res.data.author.name)
})

return nameCache[id]
}

function imageUrl () {
// returns nothing, so will fall back to original
}

function fetchCurrentName (feedId, cb) {
api.graphql.async.query({ query: nameQuery, variables: { feedId } }, cb)
}
}
11 changes: 11 additions & 0 deletions app/styles/css/open-dyslexic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const nest = require('depnest')
const requireStyle = require('require-style')
const { assign } = Object

exports.gives = nest('styles.css')

exports.create = function (api) {
return nest('styles.css', (sofar = {}) => {
return assign(sofar, { openDyslexic: requireStyle('open-dyslexic') })
})
}
79 changes: 79 additions & 0 deletions app/sync/initialise/patchql.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const pull = require('pull-stream')
const nest = require('depnest')
const { ApolloClient } = require('apollo-client')
const { InMemoryCache } = require('apollo-cache-inmemory')
const { createHttpLink } = require('apollo-link-http')
const gql = require('graphql-tag').default
// NOTE also depends on graphql module

exports.needs = nest({
'sbot.async.run': 'first'
})
exports.gives = nest({
'app.sync.initialise': true,
'graphql.async.query': true
})

const mutation = gql`
mutation process($chunkSize: Int) {
process(chunkSize: $chunkSize) {
chunkSize,
latestSequence
}
}
`
exports.create = function (api) {
var graphql

return nest({
'app.sync.initialise': startPatchql,
'graphql.async.query': graphQuery
})

function startPatchql () {
if (process.env.PATCHQL === 'false') return

graphql = GraphqlClient()

var latestSequence
var nextSequence

api.sbot.async.run(server => {
server.jsbotPatchql.start({}, () => {
indexLoop()
})
})

function indexLoop () {
graphql.mutate({ mutation, variables: { chunkSize: 10e3 } })
.then(res => {
nextSequence = res.data.process.latestSequence
if (latestSequence === nextSequence) setTimeout(indexLoop, 5e3)
else {
latestSequence = nextSequence
console.log('patchql latestSequence:', latestSequence)
indexLoop()
}
})
.catch(err => {
console.error(err)
return setTimeout(indexLoop, 2e3)
})
}
}

function graphQuery ({ query, variables }, cb) {
if (!graphql) return setTimeout(() => graphQuery({ query, variables }, cb), 1e3)

graphql.query({ query, variables })
.then(res => cb(null, res))
.catch(err => cb(err))
}

function GraphqlClient () {
return new ApolloClient({
link: createHttpLink({ uri: 'http://localhost:8080/graphql' }), // set by jsbot-patchql
cache: new InMemoryCache()
})
}
}
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ const plugins = [
'ssb-search',
'ssb-suggest',
'ssb-tangle',
'ssb-unread'
'ssb-unread',

'jsbot-patchql'
]

ahoy(
Expand Down
Loading