Skip to content

Commit 83a4c45

Browse files
authored
Merge pull request #194 from MathMan05/newerEmmaFixes
Newer emma fixes
2 parents 6910a5b + b2cbdf9 commit 83a4c45

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+8315
-7902
lines changed

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ const app = http.createServer(async (req, res) => {
130130
sendFile(filePath);
131131
});
132132

133-
export type instace = {
133+
export type instance = {
134134
name: string;
135135
description?: string;
136136
descriptionLong?: string;
@@ -157,7 +157,7 @@ export type instace = {
157157
};
158158
const instances = JSON.parse(
159159
readFileSync(process.env.JANK_INSTANCES_PATH || __dirname + "/webpage/instances.json").toString(),
160-
) as instace[];
160+
) as instance[];
161161

162162
const instanceNames = new Map<string, Instance>();
163163

src/utils.ts

Lines changed: 77 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import {instace} from "./index.js";
1+
import {instance} from "./index.js";
2+
23
interface ApiUrls {
34
api: string;
45
gateway: string;
@@ -8,16 +9,17 @@ interface ApiUrls {
89

910
export async function getApiUrls(
1011
url: string,
11-
instances: instace[],
12+
instances: instance[],
1213
check = true,
1314
): Promise<ApiUrls | null> {
1415
if (!url.endsWith("/")) {
1516
url += "/";
1617
}
18+
1719
if (check) {
1820
let valid = false;
19-
for (const instace of instances) {
20-
const urlstr = instace.url || instace.urls?.api;
21+
for (const instance of instances) {
22+
const urlstr = instance.url || instance.urls?.api;
2123
if (!urlstr) {
2224
continue;
2325
}
@@ -34,21 +36,77 @@ export async function getApiUrls(
3436
throw new Error("Invalid instance");
3537
}
3638
}
39+
40+
const hostName = new URL(url).hostname;
3741
try {
38-
const info: ApiUrls = await fetch(`${url}.well-known/spacebar`).then((res) => res.json());
39-
const api = info.api;
40-
const apiUrl = new URL(api);
41-
const policies: any = await fetch(
42-
`${api}${apiUrl.pathname.includes("api") ? "" : "api"}/policies/instance/domains`,
43-
).then((res) => res.json());
44-
return {
45-
api: policies.apiEndpoint,
46-
gateway: policies.gateway,
47-
cdn: policies.cdn,
48-
wellknown: url,
49-
};
50-
} catch (error) {
51-
console.error("Error fetching API URLs:", error);
52-
return null;
42+
return await getApiUrlsV2(url);
43+
} catch (e) {
44+
console.warn(
45+
`[WARN] Failed to get V2 API URLs for ${hostName}, trying V1...`,
46+
(e as Error).message,
47+
);
48+
try {
49+
return await getApiUrlsV1(url);
50+
} catch (e) {
51+
console.error(`[ERROR] Failed to get V1 API URLs for ${hostName}:`, (e as Error).message);
52+
throw e;
53+
}
5354
}
5455
}
56+
57+
//region Well-Known V1 Interfaces
58+
59+
interface WellKnownV1 {
60+
api: string;
61+
}
62+
63+
export async function getApiUrlsV1(url: string): Promise<ApiUrls | null> {
64+
const info: WellKnownV1 = await fetch(`${url}.well-known/spacebar`).then((res) => res.json());
65+
const api = info.api;
66+
const apiUrl = new URL(api);
67+
const policies: any = await fetch(
68+
`${api}${apiUrl.pathname.includes("api") ? "" : "api"}/policies/instance/domains`,
69+
).then((res) => res.json());
70+
return {
71+
api: policies.apiEndpoint,
72+
gateway: policies.gateway,
73+
cdn: policies.cdn,
74+
wellknown: url,
75+
};
76+
}
77+
//endregion
78+
79+
//region Well-Known V2 Interfaces
80+
interface WellKnownV2BasicEndpoint {
81+
baseUrl: string;
82+
}
83+
84+
interface WellKnownV2ApiVersions {
85+
default: string;
86+
active: string[];
87+
}
88+
89+
interface WellKnownV2GatewayOptions {
90+
encoding: ("json" | "etf")[];
91+
compression: ("zlib-stream" | "zstd-stream" | null)[];
92+
}
93+
94+
interface WellKnownV2 {
95+
admin?: WellKnownV2BasicEndpoint;
96+
api: WellKnownV2BasicEndpoint & {apiVersions: WellKnownV2ApiVersions};
97+
cdn: WellKnownV2BasicEndpoint;
98+
gateway: WellKnownV2BasicEndpoint & WellKnownV2GatewayOptions;
99+
}
100+
101+
export async function getApiUrlsV2(url: string): Promise<ApiUrls | null> {
102+
const info: WellKnownV2 = await fetch(`${url}.well-known/spacebar/client`).then((res) =>
103+
res.json(),
104+
);
105+
return {
106+
api: info.api.baseUrl + "/api/v" + info.api.apiVersions.default,
107+
gateway: info.gateway.baseUrl,
108+
cdn: info.cdn.baseUrl,
109+
wellknown: url,
110+
};
111+
}
112+
//endregion

src/webpage/404.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {I18n} from "./i18n";
22
import {setTheme, SW} from "./utils/utils";
33

4-
setTheme();
4+
await setTheme();
55
await I18n.done;
66
I18n.translatePage();
77

src/webpage/audio/page.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {BinWrite} from "../utils/binaryUtils.js";
22
import {setTheme} from "../utils/utils.js";
33
import {Play} from "./play.js";
44

5-
setTheme();
5+
await setTheme();
66
const w = new BinWrite(2 ** 12);
77
w.writeStringNo("jasf");
88
w.write8(4);

src/webpage/channel.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class Channel extends SnowFlake {
9999
this.createInvite();
100100
},
101101
{
102-
visable: function () {
102+
visible: function () {
103103
return this.hasPermission("CREATE_INSTANT_INVITE") && this.type !== 4;
104104
},
105105
color: "blue",
@@ -119,7 +119,7 @@ class Channel extends SnowFlake {
119119
this.muteChannel();
120120
},
121121
{
122-
visable: function () {
122+
visible: function () {
123123
return !this.muted && this.type !== 4;
124124
},
125125
},
@@ -130,7 +130,7 @@ class Channel extends SnowFlake {
130130
this.unmuteChannel();
131131
},
132132
{
133-
visable: function () {
133+
visible: function () {
134134
return this.muted;
135135
},
136136
},
@@ -142,7 +142,7 @@ class Channel extends SnowFlake {
142142
this.generateSettings();
143143
},
144144
{
145-
visable: function () {
145+
visible: function () {
146146
return this.hasPermission("MANAGE_CHANNELS");
147147
},
148148
icon: {
@@ -162,7 +162,7 @@ class Channel extends SnowFlake {
162162
this.deleteChannel();
163163
},
164164
{
165-
visable: function () {
165+
visible: function () {
166166
return this.hasPermission("MANAGE_CHANNELS");
167167
},
168168
icon: {
@@ -741,7 +741,7 @@ class Channel extends SnowFlake {
741741
}
742742
static dragged: [Channel, HTMLDivElement] | [] = [];
743743
html: WeakRef<HTMLElement> | undefined;
744-
get visable() {
744+
get visible() {
745745
return this.hasPermission("VIEW_CHANNEL");
746746
}
747747
voiceUsers = new WeakRef(document.createElement("div"));
@@ -807,10 +807,10 @@ class Channel extends SnowFlake {
807807
);
808808
}
809809
this.html = new WeakRef(div);
810-
if (!this.visable) {
810+
if (!this.visible) {
811811
let quit = true;
812812
for (const thing of this.children) {
813-
if (thing.visable) {
813+
if (thing.visible) {
814814
quit = false;
815815
}
816816
}
@@ -1706,7 +1706,7 @@ class Channel extends SnowFlake {
17061706
command.render(typebox, this);
17071707
}
17081708
async getHTML(addstate = true, getMessages: boolean | void = undefined, aroundMessage?: string) {
1709-
if (!this.visable) {
1709+
if (!this.visible) {
17101710
this.guild.loadChannel();
17111711
return;
17121712
}

src/webpage/contextmenu.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class ContextButton<x, y> implements menuPart<x, y> {
2525
private text: string | ((this: x, arg: y) => string);
2626
private onClick: (this: x, arg: y, e: MouseEvent) => void;
2727
private icon?: iconJson;
28-
private visable?: (this: x, arg: y) => boolean;
28+
private visible?: (this: x, arg: y) => boolean;
2929
private enabled?: (this: x, arg: y) => boolean;
3030
//TODO there *will* be more colors
3131
private color?: "red" | "blue";
@@ -35,7 +35,7 @@ class ContextButton<x, y> implements menuPart<x, y> {
3535
onClick: ContextButton<x, y>["onClick"],
3636
addProps: {
3737
icon?: iconJson;
38-
visable?: (this: x, arg: y) => boolean;
38+
visible?: (this: x, arg: y) => boolean;
3939
enabled?: (this: x, arg: y) => boolean;
4040
color?: "red" | "blue";
4141
group?: string;
@@ -44,17 +44,17 @@ class ContextButton<x, y> implements menuPart<x, y> {
4444
this.text = text;
4545
this.onClick = onClick;
4646
this.icon = addProps.icon;
47-
this.visable = addProps.visable;
47+
this.visible = addProps.visible;
4848
this.enabled = addProps.enabled;
4949
this.color = addProps.color;
5050
this.group = addProps.group;
5151
}
52-
isVisable(obj1: x, obj2: y): boolean {
53-
if (!this.visable) return true;
54-
return this.visable.call(obj1, obj2);
52+
isVisible(obj1: x, obj2: y): boolean {
53+
if (!this.visible) return true;
54+
return this.visible.call(obj1, obj2);
5555
}
5656
makeContextHTML(obj1: x, obj2: y, menu: HTMLDivElement) {
57-
if (!this.isVisable(obj1, obj2)) {
57+
if (!this.isVisible(obj1, obj2)) {
5858
return;
5959
}
6060

@@ -113,22 +113,22 @@ class ContextButton<x, y> implements menuPart<x, y> {
113113
}
114114
}
115115
class ContextGroup<x, y> implements menuPart<x, y> {
116-
private visable?: (this: x, arg: y) => boolean;
116+
private visible?: (this: x, arg: y) => boolean;
117117
groupSel: string;
118118
group = undefined;
119119
constructor(
120120
group: string,
121121
addProps: {
122-
visable?: (this: x, arg: y) => boolean;
122+
visible?: (this: x, arg: y) => boolean;
123123
} = {},
124124
) {
125-
this.visable = addProps.visable;
125+
this.visible = addProps.visible;
126126

127127
this.groupSel = group;
128128
}
129-
isVisable(obj1: x, obj2: y): boolean {
130-
if (!this.visable) return true;
131-
return this.visable.call(obj1, obj2);
129+
isVisible(obj1: x, obj2: y): boolean {
130+
if (!this.visible) return true;
131+
return this.visible.call(obj1, obj2);
132132
}
133133
makeContextHTML(
134134
x: x,
@@ -137,7 +137,7 @@ class ContextGroup<x, y> implements menuPart<x, y> {
137137
layered: contextCluster<unknown, unknown>[],
138138
processed: WeakSet<menuPart<unknown, unknown>>,
139139
) {
140-
if (!this.isVisable(x, y)) {
140+
if (!this.isVisible(x, y)) {
141141
return;
142142
}
143143
for (const [menu, x, y] of layered) {
@@ -151,14 +151,14 @@ class ContextGroup<x, y> implements menuPart<x, y> {
151151
}
152152
}
153153
class Seperator<x, y> implements menuPart<x, y> {
154-
private visable?: (obj1: x, obj2: y) => boolean;
154+
private visible?: (obj1: x, obj2: y) => boolean;
155155
group?: string;
156-
constructor(visable?: (obj1: x, obj2: y) => boolean, group?: string) {
157-
this.visable = visable;
156+
constructor(visible?: (obj1: x, obj2: y) => boolean, group?: string) {
157+
this.visible = visible;
158158
this.group = group;
159159
}
160160
makeContextHTML(obj1: x, obj2: y, menu: HTMLDivElement): void {
161-
if (!this.visable || this.visable(obj1, obj2)) {
161+
if (!this.visible || this.visible(obj1, obj2)) {
162162
if (menu.children[menu.children.length - 1].tagName === "HR") {
163163
return;
164164
}
@@ -236,21 +236,21 @@ class Contextmenu<x, y> {
236236
onClick: ContextButton<x, y>["onClick"],
237237
addProps: {
238238
icon?: iconJson;
239-
visable?: (this: x, arg: y) => boolean;
239+
visible?: (this: x, arg: y) => boolean;
240240
enabled?: (this: x, arg: y) => boolean;
241241
color?: "red" | "blue";
242242
group?: string;
243243
} = {},
244244
) {
245245
this.buttons.push(new ContextButton(text, onClick, addProps));
246246
}
247-
addSeperator(visable?: (obj1: x, obj2: y) => boolean, group?: string) {
248-
this.buttons.push(new Seperator(visable, group));
247+
addSeperator(visible?: (obj1: x, obj2: y) => boolean, group?: string) {
248+
this.buttons.push(new Seperator(visible, group));
249249
}
250250
addGroup(
251251
group: string,
252252
addprops?: {
253-
visable?: (this: x, arg: y) => boolean;
253+
visible?: (this: x, arg: y) => boolean;
254254
},
255255
) {
256256
this.buttons.push(new ContextGroup<x, y>(group, addprops));

src/webpage/direct.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ class Group extends Channel {
410410
},
411411
{
412412
group: "default",
413-
visable: function (user) {
413+
visible: function (user) {
414414
return this.localuser.user.id !== user.id && this.owner_id === this.localuser.user.id;
415415
},
416416
color: "red",
@@ -435,7 +435,7 @@ class Group extends Channel {
435435
this.edit();
436436
},
437437
{
438-
visable: function () {
438+
visible: function () {
439439
return this.type !== 1;
440440
},
441441
},
@@ -466,7 +466,7 @@ class Group extends Channel {
466466
navigator.clipboard.writeText(this.users[0].id);
467467
},
468468
{
469-
visable: function () {
469+
visible: function () {
470470
return this.type === 1;
471471
},
472472
},
@@ -576,7 +576,6 @@ class Group extends Channel {
576576
} else if (this.lastmessage) {
577577
this.position = this.lastmessage.getTimeStamp();
578578
} else if (this.lastmessageid) {
579-
console.log(this.lastmessageid);
580579
this.position = SnowFlake.stringToUnixTime(this.lastmessageid);
581580
} else {
582581
this.position = 0;

0 commit comments

Comments
 (0)