Skip to content

Add required info into events #1461

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

Merged
merged 3 commits into from
Jan 24, 2025
Merged
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 @@ -24,6 +24,7 @@ public abstract class AbstractEvent implements LowcoderEvent

public Map<String, Object> details()
{
this.details.put("environmentId", environmentID);
return this.details;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@ public class ApplicationCommonEvent extends AbstractEvent {
private final String applicationId;
private final String applicationGid;
private final String applicationName;
private final String applicationCategory;
private final String applicationDescription;
private final EventType type;
@Nullable
private final String folderId;
@Nullable
private final String folderName;
@Nullable
private final String oldFolderId;
@Nullable
private final String oldFolderName;

@Override
public EventType getEventType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ public Mono<ResponseView<ApplicationView>> createFromTemplate(@RequestParam Stri
public Mono<ResponseView<Boolean>> recycle(@PathVariable String applicationId) {
return gidService.convertApplicationIdToObjectId(applicationId).flatMap(appId ->
applicationApiService.recycle(appId)
.delayUntil(__ -> businessEventPublisher.publishApplicationCommonEvent(applicationId, null, APPLICATION_RECYCLED))
.delayUntil(__ -> businessEventPublisher.publishApplicationCommonEvent(applicationId, null, null, APPLICATION_RECYCLED))
.map(ResponseView::success));
}

@Override
public Mono<ResponseView<Boolean>> restore(@PathVariable String applicationId) {
return gidService.convertApplicationIdToObjectId(applicationId).flatMap(appId ->
applicationApiService.restore(appId)
.delayUntil(__ -> businessEventPublisher.publishApplicationCommonEvent(applicationId, null, APPLICATION_RESTORE))
.delayUntil(__ -> businessEventPublisher.publishApplicationCommonEvent(applicationId, null, null, APPLICATION_RESTORE))
.map(ResponseView::success));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public class ApplicationInfoView {
private final Object containerSize; // for module size
@Nullable
private String folderId;
@Nullable
@JsonInclude(Include.NON_NULL)
private String folderIdFrom;

@Nullable
private final Instant lastViewTime; // user last visit time for this app
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
package org.lowcoder.api.home;

import static org.lowcoder.plugin.api.event.LowcoderEvent.EventType.APPLICATION_MOVE;
import static org.lowcoder.sdk.exception.BizError.INVALID_PARAMETER;
import static org.lowcoder.sdk.util.ExceptionUtils.ofError;

import lombok.RequiredArgsConstructor;
import org.lowcoder.api.application.view.ApplicationPermissionView;
import org.lowcoder.api.framework.view.PageResponseView;
import org.lowcoder.api.framework.view.ResponseView;
import org.lowcoder.api.util.BusinessEventPublisher;
import org.lowcoder.api.util.GidService;
import org.lowcoder.domain.application.model.ApplicationType;
import org.lowcoder.domain.folder.model.Folder;
import org.lowcoder.domain.folder.service.FolderElementRelationService;
import org.lowcoder.domain.folder.service.FolderService;
import org.lowcoder.domain.permission.model.ResourceRole;
import org.lowcoder.plugin.api.event.LowcoderEvent.EventType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import lombok.RequiredArgsConstructor;
import reactor.core.publisher.Mono;

import java.util.List;

import static org.lowcoder.plugin.api.event.LowcoderEvent.EventType.APPLICATION_MOVE;
import static org.lowcoder.sdk.exception.BizError.INVALID_PARAMETER;
import static org.lowcoder.sdk.util.ExceptionUtils.ofError;

@RequiredArgsConstructor
@RestController
public class FolderController implements FolderEndpoints
Expand All @@ -31,6 +33,7 @@ public class FolderController implements FolderEndpoints
private final FolderApiService folderApiService;
private final BusinessEventPublisher businessEventPublisher;
private final GidService gidService;
private final FolderElementRelationService folderElementRelationService;

@Override
public Mono<ResponseView<FolderInfoView>> create(@RequestBody Folder folder) {
Expand Down Expand Up @@ -89,10 +92,11 @@ public Mono<PageResponseView<?>> getElements(@RequestParam(value = "id", require
@Override
public Mono<ResponseView<Void>> move(@PathVariable("id") String applicationLikeId,
@RequestParam(value = "targetFolderId", required = false) String targetFolderId) {
return gidService.convertFolderIdToObjectId(targetFolderId).flatMap(objectId ->
folderApiService.move(applicationLikeId, objectId.orElse(null))
.then(businessEventPublisher.publishApplicationCommonEvent(applicationLikeId, objectId.orElse(null), APPLICATION_MOVE))
.then(Mono.fromSupplier(() -> ResponseView.success(null))));
return folderElementRelationService.getByElementIds(List.of(applicationLikeId)).next().flatMap(folderElement ->
gidService.convertFolderIdToObjectId(targetFolderId).flatMap(objectId ->
folderApiService.move(applicationLikeId, objectId.orElse(null))
.then(businessEventPublisher.publishApplicationCommonEvent(applicationLikeId, folderElement.folderId(), objectId.orElse(null), APPLICATION_MOVE))
.then(Mono.fromSupplier(() -> ResponseView.success(null)))));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.lowcoder.api.application.view.ApplicationInfoView;
import org.lowcoder.api.application.view.ApplicationView;
import org.lowcoder.api.home.SessionUserService;
import org.lowcoder.api.usermanagement.view.AddMemberRequest;
import org.lowcoder.api.usermanagement.view.UpdateRoleRequest;
import org.lowcoder.domain.application.service.ApplicationRecordServiceImpl;
import org.lowcoder.domain.application.service.ApplicationService;
import org.lowcoder.domain.datasource.model.Datasource;
import org.lowcoder.domain.datasource.service.DatasourceService;
Expand Down Expand Up @@ -40,6 +42,7 @@
import org.lowcoder.infra.event.groupmember.GroupMemberRoleUpdateEvent;
import org.lowcoder.infra.event.user.UserLoginEvent;
import org.lowcoder.infra.event.user.UserLogoutEvent;
import org.lowcoder.infra.util.TupleUtils;
import org.lowcoder.plugin.api.event.LowcoderEvent.EventType;
import org.lowcoder.sdk.constants.Authentication;
import org.lowcoder.sdk.util.LocaleUtils;
Expand All @@ -65,6 +68,7 @@ public class BusinessEventPublisher {
private final ApplicationService applicationService;
private final DatasourceService datasourceService;
private final ResourcePermissionService resourcePermissionService;
private final ApplicationRecordServiceImpl applicationRecordServiceImpl;

public Mono<Void> publishFolderCommonEvent(String folderId, String folderName, EventType eventType) {

Expand Down Expand Up @@ -93,13 +97,14 @@ public Mono<Void> publishFolderCommonEvent(String folderId, String folderName, E
});
}

public Mono<Void> publishApplicationCommonEvent(String applicationId, @Nullable String folderId, EventType eventType) {
public Mono<Void> publishApplicationCommonEvent(String applicationId, @Nullable String folderIdFrom, @Nullable String folderId, EventType eventType) {
return applicationService.findByIdWithoutDsl(applicationId)
.map(application -> {
ApplicationInfoView applicationInfoView = ApplicationInfoView.builder()
.applicationId(applicationId)
.name(application.getName())
.folderId(folderId)
.folderIdFrom(folderIdFrom)
.build();
return ApplicationView.builder()
.applicationInfoView(applicationInfoView)
Expand All @@ -125,21 +130,48 @@ public Mono<Void> publishApplicationCommonEvent(ApplicationView applicationView,
.map(Optional::of)
.onErrorReturn(Optional.empty());
}))
.zipWith(Mono.defer(() -> {
String folderId = applicationView.getApplicationInfoView().getFolderIdFrom();
if (StringUtils.isBlank(folderId)) {
return Mono.just(Optional.<Folder> empty());
}
return folderService.findById(folderId)
.map(Optional::of)
.onErrorReturn(Optional.empty());
}), TupleUtils::merge)
.zipWith(sessionUserService.getVisitorToken())
.zipWith(Mono.defer(() -> {
String appId = applicationView.getApplicationInfoView().getApplicationId();
return applicationService.findById(appId)
.zipWhen(application -> application.getCategory(applicationRecordServiceImpl))
.zipWhen(application -> application.getT1().getDescription(applicationRecordServiceImpl))
.map(tuple -> {
String category = tuple.getT1().getT2();
String description = tuple.getT2();
return Pair.of(category, description);
});
}), TupleUtils::merge)
.doOnNext(tuple -> {
OrgMember orgMember = tuple.getT1().getT1();
Optional<Folder> optional = tuple.getT1().getT2();
Optional<Folder> optionalFrom = tuple.getT1().getT3();
String token = tuple.getT2();
String category = tuple.getT3().getLeft();
String description = tuple.getT3().getRight();
ApplicationInfoView applicationInfoView = applicationView.getApplicationInfoView();
ApplicationCommonEvent event = ApplicationCommonEvent.builder()
.orgId(orgMember.getOrgId())
.userId(orgMember.getUserId())
.applicationId(applicationInfoView.getApplicationId())
.applicationGid(applicationInfoView.getApplicationGid())
.applicationName(applicationInfoView.getName())
.applicationCategory(category)
.applicationDescription(description)
.type(eventType)
.folderId(optional.map(Folder::getId).orElse(null))
.folderName(optional.map(Folder::getName).orElse(null))
.oldFolderId(optionalFrom.map(Folder::getId).orElse(null))
.oldFolderName(optionalFrom.map(Folder::getName).orElse(null))
.isAnonymous(anonymous)
.sessionHash(Hashing.sha512().hashString(token, StandardCharsets.UTF_8).toString())
.build();
Expand Down
Loading