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,4 +1,15 @@
import _defineProperty from "@babel/runtime/helpers/defineProperty";
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isPollEvent = exports.PollEvent = exports.Poll = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _matrixEventsSdk = require("matrix-events-sdk");
var _polls = require("../@types/polls");
var _relations = require("./relations");
var _typedEventEmitter = require("./typed-event-emitter");
/*
Copyright 2023 The Matrix.org Foundation C.I.C.
@@ -14,12 +25,7 @@ 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 { M_POLL_START } from "matrix-events-sdk";
import { M_POLL_END, M_POLL_RESPONSE } from "../@types/polls.js";
import { Relations } from "./relations.js";
import { TypedEventEmitter } from "./typed-event-emitter.js";
export let PollEvent = /*#__PURE__*/function (PollEvent) {
let PollEvent = /*#__PURE__*/function (PollEvent) {
PollEvent["New"] = "Poll.new";
PollEvent["End"] = "Poll.end";
PollEvent["Update"] = "Poll.update";
@@ -28,12 +34,13 @@ export let PollEvent = /*#__PURE__*/function (PollEvent) {
PollEvent["UndecryptableRelations"] = "Poll.UndecryptableRelations";
return PollEvent;
}({});
exports.PollEvent = PollEvent;
const filterResponseRelations = (relationEvents, pollEndTimestamp) => {
const responseEvents = relationEvents.filter(event => {
if (event.isDecryptionFailure()) {
return;
}
return M_POLL_RESPONSE.matches(event.getType()) &&
return _polls.M_POLL_RESPONSE.matches(event.getType()) &&
// From MSC3381:
// "Votes sent on or before the end event's timestamp are valid votes"
event.getTs() <= pollEndTimestamp;
@@ -42,21 +49,24 @@ const filterResponseRelations = (relationEvents, pollEndTimestamp) => {
responseEvents
};
};
export class Poll extends TypedEventEmitter {
class Poll extends _typedEventEmitter.TypedEventEmitter {
constructor(rootEvent, matrixClient, room) {
super();
_defineProperty(this, "roomId", void 0);
_defineProperty(this, "pollEvent", void 0);
_defineProperty(this, "_isFetchingResponses", false);
_defineProperty(this, "relationsNextBatch", void 0);
_defineProperty(this, "responses", null);
_defineProperty(this, "endEvent", void 0);
this.rootEvent = rootEvent;
this.matrixClient = matrixClient;
this.room = room;
(0, _defineProperty2.default)(this, "roomId", void 0);
(0, _defineProperty2.default)(this, "pollEvent", void 0);
(0, _defineProperty2.default)(this, "_isFetchingResponses", false);
(0, _defineProperty2.default)(this, "relationsNextBatch", void 0);
(0, _defineProperty2.default)(this, "responses", null);
(0, _defineProperty2.default)(this, "endEvent", void 0);
/**
* Keep track of undecryptable relations
* As incomplete result sets affect poll results
*/
_defineProperty(this, "undecryptableRelationEventIds", new Set());
_defineProperty(this, "countUndecryptableEvents", events => {
(0, _defineProperty2.default)(this, "undecryptableRelationEventIds", new Set());
(0, _defineProperty2.default)(this, "countUndecryptableEvents", events => {
const undecryptableEventIds = events.filter(event => event.isDecryptionFailure()).map(event => event.getId());
const previousCount = this.undecryptableRelationsCount;
this.undecryptableRelationEventIds = new Set([...this.undecryptableRelationEventIds, ...undecryptableEventIds]);
@@ -64,9 +74,6 @@ export class Poll extends TypedEventEmitter {
this.emit(PollEvent.UndecryptableRelations, this.undecryptableRelationsCount);
}
});
this.rootEvent = rootEvent;
this.matrixClient = matrixClient;
this.room = room;
if (!this.rootEvent.getRoomId() || !this.rootEvent.getId()) {
throw new Error("Invalid poll start event.");
}
@@ -77,7 +84,8 @@ export class Poll extends TypedEventEmitter {
return this.rootEvent.getId();
}
get endEventId() {
return this.endEvent?.getId();
var _this$endEvent;
return (_this$endEvent = this.endEvent) === null || _this$endEvent === void 0 ? void 0 : _this$endEvent.getId();
}
get isEnded() {
return !!this.endEvent;
@@ -110,7 +118,8 @@ export class Poll extends TypedEventEmitter {
* @returns void
*/
onNewRelation(event) {
if (M_POLL_END.matches(event.getType()) && this.validateEndEvent(event)) {
var _this$endEvent2;
if (_polls.M_POLL_END.matches(event.getType()) && this.validateEndEvent(event)) {
this.endEvent = event;
this.refilterResponsesOnEnd();
this.emit(PollEvent.End);
@@ -120,7 +129,7 @@ export class Poll extends TypedEventEmitter {
if (!this.responses) {
return;
}
const pollEndTimestamp = this.endEvent?.getTs() || Number.MAX_SAFE_INTEGER;
const pollEndTimestamp = ((_this$endEvent2 = this.endEvent) === null || _this$endEvent2 === void 0 ? void 0 : _this$endEvent2.getTs()) || Number.MAX_SAFE_INTEGER;
const {
responseEvents
} = filterResponseRelations([event], pollEndTimestamp);
@@ -133,6 +142,7 @@ export class Poll extends TypedEventEmitter {
}
}
async fetchResponses() {
var _this$endEvent3, _allRelations$nextBat;
this._isFetchingResponses = true;
// we want:
@@ -143,21 +153,21 @@ export class Poll extends TypedEventEmitter {
from: this.relationsNextBatch || undefined
});
await Promise.all(allRelations.events.map(event => this.matrixClient.decryptEventIfNeeded(event)));
const responses = this.responses || new Relations("m.reference", M_POLL_RESPONSE.name, this.matrixClient, [M_POLL_RESPONSE.altName]);
const pollEndEvent = allRelations.events.find(event => M_POLL_END.matches(event.getType()));
const responses = this.responses || new _relations.Relations("m.reference", _polls.M_POLL_RESPONSE.name, this.matrixClient, [_polls.M_POLL_RESPONSE.altName]);
const pollEndEvent = allRelations.events.find(event => _polls.M_POLL_END.matches(event.getType()));
if (this.validateEndEvent(pollEndEvent)) {
this.endEvent = pollEndEvent;
this.refilterResponsesOnEnd();
this.emit(PollEvent.End);
}
const pollCloseTimestamp = this.endEvent?.getTs() || Number.MAX_SAFE_INTEGER;
const pollCloseTimestamp = ((_this$endEvent3 = this.endEvent) === null || _this$endEvent3 === void 0 ? void 0 : _this$endEvent3.getTs()) || Number.MAX_SAFE_INTEGER;
const {
responseEvents
} = filterResponseRelations(allRelations.events, pollCloseTimestamp);
responseEvents.forEach(event => {
responses.addEvent(event);
});
this.relationsNextBatch = allRelations.nextBatch ?? undefined;
this.relationsNextBatch = (_allRelations$nextBat = allRelations.nextBatch) !== null && _allRelations$nextBat !== void 0 ? _allRelations$nextBat : undefined;
this.responses = responses;
this.countUndecryptableEvents(allRelations.events);
@@ -182,13 +192,15 @@ export class Poll extends TypedEventEmitter {
* To ensure responses are valid
*/
refilterResponsesOnEnd() {
var _this$endEvent4;
if (!this.responses) {
return;
}
const pollEndTimestamp = this.endEvent?.getTs() || Number.MAX_SAFE_INTEGER;
const pollEndTimestamp = ((_this$endEvent4 = this.endEvent) === null || _this$endEvent4 === void 0 ? void 0 : _this$endEvent4.getTs()) || Number.MAX_SAFE_INTEGER;
this.responses.getRelations().forEach(event => {
if (event.getTs() > pollEndTimestamp) {
this.responses?.removeEvent(event);
var _this$responses;
(_this$responses = this.responses) === null || _this$responses === void 0 ? void 0 : _this$responses.removeEvent(event);
}
});
this.emit(PollEvent.Responses, this.responses);
@@ -222,8 +234,10 @@ export class Poll extends TypedEventEmitter {
* @param event - Event to test
* @returns true if the event is a poll event, else false
*/
export const isPollEvent = event => {
exports.Poll = Poll;
const isPollEvent = event => {
const eventType = event.getType();
return M_POLL_START.matches(eventType) || M_POLL_RESPONSE.matches(eventType) || M_POLL_END.matches(eventType);
return _matrixEventsSdk.M_POLL_START.matches(eventType) || _polls.M_POLL_RESPONSE.matches(eventType) || _polls.M_POLL_END.matches(eventType);
};
exports.isPollEvent = isPollEvent;
//# sourceMappingURL=poll.js.map