From 70a09941d7d2c448503fd4299a8a5e2da0f63860 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Sun, 19 May 2024 18:20:28 +0100 Subject: [PATCH] add model for notification --- internal/model/notification.go | 88 ++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 internal/model/notification.go diff --git a/internal/model/notification.go b/internal/model/notification.go new file mode 100644 index 0000000..751d1c3 --- /dev/null +++ b/internal/model/notification.go @@ -0,0 +1,88 @@ +package model + +import ( + "encoding/json" + "errors" + "fmt" + "time" +) + +type NotificationType int + +const ( + NotificationTypeFollow NotificationType = iota + NotificationTypeFollowRequest + NotificationTypeMention + NotificationTypeReblog + NotificationTypeFavourite + NotificationTypePoll + NotificationTypeStatus +) + +func ParseNotificationType(notificationType string) (NotificationType, error) { + switch notificationType { + case "follow": + return NotificationTypeFollow, nil + case "follow_request": + return NotificationTypeFollowRequest, nil + case "mention": + return NotificationTypeMention, nil + case "reblog": + return NotificationTypeReblog, nil + case "favourite": + return NotificationTypeFavourite, nil + case "poll": + return NotificationTypePoll, nil + case "status": + return NotificationTypeStatus, nil + } + + return NotificationType(-1), errors.New("invalid notification type") +} + +func (n NotificationType) String() string { + switch n { + case NotificationTypeFollow: + return "Someone followed you" + case NotificationTypeFollowRequest: + return "Someone requested to follow you" + case NotificationTypeMention: + return "Someone mentioned you in their status" + case NotificationTypeReblog: + return "Someone reposted one of your statuses" + case NotificationTypeFavourite: + return "Someone liked one of your statuses" + case NotificationTypePoll: + return "A poll you have voted in (or created) has ended" + case NotificationTypeStatus: + return "Someone you enabled notifications for has posted a status" + } + + return "" +} + +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) + } + + *n, err = ParseNotificationType(value) + if err != nil { + return fmt.Errorf("unable to parse %q as a notification type; %w", value, err) + } + + return nil +} + +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"` +}