354 lines
12 KiB
JavaScript
354 lines
12 KiB
JavaScript
import { GraphQLError } from 'graphql'
|
|
import { whenRange } from '@/lib/time'
|
|
import { ssValidate, territorySchema } from '@/lib/validate'
|
|
import { decodeCursor, LIMIT, nextCursorEncoded } from '@/lib/cursor'
|
|
import { viewGroup } from './growth'
|
|
import { notifyTerritoryTransfer } from '@/lib/webPush'
|
|
import performPaidAction from '../paidAction'
|
|
|
|
export async function getSub (parent, { name }, { models, me }) {
|
|
if (!name) return null
|
|
|
|
return await models.sub.findUnique({
|
|
where: {
|
|
name
|
|
},
|
|
...(me
|
|
? {
|
|
include: {
|
|
MuteSub: {
|
|
where: {
|
|
userId: Number(me?.id)
|
|
}
|
|
},
|
|
SubSubscription: {
|
|
where: {
|
|
userId: Number(me?.id)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
: {})
|
|
})
|
|
}
|
|
|
|
export default {
|
|
Query: {
|
|
sub: getSub,
|
|
subs: async (parent, args, { models, me }) => {
|
|
if (me) {
|
|
const currentUser = await models.user.findUnique({ where: { id: me.id } })
|
|
const showNsfw = currentUser ? currentUser.nsfwMode : false
|
|
|
|
return await models.$queryRawUnsafe(`
|
|
SELECT "Sub".*, "Sub".created_at as "createdAt", COALESCE(json_agg("MuteSub".*) FILTER (WHERE "MuteSub"."userId" IS NOT NULL), '[]') AS "MuteSub"
|
|
FROM "Sub"
|
|
LEFT JOIN "MuteSub" ON "Sub".name = "MuteSub"."subName" AND "MuteSub"."userId" = ${me.id}::INTEGER
|
|
WHERE status <> 'STOPPED' ${showNsfw ? '' : 'AND "Sub"."nsfw" = FALSE'}
|
|
GROUP BY "Sub".name, "MuteSub"."userId"
|
|
ORDER BY "Sub".name ASC
|
|
`)
|
|
}
|
|
|
|
return await models.sub.findMany({
|
|
where: {
|
|
status: {
|
|
not: 'STOPPED'
|
|
},
|
|
nsfw: false
|
|
},
|
|
orderBy: {
|
|
name: 'asc'
|
|
}
|
|
})
|
|
},
|
|
subLatestPost: async (parent, { name }, { models, me }) => {
|
|
const latest = await models.item.findFirst({
|
|
where: {
|
|
subName: name
|
|
},
|
|
orderBy: {
|
|
createdAt: 'desc'
|
|
}
|
|
})
|
|
|
|
return latest?.createdAt
|
|
},
|
|
topSubs: async (parent, { cursor, when, by, from, to, limit = LIMIT }, { models, me }) => {
|
|
const decodedCursor = decodeCursor(cursor)
|
|
const range = whenRange(when, from, to || decodeCursor.time)
|
|
|
|
let column
|
|
switch (by) {
|
|
case 'revenue': column = 'revenue'; break
|
|
case 'spent': column = 'spent'; break
|
|
case 'posts': column = 'nposts'; break
|
|
case 'comments': column = 'ncomments'; break
|
|
default: column = 'stacked'; break
|
|
}
|
|
|
|
const subs = await models.$queryRawUnsafe(`
|
|
SELECT "Sub".*,
|
|
COALESCE(floor(sum(msats_revenue)/1000), 0) as revenue,
|
|
COALESCE(floor(sum(msats_stacked)/1000), 0) as stacked,
|
|
COALESCE(floor(sum(msats_spent)/1000), 0) as spent,
|
|
COALESCE(sum(posts), 0) as nposts,
|
|
COALESCE(sum(comments), 0) as ncomments
|
|
FROM ${viewGroup(range, 'sub_stats')}
|
|
JOIN "Sub" on "Sub".name = u.sub_name
|
|
GROUP BY "Sub".name
|
|
ORDER BY ${column} DESC NULLS LAST, "Sub".created_at ASC
|
|
OFFSET $3
|
|
LIMIT $4`, ...range, decodedCursor.offset, limit)
|
|
|
|
return {
|
|
cursor: subs.length === limit ? nextCursorEncoded(decodedCursor, limit) : null,
|
|
subs
|
|
}
|
|
},
|
|
userSubs: async (_parent, { name, cursor, when, by, from, to, limit = LIMIT }, { models }) => {
|
|
if (!name) {
|
|
throw new GraphQLError('must supply user name', { extensions: { code: 'BAD_INPUT' } })
|
|
}
|
|
|
|
const user = await models.user.findUnique({ where: { name } })
|
|
if (!user) {
|
|
throw new GraphQLError('no user has that name', { extensions: { code: 'BAD_INPUT' } })
|
|
}
|
|
|
|
const decodedCursor = decodeCursor(cursor)
|
|
const range = whenRange(when, from, to || decodeCursor.time)
|
|
|
|
let column
|
|
switch (by) {
|
|
case 'revenue': column = 'revenue'; break
|
|
case 'spent': column = 'spent'; break
|
|
case 'posts': column = 'nposts'; break
|
|
case 'comments': column = 'ncomments'; break
|
|
default: column = 'stacked'; break
|
|
}
|
|
|
|
const subs = await models.$queryRawUnsafe(`
|
|
SELECT "Sub".*,
|
|
"Sub".created_at as "createdAt",
|
|
COALESCE(floor(sum(msats_revenue)/1000), 0) as revenue,
|
|
COALESCE(floor(sum(msats_stacked)/1000), 0) as stacked,
|
|
COALESCE(floor(sum(msats_spent)/1000), 0) as spent,
|
|
COALESCE(sum(posts), 0) as nposts,
|
|
COALESCE(sum(comments), 0) as ncomments
|
|
FROM ${viewGroup(range, 'sub_stats')}
|
|
JOIN "Sub" on "Sub".name = u.sub_name
|
|
WHERE "Sub"."userId" = $3
|
|
AND "Sub".status = 'ACTIVE'
|
|
GROUP BY "Sub".name
|
|
ORDER BY ${column} DESC NULLS LAST, "Sub".created_at ASC
|
|
OFFSET $4
|
|
LIMIT $5`, ...range, user.id, decodedCursor.offset, limit)
|
|
|
|
return {
|
|
cursor: subs.length === limit ? nextCursorEncoded(decodedCursor, limit) : null,
|
|
subs
|
|
}
|
|
}
|
|
},
|
|
Mutation: {
|
|
upsertSub: async (parent, { ...data }, { me, models, lnd }) => {
|
|
if (!me) {
|
|
throw new GraphQLError('you must be logged in', { extensions: { code: 'UNAUTHENTICATED' } })
|
|
}
|
|
|
|
await ssValidate(territorySchema, data, { models, me, sub: { name: data.oldName } })
|
|
|
|
if (data.oldName) {
|
|
return await updateSub(parent, data, { me, models, lnd })
|
|
} else {
|
|
return await createSub(parent, data, { me, models, lnd })
|
|
}
|
|
},
|
|
paySub: async (parent, { name }, { me, models, lnd }) => {
|
|
// check that they own the sub
|
|
const sub = await models.sub.findUnique({
|
|
where: {
|
|
name
|
|
}
|
|
})
|
|
|
|
if (!sub) {
|
|
throw new GraphQLError('sub not found', { extensions: { code: 'BAD_INPUT' } })
|
|
}
|
|
|
|
if (sub.userId !== me.id) {
|
|
throw new GraphQLError('you do not own this sub', { extensions: { code: 'BAD_INPUT' } })
|
|
}
|
|
|
|
if (sub.status === 'ACTIVE') {
|
|
return sub
|
|
}
|
|
|
|
return await performPaidAction('TERRITORY_BILLING', { name }, { me, models, lnd })
|
|
},
|
|
toggleMuteSub: async (parent, { name }, { me, models }) => {
|
|
if (!me) {
|
|
throw new GraphQLError('you must be logged in', { extensions: { code: 'UNAUTHENTICATED' } })
|
|
}
|
|
|
|
const lookupData = { userId: Number(me.id), subName: name }
|
|
const where = { userId_subName: lookupData }
|
|
const existing = await models.muteSub.findUnique({ where })
|
|
if (existing) {
|
|
await models.muteSub.delete({ where })
|
|
return false
|
|
} else {
|
|
await models.muteSub.create({ data: { ...lookupData } })
|
|
return true
|
|
}
|
|
},
|
|
toggleSubSubscription: async (sub, { name }, { me, models }) => {
|
|
if (!me) {
|
|
throw new GraphQLError('you must be logged in', { extensions: { code: 'UNAUTHENTICATED' } })
|
|
}
|
|
|
|
const lookupData = { userId: me.id, subName: name }
|
|
const where = { userId_subName: lookupData }
|
|
const existing = await models.subSubscription.findUnique({ where })
|
|
if (existing) {
|
|
await models.subSubscription.delete({ where })
|
|
return false
|
|
} else {
|
|
await models.subSubscription.create({ data: lookupData })
|
|
return true
|
|
}
|
|
},
|
|
transferTerritory: async (parent, { subName, userName }, { me, models }) => {
|
|
if (!me) {
|
|
throw new GraphQLError('you must be logged in', { extensions: { code: 'UNAUTHENTICATED' } })
|
|
}
|
|
|
|
const sub = await models.sub.findUnique({
|
|
where: {
|
|
name: subName
|
|
}
|
|
})
|
|
if (!sub) {
|
|
throw new GraphQLError('sub not found', { extensions: { code: 'BAD_INPUT' } })
|
|
}
|
|
if (sub.userId !== me.id) {
|
|
throw new GraphQLError('you do not own this sub', { extensions: { code: 'BAD_INPUT' } })
|
|
}
|
|
|
|
const user = await models.user.findFirst({ where: { name: userName } })
|
|
if (!user) {
|
|
throw new GraphQLError('user not found', { extensions: { code: 'BAD_INPUT' } })
|
|
}
|
|
if (user.id === me.id) {
|
|
throw new GraphQLError('cannot transfer territory to yourself', { extensions: { code: 'BAD_INPUT' } })
|
|
}
|
|
|
|
const [, updatedSub] = await models.$transaction([
|
|
models.territoryTransfer.create({ data: { subName, oldUserId: me.id, newUserId: user.id } }),
|
|
models.sub.update({ where: { name: subName }, data: { userId: user.id, billingAutoRenew: false } })
|
|
])
|
|
|
|
notifyTerritoryTransfer({ models, sub, to: user })
|
|
|
|
return updatedSub
|
|
},
|
|
unarchiveTerritory: async (parent, { ...data }, { me, models, lnd }) => {
|
|
if (!me) {
|
|
throw new GraphQLError('you must be logged in', { extensions: { code: 'UNAUTHENTICATED' } })
|
|
}
|
|
|
|
const { name } = data
|
|
|
|
await ssValidate(territorySchema, data, { models, me, sub: { name } })
|
|
|
|
const oldSub = await models.sub.findUnique({ where: { name } })
|
|
if (!oldSub) {
|
|
throw new GraphQLError('sub not found', { extensions: { code: 'BAD_INPUT' } })
|
|
}
|
|
if (oldSub.status !== 'STOPPED') {
|
|
throw new GraphQLError('sub is not archived', { extensions: { code: 'BAD_INPUT' } })
|
|
}
|
|
if (oldSub.billingType === 'ONCE') {
|
|
// sanity check. this should never happen but leaving this comment here
|
|
// to stop error propagation just in case and document that this should never happen.
|
|
// #defensivecode
|
|
throw new GraphQLError('sub should not be archived', { extensions: { code: 'BAD_INPUT' } })
|
|
}
|
|
|
|
return await performPaidAction('TERRITORY_UNARCHIVE', data, { me, models, lnd })
|
|
}
|
|
},
|
|
Sub: {
|
|
optional: sub => sub,
|
|
user: async (sub, args, { models }) => {
|
|
if (sub.user) {
|
|
return sub.user
|
|
}
|
|
return await models.user.findUnique({ where: { id: sub.userId } })
|
|
},
|
|
meMuteSub: async (sub, args, { models }) => {
|
|
if (sub.meMuteSub !== undefined) {
|
|
return sub.meMuteSub
|
|
}
|
|
return sub.MuteSub?.length > 0
|
|
},
|
|
nposts: async (sub, { when, from, to }, { models }) => {
|
|
if (typeof sub.nposts !== 'undefined') {
|
|
return sub.nposts
|
|
}
|
|
},
|
|
ncomments: async (sub, { when, from, to }, { models }) => {
|
|
if (typeof sub.ncomments !== 'undefined') {
|
|
return sub.ncomments
|
|
}
|
|
},
|
|
meSubscription: async (sub, args, { me, models }) => {
|
|
if (sub.meSubscription !== undefined) {
|
|
return sub.meSubscription
|
|
}
|
|
|
|
return sub.SubSubscription?.length > 0
|
|
},
|
|
createdAt: sub => sub.createdAt || sub.created_at
|
|
}
|
|
}
|
|
|
|
async function createSub (parent, data, { me, models, lnd }) {
|
|
try {
|
|
return await performPaidAction('TERRITORY_CREATE', data, { me, models, lnd })
|
|
} catch (error) {
|
|
if (error.code === 'P2002') {
|
|
throw new GraphQLError('name taken', { extensions: { code: 'BAD_INPUT' } })
|
|
}
|
|
throw error
|
|
}
|
|
}
|
|
|
|
async function updateSub (parent, { oldName, ...data }, { me, models, lnd }) {
|
|
const oldSub = await models.sub.findUnique({
|
|
where: {
|
|
name: oldName,
|
|
userId: me.id,
|
|
// this function's logic is only valid if the sub is not stopped
|
|
// so prevent updates to stopped subs
|
|
status: {
|
|
not: 'STOPPED'
|
|
}
|
|
}
|
|
})
|
|
|
|
if (!oldSub) {
|
|
throw new GraphQLError('sub not found', { extensions: { code: 'BAD_INPUT' } })
|
|
}
|
|
|
|
try {
|
|
return await performPaidAction('TERRITORY_UPDATE', { oldName, ...data }, { me, models, lnd })
|
|
} catch (error) {
|
|
if (error.code === 'P2002') {
|
|
throw new GraphQLError('name taken', { extensions: { code: 'BAD_INPUT' } })
|
|
}
|
|
throw error
|
|
}
|
|
}
|