swad-core/swad_timeline_share.c

363 lines
13 KiB
C
Raw Normal View History

2021-02-08 18:33:16 +01:00
// swad_timeline_share.c: social timeline shared
/*
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.
2021-02-09 12:43:45 +01:00
Copyright (C) 1999-2021 Antonio Ca<EFBFBD>as Vargas
2021-02-08 18:33:16 +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 "swad_database.h"
#include "swad_global.h"
#include "swad_timeline.h"
#include "swad_timeline_form.h"
2021-02-11 17:45:16 +01:00
#include "swad_timeline_notification.h"
2021-02-11 13:10:08 +01:00
#include "swad_timeline_publication.h"
2021-02-08 18:33:16 +01:00
#include "swad_timeline_share.h"
/*****************************************************************************/
/****************************** Public constants *****************************/
/*****************************************************************************/
/*****************************************************************************/
/************************* Private constants and types ***********************/
/*****************************************************************************/
2021-02-11 20:19:14 +01:00
#define TL_Sha_ICON_SHARE "share-alt.svg"
#define TL_Sha_ICON_SHARED "share-alt-green.svg"
2021-02-08 18:33:16 +01:00
/*****************************************************************************/
/************** External global variables from others modules ****************/
/*****************************************************************************/
extern struct Globals Gbl;
/*****************************************************************************/
/************************* Private global variables **************************/
/*****************************************************************************/
/*****************************************************************************/
/***************************** Private prototypes ****************************/
/*****************************************************************************/
2021-02-11 00:58:53 +01:00
static void TL_Sha_ShaNote (struct TL_Not_Note *Not);
static void TL_Sha_UnsNote (struct TL_Not_Note *Not);
2021-02-08 18:33:16 +01:00
static void TL_Sha_PutDisabledIconShare (unsigned NumShared);
static void TL_Sha_PutFormToShaUnsNote (long NotCod);
2021-02-08 18:33:16 +01:00
static bool TL_Sha_CheckIfNoteIsSharedByUsr (long NotCod,long UsrCod);
2021-02-11 00:58:53 +01:00
static void TL_Sha_ShowUsrsWhoHaveSharedNote (const struct TL_Not_Note *Not,
2021-02-11 17:20:38 +01:00
TL_Usr_HowManyUsrs_t HowManyUsrs);
2021-02-08 18:33:16 +01:00
/*****************************************************************************/
/******************************** Share a note *******************************/
/*****************************************************************************/
void TL_Sha_ShowAllSharersNoteUsr (void)
{
/***** Get user whom profile is displayed *****/
Usr_GetParamOtherUsrCodEncryptedAndGetUsrData ();
/***** Show all sharers *****/
TL_Sha_ShowAllSharersNoteGbl ();
}
void TL_Sha_ShowAllSharersNoteGbl (void)
{
2021-02-11 00:58:53 +01:00
struct TL_Not_Note Not;
2021-02-08 18:33:16 +01:00
/***** Get data of note *****/
2021-02-11 00:58:53 +01:00
Not.NotCod = TL_Not_GetParamNotCod ();
TL_Not_GetDataOfNoteByCod (&Not);
2021-02-08 18:33:16 +01:00
/***** Write HTML inside DIV with form to share/unshare *****/
TL_Sha_PutIconToShaUnsNote (&Not,TL_Usr_SHOW_ALL_USRS);
2021-02-08 18:33:16 +01:00
}
void TL_Sha_ShaNoteUsr (void)
{
/***** Get user whom profile is displayed *****/
Usr_GetParamOtherUsrCodEncryptedAndGetUsrData ();
/***** Share note *****/
TL_Sha_ShaNoteGbl ();
}
void TL_Sha_ShaNoteGbl (void)
{
2021-02-11 00:58:53 +01:00
struct TL_Not_Note Not;
2021-02-08 18:33:16 +01:00
/***** Share note *****/
2021-02-10 19:53:57 +01:00
TL_Sha_ShaNote (&Not);
2021-02-08 18:33:16 +01:00
/***** Write HTML inside DIV with form to unshare *****/
TL_Sha_PutIconToShaUnsNote (&Not,TL_Usr_SHOW_FEW_USRS);
2021-02-08 18:33:16 +01:00
}
2021-02-11 00:58:53 +01:00
static void TL_Sha_ShaNote (struct TL_Not_Note *Not)
2021-02-08 18:33:16 +01:00
{
2021-02-11 13:10:08 +01:00
struct TL_Pub_Publication Pub;
2021-02-08 18:33:16 +01:00
long OriginalPubCod;
/***** Get data of note *****/
2021-02-11 00:58:53 +01:00
Not->NotCod = TL_Not_GetParamNotCod ();
TL_Not_GetDataOfNoteByCod (Not);
2021-02-08 18:33:16 +01:00
2021-02-10 19:53:57 +01:00
if (Not->NotCod > 0)
if (Gbl.Usrs.Me.Logged && // I am logged...
!Usr_ItsMe (Not->UsrCod)) // ...but I am not the author
2021-02-10 19:53:57 +01:00
if (!TL_Sha_CheckIfNoteIsSharedByUsr (Not->NotCod,
2021-02-11 00:58:53 +01:00
Gbl.Usrs.Me.UsrDat.UsrCod)) // Not yet shared by me
2021-02-08 18:33:16 +01:00
{
/***** Share (publish note in timeline) *****/
2021-02-10 19:53:57 +01:00
Pub.NotCod = Not->NotCod;
Pub.PublisherCod = Gbl.Usrs.Me.UsrDat.UsrCod;
2021-02-11 20:19:14 +01:00
Pub.PubType = TL_Pub_SHARED_NOTE;
2021-02-11 13:10:08 +01:00
TL_Pub_PublishPubInTimeline (&Pub); // Set Pub.PubCod
2021-02-08 18:33:16 +01:00
/* Update number of times this note is shared */
2021-02-10 19:53:57 +01:00
TL_Sha_UpdateNumTimesANoteHasBeenShared (Not);
2021-02-08 18:33:16 +01:00
/**** Create notification about shared post
for the author of the post ***/
OriginalPubCod = TL_DB_GetPubCodOfOriginalNote (Not->NotCod);
2021-02-08 18:33:16 +01:00
if (OriginalPubCod > 0)
2021-02-11 20:19:14 +01:00
TL_Ntf_CreateNotifToAuthor (Not->UsrCod,OriginalPubCod,
Ntf_EVENT_TIMELINE_SHARE);
2021-02-08 18:33:16 +01:00
}
}
/*****************************************************************************/
/******************** Unshare a previously shared note ***********************/
/*****************************************************************************/
void TL_Sha_UnsNoteUsr (void)
{
/***** Get user whom profile is displayed *****/
Usr_GetParamOtherUsrCodEncryptedAndGetUsrData ();
/***** Unshare note *****/
TL_Sha_UnsNoteGbl ();
}
void TL_Sha_UnsNoteGbl (void)
{
2021-02-11 00:58:53 +01:00
struct TL_Not_Note Not;
2021-02-08 18:33:16 +01:00
/***** Unshare note *****/
2021-02-10 19:53:57 +01:00
TL_Sha_UnsNote (&Not);
2021-02-08 18:33:16 +01:00
/***** Write HTML inside DIV with form to share *****/
TL_Sha_PutIconToShaUnsNote (&Not,TL_Usr_SHOW_FEW_USRS);
2021-02-08 18:33:16 +01:00
}
2021-02-11 00:58:53 +01:00
static void TL_Sha_UnsNote (struct TL_Not_Note *Not)
2021-02-08 18:33:16 +01:00
{
long OriginalPubCod;
/***** Get data of note *****/
2021-02-11 00:58:53 +01:00
Not->NotCod = TL_Not_GetParamNotCod ();
TL_Not_GetDataOfNoteByCod (Not);
2021-02-08 18:33:16 +01:00
2021-02-10 19:53:57 +01:00
if (Not->NotCod > 0)
if (Not->NumShared &&
Gbl.Usrs.Me.Logged && // I am logged...
!Usr_ItsMe (Not->UsrCod)) // ...but I am not the author
2021-02-10 19:53:57 +01:00
if (TL_Sha_CheckIfNoteIsSharedByUsr (Not->NotCod,
2021-02-08 18:33:16 +01:00
Gbl.Usrs.Me.UsrDat.UsrCod)) // I am a sharer
{
/***** Delete publication from database *****/
DB_QueryDELETE ("can not remove a publication",
"DELETE FROM tl_pubs"
" WHERE NotCod=%ld"
" AND PublisherCod=%ld"
" AND PubType=%u",
2021-02-10 19:53:57 +01:00
Not->NotCod,
2021-02-08 18:33:16 +01:00
Gbl.Usrs.Me.UsrDat.UsrCod,
2021-02-11 20:19:14 +01:00
(unsigned) TL_Pub_SHARED_NOTE);
2021-02-08 18:33:16 +01:00
/***** Update number of times this note is shared *****/
2021-02-10 19:53:57 +01:00
TL_Sha_UpdateNumTimesANoteHasBeenShared (Not);
2021-02-08 18:33:16 +01:00
/***** Mark possible notifications on this note as removed *****/
OriginalPubCod = TL_DB_GetPubCodOfOriginalNote (Not->NotCod);
2021-02-08 18:33:16 +01:00
if (OriginalPubCod > 0)
Ntf_MarkNotifAsRemoved (Ntf_EVENT_TIMELINE_SHARE,OriginalPubCod);
}
}
void TL_Sha_PutIconToShaUnsNote (const struct TL_Not_Note *Not,
2021-02-11 17:20:38 +01:00
TL_Usr_HowManyUsrs_t HowManyUsrs)
2021-02-08 18:33:16 +01:00
{
/***** Put form to share/unshare this note *****/
HTM_DIV_Begin ("class=\"TL_ICO\"");
2021-02-10 19:53:57 +01:00
if (Not->Unavailable || // Unavailable notes can not be shared
Usr_ItsMe (Not->UsrCod)) // I am the author
2021-02-08 18:33:16 +01:00
/* Put disabled icon */
2021-02-10 19:53:57 +01:00
TL_Sha_PutDisabledIconShare (Not->NumShared);
2021-02-08 18:33:16 +01:00
else // Available and I am not the author
/* Put icon to share/unshare */
TL_Sha_PutFormToShaUnsNote (Not->NotCod);
2021-02-08 18:33:16 +01:00
HTM_DIV_End ();
/***** Show who have shared this note *****/
2021-02-10 19:53:57 +01:00
TL_Sha_ShowUsrsWhoHaveSharedNote (Not,HowManyUsrs);
2021-02-08 18:33:16 +01:00
}
/*****************************************************************************/
/*********************** Put disabled icon to share **************************/
/*****************************************************************************/
static void TL_Sha_PutDisabledIconShare (unsigned NumShared)
{
extern const char *Txt_TIMELINE_Shared_by_X_USERS;
extern const char *Txt_TIMELINE_Not_shared_by_anyone;
/***** Disabled icon to share *****/
if (NumShared)
{
Ico_PutDivIcon ("TL_ICO_DISABLED",TL_Sha_ICON_SHARE,
Str_BuildStringLong (Txt_TIMELINE_Shared_by_X_USERS,
(long) NumShared));
Str_FreeString ();
}
else
Ico_PutDivIcon ("TL_ICO_DISABLED",TL_Sha_ICON_SHARE,
Txt_TIMELINE_Not_shared_by_anyone);
}
/*****************************************************************************/
/*********************** Form to share/unshare note **************************/
/*****************************************************************************/
static void TL_Sha_PutFormToShaUnsNote (long NotCod)
{
extern const char *Txt_TIMELINE_Shared;
extern const char *Txt_TIMELINE_Share;
struct TL_Form Form[2] =
{
[false] = // I have not shared ==> share
{
.Action = TL_Frm_SHA_NOTE,
.ParamFormat = "NotCod=%ld",
.ParamCod = NotCod,
.Icon = TL_Sha_ICON_SHARE,
.Title = Txt_TIMELINE_Share,
},
[true] = // I have shared ==> unshare
{
.Action = TL_Frm_UNS_NOTE,
.ParamFormat = "NotCod=%ld",
.ParamCod = NotCod,
.Icon = TL_Sha_ICON_SHARED,
.Title = Txt_TIMELINE_Shared,
},
};
/***** Form and icon to share/unshare note *****/
TL_Frm_FormFavSha (&Form[TL_Sha_CheckIfNoteIsSharedByUsr (NotCod,Gbl.Usrs.Me.UsrDat.UsrCod)]);
}
2021-02-08 18:33:16 +01:00
/*****************************************************************************/
/****************** Check if a user has published a note *********************/
/*****************************************************************************/
static bool TL_Sha_CheckIfNoteIsSharedByUsr (long NotCod,long UsrCod)
{
return (DB_QueryCOUNT ("can not check if a user has shared a note",
"SELECT COUNT(*) FROM tl_pubs"
" WHERE NotCod=%ld"
" AND PublisherCod=%ld"
" AND PubType=%u",
NotCod,
UsrCod,
2021-02-11 20:19:14 +01:00
(unsigned) TL_Pub_SHARED_NOTE) != 0);
2021-02-08 18:33:16 +01:00
}
/*****************************************************************************/
/********** Get number of times a note has been shared in timeline ***********/
/*****************************************************************************/
2021-02-11 00:58:53 +01:00
void TL_Sha_UpdateNumTimesANoteHasBeenShared (struct TL_Not_Note *Not)
2021-02-08 18:33:16 +01:00
{
/***** Get number of times (users) this note has been shared *****/
2021-02-10 19:53:57 +01:00
Not->NumShared =
2021-02-08 18:33:16 +01:00
(unsigned) DB_QueryCOUNT ("can not get number of times"
" a note has been shared",
"SELECT COUNT(*) FROM tl_pubs"
" WHERE NotCod=%ld"
" AND PublisherCod<>%ld"
" AND PubType=%u",
2021-02-10 19:53:57 +01:00
Not->NotCod,
Not->UsrCod, // The author
2021-02-11 20:19:14 +01:00
(unsigned) TL_Pub_SHARED_NOTE);
2021-02-08 18:33:16 +01:00
}
/*****************************************************************************/
/******************* Show users who have shared this note ********************/
/*****************************************************************************/
2021-02-11 00:58:53 +01:00
static void TL_Sha_ShowUsrsWhoHaveSharedNote (const struct TL_Not_Note *Not,
2021-02-11 17:20:38 +01:00
TL_Usr_HowManyUsrs_t HowManyUsrs)
2021-02-08 18:33:16 +01:00
{
MYSQL_RES *mysql_res;
2021-02-08 19:04:47 +01:00
unsigned NumFirstUsrs;
2021-02-08 18:33:16 +01:00
/***** Get users who have shared this note *****/
2021-02-10 19:53:57 +01:00
if (Not->NumShared)
2021-02-08 18:33:16 +01:00
NumFirstUsrs =
(unsigned) DB_QuerySELECT (&mysql_res,"can not get users",
"SELECT PublisherCod FROM tl_pubs"
" WHERE NotCod=%ld"
" AND PublisherCod<>%ld"
" AND PubType=%u"
" ORDER BY PubCod LIMIT %u",
2021-02-10 19:53:57 +01:00
Not->NotCod,
Not->UsrCod,
2021-02-11 20:19:14 +01:00
(unsigned) TL_Pub_SHARED_NOTE,
2021-02-11 17:20:38 +01:00
HowManyUsrs == TL_Usr_SHOW_FEW_USRS ? TL_Usr_DEF_USRS_SHOWN :
TL_Usr_MAX_USRS_SHOWN);
2021-02-08 19:04:47 +01:00
else
NumFirstUsrs = 0;
2021-02-08 18:33:16 +01:00
/***** Show users *****/
2021-02-08 19:04:47 +01:00
/* Number of users */
2021-02-08 18:33:16 +01:00
HTM_DIV_Begin ("class=\"TL_NUM_USRS\"");
2021-02-11 17:20:38 +01:00
TL_Usr_ShowNumSharersOrFavers (Not->NumShared);
2021-02-08 18:33:16 +01:00
HTM_DIV_End ();
2021-02-08 19:04:47 +01:00
/* List users one by one */
2021-02-08 18:33:16 +01:00
HTM_DIV_Begin ("class=\"TL_USRS\"");
2021-02-11 17:20:38 +01:00
TL_Usr_ShowSharersOrFavers (&mysql_res,Not->NumShared,NumFirstUsrs);
2021-02-10 19:53:57 +01:00
if (NumFirstUsrs < Not->NumShared)
2021-02-08 19:04:47 +01:00
/* Clickable ellipsis to show all users */
TL_Frm_PutFormToSeeAllFaversSharers (TL_Frm_ALL_SHA_NOTE,
2021-02-11 20:19:14 +01:00
"NotCod=%ld",Not->NotCod,
HowManyUsrs);
2021-02-08 18:33:16 +01:00
HTM_DIV_End ();
/***** Free structure that stores the query result *****/
2021-02-10 19:53:57 +01:00
if (Not->NumShared)
2021-02-08 18:33:16 +01:00
DB_FreeMySQLResult (&mysql_res);
}