Skip to content

Commit

Permalink
notifications view (#5)
Browse files Browse the repository at this point in the history
* initial work for notifications

* reply description

* view notification cast
  • Loading branch information
treethought committed Jun 16, 2024
1 parent 66b3b6a commit c738131
Show file tree
Hide file tree
Showing 6 changed files with 351 additions and 24 deletions.
1 change: 1 addition & 0 deletions api/cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Reactions struct {
}

type Cast struct {
Object string `json:"object"`
Hash string `json:"hash"`
ThreadHash string `json:"thread_hash"`
ParentHash string `json:"parent_hash"`
Expand Down
64 changes: 64 additions & 0 deletions api/notifications.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package api

import (
"context"
"fmt"
"time"
)

type NotificationsType string
type CastReactionObjType string

const (
NotificationsTypeFollows NotificationsType = "follows"
NotificationsTypeLikes NotificationsType = "likes"
NotificationsTypeRecasts NotificationsType = "recasts"
NotificationsTypeMention NotificationsType = "mention"
NotificationsTypeReply NotificationsType = "reply"

CastReactionObjTypeLikes CastReactionObjType = "likes"
CastReactionObjTypeRecasts CastReactionObjType = "recasts"
)

type NotificationsResponse struct {
Notifications []*Notification `json:"notifications"`
Next struct {
Cursor *string `json:"cursor"`
}
}

type Notification struct {
Object string `json:"object"`
MostRecentTimestamp time.Time `json:"most_recent_timestamp"`
Type NotificationsType `json:"type"`
Cast *Cast `json:"cast"`
Follows []FollowNotification `json:"follows"`
Reactions []ReactionNotification `json:"reactions"`
}

type FollowNotification struct {
Object string `json:"object"`
User User `json:"user"`
}

type ReactionNotification struct {
Object CastReactionObjType `json:"object"`
Cast NotificationCast `json:"cast"`
User User `json:"user"`
}

type NotificationCast struct {
Cast // may be cast_dehydrated which only has hash, specifically for reactions
}

func (c *Client) GetNotifications(fid uint64, opts ...RequestOption) (*NotificationsResponse, error) {
path := fmt.Sprintf("/notifications")

opts = append(opts, WithFID(fid))

var resp NotificationsResponse
if err := c.doRequestInto(context.TODO(), path, &resp, opts...); err != nil {
return nil, err
}
return &resp, nil
}
83 changes: 63 additions & 20 deletions ui/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,21 @@ type AppContext struct {
}

type App struct {
ctx *AppContext
client *api.Client
cfg *config.Config
focusedModel tea.Model
focused string
navname string
sidebar *Sidebar
showSidebar bool
prev string
prevName string
quickSelect *QuickSelect
publish *PublishInput
statusLine *StatusLine
// signinPrompt *SigninPrompt
ctx *AppContext
client *api.Client
cfg *config.Config
focusedModel tea.Model
focused string
navname string
sidebar *Sidebar
showSidebar bool
prev string
prevName string
quickSelect *QuickSelect
publish *PublishInput
statusLine *StatusLine
notifications *NotificationsView

splash *SplashView
help *HelpView

Expand Down Expand Up @@ -135,6 +136,7 @@ func NewApp(cfg *config.Config, ctx *AppContext) *App {
a.publish = NewPublishInput(a)
a.statusLine = NewStatusLine(a)
a.help = NewHelpView(a)
a.notifications = NewNotificationsView(a)
a.splash = NewSplashView(a)
a.splash.SetActive(true)
if a.ctx.signer == nil {
Expand Down Expand Up @@ -162,6 +164,27 @@ func (a *App) focusMain() {
if a.help.IsFull() {
a.help.SetFull(false)
}
if a.notifications.Active() {
a.notifications.SetActive(false)
}
}

func (a *App) FocusPublish() {
a.publish.SetActive(true)
a.publish.SetFocus(true)
}
func (a *App) FocusHelp() {
a.help.SetFull(!a.help.IsFull())
}
func (a *App) FocusQuickSelect() {
a.quickSelect.SetActive(true)
}
func (a *App) FocusNotifications() tea.Cmd {
a.notifications.SetActive(true)
return a.notifications.Init()
}
func (a *App) ToggleHelp() {
a.help.SetFull(!a.help.IsFull())
}

func (a *App) FocusFeed() tea.Cmd {
Expand Down Expand Up @@ -216,7 +239,11 @@ func (a *App) FocusPrev() tea.Cmd {

func (a *App) Init() tea.Cmd {
cmds := []tea.Cmd{}
cmds = append(cmds, a.splash.Init(), a.sidebar.Init(), a.quickSelect.Init(), a.publish.Init())
cmds = append(cmds,
a.splash.Init(), a.sidebar.Init(),
a.quickSelect.Init(), a.publish.Init(),
a.notifications.Init(),
)
focus := a.GetFocused()
if focus != nil {
cmds = append(cmds, focus.Init())
Expand All @@ -231,6 +258,9 @@ func (a *App) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
_, sbcmd := a.statusLine.Update(msg)
cmds = append(cmds, sbcmd)
switch msg := msg.(type) {
case *notificationsMsg:
_, cmd := a.notifications.Update(msg)
return a, cmd
case *UpdateSignerMsg:
a.ctx.signer = msg.Signer
a.splash.ShowSignin(false)
Expand Down Expand Up @@ -296,10 +326,11 @@ func (a *App) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

my := min(wy, int(float64(wy)*0.9))

dialogX, dialogY := int(float64(mx)*0.8), int(float64(my)*0.8)
dialogX, dialogY := int(float64(mx)*0.8), int(float64(my)*0.9)
a.publish.SetSize(dialogX, dialogY)
a.quickSelect.SetSize(dialogX, dialogY)
a.help.SetSize(dialogX, dialogY)
a.notifications.SetSize(dialogX, dialogY)

childMsg := tea.WindowSizeMsg{
Width: mx,
Expand All @@ -318,7 +349,14 @@ func (a *App) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case "ctrl+c", "q":
return a, tea.Quit
}

cmd := NavKeyMap.HandleMsg(a, msg)
if cmd != nil {
return a, cmd
}
if a.sidebar.Active() {
_, cmd := a.sidebar.Update(msg)
return a, cmd
}
if a.splash.Active() {
_, cmd := a.splash.Update(msg)
return a, cmd
Expand All @@ -328,9 +366,11 @@ func (a *App) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return a, cmd
}

cmd := NavKeyMap.HandleMsg(a, msg)
if cmd != nil {
return a, cmd
if a.notifications.Active() {
_, cmd := a.notifications.Update(msg)
if cmd != nil {
return a, cmd
}
}

case *currentAccountMsg:
Expand Down Expand Up @@ -383,6 +423,9 @@ func (a *App) View() string {
main = lipgloss.Place(GetWidth(), GetHeight(), lipgloss.Center, lipgloss.Center, a.splash.View())
return main
}
if a.notifications.Active() {
main = a.notifications.View()
}

if a.publish.Active() {
main = a.publish.View()
Expand Down
18 changes: 14 additions & 4 deletions ui/keybindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,14 @@ type navKeymap struct {
ToggleSidebarFocus key.Binding
ToggleSidebarVisibility key.Binding
Previous key.Binding
ViewNotifications key.Binding
}

func (k navKeymap) ShortHelp() []key.Binding {
return []key.Binding{
k.Feed,
k.QuickSelect,
k.ViewNotifications,
k.Help,
}
}
Expand All @@ -99,6 +101,7 @@ func (k navKeymap) All() []key.Binding {
return []key.Binding{
k.Feed, k.QuickSelect,
k.Publish,
k.ViewNotifications,
k.Previous,
k.Help,
k.ToggleSidebarFocus, k.ToggleSidebarVisibility,
Expand Down Expand Up @@ -134,6 +137,10 @@ var NavKeyMap = navKeymap{
key.WithKeys("esc"),
key.WithHelp("esc", "focus previous"),
),
ViewNotifications: key.NewBinding(
key.WithKeys("N"),
key.WithHelp("N", "view notifications"),
),
}

func (k navKeymap) HandleMsg(a *App, msg tea.KeyMsg) tea.Cmd {
Expand All @@ -147,16 +154,19 @@ func (k navKeymap) HandleMsg(a *App, msg tea.KeyMsg) tea.Cmd {
return tea.Sequence(cmd, a.FocusFeed())

case key.Matches(msg, k.Publish):
a.publish.SetActive(true)
a.publish.SetFocus(true)
a.FocusPublish()
return noOp()

case key.Matches(msg, k.QuickSelect):
a.quickSelect.SetActive(true)
a.FocusQuickSelect()
return nil

case key.Matches(msg, k.Help):
a.help.SetFull(!a.help.IsFull())
a.FocusHelp()

case key.Matches(msg, k.ViewNotifications):
log.Println("ViewNotifications")
return a.FocusNotifications()

case key.Matches(msg, k.Previous):
return a.FocusPrev()
Expand Down
Loading

0 comments on commit c738131

Please sign in to comment.