dockered
This commit is contained in:
22
django/frontend/static/js/api/chat/Ask.js
Normal file
22
django/frontend/static/js/api/chat/Ask.js
Normal file
@ -0,0 +1,22 @@
|
||||
|
||||
class Ask {
|
||||
constructor(client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
async ask_game(asked) {
|
||||
response = await this.client._post(`/api/chat/ask/`);
|
||||
}
|
||||
|
||||
async ask_game_canceled() {
|
||||
|
||||
}
|
||||
|
||||
async ask_game_accepted() {
|
||||
|
||||
}
|
||||
|
||||
async ask_game_refused() {
|
||||
|
||||
}
|
||||
}
|
66
django/frontend/static/js/api/chat/Channel.js
Normal file
66
django/frontend/static/js/api/chat/Channel.js
Normal file
@ -0,0 +1,66 @@
|
||||
import {Message} from "./Message.js";
|
||||
|
||||
class Channel {
|
||||
constructor(client, channel, members, messages, reload) {
|
||||
this.client = client;
|
||||
this.channel = channel;
|
||||
this.members = members;
|
||||
this.messages = [];
|
||||
if (messages != undefined)
|
||||
this.updateMessages(messages);
|
||||
|
||||
this.connect(reload);
|
||||
}
|
||||
|
||||
// reload = function to use when we receive a message
|
||||
connect(reload) {
|
||||
const url = location.origin.replace('http', 'ws') +
|
||||
'/ws/chat/' +
|
||||
this.channel;
|
||||
|
||||
this.chatSocket = new WebSocket(url);
|
||||
this.chatSocket.onmessage = (event) =>{
|
||||
let data = JSON.parse(event.data);
|
||||
|
||||
this.messages.push(new Message(
|
||||
this.channel,
|
||||
data.author,
|
||||
data.content,
|
||||
data.time,
|
||||
));
|
||||
|
||||
reload();
|
||||
};
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
this.chatSocket.close();
|
||||
}
|
||||
|
||||
updateMessages(messages)
|
||||
{
|
||||
this.messages = [];
|
||||
|
||||
messages.forEach((message) => {
|
||||
this.messages.push(new Message(
|
||||
message.channel,
|
||||
message.author,
|
||||
message.content,
|
||||
message.time,
|
||||
));
|
||||
});
|
||||
}
|
||||
|
||||
async sendMessageChannel(message, receivers_id) {
|
||||
|
||||
if (this.chatSocket == undefined)
|
||||
return;
|
||||
|
||||
this.chatSocket.send(JSON.stringify({
|
||||
'message':message,
|
||||
'receivers_id':receivers_id,
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
export {Channel};
|
23
django/frontend/static/js/api/chat/Channels.js
Normal file
23
django/frontend/static/js/api/chat/Channels.js
Normal file
@ -0,0 +1,23 @@
|
||||
import {Channel} from "./Channel.js";
|
||||
|
||||
export default class Channels {
|
||||
constructor(client) {
|
||||
this.client = client;
|
||||
this.channel = undefined;
|
||||
}
|
||||
|
||||
async createChannel(members_id, reload) {
|
||||
|
||||
const response = await this.client._post("/api/chat/", {
|
||||
members_id:members_id
|
||||
});
|
||||
|
||||
if (response.status >= 300)
|
||||
return undefined;
|
||||
|
||||
const data = await response.json();
|
||||
console.log(data)
|
||||
|
||||
this.channel = new Channel(this.client, data.id, members_id, data.messages, reload);
|
||||
}
|
||||
}
|
10
django/frontend/static/js/api/chat/Message.js
Normal file
10
django/frontend/static/js/api/chat/Message.js
Normal file
@ -0,0 +1,10 @@
|
||||
class Message {
|
||||
constructor(channel, author, content, time) {
|
||||
this.channel = channel;
|
||||
this.author = author;
|
||||
this.content = content;
|
||||
this.time = time;
|
||||
}
|
||||
}
|
||||
|
||||
export {Message};
|
Reference in New Issue
Block a user