Monorepo for Tangled tangled.org
1package notify 2 3import ( 4 "context" 5 "log/slog" 6 "reflect" 7 "sync" 8 9 "github.com/bluesky-social/indigo/atproto/syntax" 10 "tangled.org/core/appview/models" 11 "tangled.org/core/log" 12) 13 14type mergedNotifier struct { 15 notifiers []Notifier 16 logger *slog.Logger 17} 18 19func NewMergedNotifier(notifiers []Notifier, logger *slog.Logger) Notifier { 20 return &mergedNotifier{notifiers, logger} 21} 22 23var _ Notifier = &mergedNotifier{} 24 25// fanout calls the same method on all notifiers concurrently 26func (m *mergedNotifier) fanout(method string, ctx context.Context, args ...any) { 27 ctx = log.IntoContext(ctx, m.logger.With("method", method)) 28 var wg sync.WaitGroup 29 for _, n := range m.notifiers { 30 wg.Add(1) 31 go func(notifier Notifier) { 32 defer wg.Done() 33 v := reflect.ValueOf(notifier).MethodByName(method) 34 in := make([]reflect.Value, len(args)+1) 35 in[0] = reflect.ValueOf(ctx) 36 for i, arg := range args { 37 in[i+1] = reflect.ValueOf(arg) 38 } 39 v.Call(in) 40 }(n) 41 } 42} 43 44func (m *mergedNotifier) NewRepo(ctx context.Context, repo *models.Repo) { 45 m.fanout("NewRepo", ctx, repo) 46} 47 48func (m *mergedNotifier) NewStar(ctx context.Context, star *models.Star) { 49 m.fanout("NewStar", ctx, star) 50} 51 52func (m *mergedNotifier) DeleteStar(ctx context.Context, star *models.Star) { 53 m.fanout("DeleteStar", ctx, star) 54} 55 56func (m *mergedNotifier) NewIssue(ctx context.Context, issue *models.Issue, mentions []syntax.DID) { 57 m.fanout("NewIssue", ctx, issue, mentions) 58} 59 60func (m *mergedNotifier) NewIssueComment(ctx context.Context, comment *models.IssueComment, mentions []syntax.DID) { 61 m.fanout("NewIssueComment", ctx, comment, mentions) 62} 63 64func (m *mergedNotifier) NewIssueState(ctx context.Context, actor syntax.DID, issue *models.Issue) { 65 m.fanout("NewIssueState", ctx, actor, issue) 66} 67 68func (m *mergedNotifier) DeleteIssue(ctx context.Context, issue *models.Issue) { 69 m.fanout("DeleteIssue", ctx, issue) 70} 71 72func (m *mergedNotifier) NewFollow(ctx context.Context, follow *models.Follow) { 73 m.fanout("NewFollow", ctx, follow) 74} 75 76func (m *mergedNotifier) DeleteFollow(ctx context.Context, follow *models.Follow) { 77 m.fanout("DeleteFollow", ctx, follow) 78} 79 80func (m *mergedNotifier) NewPull(ctx context.Context, pull *models.Pull) { 81 m.fanout("NewPull", ctx, pull) 82} 83 84func (m *mergedNotifier) NewPullComment(ctx context.Context, comment *models.PullComment, mentions []syntax.DID) { 85 m.fanout("NewPullComment", ctx, comment, mentions) 86} 87 88func (m *mergedNotifier) NewPullState(ctx context.Context, actor syntax.DID, pull *models.Pull) { 89 m.fanout("NewPullState", ctx, actor, pull) 90} 91 92func (m *mergedNotifier) UpdateProfile(ctx context.Context, profile *models.Profile) { 93 m.fanout("UpdateProfile", ctx, profile) 94} 95 96func (m *mergedNotifier) NewString(ctx context.Context, s *models.String) { 97 m.fanout("NewString", ctx, s) 98} 99 100func (m *mergedNotifier) EditString(ctx context.Context, s *models.String) { 101 m.fanout("EditString", ctx, s) 102} 103 104func (m *mergedNotifier) DeleteString(ctx context.Context, did, rkey string) { 105 m.fanout("DeleteString", ctx, did, rkey) 106}