|
9 | 9 | "google.golang.org/protobuf/proto" |
10 | 10 |
|
11 | 11 | chatpb "github.com/code-payments/code-protobuf-api/generated/go/chat/v1" |
| 12 | + chatv2pb "github.com/code-payments/code-protobuf-api/generated/go/chat/v2" |
12 | 13 | commonpb "github.com/code-payments/code-protobuf-api/generated/go/common/v1" |
13 | 14 |
|
14 | 15 | chat_util "github.com/code-payments/code-server/pkg/code/chat" |
@@ -412,3 +413,147 @@ func SendChatMessagePushNotification( |
412 | 413 | } |
413 | 414 | return nil |
414 | 415 | } |
| 416 | + |
| 417 | +func SendChatMessagePushNotificationV2( |
| 418 | + ctx context.Context, |
| 419 | + data code_data.Provider, |
| 420 | + pusher push_lib.Provider, |
| 421 | + chatTitle string, |
| 422 | + owner *common.Account, |
| 423 | + chatMessage *chatv2pb.ChatMessage, |
| 424 | +) error { |
| 425 | + log := logrus.StandardLogger().WithFields(logrus.Fields{ |
| 426 | + "method": "SendChatMessagePushNotificationV2", |
| 427 | + "owner": owner.PublicKey().ToBase58(), |
| 428 | + "chat": chatTitle, |
| 429 | + }) |
| 430 | + |
| 431 | + // Best-effort try to update the badge count before pushing message content |
| 432 | + // |
| 433 | + // Note: Only chat messages generate badge counts |
| 434 | + err := UpdateBadgeCount(ctx, data, pusher, owner) |
| 435 | + if err != nil { |
| 436 | + log.WithError(err).Warn("failure updating badge count on device") |
| 437 | + } |
| 438 | + |
| 439 | + locale, err := data.GetUserLocale(ctx, owner.PublicKey().ToBase58()) |
| 440 | + if err != nil { |
| 441 | + log.WithError(err).Warn("failure getting user locale") |
| 442 | + return err |
| 443 | + } |
| 444 | + |
| 445 | + var localizedPushTitle string |
| 446 | + |
| 447 | + chatProperties, ok := chat_util.InternalChatProperties[chatTitle] |
| 448 | + if ok { |
| 449 | + localized, err := localization.Localize(locale, chatProperties.TitleLocalizationKey) |
| 450 | + if err != nil { |
| 451 | + return nil |
| 452 | + } |
| 453 | + localizedPushTitle = localized |
| 454 | + } else { |
| 455 | + domainDisplayName, err := thirdparty.GetDomainDisplayName(chatTitle) |
| 456 | + if err == nil { |
| 457 | + localizedPushTitle = domainDisplayName |
| 458 | + } else { |
| 459 | + return nil |
| 460 | + } |
| 461 | + } |
| 462 | + |
| 463 | + var anyErrorPushingContent bool |
| 464 | + for _, content := range chatMessage.Content { |
| 465 | + var contentToPush *chatv2pb.Content |
| 466 | + switch typedContent := content.Type.(type) { |
| 467 | + case *chatv2pb.Content_Localized: |
| 468 | + localizedPushBody, err := localization.Localize(locale, typedContent.Localized.KeyOrText) |
| 469 | + if err != nil { |
| 470 | + continue |
| 471 | + } |
| 472 | + |
| 473 | + contentToPush = &chatv2pb.Content{ |
| 474 | + Type: &chatv2pb.Content_Localized{ |
| 475 | + Localized: &chatv2pb.LocalizedContent{ |
| 476 | + KeyOrText: localizedPushBody, |
| 477 | + }, |
| 478 | + }, |
| 479 | + } |
| 480 | + case *chatv2pb.Content_ExchangeData: |
| 481 | + var currencyCode currency_lib.Code |
| 482 | + var nativeAmount float64 |
| 483 | + if typedContent.ExchangeData.GetExact() != nil { |
| 484 | + exchangeData := typedContent.ExchangeData.GetExact() |
| 485 | + currencyCode = currency_lib.Code(exchangeData.Currency) |
| 486 | + nativeAmount = exchangeData.NativeAmount |
| 487 | + } else { |
| 488 | + exchangeData := typedContent.ExchangeData.GetPartial() |
| 489 | + currencyCode = currency_lib.Code(exchangeData.Currency) |
| 490 | + nativeAmount = exchangeData.NativeAmount |
| 491 | + } |
| 492 | + |
| 493 | + localizedPushBody, err := localization.LocalizeFiatWithVerb( |
| 494 | + locale, |
| 495 | + chatpb.ExchangeDataContent_Verb(typedContent.ExchangeData.Verb), |
| 496 | + currencyCode, |
| 497 | + nativeAmount, |
| 498 | + true, |
| 499 | + ) |
| 500 | + if err != nil { |
| 501 | + continue |
| 502 | + } |
| 503 | + |
| 504 | + contentToPush = &chatv2pb.Content{ |
| 505 | + Type: &chatv2pb.Content_Localized{ |
| 506 | + Localized: &chatv2pb.LocalizedContent{ |
| 507 | + KeyOrText: localizedPushBody, |
| 508 | + }, |
| 509 | + }, |
| 510 | + } |
| 511 | + case *chatv2pb.Content_NaclBox, *chatv2pb.Content_Text: |
| 512 | + contentToPush = content |
| 513 | + case *chatv2pb.Content_ThankYou: |
| 514 | + contentToPush = &chatv2pb.Content{ |
| 515 | + Type: &chatv2pb.Content_Localized{ |
| 516 | + Localized: &chatv2pb.LocalizedContent{ |
| 517 | + // todo: localize this |
| 518 | + KeyOrText: "🙏 They thanked you for their tip", |
| 519 | + }, |
| 520 | + }, |
| 521 | + } |
| 522 | + } |
| 523 | + |
| 524 | + if contentToPush == nil { |
| 525 | + continue |
| 526 | + } |
| 527 | + |
| 528 | + marshalledContent, err := proto.Marshal(contentToPush) |
| 529 | + if err != nil { |
| 530 | + log.WithError(err).Warn("failure marshalling chat content") |
| 531 | + return err |
| 532 | + } |
| 533 | + |
| 534 | + kvs := map[string]string{ |
| 535 | + "chat_title": localizedPushTitle, |
| 536 | + "message_content": base64.StdEncoding.EncodeToString(marshalledContent), |
| 537 | + } |
| 538 | + |
| 539 | + err = sendMutableNotificationToOwner( |
| 540 | + ctx, |
| 541 | + data, |
| 542 | + pusher, |
| 543 | + owner, |
| 544 | + chatMessageDataPush, |
| 545 | + chatTitle, |
| 546 | + kvs, |
| 547 | + ) |
| 548 | + if err != nil { |
| 549 | + anyErrorPushingContent = true |
| 550 | + log.WithError(err).Warn("failure sending data push notification") |
| 551 | + } |
| 552 | + } |
| 553 | + |
| 554 | + if anyErrorPushingContent { |
| 555 | + return errors.New("at least one piece of content failed to push") |
| 556 | + } |
| 557 | + |
| 558 | + return nil |
| 559 | +} |
0 commit comments