-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
メッセージの通知 subscription #200
base: main
Are you sure you want to change the base?
Changes from all commits
bec934f
7b6ca29
e4b04c0
dc695b4
deabb7d
faa7afc
48afd96
2174189
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,15 +57,11 @@ export const MessageQuery = extendType({ | |
if (!post) { | ||
throw new Error("post doesn't exist"); | ||
} | ||
if (profileId != post.driverId && profileId != post.navigatorId) { | ||
throw new Error("no right to see messages"); | ||
} | ||
validateOwnership(profileId, post); | ||
|
||
return await context.prisma.message.findMany({ | ||
where: { postId }, | ||
orderBy: { | ||
createdAt: "asc", | ||
}, | ||
orderBy: { id: "asc" }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. createdAtが直感的かなと思ったのですが、変更した理由はなんです? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indexつけるの |
||
}); | ||
}, | ||
}); | ||
|
@@ -85,6 +81,14 @@ export const MessageMutation = extendType({ | |
const { postId, content } = args; | ||
const { profileId } = context.expectUserJoinedCommunity(); | ||
|
||
const post = await context.prisma.post.findUnique({ | ||
where: { id: postId }, | ||
}); | ||
if (!post) { | ||
throw new Error("post doesn't exist"); | ||
} | ||
validateOwnership(profileId, post); | ||
|
||
const newMessage = await context.prisma.message.create({ | ||
data: { | ||
post: { | ||
|
@@ -96,8 +100,16 @@ export const MessageMutation = extendType({ | |
}, | ||
}, | ||
}); | ||
const opponentId = ( | ||
post.driverId == profileId ? post.navigatorId : post.driverId | ||
) as number; | ||
|
||
// todo: is 'await' necessary? (https://codesandbox.io/s/nexus-example-subscriptions-59kdb?file=/src/schema/index.ts) | ||
await context.pubsub.publish(postId.toString(), newMessage); | ||
await context.pubsub.publish( | ||
"messageNotification:" + opponentId.toString(), | ||
post | ||
); | ||
return newMessage; | ||
}, | ||
}); | ||
|
@@ -116,9 +128,7 @@ export const MessageMutation = extendType({ | |
if (!post) { | ||
throw new Error("post doesn't exist"); | ||
} | ||
if (profileId != post.driverId && profileId != post.navigatorId) { | ||
throw new Error("no right to see messages"); | ||
} | ||
validateOwnership(profileId, post); | ||
|
||
const where = { postId, createdById: { not: profileId } }; | ||
await context.prisma.message.updateMany({ | ||
|
@@ -138,18 +148,46 @@ export const MessageMutation = extendType({ | |
export const MessageSubscription = subscriptionType({ | ||
definition(t) { | ||
// todo: should it be non-nullable? | ||
t.field("waitForMessage", { | ||
t.nonNull.field("waitForMessage", { | ||
type: "Message", | ||
args: { | ||
postId: nonNull(stringArg()), | ||
}, | ||
subscribe(parent, args, context) { | ||
async subscribe(parent, args, context) { | ||
const { postId } = args; | ||
const { profileId } = context.expectUserJoinedCommunity(); | ||
const post = await context.prisma.post.findUnique({ | ||
where: { id: postId }, | ||
}); | ||
if (!post) { | ||
throw new Error("post doesn't exist"); | ||
} | ||
validateOwnership(profileId, post); | ||
|
||
return context.pubsub.asyncIterator(postId); | ||
}, | ||
async resolve(messagePromise: Promise<Message>) { | ||
return await messagePromise; | ||
}, | ||
}); | ||
|
||
t.nonNull.field("waitForMessageNotification", { | ||
type: "Post", | ||
subscribe(parent, args, context) { | ||
const { profileId } = context.expectUserJoinedCommunity(); | ||
return context.pubsub.asyncIterator( | ||
"messageNotification:" + profileId.toString() | ||
); | ||
}, | ||
async resolve(postPromise: Promise<Post>) { | ||
return await postPromise; | ||
}, | ||
}); | ||
}, | ||
}); | ||
|
||
const validateOwnership = (profileId: number, post: Post) => { | ||
if (profileId != post.driverId && profileId != post.navigatorId) { | ||
throw new Error("no right to see messages"); | ||
} | ||
Comment on lines
+189
to
+192
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good! |
||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wsって何の略ですか?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
web socket ですかね。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ですです