Move cleanOutdated

This commit is contained in:
philipp lang 2021-10-30 13:32:09 +02:00
parent a5aec87b86
commit 5c5526f4c7
5 changed files with 21 additions and 26 deletions

View File

@ -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

View File

@ -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();
}
}

View File

@ -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);

View File

@ -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);
});
}

View File

@ -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();
}
}