diff --git a/app/Models/HashtagRelated.php b/app/Models/HashtagRelated.php new file mode 100644 index 000000000..42722a205 --- /dev/null +++ b/app/Models/HashtagRelated.php @@ -0,0 +1,22 @@ + 'array', + 'last_calculated_at' => 'datetime', + 'last_moderated_at' => 'datetime', + ]; +} diff --git a/app/Services/HashtagRelatedService.php b/app/Services/HashtagRelatedService.php new file mode 100644 index 000000000..53a387b45 --- /dev/null +++ b/app/Services/HashtagRelatedService.php @@ -0,0 +1,36 @@ +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; + } +} diff --git a/database/migrations/2023_11_16_124107_create_hashtag_related_table.php b/database/migrations/2023_11_16_124107_create_hashtag_related_table.php new file mode 100644 index 000000000..33d7494d8 --- /dev/null +++ b/database/migrations/2023_11_16_124107_create_hashtag_related_table.php @@ -0,0 +1,33 @@ +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'); + } +};