Seed feeds with Mastodon posts on startup

This commit is contained in:
Christian Muehlhaeuser 2019-05-05 14:43:20 +02:00
parent 2abf333c19
commit 0c927a4d0e
No known key found for this signature in database
GPG key ID: 3CF9FA45CA1EBB7E

View file

@ -87,10 +87,33 @@ func (mod *Account) Run(eventChan chan interface{}) {
Name: mod.self.DisplayName, Name: mod.self.DisplayName,
Avatar: mod.self.Avatar, Avatar: mod.self.Avatar,
ProfileURL: mod.self.URL, ProfileURL: mod.self.URL,
Posts: mod.self.StatusesCount,
Follows: mod.self.FollowingCount,
Followers: mod.self.FollowersCount,
} }
mod.evchan <- ev mod.evchan <- ev
// FIXME: retrieve initial feed // seed feeds initially
nn, err := mod.client.GetNotifications(context.Background(), &mastodon.Pagination{
Limit: initialNotificationsCount,
})
if err != nil {
panic(err)
}
for _, n := range nn {
mod.handleNotification(n)
}
tt, err := mod.client.GetTimelineHome(context.Background(), &mastodon.Pagination{
Limit: initialNotificationsCount,
})
if err != nil {
panic(err)
}
for _, t := range tt {
mod.handleStatus(t)
}
mod.handleStream() mod.handleStream()
} }
@ -168,106 +191,114 @@ func parseBody(body string) string {
*/ */
} }
func (mod *Account) handleStreamEvent(item interface{}) { func (mod *Account) handleNotification(n *mastodon.Notification) {
spw := &spew.ConfigState{Indent: " ", DisableCapacities: true, DisablePointerAddresses: true}
log.Println("Message received:", spw.Sdump(item))
switch status := item.(type) {
case *mastodon.NotificationEvent:
var ev accounts.MessageEvent var ev accounts.MessageEvent
if status.Notification.Status != nil { if n.Status != nil {
ev = accounts.MessageEvent{ ev = accounts.MessageEvent{
Account: "mastodon", Account: "mastodon",
Name: "post", Name: "post",
Notification: true, Notification: true,
Post: accounts.Post{ Post: accounts.Post{
MessageID: string(status.Notification.Status.ID), MessageID: string(n.Status.ID),
Body: status.Notification.Status.Content, Body: parseBody(n.Status.Content),
Author: status.Notification.Account.Username, Author: n.Account.Username,
AuthorName: status.Notification.Account.DisplayName, AuthorName: n.Account.DisplayName,
AuthorURL: status.Notification.Account.URL, AuthorURL: n.Account.URL,
Avatar: status.Notification.Account.Avatar, Avatar: n.Account.Avatar,
CreatedAt: time.Now(), CreatedAt: n.CreatedAt,
URL: status.Notification.Status.URL, URL: n.Status.URL,
}, },
} }
for _, v := range status.Notification.Status.MediaAttachments { for _, v := range n.Status.MediaAttachments {
ev.Media = append(ev.Media, v.PreviewURL) ev.Media = append(ev.Media, v.PreviewURL)
} }
} }
switch status.Notification.Type { switch n.Type {
case "mention": case "mention":
if status.Notification.Status.InReplyToID != nil { if n.Status.InReplyToID != nil {
ev.Mention = true ev.Mention = true
ev.Post.ReplyToAuthor = status.Notification.Status.InReplyToAccountID.(string) ev.Post.ReplyToAuthor = n.Status.InReplyToAccountID.(string)
ev.Post.ReplyToID = status.Notification.Status.InReplyToID.(string) ev.Post.ReplyToID = n.Status.InReplyToID.(string)
} }
case "reblog": case "reblog":
ev.Forward = true ev.Forward = true
ev.Post.Author = status.Notification.Status.Account.Username ev.Post.Author = n.Status.Account.Username
ev.Post.AuthorName = status.Notification.Status.Account.DisplayName ev.Post.AuthorName = n.Status.Account.DisplayName
ev.Post.AuthorURL = status.Notification.Status.Account.URL ev.Post.AuthorURL = n.Status.Account.URL
// ev.Post.Avatar = status.Notification.Status.Account.Avatar // ev.Post.Avatar = n.Status.Account.Avatar
ev.Post.Actor = status.Notification.Account.Username ev.Post.Actor = n.Account.Username
ev.Post.ActorName = status.Notification.Account.DisplayName ev.Post.ActorName = n.Account.DisplayName
case "favourite": case "favourite":
ev.Like = true ev.Like = true
ev.Post.Author = status.Notification.Status.Account.Username ev.Post.Author = n.Status.Account.Username
ev.Post.AuthorName = status.Notification.Status.Account.DisplayName ev.Post.AuthorName = n.Status.Account.DisplayName
ev.Post.AuthorURL = status.Notification.Status.Account.URL ev.Post.AuthorURL = n.Status.Account.URL
// ev.Post.Avatar = status.Notification.Status.Account.Avatar // ev.Post.Avatar = n.Status.Account.Avatar
ev.Post.Actor = status.Notification.Account.Username ev.Post.Actor = n.Account.Username
ev.Post.ActorName = status.Notification.Account.DisplayName ev.Post.ActorName = n.Account.DisplayName
default: default:
fmt.Println("Unknown type:", status.Notification.Type) fmt.Println("Unknown type:", n.Type)
return return
} }
mod.evchan <- ev mod.evchan <- ev
}
case *mastodon.UpdateEvent: func (mod *Account) handleStatus(s *mastodon.Status) {
ev := accounts.MessageEvent{ ev := accounts.MessageEvent{
Account: "mastodon", Account: "mastodon",
Name: "post", Name: "post",
Post: accounts.Post{ Post: accounts.Post{
MessageID: string(status.Status.ID), MessageID: string(s.ID),
Body: status.Status.Content, Body: parseBody(s.Content),
Author: status.Status.Account.Acct, Author: s.Account.Acct,
AuthorName: status.Status.Account.DisplayName, AuthorName: s.Account.DisplayName,
AuthorURL: status.Status.Account.URL, AuthorURL: s.Account.URL,
Avatar: status.Status.Account.Avatar, Avatar: s.Account.Avatar,
CreatedAt: time.Now(), CreatedAt: s.CreatedAt,
URL: status.Status.URL, URL: s.URL,
}, },
} }
for _, v := range status.Status.MediaAttachments { for _, v := range s.MediaAttachments {
ev.Media = append(ev.Media, v.PreviewURL) ev.Media = append(ev.Media, v.PreviewURL)
} }
if status.Status.Reblog != nil { if s.Reblog != nil {
ev.Forward = true ev.Forward = true
for _, v := range status.Status.Reblog.MediaAttachments { for _, v := range s.Reblog.MediaAttachments {
ev.Media = append(ev.Media, v.PreviewURL) ev.Media = append(ev.Media, v.PreviewURL)
} }
ev.Post.URL = status.Status.Reblog.URL ev.Post.URL = s.Reblog.URL
ev.Post.Author = status.Status.Reblog.Account.Username ev.Post.Author = s.Reblog.Account.Username
ev.Post.AuthorName = status.Status.Reblog.Account.DisplayName ev.Post.AuthorName = s.Reblog.Account.DisplayName
ev.Post.AuthorURL = status.Status.Reblog.Account.URL ev.Post.AuthorURL = s.Reblog.Account.URL
ev.Post.Actor = status.Status.Account.DisplayName ev.Post.Actor = s.Account.DisplayName
ev.Post.ActorName = status.Status.Account.Username ev.Post.ActorName = s.Account.Username
} }
mod.evchan <- ev mod.evchan <- ev
}
func (mod *Account) handleStreamEvent(item interface{}) {
spw := &spew.ConfigState{Indent: " ", DisableCapacities: true, DisablePointerAddresses: true}
log.Println("Message received:", spw.Sdump(item))
switch e := item.(type) {
case *mastodon.NotificationEvent:
mod.handleNotification(e.Notification)
case *mastodon.UpdateEvent:
mod.handleStatus(e.Status)
} }
} }