aboutsummaryrefslogtreecommitdiffstats
path: root/src/models/Service.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/models/Service.js')
-rw-r--r--src/models/Service.js132
1 files changed, 132 insertions, 0 deletions
diff --git a/src/models/Service.js b/src/models/Service.js
new file mode 100644
index 000000000..7a0310ebc
--- /dev/null
+++ b/src/models/Service.js
@@ -0,0 +1,132 @@
1import { computed, observable } from 'mobx';
2import path from 'path';
3import normalizeUrl from 'normalize-url';
4
5export default class Service {
6 id = '';
7 recipe = '';
8 webview = null;
9 events: {};
10
11 isAttached = false;
12
13 @observable isActive = false; // Is current webview active
14
15 @observable name = '';
16 @observable unreadDirectMessageCount = 0;
17 @observable unreadIndirectMessageCount = 0;
18
19 @observable order = 99;
20 @observable isEnabled = true;
21 @observable team = '';
22 @observable customUrl = '';
23 @observable isNotificationEnabled = true;
24 @observable isIndirectMessageBadgeEnabled = true;
25 @observable customIconUrl = '';
26
27 constructor(data, recipe) {
28 if (!data) {
29 console.error('Service config not valid');
30 return null;
31 }
32
33 if (!recipe) {
34 console.error('Service recipe not valid');
35 return null;
36 }
37
38 this.id = data.id || this.id;
39 this.name = data.name || this.name;
40 this.team = data.team || this.team;
41 this.customUrl = data.customUrl || this.customUrl;
42 this.customIconUrl = data.customIconUrl || this.customIconUrl;
43
44 this.order = data.order !== undefined
45 ? data.order : this.order;
46
47 this.isEnabled = data.isEnabled !== undefined
48 ? data.isEnabled : this.isEnabled;
49
50 this.isNotificationEnabled = data.isNotificationEnabled !== undefined
51 ? data.isNotificationEnabled : this.isNotificationEnabled;
52
53 this.isIndirectMessageBadgeEnabled = data.isIndirectMessageBadgeEnabled !== undefined
54 ? data.isIndirectMessageBadgeEnabled : this.isIndirectMessageBadgeEnabled;
55
56 this.recipe = recipe;
57 }
58
59 @computed get url() {
60 if (this.recipe.hasCustomUrl && this.customUrl) {
61 let url = normalizeUrl(this.customUrl);
62
63 if (typeof this.recipe.buildUrl === 'function') {
64 url = this.recipe.buildUrl(url);
65 }
66
67 return url;
68 }
69
70 if (this.recipe.hasTeamId && this.team) {
71 return this.recipe.serviceURL.replace('{teamId}', this.team);
72 }
73
74 return this.recipe.serviceURL;
75 }
76
77 @computed get icon() {
78 if (this.hasCustomIcon) {
79 return this.customIconUrl;
80 }
81
82 return path.join(this.recipe.path, 'icon.svg');
83 }
84
85 @computed get hasCustomIcon() {
86 return (this.customIconUrl !== '');
87 }
88
89 @computed get iconPNG() {
90 return path.join(this.recipe.path, 'icon.png');
91 }
92
93 @computed get userAgent() {
94 let userAgent = window.navigator.userAgent;
95 if (typeof this.recipe.overrideUserAgent === 'function') {
96 userAgent = this.recipe.overrideUserAgent();
97 }
98
99 return userAgent;
100 }
101
102 initializeWebViewEvents(store) {
103 this.webview.addEventListener('ipc-message', e => store.actions.service.handleIPCMessage({
104 serviceId: this.id,
105 channel: e.channel,
106 args: e.args,
107 }));
108
109 this.webview.addEventListener('new-window', (event, url, frameName, options) => store.actions.service.openWindow({
110 event,
111 url,
112 frameName,
113 options,
114 }));
115 }
116
117 initializeWebViewListener() {
118 if (this.webview && this.recipe.events) {
119 Object.keys(this.recipe.events).forEach((eventName) => {
120 const eventHandler = this.recipe[this.recipe.events[eventName]];
121 if (typeof eventHandler === 'function') {
122 this.webview.addEventListener(eventName, eventHandler);
123 }
124 });
125 }
126 }
127
128 resetMessageCount() {
129 this.unreadDirectMessageCount = 0;
130 this.unreadIndirectMessageCount = 0;
131 }
132}