Add matrix service to $matrix and move room handling to that
This commit is contained in:
parent
2ca7dd3655
commit
c88931e00c
7 changed files with 239 additions and 213 deletions
|
|
@ -1,6 +1,5 @@
|
|||
global.Olm = require("olm");
|
||||
import sdk from "matrix-js-sdk";
|
||||
import Vue from 'vue';
|
||||
|
||||
const LocalStorageCryptoStore = require("matrix-js-sdk/lib/crypto/store/localStorage-crypto-store")
|
||||
.LocalStorageCryptoStore;
|
||||
|
|
@ -8,88 +7,184 @@ sdk.setCryptoStoreFactory(
|
|||
() => new LocalStorageCryptoStore(window.localStorage)
|
||||
);
|
||||
|
||||
class MatrixService {
|
||||
constructor() {
|
||||
this.matrixClient = null;
|
||||
}
|
||||
|
||||
login(user) {
|
||||
const tempMatrixClient = sdk.createClient(user.server);
|
||||
return tempMatrixClient
|
||||
.login("m.login.password", { user: user.username, password: user.password, type: "m.login.password" })
|
||||
.then((response) => {
|
||||
localStorage.setItem('user', JSON.stringify(response));
|
||||
return response;
|
||||
})
|
||||
}
|
||||
|
||||
logout() {
|
||||
localStorage.removeItem('user');
|
||||
}
|
||||
|
||||
async getMatrixClient(user) {
|
||||
const matrixStore = new sdk.MemoryStore(window.localStorage);
|
||||
const webStorageSessionStore = new sdk.WebStorageSessionStore(
|
||||
window.localStorage
|
||||
);
|
||||
|
||||
var homeServer = user.home_server;
|
||||
if (!homeServer.startsWith("https://")) {
|
||||
homeServer = "https://" + homeServer;
|
||||
export default {
|
||||
install(Vue, options) {
|
||||
if (!options || !options.store) {
|
||||
throw new Error('Please initialise plugin with a Vuex store.')
|
||||
}
|
||||
|
||||
var opts = {
|
||||
baseUrl: homeServer,
|
||||
userId: user.user_id,
|
||||
store: matrixStore,
|
||||
sessionStore: webStorageSessionStore,
|
||||
deviceId: user.device_id,
|
||||
accessToken: user.access_token
|
||||
}
|
||||
user.matrixClient = sdk.createClient(opts);
|
||||
return user.matrixClient
|
||||
.initCrypto()
|
||||
.then(() => {
|
||||
console.log("Crypto initialized");
|
||||
user.matrixClient.startClient();
|
||||
return user.matrixClient;
|
||||
})
|
||||
.then(matrixClient => {
|
||||
if (matrixClient.isInitialSyncComplete()) {
|
||||
console.log("Initial sync done already!");
|
||||
return matrixClient;
|
||||
} else {
|
||||
return new Promise((resolve, reject) => {
|
||||
matrixClient.once(
|
||||
"sync",
|
||||
function (state, ignoredprevState, ignoredres) {
|
||||
console.log(state); // state will be 'PREPARED' when the client is ready to use
|
||||
if (state == "PREPARED") {
|
||||
resolve(matrixClient);
|
||||
} else if (state == "ERROR") {
|
||||
reject("Error syncing");
|
||||
}
|
||||
}
|
||||
)
|
||||
});
|
||||
const store = options.store;
|
||||
|
||||
const matrixService = new Vue({
|
||||
store,
|
||||
|
||||
data() {
|
||||
return {
|
||||
matrixClient: null,
|
||||
rooms: [],
|
||||
}
|
||||
});
|
||||
},
|
||||
mounted() {
|
||||
console.log("Matrix service mounted");
|
||||
},
|
||||
|
||||
computed: {
|
||||
ready() {
|
||||
return this.matrixClient != null;
|
||||
},
|
||||
|
||||
currentUser() {
|
||||
return this.$store.state.auth.user;
|
||||
},
|
||||
|
||||
currentRoomId() {
|
||||
return this.$store.state.currentRoomId;
|
||||
},
|
||||
|
||||
currentRoom() {
|
||||
return this.getRoom(this.currentRoomId);
|
||||
},
|
||||
|
||||
},
|
||||
|
||||
methods: {
|
||||
login(user) {
|
||||
const tempMatrixClient = sdk.createClient(user.server);
|
||||
var retUser;
|
||||
return tempMatrixClient
|
||||
.login("m.login.password", { user: user.username, password: user.password, type: "m.login.password" })
|
||||
.then((response) => {
|
||||
localStorage.setItem('user', JSON.stringify(response));
|
||||
return response;
|
||||
})
|
||||
.then(user => {
|
||||
retUser = user;
|
||||
return this.getMatrixClient(user);
|
||||
})
|
||||
.then(() => {
|
||||
// Ready to use! Start by loading rooms.
|
||||
this.initClient();
|
||||
return retUser;
|
||||
})
|
||||
},
|
||||
|
||||
logout() {
|
||||
if (this.matrixClient) {
|
||||
this.removeMatrixClientListeners(this.matrixClient);
|
||||
this.matrixClient.stopClient();
|
||||
this.matrixClient = null;
|
||||
localStorage.removeItem('user');
|
||||
}
|
||||
this.$store.commit("setCurrentRoomId", null);
|
||||
},
|
||||
|
||||
initClient() {
|
||||
this.reloadRooms();
|
||||
},
|
||||
|
||||
async getMatrixClient(user) {
|
||||
const matrixStore = new sdk.MemoryStore(window.localStorage);
|
||||
const webStorageSessionStore = new sdk.WebStorageSessionStore(
|
||||
window.localStorage
|
||||
);
|
||||
|
||||
var homeServer = user.home_server;
|
||||
if (!homeServer.startsWith("https://")) {
|
||||
homeServer = "https://" + homeServer;
|
||||
}
|
||||
|
||||
var opts = {
|
||||
baseUrl: homeServer,
|
||||
userId: user.user_id,
|
||||
store: matrixStore,
|
||||
sessionStore: webStorageSessionStore,
|
||||
deviceId: user.device_id,
|
||||
accessToken: user.access_token
|
||||
}
|
||||
this.matrixClient = sdk.createClient(opts);
|
||||
return this.matrixClient
|
||||
.initCrypto()
|
||||
.then(() => {
|
||||
console.log("Crypto initialized");
|
||||
|
||||
this.addMatrixClientListeners(this.matrixClient);
|
||||
|
||||
this.matrixClient.startClient();
|
||||
return this.matrixClient;
|
||||
})
|
||||
.then(matrixClient => {
|
||||
if (matrixClient.isInitialSyncComplete()) {
|
||||
console.log("Initial sync done already!");
|
||||
return matrixClient;
|
||||
} else {
|
||||
return new Promise((resolve, reject) => {
|
||||
matrixClient.once(
|
||||
"sync",
|
||||
function (state, ignoredprevState, ignoredres) {
|
||||
console.log(state); // state will be 'PREPARED' when the client is ready to use
|
||||
if (state == "PREPARED") {
|
||||
resolve(matrixClient);
|
||||
} else if (state == "ERROR") {
|
||||
reject("Error syncing");
|
||||
}
|
||||
}
|
||||
)
|
||||
});
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
addMatrixClientListeners(client) {
|
||||
if (client) {
|
||||
client.on("event", this.onEvent);
|
||||
}
|
||||
},
|
||||
|
||||
removeMatrixClientListeners(client) {
|
||||
if (client) {
|
||||
client.off("event", this.onEvent);
|
||||
}
|
||||
},
|
||||
|
||||
onEvent(event) {
|
||||
if (event.getType() == "m.room.topic") {
|
||||
const room = this.matrixClient.getRoom(event.getRoomId());
|
||||
if (room) {
|
||||
Vue.set(room, "topic", event.getContent().topic);
|
||||
}
|
||||
return;
|
||||
}
|
||||
},
|
||||
|
||||
reloadRooms() {
|
||||
this.rooms = this.matrixClient.getVisibleRooms();
|
||||
},
|
||||
|
||||
setCurrentRoomId(roomId) {
|
||||
this.$store.commit("setCurrentRoomId", roomId);
|
||||
},
|
||||
|
||||
getRoom(roomId) {
|
||||
if (this.matrixClient) {
|
||||
return this.matrixClient.getRoom(roomId);
|
||||
}
|
||||
return null;
|
||||
},
|
||||
|
||||
on(event, handler) {
|
||||
if (this.matrixClient) {
|
||||
this.matrixClient.on(event, handler);
|
||||
}
|
||||
},
|
||||
|
||||
off(event, handler) {
|
||||
if (this.matrixClient) {
|
||||
this.matrixClient.off(event, handler);
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
Vue.prototype.$matrix = matrixService;
|
||||
}
|
||||
}
|
||||
|
||||
const matrixService = new MatrixService();
|
||||
const matrixServicePlugin = {}
|
||||
matrixServicePlugin.install = function (Vue, ignoredOptions) {
|
||||
Vue.prototype.$matrix = matrixService;
|
||||
|
||||
Vue.mixin({
|
||||
mounted: function () {
|
||||
// Store the VUE instance root in our own $root variable.
|
||||
matrixService.$root = this.$root;
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Vue.use(matrixServicePlugin);
|
||||
|
||||
export default MatrixService;
|
||||
Loading…
Add table
Add a link
Reference in a new issue