| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  | import logging | 
					
						
							|  |  |  | import time | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import click | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  | from celery import shared_task  # type: ignore | 
					
						
							| 
									
										
										
										
											2024-07-12 12:25:38 +08:00
										 |  |  | from flask import render_template | 
					
						
							| 
									
										
										
										
											2024-02-01 18:11:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-12 12:25:38 +08:00
										 |  |  | from configs import dify_config | 
					
						
							| 
									
										
										
										
											2024-02-06 13:21:13 +08:00
										 |  |  | from extensions.ext_mail import mail | 
					
						
							| 
									
										
										
										
											2025-05-20 12:07:50 +08:00
										 |  |  | from services.feature_service import FeatureService | 
					
						
							| 
									
										
										
										
											2024-02-06 13:21:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 13:38:37 +08:00
										 |  |  | @shared_task(queue="mail") | 
					
						
							| 
									
										
										
										
											2023-12-21 11:09:41 +08:00
										 |  |  | def send_invite_member_mail_task(language: str, to: str, token: str, inviter_name: str, workspace_name: str): | 
					
						
							| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  |     """
 | 
					
						
							|  |  |  |     Async Send invite member mail | 
					
						
							| 
									
										
										
										
											2023-12-21 11:09:41 +08:00
										 |  |  |     :param language | 
					
						
							| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  |     :param to | 
					
						
							|  |  |  |     :param token | 
					
						
							|  |  |  |     :param inviter_name | 
					
						
							|  |  |  |     :param workspace_name | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-02 09:41:45 +09:00
										 |  |  |     Usage: send_invite_member_mail_task.delay(language, to, token, inviter_name, workspace_name) | 
					
						
							| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  |     """
 | 
					
						
							|  |  |  |     if not mail.is_inited(): | 
					
						
							|  |  |  |         return | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 13:38:37 +08:00
										 |  |  |     logging.info( | 
					
						
							|  |  |  |         click.style("Start send invite member mail to {} in workspace {}".format(to, workspace_name), fg="green") | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  |     start_at = time.perf_counter() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     # send invite member mail using different languages | 
					
						
							| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  |     try: | 
					
						
							| 
									
										
										
										
											2024-08-26 13:38:37 +08:00
										 |  |  |         url = f"{dify_config.CONSOLE_WEB_URL}/activate?token={token}" | 
					
						
							|  |  |  |         if language == "zh-Hans": | 
					
						
							| 
									
										
										
										
											2025-05-20 12:07:50 +08:00
										 |  |  |             template = "invite_member_mail_template_zh-CN.html" | 
					
						
							|  |  |  |             system_features = FeatureService.get_system_features() | 
					
						
							|  |  |  |             if system_features.branding.enabled: | 
					
						
							|  |  |  |                 application_title = system_features.branding.application_title | 
					
						
							|  |  |  |                 template = "without-brand/invite_member_mail_template_zh-CN.html" | 
					
						
							|  |  |  |                 html_content = render_template( | 
					
						
							|  |  |  |                     template, | 
					
						
							|  |  |  |                     to=to, | 
					
						
							|  |  |  |                     inviter_name=inviter_name, | 
					
						
							|  |  |  |                     workspace_name=workspace_name, | 
					
						
							|  |  |  |                     url=url, | 
					
						
							|  |  |  |                     application_title=application_title, | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |                 mail.send(to=to, subject=f"立即加入 {application_title} 工作空间", html=html_content) | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 html_content = render_template( | 
					
						
							|  |  |  |                     template, to=to, inviter_name=inviter_name, workspace_name=workspace_name, url=url | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |                 mail.send(to=to, subject="立即加入 Dify 工作空间", html=html_content) | 
					
						
							| 
									
										
										
										
											2023-12-21 11:09:41 +08:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2025-05-20 12:07:50 +08:00
										 |  |  |             template = "invite_member_mail_template_en-US.html" | 
					
						
							|  |  |  |             system_features = FeatureService.get_system_features() | 
					
						
							|  |  |  |             if system_features.branding.enabled: | 
					
						
							|  |  |  |                 application_title = system_features.branding.application_title | 
					
						
							|  |  |  |                 template = "without-brand/invite_member_mail_template_en-US.html" | 
					
						
							|  |  |  |                 html_content = render_template( | 
					
						
							|  |  |  |                     template, | 
					
						
							|  |  |  |                     to=to, | 
					
						
							|  |  |  |                     inviter_name=inviter_name, | 
					
						
							|  |  |  |                     workspace_name=workspace_name, | 
					
						
							|  |  |  |                     url=url, | 
					
						
							|  |  |  |                     application_title=application_title, | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |                 mail.send(to=to, subject=f"Join {application_title} Workspace Now", html=html_content) | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 html_content = render_template( | 
					
						
							|  |  |  |                     template, to=to, inviter_name=inviter_name, workspace_name=workspace_name, url=url | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |                 mail.send(to=to, subject="Join Dify Workspace Now", html=html_content) | 
					
						
							| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         end_at = time.perf_counter() | 
					
						
							|  |  |  |         logging.info( | 
					
						
							| 
									
										
										
										
											2024-08-26 13:38:37 +08:00
										 |  |  |             click.style( | 
					
						
							|  |  |  |                 "Send invite member mail to {} succeeded: latency: {}".format(to, end_at - start_at), fg="green" | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  |     except Exception: | 
					
						
							| 
									
										
										
										
											2024-08-26 13:38:37 +08:00
										 |  |  |         logging.exception("Send invite member mail to {} failed".format(to)) |