From a8d3fff648c4c6f5be3e37c09f9168e5b7c0e8f8 Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Thu, 13 Apr 2023 12:12:07 +0200 Subject: [PATCH] Split TranscriptionFinishedEvent into Successful and Failed events Signed-off-by: Marcel Klehr --- .../SpeechToText/SpeechToTextManager.php | 5 ++- lib/private/SpeechToText/TranscriptionJob.php | 28 ++++++++++------ ...ent.php => AbstractTranscriptionEvent.php} | 32 +++---------------- .../Events/TranscriptionFailedEvent.php | 24 ++++++++++++++ .../Events/TranscriptionSuccessfulEvent.php | 26 +++++++++++++++ 5 files changed, 74 insertions(+), 41 deletions(-) rename lib/public/SpeechToText/Events/{TranscriptionFinishedEvent.php => AbstractTranscriptionEvent.php} (66%) create mode 100644 lib/public/SpeechToText/Events/TranscriptionFailedEvent.php create mode 100644 lib/public/SpeechToText/Events/TranscriptionSuccessfulEvent.php diff --git a/lib/private/SpeechToText/SpeechToTextManager.php b/lib/private/SpeechToText/SpeechToTextManager.php index a734f6bb379..c4f89ed43c7 100644 --- a/lib/private/SpeechToText/SpeechToTextManager.php +++ b/lib/private/SpeechToText/SpeechToTextManager.php @@ -33,7 +33,6 @@ use OC\AppFramework\Bootstrap\Coordinator; use OCP\BackgroundJob\IJobList; use OCP\Files\File; use OCP\Files\InvalidPathException; -use OCP\Files\IRootFolder; use OCP\Files\NotFoundException; use OCP\IServerContainer; use OCP\PreConditionNotMetException; @@ -96,7 +95,7 @@ class SpeechToTextManager implements ISpeechToTextManager { } try { $this->jobList->add(TranscriptionJob::class, ['fileId' => $file->getId()]); - }catch(NotFoundException|InvalidPathException $e) { + } catch (NotFoundException|InvalidPathException $e) { throw new InvalidArgumentException('Invalid file provided for file transcription: ' . $e->getMessage()); } } @@ -109,7 +108,7 @@ class SpeechToTextManager implements ISpeechToTextManager { try { return $provider->transcribeFile($file); - }catch (\Throwable $e) { + } catch (\Throwable $e) { $this->logger->info('SpeechToText transcription failed', ['exception' => $e]); throw new \RuntimeException('SpeechToText transcription failed: ' . $e->getMessage()); } diff --git a/lib/private/SpeechToText/TranscriptionJob.php b/lib/private/SpeechToText/TranscriptionJob.php index 296a713593b..775a2a7fadc 100644 --- a/lib/private/SpeechToText/TranscriptionJob.php +++ b/lib/private/SpeechToText/TranscriptionJob.php @@ -5,9 +5,11 @@ namespace OC\SpeechToText; use OCP\AppFramework\Utility\ITimeFactory; use OCP\BackgroundJob\QueuedJob; use OCP\EventDispatcher\IEventDispatcher; +use OCP\Files\File; use OCP\Files\IRootFolder; use OCP\PreConditionNotMetException; -use OCP\SpeechToText\Events\TranscriptionFinishedEvent; +use OCP\SpeechToText\Events\TranscriptionFailedEvent; +use OCP\SpeechToText\Events\TranscriptionSuccessfulEvent; use OCP\SpeechToText\ISpeechToTextManager; class TranscriptionJob extends QueuedJob { @@ -25,24 +27,30 @@ class TranscriptionJob extends QueuedJob { * @inheritDoc */ protected function run($argument) { + $fileId = $argument['fileId']; try { - $file = $this->rootFolder->getById($argument['fileId']); + $file = current($this->rootFolder->getById($fileId)); + if (!($file instanceof File)) { + $this->eventDispatcher->dispatchTyped( + new TranscriptionFailedEvent( + $fileId, + 'File not found', + ) + ); + return; + } $result = $this->speechToTextManager->transcribeFile($file); $this->eventDispatcher->dispatchTyped( - new TranscriptionFinishedEvent( - true, + new TranscriptionSuccessfulEvent( + $fileId, $result, - '', - $argument['context'] ) ); } catch (PreConditionNotMetException|\RuntimeException|\InvalidArgumentException $e) { $this->eventDispatcher->dispatchTyped( - new TranscriptionFinishedEvent( - false, - '', + new TranscriptionFailedEvent( + $fileId, $e->getMessage(), - $argument['context'] ) ); } diff --git a/lib/public/SpeechToText/Events/TranscriptionFinishedEvent.php b/lib/public/SpeechToText/Events/AbstractTranscriptionEvent.php similarity index 66% rename from lib/public/SpeechToText/Events/TranscriptionFinishedEvent.php rename to lib/public/SpeechToText/Events/AbstractTranscriptionEvent.php index 245ccd413e9..5b00315f74c 100644 --- a/lib/public/SpeechToText/Events/TranscriptionFinishedEvent.php +++ b/lib/public/SpeechToText/Events/AbstractTranscriptionEvent.php @@ -30,15 +30,12 @@ use OCP\EventDispatcher\Event; /** * @since 27.0.0 */ -class TranscriptionFinishedEvent extends Event { +abstract class AbstractTranscriptionEvent extends Event { /** * @since 27.0.0 */ public function __construct( - private bool $successful, - private string $transcription, - private string $errorMessage, - private array $context + private int $fileIdId ) { parent::__construct(); } @@ -46,28 +43,7 @@ class TranscriptionFinishedEvent extends Event { /** * @since 27.0.0 */ - public function getContext(): array { - return $this->context; - } - - /** - * @since 27.0.0 - */ - public function isSuccessful(): bool { - return $this->successful; - } - - /** - * @since 27.0.0 - */ - public function getErrorMessage(): string { - return $this->errorMessage; - } - - /** - * @since 27.0.0 - */ - public function getTranscription(): string { - return $this->transcription; + public function getFileId(): int { + return $this->fileIdId; } } diff --git a/lib/public/SpeechToText/Events/TranscriptionFailedEvent.php b/lib/public/SpeechToText/Events/TranscriptionFailedEvent.php new file mode 100644 index 00000000000..3a490995c10 --- /dev/null +++ b/lib/public/SpeechToText/Events/TranscriptionFailedEvent.php @@ -0,0 +1,24 @@ +errorMessage; + } +} diff --git a/lib/public/SpeechToText/Events/TranscriptionSuccessfulEvent.php b/lib/public/SpeechToText/Events/TranscriptionSuccessfulEvent.php new file mode 100644 index 00000000000..63644b98f6d --- /dev/null +++ b/lib/public/SpeechToText/Events/TranscriptionSuccessfulEvent.php @@ -0,0 +1,26 @@ +transcript; + } +} -- 2.39.5