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/UserController.php
2016-12-18 23:00:24 +01:00

151 lines
3.8 KiB
PHP

<?php
/**
* Holds the user page controllers.
* @package Sakura
*/
namespace Sakura\Controllers;
use Phroute\Phroute\Exception\HttpMethodNotAllowedException;
use Phroute\Phroute\Exception\HttpRouteNotFoundException;
use Sakura\Config;
use Sakura\CurrentSession;
use Sakura\DB;
use Sakura\Rank;
use Sakura\User;
/**
* Everything that is just for serving user data.
* @package Sakura
* @author Julian van de Groep <me@flash.moe>
*/
class UserController extends Controller
{
/**
* Display the profile of a user.
* @param int $id
* @throws HttpRouteNotFoundException
* @return string
*/
public function profile(int $id = 0): string
{
$profile = User::construct($id);
if ($profile->id === 0
|| !$profile->activated
|| (
$profile->restricted
&& (
$profile->id !== CurrentSession::$user->id
|| !(
CurrentSession::$user->perms->isMod
|| CurrentSession::$user->perms->isAdmin
)
)
)
) {
throw new HttpRouteNotFoundException;
}
return view('user/profile', compact('profile'));
}
/**
* Resolve user id from username and redirect to profile.
* @throws HttpRouteNotFoundException
* @param string $id
*/
public function resolve(string $name): string
{
$clean_name = clean_string($name, true);
$id = DB::table('users')
->where('username_clean', $clean_name)
->value('user_id');
if (!$id) {
// Fetch from username_history
$id = DB::table('username_history')
->where('username_old_clean', $clean_name)
->orderBy('change_id', 'desc')
->value('user_id');
if (!$id) {
throw new HttpRouteNotFoundException;
}
}
return redirect(route('user.profile', $id));
}
/**
* Last listened to.
* @param int $id
* @throws HttpRouteNotFoundException
* @return string
*/
public function nowPlaying(int $id): string
{
$user = User::construct($id);
if ($user->id === 0) {
throw new HttpRouteNotFoundException;
}
$user->updateLastTrack();
$artist_url = 'http://last.fm/music/' . urlencode($user->musicArtist);
$track_url = $artist_url . '/_/' . urlencode($user->musicTrack);
return $this->json([
'track' => $user->musicTrack,
'track_url' => $track_url,
'artist' => $user->musicArtist,
'artist_url' => $artist_url,
'listening' => $user->musicListening,
]);
}
/**
* Display the memberlist.
* @param int $rank
* @throws HttpMethodNotAllowedException
* @return string
*/
public function members(int $rank = null): string
{
if (!CurrentSession::$user->activated) {
throw new HttpMethodNotAllowedException;
}
// Get all ranks
$getRanks = DB::table('ranks')
->get(['rank_id']);
// Define variable
$ranks = [];
// Add the empty rank
$ranks[0] = Rank::construct(0);
// Reorder shit
foreach ($getRanks as $sortRank) {
$ranks[$sortRank->rank_id] = Rank::construct($sortRank->rank_id);
}
// Get the active rank
$rank = array_key_exists($rank, $ranks) ? $rank : ($rank ? 0 : intval(config("rank.regular")));
return view('user/members', compact('ranks', 'rank'));
}
/**
* Report a user.
* @param int $id
*/
public function report(int $id = 0): string
{
return view('user/report');
}
}