|
|
@ -2,158 +2,169 @@ import matrix from 'matrix-js-sdk';
|
|
|
|
import main from '@/main.js';
|
|
|
|
import main from '@/main.js';
|
|
|
|
// import Vue from 'vue';
|
|
|
|
// import Vue from 'vue';
|
|
|
|
|
|
|
|
|
|
|
|
var matrix_cli = matrix.createClient({
|
|
|
|
const client = matrix.createClient({
|
|
|
|
baseUrl: "https://adb.sh",
|
|
|
|
baseUrl: 'https://adb.sh',
|
|
|
|
accessToken: getCookie("accessToken"),
|
|
|
|
accessToken: getCookie('accessToken'),
|
|
|
|
userId: getCookie("userId")
|
|
|
|
userId: getCookie('userId'),
|
|
|
|
})
|
|
|
|
});
|
|
|
|
let session = {
|
|
|
|
let session = {
|
|
|
|
user: "",
|
|
|
|
user: '',
|
|
|
|
password: "",
|
|
|
|
password: '',
|
|
|
|
accessToken: "",
|
|
|
|
accessToken: '',
|
|
|
|
rooms: [],
|
|
|
|
rooms: [],
|
|
|
|
currentRoom: undefined,
|
|
|
|
currentRoom: undefined,
|
|
|
|
login: {
|
|
|
|
login: {
|
|
|
|
user: "",
|
|
|
|
user: '',
|
|
|
|
password: ""
|
|
|
|
password: '',
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
console.log(document.cookie)
|
|
|
|
console.log(document.cookie);
|
|
|
|
|
|
|
|
|
|
|
|
if (getCookie("accessToken") && getCookie("userId")){
|
|
|
|
if (getCookie('accessToken') && getCookie('userId')) {
|
|
|
|
document.cookie = `expires=${new Date(Date.now() + 86400 * 10 * 1000)}`;
|
|
|
|
document.cookie = `expires=${new Date(Date.now() + 86400 * 10 * 1000)}`;
|
|
|
|
session = {
|
|
|
|
session = {
|
|
|
|
user: getCookie("userId"),
|
|
|
|
user: getCookie('userId'),
|
|
|
|
password: "",
|
|
|
|
password: '',
|
|
|
|
accessToken: getCookie("accessToken"),
|
|
|
|
accessToken: getCookie('accessToken'),
|
|
|
|
rooms: [],
|
|
|
|
rooms: [],
|
|
|
|
currentRoom: undefined
|
|
|
|
currentRoom: undefined,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
// Vue.$router.push("/rooms/")
|
|
|
|
// Vue.$router.push("/rooms/")
|
|
|
|
window.location.href = '/#/rooms/';
|
|
|
|
window.location.href = '/#/rooms/';
|
|
|
|
matrix_cli.startClient();
|
|
|
|
client.startClient();
|
|
|
|
matrix_cli.once('sync', function (state){
|
|
|
|
client.once('sync', (state) => {
|
|
|
|
console.log(state)
|
|
|
|
console.log(state);
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else window.location.href = '/#/login';
|
|
|
|
else window.location.href = '/#/login';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
export default {
|
|
|
|
data() {
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
session: session
|
|
|
|
session,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
methods: {
|
|
|
|
login() {
|
|
|
|
login() {
|
|
|
|
if (session.accessToken !== ""){
|
|
|
|
if (session.accessToken !== '') {
|
|
|
|
main.methods.error("you are already logged in")
|
|
|
|
main.methods.error('you are already logged in');
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
} else if (session.login.user === ""){
|
|
|
|
} if (session.login.user === '') {
|
|
|
|
main.methods.error('username is empty')
|
|
|
|
main.methods.error('username is empty');
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
} else if (session.login.password === ""){
|
|
|
|
} if (session.login.password === '') {
|
|
|
|
main.methods.error('password is empty')
|
|
|
|
main.methods.error('password is empty');
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
} else if (!(session.login.user.includes("@") && session.login.user.includes(":"))){
|
|
|
|
} if (!(session.login.user.includes('@') && session.login.user.includes(':'))) {
|
|
|
|
main.methods.error('username is in wrong style')
|
|
|
|
main.methods.error('username is in wrong style');
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
matrix_cli.login("m.login.password", {
|
|
|
|
client.login('m.login.password', {
|
|
|
|
user: session.login.user,
|
|
|
|
user: session.login.user,
|
|
|
|
password: session.login.password,
|
|
|
|
password: session.login.password,
|
|
|
|
initial_device_display_name: "matrix chat",
|
|
|
|
initial_device_display_name: 'matrix chat',
|
|
|
|
}).then((response) => {
|
|
|
|
}).then((response) => {
|
|
|
|
document.cookie = `accessToken=${response.access_token}`;
|
|
|
|
document.cookie = `accessToken=${response.access_token}`;
|
|
|
|
document.cookie = `userId=${session.login.user}`;
|
|
|
|
document.cookie = `userId=${session.login.user}`;
|
|
|
|
document.cookie = `max-expires=${new Date(Date.now() + 86400 * 10 * 1000)}`;
|
|
|
|
document.cookie = `max-expires=${new Date(Date.now() + 86400 * 10 * 1000)}`;
|
|
|
|
session = {
|
|
|
|
session = {
|
|
|
|
user: session.login.user,
|
|
|
|
user: session.login.user,
|
|
|
|
password: "",
|
|
|
|
password: '',
|
|
|
|
accessToken: response.access_token,
|
|
|
|
accessToken: response.access_token,
|
|
|
|
rooms: [],
|
|
|
|
rooms: [],
|
|
|
|
currentRoom: undefined
|
|
|
|
currentRoom: undefined,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
console.log(`access token => ${response.access_token}`);
|
|
|
|
console.log(`access token => ${response.access_token}`);
|
|
|
|
if (response.error) {
|
|
|
|
if (response.error) {
|
|
|
|
main.methods.error(response.error)
|
|
|
|
main.methods.error(response.error);
|
|
|
|
console.log(`login error => ${response.error}`)
|
|
|
|
console.log(`login error => ${response.error}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
window.location.href = '/#/rooms/';
|
|
|
|
window.location.href = '/#/rooms/';
|
|
|
|
matrix_cli.startClient();
|
|
|
|
client.startClient();
|
|
|
|
matrix_cli.once('sync', function (state){
|
|
|
|
client.once('sync', (state) => {
|
|
|
|
console.log(state)
|
|
|
|
console.log(state);
|
|
|
|
})
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
},
|
|
|
|
sendMessage(msg) {
|
|
|
|
sendMessage(msg) {
|
|
|
|
let msgSend = {
|
|
|
|
const msgSend = {
|
|
|
|
type: msg.type,
|
|
|
|
type: msg.type,
|
|
|
|
content: {
|
|
|
|
content: {
|
|
|
|
body: msg.content.body,
|
|
|
|
body: msg.content.body,
|
|
|
|
msgtype: msg.content.msgtype
|
|
|
|
msgtype: msg.content.msgtype,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
};
|
|
|
|
matrix_cli.sendEvent(session.currentRoom.roomId, msgSend.type, msgSend.content, "").then(()=>{
|
|
|
|
client.sendEvent(session.currentRoom.roomId, msgSend.type, msgSend.content, '').then(() => {
|
|
|
|
console.log("message sent successfully")
|
|
|
|
console.log('message sent successfully');
|
|
|
|
}).catch(err => {
|
|
|
|
}).catch((err) => {
|
|
|
|
console.log(`error while sending message => ${err}`)
|
|
|
|
console.log(`error while sending message => ${err}`);
|
|
|
|
main.methods.error("message could not be sent")
|
|
|
|
main.methods.error('message could not be sent');
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function getCookie(key) {
|
|
|
|
function getCookie(key) {
|
|
|
|
let cookie = document.cookie.replace(/ /g, '').split(';')
|
|
|
|
const cookie = document.cookie.replace(/ /g, '').split(';')
|
|
|
|
.find(cookie => cookie.split('=')[0] === key);
|
|
|
|
.find((cookie) => cookie.split('=')[0] === key);
|
|
|
|
return cookie ? cookie.split('=')[1] : false;
|
|
|
|
return cookie ? cookie.split('=')[1] : false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
matrix_cli.on("event", event => {
|
|
|
|
client.on('event', (event) => {
|
|
|
|
console.log(event.getType());
|
|
|
|
console.log(event.getType());
|
|
|
|
console.log(event);
|
|
|
|
console.log(event);
|
|
|
|
if (event.getType() === "m.room.name") {
|
|
|
|
if (event.getType() === 'm.room.name') {
|
|
|
|
matrix_cli.getRooms().forEach(newRoom => {
|
|
|
|
client.getRooms().forEach((newRoom) => {
|
|
|
|
let room = session.rooms.find(room => room.roomId === newRoom.roomId)
|
|
|
|
const room = session.rooms.find((room) => room.roomId === newRoom.roomId);
|
|
|
|
if (!room) {
|
|
|
|
if (!room) {
|
|
|
|
session.rooms.push({name: newRoom.name, roomId: newRoom.roomId, messages: [], members: []})
|
|
|
|
session.rooms.push({
|
|
|
|
console.log(`new room => ${newRoom.name}`)
|
|
|
|
name: newRoom.name, roomId: newRoom.roomId, messages: [], members: [],
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
console.log(`new room => ${newRoom.name}`);
|
|
|
|
} else if (room.name !== newRoom.name) {
|
|
|
|
} else if (room.name !== newRoom.name) {
|
|
|
|
console.log(`roomname changed from ${room.name} to ${newRoom.name}`)
|
|
|
|
console.log(`roomname changed from ${room.name} to ${newRoom.name}`);
|
|
|
|
room.name = newRoom.name
|
|
|
|
room.name = newRoom.name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else if (event.getType() === 'm.room.member') {
|
|
|
|
else if (event.getType() === "m.room.member"){
|
|
|
|
const room = session.rooms.find((room) => room.roomId === event.event.room_id);
|
|
|
|
let room = session.rooms.find(room => room.roomId === event.event.room_id)
|
|
|
|
|
|
|
|
if (!room) {
|
|
|
|
if (!room) {
|
|
|
|
session.rooms.push({name: "undefined", roomId: event.event.room_id, messages: [], members: []})
|
|
|
|
session.rooms.push({
|
|
|
|
console.log(`new user => ${event.event.sender}`)
|
|
|
|
name: 'undefined', roomId: event.event.room_id, messages: [], members: [],
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
console.log(`new user => ${event.event.sender}`);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
room.members.push({sender: event.event.sender, content: event.event.content})
|
|
|
|
room.members.push({ sender: event.event.sender, content: event.event.content });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
console.log(session)
|
|
|
|
console.log(session);
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
matrix_cli.on("Room.timeline", event => {
|
|
|
|
client.on('Room.timeline', (event, room) => {
|
|
|
|
if (event.getType() === "m.room.message") {
|
|
|
|
if (event.getType() === 'm.room.message') {
|
|
|
|
console.log(`new message => ${event.event.content.body}`);
|
|
|
|
console.log(`message => ${event.event.content.body}`);
|
|
|
|
let room = session.rooms.find(room => room.roomId === event.event.room_id)
|
|
|
|
const thisRoom = session.rooms.find((thisRoom) => thisRoom.roomId === room.roomId);
|
|
|
|
if (!room) {
|
|
|
|
if (!thisRoom) {
|
|
|
|
session.rooms.push({name: "undefined", roomId: event.event.room_id, messages: [event.event], members: []})
|
|
|
|
session.rooms.push({
|
|
|
|
console.log(`undefined room name for => ${event.event.room_id}`)
|
|
|
|
name: room.name, roomId: event.event.room_id, messages: [event.event], members: [],
|
|
|
|
} else room.messages.push(event.event)
|
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
thisRoom.messages.push(event.event);
|
|
|
|
|
|
|
|
if (thisRoom.name === 'undefined') thisRoom.name = room.name;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let msgContainer = document.getElementById("messagesContainer")
|
|
|
|
const msgContainer = document.getElementById('messagesContainer');
|
|
|
|
if (session.currentRoom && session.currentRoom.roomId === event.event.room_id) {
|
|
|
|
if (session.currentRoom && session.currentRoom.roomId === room.roomId) {
|
|
|
|
if (event.event.sender === session.user || msgContainer.scrollHeight < msgContainer.scrollTop + 1000)
|
|
|
|
if (event.event.sender === session.user || msgContainer.scrollHeight < msgContainer.scrollTop + 1000) {
|
|
|
|
setTimeout(() => {
|
|
|
|
setTimeout(() => {
|
|
|
|
msgContainer.scrollTo(0, msgContainer.scrollHeight)
|
|
|
|
msgContainer.scrollTo(0, msgContainer.scrollHeight);
|
|
|
|
}, 10)
|
|
|
|
}, 10);
|
|
|
|
else document.getElementById("scrollDown").style.display = "block"
|
|
|
|
} else document.getElementById('scrollDown').style.display = 'block';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
client.on('Room.member', (member) => {
|
|
|
|
|
|
|
|
console.log('this is a member');
|
|
|
|
|
|
|
|
console.log(member);
|
|
|
|
|
|
|
|
});
|
|
|
|