-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
114 additions
and
3 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
app/Http/Controllers/Posts/FindPostsForAggregateController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
database/migrations/2023_08_07_102153_add_visits_to_sources.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
]); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters