Skip to content
This repository has been archived by the owner on Mar 28, 2024. It is now read-only.

Add profile invalidation for episodes/anime updates #212

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/main/kotlin/fr/ziedelth/controllers/AnimeController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package fr.ziedelth.controllers
import com.google.inject.Inject
import fr.ziedelth.entities.Anime
import fr.ziedelth.entities.isNullOrNotValid
import fr.ziedelth.repositories.AnimeRepository
import fr.ziedelth.repositories.CountryRepository
import fr.ziedelth.repositories.EpisodeRepository
import fr.ziedelth.repositories.SimulcastRepository
import fr.ziedelth.repositories.*
import fr.ziedelth.services.AnimeService
import fr.ziedelth.services.EpisodeService
import fr.ziedelth.services.ProfileService
import fr.ziedelth.utils.ImageCache
import fr.ziedelth.utils.Logger
import fr.ziedelth.utils.routes.Authorized
Expand Down Expand Up @@ -43,6 +41,12 @@ class AnimeController : AttachmentController<Anime>("/animes") {
@Inject
private lateinit var simulcastRepository: SimulcastRepository

@Inject
private lateinit var profileRepository: ProfileRepository

@Inject
private lateinit var profileService: ProfileService

@Path("/country/{country}/search/hash/{hash}")
@Get
private fun searchByCountryAndHash(country: String, hash: String): Response {
Expand Down Expand Up @@ -149,6 +153,10 @@ class AnimeController : AttachmentController<Anime>("/animes") {
savedAnime = animeRepository.save(savedAnime)
animeService.invalidateAll()
episodeService.invalidateAll()

val profiles = profileRepository.findProfilesWithAnime(anime.uuid)
profiles.forEach { profileService.invalidateProfile(it) }

return Response.ok(savedAnime)
}

Expand Down
15 changes: 15 additions & 0 deletions src/main/kotlin/fr/ziedelth/controllers/EpisodeController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import fr.ziedelth.events.EpisodesReleaseEvent
import fr.ziedelth.repositories.*
import fr.ziedelth.services.AnimeService
import fr.ziedelth.services.EpisodeService
import fr.ziedelth.services.ProfileService
import fr.ziedelth.services.SimulcastService
import fr.ziedelth.utils.CalendarConverter
import fr.ziedelth.utils.ImageCache
Expand Down Expand Up @@ -52,6 +53,12 @@ class EpisodeController : AttachmentController<Episode>("/episodes") {
@Inject
private lateinit var episodeService: EpisodeService

@Inject
private lateinit var profileRepository: ProfileRepository

@Inject
private lateinit var profileService: ProfileService

@Path("/country/{country}/page/{page}/limit/{limit}")
@Get
private fun paginationByCountry(country: String, page: Int, limit: Int): Response {
Expand Down Expand Up @@ -147,6 +154,10 @@ class EpisodeController : AttachmentController<Episode>("/episodes") {
animeService.invalidateAll()
simulcastService.invalidateAll()

val animes = savedEpisodes.mapNotNull { it.anime?.uuid }.distinct()
val profiles = animes.flatMap { profileRepository.findProfilesWithAnime(it) }.distinct()
profiles.forEach { profileService.invalidateProfile(it) }

if (savedEpisodes.size <= 5) {
Thread {
PluginManager.callEvent(EpisodesReleaseEvent(savedEpisodes))
Expand Down Expand Up @@ -189,6 +200,10 @@ class EpisodeController : AttachmentController<Episode>("/episodes") {

savedEpisode = episodeRepository.save(savedEpisode)
episodeService.invalidateAll()

val profiles = profileRepository.findProfilesWithAnime(savedEpisode.anime!!.uuid)
profiles.forEach { profileService.invalidateProfile(it) }

return Response.ok(savedEpisode)
}
}
13 changes: 13 additions & 0 deletions src/main/kotlin/fr/ziedelth/repositories/ProfileRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ class ProfileRepository : AbstractRepository<Profile>() {
}
}

fun findProfilesWithAnime(animeUuid: UUID): List<UUID> {
return database.inReadOnlyTransaction {
it.createQuery(
"""
SELECT DISTINCT pa.profile.uuid
FROM ProfileAnime pa
WHERE pa.anime.uuid = :animeUuid
""".trimIndent(),
UUID::class.java
).setParameter("animeUuid", animeUuid).resultList
}
}

/**
* Saves the provided filter data and returns the saved profile.
*
Expand Down