Skip to content
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

feat: アカウントのヘッダー/アバター画像の設定、設定解除ができるように #823

Merged
merged 12 commits into from
Nov 13, 2024
54 changes: 17 additions & 37 deletions pkg/accounts/adaptor/controller/account.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { z } from '@hono/zod-openapi';
import { Option, Result } from '@mikuroxina/mini-fn';

import type { MediumID } from '../../../drive/model/medium.js';
import type { Medium, MediumID } from '../../../drive/model/medium.js';
import type { AccountID, AccountName } from '../../model/account.js';
import type { AccountFollow } from '../../model/follow.js';
import type { AuthenticateService } from '../../service/authenticate.js';
Expand Down Expand Up @@ -38,8 +38,8 @@
private readonly unFollowService: UnfollowService;
private readonly fetchFollowService: FetchFollowService;
private readonly resendTokenService: ResendVerifyTokenService;
private readonly headerService: AccountHeaderService;
private readonly avatarService: AccountAvatarService;

Check warning on line 42 in pkg/accounts/adaptor/controller/account.ts

View check run for this annotation

Codecov / codecov/patch

pkg/accounts/adaptor/controller/account.ts#L41-L42

Added lines #L41 - L42 were not covered by tests

constructor(args: {
registerService: RegisterService;
Expand Down Expand Up @@ -67,8 +67,8 @@
this.unFollowService = args.unFollowService;
this.fetchFollowService = args.fetchFollowService;
this.resendTokenService = args.resendTokenService;
this.headerService = args.headerService;
this.avatarService = args.avatarService;

Check warning on line 71 in pkg/accounts/adaptor/controller/account.ts

View check run for this annotation

Codecov / codecov/patch

pkg/accounts/adaptor/controller/account.ts#L70-L71

Added lines #L70 - L71 were not covered by tests
}

async createAccount(
Expand Down Expand Up @@ -221,38 +221,38 @@
if (Result.isErr(res)) {
return res;
}
const account = Result.unwrap(res);

Check warning on line 224 in pkg/accounts/adaptor/controller/account.ts

View check run for this annotation

Codecov / codecov/patch

pkg/accounts/adaptor/controller/account.ts#L224

Added line #L224 was not covered by tests

const avatarRes = await this.avatarService.fetchByAccountID(
account.getID(),
);
const headerRes = await this.headerService.fetchByAccountID(
account.getID(),
);
const avatar = Result.isOk(avatarRes)
? Result.unwrap(avatarRes).getUrl()
: '';
const header = Result.isOk(headerRes)
? Result.unwrap(headerRes).getUrl()
: '';
const avatar = Result.mapOr('')((avatarImage: Medium): string =>
avatarImage.getUrl(),
)(avatarRes);
const header = Result.mapOr('')((headerImage: Medium): string =>
headerImage.getUrl(),
)(headerRes);

Check warning on line 237 in pkg/accounts/adaptor/controller/account.ts

View check run for this annotation

Codecov / codecov/patch

pkg/accounts/adaptor/controller/account.ts#L226-L237

Added lines #L226 - L237 were not covered by tests

return Result.ok({
id: account.getID(),
email: account.getMail(),
name: account.getName() as string,
nickname: account.getNickname(),
bio: account.getBio(),

Check warning on line 244 in pkg/accounts/adaptor/controller/account.ts

View check run for this annotation

Codecov / codecov/patch

pkg/accounts/adaptor/controller/account.ts#L240-L244

Added lines #L240 - L244 were not covered by tests
// ToDo: fill the following fields
avatar: avatar,
header: header,

Check warning on line 247 in pkg/accounts/adaptor/controller/account.ts

View check run for this annotation

Codecov / codecov/patch

pkg/accounts/adaptor/controller/account.ts#L246-L247

Added lines #L246 - L247 were not covered by tests
followed_count: 0,
following_count: 0,
note_count: 0,
created_at: account.getCreatedAt(),
role: account.getRole(),
frozen: account.getFrozen(),
status: account.getStatus(),
silenced: account.getSilenced(),

Check warning on line 255 in pkg/accounts/adaptor/controller/account.ts

View check run for this annotation

Codecov / codecov/patch

pkg/accounts/adaptor/controller/account.ts#L251-L255

Added lines #L251 - L255 were not covered by tests
});
}

Expand All @@ -263,38 +263,38 @@
if (Result.isErr(res)) {
return res;
}
const account = Result.unwrap(res);

Check warning on line 266 in pkg/accounts/adaptor/controller/account.ts

View check run for this annotation

Codecov / codecov/patch

pkg/accounts/adaptor/controller/account.ts#L266

Added line #L266 was not covered by tests

const avatarRes = await this.avatarService.fetchByAccountID(
account.getID(),
);
const headerRes = await this.headerService.fetchByAccountID(
account.getID(),
);
const avatar = Result.isOk(avatarRes)
? Result.unwrap(avatarRes).getUrl()
: '';
const header = Result.isOk(headerRes)
? Result.unwrap(headerRes).getUrl()
: '';
const avatar = Result.mapOr('')((avatarImage: Medium): string =>
avatarImage.getUrl(),
)(avatarRes);
const header = Result.mapOr('')((headerImage: Medium): string =>
headerImage.getUrl(),
)(headerRes);

Check warning on line 279 in pkg/accounts/adaptor/controller/account.ts

View check run for this annotation

Codecov / codecov/patch

pkg/accounts/adaptor/controller/account.ts#L268-L279

Added lines #L268 - L279 were not covered by tests
laminne marked this conversation as resolved.
Show resolved Hide resolved

return Result.ok({
id: account.getID(),
email: account.getMail(),
name: account.getName() as string,
nickname: account.getNickname(),
bio: account.getBio(),

Check warning on line 286 in pkg/accounts/adaptor/controller/account.ts

View check run for this annotation

Codecov / codecov/patch

pkg/accounts/adaptor/controller/account.ts#L282-L286

Added lines #L282 - L286 were not covered by tests
// ToDo: fill the following fields
avatar: avatar,
header: header,

Check warning on line 289 in pkg/accounts/adaptor/controller/account.ts

View check run for this annotation

Codecov / codecov/patch

pkg/accounts/adaptor/controller/account.ts#L288-L289

Added lines #L288 - L289 were not covered by tests
followed_count: 0,
following_count: 0,
note_count: 0,
created_at: account.getCreatedAt(),
role: account.getRole(),
frozen: account.getFrozen(),
status: account.getStatus(),
silenced: account.getSilenced(),

Check warning on line 297 in pkg/accounts/adaptor/controller/account.ts

View check run for this annotation

Codecov / codecov/patch

pkg/accounts/adaptor/controller/account.ts#L293-L297

Added lines #L293 - L297 were not covered by tests
});
}

Expand Down Expand Up @@ -476,99 +476,79 @@
);
}

async setAvatar(
targetAccountName: string,
actorID: string,
medium: string,
): Promise<Result.Result<Error, void>> {
const accountRes = await this.fetchService.fetchAccount(
targetAccountName as AccountName,
);
if (Result.isErr(accountRes)) {
return accountRes;
}
const account = Result.unwrap(accountRes);

Check warning on line 490 in pkg/accounts/adaptor/controller/account.ts

View check run for this annotation

Codecov / codecov/patch

pkg/accounts/adaptor/controller/account.ts#L479-L490

Added lines #L479 - L490 were not covered by tests

const res = await this.avatarService.create(
return await this.avatarService.create(
account.getID(),
medium as MediumID,
actorID as AccountID,
);
if (Result.isErr(res)) {
return res;
}

return Result.ok(undefined);
}

Check warning on line 497 in pkg/accounts/adaptor/controller/account.ts

View check run for this annotation

Codecov / codecov/patch

pkg/accounts/adaptor/controller/account.ts#L492-L497

Added lines #L492 - L497 were not covered by tests

async setHeader(
targetAccountName: string,
actorID: string,
medium: string,
): Promise<Result.Result<Error, void>> {
const accountRes = await this.fetchService.fetchAccount(
targetAccountName as AccountName,
);
if (Result.isErr(accountRes)) {
return accountRes;
}
const account = Result.unwrap(accountRes);

Check warning on line 510 in pkg/accounts/adaptor/controller/account.ts

View check run for this annotation

Codecov / codecov/patch

pkg/accounts/adaptor/controller/account.ts#L499-L510

Added lines #L499 - L510 were not covered by tests

const res = await this.headerService.create(
return await this.headerService.create(
account.getID(),
medium as MediumID,
actorID as AccountID,
);
if (Result.isErr(res)) {
return res;
}

return Result.ok(undefined);
}

Check warning on line 517 in pkg/accounts/adaptor/controller/account.ts

View check run for this annotation

Codecov / codecov/patch

pkg/accounts/adaptor/controller/account.ts#L512-L517

Added lines #L512 - L517 were not covered by tests

async unsetAvatar(
targetAccountName: string,
actorID: string,
): Promise<Result.Result<Error, void>> {
const accountRes = await this.fetchService.fetchAccount(
targetAccountName as AccountName,
);
if (Result.isErr(accountRes)) {
return accountRes;
}
const account = Result.unwrap(accountRes);

Check warning on line 529 in pkg/accounts/adaptor/controller/account.ts

View check run for this annotation

Codecov / codecov/patch

pkg/accounts/adaptor/controller/account.ts#L519-L529

Added lines #L519 - L529 were not covered by tests

const res = await this.avatarService.delete(
return await this.avatarService.delete(
account.getID(),
actorID as AccountID,
);
if (Result.isErr(res)) {
return res;
}

return Result.ok(undefined);
}

Check warning on line 535 in pkg/accounts/adaptor/controller/account.ts

View check run for this annotation

Codecov / codecov/patch

pkg/accounts/adaptor/controller/account.ts#L531-L535

Added lines #L531 - L535 were not covered by tests

async unsetHeader(
targetAccountName: string,
actorID: string,
): Promise<Result.Result<Error, void>> {
const accountRes = await this.fetchService.fetchAccount(
targetAccountName as AccountID,
);
if (Result.isErr(accountRes)) {
return accountRes;
}
const account = Result.unwrap(accountRes);

Check warning on line 547 in pkg/accounts/adaptor/controller/account.ts

View check run for this annotation

Codecov / codecov/patch

pkg/accounts/adaptor/controller/account.ts#L537-L547

Added lines #L537 - L547 were not covered by tests

const res = await this.headerService.delete(
return await this.headerService.delete(
account.getID(),
actorID as AccountID,
);
if (Result.isErr(res)) {
return res;
}

return Result.ok(undefined);
}

Check warning on line 553 in pkg/accounts/adaptor/controller/account.ts

View check run for this annotation

Codecov / codecov/patch

pkg/accounts/adaptor/controller/account.ts#L549-L553

Added lines #L549 - L553 were not covered by tests
}
12 changes: 12 additions & 0 deletions pkg/accounts/model/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ export interface AccountFollowRepository {
export const followRepoSymbol = Ether.newEtherSymbol<AccountFollowRepository>();

export interface AccountAvatarRepository {
/**
* Set an avatar image to account.\
* NOTE: This method **WILL NOT** overwrite the existing avatar. (returns error)
* @param accountID
* @param mediumID
*/
create(
accountID: AccountID,
mediumID: MediumID,
Expand All @@ -74,6 +80,12 @@ export const accountAvatarRepoSymbol =
Ether.newEtherSymbol<AccountAvatarRepository>();

export interface AccountHeaderRepository {
/**
* Set a header image to account.\
* NOTE: This method **WILL NOT** overwrite the existing header. (returns error)
* @param accountID
* @param mediumID
*/
create(
accountID: AccountID,
mediumID: MediumID,
Expand Down
4 changes: 3 additions & 1 deletion pkg/drive/service/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export class FetchMediaService {
)(res);
}

async fetchMediaByID(mediumID: MediumID) {
async fetchMediaByID(
mediumID: MediumID,
): Promise<Result.Result<Error, Medium>> {
const res = await this.mediaRepository.findById(mediumID);
return Option.okOrElse(
() => new MediaNotFoundError('Failed to fetch media', { cause: null }),
Expand Down
Loading