Beispiel #1
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();
                    }
                }
            }
        }
Beispiel #2
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);
            }
        }
Beispiel #3
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);
        }