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 24, 2024
1 parent 2bdc26e commit d23c02b
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 7 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
81 changes: 79 additions & 2 deletions seahub/notifications/models.py
Original file line number Diff line number Diff line change
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,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 [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_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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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 %}
{% 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 %}
Expand Down Expand Up @@ -133,7 +133,7 @@
{% endif %}
{% endif %}
{% endif %}
</p>
</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 d23c02b

Please sign in to comment.