Skip to content

Commit bd7727a

Browse files
committed
build out demo
1 parent c5577fd commit bd7727a

File tree

20 files changed

+1865
-223
lines changed

20 files changed

+1865
-223
lines changed
+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
MUX_WEBHOOK_SECRET=
2-
KNOCK_API_KEY="sk_12345"
3-
KNOCK_SIGNING_KEY="S25vY2sga25vY2sh..."
2+
KNOCK_SECRET_KEY=
3+
NEXT_PUBLIC_KNOCK_API_KEY="sk_12345"
4+
NEXT_PUBLIC_KNOCK_USER_ID="adfasf"
5+
NEXT_PUBLIC_KNOCK_FEED_CHANNEL_ID="asdfsdfa"

webhook-notifications-knock/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ yarn-error.log*
2727

2828
# local env files
2929
.env*.local
30+
.env.local
3031

3132
# vercel
3233
.vercel
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { revalidatePath } from 'next/cache';
2+
import { headers } from 'next/headers';
3+
4+
import { Knock } from '@knocklabs/node';
5+
const knockClient = new Knock(process.env.KNOCK_SECRET_KEY);
6+
7+
const channels = [
8+
{
9+
id: 'knock',
10+
properties: {
11+
name: 'Knock',
12+
icon: '🔔',
13+
YouTube: 'https://www.youtube.com/channel/UCDScOjujAI3-_9iJ0Me0Aug',
14+
Twitter: 'https://twitter.com/knocklabs',
15+
},
16+
},
17+
{
18+
id: 'mux',
19+
properties: {
20+
name: 'Mux',
21+
icon: '📹',
22+
YouTube: 'https://www.youtube.com/@MuxHQ',
23+
Twitter: 'https://twitter.com/MuxHQ',
24+
},
25+
},
26+
];
27+
28+
export async function POST(request: Request) {
29+
const body = await request.text();
30+
const payload = JSON.parse(body);
31+
if (payload.action === 'create') {
32+
channels.forEach(async (channel) => {
33+
await knockClient.objects.set('channels', channel.id, channel.properties);
34+
});
35+
}
36+
37+
if (payload.action === 'delete') {
38+
channels.forEach(async (channel) => {
39+
await knockClient.objects.delete('channels', channel.id);
40+
});
41+
}
42+
return Response.json({ message: 'ok' });
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { revalidatePath } from 'next/cache';
2+
import { headers } from 'next/headers';
3+
4+
import { Knock } from '@knocklabs/node';
5+
const knockClient = new Knock(process.env.KNOCK_SECRET_KEY);
6+
7+
export async function POST(request: Request) {
8+
const body = await request.text();
9+
const inlineUser = {
10+
id: 'fde69556-a3a5-4812-a3e9-c9ded0cbc950',
11+
name: 'John Hammond',
12+
13+
};
14+
knockClient.workflows.trigger('new-view', {
15+
recipients: [process.env.NEXT_PUBLIC_KNOCK_USER_ID as string],
16+
data: JSON.parse(body),
17+
});
18+
19+
return Response.json({ message: 'ok' });
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { revalidatePath } from 'next/cache';
2+
import { headers } from 'next/headers';
3+
4+
import { Knock } from '@knocklabs/node';
5+
const knockClient = new Knock(process.env.KNOCK_SECRET_KEY);
6+
7+
const subscriptions = [
8+
{
9+
id: '9d17eb2d-1980-4614-bcfe-5696ca9be631',
10+
properties: {
11+
role: 'regular',
12+
},
13+
},
14+
{
15+
id: 'a12896b9-658b-43ff-8d5b-79c34215778a',
16+
properties: {
17+
role: 'allAccess',
18+
},
19+
},
20+
];
21+
22+
export async function POST(request: Request) {
23+
const body = await request.text();
24+
const payload = JSON.parse(body);
25+
if (payload.action === 'create') {
26+
channels.forEach(async (channel) => {
27+
subscriptions.forEach(async (subscription) => {
28+
await knockClient.objects.addSubscriptions('channels', channel.id, {
29+
recipients: [subscription.id],
30+
properties: subscription.properties,
31+
});
32+
});
33+
});
34+
}
35+
36+
if (payload.action === 'delete') {
37+
channels.forEach(async (channel) => {
38+
subscriptions.forEach(async (subscription) => {
39+
await knockClient.objects.deleteSubscriptions('channels', channel.id, {
40+
recipients: [subscription.id],
41+
});
42+
});
43+
});
44+
}
45+
return Response.json({ message: 'ok' });
46+
}
47+
const channels = [
48+
{
49+
id: 'knock',
50+
properties: {
51+
name: 'Knock',
52+
icon: '🔔',
53+
YouTube: 'https://www.youtube.com/channel/UCDScOjujAI3-_9iJ0Me0Aug',
54+
Twitter: 'https://twitter.com/knocklabs',
55+
},
56+
},
57+
{
58+
id: 'mux',
59+
properties: {
60+
name: 'Mux',
61+
icon: '📹',
62+
YouTube: 'https://www.youtube.com/@MuxHQ',
63+
Twitter: 'https://twitter.com/MuxHQ',
64+
},
65+
},
66+
];

webhook-notifications-knock/app/api/webhooks/mux/route.ts

+33-27
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,42 @@ import { headers } from 'next/headers';
33

44
import Mux from '@mux/mux-node';
55

6-
const { Knock } = require("@knocklabs/node");
7-
const knockClient = new Knock(process.env.KNOCK_API_KEY);
6+
import { Knock } from '@knocklabs/node';
7+
const knockClient = new Knock(process.env.KNOCK_SECRET_KEY);
88

9-
const mux = new Mux({
10-
webhookSecret: process.env.MUX_WEBHOOK_SECRET,
11-
});
9+
// const mux = new Mux({
10+
// webhookSecret: process.env.MUX_WEBHOOK_SECRET,
11+
// });
1212

1313
export async function POST(request: Request) {
14-
const headersList = headers();
15-
const body = await request.text();
16-
const event = mux.webhooks.unwrap(body, headersList);
14+
const headersList = headers();
15+
const body = await request.text();
16+
const event = JSON.parse(body);
17+
console.log(event);
18+
// const event = mux.webhooks.unwrap(body, headersList);
1719

18-
switch (event.type) {
19-
case 'video.live_stream.active':
20-
case 'video.asset.ready':
21-
// The key of the workflow (from Knock dashboard)
22-
await knockClient.notify("dinosaurs-loose", {
23-
// user id of who performed the action
24-
actor: "dnedry",
25-
// list of user ids for who should receive the notification
26-
recipients: ["jhammond", "agrant", "imalcolm", "esattler"],
27-
// data payload to send through
28-
data: event.data,
29-
// an optional identifier for the tenant that the notifications belong to
30-
tenant: "jurassic-park",
31-
});
32-
break;
33-
default:
34-
break;
35-
}
20+
switch (event.type) {
21+
case 'video.live_stream.active':
22+
console.log('📺 live stream started.....');
23+
const channel = await knockClient.objects.get('channels', 'knock');
24+
const { workflow_run_id } = await knockClient.workflows.trigger(
25+
'new-stream',
26+
{
27+
recipients: [{ collection: 'channels', id: 'knock' }],
28+
data: {
29+
channel,
30+
},
31+
}
32+
);
33+
break;
34+
case 'video.asset.ready':
35+
console.log('🎞️ new asset ready.....');
3636

37-
return Response.json({ message: 'ok' });
37+
console.log(`📨 new workflow run started: ${workflow_run_id}`);
38+
break;
39+
default:
40+
break;
41+
}
42+
43+
return Response.json({ message: 'ok' });
3844
}

0 commit comments

Comments
 (0)