Skip to content

Commit

Permalink
Add additional checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Tschucki committed Jul 24, 2024
1 parent eb45ce5 commit 84731d5
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions app/Jobs/CreateTldrCommentJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class CreateTldrCommentJob implements ShouldBeUnique, ShouldQueue

public function uniqueId(): string
{
return (string) $this->message->messageId;
return (string)$this->message->messageId;
}

public function __construct(Message $message)
Expand All @@ -62,20 +62,20 @@ public function handle(): void
$postInfo = Pr0grammApi::Post()->info($this->message->itemId);
$comment = collect($postInfo['comments'])->firstWhere('id', $this->message->messageId);

if (! $comment) {
if (!$comment) {
return;
}

// check if the comment has a parent
if ($comment['parent'] === 0) {
if ($comment['parent'] === 0 && $this->commentOnlyContainsMention($this->message->message)) {
$this->summarizePost();
} else {
/**
* @var array $commentToSummarize
* */
$commentToSummarize = $this->getCommentToSummarize($comment);

if (! $commentToSummarize || $this->commentIsMine($commentToSummarize)) {
if (!$commentToSummarize || $this->commentIsMine($commentToSummarize)) {
return;
}

Expand All @@ -84,7 +84,7 @@ public function handle(): void
if ($this->commentIsLongEnough($commentToSummarize['content'])) {
$summary = $this->getTldrValue($commentToSummarize['content']);
if ($summary !== null && $summary !== '' && Str::wordCount($summary) > 5) {
$tldrValue = "TLDR: \n".$summary;
$tldrValue = "TLDR: \n" . $summary;
}
} else if (random_int(0, 2) === 0) {
$tldrValue = $this->notLongEnoughTexts[array_rand($this->notLongEnoughTexts)];
Expand All @@ -101,7 +101,7 @@ public function handle(): void
->first();

if ($otherTldrComments !== null) {
$tldrValue = ' https://pr0gramm.com/new/'.$this->message->itemId.':comment'.$otherTldrComments->messageId;
$tldrValue = ' https://pr0gramm.com/new/' . $this->message->itemId . ':comment' . $otherTldrComments->messageId;
}

$this->addTldrComment($tldrValue);
Expand All @@ -116,25 +116,30 @@ public function handle(): void
}
}

protected function commentOnlyContainsMention(string $comment): bool
{
$comment = trim($comment);

return Str::lower($comment) === '@tldr';
}

protected function getPersonalizedNotLongEnoughText(string $name): string
{
$randomText = $this->notLongEnoughPersonalTexts[array_rand($this->notLongEnoughPersonalTexts)];
return Str::replace('$name', $name, $randomText);

Check failure on line 129 in app/Jobs/CreateTldrCommentJob.php

View workflow job for this annotation

GitHub Actions / phpstan

Method App\Jobs\CreateTldrCommentJob::getPersonalizedNotLongEnoughText() should return string but returns iterable<string>|string.

Check failure on line 129 in app/Jobs/CreateTldrCommentJob.php

View workflow job for this annotation

GitHub Actions / phpstan

Unable to resolve the template type TSubject in call to method static method Illuminate\Support\Str::replace()
}

protected function summarizePost()
protected function summarizePost(): void
{
// download the image and get the text from ocr if the post is an image
// check
$image = $this->message->image;
$imageUrl = 'https://img.pr0gramm.com/'.$image;
if ($imageUrl) {
$imageText = $this->getImageText($imageUrl);
if ($imageText) {
$tldrValue = $this->getTldrValue($imageText);
if ($tldrValue !== null && $tldrValue !== '' && Str::wordCount($tldrValue) > 5) {
$this->addTldrComment("TLDR: \n".$tldrValue);
}
$imageUrl = 'https://img.pr0gramm.com/' . $image;
$imageText = $this->getImageText($imageUrl);
if ($imageText && Str::wordCount($imageText) >= 40) {
$tldrValue = $this->getTldrValue($imageText);
if ($tldrValue !== null && $tldrValue !== '' && Str::wordCount($tldrValue) > 5) {
$this->addTldrComment("TLDR: \n" . $tldrValue);
}
}
}
Expand All @@ -149,7 +154,7 @@ protected function getImageText(string $imageUrl): ?string
$fileName = end($fileNameFragments);

// check if the image is a image
if (! Str::endsWith($fileName, ['.jpg', '.jpeg', '.png', '.tiff'])) {
if (!Str::endsWith($fileName, ['.jpg', '.jpeg', '.png', '.tiff'])) {
return null;
}

Expand Down Expand Up @@ -207,7 +212,7 @@ protected function getTldrValue(string $comment): ?string
$response = $client->chat()->create([
'model' => $this->getGptModel($comment),
'messages' => [
['role' => 'user', 'content' => $this->basePrompt.$comment],
['role' => 'user', 'content' => $this->basePrompt . $comment],
],
]);

Expand Down Expand Up @@ -261,6 +266,6 @@ protected function commentEndsWithPattern(string $comment): bool
// Pattern: Comments ends with @tldr but has at least 200 characters
$endsWithPattern = '/^.{200,}.*@tldr\s*$/is';

return (bool) preg_match($endsWithPattern, $comment);
return (bool)preg_match($endsWithPattern, $comment);
}
}

0 comments on commit 84731d5

Please sign in to comment.