add model for notification

This commit is contained in:
Dan Anglin 2024-05-19 18:20:28 +01:00
parent b91ebe5864
commit 70a09941d7
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638

View file

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