Something went wrong. Try again.
Monorepo for Tangled tangled.org
Something went wrong. Try again.
Go
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223package models
import ( "context" "fmt" "time"
"github.com/bluesky-social/indigo/atproto/syntax" "tangled.org/core/idresolver")
type NotificationType string
const ( NotificationTypeRepoStarred NotificationType = "repo_starred" NotificationTypeIssueCreated NotificationType = "issue_created" NotificationTypeIssueCommented NotificationType = "issue_commented" NotificationTypePullCreated NotificationType = "pull_created" NotificationTypePullCommented NotificationType = "pull_commented" NotificationTypeFollowed NotificationType = "followed" NotificationTypePullMerged NotificationType = "pull_merged" NotificationTypeIssueClosed NotificationType = "issue_closed" NotificationTypeIssueReopen NotificationType = "issue_reopen" NotificationTypePullClosed NotificationType = "pull_closed" NotificationTypePullReopen NotificationType = "pull_reopen" NotificationTypeUserMentioned NotificationType = "user_mentioned" NotificationTypeIssueAssigned NotificationType = "issue_assigned" NotificationTypeIssueUnassigned NotificationType = "issue_unassigned" NotificationTypePullAssigned NotificationType = "pull_assigned" NotificationTypePullUnassigned NotificationType = "pull_unassigned" NotificationTypeCollaboratorInvited NotificationType = "collaborator_invited")
var SocialNotificationTypes = []NotificationType{ NotificationTypeRepoStarred, NotificationTypeFollowed,}
var WorkNotificationTypes = []NotificationType{ NotificationTypeIssueCreated, NotificationTypeIssueCommented, NotificationTypeIssueClosed, NotificationTypeIssueReopen, NotificationTypePullCreated, NotificationTypePullCommented, NotificationTypePullMerged, NotificationTypePullClosed, NotificationTypePullReopen, NotificationTypeUserMentioned, NotificationTypeIssueAssigned, NotificationTypeIssueUnassigned, NotificationTypePullAssigned, NotificationTypePullUnassigned, NotificationTypeCollaboratorInvited,}
// email digest types; social types (repo_starred, followed) are excluded.var EmailNotificationTypes = []NotificationType{ NotificationTypeIssueCreated, NotificationTypeIssueCommented, NotificationTypeIssueClosed, NotificationTypeIssueReopen, NotificationTypePullCreated, NotificationTypePullCommented, NotificationTypePullMerged, NotificationTypePullClosed, NotificationTypePullReopen, NotificationTypeUserMentioned, NotificationTypeIssueAssigned, NotificationTypeIssueUnassigned, NotificationTypePullAssigned, NotificationTypePullUnassigned,}
type Notification struct { ID int64 RecipientDid string AtUri string // source record (comment/issue/pull/star/follow); dedupe key Type NotificationType ActorDid string RepoDid string KnotDid string RepoName string // not stored; filled from the repo cache when the digest reads EntityAt string // related issue/pull at-uri (for links); source itself for issue/pull EntityTitle string Read bool Emailed bool Created time.Time}
func (n *Notification) Icon() string { switch n.Type { case NotificationTypeRepoStarred: return "star" case NotificationTypeIssueCreated, NotificationTypeIssueReopen: return "circle-dot" case NotificationTypeIssueCommented, NotificationTypePullCommented: return "message-square" case NotificationTypeIssueClosed: return "ban" case NotificationTypePullCreated, NotificationTypePullReopen: return "git-pull-request-create" case NotificationTypePullMerged: return "git-merge" case NotificationTypePullClosed: return "git-pull-request-closed" case NotificationTypeFollowed: return "user-plus" case NotificationTypeUserMentioned: return "at-sign" case NotificationTypeIssueAssigned, NotificationTypePullAssigned: return "user-round-arrow-forward" case NotificationTypeIssueUnassigned, NotificationTypePullUnassigned: return "user-round-minus" case NotificationTypeCollaboratorInvited: return "users-round" default: return "" }}
func (n *Notification) URL(res *idresolver.Resolver) string { resolve := func(did string) string { if id, err := res.ResolveIdent(context.Background(), did); err == nil && !id.Handle.IsInvalidHandle() { return id.Handle.String() } return did }
switch n.Type { case NotificationTypeFollowed: return "/" + resolve(n.ActorDid) } if n.RepoDid == "" || n.RepoName == "" { return "" } repoHandle := resolve(n.RepoDid) if n.EntityAt != "" { switch syntax.ATURI(n.EntityAt).Collection().String() { case "sh.tangled.repo.issue": return fmt.Sprintf("/%s/%s/issues/%s", repoHandle, n.RepoName, n.EntityAt) case "sh.tangled.repo.pull": return fmt.Sprintf("/%s/%s/pulls/%s", repoHandle, n.RepoName, n.EntityAt) } } return fmt.Sprintf("/%s/%s", repoHandle, n.RepoName)}
func Category(t NotificationType) string { for _, st := range SocialNotificationTypes { if st == t { return "social" } } return "work"}
type NotificationPreferences struct { ID int64 UserDid syntax.DID RepoStarred bool IssueCreated bool IssueCommented bool PullCreated bool PullCommented bool Followed bool UserMentioned bool PullMerged bool IssueClosed bool EmailNotifications bool}
func (prefs *NotificationPreferences) ShouldNotify(t NotificationType) bool { switch t { case NotificationTypeRepoStarred: return prefs.RepoStarred case NotificationTypeIssueCreated: return prefs.IssueCreated case NotificationTypeIssueCommented: return prefs.IssueCommented case NotificationTypeIssueClosed: return prefs.IssueClosed case NotificationTypeIssueReopen: return prefs.IssueCreated case NotificationTypePullCreated: return prefs.PullCreated case NotificationTypePullCommented: return prefs.PullCommented case NotificationTypePullMerged: return prefs.PullMerged case NotificationTypePullClosed: return prefs.PullMerged case NotificationTypePullReopen: return prefs.PullCreated case NotificationTypeFollowed: return prefs.Followed case NotificationTypeUserMentioned, NotificationTypeIssueAssigned, NotificationTypeIssueUnassigned, NotificationTypePullAssigned, NotificationTypePullUnassigned: return prefs.UserMentioned default: return false }}
func DefaultNotificationPreferences(user syntax.DID) *NotificationPreferences { return &NotificationPreferences{ UserDid: user, RepoStarred: true, IssueCreated: true, IssueCommented: true, PullCreated: true, PullCommented: true, Followed: true, UserMentioned: true, PullMerged: true, IssueClosed: true, EmailNotifications: false, }}