back to the past bcb072f7d9f5f7817d949837219231232160ff11; Chatte
This commit is contained in:
		@ -1,77 +1,9 @@
 | 
			
		||||
import {Message} from "./message.js"
 | 
			
		||||
 | 
			
		||||
class Channel {
 | 
			
		||||
	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);
 | 
			
		||||
	constructor(id, members, messages) {
 | 
			
		||||
		this.id = id;
 | 
			
		||||
		this.members = members;
 | 
			
		||||
		this.messages = messages;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 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,37 +1,19 @@
 | 
			
		||||
import {Channel} from "./channel.js"
 | 
			
		||||
import {Message} from "./message.js"
 | 
			
		||||
 | 
			
		||||
class Channels {
 | 
			
		||||
	constructor(client) {
 | 
			
		||||
		this.client = client;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	async createChannel(users_id, reload) {
 | 
			
		||||
	async createChannel(users_id) {
 | 
			
		||||
		console.log(users_id);
 | 
			
		||||
		let response = await this.client._post("/api/chat/", {
 | 
			
		||||
			users_id:users_id
 | 
			
		||||
		});
 | 
			
		||||
 | 
			
		||||
		let data = await response.json();
 | 
			
		||||
		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)
 | 
			
		||||
		if (data == "Channel already exist")
 | 
			
		||||
			return null;
 | 
			
		||||
		return data;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export {Channels}
 | 
			
		||||
 | 
			
		||||
@ -1,10 +0,0 @@
 | 
			
		||||
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}
 | 
			
		||||
		Reference in New Issue
	
	Block a user