Skip to content

Commit

Permalink
Implemented notifications, optimized colors
Browse files Browse the repository at this point in the history
  • Loading branch information
mrusme committed Apr 5, 2021
1 parent a6026cc commit 24bf3ff
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 6 deletions.
11 changes: 9 additions & 2 deletions mast/timeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func (timeline *Timeline) GetCurrentOptions() (TimelineOptions) {

func (timeline *Timeline) Load() (error) {
var statuses []*mastodon.Status
var notifications []*mastodon.Notification
var err error

account, err := timeline.client.GetAccountCurrentUser(context.Background())
Expand All @@ -92,7 +93,7 @@ func (timeline *Timeline) Load() (error) {
statuses, err =
timeline.client.GetTimelinePublic(context.Background(), false, nil)
case TimelineNotifications:
notifications, err :=
notifications, err =
timeline.client.GetNotifications(context.Background(), nil)
if err != nil {
return err
Expand Down Expand Up @@ -123,15 +124,21 @@ func (timeline *Timeline) Load() (error) {
}

oldestStatusIndex := len(statuses) - 1
oldestNotificationIndex := len(notifications) - 1
for i := oldestStatusIndex; i >= 0; i-- {
status := statuses[i]
var notification *mastodon.Notification = nil

if oldestNotificationIndex >= i {
notification = notifications[i]
}

statusID := string(status.ID)
_, exists := timeline.TootIndexStatusIDMappings[statusID]
if exists == false {
tootIndex := len(timeline.Toots)
timeline.Toots =
append(timeline.Toots, NewToot(timeline.client, status, tootIndex))
append(timeline.Toots, NewToot(timeline.client, status, notification, tootIndex))

timeline.TootIndexStatusIDMappings[statusID] = tootIndex
timeline.KnownUsers[string(status.Account.ID)] = status.Account.Acct
Expand Down
11 changes: 10 additions & 1 deletion mast/toot.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ type Toot struct {

ID int
Status mastodon.Status

IsNotification bool
Notification mastodon.Notification
}

func NewToot(
mastodonClient *mastodon.Client,
mastodonStatus *mastodon.Status ,
mastodonStatus *mastodon.Status,
mastodonNotification *mastodon.Notification,
id int) Toot {
toot := Toot{
client: mastodonClient,
Expand All @@ -31,5 +35,10 @@ func NewToot(
Status: *mastodonStatus,
}

if mastodonNotification != nil {
toot.IsNotification = true
toot.Notification = *mastodonNotification
}

return toot
}
2 changes: 1 addition & 1 deletion tui/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func RenderProfile(

for i := 0; i < fieldsNumber; i++ {
field := profile.Fields[i]
output = fmt.Sprintf("%s[grey]%s:[-] [magenta]%s[-]\n",
output = fmt.Sprintf("%s[grey]%s:[-] [purple]%s[-]\n",
output,
runewidth.Truncate(
field.Name,
Expand Down
52 changes: 50 additions & 2 deletions tui/toot.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,55 @@ func RenderToot(toot *mast.Toot, width int, showImages bool) (string, error) {
// https://github.com/mattn/go-runewidth/issues/36
runewidth.StringWidth(inReplyTo)

output = fmt.Sprintf("%s[blue]%s[-] [grey]%s[-][magenta]%s[-][grey]%*d[-]\n",
if toot.IsNotification == true {
notification := &toot.Notification

notificationText := ""

notificationAccount := notification.Account.Acct
if notificationAccount == "" {
notificationAccount = notification.Account.Username
}

// https://docs.joinmastodon.org/entities/notification/#type
switch notification.Type {
case "follow":
notificationText = fmt.Sprintf("[red]%s followed you[-]",
notificationAccount,
)
case "follow_request":
notificationText = fmt.Sprintf("[blue]%s requested to follow you[-]",
notificationAccount,
)
case "mention":
notificationText = fmt.Sprintf("[purple]\xe2\x86\xab %s mentioned you[-]",
notificationAccount,
)
case "reblog":
notificationText = fmt.Sprintf("[green]\xe2\x86\xbb %s boosted your toot[-]",
notificationAccount,
)
case "favourite":
notificationText = fmt.Sprintf("[yellow]\xe2\x98\x85 %s faved your toot[-]",
notificationAccount,
)
case "poll":
notificationText = fmt.Sprintf("[grey]A poll by %s has ended[-]",
notificationAccount,
)
case "status":
notificationText = fmt.Sprintf("[grey]%s posted a toot[-]",
notificationAccount,
)
}

output = fmt.Sprintf("%s%s\n",
output,
notificationText,
)
}

output = fmt.Sprintf("%s[blue]%s[-] [grey]%s[-][purple]%s[-][grey]%*d[-]\n",
output,
status.Account.DisplayName,
account,
Expand Down Expand Up @@ -71,7 +119,7 @@ func RenderToot(toot *mast.Toot, width int, showImages bool) (string, error) {
}
}

output = fmt.Sprintf("%s[magenta]\xe2\x86\xab %d[-] ",
output = fmt.Sprintf("%s[purple]\xe2\x86\xab %d[-] ",
output,
status.RepliesCount,
)
Expand Down

0 comments on commit 24bf3ff

Please sign in to comment.