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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
###
# ownCloud news app
#
# @author Alessandro Cosentino
# @author Bernhard Posselt
# Copyright (c) 2012 - Alessandro Cosentino <cosenal@gmail.com>
# Copyright (c) 2012 - Bernhard Posselt <nukeawhale@gmail.com>
#
# This file is licensed under the Affero General Public License version 3 or
# later.
#
# See the COPYING-README file
#
###
angular.module('News').factory '_FeedController', ['Controller', (Controller) ->
class FeedController extends Controller
constructor: (@$scope, @feedModel, @folderModel, @feedType, @activeFeed,
@persistence, @starredCount, @showAll, @itemModel,
@garbageRegistry, @$rootScope, @loading, @config) ->
@showSubscriptions = true
@$scope.feeds = @feedModel.getItems()
@$scope.folders = @folderModel.getItems()
@$scope.feedType = @feedType
@$scope.getShowAll = =>
return @showAll.showAll
@$scope.setShowAll = (value) =>
@showAll.showAll = value
@persistence.showAll(value)
@$rootScope.$broadcast('triggerHideRead')
@$scope.addFeed = (url, folder) =>
@$scope.feedEmptyError = false
@$scope.feedExistsError = false
@$scope.feedError = false
if url == undefined or url.trim() == ''
@$scope.feedEmptyError = true
else
url = url.trim()
for feed in @feedModel.getItems()
if url == feed.url # FIXME: can we really compare this
@$scope.feedExistsError = true
if not (@$scope.feedEmptyError or @$scope.feedExistsError)
if folder == undefined
folderId = 0
else
folderId = folder.id
@addingFeed = true
onSuccess = =>
@$scope.feedUrl = ''
@addingFeed = false
onError = =>
@$scope.feedError = true
@addingFeed = false
@persistence.createFeed(url, folderId, onSuccess, onError)
@$scope.addFolder = (name) =>
@$scope.folderEmptyError = false
@$scope.folderExistsError = false
if name == undefined or name.trim() == ''
@$scope.folderEmptyError = true
else
name = name.trim()
for folder in @folderModel.getItems()
if name.toLowerCase() == folder.name.toLowerCase()
@$scope.folderExistsError = true
if not (@$scope.folderEmptyError or @$scope.folderExistsError)
@addingFolder = true
onSuccess = =>
@$scope.folderName = ''
@addingFolder = false
@persistence.createFolder(name, onSuccess)
@$scope.toggleFolder = (folderId) =>
folder = @folderModel.getItemById(folderId)
folder.open = !folder.open
@persistence.collapseFolder(folder.id, folder.open)
@$scope.isFeedActive = (type, id) =>
if type == @activeFeed.type && id == @activeFeed.id
return true
else
return false
@$scope.loadFeed = (type, id) =>
@loadFeed(type, id)
@$scope.getUnreadCount = (type, id) =>
count = @getUnreadCount(type, id)
if count > 999
return "999+"
else
return count
@$scope.renameFolder = ->
alert 'not implemented yet, needs better solution'
@$scope.triggerHideRead = =>
@triggerHideRead()
@$scope.isShown = (type, id) =>
switch type
when @feedType.Subscriptions then return @showSubscriptions
when @feedType.Starred then return @starredCount.count > 0
@$scope.delete = (type, id) =>
switch type
when @feedType.Folder
@folderModel.removeById(id)
@persistence.deleteFolder(id)
when @feedType.Feed
@feedModel.removeById(id)
@persistence.deleteFeed(id)
@$scope.markAllRead = (type, id) =>
switch type
when @feedType.Feed
for itemId, item of @itemModel.getItemsByTypeAndId(type, id)
item.isRead = true
feed = @feedModel.getItemById(id)
feed.unreadCount = 0
mostRecentItemId = @itemModel.getHighestId(type, id)
@persistence.setAllItemsRead(feed.id, mostRecentItemId)
when @feedType.Folder
for itemId, item of @itemModel.getItemsByTypeAndId(type, id)
item.isRead = true
for feedId in @itemModel.getFeedsOfFolderId(id)
feed = @feedModel.getItemById(feedId)
feed.unreadCount = 0
mostRecentItemId = @itemModel.getHighestId(type, feedId)
@persistence.setAllItemsRead(feedId, mostRecentItemId)
when @feedType.Subscriptions
for itemId, item of @itemModel.getItemsByTypeAndId(type, id)
item.isRead = true
for feed in @feedModel.getItems()
feed.unreadCount = 0
mostRecentItemId = @itemModel.getHighestId(type, feed.id)
@persistence.setAllItemsRead(feed.id, mostRecentItemId)
@$scope.$on 'triggerHideRead', =>
@itemModel.clearCache()
@triggerHideRead()
@loadFeed(activeFeed.type, activeFeed.id)
@$scope.$on 'loadFeed', (scope, params) =>
@loadFeed(params.type, params.id)
@$scope.$on 'moveFeedToFolder', (scope, params) =>
@moveFeedToFolder(params.feedId, params.folderId)
setInterval =>
@updateFeeds()
, @config.FeedUpdateInterval
updateFeeds: ->
for feed in @feedModel.getItems()
@persistence.updateFeed(feed.id)
moveFeedToFolder: (feedId, folderId) ->
feed = @feedModel.getItemById(feedId)
if feed.folderId != folderId
feed.folderId = folderId
@feedModel.markAccessed()
@persistence.moveFeedToFolder(feedId, folderId)
loadFeed: (type, id) ->
# to not go crazy with autopaging, clear the caches if we switch the
# type of the feed. if the caches only contain seperate feeds, the
# cache and autopage logic works fine. if the feed contains more than
# one
if type != @activeFeed.type or id != @activeFeed.id
if not (type == @feedType.Feed && @activeFeed.type == @feedType.Feed)
@itemModel.clearCache()
@activeFeed.id = id
@activeFeed.type = type
@$scope.triggerHideRead()
@persistence.loadFeed(type, id,
@itemModel.getHighestId(type, id),
@itemModel.getHighestTimestamp(type, id), @config.initialLoadedItemsNr)
triggerHideRead: () ->
preventParentFolder = 0
# feeds
for feed in @feedModel.getItems()
if @showAll.showAll == false && @getUnreadCount(@feedType.Feed, feed.id) == 0
# we dont hide the selected feed and folder. But we also dont hide
# the parent folder of the selcted feed
if @activeFeed.type == @feedType.Feed && @activeFeed.id == feed.id
feed.show = true
preventParentFolder = feed.folderId
else
feed.show = false
else
feed.show = true
# folders
for folder in @folderModel.getItems()
if @showAll.showAll == false && @getUnreadCount(@feedType.Folder, folder.id) == 0
# prevent hiding when childfeed is active
if (@activeFeed.type == @feedType.Folder && @activeFeed.id == folder.id)
|