From a700a3512a5736c59e5bbf02ca189bcb062ae8e0 Mon Sep 17 00:00:00 2001 From: acanas Date: Mon, 1 Mar 2021 22:31:56 +0100 Subject: [PATCH] Version 20.37: Mar 01, 2021 Code refactoring and more error messages in timeline. --- swad_changelog.h | 3 +- swad_timeline_comment.c | 241 +++++++++++++++++++++----------------- swad_timeline_database.c | 14 +-- swad_timeline_favourite.c | 229 +++++++++++++++++++++++++----------- swad_timeline_note.c | 142 ++++++++++++---------- swad_timeline_share.c | 111 ++++++++++++------ 6 files changed, 458 insertions(+), 282 deletions(-) diff --git a/swad_changelog.h b/swad_changelog.h index 2ae72a23..312bde97 100644 --- a/swad_changelog.h +++ b/swad_changelog.h @@ -598,10 +598,11 @@ TODO: DNI de un estudiante sale err TODO: BUG: Cuando un tipo de grupo sólo tiene un grupo, inscribirse es voluntario, el estudiante sólo puede pertenecer a un grupo, y se inscribe en él, debería poder desapuntarse. Ahora no puede. TODO: Salvador Romero Cortés: @acanas opción para editar posts */ -#define Log_PLATFORM_VERSION "SWAD 20.36.49 (2021-03-01)" +#define Log_PLATFORM_VERSION "SWAD 20.37 (2021-03-01)" #define CSS_FILE "swad20.33.9.css" #define JS_FILE "swad20.6.2.js" /* + Version 20.37: Mar 01, 2021 Code refactoring and more error messages in timeline. (305681 lines) Version 20.36.49: Mar 01, 2021 Cleaning unused header files in timeline modules. (305544 lines) Version 20.36.48: Mar 01, 2021 Query moved to module swad_timeline_database. (305553 lines) Version 20.36.47: Mar 01, 2021 Query moved to module swad_timeline_database. (305543 lines) diff --git a/swad_timeline_comment.c b/swad_timeline_comment.c index ce457090..e5c6a074 100644 --- a/swad_timeline_comment.c +++ b/swad_timeline_comment.c @@ -476,13 +476,18 @@ static void TL_Com_CheckAndWriteComment (const struct TL_Timeline *Timeline, | | Favs |Remove| > buttons | |________|___________________________|______| / / */ - if (Com->PubCod > 0 && - Com->NotCod > 0 && - Com->UsrCod > 0) - /***** Write comment *****/ - TL_Com_WriteComment (Timeline,Com); - else + + /***** Trivial check: codes *****/ + if (Com->PubCod <= 0 || + Com->NotCod <= 0 || + Com->UsrCod <= 0) + { Ale_ShowAlert (Ale_ERROR,"Error in comment."); + return; + } + + /***** Write comment *****/ + TL_Com_WriteComment (Timeline,Com); } /*****************************************************************************/ @@ -696,50 +701,52 @@ static long TL_Com_ReceiveComment (void) Not.NotCod = TL_Not_GetParamNotCod (); TL_Not_GetDataOfNoteByCod (&Not); - if (Not.NotCod > 0) + /***** Trivial check: note code *****/ + if (Not.NotCod <= 0) { - /***** Get the content of the comment *****/ - Par_GetParAndChangeFormat ("Txt",Content.Txt,Cns_MAX_BYTES_LONG_TEXT, - Str_TO_RIGOROUS_HTML,true); - - /***** Initialize image *****/ - Med_MediaConstructor (&Content.Media); - - /***** Get attached image (action, file and title) *****/ - Content.Media.Width = TL_IMAGE_SAVED_MAX_WIDTH; - Content.Media.Height = TL_IMAGE_SAVED_MAX_HEIGHT; - Content.Media.Quality = TL_IMAGE_SAVED_QUALITY; - Med_GetMediaFromForm (-1L,-1L,-1,&Content.Media,NULL,NULL); - Ale_ShowAlerts (NULL); - - if (Content.Txt[0] || // Text not empty - Content.Media.Status == Med_PROCESSED) // A media is attached - { - /***** Store media in filesystem and database *****/ - Med_RemoveKeepOrStoreMedia (-1L,&Content.Media); - - /***** Publish *****/ - /* Insert into publications */ - Pub.NotCod = Not.NotCod; - Pub.PublisherCod = Gbl.Usrs.Me.UsrDat.UsrCod; - Pub.PubType = TL_Pub_COMMENT_TO_NOTE; - TL_Pub_PublishPubInTimeline (&Pub); // Set Pub.PubCod - - /* Insert comment content in the database */ - TL_DB_InsertCommentContent (Pub.PubCod,&Content); - - /***** Store notifications about the new comment *****/ - Ntf_StoreNotifyEventsToAllUsrs (Ntf_EVENT_TIMELINE_COMMENT,Pub.PubCod); - - /***** Analyze content and store notifications about mentions *****/ - Str_AnalyzeTxtAndStoreNotifyEventToMentionedUsrs (Pub.PubCod,Content.Txt); - } - - /***** Free image *****/ - Med_MediaDestructor (&Content.Media); - } - else Ale_ShowAlert (Ale_WARNING,Txt_The_original_post_no_longer_exists); + return -1L; + } + + /***** Get the content of the comment *****/ + Par_GetParAndChangeFormat ("Txt",Content.Txt,Cns_MAX_BYTES_LONG_TEXT, + Str_TO_RIGOROUS_HTML,true); + + /***** Initialize image *****/ + Med_MediaConstructor (&Content.Media); + + /***** Get attached image (action, file and title) *****/ + Content.Media.Width = TL_IMAGE_SAVED_MAX_WIDTH; + Content.Media.Height = TL_IMAGE_SAVED_MAX_HEIGHT; + Content.Media.Quality = TL_IMAGE_SAVED_QUALITY; + Med_GetMediaFromForm (-1L,-1L,-1,&Content.Media,NULL,NULL); + Ale_ShowAlerts (NULL); + + if (Content.Txt[0] || // Text not empty + Content.Media.Status == Med_PROCESSED) // A media is attached + { + /***** Store media in filesystem and database *****/ + Med_RemoveKeepOrStoreMedia (-1L,&Content.Media); + + /***** Publish *****/ + /* Insert into publications */ + Pub.NotCod = Not.NotCod; + Pub.PublisherCod = Gbl.Usrs.Me.UsrDat.UsrCod; + Pub.PubType = TL_Pub_COMMENT_TO_NOTE; + TL_Pub_PublishPubInTimeline (&Pub); // Set Pub.PubCod + + /* Insert comment content in the database */ + TL_DB_InsertCommentContent (Pub.PubCod,&Content); + + /***** Store notifications about the new comment *****/ + Ntf_StoreNotifyEventsToAllUsrs (Ntf_EVENT_TIMELINE_COMMENT,Pub.PubCod); + + /***** Analyze content and store notifications about mentions *****/ + Str_AnalyzeTxtAndStoreNotifyEventToMentionedUsrs (Pub.PubCod,Content.Txt); + } + + /***** Free image *****/ + Med_MediaDestructor (&Content.Media); return Not.NotCod; } @@ -794,45 +801,53 @@ static void TL_Com_RequestRemovalComment (struct TL_Timeline *Timeline) extern const char *Txt_Do_you_really_want_to_remove_the_following_comment; struct TL_Com_Comment Com; - /***** Initialize image *****/ + /***** Initialize media *****/ Med_MediaConstructor (&Com.Content.Media); /***** Get data of comment *****/ Com.PubCod = TL_Pub_GetParamPubCod (); TL_Com_GetDataOfCommByCod (&Com); - if (Com.PubCod > 0) + /***** Trivial check 1: publication code *****/ + if (Com.PubCod <= 0) { - if (Usr_ItsMe (Com.UsrCod)) // I am the author of this comment - { - /***** Show question and button to remove comment *****/ - /* Begin alert */ - TL_Frm_BeginAlertRemove (Txt_Do_you_really_want_to_remove_the_following_comment); - - /* Show comment */ - Box_BoxBegin (NULL,NULL, - NULL,NULL, - NULL,Box_NOT_CLOSABLE); - - HTM_DIV_Begin ("class=\"TL_LEFT_PHOTO\""); - HTM_DIV_End (); - - HTM_DIV_Begin ("class=\"TL_RIGHT_CONT TL_RIGHT_WIDTH\""); - TL_Com_CheckAndWriteComment (Timeline,&Com); - HTM_DIV_End (); - - Box_BoxEnd (); - - /* End alert */ - Timeline->PubCod = Com.PubCod; // Publication to be removed - TL_Frm_EndAlertRemove (Timeline,TL_Frm_REM_COMM, - TL_Com_PutParamsRemoveComment); - } - } - else + Med_MediaDestructor (&Com.Content.Media); Ale_ShowAlert (Ale_WARNING,Txt_The_comment_no_longer_exists); + return; + } - /***** Free image *****/ + /***** Trivial check 2: only if I am the author of this comment *****/ + if (!Usr_ItsMe (Com.UsrCod)) + { + Med_MediaDestructor (&Com.Content.Media); + Ale_ShowAlert (Ale_ERROR,"You are not the author."); + return; + } + + /***** Show question and button to remove comment *****/ + /* Begin alert */ + TL_Frm_BeginAlertRemove (Txt_Do_you_really_want_to_remove_the_following_comment); + + /* Show comment */ + Box_BoxBegin (NULL,NULL, + NULL,NULL, + NULL,Box_NOT_CLOSABLE); + + HTM_DIV_Begin ("class=\"TL_LEFT_PHOTO\""); + HTM_DIV_End (); + + HTM_DIV_Begin ("class=\"TL_RIGHT_CONT TL_RIGHT_WIDTH\""); + TL_Com_CheckAndWriteComment (Timeline,&Com); + HTM_DIV_End (); + + Box_BoxEnd (); + + /* End alert */ + Timeline->PubCod = Com.PubCod; // Publication to be removed + TL_Frm_EndAlertRemove (Timeline,TL_Frm_REM_COMM, + TL_Com_PutParamsRemoveComment); + + /***** Free media *****/ Med_MediaDestructor (&Com.Content.Media); } @@ -902,33 +917,41 @@ static void TL_Com_RemoveComment (void) extern const char *Txt_Comment_removed; struct TL_Com_Comment Com; - /***** Initialize image *****/ + /***** Initialize media *****/ Med_MediaConstructor (&Com.Content.Media); /***** Get data of comment *****/ Com.PubCod = TL_Pub_GetParamPubCod (); TL_Com_GetDataOfCommByCod (&Com); - if (Com.PubCod > 0) + /***** Trivial check 1: publication code *****/ + if (Com.PubCod <= 0) { - if (Usr_ItsMe (Com.UsrCod)) // I am the author of this comment - { - /***** Remove media associated to comment - and delete comment from database *****/ - TL_Com_RemoveCommentMediaAndDBEntries (Com.PubCod); - - /***** Reset fields of comment *****/ - TL_Com_ResetComment (&Com); - - /***** Message of success *****/ - Ale_ShowAlert (Ale_SUCCESS,Txt_Comment_removed); - } - } - else + Med_MediaDestructor (&Com.Content.Media); Ale_ShowAlert (Ale_WARNING,Txt_The_comment_no_longer_exists); + return; + } - /***** Free image *****/ + /***** Trivial check 2: only if I am the author of this comment *****/ + if (!Usr_ItsMe (Com.UsrCod)) + { + Med_MediaDestructor (&Com.Content.Media); + Ale_ShowAlert (Ale_ERROR,"You are not the author."); + return; + } + + /***** Remove media associated to comment + and delete comment from database *****/ + TL_Com_RemoveCommentMediaAndDBEntries (Com.PubCod); + + /***** Free media *****/ Med_MediaDestructor (&Com.Content.Media); + + /***** Reset fields of comment *****/ + TL_Com_ResetComment (&Com); + + /***** Message of success *****/ + Ale_ShowAlert (Ale_SUCCESS,Txt_Comment_removed); } /*****************************************************************************/ @@ -964,25 +987,27 @@ void TL_Com_GetDataOfCommByCod (struct TL_Com_Comment *Com) MYSQL_RES *mysql_res; MYSQL_ROW row; - if (Com->PubCod > 0) + /***** Trivial check: publication code should be > 0 *****/ + if (Com->PubCod <= 0) { - /***** Get data of comment from database *****/ - if (TL_DB_GetDataOfCommByCod (Com->PubCod,&mysql_res)) - { - /***** Get data of comment *****/ - row = mysql_fetch_row (mysql_res); - TL_Com_GetDataOfCommentFromRow (row,Com); - } - else - /***** Reset fields of comment *****/ - TL_Com_ResetComment (Com); + /***** Reset fields of comment *****/ + TL_Com_ResetComment (Com); + return; + } - /***** Free structure that stores the query result *****/ - DB_FreeMySQLResult (&mysql_res); + /***** Get data of comment from database *****/ + if (TL_DB_GetDataOfCommByCod (Com->PubCod,&mysql_res)) + { + /***** Get data of comment *****/ + row = mysql_fetch_row (mysql_res); + TL_Com_GetDataOfCommentFromRow (row,Com); } else /***** Reset fields of comment *****/ TL_Com_ResetComment (Com); + + /***** Free structure that stores the query result *****/ + DB_FreeMySQLResult (&mysql_res); } /*****************************************************************************/ diff --git a/swad_timeline_database.c b/swad_timeline_database.c index 2c7c43c1..9cf9ab87 100644 --- a/swad_timeline_database.c +++ b/swad_timeline_database.c @@ -70,7 +70,7 @@ extern struct Globals Gbl; /***************************** Private prototypes ****************************/ /*****************************************************************************/ -static long TL_DB_GetMedCodFromPub (long PubCod,const char *DBTable); +static long TL_DB_GetMedCod (const char *Table,const char *Field,long Cod); /*****************************************************************************/ /********************* Get which users to show in timeline *******************/ @@ -409,9 +409,9 @@ unsigned TL_DB_GetPostByCod (long PstCod,MYSQL_RES **mysql_res) /***************** Get code of media associated to post **********************/ /*****************************************************************************/ -long TL_DB_GetMedCodFromPost (long PubCod) +long TL_DB_GetMedCodFromPost (long PstCod) { - return TL_DB_GetMedCodFromPub (PubCod,"tl_posts"); + return TL_DB_GetMedCod ("tl_posts","PstCod",PstCod); } /*****************************************************************************/ @@ -597,7 +597,7 @@ void TL_DB_InsertCommentContent (long PubCod, long TL_DB_GetMedCodFromComment (long PubCod) { - return TL_DB_GetMedCodFromPub (PubCod,"tl_comments"); + return TL_DB_GetMedCod ("tl_comments","PubCod",PubCod); } /*****************************************************************************/ @@ -680,7 +680,7 @@ void TL_DB_RemoveAllCommentsMadeBy (long UsrCod) /*************** Get code of media associated to post/comment ****************/ /*****************************************************************************/ -static long TL_DB_GetMedCodFromPub (long PubCod,const char *DBTable) +static long TL_DB_GetMedCod (const char *Table,const char *Field,long Cod) { MYSQL_RES *mysql_res; MYSQL_ROW row; @@ -690,8 +690,8 @@ static long TL_DB_GetMedCodFromPub (long PubCod,const char *DBTable) if (DB_QuerySELECT (&mysql_res,"can not get media code", "SELECT MedCod" // row[0] " FROM %s" - " WHERE PubCod=%ld", - DBTable,PubCod) == 1) // Result should have a unique row + " WHERE %s=%ld", + Table,Field,Cod) == 1) // Result should have a unique row { /* Get media code */ row = mysql_fetch_row (mysql_res); diff --git a/swad_timeline_favourite.c b/swad_timeline_favourite.c index c3b72948..34276b35 100644 --- a/swad_timeline_favourite.c +++ b/swad_timeline_favourite.c @@ -160,63 +160,102 @@ void TL_Fav_PutIconToFavUnfNote (const struct TL_Not_Note *Not, static void TL_Fav_FavNote (struct TL_Not_Note *Not) { + extern const char *Txt_The_original_post_no_longer_exists; long OriginalPubCod; /***** Get data of note *****/ Not->NotCod = TL_Not_GetParamNotCod (); TL_Not_GetDataOfNoteByCod (Not); - if (Not->NotCod > 0) + /***** Trivial check 1: note code should be > 0 *****/ + if (Not->NotCod <= 0) { - if (Gbl.Usrs.Me.Logged && // I am logged... - !Usr_ItsMe (Not->UsrCod)) // ...but I am not the author - if (!TL_DB_CheckIfFavedByUsr (TL_Fav_NOTE,Not->NotCod, - Gbl.Usrs.Me.UsrDat.UsrCod)) // I have not yet favourited the note - { - /***** Mark note as favourite in database *****/ - TL_DB_MarkAsFav (TL_Fav_NOTE,Not->NotCod); - - /***** Update number of times this note is favourited *****/ - Not->NumFavs = TL_DB_GetNumTimesHasBeenFav (TL_Fav_NOTE, - Not->NotCod,Not->UsrCod); - - /***** Create notification about favourite post - for the author of the post *****/ - OriginalPubCod = TL_DB_GetPubCodOfOriginalNote (Not->NotCod); - if (OriginalPubCod > 0) - TL_Ntf_CreateNotifToAuthor (Not->UsrCod,OriginalPubCod, - Ntf_EVENT_TIMELINE_FAV); - } + Ale_ShowAlert (Ale_WARNING,Txt_The_original_post_no_longer_exists); + return; } + + /***** Trivial check 2: I must be logged *****/ + if (!Gbl.Usrs.Me.Logged) + { + Ale_ShowAlert (Ale_ERROR,"You are not logged."); + return; + } + + /***** Trivial check 3: The author can not fav his/her own notes *****/ + if (Usr_ItsMe (Not->UsrCod)) + { + Ale_ShowAlert (Ale_ERROR,"You can not fav/unfav your own posts."); + return; + } + + /***** Trivial check 4: Have I faved this note? *****/ + if (TL_DB_CheckIfFavedByUsr (TL_Fav_NOTE,Not->NotCod, + Gbl.Usrs.Me.UsrDat.UsrCod)) + // Don't show error message + return; + + /***** Mark note as favourite in database *****/ + TL_DB_MarkAsFav (TL_Fav_NOTE,Not->NotCod); + + /***** Update number of times this note is favourited *****/ + Not->NumFavs = TL_DB_GetNumTimesHasBeenFav (TL_Fav_NOTE, + Not->NotCod,Not->UsrCod); + + /***** Create notification about favourite post + for the author of the post *****/ + OriginalPubCod = TL_DB_GetPubCodOfOriginalNote (Not->NotCod); + if (OriginalPubCod > 0) + TL_Ntf_CreateNotifToAuthor (Not->UsrCod,OriginalPubCod, + Ntf_EVENT_TIMELINE_FAV); } static void TL_Fav_UnfNote (struct TL_Not_Note *Not) { + extern const char *Txt_The_original_post_no_longer_exists; long OriginalPubCod; /***** Get data of note *****/ Not->NotCod = TL_Not_GetParamNotCod (); TL_Not_GetDataOfNoteByCod (Not); - if (Not->NotCod > 0) - if (Not->NumFavs && - Gbl.Usrs.Me.Logged && // I am logged... - !Usr_ItsMe (Not->UsrCod)) // ...but I am not the author - if (TL_DB_CheckIfFavedByUsr (TL_Fav_NOTE,Not->NotCod, - Gbl.Usrs.Me.UsrDat.UsrCod)) // I have favourited the note - { - /***** Delete the mark as favourite from database *****/ - TL_DB_UnmarkAsFav (TL_Fav_NOTE,Not->NotCod); + /***** Trivial check 1: note code should be > 0 *****/ + if (Not->NotCod <= 0) + { + Ale_ShowAlert (Ale_WARNING,Txt_The_original_post_no_longer_exists); + return; + } - /***** Update number of times this note is favourited *****/ - Not->NumFavs = TL_DB_GetNumTimesHasBeenFav (TL_Fav_NOTE, - Not->NotCod,Not->UsrCod); + /***** Trivial check 2: I must be logged *****/ + if (!Gbl.Usrs.Me.Logged) + { + Ale_ShowAlert (Ale_ERROR,"You are not logged."); + return; + } - /***** Mark possible notifications on this note as removed *****/ - OriginalPubCod = TL_DB_GetPubCodOfOriginalNote (Not->NotCod); - if (OriginalPubCod > 0) - Ntf_MarkNotifAsRemoved (Ntf_EVENT_TIMELINE_FAV,OriginalPubCod); - } + /***** Trivial check 3: The author can not unfav his/her own notes *****/ + if (Usr_ItsMe (Not->UsrCod)) + { + Ale_ShowAlert (Ale_ERROR,"You can not fav/unfav your own posts."); + return; + } + + /***** Trivial check 4: Have I faved this note? *****/ + if (!TL_DB_CheckIfFavedByUsr (TL_Fav_NOTE,Not->NotCod, + Gbl.Usrs.Me.UsrDat.UsrCod)) + // Don't show error message + return; + + /***** Delete the mark as favourite from database *****/ + TL_DB_UnmarkAsFav (TL_Fav_NOTE,Not->NotCod); + + /***** Update number of times this note is favourited *****/ + Not->NumFavs = TL_DB_GetNumTimesHasBeenFav (TL_Fav_NOTE, + Not->NotCod,Not->UsrCod); + + /***** Mark possible notifications on this note as removed *****/ + OriginalPubCod = TL_DB_GetPubCodOfOriginalNote (Not->NotCod); + if (OriginalPubCod > 0) + Ntf_MarkNotifAsRemoved (Ntf_EVENT_TIMELINE_FAV,OriginalPubCod); } /*****************************************************************************/ @@ -306,6 +345,8 @@ void TL_Fav_PutIconToFavUnfComment (const struct TL_Com_Comment *Com, static void TL_Fav_FavComment (struct TL_Com_Comment *Com) { + extern const char *Txt_The_comment_no_longer_exists; + /***** Initialize image *****/ Med_MediaConstructor (&Com->Content.Media); @@ -313,23 +354,50 @@ static void TL_Fav_FavComment (struct TL_Com_Comment *Com) Com->PubCod = TL_Pub_GetParamPubCod (); TL_Com_GetDataOfCommByCod (Com); - if (Com->PubCod > 0) - if (!Usr_ItsMe (Com->UsrCod)) // I am not the author - if (!TL_DB_CheckIfFavedByUsr (TL_Fav_COMM,Com->PubCod, - Gbl.Usrs.Me.UsrDat.UsrCod)) // I have not yet favourited the comment - { - /***** Mark comment as favourite in database *****/ - TL_DB_MarkAsFav (TL_Fav_COMM,Com->PubCod); + /***** Trivial check 1: publication code should be > 0 *****/ + if (Com->PubCod <= 0) + { + Med_MediaDestructor (&Com->Content.Media); + Ale_ShowAlert (Ale_WARNING,Txt_The_comment_no_longer_exists); + return; + } - /***** Update number of times this comment is favourited *****/ - Com->NumFavs = TL_DB_GetNumTimesHasBeenFav (TL_Fav_COMM, - Com->PubCod,Com->UsrCod); + /***** Trivial check 2: I must be logged *****/ + if (!Gbl.Usrs.Me.Logged) + { + Med_MediaDestructor (&Com->Content.Media); + Ale_ShowAlert (Ale_ERROR,"You are not logged."); + return; + } - /***** Create notification about favourite post - for the author of the post *****/ - TL_Ntf_CreateNotifToAuthor (Com->UsrCod,Com->PubCod, - Ntf_EVENT_TIMELINE_FAV); - } + /***** Trivial check 3: The author can not fav his/her own comments *****/ + if (Usr_ItsMe (Com->UsrCod)) + { + Med_MediaDestructor (&Com->Content.Media); + Ale_ShowAlert (Ale_ERROR,"You can not fav/unfav your own comments."); + return; + } + + /***** Trivial check 4: Have I faved this comment? *****/ + if (TL_DB_CheckIfFavedByUsr (TL_Fav_COMM,Com->PubCod, + Gbl.Usrs.Me.UsrDat.UsrCod)) + { + Med_MediaDestructor (&Com->Content.Media); + // Don't show error message + return; + } + + /***** Mark comment as favourite in database *****/ + TL_DB_MarkAsFav (TL_Fav_COMM,Com->PubCod); + + /***** Update number of times this comment is favourited *****/ + Com->NumFavs = TL_DB_GetNumTimesHasBeenFav (TL_Fav_COMM, + Com->PubCod,Com->UsrCod); + + /***** Create notification about favourite post + for the author of the post *****/ + TL_Ntf_CreateNotifToAuthor (Com->UsrCod,Com->PubCod, + Ntf_EVENT_TIMELINE_FAV); /***** Free image *****/ Med_MediaDestructor (&Com->Content.Media); @@ -337,6 +405,8 @@ static void TL_Fav_FavComment (struct TL_Com_Comment *Com) static void TL_Fav_UnfComment (struct TL_Com_Comment *Com) { + extern const char *Txt_The_comment_no_longer_exists; + /***** Initialize image *****/ Med_MediaConstructor (&Com->Content.Media); @@ -344,28 +414,53 @@ static void TL_Fav_UnfComment (struct TL_Com_Comment *Com) Com->PubCod = TL_Pub_GetParamPubCod (); TL_Com_GetDataOfCommByCod (Com); - if (Com->PubCod > 0) - if (Com->NumFavs && - !Usr_ItsMe (Com->UsrCod)) // I am not the author - if (TL_DB_CheckIfFavedByUsr (TL_Fav_COMM,Com->PubCod, - Gbl.Usrs.Me.UsrDat.UsrCod)) // I have favourited the comment - { - /***** Delete the mark as favourite from database *****/ - TL_DB_UnmarkAsFav (TL_Fav_COMM,Com->PubCod); + /***** Trivial check 1: publication code should be > 0 *****/ + if (Com->PubCod <= 0) + { + Med_MediaDestructor (&Com->Content.Media); + Ale_ShowAlert (Ale_WARNING,Txt_The_comment_no_longer_exists); + return; + } - /***** Update number of times this comment is favourited *****/ - Com->NumFavs = TL_DB_GetNumTimesHasBeenFav (TL_Fav_COMM, - Com->PubCod,Com->UsrCod); + /***** Trivial check 2: I must be logged *****/ + if (!Gbl.Usrs.Me.Logged) + { + Med_MediaDestructor (&Com->Content.Media); + Ale_ShowAlert (Ale_ERROR,"You are not logged."); + return; + } - /***** Mark possible notifications on this comment as removed *****/ - Ntf_MarkNotifAsRemoved (Ntf_EVENT_TIMELINE_FAV,Com->PubCod); - } + /***** Trivial check 3: The author can not fav its own notes *****/ + if (Usr_ItsMe (Com->UsrCod)) + { + Med_MediaDestructor (&Com->Content.Media); + Ale_ShowAlert (Ale_ERROR,"You can not fav/unfav your own comments."); + return; + } + + /***** Trivial check 4: Have I faved this comment? *****/ + if (!TL_DB_CheckIfFavedByUsr (TL_Fav_COMM,Com->PubCod, + Gbl.Usrs.Me.UsrDat.UsrCod)) + { + Med_MediaDestructor (&Com->Content.Media); + // Don't show error message + return; + } + + /***** Delete the mark as favourite from database *****/ + TL_DB_UnmarkAsFav (TL_Fav_COMM,Com->PubCod); + + /***** Update number of times this comment is favourited *****/ + Com->NumFavs = TL_DB_GetNumTimesHasBeenFav (TL_Fav_COMM, + Com->PubCod,Com->UsrCod); + + /***** Mark possible notifications on this comment as removed *****/ + Ntf_MarkNotifAsRemoved (Ntf_EVENT_TIMELINE_FAV,Com->PubCod); /***** Free image *****/ Med_MediaDestructor (&Com->Content.Media); } - /*****************************************************************************/ /****************** Put disabled icon to mark as favourite *******************/ /*****************************************************************************/ diff --git a/swad_timeline_note.c b/swad_timeline_note.c index 62af207c..49743fed 100644 --- a/swad_timeline_note.c +++ b/swad_timeline_note.c @@ -218,19 +218,21 @@ void TL_Not_CheckAndWriteNoteWithTopMsg (const struct TL_Timeline *Timeline, | Form to write new comment | | | |__________________________________| / / */ - if (Not->NotCod > 0 && - Not->UsrCod > 0 && - Not->NoteType != TL_NOTE_UNKNOWN) + /***** Trivial check: codes *****/ + if (Not->NotCod <= 0 || + Not->UsrCod <= 0 || + Not->NoteType == TL_NOTE_UNKNOWN) { - /***** Write sharer/commenter if distinct to author *****/ - if (TopMessage != TL_TOP_MESSAGE_NONE) - TL_Not_WriteTopMessage (TopMessage,PublisherCod); - - /***** Write note *****/ - TL_Not_WriteNote (Timeline,Not); - } - else Ale_ShowAlert (Ale_ERROR,"Error in note."); + return; + } + + /***** Write sharer/commenter if distinct to author *****/ + if (TopMessage != TL_TOP_MESSAGE_NONE) + TL_Not_WriteTopMessage (TopMessage,PublisherCod); + + /***** Write note *****/ + TL_Not_WriteNote (Timeline,Not); } /*****************************************************************************/ @@ -1074,33 +1076,39 @@ static void TL_Not_RequestRemovalNote (struct TL_Timeline *Timeline) Not.NotCod = TL_Not_GetParamNotCod (); TL_Not_GetDataOfNoteByCod (&Not); - if (Not.NotCod > 0) + /***** Trivial check 1: note code should be > 0 *****/ + if (Not.NotCod <= 0) { - if (Usr_ItsMe (Not.UsrCod)) // I am the author of this note - { - /***** Show question and button to remove note *****/ - /* Begin alert */ - TL_Frm_BeginAlertRemove (Txt_Do_you_really_want_to_remove_the_following_post); - - /* Show note */ - Box_BoxBegin (NULL,NULL, - NULL,NULL, - NULL,Box_CLOSABLE); - HTM_DIV_Begin ("class=\"TL_WIDTH\""); - TL_Not_CheckAndWriteNoteWithTopMsg (Timeline,&Not, - TL_TOP_MESSAGE_NONE, - -1L); - HTM_DIV_End (); - Box_BoxEnd (); - - /* End alert */ - Timeline->NotCod = Not.NotCod; // Note to be removed - TL_Frm_EndAlertRemove (Timeline,TL_Frm_REM_NOTE, - TL_Not_PutParamsRemoveNote); - } - } - else Ale_ShowAlert (Ale_WARNING,Txt_The_original_post_no_longer_exists); + return; + } + + /***** Trivial check 2: Am I the author of this note *****/ + if (!Usr_ItsMe (Not.UsrCod)) + { + Ale_ShowAlert (Ale_ERROR,"You are not the author."); + return; + } + + /***** Show question and button to remove note *****/ + /* Begin alert */ + TL_Frm_BeginAlertRemove (Txt_Do_you_really_want_to_remove_the_following_post); + + /* Show note */ + Box_BoxBegin (NULL,NULL, + NULL,NULL, + NULL,Box_CLOSABLE); + HTM_DIV_Begin ("class=\"TL_WIDTH\""); + TL_Not_CheckAndWriteNoteWithTopMsg (Timeline,&Not, + TL_TOP_MESSAGE_NONE, + -1L); + HTM_DIV_End (); + Box_BoxEnd (); + + /* End alert */ + Timeline->NotCod = Not.NotCod; // Note to be removed + TL_Frm_EndAlertRemove (Timeline,TL_Frm_REM_NOTE, + TL_Not_PutParamsRemoveNote); } /*****************************************************************************/ @@ -1173,22 +1181,28 @@ static void TL_Not_RemoveNote (void) Not.NotCod = TL_Not_GetParamNotCod (); TL_Not_GetDataOfNoteByCod (&Not); - if (Not.NotCod > 0) + /***** Trivial check 1: note code should be > 0 *****/ + if (Not.NotCod <= 0) { - if (Usr_ItsMe (Not.UsrCod)) // I am the author of this note - { - /***** Delete note from database *****/ - TL_Not_RemoveNoteMediaAndDBEntries (&Not); - - /***** Reset note *****/ - TL_Not_ResetNote (&Not); - - /***** Message of success *****/ - Ale_ShowAlert (Ale_SUCCESS,Txt_TIMELINE_Post_removed); - } - } - else Ale_ShowAlert (Ale_WARNING,Txt_The_original_post_no_longer_exists); + return; + } + + /***** Trivial check 2: Am I the author of this note *****/ + if (!Usr_ItsMe (Not.UsrCod)) + { + Ale_ShowAlert (Ale_ERROR,"You are not the author."); + return; + } + + /***** Delete note from database *****/ + TL_Not_RemoveNoteMediaAndDBEntries (&Not); + + /***** Reset note *****/ + TL_Not_ResetNote (&Not); + + /***** Message of success *****/ + Ale_ShowAlert (Ale_SUCCESS,Txt_TIMELINE_Post_removed); } /*****************************************************************************/ @@ -1338,23 +1352,25 @@ void TL_Not_GetDataOfNoteByCod (struct TL_Not_Note *Not) MYSQL_RES *mysql_res; MYSQL_ROW row; - if (Not->NotCod > 0) + /***** Trivial check: note code should be > 0 *****/ + if (Not->NotCod <= 0) { - /***** Get data of note from database *****/ - if (TL_DB_GetDataOfNoteByCod (Not->NotCod,&mysql_res)) - { - /***** Get data of note *****/ - row = mysql_fetch_row (mysql_res); - TL_Not_GetDataOfNoteFromRow (row,Not); - } - else - /***** Reset fields of note *****/ - TL_Not_ResetNote (Not); + /***** Reset fields of note *****/ + TL_Not_ResetNote (Not); + return; + } - /***** Free structure that stores the query result *****/ - DB_FreeMySQLResult (&mysql_res); + /***** Get data of note from database *****/ + if (TL_DB_GetDataOfNoteByCod (Not->NotCod,&mysql_res)) + { + /***** Get data of note *****/ + row = mysql_fetch_row (mysql_res); + TL_Not_GetDataOfNoteFromRow (row,Not); } else /***** Reset fields of note *****/ TL_Not_ResetNote (Not); + + /***** Free structure that stores the query result *****/ + DB_FreeMySQLResult (&mysql_res); } diff --git a/swad_timeline_share.c b/swad_timeline_share.c index 40b13df5..d324c4d9 100644 --- a/swad_timeline_share.c +++ b/swad_timeline_share.c @@ -115,6 +115,7 @@ void TL_Sha_ShaNoteGbl (void) static void TL_Sha_ShaNote (struct TL_Not_Note *Not) { + extern const char *Txt_The_original_post_no_longer_exists; struct TL_Pub_Publication Pub; long OriginalPubCod; @@ -122,28 +123,47 @@ static void TL_Sha_ShaNote (struct TL_Not_Note *Not) Not->NotCod = TL_Not_GetParamNotCod (); TL_Not_GetDataOfNoteByCod (Not); - if (Not->NotCod > 0) - if (Gbl.Usrs.Me.Logged && // I am logged... - !Usr_ItsMe (Not->UsrCod)) // ...but I am not the author - if (!TL_DB_CheckIfNoteIsSharedByUsr (Not->NotCod, - Gbl.Usrs.Me.UsrDat.UsrCod)) // Not yet shared by me - { - /***** Share (publish note in timeline) *****/ - Pub.NotCod = Not->NotCod; - Pub.PublisherCod = Gbl.Usrs.Me.UsrDat.UsrCod; - Pub.PubType = TL_Pub_SHARED_NOTE; - TL_Pub_PublishPubInTimeline (&Pub); // Set Pub.PubCod + /***** Trivial check 1: note code should be > 0 *****/ + if (Not->NotCod <= 0) + { + Ale_ShowAlert (Ale_WARNING,Txt_The_original_post_no_longer_exists); + return; + } - /***** Update number of times this note is shared *****/ - Not->NumShared = TL_DB_GetNumTimesANoteHasBeenShared (Not); + /***** Trivial check 2: Am I logged? *****/ + if (!Gbl.Usrs.Me.Logged) + { + Ale_ShowAlert (Ale_ERROR,"You are not logged."); + return; + } - /***** Create notification about shared post - for the author of the post *****/ - OriginalPubCod = TL_DB_GetPubCodOfOriginalNote (Not->NotCod); - if (OriginalPubCod > 0) - TL_Ntf_CreateNotifToAuthor (Not->UsrCod,OriginalPubCod, - Ntf_EVENT_TIMELINE_SHARE); - } + /***** Trivial check 3: Am I the author? *****/ + if (Usr_ItsMe (Not->UsrCod)) + { + Ale_ShowAlert (Ale_ERROR,"You can not share/unshare your own posts."); + return; + } + + /***** Trivial check 4: Is note already shared by me? *****/ + if (TL_DB_CheckIfNoteIsSharedByUsr (Not->NotCod,Gbl.Usrs.Me.UsrDat.UsrCod)) + // Don't show error message + return; + + /***** Share (publish note in timeline) *****/ + Pub.NotCod = Not->NotCod; + Pub.PublisherCod = Gbl.Usrs.Me.UsrDat.UsrCod; + Pub.PubType = TL_Pub_SHARED_NOTE; + TL_Pub_PublishPubInTimeline (&Pub); // Set Pub.PubCod + + /***** Update number of times this note is shared *****/ + Not->NumShared = TL_DB_GetNumTimesANoteHasBeenShared (Not); + + /***** Create notification about shared post + for the author of the post *****/ + OriginalPubCod = TL_DB_GetPubCodOfOriginalNote (Not->NotCod); + if (OriginalPubCod > 0) + TL_Ntf_CreateNotifToAuthor (Not->UsrCod,OriginalPubCod, + Ntf_EVENT_TIMELINE_SHARE); } /*****************************************************************************/ @@ -172,30 +192,49 @@ void TL_Sha_UnsNoteGbl (void) static void TL_Sha_UnsNote (struct TL_Not_Note *Not) { + extern const char *Txt_The_original_post_no_longer_exists; long OriginalPubCod; /***** Get data of note *****/ Not->NotCod = TL_Not_GetParamNotCod (); TL_Not_GetDataOfNoteByCod (Not); - if (Not->NotCod > 0) - if (Not->NumShared && - Gbl.Usrs.Me.Logged && // I am logged... - !Usr_ItsMe (Not->UsrCod)) // ...but I am not the author - if (TL_DB_CheckIfNoteIsSharedByUsr (Not->NotCod, - Gbl.Usrs.Me.UsrDat.UsrCod)) // I am a sharer - { - /***** Delete publication from database *****/ - TL_DB_RemoveSharedPub (Not->NotCod); + /***** Trivial check 1: note code should be > 0 *****/ + if (Not->NotCod <= 0) + { + Ale_ShowAlert (Ale_WARNING,Txt_The_original_post_no_longer_exists); + return; + } - /***** Update number of times this note is shared *****/ - Not->NumShared = TL_DB_GetNumTimesANoteHasBeenShared (Not); + /***** Trivial check 2: Am I logged? *****/ + if (!Gbl.Usrs.Me.Logged) + { + Ale_ShowAlert (Ale_ERROR,"You are not logged."); + return; + } - /***** Mark possible notifications on this note as removed *****/ - OriginalPubCod = TL_DB_GetPubCodOfOriginalNote (Not->NotCod); - if (OriginalPubCod > 0) - Ntf_MarkNotifAsRemoved (Ntf_EVENT_TIMELINE_SHARE,OriginalPubCod); - } + /***** Trivial check 3: Am I the author? *****/ + if (Usr_ItsMe (Not->UsrCod)) + { + Ale_ShowAlert (Ale_ERROR,"You can not share/unshare your own posts."); + return; + } + + /***** Trivial check 4: Is note already shared by me? *****/ + if (!TL_DB_CheckIfNoteIsSharedByUsr (Not->NotCod,Gbl.Usrs.Me.UsrDat.UsrCod)) + // Don't show error message + return; + + /***** Delete publication from database *****/ + TL_DB_RemoveSharedPub (Not->NotCod); + + /***** Update number of times this note is shared *****/ + Not->NumShared = TL_DB_GetNumTimesANoteHasBeenShared (Not); + + /***** Mark possible notifications on this note as removed *****/ + OriginalPubCod = TL_DB_GetPubCodOfOriginalNote (Not->NotCod); + if (OriginalPubCod > 0) + Ntf_MarkNotifAsRemoved (Ntf_EVENT_TIMELINE_SHARE,OriginalPubCod); } void TL_Sha_PutIconToShaUnsNote (const struct TL_Not_Note *Not,