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/libraries/Controllers/FriendsController.php

141 lines
3.6 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-03-31 20:03:25 +00:00
use Sakura\ActiveUser;
2016-03-30 21:30:15 +00:00
use Sakura\Notification;
use Sakura\Perms\Site;
use Sakura\Router;
use Sakura\User;
/**
* Friendly controller.
*
* @package Sakura
* @author Julian van de Groep <me@flash.moe>
*/
class FriendsController extends Controller
{
private function addNotification($friend, $user, $title, $text = "")
{
$alert = new Notification;
$alert->user = $friend->id;
$alert->time = time();
$alert->title = $title;
$alert->text = $text;
$alert->image = Router::route('file.avatar', $user->id);
$alert->timeout = 60000;
$alert->link = Router::route('user.profile', $user->id);
$alert->save();
}
public function add($id = 0)
{
2016-03-31 20:03:25 +00:00
$user = ActiveUser::$user;
2016-03-30 21:30:15 +00:00
$session = $_POST['session'] ?? '';
// Check if the user can comment
if ($session !== session_id()) {
$error = "Your session expired, refresh the page!";
return $this->json(compact('error'));
}
$friend = User::construct($id);
if ($friend->permission(Site::DEACTIVATED)
2016-03-31 20:03:25 +00:00
|| $user->permission(Site::DEACTIVATED)) {
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'));
}
public function remove($id = 0)
{
2016-03-31 20:03:25 +00:00
$user = ActiveUser::$user;
2016-03-30 21:30:15 +00:00
$session = $_POST['session'] ?? '';
// Check if the user can comment
if ($session !== session_id()) {
$error = "Your session expired, refresh the page!";
return $this->json(compact('error'));
}
$friend = User::construct($id);
if ($friend->permission(Site::DEACTIVATED)
2016-03-31 20:03:25 +00:00
|| $user->permission(Site::DEACTIVATED)) {
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'));
}
}