Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
pretty pretty code
Browse files Browse the repository at this point in the history
  • Loading branch information
farooqkz committed Feb 14, 2024
1 parent a9f46f9 commit 0d57f37
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 53 deletions.
2 changes: 1 addition & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class App extends Component<{}, AppState> {
}
});
localforage.getItem("well_known").then((well_known: unknown) => {
this.wellKnown = well_known as WellKnown;
this.wellKnown = well_known as WellKnown;
});
localforage.getItem("guide").then((value: unknown) => {
this.setState({ guide: Boolean(value) });
Expand Down
43 changes: 27 additions & 16 deletions src/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +57,36 @@ class Login extends Component<{}, LoginState> {
rightCb = () => {
switch (this.state.stage) {
case 0:
this.loginHandler.findHomeserver(this.homeserverName).then( () => {
this.setState({ cursor: 0, stage: 1})
}).catch((e: any) => {
window.alert("Could not connect to homeserver");
console.log(e);
});
this.loginHandler
.findHomeserver(this.homeserverName)
.then(() => {
this.setState({ cursor: 0, stage: 1 });
})
.catch((e: any) => {
window.alert("Could not connect to homeserver");
console.log(e);
});
break;
case 1:
this.selectedLoginFlow = this.loginHandler.loginFlows[this.state.cursor];
this.selectedLoginFlow =
this.loginHandler.loginFlows[this.state.cursor];
if (this.selectedLoginFlow.type !== "m.login.password") {
window.alert("The selected login method is not implemented, yet.");
} else {
this.setState({ cursor: 0, stage: 2 });
}
break;
case 2:
let loginData = {'username': this.username, 'password': this.password};
let loginData = { username: this.username, password: this.password };
if (this.selectedLoginFlow !== undefined) {
this.loginHandler.doLogin(this.selectedLoginFlow, loginData).then(() => {
window.location = window.location;
}).catch((e) => alert(e.message));
this.loginHandler
.doLogin(this.selectedLoginFlow, loginData)
.then(() => {
window.location = window.location;
})
.catch((e) => alert(e.message));
} else {
throw new Error("Undefined selectedLoginFlow")
throw new Error("Undefined selectedLoginFlow");
}
break;
default:
Expand All @@ -100,7 +107,7 @@ class Login extends Component<{}, LoginState> {
this.username = "";
this.password = "";
this.stageNames = ["Login Info", "Login method", "Login"];
this.loginHandler = new LoginHandler()
this.loginHandler = new LoginHandler();
this.state = {
stage: 0,
cursor: 0,
Expand Down Expand Up @@ -155,9 +162,13 @@ class Login extends Component<{}, LoginState> {
});
break;
case 1:
listViewChildren = this.loginHandler.loginFlows.map((flow: LoginFlow) => {
return <TextListItem key={"flow" + flow.type} primary={flow.type} />;
});
listViewChildren = this.loginHandler.loginFlows.map(
(flow: LoginFlow) => {
return (
<TextListItem key={"flow" + flow.type} primary={flow.type} />
);
}
);
break;
case 2:
if (!this.selectedLoginFlow) {
Expand Down
35 changes: 19 additions & 16 deletions src/LoginHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import * as localforage from "localforage";
import { createClient } from "matrix-js-sdk";
import shared from "./shared";


export type LoginData = {
username: string,
password: string,
username: string;
password: string;
};

export default class LoginHandler {
Expand Down Expand Up @@ -41,16 +40,20 @@ export default class LoginHandler {
switch (loginFlow.type) {
case "m.login.password":
let password: string = loginData.password;
loginResult = await shared.mClient
.loginWithPassword(username, password);
loginResult = await shared.mClient.loginWithPassword(
username,
password
);
break;
default:
throw new Error("Unsupported");
}
if (loginResult.well_known) {
this.setWellKnown(loginResult.well_known)
console.log("Received a well_known from client login property. Updating previous settings.")
console.log(loginResult.well_known)
this.setWellKnown(loginResult.well_known);
console.log(
"Received a well_known from client login property. Updating previous settings."
);
console.log(loginResult.well_known);
}
await localforage.setItem("login", loginResult);
alert("Logged in as " + username);
Expand All @@ -69,15 +72,15 @@ export default class LoginHandler {
break;
default:
if (e.message === "Unsupported") {
message = "Login flow selected is unsupported"
message = "Login flow selected is unsupported";
} else if (e.errcode) {
message = e.errcode;
} else {
message = `Login failed for some unknown reason: ${e.message}`;
}
break;
}
throw new Error(message)
throw new Error(message);
}
}

Expand All @@ -87,7 +90,7 @@ export default class LoginHandler {
this.homeserverName = name;
let baseUrl: string = "";
let wellKnownUrl: string = `https://${name}/.well-known/matrix/client`;
try {
try {
let r: Response = await fetch(wellKnownUrl);
if (!r.ok) {
throw new Error("404");
Expand All @@ -104,20 +107,20 @@ export default class LoginHandler {
baseUrl: baseUrl,
fetchFn: customFetch,
});
let result = await shared.mClient.loginFlows()
let result = await shared.mClient.loginFlows();
if (!result.flows) {
throw new Error("Got no flows");
}
this.loginFlows = result.flows;
} catch (e) {
alert(`No server found at ${baseUrl}`)
alert(`No server found at ${baseUrl}`);
console.error(e);
}
this.setWellKnown({
"m.homeserver": {"base_url": baseUrl},
"m.identity_server": {"base_url": "https://vector.im"},
"m.homeserver": { base_url: baseUrl },
"m.identity_server": { base_url: "https://vector.im" },
// TODO Where to infer this outside of actual .well-known?
})
});
}
}
}
24 changes: 15 additions & 9 deletions src/LoginWithQR/LoginWithQR.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,29 @@ class LoginWithQR extends Component<LoginWithQRProps, null> {
};

private readonly loginFlowsShort: Record<string, string> = {
"PASS": "m.login.password"
}
PASS: "m.login.password",
};

private async doLogin (data: string) {
private async doLogin(data: string) {
let decodedParts: string[] = data.split(" ", 4);
let flow = decodedParts[0];
const serverName = decodedParts[1];
const username = decodedParts[2];
const start = flow.length + serverName.length + username.length + 3;
let password: string = data.substring(start);
// TODO implement more flows
if (window.confirm(
`Do you confirm? Flow: ${flow} | Server name: ${serverName} | Username: ${username}`)) {
if (
window.confirm(
`Do you confirm? Flow: ${flow} | Server name: ${serverName} | Username: ${username}`
)
) {
// users can either write the full m.login.password (or whatever other flow) or use a shorthand
// This maps the shorthand to the actual flow identificator
if (!flow.startsWith("m.login")) {
flow = this.loginFlowsShort[flow];
}
if (flow !== "m.login.password") {
alert("Password authentication is the only supported flow currently")
alert("Password authentication is the only supported flow currently");
return;
}
try {
Expand All @@ -61,15 +64,18 @@ class LoginWithQR extends Component<LoginWithQRProps, null> {
}
}
if (selectedFlow !== undefined) {
const loginData: LoginData = {username: username, password: password};
await this.loginHandler.doLogin(selectedFlow, loginData);
const loginData: LoginData = {
username: username,
password: password,
};
await this.loginHandler.doLogin(selectedFlow, loginData);
window.location = window.location; // restart the app
}
} catch (e) {
alert(e);
}
}
};
}

constructor(props: any) {
super(props);
Expand Down
11 changes: 7 additions & 4 deletions src/RoomView/RoomView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class RoomView extends Component<RoomViewProps, RoomViewState> {
}

if (this.state.imageViewer) {
this.handleImageViewerEvents(evt);
this.handleImageViewerEvents(evt);
return;
}
const lastEventIndex = this.getVisibleEvents().length - 1;
Expand All @@ -146,7 +146,9 @@ class RoomView extends Component<RoomViewProps, RoomViewState> {
if (cursor === lastEventIndex) {
this.setState({ textInputFocus: true });
} else {
this.setState({ cursor: Math.min(this.getVisibleEvents().length - 1, cursor + 1) });
this.setState({
cursor: Math.min(this.getVisibleEvents().length - 1, cursor + 1),
});
}
} else if (evt.key === "ArrowUp") {
if (textInputFocus) {
Expand All @@ -156,7 +158,7 @@ class RoomView extends Component<RoomViewProps, RoomViewState> {
}
}
};

handleImageViewerEvents = (evt: KeyboardEvent) => {
if (!this.imageViewer) {
alert("Image viewer is null! This is a bug. please report it :)");
Expand Down Expand Up @@ -523,7 +525,8 @@ class RoomView extends Component<RoomViewProps, RoomViewState> {
return (
<>
{imageViewer &&
this.currentEvent?.getContent().msgtype === "m.image" && this.currentEvent?.getContent().url ? (
this.currentEvent?.getContent().msgtype === "m.image" &&
this.currentEvent?.getContent().url ? (
<ImageViewer
ref={(ref) => {
this.imageViewer = ref;
Expand Down
6 changes: 0 additions & 6 deletions src/Settings.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@



interface Preferences {
adShowFreq: "startup" | number;
serversForPublicDirectory: string[];
mediaLoading: "always" | "only_on_wifi" | "only_on_request";
inviteAutoAccept: boolean;
}




function Settings() {
return (
<p>
Expand Down
2 changes: 1 addition & 1 deletion src/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ declare global {
}
interface Window {
MozActivity: MozActivity;
getKaiAd?: KaiAdFunc
getKaiAd?: KaiAdFunc;
}
interface VolumeManager {
requestShow: () => void;
Expand Down

0 comments on commit 0d57f37

Please sign in to comment.