Skip to content

Commit

Permalink
send repo monitor notice to dingtalk
Browse files Browse the repository at this point in the history
  • Loading branch information
imwhatiam committed Oct 25, 2024
1 parent 6bac5d1 commit c7130a4
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

# https://ding-doc.dingtalk.com/doc#/serverapi3/wvdxel

########## Utility Functions ##########
# Utility Functions
def remove_html_a_element(s):
"""
Replace <a ..>xx</a> to xx and wrap content with <div></div>.
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down
32 changes: 1 addition & 31 deletions seahub/notifications/management/commands/send_notices.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,40 +248,10 @@ def format_deleted_files_msg(self, notice):
return notice

def format_repo_monitor_msg(self, notice):

d = json.loads(notice.detail)

op_user_email = d['op_user']
notice.user_url = reverse('user_profile', args=[op_user_email])
notice.user_name = email2nickname(op_user_email)
notice.avatar_src = self.get_avatar_src(op_user_email)

notice.op_type = d['op_type']

repo_id = d['repo_id']
repo_name = d['repo_name']
notice.repo_url = reverse('lib_view', args=[repo_id, repo_name, ''])
notice.repo_name = d['repo_name']

obj_type = d['obj_type']
obj_path_list = d['obj_path_list']
notice.obj_type = obj_type
notice.obj_path_count = len(obj_path_list)
notice.obj_path_count_minus_one = len(obj_path_list) - 1
notice.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:
notice.old_obj_name = os.path.basename(d['old_obj_path_list'][0])
else:
notice.old_obj_name = ''

if obj_type == 'file':
notice.obj_url = reverse('view_lib_file', args=[repo_id, obj_path_list[0]])
else:
notice.obj_url = reverse('lib_view',
args=[repo_id, repo_name, obj_path_list[0].strip('/')])

notice.repo_monitor_msg = notice.format_msg()
return notice

def format_saml_sso_error_msg(self, notice):
Expand Down
109 changes: 106 additions & 3 deletions seahub/notifications/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from seahub.base.fields import LowerCaseCharField
from seahub.base.templatetags.seahub_tags import email2nickname
from seahub.invitations.models import Invitation
from seahub.utils import normalize_cache_key
from seahub.utils import normalize_cache_key, get_site_scheme_and_netloc
from seahub.constants import HASH_URLS
from seahub.drafts.models import DraftReviewer
from seahub.file_participants.utils import list_file_participants
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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 ''

Expand Down Expand Up @@ -877,6 +879,107 @@ 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 [email protected]',
# 'op_user': '[email protected]',
# '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_id = d['repo_id']
repo_name = escape(d['repo_name'])
nickname = escape(email2nickname(d['op_user']))

op_type = d['op_type']

obj_type = _('file') if d['obj_type'] == 'file' else _('folder')
obj_path_list = d['obj_path_list']
obj_path = obj_path_list[0]
obj_name = escape(os.path.basename(obj_path))

obj_path_count = len(obj_path_list)
obj_path_count_minus_one = len(obj_path_list) - 1

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 = ''

def get_repo_url(repo_id, repo_name):
p = reverse('lib_view', args=[repo_id, repo_name, ''])
return get_site_scheme_and_netloc() + p

def get_file_url(repo_id, file_path):
p = reverse('view_lib_file', args=[repo_id, file_path])
return get_site_scheme_and_netloc() + p

def get_dir_url(repo_id, repo_name, dir_path):
p = reverse('lib_view', args=[repo_id, repo_name, dir_path.strip('/')])
return get_site_scheme_and_netloc() + p

repo_url = get_repo_url(repo_id, repo_name)
repo_link = f'<a href="{repo_url}">{repo_name}</a>'

if obj_type == 'file':
file_url = get_file_url(repo_id, obj_path)
obj_link = f'<a href="{file_url}">{obj_name}</a>'
else:
folder_url = get_dir_url(repo_id, repo_name, obj_path)
obj_link = f'<a href="{folder_url}">{obj_name}</a>'

if op_type == 'create':

if obj_path_count == 1:
message = _(f'{nickname} created {obj_type} {obj_link} in library {repo_link}.')
else:
message = _(f'{nickname} created {obj_type} {obj_link} and {obj_path_count_minus_one} other {obj_type}(s) in library {repo_link}.')

elif op_type == 'delete':

if obj_path_count == 1:
message = _(f'{nickname} deleted {obj_type} {obj_name} in library {repo_link}.')
else:
message = _(f'{nickname} deleted {obj_type} {obj_name} and {obj_path_count_minus_one} other {obj_type}(s) in library {repo_link}.')

elif op_type == 'recover':

message = _(f'{nickname} restored {obj_type} {obj_link} in library {repo_link}.')

elif op_type == 'rename':

message = _(f'{nickname} renamed {obj_type} {old_obj_name} to {obj_link} in library {repo_link}.')

elif op_type == 'move':

if obj_path_count == 1:
message = _(f'{nickname} moved {obj_type} {obj_link} in library {repo_link}.')
else:
message = _(f'{nickname} moved {obj_type} {obj_link} and {obj_path_count_minus_one} other {obj_type}(s) in library {repo_link}.')

elif op_type == 'edit':

message = _(f'{nickname} updated {obj_type} {obj_link} in library {repo_link}.')

else:
message = _(f'{nickname} {op_type} {obj_type} {obj_link} in library {repo_link}.')

return message


########## handle signals
from django.dispatch import receiver
Expand Down
63 changes: 1 addition & 62 deletions seahub/notifications/templates/notifications/notice_email.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,68 +72,7 @@
<p style="line-height:1.5; margin:.2em 10px .2em 0;">{{notice.error_msg}}</p>

{% elif notice.is_repo_monitor_msg %}
<p style="line-height:1.5; margin:.2em 10px .2em 0;">
{% if notice.obj_type == 'file' %}
{% 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 <a href="{{url_base}}{{user_url}}">{{user_name}}</a> created file <a href="{{url_base}}{{obj_url}}">{{obj_name}}</a> in library <a href="{{url_base}}{{repo_url}}">{{repo_name}}</a>.{% endblocktrans %}
{% else %}
{% 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 obj_path_count_minus_one=notice.obj_path_count_minus_one %} User <a href="{{url_base}}{{user_url}}">{{user_name}}</a> created file <a href="{{url_base}}{{obj_url}}">{{obj_name}}</a> and {{ obj_path_count_minus_one }} other file(s) in library <a href="{{url_base}}{{repo_url}}">{{repo_name}}</a>.{% endblocktrans %}
{% endif %}
{% endif %}
{% if notice.op_type == 'delete' %}
{% 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 <a href="{{url_base}}{{user_url}}">{{user_name}}</a> deleted file {{obj_name}} in library <a href="{{url_base}}{{repo_url}}">{{repo_name}}</a>.{% endblocktrans %}
{% else %}
{% 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 obj_path_count_minus_one=notice.obj_path_count_minus_one %} User <a href="{{url_base}}{{user_url}}">{{user_name}}</a> deleted file {{obj_name}} and {{ obj_path_count_minus_one }} other file(s) in library <a href="{{url_base}}{{repo_url}}">{{repo_name}}</a>.{% endblocktrans %}
{% endif %}
{% endif %}
{% if notice.op_type == 'recover' %}
{% 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 <a href="{{url_base}}{{user_url}}">{{user_name}}</a> restored file <a href="{{url_base}}{{obj_url}}">{{obj_name}}</a> in library <a href="{{url_base}}{{repo_url}}">{{repo_name}}</a>.{% endblocktrans %}
{% endif %}
{% if notice.op_type == 'rename' %}
{% 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 old_obj_name=notice.old_obj_name %} User <a href="{{url_base}}{{user_url}}">{{user_name}}</a> renamed file {{old_obj_name}} to <a href="{{url_base}}{{obj_url}}">{{obj_name}}</a> in library <a href="{{url_base}}{{repo_url}}">{{repo_name}}</a>.{% endblocktrans %}
{% endif %}
{% if notice.op_type == 'move' %}
{% 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 <a href="{{url_base}}{{user_url}}">{{user_name}}</a> moved file <a href="{{url_base}}{{obj_url}}">{{obj_name}}</a> in library <a href="{{url_base}}{{repo_url}}">{{repo_name}}</a>.{% endblocktrans %}
{% else %}
{% 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 obj_path_count_minus_one=notice.obj_path_count_minus_one %} User <a href="{{url_base}}{{user_url}}">{{user_name}}</a> moved file <a href="{{url_base}}{{obj_url}}">{{obj_name}}</a> and {{ obj_path_count_minus_one }} other file(s) in library <a href="{{url_base}}{{repo_url}}">{{repo_name}}</a>.{% endblocktrans %}
{% endif %}
{% endif %}
{% 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 <a href="{{url_base}}{{user_url}}">{{user_name}}</a> updated file <a href="{{url_base}}{{obj_url}}">{{obj_name}}</a> in library <a href="{{url_base}}{{repo_url}}">{{repo_name}}</a>.{% endblocktrans %}
{% endif %}
{% 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 <a href="{{url_base}}{{user_url}}">{{user_name}}</a> created folder <a href="{{url_base}}{{obj_url}}">{{obj_name}}</a> in library <a href="{{url_base}}{{repo_url}}">{{repo_name}}</a>.{% endblocktrans %}
{% else %}
{% 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 obj_path_count_minus_one=notice.obj_path_count_minus_one %} User <a href="{{url_base}}{{user_url}}">{{user_name}}</a> created folder <a href="{{url_base}}{{obj_url}}">{{obj_name}}</a> and {{ obj_path_count_minus_one }} other folder(s) in library <a href="{{url_base}}{{repo_url}}">{{repo_name}}</a>.{% endblocktrans %}
{% endif %}
{% endif %}
{% if notice.op_type == 'delete' %}
{% 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 <a href="{{url_base}}{{user_url}}">{{user_name}}</a> deleted folder {{obj_name}} in library <a href="{{url_base}}{{repo_url}}">{{repo_name}}</a>.{% endblocktrans %}
{% else %}
{% 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 obj_path_count_minus_one=notice.obj_path_count_minus_one %} User <a href="{{url_base}}{{user_url}}">{{user_name}}</a> deleted folder {{obj_name}} and {{ obj_path_count_minus_one }} other folder(s) in library <a href="{{url_base}}{{repo_url}}">{{repo_name}}</a>.{% endblocktrans %}
{% endif %}
{% endif %}
{% if notice.op_type == 'recover' %}
{% 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 <a href="{{url_base}}{{user_url}}">{{user_name}}</a> restored folder <a href="{{url_base}}{{obj_url}}">{{obj_name}}</a> in library <a href="{{url_base}}{{repo_url}}">{{repo_name}}</a>.{% endblocktrans %}
{% endif %}
{% if notice.op_type == 'rename' %}
{% 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 old_obj_name=notice.old_obj_name %} User <a href="{{url_base}}{{user_url}}">{{user_name}}</a> renamed folder {{old_obj_name}} to <a href="{{url_base}}{{obj_url}}">{{obj_name}}</a> in library <a href="{{url_base}}{{repo_url}}">{{repo_name}}</a>.{% endblocktrans %}
{% endif %}
{% if notice.op_type == 'move' %}
{% 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 <a href="{{url_base}}{{user_url}}">{{user_name}}</a> moved folder <a href="{{url_base}}{{obj_url}}">{{obj_name}}</a> in library <a href="{{url_base}}{{repo_url}}">{{repo_name}}</a>.{% endblocktrans %}
{% else %}
{% 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 obj_path_count_minus_one=notice.obj_path_count_minus_one %} User <a href="{{url_base}}{{user_url}}">{{user_name}}</a> moved folder <a href="{{url_base}}{{obj_url}}">{{obj_name}}</a> and {{ obj_path_count_minus_one }} other folder(s) in library <a href="{{url_base}}{{repo_url}}">{{repo_name}}</a>.{% endblocktrans %}
{% endif %}
{% endif %}
{% endif %}
</p>
<p style="line-height:1.5; margin:.2em 10px .2em 0;">{{notice.repo_monitor_msg|safe}}</p>
{% endif %}
</td>
<td style="padding: 5px 3px; border-bottom: 1px solid #eee; font-size: 13px; color: #333; word-wrap: break-word;">{{ notice.timestamp|date:"Y-m-d G:i:s"}}</td>
Expand Down

0 comments on commit c7130a4

Please sign in to comment.