2019-02-10 21:33:18 +01:00
|
|
|
import HomeServerApi from "./matrix/hs-api.js";
|
|
|
|
import Session from "./matrix/session.js";
|
|
|
|
import createIdbStorage from "./matrix/storage/idb/create.js";
|
|
|
|
import Sync from "./matrix/sync.js";
|
2019-02-27 22:50:08 +01:00
|
|
|
import SessionView from "./ui/web/SessionView.js";
|
2019-03-08 19:58:54 +01:00
|
|
|
import SessionViewModel from "./domain/session/SessionViewModel.js";
|
2019-02-07 00:50:23 +00:00
|
|
|
|
|
|
|
const HOST = "localhost";
|
|
|
|
const HOMESERVER = `http://${HOST}:8008`;
|
|
|
|
const USERNAME = "bruno1";
|
|
|
|
const USER_ID = `@${USERNAME}:${HOST}`;
|
|
|
|
const PASSWORD = "testtest";
|
2019-02-04 22:31:08 +00:00
|
|
|
|
2019-03-08 20:00:37 +01:00
|
|
|
function getSessionInfo(userId) {
|
2019-02-06 23:19:14 +00:00
|
|
|
const sessionsJson = localStorage.getItem("morpheus_sessions_v1");
|
|
|
|
if (sessionsJson) {
|
|
|
|
const sessions = JSON.parse(sessionsJson);
|
|
|
|
const session = sessions.find(session => session.userId === userId);
|
|
|
|
if (session) {
|
2019-03-08 20:00:37 +01:00
|
|
|
return session;
|
2019-02-06 23:19:14 +00:00
|
|
|
}
|
2019-02-04 22:31:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-08 20:00:37 +01:00
|
|
|
function storeSessionInfo(loginData) {
|
|
|
|
const sessionsJson = localStorage.getItem("morpheus_sessions_v1");
|
|
|
|
const sessions = sessionsJson ? JSON.parse(sessionsJson) : [];
|
|
|
|
const sessionId = (Math.floor(Math.random() * Number.MAX_SAFE_INTEGER)).toString();
|
|
|
|
const sessionInfo = {
|
|
|
|
id: sessionId,
|
|
|
|
deviceId: loginData.device_id,
|
|
|
|
userId: loginData.user_id,
|
|
|
|
homeServer: loginData.home_server,
|
|
|
|
accessToken: loginData.access_token,
|
|
|
|
};
|
|
|
|
sessions.push(sessionInfo);
|
|
|
|
localStorage.setItem("morpheus_sessions_v1", JSON.stringify(sessions));
|
|
|
|
return sessionInfo;
|
|
|
|
}
|
|
|
|
|
2019-02-06 23:19:14 +00:00
|
|
|
async function login(username, password, homeserver) {
|
2019-02-07 00:25:12 +00:00
|
|
|
const hsApi = new HomeServerApi(homeserver);
|
|
|
|
const loginData = await hsApi.passwordLogin(username, password).response();
|
2019-03-08 20:00:37 +01:00
|
|
|
return storeSessionInfo(loginData);
|
2019-02-06 23:19:14 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 22:50:08 +01:00
|
|
|
function showSession(container, session) {
|
|
|
|
const vm = new SessionViewModel(session);
|
|
|
|
const view = new SessionView(vm);
|
2019-03-08 20:00:37 +01:00
|
|
|
view.mount();
|
|
|
|
container.appendChild(view.root());
|
2019-02-26 22:45:58 +01:00
|
|
|
}
|
|
|
|
|
2019-02-10 21:25:29 +01:00
|
|
|
// eslint-disable-next-line no-unused-vars
|
2019-02-26 22:45:58 +01:00
|
|
|
export default async function main(label, button, container) {
|
2019-02-10 21:25:29 +01:00
|
|
|
try {
|
2019-03-08 20:00:37 +01:00
|
|
|
let sessionInfo = getSessionInfo(USER_ID);
|
|
|
|
if (!sessionInfo) {
|
|
|
|
sessionInfo = await login(USERNAME, PASSWORD, HOMESERVER);
|
2019-02-10 21:25:29 +01:00
|
|
|
}
|
2019-03-08 20:00:37 +01:00
|
|
|
const storage = await createIdbStorage(`morpheus_session_${sessionInfo.id}`);
|
|
|
|
const hsApi = new HomeServerApi(HOMESERVER, sessionInfo.accessToken);
|
|
|
|
const session = new Session({storage, hsApi, sessionInfo: {
|
|
|
|
deviceId: sessionInfo.deviceId,
|
|
|
|
userId: sessionInfo.userId,
|
|
|
|
homeServer: sessionInfo.homeServer, //only pass relevant fields to Session
|
|
|
|
}});
|
|
|
|
await session.load();
|
2019-02-27 23:38:03 +01:00
|
|
|
console.log("session loaded");
|
|
|
|
const needsInitialSync = !session.syncToken;
|
|
|
|
if (needsInitialSync) {
|
|
|
|
console.log("session needs initial sync");
|
|
|
|
} else {
|
|
|
|
showSession(container, session);
|
|
|
|
}
|
2019-02-10 21:25:29 +01:00
|
|
|
const sync = new Sync(hsApi, session, storage);
|
|
|
|
await sync.start();
|
2019-02-27 23:38:03 +01:00
|
|
|
if (needsInitialSync) {
|
|
|
|
showSession(container, session);
|
|
|
|
}
|
2019-02-10 21:25:29 +01:00
|
|
|
label.innerText = "sync running";
|
|
|
|
button.addEventListener("click", () => sync.stop());
|
|
|
|
sync.on("error", err => {
|
|
|
|
label.innerText = "sync error";
|
|
|
|
console.error("sync error", err);
|
|
|
|
});
|
|
|
|
sync.on("stopped", () => {
|
|
|
|
label.innerText = "sync stopped";
|
|
|
|
});
|
|
|
|
} catch(err) {
|
|
|
|
console.error(err);
|
2019-02-07 00:50:47 +00:00
|
|
|
}
|
2019-02-04 22:31:08 +00:00
|
|
|
}
|