переработка для новой бд
This commit is contained in:
parent
039496c199
commit
9569071a2a
13 changed files with 1868 additions and 32 deletions
232
docker/lib/chat.js
Normal file
232
docker/lib/chat.js
Normal file
|
@ -0,0 +1,232 @@
|
|||
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)
|
||||
}
|
||||
}
|
35
docker/lib/login.js
Normal file
35
docker/lib/login.js
Normal file
|
@ -0,0 +1,35 @@
|
|||
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)
|
||||
}
|
||||
}
|
94
docker/lib/register.js
Normal file
94
docker/lib/register.js
Normal file
|
@ -0,0 +1,94 @@
|
|||
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
|
||||
}
|
||||
|
||||
}
|
59
docker/lib/settings.js
Normal file
59
docker/lib/settings.js
Normal file
|
@ -0,0 +1,59 @@
|
|||
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)
|
||||
}
|
||||
}
|
30
docker/lib/userFunction.js
Normal file
30
docker/lib/userFunction.js
Normal file
|
@ -0,0 +1,30 @@
|
|||
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')
|
||||
|
||||
}
|
||||
|
||||
|
40
docker/lib/websocket.js
Normal file
40
docker/lib/websocket.js
Normal file
|
@ -0,0 +1,40 @@
|
|||
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
|
||||
}
|
|
@ -79,9 +79,8 @@ export async function getLastMessages(chatId,msgLoaded){
|
|||
}
|
||||
}
|
||||
|
||||
export async function getAllChats(){
|
||||
export async function getAllChats(token){
|
||||
|
||||
let token = localStorage.getItem('BPChat')
|
||||
const response = await fetch(`${PUBLIC_URL}/api/chat`,
|
||||
{
|
||||
method:"GET",
|
||||
|
@ -116,8 +115,13 @@ export async function getLastMessages(chatId,msgLoaded){
|
|||
})
|
||||
|
||||
if(response.ok){
|
||||
const data = await response.json()
|
||||
return data.pinned_messages
|
||||
let data = await response.json()
|
||||
data = data.pinned_messages
|
||||
if(data === null){
|
||||
return []
|
||||
} else {
|
||||
return data
|
||||
}
|
||||
} else {
|
||||
console.log(response.status)
|
||||
}
|
||||
|
@ -196,7 +200,6 @@ export async function getLastMessages(chatId,msgLoaded){
|
|||
}
|
||||
|
||||
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',
|
||||
|
|
|
@ -23,7 +23,7 @@ export async function handleLogin(username, password) {
|
|||
const data = await response.json()
|
||||
const token = data.authorization
|
||||
localStorage.setItem('BPChat', token)
|
||||
window.location.href = '/chat'
|
||||
window.location.href = '/'
|
||||
return "" // чтобы ничего не выводилось в качестве ошибки
|
||||
}
|
||||
else if(response.status === 401)
|
||||
|
|
|
@ -28,7 +28,7 @@ export async function checkExsistingUser(username, email) {
|
|||
}
|
||||
}
|
||||
|
||||
export async function VerificationEmail(token, Code){
|
||||
export async function VerificationEmail(Code,token){
|
||||
const response = await fetch(`${PUBLIC_URL}/api/users/email_verification`,{
|
||||
method:'POST',
|
||||
credentials:'include',
|
||||
|
@ -42,7 +42,7 @@ export async function VerificationEmail(token, Code){
|
|||
})
|
||||
|
||||
if(response.status === 200){
|
||||
window.location.href = '/chat'
|
||||
window.location.href = '/'
|
||||
return true
|
||||
}
|
||||
else{
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -24,7 +24,7 @@
|
|||
</script>
|
||||
|
||||
<header>
|
||||
<a href="chat" class="avatarDiv">
|
||||
<a href="/" class="avatarDiv">
|
||||
<img class="siteAvatar" src="./image/BP-NEON.png" alt="лого" />
|
||||
<h2>BP Chat</h2>
|
||||
</a>
|
||||
|
|
|
@ -48,13 +48,16 @@
|
|||
|
||||
onMount(async () => {
|
||||
|
||||
token = localStorage.getItem('BPChat')
|
||||
|
||||
const userData = await UserCheck();
|
||||
userId = userData.id;
|
||||
|
||||
window.addEventListener('keydown', onEnterPress);
|
||||
|
||||
chats = await getAllChats()
|
||||
if(chats == false){
|
||||
chats = await getAllChats(token)
|
||||
console.log([] == false)
|
||||
if(chats === false){
|
||||
chats = []
|
||||
warningMessage = "чтобы пользоваться чатом, подтверди почту"
|
||||
}
|
||||
|
@ -63,14 +66,15 @@
|
|||
|
||||
async function handleChatUrlChange() {
|
||||
if (window.location.hash.startsWith('#id=')) {
|
||||
|
||||
console.log(location.hash.substring(1), "FFFFFFFFFFFFFFFFFFFFFFFFFFFF")
|
||||
chatId = window.location.hash.slice(4);
|
||||
msgLoaded = 0
|
||||
//console.log(chatId, " chatId")
|
||||
|
||||
currentChat = chats.find(chat => chat.chat_id == chatId)
|
||||
|
||||
if(currentChat == undefined){
|
||||
window.location.href = '/chat'
|
||||
window.location.href = '/'
|
||||
}
|
||||
//console.log(currentChat, " текущий чат")
|
||||
pickedChatName = currentChat.chat_name
|
||||
|
@ -80,7 +84,6 @@
|
|||
msgLoaded += 15
|
||||
pinnedMsg = await getPinnedMsg(chatId)
|
||||
|
||||
token = localStorage.getItem('BPChat')
|
||||
const websocketUrl = `${PUBLIC_WSS}/api/chat/ws/${chatId}`
|
||||
|
||||
if (socket){
|
||||
|
@ -161,6 +164,8 @@
|
|||
answerMessage = ""
|
||||
answerMessageImg = ""
|
||||
|
||||
adjustTextareaHeight()
|
||||
|
||||
} else {
|
||||
|
||||
console.log("ноуп")
|
||||
|
@ -187,8 +192,8 @@
|
|||
//console.log(textarea.scrollHeight)
|
||||
if (textarea) {
|
||||
textarea.style.height = '1em'; //??
|
||||
textarea.style.height = (textarea.scrollHeight > maxRows * 18) ? `${maxRows * 18}px` : `${textarea.scrollHeight}px`; // Limit height based on maxRows and font size
|
||||
//console.log(textarea.scrollHeight)
|
||||
textarea.style.height = (textarea.scrollHeight > maxRows * 18) ? `${maxRows * 18}px` : `${textarea.scrollHeight}px`;
|
||||
console.log(textarea.scrollHeight)
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -377,12 +382,13 @@ let pinOrNot = false
|
|||
async function submtiCreateNewChat(){
|
||||
token = localStorage.getItem('BPChat')
|
||||
let newChatId = await createNewChat(newChatName, selectedUserId, token)
|
||||
window.location.href = `/#id=${newChatId}`
|
||||
|
||||
createNewChatShow = false
|
||||
//createNewChatShow = false
|
||||
|
||||
history.pushState(null, null, `/chat#id=${newChatId}`);
|
||||
//history.pushState(null, null, `/#id=${newChatId}`);
|
||||
//handleRouteChange();
|
||||
chats = await getAllChats()
|
||||
//chats = await getAllChats()
|
||||
}
|
||||
|
||||
function cancelCreateNewChat(){
|
||||
|
@ -998,6 +1004,7 @@ let pinOrNot = false
|
|||
width: 90%;
|
||||
margin-bottom: 10px;
|
||||
text-align: center;
|
||||
word-break: break-all;
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -108,7 +108,7 @@
|
|||
code = event.target.value
|
||||
|
||||
if(code.length == 6){
|
||||
let verfcode = await VerificationEmail(code)
|
||||
let verfcode = await VerificationEmail(code, token)
|
||||
if(verfcode == false)
|
||||
codeError = "Неправильный код"
|
||||
}
|
||||
|
@ -309,7 +309,7 @@
|
|||
.mainDivBack::before{
|
||||
content: '';
|
||||
position: absolute;
|
||||
background-image: url('./noise.gif');
|
||||
background-image: url('/image/noise.gif');
|
||||
background-repeat: repeat;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
|
|
Loading…
Add table
Reference in a new issue