This commit is contained in:
Hongbo Wu 2024-06-18 11:01:00 +08:00
parent 091af055c0
commit 81b0fc19a2
5 changed files with 54 additions and 32 deletions

View file

@ -11,6 +11,7 @@ import {
EXISTING_NEWSLETTER_FOLDER,
NewsletterEmail,
} from '../entity/newsletter_email'
import { Post } from '../entity/post'
import { PublicItem } from '../entity/public_item'
import { Recommendation } from '../entity/recommendation'
import {
@ -780,19 +781,25 @@ export const functionResolvers = {
recommendedAt: (recommendation: Recommendation) => recommendation.createdAt,
},
Post: {
author(post: { userId: string }, _: unknown, ctx: WithDataSourcesContext) {
author(post: Post, _: never, ctx: ResolverContext) {
return ctx.dataLoaders.users.load(post.userId)
},
ownedByViewer(post: { userId: string }, ctx: WithDataSourcesContext) {
return post.userId === ctx.uid
ownedByViewer(post: Post, _: never, ctx: ResolverContext) {
console.log('ownedByViewer: ctx.claims?.uid', ctx.claims?.uid)
return post.userId === ctx.claims?.uid
},
libraryItems(
post: { libraryItemIds: string[] },
ctx: WithDataSourcesContext
_: never,
ctx: ResolverContext
) {
return ctx.dataLoaders.libraryItems.loadMany(post.libraryItemIds)
},
highlights(post: { highlightIds: string[] }, ctx: WithDataSourcesContext) {
highlights(
post: { highlightIds: string[] },
_: never,
ctx: ResolverContext
) {
return ctx.dataLoaders.highlights.loadMany(post.highlightIds)
},
},

View file

@ -6,7 +6,7 @@ import {
QueryPostsArgs,
ResolverFn,
} from '../../generated/graphql'
import { findPostsByUserId } from '../../services/post'
import { findPublicPostsByUserId } from '../../services/post'
import { Merge } from '../../util'
import { ResolverContext } from '../types'
@ -38,7 +38,8 @@ export const postsResolver: ResolverFn<
}
}
const posts = await findPostsByUserId(userId, limit + 1, offset)
const posts = await findPublicPostsByUserId(userId, limit + 1, offset)
console.log(posts)
const hasNextPage = posts.length > limit
if (hasNextPage) {

View file

@ -2,7 +2,7 @@ import { DeepPartial } from 'typeorm'
import { Post } from '../entity/post'
import { authTrx, getRepository } from '../repository'
export const findPostsByUserId = async (
export const findPublicPostsByUserId = async (
userId: string,
limit: number,
offset: number
@ -30,11 +30,9 @@ export const createPosts = async (
userId: string,
posts: Array<DeepPartial<Post>>
) => {
return authTrx(
async (trx) => trx.getRepository(Post).save(posts),
undefined,
userId
)
return authTrx(async (trx) => trx.getRepository(Post).save(posts), {
uid: userId,
})
}
export const deletePosts = async (userId: string, postIds: string[]) => {
@ -42,7 +40,8 @@ export const deletePosts = async (userId: string, postIds: string[]) => {
async (trx) => {
await trx.getRepository(Post).delete(postIds)
},
undefined,
userId
{
uid: userId,
}
)
}

View file

@ -1,6 +1,6 @@
import { Profile } from '../entity/profile'
import { User } from '../entity/user'
import { getRepository } from '../repository'
import { authTrx, getRepository } from '../repository'
export const findProfile = async (user: User): Promise<Profile | null> => {
return getRepository(Profile).findOneBy({ user: { id: user.id } })
@ -9,14 +9,13 @@ export const findProfile = async (user: User): Promise<Profile | null> => {
export const updateProfile = async (
userId: string,
profile: Partial<Profile>
): Promise<Profile> => {
const profileRepository = getRepository(Profile)
const existingProfile = await findProfile(user)
if (!existingProfile) {
return profileRepository.save({ ...profile, user })
}
const updatedProfile = { ...existingProfile, ...profile }
return profileRepository.save(updatedProfile)
) => {
return authTrx(
(tx) => {
return tx.getRepository(Profile).update({ user: { id: userId } }, profile)
},
{
uid: userId,
}
)
}

View file

@ -1,7 +1,7 @@
import { expect } from 'chai'
import { User } from '../../src/entity/user'
import { updateUserProfileResolver } from '../../src/resolvers'
import { createPosts, deletePosts } from '../../src/services/post'
import { updateProfile } from '../../src/services/profile'
import { deleteUser } from '../../src/services/user'
import { createTestUser } from '../db'
import { graphqlRequest, loginAndGetAuthToken } from '../util'
@ -56,11 +56,13 @@ describe('Post Resolvers', () => {
title: 'Post 1',
content: 'Content 1',
user: loginUser,
createdAt: new Date('2021-01-01'),
},
{
title: 'Post 2',
content: 'Content 2',
user: loginUser,
createdAt: new Date('2021-01-02'),
},
]
const newPosts = await createPosts(loginUser.id, posts)
@ -95,9 +97,15 @@ describe('Post Resolvers', () => {
})
context('when the user is not authenticated', () => {
context('when the posts are public', () => {
context('when user profile is public', () => {
before(async () => {
await updateUserProfileResolver
await updateProfile(loginUser.id, { private: false })
})
after(async () => {
await updateProfile(loginUser.id, { private: true })
})
it('should return posts', async () => {
const response = await graphqlRequest(query, '', {
first: 10,
@ -107,18 +115,26 @@ describe('Post Resolvers', () => {
expect(response.body.data.posts.edges[0].node.id).to.eql(postIds[1])
expect(response.body.data.posts.edges[1].node.id).to.eql(postIds[0])
expect(response.body.data.posts.edges[0].node.ownedByViewer).to.be
.true
.false
})
})
context('when the posts are private', () => {
context('when user profile is private', () => {
before(async () => {
await updateProfile(loginUser.id, { private: true })
})
after(async () => {
await updateProfile(loginUser.id, { private: false })
})
it('should return empty array', async () => {
const response = await graphqlRequest(query, '', {
first: 10,
userId: loginUser.id,
})
expect(response.body.data.posts.errorCodes).to.eql(['UNAUTHORIZED'])
expect(response.body.data.posts.edges).to.be.empty
})
})
})