mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-09-15 23:08:20 +00:00
feat: 站内信 (#6183)
* 添加站内信 * s * s * 添加接口 * fix * fix * 重构了一些 * 完成 * 完善 * s * s * s * s * s * s * 测试ok * 替换业务中发送消息的方式 * 修改 * s * 去掉 update 兼容 create * 添加 unread total 接口 * 调整json字段 Co-authored-by: xinwen <coderWen@126.com>
This commit is contained in:
36
apps/notifications/backends/__init__.py
Normal file
36
apps/notifications/backends/__init__.py
Normal file
@@ -0,0 +1,36 @@
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django.db import models
|
||||
|
||||
from .dingtalk import DingTalk
|
||||
from .email import Email
|
||||
from .site_msg import SiteMessage
|
||||
from .wecom import WeCom
|
||||
|
||||
|
||||
class BACKEND(models.TextChoices):
|
||||
EMAIL = 'email', _('Email')
|
||||
WECOM = 'wecom', _('WeCom')
|
||||
DINGTALK = 'dingtalk', _('DingTalk')
|
||||
SITE_MSG = 'site_msg', _('Site message')
|
||||
|
||||
@property
|
||||
def client(self):
|
||||
client = {
|
||||
self.EMAIL: Email,
|
||||
self.WECOM: WeCom,
|
||||
self.DINGTALK: DingTalk,
|
||||
self.SITE_MSG: SiteMessage
|
||||
}[self]
|
||||
return client
|
||||
|
||||
def get_account(self, user):
|
||||
return self.client.get_account(user)
|
||||
|
||||
@property
|
||||
def is_enable(self):
|
||||
return self.client.is_enable()
|
||||
|
||||
@classmethod
|
||||
def filter_enable_backends(cls, backends):
|
||||
enable_backends = [b for b in backends if cls(b).is_enable]
|
||||
return enable_backends
|
32
apps/notifications/backends/base.py
Normal file
32
apps/notifications/backends/base.py
Normal file
@@ -0,0 +1,32 @@
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
class BackendBase:
|
||||
# User 表中的字段
|
||||
account_field = None
|
||||
|
||||
# Django setting 中的字段名
|
||||
is_enable_field_in_settings = None
|
||||
|
||||
def get_accounts(self, users):
|
||||
accounts = []
|
||||
unbound_users = []
|
||||
account_user_mapper = {}
|
||||
|
||||
for user in users:
|
||||
account = getattr(user, self.account_field, None)
|
||||
if account:
|
||||
account_user_mapper[account] = user
|
||||
accounts.append(account)
|
||||
else:
|
||||
unbound_users.append(user)
|
||||
return accounts, unbound_users, account_user_mapper
|
||||
|
||||
@classmethod
|
||||
def get_account(cls, user):
|
||||
return getattr(user, cls.account_field)
|
||||
|
||||
@classmethod
|
||||
def is_enable(cls):
|
||||
enable = getattr(settings, cls.is_enable_field_in_settings)
|
||||
return bool(enable)
|
20
apps/notifications/backends/dingtalk.py
Normal file
20
apps/notifications/backends/dingtalk.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from django.conf import settings
|
||||
|
||||
from common.message.backends.dingtalk import DingTalk as Client
|
||||
from .base import BackendBase
|
||||
|
||||
|
||||
class DingTalk(BackendBase):
|
||||
account_field = 'dingtalk_id'
|
||||
is_enable_field_in_settings = 'AUTH_DINGTALK'
|
||||
|
||||
def __init__(self):
|
||||
self.dingtalk = Client(
|
||||
appid=settings.DINGTALK_APPKEY,
|
||||
appsecret=settings.DINGTALK_APPSECRET,
|
||||
agentid=settings.DINGTALK_AGENTID
|
||||
)
|
||||
|
||||
def send_msg(self, users, msg):
|
||||
accounts, __, __ = self.get_accounts(users)
|
||||
return self.dingtalk.send_text(accounts, msg)
|
14
apps/notifications/backends/email.py
Normal file
14
apps/notifications/backends/email.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from django.conf import settings
|
||||
from django.core.mail import send_mail
|
||||
|
||||
from .base import BackendBase
|
||||
|
||||
|
||||
class Email(BackendBase):
|
||||
account_field = 'email'
|
||||
is_enable_field_in_settings = 'EMAIL_HOST_USER'
|
||||
|
||||
def send_msg(self, users, subject, message):
|
||||
from_email = settings.EMAIL_FROM or settings.EMAIL_HOST_USER
|
||||
accounts, __, __ = self.get_accounts(users)
|
||||
send_mail(subject, message, from_email, accounts)
|
14
apps/notifications/backends/site_msg.py
Normal file
14
apps/notifications/backends/site_msg.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from notifications.site_msg import SiteMessage as Client
|
||||
from .base import BackendBase
|
||||
|
||||
|
||||
class SiteMessage(BackendBase):
|
||||
account_field = 'id'
|
||||
|
||||
def send_msg(self, users, subject, message):
|
||||
accounts, __, __ = self.get_accounts(users)
|
||||
Client.send_msg(subject, message, user_ids=accounts)
|
||||
|
||||
@classmethod
|
||||
def is_enable(cls):
|
||||
return True
|
20
apps/notifications/backends/wecom.py
Normal file
20
apps/notifications/backends/wecom.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from django.conf import settings
|
||||
|
||||
from common.message.backends.wecom import WeCom as Client
|
||||
from .base import BackendBase
|
||||
|
||||
|
||||
class WeCom(BackendBase):
|
||||
account_field = 'wecom_id'
|
||||
is_enable_field_in_settings = 'AUTH_WECOM'
|
||||
|
||||
def __init__(self):
|
||||
self.wecom = Client(
|
||||
corpid=settings.WECOM_CORPID,
|
||||
corpsecret=settings.WECOM_SECRET,
|
||||
agentid=settings.WECOM_AGENTID
|
||||
)
|
||||
|
||||
def send_msg(self, users, msg):
|
||||
accounts, __, __ = self.get_accounts(users)
|
||||
return self.wecom.send_text(accounts, msg)
|
Reference in New Issue
Block a user