33from app .redis_service import RedisService
44from celery import app , group
55from celery .utils .log import get_task_logger
6- from dashboard .models import Profile
6+ from dashboard .models import Bounty , Profile
77from mattermostdriver import Driver
8+ from mattermostdriver .exceptions import ResourceNotFound
89
910logger = get_task_logger (__name__ )
1011
1314# Lock timeout of 2 minutes (just in the case that the application hangs to avoid a redis deadlock)
1415LOCK_TIMEOUT = 60 * 2
1516
16- chat_driver = Driver ({
17+ driver_opts = {
18+ 'scheme' : 'https' if settings .CHAT_PORT == 443 else 'http' ,
1719 'url' : settings .CHAT_URL ,
1820 'port' : settings .CHAT_PORT ,
1921 'token' : settings .CHAT_DRIVER_TOKEN
20- })
22+ }
23+
24+ chat_driver = Driver (driver_opts )
2125
2226
2327def get_driver ():
@@ -26,23 +30,36 @@ def get_driver():
2630
2731
2832@app .shared_task (bind = True , max_retries = 3 )
29- def create_channel (self , options , retry : bool = True ) -> None :
33+ def create_channel (self , options , bounty_id = None , retry : bool = True ) -> None :
3034 """
3135 :param options:
3236 :param retry:
3337 :return:
3438 """
3539 with redis .lock ("tasks:create_channel:%s" % options ['channel_name' ], timeout = LOCK_TIMEOUT ):
3640
41+ chat_driver .login ()
3742 try :
38- chat_driver .login ()
43+ channel_lookup = chat_driver .channels .get_channel_by_name (
44+ options ['team_id' ],
45+ options ['channel_name' ]
46+ )
47+ if bounty_id :
48+ active_bounty = Bounty .objects .get (id = bounty_id )
49+ active_bounty .chat_channel_id = channel_lookup ['id' ]
50+ active_bounty .save ()
51+ return channel_lookup
52+ except ResourceNotFound as RNF :
3953 new_channel = chat_driver .channels .create_channel (options = {
4054 'team_id' : options ['team_id' ],
4155 'name' : options ['channel_name' ],
4256 'display_name' : options ['channel_display_name' ],
4357 'type' : 'O'
4458 })
45-
59+ if bounty_id :
60+ active_bounty = Bounty .objects .get (id = bounty_id )
61+ active_bounty .chat_channel_id = new_channel ['id' ]
62+ active_bounty .save ()
4663 return new_channel
4764 except ConnectionError as exc :
4865 logger .info (str (exc ))
@@ -54,26 +71,25 @@ def create_channel(self, options, retry: bool = True) -> None:
5471
5572
5673@app .shared_task (bind = True , max_retries = 3 )
57- def add_to_channel (self , options , retry : bool = True ) -> None :
74+ def add_to_channel (self , channel_details , chat_user_ids , retry : bool = True ) -> None :
5875 """
59- :param options :
76+ :param channel_details :
6077 :param retry:
6178 :return:
6279 """
63- with redis .lock ("tasks:add_to_channel:%s" % options ['channel_id' ], timeout = LOCK_TIMEOUT ):
64- chat_driver .login ()
65- try :
66- for x in options ['profiles' ]:
67- if x is not None :
68- response = chat_driver .channels .add_user (options ['channel_id' ], options = {
69- 'user_id' : x
70- })
71- except ConnectionError as exc :
72- logger .info (str (exc ))
73- logger .info ("Retrying connection" )
74- self .retry (30 )
75- except Exception as e :
76- logger .error (str (e ))
80+ chat_driver .login ()
81+ try :
82+ for chat_id in chat_user_ids :
83+ if chat_id :
84+ response = chat_driver .channels .add_user (channel_details ['id' ], options = {
85+ 'user_id' : chat_id
86+ })
87+ except ConnectionError as exc :
88+ logger .info (str (exc ))
89+ logger .info ("Retrying connection" )
90+ self .retry (30 )
91+ except Exception as e :
92+ logger .error (str (e ))
7793
7894
7995@app .shared_task (bind = True , max_retries = 1 )
@@ -113,9 +129,12 @@ def update_user(self, query_opts, update_opts, retry: bool = True) -> None:
113129 try :
114130
115131 if query_opts ['chat_id' ] is None :
116- chat_user = chat_driver .users .get_user_by_username (query_opts ['handle' ])
117- if 'message' not in chat_user :
132+ try :
133+
134+ chat_user = chat_driver .users .get_user_by_username (query_opts ['handle' ])
118135 chat_id = chat_user ['id' ]
136+ except Exception as e :
137+ logger .info (f"Unable to find chat user for { query_opts ['handle' ]} " )
119138 else :
120139 chat_id = query_opts ['chat_id' ]
121140
0 commit comments