Merge pull request #52 from fraction/follow-unfollow

Add follow and unfollow
This commit is contained in:
Christian Bundy 2020-01-14 09:48:36 -08:00 committed by GitHub
commit aa9b7dda1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 59 additions and 1 deletions

View File

@ -16,6 +16,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Security
-->
## [Unreleased]
### Added
- Add follow/unfollow
## [2.1.0] - 2019-09-24
### Added

View File

@ -454,6 +454,18 @@ router
ctx.body = await publish({ text })
ctx.redirect('/')
})
.post('/follow/:feed', koaBody(), async (ctx) => {
const { feed } = ctx.params
const referer = new URL(ctx.request.header.referer)
ctx.body = await friend.follow(feed)
ctx.redirect(referer)
})
.post('/unfollow/:feed', koaBody(), async (ctx) => {
const { feed } = ctx.params
const referer = new URL(ctx.request.header.referer)
ctx.body = await friend.unfollow(feed)
ctx.redirect(referer)
})
.post('/like/:message', koaBody(), async (ctx) => {
const { message } = ctx.params
// TODO: convert all so `message` is full message and `messageKey` is key

View File

@ -126,6 +126,29 @@ module.exports = (cooler) => {
)
return isFollowing
},
setFollowing: async ({ feedId, following }) => {
const ssb = await cooler.connect()
const content = {
type: 'contact',
contact: feedId,
following
}
return cooler.get(ssb.publish, content)
},
follow: async (feedId) => {
const isFollowing = await models.friend.isFollowing(feedId)
if (!isFollowing) {
await models.friend.setFollowing({ feedId, following: true })
}
},
unfollow: async (feedId) => {
const isFollowing = await models.friend.isFollowing(feedId)
if (isFollowing) {
await models.friend.setFollowing({ feedId, following: false })
}
},
getRelationship: async (feedId) => {
const ssb = await cooler.connect()
const { id } = ssb
@ -160,6 +183,7 @@ module.exports = (cooler) => {
return 'you are following and blocking (!)'
}
}
}
models.meta = {

View File

@ -46,6 +46,21 @@ exports.authorView = ({
const mention = `[@${name}](${feedId})`
const markdownMention = highlightJs.highlight('markdown', mention).value
const areFollowing = relationship === 'you are following'
const contactFormType = areFollowing
? 'unfollow'
: 'follow'
// We're on our own profile!
const contactForm = relationship !== null
? form({ action: `/${contactFormType}/${encodeURIComponent(feedId)}`, method: 'post' },
button({
type: 'submit'
},
contactFormType))
: null
const prefix = section({ class: 'message' },
header({ class: 'profile' },
img({ class: 'avatar', src: avatarUrl }),
@ -59,7 +74,8 @@ exports.authorView = ({
: null,
footer(
a({ href: `/likes/${encodeURIComponent(feedId)}` }, 'view likes'),
span(relationship)
span(relationship),
contactForm
)
)