diff --git a/classes/FacebookService.php b/classes/FacebookService.php index 874ce0f..e752887 100644 --- a/classes/FacebookService.php +++ b/classes/FacebookService.php @@ -6,7 +6,6 @@ use Carbon\Carbon; use GuzzleHttp\Client; use Zoomyboy\Social\Models\Page; use Zoomyboy\Social\Models\Post; -use Zoomyboy\Social\Models\Setting; class FacebookService extends SocialService { @@ -36,11 +35,6 @@ class FacebookService extends SocialService { ); } - public function saveCover() { - $cover = $this->get($this->page->remote_id, ['fields' => 'cover'], 'cover'); - $this->page->update([ 'cover' => $this->saveUrl($cover['source']) ]); - } - public function saveAttachments(Post $post, $data) { foreach($data as $i => $attachment) { $fid = null; @@ -76,8 +70,6 @@ class FacebookService extends SocialService { public function sync(): void { - $this->saveCover(); - foreach ($this->posts() as $post) { if (!data_get($post, 'message')) { continue; @@ -105,13 +97,7 @@ class FacebookService extends SocialService { $this->saveAttachments($existing, data_get($post, 'attachments.data')); } - if (!is_numeric(Setting::get('max_posts'))) { - return; - } - - while ($this->page->posts()->get()->count() > 0 && $this->page->posts()->get()->count() > Setting::get('max_posts')) { - $this->page->posts()->orderBy('created_at')->first()->delete(); - } + $this->cleanOutdated(); } private function get(string $url, array $query = [], ?string $return = null): array diff --git a/classes/InstagramService.php b/classes/InstagramService.php index 0fdb70d..87811e7 100644 --- a/classes/InstagramService.php +++ b/classes/InstagramService.php @@ -89,7 +89,7 @@ class InstagramService extends SocialService { return json_decode((string) $response->getBody(), true); } - public function sync(): array + public function sync(): void { foreach ($this->posts()['data'] as $image) { $payload = [ @@ -112,6 +112,8 @@ class InstagramService extends SocialService { 'type' => 'image', ]); } + + $this->cleanOutdated(); } } diff --git a/classes/SocialService.php b/classes/SocialService.php index e32e9ea..c3a00bc 100644 --- a/classes/SocialService.php +++ b/classes/SocialService.php @@ -6,6 +6,7 @@ use Event; use Illuminate\Support\Collection; use Media\Classes\MediaLibrary; use Zoomyboy\Social\Models\Page; +use Zoomyboy\Social\Models\Setting; abstract class SocialService { @@ -40,7 +41,7 @@ abstract class SocialService { } } - protected function pages(): Collection + public function pages(): Collection { return Page::where('type', $this->getType())->get(); } @@ -49,6 +50,17 @@ abstract class SocialService { $this->page->delete(); } + public function cleanOutdated(): void + { + if (!is_numeric(Setting::get('max_posts'))) { + return; + } + + while ($this->page->posts()->count() > 0 && $this->page->posts()->count() > Setting::get('max_posts')) { + $this->page->posts()->orderBy('created_at')->first()->delete(); + } + } + public function saveUrl(string $source, ?string $filename = null): string { $filename = $filename ?: pathinfo(parse_url($source, PHP_URL_PATH), PATHINFO_BASENAME); diff --git a/console/SocialRefresh.php b/console/SocialRefresh.php index 9faaa72..11d7a16 100644 --- a/console/SocialRefresh.php +++ b/console/SocialRefresh.php @@ -26,8 +26,8 @@ class SocialRefresh extends Command */ public function handle() { - Page::get()->each(function($account) { - app(InstagramService::class)->refresh($account); + app(InstagramService::class)->pages()->each(function($page) { + app(InstagramService::class)->refresh($page); }); } diff --git a/models/Setting.php b/models/Setting.php index c692d05..d425cdc 100644 --- a/models/Setting.php +++ b/models/Setting.php @@ -17,17 +17,12 @@ class Setting extends Model public function getFacebookPagesOptions(): array { - return Page::where('type', app(FacebookService::class)->getType())->pluck('name', 'id')->toArray(); - } - - public static function synchedPages(): array - { - return (new static([]))->getSynchedPagesOptions(); + return app(FacebookService::class)->pages()->pluck('name', 'id')->toArray(); } public function getInstagramUsersOptions(): array { - return Page::where('type', app(InstagramService::class)->getType())->pluck('name', 'id')->toArray(); + return app(InstagramService::class)->pages()->pluck('name', 'id')->toArray(); } }