summaryrefslogtreecommitdiffstats
path: root/src/store/feed.ts
blob: aaf9a96f064b0b38f8d3bdf1cf21fdfd5655ca62 (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
import axios from "@nextcloud/axios";
import { generateUrl } from "@nextcloud/router";

import { ActionParams, AppState } from 'src/store'
import { Feed } from '../types/Feed'

export const FEED_MUTATION_TYPES = {
  SET_FEEDS: 'SET_FEEDS',
}

export const FEED_ACTION_TYPES = {
  ADD_FEED: 'ADD_FEED',
  FETCH_FEEDS: 'FETCH_FEEDS',
}

const feedUrl = generateUrl("/apps/news/feeds")

export const FEED_ACTIONS = {
  async [FEED_ACTION_TYPES.FETCH_FEEDS] ({ commit }: ActionParams) {
    const feeds = await axios.get(
      generateUrl("/apps/news/feeds")
    );

    commit(FEED_MUTATION_TYPES.SET_FEEDS, feeds.data.feeds);
  },
  [FEED_ACTION_TYPES.ADD_FEED] ({ commit }: ActionParams, { feedReq }: { feedReq: { url: string; folder?: { id: number } } }) {
      console.log(feedReq)
      let url = feedReq.url.trim();
      if (!url.startsWith('http')) {
          url = 'https://' + url;
      }

      /**
      if (title !== undefined) {
          title = title.trim();
      }
      */

      let feed: Feed = {
          url: url,
          folderId: feedReq.folder?.id || 0,
          title: undefined,
          unreadCount: 0,
          autoDiscover: undefined // TODO
      };

      // this.add(feed);
      // this.updateFolderCache();

      axios.post(feedUrl, {
          url: feed.url,
          parentFolderId: feed.folderId,
          title: null,
          user: null,
          password: null,
          fullDiscover: feed.autoDiscover
      }).then(() => {
        commit('addFeed', feed)
      });
  }
}

export const FEED_MUTATIONS = {
  [FEED_MUTATION_TYPES.SET_FEEDS] (state: AppState, feeds: Feed[]) {
    feeds.forEach(it => {
      state.feeds.push(it)
      const folder = state.folders.find(folder => folder.id === it.folderId)
      if (folder) {
        folder.feeds.push(it)
        folder.feedCount += it.unreadCount
      }
    })
  },
}