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(api): ユーザーをフォローできるように #172

Merged
merged 2 commits into from
Sep 7, 2023
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
4 changes: 0 additions & 4 deletions src/domain/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,6 @@ export class UserAPData {
}

export class UserFollowEvent {
// ToDo:
// DTOなどへの変換時に再帰的に変換が行われる可能性がある
// ->必要なデータ: id,nickName, fullHandle, iconImageURL, bio

// フォローされたユーザー(dst)
private readonly _follower: FollowUser;
// フォローしたユーザー(from)
Expand Down
27 changes: 25 additions & 2 deletions src/repository/inmemory/user.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { IUserRepository } from "../user.js";
import { AsyncResult, Failure, Result, Success } from "../../helpers/result.js";
import { User } from "../../domain/user.js";
import { User, UserFollowEvent } from "../../domain/user.js";
import { Snowflake } from "../../helpers/id_generator.js";

export class UserRepository implements IUserRepository {
private data: Set<User>;
private followData: Set<UserFollowEvent>;

constructor(data: User[]) {
constructor(data: User[], followData?: UserFollowEvent[]) {
this.data = new Set(data);
this.followData = new Set(followData);
}

async Create(u: User): Promise<Result<User, Error>> {
Expand Down Expand Up @@ -66,4 +68,25 @@ export class UserRepository implements IUserRepository {
);
}
}

async CreateFollow(u: UserFollowEvent): AsyncResult<UserFollowEvent, Error> {
try {
this.followData.add(u);
return new Success(u);
} catch (e: unknown) {
return new Failure(new Error(e as any));
}
}

async FindFollowEvent(
followingID: Snowflake,
followerID: Snowflake,
): AsyncResult<UserFollowEvent, Error> {
for (const v of [...this.followData]) {
if (v.follower.id === followerID && v.following.id === followingID) {
return new Success(v);
}
}
return new Failure(new Error("failed to find follow event"));
}
}
76 changes: 76 additions & 0 deletions src/repository/prisma/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,33 @@ export class UserRepository implements IUserRepository {
}
}

async CreateFollow(u: UserFollowEvent): AsyncResult<UserFollowEvent, Error> {
try {
const res = await this.prisma.userFollowEvent.create({
data: {
follower: {
connect: {
id: u.follower.id,
},
},
following: {
connect: {
id: u.following.id,
},
},
},
include: {
follower: true,
following: true,
},
});
const resp: UserFollowEvent = this.convertToFollowEventDomain(res);
return new Success(resp);
} catch (e: unknown) {
return new Failure(PrismaErrorConverter(e));
}
}

async FindByHandle(handle: string): Promise<Result<User, Error>> {
try {
const res = await this.prisma.user.findUniqueOrThrow({
Expand Down Expand Up @@ -247,6 +274,38 @@ export class UserRepository implements IUserRepository {
});
});
}

private convertToFollowEventDomain<T extends UserFollowEventEntity>(
i: T,
): UserFollowEvent {
return new UserFollowEvent(
{ ...i.following, id: i.following.id as Snowflake },
{ ...i.follower, id: i.follower.id as Snowflake },
);
}

async FindFollowEvent(
followingID: Snowflake,
followerID: Snowflake,
): AsyncResult<UserFollowEvent, Error> {
try {
const res = await this.prisma.userFollowEvent.findUniqueOrThrow({
where: {
followingID_followerID: {
followingID: followingID,
followerID: followerID,
},
},
include: {
follower: true,
following: true,
},
});
return new Success(this.convertToFollowEventDomain(res));
} catch (e: unknown) {
return new Failure(PrismaErrorConverter(e));
}
}
}

export type UserEntity = {
Expand Down Expand Up @@ -288,3 +347,20 @@ export type UserEntity = {
id: string;
};
};

export type UserFollowEventEntity = {
follower: {
id: string;
fullHandle: string;
nickName: string;
bio: string;
iconImageURL: string;
};
following: {
id: string;
fullHandle: string;
nickName: string;
bio: string;
iconImageURL: string;
};
};
7 changes: 6 additions & 1 deletion src/repository/user.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { User } from "../domain/user.js";
import { User, UserFollowEvent } from "../domain/user.js";
import { AsyncResult } from "../helpers/result.js";
import { Snowflake } from "../helpers/id_generator.js";

export interface IUserRepository {
Create(u: User): AsyncResult<User, Error>;
Update(u: User): AsyncResult<User, Error>;
CreateFollow(u: UserFollowEvent): AsyncResult<UserFollowEvent, Error>;

FindByID(id: Snowflake): AsyncResult<User, Error>;
FindByHandle(handle: string): AsyncResult<User, Error>;
FindFollowing(id: Snowflake): AsyncResult<Array<User>, Error>;
FindFollower(id: Snowflake): AsyncResult<Array<User>, Error>;
FindFollowEvent(
followingID: Snowflake,
followerID: Snowflake,
): AsyncResult<UserFollowEvent, Error>;
}
24 changes: 23 additions & 1 deletion src/server/controller/user.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { FindUserService } from "../../service/user/find_user_service.js";
import { AsyncResult, Failure, Result, Success } from "../../helpers/result.js";
import { UserResponse } from "../types/user.js";
import {
DomainToUserFollowResponse,
UserFollowResponse,
UserResponse,
} from "../types/user.js";
import { FindServerService } from "../../service/server/find_server_service.js";
import { FindPostService } from "../../service/post/find_post_service.js";
import {
Expand All @@ -10,20 +14,24 @@ import {
} from "../types/post.js";
import { Snowflake } from "../../helpers/id_generator.js";
import { PostData } from "../../service/data/post.js";
import { CreateFollowService } from "../../service/user/create_follow_service.js";

export class UserController {
private readonly findUserService: FindUserService;
private readonly findServerService: FindServerService;
private readonly findPostService: FindPostService;
private readonly createFollowService: CreateFollowService;

constructor(args: {
findUserService: FindUserService;
findServerService: FindServerService;
findPostService: FindPostService;
createFollowService: CreateFollowService;
}) {
this.findUserService = args.findUserService;
this.findServerService = args.findServerService;
this.findPostService = args.findPostService;
this.createFollowService = args.createFollowService;
}

async FindByHandle(name: string): AsyncResult<UserResponse, Error> {
Expand Down Expand Up @@ -120,6 +128,20 @@ export class UserController {
);
}

async CreateFollow(
followerID: string,
followingID: string,
): AsyncResult<UserFollowResponse, Error> {
const res = await this.createFollowService.Handle(
followingID as Snowflake,
followerID as Snowflake,
);
if (res.isFailure()) {
return new Failure(res.value);
}
return new Success(DomainToUserFollowResponse(res.value));
}

private acctConverter(acct: string): Result<string, Error> {
const split = acct.split("@");
switch (split.length) {
Expand Down
12 changes: 12 additions & 0 deletions src/server/handlers/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,16 @@ export class UserHandlers {
r.code(200).send(res.value);
return;
};

public CreateFollow: FastifyHandlerMethod<{ Params: { id: string } }> =
async (q, r) => {
// APIを叩いたユーザー - フォロー > フォロー先(params.id)
const res = await this.controller.CreateFollow(q.params.id, "123");
if (res.isFailure()) {
const [code, message] = ErrorConverter(res.value);
return r.code(code).send(message);
}

r.code(200).send(res.value);
};
}
3 changes: 3 additions & 0 deletions src/server/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { NodeInfoController } from "./controller/activitypub/nodeinfo.js";
import { PersonHandler } from "./handlers/activitypub/person.js";
import { PersonController } from "./controller/activitypub/person.js";
import logger from "../helpers/logger.js";
import { CreateFollowService } from "../service/user/create_follow_service.js";

export async function StartServer(port: number) {
const app = fastify({
Expand Down Expand Up @@ -71,6 +72,7 @@ export async function StartServer(port: number) {
findServerService: new FindServerService(serverRepository),
findUserService: new FindUserService(userRepository),
findPostService: new FindPostService(postRepository),
createFollowService: new CreateFollowService(userRepository),
}),
);
const apHandler = new WebFingerHandler(
Expand All @@ -90,6 +92,7 @@ export async function StartServer(port: number) {
app.delete("/api/v1/posts/:id/reaction", postHandler.UndoReaction);
app.post("/api/v1/posts", postHandler.CreatePost);
app.get("/api/v1/users/:name", userHandler.FindByHandle);
app.post("/api/v1/users/:id/follow", userHandler.CreateFollow);
app.get("/api/v1/users/:name/posts", userHandler.FindUserPosts);
app.get("/api/v1/timeline/home", postHandler.GetTimeline);

Expand Down
40 changes: 40 additions & 0 deletions src/server/types/user.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { UserFollowEventData } from "../../service/data/user.js";

export interface UserResponse {
id: string;
host: string;
Expand All @@ -9,3 +11,41 @@ export interface UserResponse {
following: Array<object>;
softwareName: string;
}

export interface UserFollowResponse {
following: {
id: string;
nickName: string;
fullHandle: string;
iconImageURL: string;
bio: string;
};
follower: {
id: string;
nickName: string;
fullHandle: string;
iconImageURL: string;
bio: string;
};
}

export function DomainToUserFollowResponse(
d: UserFollowEventData,
): UserFollowResponse {
return {
following: {
id: d.following.id,
nickName: d.following.nickName,
fullHandle: d.following.fullHandle,
iconImageURL: d.following.iconImageURL,
bio: d.following.bio,
},
follower: {
id: d.follower.id,
nickName: d.follower.nickName,
fullHandle: d.follower.fullHandle,
iconImageURL: d.follower.iconImageURL,
bio: d.follower.bio,
},
};
}
6 changes: 6 additions & 0 deletions src/service/data/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,12 @@ export class UserFollowEventData {
}
}

export function UserFollowEventToUserFollowEventData(
u: UserFollowEvent,
): UserFollowEventData {
return new UserFollowEventData(u.following, u.follower);
}

export interface FollowUserData {
id: Snowflake;
nickName: string;
Expand Down
Loading