window.matrix = (opts) => new Proxy({ el: null, // HTML element plugin:{ type: 'network', name: '[Matrix]', description: 'a standardized decentralized privacy-friendly protocol', url: 'https://matrix.org', protocol: 'matrix://', video: false, audio: false, chat: true, scene: true }, channel: '#xrfragment-test:matrix.org', server: 'https://matrix.org', username:'', auth: 'via password', authkey: '', client: null, roomid: '', html: { generic: (opts) => `
` }, init(){ frontend.plugin['matrix'] = this $connections.chatnetwork = $connections.chatnetwork.concat([this]) $connections.scene = $connections.scene.concat([this]) this.reactToConnectionHrefs() this.nickname = localStorage.getItem("nickname") || `human${String(Math.random()).substr(5,4)}` document.addEventListener('network.connect', (e) => this.connect(e.detail) ) document.addEventListener('network.init', () => { let meeting = network.getMeetingFromUrl(document.location.href) if( meeting.match(this.plugin.protocol) ){ this.parseLink( meeting ) } }) }, connect(opts){ console.log("connecting "+this.plugin.name) this.channel = document.querySelector("#matrix input#channel").value this.server = document.querySelector("#matrix input#server").value this.username = document.querySelector("#matrix input#username").value this.auth = document.querySelector("#matrix select#auth").value let secret = document.querySelector("#matrix input#secret").value document.querySelector("#matrix input#secret").value = '' let credentials = { baseUrl: this.server } if( this.auth == 'via access token'){ credentials.accessToken = secret credentials.userId = this.username } this.client = Matrix.sdk.createClient(credentials) if( this.auth == 'via password'){ this.client.login("m.login.password",{"user": this.username, password: secret}) .then( () => this.onMatrixConnect() ) .catch( () => window.notify("authentication was not succesful 😞")) }else this.onMatrixConnect() }, onMatrixConnect(){ // token: this.matrixclient.getAccessToken() this.client.startClient() client.once('sync', function(state, prevState, res) { if( state == 'PREPARED' ) this.setupListeners() else console.log("state: "+state) }); }, setupListeners(){ let rooms = this.client.getRooms(); rooms.forEach(room => { console.log(room); }); this.client.on("Room.timeline", function(event, room, toStartOfTimeline) { if( event.room_id && event.room_id == this.roomid ){ console.dir(event); } }); }, config(opts){ opts = {...opts, ...this.plugin } this.el = document.createElement('div') let html = this.html.generic(opts) for( let i in opts ){ if( this.html[i] ) html += this.html[i](opts) } this.el.innerHTML = html this.el.querySelector('#auth').addEventListener('change', (e) => { this.el.querySelector('#secret').setAttribute('placeholder', `enter ${e.target.value.replace(/.* /,'')}`) }) return this.el }, info(opts){ window.notify(`${this.plugin.name} is ${this.plugin.description}, it is the hottest internet technology available at this moment.