Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt committed Aug 7, 2023
1 parent a42f664 commit 2810767
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 3 deletions.
77 changes: 77 additions & 0 deletions app/Http/Controllers/Posts/FindPostsForAggregateController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace App\Http\Controllers\Posts;

use App\Models\Post;
use App\Models\PostShare;
use App\Models\PostShareSnooze;
use App\Models\PostState;
use App\Models\Source;
use App\Models\SourceState;
use App\Services\PostSharing\SharingChannel;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;

final class FindPostsForAggregateController
{
public function __invoke(Request $request)
{
$topSources = Source::query()
->where('state', SourceState::PUBLISHED)
->orderByDesc('visits')
->limit(15)
->whereNotIn('name', [
'https://externals.io',
'https://laravel-news.com',
'https://xkcd.com',
'https://blog.jetbrains.com',
'https://thephp.foundation',
'https://24daysindecember.net',
'https://blog.laravel.com',
])
->get();

$query = Post::query()
->with('source', 'pendingShares', 'comments')
->where('state', '<>', PostState::PUBLISHED)
->orderByDesc('created_at')

// Only for top sources
->whereIn('source_id', $topSources->pluck('id'))

// No pending share for same channel
->whereDoesntHave(
relation: 'pendingShares',
callback: function (Builder|PostShare $builder) {
$builder->where('channel', SharingChannel::AGGREGATE);
})

// No share less than repost grace period for channel
->whereDoesntHave(
relation: 'shares',
callback: function (Builder|PostShare $builder) {
$builder
->where('channel', SharingChannel::AGGREGATE)
->where('shared_at', '>', now()->sub(SharingChannel::AGGREGATE->getSchedule()->cannotRepostWithin())->addDay());
},
)
->whereDoesntHave(
relation: 'shareSnoozes',
callback: function (Builder|PostShareSnooze $builder) {
$builder
->where('channel', SharingChannel::AGGREGATE)
->where('snooze_until', '>', now());
},
);

$posts = $query->paginate(50);

return view('find', [
'user' => $request->user(),
'posts' => $posts,
'message' => $request->get('message'),
'filter' => SharingChannel::AGGREGATE,
'q' => null,
]);
}
}
4 changes: 4 additions & 0 deletions app/Http/Controllers/Posts/SnoozeShareController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public function __invoke(Post $post, string $channel, Request $request)
: now()->addMonths(2),
]);

if ($back = $request->get('back')) {
return redirect()->to($back);
}

return redirect()->action(FindPostController::class, ['filter' => $channel->value]);
}
}
3 changes: 2 additions & 1 deletion app/Jobs/AddPostVisitJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Cache;

class AddPostVisitJob implements ShouldQueue
{
Expand All @@ -31,6 +30,8 @@ public function handle()
'post_id' => $post->id,
]);

$post->source->increment('visits');

// Post::query()
// ->homePage()
// ->paginate(20)
Expand Down
1 change: 1 addition & 0 deletions app/Models/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models;

use App\Actions\ResolveSourceName;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;

use Illuminate\Database\Eloquent\Model;
Expand Down
21 changes: 21 additions & 0 deletions database/migrations/2023_08_07_102153_add_visits_to_sources.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use App\Models\Source;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
public function up(): void
{
Schema::table('sources', function (Blueprint $table) {
$table->unsignedBigInteger('visits')->default(0)->after('state');
});

Source::each(function (Source $source) {
$source->update([
'visits' => $source->posts()->sum('visits'),
]);
});
}
};
9 changes: 7 additions & 2 deletions resources/views/find.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class="text-sm {{ $filter === \App\Services\PostSharing\SharingChannel::LOBSTERS
color="#fcf4f5"
border-color="#fe2977"
class="text-sm {{ $filter === \App\Services\PostSharing\SharingChannel::AGGREGATE ? 'font-bold' : '' }}"
:url="action(\App\Http\Controllers\Posts\FindPostController::class, ['filter' => \App\Services\PostSharing\SharingChannel::AGGREGATE->value])">
:url="action(\App\Http\Controllers\Posts\FindPostsForAggregateController::class)">
Aggregate
</x-tag>
<form method="get" action="{{ action(\App\Http\Controllers\Posts\FindPostController::class) }}" class="text-sm">
Expand Down Expand Up @@ -90,7 +90,12 @@ class="text-sm {{ $filter === \App\Services\PostSharing\SharingChannel::AGGREGAT
</span>
</x-tag>
<x-tag
:url="action(\App\Http\Controllers\Posts\SnoozeShareController::class, ['post' => $post, 'channel' => $filter->value, 'permanent' => true])"
:url="action(\App\Http\Controllers\Posts\SnoozeShareController::class, [
'post' => $post,
'channel' => $filter->value,
'permanent' => true,
'back' => request()->url(),
])"
color="red"
>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" class="w-5 h-5 text-gray-500">
Expand Down
2 changes: 2 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\Http\Controllers\LatestMailController;
use App\Http\Controllers\Posts\DeletePostCommentController;
use App\Http\Controllers\Posts\FindPostController;
use App\Http\Controllers\Posts\FindPostsForAggregateController;
use App\Http\Controllers\Posts\HidePostController;
use App\Http\Controllers\Links\AdminLinksController;
use App\Http\Controllers\Links\CreateLinkController;
Expand Down Expand Up @@ -106,6 +107,7 @@
Route::get('/twitter/status', TwitterOAuthStatusController::class);

Route::get('/find', FindPostController::class);
Route::get('/find/aggregate', FindPostsForAggregateController::class);
Route::get('/info', AdminInfoController::class);
Route::get('/stats', StatsController::class);
Route::get('/posts', AdminPostsController::class);
Expand Down

0 comments on commit 2810767

Please sign in to comment.