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,3 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.PolicyScope = exports.POLICIES_ACCOUNT_EVENT_TYPE = exports.IgnoredInvites = exports.IGNORE_INVITES_ACCOUNT_EVENT_KEY = void 0;
var _matrixEventsSdk = require("matrix-events-sdk");
var _eventTimeline = require("./event-timeline");
var _partials = require("../@types/partials");
var _utils = require("../utils");
/*
Copyright 2022 The Matrix.org Foundation C.I.C.
@@ -14,18 +24,32 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import { EventTimeline } from "./event-timeline.js";
import { Preset } from "../@types/partials.js";
import { globToRegexp } from "../utils.js";
import { EventType } from "../@types/event.js";
import { IGNORE_INVITES_ACCOUNT_EVENT_KEY, POLICIES_ACCOUNT_EVENT_TYPE, PolicyRecommendation, PolicyScope } from "./invites-ignorer-types.js";
export { IGNORE_INVITES_ACCOUNT_EVENT_KEY, POLICIES_ACCOUNT_EVENT_TYPE, PolicyRecommendation, PolicyScope };
const scopeToEventTypeMap = {
[PolicyScope.User]: EventType.PolicyRuleUser,
[PolicyScope.Room]: EventType.PolicyRuleRoom,
[PolicyScope.Server]: EventType.PolicyRuleServer
};
/// The event type storing the user's individual policies.
///
/// Exported for testing purposes.
const POLICIES_ACCOUNT_EVENT_TYPE = new _matrixEventsSdk.UnstableValue("m.policies", "org.matrix.msc3847.policies");
/// The key within the user's individual policies storing the user's ignored invites.
///
/// Exported for testing purposes.
exports.POLICIES_ACCOUNT_EVENT_TYPE = POLICIES_ACCOUNT_EVENT_TYPE;
const IGNORE_INVITES_ACCOUNT_EVENT_KEY = new _matrixEventsSdk.UnstableValue("m.ignore.invites", "org.matrix.msc3847.ignore.invites");
/// The types of recommendations understood.
exports.IGNORE_INVITES_ACCOUNT_EVENT_KEY = IGNORE_INVITES_ACCOUNT_EVENT_KEY;
var PolicyRecommendation = /*#__PURE__*/function (PolicyRecommendation) {
PolicyRecommendation["Ban"] = "m.ban";
return PolicyRecommendation;
}(PolicyRecommendation || {});
/**
* The various scopes for policies.
*/
let PolicyScope = /*#__PURE__*/function (PolicyScope) {
PolicyScope["User"] = "m.policy.user";
PolicyScope["Room"] = "m.policy.room";
PolicyScope["Server"] = "m.policy.server";
return PolicyScope;
}({});
/**
* A container for ignored invites.
*
@@ -36,7 +60,8 @@ const scopeToEventTypeMap = {
* applications turn out to require longer lists, we may need to rework
* our data structures.
*/
export class IgnoredInvites {
exports.PolicyScope = PolicyScope;
class IgnoredInvites {
constructor(client) {
this.client = client;
}
@@ -51,7 +76,7 @@ export class IgnoredInvites {
*/
async addRule(scope, entity, reason) {
const target = await this.getOrCreateTargetRoom();
const response = await this.client.sendStateEvent(target.roomId, scopeToEventTypeMap[scope], {
const response = await this.client.sendStateEvent(target.roomId, scope, {
entity,
reason,
recommendation: PolicyRecommendation.Ban
@@ -104,9 +129,8 @@ export class IgnoredInvites {
/**
* Find out whether an invite should be ignored.
*
* @param params
* @param params.sender - The user id for the user who issued the invite.
* @param params.roomId - The room to which the user is invited.
* @param sender - The user id for the user who issued the invite.
* @param roomId - The room to which the user is invited.
* @returns A rule matching the entity, if any was found, `null` otherwise.
*/
async getRuleForInvite({
@@ -127,7 +151,7 @@ export class IgnoredInvites {
const senderServer = sender.split(":")[1];
const roomServer = roomId.split(":")[1];
for (const room of policyRooms) {
const state = room.getUnfilteredTimelineSet().getLiveTimeline().getState(EventTimeline.FORWARDS);
const state = room.getUnfilteredTimelineSet().getLiveTimeline().getState(_eventTimeline.EventTimeline.FORWARDS);
for (const {
scope,
entities
@@ -141,22 +165,22 @@ export class IgnoredInvites {
scope: PolicyScope.Server,
entities: [senderServer, roomServer]
}]) {
const events = state.getStateEvents(scopeToEventTypeMap[scope]);
const events = state.getStateEvents(scope);
for (const event of events) {
const content = event.getContent();
if (content?.recommendation != PolicyRecommendation.Ban) {
if ((content === null || content === void 0 ? void 0 : content.recommendation) != PolicyRecommendation.Ban) {
// Ignoring invites only looks at `m.ban` recommendations.
continue;
}
const glob = content?.entity;
const glob = content === null || content === void 0 ? void 0 : content.entity;
if (!glob) {
// Invalid event.
continue;
}
let regexp;
try {
regexp = new RegExp(globToRegexp(glob));
} catch {
regexp = new RegExp((0, _utils.globToRegexp)(glob));
} catch (ex) {
// Assume invalid event.
continue;
}
@@ -169,6 +193,7 @@ export class IgnoredInvites {
}
}
}
return null;
}
@@ -206,7 +231,7 @@ export class IgnoredInvites {
// We need to create our own policy room for ignoring invites.
target = (await this.client.createRoom({
name: "Individual Policy Room",
preset: Preset.PrivateChat
preset: _partials.Preset.PrivateChat
})).room_id;
await this.withIgnoreInvitesPolicies(ignoreInvitesPolicies => {
ignoreInvitesPolicies.target = target;
@@ -299,10 +324,11 @@ export class IgnoredInvites {
getPoliciesAndIgnoreInvitesPolicies() {
let policies = {};
for (const key of [POLICIES_ACCOUNT_EVENT_TYPE.name, POLICIES_ACCOUNT_EVENT_TYPE.altName]) {
var _this$client$getAccou;
if (!key) {
continue;
}
const value = this.client.getAccountData(key)?.getContent();
const value = (_this$client$getAccou = this.client.getAccountData(key)) === null || _this$client$getAccou === void 0 ? void 0 : _this$client$getAccou.getContent();
if (value) {
policies = value;
break;
@@ -330,4 +356,5 @@ export class IgnoredInvites {
};
}
}
exports.IgnoredInvites = IgnoredInvites;
//# sourceMappingURL=invites-ignorer.js.map