Add thread subscriptions (#293)
* Add thread subscriptions * remove dead code: reply only notifications * break out thread subscription queries to reduce search space * one db dip for item lists/threads re:meSubscription --------- Co-authored-by: ekzyis <ek@stacker.news> Co-authored-by: keyan <keyan.kousha+huumn@gmail.com>
This commit is contained in:
parent
e97509eea7
commit
0c251ca376
|
@ -173,12 +173,14 @@ async function itemQueryWithMeta ({ me, models, query, orderBy = '' }, ...args)
|
|||
} else {
|
||||
return await models.$queryRaw(`
|
||||
SELECT "Item".*, to_json(users.*) as user, COALESCE("ItemAct"."meMsats", 0) as "meMsats",
|
||||
COALESCE("ItemAct"."meDontLike", false) as "meDontLike", "Bookmark"."itemId" IS NOT NULL AS "meBookmark"
|
||||
COALESCE("ItemAct"."meDontLike", false) as "meDontLike", "Bookmark"."itemId" IS NOT NULL AS "meBookmark",
|
||||
"ThreadSubscription"."itemId" IS NOT NULL AS "meSubscription"
|
||||
FROM (
|
||||
${query}
|
||||
) "Item"
|
||||
JOIN users ON "Item"."userId" = users.id
|
||||
LEFT JOIN "Bookmark" ON "Bookmark"."itemId" = "Item".id AND "Bookmark"."userId" = ${me.id}
|
||||
LEFT JOIN "ThreadSubscription" ON "ThreadSubscription"."itemId" = "Item".id AND "ThreadSubscription"."userId" = ${me.id}
|
||||
LEFT JOIN LATERAL (
|
||||
SELECT "itemId", sum("ItemAct".msats) FILTER (WHERE act = 'FEE' OR act = 'TIP') AS "meMsats",
|
||||
bool_or(act = 'DONT_LIKE_THIS') AS "meDontLike"
|
||||
|
@ -719,6 +721,14 @@ export default {
|
|||
} else await models.bookmark.create({ data })
|
||||
return { id }
|
||||
},
|
||||
subscribeItem: async (parent, { id }, { me, models }) => {
|
||||
const data = { itemId: Number(id), userId: me.id }
|
||||
const old = await models.threadSubscription.findUnique({ where: { userId_itemId: data } })
|
||||
if (old) {
|
||||
await models.threadSubscription.delete({ where: { userId_itemId: data } })
|
||||
} else await models.threadSubscription.create({ data })
|
||||
return { id }
|
||||
},
|
||||
deleteItem: async (parent, { id }, { me, models }) => {
|
||||
const old = await models.item.findUnique({ where: { id: Number(id) } })
|
||||
if (Number(old.userId) !== Number(me?.id)) {
|
||||
|
@ -1067,6 +1077,21 @@ export default {
|
|||
|
||||
return !!bookmark
|
||||
},
|
||||
meSubscription: async (item, args, { me, models }) => {
|
||||
if (!me) return false
|
||||
if (typeof item.meSubscription === 'boolean') return item.meSubscription
|
||||
|
||||
const subscription = await models.threadSubscription.findUnique({
|
||||
where: {
|
||||
userId_itemId: {
|
||||
itemId: Number(item.id),
|
||||
userId: me.id
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return !!subscription
|
||||
},
|
||||
outlawed: async (item, args, { me, models }) => {
|
||||
if (me && Number(item.userId) === Number(me.id)) {
|
||||
return false
|
||||
|
|
|
@ -69,23 +69,26 @@ export default {
|
|||
|
||||
const queries = []
|
||||
|
||||
if (inc === 'replies') {
|
||||
queries.push(
|
||||
`SELECT DISTINCT "Item".id::TEXT, "Item".created_at AS "sortTime", NULL::BIGINT as "earnedSats",
|
||||
'Reply' AS type
|
||||
FROM "Item"
|
||||
JOIN "Item" p ON ${meFull.noteAllDescendants ? '"Item".path <@ p.path' : '"Item"."parentId" = p.id'}
|
||||
WHERE p."userId" = $1
|
||||
AND "Item"."userId" <> $1 AND "Item".created_at <= $2
|
||||
${await filterClause(me, models)}`
|
||||
)
|
||||
} else {
|
||||
queries.push(
|
||||
`(SELECT DISTINCT "Item".id::TEXT, "Item".created_at AS "sortTime", NULL::BIGINT as "earnedSats",
|
||||
'Reply' AS type
|
||||
FROM "Item"
|
||||
JOIN "Item" p ON ${meFull.noteAllDescendants ? '"Item".path <@ p.path' : '"Item"."parentId" = p.id'}
|
||||
WHERE p."userId" = $1
|
||||
WHERE p."userId" = $1 AND "Item"."userId" <> $1 AND "Item".created_at <= $2
|
||||
${await filterClause(me, models)}
|
||||
ORDER BY "sortTime" DESC
|
||||
LIMIT ${LIMIT}+$3)`
|
||||
)
|
||||
|
||||
// break out thread subscription to decrease the search space of the already expensive reply query
|
||||
queries.push(
|
||||
`(SELECT DISTINCT "Item".id::TEXT, "Item".created_at AS "sortTime", NULL::BIGINT as "earnedSats",
|
||||
'Reply' AS type
|
||||
FROM "ThreadSubscription"
|
||||
JOIN "Item" p ON "ThreadSubscription"."itemId" = p.id
|
||||
JOIN "Item" ON ${meFull.noteAllDescendants ? '"Item".path <@ p.path' : '"Item"."parentId" = p.id'}
|
||||
WHERE
|
||||
"ThreadSubscription"."userId" = $1
|
||||
AND "Item"."userId" <> $1 AND "Item".created_at <= $2
|
||||
${await filterClause(me, models)}
|
||||
ORDER BY "sortTime" DESC
|
||||
|
@ -190,7 +193,6 @@ export default {
|
|||
AND updated_at <= $2`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// we do all this crazy subquery stuff to make 'reward' islands
|
||||
const notifications = await models.$queryRaw(
|
||||
|
|
|
@ -291,8 +291,8 @@ export default {
|
|||
JOIN "Item" p ON
|
||||
"Item".created_at >= p.created_at
|
||||
AND ${user.noteAllDescendants ? '"Item".path <@ p.path' : '"Item"."parentId" = p.id'}
|
||||
AND "Item"."userId" <> p."userId"
|
||||
WHERE p."userId" = $1
|
||||
AND "Item"."userId" <> $1
|
||||
WHERE (p."userId" = $1 OR p.id = ANY(SELECT "itemId" FROM "ThreadSubscription" WHERE "userId" = $1))
|
||||
AND "Item".created_at > $2::timestamp(3) without time zone
|
||||
${await filterClause(me, models)}
|
||||
LIMIT 1`, me.id, lastChecked)
|
||||
|
@ -300,6 +300,21 @@ export default {
|
|||
return true
|
||||
}
|
||||
|
||||
// break out thread subscription to decrease the search space of the already expensive reply query
|
||||
const newtsubs = await models.$queryRaw(`
|
||||
SELECT 1
|
||||
FROM "ThreadSubscription"
|
||||
JOIN "Item" p ON "ThreadSubscription"."itemId" = p.id
|
||||
JOIN "Item" ON ${user.noteAllDescendants ? '"Item".path <@ p.path' : '"Item"."parentId" = p.id'}
|
||||
WHERE
|
||||
"ThreadSubscription"."userId" = $1
|
||||
AND "Item".created_at > $2::timestamp(3) without time zone
|
||||
${await filterClause(me, models)}
|
||||
LIMIT 1`, me.id, lastChecked)
|
||||
if (newtsubs.length > 0) {
|
||||
return true
|
||||
}
|
||||
|
||||
// check if they have any mentions since checkedNotesAt
|
||||
if (user.noteMentions) {
|
||||
const newMentions = await models.$queryRaw(`
|
||||
|
|
|
@ -34,6 +34,7 @@ export default gql`
|
|||
|
||||
extend type Mutation {
|
||||
bookmarkItem(id: ID): Item
|
||||
subscribeItem(id: ID): Item
|
||||
deleteItem(id: ID): Item
|
||||
upsertLink(id: ID, sub: String, title: String!, url: String!, boost: Int, forward: String): Item!
|
||||
upsertDiscussion(id: ID, sub: String, title: String!, text: String, boost: Int, forward: String): Item!
|
||||
|
@ -102,6 +103,7 @@ export default gql`
|
|||
meSats: Int!
|
||||
meDontLike: Boolean!
|
||||
meBookmark: Boolean!
|
||||
meSubscription: Boolean!
|
||||
outlawed: Boolean!
|
||||
freebie: Boolean!
|
||||
paidImgLink: Boolean
|
||||
|
|
|
@ -13,6 +13,7 @@ import { useMe } from './me'
|
|||
import MoreIcon from '../svgs/more-fill.svg'
|
||||
import DontLikeThisDropdownItem from './dont-link-this'
|
||||
import BookmarkDropdownItem from './bookmark'
|
||||
import SubscribeDropdownItem from './subscribe'
|
||||
import { CopyLinkDropdownItem } from './share'
|
||||
|
||||
export default function ItemInfo ({ item, full, commentsText, className, embellishUser, extraInfo, onEdit, editText }) {
|
||||
|
@ -98,6 +99,7 @@ export default function ItemInfo ({ item, full, commentsText, className, embelli
|
|||
<ItemDropdown>
|
||||
<CopyLinkDropdownItem item={item} />
|
||||
{me && <BookmarkDropdownItem item={item} />}
|
||||
{me && item.user.id !== me.id && <SubscribeDropdownItem item={item} />}
|
||||
{item.otsHash &&
|
||||
<Dropdown.Item>
|
||||
<Link passHref href={`/items/${item.id}/ots`}>
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
import { useMutation } from '@apollo/client'
|
||||
import { gql } from 'apollo-server-micro'
|
||||
import { Dropdown } from 'react-bootstrap'
|
||||
|
||||
export default function SubscribeDropdownItem ({ item: { id, meSubscription } }) {
|
||||
const [subscribeItem] = useMutation(
|
||||
gql`
|
||||
mutation subscribeItem($id: ID!) {
|
||||
subscribeItem(id: $id) {
|
||||
meSubscription
|
||||
}
|
||||
}`, {
|
||||
update (cache, { data: { subscribeItem } }) {
|
||||
cache.modify({
|
||||
id: `Item:${id}`,
|
||||
fields: {
|
||||
meSubscription: () => subscribeItem.meSubscription
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
)
|
||||
return (
|
||||
<Dropdown.Item
|
||||
onClick={() => subscribeItem({ variables: { id } })}
|
||||
>
|
||||
{meSubscription ? 'remove subscription' : 'subscribe'}
|
||||
</Dropdown.Item>
|
||||
)
|
||||
}
|
|
@ -19,6 +19,7 @@ export const COMMENT_FIELDS = gql`
|
|||
meSats
|
||||
meDontLike
|
||||
meBookmark
|
||||
meSubscription
|
||||
outlawed
|
||||
freebie
|
||||
path
|
||||
|
|
|
@ -27,6 +27,7 @@ export const ITEM_FIELDS = gql`
|
|||
meSats
|
||||
meDontLike
|
||||
meBookmark
|
||||
meSubscription
|
||||
outlawed
|
||||
freebie
|
||||
ncomments
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
-- CreateTable
|
||||
CREATE TABLE "ThreadSubscription" (
|
||||
"userId" INTEGER NOT NULL,
|
||||
"itemId" INTEGER NOT NULL,
|
||||
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
PRIMARY KEY ("userId","itemId")
|
||||
);
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "ThreadSubscription" ADD FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "ThreadSubscription" ADD FOREIGN KEY ("itemId") REFERENCES "Item"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "ThreadSubscription.created_at_index" ON "ThreadSubscription"("created_at");
|
|
@ -0,0 +1,38 @@
|
|||
CREATE OR REPLACE FUNCTION item_comments_with_me(_item_id int, _me_id int, _level int, _where text, _order_by text)
|
||||
RETURNS jsonb
|
||||
LANGUAGE plpgsql STABLE PARALLEL SAFE AS
|
||||
$$
|
||||
DECLARE
|
||||
result jsonb;
|
||||
BEGIN
|
||||
IF _level < 1 THEN
|
||||
RETURN '[]'::jsonb;
|
||||
END IF;
|
||||
|
||||
EXECUTE ''
|
||||
|| 'SELECT COALESCE(jsonb_agg(sub), ''[]''::jsonb) AS comments '
|
||||
|| 'FROM ( '
|
||||
|| ' SELECT "Item".*, "Item".created_at at time zone ''UTC'' AS "createdAt", "Item".updated_at at time zone ''UTC'' AS "updatedAt", '
|
||||
|| ' item_comments_with_me("Item".id, $5, $2 - 1, $3, $4) AS comments, to_jsonb(users.*) as user, '
|
||||
|| ' COALESCE("ItemAct"."meMsats", 0) AS "meMsats", COALESCE("ItemAct"."meDontLike", false) AS "meDontLike", '
|
||||
|| ' "Bookmark"."itemId" IS NOT NULL AS "meBookmark", "ThreadSubscription"."itemId" IS NOT NULL AS "meSubscription" '
|
||||
|| ' FROM "Item" p '
|
||||
|| ' JOIN "Item" ON "Item"."parentId" = p.id '
|
||||
|| ' JOIN users ON users.id = "Item"."userId" '
|
||||
|| ' LEFT JOIN "Bookmark" ON "Bookmark"."itemId" = "Item".id AND "Bookmark"."userId" = $5 '
|
||||
|| ' LEFT JOIN "ThreadSubscription" ON "ThreadSubscription"."itemId" = "Item".id AND "ThreadSubscription"."userId" = $5 '
|
||||
|| ' LEFT JOIN LATERAL ( '
|
||||
|| ' SELECT "itemId", sum("ItemAct".msats) FILTER (WHERE act = ''FEE'' OR act = ''TIP'') AS "meMsats", '
|
||||
|| ' bool_or(act = ''DONT_LIKE_THIS'') AS "meDontLike" '
|
||||
|| ' FROM "ItemAct" '
|
||||
|| ' WHERE "ItemAct"."userId" = $5 '
|
||||
|| ' AND "ItemAct"."itemId" = "Item".id '
|
||||
|| ' GROUP BY "ItemAct"."itemId" '
|
||||
|| ' ) "ItemAct" ON true '
|
||||
|| ' WHERE p.id = $1 ' || _where || ' '
|
||||
|| _order_by
|
||||
|| ' ) sub'
|
||||
INTO result USING _item_id, _level, _where, _order_by, _me_id;
|
||||
RETURN result;
|
||||
END
|
||||
$$;
|
|
@ -93,6 +93,7 @@ model User {
|
|||
Streak Streak[]
|
||||
Bookmarks Bookmark[]
|
||||
Subscriptions Subscription[]
|
||||
ThreadSubscriptions ThreadSubscription[]
|
||||
|
||||
@@index([createdAt])
|
||||
@@index([inviteId])
|
||||
|
@ -304,6 +305,7 @@ model Item {
|
|||
PollOption PollOption[]
|
||||
PollVote PollVote[]
|
||||
Bookmark Bookmark[]
|
||||
ThreadSubscription ThreadSubscription[]
|
||||
|
||||
@@index([weightedVotes])
|
||||
@@index([weightedDownVotes])
|
||||
|
@ -561,3 +563,14 @@ model Bookmark {
|
|||
@@id([userId, itemId])
|
||||
@@index([createdAt])
|
||||
}
|
||||
|
||||
model ThreadSubscription {
|
||||
user User @relation(fields: [userId], references: [id])
|
||||
userId Int
|
||||
item Item @relation(fields: [itemId], references: [id])
|
||||
itemId Int
|
||||
createdAt DateTime @default(now()) @map(name: "created_at")
|
||||
|
||||
@@id([userId, itemId])
|
||||
@@index([createdAt])
|
||||
}
|
Loading…
Reference in New Issue