public EmptyResult ReaderComment(long id) { try { this.EntityContext.TryAttach(this.ActiveUser); Data.Comic comic = this.EntityContext.TryGetComic(id, this.ActiveUser); if (comic != null) { comic.AuthorReference.Load(); UserEngage engage = this.GetUserEngage(comic.Author); comic.Author.UserEngageReference.Load(); UserEngageHistory history = comic.Author.UserEngageHistory .OrderByDescending(h => h.EngageTime) .FirstOrDefault(h => h.Engagement == UserEngageHistory.EngagementType.Comment); if (!engage.Unsubscribe && engage.Comment && (history == null || history.EngageTime <= DateTime.Now.AddDays(-1))) { ClientComic c = new ClientComic(comic); // create & save history history = new UserEngageHistory(); history.Engagement = UserEngageHistory.EngagementType.Comment; history.EngageTime = DateTime.Now; history.User = comic.Author; this.EntityContext.AddToUserEngageHistory(history); this.EntityContext.SaveChanges(); // Generate email message EmailManager email = new EmailManager(this.Server); Dictionary <string, string> data = new Dictionary <string, string>(); data.Add("id", history.EngageHistoryId.ToString()); data.Add("title", String.Format("New Comment - {0}", comic.Title)); data.Add("reader.name", this.ActiveUser.Nickname); data.Add("comic.title", comic.Title); data.Add("comic.readUrl", c.ReadUrl); // Send email email.SendEmail(comic.Author, "Comment.html", data); } } } finally { this.EntityContext.TryDetach(this.ActiveUser); } return(new EmptyResult()); }
public JsonResult Publish(long comicId, string title, string description, bool isPrivate) { Data.Comic comic = null; ClientComic c = null; try { this.EntityContext.TryAttach(this.ActiveUser); comic = this.EntityContext.TryGetUnpublishedComic(comicId, this.ActiveUser); if (comic == null || comic.Uid != this.ActiveUser.Uid) { throw new Exception("Could not find the requested comic."); } comic.Title = title; comic.Description = description; comic.IsPrivate = isPrivate; // Get bytecode from storage CloudBlobContainer container = this.BlobClient.GetContainerReference(ComicConfigSectionGroup.Blob.RenderContainer); CloudBlobDirectory directory = container.GetDirectoryReference(ComicConfigSectionGroup.Blob.ComicDirectory); CloudBlob blob = directory.GetBlobReference(comic.StorageKey); ClientComic clientComic = new ClientComic(comic); // Publish to facebook album for better visibility MemoryStream photoStream = new MemoryStream(); try { blob.DownloadToStream(photoStream); FacebookMediaObject fbMedia = new FacebookMediaObject { ContentType = "image/jpeg", FileName = String.Format("{0}.jpg", comic.StorageKey) }; fbMedia.SetValue(photoStream.ToArray()); Dictionary <string, object> photoParams = new Dictionary <string, object>(); photoParams.Add("message", String.Format("{0} - Remix this comic at {1}", comic.Description, clientComic.RemixUrl)); photoParams.Add("source", fbMedia); this.Facebook.Post("/me/photos", photoParams); } catch (Exception x) { this.Log.Error("Unable to publish comic to facebook album.", x); } finally { photoStream.Dispose(); } this.EntityContext.PublishComic(comic, this.ActiveUser); c = new ClientComic(comic); this.EntityContext.SaveChanges(); // Email notifications UserEngage engage = this.GetUserEngage(this.ActiveUser); if (!engage.Unsubscribe && engage.ComicCreate) { // create & save history UserEngageHistory history = new UserEngageHistory(); history.Engagement = UserEngageHistory.EngagementType.ComicCreate; history.EngageTime = DateTime.Now; history.User = this.ActiveUser; this.EntityContext.AddToUserEngageHistory(history); this.EntityContext.SaveChanges(); // Generate email message EmailManager email = new EmailManager(this.Server); Dictionary <string, string> data = new Dictionary <string, string>(); data.Add("id", history.EngageHistoryId.ToString()); data.Add("title", String.Format("New Comic - {0}", comic.Title)); data.Add("comic.title", c.Title); data.Add("comic.readUrl", c.ReadUrl); // Send email email.SendEmail(this.ActiveUser, "ComicCreate.html", data); } // Check for notifications of a remixed comic if (comic.RemixedComic != null) { engage = this.EntityContext.TryGetUserEngage(comic.RemixedComic.Author); ClientComic remixed = new ClientComic(comic.RemixedComic); if (!engage.Unsubscribe && engage.ComicRemix) { UserEngageHistory history = new UserEngageHistory(); history.Engagement = UserEngageHistory.EngagementType.ComicRemix; history.EngageTime = DateTime.Now; history.User = comic.RemixedComic.Author; this.EntityContext.AddToUserEngageHistory(history); this.EntityContext.SaveChanges(); // Generate email message EmailManager email = new EmailManager(this.Server); Dictionary <string, string> data = new Dictionary <string, string>(); data.Add("id", history.EngageHistoryId.ToString()); data.Add("title", String.Format("Remixed Comic - {0}", comic.Title)); data.Add("comic.title", c.Title); data.Add("comic.readUrl", c.ReadUrl); data.Add("remix.title", remixed.Title); data.Add("remix.readUrl", remixed.ReadUrl); // Send email email.SendEmail(comic.RemixedComic.Author, "ComicRemix.html", data); } } } finally { this.EntityContext.TryDetach(this.ActiveUser); } return(this.Json(c, JsonRequestBehavior.DenyGet)); }
public ViewResult Create(long?id) { ClientComic comic = null; List <ClientTemplate> templates = this.EntityContext.ListTemplates() .ToList() .Select(t => new ClientTemplate(t)) .ToList(); foreach (ClientTemplate t in templates) { int width = Math.Min(t.Width, 734); double factor = Convert.ToDouble(width) / Convert.ToDouble(t.Width); t.Width = width; t.Height = Convert.ToInt32(Convert.ToDouble(t.Height) * factor); foreach (ClientTemplateItem i in t.TemplateItems) { i.Width = Convert.ToInt32(Convert.ToDouble(i.Width) * factor); i.Height = Convert.ToInt32(Convert.ToDouble(i.Height) * factor); i.X = Convert.ToInt32(Convert.ToDouble(i.X) * factor); i.Y = Convert.ToInt32(Convert.ToDouble(i.Y) * factor); } } if (id.HasValue) { // Facebook shared comics Data.Comic c = this.EntityContext.TryGetComic(id.Value, this.ActiveUser) ?? this.EntityContext.TryGetUnpublishedComic(id.Value, this.ActiveUser); if (c != null) { c.ComicTextBubbles.Load(); c.ComicPhotos.Load(); comic = new ClientComic(c); // inject newlines into text int maxWidth = templates.SelectMany(t => t.TemplateItems).Select(t => t.Width).Min(); Font measureFont = new Font(ComicGenerator.ComicFont, 7, FontStyle.Regular, GraphicsUnit.Point); ComicGenerator generator = new ComicGenerator(templates.Min(t => t.Width), templates.Min(t => t.Height)); foreach (ComicTextBubble bubble in c.ComicTextBubbles) { Size rawTextSize = generator.MeasureText(bubble.Text, maxWidth, measureFont).ToSize(); Size textSize = this.CalculateTextSize(bubble.Text, rawTextSize, bubble.TextBubbleDirection.TextBubble); textSize.Width = Math.Min(textSize.Width, maxWidth); // Don't exceede template item area textSize = generator.MeasureText(bubble.Text, textSize.Width, measureFont).ToSize(); textSize.Width += 6; // Measure error ? int lines = textSize.Height / measureFont.Height; int cutWidth = (bubble.Text.Length / lines) - 4; for (int l = 1; l < lines; l++) { int cut = cutWidth * l; int space = bubble.Text.IndexOf(' ', cut); if (space > 0) { bubble.Text = bubble.Text.ReplaceAt(space, '\n'); } } comic.Bubbles.First(b => b.ComicTextBubbleId == bubble.ComicTextBubbleId).Text = bubble.Text; } } } List <ClientTextBubbleDirection> bubbles = this.EntityContext.ListTextBubbles() .SelectMany(b => b.TextBubbleDirections) .ToList() .Select(d => new ClientTextBubbleDirection(d)) .ToList(); return(this.View(new ViewCreate(comic, templates, this.GetEffects(), bubbles))); }