forked from tangled.org/core
Monorepo for Tangled — https://tangled.org
1package models 2 3import ( 4 "time" 5 6 "github.com/bluesky-social/indigo/atproto/syntax" 7) 8 9type NotificationType string 10 11const ( 12 NotificationTypeRepoStarred NotificationType = "repo_starred" 13 NotificationTypeIssueCreated NotificationType = "issue_created" 14 NotificationTypeIssueCommented NotificationType = "issue_commented" 15 NotificationTypePullCreated NotificationType = "pull_created" 16 NotificationTypePullCommented NotificationType = "pull_commented" 17 NotificationTypeFollowed NotificationType = "followed" 18 NotificationTypePullMerged NotificationType = "pull_merged" 19 NotificationTypeIssueClosed NotificationType = "issue_closed" 20 NotificationTypePullClosed NotificationType = "pull_closed" 21) 22 23type Notification struct { 24 ID int64 25 RecipientDid string 26 ActorDid string 27 Type NotificationType 28 EntityType string 29 EntityId string 30 Read bool 31 Created time.Time 32 33 // foreign key references 34 RepoId *int64 35 IssueId *int64 36 PullId *int64 37} 38 39// lucide icon that represents this notification 40func (n *Notification) Icon() string { 41 switch n.Type { 42 case NotificationTypeRepoStarred: 43 return "star" 44 case NotificationTypeIssueCreated: 45 return "circle-dot" 46 case NotificationTypeIssueCommented: 47 return "message-square" 48 case NotificationTypeIssueClosed: 49 return "ban" 50 case NotificationTypePullCreated: 51 return "git-pull-request-create" 52 case NotificationTypePullCommented: 53 return "message-square" 54 case NotificationTypePullMerged: 55 return "git-merge" 56 case NotificationTypePullClosed: 57 return "git-pull-request-closed" 58 case NotificationTypeFollowed: 59 return "user-plus" 60 default: 61 return "" 62 } 63} 64 65type NotificationWithEntity struct { 66 *Notification 67 Repo *Repo 68 Issue *Issue 69 Pull *Pull 70} 71 72type NotificationPreferences struct { 73 ID int64 74 UserDid syntax.DID 75 RepoStarred bool 76 IssueCreated bool 77 IssueCommented bool 78 PullCreated bool 79 PullCommented bool 80 Followed bool 81 PullMerged bool 82 IssueClosed bool 83 EmailNotifications bool 84} 85 86func (prefs *NotificationPreferences) ShouldNotify(t NotificationType) bool { 87 switch t { 88 case NotificationTypeRepoStarred: 89 return prefs.RepoStarred 90 case NotificationTypeIssueCreated: 91 return prefs.IssueCreated 92 case NotificationTypeIssueCommented: 93 return prefs.IssueCommented 94 case NotificationTypeIssueClosed: 95 return prefs.IssueClosed 96 case NotificationTypePullCreated: 97 return prefs.PullCreated 98 case NotificationTypePullCommented: 99 return prefs.PullCommented 100 case NotificationTypePullMerged: 101 return prefs.PullMerged 102 case NotificationTypePullClosed: 103 return prefs.PullMerged // same pref for now 104 case NotificationTypeFollowed: 105 return prefs.Followed 106 default: 107 return false 108 } 109} 110 111func DefaultNotificationPreferences(user syntax.DID) *NotificationPreferences { 112 return &NotificationPreferences{ 113 UserDid: user, 114 RepoStarred: true, 115 IssueCreated: true, 116 IssueCommented: true, 117 PullCreated: true, 118 PullCommented: true, 119 Followed: true, 120 PullMerged: true, 121 IssueClosed: true, 122 EmailNotifications: false, 123 } 124}