Пример #1
0
 public DreamMessage PostFeedEntry(XDoc body)
 {
     var published = body["published"].AsDate ?? DateTime.UtcNow;
     var title = body["title"].AsText;
     var summary = body["summary"].AsText;
     var content = body["content"].AsText;
     var feed = LoadFeed();
     var feedUri = feed["_:link[@rel='self']/@href"].AsUri;
     var link = feedUri.At(published.ToString("yyyy-MM-dd")).At(GetUriTitle(title));
     var entry = new XAtomEntry(title, published, published);
     entry.AddLink(link, XAtomBase.LinkRelation.Self, null, null, title);
     entry.AddSummary(summary);
     entry.AddContent(content);
     feed.Add(entry);
     SaveFeed(feed);
     return DreamMessage.Ok(entry);
 }
Пример #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;
        }
Пример #3
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;
        }
Пример #4
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;
        }