xrfragment-haxe/src/3rd/js/plugin/matrix/matrix.js

124 lines
3.9 KiB
JavaScript
Raw Normal View History

2023-12-27 17:25:49 +00:00
window.matrix = (opts) => new Proxy({
2023-12-27 21:31:42 +00:00
el: null, // HTML element
2023-12-27 17:25:49 +00:00
plugin:{
type: 'network',
2024-01-03 14:23:34 +00:00
name: '[Matrix]',
2023-12-27 21:31:42 +00:00
description: 'a standardized decentralized privacy-friendly protocol',
2023-12-27 17:25:49 +00:00
url: 'https://matrix.org',
2023-12-27 21:31:42 +00:00
protocol: 'matrix://',
2023-12-27 17:25:49 +00:00
video: false,
audio: false,
chat: true,
scene: true
},
html: {
2023-12-27 21:31:42 +00:00
generic: (opts) => `<div>
<a href="${opts.url}" target="_blank" class="badge ruler">matrix</a>
<table>
<tr>
<td>channel</td>
<td>
<input type="text" id="channel" placeholder="#xrfragment:matrix.org"/>
</td>
</tr>
<tr>
<td>server</td>
<td>
<input type="text" id="server" placeholder="https://matrix.org"/>
</td>
</tr>
<tr>
<td>user</td>
<td>
<input type="text" id="username" placeholder="@you:matrix.org"/>
</td>
</tr>
<tr>
<td>auth</td>
2023-12-27 21:31:42 +00:00
<td>
<select id="auth">
2023-12-27 21:31:42 +00:00
<option>via password</option>
<option>via access token</option>
</select>
</td>
</tr>
2023-12-27 17:25:49 +00:00
<tr>
2023-12-27 21:31:42 +00:00
<td></td>
2023-12-27 17:25:49 +00:00
<td>
<input type="text" id="secret" placeholder="enter password"/>
2023-12-27 17:25:49 +00:00
</td>
</tr>
</table>
2023-12-27 21:31:42 +00:00
<small style="display:inline-block;float:right">Support for Oauth / OpenID is <a href="https://matrix.org/blog/#openid-connect" target="_blank">in progress</a></small>
2024-01-03 14:23:34 +00:00
<br>
2023-12-27 17:25:49 +00:00
</div>
`
},
init(){
2024-01-03 14:23:34 +00:00
frontend.plugin['matrix'] = this
2023-12-27 17:25:49 +00:00
$connections.chatnetwork = $connections.chatnetwork.concat([this])
$connections.scene = $connections.scene.concat([this])
2023-12-27 21:31:42 +00:00
this.reactToConnectionHrefs()
},
connect(opts){
console.log("connecting "+this.plugin.name)
console.dir(opts)
2023-12-27 17:25:49 +00:00
},
config(opts){
opts = {...opts, ...this.plugin }
2023-12-27 21:31:42 +00:00
this.el = document.createElement('div')
2023-12-27 17:25:49 +00:00
let html = this.html.generic(opts)
for( let i in opts ){
if( this.html[i] ) html += this.html[i](opts)
}
2023-12-27 21:31:42 +00:00
this.el.innerHTML = html
this.el.querySelector('#auth').addEventListener('change', (e) => {
this.el.querySelector('#secret').setAttribute('placeholder', `enter ${e.target.value.replace(/.* /,'')}`)
})
2023-12-27 21:31:42 +00:00
window.notify(`${opts.name} is ${opts.description}, it is the hottest internet technology available at this moment.<br>Read more about it <a href="${opts.url}" target="_blank">here</a>.<br>You can basically make up a new channelname or use an existing one`)
return this.el
},
reactToConnectionHrefs(){
xrf.addEventListener('href', (opts) => {
let {mesh} = opts
if( !opts.click ) return
if( mesh.userData.href.match(this.protocol) ){
let parts = mesh.userData.href.replace(this.plugin.protocol,'')
if( parts[0] == 'r' ){ // room
let server = parts.split("/")[1].replace(/:.*/,'')
let channel = parts.split("/")[1].replace(/.*:/,'')
$connections.show()
$connections.selectedChatnetwork = this.plugin.name
$connections.selectedScene = this.plugin.name
2023-12-27 21:31:42 +00:00
this.el.querySelector('#channel').value = `#${channel}:${server}`
this.el.querySelector('#server').value = server
console.log("configured matrix")
}
}else window.notify("malformed connection URI: "+mesh.userData.href)
2023-12-27 17:25:49 +00:00
})
}
},
{
// auto-trigger events on changes
get(data,k,receiver){ return data[k] },
set(data,k,v){
let from = data[k]
data[k] = v
2023-12-27 21:31:42 +00:00
//switch( k ){
// default: matrix.opts.scene.dispatchEvent({type:`matrix.${k}.change`, from, to:v})
//}
2023-12-27 17:25:49 +00:00
}
})
document.addEventListener('$connections:ready', (e) => {
matrix(e.detail).init()
})