chat functional
This commit is contained in:
@ -1,9 +1,77 @@
|
||||
import {Message} from "./message.js"
|
||||
|
||||
class Channel {
|
||||
constructor(id, members, messages) {
|
||||
this.id = id;
|
||||
this.members = members;
|
||||
this.messages = messages;
|
||||
constructor(client, channel_id, members_id, messages, reload) {
|
||||
this.client = client;
|
||||
this.channel_id = channel_id;
|
||||
this.members_id = members_id;
|
||||
this.messages = [];
|
||||
if (messages != undefined)
|
||||
this.updateMessages(messages);
|
||||
|
||||
this.connect(reload);
|
||||
}
|
||||
|
||||
// reload = function to use when we receive a message
|
||||
async connect(reload) {
|
||||
let url = `ws://${window.location.host}/ws/chat/${this.channel_id}/`;
|
||||
|
||||
this.chatSocket = new WebSocket(url);
|
||||
this.chatSocket.onmessage = (event) =>{
|
||||
let data = JSON.parse(event.data)
|
||||
|
||||
this.messages.push(new Message(
|
||||
this.channel_id,
|
||||
data.author_id,
|
||||
data.content,
|
||||
data.time,
|
||||
));
|
||||
|
||||
reload();
|
||||
};
|
||||
}
|
||||
|
||||
async disconnect() {
|
||||
this.chatSocket.close();
|
||||
}
|
||||
|
||||
updateMessages(messages) {
|
||||
messages = JSON.parse(messages);
|
||||
let new_messages = [];
|
||||
|
||||
messages.forEach((message) => {
|
||||
message = message["fields"];
|
||||
new_messages.push(new Message(
|
||||
message["channel_id"],
|
||||
message["author_id"],
|
||||
message["content"],
|
||||
message["time"],
|
||||
));
|
||||
});
|
||||
|
||||
//console.log(new_messages);
|
||||
this.messages = new_messages;
|
||||
return new_messages;
|
||||
}
|
||||
|
||||
async sendMessageChannel(message) {
|
||||
|
||||
if (this.chatSocket == undefined)
|
||||
return;
|
||||
|
||||
this.chatSocket.send(JSON.stringify({
|
||||
'message':message
|
||||
}));
|
||||
}
|
||||
|
||||
async deleteChannel() {
|
||||
let response = await this.client._delete("/api/chat/" + this.channel_id, {
|
||||
});
|
||||
|
||||
let data = await response.json();
|
||||
return data;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export {Channel}
|
||||
|
@ -1,19 +1,37 @@
|
||||
import {Channel} from "./channel.js"
|
||||
import {Message} from "./message.js"
|
||||
|
||||
class Channels {
|
||||
constructor(client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
async createChannel(users_id) {
|
||||
console.log(users_id);
|
||||
async createChannel(users_id, reload) {
|
||||
let response = await this.client._post("/api/chat/", {
|
||||
users_id:users_id
|
||||
});
|
||||
|
||||
let data = await response.json();
|
||||
if (data == "Channel already exist")
|
||||
return null;
|
||||
let exit_code = await response.status;
|
||||
if (exit_code >= 300)
|
||||
return undefined;
|
||||
|
||||
let messages = undefined;
|
||||
if (exit_code == 200)
|
||||
messages = data.messages;
|
||||
return new Channel(this.client, data.channel_id, users_id, messages, reload);
|
||||
}
|
||||
|
||||
async deleteChannel(users_id) {
|
||||
let response = await this.client._delete("/api/chat/", {
|
||||
users_id:users_id
|
||||
});
|
||||
|
||||
let data = await response.json();
|
||||
console.log(response.status)
|
||||
return data;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export {Channels}
|
||||
|
10
frontend/static/js/api/chat/message.js
Normal file
10
frontend/static/js/api/chat/message.js
Normal file
@ -0,0 +1,10 @@
|
||||
class Message {
|
||||
constructor(channel_id, author_id, content, time) {
|
||||
this.channel_id = channel_id;
|
||||
this.author_id = author_id;
|
||||
this.content = content;
|
||||
this.time = time;
|
||||
}
|
||||
}
|
||||
|
||||
export {Message}
|
@ -1,6 +1,7 @@
|
||||
import { Account } from "./account.js";
|
||||
import { Profile } from "./profile.js";
|
||||
import { Profiles } from "./profiles.js";
|
||||
import { Channels } from './chat/channels.js';
|
||||
|
||||
function getCookie(name)
|
||||
{
|
||||
@ -20,6 +21,9 @@ class Client
|
||||
this.account = new Account(this);
|
||||
this.profiles = new Profiles(this);
|
||||
this._logged = undefined;
|
||||
|
||||
this.channels = new Channels(this);
|
||||
this.channel = undefined;
|
||||
}
|
||||
|
||||
async isAuthentificate()
|
||||
|
Reference in New Issue
Block a user