Beispiel #1
0
        public async Task <SongEdit> EditSong([FromBody] Song song)
        {
            var user = await GetUserOrThrow();

            if (song.Id == null)
            {
                throw new ArgumentException("Song must have an ID");
            }

            var existingSong = await DbSession.LoadRequiredAsync <Song>(song.Id);

            var songEditId = GetSongEditId(existingSong.Id !, user.Id);
            var songEdit   = new SongEdit(existingSong, song)
            {
                Id     = songEditId,
                UserId = user.Id
            };

            if (songEdit.HasAnyChanges())
            {
                // Admins don't need approval; apply the change straight away.
                if (user.IsAdmin())
                {
                    songEdit.Apply(existingSong);
                }
                else // the user isn't an admin.
                {
                    // Store the song edit and await admin approval.
                    await DbSession.StoreAsync(songEdit);

                    DbSession.SetRavenExpiration(songEdit, DateTime.UtcNow.AddDays(30 * 6));

                    // Notify admins that a new song edit needs approval.
                    var admins = await DbSession.Query <AppUser>()
                                 .Where(u => u.Roles.Contains(AppUser.AdminRole))
                                 .ToListAsync();

                    var editNeedsApprovalNotification = Notification.SongEditsNeedApproval(appOptions.PushNotificationsImageUrl);
                    foreach (var admin in admins)
                    {
                        // If the admin already has a "song edits need approval" notification item, replace it with the unread one.
                        var existing = admin.Notifications.FirstOrDefault(n => n.Title == editNeedsApprovalNotification.Title);
                        if (existing != null)
                        {
                            admin.Notifications.Remove(existing);
                        }

                        admin.AddNotification(editNeedsApprovalNotification);
                    }
                }
            }

            return(songEdit);
        }
Beispiel #2
0
        public async Task <SongEdit> Approve([FromBody] SongEdit songEdit)
        {
            var song = await DbSession.LoadAsync <Song>(songEdit.SongId);

            if (song != null)
            {
                songEdit.Apply(song);
                songEdit.Status = SongEditStatus.Approved;
                await DbSession.StoreAsync(songEdit);

                logger.LogInformation("Applied song edit", songEdit);

                // Notify the user.
                var user = await DbSession.LoadAsync <AppUser>(songEdit.UserId);

                user?.AddNotification(Notification.SongEditApproved(song));
            }

            return(songEdit);
        }