notifications.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package notifications
  2. import (
  3. "fmt"
  4. "io"
  5. "log/slog"
  6. "net/http"
  7. neturl "net/url"
  8. "os"
  9. "path/filepath"
  10. "time"
  11. "github.com/ayn2op/discordo/internal/config"
  12. "github.com/ayn2op/discordo/internal/consts"
  13. "github.com/diamondburned/arikawa/v3/discord"
  14. "github.com/diamondburned/arikawa/v3/gateway"
  15. "github.com/diamondburned/ningen/v3"
  16. )
  17. func Notify(state *ningen.State, message *gateway.MessageCreateEvent, cfg *config.Config) error {
  18. if !cfg.Notifications.Enabled || cfg.Status == discord.DoNotDisturbStatus {
  19. return nil
  20. }
  21. mentions := state.MessageMentions(&message.Message)
  22. if mentions == 0 {
  23. return nil
  24. }
  25. // Handle sent files
  26. content := message.Content
  27. if message.Content == "" && len(message.Attachments) > 0 {
  28. content = "Uploaded " + message.Attachments[0].Filename
  29. }
  30. if content == "" {
  31. return nil
  32. }
  33. title := message.Author.DisplayOrUsername()
  34. channel, err := state.Cabinet.Channel(message.ChannelID)
  35. if err != nil {
  36. return fmt.Errorf("failed to get channel from state: %w", err)
  37. }
  38. if channel.GuildID.IsValid() {
  39. guild, err := state.Cabinet.Guild(channel.GuildID)
  40. if err != nil {
  41. return fmt.Errorf("failed to get guild from state: %w", err)
  42. }
  43. if member := message.Member; member != nil && member.Nick != "" {
  44. title = member.Nick
  45. }
  46. title += " (#" + channel.Name + ", " + guild.Name + ")"
  47. }
  48. hash := message.Author.Avatar
  49. if hash == "" {
  50. hash = "default"
  51. }
  52. imagePath, err := getCachedProfileImage(hash, message.Author.AvatarURLWithType(discord.PNGImage))
  53. if err != nil {
  54. slog.Warn("failed to get profile image from cache for notification", "err", err, "hash", hash)
  55. }
  56. shouldChime := cfg.Notifications.Sound.Enabled && (!cfg.Notifications.Sound.OnlyOnPing || mentions.Has(ningen.MessageMentions|ningen.MessageNotifies))
  57. if err := sendDesktopNotification(title, content, imagePath, shouldChime, cfg.Notifications.Duration); err != nil {
  58. return err
  59. }
  60. return nil
  61. }
  62. var avatarHTTPClient = &http.Client{Timeout: 10 * time.Second}
  63. func getCachedProfileImage(avatarHash discord.Hash, url string) (string, error) {
  64. dir := filepath.Join(consts.CacheDir(), "avatars")
  65. if err := os.MkdirAll(dir, 0700); err != nil {
  66. return "", err
  67. }
  68. safeHash := filepath.Base(string(avatarHash))
  69. path := filepath.Join(dir, safeHash+".png")
  70. if _, err := os.Stat(path); err == nil {
  71. return path, nil
  72. }
  73. parsed, err := neturl.Parse(url)
  74. if err != nil || parsed.Scheme != "https" {
  75. return "", fmt.Errorf("refusing non-HTTPS avatar URL")
  76. }
  77. resp, err := avatarHTTPClient.Get(url)
  78. if err != nil {
  79. return "", err
  80. }
  81. defer resp.Body.Close()
  82. if resp.StatusCode != http.StatusOK {
  83. return "", fmt.Errorf("unexpected status %d fetching avatar", resp.StatusCode)
  84. }
  85. file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
  86. if err != nil {
  87. return "", err
  88. }
  89. defer file.Close()
  90. if _, err := io.Copy(file, io.LimitReader(resp.Body, 10*1024*1024)); err != nil {
  91. return "", err
  92. }
  93. return path, nil
  94. }