Update PinDMs.plugin.js

This commit is contained in:
Mirco Wittrien 2021-04-22 12:17:32 +02:00
parent 14aa7fc100
commit 6966fde8fc
1 changed files with 173 additions and 130 deletions

View File

@ -2,7 +2,7 @@
* @name PinDMs * @name PinDMs
* @author DevilBro * @author DevilBro
* @authorId 278543574059057154 * @authorId 278543574059057154
* @version 1.7.9 * @version 1.8.0
* @description Allows you to pin DMs, making them appear at the top of your DMs/ServerList * @description Allows you to pin DMs, making them appear at the top of your DMs/ServerList
* @invite Jx3TjNS * @invite Jx3TjNS
* @donate https://www.paypal.me/MircoWittrien * @donate https://www.paypal.me/MircoWittrien
@ -17,12 +17,16 @@ module.exports = (_ => {
"info": { "info": {
"name": "PinDMs", "name": "PinDMs",
"author": "DevilBro", "author": "DevilBro",
"version": "1.7.9", "version": "1.8.0",
"description": "Allows you to pin DMs, making them appear at the top of your DMs/ServerList" "description": "Allows you to pin DMs, making them appear at the top of your DMs/ServerList"
}, },
"changeLog": { "changeLog": {
"improved": { "improved": {
"Canary Changes": "Preparing Plugins for the changes that are already done on Discord Canary" "Settings": "Restructured Settings",
"User Specific": "Pinned DMs are now User-Specific, meaning the Plugin will not try to load the same pinned DMs for your second account"
},
"fixed": {
"Empty List": "Deleting the last Category caused the DM list to only render with the DMs of the deleted Category"
} }
} }
}; };
@ -68,22 +72,22 @@ module.exports = (_ => {
var hoveredCategory, draggedCategory, releasedCategory; var hoveredCategory, draggedCategory, releasedCategory;
var hoveredChannel, draggedChannel, releasedChannel; var hoveredChannel, draggedChannel, releasedChannel;
var settings = {}, preCategories = {}, preCollapseStates = {};
return class PinDMs extends Plugin { return class PinDMs extends Plugin {
onLoad () { onLoad () {
this.defaults = { this.defaults = {
settings: { general: {
sortInRecentOrder: {value: false, inner: true, description: "Channel List"}, pinIcon: {value: true, description: "Show a little 'Pin' Icon for pinned DMs in the Server List"},
sortInRecentOrderGuild: {value: false, inner: true, description: "Guild List"}, unreadAmount: {value: true, description: "Shows the Amount of unread Messages in a Category in the Channel List"},
showPinIcon: {value: true, inner: false, description: "Show a little 'Pin' icon for pinned DMs in the server list: "}, channelAmount: {value: true, description: "Shows the Amount of pinned DMs in a Category in the Channel List"}
showCategoryUnread: {value: true, inner: false, description: "Show the amount of unread Messages in a category in the channel list: "}, },
showCategoryAmount: {value: true, inner: false, description: "Show the amount of pinned DMs in a category in the channel list: "} recentOrder: {
channelList: {value: false, description: "Channel List"},
guildList: {value: false, description: "Server List"},
}, },
preCategories: { preCategories: {
friends: {value: false, description: "FRIENDS"}, friends: {value: {enabled: false, collapsed: false}, description: "FRIENDS"},
blocked: {value: false, description: "BLOCKED"}, blocked: {value: {enabled: false, collapsed: false}, description: "BLOCKED"},
groups: {value: false, description: "GROUPS"} groups: {value: {enabled: false, collapsed: false}, description: "GROUPS"}
} }
}; };
@ -167,6 +171,14 @@ module.exports = (_ => {
} }
onStart () { onStart () {
if (!BDFDB.DataUtils.load(this, "pinned", BDFDB.UserUtils.me.id)) {
let pinned = {};
let channelListPinned = BDFDB.DataUtils.load(this, "dmCategories");
let guildListPinned = BDFDB.DataUtils.load(this, "pinnedRecents");
if (!BDFDB.ObjectUtils.isEmpty(channelListPinned)) pinned.channelList = channelListPinned;
if (!BDFDB.ObjectUtils.isEmpty(guildListPinned)) pinned.guildList = guildListPinned;
if (pinned.channelList || pinned.guildList) BDFDB.DataUtils.save(pinned, this, "pinned", BDFDB.UserUtils.me.id);
}
this.forceUpdateAll(); this.forceUpdateAll();
} }
@ -182,52 +194,69 @@ module.exports = (_ => {
} }
getSettingsPanel (collapseStates = {}) { getSettingsPanel (collapseStates = {}) {
let settingsPanel, settingsItems = []; let settingsPanel;
return settingsPanel = BDFDB.PluginUtils.createSettingsPanel(this, {
for (let key in settings) if (!this.defaults.settings[key].inner) settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, { collapseStates: collapseStates,
type: "Switch", children: _ => {
plugin: this, let settingsItems = [];
keys: ["settings", key],
label: this.defaults.settings[key].description, settingsItems.push(Object.keys(this.defaults.general).map(key => BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
value: settings[key] type: "Switch",
})); plugin: this,
settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsPanelList, { keys: ["general", key],
title: "Sort pinned DMs in the 'recent message' order instead of the 'pinned at' order in:", label: this.defaults.general[key].description,
dividerTop: true, value: this.settings.general[key]
dividerBottom: true, })));
children: Object.keys(settings).map(key => this.defaults.settings[key].inner && BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
type: "Switch", settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.FormComponents.FormDivider, {
plugin: this, className: BDFDB.disCN.marginbottom8
keys: ["settings", key], }));
label: this.defaults.settings[key].description,
value: settings[key] settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsPanelList, {
})) title: "Sort pinned DMs in the 'Recent Message' instead of the 'Pinned at' Order in:",
})); children: Object.keys(this.defaults.recentOrder).map(key => BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsPanelList, { type: "Switch",
title: "Add predefined category for:", plugin: this,
dividerBottom: true, keys: ["recentOrder", key],
children: Object.keys(preCategories).map(key => BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, { label: this.defaults.recentOrder[key].description,
type: "Switch", value: this.settings.recentOrder[key]
plugin: this, }))
keys: ["preCategories", key], }));
label: BDFDB.LanguageUtils.LanguageStrings[this.defaults.preCategories[key].description],
value: preCategories[key] settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.FormComponents.FormDivider, {
})) className: BDFDB.disCN.marginbottom8
})); }));
settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsItem, {
type: "Button", settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsPanelList, {
color: BDFDB.LibraryComponents.Button.Colors.RED, title: "Add predefined Category for:",
label: "Unpin all pinned DMs", children: Object.keys(this.defaults.preCategories).map(key => BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsItem, {
onClick: _ => { type: "Switch",
BDFDB.ModalUtils.confirm(this, "Are you sure you want to unpin all pinned DMs?", _ => { label: BDFDB.LanguageUtils.LanguageStrings[this.defaults.preCategories[key].description],
BDFDB.DataUtils.remove(this, "dmCategories"); value: this.settings.preCategories[key].enabled,
BDFDB.DataUtils.remove(this, "pinnedRecents"); onChange: value => {
}); this.settings.preCategories[key].enabled = value;
}, BDFDB.DataUtils.save(this.settings.preCategories, this, "preCategories");
children: BDFDB.LanguageUtils.LanguageStrings.UNPIN }
})); }))
}));
return settingsPanel = BDFDB.PluginUtils.createSettingsPanel(this, settingsItems);
settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.FormComponents.FormDivider, {
className: BDFDB.disCN.marginbottom8
}));
settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsItem, {
type: "Button",
color: BDFDB.LibraryComponents.Button.Colors.RED,
label: "Unpin all pinned DMs",
onClick: _ => {
BDFDB.ModalUtils.confirm(this, "Are you sure you want to unpin all pinned DMs?", _ => BDFDB.DataUtils.remove(this, "pinned", BDFDB.UserUtils.me.id));
},
children: BDFDB.LanguageUtils.LanguageStrings.UNPIN
}));
return settingsItems.flat(10);
}
});
} }
onSettingsClosed () { onSettingsClosed () {
@ -238,12 +267,8 @@ module.exports = (_ => {
} }
forceUpdateAll () { forceUpdateAll () {
settings = BDFDB.DataUtils.get(this, "settings");
preCategories = BDFDB.DataUtils.get(this, "preCategories");
preCollapseStates = BDFDB.DataUtils.load(this, "preCollapseStates");
BDFDB.ReactUtils.forceUpdate(BDFDB.ReactUtils.findOwner(document.querySelector(BDFDB.dotCN.app), {name: "FluxContainer(PrivateChannels)", all: true, unlimited: true}));
BDFDB.PatchUtils.forceAllUpdates(this); BDFDB.PatchUtils.forceAllUpdates(this);
BDFDB.DiscordUtils.rerenderAll();
} }
onUserContextMenu (e) { onUserContextMenu (e) {
@ -265,10 +290,10 @@ module.exports = (_ => {
injectItem (instance, id, children, index) { injectItem (instance, id, children, index) {
if (!id) return; if (!id) return;
let pinnedInGuild = this.isPinned(id, "pinnedRecents"); let pinnedInGuild = this.isPinned(id, "guildList");
let categories = this.sortAndUpdateCategories("dmCategories", true); let categories = this.sortAndUpdateCategories("channelList", true);
let currentCategory = this.getCategory(id, "dmCategories"); let currentCategory = this.getCategory(id, "channelList");
children.splice(index, 0, BDFDB.ContextMenuUtils.createItem(BDFDB.LibraryComponents.MenuItems.MenuItem, { children.splice(index, 0, BDFDB.ContextMenuUtils.createItem(BDFDB.LibraryComponents.MenuItems.MenuItem, {
label: this.labels.context_pindm, label: this.labels.context_pindm,
@ -288,7 +313,7 @@ module.exports = (_ => {
id: BDFDB.ContextMenuUtils.createItemId(this.name, "unpin-channellist"), id: BDFDB.ContextMenuUtils.createItemId(this.name, "unpin-channellist"),
color: BDFDB.LibraryComponents.MenuItems.Colors.DANGER, color: BDFDB.LibraryComponents.MenuItems.Colors.DANGER,
action: _ => { action: _ => {
this.removeFromCategory(id, currentCategory, "dmCategories"); this.removeFromCategory(id, currentCategory, "channelList");
} }
}) : BDFDB.ContextMenuUtils.createItem(BDFDB.LibraryComponents.MenuItems.MenuItem, { }) : BDFDB.ContextMenuUtils.createItem(BDFDB.LibraryComponents.MenuItems.MenuItem, {
label: this.labels.context_addtonewcategory, label: this.labels.context_addtonewcategory,
@ -296,13 +321,13 @@ module.exports = (_ => {
color: BDFDB.LibraryComponents.MenuItems.Colors.BRAND, color: BDFDB.LibraryComponents.MenuItems.Colors.BRAND,
action: _ => { action: _ => {
this.openCategorySettingsModal({ this.openCategorySettingsModal({
id: this.generateID("dmCategories").toString(), id: this.generateID("channelList").toString(),
name: `${this.labels.header_pinneddms} #${categories.length + 1}`, name: `${this.labels.header_pinneddms} #${categories.length + 1}`,
dms: [id], dms: [id],
pos: categories.length, pos: categories.length,
collapsed: false, collapsed: false,
color: null color: null
}, "dmCategories", true); }, "channelList", true);
} }
}) })
}), }),
@ -311,8 +336,8 @@ module.exports = (_ => {
label: category.name || this.labels.header_pinneddms, label: category.name || this.labels.header_pinneddms,
id: BDFDB.ContextMenuUtils.createItemId(this.name, "pin-channellist", category.id), id: BDFDB.ContextMenuUtils.createItemId(this.name, "pin-channellist", category.id),
action: _ => { action: _ => {
if (currentCategory) this.removeFromCategory(id, currentCategory, "dmCategories"); if (currentCategory) this.removeFromCategory(id, currentCategory, "channelList");
this.addToCategory(id, category, "dmCategories"); this.addToCategory(id, category, "channelList");
} }
})).filter(n => n) })).filter(n => n)
}) : null }) : null
@ -323,8 +348,8 @@ module.exports = (_ => {
id: BDFDB.ContextMenuUtils.createItemId(this.name, pinnedInGuild ? "unpin-serverlist" : "pin-serverlist"), id: BDFDB.ContextMenuUtils.createItemId(this.name, pinnedInGuild ? "unpin-serverlist" : "pin-serverlist"),
danger: pinnedInGuild, danger: pinnedInGuild,
action: _ => { action: _ => {
if (!pinnedInGuild) this.addPin(id, "pinnedRecents"); if (!pinnedInGuild) this.addPin(id, "guildList");
else this.removePin(id, "pinnedRecents"); else this.removePin(id, "guildList");
} }
}) })
].filter(n => n) ].filter(n => n)
@ -332,7 +357,7 @@ module.exports = (_ => {
} }
processPrivateChannelsList (e) { processPrivateChannelsList (e) {
let categories = this.sortAndUpdateCategories("dmCategories", true); let categories = this.sortAndUpdateCategories("channelList", true);
if (categories.length) { if (categories.length) {
e.instance.props.channels = Object.assign({}, e.instance.props.channels); e.instance.props.channels = Object.assign({}, e.instance.props.channels);
e.instance.props.privateChannelIds = [].concat(e.instance.props.privateChannelIds || []); e.instance.props.privateChannelIds = [].concat(e.instance.props.privateChannelIds || []);
@ -344,7 +369,7 @@ module.exports = (_ => {
if (category) { if (category) {
BDFDB.ArrayUtils.remove(category.dms, draggedChannel, true); BDFDB.ArrayUtils.remove(category.dms, draggedChannel, true);
category.dms.splice(categoryId != undefined ? 0 : category.dms.indexOf(releasedChannel) + 1, 0, draggedChannel); category.dms.splice(categoryId != undefined ? 0 : category.dms.indexOf(releasedChannel) + 1, 0, draggedChannel);
BDFDB.DataUtils.save(category, this, "dmCategories", category.id); this.savePinnedChannels(Object.assign({}, this.getPinnedChannels("channelList"), {[category.id]: category}), "channelList");
} }
draggedChannel = null; draggedChannel = null;
releasedChannel = null; releasedChannel = null;
@ -357,7 +382,7 @@ module.exports = (_ => {
categories.splice(categories.indexOf(maybedReleasedCategory) + 1, 0, maybedDraggedCategory); categories.splice(categories.indexOf(maybedReleasedCategory) + 1, 0, maybedDraggedCategory);
let newCategories = {}, newPos = 0; let newCategories = {}, newPos = 0;
for (let category of [].concat(categories).reverse()) if (!category.predefined) newCategories[category.id] = Object.assign(category, {pos: newPos++}); for (let category of [].concat(categories).reverse()) if (!category.predefined) newCategories[category.id] = Object.assign(category, {pos: newPos++});
BDFDB.DataUtils.save(newCategories, this, "dmCategories"); this.savePinnedChannels(newCategories, "channelList");
} }
draggedCategory = null; draggedCategory = null;
releasedCategory = null; releasedCategory = null;
@ -365,7 +390,7 @@ module.exports = (_ => {
e.instance.props.pinnedChannelIds = {}; e.instance.props.pinnedChannelIds = {};
for (let category of [].concat(categories).reverse()) { for (let category of [].concat(categories).reverse()) {
e.instance.props.pinnedChannelIds[category.id] = []; e.instance.props.pinnedChannelIds[category.id] = [];
for (let id of this.sortDMsByTime(this.filterDMs(category.dms, !category.predefined), "dmCategories").reverse()) { for (let id of this.sortDMsByTime(this.filterDMs(category.dms, !category.predefined), "channelList").reverse()) {
BDFDB.ArrayUtils.remove(e.instance.props.privateChannelIds, id, true); BDFDB.ArrayUtils.remove(e.instance.props.privateChannelIds, id, true);
if (!category.collapsed || e.instance.props.selectedChannelId == id) { if (!category.collapsed || e.instance.props.selectedChannelId == id) {
e.instance.props.privateChannelIds.unshift(id); e.instance.props.privateChannelIds.unshift(id);
@ -465,7 +490,7 @@ module.exports = (_ => {
if (category && draggedCategory != category.id) { if (category && draggedCategory != category.id) {
let color = BDFDB.ColorUtils.convert(category.color, "RGBA"); let color = BDFDB.ColorUtils.convert(category.color, "RGBA");
let foundDMs = this.filterDMs(category.dms, !category.predefined); let foundDMs = this.filterDMs(category.dms, !category.predefined);
let unreadAmount = settings.showCategoryUnread && BDFDB.ArrayUtils.sum(foundDMs.map(id => BDFDB.LibraryModules.UnreadChannelUtils.getMentionCount(id))); let unreadAmount = this.settings.general.unreadAmount && BDFDB.ArrayUtils.sum(foundDMs.map(id => BDFDB.LibraryModules.UnreadChannelUtils.getMentionCount(id)));
return category.predefined && foundDMs.length < 1 ? null : [ return category.predefined && foundDMs.length < 1 ? null : [
BDFDB.ReactUtils.createElement("h2", { BDFDB.ReactUtils.createElement("h2", {
className: BDFDB.DOMUtils.formatClassName(BDFDB.disCN.dmchannelheadercontainer, BDFDB.disCN._pindmspinnedchannelsheadercontainer, category.collapsed && BDFDB.disCN._pindmspinnedchannelsheadercollapsed, color && BDFDB.disCN._pindmspinnedchannelsheadercolored, BDFDB.disCN.namecontainernamecontainer), className: BDFDB.DOMUtils.formatClassName(BDFDB.disCN.dmchannelheadercontainer, BDFDB.disCN._pindmspinnedchannelsheadercontainer, category.collapsed && BDFDB.disCN._pindmspinnedchannelsheadercollapsed, color && BDFDB.disCN._pindmspinnedchannelsheadercolored, BDFDB.disCN.namecontainernamecontainer),
@ -477,7 +502,7 @@ module.exports = (_ => {
if (Math.sqrt((event.pageX - event2.pageX)**2) > 20 || Math.sqrt((event.pageY - event2.pageY)**2) > 20) { if (Math.sqrt((event.pageX - event2.pageX)**2) > 20 || Math.sqrt((event.pageY - event2.pageY)**2) > 20) {
BDFDB.ListenerUtils.stopEvent(event); BDFDB.ListenerUtils.stopEvent(event);
draggedCategory = category.id; draggedCategory = category.id;
this.updateContainer("dmCategories"); this.updateContainer("channelList");
let dragPreview = this.createDragPreview(node, event2); let dragPreview = this.createDragPreview(node, event2);
document.removeEventListener("mousemove", mouseMove); document.removeEventListener("mousemove", mouseMove);
document.removeEventListener("mouseup", mouseUp); document.removeEventListener("mouseup", mouseUp);
@ -487,16 +512,16 @@ module.exports = (_ => {
let categoryNode = BDFDB.DOMUtils.getParent(BDFDB.dotCN._pindmspinnedchannelsheadercontainer, placeholder ? placeholder.previousSibling : event3.target); let categoryNode = BDFDB.DOMUtils.getParent(BDFDB.dotCN._pindmspinnedchannelsheadercontainer, placeholder ? placeholder.previousSibling : event3.target);
let maybeHoveredCategory = categoryNode && categoryNode.getAttribute("categoryId"); let maybeHoveredCategory = categoryNode && categoryNode.getAttribute("categoryId");
let update = maybeHoveredCategory != hoveredCategory; let update = maybeHoveredCategory != hoveredCategory;
if (maybeHoveredCategory && !preCategories[maybeHoveredCategory]) hoveredCategory = maybeHoveredCategory; if (maybeHoveredCategory && !this.defaults.preCategories[maybeHoveredCategory]) hoveredCategory = maybeHoveredCategory;
else hoveredCategory = null; else hoveredCategory = null;
if (update) this.updateContainer("dmCategories"); if (update) this.updateContainer("channelList");
}; };
let releasing = event3 => { let releasing = event3 => {
BDFDB.DOMUtils.remove(dragPreview); BDFDB.DOMUtils.remove(dragPreview);
if (hoveredCategory) releasedCategory = hoveredCategory; if (hoveredCategory) releasedCategory = hoveredCategory;
else draggedCategory = null; else draggedCategory = null;
hoveredCategory = null; hoveredCategory = null;
this.updateContainer("dmCategories"); this.updateContainer("channelList");
document.removeEventListener("mousemove", dragging); document.removeEventListener("mousemove", dragging);
document.removeEventListener("mouseup", releasing); document.removeEventListener("mouseup", releasing);
}; };
@ -515,11 +540,11 @@ module.exports = (_ => {
if (foundDMs.length || !category.collapsed) { if (foundDMs.length || !category.collapsed) {
category.collapsed = !category.collapsed; category.collapsed = !category.collapsed;
if (category.predefined) { if (category.predefined) {
preCollapseStates[category.id] = category.collapsed; this.settings.preCategories[category.id].collapsed = category.collapsed;
BDFDB.DataUtils.save(category.collapsed, this, "preCollapseStates", category.id); BDFDB.DataUtils.save(this.settings.preCategories, this, "preCategories");
} }
else BDFDB.DataUtils.save(category, this, "dmCategories", category.id); else this.savePinnedChannels(Object.assign({}, this.getPinnedChannels("channelList"), {[category.id]: category}), "channelList");
this.updateContainer("dmCategories"); this.updateContainer("channelList");
} }
}, },
onContextMenu: event => { onContextMenu: event => {
@ -528,16 +553,17 @@ module.exports = (_ => {
label: this.labels.context_disablepredefined, label: this.labels.context_disablepredefined,
id: BDFDB.ContextMenuUtils.createItemId(this.name, "disable-predefined"), id: BDFDB.ContextMenuUtils.createItemId(this.name, "disable-predefined"),
action: _ => { action: _ => {
preCategories[category.id] = false; if (!this.settings.preCategories[category.id]) return;
BDFDB.DataUtils.save(preCategories, this, "preCategories"); this.settings.preCategories[category.id].enabled = false;
this.updateContainer("dmCategories"); BDFDB.DataUtils.save(this.settings.preCategories, this, "preCategories");
this.updateContainer("channelList");
} }
}) : [ }) : [
BDFDB.ContextMenuUtils.createItem(BDFDB.LibraryComponents.MenuItems.MenuItem, { BDFDB.ContextMenuUtils.createItem(BDFDB.LibraryComponents.MenuItems.MenuItem, {
label: BDFDB.LanguageUtils.LanguageStrings.CATEGORY_SETTINGS, label: BDFDB.LanguageUtils.LanguageStrings.CATEGORY_SETTINGS,
id: BDFDB.ContextMenuUtils.createItemId(this.name, "category-settings"), id: BDFDB.ContextMenuUtils.createItemId(this.name, "category-settings"),
action: _ => { action: _ => {
this.openCategorySettingsModal(category, "dmCategories"); this.openCategorySettingsModal(category, "channelList");
} }
}), }),
BDFDB.ContextMenuUtils.createItem(BDFDB.LibraryComponents.MenuItems.MenuItem, { BDFDB.ContextMenuUtils.createItem(BDFDB.LibraryComponents.MenuItems.MenuItem, {
@ -545,8 +571,10 @@ module.exports = (_ => {
id: BDFDB.ContextMenuUtils.createItemId(this.name, "remove-category"), id: BDFDB.ContextMenuUtils.createItemId(this.name, "remove-category"),
color: BDFDB.LibraryComponents.MenuItems.Colors.DANGER, color: BDFDB.LibraryComponents.MenuItems.Colors.DANGER,
action: _ => { action: _ => {
BDFDB.DataUtils.remove(this, "dmCategories", category.id); let newData = this.getPinnedChannels("channelList");
this.updateContainer("dmCategories"); delete newData[category.id];
this.savePinnedChannels(newData, "channelList");
this.updateContainer("channelList");
} }
}) })
] ]
@ -569,7 +597,7 @@ module.exports = (_ => {
count: unreadAmount, count: unreadAmount,
style: {backgroundColor: BDFDB.DiscordConstants.Colors.STATUS_RED} style: {backgroundColor: BDFDB.DiscordConstants.Colors.STATUS_RED}
}) : null, }) : null,
settings.showCategoryAmount ? BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Badges.NumberBadge, { this.settings.general.channelAmount ? BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Badges.NumberBadge, {
className: BDFDB.disCN._pindmspinnedchannelsheaderamount, className: BDFDB.disCN._pindmspinnedchannelsheaderamount,
count: foundDMs.length, count: foundDMs.length,
style: {backgroundColor: "var(--bdfdb-blurple)"} style: {backgroundColor: "var(--bdfdb-blurple)"}
@ -600,7 +628,7 @@ module.exports = (_ => {
processUnreadDMs (e) { processUnreadDMs (e) {
e.instance.props.pinnedPrivateChannelIds = []; e.instance.props.pinnedPrivateChannelIds = [];
let sortedRecents = this.sortAndUpdate("pinnedRecents"); let sortedRecents = this.sortAndUpdate("guildList");
if (sortedRecents.length) { if (sortedRecents.length) {
e.instance.props.unreadPrivateChannelIds = []; e.instance.props.unreadPrivateChannelIds = [];
for (let pos in sortedRecents) { for (let pos in sortedRecents) {
@ -617,7 +645,7 @@ module.exports = (_ => {
BDFDB.ArrayUtils.remove(pinnedPrivateChannelIds, draggedChannel, true); BDFDB.ArrayUtils.remove(pinnedPrivateChannelIds, draggedChannel, true);
pinnedPrivateChannelIds.splice(pinnedPrivateChannelIds.indexOf(releasedChannel) + 1, 0, draggedChannel); pinnedPrivateChannelIds.splice(pinnedPrivateChannelIds.indexOf(releasedChannel) + 1, 0, draggedChannel);
for (let pos in pinnedPrivateChannelIds) newData[pinnedPrivateChannelIds[pos]] = parseInt(pos); for (let pos in pinnedPrivateChannelIds) newData[pinnedPrivateChannelIds[pos]] = parseInt(pos);
BDFDB.DataUtils.save(newData, this, "pinnedRecents"); this.savePinnedChannels(newData, "guildList");
draggedChannel = null; draggedChannel = null;
releasedChannel = null; releasedChannel = null;
BDFDB.ReactUtils.forceUpdate(e.instance); BDFDB.ReactUtils.forceUpdate(e.instance);
@ -642,12 +670,12 @@ module.exports = (_ => {
processPrivateChannel (e) { processPrivateChannel (e) {
if (e.instance.props.channel && !this.getPredefinedCategory(e.instance.props.channel.id)) { if (e.instance.props.channel && !this.getPredefinedCategory(e.instance.props.channel.id)) {
let category = this.getCategory(e.instance.props.channel.id, "dmCategories"); let category = this.getCategory(e.instance.props.channel.id, "channelList");
if (category) { if (category) {
if (e.node) { if (e.node) {
BDFDB.DOMUtils.addClass(e.node, BDFDB.disCN._pindmsdmchannelpinned); BDFDB.DOMUtils.addClass(e.node, BDFDB.disCN._pindmsdmchannelpinned);
e.node.removeEventListener("mousedown", e.node.PinDMsMouseDownListener); e.node.removeEventListener("mousedown", e.node.PinDMsMouseDownListener);
if (!settings.sortInRecentOrder) { if (!this.settings.recentOrder.channelList) {
e.node.setAttribute("draggable", false); e.node.setAttribute("draggable", false);
e.node.PinDMsMouseDownListener = event => { e.node.PinDMsMouseDownListener = event => {
if (!this.started) e.node.removeEventListener("mousedown", e.node.PinDMsMouseDownListener); if (!this.started) e.node.removeEventListener("mousedown", e.node.PinDMsMouseDownListener);
@ -657,7 +685,7 @@ module.exports = (_ => {
if (Math.sqrt((event.pageX - event2.pageX)**2) > 20 || Math.sqrt((event.pageY - event2.pageY)**2) > 20) { if (Math.sqrt((event.pageX - event2.pageX)**2) > 20 || Math.sqrt((event.pageY - event2.pageY)**2) > 20) {
BDFDB.ListenerUtils.stopEvent(event); BDFDB.ListenerUtils.stopEvent(event);
draggedChannel = e.instance.props.channel.id; draggedChannel = e.instance.props.channel.id;
this.updateContainer("dmCategories"); this.updateContainer("channelList");
let dragPreview = this.createDragPreview(e.node, event2); let dragPreview = this.createDragPreview(e.node, event2);
document.removeEventListener("mousemove", mouseMove); document.removeEventListener("mousemove", mouseMove);
document.removeEventListener("mouseup", mouseUp); document.removeEventListener("mouseup", mouseUp);
@ -672,20 +700,20 @@ module.exports = (_ => {
else { else {
let placeholder = BDFDB.DOMUtils.getParent(BDFDB.dotCN._pindmsdmchannelplaceholder, event3.target); let placeholder = BDFDB.DOMUtils.getParent(BDFDB.dotCN._pindmsdmchannelplaceholder, event3.target);
maybeHoveredChannel = (BDFDB.ReactUtils.findValue(BDFDB.DOMUtils.getParent(BDFDB.dotCN._pindmsdmchannelpinned, placeholder ? placeholder.previousSibling : event3.target), "channel", {up: true}) || {}).id; maybeHoveredChannel = (BDFDB.ReactUtils.findValue(BDFDB.DOMUtils.getParent(BDFDB.dotCN._pindmsdmchannelpinned, placeholder ? placeholder.previousSibling : event3.target), "channel", {up: true}) || {}).id;
let maybeHoveredCategory = maybeHoveredChannel && this.getCategory(maybeHoveredChannel, "dmCategories"); let maybeHoveredCategory = maybeHoveredChannel && this.getCategory(maybeHoveredChannel, "channelList");
if (!maybeHoveredCategory || maybeHoveredCategory.id != category.id) maybeHoveredChannel = null; if (!maybeHoveredCategory || maybeHoveredCategory.id != category.id) maybeHoveredChannel = null;
}; };
let update = maybeHoveredChannel != hoveredChannel; let update = maybeHoveredChannel != hoveredChannel;
if (maybeHoveredChannel) hoveredChannel = maybeHoveredChannel; if (maybeHoveredChannel) hoveredChannel = maybeHoveredChannel;
else hoveredChannel = null; else hoveredChannel = null;
if (update) this.updateContainer("dmCategories"); if (update) this.updateContainer("channelList");
}; };
let releasing = event3 => { let releasing = event3 => {
BDFDB.DOMUtils.remove(dragPreview); BDFDB.DOMUtils.remove(dragPreview);
if (hoveredChannel) releasedChannel = hoveredChannel; if (hoveredChannel) releasedChannel = hoveredChannel;
else draggedChannel = null; else draggedChannel = null;
hoveredChannel = null; hoveredChannel = null;
this.updateContainer("dmCategories"); this.updateContainer("channelList");
document.removeEventListener("mousemove", dragging); document.removeEventListener("mousemove", dragging);
document.removeEventListener("mouseup", releasing); document.removeEventListener("mouseup", releasing);
}; };
@ -711,7 +739,7 @@ module.exports = (_ => {
className: BDFDB.disCN._pindmsunpinbutton, className: BDFDB.disCN._pindmsunpinbutton,
onClick: event => { onClick: event => {
BDFDB.ListenerUtils.stopEvent(event); BDFDB.ListenerUtils.stopEvent(event);
this.removeFromCategory(e.instance.props.channel.id, category, "dmCategories"); this.removeFromCategory(e.instance.props.channel.id, category, "channelList");
}, },
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SvgIcon, { children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SvgIcon, {
className: BDFDB.disCN._pindmsunpinicon, className: BDFDB.disCN._pindmsunpinicon,
@ -731,10 +759,10 @@ module.exports = (_ => {
BDFDB.DOMUtils.removeClass(e.node, BDFDB.disCN._pindmsrecentpinned); BDFDB.DOMUtils.removeClass(e.node, BDFDB.disCN._pindmsrecentpinned);
e.node.removeEventListener("contextmenu", e.node.PinDMsContextMenuListener); e.node.removeEventListener("contextmenu", e.node.PinDMsContextMenuListener);
e.node.addEventListener("contextmenu", e.node.PinDMsContextMenuListener); e.node.addEventListener("contextmenu", e.node.PinDMsContextMenuListener);
if (this.isPinned(e.instance.props.channel.id, "pinnedRecents")) { if (this.isPinned(e.instance.props.channel.id, "guildList")) {
BDFDB.DOMUtils.addClass(e.node, BDFDB.disCN._pindmsrecentpinned); BDFDB.DOMUtils.addClass(e.node, BDFDB.disCN._pindmsrecentpinned);
e.node.removeEventListener("mousedown", e.node.PinDMsMouseDownListener); e.node.removeEventListener("mousedown", e.node.PinDMsMouseDownListener);
if (!settings.sortInRecentOrderGuild) { if (!this.settings.recentOrder.guildList) {
for (let child of e.node.querySelectorAll("a")) child.setAttribute("draggable", false); for (let child of e.node.querySelectorAll("a")) child.setAttribute("draggable", false);
e.node.PinDMsMouseDownListener = event => { e.node.PinDMsMouseDownListener = event => {
let mousemove = event2 => { let mousemove = event2 => {
@ -781,7 +809,7 @@ module.exports = (_ => {
if (e.node && e.methodname == "componentWillUnmount") { if (e.node && e.methodname == "componentWillUnmount") {
BDFDB.PatchUtils.forceAllUpdates(this, "PrivateChannelsList"); BDFDB.PatchUtils.forceAllUpdates(this, "PrivateChannelsList");
} }
if (e.returnvalue && this.isPinned(e.instance.props.channel.id, "pinnedRecents") && settings.showPinIcon) { if (e.returnvalue && this.settings.general.pinIcon && this.isPinned(e.instance.props.channel.id, "guildList")) {
let [children, index] = BDFDB.ReactUtils.findParent(e.returnvalue, {name: "BlobMask"}); let [children, index] = BDFDB.ReactUtils.findParent(e.returnvalue, {name: "BlobMask"});
if (index > -1) children[index].props.upperLeftBadge = BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Badges.IconBadge, { if (index > -1) children[index].props.upperLeftBadge = BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Badges.IconBadge, {
className: BDFDB.disCN.guildiconbadge, className: BDFDB.disCN.guildiconbadge,
@ -792,10 +820,22 @@ module.exports = (_ => {
} }
} }
} }
getPinnedChannels (type) {
return ((BDFDB.DataUtils.load(this, "pinned", BDFDB.UserUtils.me.id) || {})[type] || {});
}
savePinnedChannels (channels, type) {
let pinned = BDFDB.DataUtils.load(this, "pinned", BDFDB.UserUtils.me.id) || {};
if (BDFDB.ObjectUtils.is(channels) && !BDFDB.ObjectUtils.isEmpty(channels)) pinned[type] = channels;
else delete pinned[type];
if (!BDFDB.ObjectUtils.isEmpty(pinned)) BDFDB.DataUtils.save(pinned, this, "pinned", BDFDB.UserUtils.me.id);
else BDFDB.DataUtils.remove(this, "pinned", BDFDB.UserUtils.me.id);
}
generateID (type) { generateID (type) {
if (!type) return null; if (!type) return null;
let categories = BDFDB.DataUtils.load(this, type); let categories = this.getPinnedChannels(type);
let id = Math.round(Math.random() * 10000000000000000); let id = Math.round(Math.random() * 10000000000000000);
return categories[id] ? this.generateID() : id; return categories[id] ? this.generateID() : id;
} }
@ -808,8 +848,8 @@ module.exports = (_ => {
if (!id || !category || !type) return; if (!id || !category || !type) return;
let wasEmpty = !this.filterDMs(category.dms).length; let wasEmpty = !this.filterDMs(category.dms).length;
if (!category.dms.includes(id)) category.dms.unshift(id); if (!category.dms.includes(id)) category.dms.unshift(id);
this.savePinnedChannels(Object.assign({}, this.getPinnedChannels(type), {[category.id]: category}), type);
if (wasEmpty && category.dms.length) category.collapsed = false; if (wasEmpty && category.dms.length) category.collapsed = false;
BDFDB.DataUtils.save(category, this, type, category.id);
this.updateContainer(type); this.updateContainer(type);
} }
@ -817,13 +857,13 @@ module.exports = (_ => {
if (!id || !category || !type) return; if (!id || !category || !type) return;
BDFDB.ArrayUtils.remove(category.dms, id, true); BDFDB.ArrayUtils.remove(category.dms, id, true);
if (!this.filterDMs(category.dms).length) category.collapsed = true; if (!this.filterDMs(category.dms).length) category.collapsed = true;
BDFDB.DataUtils.save(category, this, type, category.id); this.savePinnedChannels(Object.assign({}, this.getPinnedChannels(type), {[category.id]: category}), type);
this.updateContainer(type); this.updateContainer(type);
} }
getCategory (id, type) { getCategory (id, type) {
if (!id || !type) return null; if (!id || !type) return null;
let categories = BDFDB.DataUtils.load(this, type); let categories = this.getPinnedChannels(type);
for (let catId in categories) if (categories[catId].dms.includes(id)) return categories[catId]; for (let catId in categories) if (categories[catId].dms.includes(id)) return categories[catId];
return null; return null;
} }
@ -832,14 +872,14 @@ module.exports = (_ => {
if (!id) return ""; if (!id) return "";
let channel = BDFDB.LibraryModules.ChannelStore.getChannel(id); let channel = BDFDB.LibraryModules.ChannelStore.getChannel(id);
if (!channel) return ""; if (!channel) return "";
else if (preCategories.friends && channel.isDM() && BDFDB.LibraryModules.RelationshipStore.isFriend(channel.recipients[0])) return "friends"; else if (this.settings.preCategories.friends.enabled && channel.isDM() && BDFDB.LibraryModules.RelationshipStore.isFriend(channel.recipients[0])) return "friends";
else if (preCategories.blocked && channel.isDM() && BDFDB.LibraryModules.RelationshipStore.isBlocked(channel.recipients[0])) return "blocked"; else if (this.settings.preCategories.blocked.enabled && channel.isDM() && BDFDB.LibraryModules.RelationshipStore.isBlocked(channel.recipients[0])) return "blocked";
else if (preCategories.groups && channel.isGroupDM()) return "groups"; else if (this.settings.preCategories.groups.enabled && channel.isGroupDM()) return "groups";
return ""; return "";
} }
sortAndUpdateCategories (type, reverse) { sortAndUpdateCategories (type, reverse) {
let data = BDFDB.ObjectUtils.sort(BDFDB.DataUtils.load(this, type), "pos"), newData = {}; let data = BDFDB.ObjectUtils.sort(this.getPinnedChannels(type), "pos"), newData = {};
let sorted = [], pos = 0, sort = id => { let sorted = [], pos = 0, sort = id => {
if (sorted[pos] === undefined) { if (sorted[pos] === undefined) {
newData[id] = Object.assign({}, data[id], {pos}); newData[id] = Object.assign({}, data[id], {pos});
@ -851,8 +891,8 @@ module.exports = (_ => {
} }
}; };
for (let id in data) sort(id); for (let id in data) sort(id);
if (!BDFDB.equals(data, newData)) BDFDB.DataUtils.save(newData, this, type); if (!BDFDB.equals(data, newData)) this.savePinnedChannels(newData, type);
if (type == "dmCategories" && Object.keys(preCategories).some(type => preCategories[type])) { if (type == "channelList" && Object.keys(this.settings.preCategories).some(type => this.settings.preCategories[type].enabled)) {
let predefinedDMs = {}; let predefinedDMs = {};
for (let channelId of BDFDB.LibraryModules.DirectMessageStore.getPrivateChannelIds()) { for (let channelId of BDFDB.LibraryModules.DirectMessageStore.getPrivateChannelIds()) {
let category = this.getPredefinedCategory(channelId); let category = this.getPredefinedCategory(channelId);
@ -863,7 +903,7 @@ module.exports = (_ => {
} }
for (let type in predefinedDMs) if (predefinedDMs[type].length) sorted.unshift({ for (let type in predefinedDMs) if (predefinedDMs[type].length) sorted.unshift({
predefined: true, predefined: true,
collapsed: preCollapseStates[type], collapsed: this.settings.preCategories[type].collapsed,
color: null, color: null,
dms: predefinedDMs[type], dms: predefinedDMs[type],
id: type, id: type,
@ -874,7 +914,7 @@ module.exports = (_ => {
} }
sortDMsByTime (dms, type) { sortDMsByTime (dms, type) {
if (dms.length > 1 && settings[type == "dmCategories" ? "sortInRecentOrder" : "sortInRecentOrderGuild"]) { if (dms.length > 1 && this.settings.recentOrder[type]) {
let timestamps = BDFDB.LibraryModules.DirectMessageStore.getPrivateChannelIds().reduce((newObj, channelId) => (newObj[channelId] = BDFDB.LibraryModules.UnreadChannelUtils.lastMessageId(channelId), newObj), {}); let timestamps = BDFDB.LibraryModules.DirectMessageStore.getPrivateChannelIds().reduce((newObj, channelId) => (newObj[channelId] = BDFDB.LibraryModules.UnreadChannelUtils.lastMessageId(channelId), newObj), {});
return [].concat(dms).sort(function (x, y) {return timestamps[x] > timestamps[y] ? -1 : timestamps[x] < timestamps[y] ? 1 : 0;}); return [].concat(dms).sort(function (x, y) {return timestamps[x] > timestamps[y] ? -1 : timestamps[x] < timestamps[y] ? 1 : 0;});
} }
@ -926,7 +966,7 @@ module.exports = (_ => {
else if (newData.color[0] > 225 && newData.color[1] > 225 && newData.color[2] > 225) newData.color = BDFDB.ColorUtils.change(newData.color, -30); else if (newData.color[0] > 225 && newData.color[1] > 225 && newData.color[2] > 225) newData.color = BDFDB.ColorUtils.change(newData.color, -30);
} }
BDFDB.DataUtils.save(newData, this, type, data.id); this.savePinnedChannels(Object.assign({}, this.getPinnedChannels(type), {[data.id]: newData}), type);
this.updateContainer(type); this.updateContainer(type);
} }
@ -936,36 +976,39 @@ module.exports = (_ => {
addPin (newId, type) { addPin (newId, type) {
if (!newId) return; if (!newId) return;
let pinnedDMs = BDFDB.DataUtils.load(this, type); let channels = this.getPinnedChannels(type);
for (let id in pinnedDMs) pinnedDMs[id] = pinnedDMs[id] + 1; for (let id in channels) channels[id] = channels[id] + 1;
pinnedDMs[newId] = 0; channels[newId] = 0;
BDFDB.DataUtils.save(pinnedDMs, this, type); this.savePinnedChannels(channels, type);
this.updateContainer(type); this.updateContainer(type);
} }
removePin (id, type) { removePin (id, type) {
if (!id) return; if (!id) return;
BDFDB.DataUtils.remove(this, type, id); let newData = this.getPinnedChannels(type);
delete newData[category.id];
this.savePinnedChannels(newData, type);
this.updateContainer(type); this.updateContainer(type);
} }
isPinned (id, type) { isPinned (id, type) {
return BDFDB.DataUtils.load(this, type, id) != undefined; return this.getPinnedChannels(type)[id] != undefined;
} }
updateContainer (type) { updateContainer (type) {
switch (type) { switch (type) {
case "dmCategories": case "channelList":
BDFDB.PatchUtils.forceAllUpdates(this, "PrivateChannelsList"); BDFDB.PatchUtils.forceAllUpdates(this, "PrivateChannelsList");
if (!Object.keys(this.settings.preCategories).every(type => this.settings.preCategories[type].enabled) && BDFDB.ObjectUtils.isEmpty(this.getPinnedChannels(type))) this.forceUpdateAll();
break; break;
case "pinnedRecents": case "guildList":
BDFDB.PatchUtils.forceAllUpdates(this, "UnreadDMs"); BDFDB.PatchUtils.forceAllUpdates(this, "UnreadDMs");
break; break;
} }
} }
sortAndUpdate (type) { sortAndUpdate (type) {
let data = BDFDB.DataUtils.load(this, type), newData = {}; let data = this.getPinnedChannels(type), newData = {};
delete data[""]; delete data[""];
delete data["null"]; delete data["null"];
let sortedDMs = [], existingDMs = [], sortDM = (id, pos) => { let sortedDMs = [], existingDMs = [], sortDM = (id, pos) => {
@ -978,7 +1021,7 @@ module.exports = (_ => {
newData[sortedDMs[pos]] = parseInt(pos); newData[sortedDMs[pos]] = parseInt(pos);
if (BDFDB.LibraryModules.ChannelStore.getChannel(sortedDMs[pos])) existingDMs.push(sortedDMs[pos]); if (BDFDB.LibraryModules.ChannelStore.getChannel(sortedDMs[pos])) existingDMs.push(sortedDMs[pos]);
} }
if (!BDFDB.equals(data, newData)) BDFDB.DataUtils.save(newData, this, type); if (!BDFDB.equals(data, newData)) this.savePinnedChannels(newData, this);
return this.sortDMsByTime(existingDMs, type); return this.sortDMsByTime(existingDMs, type);
} }