2022-08-08 05:12:31 +03:00
|
|
|
import gateway, { GatewayEventType, GatewayPayloadType, GatewayPresenceStatus } from "./gateway";
|
2022-05-05 20:52:35 +03:00
|
|
|
import logger from "./logging";
|
2022-04-19 21:21:55 +03:00
|
|
|
import request from "./request";
|
2022-05-06 01:55:21 +03:00
|
|
|
import { apiRoute, getItem, setItem } from "./storage";
|
2022-04-17 04:08:40 +03:00
|
|
|
|
2022-05-05 20:52:35 +03:00
|
|
|
const storeLog = logger("Store");
|
2022-04-23 23:07:46 +03:00
|
|
|
|
2022-04-17 20:23:20 +03:00
|
|
|
class Store {
|
2022-05-07 04:04:54 +03:00
|
|
|
constructor(value=null, name="[no name]") {
|
2022-04-17 20:23:20 +03:00
|
|
|
this._handlers = [];
|
2022-04-19 02:22:32 +03:00
|
|
|
this.value = value;
|
2022-05-05 15:33:22 +03:00
|
|
|
this.name = name;
|
2022-04-17 20:23:20 +03:00
|
|
|
}
|
|
|
|
|
2022-05-07 04:04:54 +03:00
|
|
|
// like subscribe, but without initially calling the handler
|
2022-05-07 03:27:41 +03:00
|
|
|
watch(handler) {
|
2022-05-07 04:04:54 +03:00
|
|
|
const handlerIndex = this._handlers.push(handler) - 1;
|
2022-05-07 03:27:41 +03:00
|
|
|
return () => {
|
|
|
|
this._handlers.splice(handlerIndex, 1);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-17 04:08:40 +03:00
|
|
|
subscribe(handler) {
|
2022-05-07 04:04:54 +03:00
|
|
|
const handlerIndex = this._handlers.push(handler) - 1;
|
2022-08-04 04:59:28 +03:00
|
|
|
storeLog(`(${this.name}) (subscribe/initial)`, this.value);
|
2022-04-19 02:17:24 +03:00
|
|
|
handler(this.value);
|
2022-04-17 04:08:40 +03:00
|
|
|
return () => {
|
|
|
|
this._handlers.splice(handlerIndex, 1);
|
|
|
|
};
|
2022-04-17 20:23:20 +03:00
|
|
|
}
|
|
|
|
|
2022-04-28 18:40:38 +03:00
|
|
|
set(value) {
|
2022-05-07 04:04:54 +03:00
|
|
|
if (value === this.value)
|
|
|
|
return;
|
|
|
|
|
2022-04-28 18:40:38 +03:00
|
|
|
this.value = value;
|
|
|
|
this.updated();
|
|
|
|
}
|
|
|
|
|
2022-09-01 19:56:19 +03:00
|
|
|
// like set(), but without checking if the value is the same
|
|
|
|
update(value) {
|
|
|
|
this.value = value;
|
|
|
|
this.updated();
|
|
|
|
}
|
|
|
|
|
2022-04-19 02:17:24 +03:00
|
|
|
updated() {
|
2022-05-07 04:04:54 +03:00
|
|
|
storeLog(`(${this.name}) (updated) Calling all (${this._handlers.length}) handlers`, this.value);
|
|
|
|
for (let i = this._handlers.length - 1; i >= 0; i--) {
|
|
|
|
this._handlers[i](this.value);
|
|
|
|
}
|
2022-04-17 20:23:20 +03:00
|
|
|
}
|
|
|
|
}
|
2022-04-17 04:08:40 +03:00
|
|
|
|
2022-05-07 03:36:27 +03:00
|
|
|
class StorageItemStore extends Store {
|
|
|
|
constructor(key) {
|
|
|
|
super(getItem(key), `StorageItemStore[key=${key}]`);
|
|
|
|
this.watch(e => setItem(key, e));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-17 20:23:20 +03:00
|
|
|
class ChannelsStore extends Store {
|
|
|
|
constructor() {
|
2022-05-05 15:33:22 +03:00
|
|
|
super(gateway.channels || [], "ChannelsStore");
|
2022-04-17 04:08:40 +03:00
|
|
|
|
|
|
|
gateway.subscribe(GatewayEventType.Ready, ({ channels }) => {
|
2022-04-19 02:17:24 +03:00
|
|
|
this.value = channels;
|
2022-08-04 02:00:55 +03:00
|
|
|
if (getItem("ui:stateful:presistSelectedChannel")) {
|
2022-08-04 04:59:28 +03:00
|
|
|
selectedChannel.value.id = getItem("state:openChannelId");
|
2022-08-04 02:00:55 +03:00
|
|
|
}
|
2022-04-27 23:41:50 +03:00
|
|
|
if (channels.length >= 1) {
|
2022-05-06 01:55:21 +03:00
|
|
|
if (!selectedChannel.value || selectedChannel.value.id === -1) {
|
|
|
|
selectedChannel.set(channels[0]);
|
|
|
|
} else {
|
|
|
|
// if a channel id is already selected, we'll populate it with the data we just got from the gateway
|
|
|
|
const index = this.value.findIndex(e => e.id === selectedChannel.value.id);
|
|
|
|
if (index !== -1)
|
|
|
|
selectedChannel.set(this.value[index]);
|
2022-05-06 02:56:22 +03:00
|
|
|
else // if the channel doesn't exist, just select the first one
|
|
|
|
selectedChannel.set(channels[0]);
|
2022-05-06 01:55:21 +03:00
|
|
|
}
|
2022-04-27 23:41:50 +03:00
|
|
|
}
|
2022-04-19 02:17:24 +03:00
|
|
|
this.updated();
|
2022-04-17 04:08:40 +03:00
|
|
|
});
|
|
|
|
gateway.subscribe(GatewayEventType.ChannelCreate, (channel) => {
|
2022-04-19 02:17:24 +03:00
|
|
|
this.value.push(channel);
|
|
|
|
this.updated();
|
2022-04-17 04:08:40 +03:00
|
|
|
});
|
|
|
|
gateway.subscribe(GatewayEventType.ChannelDelete, ({ id }) => {
|
2022-04-19 02:17:24 +03:00
|
|
|
const index = this.value.findIndex(e => e.id === id);
|
2022-04-19 02:19:24 +03:00
|
|
|
if (index === -1)
|
2022-04-17 04:08:40 +03:00
|
|
|
return;
|
|
|
|
|
2022-04-19 02:17:24 +03:00
|
|
|
this.value.splice(index, 1);
|
|
|
|
this.updated();
|
2022-04-17 04:08:40 +03:00
|
|
|
});
|
|
|
|
gateway.subscribe(GatewayEventType.ChannelUpdate, (data) => {
|
2022-04-19 02:17:24 +03:00
|
|
|
const index = this.value.findIndex(e => e.id === data.id);
|
2022-04-19 02:19:24 +03:00
|
|
|
if (index === -1)
|
2022-04-17 04:08:40 +03:00
|
|
|
return;
|
2022-04-19 02:17:24 +03:00
|
|
|
if (!this.value[index])
|
2022-04-17 04:08:40 +03:00
|
|
|
return;
|
|
|
|
|
2022-04-19 02:17:24 +03:00
|
|
|
this.value[index] = data;
|
|
|
|
this.updated();
|
2022-04-17 04:08:40 +03:00
|
|
|
});
|
2022-04-28 18:40:38 +03:00
|
|
|
gateway.subscribe(GatewayEventType.MessageCreate, ({ channel_id }) => {
|
|
|
|
const index = this.value.findIndex(e => e.id === channel_id);
|
2022-04-28 18:48:44 +03:00
|
|
|
if (index === -1 || !this.value[index] || selectedChannel.value.id === channel_id)
|
2022-04-28 18:40:38 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
this.value[index]._hasUnreads = true;
|
|
|
|
this.updated();
|
|
|
|
});
|
|
|
|
selectedChannel.subscribe(({ id }) => {
|
|
|
|
const index = this.value.findIndex(e => e.id === id);
|
2022-04-28 18:48:44 +03:00
|
|
|
if (index === -1 || !this.value[index] || !this.value[index]._hasUnreads)
|
2022-04-28 18:40:38 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
this.value[index]._hasUnreads = false;
|
|
|
|
this.updated();
|
|
|
|
});
|
2022-04-17 04:08:40 +03:00
|
|
|
}
|
2022-04-17 20:23:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
class GatewayStatusStore extends Store {
|
|
|
|
constructor() {
|
2022-08-04 04:59:28 +03:00
|
|
|
super({ ready: gateway.authenticated }, "GatewayStatusStore");
|
2022-04-17 20:23:20 +03:00
|
|
|
|
|
|
|
gateway.subscribe(GatewayEventType.Close, () => {
|
2022-04-19 02:17:24 +03:00
|
|
|
this.value.ready = false;
|
|
|
|
this.updated();
|
2022-04-17 20:23:20 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
gateway.subscribe(GatewayEventType.Ready, () => {
|
2022-04-19 02:17:24 +03:00
|
|
|
this.value.ready = true;
|
|
|
|
this.updated();
|
2022-04-17 20:23:20 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 03:44:48 +03:00
|
|
|
class UserInfoStore extends Store {
|
|
|
|
constructor() {
|
2022-05-05 15:33:22 +03:00
|
|
|
super(null, "UserInfoStore");
|
2022-04-20 03:44:48 +03:00
|
|
|
|
|
|
|
gateway.subscribe(GatewayEventType.Ready, ({ user }) => {
|
|
|
|
this.value = user;
|
|
|
|
this.updated();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 21:21:55 +03:00
|
|
|
class MessageStore extends Store {
|
|
|
|
constructor(channelId) {
|
2022-05-05 15:33:22 +03:00
|
|
|
super([], `MessageStore[channelId=${channelId}]`);
|
2022-04-19 21:21:55 +03:00
|
|
|
this.channelId = channelId;
|
|
|
|
this.isCollectingOldMessages = true;
|
|
|
|
this.didDoInitialLoad = false;
|
|
|
|
}
|
|
|
|
|
2022-04-21 01:55:37 +03:00
|
|
|
setMessage(id, message) {
|
|
|
|
const index = this.value.findIndex(e => e.id === id);
|
|
|
|
if (index === -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this.value[index] = message;
|
|
|
|
|
|
|
|
this.updated();
|
|
|
|
}
|
|
|
|
|
2022-04-19 21:21:55 +03:00
|
|
|
addMessage(message) {
|
2022-08-20 00:12:27 +03:00
|
|
|
if (message.optimistic_id) {
|
|
|
|
const index = this.value.findIndex(e => e.id === message.optimistic_id);
|
|
|
|
if (index !== -1) {
|
|
|
|
this.value[index] = message;
|
|
|
|
this.updated();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 21:21:55 +03:00
|
|
|
this.value.push(message);
|
|
|
|
// only dispatch update if collectOldMessages didn't
|
|
|
|
if (!this.collectOldMessages()) {
|
|
|
|
this.updated();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 03:44:48 +03:00
|
|
|
updateId(oldId, newId) {
|
|
|
|
const index = this.value.findIndex(e => e.id === oldId);
|
|
|
|
if (index === -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this.value[index].id = newId;
|
|
|
|
|
|
|
|
this.updated();
|
|
|
|
}
|
|
|
|
|
2022-04-19 21:21:55 +03:00
|
|
|
updateMessage(message) {
|
|
|
|
const index = this.value.findIndex(e => e.id === message.id);
|
|
|
|
if (index === -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this.value[index] = message;
|
|
|
|
|
|
|
|
this.updated();
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteMessage({ id }) {
|
|
|
|
const index = this.value.findIndex(e => e.id === id);
|
|
|
|
if (index === -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this.value.splice(index, 1);
|
|
|
|
|
|
|
|
this.updated();
|
|
|
|
}
|
|
|
|
|
|
|
|
collectOldMessages() {
|
|
|
|
if (!this.isCollectingOldMessages)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const target = 50;
|
|
|
|
const delta = this.value.length - target;
|
|
|
|
if (delta >= 1) {
|
|
|
|
this.value.splice(0, delta);
|
|
|
|
this.updated();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setIsCollectingOldMessages(isCollectingOldMessages) {
|
|
|
|
this.isCollectingOldMessages = isCollectingOldMessages;
|
|
|
|
this.collectOldMessages();
|
|
|
|
}
|
|
|
|
|
2022-04-22 16:43:26 +03:00
|
|
|
async loadOlderMessages(beforeCommitToStore=null) {
|
2022-08-31 11:40:10 +03:00
|
|
|
if (!getItem("ui:online:loadMessageHistory") || this.channelId === -1)
|
2022-05-08 22:02:16 +03:00
|
|
|
return;
|
|
|
|
|
2022-04-19 21:21:55 +03:00
|
|
|
const oldestMessage = this.value[0];
|
|
|
|
const endpoint = oldestMessage ? `channels/${this.channelId}/messages/?before=${oldestMessage.id}` : `channels/${this.channelId}/messages`;
|
|
|
|
const res = await request("GET", apiRoute(endpoint), true, null);
|
2022-04-26 04:00:18 +03:00
|
|
|
if (res.success && res.ok && res.json) {
|
|
|
|
if (res.json.length < 1)
|
|
|
|
return;
|
2022-04-22 16:43:26 +03:00
|
|
|
if (beforeCommitToStore)
|
|
|
|
beforeCommitToStore(res.json);
|
2022-04-20 03:14:28 +03:00
|
|
|
res.json.reverse();
|
2022-04-19 21:21:55 +03:00
|
|
|
this.value = res.json.concat(this.value);
|
|
|
|
this.updated();
|
2022-04-26 03:47:51 +03:00
|
|
|
} else {
|
|
|
|
overlayStore.open("toast", {
|
|
|
|
message: "Messages failed to load"
|
|
|
|
});
|
2022-04-19 21:21:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async doInitialLoad() {
|
2022-08-03 02:46:48 +03:00
|
|
|
if (this.channelId === -1 || !getItem("auth:token") || getItem("auth:token").length < 1)
|
2022-04-27 22:03:51 +03:00
|
|
|
return;
|
|
|
|
|
2022-04-19 21:21:55 +03:00
|
|
|
await this.loadOlderMessages();
|
|
|
|
this.didDoInitialLoad = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MessagesStoreProvider {
|
|
|
|
constructor() {
|
|
|
|
this.storeByChannel = new Map();
|
|
|
|
|
|
|
|
gateway.subscribe(GatewayEventType.MessageCreate, (message) => {
|
2022-04-26 23:20:28 +03:00
|
|
|
const store = this.getStoreOrNull(message.channel_id);
|
|
|
|
if (store)
|
|
|
|
store.addMessage(message);
|
2022-04-19 21:21:55 +03:00
|
|
|
});
|
|
|
|
gateway.subscribe(GatewayEventType.MessageUpdate, (message) => {
|
2022-04-26 23:20:28 +03:00
|
|
|
const store = this.getStoreOrNull(message.channel_id);
|
|
|
|
if (store)
|
|
|
|
store.updateMessage(message);
|
2022-04-19 21:21:55 +03:00
|
|
|
});
|
|
|
|
gateway.subscribe(GatewayEventType.MessageDelete, (message) => {
|
2022-04-26 23:20:28 +03:00
|
|
|
const store = this.getStoreOrNull(message.channel_id);
|
|
|
|
if (store)
|
|
|
|
store.deleteMessage(message);
|
2022-04-19 21:21:55 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-26 23:20:28 +03:00
|
|
|
getStoreOrNull(channelId) {
|
|
|
|
return this.storeByChannel.get(channelId);
|
|
|
|
}
|
|
|
|
|
2022-04-19 21:21:55 +03:00
|
|
|
getStore(channelId) {
|
2022-04-20 03:14:28 +03:00
|
|
|
if (!this.storeByChannel.get(channelId)) {
|
2022-04-20 03:44:48 +03:00
|
|
|
const store = new MessageStore(channelId);
|
|
|
|
store.doInitialLoad();
|
|
|
|
this.storeByChannel.set(channelId, store);
|
2022-04-19 21:21:55 +03:00
|
|
|
}
|
2022-04-20 03:14:28 +03:00
|
|
|
return this.storeByChannel.get(channelId);
|
2022-04-19 21:21:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-25 18:47:27 +03:00
|
|
|
class OverlayStore extends Store {
|
|
|
|
constructor() {
|
|
|
|
super({
|
2022-04-26 03:01:21 +03:00
|
|
|
createChannel: null,
|
2022-04-26 22:45:40 +03:00
|
|
|
editChannel: null,
|
|
|
|
toast: null,
|
|
|
|
login: null,
|
2022-04-27 22:03:51 +03:00
|
|
|
createAccount: null,
|
2022-05-08 22:05:58 +03:00
|
|
|
editMessage: null,
|
|
|
|
settings: null,
|
2022-08-03 02:34:15 +03:00
|
|
|
prompt: null,
|
2022-05-05 15:33:22 +03:00
|
|
|
}, "OverlayStore");
|
2022-04-25 18:47:27 +03:00
|
|
|
}
|
|
|
|
|
2022-04-25 23:02:15 +03:00
|
|
|
open(name, props={}) {
|
2022-05-08 22:05:58 +03:00
|
|
|
if (this.value[name] === undefined)
|
|
|
|
throw new Error(`OverlayStore.open: tried to open unknown overlay with name '${name}' (undefined in overlay map)`);
|
2022-04-25 23:02:15 +03:00
|
|
|
this.value[name] = props;
|
2022-04-25 18:47:27 +03:00
|
|
|
this.updated();
|
|
|
|
}
|
|
|
|
|
|
|
|
close(name) {
|
2022-05-07 04:04:54 +03:00
|
|
|
if (!this.value[name])
|
|
|
|
return;
|
|
|
|
|
2022-04-25 23:02:15 +03:00
|
|
|
this.value[name] = null;
|
2022-04-25 18:47:27 +03:00
|
|
|
this.updated();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-07 03:00:14 +03:00
|
|
|
class TypingStore extends Store {
|
|
|
|
constructor() {
|
|
|
|
super([], "TypingStore");
|
|
|
|
this.timeouts = new Map();
|
|
|
|
this.ownTimeout = null;
|
|
|
|
this.ownNeedsUpdate = true;
|
|
|
|
|
2022-08-31 11:40:10 +03:00
|
|
|
if (getItem("ui:online:processRemoteTypingEvents")) {
|
|
|
|
gateway.subscribe(GatewayPayloadType.TypingStart, ({ user, channel, time }) => {
|
|
|
|
if (userInfoStore && user.id === userInfoStore.value.id)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this.startedTyping(user, channel.id, time);
|
|
|
|
});
|
|
|
|
|
|
|
|
// assume someone has stopped typing once they send a message
|
|
|
|
gateway.subscribe(GatewayPayloadType.MessageCreate, ({ author_id }) => {
|
|
|
|
this.stoppedTyping(author_id);
|
|
|
|
});
|
|
|
|
}
|
2022-08-07 03:00:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
stoppedTyping(id) {
|
2022-08-07 21:57:41 +03:00
|
|
|
const index = this.value.findIndex(e => e.user.id === id);
|
2022-08-07 03:00:14 +03:00
|
|
|
this.value.splice(index, 1);
|
|
|
|
|
|
|
|
if (this.timeouts.get(id)) {
|
|
|
|
clearTimeout(this.timeouts.get(id));
|
|
|
|
this.timeouts.delete(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (userInfoStore.value && id === userInfoStore.value.id) {
|
|
|
|
clearTimeout(this.ownTimeout);
|
|
|
|
this.ownTimeout = null;
|
|
|
|
this.ownNeedsUpdate = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.updated();
|
|
|
|
}
|
|
|
|
|
2022-08-07 21:57:41 +03:00
|
|
|
startedTyping(user, channelId, time) {
|
2022-08-07 03:00:14 +03:00
|
|
|
if (this.timeouts.get(user.id)) {
|
|
|
|
clearTimeout(this.timeouts.get(user.id));
|
|
|
|
}
|
|
|
|
|
|
|
|
this.timeouts.set(user.id, setTimeout(() => {
|
|
|
|
this.stoppedTyping(user.id);
|
|
|
|
}, time));
|
|
|
|
|
|
|
|
if (userInfoStore.value && user.id === userInfoStore.value.id && !this.ownTimeout) {
|
|
|
|
this.ownTimeout = setTimeout(() => {
|
|
|
|
this.ownNeedsUpdate = true;
|
|
|
|
this.ownTimeout = null;
|
|
|
|
}, time);
|
|
|
|
}
|
|
|
|
|
2022-08-07 21:57:41 +03:00
|
|
|
const index = this.value.findIndex(e => e.user.id === user.id);
|
2022-08-07 03:00:14 +03:00
|
|
|
if (index === -1) {
|
2022-08-07 21:57:41 +03:00
|
|
|
this.value.push({
|
|
|
|
user,
|
|
|
|
channelId
|
|
|
|
});
|
|
|
|
this.updated();
|
|
|
|
} else if (this.value[index].channelId !== channelId) { // user just switched the channel they're typing in
|
|
|
|
this.value[index].channelId = channelId;
|
2022-08-07 03:00:14 +03:00
|
|
|
this.updated();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async didInputKey() {
|
2022-08-31 11:40:10 +03:00
|
|
|
if (!userInfoStore.value || !getItem("ui:online:sendTypingUpdates"))
|
2022-08-07 03:00:14 +03:00
|
|
|
return;
|
|
|
|
|
2022-08-07 21:57:41 +03:00
|
|
|
this.startedTyping(userInfoStore.value, selectedChannel.value.id, 6500);
|
2022-08-07 03:00:14 +03:00
|
|
|
if (this.ownNeedsUpdate) {
|
|
|
|
this.ownNeedsUpdate = false;
|
2022-08-07 21:57:41 +03:00
|
|
|
await request("POST", apiRoute(`channels/${selectedChannel.value.id}/typing`), true, {});
|
2022-08-07 03:00:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-08 05:12:31 +03:00
|
|
|
class PresenceStore extends Store {
|
|
|
|
constructor() {
|
|
|
|
super([], "PresenceStore");
|
|
|
|
|
2022-08-31 11:40:10 +03:00
|
|
|
if (getItem("ui:online:processRemotePresenceEvents")) {
|
|
|
|
gateway.subscribe(GatewayEventType.Ready, ({ presence }) => {
|
|
|
|
this.ingestPresenceUpdate(presence);
|
|
|
|
});
|
|
|
|
|
|
|
|
gateway.subscribe(GatewayEventType.PresenceUpdate, (data) => {
|
|
|
|
this.ingestPresenceUpdate(data);
|
|
|
|
});
|
|
|
|
}
|
2022-08-08 05:12:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
entryIndexByUserId(userId) {
|
|
|
|
return this.value.findIndex(a => a.user.id === userId);
|
|
|
|
}
|
|
|
|
|
|
|
|
ingestPresenceUpdate(payload) {
|
|
|
|
payload.forEach((entry) => {
|
|
|
|
const existingEntry = this.entryIndexByUserId(entry.user.id);
|
|
|
|
if (existingEntry !== -1 && entry.status === GatewayPresenceStatus.Offline) {
|
|
|
|
this.value.splice(existingEntry, 1);
|
|
|
|
} else if (existingEntry !== -1 && entry.status !== GatewayPresenceStatus.Offline) {
|
|
|
|
this.value[existingEntry] = entry;
|
|
|
|
} else {
|
|
|
|
// don't need to push the status, since we remove offline members from the presence list
|
|
|
|
this.value.push({
|
|
|
|
user: entry.user
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.updated();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-03 02:46:48 +03:00
|
|
|
export const selectedChannel = new Store({ id: -1, name: "none", creator_id: -1 }, "selectedChannel");
|
2022-05-07 04:04:54 +03:00
|
|
|
export const showSidebar = new Store(true, "showSidebar");
|
2022-08-08 05:12:31 +03:00
|
|
|
export const showPresenceSidebar = new Store(false, "showPresenceSidebar");
|
2022-05-05 15:33:22 +03:00
|
|
|
export const smallViewport = new Store(false, "smallViewport");
|
2022-05-07 04:04:54 +03:00
|
|
|
export const showChannelView = new Store(true, "showChannelView");
|
2022-08-04 02:00:55 +03:00
|
|
|
export const theme = new StorageItemStore("ui:theme");
|
|
|
|
export const doAnimations = new StorageItemStore("ui:doAnimations");
|
2022-04-17 20:23:20 +03:00
|
|
|
export const channels = new ChannelsStore();
|
|
|
|
export const gatewayStatus = new GatewayStatusStore();
|
2022-04-19 21:21:55 +03:00
|
|
|
export const messagesStoreProvider = new MessagesStoreProvider();
|
2022-04-20 03:44:48 +03:00
|
|
|
export const userInfoStore = new UserInfoStore();
|
2022-04-25 18:47:27 +03:00
|
|
|
export const overlayStore = new OverlayStore();
|
2022-08-07 03:00:14 +03:00
|
|
|
export const typingStore = new TypingStore();
|
2022-08-08 05:12:31 +03:00
|
|
|
export const presenceStore = new PresenceStore();
|
2022-09-01 19:56:19 +03:00
|
|
|
export const setMessageInputEvent = new Store(null, "event:setMessageInput");
|
2022-05-07 03:27:41 +03:00
|
|
|
|
2022-05-05 21:49:25 +03:00
|
|
|
export const allStores = {
|
|
|
|
selectedChannel,
|
|
|
|
showSidebar,
|
2022-08-08 05:12:31 +03:00
|
|
|
showPresenceSidebar,
|
2022-05-05 21:49:25 +03:00
|
|
|
showChannelView,
|
|
|
|
smallViewport,
|
2022-05-07 03:27:41 +03:00
|
|
|
theme,
|
2022-05-07 03:45:09 +03:00
|
|
|
doAnimations,
|
2022-05-05 21:49:25 +03:00
|
|
|
channels,
|
|
|
|
gatewayStatus,
|
|
|
|
messagesStoreProvider,
|
|
|
|
userInfoStore,
|
|
|
|
overlayStore,
|
2022-08-07 03:00:14 +03:00
|
|
|
typingStore,
|
2022-05-05 21:49:25 +03:00
|
|
|
};
|
2022-05-06 01:55:21 +03:00
|
|
|
|
2022-05-07 03:27:41 +03:00
|
|
|
selectedChannel.watch((newSelectedChannel) => {
|
2022-08-04 02:00:55 +03:00
|
|
|
if (getItem("ui:stateful:presistSelectedChannel")) {
|
|
|
|
setItem("state:openChannelId", newSelectedChannel.id);
|
|
|
|
}
|
2022-08-03 02:46:48 +03:00
|
|
|
});
|