-
Notifications
You must be signed in to change notification settings - Fork 2
/
plugin.head.html
290 lines (238 loc) · 10.4 KB
/
plugin.head.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<!--
* Copyright 2022 Nordeck IT + Consulting GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations
* under the License.
-->
<script type="application/javascript">
console.log(`FEEDBACK html plugin loaded.`);
const LOG = 'FEEDBACK_INJECTOR';
// DON'T DEPLOY THIS PLUGIN AND feedback.js ON THE SAME SERVER, THEY WILL CAUSE CONFLICT.
// REMOVE feedback.js FROM config.analytics.scriptURLs
// save the original function
const oldInitJitsiConference = window.JitsiMeetJS.JitsiConnection.prototype.initJitsiConference;
window.JitsiMeetJS.JitsiConnection.prototype.initJitsiConference = function () {
console.log('initJitsiConference', arguments);
// leaving conference global allows us to collect metrics from here later
conference = oldInitJitsiConference.apply(this, arguments);
// isCallstatsEnabled = true enables the feedback form when leaving the application
// note that in order to enable the feedback button in the toolbar, a callStatsID must be configured regardless
conference.statistics.isCallstatsEnabled = () => true;
const feedback = new Feedback();
// get the JWT on the conference initialization
feedback.sendEvent({
action: 'connection.stage.reached',
actionSubject: 'conference_muc.joined'
});
// overriding sendFeedback achieves 2 of our goals:
// 1. it lets us inject code in the right place (feedback submission)
// 2. it disables sending feedback to other backends, primarily callstats.io
conference.statistics.sendFeedback = async function (score, details) {
feedback.sendEvent({
action: 'feedback',
actionSubject: 'feedback',
attributes: {
rating: score,
comment: details
}
});
}
return conference;
}
// ------------------------------------------------------------------------------------------
class Feedback {
constructor(options) {
this.sendEvent = this.sendEvent.bind(this);
this.setUserProperties = this.setUserProperties.bind(this);
}
// called from lib-jitsi-meet
sendEvent(event) {
// console.log(`${LOG} handler`, event);
if (event.action === 'connection.stage.reached' && event.actionSubject === 'conference_muc.joined') {
this.handleJoin();
return;
}
if (event.action === 'feedback' && event.actionSubject === 'feedback') {
this.handleFeedback(event.attributes);
return;
}
}
// called from lib-jitsi-meet
setUserProperties(permanentProperties) {
// nothing
}
handleFeedback(data) {
const {rating, comment} = data;
console.log(`${LOG} Feedback`, rating, comment);
const jwt = window.APP.conference.feedbackToken;
console.log(`${LOG} gather metrics for JWT: ${jwt}`);
const postFeedback = async (jwt, payload) => {
const baseUrl = APP.store.getState()['features/base/config'].feedbackBackend;
const url = `${baseUrl}/feedback`;
const headers = {
'authorization': `Bearer ${jwt}`
};
const res = await fetch(url, {
method: 'POST',
headers,
body: JSON.stringify(payload)
});
if (!res.ok) {
throw `${LOG} Status error: ${res.status}`;
}
return res.text();
};
const metrics = this._gatherMetrics();
const payload = {
rating: rating,
rating_comment: comment,
metadata: {
...metrics
}
}
postFeedback(jwt, payload)
.then(result => {
console.log(`${LOG} feedback result: `, payload);
})
.catch(e => console.error(`${LOG} failed to feedback`, e));
}
handleJoin() {
// Extract matrix openId token from the Jitsi JWT token
const oidToken = this._getMatrixContext().matrix.token;
console.log(`${LOG} Extracted matrix token: ${oidToken}`);
const getToken = async (oidToken) => {
const baseUrl = APP.store.getState()['features/base/config'].feedbackBackend;
const url = `${baseUrl}/token`;
const headers = {
'authorization': `Bearer ${oidToken}`
};
const res = await fetch(url, {
method: 'GET',
headers
});
if (!res.ok) {
throw `${LOG} Status error: ${res.status}`;
}
return res.text();
};
// get the feedback JWT token as soon as possible
getToken(oidToken)
.then(feedbackToken => {
console.log(`${LOG} feedback JWT: ${feedbackToken}`);
window.APP.conference.feedbackToken = feedbackToken;
})
.catch(e => console.error(`${LOG} failed to fetch JWT`, e));
return;
}
_gatherMetrics() {
const metrics = {};
const config = APP.store.getState()['features/base/config'];
const flags = config.metadata || [];
flags.forEach(flag => {
try {
this._addMetric(flag, metrics)
} catch (e) {
console.error(`${LOG} Metrics error ${flag}:`, e);
}
});
return metrics;
}
_getMatrixContext() {
const token = window.APP.store.getState()['features/base/jwt'].jwt;
const payload = token.split('.')[1];
const content = JSON.parse(atob(payload));
return content.context;
}
_addMetric(flag, metrics) {
const config = APP.store.getState()['features/base/config'];
const conference = window.APP.store.getState()['features/base/conference'].conference;
const localParticipant = window.APP.store.getState()['features/base/participants'].local;
switch (flag) {
// meetingId
case 'MEETING_URL':
metrics.meetingUrl = window.location.href;
break;
case 'MEETING_ID':
metrics.meetingId = JitsiMeetJS.analytics.permanentProperties.conference_name;
break;
// participantID
case 'PARTICIPANT_ID':
metrics.participantId = conference.myUserId();
break;
// matrix user Id
case 'MATRIX_USER_ID':
metrics.matrixUserId = localParticipant.email;
break;
case 'DISPLAY_NAME':
metrics.displayName = localParticipant.name;
break;
case 'USER_REGION':
metrics.userRegion = JitsiMeetJS.analytics.permanentProperties.userRegion;
break;
// app data
case 'APP_LIB_VERSION':
metrics.appLibVersion = JitsiMeetJS.version;
break;
case 'APP_FOCUS_VERSION':
metrics.appFocusVersion = conference.componentsVersions.versions.focus;
break;
case 'APP_NAME':
metrics.appName = JitsiMeetJS.analytics.permanentProperties.appName;
break;
case 'APP_MEETING_REGION':
metrics.appMeetingRegion = config.deploymentInfo.region;
break;
case 'APP_SHARD':
metrics.appShard = config.deploymentInfo.shard;
break;
case 'APP_REGION':
metrics.appRegion = config.deploymentInfo.region;
break;
case 'APP_ENVIRONMENT':
metrics.appEnvironment = config.deploymentInfo.environment;
break;
case 'APP_ENV_TYPE':
metrics.appEnvType = config.deploymentInfo.envType;
break;
case 'APP_BACKEND_RELEASE':
metrics.appBackendRelease = config.deploymentInfo.backendRelease;
break;
// browser, os
case 'USER_AGENT':
metrics.userAgent = JitsiMeetJS.analytics.permanentProperties.user_agent;
break;
case 'BROWSER_NAME':
metrics.browserName = JitsiMeetJS.util.browser.getName();
break;
case 'BROWSER_VERSION':
metrics.browserVersion = JitsiMeetJS.util.browser.getVersion();
break;
case 'OS_NAME':
metrics.osName = JitsiMeetJS.util.browser._bowser.parseOS().name;
break;
case 'OS_VERSION':
metrics.osVersion = JitsiMeetJS.util.browser._bowser.parseOS().version;
break;
case 'OS_VERSION_NAME':
metrics.osVersionName = JitsiMeetJS.util.browser._bowser.parseOS().versionName;
break;
// standalone or embedded
case 'EXTERNAL_API':
metrics.externalApi = JitsiMeetJS.analytics.permanentProperties.externalApi;
break;
case 'IN_IFRAME':
metrics.inIframe = JitsiMeetJS.analytics.permanentProperties.inIframe;
break;
}
}
}
</script>