2019-06-20 20:03:48 +02:00
|
|
|
import {WebpackModules} from "modules";
|
|
|
|
|
|
|
|
const SortedGuildStore = WebpackModules.getByProps("getSortedGuilds");
|
|
|
|
const AvatarDefaults = WebpackModules.getByProps("getUserAvatarURL", "DEFAULT_AVATARS");
|
|
|
|
const InviteActions = WebpackModules.getByProps("acceptInvite");
|
|
|
|
|
|
|
|
const BrowserWindow = require("electron").remote.BrowserWindow;
|
|
|
|
|
2020-10-27 19:42:49 +01:00
|
|
|
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
|
|
|
|
};
|
2019-06-20 20:03:48 +02:00
|
|
|
|
2020-10-27 19:42:49 +01:00
|
|
|
export default new class PublicServersConnection {
|
2019-06-20 20:03:48 +02:00
|
|
|
|
2020-10-27 19:42:49 +01:00
|
|
|
constructor() {
|
|
|
|
this.cache = new Map();
|
|
|
|
this.cache.set("featured", [betterDiscordServer]);
|
|
|
|
this.cache.set("popular", []);
|
|
|
|
this.cache.set("keywords", []);
|
|
|
|
this.cache.set("accessToken", "");
|
2019-06-20 20:03:48 +02:00
|
|
|
|
2020-10-27 19:42:49 +01:00
|
|
|
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() {
|
2019-06-20 20:03:48 +02:00
|
|
|
return AvatarDefaults.DEFAULT_AVATARS[Math.floor(Math.random() * 5)];
|
|
|
|
}
|
|
|
|
|
2020-10-27 19:42:49 +01:00
|
|
|
hasJoined(id) {
|
2019-09-27 04:50:53 +02:00
|
|
|
return SortedGuildStore.getFlattenedGuildIds().includes(id);
|
2019-06-20 20:03:48 +02:00
|
|
|
}
|
|
|
|
|
2020-10-27 19:42:49 +01:00
|
|
|
async search({term = "", keyword = "", from = 0} = {}) {
|
|
|
|
if (this.cache.has(term + keyword + from)) return this.cache.get(term + keyword + from);
|
|
|
|
|
|
|
|
const queries = [];
|
|
|
|
if (keyword) queries.push(`keyword=${keyword.replace(/ /g, "%20").toLowerCase()}`);
|
|
|
|
if (term) queries.push(`term=${term.replace(/ /g, "%20")}`);
|
|
|
|
if (from) queries.push(`from=${from}`);
|
|
|
|
const query = `?${queries.join("&")}`;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await fetch(`${this.endPoint}${query}`, {method: "GET"});
|
|
|
|
const data = await response.json();
|
|
|
|
const next = data.size + data.from;
|
|
|
|
const results = {
|
|
|
|
servers: data.results,
|
|
|
|
size: data.size,
|
|
|
|
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;
|
|
|
|
}
|
2019-06-20 20:03:48 +02:00
|
|
|
}
|
|
|
|
|
2020-10-27 19:42:49 +01:00
|
|
|
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) {
|
2020-07-16 23:17:02 +02:00
|
|
|
if (native) return InviteActions.acceptInvite(id);
|
|
|
|
try {
|
|
|
|
await fetch(`${this.joinEndPoint}/${id}`,{
|
2019-06-20 20:03:48 +02:00
|
|
|
method: "GET",
|
2020-07-16 23:17:02 +02:00
|
|
|
credentials: "include",
|
|
|
|
mode: "cors",
|
2019-06-20 20:03:48 +02:00
|
|
|
headers: {
|
2020-07-16 23:17:02 +02:00
|
|
|
"Accept": "application/json",
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
}
|
2019-06-20 20:03:48 +02:00
|
|
|
});
|
2020-07-16 23:17:02 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-06-20 20:03:48 +02:00
|
|
|
}
|
|
|
|
|
2020-10-27 19:42:49 +01:00
|
|
|
async checkConnection() {
|
2020-07-16 23:17:02 +02:00
|
|
|
try {
|
2020-10-24 08:13:16 +02:00
|
|
|
const response = await fetch(this.connectEndPoint, {
|
2020-07-16 23:17:02 +02:00
|
|
|
method: "GET",
|
|
|
|
credentials: "include",
|
|
|
|
mode: "cors",
|
|
|
|
headers: {
|
|
|
|
"Accept": "application/json",
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
this._accessToken = data.access_token;
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
catch (error) {
|
2020-10-24 08:13:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-27 19:42:49 +01:00
|
|
|
connect() {
|
2019-06-20 20:03:48 +02:00
|
|
|
return new Promise(resolve => {
|
|
|
|
const joinWindow = new BrowserWindow(this.windowOptions);
|
|
|
|
const url = `https://auth.discordservers.com/connect?scopes=guilds.join&previousUrl=${this.connectEndPoint}`;
|
|
|
|
joinWindow.webContents.on("did-navigate", (event, navUrl) => {
|
|
|
|
if (navUrl != this.connectEndPoint) return;
|
|
|
|
joinWindow.close();
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
joinWindow.loadURL(url);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-27 19:42:49 +01:00
|
|
|
get windowOptions() {
|
2019-06-20 20:03:48 +02:00
|
|
|
return {
|
2020-10-20 05:45:32 +02:00
|
|
|
width: 490,
|
|
|
|
height: 500,
|
2019-06-20 20:03:48 +02:00
|
|
|
backgroundColor: "#282b30",
|
|
|
|
show: true,
|
2019-06-21 06:32:59 +02:00
|
|
|
resizable: true,
|
2019-06-20 20:03:48 +02:00
|
|
|
maximizable: false,
|
|
|
|
minimizable: false,
|
|
|
|
alwaysOnTop: true,
|
|
|
|
frame: false,
|
|
|
|
center: true,
|
|
|
|
webPreferences: {
|
|
|
|
nodeIntegration: false
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2020-10-27 19:42:49 +01:00
|
|
|
};
|