Skip to content

Commit

Permalink
feat(sandside): #178 avoid relink standalone news if exits in user su…
Browse files Browse the repository at this point in the history
…bscriptions
  • Loading branch information
Marthym committed Nov 17, 2023
1 parent aba9108 commit 5113eb8
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import fr.ght1pc9kc.baywatch.indexer.domain.model.IndexableFeedEntry;
import fr.ght1pc9kc.baywatch.indexer.domain.ports.IndexableDataPort;
import fr.ght1pc9kc.baywatch.techwatch.api.SystemMaintenanceService;
import fr.ght1pc9kc.baywatch.techwatch.api.model.News;
import fr.ght1pc9kc.juery.api.PageRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
Expand All @@ -24,6 +25,7 @@ public Flux<IndexableFeed> listFeed() {
@Override
public Flux<IndexableFeedEntry> listEntries(PageRequest pg) {
return systemMaintenanceService.newsList(pg)
.map(News::getRaw)
.map(mapper::getIndexableFromEntry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import fr.ght1pc9kc.baywatch.techwatch.api.model.News;
import fr.ght1pc9kc.baywatch.techwatch.api.model.RawNews;
import fr.ght1pc9kc.baywatch.techwatch.api.model.State;
import fr.ght1pc9kc.juery.api.Criteria;
import fr.ght1pc9kc.juery.api.PageRequest;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
Expand All @@ -33,6 +35,9 @@
import java.util.List;
import java.util.Set;

import static fr.ght1pc9kc.baywatch.common.api.model.EntitiesProperties.ID;
import static fr.ght1pc9kc.baywatch.common.api.model.EntitiesProperties.USER_ID;

@RequiredArgsConstructor
public class ScrapEnrichmentServiceImpl implements ScrapEnrichmentService {
private static final String OPERATION_NOT_PERMITTED = "Operation not permitted !";
Expand Down Expand Up @@ -60,8 +65,10 @@ public Mono<Void> scrapSingleNews(URI uri) {
return authFacade.getConnectedUser()
.transformDeferredContextual((original, context) -> original.doOnNext(user -> buildStandaloneNews(uri)
.flatMap(this::applyNewsFilters)
.flatMap(t -> Mono.fromCallable(t::get))
.flatMap(this::saveAndShare)
.flatMap(t -> Mono.fromCallable(t::get)
.filterWhen(this::notAlreadyExists)
.flatMap(this::saveAndShare)
.switchIfEmpty(Mono.fromCallable(t::get)))
.contextWrite(context)
.subscribeOn(scraperScheduler)
.subscribe(n -> notifyService.send(user.id, EventType.USER_NOTIFICATION,
Expand Down Expand Up @@ -109,6 +116,21 @@ public Mono<Try<News>> applyNewsFilters(News news) {
});
}

private Mono<Boolean> notAlreadyExists(News news) {
return authFacade.getConnectedUser()
.filter(u -> RoleUtils.hasRole(u.self, Role.USER))
.switchIfEmpty(Mono.error(() -> new UnauthorizedException(OPERATION_NOT_PERMITTED)))

.flatMapMany(u ->
systemMaintenanceService.newsList(PageRequest.one(Criteria.property(ID).eq(news.id())))
.map(News::getFeeds)
.flatMap(feeds -> systemMaintenanceService.feedList(PageRequest.all(Criteria.property(ID).in(feeds)
.and(Criteria.property(USER_ID).eq(u.id)))))
.contextWrite(AuthenticationFacade.withSystemAuthentication()))

.hasElements().map(b -> !b);
}

@Override
public Mono<News> saveAndShare(News news) {
return authFacade.getConnectedUser()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ public interface SystemMaintenanceService {
Mono<Integer> feedDelete(Collection<String> toDelete);

/**
* List {@link RawNews} for connected user or {@link RawNews} for anonymous.
* List {@link News} for connected user or {@link News} for anonymous.
* For Anonymous, {@link State} is always
* {@link State#NONE}
*
* @param pageRequest {@see PageRequest}
* @return The {@link RawNews} for connected user or {@link RawNews} for anonymous
* @return The {@link News} for connected user or {@link News} for anonymous
*/
Flux<RawNews> newsList(PageRequest pageRequest);
Flux<News> newsList(PageRequest pageRequest);

Flux<String> newsIdList(PageRequest pageRequest);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.util.Set;

/**
* The News element customized with state and {@link RawFeed#id}
* The News element customized with state and {@link WebFeed#reference()}
*/
@With
@Value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,11 @@ public Mono<Integer> feedDelete(Collection<String> toDelete) {
}

@Override
public Flux<RawNews> newsList(PageRequest pageRequest) {
public Flux<News> newsList(PageRequest pageRequest) {
return authFacade.getConnectedUser()
.filter(user -> RoleUtils.hasRole(user.self, Role.SYSTEM))
.switchIfEmpty(Mono.error(() -> new UnauthorizedException(EXCEPTION_MESSAGE)))
.flatMapMany(u -> newsRepository.list(QueryContext.from(pageRequest)))
.map(News::getRaw);
.flatMapMany(u -> newsRepository.list(QueryContext.from(pageRequest)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@
import fr.ght1pc9kc.baywatch.scraper.api.ScrapEnrichmentService;
import fr.ght1pc9kc.baywatch.security.api.AuthenticationFacade;
import fr.ght1pc9kc.baywatch.techwatch.api.SystemMaintenanceService;
import fr.ght1pc9kc.baywatch.tests.samples.FeedSamples;
import fr.ght1pc9kc.baywatch.tests.samples.NewsSamples;
import fr.ght1pc9kc.baywatch.tests.samples.UserSamples;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import reactor.test.StepVerifier;

import java.util.List;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyCollection;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand All @@ -36,9 +40,23 @@ void setUp() {

@Test
void should_scrap_single_news() {
when(mockSystemMaintenance.newsList(any()))
.thenReturn(Flux.just(NewsSamples.A_NEW_HOPE));
when(mockSystemMaintenance.feedList(any())).thenReturn(Flux.empty());
StepVerifier.create(tested.scrapSingleNews(NewsSamples.MAY_THE_FORCE.getRaw().link()))
.verifyComplete();

verify(mockSystemMaintenance).newsLoad(anyCollection());
}

@Test
void should_scrap_existing_single_news() {
when(mockSystemMaintenance.newsList(any()))
.thenReturn(Flux.just(NewsSamples.MAY_THE_FORCE));
StepVerifier.create(tested.scrapSingleNews(NewsSamples.MAY_THE_FORCE.getRaw().link()))
.verifyComplete();

verify(mockSystemMaintenance, never()).newsLoad(anyCollection());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import fr.ght1pc9kc.baywatch.techwatch.api.NewsService;
import fr.ght1pc9kc.baywatch.techwatch.api.SystemMaintenanceService;
import fr.ght1pc9kc.baywatch.techwatch.api.model.News;
import fr.ght1pc9kc.baywatch.tests.samples.FeedSamples;
import fr.ght1pc9kc.baywatch.tests.samples.NewsSamples;
import org.assertj.core.api.Assertions;
Expand Down Expand Up @@ -30,7 +29,7 @@ class DeleteOrphanFeedHandlerTest {
void setUp() {
when(systemMaintenanceMock.feedList(any())).thenReturn(Flux.fromIterable(FeedSamples.SAMPLES));
when(systemMaintenanceMock.feedDelete(anyCollection())).thenReturn(Mono.just(2));
when(systemMaintenanceMock.newsList(any())).thenReturn(Flux.fromIterable(NewsSamples.SAMPLES).map(News::getRaw));
when(systemMaintenanceMock.newsList(any())).thenReturn(Flux.fromIterable(NewsSamples.SAMPLES));
when(systemMaintenanceMock.newsDelete(anyCollection())).thenReturn(Mono.just(2));

NewsService newsServiceMock = mock(NewsService.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ void should_list_news_for_authenticated_user() {
StepVerifier.create(tested.newsList(PageRequest.all(filter)))
.assertNext(actual -> {
Assertions.assertThat(actual).isNotNull();
Assertions.assertThat(actual).isEqualTo(MAY_THE_FORCE.getRaw());
Assertions.assertThat(actual).isEqualTo(MAY_THE_FORCE);
})
.expectNextCount(1)
.verifyComplete();
Expand Down

0 comments on commit 5113eb8

Please sign in to comment.