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