diff --git a/api/resolvers/item.js b/api/resolvers/item.js
index 95ca9d4d..27772fa4 100644
--- a/api/resolvers/item.js
+++ b/api/resolvers/item.js
@@ -611,6 +611,12 @@ export default {
},
user: async (item, args, { models }) =>
await models.user.findUnique({ where: { id: item.userId } }),
+ fwdUser: async (item, args, { models }) => {
+ if (!item.fwdUserId) {
+ return null
+ }
+ return await models.user.findUnique({ where: { id: item.fwdUserId } })
+ },
ncomments: async (item, args, { models }) => {
const [{ count }] = await models.$queryRaw`
SELECT count(*)
@@ -793,12 +799,29 @@ const createItem = async (parent, { title, url, text, boost, forward, parentId }
throw new UserInputError(`boost must be at least ${BOOST_MIN}`, { argumentName: 'boost' })
}
+ let fwdUser
+ if (forward) {
+ fwdUser = await models.user.findUnique({ where: { name: forward } })
+ if (!fwdUser) {
+ throw new UserInputError('forward user does not exist', { argumentName: 'forward' })
+ }
+ }
+
const [item] = await serialize(models,
models.$queryRaw(`${SELECT} FROM create_item($1, $2, $3, $4, $5, $6) AS "Item"`,
title, url, text, Number(boost || 0), Number(parentId), Number(me.id)))
await createMentions(item, models)
+ if (fwdUser) {
+ await models.item.update({
+ where: { id: item.id },
+ data: {
+ fwdUserId: fwdUser.id
+ }
+ })
+ }
+
item.comments = []
return item
}
@@ -831,7 +854,7 @@ function nestComments (flat, parentId) {
// we have to do our own query because ltree is unsupported
export const SELECT =
`SELECT "Item".id, "Item".created_at as "createdAt", "Item".updated_at as "updatedAt", "Item".title,
- "Item".text, "Item".url, "Item"."userId", "Item"."parentId", "Item"."pinId", "Item"."maxBid",
+ "Item".text, "Item".url, "Item"."userId", "Item"."fwdUserId", "Item"."parentId", "Item"."pinId", "Item"."maxBid",
"Item".company, "Item".location, "Item".remote,
"Item"."subName", "Item".status, ltree2text("Item"."path") AS "path"`
diff --git a/api/resolvers/wallet.js b/api/resolvers/wallet.js
index a9abc972..fb752e82 100644
--- a/api/resolvers/wallet.js
+++ b/api/resolvers/wallet.js
@@ -97,7 +97,9 @@ export default {
FROM "ItemAct"
JOIN "Item" on "ItemAct"."itemId" = "Item".id
WHERE "ItemAct"."userId" <> $1 AND "ItemAct".act <> 'BOOST'
- AND "Item"."userId" = $1 AND "ItemAct".created_at <= $2
+ AND (("Item"."userId" = $1 AND "Item"."fwdUserId" IS NULL)
+ OR ("Item"."fwdUserId" = $1 AND "ItemAct"."userId" <> "Item"."userId"))
+ AND "ItemAct".created_at <= $2
GROUP BY "Item".id)`)
queries.push(
`(SELECT ('earn' || "Earn".id) as id, "Earn".id as "factId", NULL as bolt11,
diff --git a/api/typeDefs/item.js b/api/typeDefs/item.js
index 6e00fe79..642069a1 100644
--- a/api/typeDefs/item.js
+++ b/api/typeDefs/item.js
@@ -52,6 +52,7 @@ export default gql`
root: Item
user: User!
userId: Int!
+ fwdUser: User
depth: Int!
mine: Boolean!
boost: Int!
diff --git a/components/adv-post-form.js b/components/adv-post-form.js
index 2b314fa9..5328e2df 100644
--- a/components/adv-post-form.js
+++ b/components/adv-post-form.js
@@ -3,11 +3,23 @@ import * as Yup from 'yup'
import { Input } from './form'
import { InputGroup } from 'react-bootstrap'
import { BOOST_MIN } from '../lib/constants'
+import { NAME_QUERY } from '../fragments/users'
-export const AdvPostSchema = {
- boost: Yup.number().typeError('must be a number')
- .min(BOOST_MIN, `must be at least ${BOOST_MIN}`).integer('must be whole'),
- forward: Yup.string().trim()
+export function AdvPostSchema (client) {
+ return {
+ boost: Yup.number().typeError('must be a number')
+ .min(BOOST_MIN, `must be at least ${BOOST_MIN}`).integer('must be whole'),
+ forward: Yup.string()
+ .test({
+ name: 'name',
+ test: async name => {
+ if (!name || !name.length) return true
+ const { data } = await client.query({ query: NAME_QUERY, variables: { name }, fetchPolicy: 'network-only' })
+ return !data.nameAvailable
+ },
+ message: 'user does not exist'
+ })
+ }
}
export const AdvPostInitial = {
@@ -30,8 +42,9 @@ export default function AdvPostForm () {
100% of sats earned will be sent to this user}
+ hint={100% of sats will be sent to this user}
prepend=