aboutsummaryrefslogtreecommitdiff
path: root/back-end/src/Endpoints/WidgetEndpoint.cs
blob: 13694e3ce8ee14c6f0df896f687c8572cba2f4d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright (C) 2024 Vaughn Nugent
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

using System;
using System.Net;
using System.Linq;
using System.Threading.Tasks;
using System.Text;
using System.Collections.Generic;

using VNLib.Utils.IO;
using VNLib.Utils.Memory;
using VNLib.Utils.Logging;
using VNLib.Utils.Extensions;
using VNLib.Net.Http;
using VNLib.Plugins;
using VNLib.Plugins.Essentials;
using VNLib.Plugins.Essentials.Endpoints;
using VNLib.Plugins.Extensions.Loading;
using VNLib.Plugins.Essentials.Extensions;
using VNLib.Plugins.Extensions.Loading.Routing;

using SimpleBookmark.Model;
using SimpleBookmark.Model.Widget;

namespace SimpleBookmark.Endpoints
{
    [EndpointPath("{{path}}")]
    [EndpointLogName("widget-endpoint")]
    [ConfigurationName("widgets")]
    internal sealed class WidgetEndpoint(PluginBase plugin, IConfigScope config) : UnprotectedWebEndpoint
    {
        private readonly BookmarkStore bookmarks = plugin.GetOrCreateSingleton<BookmarkStore>();
        private readonly WidgetAuthManager authManager = plugin.GetOrCreateSingleton<WidgetAuthManager>();
        private readonly bool Enabled = config.GetValueOrDefault("enabled", false);
        private readonly string? CorsAclHeaderDomains = config.GetValueOrDefault<string?>("cors-urls", null);

        protected override ProtectionSettings EndpointProtectionSettings { get; } = new()
        {
            DisableSessionsRequired = true,
            DisabledTlsRequired = true,
            DisableRefererMatch = true,
        };

        protected override async ValueTask<VfReturnType> GetAsync(HttpEntity entity)
        {
            if (!Enabled)
            {
                return VfReturnType.NotFound;
            }

            string limitStr = entity.QueryArgs.GetValueOrDefault("l", "25");
            string pageStr = entity.QueryArgs.GetValueOrDefault("p", "1");

            //Get any query arguments
            if (entity.QueryArgs.TryGetNonEmptyValue("q", out string? query))
            {
                //Replace percent encoding with spaces
                query = query.Replace('+', ' ');
            }

            string[] tags = [];

            //Get tags
            if (entity.QueryArgs.TryGetNonEmptyValue("t", out string? tagsS))
            {
                //Split tags at spaces and remove empty entries
                tags = tagsS.Split('+')
                    .Where(static t => !string.IsNullOrWhiteSpace(t))
                    .ToArray();
            }

            if (!int.TryParse(limitStr, out int limit) || !int.TryParse(pageStr, out int page))
            {
                return VirtualClose(entity, HttpStatusCode.BadRequest);
            }

            limit = Math.Clamp(limit, 0, 100);
            page = Math.Clamp(page, 0, 100);

            /* if (!await authManager.IsTokenValidAsync(entity))
             {
                 return VirtualClose(entity, HttpStatusCode.Unauthorized);
             }*/

            //Widgets might be loaded in an iframe, so we need to allow cross-site requests
            if (CorsAclHeaderDomains is not null && entity.Server.IsCrossSite())
            {
                entity.Server.Headers.Append("Access-Control-Allow-Origin", CorsAclHeaderDomains);
            }

            /*
             * For now this just returns bookmarks with the "favorite" tag
             * an assume the auth manager has set the session's user-id field
             */

            BookmarkEntry[] boomarks = await bookmarks.SearchBookmarksAsync(
                userId: entity.Session.UserID,
                query: query,
                tags: tags,
                limit: limit,
                page: page,
                entity.EventCancellation
            );

            //Output memory buffer
            VnMemoryStream output = new(32 * 1024, false);

            try
            {
                string baseUrl = entity.Server.RequestUri.GetLeftPart(UriPartial.Authority);

                CompileGlanceTemplate(baseUrl, output, boomarks);

                //Assign glance template
                entity.Server.Headers["Widget-Title"] = "Simple-Bookmark";
                entity.Server.Headers["Widget-Content-Type"] = "html";

                return VirtualClose(entity, HttpStatusCode.OK, ContentType.Html, output);
            }
            catch(Exception ex)
            {
                output.Dispose();

                Log.Error(ex, "Failed to complie glance widget template");
                return VirtualClose(entity, HttpStatusCode.InternalServerError);
            }
        }

        private static void CompileGlanceTemplate(string hostUrl, VnMemoryStream vms, BookmarkEntry[] bookmarks)
        {
            using IMemoryHandle<char> buffer = MemoryUtil.SafeAlloc<char>(32 * 1024, false);

            ForwardOnlyWriter<char> writer = new(buffer.Span);

            writer.Append("<!DOCTYPE html>");
            writer.Append("<html>");
            writer.Append("<head>");
            writer.Append("<title>Simple-Bookmark Widget</title>");
            writer.Append("</head>");
            writer.Append("<body>");

            writer.Append("<p class='size-h3'>");
            writer.Append("<a class='bookmarks-link color-highlight size-h3' href='");
            writer.Append(hostUrl);
            writer.Append("'>Favorites</a>");
            writer.Append("</p>");
            writer.Append("<hr class='margin-block-15'/>");

            //Start bookmarks list
            writer.Append("<ul class='list list-gap-24 list-with-separator'>");
            writer.Append("<li class='bookmarks-group'>");
            writer.Append("<ul class='list list-gap-2'>");

            foreach (BookmarkEntry entry in bookmarks)
            {
                ReadOnlySpan<char> nameSpan = entry.Name.AsSpan();

                nameSpan = nameSpan[..Math.Min(nameSpan.Length, 20)];

                writer.Append("<li class='flex items-center gap-10'>");

                //Enable the built-in bookmarks link (makes it prettier)
                writer.Append("<a class='bookmarks-link color-highlight' href='");
                writer.Append(entry.Url);
                writer.Append("'>");
                writer.Append(nameSpan);

                writer.Append("</a>");
                writer.Append("</li>");
            }

            writer.Append("</ul>");
            writer.Append("</li>");
            writer.Append("</ul>");

            //Close the document
            writer.Append("</body>");
            writer.Append("</html>");

            int byteCount = Encoding.UTF8.GetByteCount(writer.AsSpan());

            using (UnsafeMemoryHandle<byte> utf8Buffer = MemoryUtil.UnsafeAllocNearestPage(byteCount, true))
            {
                //Encode utf8 bytes
                Encoding.UTF8.GetBytes(writer.AsSpan(), utf8Buffer.Span);

                vms.Write(utf8Buffer.AsSpan(0, byteCount));
            }

            vms.Seek(0, System.IO.SeekOrigin.Begin);
        }
    }
}