-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathaccount.go
288 lines (251 loc) · 6.69 KB
/
account.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
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
282
283
284
285
286
287
288
package main
import (
"fmt"
"log"
"net/url"
"strings"
"github.com/muesli/telephant/accounts/mastodon"
)
// connectToInstance registers an app with the instance and retrieves an
// authentication URI.
func connectToInstance(instance string) bool {
var authURI string
var redirectURI string
var err error
instance = addHTTPPrefixIfNeeded(instance)
tc, authURI, redirectURI, err = mastodon.RegisterAccount(instance)
if err != nil {
log.Println("Error registering app:", err)
accountBridge.SetError(err.Error())
return false
}
configBridge.SetAuthURL(authURI)
configBridge.SetRedirectURL(redirectURI)
log.Println("auth uri:", authURI)
log.Println("redirect uri:", redirectURI)
return true
}
// addHTTPPrefixIfNeeded adds "https://" to an instance URL where it's missing.
func addHTTPPrefixIfNeeded(instance string) string {
if !strings.HasPrefix(instance, "http://") && !strings.HasPrefix(instance, "https://") {
return "https://" + instance
}
return instance
}
// authInstance authenticates a user via OAuth and retrieves an accesstoken
// we'll use for future logins.
func authInstance(code, redirectURI string) bool {
instance, token, clientID, clientSecret, err := tc.Authenticate(code, redirectURI)
if err != nil {
log.Println("Error authenticating with instance:", err)
accountBridge.SetError(err.Error())
return false
}
config.Account = append(config.Account, Account{
Instance: instance,
ClientID: clientID,
ClientSecret: clientSecret,
Token: token,
})
config.FirstRun = false
SaveConfig(configFile, config)
setupMastodon(config.Account[0])
return true
}
func postLimitCount(body string) int {
return tc.PostLimitCount(body)
}
// reply is used to post a new message
// if replyid is > 0, it's send as a reply
func reply(replyid string, message string, visibility string) {
var attachments []string
for _, v := range attachmentModel.Attachments() {
attachments = append(attachments, v.ID)
}
var err error
if replyid != "" {
log.Println("Sending reply to:", replyid, attachments, message, visibility)
err = tc.Reply(replyid, message, visibility, attachments)
} else {
log.Println("Posting:", attachments, message, visibility)
err = tc.Post(message, visibility, attachments)
}
if err != nil {
accountBridge.SetError(err.Error())
log.Println("Error posting:", err)
}
}
func uploadAttachment(pathurl string) {
u, _ := url.ParseRequestURI(pathurl)
log.Println("Uploding:", u.Path)
tc.UploadAttachment(u.Path)
}
// deletePost deletes a post
func deletePost(id string) {
log.Println("Deleting:", id)
if err := tc.DeletePost(id); err != nil {
accountBridge.SetError(err.Error())
log.Println("Error deleting:", err)
}
}
// share a post
func share(id string) {
log.Println("Sharing:", id)
if err := tc.Share(id); err != nil {
accountBridge.SetError(err.Error())
log.Println("Error sharing:", err)
return
}
m := getMessage(id)
if m == nil {
fmt.Println("Could not retrieve message from store:", id)
return
}
m.Shared = true
m.SharesCount++
updateMessage(id)
}
// unshare a post
func unshare(id string) {
log.Println("Unsharing:", id)
if err := tc.Unshare(id); err != nil {
accountBridge.SetError(err.Error())
log.Println("Error unsharing:", err)
return
}
m := getMessage(id)
if m == nil {
fmt.Println("Could not retrieve message from store:", id)
return
}
m.Shared = false
m.SharesCount--
updateMessage(id)
}
// like a post
func like(id string) {
log.Println("Liking:", id)
if err := tc.Like(id); err != nil {
accountBridge.SetError(err.Error())
log.Println("Error liking:", err)
return
}
m := getMessage(id)
if m == nil {
fmt.Println("Could not retrieve message from store:", id)
return
}
m.Liked = true
m.LikesCount++
updateMessage(id)
}
// unlike a post
func unlike(id string) {
log.Println("Unliking:", id)
if err := tc.Unlike(id); err != nil {
accountBridge.SetError(err.Error())
log.Println("Error unliking:", err)
return
}
m := getMessage(id)
if m == nil {
fmt.Println("Could not retrieve message from store:", id)
return
}
m.Liked = false
m.LikesCount--
updateMessage(id)
}
// follow changes the relationship to another user
func follow(id string, follow bool) {
if follow {
log.Println("Following:", id)
if err := tc.Follow(id); err != nil {
accountBridge.SetError(err.Error())
log.Println("Error following user:", err)
return
}
} else {
log.Println("Unfollowing:", id)
if err := tc.Unfollow(id); err != nil {
accountBridge.SetError(err.Error())
log.Println("Error unfollowing user:", err)
return
}
}
profileBridge.SetFollowing(follow)
}
// loadConversation loads a message thread
func loadConversation(id string) {
log.Println("Loading conversation:", id)
messages, err := tc.LoadConversation(id)
if err != nil {
log.Println("Error loading conversation:", err)
return
}
debugln("Found conversation posts:", len(messages))
conversationModel.Clear()
for _, m := range messages {
p := messageFromEvent(m)
conversationModel.AppendMessage(p)
}
}
// loadAccount loads an entire profile
func loadAccount(id string) {
debugln("Loading account:", id)
profile, messages, err := tc.LoadAccount(id)
if err != nil {
log.Println("Error loading account:", err)
return
}
profileBridge.SetUsername(profile.Username)
profileBridge.SetName(profile.Name)
profileBridge.SetAvatar(profile.Avatar)
profileBridge.SetProfileURL(profile.ProfileURL)
profileBridge.SetProfileID(profile.ProfileID)
profileBridge.SetPosts(profile.Posts)
profileBridge.SetFollowCount(profile.FollowCount)
profileBridge.SetFollowerCount(profile.FollowerCount)
profileBridge.SetFollowing(profile.Following)
profileBridge.SetFollowedBy(profile.FollowedBy)
debugln("Found account posts:", len(messages))
accountMessagesModel.Clear()
for _, m := range messages {
p := messageFromEvent(m)
accountMessagesModel.AppendMessage(p)
}
}
// search
func search(token string) {
model := NewMessageModel(nil)
evchan := make(chan interface{})
go handleEvents(evchan, model)
log.Println("Search:", token)
if err := tc.Search(token, evchan); err != nil {
accountBridge.SetError(err.Error())
log.Println("Error retrieving search:", err)
return
}
var pane = NewPane(nil)
pane.ID = "search_" + token
pane.Name = "Search: " + token
pane.Model = model
paneModel.AddPane(pane)
}
// tag
func tag(token string) {
model := NewMessageModel(nil)
evchan := make(chan interface{})
go handleEvents(evchan, model)
log.Println("Hashtag:", token)
if err := tc.Tag(token, evchan); err != nil {
accountBridge.SetError(err.Error())
log.Println("Error retrieving hashtag:", err)
return
}
var pane = NewPane(nil)
pane.ID = "tag_" + token
pane.Name = "Tag: #" + token
pane.Model = model
paneModel.AddPane(pane)
}