1<?php
2/**
3 * @copyright Copyright (c) 2019 Julius Härtl <jus@bitgrid.net>
4 *
5 * @author Julius Härtl <jus@bitgrid.net>
6 *
7 * @license GNU AGPL version 3 or any later version
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as
11 * published by the Free Software Foundation, either version 3 of the
12 * License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24declare(strict_types=1);
25/**
26 * @copyright Copyright (c) 2019 Julius Härtl <jus@bitgrid.net>
27 *
28 * @author Julius Härtl <jus@bitgrid.net>
29 *
30 * @license GNU AGPL version 3 or any later version
31 *
32 * This program is free software: you can redistribute it and/or modify
33 * it under the terms of the GNU Affero General Public License as
34 * published by the Free Software Foundation, either version 3 of the
35 * License, or (at your option) any later version.
36 *
37 * This program is distributed in the hope that it will be useful,
38 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
40 * GNU Affero General Public License for more details.
41 *
42 * You should have received a copy of the GNU Affero General Public License
43 * along with this program. If not, see <http://www.gnu.org/licenses/>.
44 *
45 */
46
47namespace OCA\Text\Controller;
48
49use OC\Authentication\Exceptions\InvalidTokenException;
50use OCA\Text\Service\ApiService;
51use OCP\AppFramework\Controller;
52use OCP\AppFramework\Http\Response;
53use OCP\DirectEditing\IManager;
54use OCP\Share\IManager as ShareManager;
55use OCP\AppFramework\Http\DataResponse;
56use OCP\IRequest;
57use OCP\Share\IShare;
58
59class DirectSessionController extends Controller {
60
61	/** @var ShareManager */
62	private $shareManager;
63
64	/** @var IShare */
65	private $share;
66
67	/** @var ApiService */
68	private $apiService;
69	/**  @var IManager */
70	private $directManager;
71
72	public function __construct(string $appName, IRequest $request, ShareManager $shareManager, ApiService $apiService, IManager $directManager) {
73		parent::__construct($appName, $request);
74		$this->shareManager = $shareManager;
75		$this->apiService = $apiService;
76		$this->directManager = $directManager;
77	}
78
79	/**
80	 * @PublicPage
81	 */
82	public function create(string $token, string $file = null, $guestName = null, bool $forceRecreate = false): DataResponse {
83		try {
84			$tokenObject = $this->directManager->getToken($token);
85			$tokenObject->extend();
86			$tokenObject->useTokenScope();
87			$node = $tokenObject->getFile();
88			$node->touch();
89			return new DataResponse([
90				'mtime' => $node->getMTime()
91			]);
92		} catch (InvalidTokenException $e) {
93			return new DataResponse('error');
94		}
95		//return $this->apiService->create(null, $file, $token, $guestName, $forceRecreate);
96	}
97
98	/**
99	 * @NoAdminRequired
100	 * @PublicPage
101	 */
102	public function fetch(int $documentId, string $sessionId, string $sessionToken): Response {
103		return $this->apiService->fetch($documentId, $sessionId, $sessionToken);
104	}
105
106	/**
107	 * @NoAdminRequired
108	 * @PublicPage
109	 */
110	public function close(int $documentId, int $sessionId, string $sessionToken): DataResponse {
111		return $this->apiService->close($documentId, $sessionId, $sessionToken);
112	}
113
114	/**
115	 * @NoAdminRequired
116	 * @PublicPage
117	 */
118	public function push(int $documentId, int $sessionId, string $sessionToken, int $version, array $steps, string $token): DataResponse {
119		return $this->apiService->push($documentId, $sessionId, $sessionToken, $version, $steps, $token);
120	}
121
122	/**
123	 * @NoAdminRequired
124	 * @PublicPage
125	 */
126	public function sync(string $token, int $documentId, int $sessionId, string $sessionToken, int $version = 0, string $autosaveContent = null, bool $force = false, bool $manualSave = false): DataResponse {
127		return $this->apiService->sync($documentId, $sessionId, $sessionToken, $version, $autosaveContent, $force, $manualSave, $token);
128	}
129
130	/**
131	 * @NoAdminRequired
132	 * @PublicPage
133	 */
134	public function updateSession(int $documentId, int $sessionId, string $sessionToken, string $guestName) {
135		return $this->apiService->updateSession($documentId, $sessionId, $sessionToken, $guestName);
136	}
137}
138