fix friend and block requests and replace dummy in New conversation

This commit is contained in:
kinou-p 2023-06-14 17:54:06 +02:00
parent f678d37739
commit c8a26fd297
6 changed files with 112 additions and 28 deletions

View File

@ -60,6 +60,13 @@ export class AppController {
return await this.userService.findOne(data.username); return await this.userService.findOne(data.username);
} }
@UseGuards(JwtAuthGuard)
@Get('/users')
async getUsers( @Body() data: any) {
console.log(`usernamewwww= ${data.username}`)
return await this.userService.findAll();
}
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get('/friends') @Get('/friends')
async getFriends(@Request() req) { async getFriends(@Request() req) {
@ -68,11 +75,13 @@ export class AppController {
} }
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Post('/friend') @Post('/friend')//need to do it 2 time when user accept one for each
async newFriend(@Request() req, @Body() data: any) { async newFriend(@Request() req, @Body() data: any) {
// return await this.userService.getFriends(req.user.username); // return await this.userService.getFriends(req.user.username);
console.log(`user= ${req.user.username}`) console.log(`user= ${req.user.username}`)
const user = await this.userService.findOne(req.user.username) const user = await this.userService.findOne(req.user.username)
if (!user)
return (0);
return await this.userService.addFriend(user, data.username); return await this.userService.addFriend(user, data.username);
} }
@ -86,7 +95,17 @@ export class AppController {
} }
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get('/invite') @Post('/invite')
async newInvite(@Request() req, @Body() data: any) {
console.log(`user= ${req.user.username}`)
const user = await this.userService.findOne(data.username)
if (!user)
return (0);
return await this.userService.newInvite(user, req.user.username);
}
@UseGuards(JwtAuthGuard)
@Get('/inviteRequest')
async getInvite(@Request() req) { async getInvite(@Request() req) {
// return await this.userService.getFriends(req.user.username); // return await this.userService.getFriends(req.user.username);
console.log(`useawdawd\n\n\nr= ${req.user.username}`) console.log(`useawdawd\n\n\nr= ${req.user.username}`)
@ -94,6 +113,15 @@ export class AppController {
return await this.userService.getInvite(req.user.username); return await this.userService.getInvite(req.user.username);
} }
@UseGuards(JwtAuthGuard)
@Post('/refuseInvite')
async refuseInvite(@Request() req, @Body() data: any) {
// return await this.userService.getFriends(req.user.username);
// console.log(`useawdawd\n\n\nr= ${req.user.username}`)
const user = await this.userService.findOne(req.user.username)
return await this.userService.refuseInvite(user, data.username);
}
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Post('/status') @Post('/status')
async setStatus(@Request() req, @Body() data: any) { async setStatus(@Request() req, @Body() data: any) {

View File

@ -49,6 +49,20 @@ export class UsersService {
return (friends) return (friends)
} }
async newInvite(user: User, username: string) {
if (!(await this.findOne(username)))
return (0);
// user.friendRequest = user.friendRequest || [];
// console.log("newInvite")
// console.log(user.friendRequest)
user.friendRequest = user.friendRequest || [];
if (user.friendRequest.find(item => item === username))
return (1);
user.friendRequest.push(username);
this.save(user);
return (1);
}
async getInvite(username: string) { async getInvite(username: string) {
const user = await this.findOne(username) const user = await this.findOne(username)
let friendsTab = user.friendRequest let friendsTab = user.friendRequest
@ -61,6 +75,11 @@ export class UsersService {
return (friends) return (friends)
} }
async refuseInvite(user: User, username: string) {
user.friendRequest = user.friendRequest.filter((item) => item !== username);
this.save(user);
}
async getHistory(username: string) { async getHistory(username: string) {
const user = await this.findOne(username); const user = await this.findOne(username);
@ -69,20 +88,22 @@ export class UsersService {
console.log(user); console.log(user);
console.log(user.children); // or perform any operations with the children console.log(user.children); // or perform any operations with the children
return children; return children;
// You can also access specific properties of each child
// children.forEach((child) => {
// console.log(child.id);
// console.log(child.opponent);
// // Access other child properties as needed
// });
} }
} }
async addFriend(user: User, username: string) { async addFriend(user: User, username: string) {
if (!(await this.findOne(username))) if (!(await this.findOne(username)))
return (0); return (0);
// user.friendRequest = user.friendRequest || [];
user.friends = user.friends || []; user.friends = user.friends || [];
if (user.friends.find(item => item === username))
{
user.friendRequest = user.friendRequest.filter((item) => item !== username);
this.save(user);
return (1);
}
user.friends.push(username); user.friends.push(username);
user.friendRequest = user.friendRequest.filter((item) => item !== username);
this.save(user); this.save(user);
return (1); return (1);
} }
@ -91,6 +112,8 @@ export class UsersService {
if (!(await this.findOne(username))) if (!(await this.findOne(username)))
return (0); return (0);
user.blocked = user.blocked || []; user.blocked = user.blocked || [];
if (user.blocked.find(item => item === username))
return (1);
user.blocked.push(username); user.blocked.push(username);
this.save(user); this.save(user);
return (1); return (1);
@ -103,10 +126,6 @@ export class UsersService {
async getPic( username: string) { async getPic( username: string) {
// const user = await this.findOne(username); // const user = await this.findOne(username);
let result = await this.userRepository.query("select encode(photo, 'base64') FROM public.\"User\" WHERE username = $1;", [username]); let result = await this.userRepository.query("select encode(photo, 'base64') FROM public.\"User\" WHERE username = $1;", [username]);
// console.log(`result= ${result}`)
// console.log(`result= ${result.text}`)
// console.log(`encode= ${result.encode}`)
// console.log(`encode= ${result.string}`)
if (result.length > 0) { if (result.length > 0) {
const encodedPhoto = result[0].encode; const encodedPhoto = result[0].encode;
console.log(`pic!!! =`) console.log(`pic!!! =`)

View File

@ -248,7 +248,7 @@ function Chats(){
const handleAddFriend = async () => { const handleAddFriend = async () => {
try{ try{
const res = await api.post("/friend", {username: friend}) const res = await api.post("/invite", {username: friend})
// if (res.data === 1) // if (res.data === 1)
// console.log("res in friend= ", res) // console.log("res in friend= ", res)
console.log("res in friend= ", res.data) console.log("res in friend= ", res.data)
@ -368,7 +368,7 @@ function Chats(){
</motion.div> </motion.div>
<AnimatePresence initial={false} onExitComplete={() => null}> <AnimatePresence initial={false} onExitComplete={() => null}>
{showAddFriendAlert && addFriend && ( {showAddFriendAlert && addFriend && (
<GreenAlert handleClose={closeAddFriend} text={friend + ' was successfully added'} /> <GreenAlert handleClose={closeAddFriend} text={ 'invitation sent to ' + friend} />
)} )}
{showAddFriendAlert && !addFriend && ( {showAddFriendAlert && !addFriend && (
<RedAlert handleClose={closeAddFriend} text={friend + ' was not found'} /> <RedAlert handleClose={closeAddFriend} text={friend + ' was not found'} />

View File

@ -1,8 +1,8 @@
import { motion } from "framer-motion"; import { motion } from "framer-motion";
import Backdrop from "../Sidebar/Backdrop"; import Backdrop from "../Sidebar/Backdrop";
import { Rank } from "../../DataBase/DataRank" // import { Rank } from "../../DataBase/DataRank"
import '../../styles/Messages.css' import '../../styles/Messages.css'
import { useState } from "react"; import { useState, useEffect } from "react";
import { GrAdd } from "react-icons/gr"; import { GrAdd } from "react-icons/gr";
import { Link } from "react-router-dom"; import { Link } from "react-router-dom";
import api from "../../script/axiosApi"; import api from "../../script/axiosApi";
@ -23,10 +23,25 @@ const dropIn = {
}; };
const Modal = ({handleClose, text}) => { const Modal = ({handleClose}) => {
// const [multi, setMulti] = useState(false); // const [multi, setMulti] = useState(false);
const [selectTags, setSelectTag] = useState([{ id: 1, selectedOption: ''}]); const [selectTags, setSelectTag] = useState([{ id: 1, selectedOption: ''}]);
const [selectedOptionArray, setSelectedOptionArray] = useState([]); const [selectedOptionArray, setSelectedOptionArray] = useState([]);
const [users, setUsers] = useState([]);
useEffect(()=> {
const getConv = async ()=>{
try {
const tmpUsers = await api.get("/users");
console.log("users=", tmpUsers.data);
setUsers(tmpUsers.data);
} catch(err){
console.log(err)
}
}
getConv();
}, []);
const handleOptionChange = (selectId, selectedOption) => { const handleOptionChange = (selectId, selectedOption) => {
console.log("selected Option=", selectedOption) console.log("selected Option=", selectedOption)
@ -62,7 +77,8 @@ const Modal = ({handleClose, text}) => {
setSelectedOptionArray(selectedOptions); setSelectedOptionArray(selectedOptions);
} }
let new_name; // let new_name;
return ( return (
<Backdrop> <Backdrop>
<motion.div <motion.div
@ -84,9 +100,9 @@ const Modal = ({handleClose, text}) => {
<option value="">{ <option value="">{
selectTag.selectedOption ? selectTag.selectedOption : "Select an option" selectTag.selectedOption ? selectTag.selectedOption : "Select an option"
}</option> }</option>
{Rank.filter((item) => !selectTags.some((tag) => tag.selectedOption === item.name)).map((item, index) => ( {users.filter((item) => !selectTags.some((tag) => tag.selectedOption === item.name)).map((item, index) => (
<option key={index} value={item.name}> <option key={index} value={item.username}>
{item.name} {item.username}
</option> </option>
))} ))}
</select> </select>

View File

@ -28,14 +28,18 @@ const SideP = styled.p`
export default function Friend({currentUser}) export default function Friend({currentUser})
{ {
const [profilePicture, setProfilePicture] = useState(''); const [profilePicture, setProfilePicture] = useState('');
const [request, setRequest] = useState(''); const [request, setRequest] = useState(''); //user who invite
const [clickEvent, setClickEvent] = useState(false);
// const [user, setUser] = useState(null);
useEffect(() => { useEffect(() => {
const fetchProfilePicture = async () => { const fetchProfilePicture = async () => {
try { try {
// const user = await api.get("/profile"); // const user = await api.get("/profile");\
// const tmpUser = await api.get("/profile")
const pic = await api.post("/getPicture", {username: currentUser.username}) const pic = await api.post("/getPicture", {username: currentUser.username})
const tmpRequest = await api.post("/user", {username: currentUser.username}) const tmpRequest = await api.post("/user", {username: currentUser.username})
// setUser(tmpUser.data);
setRequest(tmpRequest.data); setRequest(tmpRequest.data);
// console.log(`user naem profile pic222= ${currentUser.username}`) // console.log(`user naem profile pic222= ${currentUser.username}`)
// console.log(` profile pic222= ${pic.data}`) // console.log(` profile pic222= ${pic.data}`)
@ -46,7 +50,7 @@ export default function Friend({currentUser})
}; };
fetchProfilePicture(); fetchProfilePicture();
}, []) }, [clickEvent])
const handleButtonClick = (user) => { const handleButtonClick = (user) => {
let path = `http://localhost/profile/${user.username}`; let path = `http://localhost/profile/${user.username}`;
@ -56,16 +60,33 @@ export default function Friend({currentUser})
window.location.reload(false); window.location.reload(false);
}; };
const Accept = (user) => { const Accept = async (request) => {
try{
await api.post("/friend", {username: request.username})
setClickEvent(true);
} catch(err) {
console.log(err);
}
console.log("accept") console.log("accept")
console.log(`request = ${request}`) console.log(`request = ${request}`)
} }
const Refuse = (user) => { const Refuse = async (request) => {
try{
await api.post("/refuseInvite", {username: request.username})
setClickEvent(true);
} catch(err) {
console.log(err);
}
console.log("refuse") console.log("refuse")
console.log(`request = ${request}`) console.log(`request = ${request}`)
} }
// Vérifier si le contenu doit être caché
if (clickEvent) {
return null; // Rendre null pour ne pas afficher le contenu
}
return ( return (
<UserChat> <UserChat>
{profilePicture ? ( {profilePicture ? (

View File

@ -44,7 +44,7 @@ function Social (){
try{ try{
const tmpFriends = await api.get("/friends") const tmpFriends = await api.get("/friends")
const tmpUser = await api.get("/profile") const tmpUser = await api.get("/profile")
const tmpInv = await api.get("/invite") const tmpInv = await api.get("/inviteRequest")
const pic = await api.post("/getPicture", {username: tmpUser.data.username}) const pic = await api.post("/getPicture", {username: tmpUser.data.username})
setInvite(tmpInv.data); setInvite(tmpInv.data);