Move cleanOutdated
This commit is contained in:
parent
a5aec87b86
commit
5c5526f4c7
|
@ -6,7 +6,6 @@ use Carbon\Carbon;
|
||||||
use GuzzleHttp\Client;
|
use GuzzleHttp\Client;
|
||||||
use Zoomyboy\Social\Models\Page;
|
use Zoomyboy\Social\Models\Page;
|
||||||
use Zoomyboy\Social\Models\Post;
|
use Zoomyboy\Social\Models\Post;
|
||||||
use Zoomyboy\Social\Models\Setting;
|
|
||||||
|
|
||||||
class FacebookService extends SocialService {
|
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) {
|
public function saveAttachments(Post $post, $data) {
|
||||||
foreach($data as $i => $attachment) {
|
foreach($data as $i => $attachment) {
|
||||||
$fid = null;
|
$fid = null;
|
||||||
|
@ -76,8 +70,6 @@ class FacebookService extends SocialService {
|
||||||
|
|
||||||
public function sync(): void
|
public function sync(): void
|
||||||
{
|
{
|
||||||
$this->saveCover();
|
|
||||||
|
|
||||||
foreach ($this->posts() as $post) {
|
foreach ($this->posts() as $post) {
|
||||||
if (!data_get($post, 'message')) {
|
if (!data_get($post, 'message')) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -105,13 +97,7 @@ class FacebookService extends SocialService {
|
||||||
$this->saveAttachments($existing, data_get($post, 'attachments.data'));
|
$this->saveAttachments($existing, data_get($post, 'attachments.data'));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!is_numeric(Setting::get('max_posts'))) {
|
$this->cleanOutdated();
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function get(string $url, array $query = [], ?string $return = null): array
|
private function get(string $url, array $query = [], ?string $return = null): array
|
||||||
|
|
|
@ -89,7 +89,7 @@ class InstagramService extends SocialService {
|
||||||
return json_decode((string) $response->getBody(), true);
|
return json_decode((string) $response->getBody(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function sync(): array
|
public function sync(): void
|
||||||
{
|
{
|
||||||
foreach ($this->posts()['data'] as $image) {
|
foreach ($this->posts()['data'] as $image) {
|
||||||
$payload = [
|
$payload = [
|
||||||
|
@ -112,6 +112,8 @@ class InstagramService extends SocialService {
|
||||||
'type' => 'image',
|
'type' => 'image',
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->cleanOutdated();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ use Event;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Media\Classes\MediaLibrary;
|
use Media\Classes\MediaLibrary;
|
||||||
use Zoomyboy\Social\Models\Page;
|
use Zoomyboy\Social\Models\Page;
|
||||||
|
use Zoomyboy\Social\Models\Setting;
|
||||||
|
|
||||||
abstract class SocialService {
|
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();
|
return Page::where('type', $this->getType())->get();
|
||||||
}
|
}
|
||||||
|
@ -49,6 +50,17 @@ abstract class SocialService {
|
||||||
$this->page->delete();
|
$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
|
public function saveUrl(string $source, ?string $filename = null): string
|
||||||
{
|
{
|
||||||
$filename = $filename ?: pathinfo(parse_url($source, PHP_URL_PATH), PATHINFO_BASENAME);
|
$filename = $filename ?: pathinfo(parse_url($source, PHP_URL_PATH), PATHINFO_BASENAME);
|
||||||
|
|
|
@ -26,8 +26,8 @@ class SocialRefresh extends Command
|
||||||
*/
|
*/
|
||||||
public function handle()
|
public function handle()
|
||||||
{
|
{
|
||||||
Page::get()->each(function($account) {
|
app(InstagramService::class)->pages()->each(function($page) {
|
||||||
app(InstagramService::class)->refresh($account);
|
app(InstagramService::class)->refresh($page);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,17 +17,12 @@ class Setting extends Model
|
||||||
|
|
||||||
public function getFacebookPagesOptions(): array
|
public function getFacebookPagesOptions(): array
|
||||||
{
|
{
|
||||||
return Page::where('type', app(FacebookService::class)->getType())->pluck('name', 'id')->toArray();
|
return app(FacebookService::class)->pages()->pluck('name', 'id')->toArray();
|
||||||
}
|
|
||||||
|
|
||||||
public static function synchedPages(): array
|
|
||||||
{
|
|
||||||
return (new static([]))->getSynchedPagesOptions();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getInstagramUsersOptions(): array
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue