Mirror of @tangled.org/core. Running on a Raspberry Pi Zero 2

appview/notify: introduce loggingNotifier as a separate notifier

this was previously clubbed into mergedNotifier. this changeset also
employes the new context-logger in every notifier.

Signed-off-by: oppiliappan <me@oppi.li>

authored by oppi.li and committed by tangled.org 306613d5 c94a9d40

+149 -15
+42 -14
appview/notify/db/db.go
··· 2 2 3 3 import ( 4 4 "context" 5 - "log" 6 5 "slices" 7 6 8 7 "github.com/bluesky-social/indigo/atproto/syntax" ··· 10 11 "tangled.org/core/appview/models" 11 12 "tangled.org/core/appview/notify" 12 13 "tangled.org/core/idresolver" 14 + "tangled.org/core/log" 13 15 "tangled.org/core/orm" 14 16 "tangled.org/core/sets" 15 17 ) ··· 38 38 } 39 39 40 40 func (n *databaseNotifier) NewStar(ctx context.Context, star *models.Star) { 41 + l := log.FromContext(ctx) 42 + 41 43 if star.RepoAt.Collection().String() != tangled.RepoNSID { 42 44 // skip string stars for now 43 45 return ··· 47 45 var err error 48 46 repo, err := db.GetRepo(n.db, orm.FilterEq("at_uri", string(star.RepoAt))) 49 47 if err != nil { 50 - log.Printf("NewStar: failed to get repos: %v", err) 48 + l.Error("failed to get repos", "err", err) 51 49 return 52 50 } 53 51 ··· 61 59 var pullId *int64 62 60 63 61 n.notifyEvent( 62 + ctx, 64 63 actorDid, 65 64 recipients, 66 65 eventType, ··· 78 75 } 79 76 80 77 func (n *databaseNotifier) NewIssue(ctx context.Context, issue *models.Issue, mentions []syntax.DID) { 78 + l := log.FromContext(ctx) 79 + 81 80 collaborators, err := db.GetCollaborators(n.db, orm.FilterEq("repo_at", issue.Repo.RepoAt())) 82 81 if err != nil { 83 - log.Printf("failed to fetch collaborators: %v", err) 82 + l.Error("failed to fetch collaborators", "err", err) 84 83 return 85 84 } 86 85 ··· 106 101 var pullId *int64 107 102 108 103 n.notifyEvent( 104 + ctx, 109 105 actorDid, 110 106 recipients, 111 107 models.NotificationTypeIssueCreated, ··· 117 111 pullId, 118 112 ) 119 113 n.notifyEvent( 114 + ctx, 120 115 actorDid, 121 116 sets.Collect(slices.Values(mentions)), 122 117 models.NotificationTypeUserMentioned, ··· 130 123 } 131 124 132 125 func (n *databaseNotifier) NewIssueComment(ctx context.Context, comment *models.IssueComment, mentions []syntax.DID) { 126 + l := log.FromContext(ctx) 127 + 133 128 issues, err := db.GetIssues(n.db, orm.FilterEq("at_uri", comment.IssueAt)) 134 129 if err != nil { 135 - log.Printf("NewIssueComment: failed to get issues: %v", err) 130 + l.Error("failed to get issues", "err", err) 136 131 return 137 132 } 138 133 if len(issues) == 0 { 139 - log.Printf("NewIssueComment: no issue found for %s", comment.IssueAt) 134 + l.Error("no issue found for", "err", comment.IssueAt) 140 135 return 141 136 } 142 137 issue := issues[0] ··· 179 170 var pullId *int64 180 171 181 172 n.notifyEvent( 173 + ctx, 182 174 actorDid, 183 175 recipients, 184 176 models.NotificationTypeIssueCommented, ··· 190 180 pullId, 191 181 ) 192 182 n.notifyEvent( 183 + ctx, 193 184 actorDid, 194 185 sets.Collect(slices.Values(mentions)), 195 186 models.NotificationTypeUserMentioned, ··· 215 204 var repoId, issueId, pullId *int64 216 205 217 206 n.notifyEvent( 207 + ctx, 218 208 actorDid, 219 209 recipients, 220 210 eventType, ··· 232 220 } 233 221 234 222 func (n *databaseNotifier) NewPull(ctx context.Context, pull *models.Pull) { 223 + l := log.FromContext(ctx) 224 + 235 225 repo, err := db.GetRepo(n.db, orm.FilterEq("at_uri", string(pull.RepoAt))) 236 226 if err != nil { 237 - log.Printf("NewPull: failed to get repos: %v", err) 227 + l.Error("failed to get repos", "err", err) 238 228 return 239 229 } 240 230 collaborators, err := db.GetCollaborators(n.db, orm.FilterEq("repo_at", repo.RepoAt())) 241 231 if err != nil { 242 - log.Printf("failed to fetch collaborators: %v", err) 232 + l.Error("failed to fetch collaborators", "err", err) 243 233 return 244 234 } 245 235 ··· 263 249 pullId := &p 264 250 265 251 n.notifyEvent( 252 + ctx, 266 253 actorDid, 267 254 recipients, 268 255 eventType, ··· 276 261 } 277 262 278 263 func (n *databaseNotifier) NewPullComment(ctx context.Context, comment *models.PullComment, mentions []syntax.DID) { 264 + l := log.FromContext(ctx) 265 + 279 266 pull, err := db.GetPull(n.db, 280 267 syntax.ATURI(comment.RepoAt), 281 268 comment.PullId, 282 269 ) 283 270 if err != nil { 284 - log.Printf("NewPullComment: failed to get pulls: %v", err) 271 + l.Error("failed to get pulls", "err", err) 285 272 return 286 273 } 287 274 288 275 repo, err := db.GetRepo(n.db, orm.FilterEq("at_uri", comment.RepoAt)) 289 276 if err != nil { 290 - log.Printf("NewPullComment: failed to get repos: %v", err) 277 + l.Error("failed to get repos", "err", err) 291 278 return 292 279 } 293 280 ··· 315 298 pullId := &p 316 299 317 300 n.notifyEvent( 301 + ctx, 318 302 actorDid, 319 303 recipients, 320 304 eventType, ··· 326 308 pullId, 327 309 ) 328 310 n.notifyEvent( 311 + ctx, 329 312 actorDid, 330 313 sets.Collect(slices.Values(mentions)), 331 314 models.NotificationTypeUserMentioned, ··· 355 336 } 356 337 357 338 func (n *databaseNotifier) NewIssueState(ctx context.Context, actor syntax.DID, issue *models.Issue) { 339 + l := log.FromContext(ctx) 340 + 358 341 collaborators, err := db.GetCollaborators(n.db, orm.FilterEq("repo_at", issue.Repo.RepoAt())) 359 342 if err != nil { 360 - log.Printf("failed to fetch collaborators: %v", err) 343 + l.Error("failed to fetch collaborators", "err", err) 361 344 return 362 345 } 363 346 ··· 389 368 } 390 369 391 370 n.notifyEvent( 371 + ctx, 392 372 actor, 393 373 recipients, 394 374 eventType, ··· 402 380 } 403 381 404 382 func (n *databaseNotifier) NewPullState(ctx context.Context, actor syntax.DID, pull *models.Pull) { 383 + l := log.FromContext(ctx) 384 + 405 385 // Get repo details 406 386 repo, err := db.GetRepo(n.db, orm.FilterEq("at_uri", string(pull.RepoAt))) 407 387 if err != nil { 408 - log.Printf("NewPullState: failed to get repos: %v", err) 388 + l.Error("failed to get repos", "err", err) 409 389 return 410 390 } 411 391 412 392 collaborators, err := db.GetCollaborators(n.db, orm.FilterEq("repo_at", repo.RepoAt())) 413 393 if err != nil { 414 - log.Printf("failed to fetch collaborators: %v", err) 394 + l.Error("failed to fetch collaborators", "err", err) 415 395 return 416 396 } 417 397 ··· 441 417 case models.PullMerged: 442 418 eventType = models.NotificationTypePullMerged 443 419 default: 444 - log.Println("NewPullState: unexpected new PR state:", pull.State) 420 + l.Error("unexpected new PR state", "state", pull.State) 445 421 return 446 422 } 447 423 p := int64(pull.ID) 448 424 pullId := &p 449 425 450 426 n.notifyEvent( 427 + ctx, 451 428 actor, 452 429 recipients, 453 430 eventType, ··· 461 436 } 462 437 463 438 func (n *databaseNotifier) notifyEvent( 439 + ctx context.Context, 464 440 actorDid syntax.DID, 465 441 recipients sets.Set[syntax.DID], 466 442 eventType models.NotificationType, ··· 471 445 issueId *int64, 472 446 pullId *int64, 473 447 ) { 448 + l := log.FromContext(ctx) 449 + 474 450 // if the user is attempting to mention >maxMentions users, this is probably spam, do not mention anybody 475 451 if eventType == models.NotificationTypeUserMentioned && recipients.Len() > maxMentions { 476 452 return ··· 522 494 } 523 495 524 496 if err := db.CreateNotification(tx, notif); err != nil { 525 - log.Printf("notifyEvent: failed to create notification for %s: %v", recipientDid, err) 497 + l.Error("failed to create notification", "recipientDid", recipientDid, "err", err) 526 498 } 527 499 } 528 500
+105
appview/notify/logging_notifier.go
··· 1 + package notify 2 + 3 + import ( 4 + "context" 5 + "log/slog" 6 + 7 + "tangled.org/core/appview/models" 8 + tlog "tangled.org/core/log" 9 + 10 + "github.com/bluesky-social/indigo/atproto/syntax" 11 + ) 12 + 13 + type loggingNotifier struct { 14 + inner Notifier 15 + logger *slog.Logger 16 + } 17 + 18 + func NewLoggingNotifier(inner Notifier, logger *slog.Logger) Notifier { 19 + return &loggingNotifier{ 20 + inner, 21 + logger, 22 + } 23 + } 24 + 25 + var _ Notifier = &loggingNotifier{} 26 + 27 + func (l *loggingNotifier) NewRepo(ctx context.Context, repo *models.Repo) { 28 + ctx = tlog.IntoContext(ctx, tlog.SubLogger(l.logger, "NewRepo")) 29 + l.inner.NewRepo(ctx, repo) 30 + } 31 + 32 + func (l *loggingNotifier) NewStar(ctx context.Context, star *models.Star) { 33 + ctx = tlog.IntoContext(ctx, tlog.SubLogger(l.logger, "NewStar")) 34 + l.inner.NewStar(ctx, star) 35 + } 36 + 37 + func (l *loggingNotifier) DeleteStar(ctx context.Context, star *models.Star) { 38 + ctx = tlog.IntoContext(ctx, tlog.SubLogger(l.logger, "DeleteStar")) 39 + l.inner.DeleteStar(ctx, star) 40 + } 41 + 42 + func (l *loggingNotifier) NewIssue(ctx context.Context, issue *models.Issue, mentions []syntax.DID) { 43 + ctx = tlog.IntoContext(ctx, tlog.SubLogger(l.logger, "NewIssue")) 44 + l.inner.NewIssue(ctx, issue, mentions) 45 + } 46 + 47 + func (l *loggingNotifier) NewIssueComment(ctx context.Context, comment *models.IssueComment, mentions []syntax.DID) { 48 + ctx = tlog.IntoContext(ctx, tlog.SubLogger(l.logger, "NewIssueComment")) 49 + l.inner.NewIssueComment(ctx, comment, mentions) 50 + } 51 + 52 + func (l *loggingNotifier) NewIssueState(ctx context.Context, actor syntax.DID, issue *models.Issue) { 53 + ctx = tlog.IntoContext(ctx, tlog.SubLogger(l.logger, "NewIssueState")) 54 + l.inner.NewIssueState(ctx, actor, issue) 55 + } 56 + 57 + func (l *loggingNotifier) DeleteIssue(ctx context.Context, issue *models.Issue) { 58 + ctx = tlog.IntoContext(ctx, tlog.SubLogger(l.logger, "DeleteIssue")) 59 + l.inner.DeleteIssue(ctx, issue) 60 + } 61 + 62 + func (l *loggingNotifier) NewFollow(ctx context.Context, follow *models.Follow) { 63 + ctx = tlog.IntoContext(ctx, tlog.SubLogger(l.logger, "NewFollow")) 64 + l.inner.NewFollow(ctx, follow) 65 + } 66 + 67 + func (l *loggingNotifier) DeleteFollow(ctx context.Context, follow *models.Follow) { 68 + ctx = tlog.IntoContext(ctx, tlog.SubLogger(l.logger, "DeleteFollow")) 69 + l.inner.DeleteFollow(ctx, follow) 70 + } 71 + 72 + func (l *loggingNotifier) NewPull(ctx context.Context, pull *models.Pull) { 73 + ctx = tlog.IntoContext(ctx, tlog.SubLogger(l.logger, "NewPull")) 74 + l.inner.NewPull(ctx, pull) 75 + } 76 + 77 + func (l *loggingNotifier) NewPullComment(ctx context.Context, comment *models.PullComment, mentions []syntax.DID) { 78 + ctx = tlog.IntoContext(ctx, tlog.SubLogger(l.logger, "NewPullComment")) 79 + l.inner.NewPullComment(ctx, comment, mentions) 80 + } 81 + 82 + func (l *loggingNotifier) NewPullState(ctx context.Context, actor syntax.DID, pull *models.Pull) { 83 + ctx = tlog.IntoContext(ctx, tlog.SubLogger(l.logger, "NewPullState")) 84 + l.inner.NewPullState(ctx, actor, pull) 85 + } 86 + 87 + func (l *loggingNotifier) UpdateProfile(ctx context.Context, profile *models.Profile) { 88 + ctx = tlog.IntoContext(ctx, tlog.SubLogger(l.logger, "UpdateProfile")) 89 + l.inner.UpdateProfile(ctx, profile) 90 + } 91 + 92 + func (l *loggingNotifier) NewString(ctx context.Context, s *models.String) { 93 + ctx = tlog.IntoContext(ctx, tlog.SubLogger(l.logger, "NewString")) 94 + l.inner.NewString(ctx, s) 95 + } 96 + 97 + func (l *loggingNotifier) EditString(ctx context.Context, s *models.String) { 98 + ctx = tlog.IntoContext(ctx, tlog.SubLogger(l.logger, "EditString")) 99 + l.inner.EditString(ctx, s) 100 + } 101 + 102 + func (l *loggingNotifier) DeleteString(ctx context.Context, did, rkey string) { 103 + ctx = tlog.IntoContext(ctx, tlog.SubLogger(l.logger, "DeleteString")) 104 + l.inner.DeleteString(ctx, did, rkey) 105 + }
+2 -1
appview/state/state.go
··· 173 173 notifiers = append(notifiers, phnotify.NewPosthogNotifier(posthog)) 174 174 } 175 175 notifiers = append(notifiers, indexer) 176 - notifier := notify.NewMergedNotifier(notifiers, tlog.SubLogger(logger, "notify")) 176 + notifier := notify.NewMergedNotifier(notifiers) 177 + notifier = notify.NewLoggingNotifier(notifier, tlog.SubLogger(logger, "notify")) 177 178 178 179 state := &State{ 179 180 d,