feathermost/js/store.js

127 lines
3.4 KiB
JavaScript

const store = (function() { "use strict";
class ChannelStore {
constructor(client) {
this.client = client;
this._channels = null;
this._unread = null;
this._promise = null;
this._fetching = false;
}
fetch() {
if (this._fetching) {
return;
}
this._fetching = true;
this._channels = null;
this._unread = null;
const teams = this.client.myTeams();
const channels = teams
.then(teams => Promise.all(teams.map(team => this.client.myChannels(team.id))))
.then(teams => teams.map(channels => arrayToHashmap(channels, "id")))
.then(flattenHashmaps);
const unread = teams
.then(teams => Promise.all(teams.map(team => this.client.getUnread(team.id))))
.then(flattenArrays);
function processUnread(channels, unread) {
// In the Mattermost API, not the number of *unread* messages but the number of *read*
// messages is returned, so we have to subtract that from the number of total messages.
let result = Object.create(null);
for (let x of unread) {
if (!(x["channel_id"] in channels)) continue;
let object = Object.create(null);
object.unread = channels[x["channel_id"]]["total_msg_count"] - x["msg_count"];
object.mentions = x["mention_count"];
result[x["channel_id"]] = object;
}
return result;
}
this._promise = Promise.all([teams, channels, unread])
.then(([teams, channels, unread]) => {
this._teams = arrayToHashmap(teams, "id");
this._channels = channels;
this._unread = processUnread(channels, unread);
this._fetching = false;
pubsub.publish("CHANNELS_RELOADED");
})
.catch(() => {this._fetching = false;});
}
async get() {
if (this._promise === null) {
this.fetch();
}
await this._promise;
return {teams: this._teams, channels: this._channels, unread: this._unread};
}
getNoFetch() {
if (this._promise === null) {
return null;
}
return {teams: this._teams, channels: this._channels, unread: this._unread};
}
async updateChannel(channel) {
if (this._promise === null) {
// Data does not exist yet and is not being requested
return;
}
await this._promise;
this._channels[channel["id"]] = channel;
}
async increaseUnread(post) {
if (this._promise === null) {
// Data does not exist yet and is not being requested
return;
}
await this._promise;
// TODO Check if post is mention and increase mentions counter if it is
this._unread[post["channel_id"]].unread += 1;
const unread = this._unread[post["channel_id"]].unread;
const mentions = this._unread[post["channel_id"]].mentions;
pubsub.publish("CHANNEL_UNREAD_UPDATED", {endpoint: this.client.endpoint, channel_id: post["channel_id"], unread, mentions});
}
async setUnread(channel_id, unread, mentions) {
if (this._promise === null) {
// Data does not exist yet and is not being requested
return;
}
await this._promise;
this._unread[channel_id].unread = unread;
this._unread[channel_id].mentions = mentions;
pubsub.publish("CHANNEL_UNREAD_UPDATED", {endpoint: this.client.endpoint, channel_id, unread, mentions});
}
async updateLastPostTime(channel_id, time) {
if (this._promise === null) {
// Data does not exist yet and is not being requested
return;
}
await this._promise;
this._channels[channel_id]["last_post_at"] = time;
pubsub.publish("CHANNEL_LAST_POST_TIME_UPDATED", {endpoint: this.client.endpoint, channel_id, time});
}
}
return {ChannelStore};
})();