diff --git a/src/assets/views/url-shrinker.njk b/src/assets/views/url-shrinker.njk deleted file mode 100644 index 83e520b..0000000 --- a/src/assets/views/url-shrinker.njk +++ /dev/null @@ -1,75 +0,0 @@ -{% extends 'layouts' %} - -{% set title = app.name + ' - Shrink URL' %} - -{% block scripts %} - -{% endblock %} - -{% block body %} -

Shrink URLs

-

For security reasons, shrinked URLs cannot be deleted.

- -
-
-

Shrink a URL

- -
- {{ macros.field(_locals, 'text', 'target_url', '', 'Target URL', 'Only valid URLs starting with http:// or https://', validation_attributes='required') }} - - {{ macros.field(_locals, 'checkbox', 'autogen_url', '', 'Generate url automatically', null, validation_attributes='checked') }} - {{ macros.field(_locals, 'text', 'slug', '', 'Custom url slug', 'Example: bear sets url to https://'+default_domain+'/bear', validation_attributes='disabled') }} - - {{ macros.csrf(getCsrfToken) }} - - -
- - {% set url = flash('url') %} - {% if url | length %} -
-
URL
-
{{ url }}
- -
- {% endif %} -
- - -
-

URL list

- - {{ macros.paginate(urls.pagination, 'url-shrinker', 3) }} - - - - - - - - - - - - {% for url in urls %} - - - - - - {% else %} - - - - {% endfor %} - -
#TargetActions
{{ url.id }}
{{ url.target_url }}
-
- -
-
You haven't shrunk any url yet.
- - {{ macros.paginate(urls.pagination, 'url-shrinker', 3) }} -
-
-{% endblock %} diff --git a/src/assets/views/url-shrinker.svelte b/src/assets/views/url-shrinker.svelte new file mode 100644 index 0000000..908f005 --- /dev/null +++ b/src/assets/views/url-shrinker.svelte @@ -0,0 +1,83 @@ + + + +

Shrink URLs

+ +
+

+ + Shrink a URL +

+

For security reasons, shrinked URLs cannot be deleted.

+ +
+ + + + {#if !autogenUrl} + + {/if} + + + {#if $locals.flash.url} + + {/if} +
+ + +
+

+ + URL list +

+ + + + + + + + + + + + + + {#each $locals.urls as url} + + + + + + {:else} + + + + {/each} + +
#TargetActions
{url.id} + +
{url.target_url}
+
+
+ +
You haven't shrunk any url yet.
+ + +
+
diff --git a/src/controllers/URLRedirectController.ts b/src/controllers/URLRedirectController.ts index 5552adb..766360d 100644 --- a/src/controllers/URLRedirectController.ts +++ b/src/controllers/URLRedirectController.ts @@ -18,10 +18,15 @@ export default class URLRedirectController extends Controller { protected async getUrlShrinker(req: Request, res: Response): Promise { const user = req.as(RequireAuthMiddleware).getUser(); const allowedDomains = config.get('allowed_url_domains'); + const urls = await URLRedirect.paginateForUser(req, 25, user.getOrFail('id')); res.render('url-shrinker', { allowed_domains: allowedDomains, default_domain: allowedDomains[config.get('default_url_domain_for_urls')], - urls: await URLRedirect.paginateForUser(req, 25, user.getOrFail('id')), + urls: urls.map(url => ({ + ...url, + url: url.getURL(), + })), + pagination: urls.pagination?.serialize(), }); } diff --git a/src/models/URLRedirect.ts b/src/models/URLRedirect.ts index acf06a8..e6e343e 100644 --- a/src/models/URLRedirect.ts +++ b/src/models/URLRedirect.ts @@ -3,6 +3,7 @@ import {Request} from "express"; import User from "swaf/auth/models/User"; import {route} from "swaf/common/Routing"; import Model from "swaf/db/Model"; +import {ModelQueryResult} from "swaf/db/ModelQuery"; import FileModel from "./FileModel.js"; @@ -15,7 +16,11 @@ export default class URLRedirect extends Model { return await this.select().where('slug', slug).first(); } - public static async paginateForUser(req: Request, perPage: number, user_id: number): Promise { + public static async paginateForUser( + req: Request, + perPage: number, + user_id: number, + ): Promise> { req.params.sortBy = 'created_at'; req.params.sortDirection = 'DESC'; return await this.paginate(req, perPage, this.select().where('user_id', user_id));