Skip to content

Commit

Permalink
Merge pull request #2 from julien-nc/feat/ironing_out
Browse files Browse the repository at this point in the history
fix AppAPI dep checking
  • Loading branch information
marcelklehr authored Nov 8, 2023
2 parents 44bad32 + 04c8fe3 commit 4a7ebb4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 38 deletions.
23 changes: 11 additions & 12 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,22 @@

use OCA\Cwyd\Listener\FileListener;
use OCA\Cwyd\TextProcessing\CwydTextProcessingProvider;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\Files\Cache\CacheEntryInsertedEvent;
use OCP\Files\Events\Node\BeforeNodeDeletedEvent;
use OCP\Files\Events\Node\NodeCreatedEvent;
use OCP\Files\Events\Node\NodeWrittenEvent;
use OCP\Files\Events\NodeRemovedFromCache;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\Share\Events\ShareCreatedEvent;
use OCP\Share\Events\ShareDeletedEvent;

class Application extends App implements IBootstrap {

public const APP_ID = 'cwyd';

public const LANGROPE_BASE_URL = 'http://localhost:8008';
public const CWYD_DEFAULT_REQUEST_TIMEOUT = 60 * 4;
// max size per file + max size of the batch of files to be embedded in a single request
public const CWYD_MAX_SIZE = 20 * 1024 * 1024; // 20MB
Expand All @@ -43,14 +42,14 @@ public function __construct(array $urlParams = []) {
}

public function register(IRegistrationContext $context): void {
$context->registerEventListener(BeforeNodeDeletedEvent::class, FileListener::class);
$context->registerEventListener(NodeCreatedEvent::class, FileListener::class);
$context->registerEventListener(ShareCreatedEvent::class, FileListener::class);
$context->registerEventListener(ShareDeletedEvent::class, FileListener::class);
$context->registerEventListener(CacheEntryInsertedEvent::class, FileListener::class);
$context->registerEventListener(NodeRemovedFromCache::class, FileListener::class);
$context->registerEventListener(NodeWrittenEvent::class, FileListener::class);
$context->registerTextProcessingProvider(CwydTextProcessingProvider::class);
$context->registerEventListener(BeforeNodeDeletedEvent::class, FileListener::class);
$context->registerEventListener(NodeCreatedEvent::class, FileListener::class);
$context->registerEventListener(ShareCreatedEvent::class, FileListener::class);
$context->registerEventListener(ShareDeletedEvent::class, FileListener::class);
$context->registerEventListener(CacheEntryInsertedEvent::class, FileListener::class);
$context->registerEventListener(NodeRemovedFromCache::class, FileListener::class);
$context->registerEventListener(NodeWrittenEvent::class, FileListener::class);
$context->registerTextProcessingProvider(CwydTextProcessingProvider::class);
}

public function boot(IBootContext $context): void {
Expand Down
59 changes: 33 additions & 26 deletions lib/Service/LangRopeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
* later. See the COPYING file.
*
* @author Julien Veyssier
* @author Anupam Kumar
* @author AppAPI Developers
* @copyright Julien Veyssier 2023
*/

namespace OCA\Cwyd\Service;

use DateTime;
use OCA\AppAPI\Db\ExApp;
use OCA\AppAPI\Service\AppAPIService;
use OCA\Cwyd\AppInfo\Application;
use OCA\Cwyd\Type\Source;
use OCP\App\IAppManager;
Expand All @@ -32,33 +31,43 @@ public function __construct(
private IL10N $l10n,
private IConfig $config,
private IAppManager $appManager,
private AppAPIService $appAPIService,
IClientService $clientService,
) {
$this->client = $clientService->newClient();
}

private function getExApp() {
if (!class_exists('\OCA\AppAPI\Db\ExApp')) {
$this->logger->error('ExApp class not found, please install the AppAPI app from the Nextcloud AppStore');
return null;
}

$exApp = \OCP\Server::get(\OCA\AppAPI\Service\AppAPIService::class)->getExApp('schackles');
return $exApp;
}

public function indexSources(string $userId, array $sources): void {
if (count($sources) === 0) {
return;
}

$params = [
...array_map(function (Source $source) {
return [
'name' => 'sources',
'filename' => $source->reference, // 'file: 555'
'contents' => $source->content,
'headers' => [
'userId' => $source->userId,
'type' => $source->type,
'modified' => $source->modified,
],
];
}, $sources),
];
$params = array_map(function (Source $source) {
return [
'name' => 'sources',
'filename' => $source->reference, // 'file: 555'
'contents' => $source->content,
'headers' => [
'userId' => $source->userId,
'type' => $source->type,
'modified' => $source->modified,
],
];
}, $sources);

$exApp = $this->appAPIService->getExApp('schackles');
$exApp = $this->getExApp();
if ($exApp === null) {
return;
}
$this->requestToExApp($userId, $exApp, '/loadSources', 'PUT', $params, 'multipart/form-data');
}

Expand All @@ -68,7 +77,10 @@ public function query(string $userId, string $prompt): array {
'userId' => $userId,
];

$exApp = $this->appAPIService->getExApp('schackles');
$exApp = $this->getExApp();
if ($exApp === null) {
return ['error' => $this->l10n->t('ExApp class not found, please install the AppAPI app from the Nextcloud AppStore')];
}
return $this->requestToExApp($userId, $exApp, '/query', 'GET', $params);
}

Expand All @@ -89,17 +101,12 @@ private static function getExAppUrl(string $protocol, string $host, int $port):
*/
public function requestToExApp(
?string $userId,
ExApp $exApp,
\OCA\AppAPI\Db\ExApp $exApp,
string $route,
string $method = 'POST',
array $params = [],
?string $contentType = null,
): array | IResponse | null {
if (!class_exists('\OCA\AppAPI\Db\ExApp')) {
$this->logger->error('ExApp class not found, please install the AppAPI app from the Nextcloud AppStore');
return ['error' => 'ExApp class not found, please install the AppAPI app from the Nextcloud AppStore'];
}

try {
$url = self::getExAppUrl(
$exApp->getProtocol(),
Expand Down
1 change: 1 addition & 0 deletions lib/Service/ScanService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* later. See the COPYING file.
*
* @author Julien Veyssier
* @author Anupam Kumar
* @copyright Julien Veyssier 2023
*/

Expand Down

0 comments on commit 4a7ebb4

Please sign in to comment.