internal static int? GetThreadIdOrNull(Timeline tl) { if (tl == null) return null; if (tl.Type != TimelineType.PersonalThread) return null; var value = tl.RouteValues["threadId"] as IConvertible; if (value == null) return null; return Convert.ToInt32(value); }
public ActionResult CreatePersonalThread(string body, HttpPostedFileBase uploadedFile) { if (string.IsNullOrEmpty(body)) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } // ユーザーのマイページスレッドを新規追加する db.Users.Attach(ctx.CurrentUser); var newThread = new PersonalThread() { // マイページスレッドにはスレッド名は不要だが、Required属性がついているため日時を設定しておく。 ThreadName = ctx.Now.ToString("yyyy/MM/dd HH:mm:ss"), // マイページスレッドには期間は不要。 Duration = null, OwnerUser = (ParticipantUser)ctx.CurrentUser }; db.Threads.Add(newThread); var newMessage = threadBusiness.AddMessage(newThread, body, uploadedFile); try { db.SaveChanges(); } catch (System.Data.Entity.Infrastructure.DbUpdateException ex) { var es = ex.Entries.ToArray(); var iex = ex.InnerException as OptimisticConcurrencyException; System.Diagnostics.Debugger.Break(); throw; } // 自分のおよび友人のタイムラインを更新する // TODO: 非同期処理の検討 var myTimeLine = new Timeline { OwnerID = ctx.CurrentUser.ID, Timestamp = ctx.Now, Type = TimelineType.PersonalThread, RouteValuesJSON = Timeline.ToJSON(new { threadId = newThread.ID }), }; var friends = new Business.FriendGraph(db) .GetFriends(ctx.CurrentUser.ID); var summary = newMessage.BodySummary; var friendTimeLines = friends.Select(u => new Timeline { OwnerID = u.ID, Timestamp = ctx.Now, Type = TimelineType.FriendThread, SourceName = ctx.CurrentUser.DisplayName, Summary = summary, ActionName = "Index", ControllerName = "User", RouteValuesJSON = Timeline.ToJSON(new { id = ctx.CurrentUser.ID, threadId = newThread.ID }), }); using (var publisher = new Business.TimelinePublisher()) { publisher.Publish(new[] { myTimeLine }.Concat(friendTimeLines)); } return RedirectToAction("Index"); }