-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-Mattermost.js
95 lines (83 loc) · 2.79 KB
/
MMM-Mattermost.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
/* global Module */
/* Magic Mirror
* Module: MMM-Mattermost
*
* By Mikael Östberg
* MIT Licensed.
*/
Module.register("MMM-Mattermost", {
defaults: {
animationSpeed: 2000,
updateInterval: 60000,
rotationInterval: 15000,
retryDelay: 5000,
teamId: "",
searchTerms: "",
limit: 10,
title: "Mattermost",
accesstoken: ""
},
currentMessage: undefined,
posts: undefined,
postIds: [],
requiresVersion: "2.1.0",
currentMessageIndex: 0,
rotateMessage: function(){
this.currentMessage = this.postIds[this.currentMessageIndex];
this.currentMessageIndex = this.currentMessageIndex == this.config.limit ? 0 : this.currentMessageIndex + 1;
if(this.postIds.length <= this.currentMessageIndex) {
this.currentMessageIndex = 0;
}
this.updateDom(this.config.animationSpeed);
},
fetchMessages: function(){
this.sendSocketNotification("MMM-Mattermost-fetch-messages");
},
start: function() {
this.sendSocketNotification("MMM-Mattermost-set-config", this.config);
this.fetchMessages();
setInterval(this.fetchMessages.bind(this), this.config.updateInterval);
setInterval(this.rotateMessage.bind(this), this.config.rotationInterval);
},
getIsoDateString(timestamp){
let dateString = new Date(timestamp).toISOString().replace("T", " ").replace("Z", " ");
let dotIx = dateString.indexOf('.');
return dateString.substring(0, dotIx);
},
getDom: function() {
let wrapper = document.createElement("div");
let message = document.createElement("label");
let author = document.createElement("div");
let authorImage = document.createElement("img");
let authorText = document.createElement("span");
authorText.style = "color: #444; font-size: 14px;"
author.appendChild(authorImage);
author.appendChild(authorText);
let header = document.createElement("h2");
header.innerHTML = this.config.title;
if(this.posts !== undefined) {
let post = this.posts.find(p => p.id === this.currentMessage);
let dateString = this.getIsoDateString(post.create_at);
message.innerHTML = post.message
authorImage.style = 'width:15px;height:15px; border-radius:15px;'
authorImage.src = "data:image/jpeg;base64, " + post.user.b64;
authorText.innerHTML = " " + [post.user.first_name, post.user.last_name].join(' ') + " at " + dateString;
} else {
message.innerHTML = "No mattermost messages found, try modifying your searchterms";
}
wrapper.appendChild(header);
wrapper.appendChild(message);
wrapper.appendChild(author);
return wrapper;
},
socketNotificationReceived: function (notification, payload) {
if(notification === "MMM-Mattermost-received-messages") {
this.postIds = payload.order.slice(0, this.config.limit-1);
this.posts = payload.posts
if(!this.currentMessage){
this.rotateMessage();
}
this.updateDom(this.config.animationSpeed);
}
}
});