This repository has been archived on 2024-06-26. You can view files and clone it, but cannot push or open issues or pull requests.
sakura/app/Controllers/FriendsController.php

148 lines
3.7 KiB
PHP
Raw Normal View History

2016-03-30 21:30:15 +00:00
<?php
/**
* Holds the friends controller.
* @package Sakura
*/
namespace Sakura\Controllers;
2016-08-07 14:10:27 +00:00
use Sakura\CurrentSession;
2016-03-30 21:30:15 +00:00
use Sakura\Notification;
use Sakura\User;
/**
* Friendly controller.
* @package Sakura
* @author Julian van de Groep <me@flash.moe>
*/
class FriendsController extends Controller
{
2016-08-05 02:35:37 +00:00
/**
* Add a notification.
* @param User $friend
* @param User $user
* @param string $title
* @param string $text
*/
2016-12-04 16:33:52 +00:00
private function addNotification(User $friend, User $user, string $title, string $text = ""): void
2016-03-30 21:30:15 +00:00
{
$alert = new Notification;
$alert->user = $friend->id;
$alert->time = time();
$alert->title = $title;
$alert->text = $text;
2016-09-13 22:05:03 +00:00
$alert->image = route('user.avatar', $user->id);
2016-03-30 21:30:15 +00:00
$alert->timeout = 60000;
2016-09-11 14:25:22 +00:00
$alert->link = route('user.profile', $user->id);
2016-03-30 21:30:15 +00:00
$alert->save();
}
2016-08-05 02:35:37 +00:00
/**
* Add a friend.
* @param int $id
* @return string
*/
2016-12-04 16:33:52 +00:00
public function add(int $id = 0): string
2016-03-30 21:30:15 +00:00
{
2016-08-07 14:10:27 +00:00
$user = CurrentSession::$user;
2016-03-30 21:30:15 +00:00
// Check if the user can comment
if (!session_check()) {
2016-03-30 21:30:15 +00:00
$error = "Your session expired, refresh the page!";
return $this->json(compact('error'));
}
$friend = User::construct($id);
if (!$friend->activated || !$user->activated) {
2016-03-30 21:30:15 +00:00
$error = "The user you tried to add does not exist!";
return $this->json(compact('error'));
}
2016-03-31 20:03:25 +00:00
if ($friend->id === $user->id) {
2016-03-30 21:30:15 +00:00
$error = "You can't be friends with yourself, stop trying to bend reality!";
return $this->json(compact('error'));
}
2016-03-31 20:03:25 +00:00
if ($user->isFriends($friend->id)) {
2016-03-30 21:30:15 +00:00
$error = "You are already friends with this person!";
return $this->json(compact('error'));
}
// Add friend
2016-03-31 20:03:25 +00:00
$user->addFriend($friend->id);
2016-03-30 21:30:15 +00:00
2016-03-31 20:03:25 +00:00
$level = $user->isFriends($friend->id);
2016-03-30 21:30:15 +00:00
$mutual = $level === 2;
$alertTitle = $mutual
2016-03-31 20:03:25 +00:00
? "{$user->username} accepted your friend request!"
: "{$user->username} added you as a friend!";
2016-03-30 21:30:15 +00:00
$alertText = $mutual
? ""
: "Click here to add them as well.";
$this->addNotification(
$friend,
2016-03-31 20:03:25 +00:00
$user,
2016-03-30 21:30:15 +00:00
$alertTitle,
$alertText
);
$message = $mutual
? "You are now mutual friends with {$friend->username}!"
: "A friend request has been sent to {$friend->username}!";
return $this->json(compact('message', 'level'));
}
2016-08-05 02:35:37 +00:00
/**
* Removes a friend.
* @param int $id
* @return string
*/
2016-12-04 16:33:52 +00:00
public function remove(int $id = 0): string
2016-03-30 21:30:15 +00:00
{
2016-08-07 14:10:27 +00:00
$user = CurrentSession::$user;
2016-03-30 21:30:15 +00:00
// Check if the user can comment
if (!session_check()) {
2016-03-30 21:30:15 +00:00
$error = "Your session expired, refresh the page!";
return $this->json(compact('error'));
}
$friend = User::construct($id);
if (!$friend->activated || !$user->activated) {
2016-03-30 21:30:15 +00:00
$error = "The user you tried to remove does not exist!";
return $this->json(compact('error'));
}
2016-03-31 20:03:25 +00:00
if (!$user->isFriends($friend->id)) {
2016-03-30 21:30:15 +00:00
$error = "You aren't even friends with that person!";
return $this->json(compact('error'));
}
// Add friend
2016-03-31 20:03:25 +00:00
$user->removeFriend($friend->id);
2016-03-30 21:30:15 +00:00
2016-03-31 20:03:25 +00:00
$level = $user->isFriends($friend->id);
2016-03-30 21:30:15 +00:00
2016-03-31 20:03:25 +00:00
$alertTitle = "{$user->username} removed you from their friends!";
2016-03-30 21:30:15 +00:00
$this->addNotification(
$friend,
2016-03-31 20:03:25 +00:00
$user,
2016-03-30 21:30:15 +00:00
$alertTitle
);
$message = "Removed {$friend->username} from your friends!";
return $this->json(compact('message', 'level'));
}
}