notifications.go 2.7 KB

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