summaryrefslogtreecommitdiffstats
path: root/js/app/Run.js
blob: 0500faa35657b07dcbdd50150f79827059eba4cd (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
/**
 * Nextcloud - News
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author Bernhard Posselt <dev@bernhard-posselt.com>
 * @copyright Bernhard Posselt 2014
 */
app.run(function ($rootScope, $location, $http, $q, $interval, $route, Loading, ItemResource, FeedResource,
                  FolderResource, SettingsResource, Publisher, BASE_URL, FEED_TYPE, REFRESH_RATE) {
    'use strict';

    // show Loading screen
    Loading.setLoading('global', true);

    // listen to keys in returned queries to automatically distribute the
    // incoming values to models
    Publisher.subscribe(ItemResource).toChannels(['items', 'newestItemId',
        'starred']);
    Publisher.subscribe(FolderResource).toChannels(['folders']);
    Publisher.subscribe(FeedResource).toChannels(['feeds']);
    Publisher.subscribe(SettingsResource).toChannels(['settings']);

    // load feeds, settings and last read feed
    var settingsPromise = $http.get(BASE_URL + '/settings').then(function (response) {
        Publisher.publishAll(response.data);
        return response.data;
    });

    var path = $location.path();
    var activeFeedPromise = $http.get(BASE_URL + '/feeds/active')
        .then(function (response) {
            var url;
            switch (response.data.activeFeed.type) {
                case FEED_TYPE.FEED:
                    url = '/items/feeds/' + response.data.activeFeed.id;
                    break;

                case FEED_TYPE.FOLDER:
                    url = '/items/folders/' + response.data.activeFeed.id;
                    break;

                case FEED_TYPE.STARRED:
                    url = '/items/starred';
                    break;

                case FEED_TYPE.EXPLORE:
                    url = '/explore';
                    break;

                default:
                    url = '/items';
            }

            // only redirect if url is empty or faulty
            if (!/^\/items(\/(starred|explore|feeds\/\d+|folders\/\d+))?\/?$/
                .test(path)) {
                $location.path(url);
            }

            return response.data;
        });

    var feeds;
    var feedPromise = $http.get(BASE_URL + '/feeds').then(function (response) {
        feeds = response.data;
        return feeds;
    });

    var folders;
    var folderPromise = $http.get(BASE_URL + '/folders')
        .then(function (response) {
            folders = response.data;
            return folders;
        });

    $q.all([
        feedPromise,
        folderPromise
    ]).then(function () {
        // first publish feeds to correctly update the folder resource unread
        // cache
        Publisher.publishAll(feeds);
        Publisher.publishAll(folders);
        if (feeds.feeds.length === 0 && folders.folders.length === 0) {
            $location.path('/explore');
        }
    });

    // disable loading if all initial requests finished
    $q.all(
        [
            settingsPromise,
            activeFeedPromise,
            feedPromise,
            folderPromise
        ]
    )
        .then(function () {
            $route.reload();
            Loading.setLoading('global', false);
        });

    // refresh feeds and folders
    $interval(function () {
        $http.get(BASE_URL + '/feeds').then(function (response) {
            Publisher.publishAll(response.data);
        });
        $http.get(BASE_URL + '/folders').then(function (response) {
            Publisher.publishAll(response.data);
        });
    }, REFRESH_RATE * 1000);


    $rootScope.$on('$routeChangeStart', function () {
        Loading.setLoading('content', true);
    });

    $rootScope.$on('$routeChangeSuccess', function () {
        Loading.setLoading('content', false);
    });

    // in case of wrong id etc show all items
    $rootScope.$on('$routeChangeError', function () {
        $location.path('/items');
    });

});