Adding badge support
This commit is contained in:
parent
075b03214e
commit
cdaeaeadcd
|
@ -11,8 +11,9 @@ import DOM from "./domtools";
|
|||
|
||||
import BDLogo from "../ui/bdLogo";
|
||||
import TooltipWrap from "../ui/tooltipWrap";
|
||||
import LightcordLogo from "../ui/lightcordLogo";
|
||||
import LightcordLogo from "../svg/lightcord";
|
||||
import PluginCertifier from "./pluginCertifier";
|
||||
import distant, { uuidv4 } from "./distant";
|
||||
|
||||
function Core() {
|
||||
// Object.assign(bdConfig, __non_webpack_require__(DataStore.configFile));
|
||||
|
@ -232,8 +233,8 @@ Core.prototype.patchSocial = function() {
|
|||
const returnVal = original(...arguments);
|
||||
returnVal.props.children.push(
|
||||
BDV2.React.createElement(TooltipWrap, {color: "black", side: "top", text: "Lightcord"},
|
||||
BDV2.React.createElement(Anchor, {className: "bd-social-link "+socialModule1.link, href: "https://github.com/jeanouina/Lightcord", title: "Lightcord", target: "_blank"},
|
||||
BDV2.React.createElement(LightcordLogo, {size: "17px", className: "bd-social-logo"})
|
||||
BDV2.React.createElement(Anchor, {className: "bd-social-link "+socialModule1.link, href: "https://github.com/Lightcord/Lightcord", title: "Lightcord", target: "_blank"},
|
||||
BDV2.React.createElement(LightcordLogo, {size: "16px", className: "bd-social-logo"})
|
||||
)
|
||||
)
|
||||
);
|
||||
|
@ -345,7 +346,7 @@ Core.prototype.patchMessageHeader = function() {
|
|||
if (!children || !author || !author.id)return
|
||||
// if (header && header.className) header.className += " "
|
||||
if (!Array.isArray(children)) return;
|
||||
if (author.id === "249746236008169473") {
|
||||
if (author.id === "249746236008169473") { // Rauenzi: BandagedBD Developer
|
||||
children.push(
|
||||
BDV2.React.createElement(TooltipWrap, {color: "black", side: "top", text: "BandagedBD Developer"},
|
||||
BDV2.React.createElement(Anchor, {className: "bd-chat-badge", href: "https://github.com/rauenzi/BetterDiscordApp", title: "BandagedBD", target: "_blank"},
|
||||
|
@ -353,18 +354,57 @@ Core.prototype.patchMessageHeader = function() {
|
|||
)
|
||||
)
|
||||
);
|
||||
} else if (author.id === "696481194443014174" || author.id === "696003456611385396"){
|
||||
} else if (author.id === "696481194443014174" || author.id === "696003456611385396"){ // Not Thomiz: Lightcord Developer, Phorcys: Lightcord Developer
|
||||
children.push(
|
||||
BDV2.React.createElement(TooltipWrap, {color: "black", side: "top", text: "Lightcord Developer"},
|
||||
BDV2.React.createElement(Anchor, {className: "bd-chat-badge", href: "https://github.com/jeanouina/Lightcord", title: "Lightcord", target: "_blank"},
|
||||
BDV2.React.createElement(LightcordLogo, {size: "32px", className: "bd-logo"})
|
||||
BDV2.React.createElement(Anchor, {className: "bd-chat-badge", href: "https://github.com/Lightcord/Lightcord", title: "Lightcord", target: "_blank"},
|
||||
BDV2.React.createElement(LightcordLogo, {size: "16px", className: "bd-logo"})
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
const id = uuidv4()
|
||||
children.push(
|
||||
BDV2.React.createElement("div", {
|
||||
id: "badges-"+id,
|
||||
style: {
|
||||
display: "inline"
|
||||
}
|
||||
})
|
||||
)
|
||||
applyBadges(id, author, true)
|
||||
}});
|
||||
};
|
||||
|
||||
function applyBadges(id, user, chat){
|
||||
process.nextTick(() => {
|
||||
const div = document.getElementById("badges-"+id)
|
||||
if(!div || div.childNodes.length > 0)return
|
||||
|
||||
const Anchor = WebpackModules.find(m => m.displayName == "Anchor");
|
||||
|
||||
distant.getBadges(user.id)
|
||||
.then(badges => {
|
||||
badges.forEach(badge => {
|
||||
const props = {size: "16px", className: "bd-logo"}
|
||||
badge.scopes.forEach(scope => {
|
||||
// TODO: implement scope for badges (user, channel, etc)
|
||||
})
|
||||
const element = BDV2.React.createElement(TooltipWrap, {color: "black", side: "top", text: badge.name},
|
||||
BDV2.React.createElement(Anchor, {className: chat ? "bd-chat-badge" : "bd-member-badge", href: badge.href, title: badge.name, target: "_blank"},
|
||||
BDV2.React.createElement(badge.component, props)
|
||||
)
|
||||
)
|
||||
const div2 = document.createElement("div")
|
||||
BDV2.reactDom.render(element, div2)
|
||||
div2.childNodes.forEach(node => {
|
||||
div.appendChild(node)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Core.prototype.patchMemberList = function() {
|
||||
if (this.memberListPatch) return;
|
||||
const MemberListItem = WebpackModules.findByDisplayName("MemberListItem");
|
||||
|
@ -387,12 +427,17 @@ Core.prototype.patchMemberList = function() {
|
|||
} else if (user.id === "696481194443014174" || user.id === "696003456611385396"){
|
||||
children.push(
|
||||
BDV2.React.createElement(TooltipWrap, {color: "black", side: "top", text: "Lightcord Developer"},
|
||||
BDV2.React.createElement(Anchor, {className: "bd-member-badge", href: "https://github.com/jeanouina/Lightcord", title: "Lightcord", target: "_blank"},
|
||||
BDV2.React.createElement(Anchor, {className: "bd-member-badge", href: "https://github.com/Lightcord/Lightcord", title: "Lightcord", target: "_blank"},
|
||||
BDV2.React.createElement(LightcordLogo, {size: "32px", className: "bd-logo"})
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
const id = uuidv4()
|
||||
children.push(
|
||||
BDV2.React.createElement("div", {id: "badges-"+id})
|
||||
)
|
||||
applyBadges(id, user, false)
|
||||
}});
|
||||
};
|
||||
|
||||
|
|
|
@ -0,0 +1,106 @@
|
|||
import BugHunterBadge from "../svg/bug_hunter"
|
||||
|
||||
export function uuidv4() { // Generate UUID (No crypto rng)
|
||||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
||||
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
|
||||
return v.toString(16);
|
||||
});
|
||||
}
|
||||
|
||||
const awaitingBadgesPromises = {}
|
||||
|
||||
export default new class DistantServer {
|
||||
constructor(){
|
||||
this._cache = {
|
||||
badges: {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
get cache(){
|
||||
return this._cache
|
||||
}
|
||||
|
||||
set cache(data){
|
||||
if(typeof data !== "object" || typeof this._cache !== "object")return this._cache = data
|
||||
return this._cache = Object.assign(this._cache, data)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get custom badges from the user ID.
|
||||
* @param {string} user The user ID
|
||||
* @returns {Promise<Constants["badges"]>}
|
||||
*/
|
||||
async getBadges(user){
|
||||
if(this.cache.badges[user])return this.cache.badges[user]
|
||||
if(awaitingBadgesPromises[user])return awaitingBadgesPromises[user]
|
||||
|
||||
let resolve
|
||||
const newPromise = new Promise((res) => (resolve = res))
|
||||
awaitingBadgesPromises[user] = newPromise
|
||||
|
||||
const badges = []
|
||||
for(let badge of Constants.badges){
|
||||
if(badge.defaultUsers.includes(user))badges.push(badge)
|
||||
}
|
||||
const fetchedBadges = await new Promise((resolve) => {
|
||||
handleRequest(Routes.badges(user), "GET")
|
||||
.then(async res => {
|
||||
if(res.status !== 200){// Couldn't fetch badges: server error
|
||||
return resolve([]) // no badge fetched
|
||||
}
|
||||
return resolve(await res.json())
|
||||
}).catch(() => {// Couldn't fetch badges: error
|
||||
return resolve([]) // no badge fetched
|
||||
})
|
||||
})
|
||||
for(let badge of fetchedBadges){
|
||||
if(!Constants.badges.find(e => e.id === badge))continue // We do not have the Component, skip it.
|
||||
if(badges.find(e => e.id === badge))continue // Already inserted.
|
||||
badges.push(Constants.badges.find(e => e.id === badge))
|
||||
}
|
||||
this.cache = {
|
||||
badges: Object.assign(this.cache.badges, {[user]: badges})
|
||||
}
|
||||
setTimeout(() => {
|
||||
delete this.cache.badges[user]
|
||||
}, 600000);
|
||||
|
||||
resolve(badges)
|
||||
delete awaitingBadgesPromises[user]
|
||||
return badges
|
||||
}
|
||||
}
|
||||
|
||||
const handleRequest = function(route, method){
|
||||
return fetch(`${Constants.SERVER_URL}/api/v1${route}`, {
|
||||
method,
|
||||
headers: {
|
||||
"CLIENT": "Lightcord"
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export const Constants = {
|
||||
SERVER_URL: "http://127.0.0.1",
|
||||
badges: [
|
||||
{
|
||||
name: "Bug Hunter",
|
||||
id: "f04698f5-816b-41e3-bd01-92291193d7a5",
|
||||
defaultUsers: [
|
||||
"696481194443014174",
|
||||
"585858920149549067"
|
||||
],
|
||||
scopes: [],
|
||||
component: BugHunterBadge,
|
||||
href: "https://github.com/lightcord/lightcord/wiki/badges/bug_hunter"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
export const Routes = {
|
||||
badges(user){
|
||||
return `/${user}/badges`
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue