Skip to content

Commit

Permalink
Comments + augments profile picture file size
Browse files Browse the repository at this point in the history
  • Loading branch information
Saodus committed Dec 17, 2024
1 parent fa3d5c1 commit 11bd3a1
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 7 deletions.
8 changes: 6 additions & 2 deletions app/Http/Controllers/ChatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public function leaveChat($chatId)
* @return \Illuminate\Http\JsonResponse A JSON response with a message (or an error).
*/
public function deleteMessage($discussionId, $messageId)
{
{
$message = Message::where('id', $messageId)->where('chat_id', $discussionId)->first();

if (!$message) {
Expand All @@ -273,8 +273,10 @@ public function deleteMessage($discussionId, $messageId)
if ($message->user_id !== auth()->id()) {
return response()->json(['error' => 'Unauthorized.'], 403);
}
//Check if the message has a corresponding capsule
if (Message::find($message->id + 10000000))
{
{
//If the message has a corresponding capsule, we delete the capsule and the message
$capsule = Message::where('id', $messageId + 10000000)->where('chat_id', $discussionId)->first();
if ($message->media_url) {
$mediaPath = public_path('source/assets/media/' . $message->media_url);
Expand All @@ -285,6 +287,8 @@ public function deleteMessage($discussionId, $messageId)
$capsule->delete();
$message->delete();
}
//If the message doesn't have a corresponding capsule, we delete the message
//We also check if the message has a media file and delete it if it exists
else{
if ($message->media_url) {
$mediaPath = public_path("/source/media/" . $message->media_url);
Expand Down
42 changes: 40 additions & 2 deletions app/Http/Controllers/FriendshipsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@

class FriendshipsController extends Controller
{
/**
* Display a listing of the friendships of the user.
*/
public function index()
{

// Auth id can be the user_id or the friend_id in the db so we need to check both
$friendsFromUser = Friendship::where('user_id', Auth::id())
->where('status', 'accepted')
->with(['user', 'friend'])
Expand All @@ -29,6 +34,9 @@ public function index()
]);
}

/**
* Show the form for creating a new friendship.
*/
public function create()
{
return view('friendships.create');
Expand All @@ -53,6 +61,12 @@ public function pending()
]);
}

/**
* Store a newly created friendship in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$validated = $request->validate([
Expand Down Expand Up @@ -86,7 +100,13 @@ public function store(Request $request)
return back()->with('success', 'Friend request sent !');
}

public function accept(Request $request, $friendship_id)
/**
* Accept a friendship request.
*
* @param int $friendship_id
* @return \Illuminate\Http\Response
*/
public function accept($friendship_id)
{
$friendship = Friendship::find($friendship_id);

Expand All @@ -99,7 +119,13 @@ public function accept(Request $request, $friendship_id)
return back()->with('error', 'Could not accept the friend request.');
}

public function decline(Request $request, $friendship_id)
/**
* Decline a friendship request.
*
* @param int $friendship_id
* @return \Illuminate\Http\Response
*/
public function decline($friendship_id)
{
$friendship = Friendship::find($friendship_id);

Expand All @@ -111,6 +137,12 @@ public function decline(Request $request, $friendship_id)
return back()->with('error', 'Could not decline the friend request.');
}

/**
* Remove the specified friendship from storage.
*
* @param int $friend
* @return \Illuminate\Http\Response
*/
public function destroy($friend)
{
$friendship = Friendship::where(function ($query) use ($friend) {
Expand All @@ -129,6 +161,12 @@ public function destroy($friend)
return back()->with('error', 'Could not find the friend to remove.');
}

/**
* Block a user.
*
* @param int $friend
* @return \Illuminate\Http\Response
*/
public function block($friend)
{
$friend = User::findOrFail($friend);
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Requests/ProfileUpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function rules(): array

Rule::unique(User::class)->ignore($this->user()->id),
],
'image' => ['nullable', 'image', 'mimes:jpeg,png,jpg', 'max:2048'],
'image' => ['nullable', 'image', 'mimes:jpeg,png,jpg', 'max:10240'],
];
}
}
18 changes: 16 additions & 2 deletions resources/views/dashboard.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,9 @@ function leaveChat() {
alert("Informations de message ou discussion manquantes.");
return;
}
console.log("messageId", messageId)
// if the message has an id greater than 10000000, it means it is a capsule message
// so we need to delete the message with the id - 10000000 to delete the original message
if(messageId >= 10000000)
{
const response = await fetch(`/chat/${discussionId}/${messageId - 10000000}/delete`, {
Expand All @@ -379,6 +380,7 @@ function leaveChat() {
}
});
}
// if the message has an id less than 10000000, it means it is a normal message
else{
const response = await fetch(`/chat/${discussionId}/${messageId}/delete`, {
method: 'DELETE',
Expand All @@ -393,14 +395,19 @@ function leaveChat() {
console.error("Erreur de suppression, statut:", response.status);
throw new Error("Impossible de supprimer le message.");
}
const messageElement = document.getElementById(`message-div-${messageId}`);
if (messageElement) {
messageElement.remove();
}
}
/**
* Function to leave a chat with a loader
*
* @returns {void}
*/
function leaveChatWithLoad()
{
const loader = document.getElementById('leave-chat-loader');
Expand All @@ -420,6 +427,13 @@ function leaveChatWithLoad()
});
}
/**
* Function to delete a message with a loader
*
* @param {number} messageId The ID of the message
* @param {number} chatId The ID of the chat
* @returns {void}
*/
async function deleteMessageWithLoader(messageId, chatId) {
const loader = document.getElementById(`loader-${messageId}`);
const button = event.target;
Expand Down
3 changes: 3 additions & 0 deletions resources/views/friendships/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class="mt-1 block w-full px-4 py-2 border border-gray-300 rounded-md shadow-sm f
</x-app-layout>

<script>
/**
* Function to display the loader when the user clicks on the button to add a friend
*/
function addFriendWithLoad() {
document.getElementById('add-friend-btn').style.display = 'none';
document.getElementById('add-friend-loader').style.display = 'block';
Expand Down

0 comments on commit 11bd3a1

Please sign in to comment.