Skip to content

Commit efc46fb

Browse files
committed
WIP
1 parent b2300d5 commit efc46fb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1068
-342
lines changed

app/Http/Controllers/CommentsController.php

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ class CommentsController extends Controller
1616
*/
1717
public function show(Post $post, array $data = [])
1818
{
19+
$post->load(['comments' => function ($query) {
20+
$query->withCount('likers');
21+
}]);
22+
23+
$post = auth()->user()->attachLikeStatus($post);
24+
1925
return view('components.comments', array_merge($data, [
2026
'model' => $post,
2127
]))
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Models\Comment;
6+
use App\Models\Post;
7+
use Illuminate\Http\Request;
8+
use Illuminate\Support\Facades\Blade;
9+
10+
class LikeController
11+
{
12+
/**
13+
* @param \App\Models\Post $post
14+
* @param \Illuminate\Http\Request $request
15+
*
16+
* @return string
17+
*/
18+
public function togglePost(Post $post, Request $request)
19+
{
20+
$request->user()->toggleLike($post);
21+
22+
return turbo_stream()
23+
->target(dom_id($post, 'like'))
24+
->action('replace')
25+
->view('particles.streams.like', ['model' => $post]);
26+
}
27+
28+
/**
29+
* @param \App\Models\Comment $comment
30+
* @param \Illuminate\Http\Request $request
31+
*
32+
* @return string
33+
*/
34+
public function toggleComment(Comment $comment, Request $request)
35+
{
36+
$request->user()->toggleLike($comment);
37+
38+
return turbo_stream()
39+
->target(dom_id($comment, 'like'))
40+
->action('replace')
41+
->view('particles.streams.like', ['model' => $comment]);
42+
}
43+
}

app/Http/Controllers/PostController.php

+23-2
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,32 @@ class PostController extends Controller
1515
{
1616
public $action = "append";
1717

18+
/**
19+
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Foundation\Application|null
20+
*/
21+
public function feed()
22+
{
23+
$popular = Post::withCount('likers')
24+
->withCount('comments')
25+
->orderBy('likers_count', 'desc')
26+
->orderBy('created_at', 'desc')
27+
->cursorPaginate(5);
28+
29+
return view('post.list', [
30+
'popular' => $popular,
31+
]);
32+
}
33+
1834
/**
1935
* @param \App\Models\Post $post
2036
*
2137
* @return \Illuminate\Contracts\View\View
2238
*/
2339
public function show(Post $post)
2440
{
25-
$post->with('comments');
41+
$post->load(['comments', 'author'])
42+
->loadCount('comments')
43+
->loadCount('likers');
2644

2745
return view('post.show', [
2846
'post' => $post,
@@ -103,12 +121,15 @@ public function delete(Request $request, Post $post)
103121
*/
104122
public function list(Request $request)
105123
{
106-
$posts = Post::with(['user'])
124+
$posts = Post::with(['author'])
107125
->withCount('comments')
126+
->withCount('likers')
108127
->when($request->has('user_id'), fn(Builder $query) => $query->where('user_id', $request->get('user_id')))
109128
->orderBy('id', 'desc')
110129
->cursorPaginate(3);
111130

131+
$posts = $request->user()->attachLikeStatus($posts);
132+
112133
return view('particles.posts.list', [
113134
'posts' => $posts,
114135
'isMyProfile' => $request->has('user_id') && $request->user()?->id == $request->get('user_id'),

app/Models/Comment.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
use Illuminate\Database\Eloquent\Model;
99
use Illuminate\Database\Eloquent\SoftDeletes;
1010
use Illuminate\Support\Str;
11+
use Overtrue\LaravelLike\Traits\Likeable;
1112

1213
class Comment extends Model
1314
{
14-
use HasFactory, SoftDeletes;
15+
use HasFactory, SoftDeletes, Likeable;
1516

1617
/**
1718
* @var string[]

app/Models/Post.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
use Illuminate\Database\Eloquent\Model;
1212
use Illuminate\Support\Str;
1313
use Laravel\Scout\Searchable;
14+
use Overtrue\LaravelLike\Traits\Likeable;
1415
use Spatie\Feed\Feedable;
1516
use Spatie\Feed\FeedItem;
1617

1718
class Post extends Model implements Feedable
1819
{
19-
use HasFactory, HasComments, Taggable, Searchable;
20+
use HasFactory, HasComments, Taggable, Searchable, Likeable;
2021

2122
/**
2223
* @var string[]
@@ -172,6 +173,6 @@ public static function getFeedItems()
172173
*/
173174
public function estimatedReadingTime(int $wordsPerMinute = 100): int
174175
{
175-
return ceil(Str::of($this->content)->wordCount() / $wordsPerMinute);
176+
return ceil(Str::of($this->content)->matchAll("/\s+/")->count() / $wordsPerMinute);
176177
}
177178
}

app/Models/User.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
use Illuminate\Foundation\Auth\User as Authenticatable;
99
use Illuminate\Notifications\Notifiable;
1010
use Laravel\Sanctum\HasApiTokens;
11+
use Overtrue\LaravelLike\Traits\Liker;
1112

1213
class User extends Authenticatable
1314
{
14-
use HasApiTokens, HasFactory, Notifiable;
15+
use HasApiTokens, HasFactory, Notifiable, Liker;
1516

1617
/**
1718
* The attributes that are mass assignable.

app/Providers/AppServiceProvider.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ class AppServiceProvider extends ServiceProvider
1212
*/
1313
public function register(): void
1414
{
15-
//
15+
//if($this->app->environment('production')) {
16+
\URL::forceScheme('https');
17+
//}
1618
}
1719

1820
/**

composer.json

+2
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
"cagilo/cagilo": "^3.2",
1010
"doctrine/dbal": "^3.7",
1111
"guzzlehttp/guzzle": "^7.2",
12+
"hotwired/turbo-laravel": "^1.12",
1213
"jolicode/jolitypo": "^1.4",
1314
"laravel/framework": "^10.31",
1415
"laravel/sanctum": "^3.2",
1516
"laravel/scout": "^10.5",
1617
"laravel/socialite": "^5.9",
1718
"laravel/tinker": "^2.8",
19+
"overtrue/laravel-like": "^5.2",
1820
"spatie/laravel-feed": "^4.3",
1921
"symfony/dom-crawler": "^6.3",
2022
"symfony/yaml": "^6.3",

composer.lock

+138-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/img/ui/info.svg

+33
Loading

0 commit comments

Comments
 (0)