Debian Codegraph @

addons/mail/static/src/discuss/call/common/ptt_extension_service.js

raw symbols SCIP
1 import { markup, reactive } from "@odoo/owl";
2
3 import { parseVersion } from "@mail/utils/common/misc";
4 import { browser } from "@web/core/browser/browser";
5 import { registry } from "@web/core/registry";
6 import { _t } from "@web/core/l10n/translation";
7
8 /** In object so it's patchable */
9 export const pttExtensionServiceInternal = {
10 onAnswerIsEnabled(pttService) {
11 pttService.isEnabled = true;
12 },
13 };
14
15 export const pttExtensionHookService = {
16 start(env) {
17 const INITIAL_RELEASE_TIMEOUT = 750;
18 const COMMON_RELEASE_TIMEOUT = 200;
19 // https://chromewebstore.google.com/detail/discuss-push-to-talk/mdiacebcbkmjjlpclnbcgiepgifcnpmg
20 const EXT_ID = "mdiacebcbkmjjlpclnbcgiepgifcnpmg";
21 const versionPromise =
22 window.chrome?.runtime
23 ?.sendMessage(EXT_ID, { type: "ask-version" })
24 .catch(() => "1.0.0.0") ?? Promise.resolve("1.0.0.0");
25 const self = reactive({
26 isEnabled: undefined,
27 voiceActivated: undefined,
28 notifyIsTalking(isTalking) {
29 sendMessage("is-talking", isTalking);
30 },
31 subscribe() {
32 sendMessage("subscribe");
33 },
34 unsubscribe() {
35 self.voiceActivated = false;
36 sendMessage("unsubscribe");
37 },
38 downloadURL: `https://chromewebstore.google.com/detail/discuss-push-to-talk/${EXT_ID}`,
39 get downloadText() {
40 return _t(
41 "The Push-to-Talk feature is only accessible within tab focus. To enable the Push-to-Talk functionality outside of this tab, we recommend downloading our %(anchor_start)sextension%(anchor_end)s.",
42 {
43 anchor_start: markup`<a href="${this.downloadURL}" target="_blank" class="text-reset text-decoration-underline">`,
44 anchor_end: markup`</a>`,
45 }
46 );
47 },
48 });
49
50 browser.addEventListener("message", ({ data, origin, source }) => {
51 const rtc = env.services["discuss.rtc"];
52 if (
53 source !== window ||
54 origin !== location.origin ||
55 data.from !== "discuss-push-to-talk" ||
56 (!rtc && data.type !== "answer-is-enabled")
57 ) {
58 return;
59 }
60 switch (data.type) {
61 case "push-to-talk-pressed":
62 {
63 self.voiceActivated = false;
64 const isFirstPress = !rtc.selfSession?.isTalking;
65 rtc.onPushToTalk();
66 if (rtc.selfSession?.isTalking) {
67 // Second key press is slow to come thus, the first timeout
68 // must be greater than the following ones.
69 rtc.setPttReleaseTimeout(
70 isFirstPress ? INITIAL_RELEASE_TIMEOUT : COMMON_RELEASE_TIMEOUT
71 );
72 }
73 }
74 break;
75 case "toggle-voice":
76 {
77 if (self.voiceActivated) {
78 rtc.setPttReleaseTimeout(0);
79 } else {
80 rtc.onPushToTalk();
81 }
82 self.voiceActivated = !self.voiceActivated;
83 }
84 break;
85 case "answer-is-enabled":
86 pttExtensionServiceInternal.onAnswerIsEnabled(self);
87 break;
88 }
89 });
90
91 /**
92 * Send a message to the PTT extension.
93 *
94 * @param {"ask-is-enabled" | "subscribe" | "unsubscribe" | "is-talking"} type
95 * @param {*} value
96 */
97 async function sendMessage(type, value) {
98 if (!self.isEnabled && type !== "ask-is-enabled") {
99 return;
100 }
101 const version = parseVersion(await versionPromise);
102 if (location.origin === "null") {
103 return;
104 }
105 if (version.isLowerThan("1.0.0.2")) {
106 window.postMessage({ from: "discuss", type, value }, location.origin);
107 return;
108 }
109 window.chrome?.runtime?.sendMessage(EXT_ID, { type, value });
110 }
111
112 sendMessage("ask-is-enabled");
113
114 return self;
115 },
116 };
117
118 registry.category("services").add("discuss.ptt_extension", pttExtensionHookService);