swad-core/swad_timeline_notification.c

209 lines
7.2 KiB
C
Raw Normal View History

2021-02-11 17:45:33 +01:00
// swad_timeline_notification.c: social timeline notifications
/*
SWAD (Shared Workspace At a Distance),
is a web platform developed at the University of Granada (Spain),
and used to support university teaching.
This file is part of SWAD core.
Copyright (C) 1999-2023 Antonio Ca<EFBFBD>as Vargas
2021-02-11 17:45:33 +01:00
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General 3 License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*****************************************************************************/
/*********************************** Headers *********************************/
/*****************************************************************************/
#include <stdbool.h> // For boolean type
#include <string.h> // For string functions
#include "swad_global.h"
#include "swad_notification_database.h"
2021-02-11 17:45:33 +01:00
#include "swad_timeline.h"
#include "swad_timeline_database.h"
2021-02-11 17:45:33 +01:00
#include "swad_timeline_note.h"
#include "swad_timeline_notification.h"
#include "swad_timeline_post.h"
#include "swad_timeline_publication.h"
/*****************************************************************************/
/************** External global variables from others modules ****************/
/*****************************************************************************/
extern struct Globals Gbl;
/*****************************************************************************/
/*********** Create a notification for the author of a post/comment **********/
/*****************************************************************************/
void TmlNtf_CreateNotifToAuthor (long AuthorCod,long PubCod,
Ntf_NotifyEvent_t NotifyEvent)
2021-02-11 17:45:33 +01:00
{
struct Usr_Data UsrDat;
2021-02-11 17:45:33 +01:00
bool CreateNotif;
bool NotifyByEmail;
/***** Initialize structure with user's data *****/
Usr_UsrDataConstructor (&UsrDat);
UsrDat.UsrCod = AuthorCod;
if (Usr_ChkUsrCodAndGetAllUsrDataFromUsrCod (&UsrDat,
Usr_DONT_GET_PREFS,
Usr_DONT_GET_ROLE_IN_CURRENT_CRS))
2021-02-11 17:45:33 +01:00
{
/***** This fav must be notified by email? *****/
CreateNotif = (UsrDat.NtfEvents.CreateNotif & (1 << NotifyEvent));
NotifyByEmail = CreateNotif &&
(UsrDat.NtfEvents.SendEmail & (1 << NotifyEvent));
/***** Create notification for the author of the post.
If this author wants to receive notifications by email,
activate the sending of a notification *****/
if (CreateNotif)
Ntf_DB_StoreNotifyEventToUsr (NotifyEvent,UsrDat.UsrCod,PubCod,
(Ntf_Status_t) (NotifyByEmail ? Ntf_STATUS_BIT_EMAIL :
0),
Gbl.Hierarchy.Ins.InsCod,
Gbl.Hierarchy.Ctr.CtrCod,
Gbl.Hierarchy.Deg.DegCod,
Gbl.Hierarchy.Crs.CrsCod);
2021-02-11 17:45:33 +01:00
}
/***** Free memory used for user's data *****/
Usr_UsrDataDestructor (&UsrDat);
}
/*****************************************************************************/
/***************** Get notification of a new publication *********************/
/*****************************************************************************/
void TmlNtf_GetNotifPublication (char SummaryStr[Ntf_MAX_BYTES_SUMMARY + 1],
char **ContentStr,
long PubCod,bool GetContent)
2021-02-11 17:45:33 +01:00
{
MYSQL_RES *mysql_res;
MYSQL_ROW row;
struct TmlPub_Publication Pub;
struct TmlNot_Note Not;
struct TmlPst_Content Content;
2021-02-11 17:45:33 +01:00
size_t Length;
bool ContentCopied = false;
/***** Return nothing on error *****/
Pub.Type = TmlPub_UNKNOWN;
SummaryStr[0] = '\0'; // Return nothing on error
2021-02-11 17:45:33 +01:00
Content.Txt[0] = '\0';
/***** Get summary and content from post from database *****/
if (Tml_DB_GetPubDataByCod (PubCod,&mysql_res) == 1) // Result should have a unique row
2021-02-11 17:45:33 +01:00
/* Get data of publication from row */
TmlPub_GetPubDataFromRow (mysql_res,&Pub);
2021-02-11 17:45:33 +01:00
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
/***** Get summary and content *****/
switch (Pub.Type)
2021-02-11 17:45:33 +01:00
{
case TmlPub_UNKNOWN:
2021-02-11 17:45:33 +01:00
break;
case TmlPub_ORIGINAL_NOTE:
case TmlPub_SHARED_NOTE:
2021-02-11 17:45:33 +01:00
/* Get data of note */
Not.NotCod = Pub.NotCod;
TmlNot_GetNoteDataByCod (&Not);
2021-02-11 17:45:33 +01:00
if (Not.Type == TmlNot_POST)
2021-02-11 17:45:33 +01:00
{
/***** Get post from database *****/
if (Tml_DB_GetPostByCod (Not.Cod,&mysql_res) == 1) // Result should have a unique row
2021-02-11 17:45:33 +01:00
{
/* Get row */
2021-02-11 17:45:33 +01:00
row = mysql_fetch_row (mysql_res);
/* Get only text content (row[0]) */
Str_Copy (Content.Txt,row[0],sizeof (Content.Txt) - 1);
2021-02-11 17:45:33 +01:00
}
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
/***** Copy content string *****/
if (GetContent)
{
Length = strlen (Content.Txt);
if ((*ContentStr = malloc (Length + 1)) != NULL)
2021-02-11 17:45:33 +01:00
{
Str_Copy (*ContentStr,Content.Txt,Length);
2021-02-11 17:45:33 +01:00
ContentCopied = true;
}
}
/***** Copy summary string *****/
Str_LimitLengthHTMLStr (Content.Txt,Ntf_MAX_CHARS_SUMMARY);
Str_Copy (SummaryStr,Content.Txt,Ntf_MAX_BYTES_SUMMARY);
2021-02-11 17:45:33 +01:00
}
else
TmlNot_GetNoteSummary (&Not,SummaryStr);
2021-02-11 17:45:33 +01:00
break;
case TmlPub_COMMENT_TO_NOTE:
/***** Get content of comment from database *****/
if (Tml_DB_GetCommDataByCod (Pub.PubCod,&mysql_res) == 1) // Result should have a unique row
2021-02-11 17:45:33 +01:00
{
/* Get row */
2021-02-11 17:45:33 +01:00
row = mysql_fetch_row (mysql_res);
/* Get only text content (row[4]) */
Str_Copy (Content.Txt,row[4],sizeof (Content.Txt) - 1);
2021-02-11 17:45:33 +01:00
}
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
/***** Copy content string *****/
if (GetContent)
{
Length = strlen (Content.Txt);
if ((*ContentStr = malloc (Length + 1)) != NULL)
2021-02-11 17:45:33 +01:00
{
Str_Copy (*ContentStr,Content.Txt,Length);
2021-02-11 17:45:33 +01:00
ContentCopied = true;
}
}
/***** Copy summary string *****/
Str_LimitLengthHTMLStr (Content.Txt,Ntf_MAX_CHARS_SUMMARY);
Str_Copy (SummaryStr,Content.Txt,Ntf_MAX_BYTES_SUMMARY);
2021-02-11 17:45:33 +01:00
break;
}
/***** Create empty content string if nothing copied *****/
if (GetContent && !ContentCopied)
if ((*ContentStr = malloc (1)) != NULL)
2021-02-11 17:45:33 +01:00
(*ContentStr)[0] = '\0';
}
/*****************************************************************************/
/************ Mark all my notifications about timeline as seen ***************/
/*****************************************************************************/
// Must be executed as a priori function
void TmlNtf_MarkMyNotifAsSeen (void)
2021-02-11 17:45:33 +01:00
{
Ntf_DB_MarkNotifsAsSeen (Ntf_EVENT_TML_COMMENT);
Ntf_DB_MarkNotifsAsSeen (Ntf_EVENT_TML_FAV );
Ntf_DB_MarkNotifsAsSeen (Ntf_EVENT_TML_SHARE );
Ntf_DB_MarkNotifsAsSeen (Ntf_EVENT_TML_MENTION);
2021-02-11 17:45:33 +01:00
}