author | Mikael Berthe <mikael@lilotux.net> |
Fri, 07 Sep 2018 19:17:12 +0200 | |
changeset 242 | 2ec8b4cdf94e |
parent 214 | 642d690de0ba |
child 244 | 4508376c8fcb |
permissions | -rw-r--r-- |
130 | 1 |
/* |
207 | 2 |
Copyright 2017-2018 Mikael Berthe |
130 | 3 |
|
4 |
Licensed under the MIT license. Please see the LICENSE file is this directory. |
|
5 |
*/ |
|
6 |
||
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
7 |
package madon |
88 | 8 |
|
9 |
import ( |
|
10 |
"strings" |
|
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
160
diff
changeset
|
11 |
|
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
160
diff
changeset
|
12 |
"github.com/pkg/errors" |
88 | 13 |
) |
14 |
||
15 |
// GetTimelines returns a timeline (a list of statuses |
|
213
3cade8971ed9
GetTimelines: Add support for list-based timelines
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
16 |
// timeline can be "home", "public", a hashtag (use ":hashtag" or "#hashtag") |
3cade8971ed9
GetTimelines: Add support for list-based timelines
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
17 |
// or a list (use "!N", e.g. "!42" for list ID #42). |
118
d9c798e09f0a
Add an option to get the local timeline
Mikael Berthe <mikael@lilotux.net>
parents:
98
diff
changeset
|
18 |
// For the public timelines, you can set 'local' to true to get only the |
d9c798e09f0a
Add an option to get the local timeline
Mikael Berthe <mikael@lilotux.net>
parents:
98
diff
changeset
|
19 |
// local instance. |
214
642d690de0ba
GetTimelines: Add parameter 'onlyMedia' (Mastodon 2.3.0+)
Mikael Berthe <mikael@lilotux.net>
parents:
213
diff
changeset
|
20 |
// Set 'onlyMedia' to true to only get statuses that have media attachments. |
156
70aadba26338
Add field "All" to LimitParams, change Limit behaviour
Mikael Berthe <mikael@lilotux.net>
parents:
155
diff
changeset
|
21 |
// If lopt.All is true, several requests will be made until the API server |
70aadba26338
Add field "All" to LimitParams, change Limit behaviour
Mikael Berthe <mikael@lilotux.net>
parents:
155
diff
changeset
|
22 |
// has nothing to return. |
70aadba26338
Add field "All" to LimitParams, change Limit behaviour
Mikael Berthe <mikael@lilotux.net>
parents:
155
diff
changeset
|
23 |
// If lopt.Limit is set (and not All), several queries can be made until the |
70aadba26338
Add field "All" to LimitParams, change Limit behaviour
Mikael Berthe <mikael@lilotux.net>
parents:
155
diff
changeset
|
24 |
// limit is reached. |
214
642d690de0ba
GetTimelines: Add parameter 'onlyMedia' (Mastodon 2.3.0+)
Mikael Berthe <mikael@lilotux.net>
parents:
213
diff
changeset
|
25 |
func (mc *Client) GetTimelines(timeline string, local, onlyMedia bool, lopt *LimitParams) ([]Status, error) { |
88 | 26 |
var endPoint string |
27 |
||
118
d9c798e09f0a
Add an option to get the local timeline
Mikael Berthe <mikael@lilotux.net>
parents:
98
diff
changeset
|
28 |
switch { |
d9c798e09f0a
Add an option to get the local timeline
Mikael Berthe <mikael@lilotux.net>
parents:
98
diff
changeset
|
29 |
case timeline == "home", timeline == "public": |
88 | 30 |
endPoint = "timelines/" + timeline |
127
96a7f2432d27
GetTimelines: Allow '#' as hashtag prefix
Mikael Berthe <mikael@lilotux.net>
parents:
120
diff
changeset
|
31 |
case strings.HasPrefix(timeline, ":"), strings.HasPrefix(timeline, "#"): |
94
beee0238a82e
Timelines: fix hashtag timelines
Mikael Berthe <mikael@lilotux.net>
parents:
92
diff
changeset
|
32 |
hashtag := timeline[1:] |
beee0238a82e
Timelines: fix hashtag timelines
Mikael Berthe <mikael@lilotux.net>
parents:
92
diff
changeset
|
33 |
if hashtag == "" { |
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
160
diff
changeset
|
34 |
return nil, errors.New("timelines API: empty hashtag") |
94
beee0238a82e
Timelines: fix hashtag timelines
Mikael Berthe <mikael@lilotux.net>
parents:
92
diff
changeset
|
35 |
} |
beee0238a82e
Timelines: fix hashtag timelines
Mikael Berthe <mikael@lilotux.net>
parents:
92
diff
changeset
|
36 |
endPoint = "timelines/tag/" + hashtag |
213
3cade8971ed9
GetTimelines: Add support for list-based timelines
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
37 |
case len(timeline) > 1 && strings.HasPrefix(timeline, "!"): |
3cade8971ed9
GetTimelines: Add support for list-based timelines
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
38 |
// Check the timeline is a number |
3cade8971ed9
GetTimelines: Add support for list-based timelines
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
39 |
for _, n := range timeline[1:] { |
3cade8971ed9
GetTimelines: Add support for list-based timelines
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
40 |
if n < '0' || n > '9' { |
3cade8971ed9
GetTimelines: Add support for list-based timelines
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
41 |
return nil, errors.New("timelines API: invalid list ID") |
3cade8971ed9
GetTimelines: Add support for list-based timelines
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
42 |
} |
3cade8971ed9
GetTimelines: Add support for list-based timelines
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
43 |
} |
3cade8971ed9
GetTimelines: Add support for list-based timelines
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
44 |
endPoint = "timelines/list/" + timeline[1:] |
118
d9c798e09f0a
Add an option to get the local timeline
Mikael Berthe <mikael@lilotux.net>
parents:
98
diff
changeset
|
45 |
default: |
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
160
diff
changeset
|
46 |
return nil, errors.New("GetTimelines: bad timelines argument") |
118
d9c798e09f0a
Add an option to get the local timeline
Mikael Berthe <mikael@lilotux.net>
parents:
98
diff
changeset
|
47 |
} |
d9c798e09f0a
Add an option to get the local timeline
Mikael Berthe <mikael@lilotux.net>
parents:
98
diff
changeset
|
48 |
|
120 | 49 |
params := make(apiCallParams) |
50 |
if timeline == "public" && local { |
|
51 |
params["local"] = "true" |
|
88 | 52 |
} |
214
642d690de0ba
GetTimelines: Add parameter 'onlyMedia' (Mastodon 2.3.0+)
Mikael Berthe <mikael@lilotux.net>
parents:
213
diff
changeset
|
53 |
if onlyMedia { |
642d690de0ba
GetTimelines: Add parameter 'onlyMedia' (Mastodon 2.3.0+)
Mikael Berthe <mikael@lilotux.net>
parents:
213
diff
changeset
|
54 |
params["only_media"] = "true" |
642d690de0ba
GetTimelines: Add parameter 'onlyMedia' (Mastodon 2.3.0+)
Mikael Berthe <mikael@lilotux.net>
parents:
213
diff
changeset
|
55 |
} |
88 | 56 |
|
160
9f7e683b323f
Refactor methods returning a list of statuses
Mikael Berthe <mikael@lilotux.net>
parents:
156
diff
changeset
|
57 |
return mc.getMultipleStatuses(endPoint, params, lopt) |
88 | 58 |
} |