Skip to content

Commit

Permalink
refactor: Role has its own class
Browse files Browse the repository at this point in the history
  • Loading branch information
azmeuk committed Mar 27, 2024
1 parent 87ff703 commit f5f7293
Show file tree
Hide file tree
Showing 18 changed files with 153 additions and 75 deletions.
31 changes: 29 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ gunicorn = "21.2.0"
netaddr = "^1.2.1"
psycopg2 = "^2.9.3"
pydantic-settings = "^2.1.0"
python-slugify = "^8.0.4"
redis = "^5.0.1"
requests = "^2.27.1"
sqlalchemy = "^1.4.31"
Expand Down
7 changes: 7 additions & 0 deletions web/b3desk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from b3desk.settings import MainSettings
from b3desk.utils import is_rie

from .utils import enum_converter
from .utils import model_converter

__version__ = "0.0.0"
Expand Down Expand Up @@ -109,6 +110,7 @@ def setup_database(app):


def setup_jinja(app):
from b3desk.models.meetings import Role
from b3desk.session import get_current_user
from b3desk.session import has_user_session

Expand Down Expand Up @@ -136,6 +138,7 @@ def global_processor():
"is_rie": is_rie(),
"version": __version__,
"LANGUAGES": LANGUAGES,
"Role": Role,
**app.config["WORDINGS"],
**session_dict,
}
Expand All @@ -144,11 +147,15 @@ def global_processor():
def setup_flask(app):
with app.app_context():
from b3desk.models.meetings import Meeting
from b3desk.models.meetings import Role
from b3desk.models.users import User

for model in (Meeting, User):
app.url_map.converters[model.__name__.lower()] = model_converter(model)

for enum in (Role,):
app.url_map.converters[enum.__name__.lower()] = enum_converter(enum)


def setup_error_pages(app):
@app.errorhandler(400)
Expand Down
5 changes: 3 additions & 2 deletions web/b3desk/endpoints/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from flask import Blueprint
from flask import request

from b3desk.models.meetings import Role
from b3desk.models.users import get_or_create_user

from .. import auth
Expand All @@ -20,8 +21,8 @@ def api_meetings():
"meetings": [
{
"name": meeting.name,
"moderator_url": meeting.get_signin_url("moderator"),
"attendee_url": meeting.get_signin_url("attendee"),
"moderator_url": meeting.get_signin_url(Role.moderator),
"attendee_url": meeting.get_signin_url(Role.attendee),
}
for meeting in user.meetings
]
Expand Down
18 changes: 8 additions & 10 deletions web/b3desk/endpoints/join.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from b3desk.forms import JoinMeetingForm
from b3desk.models import db
from b3desk.models.meetings import Meeting
from b3desk.models.meetings import Role
from b3desk.models.meetings import get_mail_meeting
from b3desk.models.meetings import get_meeting_from_meeting_id_and_user_id

Expand Down Expand Up @@ -60,7 +61,7 @@ def signin_mail_meeting(meeting_fake_id, expiration, h):
expiration=expiration,
user_id="fakeuserId",
h=h,
role="moderator",
role=Role.moderator,
)


Expand All @@ -81,7 +82,7 @@ def signin_meeting(meeting_fake_id, creator, h):
current_user_id = get_current_user().id if has_user_session() else None
role = meeting.get_role(h, current_user_id)

if role == "authenticated":
if role == Role.authenticated:
return redirect(
url_for("join.join_meeting_as_authenticated", meeting_id=meeting_fake_id)
)
Expand Down Expand Up @@ -162,7 +163,7 @@ def join_meeting():
current_user_id = get_current_user().id if has_user_session() else None
role = meeting.get_role(h, current_user_id)
fullname_suffix = form["fullname_suffix"].data
if role == "authenticated":
if role == Role.authenticated:
fullname = get_authenticated_attendee_fullname()
elif not role:
return redirect(url_for("public.index"))
Expand Down Expand Up @@ -210,15 +211,15 @@ def join_mail_meeting():
flash(_("Lien expiré"), "error")
return redirect(url_for("public.index"))

return redirect(meeting.get_join_url("moderator", fullname, create=True))
return redirect(meeting.get_join_url(Role.moderator, fullname, create=True))


# Cannot use a flask converter here because sometimes 'meeting_id' is a 'fake_id'
@bp.route("/meeting/join/<int:meeting_id>/authenticated")
@auth.oidc_auth("attendee")
def join_meeting_as_authenticated(meeting_id):
meeting = db.session.get(Meeting, meeting_id) or abort(404)
role = "authenticated"
role = Role.authenticated
fullname = get_authenticated_attendee_fullname()
return redirect(
url_for(
Expand All @@ -231,11 +232,8 @@ def join_meeting_as_authenticated(meeting_id):
)


@bp.route("/meeting/join/<meeting:meeting>/<role>")
@bp.route("/meeting/join/<meeting:meeting>/<role:role>")
@auth.oidc_auth("default")
@meeting_owner_needed
def join_meeting_as_role(meeting, role, owner):
if role not in ("attendee", "moderator"):
abort(404)

def join_meeting_as_role(meeting, role: Role, owner):
return redirect(meeting.get_join_url(role, owner.fullname, create=True))
13 changes: 7 additions & 6 deletions web/b3desk/endpoints/meetings.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from b3desk.forms import ShowMeetingForm
from b3desk.models import db
from b3desk.models.meetings import Meeting
from b3desk.models.meetings import Role
from b3desk.models.meetings import get_quick_meeting_from_user_and_random_string
from b3desk.models.users import User

Expand All @@ -38,14 +39,14 @@
bp = Blueprint("meetings", __name__)


def meeting_mailto_params(meeting, role):
if role == "moderator":
def meeting_mailto_params(meeting, role: Role):
if role == Role.moderator:
return render_template(
"meeting/mailto/mail_href.txt", meeting=meeting, role="moderator"
"meeting/mailto/mail_href.txt", meeting=meeting, role=role
).replace("\n", "%0D%0A")
elif role == "attendee":
elif role == Role.attendee:
return render_template(
"meeting/mailto/mail_href.txt", meeting=meeting, role="attendee"
"meeting/mailto/mail_href.txt", meeting=meeting, role=role
).replace("\n", "%0D%0A")


Expand Down Expand Up @@ -83,7 +84,7 @@ def quick_mail_meeting():
def quick_meeting():
user = get_current_user()
meeting = get_quick_meeting_from_user_and_random_string(user)
return redirect(meeting.get_join_url("moderator", user.fullname, create=True))
return redirect(meeting.get_join_url(Role.moderator, user.fullname, create=True))


@bp.route("/meeting/show/<meeting:meeting>")
Expand Down
15 changes: 10 additions & 5 deletions web/b3desk/models/bbb.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ def is_meeting_running(self):

def create(self):
"""https://docs.bigbluebutton.org/development/api/#create"""
from .meetings import Role

params = {
"meetingID": self.meeting.meetingID,
"name": self.meeting.name,
Expand Down Expand Up @@ -161,9 +163,9 @@ def create(self):
"meeting/signin_links.html",
moderator_message=self.meeting.moderatorOnlyMessage,
moderator_link_introduction=quick_meeting_moderator_link_introduction,
moderator_signin_url=self.meeting.get_signin_url("moderator"),
moderator_signin_url=self.meeting.get_signin_url(Role.moderator),
attendee_link_introduction=quick_meeting_attendee_link_introduction,
attendee_signin_url=self.meeting.get_signin_url("attendee"),
attendee_signin_url=self.meeting.get_signin_url(Role.attendee),
)
params["guestPolicy"] = (
"ASK_MODERATOR" if self.meeting.guestPolicy else "ALWAYS_ACCEPT"
Expand Down Expand Up @@ -267,17 +269,20 @@ def update_recordings(self, recording_ids, metadata):

def prepare_request_to_join_bbb(self, meeting_role, fullname):
"""https://docs.bigbluebutton.org/dev/api.html#join"""

from .meetings import Role

params = {
"fullName": fullname,
"meetingID": self.meeting.meetingID,
"redirect": "true",
}
if meeting_role == "attendee":
if meeting_role == Role.attendee:
params["role"] = "viewer"
params["guest"] = "true"
elif meeting_role == "authenticated":
elif meeting_role == Role.authenticated:
params["role"] = "viewer"
elif meeting_role == "moderator":
elif meeting_role == Role.moderator:
params["role"] = "moderator"

return self.bbb_request("join", params=params)
Expand Down
35 changes: 22 additions & 13 deletions web/b3desk/models/meetings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import hashlib
from datetime import datetime
from datetime import timedelta
from enum import StrEnum

from flask import current_app
from flask import url_for
Expand All @@ -25,6 +26,12 @@
MODERATOR_ONLY_MESSAGE_MAXLENGTH = 150


class Role(StrEnum):
attendee = "attendee"
moderator = "moderator"
authenticated = "authenticated"


class MeetingFiles(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.Unicode(4096))
Expand Down Expand Up @@ -154,7 +161,7 @@ def fake_id(self, fake_value):
def fake_id(self):
del self._fake_id

def get_hash(self, role):
def get_hash(self, role: Role):
s = f"{self.meetingID}|{self.attendeePW}|{self.name}|{role}"
return hashlib.sha1(s.encode("utf-8")).hexdigest()

Expand Down Expand Up @@ -194,10 +201,12 @@ def update_recording_name(self, recording_id, name):
recording_ids=[recording_id], metadata={"name": name}
)

def get_join_url(self, meeting_role, fullname, fullname_suffix="", create=False):
def get_join_url(
self, meeting_role: Role, fullname, fullname_suffix="", create=False
):
is_meeting_available = self.is_running()
should_create_room = (
not is_meeting_available and (meeting_role == "moderator") and create
not is_meeting_available and (meeting_role == Role.moderator) and create
)
if should_create_room:
data = self.create_bbb()
Expand All @@ -219,7 +228,7 @@ def get_join_url(self, meeting_role, fullname, fullname_suffix="", create=False)
fullname_suffix=fullname_suffix,
)

def get_signin_url(self, meeting_role):
def get_signin_url(self, meeting_role: Role):
return url_for(
"join.signin_meeting",
meeting_fake_id=self.fake_id,
Expand Down Expand Up @@ -247,18 +256,18 @@ def get_mail_signin_url(self):
_external=True,
)

def get_role(self, hashed_role, user_id=None):
def get_role(self, hashed_role, user_id=None) -> Role | None:
if user_id and self.user.id == user_id:
return "moderator"
elif self.get_hash("attendee") == hashed_role:
role = "attendee"
elif self.get_hash("moderator") == hashed_role:
role = "moderator"
elif self.get_hash("authenticated") == hashed_role:
return Role.moderator
elif self.get_hash(Role.attendee) == hashed_role:
role = Role.attendee
elif self.get_hash(Role.moderator) == hashed_role:
role = Role.moderator
elif self.get_hash(Role.authenticated) == hashed_role:
role = (
"authenticated"
Role.authenticated
if current_app.config["OIDC_ATTENDEE_ENABLED"]
else "attendee"
else Role.attendee
)
else:
role = None
Expand Down
8 changes: 4 additions & 4 deletions web/b3desk/templates/meeting/join.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ <h1 class="fr-h2">{% trans %}Rejoindre {{ the_meeting }}{% endtrans %}</h1>
<div class="fr-grid-row fr-grid-row--gutters">
<div class="fr-col">
<label for="fullname" class="fr-label" aria-describedby="text-input-desc">{% trans %}Votre nom{% endtrans %}</label>
{% if role != "authenticated" %}<p class="fr-hint-text" id="text-input-desc">{% trans %}Vous pouvez également préciser votre service ou votre fonction.{% endtrans %}</p>{% endif %}
{% if role != Role.authenticated %}<p class="fr-hint-text" id="text-input-desc">{% trans %}Vous pouvez également préciser votre service ou votre fonction.{% endtrans %}</p>{% endif %}
<input type="text" required class="fr-input" id="fullname" name="fullname" {% if fullname %}value="{{ fullname }}"{% endif %} {% if fullname and not user %}readonly="readonly"{% endif %} />
</div>
</div>

{% if role == "authenticated" %}
{% if role == Role.authenticated %}
<div class="fr-grid-row fr-grid-row--gutters">
<div class="fr-col">
<label for="fullname_suffix" class="fr-label" aria-describedby="text-input-desc">Informations facultatives</label>
Expand All @@ -39,7 +39,7 @@ <h1 class="fr-h2">{% trans %}Rejoindre {{ the_meeting }}{% endtrans %}</h1>
</div>
</div>

{% if role != "authenticated" and not user %}
{% if role != Role.authenticated and not user %}
<div class="fr-grid-row fr-grid-row--gutters">
<div class="fr-col">
{% trans %}Vous êtes propriétaire de cette salle ?{% endtrans %}
Expand All @@ -52,7 +52,7 @@ <h1 class="fr-h2">{% trans %}Rejoindre {{ the_meeting }}{% endtrans %}</h1>
</div>
</form>

{% if role == "authenticated" %}
{% if role == Role.authenticated %}
<script>
document.addEventListener('keyup', updatePreview);

Expand Down
Loading

0 comments on commit f5f7293

Please sign in to comment.