Skip to main content

headless_lms_models/
proposed_page_edits.rs

1use std::collections::{HashMap, hash_map::Entry};
2
3use futures::future::BoxFuture;
4use headless_lms_utils::{document_schema_processor::GutenbergBlock, merge_edits};
5use serde_json::Value;
6use url::Url;
7use utoipa::ToSchema;
8
9use crate::{
10    SpecFetcher,
11    exercise_service_info::ExerciseServiceInfoApi,
12    page_history::HistoryChangeReason,
13    pages::{CmsPageUpdate, PageUpdateArgs},
14    prelude::*,
15    proposed_block_edits::{
16        BlockProposal, BlockProposalAction, BlockProposalInfo, EditedBlockNoLongerExistsData,
17        EditedBlockStillExistsData, NewProposedBlockEdit, ProposalStatus,
18    },
19};
20
21#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Eq, ToSchema)]
22
23pub struct NewProposedPageEdits {
24    pub page_id: Uuid,
25    pub block_edits: Vec<NewProposedBlockEdit>,
26}
27
28#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Eq, ToSchema)]
29pub struct ProposedPageEdit {
30    pub id: Uuid,
31    pub course_id: Uuid,
32    pub page_id: Uuid,
33    pub user_id: Option<Uuid>,
34    pub pending: bool,
35    pub created_at: DateTime<Utc>,
36    pub updated_at: DateTime<Utc>,
37    pub deleted_at: Option<DateTime<Utc>>,
38}
39
40#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Eq, ToSchema)]
41
42pub struct PageProposal {
43    pub id: Uuid,
44    pub page_id: Uuid,
45    pub user_id: Option<Uuid>,
46    pub pending: bool,
47    pub created_at: DateTime<Utc>,
48    pub block_proposals: Vec<BlockProposal>,
49    pub page_title: String,
50    pub page_url_path: String,
51}
52
53#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Eq, ToSchema)]
54
55pub struct EditProposalInfo {
56    pub page_id: Uuid,
57    pub page_proposal_id: Uuid,
58    pub block_proposals: Vec<BlockProposalInfo>,
59}
60
61#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Eq, ToSchema)]
62
63pub struct ProposalCount {
64    pub pending: u32,
65    pub handled: u32,
66}
67
68pub async fn insert(
69    conn: &mut PgConnection,
70    pkey_policy: PKeyPolicy<Uuid>,
71    course_id: Uuid,
72    user_id: Option<Uuid>,
73    edits: &NewProposedPageEdits,
74) -> ModelResult<(Uuid, Vec<Uuid>)> {
75    if edits.block_edits.is_empty() {
76        return Err(model_err!(Generic, "No block edits".to_string()));
77    }
78
79    let mut tx = conn.begin().await?;
80    let page_res = sqlx::query!(
81        "
82INSERT INTO proposed_page_edits (id, course_id, page_id, user_id)
83VALUES ($1, $2, $3, $4)
84RETURNING id
85        ",
86        pkey_policy.into_uuid(),
87        course_id,
88        edits.page_id,
89        user_id,
90    )
91    .fetch_one(&mut *tx)
92    .await?;
93
94    let mut block_ids = vec![];
95    for block_edit in &edits.block_edits {
96        let res = sqlx::query!(
97            "
98INSERT INTO proposed_block_edits (
99  proposal_id,
100  block_id,
101  block_attribute,
102  original_text,
103  changed_text
104)
105VALUES ($1, $2, $3, $4, $5)
106RETURNING id
107",
108            page_res.id,
109            block_edit.block_id,
110            block_edit.block_attribute,
111            block_edit.original_text,
112            block_edit.changed_text
113        )
114        .fetch_one(&mut *tx)
115        .await?;
116        block_ids.push(res.id);
117    }
118    tx.commit().await?;
119    Ok((page_res.id, block_ids))
120}
121
122pub async fn get_by_id(conn: &mut PgConnection, id: Uuid) -> ModelResult<ProposedPageEdit> {
123    let res = sqlx::query_as!(
124        ProposedPageEdit,
125        r#"
126SELECT *
127FROM proposed_page_edits
128WHERE id = $1
129  AND deleted_at IS NULL
130        "#,
131        id
132    )
133    .fetch_one(conn)
134    .await?;
135    Ok(res)
136}
137
138pub async fn create_for_page_id_and_course_id(
139    conn: &mut PgConnection,
140    pkey_policy: PKeyPolicy<Uuid>,
141    course_id: Uuid,
142    user_id: Option<Uuid>,
143    edits: &NewProposedPageEdits,
144) -> ModelResult<(Uuid, Vec<Uuid>)> {
145    if edits.block_edits.is_empty() {
146        return Err(model_err!(Generic, "No block edits".to_string()));
147    }
148
149    let mut tx = conn.begin().await?;
150    let page_res = sqlx::query!(
151        r#"
152INSERT INTO proposed_page_edits (id, course_id, page_id, user_id)
153SELECT $1, $2, pages.id, $4
154FROM pages
155WHERE pages.id = $3
156  AND pages.course_id = $2
157  AND pages.deleted_at IS NULL
158RETURNING id
159        "#,
160        pkey_policy.into_uuid(),
161        course_id,
162        edits.page_id,
163        user_id
164    )
165    .fetch_one(&mut *tx)
166    .await?;
167
168    let mut block_ids = vec![];
169    for block_edit in &edits.block_edits {
170        let res = sqlx::query!(
171            "
172INSERT INTO proposed_block_edits (
173  proposal_id,
174  block_id,
175  block_attribute,
176  original_text,
177  changed_text
178)
179VALUES ($1, $2, $3, $4, $5)
180RETURNING id
181",
182            page_res.id,
183            block_edit.block_id,
184            block_edit.block_attribute,
185            block_edit.original_text,
186            block_edit.changed_text
187        )
188        .fetch_one(&mut *tx)
189        .await?;
190        block_ids.push(res.id);
191    }
192    tx.commit().await?;
193    Ok((page_res.id, block_ids))
194}
195
196pub async fn get_proposals_for_course(
197    conn: &mut PgConnection,
198    course_id: Uuid,
199    pending: bool,
200    pagination: Pagination,
201) -> ModelResult<Vec<PageProposal>> {
202    let res = sqlx::query!(
203        r#"
204SELECT proposed_page_edits.id AS "page_proposal_id!",
205  proposed_block_edits.id AS "block_proposal_id!",
206  page_id as "page_id!",
207  user_id,
208  block_id,
209  original_text,
210  changed_text,
211  proposed_page_edits.pending as "pending!",
212  block_attribute,
213  proposed_block_edits.status as "block_proposal_status: ProposalStatus",
214  proposed_page_edits.created_at as "created_at!",
215  pages.title as "page_title!",
216  pages.url_path as "page_url_path!"
217FROM (
218    SELECT id,
219      page_id,
220      user_id,
221      pending,
222      created_at
223    FROM proposed_page_edits
224    WHERE course_id = $1
225      AND pending = $2
226      AND deleted_at IS NULL
227    ORDER BY created_at DESC,
228      id
229    LIMIT $3 OFFSET $4
230  ) proposed_page_edits
231  LEFT JOIN proposed_block_edits ON proposed_page_edits.id = proposed_block_edits.proposal_id
232  LEFT JOIN pages ON proposed_page_edits.page_id = pages.id
233WHERE proposed_block_edits.deleted_at IS NULL
234"#,
235        course_id,
236        pending,
237        pagination.limit(),
238        pagination.offset(),
239    )
240    .fetch_all(&mut *conn)
241    .await?;
242
243    let mut proposals = HashMap::new();
244    let mut pages = HashMap::new();
245
246    for r in res {
247        let page_proposal_id = r.page_proposal_id;
248        let page_id = r.page_id;
249        let user_id = r.user_id;
250        let page_proposal_pending = r.pending;
251        let created_at = r.created_at;
252        let original_text = r.original_text;
253        let changed_text = r.changed_text;
254        let page_proposal =
255            proposals
256                .entry(r.page_proposal_id)
257                .or_insert_with(move || PageProposal {
258                    id: page_proposal_id,
259                    page_id,
260                    user_id,
261                    pending: page_proposal_pending,
262                    created_at,
263                    block_proposals: Vec::new(),
264                    page_title: r.page_title,
265                    page_url_path: r.page_url_path,
266                });
267
268        let content = match pages.entry(r.page_id) {
269            Entry::Occupied(o) => o.into_mut(),
270            Entry::Vacant(v) => {
271                let page = crate::pages::get_page(&mut *conn, r.page_id).await?;
272                let content: Vec<GutenbergBlock> = serde_json::from_value(page.content)?;
273                v.insert(content)
274            }
275        };
276
277        let block = content.iter().find(|b| b.client_id == r.block_id);
278
279        let block_proposal_id = r.block_proposal_id;
280        let block_id = r.block_id;
281        let block_proposal_status = r.block_proposal_status;
282
283        if let Some(block) = block {
284            let content = block
285                .attributes
286                .get(&r.block_attribute)
287                .ok_or_else(|| {
288                    ModelError::new(
289                        ModelErrorType::Generic,
290                        format!(
291                            "Missing expected attribute '{}' in edited block",
292                            r.block_attribute
293                        ),
294                        None,
295                    )
296                })?
297                .as_str()
298                .ok_or_else(|| {
299                    ModelError::new(
300                        ModelErrorType::Generic,
301                        format!("Attribute '{}' did not contain a string", r.block_attribute),
302                        None,
303                    )
304                })?
305                .to_string();
306            page_proposal
307                .block_proposals
308                .push(BlockProposal::EditedBlockStillExists(
309                    EditedBlockStillExistsData {
310                        accept_preview: merge_edits::merge(&original_text, &changed_text, &content),
311                        id: block_proposal_id,
312                        block_id,
313                        current_text: content.to_string(),
314                        changed_text: changed_text.to_string(),
315                        status: block_proposal_status,
316                        original_text: original_text.to_string(),
317                    },
318                ));
319        } else {
320            page_proposal
321                .block_proposals
322                .push(BlockProposal::EditedBlockNoLongerExists(
323                    EditedBlockNoLongerExistsData {
324                        id: block_proposal_id,
325                        block_id,
326                        changed_text: changed_text.to_string(),
327                        status: block_proposal_status,
328                        original_text: original_text.to_string(),
329                    },
330                ));
331        }
332    }
333
334    let mut proposals = proposals.into_values().collect::<Vec<_>>();
335    proposals.sort_by(|left, right| left.created_at.cmp(&right.created_at).reverse());
336    Ok(proposals)
337}
338
339pub async fn get_proposal_count_for_course(
340    conn: &mut PgConnection,
341    course_id: Uuid,
342) -> ModelResult<ProposalCount> {
343    let res = sqlx::query!(
344        "
345SELECT COUNT(*) filter (
346  where proposed_page_edits.pending = true
347) AS pending,
348COUNT(*) filter (
349  where proposed_page_edits.pending = false
350) AS handled
351FROM proposed_page_edits
352WHERE proposed_page_edits.course_id = $1
353AND proposed_page_edits.deleted_at IS NULL
354",
355        course_id,
356    )
357    .fetch_one(conn)
358    .await?;
359    let count = ProposalCount {
360        pending: res.pending.unwrap_or_default().try_into()?,
361        handled: res.handled.unwrap_or_default().try_into()?,
362    };
363    Ok(count)
364}
365
366pub async fn process_proposal(
367    conn: &mut PgConnection,
368    page_id: Uuid,
369    page_proposal_id: Uuid,
370    block_proposals: Vec<BlockProposalInfo>,
371    author: Uuid,
372    spec_fetcher: impl SpecFetcher,
373    fetch_service_info: impl Fn(Url) -> BoxFuture<'static, ModelResult<ExerciseServiceInfoApi>>,
374) -> ModelResult<()> {
375    if block_proposals.is_empty() {
376        return Err(model_err!(
377            Generic,
378            "No block proposals to process".to_string()
379        ));
380    }
381
382    let mut tx = conn.begin().await?;
383    let page_with_exercises = crate::pages::get_page_with_exercises(&mut tx, page_id).await?;
384    let mut blocks = page_with_exercises.page.blocks_cloned()?;
385    for BlockProposalInfo { id, action } in block_proposals {
386        match action {
387            BlockProposalAction::Accept(contents) => {
388                let res = sqlx::query!(
389                    "
390UPDATE proposed_block_edits
391SET status = 'accepted'
392WHERE id = $1
393RETURNING block_id,
394    block_attribute,
395    original_text,
396    changed_text
397",
398                    id
399                )
400                .fetch_one(&mut *tx)
401                .await?;
402                let block = blocks
403                    .iter_mut()
404                    .find(|b| b.client_id == res.block_id)
405                    .ok_or_else(|| {
406                        ModelError::new(
407                            ModelErrorType::Generic,
408                            "Failed to find the block which the proposal was for".to_string(),
409                            None,
410                        )
411                    })?;
412                let current_content =
413                    block
414                        .attributes
415                        .get_mut(&res.block_attribute)
416                        .ok_or_else(|| {
417                            ModelError::new(
418                                ModelErrorType::Generic,
419                                format!("Edited block has no attribute {}", &res.block_attribute),
420                                None,
421                            )
422                        })?;
423                if let Value::String(s) = current_content {
424                    *s = contents;
425                } else {
426                    return Err(ModelError::new(
427                        ModelErrorType::Generic,
428                        format!(
429                            "Block attribute {} did not contain a string",
430                            res.block_attribute
431                        ),
432                        None,
433                    ));
434                }
435            }
436            BlockProposalAction::Reject => {
437                sqlx::query!(
438                    "
439UPDATE proposed_block_edits
440SET status = 'rejected'
441WHERE id = $1
442",
443                    id
444                )
445                .execute(&mut *tx)
446                .await?;
447            }
448        }
449    }
450
451    let cms_page_update = CmsPageUpdate {
452        content: blocks,
453        exercises: page_with_exercises.exercises,
454        exercise_slides: page_with_exercises.exercise_slides,
455        exercise_tasks: page_with_exercises.exercise_tasks,
456        url_path: page_with_exercises.page.url_path,
457        title: page_with_exercises.page.title,
458        chapter_id: page_with_exercises.page.chapter_id,
459    };
460    crate::pages::update_page(
461        &mut tx,
462        PageUpdateArgs {
463            page_id: page_with_exercises.page.id,
464            author,
465            cms_page_update,
466            retain_ids: true,
467            history_change_reason: HistoryChangeReason::PageSaved,
468            is_exam_page: page_with_exercises.page.exam_id.is_some(),
469        },
470        spec_fetcher,
471        fetch_service_info,
472    )
473    .await?;
474
475    update_page_edit_status(&mut tx, page_proposal_id).await?;
476
477    tx.commit().await?;
478    Ok(())
479}
480
481pub async fn process_by_id_and_page_id(
482    conn: &mut PgConnection,
483    page_id: Uuid,
484    page_proposal_id: Uuid,
485    block_proposals: Vec<BlockProposalInfo>,
486    author: Uuid,
487    spec_fetcher: impl SpecFetcher,
488    fetch_service_info: impl Fn(Url) -> BoxFuture<'static, ModelResult<ExerciseServiceInfoApi>>,
489) -> ModelResult<()> {
490    if block_proposals.is_empty() {
491        return Err(model_err!(
492            Generic,
493            "No block proposals to process".to_string()
494        ));
495    }
496
497    let block_proposal_ids = block_proposals.iter().map(|bp| bp.id).collect::<Vec<_>>();
498    let matching_block_count = sqlx::query!(
499        r#"
500SELECT COUNT(*) AS count
501FROM proposed_page_edits ppe
502  JOIN proposed_block_edits pbe ON pbe.proposal_id = ppe.id
503WHERE ppe.id = $1
504  AND ppe.page_id = $2
505  AND ppe.deleted_at IS NULL
506  AND pbe.id = ANY($3)
507  AND pbe.deleted_at IS NULL
508        "#,
509        page_proposal_id,
510        page_id,
511        &block_proposal_ids
512    )
513    .fetch_one(&mut *conn)
514    .await?
515    .count
516    .unwrap_or(0);
517
518    if matching_block_count != block_proposal_ids.len() as i64 {
519        return Err(model_err!(
520            PreconditionFailed,
521            "Block proposals do not all belong to the requested page proposal".to_string()
522        ));
523    }
524
525    process_proposal(
526        conn,
527        page_id,
528        page_proposal_id,
529        block_proposals,
530        author,
531        spec_fetcher,
532        fetch_service_info,
533    )
534    .await
535}
536
537pub async fn update_page_edit_status(conn: &mut PgConnection, id: Uuid) -> ModelResult<()> {
538    let block_proposals = sqlx::query!(
539        r#"
540SELECT status AS "status: ProposalStatus"
541FROM proposed_block_edits
542WHERE proposal_id = $1
543AND deleted_at IS NULL
544"#,
545        id
546    )
547    .fetch_all(&mut *conn)
548    .await?;
549    let pending = block_proposals
550        .iter()
551        .any(|bp| bp.status == ProposalStatus::Pending);
552    sqlx::query!(
553        "
554UPDATE proposed_page_edits
555SET pending = $1
556WHERE id = $2
557",
558        pending,
559        id,
560    )
561    .execute(&mut *conn)
562    .await?;
563    Ok(())
564}
565
566#[cfg(test)]
567mod test {
568    use headless_lms_utils::document_schema_processor::{GutenbergBlock, attributes};
569
570    use super::*;
571    use crate::{pages::PageUpdateArgs, proposed_block_edits::*, test_helper::*};
572
573    async fn init_content(
574        conn: &mut PgConnection,
575        chapter: Uuid,
576        page: Uuid,
577        user: Uuid,
578        content: &str,
579    ) -> Uuid {
580        let client_id = Uuid::new_v4();
581        let new_content: Vec<GutenbergBlock> = vec![GutenbergBlock {
582            client_id,
583            name: "core/paragraph".to_string(),
584            is_valid: true,
585            attributes: attributes! {
586                "content": content
587            },
588            inner_blocks: vec![],
589        }];
590        let cms_page_update = CmsPageUpdate {
591            content: new_content,
592            url_path: "".to_string(),
593            title: "".to_string(),
594            chapter_id: Some(chapter),
595            exercises: vec![],
596            exercise_slides: vec![],
597            exercise_tasks: vec![],
598        };
599        crate::pages::update_page(
600            conn,
601            PageUpdateArgs {
602                page_id: page,
603                author: user,
604                cms_page_update,
605                retain_ids: true,
606                history_change_reason: HistoryChangeReason::PageSaved,
607                is_exam_page: false,
608            },
609            |_, _, _| unimplemented!(),
610            |_| unimplemented!(),
611        )
612        .await
613        .unwrap();
614        client_id
615    }
616
617    async fn assert_content(conn: &mut PgConnection, page_id: Uuid, expected: &str) {
618        let page = crate::pages::get_page(conn, page_id).await.unwrap();
619        let mut new_content: Vec<GutenbergBlock> = serde_json::from_value(page.content).unwrap();
620        let block = new_content.pop().unwrap();
621        let content = block.attributes.get("content").unwrap().as_str().unwrap();
622        assert_eq!(content, expected);
623    }
624
625    #[tokio::test]
626    async fn typo_fix() {
627        insert_data!(:tx, :user, :org, :course, instance: _instance, :course_module, :chapter, :page);
628        let block_id = init_content(
629            tx.as_mut(),
630            chapter,
631            page,
632            user,
633            "Content with a tpo in it.",
634        )
635        .await;
636
637        let new = NewProposedPageEdits {
638            page_id: page,
639            block_edits: vec![NewProposedBlockEdit {
640                block_id,
641                block_attribute: "content".to_string(),
642                original_text: "Content with a tpo in it.".to_string(),
643                changed_text: "Content with a typo in it.".to_string(),
644            }],
645        };
646        insert(tx.as_mut(), PKeyPolicy::Generate, course, None, &new)
647            .await
648            .unwrap();
649        let mut ps = get_proposals_for_course(tx.as_mut(), course, true, Pagination::default())
650            .await
651            .unwrap();
652        let mut p = ps.pop().unwrap();
653        let b = p.block_proposals.pop().unwrap();
654        match b {
655            BlockProposal::EditedBlockStillExists(b) => {
656                assert_eq!(b.accept_preview.unwrap(), "Content with a typo in it.");
657                process_proposal(
658                    tx.as_mut(),
659                    page,
660                    p.id,
661                    vec![BlockProposalInfo {
662                        id: b.id,
663                        action: BlockProposalAction::Accept(
664                            "Content with a typo in it.".to_string(),
665                        ),
666                    }],
667                    user,
668                    |_, _, _| unimplemented!(),
669                    |_| unimplemented!(),
670                )
671                .await
672                .unwrap();
673
674                let mut ps =
675                    get_proposals_for_course(tx.as_mut(), course, false, Pagination::default())
676                        .await
677                        .unwrap();
678                let _ = ps.pop().unwrap();
679
680                assert_content(tx.as_mut(), page, "Content with a typo in it.").await;
681            }
682            BlockProposal::EditedBlockNoLongerExists(_o) => panic!("Wrong block proposal"),
683        };
684    }
685
686    #[tokio::test]
687    async fn rejection() {
688        insert_data!(:tx, :user, :org, :course, instance: _instance, :course_module, :chapter, :page);
689        let block_id = init_content(
690            tx.as_mut(),
691            chapter,
692            page,
693            user,
694            "Content with a tpo in it.",
695        )
696        .await;
697        let new = NewProposedPageEdits {
698            page_id: page,
699            block_edits: vec![NewProposedBlockEdit {
700                block_id,
701                block_attribute: "content".to_string(),
702                original_text: "Content with a tpo in it.".to_string(),
703                changed_text: "Content with a typo in it.".to_string(),
704            }],
705        };
706        insert(tx.as_mut(), PKeyPolicy::Generate, course, None, &new)
707            .await
708            .unwrap();
709
710        let mut ps = get_proposals_for_course(tx.as_mut(), course, true, Pagination::default())
711            .await
712            .unwrap();
713        let mut p = ps.pop().unwrap();
714        let b = p.block_proposals.pop().unwrap();
715        match b {
716            BlockProposal::EditedBlockStillExists(b) => {
717                assert_eq!(b.accept_preview.unwrap(), "Content with a typo in it.");
718                assert_eq!(b.status, ProposalStatus::Pending);
719
720                process_proposal(
721                    tx.as_mut(),
722                    page,
723                    p.id,
724                    vec![BlockProposalInfo {
725                        id: b.id,
726                        action: BlockProposalAction::Reject,
727                    }],
728                    user,
729                    |_, _, _| unimplemented!(),
730                    |_| unimplemented!(),
731                )
732                .await
733                .unwrap();
734
735                let mut ps =
736                    get_proposals_for_course(tx.as_mut(), course, false, Pagination::default())
737                        .await
738                        .unwrap();
739                let _ = ps.pop().unwrap();
740
741                assert_content(tx.as_mut(), page, "Content with a tpo in it.").await;
742            }
743            BlockProposal::EditedBlockNoLongerExists(_o) => panic!("Wrong block proposal"),
744        };
745    }
746}