From 47fbd4fbb72cfc0877078477084fc0f5f2932299 Mon Sep 17 00:00:00 2001 From: uniknow <000-drakon@mail.ru> Date: Fri, 4 Oct 2024 17:39:03 +0400 Subject: [PATCH] =?UTF-8?q?=D1=83=D0=B1=D1=80=D0=B0=D0=BB=20=D0=BB=D0=B8?= =?UTF-8?q?=D1=88=D0=BD=D1=8E=D1=8E=20=D0=BF=D0=B0=D0=BF=D0=BA=D1=83=20lib?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker/lib/chat.js | 232 ------------------------------------- docker/lib/login.js | 35 ------ docker/lib/register.js | 94 --------------- docker/lib/settings.js | 59 ---------- docker/lib/userFunction.js | 30 ----- docker/lib/websocket.js | 40 ------- 6 files changed, 490 deletions(-) delete mode 100644 docker/lib/chat.js delete mode 100644 docker/lib/login.js delete mode 100644 docker/lib/register.js delete mode 100644 docker/lib/settings.js delete mode 100644 docker/lib/userFunction.js delete mode 100644 docker/lib/websocket.js diff --git a/docker/lib/chat.js b/docker/lib/chat.js deleted file mode 100644 index b7d643c..0000000 --- a/docker/lib/chat.js +++ /dev/null @@ -1,232 +0,0 @@ -import { PUBLIC_URL } from '$env/static/public'; - -export async function getLastMessages(chatId,msgLoaded){ - - let token = localStorage.getItem('BPChat') - const response = await fetch(`${PUBLIC_URL}/api/chat/get_some_messages/${chatId}?messages_loaded=${msgLoaded}&messages_to_get=15`, - { - method:'GET', - credentials:'include', - headers: {'accept': 'application/json', - 'Authorization': token, - }, - }) - - if(response.ok){ - let data = await response.json() - let msgMassive = data.messages - console.log(msgMassive, " аа сообщения", chatId, " - айди") - - if(msgMassive === null){ - return [] - } else { - return msgMassive - } - } - - else if(response.status === 404){ - console.log(response) - } - - else if(!response.ok) - console.log(response.status) - - } - - export async function getMessageById(chatId,msgId){ - - let token = localStorage.getItem('BPChat') - const response = await fetch(`${PUBLIC_URL}/chat/message/${chatId}?message_id=${msgId}`,{ - method:"GET", - credentials:'include', - headers: {'Authorization': token }, - - }) - - if(response.ok){ - let data = await response.json(); - return data; - } - else{ - return { message: "Сообщение не найдено" }; - } - - } - - export async function MessagePicToUrl(messagePic){ - - let token = localStorage.getItem('BPChat') - - const DataForm = new FormData(); - DataForm.append('file', messagePic) - - const response = await fetch('${PUBLIC_URL}/api/images/upload_image', - { - method:"POST", - body:DataForm, - headers: {'Authorization': token }, - }) - - if(!response.ok) - console.log("ошибка", response.status) - - - if(response.ok){ - const data = await response.json(); - console.log("картинка принята") - return data.image_url; - - } - } - - export async function getAllChats(){ - - let token = localStorage.getItem('BPChat') - const response = await fetch(`${PUBLIC_URL}/api/chat`, - { - method:"GET", - credentials:'include', - headers: {'Authorization': token }, - - }) - if(response.ok){ - let data = await response.json(); - data = data.allowed_chats - console.log(data) - - if(data == null) - return [] - else - return data - - } else if(response.status == 409){ - return false //если пользователь не подтвердил почту - } else { - console.log(response) - } - } - - export async function getPinnedMsg(ID){ - - let token = localStorage.getItem('BPChat') - const response = await fetch(`${PUBLIC_URL}/api/chat/pinned_messages/${ID}`,{ - method:"GET", - credentials:'include', - headers: {'Authorization': token }, - }) - - if(response.ok){ - const data = await response.json() - return data.pinned_messages - } else { - console.log(response.status) - } - } - - export async function pinMessage(chatId,messageId){ - - let token = localStorage.getItem('BPChat') - const response = await fetch(`${PUBLIC_URL}/api/chat/pinn_message?chat_id=${chatId}&message_id=${messageId}`,{ - method:"POST", - credentials:'include', - headers: {'Authorization': token }, - }) - if(response.ok){ - const data = await response.json() - return data.pinned_message - } else - console.log(response.status) - - } - - export async function unpinMessage(chatId, messageId){ - - let token = localStorage.getItem('BPChat') - const response = await fetch(`${PUBLIC_URL}/api/chat/unpinn_message?chat_id=${chatId}&message_id=${messageId}`,{ - method:"DELETE", - credentials:'include', - headers: {'Authorization': token }, - }) - if(response.ok){ - return await response.json(); - } - else - console.log(response.status) - - } - - export async function uploadImages(image) { - - let token = localStorage.getItem('BPChat') - const formData = new FormData(); - formData.append('file', image); - - const response = await fetch(`${PUBLIC_URL}/api/images/upload_image`, { - method: 'POST', - credentials: 'include', - body: formData, - headers: {'Authorization': token }, - }); - - if (response.ok) { - let data = await response.json(); - return data.image_url - } else { - console.log(response); - } - } - - export async function getAllUsers(nameToFind){ - - const response = await fetch(`${PUBLIC_URL}/api/users?username=${nameToFind}`,{ - method:"GET", - credentials:'include' - }) - - if(response.ok){ - let data = await response.json() - let anotherData = data.users - - if(anotherData == null){ - return [] - } - return data.users - } - - } - - export async function createNewChat(chatName, excludeUser, token){ - console.log(token, "token") - const response = await fetch(`${PUBLIC_URL}/api/chat/create_chat?user_to_exclude=${excludeUser}&chat_name=${chatName}`,{ - method: 'POST', - credentials: 'include', - headers: {'Authorization': token }, - }) - - if(response.ok){ - let data = await response.json() - let id = data.chat_id - return id - } - else{ - console.log(response) - } - - } - - export async function deleteChat(chatId, token){ - - const response = await fetch(`${PUBLIC_URL}/api/chat/delete_chat/${chatId}`,{ - method: 'DELETE', - credentials: 'include', - headers: {'Authorization': token }, - }) - - if(response.ok){ - return "Чат удален" - } - - else{ - console.log(response) - } - } \ No newline at end of file diff --git a/docker/lib/login.js b/docker/lib/login.js deleted file mode 100644 index 614268c..0000000 --- a/docker/lib/login.js +++ /dev/null @@ -1,35 +0,0 @@ -import { PUBLIC_URL } from '$env/static/public'; - -export async function handleLogin(username, password) { - - const response = await fetch(`${PUBLIC_URL}/api/users/login`, { - method: 'POST', - credentials:'include', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify - ({ - email_or_username: username, - password: password - }) - - }) - - if(response.status === 200) - { - console.log("залогинен") - - const data = await response.json() - const token = data.authorization - localStorage.setItem('BPChat', token) - window.location.href = '/chat' - return "" // чтобы ничего не выводилось в качестве ошибки - } - else if(response.status === 401) - { - return "Неправильный логин или пароль" - } else { - console.log(response) - } - } \ No newline at end of file diff --git a/docker/lib/register.js b/docker/lib/register.js deleted file mode 100644 index d1ca8ab..0000000 --- a/docker/lib/register.js +++ /dev/null @@ -1,94 +0,0 @@ -import { PUBLIC_URL } from '$env/static/public'; - -export async function checkExsistingUser(username, email) { - const response = await fetch(`${PUBLIC_URL}/api/users/check_existing_user`, { - method:'POST', - credentials:"include", - headers:{ - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - "username": username, - "email": email - }) - }) - if(response.status === 200){ - return "" - } - else if(response.status === 409){ - let data = "ЗАНЯТО НАХУЙ" - return data - } - else if(response.status === 422){ - let data = await response.json(); - return data - } - else{ - console.log(response.status) - } -} - -export async function VerificationEmail(token, Code){ - const response = await fetch(`${PUBLIC_URL}/api/users/email_verification`,{ - method:'POST', - credentials:'include', - headers:{ - 'Content-Type' : 'application/json', - 'Authorization': token - }, - body: JSON.stringify({ - "user_code": Code - }) - }) - - if(response.status === 200){ - window.location.href = '/chat' - return true - } - else{ - return false - } -} - -export async function resendVerification(token){ - const response = await fetch(`${PUBLIC_URL}/api/users/resend_email_verification`,{ - method:'POST', - credentials:'include', - headers: {'Authorization': token } - }) - - if(response.ok){ - return "Отправлено" - } else { - console.log(response) - return "Произошла ошибка" - } -} - - export async function handleRegister(username,password,password2,email,date_of_birth){ - const response = await fetch(`${PUBLIC_URL}/api/users/register`,{ - method:'POST', - credentials:"include", - headers:{ 'Content-Type': 'application/json' }, - body: JSON.stringify({ - "email": email, - "username": username, - "password": password, - "password2": password2, - "date_of_birth": date_of_birth - }) - }) - - if(response.status === 201){ - const data = await response.json(); - const token = data.authorization - localStorage.setItem('BPChat', token) - return "" - } - else if(response.status === 422){ - console.log(response.status) - let data = "Validation Error" - return data - } - - } \ No newline at end of file diff --git a/docker/lib/settings.js b/docker/lib/settings.js deleted file mode 100644 index e2fcf9e..0000000 --- a/docker/lib/settings.js +++ /dev/null @@ -1,59 +0,0 @@ -import { PUBLIC_URL } from '$env/static/public'; - -export async function getAvatarHistory(token){ - - let response = await fetch(`${PUBLIC_URL}/api/users/avatars`,{ - method: 'GET', - credentials:'include', - headers: {'Authorization': token }, - }) - - if(response.ok){ - let data = await response.json(); - data = data.user_avatars - data.reverse(); - return data; - } else { - console.log(response) - } -} - -export async function getConfirmationCode(token, email){ - console.log(token, email, "<-- лох") - const response = await fetch(`${PUBLIC_URL}/api/users/send_confirmation_code`,{ - method: 'POST', - credentials:'include', - headers: { 'Content-Type': 'application/json', - 'Authorization': token }, - body: JSON.stringify({'email': email}) - }) - - if(response.ok){ - return true - } else { - console.log(response) - } -} - -export async function changeUserData(token, username, email, password, avatar, code){ - let response = await fetch(`${PUBLIC_URL}/api/users/change_data`,{ - - method: 'POST', - credentials:'include', - headers: {'Content-Type': 'application/json', - 'Authorization': token }, - body: JSON.stringify({"verification_code": code, - "email": email, - "username": username, - "new_password": password, - "avatar_url": avatar}) - }) - - if(response.ok){ - return true - } else if (response.status == 409){ - - } else { - console.log(response) - } -} \ No newline at end of file diff --git a/docker/lib/userFunction.js b/docker/lib/userFunction.js deleted file mode 100644 index b03b5e9..0000000 --- a/docker/lib/userFunction.js +++ /dev/null @@ -1,30 +0,0 @@ -import { PUBLIC_URL } from '$env/static/public'; - -export async function UserCheck(){ - - let token = localStorage.getItem('BPChat') - const response = await fetch(`${PUBLIC_URL}/api/users/me`, { - method: 'GET', - credentials:'include', - headers: {'Authorization': token }, - }) - - if(response.ok){ - const data = await response.json(); - return data - } - - else{ - console.log(response) - location.assign('/login') - } - } - -export async function handleLogout() { - - localStorage.removeItem('BPChat') - location.assign('/login') - -} - - \ No newline at end of file diff --git a/docker/lib/websocket.js b/docker/lib/websocket.js deleted file mode 100644 index 41d2a05..0000000 --- a/docker/lib/websocket.js +++ /dev/null @@ -1,40 +0,0 @@ -export default function createWebSocket(url, token, onMessageCallback) { - let socket; - let retries = 0; - const maxRetries = 5 - let messageQueue = []; - - function connect() { - - socket = new WebSocket(url, [token]); - - socket.addEventListener('message', (event) => { - const jsonData = JSON.parse(event.data) - onMessageCallback(jsonData) - }); - - socket.onopen = () => { - console.log('WebSocket is open now.') - console.log(socket) - retries = 0 //сброс попыток - - while (messageQueue.length > 0) { - socket.send(messageQueue.shift()); - } - }; - - socket.onclose = (event) => { - console.log('WebSocket is closed now.', event) - }; - - socket.onerror = (error) => { - console.error('WebSocket error observed:', error) - }; - - - } - - connect() - - return socket -}