refactor: use cached uid and isAdmin

Signed-off-by: Varun Patil <radialapps@gmail.com>
pull/803/head
Varun Patil 2023-08-25 21:41:42 -07:00
parent 78de9601ef
commit bca08f7eb2
19 changed files with 56 additions and 63 deletions

View File

@ -51,7 +51,6 @@ import NcContent from '@nextcloud/vue/dist/Components/NcContent';
import NcAppContent from '@nextcloud/vue/dist/Components/NcAppContent';
import NcButton from '@nextcloud/vue/dist/Components/NcButton';
import { getCurrentUser } from '@nextcloud/auth';
import axios from '@nextcloud/axios';
import banner from '../assets/banner.svg';
@ -85,7 +84,7 @@ export default defineComponent({
computed: {
isAdmin(): boolean {
return Boolean(getCurrentUser()?.isAdmin);
return utils.isAdmin;
},
},

View File

@ -50,7 +50,6 @@ import NcActionButton from '@nextcloud/vue/dist/Components/NcActionButton';
import { translate as t, translatePlural as n } from '@nextcloud/l10n';
import { subscribe, unsubscribe } from '@nextcloud/event-bus';
import { getCurrentUser } from '@nextcloud/auth';
import * as dav from '../services/dav';
import * as utils from '../services/utils';
@ -899,7 +898,7 @@ export default defineComponent({
}
// Check photo ownership
if (this.$route.params.user !== getCurrentUser()?.uid) {
if (this.$route.params.user !== utils.uid) {
showError(this.t('memories', 'Only user "{user}" can update this person', { user }));
return;
}

View File

@ -145,8 +145,6 @@ input[type='text'] {
<script lang="ts">
import { defineComponent } from 'vue';
import { getCurrentUser } from '@nextcloud/auth';
import UserConfig from '../mixins/UserConfig';
import * as utils from '../services/utils';
import * as nativex from '../native';
@ -192,7 +190,7 @@ export default defineComponent({
},
user(): string {
return getCurrentUser()?.uid.toString() ?? '';
return utils.uid ?? String();
},
},

View File

@ -28,7 +28,6 @@
<script lang="ts">
import { defineComponent, PropType } from 'vue';
import { getCurrentUser } from '@nextcloud/auth';
import NcCounterBubble from '@nextcloud/vue/dist/Components/NcCounterBubble';
import type { IAlbum, ICluster, IFace, IPhoto } from '../../types';
@ -36,6 +35,7 @@ import { getPreviewUrl } from '../../services/utils/helpers';
import errorsvg from '../../assets/error.svg';
import plussvg from '../../assets/plus.svg';
import * as utils from '../../services/utils';
import { API } from '../../services/API';
import Vue from 'vue';
@ -95,7 +95,7 @@ export default defineComponent({
text = this.n('memories', '{n} item', '{n} items', this.album.count, { n: this.album.count });
}
if (this.album.user !== getCurrentUser()?.uid) {
if (this.album.user !== utils.uid) {
text +=
' / ' +
this.t('memories', 'Shared by {user}', {

View File

@ -138,7 +138,6 @@ import Earth from 'vue-material-design-icons/Earth.vue';
import axios from '@nextcloud/axios';
import { showError } from '@nextcloud/dialogs';
import { getCurrentUser } from '@nextcloud/auth';
import { generateOcsUrl, generateUrl } from '@nextcloud/router';
import NcButton from '@nextcloud/vue/dist/Components/NcButton';
@ -148,6 +147,7 @@ const NcTextField = () => import('@nextcloud/vue/dist/Components/NcTextField');
const NcListItemIcon = () => import('@nextcloud/vue/dist/Components/NcListItemIcon');
import * as dav from '../../services/dav';
import * as utils from '../../services/utils';
import * as nativex from '../../native';
import { Type } from '@nextcloud/sharing';
@ -208,7 +208,7 @@ export default defineComponent({
computed: {
searchResults(): string[] {
return this.currentSearchResults
.filter(({ id }) => id !== getCurrentUser()?.uid)
.filter(({ id }) => id !== utils.uid)
.map(({ type, id }) => `${type}:${id}`)
.filter((collaboratorKey) => !this.selectedCollaboratorsKeys.includes(collaboratorKey));
},
@ -333,9 +333,8 @@ export default defineComponent({
this.loadingAlbum = true;
this.errorFetchingAlbum = null;
const uid = getCurrentUser()?.uid.toString();
if (!uid) return;
const album = await dav.getAlbum(uid, this.albumName);
if (!utils.uid) return;
const album = await dav.getAlbum(utils.uid, this.albumName);
this.populateCollaborators(album.collaborators);
// Direct share if native share is available
@ -366,9 +365,8 @@ export default defineComponent({
async updateAlbumCollaborators() {
try {
const uid = getCurrentUser()?.uid?.toString();
if (!uid) return;
const album = await dav.getAlbum(uid, this.albumName);
if (!utils.uid) return;
const album = await dav.getAlbum(utils.uid, this.albumName);
await dav.updateAlbum(album, {
albumName: this.albumName,
properties: {

View File

@ -58,7 +58,7 @@ export default defineComponent({
computed: {
owned() {
return this.user === utils.uid();
return this.user === utils.uid;
},
},

View File

@ -89,12 +89,12 @@
<script lang="ts">
import { defineComponent, PropType } from 'vue';
import { getCurrentUser } from '@nextcloud/auth';
import { showError } from '@nextcloud/dialogs';
import NcButton from '@nextcloud/vue/dist/Components/NcButton';
const NcTextField = () => import('@nextcloud/vue/dist/Components/NcTextField');
import { DateTime } from 'luxon';
import * as utils from '../../services/utils';
import * as dav from '../../services/dav';
import AlbumCollaborators from './AlbumCollaborators.vue';
@ -186,7 +186,7 @@ export default defineComponent({
this.loading = true;
let album = {
basename: this.albumName,
filename: `/photos/${getCurrentUser()?.uid}/albums/${this.albumName}`,
filename: `/photos/${utils.uid}/albums/${this.albumName}`,
nbItems: 0,
location: this.albumLocation,
lastPhoto: -1,

View File

@ -34,11 +34,11 @@
import { defineComponent, PropType } from 'vue';
import { getPreviewUrl } from '../../services/utils/helpers';
import { getCurrentUser } from '@nextcloud/auth';
import NcButton from '@nextcloud/vue/dist/Components/NcButton';
const NcListItem = () => import('@nextcloud/vue/dist/Components/NcListItem');
import * as utils from '../../services/utils';
import type { IAlbum, IPhoto } from '../../types';
import ImageMultipleIcon from 'vue-material-design-icons/ImageMultiple.vue';
@ -93,7 +93,7 @@ export default defineComponent({
getSubtitle(album: IAlbum) {
let text = this.n('memories', '%n item', '%n items', album.count);
if (album.user !== getCurrentUser()?.uid) {
if (album.user !== utils.uid) {
text +=
' / ' +
this.t('memories', 'shared by {owner}', {

View File

@ -21,8 +21,10 @@ import NcButton from '@nextcloud/vue/dist/Components/NcButton';
const NcTextField = () => import('@nextcloud/vue/dist/Components/NcTextField');
import { showError } from '@nextcloud/dialogs';
import { getCurrentUser } from '@nextcloud/auth';
import Modal from './Modal.vue';
import * as utils from '../../services/utils';
import * as dav from '../../services/dav';
export default defineComponent({
@ -57,7 +59,7 @@ export default defineComponent({
open() {
const user = this.$route.params.user || '';
if (this.$route.params.user !== getCurrentUser()?.uid) {
if (this.$route.params.user !== utils.uid) {
showError(
this.t('memories', 'Only user "{user}" can delete this person', {
user,

View File

@ -31,8 +31,10 @@ import NcButton from '@nextcloud/vue/dist/Components/NcButton';
const NcTextField = () => import('@nextcloud/vue/dist/Components/NcTextField');
import { showError } from '@nextcloud/dialogs';
import { getCurrentUser } from '@nextcloud/auth';
import Modal from './Modal.vue';
import * as utils from '../../services/utils';
import * as dav from '../../services/dav';
export default defineComponent({
@ -74,7 +76,7 @@ export default defineComponent({
open() {
const user = this.$route.params.user || '';
if (this.$route.params.user !== getCurrentUser()?.uid) {
if (this.$route.params.user !== utils.uid) {
showError(
this.t('memories', 'Only user "{user}" can update this person', {
user,

View File

@ -28,7 +28,6 @@ const NcTextField = () => import('@nextcloud/vue/dist/Components/NcTextField');
const NcProgressBar = () => import('@nextcloud/vue/dist/Components/NcProgressBar');
import { showError } from '@nextcloud/dialogs';
import { getCurrentUser } from '@nextcloud/auth';
import { IFileInfo, IFace } from '../../types';
import Cluster from '../frame/Cluster.vue';
import FaceList from './FaceList.vue';
@ -63,7 +62,7 @@ export default defineComponent({
open() {
const user = this.$route.params.user || '';
if (this.$route.params.user !== getCurrentUser()?.uid) {
if (this.$route.params.user !== utils.uid) {
showError(
this.t('memories', 'Only user "{user}" can update this person', {
user,

View File

@ -23,7 +23,6 @@ import NcButton from '@nextcloud/vue/dist/Components/NcButton';
const NcTextField = () => import('@nextcloud/vue/dist/Components/NcTextField');
import { showError } from '@nextcloud/dialogs';
import { getCurrentUser } from '@nextcloud/auth';
import { IPhoto, IFace } from '../../types';
import Cluster from '../frame/Cluster.vue';
import FaceList from './FaceList.vue';
@ -63,7 +62,7 @@ export default defineComponent({
// check ownership
const user = this.$route.params.user || '';
if (this.$route.params.user !== getCurrentUser()?.uid) {
if (this.$route.params.user !== utils.uid) {
showError(
this.t('memories', 'Only user "{user}" can update this person', {
user,

View File

@ -102,7 +102,6 @@ import NcActionButton from '@nextcloud/vue/dist/Components/NcActionButton';
import NcActionCheckbox from '@nextcloud/vue/dist/Components/NcActionCheckbox';
import NcActionRadio from '@nextcloud/vue/dist/Components/NcActionRadio';
import { getCurrentUser } from '@nextcloud/auth';
import axios from '@nextcloud/axios';
import AlbumCreateModal from '../modal/AlbumCreateModal.vue';
@ -154,7 +153,7 @@ export default defineComponent({
},
canEditAlbum(): boolean {
return !this.isAlbumList && this.$route.params.user === getCurrentUser()?.uid;
return !this.isAlbumList && this.$route.params.user === utils.uid;
},
name(): string {

View File

@ -67,7 +67,6 @@ import NcActionButton from '@nextcloud/vue/dist/Components/NcActionButton';
import NcActionCheckbox from '@nextcloud/vue/dist/Components/NcActionCheckbox';
import { emit } from '@nextcloud/event-bus';
import { getCurrentUser } from '@nextcloud/auth';
import FaceEditModal from '../modal/FaceEditModal.vue';
import FaceDeleteModal from '../modal/FaceDeleteModal.vue';
@ -132,7 +131,7 @@ export default defineComponent({
this.$router.push({
name: this.$route.name as string,
params: {
user: String(getCurrentUser()?.uid),
user: utils.uid as string,
name: utils.constants.FACE_NULL,
},
});

View File

@ -5,7 +5,6 @@ import * as utils from '../../services/utils';
import { showError } from '@nextcloud/dialogs';
import { translate as t } from '@nextcloud/l10n';
import { getCurrentUser } from '@nextcloud/auth';
import { API } from '../../services/API';
import type { PsContent, PsEvent } from './types';
@ -203,7 +202,7 @@ class VideoContentSetup {
hlsFailed = true;
console.warn('PsVideo: HLS stream could not be opened.');
if (getCurrentUser()?.isAdmin) {
if (utils.isAdmin) {
showError(t('memories', 'Transcoding failed, check Nextcloud logs.'));
}

View File

@ -1,22 +1,24 @@
import * as base from './base';
import { getCurrentUser } from '@nextcloud/auth';
import axios from '@nextcloud/axios';
import { showError } from '@nextcloud/dialogs';
import { translate as t } from '@nextcloud/l10n';
import { IAlbum, IFileInfo, IPhoto } from '../../types';
import { API } from '../API';
import axios from '@nextcloud/axios';
import client from './client';
import * as utils from '../utils';
/**
* Get DAV path for album
*/
export function getAlbumPath(user: string, name: string) {
// Folder in the dav collection for user
const cuid = getCurrentUser()?.uid;
if (user === cuid) {
return `/photos/${cuid}/albums/${name}`;
if (user === utils.uid) {
return `/photos/${utils.uid}/albums/${name}`;
} else {
return `/photos/${cuid}/sharedalbums/${name} (${user})`;
return `/photos/${utils.uid}/sharedalbums/${name} (${user})`;
}
}
@ -116,7 +118,7 @@ export async function* removeFromAlbum(user: string, name: string, photos: IPhot
*/
export async function createAlbum(albumName: string) {
try {
await client.createDirectory(`/photos/${getCurrentUser()?.uid}/albums/${albumName}`);
await client.createDirectory(`/photos/${utils.uid}/albums/${albumName}`);
} catch (error) {
console.error(error);
showError(t('photos', 'Failed to create {albumName}.', { albumName }));
@ -214,8 +216,8 @@ export async function renameAlbum(album: any, { currentAlbumName, newAlbumName }
const newAlbum = { ...album, basename: newAlbumName };
try {
await client.moveFile(
`/photos/${getCurrentUser()?.uid}/albums/${currentAlbumName}`,
`/photos/${getCurrentUser()?.uid}/albums/${newAlbumName}`
`/photos/${utils.uid}/albums/${currentAlbumName}`,
`/photos/${utils.uid}/albums/${newAlbumName}`
);
return newAlbum;
} catch (error) {
@ -232,11 +234,10 @@ export async function renameAlbum(album: any, { currentAlbumName, newAlbumName }
/** Get fileinfo objects from album photos */
export function getAlbumFileInfos(photos: IPhoto[], albumUser: string, albumName: string): IFileInfo[] {
const uid = getCurrentUser()?.uid;
const collection =
albumUser === uid
? `/photos/${uid}/albums/${albumName}`
: `/photos/${uid}/sharedalbums/${albumName} (${albumUser})`;
albumUser === utils.uid
? `/photos/${utils.uid}/albums/${albumName}`
: `/photos/${utils.uid}/sharedalbums/${albumName} (${albumUser})`;
return photos.map((photo) => {
const basename = `${photo.fileid}-${photo.basename}`;

View File

@ -1,4 +1,3 @@
import { getCurrentUser } from '@nextcloud/auth';
import { showError } from '@nextcloud/dialogs';
import { translate as t } from '@nextcloud/l10n';
import axios from '@nextcloud/axios';
@ -60,8 +59,7 @@ export async function getFiles(photos: IPhoto[]): Promise<IFileInfo[]> {
const rest: IPhoto[] = [];
// Partition photos with and without cache
const uid = getCurrentUser()?.uid;
if (uid) {
if (utils.uid) {
for (const photo of photos) {
const filename = photo.imageInfo?.filename;
if (filename) {
@ -69,7 +67,7 @@ export async function getFiles(photos: IPhoto[]): Promise<IFileInfo[]> {
id: photo.fileid,
fileid: photo.fileid,
basename: photo.basename ?? filename.split('/').pop() ?? '',
originalFilename: `/files/${uid}${filename}`,
originalFilename: `/files/${utils.uid}${filename}`,
filename: filename,
});
} else {
@ -97,7 +95,7 @@ async function getFilesInternal1(photos: IPhoto[]): Promise<IFileInfo[]> {
}
async function getFilesInternal2(fileIds: number[]): Promise<IFileInfo[]> {
const prefixPath = `/files/${getCurrentUser()?.uid}`;
const prefixPath = `/files/${utils.uid}`;
// IMPORTANT: if this isn't there, then a blank
// returns EVERYTHING on the server!
@ -311,7 +309,7 @@ export async function* movePhotos(photos: IPhoto[], destination: string, overwri
}
// Set absolute target path
const prefixPath = `files/${getCurrentUser()?.uid}`;
const prefixPath = `files/${utils.uid}`;
let targetPath = prefixPath + destination;
if (!targetPath.endsWith('/')) {
targetPath += '/';

View File

@ -1,9 +1,7 @@
import { getCurrentUser } from '@nextcloud/auth';
import config from '../static-config';
import { uid } from './helpers';
/** Cache keys */
const uid = getCurrentUser()?.uid || 'guest';
async function getCacheName() {
const ver = await config.get('version');
return `memories-data-${ver}-${uid}`;
@ -11,7 +9,7 @@ async function getCacheName() {
// Clear all caches except the current one
(async function clearCaches() {
if (uid === 'guest') return;
if (!uid) return;
const keys = await window.caches?.keys();
if (!keys?.length) return;

View File

@ -8,9 +8,12 @@ import * as nativex from '../../native';
/**
* Get the current user UID
*/
export function uid() {
return String(getCurrentUser()?.uid || String()) || null;
}
export const uid = String(getCurrentUser()?.uid || String()) || null;
/**
* Check if the current user is an admin
*/
export const isAdmin = Boolean(getCurrentUser()?.isAdmin);
/**
* Check if width <= 768px