Add ImportPost model, migration and service

This commit is contained in:
Daniel Supernault 2023-06-12 02:51:29 -06:00
parent c50e0966db
commit 8c9f4da48a
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7
3 changed files with 151 additions and 0 deletions

17
app/Models/ImportPost.php Normal file
View file

@ -0,0 +1,17 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class ImportPost extends Model
{
use HasFactory;
protected $casts = [
'media' => 'array',
'creation_date' => 'datetime',
'metadata' => 'json'
];
}

View file

@ -0,0 +1,89 @@
<?php
namespace App\Services;
use App\Models\ImportPost;
use Cache;
class ImportService
{
const CACHE_KEY = 'pf:import-service:';
public static function getId($userId, $year, $month, $day)
{
if($userId > 999999) {
return;
}
if($year < 9 || $year > 23) {
return;
}
if($month < 1 || $month > 12) {
return;
}
if($day < 1 || $day > 31) {
return;
}
$start = 1;
$key = self::CACHE_KEY . 'getIdRange:incr:byUserId:' . $userId . ':y-' . $year . ':m-' . $month . ':d-' . $day;
$incr = Cache::increment($key, random_int(3, 19));
if($incr > 999) {
$daysInMonth = now()->parse($day . '-' . $month . '-' . $year)->daysInMonth;
if($month == 12) {
$year = $year + 1;
$month = 1;
$day = 0;
}
if($day + 1 >= $daysInMonth) {
$day = 1;
$month = $month + 1;
} else {
$day = $day + 1;
}
return self::getId($userId, $year, $month, $day);
}
$uid = str_pad($userId, 6, 0, STR_PAD_LEFT);
$year = str_pad($year, 2, 0, STR_PAD_LEFT);
$month = str_pad($month, 2, 0, STR_PAD_LEFT);
$day = str_pad($day, 2, 0, STR_PAD_LEFT);
$zone = $year . $month . $day . str_pad($incr, 3, 0, STR_PAD_LEFT);
return [
'id' => $start . $uid . $zone,
'year' => $year,
'month' => $month,
'day' => $day,
'incr' => $incr,
];
}
public static function getPostCount($profileId, $refresh = false)
{
$key = self::CACHE_KEY . 'totalPostCountByProfileId:' . $profileId;
if($refresh) {
Cache::forget($key);
}
return intval(Cache::remember($key, 21600, function() use($profileId) {
return ImportPost::whereProfileId($profileId)->count();
}));
}
public static function getAttempts($profileId)
{
$key = self::CACHE_KEY . 'attemptsByProfileId:' . $profileId;
return intval(Cache::remember($key, 21600, function() use($profileId) {
return ImportPost::whereProfileId($profileId)
->get()
->groupBy(function($item) {
return $item->created_at->format('Y-m-d');
})
->count();
}));
}
public static function clearAttempts($profileId)
{
$key = self::CACHE_KEY . 'attemptsByProfileId:' . $profileId;
return Cache::forget($key);
}
}

View file

@ -0,0 +1,45 @@
<?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('import_posts', function (Blueprint $table) {
$table->id();
$table->bigInteger('profile_id')->unsigned()->index();
$table->unsignedInteger('user_id')->index();
$table->string('service')->index();
$table->string('post_hash')->nullable()->index();
$table->string('filename')->index();
$table->tinyInteger('media_count')->unsigned();
$table->string('post_type')->nullable();
$table->text('caption')->nullable();
$table->json('media')->nullable();
$table->tinyInteger('creation_year')->unsigned()->nullable();
$table->tinyInteger('creation_month')->unsigned()->nullable();
$table->tinyInteger('creation_day')->unsigned()->nullable();
$table->tinyInteger('creation_id')->unsigned()->nullable();
$table->bigInteger('status_id')->unsigned()->nullable()->unique()->index();
$table->timestamp('creation_date')->nullable();
$table->json('metadata')->nullable();
$table->unique(['user_id', 'post_hash']);
$table->unique(['user_id', 'creation_year', 'creation_month', 'creation_day', 'creation_id'], 'import_posts_uid_phash_unique');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('import_posts');
}
};