headless_lms_server/controllers/course_material/
oembed.rs

1use crate::prelude::*;
2
3use headless_lms_utils::url_to_oembed_endpoint::{
4    OEmbedRequest, OEmbedResponse, mentimeter_oembed_response_builder,
5};
6use utoipa::{OpenApi, ToSchema};
7
8#[derive(OpenApi)]
9#[openapi(paths(get_mentimeter_oembed_data))]
10pub(crate) struct CourseMaterialOembedApiDoc;
11
12#[derive(Debug, Serialize, Deserialize, ToSchema)]
13
14pub struct CourseMaterialOEmbedResponse {
15    pub author_name: String,
16    pub author_url: String,
17    pub html: String,
18    pub provider_name: String,
19    pub provider_url: String,
20    pub title: String,
21    pub version: String,
22}
23
24impl From<OEmbedResponse> for CourseMaterialOEmbedResponse {
25    fn from(value: OEmbedResponse) -> Self {
26        Self {
27            author_name: value.author_name,
28            author_url: value.author_url,
29            html: value.html,
30            provider_name: value.provider_name,
31            provider_url: value.provider_url,
32            title: value.title,
33            version: value.version,
34        }
35    }
36}
37
38/**
39GET `/api/v0/course-material/oembed/mentimeter?url=https://menti.com/123qwerty`
40*/
41#[utoipa::path(
42    get,
43    path = "/mentimeter",
44    operation_id = "getCourseMaterialMentimeterOembed",
45    tag = "course-material-oembed",
46    params(
47        ("url" = String, Query, description = "Mentimeter URL")
48    ),
49    responses(
50        (
51            status = 200,
52            description = "Mentimeter oEmbed response",
53            body = CourseMaterialOEmbedResponse
54        )
55    )
56)]
57async fn get_mentimeter_oembed_data(
58    query_params: web::Query<OEmbedRequest>,
59    app_conf: web::Data<ApplicationConfiguration>,
60    user: AuthUser,
61    pool: web::Data<PgPool>,
62) -> ControllerResult<web::Json<CourseMaterialOEmbedResponse>> {
63    let mut conn = pool.acquire().await?;
64    let url = query_params.url.to_string();
65    let response = mentimeter_oembed_response_builder(url, app_conf.base_url.to_string())?;
66    let token = authorize(&mut conn, Act::View, Some(user.id), Res::AnyCourse).await?;
67    token.authorized_ok(web::Json(response.into()))
68}
69
70/**
71Add a route for each controller in this module.
72
73The name starts with an underline in order to appear before other functions in the module documentation.
74
75We add the routes by calling the route method instead of using the route annotations because this method preserves the function signatures for documentation.
76*/
77pub fn _add_routes(cfg: &mut ServiceConfig) {
78    cfg.route("/mentimeter", web::get().to(get_mentimeter_oembed_data));
79}