This repository has been archived by the owner on Aug 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
48 lines (38 loc) · 1.54 KB
/
index.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
const { Plugin } = require('powercord/entities');
const { getModule, FluxDispatcher } = require('powercord/webpack');
const { inject, uninject } = require('powercord/injector');
/* eslint-disable object-property-newline */
// noinspection JSUnresolvedVariable,JSUnresolvedFunction
module.exports = class FixUnreadChannels extends Plugin {
async startPlugin () {
this.loadMessages = this.loadMessages.bind(this);
this.patchHasUnread();
FluxDispatcher.subscribe('MESSAGE_DELETE', this.loadMessages);
}
pluginWillUnload () {
uninject('fix-unread-channels');
FluxDispatcher.unsubscribe('MESSAGE_DELETE', this.loadMessages);
}
patchHasUnread () {
const { getMessages } = getModule([ 'getMessages' ], false);
const ChannelsStore = getModule([ 'hasUnread' ], false);
inject('fix-unread-channels', ChannelsStore, 'hasUnread', ([ id ]) => {
const channel = ChannelsStore.getForDebugging(id);
const messages = getMessages(id);
if (channel) {
const ackTimestamp = channel.getAckTimestamp();
return !(channel._isThread && !channel._isActiveJoinedThread) && (
(messages.length) ? messages.last().timestamp.isAfter(ackTimestamp) : ackTimestamp < channel._lastMessageTimestamp
);
}
return false;
});
}
loadMessages ({ channelId }) {
const { getMessages } = getModule([ 'getMessages' ], false);
const { fetchMessages } = getModule([ 'fetchMessages' ], false);
if (!getMessages(channelId).length) {
fetchMessages({ channelId, limit: 1 });
}
}
};