Split method

This commit is contained in:
RMidhunSuresh 2022-10-11 18:05:54 +05:30
parent a448c0218d
commit dcba6d1500
No known key found for this signature in database

View File

@ -137,7 +137,7 @@ export class Client {
async startRegistration(homeserver, username, password, initialDeviceDisplayName, flowSelector) { async startRegistration(homeserver, username, password, initialDeviceDisplayName, flowSelector) {
const request = this._platform.request; const request = this._platform.request;
const hsApi = new HomeServerApi({homeserver, request}); const hsApi = new HomeServerApi({homeserver, request});
const registration = new Registration(hsApi, { const registration = new Registration(homeserver, hsApi, {
username, username,
password, password,
initialDeviceDisplayName, initialDeviceDisplayName,
@ -146,6 +146,21 @@ export class Client {
return registration; return registration;
} }
async startWithFinishedRegistration(registration) {
this._platform.logger.run("startWithFinishedRegistration", async (log) => {
const sessionInfo = registration.sessionInfo;
if (!sessionInfo) {
throw new Error("Registration.sessionInfo is not available; are you sure that registration is finished?");
}
await this.startWithSessionInfo({
accessToken: sessionInfo.access_token,
deviceId: sessionInfo.device_id,
userId: sessionInfo.user_id,
homeserver: registration.homeserver,
}, true, log);
});
}
async startWithLogin(loginMethod, {inspectAccountSetup} = {}) { async startWithLogin(loginMethod, {inspectAccountSetup} = {}) {
const currentStatus = this._status.get(); const currentStatus = this._status.get();
if (currentStatus !== LoadStatus.LoginFailed && if (currentStatus !== LoadStatus.LoginFailed &&
@ -156,23 +171,17 @@ export class Client {
this._resetStatus(); this._resetStatus();
await this._platform.logger.run("login", async log => { await this._platform.logger.run("login", async log => {
this._status.set(LoadStatus.Login); this._status.set(LoadStatus.Login);
const clock = this._platform.clock;
let sessionInfo; let sessionInfo;
try { try {
const request = this._platform.request; const request = this._platform.request;
const hsApi = new HomeServerApi({homeserver: loginMethod.homeserver, request}); const hsApi = new HomeServerApi({homeserver: loginMethod.homeserver, request});
const loginData = await loginMethod.login(hsApi, "Hydrogen", log); const loginData = await loginMethod.login(hsApi, "Hydrogen", log);
const sessionId = this.createNewSessionId();
sessionInfo = { sessionInfo = {
id: sessionId,
deviceId: loginData.device_id, deviceId: loginData.device_id,
userId: loginData.user_id, userId: loginData.user_id,
homeServer: loginMethod.homeserver, // deprecate this over time
homeserver: loginMethod.homeserver, homeserver: loginMethod.homeserver,
accessToken: loginData.access_token, accessToken: loginData.access_token,
lastUsed: clock.now()
}; };
log.set("id", sessionId);
} catch (err) { } catch (err) {
this._error = err; this._error = err;
if (err.name === "HomeServerError") { if (err.name === "HomeServerError") {
@ -191,9 +200,26 @@ export class Client {
} }
return; return;
} }
await this.startWithSessionInfo(sessionInfo, inspectAccountSetup, log);
});
}
async startWithSessionInfo({deviceId, userId, accessToken, homeserver}, inspectAccountSetup, log) {
await log.wrap("startWithSessionInfo", async (l) => {
const id = this.createNewSessionId();
const lastUsed = this._platform.clock.now();
const sessionInfo = {
id,
deviceId,
userId,
homeServer: homeserver, // deprecate this over time
homeserver,
accessToken,
lastUsed,
};
let dehydratedDevice; let dehydratedDevice;
if (inspectAccountSetup) { if (inspectAccountSetup) {
dehydratedDevice = await this._inspectAccountAfterLogin(sessionInfo, log); dehydratedDevice = await this._inspectAccountAfterLogin(sessionInfo, l);
if (dehydratedDevice) { if (dehydratedDevice) {
sessionInfo.deviceId = dehydratedDevice.deviceId; sessionInfo.deviceId = dehydratedDevice.deviceId;
} }
@ -203,10 +229,10 @@ export class Client {
// LoadStatus.Error in case of an error, // LoadStatus.Error in case of an error,
// so separate try/catch // so separate try/catch
try { try {
await this._loadSessionInfo(sessionInfo, dehydratedDevice, log); await this._loadSessionInfo(sessionInfo, dehydratedDevice, l);
log.set("status", this._status.get()); l.set("status", this._status.get());
} catch (err) { } catch (err) {
log.catch(err); l.catch(err);
// free olm Account that might be contained // free olm Account that might be contained
dehydratedDevice?.dispose(); dehydratedDevice?.dispose();
this._error = err; this._error = err;