From bc2b9ea929d7f9530e8a9fbc3e2673fbffa55d99 Mon Sep 17 00:00:00 2001 From: Christian Bundy Date: Wed, 8 Jan 2020 13:10:49 -0800 Subject: [PATCH] Change models to follow mediator pattern --- src/controller/index.js | 14 +- src/controller/models/about.js | 77 ------- src/controller/models/blob.js | 19 -- src/controller/models/{lib => }/configure.js | 0 src/controller/models/{lib => }/cooler.js | 0 src/controller/models/friend.js | 53 ----- src/controller/models/{post.js => index.js} | 210 ++++++++++++++++++- src/controller/models/{lib => }/markdown.js | 0 src/controller/models/meta.js | 40 ---- src/controller/models/{lib => }/server.js | 0 src/controller/models/vote.js | 20 -- 11 files changed, 208 insertions(+), 225 deletions(-) delete mode 100644 src/controller/models/about.js delete mode 100644 src/controller/models/blob.js rename src/controller/models/{lib => }/configure.js (100%) rename src/controller/models/{lib => }/cooler.js (100%) delete mode 100644 src/controller/models/friend.js rename src/controller/models/{post.js => index.js} (83%) rename src/controller/models/{lib => }/markdown.js (100%) delete mode 100644 src/controller/models/meta.js rename src/controller/models/{lib => }/server.js (100%) delete mode 100644 src/controller/models/vote.js diff --git a/src/controller/index.js b/src/controller/index.js index 994c9f8..6d2a19f 100644 --- a/src/controller/index.js +++ b/src/controller/index.js @@ -7,12 +7,14 @@ const debug = require('debug')('oasis') const ssbMentions = require('ssb-mentions') const ssbRef = require('ssb-ref') -const about = require('./models/about') -const blob = require('./models/blob') -const friend = require('./models/friend') -const meta = require('./models/meta') -const post = require('./models/post') -const vote = require('./models/vote') +const { + about, + blob, + friend, + meta, + post, + vote +} = require('./models') const { authorView, diff --git a/src/controller/models/about.js b/src/controller/models/about.js deleted file mode 100644 index 30cde71..0000000 --- a/src/controller/models/about.js +++ /dev/null @@ -1,77 +0,0 @@ -'use strict' - -const cooler = require('./lib/cooler') -const markdown = require('./lib/markdown') -const pull = require('pull-stream') - -const nullImage = `&${'0'.repeat(43)}=.sha256` - -module.exports = { - name: async (feedId) => { - const ssb = await cooler.connect() - return cooler.get( - ssb.about.socialValue, { - key: 'name', - dest: feedId - } - ) - }, - image: async (feedId) => { - const ssb = await cooler.connect() - const raw = await cooler.get( - ssb.about.socialValue, { - key: 'image', - dest: feedId - } - ) - - if (raw == null || raw.link == null) { - return nullImage - } if (typeof raw.link === 'string') { - return raw.link - } - return raw - }, - description: async (feedId) => { - const ssb = await cooler.connect() - const raw = await cooler.get( - ssb.about.socialValue, { - key: 'description', - dest: feedId - } - ) - return markdown(raw) - }, - all: async (feedId) => { - const ssb = await cooler.connect() - const raw = await cooler.read( - ssb.about.read, { - dest: feedId - } - ) - - return new Promise((resolve, reject) => - pull( - raw, - pull.filter((message) => message.value.author === feedId), - pull.reduce((acc, cur) => { - const metaKeys = ['type', 'about'] - - Object.entries(cur.value.content).filter(([key]) => - metaKeys.includes(key) === false - ).forEach(([key, value]) => { - acc[key] = value - }) - - return acc - }, {}, (err, val) => { - if (err) { - reject(err) - } else { - resolve(val) - } - }) - ) - ) - } -} diff --git a/src/controller/models/blob.js b/src/controller/models/blob.js deleted file mode 100644 index 18b8d0c..0000000 --- a/src/controller/models/blob.js +++ /dev/null @@ -1,19 +0,0 @@ -'use strict' - -const debug = require('debug')('oasis') -const cooler = require('./lib/cooler') - -module.exports = { - get: async ({ blobId }) => { - debug('get blob: %s', blobId) - const ssb = await cooler.connect() - return cooler.read(ssb.blobs.get, blobId) - }, - want: async ({ blobId }) => { - debug('want blob: %s', blobId) - const ssb = await cooler.connect() - - // This does not wait for the blob. - cooler.get(ssb.blobs.want, blobId) - } -} diff --git a/src/controller/models/lib/configure.js b/src/controller/models/configure.js similarity index 100% rename from src/controller/models/lib/configure.js rename to src/controller/models/configure.js diff --git a/src/controller/models/lib/cooler.js b/src/controller/models/cooler.js similarity index 100% rename from src/controller/models/lib/cooler.js rename to src/controller/models/cooler.js diff --git a/src/controller/models/friend.js b/src/controller/models/friend.js deleted file mode 100644 index fbdfa5e..0000000 --- a/src/controller/models/friend.js +++ /dev/null @@ -1,53 +0,0 @@ -'use strict' - -const cooler = require('./lib/cooler') - -module.exports = { - isFollowing: async (feedId) => { - const ssb = await cooler.connect() - const { id } = ssb - - const isFollowing = await cooler.get( - ssb.friends.isFollowing, - { - source: id, - dest: feedId - } - ) - return isFollowing - }, - getRelationship: async (feedId) => { - const ssb = await cooler.connect() - const { id } = ssb - - if (feedId === id) { - return 'this is you' - } - - const isFollowing = await cooler.get( - ssb.friends.isFollowing, - { - source: id, - dest: feedId - } - ) - - const isBlocking = await cooler.get( - ssb.friends.isBlocking, - { - source: id, - dest: feedId - } - ) - - if (isFollowing === true && isBlocking === false) { - return 'you are following' - } else if (isFollowing === false && isBlocking === true) { - return 'you are blocking' - } else if (isFollowing === false && isBlocking === false) { - return 'you are not following or blocking' - } else { - return 'you are following and blocking (!)' - } - } -} diff --git a/src/controller/models/post.js b/src/controller/models/index.js similarity index 83% rename from src/controller/models/post.js rename to src/controller/models/index.js index 955263c..6b126ce 100644 --- a/src/controller/models/post.js +++ b/src/controller/models/index.js @@ -1,16 +1,194 @@ 'use strict' -const debug = require('debug')('oasis:model-post') +const debug = require('debug')('oasis') +const { isRoot, isReply } = require('ssb-thread-schema') const lodash = require('lodash') const prettyMs = require('pretty-ms') -const pull = require('pull-stream') const pullParallelMap = require('pull-paramap') +const pull = require('pull-stream') const pullSort = require('pull-sort') -const { isRoot, isReply } = require('ssb-thread-schema') // HACK: https://github.com/ssbc/ssb-thread-schema/issues/4 const isNestedReply = require('ssb-thread-schema/post/nested-reply/validator') +const configure = require('./configure') +const cooler = require('./cooler') +const markdown = require('./markdown') + +const nullImage = `&${'0'.repeat(43)}=.sha256` + +exports.about = { + + name: async (feedId) => { + const ssb = await cooler.connect() + return cooler.get( + ssb.about.socialValue, { + key: 'name', + dest: feedId + } + ) + }, + image: async (feedId) => { + const ssb = await cooler.connect() + const raw = await cooler.get( + ssb.about.socialValue, { + key: 'image', + dest: feedId + } + ) + + if (raw == null || raw.link == null) { + return nullImage + } if (typeof raw.link === 'string') { + return raw.link + } + return raw + }, + description: async (feedId) => { + const ssb = await cooler.connect() + const raw = await cooler.get( + ssb.about.socialValue, { + key: 'description', + dest: feedId + } + ) + return markdown(raw) + }, + all: async (feedId) => { + const ssb = await cooler.connect() + const raw = await cooler.read( + ssb.about.read, { + dest: feedId + } + ) + + return new Promise((resolve, reject) => + pull( + raw, + pull.filter((message) => message.value.author === feedId), + pull.reduce((acc, cur) => { + const metaKeys = ['type', 'about'] + + Object.entries(cur.value.content).filter(([key]) => + metaKeys.includes(key) === false + ).forEach(([key, value]) => { + acc[key] = value + }) + + return acc + }, {}, (err, val) => { + if (err) { + reject(err) + } else { + resolve(val) + } + }) + ) + ) + } +} + +exports.blob = { + get: async ({ blobId }) => { + debug('get blob: %s', blobId) + const ssb = await cooler.connect() + return cooler.read(ssb.blobs.get, blobId) + }, + want: async ({ blobId }) => { + debug('want blob: %s', blobId) + const ssb = await cooler.connect() + + // This does not wait for the blob. + cooler.get(ssb.blobs.want, blobId) + } +} + +exports.friend = { + isFollowing: async (feedId) => { + const ssb = await cooler.connect() + const { id } = ssb + + const isFollowing = await cooler.get( + ssb.friends.isFollowing, + { + source: id, + dest: feedId + } + ) + return isFollowing + }, + getRelationship: async (feedId) => { + const ssb = await cooler.connect() + const { id } = ssb + + if (feedId === id) { + return 'this is you' + } + + const isFollowing = await cooler.get( + ssb.friends.isFollowing, + { + source: id, + dest: feedId + } + ) + + const isBlocking = await cooler.get( + ssb.friends.isBlocking, + { + source: id, + dest: feedId + } + ) + + if (isFollowing === true && isBlocking === false) { + return 'you are following' + } else if (isFollowing === false && isBlocking === true) { + return 'you are blocking' + } else if (isFollowing === false && isBlocking === false) { + return 'you are not following or blocking' + } else { + return 'you are following and blocking (!)' + } + } +} + +exports.meta = { + myFeedId: async () => { + const ssb = await cooler.connect() + const { id } = ssb + return id + }, + get: async (msgId) => { + const ssb = await cooler.connect() + return cooler.get(ssb.get, { + id: msgId, + meta: true, + private: true + }) + }, + status: async () => { + const ssb = await cooler.connect() + return cooler.get(ssb.status) + }, + peers: async () => { + const ssb = await cooler.connect() + const peersSource = await cooler.read(ssb.conn.peers) + + return new Promise((resolve, reject) => { + pull( + peersSource, + // https://github.com/staltz/ssb-conn/issues/9 + pull.take(1), + pull.collect((err, val) => { + if (err) return reject(err) + resolve(val[0]) + }) + ) + }) + } +} + const isPost = (message) => lodash.get(message, 'value.content.type') === 'post' && lodash.get(message, 'value.content.text') != null @@ -45,10 +223,6 @@ const isLooseComment = (message) => { return conditions.every(x => x === true) } -const configure = require('./lib/configure') -const cooler = require('./lib/cooler') -const markdown = require('./lib/markdown') - const maxMessages = 64 const getMessages = async ({ myFeedId, customOptions, ssb, query, filter = null }) => { @@ -497,11 +671,11 @@ const post = { pull( pull.values(arr), - pullSort(([aKey, aVal], [bKey, bVal]) => + pullSort(([, aVal], [, bVal]) => bVal - aVal ), pull.take(Math.min(length, maxMessages)), - pull.map(([key, value]) => key), + pull.map(([key]) => key), pullParallelMap(async (key, cb) => { try { const msg = await post.get(key) @@ -819,5 +993,21 @@ const post = { return messages } } +exports.post = post -module.exports = post +exports.vote = { + publish: async ({ messageKey, value, recps }) => { + const ssb = await cooler.connect() + const branch = await cooler.get(ssb.tangle.branch, messageKey) + + await cooler.get(ssb.publish, { + type: 'vote', + vote: { + link: messageKey, + value: Number(value) + }, + branch, + recps + }) + } +} diff --git a/src/controller/models/lib/markdown.js b/src/controller/models/markdown.js similarity index 100% rename from src/controller/models/lib/markdown.js rename to src/controller/models/markdown.js diff --git a/src/controller/models/meta.js b/src/controller/models/meta.js deleted file mode 100644 index d4d2b48..0000000 --- a/src/controller/models/meta.js +++ /dev/null @@ -1,40 +0,0 @@ -'use strict' - -const cooler = require('./lib/cooler') -const pull = require('pull-stream') - -module.exports = { - myFeedId: async () => { - const ssb = await cooler.connect() - const { id } = ssb - return id - }, - get: async (msgId) => { - const ssb = await cooler.connect() - return cooler.get(ssb.get, { - id: msgId, - meta: true, - private: true - }) - }, - status: async () => { - const ssb = await cooler.connect() - return cooler.get(ssb.status) - }, - peers: async () => { - const ssb = await cooler.connect() - const peersSource = await cooler.read(ssb.conn.peers) - - return new Promise((resolve, reject) => { - pull( - peersSource, - // https://github.com/staltz/ssb-conn/issues/9 - pull.take(1), - pull.collect((err, val) => { - if (err) return reject(err) - resolve(val[0]) - }) - ) - }) - } -} diff --git a/src/controller/models/lib/server.js b/src/controller/models/server.js similarity index 100% rename from src/controller/models/lib/server.js rename to src/controller/models/server.js diff --git a/src/controller/models/vote.js b/src/controller/models/vote.js deleted file mode 100644 index bbdd425..0000000 --- a/src/controller/models/vote.js +++ /dev/null @@ -1,20 +0,0 @@ -'use strict' - -const cooler = require('./lib/cooler') - -module.exports = { - publish: async ({ messageKey, value, recps }) => { - const ssb = await cooler.connect() - const branch = await cooler.get(ssb.tangle.branch, messageKey) - - await cooler.get(ssb.publish, { - type: 'vote', - vote: { - link: messageKey, - value: Number(value) - }, - branch, - recps - }) - } -}