Example #1
0
        /// <summary>
        /// Adds a new entry to the timeline.
        /// </summary>
        /// <param name="createMessage">The entry to add.</param>
        /// <returns></returns>
        public async Task Post(CreateTimelineEntry createMessage)
        {
            UserInfo userEntity = await GetUserInfoAsync();
            HttpStatusCode result = await TimelineOperations.AddTimelineEntryAsync(createMessage, DbContext, userEntity);

            result.ThrowHttpResponseExceptionIfNotSuccessful();
        }
Example #2
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;
        }