be more permissible with links and nofollow when low sats

This commit is contained in:
keyan 2021-05-20 14:11:58 -05:00
parent 57e96ac02b
commit a68da87382
5 changed files with 31 additions and 4 deletions

View File

@ -1,4 +1,5 @@
import { UserInputError, AuthenticationError } from 'apollo-server-micro' import { UserInputError, AuthenticationError } from 'apollo-server-micro'
import { ensureProtocol } from '../../lib/url'
import serialize from './serial' import serialize from './serial'
async function comments (models, id) { async function comments (models, id) {
@ -77,7 +78,9 @@ export default {
throw new UserInputError('link must have url', { argumentName: 'url' }) throw new UserInputError('link must have url', { argumentName: 'url' })
} }
return await createItem(parent, { title, url }, { me, models }) console.log(ensureProtocol(url))
return await createItem(parent, { title, url: ensureProtocol(url) }, { me, models })
}, },
createDiscussion: async (parent, { title, text }, { me, models }) => { createDiscussion: async (parent, { title, text }, { me, models }) => {
if (!title) { if (!title) {
@ -171,10 +174,12 @@ const createItem = async (parent, { title, url, text, parentId }, { me, models }
throw new AuthenticationError('you must be logged in') throw new AuthenticationError('you must be logged in')
} }
console.log('before')
const [item] = await serialize(models, models.$queryRaw( const [item] = await serialize(models, models.$queryRaw(
`${SELECT} FROM create_item($1, $2, $3, $4, $5) AS "Item"`, `${SELECT} FROM create_item($1, $2, $3, $4, $5) AS "Item"`,
title, url, text, Number(parentId), me.name)) title, url, text, Number(parentId), me.name))
item.comments = [] item.comments = []
console.log('after')
return item return item
} }

View File

@ -2,7 +2,7 @@ const { UserInputError } = require('apollo-server-micro')
const retry = require('async-retry') const retry = require('async-retry')
async function serialize (models, call) { async function serialize (models, call) {
await retry(async bail => { return await retry(async bail => {
try { try {
const [, result] = await models.$transaction([ const [, result] = await models.$transaction([
models.$executeRaw(SERIALIZE), models.$executeRaw(SERIALIZE),

View File

@ -19,7 +19,10 @@ export default function Item ({ item, rank, children }) {
<Link href={`/items/${item.id}`} passHref> <Link href={`/items/${item.id}`} passHref>
<a className={`${styles.title} text-reset flex-md-shrink-0 mr-2`}>{item.title}</a> <a className={`${styles.title} text-reset flex-md-shrink-0 mr-2`}>{item.title}</a>
</Link> </Link>
{item.url && <a className={styles.link} href={item.url}>{item.url.replace(/(^\w+:|^)\/\//, '')}</a>} {item.url &&
<a className={styles.link} href={item.url} rel={item.sats > 5 ? null : 'nofollow'}>
{item.url.replace(/(^https?:|^)\/\//, '')}
</a>}
</div> </div>
<div className={`${styles.other}`}> <div className={`${styles.other}`}>
<span>{item.sats} sats</span> <span>{item.sats} sats</span>

6
lib/url.js Normal file
View File

@ -0,0 +1,6 @@
export function ensureProtocol (value) {
if (!/^[a-z0-9]+:(\/\/)?/.test(value)) {
value = 'http://' + value
}
return value
}

View File

@ -5,6 +5,7 @@ import Link from 'next/link'
import * as Yup from 'yup' import * as Yup from 'yup'
import { gql, useMutation } from '@apollo/client' import { gql, useMutation } from '@apollo/client'
import LayoutCenter from '../components/layout-center' import LayoutCenter from '../components/layout-center'
import { ensureProtocol } from '../lib/url'
export const DiscussionSchema = Yup.object({ export const DiscussionSchema = Yup.object({
title: Yup.string().required('required').trim() title: Yup.string().required('required').trim()
@ -55,7 +56,19 @@ export function DiscussionForm () {
export const LinkSchema = Yup.object({ export const LinkSchema = Yup.object({
title: Yup.string().required('required').trim(), title: Yup.string().required('required').trim(),
url: Yup.string().url('invalid url').required('required') url: Yup.string().test({
name: 'url',
test: (value) => {
try {
value = ensureProtocol(value)
const valid = new URL(value)
return Boolean(valid)
} catch {
return false
}
},
message: 'invalid url'
}).required('required')
}) })
export function LinkForm () { export function LinkForm () {