I have a plugin that hooks into the event PublicApi.ConversationMessages.Events.BeforeCreate - I need to intercept the sender and recipient as I have some custom logic I need to run to determine if the two individuals can contact eachother.
I'm not certain how to get the recipient.
I am on Version 9.0
Participant list on the Conversation should include all the members who would receive the message (including the sender). The currently logged in user would be the one who sent the message.
Mathew D'Amico - I'm not seeing that available. Or do I need to take the Conversation ID and actually get the object from the API?
However, I would ideally like to call this OnBeforeCreate - because if I detect I have two people that should not be able to PM each other, I want to throw an exception and kill the conversation before it is saved.
Doesn't look like there is an event available to get to the BeforeCreate of a Conversation. So the best you can do is the BeforeCreate on the ConversationMessage. Load the conversation and then delete it from there based on your logic.
Ok. Will there be any notifications sent? Or if I delete the conversation before the ConversationMessage is created will that fix it?
I don't believe there would be any notification. The messages are what cause the notification, the conversation itself doesn't cause a notification.
Thanks Matthew...here is what I ended up doing:
public void DisableCrossGroupConversations(ConversationMessageBeforeCreateEventArgs e)
var conversation = PublicApi.Conversations.Get(e.ConversationId.Value);
foreach (var participant in conversation.Participants)
if (!Components.Permissions.CanInteract(e.Author.Id.Value, participant.Id.Value))
throw new NotSupportedException();
Mathew D'Amico - when I run this code which is triggered from ConversationMessages.Events.BeforeCreate, my conversation object is always null even though e.ConversationId.Value is a GUID
This line returns a null object:var conversation = PublicApi.Conversations.Get(e.ConversationId.Value);
For a new conversation, the conversation object and first message itself are added to the DB together, so this Message BeforeCreate event will unfortunately not work to retrieve conversation data from the DB.