Adds caching to public servers
Also fixes bug found by Tropical regarding confirming close with live update enabled.
This commit is contained in:
parent
2d38c277d2
commit
eecdcebb01
|
@ -107,6 +107,7 @@
|
||||||
"Proxy": "readonly",
|
"Proxy": "readonly",
|
||||||
"Set": "readonly",
|
"Set": "readonly",
|
||||||
"WeakMap": "readonly",
|
"WeakMap": "readonly",
|
||||||
|
"Map": "readonly",
|
||||||
"Promise": "readonly",
|
"Promise": "readonly",
|
||||||
"ace": "readonly",
|
"ace": "readonly",
|
||||||
"Reflect": "readonly",
|
"Reflect": "readonly",
|
||||||
|
|
|
@ -148,6 +148,7 @@ export default new class CustomCSS extends Builtin {
|
||||||
children: editor,
|
children: editor,
|
||||||
confirmClose: () => {
|
confirmClose: () => {
|
||||||
if (!editorRef || !editorRef.current) return false;
|
if (!editorRef || !editorRef.current) return false;
|
||||||
|
if (Settings.get("settings", "customcss", "liveUpdate")) return false;
|
||||||
return editorRef.current.hasUnsavedChanges;
|
return editorRef.current.hasUnsavedChanges;
|
||||||
},
|
},
|
||||||
confirmationText: Strings.CustomCSS.confirmationText
|
confirmationText: Strings.CustomCSS.confirmationText
|
||||||
|
|
|
@ -273,24 +273,4 @@ export default new class EmoteModule extends Builtin {
|
||||||
for (const cat of categories) DataStore.invalidateCache("emotes", cat);
|
for (const cat of categories) DataStore.invalidateCache("emotes", cat);
|
||||||
this.loadEmoteData();
|
this.loadEmoteData();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// (async () => {
|
|
||||||
// const emoteData = await new Promise(resolve => {
|
|
||||||
// const req = require("request");
|
|
||||||
// req.get({url: "https://twitchemotes.com/api_cache/v3/global.json", json: true}, (err, resp, parsedData) => {
|
|
||||||
// for (const emote in parsedData) {
|
|
||||||
// if (emote.length < 4 || window.bemotes.includes(emote)) {
|
|
||||||
// delete parsedData[emote];
|
|
||||||
// continue;
|
|
||||||
// }
|
|
||||||
// parsedData[emote] = parsedData[emote].id;
|
|
||||||
// }
|
|
||||||
// resolve(parsedData);
|
|
||||||
// });
|
|
||||||
// });
|
|
||||||
// const fs = require("fs");
|
|
||||||
// fs.writeFileSync("Z:\\Programming\\BetterDiscordStuff\\BetterDiscordApp\\data\\emotes\\global.json", JSON.stringify(emoteData));
|
|
||||||
// return emoteData;
|
|
||||||
// })();
|
|
|
@ -6,44 +6,96 @@ const InviteActions = WebpackModules.getByProps("acceptInvite");
|
||||||
|
|
||||||
const BrowserWindow = require("electron").remote.BrowserWindow;
|
const BrowserWindow = require("electron").remote.BrowserWindow;
|
||||||
|
|
||||||
|
const betterDiscordServer = {
|
||||||
|
name: "BetterDiscord",
|
||||||
|
members: 55000,
|
||||||
|
categories: ["community", "programming", "support"],
|
||||||
|
description: "Official BetterDiscord server for plugins, themes, support, etc",
|
||||||
|
identifier: "86004744966914048",
|
||||||
|
iconUrl: "https://cdn.discordapp.com/icons/86004744966914048/292e7f6bfff2b71dfd13e508a859aedd.webp",
|
||||||
|
nativejoin: true,
|
||||||
|
invite_code: "BJD2yvJ",
|
||||||
|
pinned: true,
|
||||||
|
insertDate: 1517806800
|
||||||
|
};
|
||||||
|
|
||||||
export default class PublicServersConnection {
|
export default new class PublicServersConnection {
|
||||||
|
|
||||||
static get endPoint() {return "https://search.discordservers.com";}
|
constructor() {
|
||||||
static get joinEndPoint() {return "https://j.discordservers.com";}
|
this.cache = new Map();
|
||||||
static get connectEndPoint() {return "https://auth.discordservers.com/info";}
|
this.cache.set("featured", [betterDiscordServer]);
|
||||||
|
this.cache.set("popular", []);
|
||||||
|
this.cache.set("keywords", []);
|
||||||
|
this.cache.set("accessToken", "");
|
||||||
|
|
||||||
static getDefaultAvatar() {
|
window.debugPS = this;
|
||||||
|
}
|
||||||
|
|
||||||
|
get endPoint() {return "https://search.discordservers.com";}
|
||||||
|
get joinEndPoint() {return "https://j.discordservers.com";}
|
||||||
|
get connectEndPoint() {return "https://auth.discordservers.com/info";}
|
||||||
|
|
||||||
|
getDefaultAvatar() {
|
||||||
return AvatarDefaults.DEFAULT_AVATARS[Math.floor(Math.random() * 5)];
|
return AvatarDefaults.DEFAULT_AVATARS[Math.floor(Math.random() * 5)];
|
||||||
}
|
}
|
||||||
|
|
||||||
static hasJoined(id) {
|
hasJoined(id) {
|
||||||
return SortedGuildStore.getFlattenedGuildIds().includes(id);
|
return SortedGuildStore.getFlattenedGuildIds().includes(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
static search({term = "", keyword = "", from = 0} = {}) {
|
async search({term = "", keyword = "", from = 0} = {}) {
|
||||||
const request = require("request");
|
if (this.cache.has(term + keyword + from)) return this.cache.get(term + keyword + from);
|
||||||
return new Promise(resolve => {
|
|
||||||
const queries = [];
|
const queries = [];
|
||||||
if (keyword) queries.push(`keyword=${keyword.replace(/ /g, "%20").toLowerCase()}`);
|
if (keyword) queries.push(`keyword=${keyword.replace(/ /g, "%20").toLowerCase()}`);
|
||||||
if (term) queries.push(`term=${term.replace(/ /g, "%20")}`);
|
if (term) queries.push(`term=${term.replace(/ /g, "%20")}`);
|
||||||
if (from) queries.push(`from=${from}`);
|
if (from) queries.push(`from=${from}`);
|
||||||
const query = `?${queries.join("&")}`;
|
const query = `?${queries.join("&")}`;
|
||||||
request.get({url: `${this.endPoint}${query}${query ? "&schema=new" : "?schema=new"}`, json: true}, (err, resp, data) => {
|
|
||||||
if (err) return resolve(null);
|
try {
|
||||||
const next = data.size + data.from;
|
const response = await fetch(`${this.endPoint}${query}`, {method: "GET"});
|
||||||
resolve({
|
const data = await response.json();
|
||||||
servers: data.results,
|
const next = data.size + data.from;
|
||||||
size: data.size,
|
const results = {
|
||||||
from: data.from,
|
servers: data.results,
|
||||||
total: data.total,
|
size: data.size,
|
||||||
next: next >= data.total ? null : next
|
from: data.from,
|
||||||
});
|
total: data.total,
|
||||||
});
|
next: next >= data.total ? null : next
|
||||||
});
|
};
|
||||||
|
this.cache.set(term + keyword + from, results);
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static async join(id, native = false) {
|
async getDashboard() {
|
||||||
|
const cachedKeywords = this.cache.get("keywords");
|
||||||
|
if (cachedKeywords && cachedKeywords.length) return {featured: this.cache.get("featured"), popular: this.cache.get("popular"), keywords: cachedKeywords};
|
||||||
|
try {
|
||||||
|
const response = await fetch(`${this.endPoint}/dashboard`, {method: "GET"});
|
||||||
|
const data = await response.json();
|
||||||
|
|
||||||
|
const featuredFirst = data.results[0].key === "featured";
|
||||||
|
const featuredServers = data.results[featuredFirst ? 0 : 1].response.hits;
|
||||||
|
const popularServers = data.results[featuredFirst ? 1 : 0].response.hits;
|
||||||
|
const mainKeywords = data.mainKeywords.map(k => k.charAt(0).toUpperCase() + k.slice(1)).sort();
|
||||||
|
|
||||||
|
featuredServers.unshift(betterDiscordServer);
|
||||||
|
|
||||||
|
this.cache.set("featured", featuredServers);
|
||||||
|
this.cache.set("popular", popularServers);
|
||||||
|
this.cache.set("keywords", mainKeywords);
|
||||||
|
return {featured: this.cache.get("featured"), popular: this.cache.get("popular"), keywords: this.cache.get("keywords")};
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
return {featured: this.cache.get("featured"), popular: this.cache.get("popular"), keywords: this.cache.get("keywords")};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async join(id, native = false) {
|
||||||
if (native) return InviteActions.acceptInvite(id);
|
if (native) return InviteActions.acceptInvite(id);
|
||||||
try {
|
try {
|
||||||
await fetch(`${this.joinEndPoint}/${id}`,{
|
await fetch(`${this.joinEndPoint}/${id}`,{
|
||||||
|
@ -62,7 +114,7 @@ export default class PublicServersConnection {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static async checkConnection() {
|
async checkConnection() {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(this.connectEndPoint, {
|
const response = await fetch(this.connectEndPoint, {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
|
@ -82,20 +134,7 @@ export default class PublicServersConnection {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static async getDashboard() {
|
connect() {
|
||||||
try {
|
|
||||||
const response = await fetch(`${this.endPoint}/dashboard`, {
|
|
||||||
method: "GET"
|
|
||||||
});
|
|
||||||
const data = await response.json();
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
catch (error) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static connect() {
|
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
const joinWindow = new BrowserWindow(this.windowOptions);
|
const joinWindow = new BrowserWindow(this.windowOptions);
|
||||||
const url = `https://auth.discordservers.com/connect?scopes=guilds.join&previousUrl=${this.connectEndPoint}`;
|
const url = `https://auth.discordservers.com/connect?scopes=guilds.join&previousUrl=${this.connectEndPoint}`;
|
||||||
|
@ -108,7 +147,7 @@ export default class PublicServersConnection {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
static get windowOptions() {
|
get windowOptions() {
|
||||||
return {
|
return {
|
||||||
width: 490,
|
width: 490,
|
||||||
height: 500,
|
height: 500,
|
||||||
|
@ -125,5 +164,4 @@ export default class PublicServersConnection {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
};
|
||||||
}
|
|
||||||
|
|
|
@ -11,19 +11,6 @@ const SettingsView = WebpackModules.getByDisplayName("SettingsView");
|
||||||
const GuildActions = WebpackModules.getByProps("transitionToGuildSync");
|
const GuildActions = WebpackModules.getByProps("transitionToGuildSync");
|
||||||
const LayerManager = WebpackModules.getByProps("popLayer");
|
const LayerManager = WebpackModules.getByProps("popLayer");
|
||||||
|
|
||||||
const betterDiscordServer = {
|
|
||||||
name: "BetterDiscord",
|
|
||||||
members: 55000,
|
|
||||||
categories: ["community", "programming", "support"],
|
|
||||||
description: "Official BetterDiscord server for plugins, themes, support, etc",
|
|
||||||
identifier: "86004744966914048",
|
|
||||||
iconUrl: "https://cdn.discordapp.com/icons/86004744966914048/292e7f6bfff2b71dfd13e508a859aedd.webp",
|
|
||||||
nativejoin: true,
|
|
||||||
invite_code: "BJD2yvJ",
|
|
||||||
pinned: true,
|
|
||||||
insertDate: 1517806800
|
|
||||||
};
|
|
||||||
|
|
||||||
export default class PublicServers extends React.Component {
|
export default class PublicServers extends React.Component {
|
||||||
|
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
|
@ -67,19 +54,15 @@ export default class PublicServers extends React.Component {
|
||||||
|
|
||||||
async getDashboard() {
|
async getDashboard() {
|
||||||
const dashboardData = await Connection.getDashboard();
|
const dashboardData = await Connection.getDashboard();
|
||||||
const featuredFirst = dashboardData.results[0].key === "featured";
|
|
||||||
const featuredServers = dashboardData.results[featuredFirst ? 0 : 1].response.hits;
|
|
||||||
const popularServers = dashboardData.results[featuredFirst ? 1 : 0].response.hits;
|
|
||||||
const mainKeywords = dashboardData.mainKeywords.map(k => k.charAt(0).toUpperCase() + k.slice(1)).sort();
|
|
||||||
|
|
||||||
featuredServers.unshift(betterDiscordServer);
|
|
||||||
|
|
||||||
this.featured = featuredServers;
|
this.featured = dashboardData.featured;
|
||||||
this.popular = popularServers;
|
this.popular = dashboardData.popular;
|
||||||
this.keywords = mainKeywords;
|
this.keywords = dashboardData.keywords;
|
||||||
|
|
||||||
this.setState({loading: false});
|
this.setState({loading: false});
|
||||||
this.changeTab(this.state.tab);
|
this.changeTab(this.state.tab);
|
||||||
|
|
||||||
|
if (!this.keywords || !this.keywords.length) Modals.showConfirmationModal("Connection Error", "There was an error connecting to DiscordServers.com, it's possible their website/api is down. Please try again later.");
|
||||||
}
|
}
|
||||||
|
|
||||||
async connect() {
|
async connect() {
|
||||||
|
|
|
@ -2,9 +2,20 @@ import {React} from "modules";
|
||||||
import SearchIcon from "../../icons/search";
|
import SearchIcon from "../../icons/search";
|
||||||
|
|
||||||
export default class Search extends React.Component {
|
export default class Search extends React.Component {
|
||||||
|
constructor(props) {
|
||||||
|
super(props);
|
||||||
|
this.state = {value: this.props.value};
|
||||||
|
this.onChange = this.onChange.bind(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
onChange(e) {
|
||||||
|
this.setState({value: e.target.value});
|
||||||
|
if (this.props.onChange) this.props.onChange(e);
|
||||||
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
return <div className={"bd-search-wrapper" + (this.props.className ? ` ${this.props.className}` : "")}>
|
return <div className={"bd-search-wrapper" + (this.props.className ? ` ${this.props.className}` : "")}>
|
||||||
<input onChange={this.props.onChange} onKeyDown={this.props.onKeyDown} type="text" className="bd-search" placeholder={this.props.placeholder} maxLength="50" />
|
<input onChange={this.onChange} onKeyDown={this.props.onKeyDown} type="text" className="bd-search" placeholder={this.props.placeholder} maxLength="50" value={this.state.value} />
|
||||||
<SearchIcon />
|
<SearchIcon />
|
||||||
</div>;
|
</div>;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue