Skip to content
This repository has been archived by the owner on Aug 9, 2024. It is now read-only.

[Issue] Lynts with replies cannot be deleted. #91

Open
FoxTale-Labs opened this issue Aug 7, 2024 · 5 comments
Open

[Issue] Lynts with replies cannot be deleted. #91

FoxTale-Labs opened this issue Aug 7, 2024 · 5 comments

Comments

@FoxTale-Labs
Copy link

A lynt with replies cannot be deleted and it'll error out with "An unknown error occured: 500"

@GStudiosX2
Copy link
Contributor

A lynt with replies cannot be deleted and it'll error out with "An unknown error occured: 500"

I remember someone talking about this in the discord before I haven't seen it for myself

@Puyodead1
Copy link

same issue (ignore the content lmao)

firefox_LsSSbbbWai.mp4

@NobreHD
Copy link
Contributor

NobreHD commented Aug 9, 2024

The way that deleting posts works is kinda dumb

From src/api/utils.ts

export async function deleteLynt(lyntId: string) {
	await db.transaction(async (trx) => {
		// Get all comments under this lynt
		const comments = await trx.select({ id: lynts.id }).from(lynts).where(eq(lynts.parent, lyntId));

		const commentIds = comments.map((comment) => comment.id);
		const allIds = [lyntId, ...commentIds];

		// Delete likes associated with the comments and the original lynt
		await trx.delete(likes).where(inArray(likes.lynt_id, allIds));

		// Delete notifications associated with the comments and the original lynt
		await trx.delete(notifications).where(inArray(notifications.lyntId, allIds));

		// Delete history entries associated with the comments and the original lynt
		await trx.delete(history).where(inArray(history.lynt_id, allIds));

		// Delete all comments under this lynt
		await trx.delete(lynts).where(and(eq(lynts.parent, lyntId), eq(lynts.reposted, false)));

		// Update reposts of this lynt
		await trx
			.update(lynts)
			.set({
				content: sql`${lynts.content} || '\nThe Lynt this user is reposting has been since deleted.'`,
				parent: null
			})
			.where(and(eq(lynts.parent, lyntId), eq(lynts.reposted, true)));

		// Delete the original lynt
		await trx.delete(lynts).where(eq(lynts.id, lyntId));
	});
}

All this code could have been replaced with a few cascade delete, I'm pretty sure

@GStudiosX2
Copy link
Contributor

The way that deleting posts works is kinda dumb

From src/api/utils.ts

export async function deleteLynt(lyntId: string) {
	await db.transaction(async (trx) => {
		// Get all comments under this lynt
		const comments = await trx.select({ id: lynts.id }).from(lynts).where(eq(lynts.parent, lyntId));

		const commentIds = comments.map((comment) => comment.id);
		const allIds = [lyntId, ...commentIds];

		// Delete likes associated with the comments and the original lynt
		await trx.delete(likes).where(inArray(likes.lynt_id, allIds));

		// Delete notifications associated with the comments and the original lynt
		await trx.delete(notifications).where(inArray(notifications.lyntId, allIds));

		// Delete history entries associated with the comments and the original lynt
		await trx.delete(history).where(inArray(history.lynt_id, allIds));

		// Delete all comments under this lynt
		await trx.delete(lynts).where(and(eq(lynts.parent, lyntId), eq(lynts.reposted, false)));

		// Update reposts of this lynt
		await trx
			.update(lynts)
			.set({
				content: sql`${lynts.content} || '\nThe Lynt this user is reposting has been since deleted.'`,
				parent: null
			})
			.where(and(eq(lynts.parent, lyntId), eq(lynts.reposted, true)));

		// Delete the original lynt
		await trx.delete(lynts).where(eq(lynts.id, lyntId));
	});
}

All this code could have been replaced with a few cascade delete, I'm pretty sure

A lot of stuff in the codebase is pretty shit

@GStudiosX2
Copy link
Contributor

same issue (ignore the content lmao)

firefox_LsSSbbbWai.mp4

I can't play this video for some reason

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants