aboutsummaryrefslogtreecommitdiff
path: root/back-end/src/Model/ChannelManager.cs
blob: 22dbab97e5bed10b2e486247cbb31dd3c2abe770 (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
/*
* Copyright (c) 2023 Vaughn Nugent
* 
* Library: CMNext
* Package: Content.Publishing.Blog.Admin
* File: ChannelManager.cs 
*
* CMNext 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.
*
* CMNext 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.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;

using VNLib.Utils.IO;
using VNLib.Hashing;
using VNLib.Plugins;
using VNLib.Plugins.Extensions.Loading;

using Content.Publishing.Blog.Admin.Storage;


namespace Content.Publishing.Blog.Admin.Model
{

    [ConfigurationName("blog_channels")]
    internal sealed class ChannelManager : IChannelContextManager
    {
        private readonly ISimpleFilesystem Storage;
        private readonly string _indexPath;


        public ChannelManager(PluginBase plugin, IConfigScope config)
        {
            //Init minio client
            Storage = plugin.GetOrCreateSingleton<ManagedStorage>();

            _indexPath = config["index_file_name"].GetString() ?? "channels.json";
        }

        ///<inheritdoc/>
        public async Task<bool> CreateChannelAsync(BlogChannel context, CancellationToken cancellation)
        {
            _ = context.Id ?? throw new ArgumentNullException(nameof(context.Id));

            IRecordDb<BlogChannel> db = await LoadDb(cancellation);

            BlogChannel? existing = db.GetRecord(context.Id);

            //Make sure the id is unique
            if (existing != null)
            {
                return false;
            }

            //Add to the index
            db.SetRecord(context);

            //Publish updated index to storage
            await StoreDb(db, cancellation);

            return true;
        }

        ///<inheritdoc/>
        public async Task<bool> UpdateChannelAsync(BlogChannel channel, CancellationToken cancellation)
        {
            _ = channel.Id ?? throw new ArgumentNullException(nameof(channel.Id));

            IRecordDb<BlogChannel> db = await LoadDb(cancellation);

            //Get the original context for the channel
            _ = db.GetRecord(channel.Id) ?? throw new KeyNotFoundException("The requested channel does not exist");

            //Add the updated context to the index
            db.SetRecord(channel);

            //Publish updated index to storage
            await StoreDb(db, cancellation);

            return true;
        }

        ///<inheritdoc/>
        public async Task DeleteChannelAsync(IChannelContext context, CancellationToken cancellation)
        {
            _ = context?.Id ?? throw new ArgumentNullException(nameof(context));

            IRecordDb<BlogChannel> db = await LoadDb(cancellation);

            //Remove from the index
            db.RemoveRecord(context.Id);

            //Delete the channel dir
            await Storage.DeleteFileAsync(context.BaseDir, cancellation);

            //Publish updated index to storage
            await StoreDb(db, cancellation);
        }

        /// <summary>
        /// Computes the unique id for a context
        /// </summary>
        /// <param name="context">The context to produce the context id for</param>
        /// <returns>The unique context id</returns>
        public static string ComputeContextId(IChannelContext context)
        {
            //Context-id is the hash of the base dir and index file path
            return ManagedHash.ComputeHexHash($"{context.BaseDir}/{context.IndexPath}", HashAlg.SHA1).ToLowerInvariant();
        }

        ///<inheritdoc/>
        public async Task<IChannelContext?> GetChannelAsync(string id, CancellationToken cancellation)
        {
            //Recover the db
            IRecordDb<BlogChannel> db = await LoadDb(cancellation);

            //Get the channel
            return db.GetRecord(id);
        }

        ///<inheritdoc/>
        public async Task<object[]> GetAllContextsAsync(CancellationToken cancellation)
        {
            //Recover the db
            IRecordDb<BlogChannel> db = await LoadDb(cancellation);

            //Get the channel
            return db.GetRecords().ToArray();
        }

       
        private Task<IRecordDb<BlogChannel>> LoadDb(CancellationToken cancellation)
        {
            return Storage.LoadDbAsync<BlogChannel>(_indexPath, cancellation);
        }

        private Task StoreDb(IRecordDb<BlogChannel> db, CancellationToken cancellation)
        {
            return Storage.StoreAsync(_indexPath, db, cancellation);
        }

    }
}