2021-05-22 00:09:11 +00:00
|
|
|
import { AuthenticationError, UserInputError } from 'apollo-server-errors'
|
2021-05-21 22:32:21 +00:00
|
|
|
|
2021-03-25 19:29:24 +00:00
|
|
|
export default {
|
|
|
|
Query: {
|
|
|
|
me: async (parent, args, { models, me }) =>
|
2021-06-27 03:09:39 +00:00
|
|
|
me ? await models.user.findUnique({ where: { id: me.id } }) : null,
|
2021-04-22 22:14:32 +00:00
|
|
|
user: async (parent, { name }, { models }) => {
|
|
|
|
return await models.user.findUnique({ where: { name } })
|
|
|
|
},
|
2021-03-25 19:29:24 +00:00
|
|
|
users: async (parent, args, { models }) =>
|
2021-05-21 22:32:21 +00:00
|
|
|
await models.user.findMany(),
|
|
|
|
nameAvailable: async (parent, { name }, { models, me }) => {
|
|
|
|
if (!me) {
|
|
|
|
throw new AuthenticationError('you must be logged in')
|
|
|
|
}
|
|
|
|
|
|
|
|
return me.name === name || !(await models.user.findUnique({ where: { name } }))
|
2021-06-24 23:56:01 +00:00
|
|
|
},
|
|
|
|
recentlyStacked: async (parent, args, { models, me }) => {
|
|
|
|
if (!me) {
|
|
|
|
throw new AuthenticationError('you must be logged in')
|
|
|
|
}
|
|
|
|
|
2021-06-27 03:09:39 +00:00
|
|
|
const user = await models.user.findUnique({ where: { id: me.id } })
|
2021-06-24 23:56:01 +00:00
|
|
|
|
|
|
|
const [{ sum }] = await models.$queryRaw(`
|
|
|
|
SELECT sum("Vote".sats)
|
2021-08-17 23:07:52 +00:00
|
|
|
FROM "Vote"
|
|
|
|
JOIN "Item" on "Vote"."itemId" = "Item".id
|
|
|
|
WHERE "Vote"."userId" <> $1
|
2021-06-24 23:56:01 +00:00
|
|
|
AND ("Vote".created_at > $2 OR $2 IS NULL)
|
|
|
|
AND "Vote".boost = false
|
2021-08-17 23:07:52 +00:00
|
|
|
AND "Item"."userId" = $1`, user.id, user.checkedNotesAt)
|
2021-06-24 23:56:01 +00:00
|
|
|
|
2021-06-27 03:09:39 +00:00
|
|
|
await models.user.update({ where: { id: me.id }, data: { checkedNotesAt: new Date() } })
|
2021-06-24 23:56:01 +00:00
|
|
|
return sum || 0
|
2021-05-21 22:32:21 +00:00
|
|
|
}
|
2021-03-25 19:29:24 +00:00
|
|
|
},
|
|
|
|
|
2021-05-22 00:09:11 +00:00
|
|
|
Mutation: {
|
|
|
|
setName: async (parent, { name }, { me, models }) => {
|
|
|
|
if (!me) {
|
|
|
|
throw new AuthenticationError('you must be logged in')
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2021-06-27 03:09:39 +00:00
|
|
|
await models.user.update({ where: { id: me.id }, data: { name } })
|
2021-05-22 00:09:11 +00:00
|
|
|
} catch (error) {
|
|
|
|
if (error.code === 'P2002') {
|
|
|
|
throw new UserInputError('name taken')
|
|
|
|
}
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-03-25 19:29:24 +00:00
|
|
|
User: {
|
2021-04-22 22:14:32 +00:00
|
|
|
nitems: async (user, args, { models }) => {
|
|
|
|
return await models.item.count({ where: { userId: user.id, parentId: null } })
|
|
|
|
},
|
|
|
|
ncomments: async (user, args, { models }) => {
|
|
|
|
return await models.item.count({ where: { userId: user.id, parentId: { not: null } } })
|
|
|
|
},
|
2021-04-27 21:30:58 +00:00
|
|
|
stacked: async (user, args, { models }) => {
|
|
|
|
const [{ sum }] = await models.$queryRaw`
|
|
|
|
SELECT sum("Vote".sats)
|
2021-08-17 23:07:52 +00:00
|
|
|
FROM "Vote"
|
|
|
|
JOIN "Item" on "Vote"."itemId" = "Item".id
|
|
|
|
WHERE "Vote"."userId" <> ${user.id} AND boost = false
|
|
|
|
AND "Item"."userId" = ${user.id}`
|
2021-05-11 15:52:50 +00:00
|
|
|
return sum || 0
|
2021-04-27 21:30:58 +00:00
|
|
|
},
|
2021-05-11 15:52:50 +00:00
|
|
|
sats: async (user, args, { models }) => {
|
|
|
|
return Math.floor(user.msats / 1000)
|
2021-06-24 23:56:01 +00:00
|
|
|
},
|
|
|
|
hasNewNotes: async (user, args, { models }) => {
|
|
|
|
// check if any votes have been cast for them since checkedNotesAt
|
|
|
|
const votes = await models.$queryRaw(`
|
|
|
|
SELECT "Vote".id, "Vote".created_at
|
|
|
|
FROM "Vote"
|
2021-08-17 23:07:52 +00:00
|
|
|
JOIN "Item" on "Vote"."itemId" = "Item".id
|
|
|
|
WHERE "Vote"."userId" <> $1
|
2021-06-24 23:56:01 +00:00
|
|
|
AND ("Vote".created_at > $2 OR $2 IS NULL)
|
|
|
|
AND "Vote".boost = false
|
2021-08-17 23:07:52 +00:00
|
|
|
AND "Item"."userId" = $1
|
2021-06-24 23:56:01 +00:00
|
|
|
LIMIT 1`, user.id, user.checkedNotesAt)
|
|
|
|
if (votes.length > 0) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if they have any replies since checkedNotesAt
|
|
|
|
const newReplies = await models.$queryRaw(`
|
|
|
|
SELECT "Item".id, "Item".created_at
|
|
|
|
From "Item"
|
2021-08-17 23:07:52 +00:00
|
|
|
JOIN "Item" p ON "Item"."parentId" = p.id
|
|
|
|
WHERE p."userId" = $1
|
2021-06-24 23:56:01 +00:00
|
|
|
AND ("Item".created_at > $2 OR $2 IS NULL) AND "Item"."userId" <> $1
|
|
|
|
LIMIT 1`, user.id, user.checkedNotesAt)
|
2021-08-18 23:00:54 +00:00
|
|
|
if (newReplies.length > 0) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if they have any mentions since checkedNotesAt
|
|
|
|
const newMentions = await models.$queryRaw(`
|
|
|
|
SELECT "Item".id, "Item".created_at
|
|
|
|
From "Mention"
|
|
|
|
JOIN "Item" ON "Mention"."itemId" = "Item".id
|
|
|
|
WHERE "Mention"."userId" = $1
|
|
|
|
AND ("Mention".created_at > $2 OR $2 IS NULL)
|
|
|
|
AND "Item"."userId" <> $1
|
|
|
|
LIMIT 1`, user.id, user.checkedNotesAt)
|
|
|
|
return newMentions.length > 0
|
2021-05-11 15:52:50 +00:00
|
|
|
}
|
2021-03-25 19:29:24 +00:00
|
|
|
}
|
|
|
|
}
|