Skip to content

Commit

Permalink
deprecation: bookmarks feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Creaous committed Nov 13, 2024
1 parent 40ae865 commit 2cde604
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 78 deletions.
57 changes: 57 additions & 0 deletions src/drizzle/schema/post/Collection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { pgTable, primaryKey, text, uuid } from 'drizzle-orm/pg-core';
import { post, user } from '..';
import { relations } from 'drizzle-orm';

export const postCollection = pgTable(
'post_collection',
{
id: uuid('id').notNull().defaultRandom(),
name: text('name').notNull(),
description: text('description').notNull(),
userId: text('user_id')
.notNull()
.references(() => user.id)
},
(t) => ({
pk: primaryKey(t.id, t.name, t.userId)
})
);

export const postCollectionRelations = relations(postCollection, ({ one }) => ({
user: one(user, {
fields: [postCollection.userId],
references: [user.id],
relationName: 'user__collections'
})
}));

export const postCollectionItem = pgTable(
'post_collection_item',
{
collectionId: text('collection_id')
.notNull()
.references(() => postCollection.id),
postId: uuid('post_id')
.notNull()
.references(() => post.id)
},
(t) => ({
pk: primaryKey(t.collectionId, t.postId)
})
);

export const postCollectionItemRelations = relations(
postCollectionItem,
({ one }) => ({
collection: one(postCollection, {
fields: [postCollectionItem.collectionId],
references: [postCollection.id],
relationName: 'collection__items'
}),
post: one(post, {
fields: [postCollectionItem.postId],
references: [post.id],
relationName: 'post__collections'
})
})
);
1 change: 0 additions & 1 deletion src/drizzle/schema/post/Interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { user } from '..';

export const postInteractionType = pgEnum('post_interaction_type', [
'LIKE',
'BOOKMARK',
'REPOST'
]);

Expand Down
2 changes: 1 addition & 1 deletion src/mutations/post/Interaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { db } from '../../drizzle/db';
import { post } from '../../drizzle/schema';
import { createUser, makeGQLRequest, removeUser } from '../../lib/tests';

const types = ['like', 'bookmark', 'repost'];
const types = ['like', 'repost'];

for (const type of types) {
test(`Authenticated | Interactions - It should check ${type}s`, async () => {
Expand Down
32 changes: 9 additions & 23 deletions src/mutations/post/Interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,14 @@ import { builder } from '../../builder';
import { db } from '../../drizzle/db';
import {
postInteraction,
PostInteraction as PostInteractionSchema
PostInteractionSchemaType
} from '../../drizzle/schema';
import { privacyGuardian } from '../../lib/guardian';
import { PostInteraction } from '../../types/post/Interaction';
import { Context } from '../../context';

// Define the possible interaction types
const interactionTypes = [
'LIKE',
'UNLIKE',
'BOOKMARK',
'UNBOOKMARK',
'REPOST',
'UNREPOST'
] as const;
const interactionTypes = ['LIKE', 'UNLIKE', 'REPOST', 'UNREPOST'] as const;

// Create mutation fields for each interaction type
interactionTypes.forEach((type) => {
Expand All @@ -39,8 +32,8 @@ interactionTypes.forEach((type) => {
async function postInteract(
ctx: Context,
args: { id: string; reason?: string | null },
type: 'LIKE' | 'UNLIKE' | 'BOOKMARK' | 'UNBOOKMARK' | 'REPOST' | 'UNREPOST'
): Promise<PostInteractionSchema | null> {
type: 'LIKE' | 'UNLIKE' | 'REPOST' | 'UNREPOST'
): Promise<PostInteractionSchemaType | null> {
// Check if ID is provided
if (!args.id) {
throw new GraphQLError('You must provide an ID.', {
Expand Down Expand Up @@ -71,8 +64,6 @@ async function postInteract(
const interactionMap = {
LIKE: 'LIKE',
UNLIKE: 'UNLIKE',
BOOKMARK: 'BOOKMARK',
UNBOOKMARK: 'BOOKMARK',
REPOST: 'REPOST',
UNREPOST: 'REPOST'
};
Expand All @@ -81,8 +72,6 @@ async function postInteract(
const errorMap = {
LIKE: 'You have already liked this post.',
UNLIKE: 'You are not currently liking this post.',
BOOKMARK: 'You have already bookmarked this post.',
UNBOOKMARK: 'You are not currently bookmarking this post.',
REPOST: 'You have already reposted this post.',
UNREPOST: 'You are not currently reposting this post.'
};
Expand All @@ -96,16 +85,13 @@ async function postInteract(
eq(postInteraction.postId, args.id),
eq(
postInteraction.type,
requestedType.replace('UN', '') as
| 'LIKE'
| 'BOOKMARK'
| 'REPOST'
requestedType.replace('UN', '') as 'LIKE' | 'REPOST'
)
)
});

// Handle LIKE, BOOKMARK, REPOST actions
if (['LIKE', 'BOOKMARK', 'REPOST'].includes(type)) {
// Handle LIKE & REPOST actions
if (['LIKE', 'REPOST'].includes(type)) {
if (existingInteraction) {
throw new GraphQLError(errorMap[type], {
extensions: { code: `POST_ALREADY_${type}ED` }
Expand All @@ -116,7 +102,7 @@ async function postInteract(
.values({
userId: ctx.oidc.sub,
postId: args.id,
type: type as 'LIKE' | 'BOOKMARK' | 'REPOST'
type: type as 'LIKE' | 'REPOST'
})
.returning()
.then((res) => res[0]);
Expand All @@ -135,7 +121,7 @@ async function postInteract(
eq(postInteraction.postId, args.id),
eq(
postInteraction.type,
type.replace('UN', '') as 'LIKE' | 'BOOKMARK' | 'REPOST'
type.replace('UN', '') as 'LIKE' | 'REPOST'
)
)
)
Expand Down
6 changes: 3 additions & 3 deletions src/mutations/user/Relationship.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { GraphQLError } from 'graphql';
import { builder } from '../../builder';
import { db } from '../../drizzle/db';
import {
UserRelationship as UserRelationshipSchema,
userRelationship
userRelationship,
UserRelationshipSchemaType
} from '../../drizzle/schema';
import { Context } from '../../context';
import { UserRelationship } from '../../types';
Expand Down Expand Up @@ -143,7 +143,7 @@ async function modifyRelationship(
ctx: Context,
args: { id: string; reason?: string | null },
type: 'BLOCK' | 'UNBLOCK' | 'MUTE' | 'UNMUTE' | 'FOLLOW' | 'UNFOLLOW'
): Promise<UserRelationshipSchema | null> {
): Promise<UserRelationshipSchemaType | null> {
// Check if ID is provided
if (!args.id) {
throw new GraphQLError('You must provide an ID.', {
Expand Down
36 changes: 2 additions & 34 deletions src/types/post/Post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,8 @@ Post.implement({
unauthorizedResolver: () => [],
resolve: async (post, args, context: Context) => {
const result = await db.query.postInteraction.findMany({
where: (postInteraction, { and, eq, ne }) =>
and(
eq(postInteraction.postId, post.id),
ne(postInteraction.type, 'BOOKMARK')
),
where: (postInteraction, { eq }) =>
eq(postInteraction.postId, post.id),
with: {
user: true,
post: true
Expand Down Expand Up @@ -141,21 +138,6 @@ Post.implement({
}),
createdAt: t.expose('createdAt', { type: 'Date', nullable: false }),
updatedAt: t.expose('updatedAt', { type: 'Date', nullable: false }),
bookmarked: t.field({
type: 'Boolean',
nullable: false,
resolve: async (parent, _args, context: Context) => {
const result = await db.query.postInteraction.findMany({
where: (postInteraction, { and, eq }) =>
and(
eq(postInteraction.postId, parent.id),
eq(postInteraction.userId, context.oidc?.sub),
eq(postInteraction.type, 'BOOKMARK')
)
});
return result!.length > 0;
}
}),
liked: t.field({
type: 'Boolean',
nullable: false,
Expand Down Expand Up @@ -185,20 +167,6 @@ Post.implement({
return result!.length ?? 0;
}
}),
bookmarksCount: t.field({
type: 'Int',
nullable: false,
resolve: async (parent) => {
const result = await db.query.postInteraction.findMany({
where: (postInteraction, { and, eq }) =>
and(
eq(postInteraction.postId, parent.id),
eq(postInteraction.type, 'BOOKMARK')
)
});
return result!.length ?? 0;
}
}),
/* TODO: IMPLEMENT */
repostsCount: t.field({
type: 'Int',
Expand Down
17 changes: 1 addition & 16 deletions src/types/user/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ User.implement({
},
unauthorizedResolver: () => [] as any,
resolve: async (user, args, context: Context) => {
const type = args.type! as 'LIKE' | 'BOOKMARK' | 'REPOST';
const type = args.type! as 'LIKE' | 'REPOST';

const result = await db.query.postInteraction.findMany({
where: (postInteraction, { and, eq, ne }) =>
Expand All @@ -158,7 +158,6 @@ User.implement({
)
: and(
eq(postInteraction.userId, user.id),
ne(postInteraction.type, 'BOOKMARK'),
type && eq(postInteraction.type, type)
),
with: {
Expand Down Expand Up @@ -219,20 +218,6 @@ User.implement({
}),
createdAt: t.expose('createdAt', { type: 'Date', nullable: false }),
updatedAt: t.expose('updatedAt', { type: 'Date', nullable: false }),
bookmarksCount: t.field({
type: 'Int',
nullable: false,
resolve: async (user) => {
const result = await db.query.postInteraction.findMany({
where: (postInteraction, { and, eq }) =>
and(
eq(postInteraction.userId, user.id),
eq(postInteraction.type, 'BOOKMARK')
)
});
return result!.length ?? 0;
}
}),
likesCount: t.field({
type: 'Int',
nullable: false,
Expand Down

0 comments on commit 2cde604

Please sign in to comment.