Skip to content

Commit

Permalink
Merge branch 'dev' into messages
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyNadd committed Dec 12, 2024
2 parents ba95957 + 387f688 commit 55a7fe0
Show file tree
Hide file tree
Showing 22 changed files with 441 additions and 168 deletions.
50 changes: 42 additions & 8 deletions app/Http/Controllers/ChatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace App\Http\Controllers;
use App\Models\Message;
use App\Models\Friendship;
use App\Models\Chat;


use Illuminate\Http\Request;

Expand All @@ -10,24 +13,55 @@ class ChatController extends Controller
public function index()
{
$discussions = auth()->user()->chats()
->with([
'messages' => function ($query) {
$query->latest()->limit(1);
}
])
->latest()
->get();
->with(['messages' => function ($query) {
$query->latest()->limit(1);
}, 'members']) // Assure-toi que la relation members est chargée
->latest()
->get()
->map(function ($discussion) {
// Vérifie si la discussion est un chat individuel (2 membres)
if ($discussion->members->count() == 2) {
// Détermine l'image en fonction des membres
$otherMember = $discussion->members->first()->id == auth()->id()
? $discussion->members->skip(1)->first()
: $discussion->members->first();

// Ajoute l'image sélectionnée comme propriété de la discussion
$discussion->discussionPicture = $otherMember->image
? asset('storage/' . $otherMember->image)
: asset('source/assets/avatar/avatar.png');
} else {
// Utilise une image par défaut pour les groupes
$discussion->discussionPicture = asset('source/assets/images/group.png');
}

return $discussion;
});

// Récupérer les amis acceptés
$friends = Friendship::where(function ($query) {
$query->where('user_id', auth()->id())
->orWhere('friend_id', auth()->id());
})
->where('status', 'accepted')
->with('user', 'friend')
->get()
->map(function ($friendship) {
return $friendship->user_id === auth()->id() ? $friendship->friend : $friendship->user;
});

return view('dashboard', [
'discussions' => $discussions,
'selectedDiscussion' => $discussions->first(),
'friends' => $friends
]);
}

public function getMessages($chatId)
{
$messages = Message::where('chat_id', $chatId)
->orderBy('created_at', 'asc')
->with('user')
->get();

return response()->json(['messages' => $messages]);
Expand All @@ -51,7 +85,7 @@ public function storeCapsule(Request $request, $chatId)
{
// Validation
$request->validate([
'file' => 'required|file|mimes:jpeg,png,jpg,gif,mp3,mp4|max:2048',
'file' => 'required|file|mimes:jpeg,png,jpg,gif,mp3,mp4|max:1024',
'message' => 'required|string',
]);

Expand Down
63 changes: 55 additions & 8 deletions app/Http/Controllers/FriendshipsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ public function create()
public function pending()
{
$pendingRequestsFromFriends = Friendship::where('friend_id', Auth::id())
->where('status', 'pending')
->with('user', 'friend')
->get();
->where('status', 'pending')
->with('user', 'friend')
->get();

$pendingRequestsFromUser = Friendship::where('user_id', Auth::id())
->where('status', 'pending')
->with(['user', 'friend'])
->get();
->where('status', 'pending')
->with(['user', 'friend'])
->get();

$pendingRequests = $pendingRequestsFromFriends->merge($pendingRequestsFromUser);

Expand Down Expand Up @@ -74,7 +74,7 @@ public function store(Request $request)
})->exists();

if ($friendshipExists) {
return back()->with('error', 'You are already friends or a request is pending.');
return back()->with('error', 'You are already friends, blocked or a request is pending.');
}

Friendship::create([
Expand Down Expand Up @@ -111,7 +111,54 @@ public function decline(Request $request, $friendship_id)
return back()->with('error', 'Could not decline the friend request.');
}

public function removeFriend(Request $request)
public function destroy($friend)
{
$friendship = Friendship::where(function ($query) use ($friend) {
$query->where('user_id', Auth::id())
->where('friend_id', $friend);
})->orWhere(function ($query) use ($friend) {
$query->where('user_id', $friend)
->where('friend_id', Auth::id());
})->first();

if ($friendship) {
$friendship->delete();
return back()->with('success', 'Friend removed successfully!');
}

return back()->with('error', 'Could not find the friend to remove.');
}

public function block($friend)
{
$friend = User::findOrFail($friend);

if ($friend->id === Auth::id()) {
return back()->with('error', 'You cannot block yourself.');
}

$friendship = Friendship::where(function ($query) use ($friend) {
$query->where('user_id', auth()->id())
->where('friend_id', $friend->id);
})->orWhere(function ($query) use ($friend) {
$query->where('user_id', $friend->id)
->where('friend_id', auth()->id());
})->first();

if ($friendship) {
if ($friendship->status === 'blocked') {
return back()->with('error', 'This user is already blocked.');
}
$friendship->update(['status' => 'blocked']);
return back()->with('success', 'User blocked successfully!');
} else {
// Create a new friendship with 'blocked' status if no existing relation is found
Friendship::create([
'user_id' => auth()->id(),
'friend_id' => $friend->id,
'status' => 'blocked',
]);
return back()->with('success', 'User blocked successfully!');
}
}
}
38 changes: 29 additions & 9 deletions app/Http/Controllers/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Illuminate\Support\Facades\Redirect;
use Illuminate\View\View;

use Illuminate\Support\Facades\Storage;

class ProfileController extends Controller
{
/**
Expand All @@ -24,18 +26,36 @@ public function edit(Request $request): View
/**
* Update the user's profile information.
*/
public function update(ProfileUpdateRequest $request): RedirectResponse
{
$request->user()->fill($request->validated());

if ($request->user()->isDirty('email')) {
$request->user()->email_verified_at = null;
}
public function update(ProfileUpdateRequest $request): RedirectResponse
{
$validated = $request->validated();

if ($request->hasFile('image')) {
$user = $request->user();

$request->user()->save();
if ($user->image) {
Storage::disk('public')->delete($user->image);
}

$path = $request->file('image')->store('avatar', 'public');
$validated['image'] = $path;
} else {
unset($validated['image']);
}

$request->user()->fill($validated);

if ($request->user()->isDirty('email')) {
$request->user()->email_verified_at = null;
}

$request->user()->save();

return Redirect::route('profile.edit')->with('status', 'profile-updated');
}


return Redirect::route('profile.edit')->with('status', 'profile-updated');
}

/**
* Delete the user's account.
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Requests/ProfileUpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ public function rules(): array
'lowercase',
'email',
'max:255',

Rule::unique(User::class)->ignore($this->user()->id),
],
'image' => ['nullable', 'image', 'mimes:jpeg,png,jpg', 'max:2048'],
];
}
}
5 changes: 5 additions & 0 deletions app/Models/Chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public function users()
return $this->belongsToMany(User::class, 'user_chat');
}

public function members()
{
return $this->users();
}

/**
* Messages in the chat
*
Expand Down
7 changes: 1 addition & 6 deletions app/Models/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@ public function chats()
return $this->belongsTo(Chat::class);
}

/**
* User who sent the message
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function users()
public function user() // Utiliser 'user()' au lieu de 'users()'
{
return $this->belongsTo(User::class);
}
Expand Down
1 change: 1 addition & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class User extends Authenticatable
protected $fillable = [
'name',
'email',
'image',
'password',
];

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
//
$table->string('image')->nullable();

});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
//
$table->dropColumn('image');
});
}
};
Binary file added public/source/assets/avatar/avatar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/source/assets/images/block_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/source/assets/images/group.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions resources/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,10 @@
.messagesBackground {
background-image: url('../../public/source/assets/images/messageBackground.png');
}

.avatar {
vertical-align: middle;
width: 50px;
height: 50px;
border-radius: 50%;
}
4 changes: 2 additions & 2 deletions resources/js/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import './bootstrap';
import "./bootstrap";

import Alpine from 'alpinejs';
import Alpine from "alpinejs";

window.Alpine = Alpine;

Expand Down
Loading

0 comments on commit 55a7fe0

Please sign in to comment.