Ejemplo n.º 1
0
        public static XDoc GetCommentXmlAsAtom(IList <CommentBE> comments, XUri feedUri, PageBE page)
        {
            var       resources = DekiContext.Current.Resources;
            string    title     = resources.Localize(DekiResources.COMMENT_FOR(page.Title.AsUserFriendlyName()));
            XAtomFeed feed      = new XAtomFeed(title, feedUri, DateTime.UtcNow);

            feed.AddLink(PageBL.GetUriUi(page), XAtomBase.LinkRelation.Alternate, MimeType.XHTML, null, page.Title.AsUserFriendlyName());
            feed.Id = feedUri;

            foreach (CommentBE c in comments)
            {
                UserBE posterUser = UserBL.GetUserById(c.PosterUserId);
                title = c.Title;
                if (string.IsNullOrEmpty(title))
                {
                    title = resources.Localize(DekiResources.COMMENT_BY_TO(posterUser.Name, page.Title.AsUserFriendlyName()));
                }
                XAtomEntry entry = feed.StartEntry(title, c.CreateDate, (c.LastEditDate == null || c.LastEditDate == DateTime.MinValue) ? c.CreateDate : c.LastEditDate.Value);
                entry.Id = GetUri(c);
                entry.AddAuthor(posterUser.Name, UserBL.GetUriUiHomePage(posterUser), posterUser.Email);
                MimeType commentMimetype;
                MimeType.TryParse(c.ContentMimeType, out commentMimetype);
                entry.AddContent(c.Content);

                XUri entryLink = PageBL.GetUriUi(page).WithFragment("comment" + c.Number);
                entry.AddLink(entryLink, XAtomBase.LinkRelation.Alternate, null, null, null);
                entry.AddLink(GetUri(c).At("content"), XAtomBase.LinkRelation.Enclosure, commentMimetype, c.Content.Length, "content");
                feed.End();
            }

            return(feed);
        }
Ejemplo n.º 2
0
        public Yield GetEntry(DreamContext context, DreamMessage request, Result <DreamMessage> response)
        {
            string     id    = context.GetParam("id");
            XAtomEntry entry = null;

            // get feed
            XAtomFeed feed = _feed;

            if (feed != null)
            {
                lock (feed) {
                    entry = new XAtomEntry(feed[string.Format("entry[id='{0}']", Self.At(id).Uri)]);
                }
            }
            else
            {
                throw new DreamBadRequestException("not initialized");
            }
            if (entry.IsEmpty)
            {
                response.Return(DreamMessage.NotFound("entry not found"));
            }
            else
            {
                response.Return(DreamMessage.Ok(MimeType.ATOM, entry));
            }
            yield break;
        }
Ejemplo n.º 3
0
        protected override Yield Stop(Result result)
        {
            _feed = null;
            yield return(Coroutine.Invoke(base.Stop, new Result()));

            result.Return();
        }
Ejemplo n.º 4
0
        private void AutoDeletePost(TaskTimer timer)
        {
            XAtomFeed feed = _feed;

            if (feed != null)
            {
                lock (feed) {
                    feed[string.Format("_:entry[_:id='{0}']", Self.At(((int)timer.State).ToString()).Uri.AsPublicUri())].Remove();
                }
            }
        }
Ejemplo n.º 5
0
        //--- Methods ---
        protected override Yield Start(XDoc config, Result result)
        {
            yield return(Coroutine.Invoke(base.Start, config, new Result()));

            if (_feed == null)
            {
                _feed       = new XAtomFeed(config["feed-title"].AsText ?? "Atom Feed", Self, DateTime.UtcNow);
                _defaultTTL = config["default-ttl"].AsDouble ?? 3600.0;
                _counter    = 0;
            }
            result.Return();
        }
Ejemplo n.º 6
0
        private XAtomFeed LoadFeed()
        {
            var storageResponse = Storage.At("feed.xml").Get(new Result <DreamMessage>()).Wait();

            if (storageResponse.IsSuccessful)
            {
                return(new XAtomFeed(storageResponse.ToDocument()));
            }
            var feed = new XAtomFeed("some feed", Self.Uri.AsPublicUri(), DateTime.UtcNow);

            SaveFeed(feed);
            return(feed);
        }
Ejemplo n.º 7
0
        public Yield GetEntries(DreamContext context, DreamMessage request, Result <DreamMessage> response)
        {
            XAtomFeed feed = _feed;

            if (feed != null)
            {
                response.Return(DreamMessage.Ok(MimeType.ATOM, _feed));
            }
            else
            {
                throw new DreamBadRequestException("not initialized");
            }
            yield break;
        }
Ejemplo n.º 8
0
        internal Yield DeleteEntry(DreamContext context, DreamMessage request, Result <DreamMessage> response)
        {
            string    id   = context.GetParam("id");
            XAtomFeed feed = _feed;

            if (feed != null)
            {
                lock (feed) {
                    feed[string.Format("entry[id='{0}']", id)].Remove();
                }
            }
            response.Return(DreamMessage.Ok());
            yield break;
        }
Ejemplo n.º 9
0
        public XDoc Calendar(
            [DekiExtParam("Google calendar feed uri ")] XUri uri,
            [DekiExtParam("starting date (default: today)", true)] string startDate,
            [DekiExtParam("ending date (default: 7 days from today)", true)] string endDate,
            [DekiExtParam("calendar width (default: 800)", true)] float?width,
            [DekiExtParam("calendar height (default: 800)", true)] float?height
            )
        {
            XDoc result = new XDoc("html");

            // try reading supplied dates
            DateTime start;
            DateTime end;

            if (!DateTime.TryParse(startDate, out start))
            {
                start = DateTime.UtcNow;
            }
            if (!DateTime.TryParse(endDate, out end))
            {
                end = DateTime.UtcNow.AddDays(7);
            }

            Plug         calPlug  = Plug.New(uri).With("start-min", start.ToString("s")).With("start-max", end.ToString("s"));
            DreamMessage response = calPlug.GetAsync().Wait();

            if (response.IsSuccessful)
            {
                XDoc doc = response.ToDocument();
                if (doc.HasName("feed"))
                {
                    XAtomFeed calFeed = new XAtomFeed(doc);
                    calFeed.UsePrefix("atom", "http://www.w3.org/2005/Atom");
                    XUri embedUri = calFeed["atom:link[@rel='alternate']/@href"].AsUri;
                    result = NewIFrame(embedUri, width ?? 800, height ?? 800);
                }
                else
                {
                    // BUGBUGBUG (steveb): user provided an embeddable representation; we won't be able to use the start and end date parameters then.

                    result = NewIFrame(uri, width ?? 800, height ?? 800);
                }
            }
            return(result);
        }
Ejemplo n.º 10
0
        internal Yield PutEntry(DreamContext context, DreamMessage request, Result <DreamMessage> response)
        {
            if (request.ToDocument().Name != "entry")
            {
                throw new DreamBadRequestException("invalid format");
            }
            string id = context.GetParam("id");

            // prepare entry
            XAtomEntry entry = new XAtomEntry(request.ToDocument());
            XUri       link  = Self.At(id);

            entry.Id = link;
            entry.AddLink(link, XAtomBase.LinkRelation.Edit, null, 0, null);

            // update feed
            XAtomFeed  feed = _feed;
            XAtomEntry oldEntry;

            if (feed != null)
            {
                lock (feed) {
                    oldEntry = new XAtomEntry(feed[string.Format("entry[id='{0}']", link)]);
                    if (!oldEntry.IsEmpty)
                    {
                        oldEntry.Replace(entry);
                    }
                }
            }
            else
            {
                throw new DreamBadRequestException("not initialized");
            }
            if (oldEntry.IsEmpty)
            {
                response.Return(DreamMessage.NotFound("entry not found"));
            }
            else
            {
                response.Return(DreamMessage.Ok(MimeType.ATOM, entry));
            }
            yield break;
        }
Ejemplo n.º 11
0
        internal Yield PostEntries(DreamContext context, DreamMessage request, Result <DreamMessage> response)
        {
            if (request.ToDocument().Name != "entry")
            {
                throw new DreamBadRequestException("invalid format");
            }

            // prepare entry
            XAtomEntry entry  = new XAtomEntry(request.ToDocument());
            int        number = System.Threading.Interlocked.Increment(ref _counter);
            XUri       link   = Self.At(number.ToString());

            entry.Id = link;
            entry.AddLink(link, XAtomBase.LinkRelation.Edit, null, 0, null);

            // update feed
            XAtomFeed feed = _feed;

            if (feed != null)
            {
                lock (feed) {
                    feed.Add(entry);
                }
            }
            else
            {
                throw new DreamBadRequestException("not initialized");
            }

            // schedule entry deletion
            double seconds = context.GetParam <double>("ttl", _defaultTTL);

            if (seconds > 0)
            {
                TimerFactory.New(TimeSpan.FromSeconds(seconds), AutoDeletePost, number, TaskEnv.Clone());
            }

            // return updated entry
            response.Return(DreamMessage.Created(link, entry));
            yield break;
        }
Ejemplo n.º 12
0
 private void SaveFeed(XAtomFeed feed)
 {
     Storage.At("feed.xml").Put(feed);
 }