···3636 index("postNowScheduledDatePosted_idx")
3737 .on(table.posted, table.scheduledDate, table.postNow)
3838 .where(sql`posted = 0 and postNow <> 1`),
3939+ // used to lower down the amount of posts that fill up the post table
4040+ index("repostAddOn_idx").on(table.userId, table.cid)
3941]);
40424143export const reposts = sqliteTable('reposts', {
+25-15
src/utils/dbQuery.ts
···248248 }
249249 }
250250251251- // Create the posts
252252- const postUUID = uuidv4();
253253- let dbOperations: BatchItem<"sqlite">[] = [
254254- db.insert(posts).values({
255255- content: `Repost of ${url}`,
256256- uuid: postUUID,
257257- cid: cid,
258258- uri: uri,
259259- posted: true,
260260- isRepost: true,
261261- scheduledDate: scheduleDate,
262262- userId: userId
263263- })
264264- ];
265265-251251+ let postUUID;
252252+ let dbOperations: BatchItem<"sqlite">[] = [];
253253+254254+ // Check to see if the post already exists
255255+ // (check also against the userId here as well to avoid cross account data collisions)
256256+ const existingPost = await db.select({id: posts.uuid}).from(posts).where(and(
257257+ eq(posts.userId, userId), eq(posts.cid, cid))).limit(1).all();
258258+259259+ if (existingPost.length > 1) {
260260+ postUUID = existingPost[0].id;
261261+ } else {
262262+ // Create the post base for this repost
263263+ postUUID = uuidv4();
264264+ dbOperations.push(db.insert(posts).values({
265265+ content: `Repost of ${url}`,
266266+ uuid: postUUID,
267267+ cid: cid,
268268+ uri: uri,
269269+ posted: true,
270270+ isRepost: true,
271271+ scheduledDate: scheduleDate,
272272+ userId: userId
273273+ }));
274274+ }
275275+266276 // Push initial repost
267277 dbOperations.push(db.insert(reposts).values({
268278 uuid: postUUID,