Exemple #1
0
        public static async Task AddMediaToAlbumAsync(
            ApplicationDbContext dbContext,
            MediaAlbum albumEntity,
            UserMedia mediaEntity,
            AddImageToAlbum createRequest)
        {
            albumEntity.UserMedias.Add(mediaEntity);
            mediaEntity.MediaAlbum = albumEntity;
            if (mediaEntity.State == UserMediaState.UploadedButUnused)
            {
                mediaEntity.State = UserMediaState.InUse;
            }

            mediaEntity.Title = createRequest.Title;
            if (!string.IsNullOrWhiteSpace(createRequest.Description))
            {
                mediaEntity.Description = await TextOperations.CreateTextAsync(
                    dbContext, createRequest.Description);
            }

            await dbContext.SaveChangesAsync();

            await SearchOperations.IndexMediaAsync(new[] { mediaEntity });

            if (mediaEntity.Description != null)
            {
                await UserOperations.NotifyMentionsAsync(
                    dbContext, "Album Entry", mediaEntity.UserId, mediaEntity.Description);
            }
        }
Exemple #2
0
        public static async Task <HttpStatusCode> ChangeAlbumAsync(
            ApplicationDbContext dbContext,
            UserInfo user,
            int albumId,
            AlbumDefinition modifyRequest)
        {
            MediaAlbum entity = await dbContext.MediaAlbums.SingleOrDefaultAsync(
                a => a.MediaAlbumId == albumId);

            if (entity == null)
            {
                return(HttpStatusCode.NotFound);
            }

            if (entity.UserId != user.UserInfoId)
            {
                return(HttpStatusCode.Forbidden);
            }

            entity.Title       = modifyRequest.Title;
            entity.Description = modifyRequest.Description;

            await dbContext.SaveChangesAsync();

            await SearchOperations.IndexAlbumAsync(entity);

            return(HttpStatusCode.OK);
        }
Exemple #3
0
        public static async Task <AlbumSummary> CreateAlbumAsync(
            ApplicationDbContext dbContext,
            UserInfo user,
            AlbumDefinition createRequest)
        {
            var entity = new MediaAlbum
            {
                User        = user,
                Title       = createRequest.Title,
                Description = createRequest.Description
            };

            dbContext.MediaAlbums.Add(entity);
            await dbContext.SaveChangesAsync();

            await SearchOperations.IndexAlbumAsync(entity);

            return(new AlbumSummary
            {
                Id = entity.MediaAlbumId,
                UserId = user.UserInfoId,
                Title = entity.Title,
                Description = entity.Description,
                SampleMediaUrls = new string[0]
            });
        }
Exemple #4
0
        public static async Task <HttpStatusCode> AddCommentAsync <T>(
            ApplicationDbContext dbContext,
            string userId,
            CommentItemIds ids,
            Expression <Func <T, CommentThread> > commentThreadProperty,
            T commentTargetEntity,
            CommentRequest comment)
        {
            using (var transaction = dbContext.Database.BeginTransaction())
            {
                bool succeeded = false;
                try
                {
                    var text = await TextOperations.CreateTextAsync(
                        dbContext, comment.Message);

                    dbContext.UserTexts.Add(text);

                    var           commentThreadPropInfo = (PropertyInfo)((MemberExpression)commentThreadProperty.Body).Member;
                    CommentThread commentThread         = commentThreadPropInfo.GetValue(commentTargetEntity) as CommentThread;
                    if (commentThread == null)
                    {
                        commentThread = new CommentThread
                        {
                            Comments = new List <Comment>()
                        };
                        dbContext.CommentThreads.Add(commentThread);
                        commentThreadPropInfo.SetValue(commentTargetEntity, commentThread);
                    }

                    var commentEntity = new Comment
                    {
                        Text   = text,
                        Thread = commentThread,
                        UserId = userId
                    };
                    dbContext.Comments.Add(commentEntity);
                    commentThread.Comments.Add(commentEntity);

                    await dbContext.SaveChangesAsync();

                    transaction.Commit();
                    succeeded = true;

                    await UserOperations.NotifyMentionsAsync(
                        dbContext, "Comment", userId, text);

                    await SearchOperations.IndexCommentAsync(commentEntity, ids);

                    return(HttpStatusCode.OK);
                }
                finally
                {
                    if (!succeeded)
                    {
                        transaction.Rollback();
                    }
                }
            }
        }
        public static async Task <MediaUploadResults> StoreMediaAsync(
            ApplicationDbContext dbContext,
            UserInfo userEntity,
            IEnumerable <UploadedMedia> items)
        {
            var newEntities = new List <UserMedia>();

            foreach (UploadedMedia item in items)
            {
                if (new FileInfo(item.LocalFileName).Length > 0)
                {
                    string ext    = Path.GetExtension(item.OriginalName.Trim('\"')).Substring(1);
                    var    entity = new UserMedia
                    {
                        User       = userEntity,
                        State      = UserMediaState.UploadedButUnused,
                        CreatedUtc = DateTime.UtcNow,
                        Extension  = ext
                    };
                    var dataEntity = new UserMediaData
                    {
                        UserMedia = entity,
                        ImageData = File.ReadAllBytes(item.LocalFileName)
                    };
                    File.Delete(item.LocalFileName);
                    dbContext.UserMedias.Add(entity);
                    dbContext.UserMediaDatas.Add(dataEntity);

                    newEntities.Add(entity);
                }
            }

            await dbContext.SaveChangesAsync();

            await SearchOperations.IndexMediaAsync(newEntities);

            return(new MediaUploadResults
            {
                Files = newEntities
                        .Select(
                    e => new MediaUploadResult
                {
                    Id = e.UserMediaId
                })
                        .ToList()
            });
        }
Exemple #6
0
        public static async Task <HttpStatusCode> AddTimelineEntryAsync(
            CreateTimelineEntry createMessage,
            ApplicationDbContext dbContext,
            UserInfo userEntity)
        {
            if (string.IsNullOrWhiteSpace(createMessage?.Message))
            {
                return(HttpStatusCode.BadRequest);
            }

            var text = await TextOperations.CreateTextAsync(dbContext, createMessage.Message);

            var timelineEntity = new TimelineEntry
            {
                UserId  = userEntity.UserInfoId,
                Message = text
            };

            if (createMessage.MediaIds != null && createMessage.MediaIds.Count > 0)
            {
                MediaAlbum timelineAlbum = await EnsureTimelinePhotoAlbumExistsAsync(dbContext, userEntity);

                timelineEntity.Media = new List <TimelineEntryMedia>();
                int sequence      = 0;
                var includedMedia = new List <UserMedia>();
                foreach (int id in createMessage.MediaIds)
                {
                    UserMedia mediaEntity = await dbContext.UserMedias
                                            .SingleAsync(um => um.UserMediaId == id);

                    if (mediaEntity.UserId != userEntity.UserInfoId)
                    {
                        // Only allowed to post your own images here
                        return(HttpStatusCode.BadRequest);
                    }
                    includedMedia.Add(mediaEntity);
                    mediaEntity.MediaAlbum = timelineAlbum;
                    var mediaEntry =
                        new TimelineEntryMedia
                    {
                        Media         = mediaEntity,
                        Sequence      = sequence++,
                        TimelineEntry = timelineEntity
                    };
                    dbContext.TimelineEntryMedia.Add(mediaEntry);
                    timelineEntity.Media.Add(mediaEntry);
                }
                foreach (UserMedia media in includedMedia)
                {
                    if (media.State == UserMediaState.UploadedButUnused)
                    {
                        media.State = UserMediaState.InUse;
                    }
                }
            }

            dbContext.UserTexts.Add(text);
            dbContext.TimelineEntries.Add(timelineEntity);
            await dbContext.SaveChangesAsync();

            await UserOperations.NotifyMentionsAsync(
                dbContext, "Timeline Entry", userEntity.UserInfoId, text);

            await SearchOperations.IndexTimelineMessageAsync(timelineEntity);

            return(HttpStatusCode.OK);
        }