diff --git a/app/Models/ImportPost.php b/app/Models/ImportPost.php new file mode 100644 index 000000000..075b63111 --- /dev/null +++ b/app/Models/ImportPost.php @@ -0,0 +1,17 @@ + 'array', + 'creation_date' => 'datetime', + 'metadata' => 'json' + ]; +} diff --git a/app/Services/ImportService.php b/app/Services/ImportService.php new file mode 100644 index 000000000..5216f3947 --- /dev/null +++ b/app/Services/ImportService.php @@ -0,0 +1,89 @@ + 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); + } +} diff --git a/database/migrations/2023_06_10_031634_create_import_posts_table.php b/database/migrations/2023_06_10_031634_create_import_posts_table.php new file mode 100644 index 000000000..eea0b5114 --- /dev/null +++ b/database/migrations/2023_06_10_031634_create_import_posts_table.php @@ -0,0 +1,45 @@ +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'); + } +};