From d23c02b83c74b4f2f401a2b5d426019165c255d6 Mon Sep 17 00:00:00 2001
From: lian
Date: Thu, 24 Oct 2024 17:52:42 +0800
Subject: [PATCH] send repo monitor notice to dingtalk
---
.../commands/send_dingtalk_notifications.py | 14 +++-
seahub/notifications/models.py | 81 ++++++++++++++++++-
.../templates/notifications/notice_email.html | 4 +-
3 files changed, 92 insertions(+), 7 deletions(-)
diff --git a/seahub/notifications/management/commands/send_dingtalk_notifications.py b/seahub/notifications/management/commands/send_dingtalk_notifications.py
index 8388e13dfa2..61911df881b 100644
--- a/seahub/notifications/management/commands/send_dingtalk_notifications.py
+++ b/seahub/notifications/management/commands/send_dingtalk_notifications.py
@@ -36,7 +36,7 @@
# https://ding-doc.dingtalk.com/doc#/serverapi3/wvdxel
-########## Utility Functions ##########
+# Utility Functions
def remove_html_a_element(s):
"""
Replace xx to xx and wrap content with .
@@ -167,9 +167,17 @@ def do_action(self):
# 4. send msg to users
for username, uid in users:
+
user_id = user_uid_map[username]
notices = user_notices.get(username, [])
- count = len(notices)
+
+ content_list = []
+ for notice in notices:
+ if not notice.format_msg():
+ continue
+ content_list.append(remove_html_a_element(notice.format_msg()))
+
+ count = len(content_list)
if count == 0:
continue
@@ -181,7 +189,7 @@ def do_action(self):
count
) % {'num': count, 'site_name': site_name, }
- content = ' \n '.join([remove_html_a_element(x.format_msg()) for x in notices])
+ content = ' \n '.join(content_list)
self.send_dingtalk_msg(user_id, title, content)
# reset language
diff --git a/seahub/notifications/models.py b/seahub/notifications/models.py
index 68bb3493ec2..797ca5f8183 100644
--- a/seahub/notifications/models.py
+++ b/seahub/notifications/models.py
@@ -330,12 +330,12 @@ def add_file_comment_msg(self, to_user, detail):
return self._add_user_notification(to_user, MSG_TYPE_FILE_COMMENT, detail)
def add_draft_comment_msg(self, to_user, detail):
- """Notify ``to_user`` that review creator
+ """Notify ``to_user`` that review creator
"""
return self._add_user_notification(to_user, MSG_TYPE_DRAFT_COMMENT, detail)
def add_request_reviewer_msg(self, to_user, detail):
- """Notify ``to_user`` that reviewer
+ """Notify ``to_user`` that reviewer
"""
return self._add_user_notification(to_user, MSG_TYPE_DRAFT_REVIEWER, detail)
@@ -520,6 +520,8 @@ def format_msg(self):
return self.format_add_user_to_group()
elif self.is_repo_transfer_msg():
return self.format_repo_transfer_msg()
+ elif self.is_repo_monitor_msg():
+ return self.format_repo_monitor_msg()
else:
return ''
@@ -877,6 +879,81 @@ def format_repo_transfer_msg(self):
}
return msg
+ def format_repo_monitor_msg(self):
+ """
+
+ Arguments:
+ - `self`:
+ """
+ # {'commit_id': '5a52250eec53f32e771e7e032e5a40fd33143610',
+ # 'repo_id': 'b37d325a-5e72-416b-aa36-10643cee2f42',
+ # 'repo_name': 'lib of lian@seafile.com',
+ # 'op_user': 'lian@lian.com',
+ # 'op_type': 'create',
+ # 'obj_type': 'file',
+ # 'obj_path_list': ['/sdf.md'],
+ # 'old_obj_path_list': []}
+
+ try:
+ d = json.loads(self.detail)
+ except Exception as e:
+ logger.error(e)
+ return ""
+
+ repo_name = d['repo_name']
+ op_user = d['op_user']
+ op_type = d['op_type']
+
+ obj_type = d['obj_type']
+ obj_path_list = d['obj_path_list']
+ obj_path_count = len(obj_path_list)
+ obj_path_count_minus_one = len(obj_path_list) - 1
+ obj_name = os.path.basename(d['obj_path_list'][0])
+
+ old_obj_path_list = d.get('old_obj_path_list', [])
+ if old_obj_path_list:
+ old_obj_name = os.path.basename(d['old_obj_path_list'][0])
+ else:
+ old_obj_name = ''
+
+ if op_type == 'create':
+
+ if obj_path_count == 1:
+ message = _(f'User {op_user} created {obj_type} {obj_name} in library {repo_name}.')
+ else:
+ message = _(f'User {op_user} created {obj_type} {obj_name} and {obj_path_count_minus_one} other {obj_type}(s) in library {repo_name}.')
+
+ elif op_type == 'delete':
+
+ if obj_path_count == 1:
+ message = _(f'User {op_user} deleted {obj_type} {obj_name} in library {repo_name}.')
+ else:
+ message = _(f'User {op_user} deleted {obj_type} {obj_name} and {obj_path_count_minus_one} other {obj_type}(s) in library {repo_name}.')
+
+ elif op_type == 'recover':
+
+ message = _(f'User {op_user} restored {obj_type} {obj_name} in library {repo_name}.')
+
+ elif op_type == 'rename':
+
+ message = _(f'User {op_user} renamed {obj_type} {old_obj_name} to {obj_name} in library {repo_name}.')
+
+ elif op_type == 'move':
+
+ if obj_path_count == 1:
+ message = _(f'User {op_user} moved {obj_type} {obj_name} in library {repo_name}.')
+ else:
+ message = _(f'User {op_user} moved {obj_type} {obj_name} and {obj_path_count_minus_one} other {obj_type}(s) in library {repo_name}.')
+
+ elif op_type == 'edit':
+
+ message = _(f'User {op_user} updated {obj_type} {obj_name} in library {repo_name}.')
+
+ else:
+ message = _(f'User {op_user} {op_type} {obj_type} {obj_name} in library {repo_name}.')
+
+ return message
+
########## handle signals
from django.dispatch import receiver
diff --git a/seahub/notifications/templates/notifications/notice_email.html b/seahub/notifications/templates/notifications/notice_email.html
index dacbeb3b1e5..6a9723e751b 100644
--- a/seahub/notifications/templates/notifications/notice_email.html
+++ b/seahub/notifications/templates/notifications/notice_email.html
@@ -104,7 +104,7 @@
{% if notice.op_type == 'edit' %}
{% blocktrans with user_url=notice.user_url user_name=notice.user_name obj_url=notice.obj_url obj_name=notice.obj_name repo_url=notice.repo_url repo_name=notice.repo_name %} User {{user_name}} updated file {{obj_name}} in library {{repo_name}}.{% endblocktrans %}
{% endif %}
- {% else %}
+ {% else %}
{% if notice.op_type == 'create' %}
{% if notice.obj_path_count == 1 %}
{% blocktrans with user_url=notice.user_url user_name=notice.user_name obj_url=notice.obj_url obj_name=notice.obj_name repo_url=notice.repo_url repo_name=notice.repo_name %} User {{user_name}} created folder {{obj_name}} in library {{repo_name}}.{% endblocktrans %}
@@ -133,7 +133,7 @@
{% endif %}
{% endif %}
{% endif %}
-
+
{% endif %}
{{ notice.timestamp|date:"Y-m-d G:i:s"}} |