Skip to content

Commit

Permalink
function to join a room
Browse files Browse the repository at this point in the history
  • Loading branch information
burnout87 committed Nov 1, 2023
1 parent 56dcc0e commit 7a8d8fe
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions cdci_data_analysis/analysis/matrix_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,44 @@ def send_job_message(
return res_content


def join_room(
logger,
url_server=None,
sender_access_token=None,
room_id=None,
):
logger.info(f"Joining room wth id: {room_id}")
url = os.path.join(url_server, f'_matrix/client/v3/rooms/{room_id}/join')

Check warning on line 287 in cdci_data_analysis/analysis/matrix_helper.py

View check run for this annotation

Codecov / codecov/patch

cdci_data_analysis/analysis/matrix_helper.py#L286-L287

Added lines #L286 - L287 were not covered by tests

headers = {

Check warning on line 289 in cdci_data_analysis/analysis/matrix_helper.py

View check run for this annotation

Codecov / codecov/patch

cdci_data_analysis/analysis/matrix_helper.py#L289

Added line #L289 was not covered by tests
'Authorization': ' '.join(['Bearer', sender_access_token]),
'Content-type': 'application/json'
}

res = requests.post(url, headers=headers)

Check warning on line 294 in cdci_data_analysis/analysis/matrix_helper.py

View check run for this annotation

Codecov / codecov/patch

cdci_data_analysis/analysis/matrix_helper.py#L294

Added line #L294 was not covered by tests

msg_response_data = None
if res.status_code in [403, 429]:
msg_response_data = res.json()
error_code = ""
if "errcode" in msg_response_data:
error_code = msg_response_data["errcode"]
error = ""
if "error" in msg_response_data:
error = msg_response_data["error"]
logger.info(f"Could not join the room: {room_id}, for the following reason: {error_code} - {error}")

Check warning on line 305 in cdci_data_analysis/analysis/matrix_helper.py

View check run for this annotation

Codecov / codecov/patch

cdci_data_analysis/analysis/matrix_helper.py#L296-L305

Added lines #L296 - L305 were not covered by tests

sentry.capture_message(f"Could not join the room: {room_id}, for the following reason: {error_code}: {error}")
raise MatrixMessageNotSent(f"Could not join the room: {room_id}, for the following reason: {error_code}: {error}",

Check warning on line 308 in cdci_data_analysis/analysis/matrix_helper.py

View check run for this annotation

Codecov / codecov/patch

cdci_data_analysis/analysis/matrix_helper.py#L307-L308

Added lines #L307 - L308 were not covered by tests
status_code=res.status_code,
payload={'matrix_error_message': f"{error_code} - {error}"})

elif res.status_code == 200:
logger.info(f"Successfully joined the room: {room_id}")

Check warning on line 313 in cdci_data_analysis/analysis/matrix_helper.py

View check run for this annotation

Codecov / codecov/patch

cdci_data_analysis/analysis/matrix_helper.py#L312-L313

Added lines #L312 - L313 were not covered by tests

return msg_response_data

Check warning on line 315 in cdci_data_analysis/analysis/matrix_helper.py

View check run for this annotation

Codecov / codecov/patch

cdci_data_analysis/analysis/matrix_helper.py#L315

Added line #L315 was not covered by tests


def send_message(
logger,
url_server=None,
Expand Down

0 comments on commit 7a8d8fe

Please sign in to comment.