-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* limit tracking to public pastes * address TS errors
- Loading branch information
querwurzel
committed
Apr 29, 2024
1 parent
c2df20c
commit 9320f61
Showing
25 changed files
with
549 additions
and
373 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
backend/src/main/java/com/github/binpastes/paste/application/PasteViewService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package com.github.binpastes.paste.application; | ||
|
||
import com.github.binpastes.paste.api.model.CreateCmd; | ||
import com.github.binpastes.paste.api.model.DetailView; | ||
import com.github.binpastes.paste.api.model.ListView; | ||
import com.github.binpastes.paste.api.model.ListView.ListItemView; | ||
import com.github.binpastes.paste.api.model.SearchView; | ||
import com.github.binpastes.paste.api.model.SearchView.SearchItemView; | ||
import com.github.binpastes.paste.application.tracking.TrackingService; | ||
import com.github.binpastes.paste.domain.PasteService; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Service | ||
public class PasteViewService { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(PasteViewService.class); | ||
|
||
private final PasteService pasteService; | ||
private final TrackingService trackingService; | ||
|
||
@Autowired | ||
public PasteViewService(PasteService pasteService, TrackingService trackingService) { | ||
this.pasteService = pasteService; | ||
this.trackingService = trackingService; | ||
} | ||
|
||
public Mono<DetailView> viewPaste(String id, String remoteAddress) { | ||
return pasteService.find(id) | ||
.doOnNext(paste -> { | ||
if (paste.isPublic()) { | ||
trackingService.trackView(paste.getId()); | ||
} | ||
}) | ||
.map(paste -> { | ||
if (paste.isOneTime()) { | ||
return new DetailView( | ||
paste.getId(), | ||
null, | ||
null, | ||
0, | ||
paste.isPublic(), | ||
paste.isErasable(remoteAddress), | ||
paste.isEncrypted(), | ||
paste.isOneTime(), | ||
paste.isPermanent(), | ||
paste.getDateCreated(), | ||
paste.getDateOfExpiry(), | ||
paste.getLastViewed(), | ||
paste.getViews() | ||
); | ||
} else { | ||
return DetailView.of(paste, remoteAddress); | ||
} | ||
}); | ||
} | ||
|
||
public Mono<DetailView> viewOneTimePaste(String id) { | ||
var now = LocalDateTime.now(); | ||
return pasteService.findAndBurn(id) | ||
.map(paste -> new DetailView( | ||
paste.getId(), | ||
paste.getTitle(), | ||
paste.getContent(), | ||
paste.getContent().getBytes().length, | ||
paste.isPublic(), | ||
false, // paste just burnt | ||
paste.isEncrypted(), | ||
paste.isOneTime(), | ||
paste.isPermanent(), | ||
paste.getDateCreated(), | ||
paste.getDateOfExpiry(), | ||
paste.getLastViewed(), | ||
paste.getViews() | ||
)); | ||
} | ||
|
||
public Mono<ListView> viewAllPastes() { | ||
return pasteService.findAll() | ||
.map(ListItemView::of) | ||
.collectList() | ||
.map(ListView::of); | ||
} | ||
|
||
public Mono<SearchView> searchByFullText(String term) { | ||
return pasteService.findByFullText(term) | ||
.map(paste -> SearchItemView.of(paste, term)) | ||
.collectList() | ||
.map(SearchView::of) | ||
.doOnSuccess(searchView -> log.info("Found {} pastes searching for: {}", searchView.pastes().size(), term)); | ||
} | ||
|
||
public Mono<DetailView> createPaste(CreateCmd cmd, String remoteAddress) { | ||
return pasteService.create( | ||
cmd.title(), | ||
cmd.content(), | ||
cmd.dateOfExpiry(), | ||
cmd.isEncrypted(), | ||
cmd.pasteExposure(), | ||
remoteAddress | ||
).map(paste -> { | ||
if (paste.isOneTime()) { | ||
return new DetailView( | ||
paste.getId(), | ||
null, | ||
null, | ||
0, | ||
paste.isPublic(), | ||
paste.isErasable(remoteAddress), | ||
paste.isEncrypted(), | ||
paste.isOneTime(), | ||
paste.isPermanent(), | ||
paste.getDateCreated(), | ||
paste.getDateOfExpiry(), | ||
paste.getLastViewed(), | ||
paste.getViews() | ||
); | ||
} else { | ||
return DetailView.of(paste, remoteAddress); | ||
} | ||
}); | ||
} | ||
|
||
public void requestDeletion(String id, String remoteAddress) { | ||
pasteService.requestDeletion(id, remoteAddress); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.