Merge pull request #1871 from pixelfed/staging

Add Profile Embeds
This commit is contained in:
daniel 2019-12-04 21:28:13 -07:00 committed by GitHub
commit 7fa4d4a669
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 304 additions and 33 deletions

View file

@ -6,6 +6,7 @@
- Added drafts API endpoint for Camera Roll ([bad2ecde](https://github.com/pixelfed/pixelfed/commit/bad2ecde))
- Added AccountService ([885a1258](https://github.com/pixelfed/pixelfed/commit/885a1258))
- Added post embeds ([1fecf717](https://github.com/pixelfed/pixelfed/commit/1fecf717))
- Added profile embeds ([fb7a3cf0](https://github.com/pixelfed/pixelfed/commit/fb7a3cf0))
### Fixed
- Fixed like and share/reblog count on profiles ([86cb7d09](https://github.com/pixelfed/pixelfed/commit/86cb7d09))
@ -51,6 +52,7 @@
- Updated ApiV1Controller, add ```mobile_apis``` to /api/v1/instance endpoint ([57407463](https://github.com/pixelfed/pixelfed/commit/57407463))
- Updated PublicTimelineService, add video media scopes ([7b00eba3](https://github.com/pixelfed/pixelfed/commit/7b00eba3))
- Updated PublicApiController, add AccountService ([5ebd2c8a](https://github.com/pixelfed/pixelfed/commit/5ebd2c8a))
- Update CommentController, fix scope bug ([45ecad2a](https://github.com/pixelfed/pixelfed/45ecad2a))
## Deprecated

View file

@ -115,8 +115,8 @@ class DiscoverController extends Controller
abort_if(!config('instance.discover.tags.is_public') && !$auth, 403);
$this->validate($request, [
'hashtag' => 'required|alphanum|min:2|max:124',
'page' => 'nullable|integer|min:1|max:' . ($auth ? 19 : 3)
'hashtag' => 'required|alphanum|min:1|max:124',
'page' => 'nullable|integer|min:1|max:' . ($auth ? 29 : 10)
]);
$page = $request->input('page') ?? '1';

View file

@ -23,18 +23,20 @@ class FollowerController extends Controller
public function store(Request $request)
{
$this->validate($request, [
'item' => 'required|string',
'item' => 'required|string',
'force' => 'nullable|boolean',
]);
$force = (bool) $request->input('force', true);
$item = (int) $request->input('item');
$this->handleFollowRequest($item);
if($request->wantsJson()) {
$url = $this->handleFollowRequest($item, $force);
if($request->wantsJson() == true) {
return response()->json(200);
} else {
return redirect()->back();
return redirect($url);
}
}
protected function handleFollowRequest($item)
protected function handleFollowRequest($item, $force)
{
$user = Auth::user()->profile;
@ -87,17 +89,19 @@ class FollowerController extends Controller
}
FollowPipeline::dispatch($follower);
} else {
$request = FollowRequest::whereFollowerId($user->id)->whereFollowingId($target->id)->exists();
$follower = Follower::whereProfileId($user->id)->whereFollowingId($target->id)->exists();
if($remote == true && $request && !$follower) {
$this->sendFollow($user, $target);
if($force == true) {
$request = FollowRequest::whereFollowerId($user->id)->whereFollowingId($target->id)->exists();
$follower = Follower::whereProfileId($user->id)->whereFollowingId($target->id)->exists();
if($remote == true && $request && !$follower) {
$this->sendFollow($user, $target);
}
if($remote == true && $follower) {
$this->sendUndoFollow($user, $target);
}
Follower::whereProfileId($user->id)
->whereFollowingId($target->id)
->delete();
}
if($remote == true && $follower) {
$this->sendUndoFollow($user, $target);
}
Follower::whereProfileId($user->id)
->whereFollowingId($target->id)
->delete();
}
Cache::forget('profile:following:'.$target->id);
@ -107,6 +111,8 @@ class FollowerController extends Controller
Cache::forget('api:local:exp:rec:'.$user->id);
Cache::forget('user:account:id:'.$target->user_id);
Cache::forget('user:account:id:'.$user->user_id);
return $target->url();
}
public function sendFollow($user, $target)

View file

@ -5,6 +5,7 @@ namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
use Cache;
use View;
use App\Follower;
use App\FollowRequest;
use App\Profile;
@ -189,4 +190,29 @@ class ProfileController extends Controller
abort_if(!Auth::check(), 404);
return redirect(Auth::user()->url());
}
public function embed(Request $request, $username)
{
$res = view('profile.embed-removed');
if(strlen($username) > 15 || strlen($username) < 2) {
return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
}
$profile = Profile::whereUsername($username)
->whereIsPrivate(false)
->whereNull('status')
->whereNull('domain')
->first();
if(!$profile) {
return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
}
$content = Cache::remember('profile:embed:'.$profile->id, now()->addHours(12), function() use($profile) {
return View::make('profile.embed')->with(compact('profile'))->render();
});
return response($content)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
}
}

View file

@ -7,6 +7,7 @@ use App, Auth, Cache, View;
use App\Util\Lexer\PrettyNumber;
use App\{Follower, Page, Profile, Status, User, UserFilter};
use App\Util\Localization\Localization;
use App\Services\FollowerService;
class SiteController extends Controller
{
@ -98,4 +99,25 @@ class SiteController extends Controller
});
return View::make('site.terms')->with(compact('page'))->render();
}
public function redirectUrl(Request $request)
{
$this->validate($request, [
'url' => 'required|url'
]);
$url = urldecode(request()->input('url'));
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();
abort_if($user && $profile->id == $user->profile_id, 404);
$following = $user != null ? FollowerService::follows($user->profile_id, $profile->id) : false;
return view('site.intents.follow', compact('profile', 'user', 'following'));
}
}

View file

@ -12,8 +12,8 @@ use App\{
class FollowerService {
protected $profile;
protected $follower_prefix;
protected $following_prefix;
static $follower_prefix = 'px:profile:followers-v1.3:';
static $following_prefix = 'px:profile:following-v1.3:';
public static function build()
{
@ -23,35 +23,48 @@ class FollowerService {
public function profile(Profile $profile)
{
$this->profile = $profile;
$this->follower_prefix = config('cache.prefix').':profile:followers:'.$profile->id;
$this->following_prefix = config('cache.prefix').':profile:following:'.$profile->id;
self::$follower_prefix .= $profile->id;
self::$following_prefix .= $profile->id;
return $this;
}
public function followers($limit = 100, $offset = 0)
public function followers($limit = 100, $offset = 1)
{
if(Redis::llen($this->follower_prefix) == 0) {
$followers = $this->profile->followers;
if(Redis::zcard(self::$follower_prefix) == 0) {
$followers = $this->profile->followers()->pluck('profile_id');
$followers->map(function($i) {
Redis::lpush($this->follower_prefix, $i->id);
Redis::zadd(self::$follower_prefix, $i, $i);
});
return $followers;
return Redis::zrevrange(self::$follower_prefix, $offset, $limit);
} else {
return Redis::lrange($this->follower_prefix, $offset, $limit);
return Redis::zrevrange(self::$follower_prefix, $offset, $limit);
}
}
public function following($limit = 100, $offset = 0)
public function following($limit = 100, $offset = 1)
{
if(Redis::llen($this->following_prefix) == 0) {
$following = $this->profile->following;
if(Redis::zcard(self::$following_prefix) == 0) {
$following = $this->profile->following()->pluck('following_id');
$following->map(function($i) {
Redis::lpush($this->following_prefix, $i->id);
Redis::zadd(self::$following_prefix, $i, $i);
});
return $following;
return Redis::zrevrange(self::$following_prefix, $offset, $limit);
} else {
return Redis::lrange($this->following_prefix, $offset, $limit);
return Redis::zrevrange(self::$following_prefix, $offset, $limit);
}
}
public static function follows(string $actor, string $target)
{
$key = self::$follower_prefix . $target;
if(Redis::zcard($key) == 0) {
$p = Profile::findOrFail($target);
self::build()->profile($p)->followers(1);
self::build()->profile($p)->following(1);
return (bool) Redis::zrank($key, $actor);
} else {
return (bool) Redis::zrank($key, $actor);
}
}

View file

@ -0,0 +1,46 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="mobile-web-app-capable" content="yes">
<title>Pixelfed | 404 Embed Not Found</title>
<meta property="og:site_name" content="{{ config('app.name', 'pixelfed') }}">
<meta property="og:title" content="{{ $title ?? config('app.name', 'pixelfed') }}">
<meta name="medium" content="image">
<meta name="theme-color" content="#10c5f8">
<meta name="apple-mobile-web-app-capable" content="yes">
<link rel="shortcut icon" type="image/png" href="/img/favicon.png?v=2">
<link rel="apple-touch-icon" type="image/png" href="/img/favicon.png?v=2">
<link href="{{ mix('css/app.css') }}" rel="stylesheet">
<style type="text/css">
body.embed-card {
background: #fff !important;
margin: 0;
padding-bottom: 0;
}
.status-card-embed {
box-shadow: none;
border-radius: 4px;
overflow: hidden;
}
</style>
</head>
<body class="bg-white">
<div class="embed-card">
<div class="card status-card-embed card-md-rounded-0 border card-body border shadow-none rounded-0 d-flex justify-content-center align-items-center">
<div class="text-center p-5">
<img src="/img/pixelfed-icon-color.svg" width="40px" height="40px">
<p class="h2 py-3 font-weight-bold">Pixelfed</p>
<p style="font-size:14px;font-weight: 500;" class="px-2 py-4">Cannot display profile embed, it may be deleted or set to private.</p>
<p><a href="{{config('app.url')}}" class="font-weight-bold" target="_blank">Visit Pixelfed</a></p>
</div>
</div>
</div>
<script type="text/javascript">window.addEventListener("message",e=>{const t=e.data||{};window.parent&&"setHeight"===t.type&&window.parent.postMessage({type:"setHeight",id:t.id,height:document.getElementsByTagName("html")[0].scrollHeight},"*")});</script>
</body>
</html>

View file

@ -0,0 +1,101 @@
<!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="mobile-web-app-capable" content="yes">
<title>{{ $title ?? config('app.name', 'Pixelfed') }}</title>
<meta property="og:site_name" content="{{ config('app.name', 'pixelfed') }}">
<meta property="og:title" content="{{ $title ?? config('app.name', 'pixelfed') }}">
<meta property="og:type" content="article">
<meta property="og:url" content="{{$profile->url()}}">
<meta name="medium" content="image">
<meta name="theme-color" content="#10c5f8">
<meta name="apple-mobile-web-app-capable" content="yes">
<link rel="shortcut icon" type="image/png" href="/img/favicon.png?v=2">
<link rel="apple-touch-icon" type="image/png" href="/img/favicon.png?v=2">
<link href="{{ mix('css/app.css') }}" rel="stylesheet">
<style type="text/css">
body.embed-card {
background: #fff !important;
margin: 0;
padding-bottom: 0;
}
.status-card-embed {
box-shadow: none;
border-radius: 4px;
overflow: hidden;
}
</style>
</head>
<body class="bg-white">
<div class="embed-card">
<div class="card status-card-embed card-md-rounded-0 border">
<div class="card-header d-inline-flex align-items-center justify-content-between bg-white">
<div>
<img src="{{$profile->avatarUrl()}}" width="32px" height="32px" target="_blank" style="border-radius: 32px;">
<a class="username font-weight-bold pl-2 text-dark" target="_blank" href="{{$profile->url()}}">
{{$profile->username}}
</a>
</div>
<div>
<a class="small font-weight-bold text-muted pr-1" href="{{config('app.url')}}" target="_blank">{{config('pixelfed.domain.app')}}</a>
<img src="/img/pixelfed-icon-color.svg" width="26px">
</div>
</div>
<div class="card-body pb-1">
<div class="d-flex justify-content-between align-items-center">
<div class="text-center">
<p class="mb-0 font-weight-bold prettyCount">{{$profile->statuses()->count()}}</p>
<p class="mb-0 text-muted text-uppercase small font-weight-bold">Posts</p>
</div>
<div class="text-center">
<p class="mb-0 font-weight-bold prettyCount">{{$profile->followers()->count()}}</p>
<p class="mb-0 text-muted text-uppercase small font-weight-bold">Followers</p>
</div>
<div class="text-center">
<p class="mb-0 font-weight-bold prettyCount">{{$profile->following()->count()}}</p>
<p class="mb-0 text-muted text-uppercase small font-weight-bold">Following</p>
</div>
<div class="text-center">
<p class="mb-0"><a href="/i/intent/follow?user={{$profile->username}}" class="btn btn-primary btn-sm py-1 px-4 text-uppercase font-weight-bold" target="_blank">Follow</a></p>
</div>
</div>
<div class="row mt-4 mb-1">
@foreach($profile->statuses()->latest()->whereScope('public')->whereIsNsfw(false)->has('media')->whereType('photo')->take(9)->get() as $post)
<div class="col-4 mt-2 px-0">
<a class="card info-overlay card-md-border-0 px-1 shadow-none" href="{{$post->url()}}" target="_blank">
<div class="square">
<div class="square-content" style="background-image: url('{{$post->thumb()}}')">
</div>
<div class="info-overlay-text">
</div>
</div>
</a>
</div>
@endforeach
</div>
</div>
<div class="card-footer bg-white">
<p class="text-center mb-0">
<a href="{{$profile->url()}}" class="font-weight-bold" target="_blank">View More Posts</a>
</p>
</div>
</div>
</div>
<script type="text/javascript" src="{{mix('js/manifest.js')}}"></script>
<script type="text/javascript" src="{{mix('js/vendor.js')}}"></script>
<script type="text/javascript" src="{{mix('js/app.js')}}"></script>
<script type="text/javascript">window.addEventListener("message",e=>{const t=e.data||{};window.parent&&"setHeight"===t.type&&window.parent.postMessage({type:"setHeight",id:t.id,height:document.getElementsByTagName("html")[0].scrollHeight},"*")});</script>
<script type="text/javascript">document.querySelectorAll('.caption-container a').forEach(function(i) {i.setAttribute('target', '_blank');});</script>
<script type="text/javascript">
document.querySelectorAll('.prettyCount').forEach(function(i) {
i.innerText = App.util.format.count(i.innerText);
});
</script>
</body>
</html>

View file

@ -0,0 +1,53 @@
@extends('layouts.blank')
@section('content')
<div class="container">
<div class="row">
<div class="col-12 col-md-6 offset-md-3 pt-5">
<p class="h3 text-center font-weight-lighter py-3 mb-4 text-secondary">Follow <span class="text-dark">{{$profile->username}}</span> on Pixelfed</p>
<div class="card">
<div class="card-header p-0 m-0">
<div style="width: 100%;height: 140px;background: #0070b7"></div>
</div>
<div class="card-body">
<div class="text-center mt-n5 mb-4">
<img class="rounded-circle p-1 border mt-n4 bg-white shadow" src="{{$profile->avatarUrl()}}" width="90px" height="90px;">
</div>
<p class="text-center lead font-weight-bold mb-1">{{$profile->username}}</p>
<p class="text-center text-muted small text-uppercase mb-4">{{$profile->followers->count()}} followers</p>
<div class="d-flex justify-content-center">
@if($following == true)
<form class="d-inline-block" action="/i/follow" method="post">
@csrf
<input type="hidden" name="item" value="{{(string)$profile->id}}">
<input type="hidden" name="force" value="0">
<button type="submit" class="btn btn-outline-secondary btn-sm py-1 px-4 text-uppercase font-weight-bold mr-3" style="font-weight: 500">Unfollow</button>
</form>
@else
<form class="d-inline-block" action="/i/follow" method="post">
@csrf
<input type="hidden" name="item" value="{{(string)$profile->id}}">
<input type="hidden" name="force" value="0">
<button type="submit" class="btn btn-primary btn-sm py-1 px-4 text-uppercase font-weight-bold mr-3" style="font-weight: 500">Follow</button>
</form>
@endif
<a class="btn btn-outline-primary btn-sm py-1 px-4 text-uppercase font-weight-bold" href="{{$profile->url()}}" style="font-weight: 500">View Profile</a>
</div>
</div>
</div>
@auth
<div class="d-flex justify-content-between pt-4 small">
<a class="text-lighter text-decoration-none" href="/{{$user->username}}">Logged in as: <span class="font-weight-bold text-muted">{{$user->username}}</span></a>
<span>
<a class="text-decoration-none text-muted font-weight-bold mr-3" href="/site/help">Help</a>
<a class="text-decoration-none text-muted font-weight-bold" href="{{ route('logout') }}" onclick="event.preventDefault();document.getElementById('logout-form').submit();">Logout</a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
@csrf
</form>
</span>
</div>
@endauth
</div>
</div>
</div>
@endsection

View file

@ -227,6 +227,7 @@ Route::domain(config('pixelfed.domain.app'))->middleware(['validemail', 'twofact
Route::get('collections/create', 'CollectionController@create');
Route::get('me', 'ProfileController@meRedirect');
Route::get('intent/follow', 'SiteController@followIntent');
});
Route::group(['prefix' => 'account'], function () {
@ -381,5 +382,6 @@ Route::domain(config('pixelfed.domain.app'))->middleware(['validemail', 'twofact
Route::post('p/{username}/{id}/edit', 'StatusController@editStore');
Route::get('p/{username}/{id}.json', 'StatusController@showObject');
Route::get('p/{username}/{id}', 'StatusController@show');
Route::get('{username}/embed', 'ProfileController@embed');
Route::get('{username}', 'ProfileController@show');
});