diff --git a/apiserver/plane/api/views/issue.py b/apiserver/plane/api/views/issue.py index 938633ab4..f743ad4f0 100644 --- a/apiserver/plane/api/views/issue.py +++ b/apiserver/plane/api/views/issue.py @@ -621,6 +621,54 @@ class IssueLinkViewSet(BaseViewSet): project_id=self.kwargs.get("project_id"), issue_id=self.kwargs.get("issue_id"), ) + issue_activity.delay( + type="link.activity.created", + requested_data=json.dumps(serializer.data, cls=DjangoJSONEncoder), + actor_id=str(self.request.user.id), + issue_id=str(self.kwargs.get("issue_id")), + project_id=str(self.kwargs.get("project_id")), + current_instance=None, + ) + + def perform_update(self, serializer): + requested_data = json.dumps(self.request.data, cls=DjangoJSONEncoder) + current_instance = ( + self.get_queryset().filter(pk=self.kwargs.get("pk", None)).first() + ) + if current_instance is not None: + issue_activity.delay( + type="link.activity.updated", + requested_data=requested_data, + actor_id=str(self.request.user.id), + issue_id=str(self.kwargs.get("issue_id", None)), + project_id=str(self.kwargs.get("project_id", None)), + current_instance=json.dumps( + IssueLinkSerializer(current_instance).data, + cls=DjangoJSONEncoder, + ), + ) + + return super().perform_update(serializer) + + def perform_destroy(self, instance): + current_instance = ( + self.get_queryset().filter(pk=self.kwargs.get("pk", None)).first() + ) + if current_instance is not None: + issue_activity.delay( + type="link.activity.deleted", + requested_data=json.dumps( + {"link_id": str(self.kwargs.get("pk", None))} + ), + actor_id=str(self.request.user.id), + issue_id=str(self.kwargs.get("issue_id", None)), + project_id=str(self.kwargs.get("project_id", None)), + current_instance=json.dumps( + IssueLinkSerializer(current_instance).data, + cls=DjangoJSONEncoder, + ), + ) + return super().perform_destroy(instance) def get_queryset(self): return ( @@ -687,6 +735,17 @@ class IssueAttachmentEndpoint(BaseAPIView): serializer = IssueAttachmentSerializer(data=request.data) if serializer.is_valid(): serializer.save(project_id=project_id, issue_id=issue_id) + issue_activity.delay( + type="attachment.activity.created", + requested_data=request.data, + actor_id=str(self.request.user.id), + issue_id=str(self.kwargs.get("issue_id", None)), + project_id=str(self.kwargs.get("project_id", None)), + current_instance=json.dumps( + serializer.data, + cls=DjangoJSONEncoder, + ), + ) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) except Exception as e: @@ -700,6 +759,15 @@ class IssueAttachmentEndpoint(BaseAPIView): try: issue_attachment = IssueAttachment.objects.get(pk=pk) issue_attachment.delete() + issue_activity.delay( + type="attachment.activity.deleted", + requested_data=request.data, + actor_id=str(self.request.user.id), + issue_id=str(self.kwargs.get("issue_id", None)), + project_id=str(self.kwargs.get("project_id", None)), + current_instance=None, + ) + return Response(status=status.HTTP_204_NO_CONTENT) except IssueAttachment.DoesNotExist: return Response( diff --git a/apiserver/plane/bgtasks/issue_activites_task.py b/apiserver/plane/bgtasks/issue_activites_task.py index a0e5dc4eb..192f5170b 100644 --- a/apiserver/plane/bgtasks/issue_activites_task.py +++ b/apiserver/plane/bgtasks/issue_activites_task.py @@ -653,15 +653,9 @@ def update_issue_activity( "modules_list": track_modules, } - requested_data = ( - json.loads(requested_data) - if requested_data is not None - else None - ) + requested_data = json.loads(requested_data) if requested_data is not None else None current_instance = ( - json.loads(current_instance) - if current_instance is not None - else None + json.loads(current_instance) if current_instance is not None else None ) for key in requested_data: @@ -677,9 +671,29 @@ def update_issue_activity( ) +def delete_issue_activity( + requested_data, current_instance, issue_id, project, actor, issue_activities +): + issue_activities.append( + IssueActivity( + project=project, + workspace=project.workspace, + comment=f"{actor.email} deleted the issue", + verb="deleted", + actor=actor, + field="issue", + ) + ) + + def create_comment_activity( requested_data, current_instance, issue_id, project, actor, issue_activities ): + requested_data = json.loads(requested_data) if requested_data is not None else None + current_instance = ( + json.loads(current_instance) if current_instance is not None else None + ) + issue_activities.append( IssueActivity( issue_id=issue_id, @@ -699,6 +713,11 @@ def create_comment_activity( def update_comment_activity( requested_data, current_instance, issue_id, project, actor, issue_activities ): + requested_data = json.loads(requested_data) if requested_data is not None else None + current_instance = ( + json.loads(current_instance) if current_instance is not None else None + ) + if current_instance.get("comment_html") != requested_data.get("comment_html"): issue_activities.append( IssueActivity( @@ -718,21 +737,6 @@ def update_comment_activity( ) -def delete_issue_activity( - requested_data, current_instance, issue_id, project, actor, issue_activities -): - issue_activities.append( - IssueActivity( - project=project, - workspace=project.workspace, - comment=f"{actor.email} deleted the issue", - verb="deleted", - actor=actor, - field="issue", - ) - ) - - def delete_comment_activity( requested_data, current_instance, issue_id, project, actor, issue_activities ): @@ -749,6 +753,113 @@ def delete_comment_activity( ) +def create_link_activity( + requested_data, current_instance, issue_id, project, actor, issue_activities +): + requested_data = json.loads(requested_data) if requested_data is not None else None + current_instance = ( + json.loads(current_instance) if current_instance is not None else None + ) + + issue_activities.append( + IssueActivity( + issue_id=issue_id, + project=project, + workspace=project.workspace, + comment=f"{actor.email} created a link", + verb="created", + actor=actor, + field="link", + new_value=requested_data.get("url", ""), + new_identifier=requested_data.get("id", None), + issue_comment_id=requested_data.get("id", None), + ) + ) + + +def update_link_activity( + requested_data, current_instance, issue_id, project, actor, issue_activities +): + requested_data = json.loads(requested_data) if requested_data is not None else None + current_instance = ( + json.loads(current_instance) if current_instance is not None else None + ) + + if current_instance.get("url") != requested_data.get("url"): + issue_activities.append( + IssueActivity( + issue_id=issue_id, + project=project, + workspace=project.workspace, + comment=f"{actor.email} updated a link", + verb="updated", + actor=actor, + field="link", + old_value=current_instance.get("url", ""), + old_identifier=current_instance.get("id"), + new_value=requested_data.get("url", ""), + new_identifier=current_instance.get("id", None), + issue_comment_id=current_instance.get("id", None), + ) + ) + + +def delete_link_activity( + requested_data, current_instance, issue_id, project, actor, issue_activities +): + issue_activities.append( + IssueActivity( + issue_id=issue_id, + project=project, + workspace=project.workspace, + comment=f"{actor.email} deleted the link", + verb="deleted", + actor=actor, + field="link", + ) + ) + + +def create_attachment_activity( + requested_data, current_instance, issue_id, project, actor, issue_activities +): + requested_data = json.loads(requested_data) if requested_data is not None else None + current_instance = ( + json.loads(current_instance) if current_instance is not None else None + ) + + issue_activities.append( + IssueActivity( + issue_id=issue_id, + project=project, + workspace=project.workspace, + comment=f"{actor.email} created a attachment", + verb="created", + actor=actor, + field="attachment", + new_value=requested_data.get("url", ""), + new_identifier=requested_data.get("id", None), + issue_comment_id=requested_data.get("id", None), + ) + ) + + +def delete_attachment_activity( + requested_data, current_instance, issue_id, project, actor, issue_activities +): + issue_activities.append( + IssueActivity( + issue_id=issue_id, + project=project, + workspace=project.workspace, + comment=f"{actor.email} deleted the attachment", + verb="deleted", + actor=actor, + field="attachment", + ) + ) + + # Receive message from room group @shared_task def issue_activity( @@ -767,6 +878,11 @@ def issue_activity( "comment.activity.created": create_comment_activity, "comment.activity.updated": update_comment_activity, "comment.activity.deleted": delete_comment_activity, + "link.activity.created": create_link_activity, + "link.activity.updated": update_link_activity, + "link.activity.deleted": delete_link_activity, + "attachment.activity.created": create_attachment_activity, + "attachment.activity.deleted": delete_attachment_activity, } func = ACTIVITY_MAPPER.get(type)