Update code style in `MessageController.sendMessage()`

This commit is contained in:
ameya-signal 2024-09-13 07:53:40 -07:00 committed by GitHub
parent 42d4574213
commit 8bafb1a641
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 34 additions and 42 deletions

View File

@ -279,26 +279,26 @@ public class MessageController {
@ApiResponse( @ApiResponse(
responseCode = "410", description = "Mismatched registration ids supplied for some recipient devices", responseCode = "410", description = "Mismatched registration ids supplied for some recipient devices",
content = @Content(schema = @Schema(implementation = AccountStaleDevices[].class))) content = @Content(schema = @Schema(implementation = AccountStaleDevices[].class)))
public Response sendMessage(@ReadOnly @Auth Optional<AuthenticatedDevice> source, public Response sendMessage(@ReadOnly @Auth final Optional<AuthenticatedDevice> source,
@Parameter(description="The recipient's unidentified access key") @Parameter(description="The recipient's unidentified access key")
@HeaderParam(HeaderUtils.UNIDENTIFIED_ACCESS_KEY) Optional<Anonymous> accessKey, @HeaderParam(HeaderUtils.UNIDENTIFIED_ACCESS_KEY) final Optional<Anonymous> accessKey,
@Parameter(description="A group send endorsement token covering the recipient. Must not be combined with `Unidentified-Access-Key` or set on a story message.") @Parameter(description="A group send endorsement token covering the recipient. Must not be combined with `Unidentified-Access-Key` or set on a story message.")
@HeaderParam(HeaderUtils.GROUP_SEND_TOKEN) @HeaderParam(HeaderUtils.GROUP_SEND_TOKEN)
@Nullable GroupSendTokenHeader groupSendToken, @Nullable final GroupSendTokenHeader groupSendToken,
@HeaderParam(HttpHeaders.USER_AGENT) String userAgent, @HeaderParam(HttpHeaders.USER_AGENT) final String userAgent,
@Parameter(description="If true, deliver the message only to recipients that are online when it is sent") @Parameter(description="If true, deliver the message only to recipients that are online when it is sent")
@PathParam("destination") ServiceIdentifier destinationIdentifier, @PathParam("destination") final ServiceIdentifier destinationIdentifier,
@Parameter(description="If true, the message is a story; access tokens are not checked and sending to nonexistent recipients is permitted") @Parameter(description="If true, the message is a story; access tokens are not checked and sending to nonexistent recipients is permitted")
@QueryParam("story") boolean isStory, @QueryParam("story") final boolean isStory,
@Parameter(description="The encrypted message payloads for each recipient device") @Parameter(description="The encrypted message payloads for each recipient device")
@NotNull @Valid IncomingMessageList messages, @NotNull @Valid final IncomingMessageList messages,
@Context ContainerRequestContext context) throws RateLimitExceededException { @Context final ContainerRequestContext context) throws RateLimitExceededException {
final Sample sample = Timer.start(); final Sample sample = Timer.start();
try { try {
@ -307,7 +307,7 @@ public class MessageController {
} }
if (groupSendToken != null) { if (groupSendToken != null) {
if (!source.isEmpty() || !accessKey.isEmpty()) { if (source.isPresent() || accessKey.isPresent()) {
throw new BadRequestException( throw new BadRequestException(
"Group send endorsement tokens should not be combined with other authentication"); "Group send endorsement tokens should not be combined with other authentication");
} else if (isStory) { } else if (isStory) {
@ -315,24 +315,17 @@ public class MessageController {
} }
} }
final String senderType; final String senderType = source.map(
if (source.isPresent()) { s -> s.getAccount().isIdentifiedBy(destinationIdentifier) ? SENDER_TYPE_SELF : SENDER_TYPE_IDENTIFIED)
if (source.get().getAccount().isIdentifiedBy(destinationIdentifier)) { .orElse(SENDER_TYPE_UNIDENTIFIED);
senderType = SENDER_TYPE_SELF;
} else {
senderType = SENDER_TYPE_IDENTIFIED;
}
} else {
senderType = SENDER_TYPE_UNIDENTIFIED;
}
boolean isSyncMessage = source.isPresent() && source.get().getAccount().isIdentifiedBy(destinationIdentifier); final boolean isSyncMessage = senderType.equals(SENDER_TYPE_SELF);
if (isSyncMessage && destinationIdentifier.identityType() == IdentityType.PNI) { if (isSyncMessage && destinationIdentifier.identityType() == IdentityType.PNI) {
throw new WebApplicationException(Status.FORBIDDEN); throw new WebApplicationException(Status.FORBIDDEN);
} }
Optional<Account> destination; final Optional<Account> destination;
if (!isSyncMessage) { if (!isSyncMessage) {
destination = accountsManager.getByServiceIdentifier(destinationIdentifier); destination = accountsManager.getByServiceIdentifier(destinationIdentifier);
} else { } else {
@ -387,7 +380,7 @@ public class MessageController {
destinationIdentifier); destinationIdentifier);
} }
boolean needsSync = !isSyncMessage && source.isPresent() && source.get().getAccount().getDevices().size() > 1; final boolean needsSync = !isSyncMessage && source.isPresent() && source.get().getAccount().getDevices().size() > 1;
// We return 200 when stories are sent to a non-existent account. Since story sends bypass OptionalAccess.verify // We return 200 when stories are sent to a non-existent account. Since story sends bypass OptionalAccess.verify
// we leak information about whether a destination UUID exists if we return any other code (e.g. 404) from // we leak information about whether a destination UUID exists if we return any other code (e.g. 404) from
@ -444,34 +437,33 @@ public class MessageController {
Tag.of(AUTH_TYPE_TAG_NAME, authType), Tag.of(AUTH_TYPE_TAG_NAME, authType),
Tag.of(IDENTITY_TYPE_TAG_NAME, destinationIdentifier.identityType().name())); Tag.of(IDENTITY_TYPE_TAG_NAME, destinationIdentifier.identityType().name()));
for (IncomingMessage incomingMessage : messages.messages()) { for (final IncomingMessage incomingMessage : messages.messages()) {
Optional<Device> destinationDevice = destination.get().getDevice(incomingMessage.destinationDeviceId()); destination.get().getDevice(incomingMessage.destinationDeviceId())
.ifPresent(destinationDevice -> {
if (destinationDevice.isPresent()) { Metrics.counter(SENT_MESSAGE_COUNTER_NAME, tags).increment();
Metrics.counter(SENT_MESSAGE_COUNTER_NAME, tags).increment(); sendIndividualMessage(
sendIndividualMessage( source,
source, destination.get(),
destination.get(), destinationDevice,
destinationDevice.get(), destinationIdentifier,
destinationIdentifier, messages.timestamp(),
messages.timestamp(), messages.online(),
messages.online(), isStory,
isStory, messages.urgent(),
messages.urgent(), incomingMessage,
incomingMessage, userAgent,
userAgent, spamReportToken);
spamReportToken); });
}
} }
return Response.ok(new SendMessageResponse(needsSync)).build(); return Response.ok(new SendMessageResponse(needsSync)).build();
} catch (MismatchedDevicesException e) { } catch (final MismatchedDevicesException e) {
throw new WebApplicationException(Response.status(409) throw new WebApplicationException(Response.status(409)
.type(MediaType.APPLICATION_JSON_TYPE) .type(MediaType.APPLICATION_JSON_TYPE)
.entity(new MismatchedDevices(e.getMissingDevices(), .entity(new MismatchedDevices(e.getMissingDevices(),
e.getExtraDevices())) e.getExtraDevices()))
.build()); .build());
} catch (StaleDevicesException e) { } catch (final StaleDevicesException e) {
throw new WebApplicationException(Response.status(410) throw new WebApplicationException(Response.status(410)
.type(MediaType.APPLICATION_JSON) .type(MediaType.APPLICATION_JSON)
.entity(new StaleDevices(e.getStaleDevices())) .entity(new StaleDevices(e.getStaleDevices()))