aboutsummaryrefslogtreecommitdiff
path: root/lib/client/src/channels.ts
blob: 6ddeccab11e4b7ee65e1530139c01920530ca0e9 (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
// Copyright (C) 2023 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/>.

import { find, isEqual } from 'lodash-es';
import { CMNextApi, CMNextIndex, ChannelMeta } from './types'

export interface ChannelApi extends CMNextApi<ChannelMeta> {
    /**
     * Gets the endpoint url for the channel
     */
    readonly channelFile: string;
}

/**
 * Gets the channel api for the given channel json file url
 * @param endpoint The url of the channel json file
 */
export const createChannelApi = (channelFile: string): ChannelApi => {

    const getIndex = async () : Promise<CMNextIndex<ChannelMeta>> => {
        const res = await fetch(channelFile)
        return await res.json()
    }

    return { channelFile, getIndex }
}

export interface ScopedChannelApi extends ChannelApi {
    /**
     * Gets the post index path for the currently selected channel
     */
    getPostIndexPath(): Promise<string | undefined>;
    /**
     * Gets the content index path for the currently selected channel
     */
    getContentIndexPath(): Promise<string | undefined>;
    /**
     * Gets the base dir for the currently selected channel
     */
    getBaseDir(): Promise<string | undefined>;
    /**
     * Gets the content dir for the currently selected channel
     */
    getContentDir(): Promise<string | undefined>;
}

export const createScopedChannelApi = (channelFile: string, channelId: string): ScopedChannelApi => {

    const channelApi = createChannelApi(channelFile);

    const getSelectedChannel = async (): Promise<ChannelMeta | undefined> => {
        const index = await channelApi.getIndex()
        //Get the selected channel from the channels
        return find(index.records, i => isEqual(i.id, channelId))
    }

    //begin getting the selected channel
    const index = getSelectedChannel();

    const getPostIndexPath = async (): Promise<string | undefined> => {
        //Await the selected channel index
        const channel = await index
        return channel ? `${channel.path}/${channel.index}` : undefined;
    }

    const getContentDir = async (): Promise<string | undefined> => {
        //Await the selected channel index
        const channel = await index
        return channel ? channel.content : undefined;
    }

    const getBaseDir = async (): Promise<string | undefined> => {
        //Await the selected channel index
        const channel = await index
        return channel ? channel.path : undefined;
    }

    const getContentIndexPath = async (): Promise<string | undefined> => {
        //Await the selected channel index
        const channel = await index
        //Get the post index from the channel
        return channel ? `${channel.path}/${channel.content}` : undefined;
    }

    return{
        ...channelApi,
        getBaseDir,
        getContentDir,
        getPostIndexPath,
        getContentIndexPath,
    }
}