Ejemplo n.º 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);
            }
        }
Ejemplo n.º 2
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();
                    }
                }
            }
        }
Ejemplo n.º 3
0
        private static async Task <IList <TimelineEntryDetails> > EntriesQueryToDetailsAsync(
            IQueryable <TimelineEntry> q)
        {
            var results = await q
                          .Include(te => te.User.Avatar)
                          .Include(te => te.Message.Likes.Select(like => like.User))
                          .Include(te => te.CommentThread.Comments.Select(c => c.Text))
                          .Include(te => te.CommentThread.Comments.Select(c => c.User.Avatar))
                          .Select(
                te =>
                new
            {
                te.TimelineEntryId,
                User     = te.User,
                Message  = te.Message.Content,
                DateTime = te.Message.CreatedUtc,
                Media    = te.Media.Select(tme => tme.Media),
                Likes    = te.Message.Likes,
                te.CommentThread
            })
                          .ToListAsync();

            return(results.Select(
                       te =>
                       new TimelineEntryDetails
            {
                UserId = te.User.UserInfoId,
                UserName = te.User.Name,
                AvatarUrl = UserOperations.GetAvatarUrl(te.User),
                Message = te.Message,
                DateTime = te.DateTime.ToString("s"),
                MediaUrls = te.Media.Select(UserMediaOperations.GetUrl).ToList(),
                LikeUrl = $"/api/Timeline/{te.TimelineEntryId}/Like",
                LikeGroups = LikeOperations.MakeLikeGroups(te.Likes),
                CommentUrl = $"/api/Timeline/{te.TimelineEntryId}/Comment",
                Comments = CommentOperations.GetComments(te.CommentThread)
            })
                   .ToList());
        }
Ejemplo n.º 4
0
        public static IList <CommentDetails> GetComments(CommentThread commentThread)
        {
            if (commentThread == null)
            {
                return(new CommentDetails[0]);
            }

            return(commentThread.Comments
                   .OrderBy(c => c.Text.CreatedUtc)
                   .Select(
                       c =>
                       new CommentDetails
            {
                UserName = c.User.Name,
                UserId = c.UserId,
                AvatarUrl = UserOperations.GetAvatarUrl(c.User),
                Message = c.Text.Content,
                TimeAgo = TimeOperations.GetTimeAgo(c.Text.CreatedUtc),
                LikeUrl = $"/api/Comments/{c.CommentId}/Like",
                LikeGroups = LikeOperations.MakeLikeGroups(c.Text.Likes)
            })
                   .ToList());
        }
Ejemplo n.º 5
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);
        }