Skip to content

Commit

Permalink
Merge pull request #5 from drawn-app/feature/chat-service
Browse files Browse the repository at this point in the history
Support HTTP to gRPC translation
  • Loading branch information
jutatuch authored Oct 19, 2024
2 parents e0b436e + 6683585 commit 3279eb2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
33 changes: 27 additions & 6 deletions proto/chat.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,33 @@ package chat;
import "google/protobuf/timestamp.proto";

service Chat {
rpc GetMessages (RoomRequest) returns (MessageList) {}
rpc SendMessage (MessageRequest) returns (Empty) {}
rpc UpdateMessage (UpdateMessageRequest) returns (Empty) {}
rpc DeleteMessage (DeleteMessageRequest) returns (Empty) {}
rpc ReceiveMessages (RoomRequest) returns (stream ReceiveMessageResponse) {}
rpc GetMessages (RoomRequest) returns (MessageList) {
option (google.api.http) = {
get: "/chat/{workspaceId}/messages"
};
}
rpc SendMessage (MessageRequest) returns (Empty) {
option (google.api.http) = {
post: "/chat/{workspaceId}/messages"
body: "*"
};
}
rpc UpdateMessage (UpdateMessageRequest) returns (Empty) {
option (google.api.http) = {
put: "/chat/{workspaceId}/messages/{id}"
body: "*"
};
}
rpc DeleteMessage (DeleteMessageRequest) returns (Empty) {
option (google.api.http) = {
delete: "/chat/{workspaceId}/messages/{id}"
};
}
rpc ReceiveMessages (RoomRequest) returns (stream ReceiveMessageResponse) {
option (google.api.http) = {
post: "/chat/{workspaceId}/receive"
};
}
}

message Empty {}
Expand All @@ -30,7 +52,6 @@ message MessageRequest {
}

message RoomRequest {
string userId = 1;
int32 workspaceId = 2;
}

Expand Down
2 changes: 0 additions & 2 deletions proto/generatedTypes/chat/RoomRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@


export interface RoomRequest {
'userId'?: (string);
'workspaceId'?: (number);
}

export interface RoomRequest__Output {
'userId'?: (string);
'workspaceId'?: (number);
}
20 changes: 12 additions & 8 deletions services/chatService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import { DeleteMessageRequest__Output } from "../proto/generatedTypes/chat/Delet

export async function GetMessages(call: ServerUnaryCall<RoomRequest__Output, MessageList>, callback: sendUnaryData<MessageList>) {
console.log('GetMessages called with', call.request)
const userId = (call.metadata.get('x-id').length > 0) ? call.metadata.get('x-id')[0] : null
const role = (call.metadata.get('x-role').length > 0) ? call.metadata.get('x-role')[0] : null

// Validate request
if (!call.request.userId) {
if (!userId || !role) {
callback({
code: status.INVALID_ARGUMENT,
message: 'userId is required'
code: status.UNAUTHENTICATED,
message: 'User not authenticated'
})
return
}
Expand Down Expand Up @@ -92,9 +94,11 @@ export async function SendMessage(call: ServerUnaryCall<MessageRequest__Output,

export async function ReceiveMessages(call: ServerWritableStream<RoomRequest__Output, ReceiveMessageResponse>) {
console.log('ReceiveMessages called with', call.request)
const userId = (call.metadata.get('x-id').length > 0) ? call.metadata.get('x-id')[0] : null
const role = (call.metadata.get('x-role').length > 0) ? call.metadata.get('x-role')[0] : null

// Validate request
if (!call.request.userId) {
if (!userId || !role) {
call.emit('error', {
code: status.INVALID_ARGUMENT,
message: 'userId is required'
Expand All @@ -105,22 +109,22 @@ export async function ReceiveMessages(call: ServerWritableStream<RoomRequest__Ou
if (!call.request.workspaceId) {
call.emit('error', {
code: status.INVALID_ARGUMENT,
message: 'userId is required'
message: 'workspaceId is required'
})
return
}

call.on('cancelled', () => {
console.log('Stream cancelled', call.request)
chatManager.removeSession(call.request.workspaceId || -1, call.request.userId || "")
chatManager.removeSession(call.request.workspaceId || -1, userId.toString() || "")
})

call.on('error', function(e) {
console.log('Stream error', call.request)
chatManager.removeSession(call.request.workspaceId || -1, call.request.userId || "")
chatManager.removeSession(call.request.workspaceId || -1, userId.toString() || "")
});

chatManager.addSession(call.request.workspaceId, call.request.userId, call)
chatManager.addSession(call.request.workspaceId, userId.toString(), call)

}

Expand Down

0 comments on commit 3279eb2

Please sign in to comment.