Signed-off-by: Joas Schilling <coding@schilljs.com>tags/v22.0.0beta5
@@ -25,7 +25,10 @@ declare(strict_types=1); | |||
*/ | |||
namespace OCA\UserStatus\Controller; | |||
use OCA\UserStatus\Db\UserStatus; | |||
use OCA\UserStatus\Service\StatusService; | |||
use OCP\AppFramework\Controller; | |||
use OCP\AppFramework\Db\DoesNotExistException; | |||
use OCP\AppFramework\Http; | |||
use OCP\AppFramework\Http\JSONResponse; | |||
use OCP\AppFramework\Utility\ITimeFactory; | |||
@@ -46,24 +49,20 @@ class HeartbeatController extends Controller { | |||
/** @var ITimeFactory */ | |||
private $timeFactory; | |||
/** | |||
* HeartbeatController constructor. | |||
* | |||
* @param string $appName | |||
* @param IRequest $request | |||
* @param IEventDispatcher $eventDispatcher | |||
* @param IUserSession $userSession | |||
* @param ITimeFactory $timeFactory | |||
*/ | |||
/** @var StatusService */ | |||
private $service; | |||
public function __construct(string $appName, | |||
IRequest $request, | |||
IEventDispatcher $eventDispatcher, | |||
IUserSession $userSession, | |||
ITimeFactory $timeFactory) { | |||
ITimeFactory $timeFactory, | |||
StatusService $service) { | |||
parent::__construct($appName, $request); | |||
$this->eventDispatcher = $eventDispatcher; | |||
$this->userSession = $userSession; | |||
$this->timeFactory = $timeFactory; | |||
$this->service = $service; | |||
} | |||
/** | |||
@@ -90,6 +89,25 @@ class HeartbeatController extends Controller { | |||
) | |||
); | |||
return new JSONResponse([], Http::STATUS_NO_CONTENT); | |||
try { | |||
$userStatus = $this->service->findByUserId($user->getUID()); | |||
} catch (DoesNotExistException $ex) { | |||
return new JSONResponse([], Http::STATUS_NO_CONTENT); | |||
} | |||
return new JSONResponse($this->formatStatus($userStatus)); | |||
} | |||
private function formatStatus(UserStatus $status): array { | |||
return [ | |||
'userId' => $status->getUserId(), | |||
'message' => $status->getCustomMessage(), | |||
'messageId' => $status->getMessageId(), | |||
'messageIsPredefined' => $status->getMessageId() !== null, | |||
'icon' => $status->getCustomIcon(), | |||
'clearAt' => $status->getClearAt(), | |||
'status' => $status->getStatus(), | |||
'statusIsUserDefined' => $status->getIsUserDefined(), | |||
]; | |||
} | |||
} |
@@ -156,12 +156,15 @@ export default { | |||
*/ | |||
async _backgroundHeartbeat() { | |||
try { | |||
await sendHeartbeat(this.isAway) | |||
const status = await sendHeartbeat(this.isAway) | |||
if (status?.userId) { | |||
this.$store.dispatch('setStatusFromHeartbeat', status) | |||
} else { | |||
await this.$store.dispatch('reFetchStatusFromServer') | |||
} | |||
} catch (error) { | |||
console.debug('Failed sending heartbeat, got: ' + error.response.status) | |||
return | |||
} | |||
await this.$store.dispatch('reFetchStatusFromServer') | |||
}, | |||
}, | |||
} |
@@ -31,9 +31,10 @@ import { generateUrl } from '@nextcloud/router' | |||
*/ | |||
const sendHeartbeat = async(isAway) => { | |||
const url = generateUrl('/apps/user_status/heartbeat') | |||
await HttpClient.put(url, { | |||
const response = await HttpClient.put(url, { | |||
status: isAway ? 'away' : 'online', | |||
}) | |||
return response.data | |||
} | |||
export { |
@@ -252,6 +252,25 @@ const actions = { | |||
commit('loadStatusFromServer', status) | |||
}, | |||
/** | |||
* Stores the status we got in the reply of the heartbeat | |||
* | |||
* @param {Object} vuex The Vuex destructuring object | |||
* @param {Function} vuex.commit The Vuex commit function | |||
* @param {Object} status The data destructuring object | |||
* @param {String} status.status The status type | |||
* @param {Boolean} status.statusIsUserDefined Whether or not this status is user-defined | |||
* @param {String} status.message The message | |||
* @param {String} status.icon The icon | |||
* @param {Number} status.clearAt When to automatically clear the status | |||
* @param {Boolean} status.messageIsPredefined Whether or not the message is predefined | |||
* @param {string} status.messageId The id of the predefined message | |||
* @returns {Promise<void>} | |||
*/ | |||
async setStatusFromHeartbeat({ commit }, status) { | |||
commit('loadStatusFromServer', status) | |||
}, | |||
/** | |||
* Loads the server from the initial state | |||
* |