Esempio n. 1
0
        private void SendEmailVerificationRequest(IRepositoryContext <User> ctx, User user, string resetUrl, string subject)
        {
            var request = new PasswordResetRequest(user);

            ctx.Save(request);

            var body = string.Format("Please click the link below to verify your email at VocaDB.\n{0}?token={1}", resetUrl, request.Id);

            mailer.SendEmail(request.User.Email, request.User.Name, subject, body);
        }
Esempio n. 2
0
        private void SendPrivateMessageNotification(string mySettingsUrl, string messagesUrl, UserMessage message)
        {
            ParamIs.NotNull(() => message);

            var subject = string.Format("New private message from {0}", message.Sender.Name);
            var body    = string.Format(
                "You have received a message from {0}. " +
                "You can view your messages at {1}." +
                "\n\n" +
                "If you do not wish to receive more email notifications such as this, you can adjust your settings at {2}.",
                message.Sender.Name, messagesUrl, mySettingsUrl);

            userMessageMailer.SendEmail(message.Receiver.Email, message.Receiver.Name, subject, body);
        }
        /// <summary>
        /// Sends notifications
        /// </summary>
        /// <param name="ctx">Repository context. Cannot be null.</param>
        /// <param name="entry">Entry that was created. Cannot be null.</param>
        /// <param name="artists">List of artists for the entry. Cannot be null.</param>
        /// <param name="creator">User who created the entry. The creator will be excluded from all notifications. Cannot be null.</param>
        public User[] SendNotifications(IDatabaseContext ctx, IEntryWithNames entry, IEnumerable <Artist> artists, IUser creator)
        {
            ParamIs.NotNull(() => ctx);
            ParamIs.NotNull(() => entry);
            ParamIs.NotNull(() => artists);
            ParamIs.NotNull(() => creator);
            ParamIs.NotNull(() => entryLinkFactory);
            ParamIs.NotNull(() => mailer);

            var coll      = artists.ToArray();
            var artistIds = coll.Select(a => a.Id).ToArray();

            log.Info("Sending notifications for {0} artists", artistIds.Length);

            // Get users with less than maximum number of unread messages, following any of the artists
            var usersWithArtists = ctx.OfType <ArtistForUser>()
                                   .Query()
                                   .Where(afu =>
                                          artistIds.Contains(afu.Artist.Id) &&
                                          afu.User.Id != creator.Id && afu.User.Active &&
                                          afu.SiteNotifications &&
                                          afu.User.ReceivedMessages.Count(m => m.Inbox == UserInboxType.Notifications && !m.Read) < afu.User.Options.UnreadNotificationsToKeep)
                                   .Select(afu => new {
                UserId   = afu.User.Id,
                ArtistId = afu.Artist.Id
            })
                                   .ToArray()
                                   .GroupBy(afu => afu.UserId)
                                   .ToDictionary(afu => afu.Key, afu => afu.Select(a => a.ArtistId));

            var userIds = usersWithArtists.Keys;

            log.Debug("Found {0} users subscribed to artists", userIds.Count);

            if (!userIds.Any())
            {
                return(new User[0]);
            }

            var entryTypeNames = enumTranslations.Translations <EntryType>();
            var users          = ctx.OfType <User>().Query().Where(u => userIds.Contains(u.Id)).ToArray();

            foreach (var user in users)
            {
                var artistIdsForUser = new HashSet <int>(usersWithArtists[user.Id]);
                var followedArtists  = coll.Where(a => artistIdsForUser.Contains(a.Id)).ToArray();

                if (followedArtists.Length == 0)
                {
                    continue;
                }

                string title;

                var culture       = CultureHelper.GetCultureOrDefault(user.LanguageOrLastLoginCulture);
                var entryTypeName = entryTypeNames.GetName(entry.EntryType, culture).ToLowerInvariant();
                var entrySubType  = entrySubTypeNameFactory.GetEntrySubTypeName(entry, enumTranslations, culture)?.ToLowerInvariant();

                if (!string.IsNullOrEmpty(entrySubType))
                {
                    entryTypeName += $" ({entrySubType})";
                }

                var msg = CreateMessageBody(followedArtists, user, entry, true, entryTypeName);

                if (followedArtists.Length == 1)
                {
                    var artistName = followedArtists.First().TranslatedName[user.DefaultLanguageSelection];
                    title = string.Format("New {0} by {1}", entryTypeName, artistName);
                }
                else
                {
                    title = string.Format("New {0}", entryTypeName);
                }

                var notification = new UserMessage(user, title, msg, false);
                user.Messages.Add(notification);
                ctx.Save(notification);

                if (user.EmailOptions != UserEmailOptions.NoEmail && !string.IsNullOrEmpty(user.Email) &&
                    followedArtists.Any(a => a.Users.Any(u => u.User.Equals(user) && u.EmailNotifications)))
                {
                    mailer.SendEmail(user.Email, user.Name, title, CreateMessageBody(followedArtists, user, entry, false, entryTypeName));
                }
            }

            return(users);
        }
		/// <summary>
		/// Sends notifications
		/// </summary>
		/// <param name="ctx">Repository context. Cannot be null.</param>
		/// <param name="entry">Entry that was created. Cannot be null.</param>
		/// <param name="artists">List of artists for the entry. Cannot be null.</param>
		/// <param name="creator">User who created the entry. The creator will be excluded from all notifications. Cannot be null.</param>
		/// <param name="entryLinkFactory">Factory for creating links to entries. Cannot be null.</param>
		/// <param name="mailer">Mailer for user email messages. Cannot be null.</param>
		public void SendNotifications(IRepositoryContext<UserMessage> ctx, IEntryWithNames entry, 
			IEnumerable<Artist> artists, IUser creator, IEntryLinkFactory entryLinkFactory,
			IUserMessageMailer mailer) {

			ParamIs.NotNull(() => ctx);
			ParamIs.NotNull(() => entry);
			ParamIs.NotNull(() => artists);
			ParamIs.NotNull(() => creator);
			ParamIs.NotNull(() => entryLinkFactory);
			ParamIs.NotNull(() => mailer);

			var coll = artists.ToArray();
			var artistIds = coll.Select(a => a.Id).ToArray();

			// Get users with 3 or less unread messages, following any of the artists
			var usersWithArtists = ctx.OfType<ArtistForUser>()
				.Query()
				.Where(afu => 
					artistIds.Contains(afu.Artist.Id) 
					&& afu.User.Id != creator.Id && afu.User.Active
					&& afu.SiteNotifications
					&& afu.User.ReceivedMessages.Count(m => !m.Read) <= 9)
				.Select(afu => new {
					UserId = afu.User.Id, 
					ArtistId = afu.Artist.Id
				})
				.ToArray()
				.GroupBy(afu => afu.UserId)
				.ToDictionary(afu => afu.Key, afu => afu.Select(a => a.ArtistId));

			var userIds = usersWithArtists.Keys;

			if (!userIds.Any())
				return;

			var users = ctx.OfType<User>().Query().Where(u => userIds.Contains(u.Id)).ToArray();

			foreach (var user in users) {

				var artistIdsForUser = new HashSet<int>(usersWithArtists[user.Id]);
				var followedArtists = coll.Where(a => artistIdsForUser.Contains(a.Id)).ToArray();

				if (followedArtists.Length == 0)
					continue;

				string title;

				var entryTypeName = entry.EntryType.ToString().ToLowerInvariant();
				var msg = CreateMessageBody(followedArtists, user, entry, entryLinkFactory, true);

				if (followedArtists.Length == 1) {

					var artistName = followedArtists.First().TranslatedName[user.DefaultLanguageSelection];
					title = string.Format("New {0} by {1}", entryTypeName, artistName);

				} else {

					title = string.Format("New {0}", entryTypeName);

				}

				var notification = new UserMessage(user, title, msg, false);
				ctx.Save(notification);

				if (user.EmailOptions != UserEmailOptions.NoEmail && !string.IsNullOrEmpty(user.Email) 
					&& followedArtists.Any(a => a.Users.Any(u => u.User.Equals(user) && u.EmailNotifications))) {
					
					mailer.SendEmail(user.Email, user.Name, title, CreateMessageBody(followedArtists, user, entry, entryLinkFactory, false));

				}

			}

		}
Esempio n. 5
0
        /// <summary>
        /// Sends notifications
        /// </summary>
        /// <param name="ctx">Repository context. Cannot be null.</param>
        /// <param name="entry">Entry that was created. Cannot be null.</param>
        /// <param name="artists">List of artists for the entry. Cannot be null.</param>
        /// <param name="creator">User who created the entry. The creator will be excluded from all notifications. Cannot be null.</param>
        /// <param name="entryLinkFactory">Factory for creating links to entries. Cannot be null.</param>
        /// <param name="mailer">Mailer for user email messages. Cannot be null.</param>
        public void SendNotifications(IDatabaseContext <UserMessage> ctx, IEntryWithNames entry,
                                      IEnumerable <Artist> artists, IUser creator, IEntryLinkFactory entryLinkFactory,
                                      IUserMessageMailer mailer)
        {
            ParamIs.NotNull(() => ctx);
            ParamIs.NotNull(() => entry);
            ParamIs.NotNull(() => artists);
            ParamIs.NotNull(() => creator);
            ParamIs.NotNull(() => entryLinkFactory);
            ParamIs.NotNull(() => mailer);

            var coll      = artists.ToArray();
            var artistIds = coll.Select(a => a.Id).ToArray();

            // Get users with 3 or less unread messages, following any of the artists
            var usersWithArtists = ctx.OfType <ArtistForUser>()
                                   .Query()
                                   .Where(afu =>
                                          artistIds.Contains(afu.Artist.Id) &&
                                          afu.User.Id != creator.Id && afu.User.Active &&
                                          afu.SiteNotifications &&
                                          afu.User.ReceivedMessages.Count(m => m.Inbox == UserInboxType.Notifications && !m.Read) < afu.User.Options.UnreadNotificationsToKeep)
                                   .Select(afu => new {
                UserId   = afu.User.Id,
                ArtistId = afu.Artist.Id
            })
                                   .ToArray()
                                   .GroupBy(afu => afu.UserId)
                                   .ToDictionary(afu => afu.Key, afu => afu.Select(a => a.ArtistId));

            var userIds = usersWithArtists.Keys;

            if (!userIds.Any())
            {
                return;
            }

            var users = ctx.OfType <User>().Query().Where(u => userIds.Contains(u.Id)).ToArray();

            foreach (var user in users)
            {
                var artistIdsForUser = new HashSet <int>(usersWithArtists[user.Id]);
                var followedArtists  = coll.Where(a => artistIdsForUser.Contains(a.Id)).ToArray();

                if (followedArtists.Length == 0)
                {
                    continue;
                }

                string title;

                var entryTypeName = entry.EntryType.ToString().ToLowerInvariant();
                var msg           = CreateMessageBody(followedArtists, user, entry, entryLinkFactory, true);

                if (followedArtists.Length == 1)
                {
                    var artistName = followedArtists.First().TranslatedName[user.DefaultLanguageSelection];
                    title = string.Format("New {0} by {1}", entryTypeName, artistName);
                }
                else
                {
                    title = string.Format("New {0}", entryTypeName);
                }

                var notification = new UserMessage(user, title, msg, false);
                user.Messages.Add(notification);
                ctx.Save(notification);

                if (user.EmailOptions != UserEmailOptions.NoEmail && !string.IsNullOrEmpty(user.Email) &&
                    followedArtists.Any(a => a.Users.Any(u => u.User.Equals(user) && u.EmailNotifications)))
                {
                    mailer.SendEmail(user.Email, user.Name, title, CreateMessageBody(followedArtists, user, entry, entryLinkFactory, false));
                }
            }
        }