public-album: add og metadata

Signed-off-by: Varun Patil <varunpatil@ucla.edu>
pull/488/head
Varun Patil 2023-03-14 13:42:55 -07:00
parent 2a9cfaf81b
commit 7a35229b47
1 changed files with 31 additions and 3 deletions

View File

@ -10,6 +10,7 @@ use OCP\AppFramework\Http\Template\PublicTemplateResponse;
use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState; use OCP\AppFramework\Services\IInitialState;
use OCP\EventDispatcher\IEventDispatcher; use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\IRootFolder;
use OCP\IConfig; use OCP\IConfig;
use OCP\IDBConnection; use OCP\IDBConnection;
use OCP\IURLGenerator; use OCP\IURLGenerator;
@ -25,7 +26,9 @@ class PublicAlbumController extends Controller
protected IConfig $config; protected IConfig $config;
protected IDBConnection $connection; protected IDBConnection $connection;
protected IUserSession $userSession; protected IUserSession $userSession;
protected IRootFolder $rootFolder;
protected IURLGenerator $urlGenerator; protected IURLGenerator $urlGenerator;
protected TimelineQuery $timelineQuery;
public function __construct( public function __construct(
string $appName, string $appName,
@ -35,6 +38,7 @@ class PublicAlbumController extends Controller
IConfig $config, IConfig $config,
IDBConnection $connection, IDBConnection $connection,
IUserSession $userSession, IUserSession $userSession,
IRootFolder $rootFolder,
IURLGenerator $urlGenerator IURLGenerator $urlGenerator
) { ) {
$this->appName = $appName; $this->appName = $appName;
@ -44,7 +48,9 @@ class PublicAlbumController extends Controller
$this->config = $config; $this->config = $config;
$this->connection = $connection; $this->connection = $connection;
$this->userSession = $userSession; $this->userSession = $userSession;
$this->rootFolder = $rootFolder;
$this->urlGenerator = $urlGenerator; $this->urlGenerator = $urlGenerator;
$this->timelineQuery = new TimelineQuery($this->connection);
} }
/** /**
@ -55,8 +61,7 @@ class PublicAlbumController extends Controller
public function showShare(string $token) public function showShare(string $token)
{ {
// Validate token exists // Validate token exists
$timelineQuery = new TimelineQuery($this->connection); $album = $this->timelineQuery->getAlbumByLink($token);
$album = $timelineQuery->getAlbumByLink($token);
if (!$album) { if (!$album) {
return new TemplateResponse('core', '404', [], 'guest'); return new TemplateResponse('core', '404', [], 'guest');
} }
@ -67,7 +72,7 @@ class PublicAlbumController extends Controller
$uid = $user->getUID(); $uid = $user->getUID();
$albumId = (int) $album['album_id']; $albumId = (int) $album['album_id'];
if ($uid === $album['user'] || $timelineQuery->userIsAlbumCollaborator($uid, $albumId)) { if ($uid === $album['user'] || $this->timelineQuery->userIsAlbumCollaborator($uid, $albumId)) {
$idStr = $album['user'].'/'.$album['name']; $idStr = $album['user'].'/'.$album['name'];
$url = $this->urlGenerator->linkToRoute('memories.Page.albums', ['id' => $idStr]); $url = $this->urlGenerator->linkToRoute('memories.Page.albums', ['id' => $idStr]);
@ -78,6 +83,9 @@ class PublicAlbumController extends Controller
// Browse anonymously if the album is accessed as a link // Browse anonymously if the album is accessed as a link
\OC_User::setIncognitoMode(true); \OC_User::setIncognitoMode(true);
// Add OG metadata
$this->addOgMetadata($album, $token);
// Scripts // Scripts
Util::addScript($this->appName, 'memories-main'); Util::addScript($this->appName, 'memories-main');
PageController::provideCommonInitialState($this->initialState); PageController::provideCommonInitialState($this->initialState);
@ -89,4 +97,24 @@ class PublicAlbumController extends Controller
return $response; return $response;
} }
private function addOgMetadata(array $album, string $token)
{
$fileId = (int) $album['last_added_photo'];
$albumId = (int) $album['album_id'];
$owner = $this->timelineQuery->albumHasFile($albumId, $fileId);
if (!$owner) {
return;
}
$nodes = $this->rootFolder->getUserFolder($owner)->getById($fileId);
if (0 === \count($nodes)) {
return;
}
$node = $nodes[0];
$params = ['token' => $token];
$url = $this->urlGenerator->linkToRouteAbsolute('memories.PublicAlbum.showShare', $params);
\OCA\Memories\Util::addOGMetadata($node, $album['name'], $url, array_merge($params, ['album' => true]));
}
} }