Skip to content

Commit

Permalink
feat: Store user agent hint and show it for token locks
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Knorr <jus@bitgrid.net>
  • Loading branch information
juliusknorr committed Nov 18, 2024
1 parent 410d717 commit 5fdb1af
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/Db/LocksRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public function save(FileLock $lock) {
->setValue('token', $qb->createNamedParameter($lock->getToken()))
->setValue('creation', $qb->createNamedParameter($lock->getCreatedAt()))
->setValue('type', $qb->createNamedParameter($lock->getType()))
->setValue('ttl', $qb->createNamedParameter($lock->getTimeout()));
->setValue('ttl', $qb->createNamedParameter($lock->getTimeout()))
->setValue('owner', $qb->createNamedParameter($lock->getDisplayName()));

try {
$qb->execute();
Expand Down
28 changes: 26 additions & 2 deletions lib/Service/LockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use OCP\Files\Lock\OwnerLockedException;
use OCP\Files\NotFoundException;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IUserManager;
use OCP\IUserSession;

Expand All @@ -43,6 +44,7 @@ class LockService {
private IAppManager $appManager;
private IEventDispatcher $eventDispatcher;
private IUserSession $userSession;
private IRequest $request;


private array $locks = [];
Expand All @@ -61,6 +63,7 @@ public function __construct(
IAppManager $appManager,
IEventDispatcher $eventDispatcher,
IUserSession $userSession,
IRequest $request,
) {
$this->l10n = $l10n;
$this->userManager = $userManager;
Expand All @@ -70,6 +73,7 @@ public function __construct(
$this->appManager = $appManager;
$this->eventDispatcher = $eventDispatcher;
$this->userSession = $userSession;
$this->request = $request;

$this->setup('app', 'files_lock');
}
Expand Down Expand Up @@ -162,9 +166,9 @@ public function lock(LockContext $lockScope): FileLock {
$this->generateToken($lock);
$lock->setCreation(time());
$this->notice('locking file', false, ['fileLock' => $lock]);
$this->injectMetadata($lock);
$this->locksRequest->save($lock);
$this->propagateEtag($lockScope);
$this->injectMetadata($lock);
return $lock;
}
}
Expand Down Expand Up @@ -317,7 +321,11 @@ public function injectMetadata(FileLock $lock): FileLock {
$displayName = $this->getAppName($lock->getOwner()) ?? null;
}
if ($lock->getType() === ILock::TYPE_TOKEN) {
$displayName = $this->userManager->getDisplayName($lock->getOwner()) ?? $lock->getDisplayName();
$clientHint = $this->getClientHint();
$displayName = $lock->getDisplayName() ?: (
$this->userManager->getDisplayName($lock->getOwner()) . ' ' .
($clientHint ? ('(' . $clientHint . ')') : '')
);
}

if ($displayName) {
Expand All @@ -326,6 +334,22 @@ public function injectMetadata(FileLock $lock): FileLock {
return $lock;
}

private function getClientHint(): ?string {
if ($this->request->isUserAgent([IRequest::USER_AGENT_CLIENT_DESKTOP])) {
return $this->l10n->t('Desktop client');
}

if ($this->request->isUserAgent([IRequest::USER_AGENT_CLIENT_IOS])) {
return $this->l10n->t('iOS client');
}

if ($this->request->isUserAgent([IRequest::USER_AGENT_CLIENT_ANDROID])) {
return $this->l10n->t('Android client');
}

return null;
}

/**
* @param FileLock $lock
*/
Expand Down

0 comments on commit 5fdb1af

Please sign in to comment.