pixelfed/app/Http/Controllers/SiteController.php

153 lines
4.9 KiB
PHP
Raw Normal View History

2018-06-01 03:12:06 +00:00
<?php
namespace App\Http\Controllers;
2018-08-28 03:07:36 +00:00
use Illuminate\Http\Request;
use Illuminate\Support\Str;
2019-02-05 20:58:48 +00:00
use App, Auth, Cache, View;
use App\Util\Lexer\PrettyNumber;
use App\{Follower, Page, Profile, Status, User, UserFilter};
2019-02-28 03:15:18 +00:00
use App\Util\Localization\Localization;
2019-12-05 03:24:27 +00:00
use App\Services\FollowerService;
2018-06-01 03:12:06 +00:00
class SiteController extends Controller
{
2019-11-23 06:33:47 +00:00
public function home(Request $request)
2018-07-12 16:37:31 +00:00
{
2018-08-28 03:07:36 +00:00
if (Auth::check()) {
2019-11-23 06:33:47 +00:00
return $this->homeTimeline($request);
2018-07-12 16:37:31 +00:00
} else {
2018-08-28 03:07:36 +00:00
return $this->homeGuest();
2018-07-12 16:37:31 +00:00
}
}
public function homeGuest()
{
2020-04-17 22:34:19 +00:00
$data = Cache::remember('site:landing:data', now()->addHours(3), function() {
return [
'stats' => [
'posts' => App\Util\Lexer\PrettyNumber::convert(App\Status::count()),
'likes' => App\Util\Lexer\PrettyNumber::convert(App\Like::count()),
'hashtags' => App\Util\Lexer\PrettyNumber::convert(App\StatusHashtag::count())
],
];
});
return view('site.index', compact('data'));
2018-07-12 16:37:31 +00:00
}
2019-11-23 06:33:47 +00:00
public function homeTimeline(Request $request)
2018-07-12 16:37:31 +00:00
{
2019-11-23 06:33:47 +00:00
$this->validate($request, [
'layout' => 'nullable|string|in:grid,feed'
]);
$layout = $request->input('layout', 'feed');
return view('timeline.home', compact('layout'));
2018-07-12 16:37:31 +00:00
}
2018-06-05 07:49:05 +00:00
public function changeLocale(Request $request, $locale)
{
2018-11-16 03:52:36 +00:00
// todo: add other locales after pushing new l10n strings
2019-02-28 03:15:18 +00:00
$locales = Localization::languages();
2018-11-16 03:52:36 +00:00
if(in_array($locale, $locales)) {
if($request->user()) {
$user = $request->user();
$user->language = $locale;
$user->save();
}
2018-11-16 03:52:36 +00:00
session()->put('locale', $locale);
2018-06-05 07:49:05 +00:00
}
2018-08-28 03:07:36 +00:00
2019-02-28 04:52:47 +00:00
return redirect(route('site.language'));
2018-06-05 07:49:05 +00:00
}
2018-08-10 03:18:56 +00:00
public function about()
{
2020-12-27 03:28:12 +00:00
$page = Page::whereSlug('/site/about')->whereActive(true)->first();
$stats = Cache::remember('site:about:stats-v1', now()->addHours(12), function() {
return [
'posts' => Status::count(),
2019-06-20 03:16:01 +00:00
'users' => User::whereNull('status')->count(),
2019-03-14 02:05:29 +00:00
'admin' => User::whereIsAdmin(true)->first()
];
});
2020-12-27 03:28:12 +00:00
$path = $page ? 'site.about-custom' : 'site.about';
return view($path, compact('page', 'stats'));
2018-11-16 03:52:36 +00:00
}
2018-08-28 03:07:36 +00:00
2018-11-16 03:52:36 +00:00
public function language()
{
return view('site.language');
2018-08-10 03:18:56 +00:00
}
2019-02-05 20:58:48 +00:00
public function communityGuidelines(Request $request)
{
return Cache::remember('site:help:community-guidelines', now()->addDays(120), function() {
$slug = '/site/kb/community-guidelines';
$page = Page::whereSlug($slug)->whereActive(true)->first();
return View::make('site.help.community-guidelines')->with(compact('page'))->render();
});
2019-02-05 20:58:48 +00:00
}
2019-06-20 02:54:27 +00:00
public function privacy(Request $request)
{
2019-09-24 06:54:17 +00:00
$page = Cache::remember('site:privacy', now()->addDays(120), function() {
2019-06-20 02:54:27 +00:00
$slug = '/site/privacy';
$page = Page::whereSlug($slug)->whereActive(true)->first();
});
2019-09-24 06:54:17 +00:00
return View::make('site.privacy')->with(compact('page'))->render();
2019-06-20 02:54:27 +00:00
}
public function terms(Request $request)
{
2019-09-24 06:54:17 +00:00
$page = Cache::remember('site:terms', now()->addDays(120), function() {
2019-06-20 02:54:27 +00:00
$slug = '/site/terms';
2019-09-24 06:54:17 +00:00
return Page::whereSlug($slug)->whereActive(true)->first();
2019-06-20 02:54:27 +00:00
});
2019-09-24 06:54:17 +00:00
return View::make('site.terms')->with(compact('page'))->render();
2019-06-20 02:54:27 +00:00
}
2019-12-05 03:24:27 +00:00
public function redirectUrl(Request $request)
{
$this->validate($request, [
'url' => 'required|url'
]);
2020-04-17 22:34:19 +00:00
$url = request()->input('url');
2019-12-05 03:24:27 +00:00
return view('site.redirect', compact('url'));
}
public function followIntent(Request $request)
{
$this->validate($request, [
'user' => 'string|min:1|max:15|exists:users,username',
]);
$profile = Profile::whereUsername($request->input('user'))->firstOrFail();
$user = $request->user();
2019-12-05 03:55:42 +00:00
abort_if($user && $profile->id == $user->profile_id, 404);
2019-12-05 03:24:27 +00:00
$following = $user != null ? FollowerService::follows($user->profile_id, $profile->id) : false;
return view('site.intents.follow', compact('profile', 'user', 'following'));
}
public function legacyProfileRedirect(Request $request, $username)
{
$username = Str::contains($username, '@') ? '@' . $username : $username;
if(str_contains($username, '@')) {
$profile = Profile::whereUsername($username)
->firstOrFail();
if($profile->domain == null) {
$url = "/$profile->username";
} else {
$url = "/i/web/profile/_/{$profile->id}";
}
} else {
$profile = Profile::whereUsername($username)
->whereNull('domain')
->firstOrFail();
$url = "/$profile->username";
}
return redirect($url);
}
2018-06-01 03:12:06 +00:00
}