From 971ccca0d3d39549a2206cfe0586c30b1b56b945 Mon Sep 17 00:00:00 2001 From: milarin Date: Thu, 2 Feb 2023 12:36:23 +0100 Subject: [PATCH] remove duplicate anime episodes from telegram report message --- telegram.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/telegram.go b/telegram.go index 507e51d..5ad7ad7 100644 --- a/telegram.go +++ b/telegram.go @@ -102,6 +102,8 @@ func sendTelegramAnimeEpMessage(animeEpisodes []model.AnimeEpisode) { return } + animeEpisodes = RemoveDuplicates(animeEpisodes) + b := new(strings.Builder) if err := TelegramOrganizeMessagePattern.Execute(b, animeEpisodes); err != nil { adverr.Println(adverr.Wrap("could not send telegram message", err)) @@ -110,6 +112,18 @@ func sendTelegramAnimeEpMessage(animeEpisodes []model.AnimeEpisode) { SendTelegramMessage(b.String()) } +func RemoveDuplicates(animeEpisodes []model.AnimeEpisode) []model.AnimeEpisode { + mapperFunc := func(animeEp model.AnimeEpisode) (model.Pair[anilist.MediaID, int], model.AnimeEpisode) { + return model.Pair[anilist.MediaID, int]{First: animeEp.Anime.ID, Second: animeEp.Episode}, animeEp + } + + unmapperFunc := func(key model.Pair[anilist.MediaID, int], value model.AnimeEpisode) model.AnimeEpisode { + return value + } + + return slices.OfMap(slices.ToMap(animeEpisodes, mapperFunc), unmapperFunc) +} + func CheckSendConditions(animeEp model.AnimeEpisode) (bool, error) { listEntry, err := GetListEntry(animeEp.Anime) if err != nil {