Skip to content

Commit

Permalink
Merge branch '0.x' of github.com:soketi/soketi
Browse files Browse the repository at this point in the history
  • Loading branch information
rennokki committed May 18, 2022
2 parents ecd184f + 8e22033 commit e15fcb7
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/webhook-sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ export class WebhookSender {
return;
}

async.each(app.webhooks, (webhook: WebhookInterface, resolveWebhook) => {
async.each(app.webhooks, (webhook: WebhookInterface, resolveWebhook) => {
const originalEventsLength = payload.events.length;
let filteredPayloadEvents = payload.events;

payload.events = payload.events.filter(function (event) {
filteredPayloadEvents = filteredPayloadEvents.filter(event => {
if (!webhook.event_types.includes(event.name)) {
return false;
}
Expand All @@ -87,12 +88,14 @@ export class WebhookSender {
});

// If there's no webhooks to send after filtration, we should resolve early.
if (payload.events.length === 0) {
if (filteredPayloadEvents.length === 0) {
return resolveWebhook();
}

// If any events have been filtered out, regenerate the signature
let pusherSignature = (originalEventsLength !== payload.events.length) ? createWebhookHmac(JSON.stringify(payload), app.secret) : originalPusherSignature;
let pusherSignature = (originalEventsLength !== filteredPayloadEvents.length)
? createWebhookHmac(JSON.stringify(payload), app.secret)
: originalPusherSignature;

if (this.server.options.debug) {
Log.webhookSenderTitle('馃殌 Processing webhook from queue.');
Expand Down
111 changes: 111 additions & 0 deletions tests/webhooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,117 @@ describe('webhooks test', () => {
});
}, 60 * 1000);

Utils.shouldRun(Utils.appManagerIs('array') && Utils.adapterIs('local'))('webhooks from member_added and member_removed alongside filtering', done => {
let channelName = `presence-${Utils.randomChannelName()}`;

let webhooks = [
{
event_types: ['member_added'],
url: 'http://127.0.0.1:3001/webhook',
filter: {
channel_name_starts_with: channelName,
},
},
{
event_types: ['member_removed'],
url: 'http://127.0.0.1:3001/webhook',
filter: {
channel_name_starts_with: channelName,
},
},
];

let john = {
user_id: 1,
user_info: {
id: 1,
name: 'John',
},
};

let alice = {
user_id: 2,
user_info: {
id: 2,
name: 'Alice',
},
};

let johnClient;
let aliceClient;

Utils.newServer({
'appManager.array.apps.0.webhooks': webhooks,
'database.redis.keyPrefix': 'presence-webhooks',
}, (server: Server) => {
Utils.newWebhookServer((req, res) => {
let app = new App(server.options.appManager.array.apps[0], server);
let rightSignature = createWebhookHmac(JSON.stringify(req.body), app.secret);

expect(req.headers['x-pusher-key']).toBe('app-key');
expect(req.headers['x-pusher-signature']).toBe(rightSignature);
expect(req.body.time_ms).toBeDefined();
expect(req.body.events).toBeDefined();
expect(req.body.events.length).toBe(1);

const webhookEvent = req.body.events[0];

if (req.body.name === 'member_added') {
expect(webhookEvent.channel).toBe(channelName);
expect(webhookEvent.user_id).toBe(2);
expect([1, 2].includes(webhookEvent.user_id)).toBe(true);
}

res.json({ ok: true });

if (webhookEvent.name === 'member_removed') {
expect(webhookEvent.channel).toBe(channelName);
expect(webhookEvent.user_id).toBe(2);
johnClient.disconnect();
done();
}
}, (activeHttpServer) => {
johnClient = Utils.newClientForPresenceUser(john);

johnClient.connection.bind('connected', () => {
let johnChannel = johnClient.subscribe(channelName);

johnChannel.bind('pusher:subscription_succeeded', (data) => {
expect(data.count).toBe(1);
expect(data.me.id).toBe(1);
expect(data.members['1'].id).toBe(1);
expect(data.me.info.name).toBe('John');

aliceClient = Utils.newClientForPresenceUser(alice);

aliceClient.connection.bind('connected', () => {
let aliceChannel = aliceClient.subscribe(channelName);

aliceChannel.bind('pusher:subscription_succeeded', (data) => {
expect(data.count).toBe(2);
expect(data.me.id).toBe(2);
expect(data.members['1'].id).toBe(1);
expect(data.members['2'].id).toBe(2);
expect(data.me.info.name).toBe('Alice');
aliceClient.disconnect();
});
});
});

johnChannel.bind('pusher:member_added', data => {
expect(data.id).toBe(2);
expect(data.info.name).toBe('Alice');
});

johnChannel.bind('pusher:member_removed', data => {
expect(data.id).toBe(2);
expect(data.info.name).toBe('Alice');
});
});
});
});
}, 60 * 1000);

Utils.shouldRun(Utils.appManagerIs('array') && Utils.adapterIs('local'))('lambda webhooks', done => {
let webhooks = [{
event_types: ['client_event'],
Expand Down

0 comments on commit e15fcb7

Please sign in to comment.