From 2919bb9fa216962bb0fe60407805b1999b10b30f Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Mon, 3 Jun 2024 10:48:46 +0100 Subject: [PATCH] checkpoint: got the list of notification IDs --- internal/client/notifications.go | 33 ++++++++++ internal/executor/const.go | 21 ++++--- internal/executor/show.go | 14 +++++ internal/model/notification.go | 103 +++++++++++++++++++++++++++++++ internal/model/status.go | 2 +- 5 files changed, 162 insertions(+), 11 deletions(-) create mode 100644 internal/client/notifications.go create mode 100644 internal/model/notification.go diff --git a/internal/client/notifications.go b/internal/client/notifications.go new file mode 100644 index 0000000..11ed640 --- /dev/null +++ b/internal/client/notifications.go @@ -0,0 +1,33 @@ +// SPDX-FileCopyrightText: 2024 Dan Anglin +// +// SPDX-License-Identifier: GPL-3.0-or-later + +package client + +import ( + "fmt" + "net/http" + + "codeflow.dananglin.me.uk/apollo/enbas/internal/model" +) + +func (g *Client) GetNotifications(limit int) ([]model.Notification, error) { + path := fmt.Sprintf("/api/v1/notifications?limit=%d", limit) + url := g.Authentication.Instance + path + + var notifications []model.Notification + + if err := g.sendRequest(http.MethodGet, url, nil, ¬ifications); err != nil { + return nil, fmt.Errorf("received an error after sending the request to get your notifications: %w", err) + } + + return notifications, nil +} + +func (g *Client) GetNotification() error { + return nil +} + +func (g *Client) DeleteAllNotifications() error { + return nil +} diff --git a/internal/executor/const.go b/internal/executor/const.go index 58263f7..b77a067 100644 --- a/internal/executor/const.go +++ b/internal/executor/const.go @@ -35,14 +35,15 @@ const ( flagType = "type" flagVisibility = "visibility" - resourceAccount = "account" - resourceBlocked = "blocked" - resourceFollowers = "followers" - resourceFollowing = "following" - resourceInstance = "instance" - resourceList = "list" - resourceNote = "note" - resourceStatus = "status" - resourceTimeline = "timeline" - resourceBookmarks = "bookmarks" + resourceAccount = "account" + resourceBlocked = "blocked" + resourceFollowers = "followers" + resourceFollowing = "following" + resourceInstance = "instance" + resourceList = "list" + resourceNote = "note" + resourceStatus = "status" + resourceTimeline = "timeline" + resourceBookmarks = "bookmarks" + resourceNotification = "notification" ) diff --git a/internal/executor/show.go b/internal/executor/show.go index a51cb97..553f712 100644 --- a/internal/executor/show.go +++ b/internal/executor/show.go @@ -67,6 +67,7 @@ func (s *ShowExecutor) Execute() error { resourceFollowing: s.showFollowing, resourceBlocked: s.showBlocked, resourceBookmarks: s.showBookmarks, + resourceNotification: s.showNotifications, } doFunc, ok := funcMap[s.resourceType] @@ -329,3 +330,16 @@ func (s *ShowExecutor) showBookmarks(gtsClient *client.Client) error { return nil } + +func (s *ShowExecutor) showNotifications(gts *client.Client) error { + notifications, err := gts.GetNotifications(s.limit) + if err != nil { + return fmt.Errorf("unable to retrieve your notifications: %w", err) + } + + for i := range notifications { + fmt.Printf("\nNotification ID: %s\n%s", notifications[i].ID, notifications[i].Type) + } + + return nil +} diff --git a/internal/model/notification.go b/internal/model/notification.go new file mode 100644 index 0000000..089edd6 --- /dev/null +++ b/internal/model/notification.go @@ -0,0 +1,103 @@ +// SPDX-FileCopyrightText: 2024 Dan Anglin +// +// SPDX-License-Identifier: GPL-3.0-or-later + +package model + +import ( + "encoding/json" + "fmt" + "time" +) + +type NotificationType int + +const ( + NotificationTypeFollow NotificationType = iota + NotificationTypeFollowRequest + NotificationTypeMention + NotificationTypeReblog + NotificationTypeFavourite + NotificationTypePoll + NotificationTypeStatus + NotificationTypeUnknown +) + +const ( + notificationFollow = "follow" + notificationFollowRequest = "follow_request" + notificationMention = "mention" + notificationReblog = "reblog" + notificationFavourite = "favourite" + notificationPoll = "poll" + notificationStatus = "status" +) + +func (n NotificationType) String() string { + mapped := map[NotificationType]string{ + NotificationTypeFollow: "Someone followed you", + NotificationTypeFollowRequest: "Someone requested to follow you", + NotificationTypeMention: "Someone mentioned you in their status", + NotificationTypeReblog: "Someone reposted one of your statuses", + NotificationTypeFavourite: "Someone liked one of your statuses", + NotificationTypePoll: "A poll you have participated in has ended", + NotificationTypeStatus: "Someone you enabled notifications for has posted a status", + } + + output, ok := mapped[n] + if !ok { + return "You have received an unknown notification" + } + + return output +} + +func ParseNotificationType(value string) (NotificationType, error) { + mapped := map[string]NotificationType{ + notificationFollow: NotificationTypeFollow, + notificationFollowRequest: NotificationTypeFollowRequest, + notificationMention: NotificationTypeMention, + notificationReblog: NotificationTypeReblog, + notificationFavourite: NotificationTypeFavourite, + notificationPoll: NotificationTypePoll, + notificationStatus: NotificationTypeStatus, + } + + output, ok := mapped[value] + if !ok { + return NotificationTypeUnknown, UnknownNotificationError{} + } + + return output, nil +} + +func (n *NotificationType) UnmarshalJSON(data []byte) error { + var ( + value string + err error + ) + + if err = json.Unmarshal(data, &value); err != nil { + return fmt.Errorf("unable to unmarshal the data: %w", err) + } + + // Ignore the error if the notification type from another Fediverse service + // is not known by enbas. It will be seen as an unknown notification to the user. + *n, _ = ParseNotificationType(value) + + return nil +} + +type UnknownNotificationError struct{} + +func (e UnknownNotificationError) Error() string { + return "unknown notification type" +} + +type Notification struct { + Account *Account `json:"account"` + CreatedAt time.Time `json:"created_at"` + ID string `json:"id"` + Status *Status `json:"status"` + Type NotificationType `json:"type"` +} diff --git a/internal/model/status.go b/internal/model/status.go index e1db973..85fb8be 100644 --- a/internal/model/status.go +++ b/internal/model/status.go @@ -83,7 +83,7 @@ type Poll struct { type PollOption struct { Title string `json:"title"` - VotesCount string `json:"votes_count"` + VotesCount int `json:"votes_count"` } type StatusReblogged struct {