Exemple #1
0
        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 ActionResult Unsubscribe(long id, string emailHash)
        {
            UserEngageHistory history = this.EntityContext.TryGetUserEngageHistory(id);

            if (history == null)
            {
                throw new Exception(String.Format("Unable to unsubscribe. Could not find the requested engagement {0}", id));
            }

            history.UserReference.Load();
            history.User.UserEngageReference.Load();

            if (history.User.Email.ComputeMd5() != emailHash)
            {
                throw new Exception(String.Format("Unable to verify hash for engagement {0}. Received hash {1}", id, emailHash));
            }

            history.RespondTime = DateTime.Now;
            history.User.UserEngage.Unsubscribe = true;
            this.EntityContext.SaveChanges();

            return(this.View());
        }
Exemple #3
0
        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));
        }