Compare commits

...

4 Commits

Author SHA1 Message Date
adb
3a8a25d2e1 add CapacitorStorageApi and some fixes 2021-05-10 00:35:43 +02:00
adb
fdfb9c0361 Merge remote-tracking branch 'origin/master' into matrix-chat-native
# Conflicts:
#	package.json
2021-05-09 16:54:06 +02:00
adb
e48c516211 Merge branch 'dev' 2021-05-09 16:47:44 +02:00
adb
9853c5327e debug and add localStorage 2021-05-09 16:07:40 +02:00
7 changed files with 79 additions and 58 deletions

View File

@ -22,7 +22,7 @@
type="submit"
title="press enter to submit"
class="sendMessageBtn"
ic="./sym/ic_send_white.svg"
:ic="isSending?'./sym/throbber.svg':'./sym/ic_send_white.svg'"
@click.native="onSubmit(event)"
/>
<sound-recorder v-else class="recorder" :on-stop="setAttachment" ref="recorder"/>
@ -36,7 +36,7 @@
<fileUpload class="leftBtn" :on-change="setAttachment"/>
</div>
<v-emoji-picker
v-if="showEmojiPicker"
v-show="showEmojiPicker"
class="emojiPicker"
@select="onSelectEmoji"
:dark="true"
@ -74,13 +74,15 @@ export default {
},
methods: {
onSubmit(event){
console.log(event)
if (this.isSending) return;
event.content.msgtype==='m.text'?this.sendEvent(event):this.sendMediaEvent(event);
},
async sendEvent(event){
if (!event.content.body.trim()) return;
if (this.replyTo) this.setReplyTo(this.replyTo);
matrix.sendEvent(new Proxy(this.event, this.eventProxyHandler), this.roomId);
this.setReplyTo(this.replyTo);
this.isSending = true;
await matrix.sendEvent(new Proxy(this.event, this.eventProxyHandler), this.roomId);
this.isSending = false;
event.content.body = '';
this.resetAttachment();
this.resetReplyTo();
@ -89,6 +91,7 @@ export default {
this.onResize(id.parentElement.clientHeight);
},
sendMediaEvent(event){
this.isSending = true;
matrix.client.uploadContent(this.attachment.blob).then(mxc => {
event.content.url = mxc;
this.sendEvent(event);
@ -139,7 +142,7 @@ export default {
};
},
resetAttachment(){
if (!this.attachment) return;
if (!this.attachment) return this.attachment = undefined;
window.URL.revokeObjectURL(this.attachment.file);
this.event.content = {
body: this.attachment?this.event.content.body.replace(this.attachment.filename, ''):'',
@ -169,20 +172,21 @@ export default {
}
}
},
showEmojiPicker: false,
waitForSendTyping: false,
attachment: undefined,
eventProxyHandler: {
set: () => true,
get: (target, key) => {
if (typeof target[key] === 'object') return new Proxy(Object.assign({}, target[key]), this.eventProxyHandler);
return target[key];
}
}
},
showEmojiPicker: false,
waitForSendTyping: false,
attachment: undefined,
isSending: false
}
},
updated() {
this.resizeMessageBanner();
this.$nextTick(this.resizeMessageBanner);
}
}
</script>

21
src/lib/DataStore.js Normal file
View File

@ -0,0 +1,21 @@
import {cookieHandler} from '@/lib/cookieHandler';
import {Capacitor, Plugins} from '@capacitor/core';
const {Storage} = Plugins;
export class DataStore{
constructor(){
this.cookie = new cookieHandler();
this.cookie.setExpire(15);
this.store = localStorage;
}
async set(key, value){
if (Capacitor.isNative) return await Storage.set({key, value: JSON.stringify(value)});
this.store.setItem(key, JSON.stringify(value));
this.cookie.set(key, JSON.stringify(value));
this.cookie.store();
}
async get(key){
if (Capacitor.isNative) return JSON.parse((await Storage.get({key})).value||'null');
return JSON.parse(this.store.getItem(key) || this.cookie.get(key) || 'null');
}
}

View File

@ -48,7 +48,8 @@ export class MatrixHandler {
baseUrl,
accessToken,
userId,
store: new matrix.MemoryStore(window.localStorage)
store: new matrix.MemoryStore(window.localStorage),
sessionStore: new matrix.WebStorageSessionStore(window.localStorage)
});
this.user = userId;
this.baseUrl = baseUrl;

View File

@ -8,11 +8,17 @@ export class cookieHandler {
getCookies(){
return this.cookies;
}
setCookie(cookies){
setCookies(cookies){
Object.keys(cookies).forEach(key => {
this.cookies[key] = cookies[key];
})
}
set(key, value){
this.cookies[key] = value;
}
get(key){
return this.cookies[key];
}
parseCookie(string){
let cookies = {};
string.replace(/ /g, '').split(';').forEach(cookie => {
@ -24,14 +30,12 @@ export class cookieHandler {
reload(){
if (document.cookie) this.cookies = this.parseCookie(document.cookie);
console.log('cookie loaded')
console.log(this.cookies);
}
store(){
Object.keys(this.cookies).forEach(key => {
document.cookie = `${key}=${this.cookies[key]}; expires=${this.expires}; SameSite=${this.SameSite}; Secure;`;
});
console.log('cookie stored');
console.log(this.cookies);
}
toString(cookies = this.cookies){
let string = '';

View File

@ -2,25 +2,29 @@ import Vue from 'vue'
import VueRouter from 'vue-router'
import App from './App.vue'
import {router} from '@/router'
import {MatrixHandler} from './lib/matrixHandler.js'
import {cookieHandler} from './lib/cookieHandler.js';
import {MatrixHandler} from './lib/MatrixHandler.js'
import {DataStore} from '@/lib/DataStore';
Vue.config.productionTip = false;
Vue.use(VueRouter);
export let matrix = new MatrixHandler();
let cookie = new cookieHandler().getCookies();
if (cookie && cookie.baseUrl && cookie.accessToken && cookie.userId) {
matrix.tokenLogin(cookie.baseUrl, cookie.accessToken, cookie.userId);
}
new Vue({
el: '#app',
router,
template: '<App/>',
components: {App},
data() {
return {}
(async () => {
let login = await new DataStore().get('login');
if (login && login.baseUrl && login.accessToken && login.userId) {
matrix.tokenLogin(login.baseUrl, login.accessToken, login.userId);
}
}).$mount('#app');
new Vue({
el: '#app',
router,
template: '<App/>',
components: {App},
data() {
return {}
}
}).$mount('#app');
})()

View File

@ -9,7 +9,7 @@ export const router = new VueRouter({
{
path: '/',
name: 'home',
component: login
component: rooms
},
{
path: '/login',

View File

@ -22,9 +22,10 @@
<script>
import textbtn from '@/components/textbtn';
import {matrix} from '@/main.js';
import {cookieHandler} from '@/lib/cookieHandler';
import ThrobberOverlay from '@/components/throbberOverlay';
import {isValidUserId} from '@/lib/matrixUtils';
import {DataStore} from '@/lib/DataStore';
const store = new DataStore();
export default {
name: 'login.vue',
@ -34,32 +35,18 @@ export default {
},
methods: {
login(){
if (matrix.client !== undefined) {
this.loginError = 'you are already logged in';
return;
} if (this.user === '') {
this.loginError = 'username is empty';
return;
} if (this.password === '') {
this.loginError = 'password is empty';
return;
} if (!isValidUserId(this.user)) {
this.loginError = 'username is in wrong style';
return;
}
// eslint-disable-next-line no-cond-assign
if (this.loginError = this.getInputErrors()) return false;
this.loading = 'logging in';
matrix.login(this.user, this.password, this.homeServer, (error) => {
this.loginError = `login failed: ${error}`;
this.loading = false;
}, token => {
this.loading = 'store token';
this.cookie.setCookie({
this.store.set('login', {
baseUrl: this.homeServer,
userId: this.user,
accessToken: token
});
this.cookie.setExpire(15);
this.cookie.store();
this.loading = false;
this.$router.push('/rooms/');
});
@ -67,17 +54,17 @@ export default {
async logout(){
this.loading = 'logging out';
await matrix.logout();
this.loading = 'remove token';
this.cookie.setCookie({
baseUrl: undefined,
userId: undefined,
accessToken: undefined
});
this.cookie.setExpire(0);
this.cookie.store();
this.store.set('login', {});
this.loading = false;
this.$forceUpdate();
},
getInputErrors(){
if (matrix.client !== undefined) return 'you are already logged in';
if (this.user === '') return 'username is empty';
if (this.password === '') return 'password is empty';
if (!isValidUserId(this.user)) return 'username is in wrong style';
return false;
},
showLogin(){
return matrix.client === undefined;
}
@ -88,7 +75,7 @@ export default {
password: '',
homeServer: 'https://adb.sh',
loginError: '',
cookie: new cookieHandler(),
store,
loading: false
}
}