Skip to content

Commit b97ca2e

Browse files
committed
refactor: remove isScheduled field, use only scheduledAt
Database Schema Changes - Removed isScheduled boolean field from Item model - Kept only scheduledAt timestamp field - Removed unnecessary indexes for isScheduled - Updated stored functions to use scheduledAt IS NULL instead of isScheduled = false API Changes - Updated scheduledOrMine() function to check scheduledAt IS NULL - Updated scheduled items query to use scheduledAt IS NOT NULL - Updated mutations to only set/unset scheduledAt field - Kept isScheduled as computed GraphQL field that returns !!item.scheduledAt Item Creation Logic - Removed isScheduled variable and logic - Only set scheduledAt field during item creation and updates Worker Functions - Updated to check !item.scheduledAt instead of !item.isScheduled - Only update scheduledAt field when publishing/canceling The refactoring maintains the same functionality while eliminating the redundant boolean field. The isScheduled GraphQL field remains available for frontend convenience as a computed property.
1 parent 9ffc455 commit b97ca2e

File tree

6 files changed

+16
-28
lines changed

6 files changed

+16
-28
lines changed

api/paidAction/itemCreate.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ export async function perform (args, context) {
9999

100100
// Check if this is a scheduled post
101101
const scheduleAt = getScheduleAt(data.text)
102-
const isScheduled = !!scheduleAt
103102

104103
// start with median vote
105104
if (me) {
@@ -117,7 +116,6 @@ export async function perform (args, context) {
117116
...data,
118117
...invoiceData,
119118
boost,
120-
isScheduled,
121119
scheduledAt: scheduleAt,
122120
threadSubscriptions: {
123121
createMany: {

api/paidAction/lib/item.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,10 @@ export async function performBotBehavior ({ text, id }, { me, tx }) {
9696
// For new items, scheduling info is set during creation
9797
// For updates, we need to update the item
9898
const existingItem = await tx.item.findUnique({ where: { id: Number(id) } })
99-
if (existingItem && !existingItem.isScheduled) {
99+
if (existingItem && !existingItem.scheduledAt) {
100100
await tx.item.update({
101101
where: { id: Number(id) },
102102
data: {
103-
isScheduled: true,
104103
scheduledAt: scheduleAt
105104
}
106105
})

api/resolvers/item.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@ export const muteClause = me =>
260260

261261
export const scheduledOrMine = (me) => {
262262
return me
263-
? `("Item"."isScheduled" = false OR "Item"."userId" = ${me.id})`
264-
: '"Item"."isScheduled" = false'
263+
? `("Item"."scheduledAt" IS NULL OR "Item"."userId" = ${me.id})`
264+
: '"Item"."scheduledAt" IS NULL'
265265
}
266266

267267
const HIDE_NSFW_CLAUSE = '("Sub"."nsfw" = FALSE OR "Sub"."nsfw" IS NULL)'
@@ -764,7 +764,7 @@ export default {
764764
${SELECT}, trim(both ' ' from
765765
coalesce(ltree2text(subpath("path", 0, -1)), '')) AS "ancestorTitles"
766766
FROM "Item"
767-
WHERE "userId" = $1 AND "isScheduled" = true AND "deletedAt" IS NULL
767+
WHERE "userId" = $1 AND "scheduledAt" IS NOT NULL AND "deletedAt" IS NULL
768768
AND ("invoiceActionState" IS NULL OR "invoiceActionState" = 'PAID')
769769
AND created_at <= $2::timestamp
770770
ORDER BY "scheduledAt" ASC
@@ -1112,7 +1112,7 @@ export default {
11121112
throw new GqlInputError('item does not belong to you')
11131113
}
11141114

1115-
if (!item.isScheduled) {
1115+
if (!item.scheduledAt) {
11161116
throw new GqlInputError('item is not scheduled')
11171117
}
11181118

@@ -1127,7 +1127,6 @@ export default {
11271127
return await models.item.update({
11281128
where: { id: Number(id) },
11291129
data: {
1130-
isScheduled: false,
11311130
scheduledAt: null
11321131
}
11331132
})
@@ -1149,7 +1148,7 @@ export default {
11491148
throw new GqlInputError('item does not belong to you')
11501149
}
11511150

1152-
if (!item.isScheduled) {
1151+
if (!item.scheduledAt) {
11531152
throw new GqlInputError('item is not scheduled')
11541153
}
11551154

@@ -1166,7 +1165,6 @@ export default {
11661165
const updatedItem = await models.item.update({
11671166
where: { id: Number(id) },
11681167
data: {
1169-
isScheduled: false,
11701168
scheduledAt: null,
11711169
createdAt: publishTime,
11721170
updatedAt: publishTime
@@ -1561,7 +1559,7 @@ export default {
15611559
return item.scheduledAt
15621560
},
15631561
isScheduled: async (item, args, { me, models }) => {
1564-
return !!item.isScheduled
1562+
return !!item.scheduledAt
15651563
}
15661564
}
15671565
}

prisma/migrations/20250611000000_scheduled_posts/migration.sql

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
-- Add fields for scheduled posts to the Item model
1+
-- Add field for scheduled posts to the Item model
22
ALTER TABLE "Item" ADD COLUMN "scheduledAt" TIMESTAMP(3);
3-
ALTER TABLE "Item" ADD COLUMN "isScheduled" BOOLEAN NOT NULL DEFAULT FALSE;
43

5-
-- Create indexes for efficient querying of scheduled posts
4+
-- Create index for efficient querying of scheduled posts
65
CREATE INDEX "Item_scheduledAt_idx" ON "Item"("scheduledAt");
7-
CREATE INDEX "Item_isScheduled_idx" ON "Item"("isScheduled");
8-
CREATE INDEX "Item_isScheduled_scheduledAt_idx" ON "Item"("isScheduled", "scheduledAt");
96

107
-- Update stored functions to filter out scheduled posts from comments for non-owners
118

@@ -28,7 +25,7 @@ BEGIN
2825
|| ' FROM "Item" '
2926
|| ' LEFT JOIN hot_score_view g(id, "hotScore", "subHotScore") ON g.id = "Item".id '
3027
|| ' WHERE "Item"."parentId" = $1 '
31-
|| ' AND ("Item"."isScheduled" = false OR "Item"."userId" = $3) '
28+
|| ' AND ("Item"."scheduledAt" IS NULL OR "Item"."userId" = $3) '
3229
|| _order_by || ' '
3330
|| ' LIMIT $4 '
3431
|| ' OFFSET $5) '
@@ -108,7 +105,7 @@ BEGIN
108105
|| ' FROM "Item" '
109106
|| ' LEFT JOIN hot_score_view g(id, "hotScore", "subHotScore") ON g.id = "Item".id '
110107
|| ' WHERE "Item"."parentId" = $1 '
111-
|| ' AND ("Item"."isScheduled" = false OR "Item"."userId" = $3) '
108+
|| ' AND ("Item"."scheduledAt" IS NULL OR "Item"."userId" = $3) '
112109
|| _order_by || ') '
113110
|| ' UNION ALL '
114111
|| ' (SELECT "Item".*, b.level + 1 '
@@ -185,14 +182,14 @@ BEGIN
185182
|| ' (SELECT "Item".*, 1 as level '
186183
|| ' FROM "Item" '
187184
|| ' WHERE "Item"."parentId" = $1 '
188-
|| ' AND "Item"."isScheduled" = false '
185+
|| ' AND "Item"."scheduledAt" IS NULL '
189186
|| _order_by || ') '
190187
|| ' UNION ALL '
191188
|| ' (SELECT "Item".*, b.level + 1 '
192189
|| ' FROM "Item" '
193190
|| ' JOIN base b ON "Item"."parentId" = b.id '
194191
|| ' WHERE b.level < $2 '
195-
|| ' AND "Item"."isScheduled" = false) '
192+
|| ' AND "Item"."scheduledAt" IS NULL) '
196193
|| ') '
197194
|| 'SELECT "Item".*, '
198195
|| ' "Item".created_at at time zone ''UTC'' AS "createdAt", '
@@ -237,7 +234,7 @@ BEGIN
237234
|| ' (SELECT "Item".*, 1 as level, ROW_NUMBER() OVER () as rn '
238235
|| ' FROM "Item" '
239236
|| ' WHERE "Item"."parentId" = $1 '
240-
|| ' AND "Item"."isScheduled" = false '
237+
|| ' AND "Item"."scheduledAt" IS NULL '
241238
|| _order_by || ' '
242239
|| ' LIMIT $2 '
243240
|| ' OFFSET $3) '
@@ -246,7 +243,7 @@ BEGIN
246243
|| ' FROM "Item" '
247244
|| ' JOIN base b ON "Item"."parentId" = b.id '
248245
|| ' WHERE b.level < $5 AND (b.level = 1 OR b.rn <= $4) '
249-
|| ' AND "Item"."isScheduled" = false) '
246+
|| ' AND "Item"."scheduledAt" IS NULL) '
250247
|| ') '
251248
|| 'SELECT "Item".*, '
252249
|| ' "Item".created_at at time zone ''UTC'' AS "createdAt", '

prisma/schema.prisma

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,6 @@ model Item {
564564
freebie Boolean @default(false)
565565
deletedAt DateTime?
566566
scheduledAt DateTime?
567-
isScheduled Boolean @default(false)
568567
otsFile Bytes?
569568
otsHash String?
570569
imgproxyUrls Json?
@@ -632,8 +631,6 @@ model Item {
632631
@@index([boost])
633632
@@index([invoicePaidAt])
634633
@@index([scheduledAt])
635-
@@index([isScheduled])
636-
@@index([isScheduled, scheduledAt])
637634
}
638635

639636
// we use this to denormalize a user's aggregated interactions (zaps) with an item

worker/publishScheduledPosts.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export async function publishScheduledPost ({ data: { itemId }, models, lnd }) {
1212
}
1313
})
1414

15-
if (!item || !item.isScheduled || item.deletedAt) {
15+
if (!item || !item.scheduledAt || item.deletedAt) {
1616
console.log('item not found, not scheduled, or deleted', itemId)
1717
return
1818
}
@@ -23,7 +23,6 @@ export async function publishScheduledPost ({ data: { itemId }, models, lnd }) {
2323
await models.item.update({
2424
where: { id: itemId },
2525
data: {
26-
isScheduled: false,
2726
scheduledAt: null,
2827
createdAt: publishTime,
2928
updatedAt: publishTime

0 commit comments

Comments
 (0)