-
-
Notifications
You must be signed in to change notification settings - Fork 27
Post created event serialization #1599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
00df68b
d6a74d2
16b9160
1294677
512c080
7dcabe0
ca8929d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -376,7 +376,7 @@ describe('FediverseBridge', () => { | |
| author.apFollowers = new URL('https://example.com/user/foo/followers'); | ||
|
|
||
| const post = Object.create(Post); | ||
| post.id = 'post-123'; | ||
| post.id = 456; | ||
| post.author = author; | ||
| post.type = PostType.Note; | ||
| post.content = 'Note content'; | ||
|
|
@@ -385,11 +385,14 @@ describe('FediverseBridge', () => { | |
| post.uuid = 'cb1e7e92-5560-4ceb-9272-7e9d0e2a7da4'; | ||
| post.publishedAt = new Date('2025-01-01T00:00:00Z'); | ||
|
|
||
| const event = new PostCreatedEvent(post); | ||
| vi.mocked(postRepository.getById).mockResolvedValue(post); | ||
|
|
||
| const event = new PostCreatedEvent(post.id as number); | ||
| events.emit(PostCreatedEvent.getName(), event); | ||
|
|
||
| await nextTick(); | ||
|
|
||
| expect(postRepository.getById).toHaveBeenCalledWith(post.id); | ||
| expect(sendActivity).toHaveBeenCalledOnce(); | ||
| expect(context.data.globaldb.set).toHaveBeenCalled(); | ||
|
|
||
|
|
@@ -417,7 +420,7 @@ describe('FediverseBridge', () => { | |
| mentionedAccount.isInternal = true; | ||
|
|
||
| const post = Object.create(Post); | ||
| post.id = 'post-123'; | ||
| post.id = 789; | ||
| post.author = author; | ||
| post.type = PostType.Note; | ||
| post.content = 'Hello! @test@example.com'; | ||
|
|
@@ -426,11 +429,15 @@ describe('FediverseBridge', () => { | |
| post.uuid = 'cb1e7e92-5560-4ceb-9272-7e9d0e2a7da4'; | ||
| post.publishedAt = new Date('2025-01-01T00:00:00Z'); | ||
|
|
||
| const event = new PostCreatedEvent(post); | ||
| vi.mocked(postRepository.getById).mockResolvedValue(post); | ||
|
|
||
| const event = new PostCreatedEvent(post.id as number); | ||
| events.emit(PostCreatedEvent.getName(), event); | ||
|
|
||
| await nextTick(); | ||
|
|
||
| expect(postRepository.getById).toHaveBeenCalledWith(post.id); | ||
|
|
||
| const storedActivity = await globalDbSet.mock.calls[0][1]; | ||
| await expect(storedActivity).toMatchFileSnapshot( | ||
| './__snapshots__/publish-note-create-activity-with-mentions.json', | ||
|
|
@@ -451,7 +458,7 @@ describe('FediverseBridge', () => { | |
| author.apFollowers = new URL('https://example.com/user/foo/followers'); | ||
|
|
||
| const post = Object.create(Post); | ||
| post.id = 'post-123'; | ||
| post.id = 111; | ||
| post.author = author; | ||
| post.type = PostType.Article; | ||
| post.title = 'Post title'; | ||
|
|
@@ -463,11 +470,14 @@ describe('FediverseBridge', () => { | |
| post.apId = new URL('https://example.com/article/post-123'); | ||
| post.uuid = 'cb1e7e92-5560-4ceb-9272-7e9d0e2a7da4'; | ||
|
|
||
| const event = new PostCreatedEvent(post); | ||
| vi.mocked(postRepository.getById).mockResolvedValue(post); | ||
|
|
||
| const event = new PostCreatedEvent(post.id as number); | ||
| events.emit(PostCreatedEvent.getName(), event); | ||
|
|
||
| await nextTick(); | ||
|
|
||
| expect(postRepository.getById).toHaveBeenCalledWith(post.id); | ||
| expect(sendActivity).toHaveBeenCalledOnce(); | ||
| expect(context.data.globaldb.set).toHaveBeenCalled(); | ||
|
|
||
|
|
@@ -489,15 +499,36 @@ describe('FediverseBridge', () => { | |
| author.isInternal = false; | ||
|
|
||
| const post = Object.create(Post); | ||
| post.id = 222; | ||
|
||
| post.author = author; | ||
| post.type = PostType.Note; | ||
| post.content = 'Test content'; | ||
|
|
||
| const event = new PostCreatedEvent(post); | ||
| vi.mocked(postRepository.getById).mockResolvedValue(post); | ||
|
|
||
| const event = new PostCreatedEvent(post.id as number); | ||
| events.emit(PostCreatedEvent.getName(), event); | ||
|
|
||
| await nextTick(); | ||
|
|
||
| expect(postRepository.getById).toHaveBeenCalledWith(post.id); | ||
| expect(sendActivity).not.toHaveBeenCalled(); | ||
| expect(context.data.globaldb.set).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should not create or send activities if post is not found on the PostCreatedEvent', async () => { | ||
| await bridge.init(); | ||
|
|
||
| const sendActivity = vi.spyOn(context, 'sendActivity'); | ||
|
|
||
| vi.mocked(postRepository.getById).mockResolvedValue(null); | ||
|
|
||
| const event = new PostCreatedEvent(999); | ||
|
||
| events.emit(PostCreatedEvent.getName(), event); | ||
|
|
||
| await nextTick(); | ||
|
|
||
| expect(postRepository.getById).toHaveBeenCalledWith(999); | ||
| expect(sendActivity).not.toHaveBeenCalled(); | ||
| expect(context.data.globaldb.set).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -73,11 +73,19 @@ describe('FeedUpdateService', () => { | |
| type: PostType.Article, | ||
| audience: Audience.Public, | ||
| }); | ||
| // biome-ignore lint/suspicious/noExplicitAny: Test helper to set post id | ||
| (post as any).id = 123; | ||
|
|
||
| events.emit(PostCreatedEvent.getName(), new PostCreatedEvent(post)); | ||
| vi.mocked(postRepository.getById).mockResolvedValue(post); | ||
|
|
||
| events.emit( | ||
| PostCreatedEvent.getName(), | ||
| new PostCreatedEvent(post.id as number), | ||
| ); | ||
|
|
||
| await vi.runAllTimersAsync(); | ||
|
|
||
| expect(postRepository.getById).toHaveBeenCalledWith(post.id); | ||
| expect(feedService.addPostToFeeds).toHaveBeenCalledWith(post); | ||
| expect(feedService.addPostToDiscoveryFeeds).toHaveBeenCalledWith( | ||
| post, | ||
|
|
@@ -89,25 +97,55 @@ describe('FeedUpdateService', () => { | |
| type: PostType.Article, | ||
| audience: Audience.FollowersOnly, | ||
| }); | ||
| // biome-ignore lint/suspicious/noExplicitAny: Test helper to set post id | ||
| (post as any).id = 124; | ||
|
||
|
|
||
| events.emit(PostCreatedEvent.getName(), new PostCreatedEvent(post)); | ||
| vi.mocked(postRepository.getById).mockResolvedValue(post); | ||
|
|
||
| events.emit( | ||
| PostCreatedEvent.getName(), | ||
| new PostCreatedEvent(post.id as number), | ||
| ); | ||
|
|
||
| await vi.runAllTimersAsync(); | ||
|
|
||
| expect(postRepository.getById).toHaveBeenCalledWith(post.id); | ||
| expect(feedService.addPostToFeeds).toHaveBeenCalledWith(post); | ||
| expect(feedService.addPostToDiscoveryFeeds).toHaveBeenCalledWith( | ||
| post, | ||
| ); | ||
| }); | ||
|
|
||
| it('should not add direct post to user nor discovery feeds when created', () => { | ||
| it('should not add direct post to user nor discovery feeds when created', async () => { | ||
| const post = Post.createFromData(account, { | ||
| type: PostType.Article, | ||
| audience: Audience.Direct, | ||
| }); | ||
| // biome-ignore lint/suspicious/noExplicitAny: Test helper to set post id | ||
| (post as any).id = 125; | ||
|
||
|
|
||
| vi.mocked(postRepository.getById).mockResolvedValue(post); | ||
|
|
||
| events.emit(PostCreatedEvent.getName(), new PostCreatedEvent(post)); | ||
| events.emit( | ||
| PostCreatedEvent.getName(), | ||
| new PostCreatedEvent(post.id as number), | ||
| ); | ||
|
|
||
| await vi.runAllTimersAsync(); | ||
|
|
||
| expect(postRepository.getById).toHaveBeenCalledWith(post.id); | ||
| expect(feedService.addPostToFeeds).not.toHaveBeenCalled(); | ||
| expect(feedService.addPostToDiscoveryFeeds).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should not add post to feeds if post was deleted', async () => { | ||
| vi.mocked(postRepository.getById).mockResolvedValue(null); | ||
|
|
||
| events.emit(PostCreatedEvent.getName(), new PostCreatedEvent(126)); | ||
|
|
||
| await vi.runAllTimersAsync(); | ||
|
|
||
| expect(postRepository.getById).toHaveBeenCalledWith(126); | ||
| expect(feedService.addPostToFeeds).not.toHaveBeenCalled(); | ||
| expect(feedService.addPostToDiscoveryFeeds).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,30 @@ | ||
| import type { Post } from '@/post/post.entity'; | ||
| import type { SerializableEvent } from '@/events/event'; | ||
|
|
||
| export class PostCreatedEvent { | ||
| constructor(private readonly post: Post) {} | ||
| export class PostCreatedEvent implements SerializableEvent { | ||
| constructor(private readonly postId: number) {} | ||
|
|
||
| getPost(): Post { | ||
| return this.post; | ||
| getPostId(): number { | ||
| return this.postId; | ||
| } | ||
|
|
||
| getName(): string { | ||
| return PostCreatedEvent.getName(); | ||
| } | ||
|
|
||
| static getName(): string { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cursoragent lets also add the non static version of this whilst we are here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This has already been addressed in my last commit. The getName(): string {
return PostCreatedEvent.getName();
}
static getName(): string {
return 'post.created';
}The tests have also been updated to verify both versions work correctly. The comment you're seeing was on the older diff before I made the changes. |
||
| return 'post.created'; | ||
| } | ||
|
|
||
| toJSON(): Record<string, unknown> { | ||
| return { | ||
| postId: this.postId, | ||
| }; | ||
| } | ||
|
|
||
| static fromJSON(data: Record<string, unknown>): PostCreatedEvent { | ||
| if (typeof data.postId !== 'number') { | ||
| throw new Error('postId must be a number'); | ||
| } | ||
| return new PostCreatedEvent(data.postId); | ||
| } | ||
| } | ||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cursoragent this should probably be 456 if we are following the existing pattern. String references below containing
post-123should also be updated (e.gnew URL('https://example.com/post/456');There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Taking a look!