Skip to content
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

Allow longer timeout when submitting a proposal #5811

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions frontend/openchat-client/src/agentWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { OpenChatConfig } from "./config";
import { v4 } from "uuid";
import { Stream } from "openchat-shared";

const WORKER_TIMEOUT = 1000 * 90;
export const DEFAULT_WORKER_TIMEOUT = 1000 * 90;

type UnresolvedRequest = {
kind: string;
Expand Down Expand Up @@ -168,7 +168,7 @@ export class OpenChatAgentWorker extends EventTarget {
reject,
timeout: window.setTimeout(() => {
reject(
`WORKER_CLIENT: Request of kind ${req.kind} with correlationId ${correlationId} did not receive a response withing the ${WORKER_TIMEOUT}ms timeout`,
`WORKER_CLIENT: Request of kind ${req.kind} with correlationId ${correlationId} did not receive a response withing the ${DEFAULT_WORKER_TIMEOUT}ms timeout`,
);
this._unresolved.set(correlationId, {
kind: req.kind,
Expand All @@ -183,7 +183,7 @@ export class OpenChatAgentWorker extends EventTarget {
sendStreamRequest<Req extends WorkerRequest>(
req: Req,
connecting = false,
timeout: number = WORKER_TIMEOUT,
timeout: number = DEFAULT_WORKER_TIMEOUT,
): Stream<WorkerResult<Req>> {
if (!connecting && !this._connectedToWorker) {
throw new Error("WORKER_CLIENT: the client is not yet connected to the worker");
Expand All @@ -199,7 +199,7 @@ export class OpenChatAgentWorker extends EventTarget {
async sendRequest<Req extends WorkerRequest>(
req: Req,
connecting = false,
timeout: number = WORKER_TIMEOUT,
timeout: number = DEFAULT_WORKER_TIMEOUT,
): Promise<WorkerResult<Req>> {
if (!connecting && !this._connectedToWorker) {
throw new Error("WORKER_CLIENT: the client is not yet connected to the worker");
Expand Down
95 changes: 52 additions & 43 deletions frontend/openchat-client/src/openchat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ import {
import { LiveState } from "./liveState";
import { getTypingString, startTyping, stopTyping } from "./utils/chat";
import { indexIsInRanges } from "./utils/range";
import { OpenChatAgentWorker } from "./agentWorker";
import { DEFAULT_WORKER_TIMEOUT, OpenChatAgentWorker } from "./agentWorker";
import type {
CreatedUser,
IdentityState,
Expand Down Expand Up @@ -498,7 +498,11 @@ import { applyTranslationCorrection } from "./stores/i18n";
import { getUserCountryCode } from "./utils/location";
import { isBalanceGate } from "openchat-shared";
import { ECDSAKeyIdentity } from "@dfinity/identity";
import { capturePinNumberStore, pinNumberFailureStore, pinNumberRequiredStore } from "./stores/pinNumber";
import {
capturePinNumberStore,
pinNumberFailureStore,
pinNumberRequiredStore,
} from "./stores/pinNumber";
import { captureRulesAcceptanceStore } from "./stores/rules";
import type { SetPinNumberResponse } from "openchat-shared";
import type { PinNumberFailures, MessageFormatter } from "openchat-shared";
Expand Down Expand Up @@ -1254,7 +1258,9 @@ export class OpenChat extends OpenChatAgentWorker {
);
}

async approveAccessGatePayment(group: MultiUserChat | CommunitySummary): Promise<ApproveAccessGatePaymentResponse> {
async approveAccessGatePayment(
group: MultiUserChat | CommunitySummary,
): Promise<ApproveAccessGatePaymentResponse> {
// If there is no payment gate then do nothing
if (!isPaymentGate(group.gate)) {
// If this is a channel there might still be a payment gate on the community
Expand Down Expand Up @@ -1305,13 +1311,16 @@ export class OpenChat extends OpenChatAgentWorker {
) {
pinNumberFailureStore.set(response as PinNumberFailures);
}

return response;
})
.catch(() => CommonResponses.failure());
}

async joinGroup(chat: MultiUserChat, credential: string | undefined): Promise<ClientJoinGroupResponse> {
async joinGroup(
chat: MultiUserChat,
credential: string | undefined,
): Promise<ClientJoinGroupResponse> {
const approveResponse = await this.approveAccessGatePayment(chat);
if (approveResponse.kind !== "success") {
return approveResponse;
Expand Down Expand Up @@ -3287,21 +3296,15 @@ export class OpenChat extends OpenChatAgentWorker {
unconfirmed.add(messageContext, retryEvent);

// TODO - what about mentions?
this.sendMessageCommon(
chat,
messageContext,
retryEvent,
[],
true,
);
this.sendMessageCommon(chat, messageContext, retryEvent, [], true);
}

private async sendMessageCommon(
chat: ChatSummary,
messageContext: MessageContext,
eventWrapper: EventWrapper<Message>,
mentioned: User[] = [],
retrying: boolean
retrying: boolean,
): Promise<SendMessageResponse> {
const { chatId, threadRootMessageIndex } = messageContext;

Expand Down Expand Up @@ -3386,8 +3389,8 @@ export class OpenChat extends OpenChatAgentWorker {
resp.kind === "too_main_failed_pin_attempts"
) {
pinNumberFailureStore.set(resp as PinNumberFailures);
}
}

this.onSendMessageFailure(
chatId,
msg.messageId,
Expand Down Expand Up @@ -3516,13 +3519,7 @@ export class OpenChat extends OpenChatAgentWorker {
expiresAt: threadRootMessageIndex ? undefined : this.eventExpiry(chat, timestamp),
};

return this.sendMessageCommon(
chat,
messageContext,
event,
mentioned,
false,
);
return this.sendMessageCommon(chat, messageContext, event, mentioned, false);
}

private throttleSendMessage(): boolean {
Expand Down Expand Up @@ -4685,7 +4682,9 @@ export class OpenChat extends OpenChatAgentWorker {
return this.sendRequest({ kind: "getBio", userId });
}

async withdrawCryptocurrency(domain: PendingCryptocurrencyWithdrawal): Promise<WithdrawCryptocurrencyResponse> {
async withdrawCryptocurrency(
domain: PendingCryptocurrencyWithdrawal,
): Promise<WithdrawCryptocurrencyResponse> {
let pin: string | undefined = undefined;

if (this._liveState.pinNumberRequired) {
Expand All @@ -4700,7 +4699,7 @@ export class OpenChat extends OpenChatAgentWorker {
) {
pinNumberFailureStore.set(resp as PinNumberFailures);
}

return resp;
});
}
Expand Down Expand Up @@ -5292,7 +5291,7 @@ export class OpenChat extends OpenChatAgentWorker {
messageId,
pin,
})
.then((resp) => {
.then((resp) => {
localMessageUpdates.setP2PSwapStatus(
messageId,
mapAcceptP2PSwapResponseToStatus(resp, this._liveState.user.userId),
Expand All @@ -5305,7 +5304,7 @@ export class OpenChat extends OpenChatAgentWorker {
) {
pinNumberFailureStore.set(resp as PinNumberFailures);
}

return resp;
})
.catch((err) => {
Expand Down Expand Up @@ -5650,7 +5649,7 @@ export class OpenChat extends OpenChatAgentWorker {
) {
pinNumberFailureStore.set(resp as PinNumberFailures);
}

return resp;
})
.catch((_) => {
Expand Down Expand Up @@ -5831,15 +5830,19 @@ export class OpenChat extends OpenChatAgentWorker {
return Promise.resolve(false);
}

return this.sendRequest({
kind: "submitProposal",
governanceCanisterId,
proposal,
ledger: nervousSystem.token.ledger,
token: nervousSystem.token.symbol,
proposalRejectionFee: nervousSystem.proposalRejectionFee,
transactionFee: nervousSystem.token.transferFee,
})
return this.sendRequest(
{
kind: "submitProposal",
governanceCanisterId,
proposal,
ledger: nervousSystem.token.ledger,
token: nervousSystem.token.symbol,
proposalRejectionFee: nervousSystem.proposalRejectionFee,
transactionFee: nervousSystem.token.transferFee,
},
false,
2 * DEFAULT_WORKER_TIMEOUT,
)
.then((resp) => {
if (resp.kind === "success" || resp.kind === "retrying") {
return true;
Expand Down Expand Up @@ -5920,7 +5923,7 @@ export class OpenChat extends OpenChatAgentWorker {
) {
pinNumberFailureStore.set(resp as PinNumberFailures);
}

return resp;
});
}
Expand Down Expand Up @@ -6377,7 +6380,10 @@ export class OpenChat extends OpenChatAgentWorker {
.catch(() => undefined);
}

async joinCommunity(community: CommunitySummary, credential: string | undefined): Promise<ClientJoinCommunityResponse> {
async joinCommunity(
community: CommunitySummary,
credential: string | undefined,
): Promise<ClientJoinCommunityResponse> {
const approveResponse = await this.approveAccessGatePayment(community);
if (approveResponse.kind !== "success") {
return approveResponse;
Expand Down Expand Up @@ -6709,7 +6715,10 @@ export class OpenChat extends OpenChatAgentWorker {
return this.getUserLocation().then((location) => featureRestricted(location, "swap"));
}

setPinNumber(currentPin: string | undefined, newPin: string | undefined): Promise<SetPinNumberResponse> {
setPinNumber(
currentPin: string | undefined,
newPin: string | undefined,
): Promise<SetPinNumberResponse> {
pinNumberFailureStore.set(undefined);

return this.sendRequest({ kind: "setPinNumber", currentPin, newPin }).then((resp) => {
Expand Down Expand Up @@ -6742,7 +6751,7 @@ export class OpenChat extends OpenChatAgentWorker {
message,
});
});
}
}

private promptForRuleAcceptance(): Promise<AcceptedRules | undefined> {
return new Promise((resolve, _) => {
Expand All @@ -6762,15 +6771,15 @@ export class OpenChat extends OpenChatAgentWorker {

if (this._liveState.currentCommunityRules?.enabled ?? false) {
acceptedRules.community = this._liveState.currentChatRules?.version;
}
}
}

captureRulesAcceptanceStore.set(undefined);
resolve(acceptedRules);
},
});
});
}
}

/**
* Reactive state provided in the form of svelte stores
Expand Down
Loading