aboutsummaryrefslogtreecommitdiff
path: root/back-end/src/Model/BookmarkStore.cs
blob: d53ab017e045c940292de5ded36b6465c5dc0326 (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// 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 Microsoft.EntityFrameworkCore;

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

using VNLib.Utils;
using VNLib.Plugins;
using VNLib.Plugins.Extensions.Data;
using VNLib.Plugins.Extensions.Loading;
using VNLib.Plugins.Extensions.Data.Abstractions;
using VNLib.Plugins.Extensions.Loading.Sql;

namespace SimpleBookmark.Model
{
    internal sealed class BookmarkStore(PluginBase plugin) : DbStore<BookmarkEntry>
    {
        private readonly IAsyncLazy<DbContextOptions> dbOptions = plugin.GetContextOptionsAsync();

        ///<inheritdoc/>
        public override IDbQueryLookup<BookmarkEntry> QueryTable { get; } = new BookmarkQueryLookup();

        ///<inheritdoc/>
        public override IDbContextHandle GetNewContext() => new SimpleBookmarkContext(dbOptions.Value);

        ///<inheritdoc/>
        public override string GetNewRecordId() => Guid.NewGuid().ToString("n");

        ///<inheritdoc/>
        public override void OnRecordUpdate(BookmarkEntry newRecord, BookmarkEntry existing)
        {
            //Update existing record
            existing.Name = newRecord.Name;
            existing.Url = newRecord.Url;
            existing.Description = newRecord.Description;
            existing.JsonTags = newRecord.JsonTags;
        }
     
        public async Task<int> AddSingleIfNotExists(string userId, BookmarkEntry entry, DateTime now, uint maxRecords, CancellationToken cancellation)
        {
            ArgumentNullException.ThrowIfNull(userId);
            ArgumentNullException.ThrowIfNull(entry);

            //Init new db connection
            await using SimpleBookmarkContext context = new(dbOptions.Value);

            //Check if any bookmarks exist for the user with a given url
            bool exists = await context.Bookmarks.AnyAsync(b => b.UserId == userId && b.Url == entry.Url, cancellation);

            //If no bookmarks exist, add a new one
            if (!exists)
            {
                //Check if the user has reached the maximum number of bookmarks
                if (await context.Bookmarks.CountAsync(b => b.UserId == userId, cancellation) >= maxRecords)
                {
                    await context.SaveAndCloseAsync(true, cancellation);
                    return -1;
                }

                context.Bookmarks.Add(new ()
                {
                    Id = GetNewRecordId(),      //Overwrite with new record id
                    UserId = userId,            //Enforce user id
                    Created = now,
                    LastModified = now,
                    Name = entry.Name,          //Copy over the entry data
                    Url = entry.Url,
                    Description = entry.Description,
                    Tags = entry.Tags
                });
            }

            await context.SaveAndCloseAsync(true, cancellation);
            return exists ? 0 : 1;         //1 if added, 0 if already exists
        }

        public async Task<BookmarkEntry[]> SearchBookmarksAsync(string userId, string? query, string[] tags, int limit, int page, CancellationToken cancellation)
        {
            BookmarkEntry[] results;

            ArgumentNullException.ThrowIfNull(userId);
            ArgumentNullException.ThrowIfNull(tags);
            ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(limit, 0);
            ArgumentOutOfRangeException.ThrowIfNegative(page);

            //Init new db connection
            await using SimpleBookmarkContext context = new(dbOptions.Value);

            //Build the query starting with the user's bookmarks
            IQueryable<BookmarkEntry> q = context.Bookmarks.Where(b => b.UserId == userId);

            //reduce result set by search query first
            if (!string.IsNullOrWhiteSpace(query))
            {               
                q = WithSearch(q, query);
            }
            
            q = q.OrderByDescending(static b => b.Created);

            /*
              *  For some databases server-side tag filtering is not supported.
              *  Client side evaluation must be used to finally filter the results.
              *  
              *  I am attempting to reduce the result set as much as possible on server-side
              *  evaluation before pulling the results into memory. So search, ordering and
              *  first tag filtering is done on server-side. The final tag filtering is done
              *  for multiple tags on client-side along with pagination. For bookmarks, I expect
              *  the result set to at worst double digits for most users, so this should be fine.
              *  
              */

            if (tags.Length > 0)
            {
             
                //filter out bookmarks that do not have any tags and reduce by the first tag
                q = q.Where(static b => b.Tags != null && b.Tags.Length > 0)
                    .Where(b => EF.Functions.Like(b.Tags, $"%{tags[0]}%"));
            }

            if(tags.Length > 1)
            {
                //Finally pull all results into memory
                BookmarkEntry[] bookmarkEntries = await q.ToArrayAsync(cancellation);

                //filter out bookmarks that do not have all requested tags, then skip and take the requested page
                results = bookmarkEntries.Where(b => tags.All(p => b.JsonTags!.Contains(p)))
                    .Skip(page * limit)
                    .Take(limit)
                    .ToArray();
            }
            else
            {
                //execute server-side query
                results = await q.Skip(page * limit)
                    .Take(limit)
                    .ToArrayAsync(cancellation);               
            }           

            //Close db and commit transaction
            await context.SaveAndCloseAsync(true, cancellation);

            return results;
        }

        private static IQueryable<BookmarkEntry> WithSearch(IQueryable<BookmarkEntry> q, string query)
        {
            //if query is set, only return bookmarks that match the query
            return q.Where(b => EF.Functions.Like(b.Name, $"%{query}%") || EF.Functions.Like(b.Description, $"%{query}%"));
        }

        public async Task<string[]> GetAllTagsForUserAsync(string userId, CancellationToken cancellation)
        {
            ArgumentNullException.ThrowIfNull(userId);

            //Init new db connection
            await using SimpleBookmarkContext context = new(dbOptions.Value);
            
            //Get all tags for the user
            string[] tags = await context.Bookmarks
                .Where(b => b.UserId == userId)
                .Select(static b => b.Tags!)
                .ToArrayAsync(cancellation);

            //Close db and commit transaction
            await context.SaveAndCloseAsync(true, cancellation);

            //Split tags into individual strings
            return tags
                .Where(static t => !string.IsNullOrWhiteSpace(t))
                .SelectMany(static t => t!.Split(','))
                .Distinct()
                .ToArray();
        }

        public async Task<ERRNO> DeleteAllForUserAsync(string userId, CancellationToken cancellation)
        {
            await using SimpleBookmarkContext context = new(dbOptions.Value);

            context.Bookmarks.RemoveRange(context.Bookmarks.Where(b => b.UserId == userId));

            return await context.SaveAndCloseAsync(true, cancellation);
        }

        public async Task<ERRNO> AddBulkAsync(IEnumerable<BookmarkEntry> bookmarks, string userId, DateTimeOffset now, CancellationToken cancellation)
        {
            //Init new db connection
            await using SimpleBookmarkContext context = new(dbOptions.Value);

            //Setup clean bookmark instances
            bookmarks = bookmarks.Select(b => new BookmarkEntry
            {
                Id = GetNewRecordId(),  //new uuid
                UserId = userId,        //Set userid
                LastModified = now.DateTime,

                //Allow reuse of created time
                Created = b.Created,
                Description = b.Description,
                Name = b.Name,
                Tags = b.Tags,
                Url = b.Url,
            });

            //Filter out bookmarks that already exist
            bookmarks = bookmarks.Where(b => !context.Bookmarks.Any(b2 => b2.UserId == userId && b2.Url == b.Url));

            //Add bookmarks to db
            context.AddRange(bookmarks);

            //Commit transaction
            return await context.SaveAndCloseAsync(true, cancellation);
        }

        private sealed class BookmarkQueryLookup : IDbQueryLookup<BookmarkEntry>
        {
            public IQueryable<BookmarkEntry> GetCollectionQueryBuilder(IDbContextHandle context, params string[] constraints)
            {
                string userId = constraints[0];

                return from b in context.Set<BookmarkEntry>()
                       where b.UserId == userId
                       orderby b.Created descending
                       select b;
            }

            public IQueryable<BookmarkEntry> GetSingleQueryBuilder(IDbContextHandle context, params string[] constraints)
            {
                string bookmarkId = constraints[0];
                string userId = constraints[1];               

                return from b in context.Set<BookmarkEntry>()
                       where b.UserId == userId && b.Id == bookmarkId
                       select b;
            }
        }
    }
}