implemented messages attachments

This commit is contained in:
Alfredo Oliviero 2024-10-22 16:37:00 +02:00
parent 660e399d44
commit 70cd652655
5 changed files with 143 additions and 52 deletions

View File

@ -4,11 +4,12 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [v2.2.0-SNAPSHOT]
## [v2.2.1-SNAPSHOT]
- moved messages from storagehub to cassandra [#27514]
- import messages from storagehub
## [v2.1.1-SNAPSHOT]
## [v2.1.1]
- tests for Mail notification Catalogue Item Rejectsed [#28020]

View File

@ -11,7 +11,7 @@
<groupId>org.gcube.portal</groupId>
<artifactId>social-networking-library</artifactId>
<version>2.2.0-SNAPSHOT</version>
<version>2.2.1-SNAPSHOT</version>
<name>gCube Social Networking Library</name>
<description>
The gCube Social Networking Library is the 'bridge' between your gCube Applications and the social networking facilities.

View File

@ -2541,8 +2541,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
_log.debug("Asking for Single Notification preference of " + userid + " Type: " + notificationType);
List<NotificationChannelType> toReturn = new ArrayList<NotificationChannelType>();
Map<NotificationType, NotificationChannelType[]> userNotPref = getUserNotificationPreferences(userid);
if(userNotPref ==null || ! userNotPref.containsKey(notificationType) || userNotPref.get(notificationType) == null ) {
_log.debug("Single Notification preference of " + userid + " Type: " + notificationType + " not existing ... creating default");
if (userNotPref == null || !userNotPref.containsKey(notificationType)
|| userNotPref.get(notificationType) == null) {
_log.debug("Single Notification preference of " + userid + " Type: " + notificationType
+ " not existing ... creating default");
return createNewNotificationType(userid, notificationType);
}
@ -2572,7 +2574,8 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
String valueToInsert = "";
NotificationChannelType[] wpTypes = NotificationChannelType.values();
// Set default channels to PORTAL for workspace notifications (starting with WP_), otherwise use all channels
// Set default channels to PORTAL for workspace notifications (starting with
// WP_), otherwise use all channels
if (notificationType.toString().startsWith("WP_")) {
valueToInsert += NotificationChannelType.PORTAL;
toReturn.add(NotificationChannelType.PORTAL);
@ -2646,7 +2649,8 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
* {@inheritDoc}
*
* by default Workspace and Calendar Notifications are set to Portal
* check if the number of preferences found is the same of NotificationType.values().length, if different creates the missing entries
* check if the number of preferences found is the same of
* NotificationType.values().length, if different creates the missing entries
*/
@Override
public Map<NotificationType, NotificationChannelType[]> getUserNotificationPreferences(String userid)
@ -2669,9 +2673,11 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
}
// Check if the number of results is different from the total number of NotificationType values, meaning some preferences are missing
// Check if the number of results is different from the total number of
// NotificationType values, meaning some preferences are missing
List<Row> results = new ArrayList<>();
if(result!=null) results = result.all();
if (result != null)
results = result.all();
if (results.size() != NotificationType.values().length) {
_log.debug("Userid " + userid + " settings incomplete, initiating missing preferences...");
@ -2691,7 +2697,8 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
_log.debug("Missing preference for " + notificationType + ", setting default.");
NotificationChannelType[] defaultChannels;
// Set default channels to PORTAL for workspace notifications (starting with WP_), otherwise use all channels
// Set default channels to PORTAL for workspace notifications (starting with
// WP_), otherwise use all channels
if (notificationType.toString().startsWith("WP_")) {
defaultChannels = new NotificationChannelType[] { NotificationChannelType.PORTAL };
} else {
@ -2715,8 +2722,14 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
for (Row row : results) {
String[] channels = row.getString(PREFERENCE).split(",");
// _log.debug("Row : " + row.getString(PREFERENCE));
if (channels != null && channels.length == 1 && channels[0].toString().equals("") ) { //it is empty, preference is set to no notification at all
//_log.debug("adding CHANNELS NULL: " + getNotificationType(row.getString(TYPE)) + ", " + new NotificationChannelType[0]);
if (channels != null && channels.length == 1 && channels[0].toString().equals("")) { // it is empty,
// preference is set
// to no
// notification at
// all
// _log.debug("adding CHANNELS NULL: " +
// getNotificationType(row.getString(TYPE)) + ", " + new
// NotificationChannelType[0]);
toReturn.put(getNotificationType(row.getString(TYPE)), new NotificationChannelType[0]);
} else {
NotificationChannelType[] toAdd = new NotificationChannelType[channels.length];
@ -2725,7 +2738,8 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
toAdd[i] = (getChannelType(channels[i]));
}
}
//_log.debug("adding channels not null: " + getNotificationType(row.getString(TYPE)) + ", " + toAdd.toString());
// _log.debug("adding channels not null: " +
// getNotificationType(row.getString(TYPE)) + ", " + toAdd.toString());
toReturn.put(getNotificationType(row.getString(TYPE)), toAdd);
}
}
@ -4933,7 +4947,8 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
.setColumn(Schema.ISDELETED, QueryBuilder.literal(true)) // Imposta il campo 'read' al valore fornito
.whereColumn(Schema.FROM_ID).isEqualTo(QueryBuilder.literal(sentMessage.getUser_id())) // Filtro per
// 'from_id'
.whereColumn(Schema.CREATION_TIME).isEqualTo(QueryBuilder.literal(sentMessage.getCreation_time().toInstant()))
.whereColumn(Schema.CREATION_TIME)
.isEqualTo(QueryBuilder.literal(sentMessage.getCreation_time().toInstant()))
.whereColumn(Schema.MESSAGE_ID).isEqualTo(QueryBuilder.literal(sentMessage.getUUID())); // Filtro
// per
// 'message_id'
@ -5014,8 +5029,20 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
return receivedMessage;
}
protected boolean saveSentMessage(Message message, CqlSession session) {
public boolean saveSentMessage(Message message, CqlSession session) {
// MessageSent messageSent = new MessageSent(message, false, false, false);
if (session == null)
session = conn.getKeyspaceSession();
boolean isread = false;
boolean isopened = false;
boolean isdeleted = false;
if (message instanceof MessageSent) {
MessageSent sent = (MessageSent) message;
isread = sent.isRead();
isopened = sent.isOpened();
isdeleted = sent.isDeleted();
}
RegularInsert insert = QueryBuilder.insertInto(Schema.TABLE_MESSAGES_SENT)
.value(Schema.MESSAGE_ID, QueryBuilder.literal(UUID.fromString(message.getId())))
@ -5025,20 +5052,43 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
.value(Schema.SUBJECT, QueryBuilder.literal(message.getSubject()))
.value(Schema.BODY, QueryBuilder.literal(message.getBody()))
.value(Schema.CREATION_TIME, QueryBuilder.literal(message.getCreation_time().toInstant()))
.value(Schema.ISREAD, QueryBuilder.literal(false))
.value(Schema.ISOPENED, QueryBuilder.literal(false))
.value(Schema.ISDELETED, QueryBuilder.literal(false));
.value(Schema.ISREAD, QueryBuilder.literal(isread))
.value(Schema.ISOPENED, QueryBuilder.literal(isopened))
.value(Schema.ISDELETED, QueryBuilder.literal(isdeleted));
session.execute(insert.build());
try {
_log.info("insert query {}", insert);
SimpleStatement builded = insert.build();
_log.info("builded query {}", builded);
session.execute(builded);
_log.debug("Wrote sent message with id " + message.getId());
} catch (Exception e) {
e.printStackTrace();
return false;
}
_log.debug("Wrote sent message with id " + message.getId());
return true;
}
protected boolean saveReceivedMessage(Message message, String recipientId, CqlSession session) {
public boolean saveReceivedMessage(Message message, String recipientId, CqlSession session) {
// MessageReceived messageReceived = new MessageReceived(message, recipientId,
// false, false, false);
if (session == null)
session = conn.getKeyspaceSession();
boolean isread = false;
boolean isopened = false;
boolean isdeleted = false;
if (message instanceof MessageReceived) {
MessageReceived received = (MessageReceived) message;
isread = received.isRead();
isopened = received.isOpened();
isdeleted = received.isDeleted();
}
RegularInsert insert = QueryBuilder.insertInto(Schema.TABLE_MESSAGES_RECEIVED)
.value(Schema.MESSAGE_ID, QueryBuilder.literal(UUID.fromString(message.getId())))
@ -5049,18 +5099,28 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
.value(Schema.SUBJECT, QueryBuilder.literal(message.getSubject()))
.value(Schema.BODY, QueryBuilder.literal(message.getBody()))
.value(Schema.CREATION_TIME, QueryBuilder.literal(message.getCreation_time().toInstant()))
.value(Schema.ISREAD, QueryBuilder.literal(false))
.value(Schema.ISOPENED, QueryBuilder.literal(false))
.value(Schema.ISDELETED, QueryBuilder.literal(false));
.value(Schema.ISREAD, QueryBuilder.literal(isread))
.value(Schema.ISOPENED, QueryBuilder.literal(isopened))
.value(Schema.ISDELETED, QueryBuilder.literal(isdeleted));
session.execute(insert.build());
try {
_log.info("insert query {}", insert);
SimpleStatement builded = insert.build();
_log.info("builded query {}", builded);
session.execute(builded);
_log.debug("Wrote sent message with id " + message.getId());
} catch (Exception e) {
e.printStackTrace();
throw e;
// return false;
}
return true;
}
protected boolean saveAttachmentMessageEntry(String messageKey, Attachment toSave, CqlSession session) {
public boolean saveAttachmentMessageEntry(String message_id, Attachment toSave, CqlSession session) {
// Inserting data
if (session == null)
session = conn.getKeyspaceSession();
@ -5068,7 +5128,7 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
try {
RegularInsert insert = QueryBuilder.insertInto(Schema.TABLE_MESSAGES_ATTACHMENTS)
.value(Schema.MESSAGE_ID, QueryBuilder.literal(UUID.fromString(messageKey)))
.value(Schema.MESSAGE_ID, QueryBuilder.literal(UUID.fromString(message_id)))
.value(Schema.ATTACH_ID, QueryBuilder.literal(UUID.fromString(toSave.getId())))
.value(Schema.URI, QueryBuilder.literal(toSave.getUri()))
.value(Schema.NAME, QueryBuilder.literal(toSave.getName()))
@ -5078,7 +5138,7 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
session.execute(insert.build());
_log.debug("Wrote attachment " + toSave.getName() + " for message with id " + messageKey);
_log.debug("Wrote attachment " + toSave.getName() + " for message with id " + message_id);
} catch (Exception e) {
@ -5155,7 +5215,12 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
@Override
public Message sendMessage(String fromId, List<String> addresses, String subject, String body,
List<Attachment> attachments) throws IllegalArgumentException {
String messageId = UUID.randomUUID().toString(); // Generate UUID for the message
return sendMessage(fromId, addresses, subject, body, attachments, Message.generateUUID());
}
@Override
public Message sendMessage(String fromId, List<String> addresses, String subject, String body,
List<Attachment> attachments, String messageId) throws IllegalArgumentException {
boolean has_attachments = attachments != null && !attachments.isEmpty();
Message message = new Message(messageId, fromId, null, addresses, subject, body, new Date(), has_attachments);
@ -5219,7 +5284,8 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
Update update = QueryBuilder.update(Schema.TABLE_MESSAGES_SENT)
.setColumn(Schema.ISREAD, QueryBuilder.literal(set_read)) // Imposta il campo 'read' al valore fornito
.whereColumn(Schema.FROM_ID).isEqualTo(QueryBuilder.literal(fromId)) // Filtro per 'from_id'
.whereColumn(Schema.CREATION_TIME).isEqualTo(QueryBuilder.literal(sentMessage.getCreation_time().toInstant()))
.whereColumn(Schema.CREATION_TIME)
.isEqualTo(QueryBuilder.literal(sentMessage.getCreation_time().toInstant()))
.whereColumn(Schema.MESSAGE_ID).isEqualTo(QueryBuilder.literal(UUID.fromString(messageId))); // Filtro
// per
// 'message_id'
@ -5258,7 +5324,7 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
}
@Override
public Attachment getMessageAttachmentById(String messageid, String attachid) {
public Attachment getMessageAttachmentByIdFilename(String messageid, String attachid) {
if (attachid == null) {
throw new NullArgumentException("the timeInMillis must be before today");

View File

@ -682,6 +682,7 @@ public interface DatabookStore {
void closeConnection();
public Message sendMessage(String fromId, List<String> addresses, String subject, String body, List<Attachment> attachments);
public Message sendMessage(String fromId, List<String> addresses, String subject, String body, List<Attachment> attachments, String messageId);
public List<MessageSent> getSentMessagesBySender(String fromId, Date timestamp, Integer limit);
public List<MessageReceived> getReceivedMessagesByRecipient(String recipientId, Date timestamp, Integer limit);
@ -697,7 +698,8 @@ public interface DatabookStore {
public MessageSent setSentMessageRead(String fromId, String messageId, boolean set_read);
public MessageReceived setReceivedMessageRead(String recipientId, String messageId, boolean set_read);
public Attachment getMessageAttachmentById(String messageId, String filename);
public Attachment getMessageAttachmentByIdFilename(String messageId, String filename);
public List<Attachment> getMessageAttachmentsById(String messageId);
public List<MessageSent> getSentMessagesBySender(String fromId, String messageId, Date timestamp, Integer limit,

View File

@ -90,6 +90,7 @@ public class MessagesTest extends BaseDbTest{
@Test
public void testMessagesReceivedByRecipient() throws Exception {
Message message = store.sendMessage(testFrom, recipients, "testMessagesReceivedByRecipient", body, null);
assertNotNull(message);
List<MessageReceived> receiveds = store.getReceivedMessagesByRecipient(recipient, null, null);
MessageReceived received = receiveds.get(0);
@ -99,6 +100,7 @@ public class MessagesTest extends BaseDbTest{
@Test
public void testUnreadMessagesReceivedByRecipient() throws Exception {
Message message = store.sendMessage(testFrom, recipients, "testUnreadMessagesReceivedByRecipient", body, null);
assertNotNull(message);
List<MessageReceived> receiveds = store.getReceivedMessagesByRecipient(recipient, null, null, 1, false, false,
null);
@ -386,10 +388,16 @@ public class MessagesTest extends BaseDbTest{
public void testSentMessagesOrderedByTimestampDescending() throws Exception {
// Invia tre messaggi con timestamp differenti
Message message1 = store.sendMessage(testFrom, recipients, subject + "1", body, null);
assertNotNull(message1);
Thread.sleep(1000); // Attendi 1 secondo per garantire un timestamp diverso
Message message2 = store.sendMessage(testFrom, recipients, subject + "2", body, null);
assertNotNull(message2);
Thread.sleep(1000); // Attendi un altro secondo
Message message3 = store.sendMessage(testFrom, recipients, subject + "3", body, null);
assertNotNull(message3);
// Recupera tutti i messaggi inviati dall'utente
List<MessageSent> sentMessages = store.getSentMessagesBySender(testFrom, null, 10);
@ -413,10 +421,16 @@ public class MessagesTest extends BaseDbTest{
public void testReceivedMessagesOrderedByTimestampDescending() throws Exception {
// Invia tre messaggi con timestamp differenti
Message message1 = store.sendMessage(testFrom, recipients, subject + "1", body, null);
assertNotNull(message1);
Thread.sleep(1000); // Attendi 1 secondo per garantire un timestamp diverso
Message message2 = store.sendMessage(testFrom, recipients, subject + "2", body, null);
assertNotNull(message2);
Thread.sleep(1000); // Attendi un altro secondo
Message message3 = store.sendMessage(testFrom, recipients, subject + "3", body, null);
assertNotNull(message3);
// Recupera tutti i messaggi ricevuti dal destinatario
List<MessageReceived> receivedMessages = store.getReceivedMessagesByRecipient(recipient, null, 10);
@ -439,6 +453,8 @@ public class MessagesTest extends BaseDbTest{
public void testUnreadMessagesOnlyReturned() throws Exception {
// Invia un messaggio normale
Message message1 = store.sendMessage(testFrom, recipients, subject + "1", body, null);
assertNotNull(message1);
// Invia un altro messaggio che verrà marcato come letto
Message message2 = store.sendMessage(testFrom, recipients, subject + "2", body, null);
@ -457,10 +473,16 @@ public class MessagesTest extends BaseDbTest{
public void testMessagesWithLimitAndOrder() throws Exception {
// Invia tre messaggi
Message message1 = store.sendMessage(testFrom, recipients, subject + "1", body, null);
assertNotNull(message1);
Thread.sleep(1000); // Attendi 1 secondo
Message message2 = store.sendMessage(testFrom, recipients, subject + "2", body, null);
assertNotNull(message2);
Thread.sleep(1000);
Message message3 = store.sendMessage(testFrom, recipients, subject + "3", body, null);
assertNotNull(message3);
// Recupera i primi due messaggi inviati, ordinati in modo decrescente per
// timestamp
@ -620,7 +642,7 @@ public class MessagesTest extends BaseDbTest{
assertNotNull("cannot obtain attach ", attach_id + "by msg id " + msg_id);
checkEqualAttachments(attach, by_message_id);
Attachment by_attach_id = store.getMessageAttachmentById(msg_id, attach_id);
Attachment by_attach_id = store.getMessageAttachmentByIdFilename(msg_id, attach_id);
assertNotNull("cannot obtain attach by id", attach_id);
checkEqualAttachments(attach, by_attach_id);
}