Browse Source

feat(reactions-webhook) Added reactions backend call for webhook (#9534)

* Added backend call for reactions webhook

* Updated webhook url

* Fixed linting error

* Code review fixes

* Fixed linting errors
j8
robertpin 3 years ago
parent
commit
ea0d953d1c
No account linked to committer's email address

+ 77
- 0
react/features/reactions/functions.any.js View File

@@ -1,5 +1,11 @@
1 1
 // @flow
2 2
 
3
+import { getLocalParticipant } from '../base/participants';
4
+import { extractFqnFromPath } from '../dynamic-branding/functions';
5
+
6
+import { REACTIONS } from './constants';
7
+import logger from './logger';
8
+
3 9
 /**
4 10
  * Returns the queue of reactions.
5 11
  *
@@ -9,3 +15,74 @@
9 15
 export function getReactionsQueue(state: Object) {
10 16
     return state['features/reactions'].queue;
11 17
 }
18
+
19
+/**
20
+ * Returns reaction key from the reaction message.
21
+ *
22
+ * @param {string} message - The reaction message.
23
+ * @returns {string}
24
+ */
25
+export function getReactionKeyByMessage(message: string): ?string {
26
+    return Object.keys(REACTIONS).find(key => REACTIONS[key].message === `:${message}:`);
27
+}
28
+
29
+/**
30
+ * Gets reactions key array from concatenated message.
31
+ *
32
+ * @param {string} message - The reaction message.
33
+ * @returns {Array}
34
+ */
35
+export function messageToKeyArray(message: string) {
36
+    let formattedMessage = message.replace(/::/g, '-');
37
+
38
+    formattedMessage = formattedMessage.replace(/:/g, '');
39
+    const messageArray = formattedMessage.split('-');
40
+
41
+    return messageArray.map<?string>(getReactionKeyByMessage);
42
+}
43
+
44
+/**
45
+ * Sends reactions to the backend.
46
+ *
47
+ * @param {Object} state - The redux state object.
48
+ * @param {Array} reactions - Reactions array to be sent.
49
+ * @returns {void}
50
+ */
51
+export async function sendReactionsWebhook(state: Object, reactions: Array<?string>) {
52
+    const { webhookProxyUrl: url } = state['features/base/config'];
53
+    const { conference } = state['features/base/conference'];
54
+    const { jwt } = state['features/base/jwt'];
55
+    const { locationURL } = state['features/base/connection'];
56
+    const localParticipant = getLocalParticipant(state);
57
+
58
+    const headers = {
59
+        'Authorization': `Bearer ${jwt}`,
60
+        'Content-Type': 'application/json'
61
+    };
62
+
63
+
64
+    const reqBody = {
65
+        meetingFqn: extractFqnFromPath(locationURL.pathname),
66
+        sessionId: conference.sessionId,
67
+        submitted: Date.now(),
68
+        reactions,
69
+        participantId: localParticipant.id,
70
+        participantName: localParticipant.name
71
+    };
72
+
73
+    if (url) {
74
+        try {
75
+            const res = await fetch(`${url}/reactions`, {
76
+                method: 'POST',
77
+                headers,
78
+                body: JSON.stringify(reqBody)
79
+            });
80
+
81
+            if (!res.ok) {
82
+                logger.error('Status error:', res.status);
83
+            }
84
+        } catch (err) {
85
+            logger.error('Could not send request', err);
86
+        }
87
+    }
88
+}

+ 5
- 0
react/features/reactions/logger.js View File

@@ -0,0 +1,5 @@
1
+// @flow
2
+
3
+import { getLogger } from '../base/logging/functions';
4
+
5
+export default getLogger('features/base/reactions');

+ 8
- 1
react/features/reactions/middleware.js View File

@@ -2,6 +2,7 @@
2 2
 
3 3
 import { ENDPOINT_REACTION_NAME } from '../../../modules/API/constants';
4 4
 import { MiddlewareRegistry } from '../base/redux';
5
+import { isVpaasMeeting } from '../billing-counter/functions';
5 6
 
6 7
 import {
7 8
     SET_REACTIONS_MESSAGE,
@@ -17,6 +18,7 @@ import {
17 18
     setReactionQueue
18 19
 } from './actions.any';
19 20
 import { REACTIONS } from './constants';
21
+import { messageToKeyArray, sendReactionsWebhook } from './functions.any';
20 22
 
21 23
 
22 24
 declare var APP: Object;
@@ -46,7 +48,12 @@ MiddlewareRegistry.register(store => next => action => {
46 48
     }
47 49
 
48 50
     case CLEAR_REACTIONS_MESSAGE: {
49
-        const { message } = getState()['features/reactions'];
51
+        const state = getState();
52
+        const { message } = state['features/reactions'];
53
+
54
+        if (isVpaasMeeting(state)) {
55
+            sendReactionsWebhook(state, messageToKeyArray(message));
56
+        }
50 57
 
51 58
         dispatch(addReactionsMessageToChat(message));
52 59
 

Loading…
Cancel
Save