1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use std::time::Duration;

use headless_lms_utils::url_to_oembed_endpoint::{
    mentimeter_oembed_response_builder, thinglink_oembed_response_builder, url_to_oembed_endpoint,
    OEmbedRequest, OEmbedResponse,
};
use serde::{Deserialize, Serialize};

use crate::prelude::*;

#[derive(Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
pub struct ThemeSupports {
    pub responsive_embeds: bool,
}

#[derive(Deserialize, Serialize)]
pub struct ThemeResponse {
    pub theme_supports: ThemeSupports,
}

// Needed for Spotify oembed, should be fetched from env?
static APP_USER_AGENT: &str = concat!("moocfi", "/", "0.1.0",);

/**
GET `/api/v0/cms/gutenberg/oembed/preview?url=https%3A%2F%2Fyoutube.com%2Fwatch%3Fv%3D123123123` - Fetch oembed response from correct provider.
Endpoint for proxying oembed requests to correct provider using url query param.

# Example

Request:
```http
GET /api/v0/cms/gutenberg/oembed/preview?url=https%3A%2F%2Fyoutube.com%2Fwatch%3Fv%3D123123123 HTTP/1.1
Content-Type: application/json

```

Response:
```json
{
    "title":"AUTHOR - Title (OFFICIAL)",
    "author_name":"Author Name",
    "author_url":"https://www.youtube.com/author",
    "type":"video",
    "height":439,
    "width":780,
    "version":"1.0",
    "provider_name":"YouTube",
    "provider_url":"https://www.youtube.com/",
    "thumbnail_height":360,"thumbnail_width":480,
    "thumbnail_url":"https://i.ytimg.com/vi/JWBo/hqdefault.jpg",
    "html":"<iframe width=\"780\" height=\"439\" src=\"https://www.youtube.com/embed/JYjVo?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>"}
}

```
*/
#[instrument(skip(pool, app_conf))]
async fn get_oembed_data_from_provider(
    query_params: web::Query<OEmbedRequest>,
    pool: web::Data<PgPool>,
    user: AuthUser,
    app_conf: web::Data<ApplicationConfiguration>,
) -> ControllerResult<web::Json<serde_json::Value>> {
    let mut conn = pool.acquire().await?;
    let token = authorize(&mut conn, Act::Teach, Some(user.id), Res::AnyCourse).await?;
    let endpoint = url_to_oembed_endpoint(
        query_params.url.to_string(),
        Some(app_conf.base_url.to_string()),
    )?;
    let client = reqwest::Client::builder()
        .user_agent(APP_USER_AGENT)
        .build()
        .map_err(|oe| anyhow::anyhow!(oe.to_string()))?;
    let res = client
        .get(endpoint)
        .timeout(Duration::from_secs(120))
        .send()
        .await
        .map_err(|oe| {
            ControllerError::new(
                ControllerErrorType::BadRequest,
                oe.to_string(),
                Some(oe.into()),
            )
        })?;
    let status = res.status();
    if !status.is_success() {
        let response_url = res.url().to_string();
        let body = res.text().await.map_err(|oe| {
            ControllerError::new(
                ControllerErrorType::BadRequest,
                oe.to_string(),
                Some(oe.into()),
            )
        })?;
        warn!(url=?response_url, status=?status, body=?body, "Could not fetch oembed data from provider");
        return Err(ControllerError::new(
            ControllerErrorType::BadRequest,
            "Could not fetch oembed data from provider".to_string(),
            None,
        ));
    }
    let res = res.json::<serde_json::Value>().await.map_err(|oe| {
        ControllerError::new(
            ControllerErrorType::BadRequest,
            oe.to_string(),
            Some(oe.into()),
        )
    })?;
    token.authorized_ok(web::Json(res))
}

/**
GET `/api/v0/cms/gutenberg/themes?context=edit&status=active&_locale=user` - Mock themes response
Endpoint for proxying themes requests.
<https://github.com/WordPress/gutenberg/blob/trunk/packages/block-library/src/embed/test/index.native.js#L128>

# Example

Request:
```http
GET /api/v0/cms/gutenberg/themes?context=edit&status=active&_locale=user HTTP/1.1
Content-Type: application/json

```

Response:
```json
{
    {
        "theme_supports": {
                "responsive-embeds": true
            }
        }
}

```
*/
#[instrument(skip(pool))]
async fn get_theme_settings(
    pool: web::Data<PgPool>,
    user: AuthUser,
) -> ControllerResult<web::Json<ThemeResponse>> {
    let mut conn = pool.acquire().await?;
    let token = authorize(&mut conn, Act::Teach, Some(user.id), Res::AnyCourse).await?;
    let response = ThemeResponse {
        theme_supports: ThemeSupports {
            responsive_embeds: true,
        },
    };
    token.authorized_ok(web::Json(response))
}

#[instrument(skip(app_conf))]
async fn get_mentimeter_oembed_data(
    query_params: web::Query<OEmbedRequest>,
    app_conf: web::Data<ApplicationConfiguration>,
    pool: web::Data<PgPool>,
) -> ControllerResult<web::Json<OEmbedResponse>> {
    let token = skip_authorize();
    let url = query_params.url.to_string();
    let response = mentimeter_oembed_response_builder(url, app_conf.base_url.to_string())?;
    token.authorized_ok(web::Json(response))
}

#[instrument(skip(app_conf))]
async fn get_thinglink_oembed_data(
    query_params: web::Query<OEmbedRequest>,
    app_conf: web::Data<ApplicationConfiguration>,
    pool: web::Data<PgPool>,
) -> ControllerResult<web::Json<OEmbedResponse>> {
    let token = skip_authorize();
    let url = query_params.url.to_string();
    let response = thinglink_oembed_response_builder(url, app_conf.base_url.to_string())?;
    token.authorized_ok(web::Json(response))
}

/**
Add a route for each controller in this module.

The name starts with an underline in order to appear before other functions in the module documentation.

We add the routes by calling the route method instead of using the route annotations because this method preserves the function signatures for documentation.
*/
pub fn _add_routes(cfg: &mut ServiceConfig) {
    cfg.route(
        "/oembed/preview",
        web::get().to(get_oembed_data_from_provider),
    )
    .route("/themes", web::get().to(get_theme_settings))
    .route(
        "/oembed/mentimeter",
        web::get().to(get_mentimeter_oembed_data),
    )
    .route(
        "/oembed/thinglink",
        web::get().to(get_thinglink_oembed_data),
    );
}