Esempio n. 1
0
        public virtual void SendMessage(Message message, IList<RoleInfo> roles, IList<UserInfo> users, IList<int> fileIDs, UserInfo sender)
        {
            if (sender == null || sender.UserID <= 0)
            {
                throw new ArgumentException(Localization.Localization.GetString("MsgSenderRequiredError", Localization.Localization.ExceptionsResourceFile));
            }

            if (message == null)
            {
                throw new ArgumentException(Localization.Localization.GetString("MsgMessageRequiredError", Localization.Localization.ExceptionsResourceFile));
            }

            if (string.IsNullOrEmpty(message.Subject) && string.IsNullOrEmpty(message.Body))
            {
                throw new ArgumentException(Localization.Localization.GetString("MsgSubjectOrBodyRequiredError", Localization.Localization.ExceptionsResourceFile));
            }

            if (roles == null && users == null)
            {
                throw new ArgumentException(Localization.Localization.GetString("MsgRolesOrUsersRequiredError", Localization.Localization.ExceptionsResourceFile));
            }

            if (!string.IsNullOrEmpty(message.Subject) && message.Subject.Length > ConstMaxSubject)
            {
                throw new ArgumentException(string.Format(Localization.Localization.GetString("MsgSubjectTooBigError", Localization.Localization.ExceptionsResourceFile), ConstMaxSubject, message.Subject.Length));
            }

            if (roles != null && roles.Count > 0 && !IsAdminOrHost(sender))
            {
                if (roles.Select(role => sender.Social.Roles.Any(userRoleInfo => role.RoleID == userRoleInfo.RoleID && userRoleInfo.IsOwner)).Any(owner => !owner))
                {
                    throw new ArgumentException(Localization.Localization.GetString("MsgOnlyHostOrAdminOrGroupOwnerCanSendToRoleError", Localization.Localization.ExceptionsResourceFile));
                }
            }

            var sbTo = new StringBuilder();
            var replyAllAllowed = true;
            if (roles != null)
            {
                foreach (var role in roles.Where(role => !string.IsNullOrEmpty(role.RoleName)))
                {
                    sbTo.Append(role.RoleName + ",");
                    replyAllAllowed = false;
                }
            }

            if (users != null)
            {
                foreach (var user in users.Where(user => !string.IsNullOrEmpty(user.DisplayName))) sbTo.Append(user.DisplayName + ",");
            }

            if (sbTo.Length == 0)
            {
                throw new ArgumentException(Localization.Localization.GetString("MsgEmptyToListFoundError", Localization.Localization.ExceptionsResourceFile));
            }

            if (sbTo.Length > ConstMaxTo)
            {
                throw new ArgumentException(string.Format(Localization.Localization.GetString("MsgToListTooBigError", Localization.Localization.ExceptionsResourceFile), ConstMaxTo, sbTo.Length));
            }

            //Cannot send message if within ThrottlingInterval
            var waitTime = InternalMessagingController.Instance.WaitTimeForNextMessage(sender);
            if (waitTime > 0)
            {
                var interval = GetPortalSettingAsInteger("MessagingThrottlingInterval", sender.PortalID, Null.NullInteger);
                throw new ThrottlingIntervalNotMetException(string.Format(Localization.Localization.GetString("MsgThrottlingIntervalNotMet", Localization.Localization.ExceptionsResourceFile), interval));
            }

            //Cannot have attachments if it's not enabled
            if (fileIDs != null && !InternalMessagingController.Instance.AttachmentsAllowed(sender.PortalID))
            {
                throw new AttachmentsNotAllowed(Localization.Localization.GetString("MsgAttachmentsNotAllowed", Localization.Localization.ExceptionsResourceFile));
            }

            //Cannot exceed RecipientLimit
            var recipientCount = 0;
            if (users != null) recipientCount += users.Count;
            if (roles != null) recipientCount += roles.Count;
            if (recipientCount > InternalMessagingController.Instance.RecipientLimit(sender.PortalID))
            {
                throw new RecipientLimitExceededException(Localization.Localization.GetString("MsgRecipientLimitExceeded", Localization.Localization.ExceptionsResourceFile));
            }

            //Profanity Filter
            var profanityFilterSetting = GetPortalSetting("MessagingProfanityFilters", sender.PortalID, "NO");
            if (profanityFilterSetting.Equals("YES", StringComparison.InvariantCultureIgnoreCase))
            {
                message.Subject = InputFilter(message.Subject);
                message.Body = InputFilter(message.Body);
            }

            message.To = sbTo.ToString().Trim(',');
            message.MessageID = Null.NullInteger;
            message.ReplyAllAllowed = replyAllAllowed;
            message.SenderUserID = sender.UserID;
            message.From = sender.DisplayName;

            message.MessageID = _dataService.SaveMessage(message, PortalController.GetEffectivePortalId(UserController.GetCurrentUserInfo().PortalID), UserController.GetCurrentUserInfo().UserID);

            //associate attachments
            if (fileIDs != null)
            {
                foreach (var attachment in fileIDs.Select(fileId => new MessageAttachment { MessageAttachmentID = Null.NullInteger, FileID = fileId, MessageID = message.MessageID }))
                {
                    _dataService.SaveMessageAttachment(attachment, UserController.GetCurrentUserInfo().UserID);
                }
            }

            //send message to Roles
            if (roles != null)
            {
                var roleIds = string.Empty;
                roleIds = roles
                    .Select(r => r.RoleID)
                    .Aggregate(roleIds, (current, roleId) => current + (roleId + ","))
                    .Trim(',');

                _dataService.CreateMessageRecipientsForRole(message.MessageID, roleIds, UserController.GetCurrentUserInfo().UserID);
            }

            //send message to each User - this should be called after CreateMessageRecipientsForRole.
            if (users == null)
            {
                users = new List<UserInfo>();
            }

            foreach (var recipient in from user in users where InternalMessagingController.Instance.GetMessageRecipient(message.MessageID, user.UserID) == null select new MessageRecipient { MessageID = message.MessageID, UserID = user.UserID, Read = false, RecipientID = Null.NullInteger })
            {
                _dataService.SaveMessageRecipient(recipient, UserController.GetCurrentUserInfo().UserID);
            }

            if (users.All(u => u.UserID != sender.UserID))
            {
                //add sender as a recipient of the message
                var recipientId = _dataService.SaveMessageRecipient(new MessageRecipient { MessageID = message.MessageID, UserID = sender.UserID, Read = false, RecipientID = Null.NullInteger }, UserController.GetCurrentUserInfo().UserID);
                InternalMessagingController.Instance.MarkMessageAsDispatched(message.MessageID, recipientId);
            }

            // Mark the conversation as read by the sender of the message.
            InternalMessagingController.Instance.MarkRead(message.MessageID, sender.UserID);
        }
Esempio n. 2
0
 public virtual void SendMessage(Message message, IList<RoleInfo> roles, IList<UserInfo> users, IList<int> fileIDs)
 {
     SendMessage(message, roles, users, fileIDs, UserController.GetCurrentUserInfo());
 }
        public HttpResponseMessage Create(CreateDTO postData)
        {
            try
            {
                var portalId = PortalController.GetEffectivePortalId(PortalSettings.PortalId);
                var roleIdsList = string.IsNullOrEmpty(postData.RoleIds) ? null : postData.RoleIds.FromJson<IList<int>>();
                var userIdsList = string.IsNullOrEmpty(postData.UserIds) ? null : postData.UserIds.FromJson<IList<int>>();
                var fileIdsList = string.IsNullOrEmpty(postData.FileIds) ? null : postData.FileIds.FromJson<IList<int>>();

                var roles = roleIdsList != null && roleIdsList.Count > 0
                    ? roleIdsList.Select(id => TestableRoleController.Instance.GetRole(portalId, r => r.RoleID == id)).Where(role => role != null).ToList()
                    : null;

                List<UserInfo> users = null;
                if (userIdsList != null)
                {
                    var userController = new UserController();
                    users = userIdsList.Select(id => userController.GetUser(portalId, id)).Where(user => user != null).ToList();
                }

                var message = new Message { Subject = HttpUtility.UrlDecode(postData.Subject), Body = HttpUtility.UrlDecode(postData.Body) };
                MessagingController.Instance.SendMessage(message, roles, users, fileIdsList);
                return Request.CreateResponse(HttpStatusCode.OK, new { Result = "success", Value = message.MessageID });
            }
            catch (Exception exc)
            {
                Logger.Error(exc);
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exc);
            }
        }
		private dynamic ToExpandoObject(Message message)
		{
			dynamic messageObj = new ExpandoObject();
			messageObj.PortalID = message.PortalID;
			messageObj.KeyID = message.KeyID;
			messageObj.MessageID = message.MessageID;
			messageObj.ConversationId = message.ConversationId;
			messageObj.SenderUserID = message.SenderUserID;
			messageObj.From = message.From;
			messageObj.To = message.To;
			messageObj.Subject = message.Subject;
			messageObj.Body = message.Body;
			messageObj.DisplayDate = message.DisplayDate;
			messageObj.ReplyAllAllowed = message.ReplyAllAllowed;
			//base entity properties
			messageObj.CreatedByUserID = message.CreatedByUserID;
			messageObj.CreatedOnDate = message.CreatedOnDate;
			messageObj.LastModifiedByUserID = message.LastModifiedByUserID;
			messageObj.LastModifiedOnDate = message.LastModifiedOnDate;
			
			return messageObj;
		}
Esempio n. 5
0
        protected void Page_Load(object sender, EventArgs e)
        {
            SmtpClient Cliente = new SmtpClient();

            Cliente.DeliveryMethod = SmtpDeliveryMethod.Network;
            Cliente.EnableSsl      = true;
            Cliente.Host           = DotNetNuke.Common.Utilities.Config.GetSetting("Mail_Host");
            Cliente.Port           = int.Parse(DotNetNuke.Common.Utilities.Config.GetSetting("Mail_Port"));
            string mail     = DotNetNuke.Common.Utilities.Config.GetSetting("Mail_From");
            string password = DotNetNuke.Common.Utilities.Config.GetSetting("Mail_Password");

            System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(mail, password);
            Cliente.Credentials = credentials;



            List <Data2.Class.Struct_AlertaProducto> P = Data2.Class.Struct_AlertaProducto.GetAllProducts();

            if (P != null && P.Count > 0)
            {
                foreach (Struct_AlertaProducto p in P)
                {
                    int currentstock = p.getProducto().CantidadINT;
                    if (p.MinCant >= currentstock)
                    {
                        MailMessage MM = new MailMessage();
                        MM.IsBodyHtml = true;
                        MM.Body      += "<div>Stock Critico en el producto:" + p.getProducto().Descripcion + "</div>";
                        MM.Body      += "Cantidad Actual:" + p.getProducto().CantidadINT;
                        MM.From       = new MailAddress(mail, "Bryz Spa");
                        MM.Subject    = "Stock Critico";

                        DotNetNuke.Services.Social.Messaging.Message Message = new DotNetNuke.Services.Social.Messaging.Message();
                        Message.Body    += "Stock Critico en el producto:" + p.getProducto().Descripcion;
                        Message.Body    += "Cantidad Actual:" + p.getProducto().CantidadINT;
                        Message.Subject += "Stock Critico";

                        List <UserInfo> ListaUsuarios = new List <UserInfo>();

                        List <Data2.Class.Struct_AlertaUsuario> U = Struct_AlertaUsuario.GetUsuarios();
                        if (U != null && U.Count > 0)
                        {
                            foreach (Struct_AlertaUsuario u in U)
                            {
                                string email = u.email;

                                Cliente.UseDefaultCredentials = false;
                                Cliente.Credentials           = credentials;
                                MailAddress MA = new MailAddress(email, "Bryz SPA");
                                MM.To.Add(MA);
                                ListaUsuarios.Add(UserController.GetUserById(0, u.Id));
                            }
                        }
                        if (MM.To != null && MM.To.Count > 0)
                        {
                            MessagingController.Instance.SendMessage(Message, null, ListaUsuarios, null, this.UserInfo);

                            Cliente.Send(MM);
                        }
                    }
                }
            }


            if (this.EditMode == true)
            {
            }



            try
            {
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }