Skip to content

Commit

Permalink
Merge branch 'main' into fix-scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
SnO2WMaN authored Dec 5, 2023
2 parents 20fd2d7 + 7f8cbc9 commit 6737d59
Show file tree
Hide file tree
Showing 13 changed files with 144 additions and 113 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
name: Docker(api)
name: Docker Images

on:
push:
branches:
- main
paths:
- "Dockerfile"
- ".dockerignore"
- "package.json"
- "package-lock.json"
- ".npmrc"
- "src/**"
- "prisma/schema.prisma"
- "codegen-plugins/**"
pull_request:
merge_group:

jobs:
build:
name: Build api
build_api:
name: Docker api
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4
Expand Down Expand Up @@ -56,3 +47,28 @@ jobs:
file: Dockerfile
push: ${{ github.event_name == 'push' }}
tags: ghcr.io/${{ github.repository_owner }}/api:${{ steps.image_tag.outputs.result }}

build_migration:
name: Docker api-postgres-migration
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3
- uses: hadolint/hadolint-action@54c9adbab1582c2ef04b2016b760714a4bfde3cf # v3.1.0
with:
dockerfile: Dockerfile.migration

- uses: docker/setup-buildx-action@885d1462b80bc1c1c7f0b00334ad271f09369c55 # v2
- uses: docker/login-action@465a07811f14bebb1938fbed4728c6a1ff8901fc # v2
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- run: echo "LATEST_MIGRATION_NAME=$(ls -1 --ignore migration_lock.toml ./prisma/migrations --reverse | head -n 1)" >> $GITHUB_ENV
- uses: docker/build-push-action@0a97817b6ade9f46837855d676c4cca3a2471fc9 # v4
with:
cache-from: type=gha
cache-to: type=gha,mode=max
context: .
file: Dockerfile.migration
push: ${{ github.event_name == 'push' }}
tags: ghcr.io/${{ github.repository_owner }}/api-postgres-migration:${{ env.LATEST_MIGRATION_NAME }}
21 changes: 21 additions & 0 deletions Dockerfile.migration
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM node:20.8.1-slim@sha256:4fa1430cd19507875e65896fdf3176fc1674bc5bbf51b5f750fa30484885c18d
WORKDIR /app

# install OpenSSL
RUN apt-get update \
&& apt-get install -y --no-install-recommends openssl=3.0.11-1~deb12u1 \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

## install tini
ADD https://github.com/krallin/tini/releases/download/v0.19.0/tini /bin/tini
RUN chmod +x /bin/tini

## install all node.js dependencies
COPY package.json package-lock.json .npmrc ./
RUN npm ci

COPY ./prisma ./prisma

ENTRYPOINT ["tini", "--"]
CMD ["./node_modules/.bin/prisma", "migrate", "deploy"]
88 changes: 44 additions & 44 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@
"@rollup/plugin-typescript": "11.1.5",
"@types/auth0": "3.3.10",
"@types/node": "20.10.3",
"@typescript-eslint/eslint-plugin": "6.13.1",
"@typescript-eslint/parser": "6.13.1",
"@typescript-eslint/eslint-plugin": "6.13.2",
"@typescript-eslint/parser": "6.13.2",
"eslint": "8.55.0",
"eslint-config-prettier": "9.1.0",
"eslint-plugin-eslint-comments": "3.2.0",
Expand Down
1 change: 1 addition & 0 deletions src/User/User.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type User implements Node {
): MylistConnection!

likes: Mylist! @auth
like(videoId: ID!): MylistRegistration @auth

hasRole(role: UserRole!): Boolean

Expand Down
21 changes: 20 additions & 1 deletion src/User/User.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import z from "zod";
import { NicovideoRegistrationRequestConnectionDTO } from "../NicovideoRegistrationRequest/dto.js";
import { cursorOptions } from "../resolvers/connection.js";
import { MylistShareRange as GqlMylistShareRange, Resolvers, UserResolvers, UserRole } from "../resolvers/graphql.js";
import { buildGqlId, parseGqlID } from "../resolvers/id.js";
import { buildGqlId, parseGqlID, parseGqlID3 } from "../resolvers/id.js";
import { MylistModel } from "../resolvers/Mylist/model.js";
import { MylistConnectionModel } from "../resolvers/MylistConnection/model.js";
import { MylistRegistrationModel } from "../resolvers/MylistRegistration/model.js";
import { NotificationConnectionModel } from "../resolvers/NotificationConnection/model.js";
import { parseOrderBy } from "../resolvers/parseSortOrder.js";
import { ResolverDeps } from "../resolvers/types.js";
Expand Down Expand Up @@ -162,6 +163,24 @@ export const resolverUserNicovideoRegistrationRequests = ({
export const resolveUser = ({ prisma, logger, userService }: Pick<ResolverDeps, "prisma" | "logger" | "userService">) =>
({
id: ({ id }): string => buildGqlId("User", id),
like: async ({ id: holderId }, { videoId: unparsedVideoId }, { currentUser }, info) => {
if (holderId !== currentUser.id) throw new GraphQLError("Not authenticated");
const videoId = parseGqlID3("Video", unparsedVideoId);
if (isErr(videoId)) {
logger.error({ path: info.path, videoId: unparsedVideoId }, "Invalid video id");
throw new GraphQLError("Invalid video id");
}

const reg = await prisma.mylistRegistration.findFirst({
where: {
mylist: { holderId, slug: "likes" },
videoId: videoId.data,
isRemoved: false,
},
});
if (!reg) return null;
return new MylistRegistrationModel(reg);
},
likes: async ({ id: holderId }, _args, { currentUser }, info) => {
if (holderId !== currentUser.id) throw new GraphQLError("Not authenticated");

Expand Down
2 changes: 1 addition & 1 deletion src/User/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class UserService {
return user;
}

public async getById(userId: string) {
public async getById(userId: string): Promise<UserDTO> {
const cached = await this.redis.get(`auth0:${userId}`);

if (cached) {
Expand Down
11 changes: 2 additions & 9 deletions src/resolvers/Mutation/likeVideo/likeVideo.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,9 @@ input LikeVideoInput {
videoId: ID!
}

union LikeVideoReturnUnion =
MutationVideoNotFoundError
| MutationInvalidVideoIdError
| LikeVideoAlreadyLikedError
| LikeVideoSucceededPayload

type LikeVideoAlreadyLikedError {
registration: MylistRegistration!
}
union LikeVideoReturnUnion = MutationVideoNotFoundError | MutationInvalidVideoIdError | LikeVideoSucceededPayload

type LikeVideoSucceededPayload {
registration: MylistRegistration!
user: User!
}
7 changes: 3 additions & 4 deletions src/resolvers/Mutation/likeVideo/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ export const like = async (
{ videoId, userId }: { videoId: string; userId: string },
): Promise<
Result<
| { type: "VIDEO_NOT_FOUND"; videoId: string }
| { type: "ALREADY_REGISTERED"; registration: MylistRegistration }
| { type: "INTERNAL_SERVER_ERROR"; error: unknown },
{ type: "VIDEO_NOT_FOUND"; videoId: string } | { type: "INTERNAL_SERVER_ERROR"; error: unknown },
MylistRegistration
>
> => {
Expand Down Expand Up @@ -39,7 +37,8 @@ export const like = async (
});

if (ext) {
if (!ext.isRemoved) return err({ type: "ALREADY_REGISTERED", registration: ext });
if (!ext.isRemoved) return ok(ext);

const registration = await prisma.mylistRegistration.update({
where: { id: ext.id },
data: { isRemoved: false, events: { create: { type: "REREGISTER", userId, payload: {} } } },
Expand Down
13 changes: 7 additions & 6 deletions src/resolvers/Mutation/likeVideo/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import { ResolverDeps } from "../../types.js";
import { likeVideoInNeo4j } from "./neo4j.js";
import { like } from "./prisma.js";

export const resolverLikeVideo = ({ prisma, neo4j, logger }: Pick<ResolverDeps, "prisma" | "neo4j" | "logger">) =>
export const resolverLikeVideo = ({
prisma,
neo4j,
logger,
userService,
}: Pick<ResolverDeps, "prisma" | "neo4j" | "logger" | "userService">) =>
(async (_parent, { input: { videoId: videoGqlId } }, { currentUser: user }, info) => {
const videoId = parseGqlID3("Video", videoGqlId);
if (isErr(videoId)) {
Expand All @@ -26,11 +31,6 @@ export const resolverLikeVideo = ({ prisma, neo4j, logger }: Pick<ResolverDeps,
__typename: "MutationVideoNotFoundError",
videoId: buildGqlId("Video", videoId.data),
} satisfies ResolversTypes["LikeVideoReturnUnion"];
case "ALREADY_REGISTERED":
return {
__typename: "LikeVideoAlreadyLikedError",
registration: new MylistRegistrationModel(result.error.registration),
} satisfies ResolversTypes["LikeVideoReturnUnion"];
case "INTERNAL_SERVER_ERROR":
logger.error({ error: result.error.error, path: info.path }, "Something error happens");
throw new GraphQLError("Internal server error");
Expand All @@ -47,5 +47,6 @@ export const resolverLikeVideo = ({ prisma, neo4j, logger }: Pick<ResolverDeps,
return {
__typename: "LikeVideoSucceededPayload",
registration: new MylistRegistrationModel(registration),
user: await userService.getById(user.id),
} satisfies ResolversTypes["LikeVideoReturnUnion"];
}) satisfies MutationResolvers["likeVideo"];
Loading

0 comments on commit 6737d59

Please sign in to comment.