-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: very basic implementation of the conversations API
- Loading branch information
Showing
13 changed files
with
231 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,82 @@ | ||
import { relations } from 'drizzle-orm'; | ||
import { InferSelectModel, relations } from 'drizzle-orm'; | ||
import { pgEnum, pgTable, timestamp, uuid } from 'drizzle-orm/pg-core'; | ||
import { citext, user } from '..'; | ||
|
||
export const conversationType = pgEnum('conversation_type', [ | ||
export const userConversationType = pgEnum('user_conversation_type', [ | ||
'DIRECT', | ||
'GROUP' | ||
]); | ||
|
||
export const conversation = pgTable('conversation', { | ||
id: uuid('id').primaryKey(), | ||
type: conversationType('conversation_type').notNull(), | ||
export const userConversation = pgTable('user_conversation', { | ||
id: uuid('id').defaultRandom().primaryKey(), | ||
name: citext('name'), | ||
type: userConversationType('conversation_type').notNull(), | ||
createdAt: timestamp('created_at').notNull().defaultNow() | ||
}); | ||
|
||
export const conversationRelations = relations(conversation, ({ many }) => ({ | ||
messages: many(conversationMessage), | ||
participants: many(conversationParticipant, { | ||
relationName: 'conversation_participants' | ||
export const userConversationRelations = relations( | ||
userConversation, | ||
({ many }) => ({ | ||
messages: many(userConversationMessage), | ||
participants: many(userConversationParticipant, { | ||
relationName: 'user_conversation_participants' | ||
}) | ||
}) | ||
})); | ||
); | ||
|
||
export const conversationMessage = pgTable('conversation_message', { | ||
id: uuid('id').primaryKey(), | ||
export const userConversationMessage = pgTable('user_conversation_message', { | ||
id: uuid('id').defaultRandom().primaryKey(), | ||
conversationId: uuid('conversation_id').notNull(), | ||
senderId: citext('sender_id').notNull(), | ||
content: citext('content').notNull(), | ||
createdAt: timestamp('created_at').notNull().defaultNow() | ||
}); | ||
|
||
export const conversationMessageRelations = relations( | ||
conversationMessage, | ||
export const userConversationMessageRelations = relations( | ||
userConversationMessage, | ||
({ one }) => ({ | ||
conversation: one(conversation, { | ||
fields: [conversationMessage.conversationId], | ||
references: [conversation.id] | ||
conversation: one(userConversation, { | ||
fields: [userConversationMessage.conversationId], | ||
references: [userConversation.id] | ||
}), | ||
sender: one(conversationParticipant, { | ||
fields: [conversationMessage.senderId], | ||
references: [conversationParticipant.userId] | ||
sender: one(userConversationParticipant, { | ||
fields: [userConversationMessage.senderId], | ||
references: [userConversationParticipant.userId] | ||
}) | ||
}) | ||
); | ||
|
||
export const conversationParticipant = pgTable('conversation_participant', { | ||
conversationId: uuid('conversation_id').notNull(), | ||
userId: citext('user_id').notNull(), | ||
joinedAt: timestamp('joined_at').notNull().defaultNow() | ||
}); | ||
export const userConversationParticipant = pgTable( | ||
'user_conversation_participant', | ||
{ | ||
conversationId: uuid('conversation_id').notNull(), | ||
userId: citext('user_id').notNull(), | ||
joinedAt: timestamp('joined_at').notNull().defaultNow() | ||
} | ||
); | ||
|
||
export const conversationParticipantRelations = relations( | ||
conversationParticipant, | ||
export const userConversationParticipantRelations = relations( | ||
userConversationParticipant, | ||
({ one, many }) => ({ | ||
messages: many(conversationMessage), | ||
conversation: one(conversation, { | ||
fields: [conversationParticipant.conversationId], | ||
references: [conversation.id], | ||
relationName: 'conversation_participants' | ||
messages: many(userConversationMessage), | ||
conversation: one(userConversation, { | ||
fields: [userConversationParticipant.conversationId], | ||
references: [userConversation.id], | ||
relationName: 'user_conversation_participants' | ||
}), | ||
user: one(user, { | ||
fields: [conversationParticipant.userId], | ||
fields: [userConversationParticipant.userId], | ||
references: [user.id] | ||
}) | ||
}) | ||
); | ||
|
||
export type UserConversationSchemaType = InferSelectModel< | ||
typeof userConversation | ||
>; | ||
export type UserConversationMessageSchemaType = InferSelectModel< | ||
typeof userConversationMessage | ||
>; | ||
export type UserConversationParticipantSchemaType = InferSelectModel< | ||
typeof userConversationParticipant | ||
>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Context } from '../../context'; | ||
import { builder } from '../../builder'; | ||
import { db } from '../../drizzle/db'; | ||
import { userConversationMessage } from '../../drizzle/schema'; | ||
import { UserConversationMessage } from '../../types/user/conversation/Message'; | ||
import { pubsub } from '../../pubsub'; | ||
|
||
builder.mutationField('createConversationMessage', (t) => | ||
t.field({ | ||
type: UserConversationMessage, | ||
args: { | ||
content: t.arg.string({ required: true }), | ||
conversationId: t.arg.string({ required: true }) | ||
}, | ||
resolve: async (_root, _args, ctx: Context) => { | ||
pubsub.publish( | ||
`userConversationMessages${_args.conversationId}`, | ||
{} | ||
); | ||
return db | ||
.insert(userConversationMessage) | ||
.values({ | ||
content: _args.content, | ||
conversationId: _args.conversationId, | ||
senderId: '1' // ctx.oidc.sub | ||
}) | ||
.returning() | ||
.then((res) => res[0]); | ||
} | ||
}) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './ProfileField'; | ||
export * from './Relationship'; | ||
export * from './Conversation'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { GraphQLError } from 'graphql'; | ||
import { builder } from '../../builder'; | ||
import { db } from '../../drizzle/db'; | ||
import { User } from '../../types'; | ||
import { Context } from '../../context'; | ||
import { UserConversationMessage } from '../../types/user/conversation/Message'; | ||
|
||
builder.queryField('userConversationMessages', (t) => | ||
t.field({ | ||
type: [UserConversationMessage], | ||
args: { | ||
id: t.arg.string({ required: true }), | ||
limit: t.arg.int({ required: false }), | ||
offset: t.arg.int({ required: false }) | ||
}, | ||
smartSubscription: true, | ||
subscribe: (subscriptions, root, args) => | ||
subscriptions.register(`userConversationMessages${args.id}`), | ||
resolve: async (_root, args, ctx: Context) => { | ||
const messages = await db.query.userConversationMessage.findMany({ | ||
where: (userConversationMessage, { eq }) => | ||
eq(userConversationMessage.conversationId, args.id), | ||
limit: args.limit!, | ||
offset: args.offset!, | ||
orderBy: (userConversationMessage, { desc }) => | ||
desc(userConversationMessage.createdAt) | ||
}); | ||
|
||
return messages; | ||
} | ||
}) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './User'; | ||
export * from './Conversation'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { User } from '../..'; | ||
import { builder } from '../../../builder'; | ||
import { db } from '../../../drizzle/db'; | ||
import { type UserConversationSchemaType } from '../../../drizzle/schema'; | ||
|
||
export const UserConversationType = builder.enumType('UserConversationType', { | ||
values: ['DIRECT', 'GROUP'] | ||
}); | ||
|
||
export const UserConversation = | ||
builder.objectRef<UserConversationSchemaType>('UserConversation'); | ||
|
||
UserConversation.implement({ | ||
fields: (t) => ({ | ||
id: t.exposeString('id', { nullable: false }), | ||
name: t.exposeString('name', { nullable: true }), | ||
type: t.expose('type', { | ||
type: UserConversationType, | ||
nullable: false | ||
}), | ||
createdAt: t.expose('createdAt', { type: 'Date', nullable: false }) | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { User, UserConversation } from '../..'; | ||
import { builder } from '../../../builder'; | ||
import { db } from '../../../drizzle/db'; | ||
import { type UserConversationMessageSchemaType } from '../../../drizzle/schema'; | ||
import { UserConversationParticipant } from './Participant'; | ||
|
||
export const UserConversationMessage = | ||
builder.objectRef<UserConversationMessageSchemaType>( | ||
'UserConversationMessage' | ||
); | ||
|
||
UserConversationMessage.implement({ | ||
fields: (t) => ({ | ||
conversation: t.field({ | ||
type: UserConversation, | ||
nullable: false, | ||
resolve: async (userConversationMessage) => { | ||
const result = await db.query.userConversation.findFirst({ | ||
where: (userConversation, { eq }) => | ||
eq( | ||
userConversation.id, | ||
userConversationMessage.conversationId | ||
) | ||
}); | ||
return result!; | ||
} | ||
}), | ||
sender: t.field({ | ||
type: UserConversationParticipant, | ||
nullable: false, | ||
resolve: async (userConversationMessage) => { | ||
const result = | ||
await db.query.userConversationParticipant.findFirst({ | ||
where: (userConversationParticipant, { eq }) => | ||
eq( | ||
userConversationParticipant.userId, | ||
userConversationMessage.senderId | ||
) | ||
}); | ||
return result!; | ||
} | ||
}), | ||
content: t.exposeString('content', { nullable: false }), | ||
createdAt: t.expose('createdAt', { type: 'Date', nullable: false }) | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { User, UserConversation } from '../..'; | ||
import { builder } from '../../../builder'; | ||
import { db } from '../../../drizzle/db'; | ||
import { type UserConversationParticipantSchemaType } from '../../../drizzle/schema'; | ||
|
||
export const UserConversationParticipant = | ||
builder.objectRef<UserConversationParticipantSchemaType>( | ||
'UserConversationParticipant' | ||
); | ||
|
||
UserConversationParticipant.implement({ | ||
fields: (t) => ({ | ||
user: t.field({ | ||
type: User, | ||
nullable: false, | ||
resolve: async (userConversationParticipant) => { | ||
const result = await db.query.user.findFirst({ | ||
where: (user, { eq }) => | ||
eq(user.id, userConversationParticipant.userId) | ||
}); | ||
return result!; | ||
} | ||
}), | ||
conversation: t.field({ | ||
type: UserConversation, | ||
nullable: false, | ||
resolve: async (userConversationParticipant) => { | ||
const result = await db.query.userConversation.findFirst({ | ||
where: (userConversation, { eq }) => | ||
eq( | ||
userConversation.id, | ||
userConversationParticipant.conversationId | ||
) | ||
}); | ||
return result!; | ||
} | ||
}), | ||
joinedAt: t.expose('joinedAt', { type: 'Date', nullable: false }) | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './Conversation'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters