You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

311 lines
8.7 KiB

package api
import (
"context"
"strings"
"github.com/RasmusLindroth/go-mastodon"
"github.com/RasmusLindroth/tut/config"
)
type TimelineType uint
func (ac *AccountClient) getStatusSimilar(statuses []*mastodon.Status, err error) ([]Item, error) {
var items []Item
if err != nil {
return items, err
}
for _, s := range statuses {
item := NewStatusItem(s, false)
items = append(items, item)
}
return items, nil
}
func (ac *AccountClient) getUserSimilar(users []*mastodon.Account, err error, data interface{}) ([]Item, error) {
var items []Item
if err != nil {
return items, err
}
ids := []string{}
for _, u := range users {
ids = append(ids, string(u.ID))
}
rel, err := ac.Client.GetAccountRelationships(context.Background(), ids)
if err != nil {
return items, err
}
for _, u := range users {
for _, r := range rel {
if u.ID == r.ID {
items = append(items, NewUserItem(&User{
Data: u,
Relation: r,
AdditionalData: data,
}, false))
break
}
}
}
return items, nil
}
func (ac *AccountClient) GetTimeline(pg *mastodon.Pagination) ([]Item, error) {
return ac.getStatusSimilar(ac.Client.GetTimelineHome(context.Background(), pg))
}
func (ac *AccountClient) GetTimelineFederated(pg *mastodon.Pagination) ([]Item, error) {
return ac.getStatusSimilar(ac.Client.GetTimelinePublic(context.Background(), false, pg))
}
func (ac *AccountClient) GetTimelineLocal(pg *mastodon.Pagination) ([]Item, error) {
return ac.getStatusSimilar(ac.Client.GetTimelinePublic(context.Background(), true, pg))
}
func (ac *AccountClient) GetNotifications(nth []config.NotificationToHide, pg *mastodon.Pagination) ([]Item, error) {
var items []Item
toHide := []string{}
for _, n := range nth {
toHide = append(toHide, string(n))
}
notifications, err := ac.Client.GetNotificationsExclude(context.Background(), &toHide, pg)
if err != nil {
return items, err
}
ids := []string{}
for _, n := range notifications {
ids = append(ids, string(n.Account.ID))
}
rel, err := ac.Client.GetAccountRelationships(context.Background(), ids)
if err != nil {
return items, err
}
for _, n := range notifications {
for _, r := range rel {
if n.Account.ID == r.ID {
item := NewNotificationItem(n, &User{
Data: &n.Account, Relation: r,
})
items = append(items, item)
break
}
}
}
return items, nil
}
func (ac *AccountClient) GetHistory(status *mastodon.Status) ([]Item, error) {
var items []Item
statuses, err := ac.Client.GetStatusHistory(context.Background(), status.ID)
if err != nil {
return items, err
}
for _, s := range statuses {
items = append(items, NewStatusHistoryItem(s))
}
return items, nil
}
func (ac *AccountClient) GetThread(status *mastodon.Status) ([]Item, error) {
var items []Item
statuses, err := ac.Client.GetStatusContext(context.Background(), status.ID)
if err != nil {
return items, err
}
for _, s := range statuses.Ancestors {
items = append(items, NewStatusItem(s, false))
}
items = append(items, NewStatusItem(status, false))
for _, s := range statuses.Descendants {
items = append(items, NewStatusItem(s, false))
}
return items, nil
}
func (ac *AccountClient) GetFavorites(pg *mastodon.Pagination) ([]Item, error) {
return ac.getStatusSimilar(ac.Client.GetFavourites(context.Background(), pg))
}
func (ac *AccountClient) GetBookmarks(pg *mastodon.Pagination) ([]Item, error) {
return ac.getStatusSimilar(ac.Client.GetBookmarks(context.Background(), pg))
}
func (ac *AccountClient) GetConversations(pg *mastodon.Pagination) ([]Item, error) {
var items []Item
conversations, err := ac.Client.GetConversations(context.Background(), pg)
if err != nil {
return items, err
}
for _, c := range conversations {
if c.LastStatus == nil {
continue
}
item := NewStatusItem(c.LastStatus, false)
items = append(items, item)
}
return items, nil
}
func (ac *AccountClient) GetUsers(search string) ([]Item, error) {
var items []Item
var users []*mastodon.Account
var err error
if strings.HasPrefix(search, "@") && len(strings.Split(search, "@")) == 3 {
users, err = ac.Client.AccountsSearchResolve(context.Background(), search, 10, true)
}
if len(users) == 0 || err != nil {
users, err = ac.Client.AccountsSearchResolve(context.Background(), search, 10, false)
}
if err != nil {
return items, err
}
ids := []string{}
for _, u := range users {
ids = append(ids, string(u.ID))
}
rel, err := ac.Client.GetAccountRelationships(context.Background(), ids)
if err != nil {
return items, err
}
for _, u := range users {
for _, r := range rel {
if u.ID == r.ID {
items = append(items, NewUserItem(&User{
Data: u,
Relation: r,
}, false))
break
}
}
}
return items, nil
}
func (ac *AccountClient) GetBoostsStatus(pg *mastodon.Pagination, id mastodon.ID) ([]Item, error) {
users, err := ac.Client.GetRebloggedBy(context.Background(), id, pg)
return ac.getUserSimilar(users, err, nil)
}
func (ac *AccountClient) GetFavoritesStatus(pg *mastodon.Pagination, id mastodon.ID) ([]Item, error) {
users, err := ac.Client.GetFavouritedBy(context.Background(), id, pg)
return ac.getUserSimilar(users, err, nil)
}
func (ac *AccountClient) GetFollowers(pg *mastodon.Pagination, id mastodon.ID) ([]Item, error) {
users, err := ac.Client.GetAccountFollowers(context.Background(), id, pg)
return ac.getUserSimilar(users, err, nil)
}
func (ac *AccountClient) GetFollowing(pg *mastodon.Pagination, id mastodon.ID) ([]Item, error) {
users, err := ac.Client.GetAccountFollowing(context.Background(), id, pg)
return ac.getUserSimilar(users, err, nil)
}
func (ac *AccountClient) GetBlocking(pg *mastodon.Pagination) ([]Item, error) {
users, err := ac.Client.GetBlocks(context.Background(), pg)
return ac.getUserSimilar(users, err, nil)
}
func (ac *AccountClient) GetMuting(pg *mastodon.Pagination) ([]Item, error) {
users, err := ac.Client.GetMutes(context.Background(), pg)
return ac.getUserSimilar(users, err, nil)
}
func (ac *AccountClient) GetFollowRequests(pg *mastodon.Pagination) ([]Item, error) {
users, err := ac.Client.GetFollowRequests(context.Background(), pg)
return ac.getUserSimilar(users, err, nil)
}
func (ac *AccountClient) GetUser(pg *mastodon.Pagination, id mastodon.ID) ([]Item, error) {
var items []Item
statuses, err := ac.Client.GetAccountStatuses(context.Background(), id, pg)
if err != nil {
return items, err
}
for _, s := range statuses {
item := NewStatusItem(s, false)
items = append(items, item)
}
return items, nil
}
func (ac *AccountClient) GetUserPinned(id mastodon.ID) ([]Item, error) {
var items []Item
statuses, err := ac.Client.GetAccountPinnedStatuses(context.Background(), id)
if err != nil {
return items, err
}
for _, s := range statuses {
item := NewStatusItem(s, true)
items = append(items, item)
}
return items, nil
}
func (ac *AccountClient) GetTags(pg *mastodon.Pagination) ([]Item, error) {
var items []Item
tags, err := ac.Client.TagsFollowed(context.Background(), pg)
if err != nil {
return items, err
}
for _, t := range tags {
items = append(items, NewTagItem(t))
}
return items, nil
}
func (ac *AccountClient) GetLists() ([]Item, error) {
var items []Item
lists, err := ac.Client.GetLists(context.Background())
if err != nil {
return items, err
}
for _, l := range lists {
items = append(items, NewListsItem(l))
}
return items, nil
}
func (ac *AccountClient) GetListStatuses(pg *mastodon.Pagination, id mastodon.ID) ([]Item, error) {
var items []Item
statuses, err := ac.Client.GetTimelineList(context.Background(), id, pg)
if err != nil {
return items, err
}
for _, s := range statuses {
item := NewStatusItem(s, false)
items = append(items, item)
}
return items, nil
}
func (ac *AccountClient) GetFollowingForList(pg *mastodon.Pagination, id mastodon.ID, data interface{}) ([]Item, error) {
users, err := ac.Client.GetAccountFollowing(context.Background(), id, pg)
return ac.getUserSimilar(users, err, data)
}
func (ac *AccountClient) GetListUsers(pg *mastodon.Pagination, id mastodon.ID, data interface{}) ([]Item, error) {
users, err := ac.Client.GetListAccounts(context.Background(), id)
return ac.getUserSimilar(users, err, data)
}
func (ac *AccountClient) GetTag(pg *mastodon.Pagination, search string) ([]Item, error) {
return ac.getStatusSimilar(ac.Client.GetTimelineHashtag(context.Background(), search, false, pg))
}
func (ac *AccountClient) GetTagMultiple(pg *mastodon.Pagination, search string) ([]Item, error) {
var s string
td := mastodon.TagData{}
parts := strings.Split(search, " ")
for i, p := range parts {
if i == 0 {
s = p
continue
}
if len(p) > 0 {
td.Any = append(td.Any, p)
}
}
items, err := ac.Client.GetTimelineHashtagMultiple(context.Background(), s, false, &td, pg)
return ac.getStatusSimilar(items, err)
}