-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
timeline.go
133 lines (106 loc) · 3.38 KB
/
timeline.go
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
package mast
import (
"context"
"github.com/mattn/go-mastodon"
)
type TimelineType int
const (
TimelineHome TimelineType = 0
TimelineLocal = 1
TimelinePublic = 2
TimelineNotifications = 3
TimelineEnd = 4
)
type Timeline struct {
client *mastodon.Client
timelineType TimelineType
LastRenderedIndex int
Account mastodon.Account
Toots []Toot
TootIndexStatusIDMappings map[string]int
KnownUsers []string
}
func NewTimeline(mastodonClient *mastodon.Client) Timeline {
timeline := Timeline{
client: mastodonClient,
timelineType: TimelineHome,
LastRenderedIndex: -1,
TootIndexStatusIDMappings: make(map[string]int),
}
return timeline
}
func (timeline *Timeline) Switch(timelineType TimelineType) {
if timeline.timelineType != timelineType {
timeline.timelineType = timelineType
timeline.Toots = []Toot{}
timeline.TootIndexStatusIDMappings = make(map[string]int)
timeline.LastRenderedIndex = -1
}
}
func (timeline *Timeline) GetCurrentType() (TimelineType) {
return timeline.timelineType
}
func (timeline *Timeline) Load() (error) {
var statuses []*mastodon.Status
var err error
account, err := timeline.client.GetAccountCurrentUser(context.Background())
if err != nil {
return err
}
timeline.Account = *account
switch timeline.timelineType {
case TimelineHome:
statuses, err = timeline.client.GetTimelineHome(context.Background(), nil)
case TimelineLocal:
statuses, err = timeline.client.GetTimelinePublic(context.Background(), true, nil)
case TimelinePublic:
statuses, err = timeline.client.GetTimelinePublic(context.Background(), false, nil)
case TimelineNotifications:
notifications, err := timeline.client.GetNotifications(context.Background(), nil)
if err != nil {
return err
}
for _, notification := range notifications {
statuses = append(statuses, notification.Status)
}
}
if err != nil {
return err
}
oldestStatusIndex := len(statuses) - 1
for i := oldestStatusIndex; i >= 0; i-- {
status := statuses[i]
id := string(status.ID)
_, exists := timeline.TootIndexStatusIDMappings[id]
if exists == false {
tootIndex := len(timeline.Toots)
timeline.Toots = append(timeline.Toots, NewToot(timeline.client, status, tootIndex))
timeline.KnownUsers = append(timeline.KnownUsers, status.Account.Acct) // TODO: Deduplicate
timeline.TootIndexStatusIDMappings[id] = tootIndex
}
}
return nil
}
func (timeline *Timeline) Toot(status *string, inReplyTo int, filesToUpload []string, visibility *string, sensitive bool, spoiler *string) (*mastodon.Status, error) {
newToot := mastodon.Toot{
Status: *status,
Visibility: *visibility,
Sensitive: sensitive,
SpoilerText: *spoiler,
}
if inReplyTo > -1 {
newToot.InReplyToID = timeline.Toots[inReplyTo].Status.ID
}
if len(filesToUpload) > 0 {
var mediaIDs []mastodon.ID
for _, fileToUpload := range filesToUpload {
attachment, err := timeline.client.UploadMedia(context.Background(), fileToUpload)
if err != nil {
return nil, err
}
mediaIDs = append(mediaIDs, attachment.ID)
}
newToot.MediaIDs = mediaIDs
}
return timeline.client.PostStatus(context.Background(), &newToot)
}