Monorepo for Tangled
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 NotificationTypeIssueReopen NotificationType = "issue_reopen"
21 NotificationTypePullClosed NotificationType = "pull_closed"
22 NotificationTypePullReopen NotificationType = "pull_reopen"
23 NotificationTypeUserMentioned NotificationType = "user_mentioned"
24)
25
26var SocialNotificationTypes = []NotificationType{
27 NotificationTypeRepoStarred,
28 NotificationTypeFollowed,
29}
30
31var WorkNotificationTypes = []NotificationType{
32 NotificationTypeIssueCreated,
33 NotificationTypeIssueCommented,
34 NotificationTypeIssueClosed,
35 NotificationTypeIssueReopen,
36 NotificationTypePullCreated,
37 NotificationTypePullCommented,
38 NotificationTypePullMerged,
39 NotificationTypePullClosed,
40 NotificationTypePullReopen,
41 NotificationTypeUserMentioned,
42}
43
44type Notification struct {
45 ID int64
46 RecipientDid string
47 ActorDid string
48 Type NotificationType
49 EntityType string
50 EntityId string
51 Read bool
52 Created time.Time
53
54 // foreign key references
55 RepoId *int64
56 IssueId *int64
57 PullId *int64
58}
59
60// lucide icon that represents this notification
61func (n *Notification) Icon() string {
62 switch n.Type {
63 case NotificationTypeRepoStarred:
64 return "star"
65 case NotificationTypeIssueCreated:
66 return "circle-dot"
67 case NotificationTypeIssueCommented:
68 return "message-square"
69 case NotificationTypeIssueClosed:
70 return "ban"
71 case NotificationTypeIssueReopen:
72 return "circle-dot"
73 case NotificationTypePullCreated:
74 return "git-pull-request-create"
75 case NotificationTypePullCommented:
76 return "message-square"
77 case NotificationTypePullMerged:
78 return "git-merge"
79 case NotificationTypePullClosed:
80 return "git-pull-request-closed"
81 case NotificationTypePullReopen:
82 return "git-pull-request-create"
83 case NotificationTypeFollowed:
84 return "user-plus"
85 case NotificationTypeUserMentioned:
86 return "at-sign"
87 default:
88 return ""
89 }
90}
91
92type NotificationWithEntity struct {
93 *Notification
94 Repo *Repo
95 Issue *Issue
96 Pull *Pull
97}
98
99type NotificationPreferences struct {
100 ID int64
101 UserDid syntax.DID
102 RepoStarred bool
103 IssueCreated bool
104 IssueCommented bool
105 PullCreated bool
106 PullCommented bool
107 Followed bool
108 UserMentioned bool
109 PullMerged bool
110 IssueClosed bool
111 EmailNotifications bool
112}
113
114func (prefs *NotificationPreferences) ShouldNotify(t NotificationType) bool {
115 switch t {
116 case NotificationTypeRepoStarred:
117 return prefs.RepoStarred
118 case NotificationTypeIssueCreated:
119 return prefs.IssueCreated
120 case NotificationTypeIssueCommented:
121 return prefs.IssueCommented
122 case NotificationTypeIssueClosed:
123 return prefs.IssueClosed
124 case NotificationTypeIssueReopen:
125 return prefs.IssueCreated // same pref for now
126 case NotificationTypePullCreated:
127 return prefs.PullCreated
128 case NotificationTypePullCommented:
129 return prefs.PullCommented
130 case NotificationTypePullMerged:
131 return prefs.PullMerged
132 case NotificationTypePullClosed:
133 return prefs.PullMerged // same pref for now
134 case NotificationTypePullReopen:
135 return prefs.PullCreated // same pref for now
136 case NotificationTypeFollowed:
137 return prefs.Followed
138 case NotificationTypeUserMentioned:
139 return prefs.UserMentioned
140 default:
141 return false
142 }
143}
144
145func DefaultNotificationPreferences(user syntax.DID) *NotificationPreferences {
146 return &NotificationPreferences{
147 UserDid: user,
148 RepoStarred: true,
149 IssueCreated: true,
150 IssueCommented: true,
151 PullCreated: true,
152 PullCommented: true,
153 Followed: true,
154 UserMentioned: true,
155 PullMerged: true,
156 IssueClosed: true,
157 EmailNotifications: false,
158 }
159}