Skip to content

Commit

Permalink
1.0.6 (#143)
Browse files Browse the repository at this point in the history
* fix streams not closing

* prepare for filters

* support for filters

* wrap words

* support for tag leader

* update config

* bump version
  • Loading branch information
RasmusLindroth authored May 14, 2022
1 parent 80ede1e commit f43fec0
Show file tree
Hide file tree
Showing 13 changed files with 316 additions and 133 deletions.
70 changes: 54 additions & 16 deletions api/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ func (ac *AccountClient) GetTimeline(pg *mastodon.Pagination) ([]Item, error) {
return items, err
}
for _, s := range statuses {
items = append(items, NewStatusItem(s))
item, filtered := NewStatusItem(s, ac.Filters, "home")
if !filtered {
items = append(items, item)
}
}
return items, nil
}
Expand All @@ -27,7 +30,10 @@ func (ac *AccountClient) GetTimelineFederated(pg *mastodon.Pagination) ([]Item,
return items, err
}
for _, s := range statuses {
items = append(items, NewStatusItem(s))
item, filtered := NewStatusItem(s, ac.Filters, "public")
if !filtered {
items = append(items, item)
}
}
return items, nil
}
Expand All @@ -39,7 +45,10 @@ func (ac *AccountClient) GetTimelineLocal(pg *mastodon.Pagination) ([]Item, erro
return items, err
}
for _, s := range statuses {
items = append(items, NewStatusItem(s))
item, filtered := NewStatusItem(s, ac.Filters, "public")
if !filtered {
items = append(items, item)
}
}
return items, nil
}
Expand All @@ -61,10 +70,12 @@ func (ac *AccountClient) GetNotifications(pg *mastodon.Pagination) ([]Item, erro
for _, n := range notifications {
for _, r := range rel {
if n.Account.ID == r.ID {
items = append(items, NewNotificationItem(n, &User{
Data: &n.Account,
Relation: r,
}))
item, filtered := NewNotificationItem(n, &User{
Data: &n.Account, Relation: r,
}, ac.Filters)
if !filtered {
items = append(items, item)
}
break
}
}
Expand All @@ -79,11 +90,20 @@ func (ac *AccountClient) GetThread(status *mastodon.Status) ([]Item, int, error)
return items, 0, err
}
for _, s := range statuses.Ancestors {
items = append(items, NewStatusItem(s))
item, filtered := NewStatusItem(s, ac.Filters, "thread")
if !filtered {
items = append(items, item)
}
}
item, filtered := NewStatusItem(status, ac.Filters, "thread")
if !filtered {
items = append(items, item)
}
items = append(items, NewStatusItem(status))
for _, s := range statuses.Descendants {
items = append(items, NewStatusItem(s))
item, filtered := NewStatusItem(s, ac.Filters, "thread")
if !filtered {
items = append(items, item)
}
}
return items, len(statuses.Ancestors), nil
}
Expand All @@ -95,7 +115,10 @@ func (ac *AccountClient) GetFavorites(pg *mastodon.Pagination) ([]Item, error) {
return items, err
}
for _, s := range statuses {
items = append(items, NewStatusItem(s))
item, filtered := NewStatusItem(s, ac.Filters, "home")
if !filtered {
items = append(items, item)
}
}
return items, nil
}
Expand All @@ -107,7 +130,10 @@ func (ac *AccountClient) GetBookmarks(pg *mastodon.Pagination) ([]Item, error) {
return items, err
}
for _, s := range statuses {
items = append(items, NewStatusItem(s))
item, filtered := NewStatusItem(s, ac.Filters, "home")
if !filtered {
items = append(items, item)
}
}
return items, nil
}
Expand All @@ -119,7 +145,10 @@ func (ac *AccountClient) GetConversations(pg *mastodon.Pagination) ([]Item, erro
return items, err
}
for _, c := range conversations {
items = append(items, NewStatusItem(c.LastStatus))
item, filtered := NewStatusItem(c.LastStatus, ac.Filters, "thread")
if !filtered {
items = append(items, item)
}
}
return items, nil
}
Expand Down Expand Up @@ -229,7 +258,10 @@ func (ac *AccountClient) GetUser(pg *mastodon.Pagination, id mastodon.ID) ([]Ite
return items, err
}
for _, s := range statuses {
items = append(items, NewStatusItem(s))
item, filtered := NewStatusItem(s, ac.Filters, "account")
if !filtered {
items = append(items, item)
}
}
return items, nil
}
Expand All @@ -253,7 +285,10 @@ func (ac *AccountClient) GetListStatuses(pg *mastodon.Pagination, id mastodon.ID
return items, err
}
for _, s := range statuses {
items = append(items, NewStatusItem(s))
item, filtered := NewStatusItem(s, ac.Filters, "home")
if !filtered {
items = append(items, item)
}
}
return items, nil
}
Expand All @@ -265,7 +300,10 @@ func (ac *AccountClient) GetTag(pg *mastodon.Pagination, search string) ([]Item,
return items, err
}
for _, s := range statuses {
items = append(items, NewStatusItem(s))
item, filtered := NewStatusItem(s, ac.Filters, "public")
if !filtered {
items = append(items, item)
}
}
return items, nil
}
70 changes: 64 additions & 6 deletions api/item.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package api

import (
"strings"
"sync"
"unicode"

"github.com/RasmusLindroth/go-mastodon"
"github.com/RasmusLindroth/tut/util"
Expand All @@ -26,8 +28,63 @@ type Item interface {
URLs() ([]util.URL, []mastodon.Mention, []mastodon.Tag, int)
}

func NewStatusItem(item *mastodon.Status) Item {
return &StatusItem{id: newID(), item: item, showSpoiler: false}
func NewStatusItem(item *mastodon.Status, filters []*mastodon.Filter, timeline string) (sitem Item, filtered bool) {
filtered = false
if item == nil {
return &StatusItem{id: newID(), item: item, showSpoiler: false}, false
}
s := util.StatusOrReblog(item)
content := s.Content
if s.Sensitive {
content += "\n" + s.SpoilerText
}
content = strings.ToLower(content)
for _, f := range filters {
apply := false
for _, c := range f.Context {
if timeline == c {
apply = true
break
}
}
if !apply {
continue
}
if f.WholeWord {
lines := strings.Split(content, "\n")
var stripped []string
for _, l := range lines {
var words []string
words = append(words, strings.Split(l, " ")...)
for _, w := range words {
ns := strings.TrimSpace(w)
ns = strings.TrimFunc(ns, func(r rune) bool {
return !unicode.IsLetter(r) && !unicode.IsNumber(r)
})
stripped = append(stripped, ns)
}
}
filter := strings.Split(strings.ToLower(f.Phrase), " ")
for i := 0; i+len(filter)-1 < len(stripped); i++ {
if strings.ToLower(f.Phrase) == strings.Join(stripped[i:i+len(filter)], " ") {
filtered = true
break
}
}
} else {
if strings.Contains(s.Content, strings.ToLower(f.Phrase)) {
filtered = true
}
if strings.Contains(s.SpoilerText, strings.ToLower(f.Phrase)) {
filtered = true
}
}
if filtered {
break
}
}
sitem = &StatusItem{id: newID(), item: item, showSpoiler: false}
return sitem, filtered
}

type StatusItem struct {
Expand Down Expand Up @@ -128,16 +185,17 @@ func (u *UserItem) URLs() ([]util.URL, []mastodon.Mention, []mastodon.Tag, int)
return urls, []mastodon.Mention{}, []mastodon.Tag{}, len(urls)
}

func NewNotificationItem(item *mastodon.Notification, user *User) Item {
n := &NotificationItem{
func NewNotificationItem(item *mastodon.Notification, user *User, filters []*mastodon.Filter) (nitem Item, filtred bool) {
status, filtred := NewStatusItem(item.Status, filters, "notifications")
nitem = &NotificationItem{
id: newID(),
item: item,
showSpoiler: false,
user: NewUserItem(user, false),
status: NewStatusItem(item.Status),
status: status,
}

return n
return nitem, filtred
}

type NotificationItem struct {
Expand Down
34 changes: 22 additions & 12 deletions api/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,28 +153,34 @@ func (ac *AccountClient) NewGenericStream(st StreamType, data string) (rec *Rece
return rec, nil
}

func (ac *AccountClient) NewHomeStream() (*Receiver, error) {
return ac.NewGenericStream(HomeStream, "")
func (ac *AccountClient) NewHomeStream() (*Receiver, string, error) {
rec, err := ac.NewGenericStream(HomeStream, "")
return rec, "home", err
}

func (ac *AccountClient) NewLocalStream() (*Receiver, error) {
return ac.NewGenericStream(LocalStream, "")
func (ac *AccountClient) NewLocalStream() (*Receiver, string, error) {
rec, err := ac.NewGenericStream(LocalStream, "")
return rec, "public", err
}

func (ac *AccountClient) NewFederatedStream() (*Receiver, error) {
return ac.NewGenericStream(FederatedStream, "")
func (ac *AccountClient) NewFederatedStream() (*Receiver, string, error) {
rec, err := ac.NewGenericStream(FederatedStream, "")
return rec, "public", err
}

func (ac *AccountClient) NewDirectStream() (*Receiver, error) {
return ac.NewGenericStream(DirectStream, "")
func (ac *AccountClient) NewDirectStream() (*Receiver, string, error) {
rec, err := ac.NewGenericStream(DirectStream, "")
return rec, "public", err
}

func (ac *AccountClient) NewListStream(id mastodon.ID) (*Receiver, error) {
return ac.NewGenericStream(ListStream, string(id))
func (ac *AccountClient) NewListStream(id mastodon.ID) (*Receiver, string, error) {
rec, err := ac.NewGenericStream(ListStream, string(id))
return rec, "home", err
}

func (ac *AccountClient) NewTagStream(tag string) (*Receiver, error) {
return ac.NewGenericStream(TagStream, tag)
func (ac *AccountClient) NewTagStream(tag string) (*Receiver, string, error) {
rec, err := ac.NewGenericStream(TagStream, tag)
return rec, "public", err
}

func (ac *AccountClient) RemoveGenericReceiver(rec *Receiver, st StreamType, data string) {
Expand Down Expand Up @@ -214,6 +220,10 @@ func (ac *AccountClient) RemoveLocalReceiver(rec *Receiver) {
ac.RemoveGenericReceiver(rec, LocalStream, "")
}

func (ac *AccountClient) RemoveConversationReceiver(rec *Receiver) {
ac.RemoveGenericReceiver(rec, DirectStream, "")
}

func (ac *AccountClient) RemoveFederatedReceiver(rec *Receiver) {
ac.RemoveGenericReceiver(rec, FederatedStream, "")
}
Expand Down
1 change: 1 addition & 0 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type AccountClient struct {
Client *mastodon.Client
Streams map[string]*Stream
Me *mastodon.Account
Filters []*mastodon.Filter
}

type User struct {
Expand Down
6 changes: 4 additions & 2 deletions config.example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,19 @@ leader-timeout=1000
#
# Available commands: home, direct, local, federated, compose, blocking,
# bookmarks, saved, favorited, boosts, favorites, following, followers, muting,
# profile, notifications, lists
# profile, notifications, lists, tag
#
# The shortcuts are up to you, but keep them quite short and make sure they
# don't collide. If you have one shortcut that is "f" and an other one that is
# "fav", the one with "f" will always run and "fav" will never run.
# "fav", the one with "f" will always run and "fav" will never run. Tag is
# special as you need to add the tag after, see the example below.
#
# Some examples:
# leader-action=local,lo
# leader-action=lists,li
# leader-action=federated,fed
# leader-action=direct,d
# leader-action=tag linux,tl
#


Expand Down
19 changes: 16 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ type Config struct {
}

type LeaderAction struct {
Command LeaderCommand
Shortcut string
Command LeaderCommand
Subaction string
Shortcut string
}

type LeaderCommand uint
Expand All @@ -67,6 +68,8 @@ const (
LeaderProfile
LeaderNotifications
LeaderLists
LeaderTag
LeaderUser
)

type General struct {
Expand Down Expand Up @@ -600,8 +603,15 @@ func parseGeneral(cfg *ini.File) General {
for i, p := range parts {
parts[i] = strings.TrimSpace(p)
}
cmd := parts[0]
var subaction string
if strings.Contains(parts[0], " ") {
p := strings.Split(cmd, " ")
cmd = p[0]
subaction = strings.Join(p[1:], " ")
}
la := LeaderAction{}
switch parts[0] {
switch cmd {
case "home":
la.Command = LeaderHome
case "direct":
Expand Down Expand Up @@ -636,6 +646,9 @@ func parseGeneral(cfg *ini.File) General {
la.Command = LeaderNotifications
case "lists":
la.Command = LeaderLists
case "tag":
la.Command = LeaderTag
la.Subaction = subaction
default:
fmt.Printf("leader-action %s is invalid\n", parts[0])
os.Exit(1)
Expand Down
Loading

0 comments on commit f43fec0

Please sign in to comment.