Update (keywords): new logic

This commit is contained in:
2023-11-23 02:15:41 +08:00
parent d2c2059531
commit b2762ecef2
15 changed files with 211 additions and 198 deletions

View File

@@ -25,7 +25,7 @@ public function home(Request $request)
// $query->whereNotIn('id', $featured_posts->pluck('id')->toArray()); // $query->whereNotIn('id', $featured_posts->pluck('id')->toArray());
// })->where('status', 'publish')->where('published_at', '<=', now())->orderBy('published_at', 'desc')->limit(10)->get(); // })->where('status', 'publish')->where('published_at', '<=', now())->orderBy('published_at', 'desc')->limit(10)->get();
$rss_posts = RssPost::where('status', 'published')->orderBy('published_at', 'desc')->paginate(15); $rss_posts = RssPost::with('entities_keywords')->where('status', 'published')->orderBy('published_at', 'desc')->paginate(15);
return response(view('front.welcome', compact('rss_posts')), 200); return response(view('front.welcome', compact('rss_posts')), 200);
} }

View File

@@ -40,7 +40,7 @@ public function searchResults(Request $request, $query)
SEOTools::jsonLd(); SEOTools::jsonLd();
SEOTools::setTitle($title, false); SEOTools::setTitle($title, false);
$rss_posts = RssPost::with('category') $rss_posts = RssPost::with('category', 'entities_keywords')
->where('status', 'published') ->where('status', 'published')
->whereRaw("to_tsvector('english', title || ' ' || bites || ' ' || keyword_list) @@ plainto_tsquery('english', ?)", [trim(preg_replace('/\s+/', ' ', $query))]) ->whereRaw("to_tsvector('english', title || ' ' || bites || ' ' || keyword_list) @@ plainto_tsquery('english', ?)", [trim(preg_replace('/\s+/', ' ', $query))])
->where('published_at', '<=', now()) ->where('published_at', '<=', now())
@@ -81,7 +81,7 @@ public function index(Request $request)
SEOTools::jsonLd(); SEOTools::jsonLd();
SEOTools::setTitle($title, false); SEOTools::setTitle($title, false);
$rss_posts = RssPost::with('category')->where('status', 'published') $rss_posts = RssPost::with('category', 'entities_keywords')->where('status', 'published')
->where('published_at', '<=', now()) ->where('published_at', '<=', now())
->orderBy('published_at', 'desc') ->orderBy('published_at', 'desc')
->cursorPaginate(60) ?? collect(); ->cursorPaginate(60) ?? collect();
@@ -128,7 +128,7 @@ public function category(Request $request, $category_slug)
SEOTools::jsonLd(); SEOTools::jsonLd();
SEOTools::setTitle($title, false); SEOTools::setTitle($title, false);
$rss_posts = RssPost::with('category')->where('status', 'published') $rss_posts = RssPost::with('category', 'entities_keywords')->where('status', 'published')
->where('category_id', $category->id) ->where('category_id', $category->id)
->where('published_at', '<=', now()) ->where('published_at', '<=', now())
->orderBy('published_at', 'desc') ->orderBy('published_at', 'desc')

View File

@@ -30,8 +30,6 @@ public function prm(Request $request)
} }
public function crawlTask(Request $request) public function crawlTask(Request $request)
{ {
$id = $request->input('id'); $id = $request->input('id');
@@ -46,7 +44,6 @@ public function crawlTask(Request $request)
} }
public function opml(Request $request) public function opml(Request $request)
{ {
$raw_posts = BrowseRSSLatestNewsTask::handleSingle('https://hnrss.org/newest?q=ai', 240); $raw_posts = BrowseRSSLatestNewsTask::handleSingle('https://hnrss.org/newest?q=ai', 240);

View File

@@ -2,7 +2,6 @@
namespace App\Jobs; namespace App\Jobs;
use App\Jobs\Tasks\CrawlRssPostTask;
use App\Models\RssPost; use App\Models\RssPost;
use App\Models\RssPostKeyword; use App\Models\RssPostKeyword;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
@@ -34,13 +33,11 @@ public function handle(): void
{ {
$rss_post = RssPost::find($this->rss_post_id); $rss_post = RssPost::find($this->rss_post_id);
if (is_null($rss_post)) if (is_null($rss_post)) {
{
return; return;
} }
if ($rss_post->keyword_saved == true) if ($rss_post->keyword_saved == true) {
{
return; return;
} }
@@ -77,10 +74,8 @@ public function handle(): void
$word = trim($word); $word = trim($word);
foreach($words_to_save as $saved_word) foreach ($words_to_save as $saved_word) {
{ if (strtolower($word) == $saved_word->value_lowercased) {
if (strtolower($word) == $saved_word->value_lowercased)
{
continue 2; continue 2;
} }
} }
@@ -106,14 +101,12 @@ public function handle(): void
$rss_post->status = 'published'; $rss_post->status = 'published';
if($rss_post->save()) if ($rss_post->save()) {
{
$has_saved_keyword = false; $has_saved_keyword = false;
$deleted_rpk = RssPostKeyword::where('rss_post_id', $rss_post->id)->delete(); $deleted_rpk = RssPostKeyword::where('rss_post_id', $rss_post->id)->delete();
foreach ($words_to_save as $word_to_save) foreach ($words_to_save as $word_to_save) {
{
$new_rpk = new RssPostKeyword; $new_rpk = new RssPostKeyword;
$new_rpk->rss_post_id = $rss_post->id; $new_rpk->rss_post_id = $rss_post->id;
@@ -124,18 +117,15 @@ public function handle(): void
$new_rpk->created_at = $rss_post->published_at; $new_rpk->created_at = $rss_post->published_at;
$new_rpk->updated_at = $rss_post->published_at; $new_rpk->updated_at = $rss_post->published_at;
if($new_rpk->save()) if ($new_rpk->save()) {
{ if (! $has_saved_keyword) {
if (!$has_saved_keyword)
{
$has_saved_keyword = true; $has_saved_keyword = true;
} }
} }
} }
if ($has_saved_keyword) if ($has_saved_keyword) {
{
$rss_post->keyword_saved = true; $rss_post->keyword_saved = true;
$rss_post->save(); $rss_post->save();
} }

View File

@@ -82,15 +82,12 @@ public static function handle(int $rss_post_id)
$word = trim($word); $word = trim($word);
foreach($words_to_save as $saved_word) foreach ($words_to_save as $saved_word) {
{ if (strtolower($word) == $saved_word->value_lowercased) {
if (strtolower($word) == $saved_word->value_lowercased)
{
continue 2; continue 2;
} }
} }
$words_to_save[] = (object) [ $words_to_save[] = (object) [
'is_main' => ! $first_keyword_found, 'is_main' => ! $first_keyword_found,
'type' => 'keyword', 'type' => 'keyword',
@@ -146,14 +143,12 @@ public static function handle(int $rss_post_id)
$rss_post->status = 'published'; $rss_post->status = 'published';
if($rss_post->save()) if ($rss_post->save()) {
{
$has_saved_keyword = false; $has_saved_keyword = false;
$deleted_rpk = RssPostKeyword::where('rss_post_id', $rss_post->id)->delete(); $deleted_rpk = RssPostKeyword::where('rss_post_id', $rss_post->id)->delete();
foreach ($words_to_save as $word_to_save) foreach ($words_to_save as $word_to_save) {
{
$new_rpk = new RssPostKeyword; $new_rpk = new RssPostKeyword;
$new_rpk->rss_post_id = $rss_post->id; $new_rpk->rss_post_id = $rss_post->id;
@@ -162,18 +157,15 @@ public static function handle(int $rss_post_id)
$new_rpk->value = $word_to_save->value; $new_rpk->value = $word_to_save->value;
$new_rpk->value_lowercased = $word_to_save->value_lowercased; $new_rpk->value_lowercased = $word_to_save->value_lowercased;
if($new_rpk->save()) if ($new_rpk->save()) {
{ if (! $has_saved_keyword) {
if (!$has_saved_keyword)
{
$has_saved_keyword = true; $has_saved_keyword = true;
} }
} }
} }
if ($has_saved_keyword) if ($has_saved_keyword) {
{
$rss_post->keyword_saved = true; $rss_post->keyword_saved = true;
$rss_post->save(); $rss_post->save();
} }

View File

@@ -69,6 +69,11 @@ class RssPost extends Model implements Feedable
'keyword_saved', 'keyword_saved',
]; ];
public function entities_keywords()
{
return $this->hasMany(RssPostKeyword::class);
}
public function category() public function category()
{ {
return $this->belongsTo(Category::class); return $this->belongsTo(Category::class);

View File

@@ -20,10 +20,7 @@
* @property string $value_lowercased * @property string $value_lowercased
* @property Carbon|null $created_at * @property Carbon|null $created_at
* @property Carbon|null $updated_at * @property Carbon|null $updated_at
*
* @property RssPost $rss_post * @property RssPost $rss_post
*
* @package App\Models
*/ */
class RssPostKeyword extends Model class RssPostKeyword extends Model
{ {
@@ -31,7 +28,7 @@ class RssPostKeyword extends Model
protected $casts = [ protected $casts = [
'rss_post_id' => 'int', 'rss_post_id' => 'int',
'is_main' => 'bool' 'is_main' => 'bool',
]; ];
protected $fillable = [ protected $fillable = [
@@ -39,7 +36,7 @@ class RssPostKeyword extends Model
'type', 'type',
'is_main', 'is_main',
'value', 'value',
'value_lowercased' 'value_lowercased',
]; ];
public function rss_post() public function rss_post()

View File

@@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('rss_post_keywords', function (Blueprint $table) {
$table->index('created_at');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('rss_post_keywords', function (Blueprint $table) {
$table->dropIndex(['created_at']);
});
}
};

View File

@@ -101,3 +101,7 @@ @keyframes shimmer {
background-position: -400% 0; background-position: -400% 0;
} }
} }
.word-wrap-break-word {
word-wrap: break-word;
}

View File

@@ -12,17 +12,19 @@
<div class="d-flex flex-wrap mb-1"> <div class="d-flex flex-wrap mb-1">
@if ($post->entities) @if ($post->entities)
@foreach ($post->entities as $key => $keyword) @foreach ($post->entities_keywords as $keyword)
@if ($key == 0) @if($keyword->type == 'entity')
@if ($keyword->is_main)
<h4 <h4
class="mb-1 pb-1 d-inline badge bg-secondary border-secondary text-white border me-1 small fw-bold"> class="mb-1 pb-1 d-inline badge bg-secondary border-secondary text-white border me-1 small fw-bold">
{{ $keyword }} {{ $keyword->value }}
</h4> </h4>
@else @else
<h4 class="mb-1 pb-1 d-inline badge text-bg-light border me-1 small fw-normal"> <h4 class="mb-1 pb-1 d-inline badge text-bg-light border me-1 small fw-normal">
{{ $keyword }} {{ $keyword->value }}
</h4> </h4>
@endif @endif
@endif
@endforeach @endforeach
@endif @endif
@if ($post->impact_level == 'high') @if ($post->impact_level == 'high')
@@ -68,10 +70,10 @@ class="font-family-roboto-condensed mb-1 pb-1 d-inline badge bg-danger border-da
</div> </div>
@endif @endif
@if ($post->entities) @if ($post->entities)
<div class="mb-2"> <div class="mb-2 text-wrap">
More about: @foreach ($post->all_keywords as $keyword) More about: @foreach ($post->entities_keywords as $keyword)
<a class="ms-1" <a class="word-wrap-break-word ms-2"
href="{{ get_route_search_result($keyword) }}">{{ $keyword }}</a> href="{{ get_route_search_result($keyword->value_lowercased) }}">#{{ $keyword->value_lowercased }}</a>
@endforeach @endforeach
</div> </div>
@endif @endif

View File

@@ -1,8 +1,7 @@
<aside> <aside>
<form class="d-flex mb-3" action="{{ route('front.search') }}" method="POST"> <form class="d-flex mb-3" action="{{ route('front.search') }}" method="POST">
@csrf @csrf
<input name="query" class="form-control me-2" type="search" placeholder="Type a keyword..." <input name="query" class="form-control me-2" type="search" placeholder="Type a keyword..." aria-label="Search">
aria-label="Search">
<button class="btn btn-outline-dark border-2" type="submit"> <button class="btn btn-outline-dark border-2" type="submit">
<i class="bi bi-search"></i> Search <i class="bi bi-search"></i> Search
</button> </button>

View File

@@ -17,7 +17,6 @@
Route::get('/', [App\Http\Controllers\Front\FrontHomeController::class, 'home'])->name('front.home')->middleware('cacheResponse:1800'); Route::get('/', [App\Http\Controllers\Front\FrontHomeController::class, 'home'])->name('front.home')->middleware('cacheResponse:1800');
Route::get('/terms', [App\Http\Controllers\Front\FrontHomeController::class, 'terms'])->name('front.terms')->middleware('cacheResponse:2630000'); Route::get('/terms', [App\Http\Controllers\Front\FrontHomeController::class, 'terms'])->name('front.terms')->middleware('cacheResponse:2630000');
Route::get('/privacy', [App\Http\Controllers\Front\FrontHomeController::class, 'privacy'])->name('front.privacy')->middleware('cacheResponse:2630000'); Route::get('/privacy', [App\Http\Controllers\Front\FrontHomeController::class, 'privacy'])->name('front.privacy')->middleware('cacheResponse:2630000');