feat: complete Ancestor quote bot with production-ready Docker support

This commit is contained in:
unfunny
2026-09-13 14:14:38 -04:00
parent f2016da05a
commit fe7351a7dc
1145 changed files with 30890 additions and 97684 deletions

View File

@@ -1,25 +1,35 @@
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
/*
Copyright 2023 The Matrix.org Foundation C.I.C.
"use strict";
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { KeysBackupRequest, KeysClaimRequest, KeysQueryRequest, KeysUploadRequest, PutDehydratedDeviceRequest, RoomMessageRequest, SignatureUploadRequest, ToDeviceRequest, UploadSigningKeysRequest } from "@matrix-org/matrix-sdk-crypto-wasm";
import { calculateRetryBackoff, Method } from "../http-api/index.js";
import { logDuration, sleep } from "../utils.js";
import { ToDeviceMessageId } from "../@types/event.js";
import { UnstablePrefix as DehydrationUnstablePrefix } from "./DehydratedDeviceManager.js";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.OutgoingRequestProcessor = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _matrixSdkCryptoWasm = require("@matrix-org/matrix-sdk-crypto-wasm");
var _logger = require("../logger");
var _httpApi = require("../http-api");
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } /*
Copyright 2023 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/**
* Common interface for all the request types returned by `OlmMachine.outgoingRequests`.
*
* @internal
*/
/**
* OutgoingRequestManager: turns `OutgoingRequest`s from the rust sdk into HTTP requests
@@ -33,9 +43,8 @@ import { UnstablePrefix as DehydrationUnstablePrefix } from "./DehydratedDeviceM
*
* @internal
*/
export class OutgoingRequestProcessor {
constructor(logger, olmMachine, http) {
this.logger = logger;
class OutgoingRequestProcessor {
constructor(olmMachine, http) {
this.olmMachine = olmMachine;
this.http = http;
}
@@ -45,77 +54,35 @@ export class OutgoingRequestProcessor {
/* refer https://docs.rs/matrix-sdk-crypto/0.6.0/matrix_sdk_crypto/requests/enum.OutgoingRequests.html
* for the complete list of request types
*/
if (msg instanceof KeysUploadRequest) {
resp = await this.requestWithRetry(Method.Post, "/_matrix/client/v3/keys/upload", {}, msg.body);
} else if (msg instanceof KeysQueryRequest) {
resp = await this.requestWithRetry(Method.Post, "/_matrix/client/v3/keys/query", {}, msg.body);
} else if (msg instanceof KeysClaimRequest) {
resp = await this.requestWithRetry(Method.Post, "/_matrix/client/v3/keys/claim", {}, msg.body);
} else if (msg instanceof SignatureUploadRequest) {
resp = await this.requestWithRetry(Method.Post, "/_matrix/client/v3/keys/signatures/upload", {}, msg.body);
} else if (msg instanceof KeysBackupRequest) {
resp = await this.requestWithRetry(Method.Put, "/_matrix/client/v3/room_keys/keys", {
version: msg.version
}, msg.body);
} else if (msg instanceof ToDeviceRequest) {
resp = await this.sendToDeviceRequest(msg);
} else if (msg instanceof RoomMessageRequest) {
const path = `/_matrix/client/v3/rooms/${encodeURIComponent(msg.room_id)}/send/` + `${encodeURIComponent(msg.event_type)}/${encodeURIComponent(msg.txn_id)}`;
resp = await this.requestWithRetry(Method.Put, path, {}, msg.body);
} else if (msg instanceof UploadSigningKeysRequest) {
await this.makeRequestWithUIA(Method.Post, "/_matrix/client/v3/keys/device_signing/upload", {}, msg.body, uiaCallback);
// SigningKeysUploadRequest does not implement OutgoingRequest and does not need to be marked as sent.
return;
} else if (msg instanceof PutDehydratedDeviceRequest) {
const path = DehydrationUnstablePrefix + "/dehydrated_device";
await this.rawJsonRequest(Method.Put, path, {}, msg.body);
// PutDehydratedDeviceRequest does not implement OutgoingRequest and does not need to be marked as sent.
return;
if (msg instanceof _matrixSdkCryptoWasm.KeysUploadRequest) {
resp = await this.rawJsonRequest(_httpApi.Method.Post, "/_matrix/client/v3/keys/upload", {}, msg.body);
} else if (msg instanceof _matrixSdkCryptoWasm.KeysQueryRequest) {
resp = await this.rawJsonRequest(_httpApi.Method.Post, "/_matrix/client/v3/keys/query", {}, msg.body);
} else if (msg instanceof _matrixSdkCryptoWasm.KeysClaimRequest) {
resp = await this.rawJsonRequest(_httpApi.Method.Post, "/_matrix/client/v3/keys/claim", {}, msg.body);
} else if (msg instanceof _matrixSdkCryptoWasm.SignatureUploadRequest) {
resp = await this.rawJsonRequest(_httpApi.Method.Post, "/_matrix/client/v3/keys/signatures/upload", {}, msg.body);
} else if (msg instanceof _matrixSdkCryptoWasm.KeysBackupRequest) {
resp = await this.rawJsonRequest(_httpApi.Method.Put, "/_matrix/client/v3/room_keys/keys", {}, msg.body);
} else if (msg instanceof _matrixSdkCryptoWasm.ToDeviceRequest) {
const path = `/_matrix/client/v3/sendToDevice/${encodeURIComponent(msg.event_type)}/` + encodeURIComponent(msg.txn_id);
resp = await this.rawJsonRequest(_httpApi.Method.Put, path, {}, msg.body);
} else if (msg instanceof _matrixSdkCryptoWasm.RoomMessageRequest) {
const path = `/_matrix/client/v3/room/${encodeURIComponent(msg.room_id)}/send/` + `${encodeURIComponent(msg.event_type)}/${encodeURIComponent(msg.txn_id)}`;
resp = await this.rawJsonRequest(_httpApi.Method.Put, path, {}, msg.body);
} else if (msg instanceof _matrixSdkCryptoWasm.SigningKeysUploadRequest) {
resp = await this.makeRequestWithUIA(_httpApi.Method.Post, "/_matrix/client/v3/keys/device_signing/upload", {}, msg.body, uiaCallback);
} else {
this.logger.warn("Unsupported outgoing message", Object.getPrototypeOf(msg));
_logger.logger.warn("Unsupported outgoing message", Object.getPrototypeOf(msg));
resp = "";
}
if (msg.id) {
try {
await logDuration(this.logger, `Mark Request as sent ${msg.type}`, async () => {
await this.olmMachine.markRequestAsSent(msg.id, msg.type, resp);
});
} catch (e) {
// Ignore errors which are caused by the olmMachine having been freed. The exact error message depends
// on whether we are using a release or develop build of rust-sdk-crypto-wasm.
if (e instanceof Error && (e.message === "Attempt to use a moved value" || e.message === "null pointer passed to rust")) {
this.logger.debug(`Ignoring error '${e.message}': client is likely shutting down`);
} else {
throw e;
}
}
} else {
this.logger.trace(`Outgoing request type:${msg.type} does not have an ID`);
await this.olmMachine.markRequestAsSent(msg.id, msg.type, resp);
}
}
/**
* Send the HTTP request for a `ToDeviceRequest`
*
* @param request - request to send
* @returns JSON-serialized body of the response, if successful
*/
async sendToDeviceRequest(request) {
// a bit of extra logging, to help trace to-device messages through the system
const parsedBody = JSON.parse(request.body);
const messageList = [];
for (const [userId, perUserMessages] of Object.entries(parsedBody.messages)) {
for (const [deviceId, message] of Object.entries(perUserMessages)) {
messageList.push(`${userId}/${deviceId} (msgid ${message[ToDeviceMessageId]})`);
}
}
this.logger.info(`Sending batch of to-device messages. type=${request.event_type} txnid=${request.txn_id}`, messageList);
const path = `/_matrix/client/v3/sendToDevice/${encodeURIComponent(request.event_type)}/` + encodeURIComponent(request.txn_id);
return await this.requestWithRetry(Method.Put, path, {}, request.body);
}
async makeRequestWithUIA(method, path, queryParams, body, uiaCallback) {
if (!uiaCallback) {
return await this.requestWithRetry(method, path, queryParams, body);
return await this.rawJsonRequest(method, path, queryParams, body);
}
const parsedBody = JSON.parse(body);
const makeRequest = async auth => {
@@ -123,29 +90,12 @@ export class OutgoingRequestProcessor {
if (auth !== null) {
newBody.auth = auth;
}
const resp = await this.requestWithRetry(method, path, queryParams, JSON.stringify(newBody));
const resp = await this.rawJsonRequest(method, path, queryParams, JSON.stringify(newBody));
return JSON.parse(resp);
};
const resp = await uiaCallback(makeRequest);
return JSON.stringify(resp);
}
async requestWithRetry(method, path, queryParams, body) {
let currentRetryCount = 0;
while (true) {
try {
return await this.rawJsonRequest(method, path, queryParams, body);
} catch (e) {
currentRetryCount++;
const backoff = calculateRetryBackoff(e, currentRetryCount, true);
if (backoff < 0) {
// Max number of retries reached, or error is not retryable. rethrow the error
throw e;
}
// wait for the specified time and then retry the request
await sleep(backoff);
}
}
}
async rawJsonRequest(method, path, queryParams, body) {
const opts = {
// inhibit the JSON stringification and parsing within HttpApi.
@@ -156,14 +106,17 @@ export class OutgoingRequestProcessor {
"Accept": "application/json"
},
// we use the full prefix
prefix: "",
// We set a timeout of 60 seconds to guard against requests getting stuck forever and wedging the
// request loop (cf https://github.com/element-hq/element-web/issues/29534).
//
// (XXX: should we do this in the whole of the js-sdk?)
localTimeoutMs: 60000
prefix: ""
};
return await this.http.authedRequest(method, path, queryParams, body, opts);
try {
const response = await this.http.authedRequest(method, path, queryParams, body, opts);
_logger.logger.info(`rust-crypto: successfully made HTTP request: ${method} ${path}`);
return response;
} catch (e) {
_logger.logger.warn(`rust-crypto: error making HTTP request: ${method} ${path}: ${e}`);
throw e;
}
}
}
exports.OutgoingRequestProcessor = OutgoingRequestProcessor;
//# sourceMappingURL=OutgoingRequestProcessor.js.map