Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f219eba

Browse files
committedFeb 20, 2025·
Prisma-handled history; prevent history also on jobs and special items
1 parent 0a6bcb6 commit f219eba

File tree

7 files changed

+25
-75
lines changed

7 files changed

+25
-75
lines changed
 

‎api/paidAction/itemUpdate.js

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PAID_ACTION_PAYMENT_METHODS, USER_ID } from '@/lib/constants'
1+
import { PAID_ACTION_PAYMENT_METHODS, USER_ID, ADMIN_ITEMS } from '@/lib/constants'
22
import { uploadFees } from '../resolvers/upload'
33
import { getItemMentions, getMentions, performBotBehavior } from './lib/item'
44
import { notifyItemMention, notifyMention } from '@/lib/webPush'
@@ -65,6 +65,28 @@ export async function perform (args, context) {
6565
data: { paid: true }
6666
})
6767

68+
// history tracking
69+
// has to be older than 10 minutes, not a bio, not a job, not deleted, not a special item.
70+
const adminItem = ADMIN_ITEMS.includes(old.id)
71+
if (old.createdAt < new Date(Date.now() - 10 * 60 * 1000) && !old.bio && old.subName !== 'jobs' && !data.deletedAt && !adminItem) {
72+
await tx.oldItem.create({
73+
data: {
74+
title: old.title,
75+
text: old.text,
76+
url: old.url,
77+
userId: old.userId,
78+
subName: old.subName,
79+
imgproxyUrls: old.imgproxyUrls,
80+
cloneBornAt: old.cloneBornAt,
81+
cloneDiedAt: new Date(),
82+
originalItemId: parseInt(id)
83+
}
84+
})
85+
86+
data.cloneBornAt = new Date()
87+
data.cloneDiedAt = null
88+
}
89+
6890
// we put boost in the where clause because we don't want to update the boost
6991
// if it has changed concurrently
7092
await tx.item.update({

‎api/typeDefs/item.js

-2
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,6 @@ export default gql`
102102
imgproxyUrls: JSONObject
103103
cloneBornAt: Date
104104
cloneDiedAt: Date
105-
uploadId: Int
106-
pollCost: Int
107105
deletedAt: Date
108106
originalItemId: Int
109107
}

‎components/item-history.js

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ export default function HistoryDropdown ({ item }) {
3434
<hr className='dropdown-divider' />
3535
<Dropdown.Item
3636
title={lastEdited}
37-
// onClick={handleLastEdit}
3837
>
3938
edited {timeSince(lastEdited)} ago (most recent)
4039
</Dropdown.Item>

‎components/item-info.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export default function ItemInfo ({
146146
yesterday
147147
</Link>
148148
</>}
149-
{item.oldVersions?.length > 0 && !item.deletedAt &&
149+
{item.oldVersions?.length > 0 && !item.deletedAt && // bios, jobs and admin items are not tracked
150150
<>
151151
<span> </span>
152152
<HistoryDropdown item={item} />
@@ -225,7 +225,7 @@ export default function ItemInfo ({
225225
<hr className='dropdown-divider' />
226226
<PinSubDropdownItem item={item} />
227227
</>}
228-
{item.mine && sub && !item.deletedAt && !item.bio && // has to have a sub for edit page
228+
{item.mine && !item.deletedAt && sub && // has to have a sub for edit page
229229
<>
230230
<hr className='dropdown-divider' />
231231
<Dropdown.Item onClick={() => !item.parentId ? router.push(`/items/${item.id}/edit`) : toggleShadowEdit(true)} className='text-reset dropdown-item'>

‎fragments/items.js

-2
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,6 @@ export const ITEM_FIELDS = gql`
8787
imgproxyUrls
8888
cloneBornAt
8989
cloneDiedAt
90-
uploadId
91-
pollCost
9290
deletedAt
9391
originalItemId
9492
}

‎prisma/migrations/20250217233746_perpetual_edit/migration.sql

-65
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ CREATE TABLE "OldItem" (
1919
"imgproxyUrls" JSONB,
2020
"cloneBornAt" TIMESTAMP(3),
2121
"cloneDiedAt" TIMESTAMP(3),
22-
"uploadId" INTEGER,
23-
"pollCost" INTEGER,
2422
"deletedAt" TIMESTAMP(3),
2523
"original_itemId" INTEGER NOT NULL,
2624

@@ -38,66 +36,3 @@ CREATE INDEX "OldItem_original_itemId_idx" ON "OldItem"("original_itemId");
3836

3937
-- AddForeignKey
4038
ALTER TABLE "OldItem" ADD CONSTRAINT "OldItem_original_itemId_fkey" FOREIGN KEY ("original_itemId") REFERENCES "Item"("id") ON DELETE CASCADE ON UPDATE CASCADE;
41-
42-
CREATE OR REPLACE FUNCTION before_item_update()
43-
RETURNS TRIGGER AS $$
44-
BEGIN
45-
-- history shall be written only if the item is older than 10 minutes and content has changed
46-
IF (OLD."created_at" < now() - interval '10 minutes')
47-
AND OLD."bio" IS FALSE
48-
AND NEW."deletedAt" IS NULL
49-
AND (OLD."text" != NEW."text" OR OLD."title" != NEW."title" OR OLD."url" != NEW."url")
50-
THEN
51-
-- TODO honestly find a better way to do this, I mean this works but it's bad
52-
INSERT INTO "OldItem" (
53-
"created_at",
54-
"updated_at",
55-
"title",
56-
"text",
57-
"url",
58-
"userId",
59-
"subName",
60-
"imgproxyUrls",
61-
"cloneBornAt",
62-
"cloneDiedAt",
63-
"uploadId",
64-
"pollCost",
65-
"deletedAt",
66-
"original_itemId"
67-
)
68-
VALUES (
69-
OLD."created_at",
70-
OLD."updated_at",
71-
OLD."title",
72-
OLD."text",
73-
OLD."url",
74-
OLD."userId",
75-
OLD."subName",
76-
OLD."imgproxyUrls",
77-
OLD."cloneBornAt",
78-
OLD."cloneDiedAt",
79-
OLD."uploadId",
80-
OLD."pollCost",
81-
OLD."deletedAt",
82-
OLD."id"
83-
);
84-
85-
-- item shall die
86-
UPDATE "OldItem"
87-
SET "cloneDiedAt" = now()
88-
WHERE "original_itemId" = OLD.id
89-
AND "cloneDiedAt" IS NULL;
90-
91-
-- to be born again
92-
NEW."cloneBornAt" = now();
93-
NEW."cloneDiedAt" = NULL;
94-
END IF;
95-
96-
RETURN NEW;
97-
END;
98-
$$ LANGUAGE plpgsql;
99-
100-
CREATE TRIGGER item_history_trigger
101-
BEFORE UPDATE ON "Item"
102-
FOR EACH ROW
103-
EXECUTE PROCEDURE before_item_update();

‎prisma/schema.prisma

-2
Original file line numberDiff line numberDiff line change
@@ -618,8 +618,6 @@ model OldItem {
618618
imgproxyUrls Json?
619619
cloneBornAt DateTime?
620620
cloneDiedAt DateTime?
621-
uploadId Int?
622-
pollCost Int?
623621
deletedAt DateTime?
624622
originalItemId Int @map("original_itemId")
625623
originalItem Item @relation(fields: [originalItemId], references: [id], onDelete: Cascade)

0 commit comments

Comments
 (0)
Please sign in to comment.