-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharticle-list.go
More file actions
142 lines (129 loc) · 3.49 KB
/
Copy patharticle-list.go
File metadata and controls
142 lines (129 loc) · 3.49 KB
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
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright Clément Joly and contributors.
//
// 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/>.
package main
import (
"strconv"
miniflux "miniflux.app/client"
)
// Treat the unread articles as a list navigated by offsets. If new articles
// are added, the offset won’t send back to the same articles, but that should
// be infrequent enough
type ArticleList struct {
miniflux.Filter
}
// Default parameters used in filters, that defines the basis for the article
// list
func defaultFilter() miniflux.Filter {
return miniflux.Filter{
Status: "unread",
Order: "published_at",
Direction: "desc",
}
}
func NewArticleList() ArticleList {
return ArticleList{defaultFilter()}
}
// ExtendFilter takes net/url.Url.Values (in the generic form of a map) and
// completes the default filter
func (al *ArticleList) Extend(values map[string][]string) {
if k, exists := values["status"]; exists {
al.Status = k[0]
}
if k, exists := values["offset"]; exists {
offset, err := strconv.Atoi(k[0])
if err == nil {
al.Offset = offset
}
}
if k, exists := values["limit"]; exists {
limit, err := strconv.Atoi(k[0])
if err == nil {
al.Limit = limit
}
}
if k, exists := values["order"]; exists {
al.Order = k[0]
}
if k, exists := values["direction"]; exists {
al.Direction = k[0]
}
if k, exists := values["starred"]; exists {
switch k[0] {
case "true", "false":
al.Starred = k[0]
}
}
if k, exists := values["before"]; exists {
before, err := strconv.ParseInt(k[0], 10, 64)
if err == nil {
al.Before = before
}
}
if k, exists := values["after"]; exists {
after, err := strconv.ParseInt(k[0], 10, 64)
if err == nil {
al.After = after
}
}
if k, exists := values["afterEntryID"]; exists {
afterEntryID, err := strconv.ParseInt(k[0], 10, 64)
if err == nil {
al.AfterEntryID = afterEntryID
}
}
if k, exists := values["beforeEntryID"]; exists {
beforeEntryID, err := strconv.ParseInt(k[0], 10, 64)
if err == nil {
al.BeforeEntryID = beforeEntryID
}
}
if k, exists := values["search"]; exists {
al.Search = k[0]
}
if k, exists := values["categoryID"]; exists {
categoryID, err := strconv.ParseInt(k[0], 10, 64)
if err == nil {
al.CategoryID = categoryID
}
}
if k, exists := values["feedID"]; exists {
feedID, err := strconv.ParseInt(k[0], 10, 64)
if err == nil {
al.FeedID = feedID
}
}
if k, exists := values["statuses"]; exists {
al.Statuses = k
}
}
// First returns the first entry
func (al *ArticleList) First(client *miniflux.Client) (*miniflux.Entry, error) {
prevLimit := al.Filter.Limit
al.Filter.Limit = 1
entrySet, err := client.Entries(&al.Filter)
if err != nil {
return nil, err
}
al.Filter.Limit = prevLimit
entries := entrySet.Entries
entry := new(miniflux.Entry)
if len(entries) > 0 {
entry = entries[0]
} else {
return nil, nil
}
return entry, err
}