diff --git a/api/resolvers/item.js b/api/resolvers/item.js index bbf62521..40ad59f6 100644 --- a/api/resolvers/item.js +++ b/api/resolvers/item.js @@ -12,7 +12,7 @@ import { msatsToSats } from '../../lib/format' import { parse } from 'tldts' import uu from 'url-unshort' -async function comments (me, models, id, sort) { +async function comments (me, models, id, sort, root) { let orderBy switch (sort) { case 'top': @@ -39,7 +39,7 @@ async function comments (me, models, id, sort) { WHERE true ${await filterClause(me, models)}) SELECT * FROM base ORDER BY sort_path`, Number(id)) - return nestComments(flat, id)[0] + return nestComments(flat, id, root)[0] } export async function getItem (parent, { id }, { me, models }) { @@ -871,7 +871,7 @@ export default { if (item.comments) { return item.comments } - return comments(me, models, item.id, 'hot') + return comments(me, models, item.id, 'hot', item) }, upvotes: async (item, args, { models }) => { const [{ count }] = await models.$queryRaw(` @@ -946,6 +946,9 @@ export default { if (!item.rootId) { return null } + if (item.root) { + return item.root + } return await models.item.findUnique({ where: { id: item.rootId } }) }, parent: async (item, args, { models }) => { @@ -1082,10 +1085,11 @@ const createItem = async (parent, { title, url, text, boost, forward, bounty, pa return item } -function nestComments (flat, parentId) { +function nestComments (flat, parentId, root) { const result = [] let added = 0 for (let i = 0; i < flat.length;) { + flat[i].root = root if (!flat[i].comments) flat[i].comments = [] if (Number(flat[i].parentId) === Number(parentId)) { result.push(flat[i]) @@ -1093,7 +1097,7 @@ function nestComments (flat, parentId) { i++ } else if (result.length > 0) { const item = result[result.length - 1] - const [nested, newAdded] = nestComments(flat.slice(i), item.id) + const [nested, newAdded] = nestComments(flat.slice(i), item.id, root) if (newAdded === 0) { break } diff --git a/fragments/items.js b/fragments/items.js index e6658f34..52ff7c3a 100644 --- a/fragments/items.js +++ b/fragments/items.js @@ -14,11 +14,7 @@ export const ITEM_FIELDS = gql` streak id } - fwdUser { - name - streak - id - } + position sats upvotes boost @@ -45,9 +41,23 @@ export const ITEM_FIELDS = gql` status uploadId mine + }` + +export const ITEM_FULL_FIELDS = gql` + ${ITEM_FIELDS} + fragment ItemFullFields on Item { + ...ItemFields + text + fwdUser { + name + streak + id + } root { id title + bounty + bountyPaidTo sub { name } @@ -87,11 +97,9 @@ export const ITEMS = gql` cursor items { ...ItemFields - position }, pins { ...ItemFields - position } } }` @@ -104,50 +112,45 @@ export const TOP_ITEMS = gql` cursor items { ...ItemFields - position }, pins { ...ItemFields - position } } }` export const OUTLAWED_ITEMS = gql` - ${ITEM_FIELDS} + ${ITEM_FULL_FIELDS} query outlawedItems($cursor: String) { outlawedItems(cursor: $cursor) { cursor items { - ...ItemFields - text + ...ItemFullFields } } }` export const BORDERLAND_ITEMS = gql` - ${ITEM_FIELDS} + ${ITEM_FULL_FIELDS} query borderlandItems($cursor: String) { borderlandItems(cursor: $cursor) { cursor items { - ...ItemFields - text + ...ItemFullFields } } }` export const FREEBIE_ITEMS = gql` - ${ITEM_FIELDS} + ${ITEM_FULL_FIELDS} query freebieItems($cursor: String) { freebieItems(cursor: $cursor) { cursor items { - ...ItemFields - text + ...ItemFullFields } } }` @@ -167,14 +170,13 @@ export const POLL_FIELDS = gql` }` export const ITEM = gql` - ${ITEM_FIELDS} + ${ITEM_FULL_FIELDS} ${POLL_FIELDS} query Item($id: ID!) { item(id: $id) { - ...ItemFields + ...ItemFullFields ...PollFields - text } }` @@ -189,15 +191,13 @@ export const COMMENTS_QUERY = gql` ` export const ITEM_FULL = gql` - ${ITEM_FIELDS} + ${ITEM_FULL_FIELDS} ${POLL_FIELDS} ${COMMENTS} query Item($id: ID!) { item(id: $id) { - ...ItemFields + ...ItemFullFields prior - position - text ...PollFields comments { ...CommentsRecursive @@ -206,11 +206,10 @@ export const ITEM_FULL = gql` }` export const ITEM_WITH_COMMENTS = gql` - ${ITEM_FIELDS} + ${ITEM_FULL_FIELDS} ${COMMENTS} fragment ItemWithComments on Item { - ...ItemFields - text + ...ItemFullFields comments { ...CommentsRecursive } @@ -228,13 +227,12 @@ export const BOUNTY_ITEMS_BY_USER_NAME = gql` }` export const ITEM_SEARCH = gql` - ${ITEM_FIELDS} + ${ITEM_FULL_FIELDS} query Search($q: String, $cursor: String, $sort: String, $what: String, $when: String) { search(q: $q, cursor: $cursor, sort: $sort, what: $what, when: $when) { cursor items { - ...ItemFields - text + ...ItemFullFields searchTitle searchText } diff --git a/fragments/notifications.js b/fragments/notifications.js index 39da59eb..8764304e 100644 --- a/fragments/notifications.js +++ b/fragments/notifications.js @@ -1,9 +1,9 @@ import { gql } from '@apollo/client' -import { ITEM_FIELDS } from './items' +import { ITEM_FULL_FIELDS } from './items' import { INVITE_FIELDS } from './invites' export const NOTIFICATIONS = gql` - ${ITEM_FIELDS} + ${ITEM_FULL_FIELDS} ${INVITE_FIELDS} query Notifications($cursor: String, $inc: String) { @@ -16,7 +16,7 @@ export const NOTIFICATIONS = gql` sortTime mention item { - ...ItemFields + ...ItemFullFields text } } @@ -24,7 +24,7 @@ export const NOTIFICATIONS = gql` sortTime earnedSats item { - ...ItemFields + ...ItemFullFields text } } @@ -48,7 +48,7 @@ export const NOTIFICATIONS = gql` ... on Reply { sortTime item { - ...ItemFields + ...ItemFullFields text } } diff --git a/fragments/users.js b/fragments/users.js index 0dca45e6..f7366e06 100644 --- a/fragments/users.js +++ b/fragments/users.js @@ -189,11 +189,9 @@ export const USER_WITH_POSTS = gql` cursor items { ...ItemFields - position } pins { ...ItemFields - position } } }` diff --git a/fragments/wallet.js b/fragments/wallet.js index 93a5b57e..c9d29066 100644 --- a/fragments/wallet.js +++ b/fragments/wallet.js @@ -1,5 +1,5 @@ import { gql } from '@apollo/client' -import { ITEM_FIELDS } from './items' +import { ITEM_FULL_FIELDS } from './items' import { USER_FIELDS } from './users' export const INVOICE = gql` @@ -27,7 +27,7 @@ export const WITHDRAWL = gql` }` export const WALLET_HISTORY = gql` - ${ITEM_FIELDS} + ${ITEM_FULL_FIELDS} ${USER_FIELDS} query WalletHistory($cursor: String, $inc: String) { @@ -46,8 +46,7 @@ export const WALLET_HISTORY = gql` type description item { - ...ItemFields - text + ...ItemFullFields } } cursor