notifications.go 2.6 KB

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