Skip to content

Commit

Permalink
Merge pull request #1201 from AletheiaFact/add-informative-news-claim…
Browse files Browse the repository at this point in the history
…-review-type

Add new Informative News report type
  • Loading branch information
thesocialdev authored May 27, 2024
2 parents 2346f80 + cf7a4e9 commit 2e70641
Show file tree
Hide file tree
Showing 34 changed files with 693 additions and 362 deletions.
2 changes: 1 addition & 1 deletion cypress/support/locators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const locators = {
},

claimReview: {
BTN_START_CLAIM_REVIEW: "[data-cy=testAddReviewButton]",
BTN_START_CLAIM_REVIEW: "[data-cy=testAddFactCheckReviewButton]",
INPUT_USER: "[data-cy=testClaimReviewusersId]",
BTN_ASSIGN_USER: "[data-cy=testClaimReviewASSIGN_USER]",
INPUT_SUMMARY: "[data-cy=testClaimReviewsummary]",
Expand Down
87 changes: 55 additions & 32 deletions lib/editor-parser.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,48 @@
import { ObjectMark, RemirrorJSON } from "remirror";
import { ReviewTaskMachineContextReviewData } from "../server/claim-review-task/dto/create-claim-review-task.dto";
import { ReportModelEnum } from "../server/types/enums";

type SchemaType = {
summary: string;
verification?: string;
report?: string;
sources: any[];
questions?: any[];
};

const EditorSchemaArray = ["summary", "report", "verification", "questions"];
const getEditorSchemaArray = (reportModel = ReportModelEnum.FactChecking) =>
reportModel === ReportModelEnum.FactChecking
? ["summary", "report", "verification", "questions"]
: ["summary"];

const MarkupCleanerRegex = /{{[^|]+\|([^}]+)}}/;

const defaultDoc: RemirrorJSON = {
type: "doc",
content: [
{
type: "summary",
content: [{ type: "paragraph" }],
},
{
type: "questions",
content: [{ type: "paragraph" }],
},
{
type: "report",
content: [{ type: "paragraph" }],
},
{
type: "verification",
content: [{ type: "paragraph" }],
},
],
const createParagraphBlock = (
type: string
): { type: string; content: [{ type: "paragraph" }] } => ({
type: type,
content: [{ type: "paragraph" }],
});

const getDefaultDoc = (reportModel: string): RemirrorJSON => {
const baseContent = [
createParagraphBlock("summary"),
createParagraphBlock("questions"),
createParagraphBlock("report"),
createParagraphBlock("verification"),
];

if (reportModel === ReportModelEnum.InformativeNews) {
return {
type: "doc",
content: [createParagraphBlock("summary")],
};
}

return {
type: "doc",
content: baseContent,
};
};

export class EditorParser {
Expand Down Expand Up @@ -109,7 +127,8 @@ export class EditorParser {
}

schema2html(
schema: ReviewTaskMachineContextReviewData
schema: ReviewTaskMachineContextReviewData,
reportModel = ReportModelEnum.FactChecking
): ReviewTaskMachineContextReviewData {
const newSchema = {
...schema,
Expand All @@ -118,7 +137,7 @@ export class EditorParser {
const hasSources = this.hasSources(newSchema?.sources);

for (const key in newSchema) {
if (EditorSchemaArray.includes(key)) {
if (getEditorSchemaArray(reportModel).includes(key)) {
const content = newSchema[key];
if (!hasSources) {
newSchema[key] =
Expand Down Expand Up @@ -146,16 +165,13 @@ export class EditorParser {
}

editor2schema(data: RemirrorJSON): ReviewTaskMachineContextReviewData {
const schema = {
const schema: SchemaType = {
summary: "",
verification: "",
report: "",
sources: [],
questions: [],
};
const questions = [];
for (const cardContent of data?.content) {
if (EditorSchemaArray?.includes(cardContent?.type)) {
if (getEditorSchemaArray().includes(cardContent?.type)) {
if (cardContent?.type === "questions") {
for (const { content } of cardContent.content) {
if (content) {
Expand All @@ -173,7 +189,13 @@ export class EditorParser {
}
}

schema.questions = questions;
/**
* Needed to do this conditional because the form validation when the reportModel
* is equal to Informative news requires the questions field.
*/
if (schema.report || schema.verification) {
schema.questions = questions;
}
schema.sources = this.replaceSourceContentToTextRange(schema);

return schema;
Expand Down Expand Up @@ -260,18 +282,19 @@ export class EditorParser {
}

async schema2editor(
schema: ReviewTaskMachineContextReviewData
schema: ReviewTaskMachineContextReviewData,
reportModel = ReportModelEnum.FactChecking
): Promise<RemirrorJSON> {
if (!schema) {
return defaultDoc;
return getDefaultDoc(reportModel);
}

const doc: RemirrorJSON = {
type: "doc",
content: [],
};
for (const key of Object.keys(schema).filter((key) =>
EditorSchemaArray.includes(key)
getEditorSchemaArray(reportModel).includes(key)
)) {
const content = schema[key];
if (!this.hasSources(schema?.sources)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Db } from "mongodb";

export async function up(db: Db) {
await db
.collection("claimreviews")
.updateMany({}, { $set: { reportModel: "Fact-checking" } });
}

export async function down(db: Db) {
await db
.collection("claimreviews")
.updateMany({}, { $unset: { reportModel: "" } });
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Db } from "mongodb";

export async function up(db: Db) {
await db
.collection("reports")
.updateMany({}, { $set: { reportModel: "Fact-checking" } });
}

export async function down(db: Db) {
await db
.collection("reports")
.updateMany({}, { $unset: { reportModel: "" } });
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Db } from "mongodb";

export async function up(db: Db) {
await db
.collection("claimreviewtasks")
.updateMany({}, { $set: { reportModel: "Fact-checking" } });
}

export async function down(db: Db) {
await db
.collection("claimreviewtasks")
.updateMany({}, { $unset: { reportModel: "" } });
}
4 changes: 3 additions & 1 deletion public/locales/en/claimReviewForm.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"titleEmpty": "Choose a sentence to review",
"title": "Review the sentence",
"classificationLabel": "Review this claim",
"addReviewButton": "Start Review",
"addReviewButton": "Start a fact-check",
"addInformativeNewsButton": "Start an informative news",
"cancelButton": "Cancel",
"classificationPlaceholder": "Choose a classification",
"not-fact": "Not fact",
Expand All @@ -22,6 +23,7 @@
"assignUserPlaceholder": "Select a user",
"summaryLabel": "Conclusion summary",
"summaryPlaceholder": "Insert summary here",
"informativeNewsLabel": "Material checado",
"questionsLabel": "Which questions must be answered",
"questionsPlaceholder": "Insert question here",
"addQuestionLabel": "Add another question",
Expand Down
4 changes: 3 additions & 1 deletion public/locales/pt/claimReviewForm.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"titleEmpty": "Escolha uma frase para revisar",
"title": "Classifique a frase",
"classificationLabel": "Revise essa frase",
"addReviewButton": "Comece uma Revisão",
"addReviewButton": "Comece uma checagem de fatos",
"addInformativeNewsButton": "Comece uma noticia informativa",
"cancelButton": "Cancelar",
"classificationPlaceholder": "Selecione uma classificação",
"not-fact": "Não é fato",
Expand All @@ -22,6 +23,7 @@
"assignUserPlaceholder": "Selecione um usuário",
"summaryLabel": "Resumo da conclusão",
"summaryPlaceholder": "Insira o resumo da conclusão",
"informativeNewsLabel": "Material checado",
"questionsLabel": "Quais perguntas a verificação deverá responder?",
"questionsPlaceholder": "Insira uma pergunta",
"addQuestionLabel": "Incluir outra pergunta",
Expand Down
9 changes: 7 additions & 2 deletions server/claim-review-task/claim-review-task.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export class ClaimReviewController {
data_hash,
claimReviewTaskBody,
claimReviewTaskBody.nameSpace,
claimReviewTaskBody.reportModel,
history
);
}
Expand All @@ -129,14 +130,18 @@ export class ClaimReviewController {
@ApiTags("claim-review-task")
@Get("api/claimreviewtask/editor-content/:data_hash")
@Header("Cache-Control", "no-cache")
async getEditorContentByDataHash(@Param("data_hash") data_hash: string) {
async getEditorContentByDataHash(
@Param("data_hash") data_hash: string,
@Query() query: { reportModel: string }
) {
const claimReviewTask =
await this.claimReviewTaskService.getClaimReviewTaskByDataHash(
data_hash
);

return this.claimReviewTaskService.getEditorContentObject(
claimReviewTask?.machine?.context?.reviewData
claimReviewTask?.machine?.context?.reviewData,
query.reportModel
);
}

Expand Down
17 changes: 11 additions & 6 deletions server/claim-review-task/claim-review-task.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,12 @@ export class ClaimReviewTaskService {
this.stateEventService.createStateEvent(stateEvent);
}

async _createReportAndClaimReview(data_hash, machine) {
async _createReportAndClaimReview(data_hash, machine, reportModel) {
const claimReviewData = machine.context.claimReview;

const newReport = Object.assign(machine.context.reviewData, {
data_hash,
reportModel,
});

const report = await this.reportService.create(newReport);
Expand All @@ -247,7 +248,8 @@ export class ClaimReviewTaskService {
...claimReviewData,
report,
},
data_hash
data_hash,
reportModel
);
}

Expand Down Expand Up @@ -322,7 +324,8 @@ export class ClaimReviewTaskService {
return this.update(
claimReviewTaskBody.data_hash,
claimReviewTaskBody,
claimReviewTaskBody.nameSpace
claimReviewTaskBody.nameSpace,
claimReviewTask.reportModel
);
} else {
const newClaimReviewTask = new this.ClaimReviewTaskModel(
Expand All @@ -339,6 +342,7 @@ export class ClaimReviewTaskService {
data_hash: string,
{ machine }: UpdateClaimReviewTaskDTO,
nameSpace: string,
reportModel: string,
history: boolean = true
) {
const loggedInUser = this.req.user;
Expand Down Expand Up @@ -370,7 +374,8 @@ export class ClaimReviewTaskService {
}
this._createReportAndClaimReview(
data_hash,
newClaimReviewTask.machine
newClaimReviewTask.machine,
reportModel
);
}

Expand Down Expand Up @@ -547,8 +552,8 @@ export class ClaimReviewTaskService {
}
}

getEditorContentObject(schema) {
return this.editorParseService.schema2editor(schema);
getEditorContentObject(schema, reportModel) {
return this.editorParseService.schema2editor(schema, reportModel);
}

async addComment(data_hash, comment) {
Expand Down
9 changes: 8 additions & 1 deletion server/claim-review-task/dto/create-claim-review-task.dto.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { IsNotEmpty, IsObject, IsString } from "class-validator";
import { IsEnum, IsNotEmpty, IsObject, IsString } from "class-validator";

import { ClassificationEnum } from "../../claim-review/dto/create-claim-review.dto";
import { Claim } from "../../claim/schemas/claim.schema";
import { Personality } from "../../personality/schemas/personality.schema";
import { User } from "../../users/schemas/user.schema";
import { ApiProperty } from "@nestjs/swagger";
import { ReportModelEnum } from "../../types/enums";

export type ReviewTaskMachineContextReviewData = {
usersId?: any[];
Expand Down Expand Up @@ -57,6 +58,12 @@ export class CreateClaimReviewTaskDTO {
@ApiProperty()
data_hash: string;

@IsNotEmpty()
@IsEnum(ReportModelEnum)
@IsString()
@ApiProperty()
reportModel: string;

@IsNotEmpty()
@IsString()
@ApiProperty()
Expand Down
12 changes: 12 additions & 0 deletions server/claim-review-task/schemas/claim-review-task.schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as mongoose from "mongoose";
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
import type { Machine } from "../dto/create-claim-review-task.dto";
import { ReportModelEnum } from "../../types/enums";

export type ClaimReviewTaskDocument = ClaimReviewTask & mongoose.Document;

Expand All @@ -11,6 +12,17 @@ export class ClaimReviewTask {

@Prop({ unique: true, required: true })
data_hash: string;

@Prop({
required: true,
validate: {
validator: (v) => {
return Object.values(ReportModelEnum).includes(v);
},
},
message: (tag) => `${tag} is not a valid report type.`,
})
reportModel: ReportModelEnum;
}

export const ClaimReviewTaskSchema =
Expand Down
Loading

0 comments on commit 2e70641

Please sign in to comment.