Skip to content

Commit

Permalink
Fix notification (#61)
Browse files Browse the repository at this point in the history
* keep cw on replies

* Don't redraw toot on new items

* show users in notifications

* update version
  • Loading branch information
RasmusLindroth committed Aug 26, 2021
1 parent fc39e4b commit 30fa45a
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 51 deletions.
46 changes: 35 additions & 11 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,24 +161,44 @@ func (api *API) GetUserStatusesNewer(u mastodon.Account, s *mastodon.Status) ([]
return api.Client.GetAccountStatuses(context.Background(), u.ID, pg)
}

func (api *API) GetNotifications() ([]*mastodon.Notification, error) {
return api.Client.GetNotifications(context.Background(), nil)
}
func (api *API) getNotifications(pg *mastodon.Pagination) ([]*Notification, error) {
var notifications []*Notification

func (api *API) GetNotificationsOlder(n *mastodon.Notification) ([]*mastodon.Notification, error) {
pg := &mastodon.Pagination{
MaxID: n.ID,
mnot, err := api.Client.GetNotifications(context.Background(), pg)
if err != nil {
return []*Notification{}, err
}

return api.Client.GetNotifications(context.Background(), pg)
for _, np := range mnot {
var r *mastodon.Relationship
if np.Type == "follow" {
r, err = api.GetRelation(&np.Account)
if err != nil {
return notifications, err
}
}
notifications = append(notifications, &Notification{N: np, R: r})
}

return notifications, err
}

func (api *API) GetNotifications() ([]*Notification, error) {
return api.getNotifications(nil)
}

func (api *API) GetNotificationsNewer(n *mastodon.Notification) ([]*mastodon.Notification, error) {
func (api *API) GetNotificationsOlder(n *Notification) ([]*Notification, error) {
pg := &mastodon.Pagination{
MinID: n.ID,
MaxID: n.N.ID,
}
return api.getNotifications(pg)
}

return api.Client.GetNotifications(context.Background(), pg)
func (api *API) GetNotificationsNewer(n *Notification) ([]*Notification, error) {
pg := &mastodon.Pagination{
MinID: n.N.ID,
}
return api.getNotifications(pg)
}

type UserData struct {
Expand All @@ -193,7 +213,7 @@ func (api *API) GetUsers(s string) ([]*UserData, error) {
return nil, err
}
for _, u := range users {
r, err := api.UserRelation(*u)
r, err := api.GetRelation(u)
if err != nil {
return ud, err
}
Expand All @@ -203,6 +223,10 @@ func (api *API) GetUsers(s string) ([]*UserData, error) {
return ud, nil
}

func (api *API) GetRelation(u *mastodon.Account) (*mastodon.Relationship, error) {
return api.UserRelation(*u)
}

func (api *API) getUserList(t UserListType, id string, pg *mastodon.Pagination) ([]*UserData, error) {

var ud []*UserData
Expand Down
94 changes: 58 additions & 36 deletions feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ func inputSimple(app *App, event *tcell.EventKey, controls ControlItem,
}
updated = true
redrawToot = true
redrawControls = true
}
case 'd', 'D':
if controls&ControlDelete != 0 {
Expand Down Expand Up @@ -390,6 +391,7 @@ func inputSimple(app *App, event *tcell.EventKey, controls ControlItem,
}
updated = true
redrawToot = true
redrawControls = true
}
case 'c', 'C':
if controls&ControlCompose != 0 {
Expand All @@ -411,6 +413,7 @@ func inputSimple(app *App, event *tcell.EventKey, controls ControlItem,
}
updated = true
redrawToot = true
redrawControls = true
}
case 'o', 'O':
if controls&ControlOpen != 0 {
Expand Down Expand Up @@ -963,10 +966,15 @@ func NewNotificationFeed(app *App, docked bool) *NotificationsFeed {
return n
}

type Notification struct {
N *mastodon.Notification
R *mastodon.Relationship
}

type NotificationsFeed struct {
app *App
timelineType TimelineType
notifications []*mastodon.Notification
notifications []*Notification
docked bool
index int
showSpoiler bool
Expand All @@ -980,7 +988,7 @@ func (n *NotificationsFeed) GetDesc() string {
return "Notifications"
}

func (n *NotificationsFeed) GetCurrentNotification() *mastodon.Notification {
func (n *NotificationsFeed) GetCurrentNotification() *Notification {
var index int
if n.docked {
index = n.app.UI.StatusView.notificationView.list.GetCurrentItem()
Expand All @@ -995,18 +1003,18 @@ func (n *NotificationsFeed) GetCurrentNotification() *mastodon.Notification {

func (n *NotificationsFeed) GetCurrentStatus() *mastodon.Status {
notification := n.GetCurrentNotification()
if notification == nil {
if notification.N == nil {
return nil
}
return notification.Status
return notification.N.Status
}

func (n *NotificationsFeed) GetCurrentUser() *mastodon.Account {
notification := n.GetCurrentNotification()
if notification == nil {
if notification.N == nil {
return nil
}
return &notification.Account
return &notification.N.Account
}

func (n *NotificationsFeed) GetFeedList() <-chan string {
Expand All @@ -1015,14 +1023,14 @@ func (n *NotificationsFeed) GetFeedList() <-chan string {
go func() {
today := time.Now()
for _, item := range notifications {
sLocal := item.CreatedAt.Local()
sLocal := item.N.CreatedAt.Local()
long := n.app.Config.General.DateFormat
short := n.app.Config.General.DateTodayFormat
relative := n.app.Config.General.DateRelative

dateOutput := OutputDate(sLocal, today, long, short, relative)

content := fmt.Sprintf("%s %s", dateOutput, item.Account.Acct)
content := fmt.Sprintf("%s %s", dateOutput, item.N.Account.Acct)
ch <- content
}
close(ch)
Expand All @@ -1031,26 +1039,26 @@ func (n *NotificationsFeed) GetFeedList() <-chan string {
}

func (n *NotificationsFeed) LoadNewer() int {
var notifications []*mastodon.Notification
var notifications []*Notification
var err error
if len(n.notifications) == 0 {
notifications, err = n.app.API.GetNotifications()
} else {
notifications, err = n.app.API.GetNotificationsNewer(n.notifications[0])
for _, o := range notifications {
switch o.Type {
switch o.N.Type {
case "follow":
Notify(n.app.Config.NotificationConfig, NotificationFollower,
"New follower", fmt.Sprintf("%s follows you", o.Account.Username))
"New follower", fmt.Sprintf("%s follows you", o.N.Account.Username))
case "favourite":
Notify(n.app.Config.NotificationConfig, NotificationFavorite,
"Favorited your toot", fmt.Sprintf("%s favorited your toot", o.Account.Username))
"Favorited your toot", fmt.Sprintf("%s favorited your toot", o.N.Account.Username))
case "reblog":
Notify(n.app.Config.NotificationConfig, NotificationBoost,
"Boosted your toot", fmt.Sprintf("%s boosted your toot", o.Account.Username))
"Boosted your toot", fmt.Sprintf("%s boosted your toot", o.N.Account.Username))
case "mention":
Notify(n.app.Config.NotificationConfig, NotificationMention,
"Mentioned in toot", fmt.Sprintf("%s mentioned you", o.Account.Username))
"Mentioned in toot", fmt.Sprintf("%s mentioned you", o.N.Account.Username))
case "poll":
Notify(n.app.Config.NotificationConfig, NotificationPoll,
"Poll has ended", "")
Expand All @@ -1071,7 +1079,7 @@ func (n *NotificationsFeed) LoadNewer() int {
}

func (n *NotificationsFeed) LoadOlder() int {
var notifications []*mastodon.Notification
var notifications []*Notification
var err error
if len(n.notifications) == 0 {
notifications, err = n.app.API.GetNotifications()
Expand Down Expand Up @@ -1118,28 +1126,30 @@ func (n *NotificationsFeed) DrawToot() {
var controls string
defer func() { n.showSpoiler = false }()

switch notification.Type {
switch notification.N.Type {
case "follow":
text = SublteText(n.app.Config.Style, FormatUsername(notification.Account)+" started following you\n\n")
controls = ColorKey(n.app.Config, "", "U", "ser")
text = SublteText(n.app.Config.Style, FormatUsername(notification.N.Account)+" started following you\n\n")
var t string
t, controls = showUser(n.app, &notification.N.Account, notification.R, true)
text += t
case "favourite":
pre := SublteText(n.app.Config.Style, FormatUsername(notification.Account)+" favorited your toot") + "\n\n"
text, controls = showTootOptions(n.app, notification.Status, n.showSpoiler)
pre := SublteText(n.app.Config.Style, FormatUsername(notification.N.Account)+" favorited your toot") + "\n\n"
text, controls = showTootOptions(n.app, notification.N.Status, n.showSpoiler)
text = pre + text
case "reblog":
pre := SublteText(n.app.Config.Style, FormatUsername(notification.Account)+" boosted your toot") + "\n\n"
text, controls = showTootOptions(n.app, notification.Status, n.showSpoiler)
pre := SublteText(n.app.Config.Style, FormatUsername(notification.N.Account)+" boosted your toot") + "\n\n"
text, controls = showTootOptions(n.app, notification.N.Status, n.showSpoiler)
text = pre + text
case "mention":
pre := SublteText(n.app.Config.Style, FormatUsername(notification.Account)+" mentioned you") + "\n\n"
text, controls = showTootOptions(n.app, notification.Status, n.showSpoiler)
pre := SublteText(n.app.Config.Style, FormatUsername(notification.N.Account)+" mentioned you") + "\n\n"
text, controls = showTootOptions(n.app, notification.N.Status, n.showSpoiler)
text = pre + text
case "poll":
pre := SublteText(n.app.Config.Style, "A poll of yours or one you participated in has ended") + "\n\n"
text, controls = showTootOptions(n.app, notification.Status, n.showSpoiler)
text, controls = showTootOptions(n.app, notification.N.Status, n.showSpoiler)
text = pre + text
case "follow_request":
text = SublteText(n.app.Config.Style, FormatUsername(notification.Account)+" wants to follow you. This is currently not implemented, so use another app to accept or reject the request.\n\n")
text = SublteText(n.app.Config.Style, FormatUsername(notification.N.Account)+" wants to follow you. This is currently not implemented, so use another app to accept or reject the request.\n\n")
default:
}

Expand All @@ -1153,9 +1163,12 @@ func (n *NotificationsFeed) RedrawControls() {
n.app.UI.StatusView.SetControls("")
return
}
switch notification.Type {
switch notification.N.Type {
case "favourite", "reblog", "mention", "poll":
_, controls := showTootOptions(n.app, notification.Status, n.showSpoiler)
_, controls := showTootOptions(n.app, notification.N.Status, n.showSpoiler)
n.app.UI.StatusView.SetControls(controls)
case "follow":
_, controls := showUser(n.app, &notification.N.Account, notification.R, true)
n.app.UI.StatusView.SetControls(controls)
}
}
Expand All @@ -1169,22 +1182,31 @@ func (n *NotificationsFeed) Input(event *tcell.EventKey) {
if notification == nil {
return
}
if notification.Type == "follow" {
controls := []ControlItem{ControlUser}
if notification.N.Type == "follow" {
controls := []ControlItem{ControlUser, ControlFollow, ControlBlock, ControlMute, ControlAvatar, ControlOpen}
options := inputOptions(controls)
inputSimple(n.app, event, options, notification.Account, nil, nil, nil, n)
_, rc, _, _, rel := inputSimple(n.app, event, options, notification.N.Account, nil, nil, notification.R, n)
if rc {
var index int
if n.docked {
index = n.app.UI.StatusView.notificationView.list.GetCurrentItem()
} else {
index = n.app.UI.StatusView.GetCurrentItem()
}
n.notifications[index].R = rel
n.RedrawControls()
}
return
}

if notification.Type == "follow_request" {
if notification.N.Type == "follow_request" {
return
}
status := notification.Status
status := notification.N.Status
originalStatus := status
if status.Reblog != nil {
status = status.Reblog
}
user := status.Account

controls := []ControlItem{
ControlAvatar, ControlThread, ControlUser, ControlSpoiler,
Expand All @@ -1193,15 +1215,15 @@ func (n *NotificationsFeed) Input(event *tcell.EventKey) {
}
options := inputOptions(controls)

updated, rc, rt, newS, _ := inputSimple(n.app, event, options, user, status, originalStatus, nil, n)
updated, rc, rt, newS, _ := inputSimple(n.app, event, options, notification.N.Account, status, originalStatus, nil, n)
if updated {
var index int
if n.docked {
index = n.app.UI.StatusView.notificationView.list.GetCurrentItem()
} else {
index = n.app.UI.StatusView.GetCurrentItem()
}
n.notifications[index].Status = newS
n.notifications[index].N.Status = newS
}
if rc {
n.RedrawControls()
Expand Down
12 changes: 10 additions & 2 deletions linkoverlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,11 @@ func (l *LinkOverlay) InputHandler(event *tcell.EventKey) {
i, _ := strconv.Atoi(s)
l.OpenCustom(i)
case 'q', 'Q':
l.app.UI.SetFocus(LeftPaneFocus)
if l.app.UI.StatusView.lastList == NotificationPaneFocus {
l.app.UI.SetFocus(NotificationPaneFocus)
} else {
l.app.UI.SetFocus(LeftPaneFocus)
}
}
} else {
switch event.Key() {
Expand All @@ -187,7 +191,11 @@ func (l *LinkOverlay) InputHandler(event *tcell.EventKey) {
case tcell.KeyDown:
l.Next()
case tcell.KeyEsc:
l.app.UI.SetFocus(LeftPaneFocus)
if l.app.UI.StatusView.lastList == NotificationPaneFocus {
l.app.UI.SetFocus(NotificationPaneFocus)
} else {
l.app.UI.SetFocus(LeftPaneFocus)
}
}
}
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/gdamore/tcell/v2"
)

const version string = "0.0.27"
const version string = "0.0.28"

func main() {
newUser := false
Expand Down
4 changes: 4 additions & 0 deletions messagebox.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ func (m *MessageBox) composeToot(status *mastodon.Status) {
visibility := mastodon.VisibilityPublic
if status != nil {
visibility = status.Visibility
if status.Sensitive {
mt.Sensitive = true
mt.SpoilerText = status.SpoilerText
}
}
m.app.UI.VisibilityOverlay.SetVisibilty(visibility)

Expand Down
1 change: 0 additions & 1 deletion statusview.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,6 @@ func (t *StatusView) loadNewer() {
}
t.list.SetCurrentItem(newIndex)
t.loadingNewer = false
t.feeds[feedIndex].DrawToot()
})
}()
}
Expand Down

0 comments on commit 30fa45a

Please sign in to comment.