notifications.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package notifications
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "log/slog"
  7. "net/http"
  8. "os"
  9. "path/filepath"
  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 HandleIncomingMessage(state *ningen.State, msg *gateway.MessageCreateEvent, cfg *config.Config) error {
  17. // Only display notification if enabled and unmuted
  18. if !cfg.Notifications.Enabled || state.MessageMentions(&msg.Message) == 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. member, err := state.Cabinet.Member(guild.ID, msg.Author.ID)
  40. if err != nil {
  41. slog.Info("failed to get member from state", "err", err, "guild_id", channel.GuildID, "user_id", msg.Author.ID)
  42. } else {
  43. if member.Nick != "" {
  44. title = member.Nick
  45. }
  46. }
  47. title += " (#" + channel.Name + ", " + guild.Name + ")"
  48. }
  49. hash := msg.Author.Avatar
  50. if hash == "" {
  51. hash = "default"
  52. }
  53. imagePath, err := getCachedProfileImage(hash, msg.Author.AvatarURLWithType(discord.PNGImage))
  54. if err != nil {
  55. slog.Info("failed to get profile image from cache for notification", "err", err, "hash", hash)
  56. }
  57. isChannelDM := channel.Type == discord.DirectMessage || channel.Type == discord.GroupDM
  58. shouldChime := cfg.Notifications.Sound.Enabled && (!cfg.Notifications.Sound.OnlyOnPing || (isChannelDM || state.MessageMentions(&msg.Message) == 3))
  59. if err := sendDesktopNotification(title, content, imagePath, shouldChime, cfg.Notifications.Duration); err != nil {
  60. return err
  61. }
  62. return nil
  63. }
  64. func getCachedProfileImage(avatarHash discord.Hash, url string) (string, error) {
  65. path, err := os.UserCacheDir()
  66. if err != nil {
  67. return "", err
  68. }
  69. path = filepath.Join(path, consts.Name, "assets")
  70. if err := os.MkdirAll(path, os.ModePerm); err != nil {
  71. return "", err
  72. }
  73. path = filepath.Join(path, avatarHash+".png")
  74. if _, err := os.Stat(path); err == nil {
  75. return path, nil
  76. }
  77. image, err := os.Create(path)
  78. if err != nil {
  79. return "", err
  80. }
  81. defer image.Close()
  82. resp, err := http.Get(url)
  83. if err != nil {
  84. return "", err
  85. }
  86. defer resp.Body.Close()
  87. if _, err := io.Copy(image, resp.Body); err != nil {
  88. return "", err
  89. }
  90. return path, nil
  91. }