Compare commits

..

No commits in common. "68c38a1a4794c3a6c3fac13c5cec8c726b5b96a1" and "6ab0eb83514b6f640445ca86572e5b230b750cee" have entirely different histories.

7 changed files with 22 additions and 34 deletions

View file

@ -15,12 +15,11 @@ export const GatewayErrors = {
export const GatewayPayloadType = {
Hello: 0,
Authenticate: 1, // client
Authenticate: 1,
Ready: 2,
Ping: 3, // client
Ping: 3,
RPCRequest: 4, // client
RPCSignal: 5, // client
RPCResponse: 6,
RPCResponse: 5,
ChannelCreate: 110,
ChannelUpdate: 111,
@ -195,11 +194,11 @@ export default {
this.handlers.delete(event);
}
},
sendRPCRequest(calls, isSignal) {
sendRPCRequest(calls) {
return new Promise((resolve, _reject) => {
this.waitingSerials.set(this.serial, resolve);
this.send({
t: isSignal ? GatewayPayloadType.RPCSignal : GatewayPayloadType.RPCRequest,
t: GatewayPayloadType.RPCRequest,
d: calls,
s: this.serial
});

View file

@ -94,11 +94,11 @@ export default function doRequest(method, endpoint, auth=true, body=null) {
});
}
export async function remoteCall({methodId, requiresAuthentication, cacheable, _isSignal=false}, ...args) {
export async function remoteCall({methodId, requiresAuthentication, cacheable}, ...args) {
const calls = [[methodId, ...args]];
if (requiresAuthentication && gateway.authenticated && !cacheable) {
const replies = await gateway.sendRPCRequest(calls, _isSignal);
const ok = Array.isArray(replies) && !(replies[0] && replies[0].code);
const replies = await gateway.sendRPCRequest(calls);
const ok = Array.isArray(replies) && replies[0] && !replies[0].code;
return {
json: ok ? replies[0] : null,
ok
@ -111,14 +111,7 @@ export async function remoteCall({methodId, requiresAuthentication, cacheable, _
} else {
response = await doRequest("POST", apiRoute("rpc"), requiresAuthentication, calls);
}
response.ok = response.ok && Array.isArray(response.json) && !(response.json[0] && response.json[0].code);
response.ok = response.ok && Array.isArray(response.json) && response.json[0] && !response.json[0].code;
response.json = response.ok ? response.json[0] : null;
return response;
}
export async function remoteSignal(method, ...args) {
return await remoteCall({
...method,
_isSignal: true
}, ...args);
}

View file

@ -1,6 +1,6 @@
import gateway, { GatewayEventType, GatewayPayloadType, GatewayPresenceStatus } from "./gateway";
import logger from "./logging";
import { methods, remoteCall, remoteSignal } from "./request";
import { methods, remoteCall } from "./request";
import { getItem, setItem } from "./storage";
const storeLog = logger("Store");
@ -539,7 +539,7 @@ class TypingStore extends Store {
this.startedTyping(userInfoStore.value, selectedChannel.value.id, 6500);
if (this.ownNeedsUpdate) {
this.ownNeedsUpdate = false;
await remoteSignal(methods.putChannelTyping, selectedChannel.value.id);
await remoteCall(methods.putChannelTyping, selectedChannel.value.id);
}
}
}
@ -695,9 +695,11 @@ export const sendMessageAction = createAction("sendMessageAction", async ({chann
const messagesStoreForChannel = messagesStoreProvider.getStore(channelId);
messagesStoreForChannel.addMessage(optimisticMessage);
const res = await remoteSignal(methods.createChannelMessage, channelId, content, optimisticMessageId, null);
const res = await remoteCall(methods.createChannelMessage, channelId, content, optimisticMessageId, null);
if (!res.ok) {
if (res.ok) {
messagesStoreForChannel.setMessage(optimisticMessageId, res.json);
} else {
messagesStoreForChannel.deleteMessage({
id: optimisticMessageId
});

View file

@ -4,7 +4,6 @@ export enum GatewayPayloadType {
Ready,
Ping, // client
RPCRequest, // client
RPCSignal, // client
RPCResponse,
ChannelCreate = 110,

View file

@ -409,19 +409,20 @@ export default function(server: Server) {
ws.state.alive = true;
break;
}
case GatewayPayloadType.RPCSignal: /* through */
case GatewayPayloadType.RPCRequest: {
if (!ws.state.ready || !ws.state.user) {
return closeWithError(ws, gatewayErrors.NOT_AUTHENTICATED);
}
// RPCSignal is like RPCRequest however it does not send RPC method output unless there is an error
processMethodBatch(ws.state.user, payload.d, (payload.t === GatewayPayloadType.RPCSignal ? true : false)).then((results) => {
processMethodBatch(ws.state.user, payload.d).then((results) => {
sendPayload(ws, {
t: GatewayPayloadType.RPCResponse,
d: results,
s: payload.s
});
}).catch(e => {
console.error("gateway: unexpected error while handling RPCRequest", e);
return closeWithError(ws, gatewayErrors.INTERNAL_ERROR);
});
break;
}

View file

@ -121,7 +121,7 @@ export const userInvokeMethod = async (user: User | null, methodId: number, args
}
};
export const processMethodBatch = async (user: User | null, calls: any, ignoreNonErrors = false) => {
export const processMethodBatch = async (user: User | null, calls: any) => {
if (!Array.isArray(calls) || !calls.length || calls.length > 5) {
return {
...errors.BAD_REQUEST,
@ -141,13 +141,7 @@ export const processMethodBatch = async (user: User | null, calls: any, ignoreNo
}
const promise = userInvokeMethod(user, call[0], call.slice(1, call.length));
promise.then(value => {
if (ignoreNonErrors && !value.code) {
responses[index] = null;
} else {
responses[index] = value;
}
});
promise.then(value => responses[index] = value);
promises[index] = promise;
});