Skip to content

Commit

Permalink
release: v1.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
joshxfi authored Aug 23, 2023
2 parents d55f887 + 095cdf7 commit 8b05213
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 28 deletions.
2 changes: 1 addition & 1 deletion apps/web/src/components/Dialog/GlobalMsg.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const GlobalMsg = ({ setIsOpen, message, ...rest }: Props) => {
: message.user?.username) || 'anonymous'}
</p>
<p className='text-secondary-400 text-center text-sm'>
{formatDistanceToNow(new Date(message.createdAt), {
{formatDistanceToNow(new Date(message.updatedAt), {
addSuffix: true,
})}
</p>
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/components/GlobalPost.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const GlobalPost = ({ message }: { message: GlobalMessage }) => {
'anonymous'}
</p>
<p className='text-secondary-400 text-center text-sm'>
{formatDistanceToNow(new Date(message.createdAt), {
{formatDistanceToNow(new Date(message.updatedAt), {
addSuffix: true,
})}
</p>
Expand Down
2 changes: 2 additions & 0 deletions apps/web/src/pages/global.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ const Global: NextPageWithLayout = () => {
<button
className='hover:underline'
onClick={() => {
window.scrollTo({ top: 0, behavior: 'smooth' });
setPageNo(1);
setCursorId('');
}}
Expand All @@ -138,6 +139,7 @@ const Global: NextPageWithLayout = () => {
<button
className='hover:underline'
onClick={() => {
window.scrollTo({ top: 0, behavior: 'smooth' });
setPageNo(cursorId ? pageNo + 1 : 2);
setCursorId(messages?.length ? messages[9]?.id! : '');
}}
Expand Down
66 changes: 45 additions & 21 deletions apps/web/src/schema/message/message.resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class MessageResolver {

if (!cursorId) {
messages = await prisma.globalMessage.findMany({
orderBy: { createdAt: 'desc' },
orderBy: { updatedAt: 'desc' },
take: 10,
include: {
user: {
Expand All @@ -54,7 +54,7 @@ export class MessageResolver {
});
} else {
messages = await prisma.globalMessage.findMany({
orderBy: { createdAt: 'desc' },
orderBy: { updatedAt: 'desc' },
take: 10,
skip: 1,
cursor: {
Expand Down Expand Up @@ -86,16 +86,18 @@ export class MessageResolver {
{ content, isAnonymous }: SendGlobalMessageInput,
@Ctx() { prisma, id }: TContext
): Promise<SendGlobalMessage> {
let latestMessage: Omit<GlobalMessage, 'user'> | null;

try {
const latestMessage = await prisma.globalMessage.findFirst({
latestMessage = await prisma.globalMessage.findFirst({
where: { userId: id },
orderBy: { createdAt: 'desc' },
orderBy: { updatedAt: 'desc' },
});

if (latestMessage?.createdAt) {
const diff = new Date().getTime() - latestMessage.createdAt.getTime();
if (latestMessage?.updatedAt) {
const diff = new Date().getTime() - latestMessage.updatedAt.getTime();

if (diff < 1000 * 60 * 5) {
if (diff < 1000 * 60 * 5 && process.env.NODE_ENV !== 'development') {
return {
error: 'You can only send a message once every 5 minutes.',
};
Expand All @@ -107,22 +109,44 @@ export class MessageResolver {
}

try {
const message = await prisma.globalMessage.create({
data: {
content,
isAnonymous,
user: id ? { connect: { id } } : undefined,
},
include: {
user: {
select: {
id: true,
username: true,
image: true,
let message: GlobalMessage;

if (!latestMessage) {
message = await prisma.globalMessage.create({
data: {
content,
isAnonymous,
user: id ? { connect: { id } } : undefined,
},
include: {
user: {
select: {
id: true,
username: true,
image: true,
},
},
},
},
});
});
} else {
message = await prisma.globalMessage.update({
where: { id: latestMessage?.id },
data: {
content,
isAnonymous,
user: id ? { connect: { id } } : undefined,
},
include: {
user: {
select: {
id: true,
username: true,
image: true,
},
},
},
});
}

return { data: message };
} catch (err) {
Expand Down
3 changes: 3 additions & 0 deletions apps/web/src/schema/message/message.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ export class GlobalMessage {
@Field(() => Date)
createdAt: Date;

@Field(() => Date)
updatedAt: Date;

@Field(() => GlobalMessageUser, { nullable: true })
user: GlobalMessageUser | null;
}
Expand Down
5 changes: 2 additions & 3 deletions packages/db/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ model Message {
receiverId String
receiver User @relation(name: "receivedMessages", fields: [receiverId], references: [id], onDelete: Cascade)
@@index([senderId, createdAt(sort: Desc)])
@@index([receiverId, createdAt(sort: Desc)])
@@index([senderId, receiverId, isOpened, createdAt(sort: Desc)])
}

model GlobalMessage {
Expand All @@ -88,5 +87,5 @@ model GlobalMessage {
userId String?
user User? @relation(name: "sentGlobalMessages", fields: [userId], references: [id], onDelete: Cascade)
@@index([userId, createdAt(sort: Desc)])
@@index([userId, updatedAt(sort: Desc)])
}
2 changes: 2 additions & 0 deletions packages/generated/graphql/message.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ query getGlobalMessages($cursorId: ID) {
id
content
createdAt
updatedAt
isAnonymous
user {
id
Expand Down Expand Up @@ -63,6 +64,7 @@ mutation sendGlobalMessage($input: SendGlobalMessageInput!) {
id
content
createdAt
updatedAt
isAnonymous
user {
id
Expand Down
7 changes: 5 additions & 2 deletions packages/generated/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export type GlobalMessage = {
createdAt: Scalars['DateTime']['output'];
id: Scalars['ID']['output'];
isAnonymous: Scalars['Boolean']['output'];
updatedAt: Scalars['DateTime']['output'];
user?: Maybe<GlobalMessageUser>;
};

Expand Down Expand Up @@ -201,7 +202,7 @@ export type GetGlobalMessagesQueryVariables = Exact<{
}>;


export type GetGlobalMessagesQuery = { __typename?: 'Query', getGlobalMessages?: Array<{ __typename?: 'GlobalMessage', id: string, content: string, createdAt: any, isAnonymous: boolean, user?: { __typename?: 'GlobalMessageUser', id: string, username?: string | null, image?: string | null } | null }> | null };
export type GetGlobalMessagesQuery = { __typename?: 'Query', getGlobalMessages?: Array<{ __typename?: 'GlobalMessage', id: string, content: string, createdAt: any, updatedAt: any, isAnonymous: boolean, user?: { __typename?: 'GlobalMessageUser', id: string, username?: string | null, image?: string | null } | null }> | null };

export type GetRecentMessagesQueryVariables = Exact<{
cursorId?: InputMaybe<Scalars['ID']['input']>;
Expand Down Expand Up @@ -251,7 +252,7 @@ export type SendGlobalMessageMutationVariables = Exact<{
}>;


export type SendGlobalMessageMutation = { __typename?: 'Mutation', sendGlobalMessage: { __typename?: 'SendGlobalMessage', error?: string | null, data?: { __typename?: 'GlobalMessage', id: string, content: string, createdAt: any, isAnonymous: boolean, user?: { __typename?: 'GlobalMessageUser', id: string, username?: string | null, image?: string | null } | null } | null } };
export type SendGlobalMessageMutation = { __typename?: 'Mutation', sendGlobalMessage: { __typename?: 'SendGlobalMessage', error?: string | null, data?: { __typename?: 'GlobalMessage', id: string, content: string, createdAt: any, updatedAt: any, isAnonymous: boolean, user?: { __typename?: 'GlobalMessageUser', id: string, username?: string | null, image?: string | null } | null } | null } };

export type AddReplyMutationVariables = Exact<{
id: Scalars['ID']['input'];
Expand Down Expand Up @@ -310,6 +311,7 @@ export const GetGlobalMessagesDocument = gql`
id
content
createdAt
updatedAt
isAnonymous
user {
id
Expand Down Expand Up @@ -377,6 +379,7 @@ export const SendGlobalMessageDocument = gql`
id
content
createdAt
updatedAt
isAnonymous
user {
id
Expand Down

1 comment on commit 8b05213

@vercel
Copy link

@vercel vercel bot commented on 8b05213 Aug 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

umamin – ./

umamin-omsimos.vercel.app
umamin-git-main-omsimos.vercel.app
www.umamin.link
umamin.link

Please sign in to comment.