Replace pages with single controller file

This commit is contained in:
Christian Bundy 2020-01-08 12:38:47 -08:00
parent ca29986131
commit 0622d97e96
44 changed files with 364 additions and 443 deletions

335
src/controller/index.js Normal file
View File

@ -0,0 +1,335 @@
'use strict'
const pull = require('pull-stream')
const { nav, ul, li, a } = require('hyperaxe')
const { themeNames } = require('@fraction/base16-css')
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 metaView = require('./views/meta')
const post = require('./models/post')
const vote = require('./models/vote')
const authorView = require('./views/author')
const commentView = require('./views/comment')
const publicView = require('./views/public')
const searchView = require('./views/search')
const replyView = require('./views/reply')
const listView = require('./views/list')
const markdownView = require('./views/markdown')
let sharp
try {
sharp = require('sharp')
} catch (e) {
// Optional dependency
}
exports.author = async (feedId) => {
const description = await about.description(feedId)
const name = await about.name(feedId)
const image = await about.image(feedId)
const aboutPairs = await about.all(feedId)
const messages = await post.fromFeed(feedId)
const relationship = await friend.getRelationship(feedId)
const avatarUrl = `/image/256/${encodeURIComponent(image)}`
return authorView({
feedId,
messages,
name,
description,
avatarUrl,
aboutPairs,
relationship
})
}
exports.blob = async ({ blobId }) => {
const bufferSource = await blob.get({ blobId })
debug('got buffer source')
return new Promise((resolve) => {
pull(
bufferSource,
pull.collect(async (err, bufferArray) => {
if (err) {
await blob.want({ blobId })
resolve(Buffer.alloc(0))
} else {
const buffer = Buffer.concat(bufferArray)
resolve(buffer)
}
})
)
})
}
exports.comment = async (parentId) => {
const parentMessage = await post.get(parentId)
const myFeedId = await meta.myFeedId()
const hasRoot = typeof parentMessage.value.content.root === 'string' && ssbRef.isMsg(parentMessage.value.content.root)
const hasFork = typeof parentMessage.value.content.fork === 'string' && ssbRef.isMsg(parentMessage.value.content.fork)
const rootMessage = hasRoot
? hasFork
? parentMessage
: await post.get(parentMessage.value.content.root)
: parentMessage
const messages = await post.threadReplies(rootMessage.key)
messages.push(rootMessage)
return commentView({ messages, myFeedId, parentMessage })
}
exports.hashtag = async (channel) => {
const messages = await post.fromHashtag(channel)
return listView({ messages })
}
const fakePixel =
Buffer.from(
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=',
'base64'
)
const fakeImage = (imageSize) =>
sharp
? sharp({
create: {
width: imageSize,
height: imageSize,
channels: 4,
background: {
r: 0, g: 0, b: 0, alpha: 0.5
}
}
})
: new Promise((resolve) => resolve(fakePixel))
const fakeId = '&0000000000000000000000000000000000000000000=.sha256'
exports.image = async ({ blobId, imageSize }) => {
const bufferSource = await blob.get({ blobId })
debug('got buffer source')
return new Promise((resolve) => {
if (blobId === fakeId) {
debug('fake image')
fakeImage(imageSize).then(result => resolve(result))
} else {
debug('not fake image')
pull(
bufferSource,
pull.collect(async (err, bufferArray) => {
if (err) {
await blob.want({ blobId })
const result = fakeImage(imageSize)
debug({ result })
resolve(result)
} else {
const buffer = Buffer.concat(bufferArray)
if (sharp) {
sharp(buffer)
.resize(imageSize, imageSize)
.png()
.toBuffer()
.then((data) => {
resolve(data)
})
} else {
resolve(buffer)
}
}
})
)
}
})
}
exports.inbox = async () => {
const messages = await post.inbox()
return listView({ messages })
}
exports.json = async (message) => {
const json = await meta.get(message)
return JSON.stringify(json, null, 2)
}
exports.like = async ({ messageKey, voteValue }) => {
const value = Number(voteValue)
const message = await post.get(messageKey)
const isPrivate = message.value.meta.private === true
const messageRecipients = isPrivate ? message.value.content.recps : []
const normalized = messageRecipients.map((recipient) => {
if (typeof recipient === 'string') {
return recipient
}
if (typeof recipient.link === 'string') {
return recipient.link
}
return null
})
const recipients = normalized.length > 0 ? normalized : undefined
return vote.publish({ messageKey, value, recps: recipients })
}
exports.likes = async ({ feed }) => {
const messages = await post.likes({ feed })
return listView({ messages })
}
exports.status = async (text) => {
return markdownView({ text })
}
exports.mentions = async () => {
const messages = await post.mentionsMe()
return listView({ messages })
}
exports.meta = async ({ theme }) => {
const status = await meta.status()
const peers = await meta.peers()
return metaView({ status, peers, theme, themeNames })
}
exports.profile = async () => {
const myFeedId = await meta.myFeedId()
const description = await about.description(myFeedId)
const name = await about.name(myFeedId)
const image = await about.image(myFeedId)
const aboutPairs = await about.all(myFeedId)
const messages = await post.fromFeed(myFeedId)
const avatarUrl = `/image/256/${encodeURIComponent(image)}`
return authorView({
feedId: myFeedId,
messages,
name,
description,
avatarUrl,
aboutPairs,
relationship: null
})
}
exports.publicLatest = async () => {
const messages = await post.latest()
return publicView({ messages })
}
exports.publicPopular = async ({ period }) => {
const messages = await post.popular({ period })
const option = (somePeriod) =>
li(
period === somePeriod
? a({ class: 'current', href: `./${somePeriod}` }, somePeriod)
: a({ href: `./${somePeriod}` }, somePeriod)
)
const prefix = nav(
ul(
option('day'),
option('week'),
option('month'),
option('year')
)
)
return publicView({
messages,
prefix
})
}
exports.publishComment = async ({ message, text }) => {
// TODO: rename `message` to `parent` or `ancestor` or similar
const mentions = ssbMentions(text).filter((mention) =>
mention != null
) || undefined
const parent = await meta.get(message)
return post.comment({
parent,
message: { text, mentions }
})
}
exports.publish = async ({ text }) => {
const mentions = ssbMentions(text).filter((mention) =>
mention != null
) || undefined
return post.root({
text,
mentions
})
}
exports.publishReply = async ({ message, text }) => {
// TODO: rename `message` to `parent` or `ancestor` or similar
const mentions = ssbMentions(text).filter((mention) =>
mention != null
) || undefined
const parent = await post.get(message)
return post.reply({
parent,
message: { text, mentions }
})
}
exports.reply = async (parentId) => {
const rootMessage = await post.get(parentId)
const myFeedId = await meta.myFeedId()
debug('%O', rootMessage)
const messages = [rootMessage]
return replyView({ messages, myFeedId })
}
exports.search = async ({ query }) => {
if (typeof query === 'string') {
// https://github.com/ssbc/ssb-search/issues/7
query = query.toLowerCase()
}
const messages = await post.search({ query })
return searchView({ messages, query })
}
exports.thread = async (message) => {
const messages = await post.fromThread(message)
debug('got %i messages', messages.length)
return listView({ messages })
}

View File

@ -30,27 +30,29 @@ const requireStyle = require('require-style')
const router = require('koa-router')()
const ssbRef = require('ssb-ref')
const author = require('./pages/author')
const hashtag = require('./pages/hashtag')
const publicPopularPage = require('./pages/public-popular')
const publicLatestPage = require('./pages/public-latest')
const profile = require('./pages/profile')
const json = require('./pages/json')
const thread = require('./pages/thread')
const like = require('./pages/like')
const likesPage = require('./pages/likes')
const meta = require('./pages/meta')
const mentions = require('./pages/mentions')
const reply = require('./pages/reply')
const comment = require('./pages/comment')
const publishReply = require('./pages/publish-reply')
const publishComment = require('./pages/publish-comment')
const image = require('./pages/image')
const blob = require('./pages/blob')
const publish = require('./pages/publish')
const markdown = require('./pages/markdown')
const inboxPage = require('./pages/inbox')
const searchPage = require('./pages/search')
const {
author,
blob,
comment,
hashtag,
image,
inbox,
json,
like,
likes,
status,
mentions,
meta,
profile,
publicLatest,
publicPopular,
publish,
publishComment,
publishReply,
reply,
search,
thread
} = require('./controller')
const defaultTheme = 'atelier-sulphurPool-light'.toLowerCase()
@ -87,10 +89,10 @@ router
})
.get('/public/popular/:period', async (ctx) => {
const { period } = ctx.params
ctx.body = await publicPopularPage({ period })
ctx.body = await publicPopular({ period })
})
.get('/public/latest', async (ctx) => {
ctx.body = await publicLatestPage()
ctx.body = await publicLatest()
})
.get('/author/:feed', async (ctx) => {
const { feed } = ctx.params
@ -98,10 +100,10 @@ router
})
.get('/search/', async (ctx) => {
const { query } = ctx.query
ctx.body = await searchPage({ query })
ctx.body = await search({ query })
})
.get('/inbox', async (ctx) => {
ctx.body = await inboxPage()
ctx.body = await inbox()
})
.get('/hashtag/:channel', async (ctx) => {
const { channel } = ctx.params
@ -147,10 +149,10 @@ router
})
.get('/likes/:feed', async (ctx) => {
const { feed } = ctx.params
ctx.body = await likesPage({ feed })
ctx.body = await likes({ feed })
})
.get('/meta/readme/', async (ctx) => {
ctx.body = await markdown(config.readme)
ctx.body = await status(config.readme)
})
.get('/mentions/', async (ctx) => {
ctx.body = await mentions()

View File

@ -1,27 +0,0 @@
'use strict'
const about = require('./models/about')
const post = require('./models/post')
const friend = require('./models/friend')
const authorView = require('./views/author')
module.exports = async function authorPage (feedId) {
const description = await about.description(feedId)
const name = await about.name(feedId)
const image = await about.image(feedId)
const aboutPairs = await about.all(feedId)
const messages = await post.fromFeed(feedId)
const relationship = await friend.getRelationship(feedId)
const avatarUrl = `/image/256/${encodeURIComponent(image)}`
return authorView({
feedId,
messages,
name,
description,
avatarUrl,
aboutPairs,
relationship
})
}

View File

@ -1,25 +0,0 @@
'use strict'
const pull = require('pull-stream')
const debug = require('debug')('oasis')
const blob = require('./models/blob')
module.exports = async function imagePage ({ blobId }) {
const bufferSource = await blob.get({ blobId })
debug('got buffer source')
return new Promise((resolve) => {
pull(
bufferSource,
pull.collect(async (err, bufferArray) => {
if (err) {
await blob.want({ blobId })
resolve(Buffer.alloc(0))
} else {
const buffer = Buffer.concat(bufferArray)
resolve(buffer)
}
})
)
})
}

View File

@ -1,26 +0,0 @@
'use strict'
const post = require('./models/post')
const meta = require('./models/meta')
const commentView = require('./views/comment')
const ssbRef = require('ssb-ref')
module.exports = async function commentPage (parentId) {
const parentMessage = await post.get(parentId)
const myFeedId = await meta.myFeedId()
const hasRoot = typeof parentMessage.value.content.root === 'string' && ssbRef.isMsg(parentMessage.value.content.root)
const hasFork = typeof parentMessage.value.content.fork === 'string' && ssbRef.isMsg(parentMessage.value.content.fork)
const rootMessage = hasRoot
? hasFork
? parentMessage
: await post.get(parentMessage.value.content.root)
: parentMessage
const messages = await post.threadReplies(rootMessage.key)
messages.push(rootMessage)
return commentView({ messages, myFeedId, parentMessage })
}

View File

@ -1,7 +0,0 @@
'use strict'
const composeView = require('./views/compose')
module.exports = async function composePage () {
return composeView()
}

View File

@ -1,10 +0,0 @@
'use strict'
const post = require('./models/post')
const listView = require('./views/list')
module.exports = async function hashtag (channel) {
const messages = await post.fromHashtag(channel)
return listView({ messages })
}

View File

@ -1,74 +0,0 @@
'use strict'
let sharp
const pull = require('pull-stream')
const debug = require('debug')('oasis')
const blob = require('./models/blob')
try {
sharp = require('sharp')
} catch (e) {
// Optional dependency
}
const fakePixel =
Buffer.from(
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=',
'base64'
)
const fakeImage = (imageSize) =>
sharp
? sharp({
create: {
width: imageSize,
height: imageSize,
channels: 4,
background: {
r: 0, g: 0, b: 0, alpha: 0.5
}
}
})
: new Promise((resolve) => resolve(fakePixel))
const fakeId = '&0000000000000000000000000000000000000000000=.sha256'
module.exports = async function imagePage ({ blobId, imageSize }) {
const bufferSource = await blob.get({ blobId })
debug('got buffer source')
return new Promise((resolve) => {
if (blobId === fakeId) {
debug('fake image')
fakeImage(imageSize).then(result => resolve(result))
} else {
debug('not fake image')
pull(
bufferSource,
pull.collect(async (err, bufferArray) => {
if (err) {
await blob.want({ blobId })
const result = fakeImage(imageSize)
debug({ result })
resolve(result)
} else {
const buffer = Buffer.concat(bufferArray)
if (sharp) {
sharp(buffer)
.resize(imageSize, imageSize)
.png()
.toBuffer()
.then((data) => {
resolve(data)
})
} else {
resolve(buffer)
}
}
})
)
}
})
}

View File

@ -1,10 +0,0 @@
module.exports = () => ''
const listView = require('./views/list')
const post = require('./models/post')
module.exports = async function publicPage () {
const messages = await post.inbox()
return listView({ messages })
}

View File

@ -1,8 +0,0 @@
'use strict'
const meta = require('./models/meta')
module.exports = async function jsonPage (message) {
const json = await meta.get(message)
return JSON.stringify(json, null, 2)
}

View File

@ -1,28 +0,0 @@
'use strict'
const vote = require('./models/vote')
const post = require('./models/post')
module.exports = async function like ({ messageKey, voteValue }) {
const value = Number(voteValue)
const message = await post.get(messageKey)
const isPrivate = message.value.meta.private === true
const messageRecipients = isPrivate ? message.value.content.recps : []
const normalized = messageRecipients.map((recipient) => {
if (typeof recipient === 'string') {
return recipient
}
if (typeof recipient.link === 'string') {
return recipient.link
}
return null
})
const recipients = normalized.length > 0 ? normalized : undefined
return vote.publish({ messageKey, value, recps: recipients })
}

View File

@ -1,9 +0,0 @@
'use strict'
const post = require('./models/post')
const listView = require('./views/list')
module.exports = async function likesPage ({ feed }) {
const messages = await post.likes({ feed })
return listView({ messages })
}

View File

@ -1,7 +0,0 @@
'use strict'
const markdownView = require('./views/markdown')
module.exports = async function statusPage (text) {
return markdownView({ text })
}

View File

@ -1,10 +0,0 @@
'use strict'
const post = require('./models/post')
const listView = require('./views/list')
module.exports = async function mentionsPage () {
const messages = await post.mentionsMe()
return listView({ messages })
}

View File

@ -1,13 +0,0 @@
'use strict'
const meta = require('./models/meta')
const metaView = require('./views/meta')
module.exports = async function metaPage ({ theme }) {
const status = await meta.status()
const peers = await meta.peers()
const { themeNames } = require('@fraction/base16-css')
return metaView({ status, peers, theme, themeNames })
}

View File

@ -1,29 +0,0 @@
'use strict'
const about = require('./models/about')
const post = require('./models/post')
const meta = require('./models/meta')
const authorView = require('./views/author')
module.exports = async function profilePage () {
const myFeedId = await meta.myFeedId()
const description = await about.description(myFeedId)
const name = await about.name(myFeedId)
const image = await about.image(myFeedId)
const aboutPairs = await about.all(myFeedId)
const messages = await post.fromFeed(myFeedId)
const avatarUrl = `/image/256/${encodeURIComponent(image)}`
return authorView({
feedId: myFeedId,
messages,
name,
description,
avatarUrl,
aboutPairs,
relationship: null
})
}

View File

@ -1,9 +0,0 @@
'use strict'
const publicView = require('./views/public')
const post = require('./models/post')
module.exports = async function publicLatestPage () {
const messages = await post.latest()
return publicView({ messages })
}

View File

@ -1,30 +0,0 @@
'use strict'
const publicView = require('./views/public')
const { nav, ul, li, a } = require('hyperaxe')
const post = require('./models/post')
module.exports = async function publicPopularPage ({ period }) {
const messages = await post.popular({ period })
const option = (somePeriod) =>
li(
period === somePeriod
? a({ class: 'current', href: `./${somePeriod}` }, somePeriod)
: a({ href: `./${somePeriod}` }, somePeriod)
)
const prefix = nav(
ul(
option('day'),
option('week'),
option('month'),
option('year')
)
)
return publicView({
messages,
prefix
})
}

View File

@ -1,18 +0,0 @@
'use strict'
const ssbMentions = require('ssb-mentions')
const post = require('./models/post')
const meta = require('./models/post')
module.exports = async function publishCommentPage ({ message, text }) {
// TODO: rename `message` to `parent` or `ancestor` or similar
const mentions = ssbMentions(text).filter((mention) =>
mention != null
) || undefined
const parent = await meta.get(message)
return post.comment({
parent,
message: { text, mentions }
})
}

View File

@ -1,17 +0,0 @@
'use strict'
const ssbMentions = require('ssb-mentions')
const post = require('./models/post')
module.exports = async function publishReplyPage ({ message, text }) {
// TODO: rename `message` to `parent` or `ancestor` or similar
const mentions = ssbMentions(text).filter((mention) =>
mention != null
) || undefined
const parent = await post.get(message)
return post.reply({
parent,
message: { text, mentions }
})
}

View File

@ -1,15 +0,0 @@
'use strict'
const ssbMentions = require('ssb-mentions')
const post = require('./models/post')
module.exports = async function publishPage ({ text }) {
const mentions = ssbMentions(text).filter((mention) =>
mention != null
) || undefined
return post.root({
text,
mentions
})
}

View File

@ -1,16 +0,0 @@
'use strict'
const debug = require('debug')('oasis')
const post = require('./models/post')
const meta = require('./models/meta')
const replyView = require('./views/reply')
module.exports = async function replyPage (parentId) {
const rootMessage = await post.get(parentId)
const myFeedId = await meta.myFeedId()
debug('%O', rootMessage)
const messages = [rootMessage]
return replyView({ messages, myFeedId })
}

View File

@ -1,15 +0,0 @@
'use strict'
const post = require('./models/post')
const searchView = require('./views/search')
module.exports = async function searchPage ({ query }) {
if (typeof query === 'string') {
// https://github.com/ssbc/ssb-search/issues/7
query = query.toLowerCase()
}
const messages = await post.search({ query })
return searchView({ messages, query })
}

View File

@ -1,13 +0,0 @@
'use strict'
const debug = require('debug')('oasis:route-thread')
const listView = require('./views/list')
const post = require('./models/post')
module.exports = async function threadPage (message) {
const messages = await post.fromThread(message)
debug('got %i messages', messages.length)
return listView({ messages })
}