// 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"` }