forked from mirror/pixelfed
Add Related Hashtags
This commit is contained in:
parent
051eb962e1
commit
175203089b
3 changed files with 91 additions and 0 deletions
22
app/Models/HashtagRelated.php
Normal file
22
app/Models/HashtagRelated.php
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class HashtagRelated extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The attributes that should be mutated to dates and other custom formats.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $casts = [
|
||||||
|
'related_tags' => 'array',
|
||||||
|
'last_calculated_at' => 'datetime',
|
||||||
|
'last_moderated_at' => 'datetime',
|
||||||
|
];
|
||||||
|
}
|
36
app/Services/HashtagRelatedService.php
Normal file
36
app/Services/HashtagRelatedService.php
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Services;
|
||||||
|
|
||||||
|
use DB;
|
||||||
|
use App\StatusHashtag;
|
||||||
|
use App\Models\HashtagRelated;
|
||||||
|
|
||||||
|
class HashtagRelatedService
|
||||||
|
{
|
||||||
|
|
||||||
|
public static function get($id)
|
||||||
|
{
|
||||||
|
return HashtagRelated::whereHashtagId($id)->first();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function fetchRelatedTags($tag)
|
||||||
|
{
|
||||||
|
$res = StatusHashtag::query()
|
||||||
|
->select('h2.name', DB::raw('COUNT(*) as related_count'))
|
||||||
|
->join('status_hashtags as hs2', function ($join) {
|
||||||
|
$join->on('status_hashtags.status_id', '=', 'hs2.status_id')
|
||||||
|
->whereRaw('status_hashtags.hashtag_id != hs2.hashtag_id')
|
||||||
|
->where('status_hashtags.created_at', '>', now()->subMonths(3));
|
||||||
|
})
|
||||||
|
->join('hashtags as h1', 'status_hashtags.hashtag_id', '=', 'h1.id')
|
||||||
|
->join('hashtags as h2', 'hs2.hashtag_id', '=', 'h2.id')
|
||||||
|
->where('h1.name', '=', $tag)
|
||||||
|
->groupBy('h2.name')
|
||||||
|
->orderBy('related_count', 'desc')
|
||||||
|
->limit(10)
|
||||||
|
->get();
|
||||||
|
|
||||||
|
return $res;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
<?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::create('hashtag_related', function (Blueprint $table) {
|
||||||
|
$table->bigIncrements('id');
|
||||||
|
$table->bigInteger('hashtag_id')->unsigned()->unique()->index();
|
||||||
|
$table->json('related_tags')->nullable();
|
||||||
|
$table->bigInteger('agg_score')->unsigned()->nullable()->index();
|
||||||
|
$table->timestamp('last_calculated_at')->nullable()->index();
|
||||||
|
$table->timestamp('last_moderated_at')->nullable()->index();
|
||||||
|
$table->boolean('skip_refresh')->default(false)->index();
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('hashtag_related');
|
||||||
|
}
|
||||||
|
};
|
Loading…
Reference in a new issue