📄 weblogservice.cs
字号:
//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
// Copyright (c) Telligent Systems Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System;
using System.Collections;
using System.Security;
using System.Web.Services;
using System.Web.Services.Protocols;
using CommunityServer.Blogs.Components;
using CommunityServer.Components;
namespace CommunityServer.Blogs.Components
{
/// <summary>
/// BlogService provides a web service interface for posting and editing posts and articles
/// </summary>
[ WebService(Name="BlogService",Description="Community Server :: Blogs Posting Web Service",Namespace="http://communityserver.org/blogs/services/posts/")]
public class BlogService : CSWebServiceBase
{
public BlogService()
{
}
#region BlogPost
public struct BlogPost
{
public int PostID;
public string Title;
public string Body;
public string FormattedBody;
public DateTime Date;
public string Excerpt;
public string Name;
public string Custom;
public string[] Categories;
public bool IsPublished;
public bool EnableComments;
public bool EnableTrackbacks;
public bool EnableRatings;
public bool Syndicate;
public bool SyndicateExcerpt;
public bool SydicateRoot;
public bool DisplayOnHome;
public bool IsArticle;
public CommentModerationType ModerationType;
}
#endregion
#region Service Methods
[WebMethod(MessageName="Blogs",Description="Returns a list of blog the current user has post access to",EnableSession=false)]
[SoapHeader("Credentials")]
public string[] Blogs()
{
Login();
ArrayList blogs = Weblogs.GetWeblogs(false,true,false);
blogs = Sections.FilterByAccessControl(blogs,Permission.Post,CurrentUser);
string[] blogList = new string[blogs.Count];
for(int i = 0; i<blogs.Count; i++)
blogList[i] = ((Weblog)blogs[i]).ApplicationKey;
return blogList;
}
[WebMethod(MessageName="Create",Description="Creates a new Post",EnableSession=false)]
[SoapHeader("Credentials")]
public int Create(BlogPost post)
{
LoginSetSection();
WeblogPost wp = CreatePost(post);
int postID = -1;
BlogPostResults result = WeblogPosts.Add(wp,CurrentUser, out postID);
ProcessPostResults(result);
return postID;
}
[WebMethod(MessageName="Update",Description="Updates an existing post",EnableSession=false)]
[SoapHeader("Credentials")]
public void Update(BlogPost post)
{
LoginSetSection();
WeblogPost wp = CreatePost(post);
BlogPostResults result = WeblogPosts.Update(wp,CurrentUser.UserID);
ProcessPostResults(result);
}
[WebMethod(MessageName="Delete",Description="Deletes a post by the PostID",EnableSession=false)]
[SoapHeader("Credentials")]
public void Delete(int PostID)
{
LoginSetSection();
WeblogPost post = WeblogPosts.GetPost(PostID,false,false,false);
if(post == null)
throw new ArgumentOutOfRangeException("Post does not exist");
if(post.SectionID == CurrentWeblog.SectionID)
{
WeblogPosts.Delete(post,CurrentUser.UserID);
return;
}
throw new SecurityException("PostID does not match current Weblog");
}
[WebMethod(MessageName="GetRecentPosts",Description="Returns an array of recent posts",EnableSession=false)]
[SoapHeader("Credentials")]
public BlogPost[] GetRecentPosts(int count)
{
LoginSetSection();
return CreatePostArray(count,false);
}
[WebMethod(MessageName="GetRecentArticles",Description="Returns an array of recent articles",EnableSession=false)]
[SoapHeader("Credentials")]
public BlogPost[] GetRecentArticles(int count)
{
LoginSetSection();
return CreatePostArray(count,true);
}
[WebMethod(MessageName="GetPostCategories",Description="Returns a list of Category Names",EnableSession=false)]
[SoapHeader("Credentials")]
public string[] GetPostCategories()
{
LoginSetSection();
ArrayList al = PostCategories.GetCategories(CurrentWeblog.SectionID,CategoryType.BlogPost);
string[] sa = new string[al.Count];
for(int i = 0; i<al.Count; i++)
{
sa[i] = ((PostCategory)al[i]).Name;
}
return sa;
}
[WebMethod(MessageName="GetArticleCategories",Description="Returns a list of Category Names",EnableSession=false)]
[SoapHeader("Credentials")]
public string[] GetArticleCategories()
{
LoginSetSection();
ArrayList al = PostCategories.GetCategories(CurrentWeblog.SectionID,CategoryType.BlogArticle);
string[] sa = new string[al.Count];
for(int i = 0; i<al.Count; i++)
{
sa[i] = ((PostCategory)al[i]).Name;
}
return sa;
}
#endregion
#region Internal Members and Helper Methods
protected Weblog CurrentWeblog = null;
#region Create Post
private void ProcessPostResults(BlogPostResults result)
{
if(result == BlogPostResults.Success)
return;
if(result == BlogPostResults.DuplicationName)
throw new Exception(ResourceManager.GetString("Weblog_Exception_DuplicatePostName"));
if(result == BlogPostResults.DuplicateBody)
throw new Exception(ResourceManager.GetString("Weblog_Exception_DuplicatePostBody"));
}
private WeblogPost CreatePost(BlogPost post)
{
if(Globals.IsNullorEmpty(post.Title) || Globals.IsNullorEmpty(post.Body))
throw new ArgumentException("Post Title and Body must not be Null or Empty");
bool isNew = post.PostID == 0;
WeblogPost wp = null;
if(isNew)
{
wp = new WeblogPost();
wp.PostDate = DateTime.Now;
wp.BlogPostType = post.IsArticle ? BlogPostType.Article : BlogPostType.Post;
wp.PostType = PostType.HTML;
wp.SectionID = CurrentWeblog.SectionID;
}
else
{
wp = WeblogPosts.GetPost(post.PostID,false, false,false);
if(wp == null)
throw new ArgumentException("Post does not exist");
if(wp.SectionID != CurrentWeblog.SectionID)
throw new SecurityException("PostID does not match current Weblog");
}
wp.Subject = post.Title;
wp.Body = post.Body;
wp.FormattedBody = post.FormattedBody;
wp.Custom = post.Custom;
wp.Categories = post.Categories;
wp.DisplayOnHomePage = post.DisplayOnHome;
wp.EnableRatings = post.EnableRatings;
wp.EnableTrackBacks = post.EnableTrackbacks;
wp.Excerpt = post.Excerpt;
wp.IsAggregated = post.Syndicate;
wp.IsApproved = post.IsPublished;
wp.IsCommunityAggregated = post.SydicateRoot;
wp.IsLocked = !post.EnableComments;
wp.ModerationType = post.ModerationType;
wp.Name = post.Name;
wp.BloggerTime = post.Date;
wp.SyndicateExcerpt = post.SyndicateExcerpt;
wp.Username = CurrentUser.Username;
return wp;
}
#endregion
protected override void AllocateSection()
{
if(Globals.IsNullorEmpty(Credentials.SectionName))
throw new SecurityException("Invalid (or unsupplied) Blogname");
CurrentWeblog = Weblogs.GetWeblog(Credentials.SectionName.ToLower(),true);
if(CurrentWeblog == null)
throw new ArgumentException(string.Format("The Weblog {0} does not exist"), Credentials.SectionName);
Permissions.AccessCheck(CurrentWeblog,Permission.Post,CurrentUser);
}
private BlogPost ConvertWeblogPostToBlogPost(WeblogPost post)
{
BlogPost bp = new BlogPost();
bp.Title = post.Subject;
bp.Body = post.Body;
bp.FormattedBody = post.FormattedBody;
bp.Custom = post.Custom;
bp.Categories = post.Categories;
bp.DisplayOnHome = post.DisplayOnHomePage;
bp.EnableRatings = post.EnableRatings;
bp.EnableTrackbacks = post.EnableTrackBacks;
bp.Excerpt = post.Excerpt;
bp.Syndicate = post.IsAggregated;
bp.IsPublished = post.IsApproved;
bp.SydicateRoot = post.IsCommunityAggregated;
bp.ModerationType = post.ModerationType;
bp.Name = post.Name;
bp.Date = post.BloggerTime;
bp.SyndicateExcerpt = post.SyndicateExcerpt;
bp.PostID = post.PostID;
return bp;
}
private BlogPost[] CreatePostArray(int count, bool isArticle)
{
WeblogConfiguration config = WeblogConfiguration.Instance();
if(count > config.ServicePostCountLimit) count = config.ServicePostCountLimit;
BlogThreadQuery query = new BlogThreadQuery();
query.BlogID = CurrentWeblog.SectionID;
query.BlogThreadType = BlogThreadType.Recent;
query.IncludeCategories = true;
query.IsPublished = false;
query.PageIndex = 0;
query.PageSize = count;
query.SortBy = BlogThreadSortBy.MostRecent;
query.SortOrder = SortOrder.Descending;
query.BlogPostType = isArticle ? BlogPostType.Article : BlogPostType.Post;
ThreadSet threads = WeblogPosts.GetBlogThreads(query,false);
BlogPost[] posts = new BlogPost[threads.Threads.Count];
for(int i =0; i<threads.Threads.Count; i++)
{
WeblogPost post = threads.Threads[i] as WeblogPost;
posts[i] = ConvertWeblogPostToBlogPost(post);
}
return posts;
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -