Skip to content

Commit

Permalink
Merge branch 'af/listen-for-intent-setup'
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewFerr committed Sep 27, 2023
2 parents e57073c + a5dcf62 commit 0c074f2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/appservice/Appservice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,8 @@ export class Appservice extends EventEmitter {
let intent: Intent = this.intentsCache.get(userId);
if (!intent) {
intent = new Intent(this.options, userId, this);
this.emit("intent.new", intent);
this.intentsCache.set(userId, intent);
this.emit("intent.new", intent);
if (this.options.intentOptions.encryption) {
intent.enableEncryption().catch(e => {
LogService.error("Appservice", `Failed to set up crypto on intent ${userId}`, e);
Expand Down
41 changes: 41 additions & 0 deletions test/appservice/AppserviceTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,47 @@ describe('Appservice', () => {
expect(intent.userId).toEqual(userId);
});

it('should emit an event for a created intent', async () => {
const appservice = new Appservice({
port: 0,
bindAddress: '',
homeserverName: 'example.org',
homeserverUrl: 'https://localhost',
registration: {
as_token: "",
hs_token: "",
sender_localpart: "_bot_",
namespaces: {
users: [{ exclusive: true, regex: "@_prefix_.*:.+" }],
rooms: [],
aliases: [],
},
},
});

let newIntent: Intent | undefined;
const intentSpy = simple.stub().callFn(intent => {
expect(intent).toBeInstanceOf(Intent);
newIntent = intent;
const sameIntent = appservice.getIntentForUserId(newIntent.userId);
expect(newIntent).toBe(sameIntent);
});
appservice.on("intent.new", intentSpy);

[
"@alice:example.org",
"@_prefix_testing:example.org",
"@_bot_:example.org",
"@test_prefix_:example.org",
].forEach((userId, index) => {
const intent = appservice.getIntentForUserId(userId);
expect(intentSpy.callCount).toBe(index+1);
expect(intent).toBeDefined();
expect(intent.userId).toEqual(userId);
expect(intent).toBe(newIntent);
});
});

it('should return a user ID for any namespaced localpart', async () => {
const appservice = new Appservice({
port: 0,
Expand Down

0 comments on commit 0c074f2

Please sign in to comment.