-
-
Notifications
You must be signed in to change notification settings - Fork 28
Migrating modification of Accounts to Entity layer #527
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
Changes from all commits
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 |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import type { Account } from './account.entity'; | ||
|
|
||
| export class AccountUpdatedEvent { | ||
| static getName(): string { | ||
| return 'account.updated.event'; | ||
| } | ||
|
|
||
| constructor(private readonly account: Account) {} | ||
|
|
||
| getAccount(): Account { | ||
| return this.account; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,25 +22,44 @@ export type AccountSite = { | |
| host: string; | ||
| }; | ||
|
|
||
| export interface ProfileUpdateParams { | ||
| name?: string | null; | ||
| bio?: string | null; | ||
| avatarUrl?: URL | null; | ||
| bannerImageUrl?: URL | null; | ||
| } | ||
|
|
||
| export class Account extends BaseEntity { | ||
| public readonly uuid: string; | ||
| public readonly url: URL; | ||
| public readonly apId: URL; | ||
| public readonly apFollowers: URL; | ||
|
|
||
| private _name: string | null; | ||
| private _bio: string | null; | ||
| private _avatarUrl: URL | null; | ||
| private _bannerImageUrl: URL | null; | ||
|
|
||
| constructor( | ||
| public readonly id: number | null, | ||
| uuid: string | null, | ||
| public readonly username: string, | ||
| public readonly name: string | null, | ||
| public readonly bio: string | null, | ||
| public readonly avatarUrl: URL | null, | ||
| public readonly bannerImageUrl: URL | null, | ||
| name: string | null, | ||
| bio: string | null, | ||
| avatarUrl: URL | null, | ||
| bannerImageUrl: URL | null, | ||
| private readonly site: AccountSite | null, | ||
| apId: URL | null, | ||
| url: URL | null, | ||
| apFollowers: URL | null, | ||
| ) { | ||
| super(id); | ||
|
|
||
| this._name = name; | ||
| this._bio = bio; | ||
| this._avatarUrl = avatarUrl; | ||
| this._bannerImageUrl = bannerImageUrl; | ||
|
|
||
| if (uuid === null) { | ||
| this.uuid = randomUUID(); | ||
| } else { | ||
|
|
@@ -63,6 +82,40 @@ export class Account extends BaseEntity { | |
| } | ||
| } | ||
|
|
||
| get name(): string | null { | ||
| return this._name; | ||
| } | ||
|
|
||
| get bio(): string | null { | ||
| return this._bio; | ||
| } | ||
|
|
||
| get avatarUrl(): URL | null { | ||
| return this._avatarUrl; | ||
| } | ||
|
|
||
| get bannerImageUrl(): URL | null { | ||
| return this._bannerImageUrl; | ||
| } | ||
|
|
||
| updateProfile(params: ProfileUpdateParams): void { | ||
|
Member
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. Do we need to add
Collaborator
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. Yeah that will come as part of the username change work |
||
| if (params.name !== undefined) { | ||
| this._name = params.name; | ||
| } | ||
|
|
||
| if (params.bio !== undefined) { | ||
| this._bio = params.bio; | ||
| } | ||
|
|
||
| if (params.avatarUrl !== undefined) { | ||
| this._avatarUrl = params.avatarUrl; | ||
| } | ||
|
|
||
| if (params.bannerImageUrl !== undefined) { | ||
| this._bannerImageUrl = params.bannerImageUrl; | ||
| } | ||
| } | ||
|
|
||
| get isInternal() { | ||
| return this.site !== null; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ import type { AsyncEvents } from 'core/events'; | |
| import type { Knex } from 'knex'; | ||
| import { parseURL } from '../core/url'; | ||
| import type { Site } from '../site/site.service'; | ||
| import { AccountUpdatedEvent } from './account-updated.event'; | ||
| import { Account, type AccountSite } from './account.entity'; | ||
|
|
||
| export class KnexAccountRepository { | ||
|
|
@@ -11,6 +12,28 @@ export class KnexAccountRepository { | |
| private readonly events: AsyncEvents, | ||
| ) {} | ||
|
|
||
| async save(account: Account): Promise<void> { | ||
| if (account.isNew) { | ||
| throw new Error( | ||
| 'Saving of new Accounts has not been implemented yet.', | ||
|
Member
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. We can rename this method to
Collaborator
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. Our repository pattern uses a single save even for both update and saves, this means that callers of the code don't need to know if the entity they have a reference to is new or not. We could change this if you think it's worth it but I think it needs to be part of a larger discussion and the PostRepository would need to be updated too
Member
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. In that case, it’s fine. I get that the single |
||
| ); | ||
| } | ||
|
|
||
| await this.db('accounts') | ||
| .update({ | ||
| name: account.name, | ||
| bio: account.bio, | ||
| avatar_url: account.avatarUrl?.href, | ||
| banner_image_url: account.bannerImageUrl?.href, | ||
| }) | ||
| .where({ id: account.id }); | ||
|
|
||
| await this.events.emitAsync( | ||
| AccountUpdatedEvent.getName(), | ||
| new AccountUpdatedEvent(account), | ||
| ); | ||
| } | ||
|
|
||
| async getBySite(site: Site): Promise<Account> { | ||
| const users = await this.db('users').where('site_id', site.id); | ||
| if (users.length === 0) { | ||
|
|
||
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.
This could just be
account.updated, but I think it would contradict the current event. Maybe something to clean up later?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.
Yeah exactly, that name is already taken so I went with this - it's abstracted away in this method so easy to change later