From a8d3fff648c4c6f5be3e37c09f9168e5b7c0e8f8 Mon Sep 17 00:00:00 2001
From: Marcel Klehr <mklehr@gmx.net>
Date: Thu, 13 Apr 2023 12:12:07 +0200
Subject: Split TranscriptionFinishedEvent into Successful and Failed events

Signed-off-by: Marcel Klehr <mklehr@gmx.net>
---
 lib/private/SpeechToText/TranscriptionJob.php | 28 +++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)

(limited to 'lib/private/SpeechToText/TranscriptionJob.php')

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']
 				)
 			);
 		}
-- 
cgit v1.2.3