aboutsummaryrefslogtreecommitdiffstats
path: root/apps/settings/src/utils
diff options
context:
space:
mode:
Diffstat (limited to 'apps/settings/src/utils')
-rw-r--r--apps/settings/src/utils/appDiscoverParser.spec.ts79
-rw-r--r--apps/settings/src/utils/appDiscoverParser.ts48
-rw-r--r--apps/settings/src/utils/handlers.ts33
-rw-r--r--apps/settings/src/utils/sorting.ts14
-rw-r--r--apps/settings/src/utils/userUtils.ts27
-rw-r--r--apps/settings/src/utils/validate.js80
6 files changed, 281 insertions, 0 deletions
diff --git a/apps/settings/src/utils/appDiscoverParser.spec.ts b/apps/settings/src/utils/appDiscoverParser.spec.ts
new file mode 100644
index 00000000000..2a631014679
--- /dev/null
+++ b/apps/settings/src/utils/appDiscoverParser.spec.ts
@@ -0,0 +1,79 @@
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+import type { IAppDiscoverElement } from '../constants/AppDiscoverTypes'
+
+import { describe, expect, it } from 'vitest'
+import { filterElements, parseApiResponse } from './appDiscoverParser'
+
+describe('App Discover API parser', () => {
+ describe('filterElements', () => {
+ it('can filter expired elements', () => {
+ const result = filterElements({ id: 'test', type: 'post', expiryDate: 100 })
+ expect(result).toBe(false)
+ })
+
+ it('can filter upcoming elements', () => {
+ const result = filterElements({ id: 'test', type: 'post', date: Date.now() + 10000 })
+ expect(result).toBe(false)
+ })
+
+ it('ignores element without dates', () => {
+ const result = filterElements({ id: 'test', type: 'post' })
+ expect(result).toBe(true)
+ })
+
+ it('allows not yet expired elements', () => {
+ const result = filterElements({ id: 'test', type: 'post', expiryDate: Date.now() + 10000 })
+ expect(result).toBe(true)
+ })
+
+ it('allows yet included elements', () => {
+ const result = filterElements({ id: 'test', type: 'post', date: 100 })
+ expect(result).toBe(true)
+ })
+
+ it('allows elements included and not expired', () => {
+ const result = filterElements({ id: 'test', type: 'post', date: 100, expiryDate: Date.now() + 10000 })
+ expect(result).toBe(true)
+ })
+
+ it('can handle null values', () => {
+ const result = filterElements({ id: 'test', type: 'post', date: null, expiryDate: null } as unknown as IAppDiscoverElement)
+ expect(result).toBe(true)
+ })
+ })
+
+ describe('parseApiResponse', () => {
+ it('can handle basic post', () => {
+ const result = parseApiResponse({ id: 'test', type: 'post' })
+ expect(result).toEqual({ id: 'test', type: 'post' })
+ })
+
+ it('can handle carousel', () => {
+ const result = parseApiResponse({ id: 'test', type: 'carousel' })
+ expect(result).toEqual({ id: 'test', type: 'carousel' })
+ })
+
+ it('can handle showcase', () => {
+ const result = parseApiResponse({ id: 'test', type: 'showcase' })
+ expect(result).toEqual({ id: 'test', type: 'showcase' })
+ })
+
+ it('throws on unknown type', () => {
+ expect(() => parseApiResponse({ id: 'test', type: 'foo-bar' })).toThrow()
+ })
+
+ it('parses the date', () => {
+ const result = parseApiResponse({ id: 'test', type: 'showcase', date: '2024-03-19T17:28:19+0000' })
+ expect(result).toEqual({ id: 'test', type: 'showcase', date: 1710869299000 })
+ })
+
+ it('parses the expiryDate', () => {
+ const result = parseApiResponse({ id: 'test', type: 'showcase', expiryDate: '2024-03-19T17:28:19Z' })
+ expect(result).toEqual({ id: 'test', type: 'showcase', expiryDate: 1710869299000 })
+ })
+ })
+})
diff --git a/apps/settings/src/utils/appDiscoverParser.ts b/apps/settings/src/utils/appDiscoverParser.ts
new file mode 100644
index 00000000000..1be44f01068
--- /dev/null
+++ b/apps/settings/src/utils/appDiscoverParser.ts
@@ -0,0 +1,48 @@
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+import type { IAppDiscoverCarousel, IAppDiscoverElement, IAppDiscoverElements, IAppDiscoverPost, IAppDiscoverShowcase } from '../constants/AppDiscoverTypes.ts'
+
+/**
+ * Helper to transform the JSON API results to proper frontend objects (app discover section elements)
+ *
+ * @param element The JSON API element to transform
+ */
+export const parseApiResponse = (element: Record<string, unknown>): IAppDiscoverElements => {
+ const appElement = { ...element }
+ if (appElement.date) {
+ appElement.date = Date.parse(appElement.date as string)
+ }
+ if (appElement.expiryDate) {
+ appElement.expiryDate = Date.parse(appElement.expiryDate as string)
+ }
+
+ if (appElement.type === 'post') {
+ return appElement as unknown as IAppDiscoverPost
+ } else if (appElement.type === 'showcase') {
+ return appElement as unknown as IAppDiscoverShowcase
+ } else if (appElement.type === 'carousel') {
+ return appElement as unknown as IAppDiscoverCarousel
+ }
+ throw new Error(`Invalid argument, app discover element with type ${element.type ?? 'unknown'} is unknown`)
+}
+
+/**
+ * Filter outdated or upcoming elements
+ * @param element Element to check
+ */
+export const filterElements = (element: IAppDiscoverElement) => {
+ const now = Date.now()
+ // Element not yet published
+ if (element.date && element.date > now) {
+ return false
+ }
+
+ // Element expired
+ if (element.expiryDate && element.expiryDate < now) {
+ return false
+ }
+ return true
+}
diff --git a/apps/settings/src/utils/handlers.ts b/apps/settings/src/utils/handlers.ts
new file mode 100644
index 00000000000..edd9a6c0cff
--- /dev/null
+++ b/apps/settings/src/utils/handlers.ts
@@ -0,0 +1,33 @@
+/**
+ * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+import type { AxiosError } from '@nextcloud/axios'
+import { showError } from '@nextcloud/dialogs'
+import { translate as t } from '@nextcloud/l10n'
+
+import logger from '../logger.ts'
+
+/**
+ * @param error the error
+ * @param message the message to display
+ */
+export function handleError(error: AxiosError, message: string) {
+ let fullMessage = ''
+
+ if (message) {
+ fullMessage += message
+ }
+
+ if (error.response?.status === 429) {
+ if (fullMessage) {
+ fullMessage += '\n'
+ }
+ fullMessage += t('settings', 'There were too many requests from your network. Retry later or contact your administrator if this is an error.')
+ }
+
+ fullMessage = fullMessage || t('settings', 'Error')
+ showError(fullMessage)
+ logger.error(fullMessage, { error })
+}
diff --git a/apps/settings/src/utils/sorting.ts b/apps/settings/src/utils/sorting.ts
new file mode 100644
index 00000000000..88f877733cc
--- /dev/null
+++ b/apps/settings/src/utils/sorting.ts
@@ -0,0 +1,14 @@
+/**
+ * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+import { getCanonicalLocale, getLanguage } from '@nextcloud/l10n'
+
+export const naturalCollator = Intl.Collator(
+ [getLanguage(), getCanonicalLocale()],
+ {
+ numeric: true,
+ usage: 'sort',
+ },
+)
diff --git a/apps/settings/src/utils/userUtils.ts b/apps/settings/src/utils/userUtils.ts
new file mode 100644
index 00000000000..7d9a516a542
--- /dev/null
+++ b/apps/settings/src/utils/userUtils.ts
@@ -0,0 +1,27 @@
+/**
+ * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+import { translate as t } from '@nextcloud/l10n'
+
+export const unlimitedQuota = {
+ id: 'none',
+ label: t('settings', 'Unlimited'),
+}
+
+export const defaultQuota = {
+ id: 'default',
+ label: t('settings', 'Default quota'),
+}
+
+/**
+ * Return `true` if the logged in user does not have permissions to view the
+ * data of `user`
+ * @param user The user to check
+ * @param user.id Id of the user
+ */
+export const isObfuscated = (user: { id: string, [key: string]: unknown }) => {
+ const keys = Object.keys(user)
+ return keys.length === 1 && keys.at(0) === 'id'
+}
diff --git a/apps/settings/src/utils/validate.js b/apps/settings/src/utils/validate.js
new file mode 100644
index 00000000000..0f76f4e6dc5
--- /dev/null
+++ b/apps/settings/src/utils/validate.js
@@ -0,0 +1,80 @@
+/**
+ * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+/*
+ * Frontend validators, less strict than backend validators
+ *
+ * TODO add nice validation errors for Profile page settings modal
+ */
+
+import { VALIDATE_EMAIL_REGEX } from '../constants/AccountPropertyConstants.ts'
+
+/**
+ * Validate the email input
+ *
+ * Compliant with PHP core FILTER_VALIDATE_EMAIL validator*
+ *
+ * Reference implementation https://github.com/mpyw/FILTER_VALIDATE_EMAIL.js/blob/71e62ca48841d2246a1b531e7e84f5a01f15e615/src/index.ts*
+ *
+ * @param {string} input the input
+ * @return {boolean}
+ */
+export function validateEmail(input) {
+ return typeof input === 'string'
+ && VALIDATE_EMAIL_REGEX.test(input)
+ && input.slice(-1) !== '\n'
+ && input.length <= 320
+ && encodeURIComponent(input).replace(/%../g, 'x').length <= 320
+}
+
+/**
+ * Validate the URL input
+ *
+ * @param {string} input the input
+ * @return {boolean}
+ */
+export function validateUrl(input) {
+ try {
+ // eslint-disable-next-line no-new
+ new URL(input)
+ return true
+ } catch (e) {
+ return false
+ }
+}
+
+/**
+ * Validate the language input
+ *
+ * @param {object} input the input
+ * @return {boolean}
+ */
+export function validateLanguage(input) {
+ return input.code !== ''
+ && input.name !== ''
+ && input.name !== undefined
+}
+
+/**
+ * Validate the locale input
+ *
+ * @param {object} input the input
+ * @return {boolean}
+ */
+export function validateLocale(input) {
+ return input.code !== ''
+ && input.name !== ''
+ && input.name !== undefined
+}
+
+/**
+ * Validate boolean input
+ *
+ * @param {boolean} input the input
+ * @return {boolean}
+ */
+export function validateBoolean(input) {
+ return typeof input === 'boolean'
+}