Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 141 additions & 0 deletions src/post/post.repository.knex.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { TABLE_LIKES, TABLE_POSTS, TABLE_REPOSTS } from '../constants';
import { client } from '../db';
import { SiteService } from '../site/site.service';
import { PostCreatedEvent } from './post-created.event';
import { PostDeletedEvent } from './post-deleted.event';
import { PostDerepostedEvent } from './post-dereposted.event';
import { PostRepostedEvent } from './post-reposted.event';
import { Post, PostType } from './post.entity';
Expand Down Expand Up @@ -65,6 +66,146 @@ describe('KnexPostRepository', () => {
postRepository = new KnexPostRepository(client, events);
});

describe('Delete', () => {
it('Can handle a deleted post', async () => {
const site = await siteService.initialiseSiteForHost('testing.com');
const account = await accountRepository.getBySite(site);
const post = Post.createArticleFromGhostPost(account, {
title: 'Title',
uuid: '3f1c5e84-9a2b-4d7f-8e62-1a6b9c9d4f10',
html: '<p>Hello, world!</p>',
excerpt: 'Hello, world!',
feature_image: null,
url: 'https://testing.com/hello-world',
published_at: '2025-01-01',
visibility: 'public',
});

await postRepository.save(post);

post.delete(account);

const postDeletedEventPromise: Promise<PostDeletedEvent> =
new Promise((resolve) => {
events.once(PostDeletedEvent.getName(), resolve);
});

await postRepository.save(post);

const rowInDb = await client(TABLE_POSTS)
.where({
uuid: post.uuid,
})
.select('*')
.first();

assert(rowInDb, 'A row should have been saved in the DB');
expect(rowInDb.deleted_at).not.toBe(null);
expect(rowInDb.title).toBe('Title');
expect(rowInDb.content).toBe('<p>Hello, world!</p>');
expect(rowInDb.excerpt).toBe('Hello, world!');

const postDeletedEvent = await postDeletedEventPromise;

expect(postDeletedEvent.getPost().id).toBe(post.id);
});

it('Can handle a deleted reply', async () => {
const site = await siteService.initialiseSiteForHost('testing.com');
const account = await accountRepository.getBySite(site);
const post = Post.createArticleFromGhostPost(account, {
title: 'Title',
uuid: '3f1c5e84-9a2b-4d7f-8e62-1a6b9c9d4f10',
html: '<p>Hello, world!</p>',
excerpt: 'Hello, world!',
feature_image: null,
url: 'https://testing.com/hello-world',
published_at: '2025-01-01',
visibility: 'public',
});

await postRepository.save(post);

const reply = Post.createFromData(account, {
type: PostType.Note,
content: 'Hey',
inReplyTo: post,
});

await postRepository.save(reply);

const postRowInDb = await client(TABLE_POSTS)
.where({
uuid: post.uuid,
})
.select('*')
.first();

expect(postRowInDb.reply_count).toBe(1);

reply.delete(account);

const postDeletedEventPromise: Promise<PostDeletedEvent> =
new Promise((resolve) => {
events.once(PostDeletedEvent.getName(), resolve);
});

await postRepository.save(reply);

const replyRowInDb = await client(TABLE_POSTS)
.where({
uuid: reply.uuid,
})
.select('*')
.first();

assert(replyRowInDb, 'A row should have been saved in the DB');
expect(replyRowInDb.deleted_at).not.toBe(null);
expect(replyRowInDb.content).toBe('Hey');

const postDeletedEvent = await postDeletedEventPromise;

expect(postDeletedEvent.getPost().id).toBe(reply.id);

const postRowAfterDelete = await client(TABLE_POSTS)
.where({
uuid: post.uuid,
})
.select('*')
.first();

expect(postRowAfterDelete.reply_count).toBe(0);
});

it('Can handle a new deleted post', async () => {
const site = await siteService.initialiseSiteForHost('testing.com');
const account = await accountRepository.getBySite(site);
const post = Post.createArticleFromGhostPost(account, {
title: 'Title',
uuid: '3f1c5e84-9a2b-4d7f-8e62-1a6b9c9d4f10',
html: '<p>Hello, world!</p>',
excerpt: 'Hello, world!',
feature_image: null,
url: 'https://testing.com/hello-world',
published_at: '2025-01-01',
visibility: 'public',
});

post.delete(account);

await postRepository.save(post);

const rowInDb = await client(TABLE_POSTS)
.where({
uuid: post.uuid,
})
.select('*')
.first();

expect(rowInDb).toBe(undefined);
});
});

it('Can save a Post', async () => {
const site = await siteService.initialiseSiteForHost('testing.com');
const account = await accountRepository.getBySite(site);
Expand Down
48 changes: 47 additions & 1 deletion src/post/post.repository.knex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Account } from '../account/account.entity';
import { TABLE_LIKES, TABLE_POSTS, TABLE_REPOSTS } from '../constants';
import { parseURL } from '../core/url';
import { PostCreatedEvent } from './post-created.event';
import { PostDeletedEvent } from './post-deleted.event';
import { PostDerepostedEvent } from './post-dereposted.event';
import { PostRepostedEvent } from './post-reposted.event';
import { Post } from './post.entity';
Expand Down Expand Up @@ -341,13 +342,26 @@ export class KnexPostRepository {
* @param post Post to save
*/
async save(post: Post): Promise<void> {
const transaction = await this.db.transaction();
const isNewPost = post.isNew;
const isDeletedPost = Post.isDeleted(post);

if (post.author.id === null) {
throw new Error(
`Unable to save Post ${post.uuid} - The author is missing an id.`,
);
}

if (isNewPost && isDeletedPost) {
return;
}

const transaction = await this.db.transaction();

try {
const { likesToAdd, likesToRemove } = post.getChangedLikes();
const { repostsToAdd, repostsToRemove } = post.getChangedReposts();
let repostAccountIds: number[] = [];
let wasDeleted = false;

if (isNewPost) {
const postId = await this.insertPost(
Expand Down Expand Up @@ -379,6 +393,31 @@ export class KnexPostRepository {
(accountId) => accountId,
);
}
} else if (isDeletedPost) {
const existingRow = await transaction('posts')
.select('deleted_at')
.where({
id: post.id,
})
.first();

if (existingRow && existingRow.deleted_at === null) {
await transaction('posts')
.update({
deleted_at: transaction.raw('CURRENT_TIMESTAMP'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on what was discussed in Slack, do we want to use UTC_TIMESTAMP here now, or do we want to do a big bang change later?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah so I wanted to do a big bang change so that we can do any migrations if necessary and all the current data is consistent

})
.where({ id: post.id });

if (post.inReplyTo) {
await transaction('posts')
.update({
reply_count: this.db.raw('reply_count - 1'),
})
.where({ id: post.inReplyTo });
}
Comment on lines +411 to +417
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think we need to cover this in the existing test or a new test


wasDeleted = true;
}
} else {
if (likesToAdd.length > 0 || likesToRemove.length > 0) {
const insertedLikesCount =
Expand Down Expand Up @@ -457,6 +496,13 @@ export class KnexPostRepository {
);
}

if (wasDeleted) {
this.events.emit(
PostDeletedEvent.getName(),
new PostDeletedEvent(post, post.author.id),
);
}

for (const accountId of repostAccountIds) {
this.events.emit(
PostRepostedEvent.getName(),
Expand Down