Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Component): Add new endpoint that allows user to subscribe and unsubscribe to a component #2737

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,31 @@ public ResponseEntity<CollectionModel<EntityModel<Component>>> getMySubscription
return new ResponseEntity<>(finalResources, status);
}

@Operation(
summary = "Toggle user subscription to a component",
description = "Subscribes or unsubscribes the user to a specified component based on their current subscription status.",
tags = {"Components"}
)
@RequestMapping(value = COMPONENTS_URL + "/{id}/subscriptions", method = RequestMethod.POST)
public ResponseEntity<String> toggleComponentSubscription(
@Parameter(description = "The ID of the component.")
@PathVariable("id") String componentId
) throws TException {
User user = restControllerHelper.getSw360UserFromAuthentication();
Component componentById = componentService.getComponentForUserById(componentId, user);
Set<String> subscribers = componentById.getSubscribers();

boolean isSubscribed = subscribers.contains(user.getEmail());

if (isSubscribed) {
componentService.unsubscribeComponent(componentId, user);
return new ResponseEntity<>("Successfully unsubscribed from the component.", HttpStatus.OK);
} else {
componentService.subscribeComponent(componentId, user);
return new ResponseEntity<>("Successfully subscribed to the component.", HttpStatus.OK);
}
}

@Operation(
summary = "Get components by external ID.",
description = "Get components by external ID.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ public List<Component> getComponentSubscriptions(User sw360User) throws TExcepti
return sw360ComponentClient.getSubscribedComponents(sw360User);
}

public RequestStatus subscribeComponent(String componentId, User sw360User) throws TException {
ComponentService.Iface sw360ComponentClient = getThriftComponentClient();
return sw360ComponentClient.subscribeComponent(componentId, sw360User);
}

public RequestStatus unsubscribeComponent(String componentId, User sw360User) throws TException {
ComponentService.Iface sw360ComponentClient = getThriftComponentClient();
return sw360ComponentClient.unsubscribeComponent(componentId, sw360User);
}

public List<Component> getRecentComponents(User sw360User) throws TException {
ComponentService.Iface sw360ComponentClient = getThriftComponentClient();
return sw360ComponentClient.getRecentComponentsSummary(5, sw360User);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.eclipse.sw360.rest.resourceserver.attachment.Sw360AttachmentService;
import org.eclipse.sw360.rest.resourceserver.component.Sw360ComponentService;
import org.eclipse.sw360.rest.resourceserver.report.SW360ReportService;
import org.eclipse.sw360.rest.resourceserver.user.Sw360UserService;
import org.eclipse.sw360.rest.resourceserver.vulnerability.Sw360VulnerabilityService;
import org.eclipse.sw360.rest.resourceserver.vendor.Sw360VendorService;
import org.hamcrest.Matchers;
Expand Down Expand Up @@ -1345,4 +1344,12 @@ public void should_document_get_component_report() throws Exception{
+ "Possible values are `<true|false>`")
)));
}

@Test
public void should_subscribe_user_to_component() throws Exception {
mockMvc.perform(post("/api/components/" + angularComponent.getId() + "/subscriptions")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", TestHelper.generateAuthHeader(testUserId, testUserPassword)))
.andExpect(status().isOk());
}
}