3030import io .papermc .fill .exception .BuildNotFoundException ;
3131import io .papermc .fill .exception .FamilyNotFoundException ;
3232import io .papermc .fill .exception .ProjectNotFoundException ;
33+ import io .papermc .fill .exception .SunsetException ;
3334import io .papermc .fill .exception .VersionNotFoundException ;
3435import io .papermc .fill .model .Build ;
3536import io .papermc .fill .model .BuildChannel ;
3637import io .papermc .fill .model .Commit ;
3738import io .papermc .fill .model .Download ;
38- import io .papermc .fill .model .Family ;
3939import io .papermc .fill .model .Keyed ;
40- import io .papermc .fill .model .Version ;
40+ import io .papermc .fill .model .Timestamped ;
4141import io .papermc .fill .model .response .v2 .BuildResponse ;
4242import io .papermc .fill .model .response .v2 .BuildsResponse ;
4343import io .papermc .fill .model .response .v2 .FamilyBuildsResponse ;
5151import io .papermc .fill .util .http .Responses ;
5252import io .swagger .v3 .oas .annotations .tags .Tag ;
5353import jakarta .validation .constraints .PositiveOrZero ;
54+ import java .time .Clock ;
5455import java .time .Duration ;
5556import java .util .List ;
5657import java .util .Map ;
@@ -79,21 +80,23 @@ public class Meta2Controller {
7980 private static final Duration CACHE_LENGTH_VERSION_BUILDS = Duration .ofMinutes (5 );
8081 private static final Duration CACHE_LENGTH_BUILD = Duration .ofDays (7 );
8182
83+ private final Clock clock ;
8284 private final ApplicationApiProperties properties ;
83-
8485 private final ProjectRepository projects ;
8586 private final FamilyRepository families ;
8687 private final VersionRepository versions ;
8788 private final BuildRepository builds ;
8889
8990 @ Autowired
9091 public Meta2Controller (
92+ final Clock clock ,
9193 final ApplicationApiProperties properties ,
9294 final ProjectRepository projects ,
9395 final FamilyRepository families ,
9496 final VersionRepository versions ,
9597 final BuildRepository builds
9698 ) {
99+ this .clock = clock ;
97100 this .properties = properties ;
98101 this .projects = projects ;
99102 this .families = families ;
@@ -104,6 +107,9 @@ public Meta2Controller(
104107 @ CrossOrigin (methods = RequestMethod .GET )
105108 @ GetMapping ("/v2/projects" )
106109 public ResponseEntity <?> getProjects () {
110+ if (this .isSunset ()) {
111+ throw new SunsetException ();
112+ }
107113 final List <ProjectEntity > projects = this .projects .findAll ();
108114 final ProjectsResponse response = new ProjectsResponse (Keyed .keysOf (projects ));
109115 return Responses .ok (response , Caching .publicShared (CACHE_LENGTH_PROJECTS ));
@@ -115,14 +121,17 @@ public ResponseEntity<?> getProject(
115121 @ PathVariable ("project" )
116122 final String projectKey
117123 ) {
124+ if (this .isSunset ()) {
125+ throw new SunsetException ();
126+ }
118127 final ProjectEntity project = this .projects .findByKey (projectKey ).orElseThrow (ProjectNotFoundException ::new );
119128 final List <FamilyEntity > families = this .families .findAllByProject (project )
120129 .filter (family -> family .createdAt ().isBefore (SharedConstants .API_V2_CUTOFF ))
121- .sorted (Family . COMPARATOR_CREATED_AT )
130+ .sorted (Timestamped . CREATED_AT_ASC )
122131 .toList ();
123132 final List <VersionEntity > versions = this .versions .findAllByProject (project )
124133 .filter (version -> version .createdAt ().isBefore (SharedConstants .API_V2_CUTOFF ))
125- .sorted (Version . COMPARATOR_CREATED_AT )
134+ .sorted (Timestamped . CREATED_AT_ASC )
126135 .toList ();
127136 final ProjectResponse response = new ProjectResponse (
128137 project .key (),
@@ -141,14 +150,17 @@ public ResponseEntity<?> getFamily(
141150 @ PathVariable ("family" )
142151 final String familyKey
143152 ) {
153+ if (this .isSunset ()) {
154+ throw new SunsetException ();
155+ }
144156 final ProjectEntity project = this .projects .findByKey (projectKey ).orElseThrow (ProjectNotFoundException ::new );
145157 final FamilyEntity family = this .families .findByProjectAndKey (project , familyKey ).orElseThrow (FamilyNotFoundException ::new );
146158 if (family .createdAt ().isAfter (SharedConstants .API_V2_CUTOFF )) {
147159 throw new FamilyNotFoundException ();
148160 }
149161 final List <VersionEntity > versions = this .versions .findAllByFamily (family )
150162 .filter (version -> version .createdAt ().isBefore (SharedConstants .API_V2_CUTOFF ))
151- .sorted (Version . COMPARATOR_CREATED_AT )
163+ .sorted (Timestamped . CREATED_AT_ASC )
152164 .toList ();
153165 final FamilyResponse response = new FamilyResponse (
154166 project .key (),
@@ -167,20 +179,23 @@ public ResponseEntity<?> getFamilyBuilds(
167179 @ PathVariable ("family" )
168180 final String familyKey
169181 ) {
182+ if (this .isSunset ()) {
183+ throw new SunsetException ();
184+ }
170185 final ProjectEntity project = this .projects .findByKey (projectKey ).orElseThrow (ProjectNotFoundException ::new );
171186 final FamilyEntity family = this .families .findByProjectAndKey (project , familyKey ).orElseThrow (FamilyNotFoundException ::new );
172187 if (family .createdAt ().isAfter (SharedConstants .API_V2_CUTOFF )) {
173188 throw new FamilyNotFoundException ();
174189 }
175190 final List <VersionEntity > versions = this .versions .findAllByFamily (family )
176191 .filter (version -> version .createdAt ().isBefore (SharedConstants .API_V2_CUTOFF ))
177- .sorted (Version . COMPARATOR_CREATED_AT )
192+ .sorted (Timestamped . CREATED_AT_ASC )
178193 .toList ();
179194 final Map <ObjectId , VersionEntity > versionsById = versions .stream ()
180195 .collect (Collectors .toMap (AbstractEntity ::_id , Function .identity ()));
181196 final List <BuildEntity > builds = this .builds .findAllByVersionIn (versionsById .keySet ())
182197 .filter (build -> build .createdAt ().isBefore (SharedConstants .API_V2_CUTOFF ))
183- .sorted (Build .COMPARATOR_NUMBER )
198+ .sorted (Build .NUMBER_ASC )
184199 .toList ();
185200 final FamilyBuildsResponse response = new FamilyBuildsResponse (
186201 project .key (),
@@ -208,14 +223,17 @@ public ResponseEntity<?> getVersion(
208223 @ PathVariable ("version" )
209224 final String versionKey
210225 ) {
226+ if (this .isSunset ()) {
227+ throw new SunsetException ();
228+ }
211229 final ProjectEntity project = this .projects .findByKey (projectKey ).orElseThrow (ProjectNotFoundException ::new );
212230 final VersionEntity version = this .versions .findByProjectAndKey (project , versionKey ).orElseThrow (VersionNotFoundException ::new );
213231 if (version .createdAt ().isAfter (SharedConstants .API_V2_CUTOFF )) {
214232 throw new VersionNotFoundException ();
215233 }
216234 final List <BuildEntity > builds = this .builds .findAllByVersion (version )
217235 .filter (build -> build .createdAt ().isBefore (SharedConstants .API_V2_CUTOFF ))
218- .sorted (Build .COMPARATOR_NUMBER )
236+ .sorted (Build .NUMBER_ASC )
219237 .toList ();
220238 final VersionResponse response = new VersionResponse (
221239 project .key (),
@@ -234,14 +252,17 @@ public ResponseEntity<?> getVersionBuilds(
234252 @ PathVariable ("version" )
235253 final String versionKey
236254 ) {
255+ if (this .isSunset ()) {
256+ throw new SunsetException ();
257+ }
237258 final ProjectEntity project = this .projects .findByKey (projectKey ).orElseThrow (ProjectNotFoundException ::new );
238259 final VersionEntity version = this .versions .findByProjectAndKey (project , versionKey ).orElseThrow (VersionNotFoundException ::new );
239260 if (version .createdAt ().isAfter (SharedConstants .API_V2_CUTOFF )) {
240261 throw new VersionNotFoundException ();
241262 }
242263 final List <BuildEntity > builds = this .builds .findAllByVersion (version )
243264 .filter (build -> build .createdAt ().isBefore (SharedConstants .API_V2_CUTOFF ))
244- .sorted (Build .COMPARATOR_NUMBER )
265+ .sorted (Build .NUMBER_ASC )
245266 .toList ();
246267 final BuildsResponse response = new BuildsResponse (
247268 project .key (),
@@ -270,6 +291,9 @@ public ResponseEntity<?> getVersionBuild(
270291 @ PositiveOrZero
271292 final int buildNumber
272293 ) {
294+ if (this .isSunset ()) {
295+ throw new SunsetException ();
296+ }
273297 final ProjectEntity project = this .projects .findByKey (projectKey ).orElseThrow (ProjectNotFoundException ::new );
274298 final VersionEntity version = this .versions .findByProjectAndKey (project , versionKey ).orElseThrow (VersionNotFoundException ::new );
275299 if (version .createdAt ().isAfter (SharedConstants .API_V2_CUTOFF )) {
@@ -293,6 +317,10 @@ public ResponseEntity<?> getVersionBuild(
293317 return Responses .ok (response , Caching .publicShared (CACHE_LENGTH_BUILD ));
294318 }
295319
320+ private boolean isSunset () {
321+ return this .clock .instant ().isAfter (SharedConstants .API_V2_SUNSET );
322+ }
323+
296324 private static boolean isPromoted (final Build build ) {
297325 return build .channel () == BuildChannel .RECOMMENDED ;
298326 }
0 commit comments