Skip to content

Commit

Permalink
Stop creating bot comment thread when user is app viewer (appsmithorg…
Browse files Browse the repository at this point in the history
…#6111)

* -stop creating bot comment thread when user is app viewer

* -added comments to the method
  • Loading branch information
nayan-rafiq authored Jul 26, 2021
1 parent decd171 commit 667179d
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.util.Map;
import java.util.Set;

import static com.appsmith.server.acl.AclPermission.MANAGE_APPLICATIONS;
import static com.appsmith.server.constants.CommentConstants.APPSMITH_BOT_NAME;
import static com.appsmith.server.constants.CommentConstants.APPSMITH_BOT_USERNAME;
import static java.lang.Boolean.FALSE;
Expand Down Expand Up @@ -117,12 +118,22 @@ public Mono<Comment> create(String threadId, Comment comment, String originHeade
threadRepository
.findById(threadId, AclPermission.COMMENT_ON_THREAD)
.switchIfEmpty(Mono.error(new AppsmithException(AppsmithError.ACL_NO_RESOURCE_FOUND, "comment thread", threadId)))
.flatMap(commentThread -> updateThreadIfRequired(commentThread, comment, user))
.flatMap(commentThread -> updateThreadOnAddComment(commentThread, comment, user))
.flatMap(commentThread -> create(commentThread, user, comment, originHeader, true))
);
}

private Mono<CommentThread> updateThreadIfRequired(CommentThread commentThread, Comment comment, User user) {
/**
* This method updates a comment thread when a new comment is added in that thread. It does the following:<ul>
* <li>Marks the thread as unread for users other than the author</li>
* <li>Mark the thread as unresolved if it's in resolved state</li>
* <li>Marks the thread as public if someone is tagged in the comment</li></ul>
* @param commentThread the thread object
* @param comment the comment object
* @param user currently logged in user aka author
* @return updated thread
*/
private Mono<CommentThread> updateThreadOnAddComment(CommentThread commentThread, Comment comment, User user) {
commentThread.setViewedByUsers(Set.of(user.getUsername()));
if(commentThread.getResolvedState().getActive() == TRUE) {
commentThread.getResolvedState().setActive(FALSE);
Expand Down Expand Up @@ -255,8 +266,11 @@ public Mono<CommentThread> createThread(CommentThread commentThread, String orig
.flatMap(tuple -> {
final UserData userData = tuple.getT1();
final Application application = tuple.getT2();
boolean shouldCreateBotThread = policyUtils.isPermissionPresentForUser(
application.getPolicies(), MANAGE_APPLICATIONS.getValue(), user.getUsername()
);
// check whether this thread should be converted to bot thread
if (userData.getLatestCommentEvent() == null) {
if (userData.getLatestCommentEvent() == null && shouldCreateBotThread) {
commentThread.setIsPrivate(true);
userData.setLatestCommentEvent(CommentBotEvent.COMMENTED);
return userDataRepository.save(userData).then(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import com.appsmith.server.domains.CommentThread;
import com.appsmith.server.domains.Organization;
import com.appsmith.server.domains.User;
import com.appsmith.server.domains.UserData;
import com.appsmith.server.helpers.PolicyUtils;
import com.appsmith.server.repositories.CommentRepository;
import com.appsmith.server.repositories.CommentThreadRepository;
import com.appsmith.server.repositories.UserDataRepository;
import com.appsmith.server.solutions.EmailEventHandler;
import com.segment.analytics.Analytics;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -32,6 +34,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
Expand Down Expand Up @@ -63,6 +66,12 @@ public class CommentServiceTest {
@Autowired
PolicyUtils policyUtils;

@Autowired
UserService userService;

@Autowired
private UserDataRepository userDataRepository;

@MockBean
private Analytics analytics;

Expand Down Expand Up @@ -332,4 +341,59 @@ public void create_WhenThreadIsResolvedAndAlreadyViewed_ThreadIsUnresolvedAndUnr

}

private Mono<CommentThread> createAndFetchTestCommentThreadForBotTest(Set<AclPermission> applicationPermissions) {
return userService.findByEmail("api_user")
.flatMap(user ->
userDataRepository.findByUserId(user.getId()) // setup userdata first
.defaultIfEmpty(new UserData(user.getId()))
.map(userData -> {
userData.setLatestCommentEvent(null);
return userData;
})
.flatMap(userDataRepository::save).thenReturn(user)

)
.flatMap(user -> {
// create an application
Application application = new Application();
Map<String, Policy> stringPolicyMap = policyUtils.generatePolicyFromPermission(
applicationPermissions, user
);
application.setPolicies(Set.copyOf(stringPolicyMap.values()));
application.setName(UUID.randomUUID().toString());
return applicationService.save(application);
}).flatMap(application -> {
// create a thread
CommentThread commentThread = new CommentThread();
commentThread.setApplicationId(application.getId());
Comment comment = makePlainTextComment("test comment here");
commentThread.setComments(List.of(comment));
return commentService.createThread(commentThread, null);
}).flatMap(thread -> commentThreadRepository.findById(thread.getId())); // fetch the thread to check
}

@Test
@WithUserDetails(value = "api_user")
public void createThread_WhenFirstCommentFromUser_CreatesBotThreadAndComment() {
Mono<CommentThread> commentThreadMono = createAndFetchTestCommentThreadForBotTest(
Set.of(AclPermission.MANAGE_APPLICATIONS, AclPermission.COMMENT_ON_APPLICATIONS)
);

StepVerifier.create(commentThreadMono).assertNext(thread -> {
assertThat(thread.getIsPrivate()).isTrue();
assertThat(thread.getSequenceId()).isEqualTo("#0");
}).verifyComplete();
}

@Test
@WithUserDetails(value = "api_user")
public void createThread_WhenFirstCommentFromViewer_BotThreadNotCreated() {
Mono<CommentThread> commentThreadMono = createAndFetchTestCommentThreadForBotTest(
Set.of(AclPermission.READ_APPLICATIONS)
);

StepVerifier.create(commentThreadMono).assertNext(thread -> {
assertThat(thread.getIsPrivate()).isNotEqualTo(Boolean.TRUE);
}).verifyComplete();
}
}

0 comments on commit 667179d

Please sign in to comment.