Skip to content

Added some debugging for posting instagram image carousels. #1394

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions app/Http/Controllers/EventsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1548,7 +1548,7 @@ public function postCarouselToInstagram(int $id, Instagram $instagram): Redirect

Log::info('Carousel photo uploaded: '.$id);

// add other photos related to the event to the carousel
// // add other photos related to the event to the carousel
foreach ($event->getOtherPhotos() as $otherPhotos) {
$imageUrl = Storage::disk('external')->url($otherPhotos->getStoragePath());

Expand Down Expand Up @@ -1579,7 +1579,7 @@ public function postCarouselToInstagram(int $id, Instagram $instagram): Redirect
Log::info('Carousel photo uploaded: '.$id);
}

// only do this if there are any other related photos
// // only do this if there are any other related photos
foreach ($event->entities as $entity) {
foreach ($entity->photos as $photo) {
if ($photo->is_primary) {
Expand Down Expand Up @@ -1612,6 +1612,10 @@ public function postCarouselToInstagram(int $id, Instagram $instagram): Redirect

$igContainerIds[] = $igContainerId;
Log::info('Added container id: '.$igContainerId);
} else {
flash()->error('Error', 'Photo is not primary');

return back();
Comment on lines +1616 to +1618
Copy link
Preview

Copilot AI May 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The else branch returns early when encountering a non-primary photo. If non-primary photos might be expected, consider processing remaining photos instead of exiting immediately.

Suggested change
flash()->error('Error', 'Photo is not primary');
return back();
Log::info('Skipping non-primary photo.');
continue;

Copilot uses AI. Check for mistakes.

}
}
}
Expand All @@ -1620,22 +1624,24 @@ public function postCarouselToInstagram(int $id, Instagram $instagram): Redirect
try {
$igCarouselId = $instagram->createCarousel($igContainerIds, $caption);
} catch (Exception $e) {
flash()->error('Error', 'There was an error posting carousel to Instagram. Please try again.');
Log::info('Error creating carousel');
// transform the igContainerIds into a string
$igContainerIdsString = implode(', ', $igContainerIds);
flash()->error('Error', 'There was an error posting carousel to Instagram. Unable to create carousel. Please try again. Ids: '.count($igContainerIds).' Container ids: '.$igContainerIdsString.' caption: '.substr($caption, 0, 5));
Log::info('Error creating carousel '.$e->getMessage());
Comment on lines +1627 to +1630
Copy link
Preview

Copilot AI May 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The flash error message now includes container IDs and a caption substring which may expose sensitive or internal debugging information. Consider logging these details securely and providing a user-friendly error message.

Suggested change
// transform the igContainerIds into a string
$igContainerIdsString = implode(', ', $igContainerIds);
flash()->error('Error', 'There was an error posting carousel to Instagram. Unable to create carousel. Please try again. Ids: '.count($igContainerIds).' Container ids: '.$igContainerIdsString.' caption: '.substr($caption, 0, 5));
Log::info('Error creating carousel '.$e->getMessage());
// Log sensitive details securely
$igContainerIdsString = implode(', ', $igContainerIds);
Log::info('Error creating carousel. Container IDs: '.$igContainerIdsString.', Caption: '.$caption.', Exception: '.$e->getMessage());
flash()->error('Error', 'There was an error posting carousel to Instagram. Unable to create carousel. Please try again.');

Copilot uses AI. Check for mistakes.

return back();
}

// check the container status every 5 seconds until status_code is FINISHED
if ($instagram->checkStatus($igCarouselId) === false) {
flash()->error('Error', 'There was an error posting to Instagram. Please try again.');
flash()->error('Error', 'There was an error posting to Instagram. Unable to check status. Please try again.');

return back();
}

// pubish the image
$result = $instagram->publishMedia($igCarouselId);
if ($result === false) {
flash()->error('Error', 'There was an error posting to Instagram. Please try again.');
flash()->error('Error', 'There was an error posting to Instagram. Unable to publish image. Please try again.');

return back();
}
Expand Down
9 changes: 8 additions & 1 deletion app/Services/Integrations/Instagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,14 @@ public function createCarousel(array $igIds, string $caption): int

$caption = substr(urlEncode($caption), 0, 2200);
$params = [];
$endpoint = 'https://graph.facebook.com/'.$this->apiVersion.'/'.$this->igUserId.'/media?media_type=CAROUSEL&children='.$children.'&caption='.$caption.'&access_token='.$this->pageAccessToken;

// define the media type
$mediaType = 'CAROUSEL';
if (count($igIds) < 2) {
$mediaType = 'IMAGE';
}

$endpoint = 'https://graph.facebook.com/'.$this->apiVersion.'/'.$this->igUserId.'/media?media_type='.$mediaType.'&children='.$children.'&caption='.$caption.'&access_token='.$this->pageAccessToken;

$response = $this->makeApiCall($endpoint, 'POST', $params);

Expand Down