Exemple #1
0
 public static Task IndexTimelineMessageAsync(
     TimelineEntry entry)
 {
     return IndexItemAsync(
         new MessageIndexEntry
         {
             ItemId = "timeline-" + entry.TimelineEntryId,
             Content = entry.Message.Content,
             TimelineEntryId = entry.TimelineEntryId
         });
 }
        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;
        }