-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMMM-social-counter.js
187 lines (153 loc) · 4.61 KB
/
MMM-social-counter.js
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
/* global Module */
/* Magic Mirror
* Module: MMM-social-counter
*
* By Niek Nijland <[email protected]>
* MIT Licensed.
*/
Module.register('MMM-social-counter', {
defaults: {
updatesEvery: 10,
size: 'medium',
icon: 'fa-twitter',
},
start: function () {
Log.info(`Starting module: ${this.name}`);
this.error;
this.twitterFollowers;
this.status = 'LOADING';
this.interval;
this.twitter = this.config.twitter;
this.updatesEvery = this.config.updatesEvery;
this.size = this.config.size;
this.icon = this.config.icon;
this.position = this.data.position;
if (typeof this.updatesEvery !== 'number') {
Log.error(
`Configuration error: "updatesEvery" should be a number, but is: "${this.updatesEvery}". Falling back to 1.`
);
this.updatesEvery = 1;
} else if (this.updatesEvery < 1) {
Log.error(
`Configuration error: "updatesEvery" should be higher than 1, but is: "${this.updatesEvery}". Falling back to 1.`
);
this.updatesEvery = 1;
}
if (
this.size !== 'small' &&
this.size !== 'medium' &&
this.size !== 'large'
) {
Log.error(
`"${this.size}" is not a supported value. Please use "small", "medium" or "large". Falling back to "medium".`
);
this.size = 'medium';
}
if (typeof this.twitter === 'undefined') {
this.status = 'ERROR';
this.error = 'Configuration error: No configurations found for twitter.';
return;
}
if (typeof this.twitter.apiKey !== 'string') {
this.status = 'ERROR';
this.error =
'Configuration error: No twitter apiKey set in configuration.';
return;
}
if (typeof this.twitter.apiKeySecret !== 'string') {
this.status = 'ERROR';
this.error =
'Configuration error: No twitter apiKeySecret set in configuration.';
return;
}
if (typeof this.twitter.username !== 'string') {
this.status = 'ERROR';
this.error =
'Configuration error: No twitter username set in configuration.';
return;
}
this.sendSocketNotification('TWITTER_AUTHENTICATE', {
apiKey: this.twitter.apiKey,
apiKeySecret: this.twitter.apiKeySecret,
});
},
socketNotificationReceived: function (notification, payload) {
switch (notification) {
case 'TWITTER_AUTHENTICATED': {
this.startInterval();
break;
}
case 'TWITTER_FOLLOWERS_COUNT': {
this.status = 'SUCCESS';
this.twitterFollowers = payload;
this.updateDom();
break;
}
case 'TWITTER_TOKEN_INVALID_OR_EXPIRED': {
this.sendSocketNotification('TWITTER_AUTHENTICATE', {
apiKey: this.twitter.apiKey,
apiKeySecret: this.twitter.apiKeySecret,
});
break;
}
case 'ERROR': {
this.status = 'ERROR';
this.error = payload;
this.updateDom();
break;
}
default: {
this.status = 'ERROR';
this.error = `Socket notivication error: Unknown notification "${notification}" received from node_helper. Please submit an issue in the MMM-social-counter repository.`;
this.updateDom();
}
}
},
startInterval: function () {
clearInterval(this.interval);
this.sendSocketNotification(
'TWITTER_GET_FOLLOWERS_COUNT',
this.twitter.username
);
this.interval = setInterval(() => {
this.sendSocketNotification(
'TWITTER_GET_FOLLOWERS_COUNT',
this.twitter.username
);
}, this.updatesEvery * 1000);
},
getDom: function () {
const wrapper = document.createElement('div');
wrapper.className = `wrapper ${this.size}`;
let justifyContent;
if (this.position.includes('right')) {
justifyContent = 'flex-end';
} else if (this.position.includes('center')) {
justifyContent = 'center';
} else {
justifyContent = 'flex-start';
}
wrapper.style.justifyContent = justifyContent;
const text = document.createElement('span');
text.classList.add('bright');
text.classList.add('text');
if (this.status === 'LOADING') {
text.textContent = 'Loading...';
return text;
}
if (this.status === 'ERROR') {
Log.error(this.error);
text.textContent = this.error;
return text;
}
const icon = document.createElement('i');
icon.className = 'fa ' + this.icon + ' icon';
text.textContent = this.twitterFollowers;
wrapper.appendChild(icon);
wrapper.appendChild(text);
return wrapper;
},
getStyles: function () {
return [this.file('css/MMM-social-counter.css')];
},
});