作者: Andy. 时间: 2015-09-29 11:06:47
必须先吐槽一下,,作业真的好多….像我这种老实人又要一个字一个字的去认真完成….哎,
在沉静一周之后,本博客再度发力…完成了多说的本地评论的反向同步(其实就几行简单的代码)、后台功能更新,bug修复(和国产软件更新日志差不多…就看到更新日志在吹…)
先说说多说这个评论反向同步吧,先看文档:
说每次那边对评论有action后就会向我们提供的接口*&&……%&之类的,先回发送一个动作标识action和base64加密后的什么什么梗喔…实际上post过来的就一个action(内容为:sync_log)一串27个字符的字符串,,反正我收到的是这个,而且绝壁不是base64编码后的…验证什么的,,直接验证这个27长度的字符串就好了。
然后按照文档说的,发送一个get请求,获取对评论的操作。。直接贴代码…
处理请求的代码:
Common.ConfigurationHelper configurationHelper = new Common.ConfigurationHelper(); string action = Request.Form["action"]; string signature = Request.Form["signature"];//action if (string.IsNullOrEmpty(signature) || string.IsNullOrEmpty(action) || action != "sync_log" || signature != configurationHelper.AppSetting("signature")) { return null; } string short_name = configurationHelper.AppSetting("short_name"); string secret = configurationHelper.AppSetting("secret"); System.Net.WebClient web = new System.Net.WebClient(); byte[] buffer = web.DownloadData("http://api.duoshuo.com/log/list.json?short_name=" + short_name + "&secret=" + secret + "&order=desc&limit=1"); web.Dispose(); System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer); string responseData = Encoding.UTF8.GetString(buffer); ms.Close(); JObject jObject = JObject.Parse(responseData); if (jObject["code"].ToString() != "0") { return null; } JObject commentObject = jObject["response"].ToArray().First().ToObject<JObject>(); if (!IsNewAction(commentObject["log_id"].ToString())) { return null; } switch (commentObject["action"].ToString()) { case "create": Create(commentObject); break; case "delete": Delete(commentObject); break; default: break; } Response.StatusCode = 404; Response.End(); return null;
这是提供给多说反向同步的接口,只实现了两个action,创建评论和删除评论的。
order
string 可选
排序方式。asc
:从旧到新;desc
:从新到旧;默认为asc
。
从官方文档上得到的,我选择的是从新到旧,然后获取一条action。然后对获取到的记录做出判断,是新的评论还是删除评论。从而做出相应的判断。
这是新建的代码:
Common.BLLContext bLLContext = new Common.BLLContext(); JObject data = jObject["meta"].ToObject<JObject>(); Model.Comment comment = new Model.Comment() { comment_agent = data["agent"].ToString(), comment_approved = data["status"].ToString(), comment_author = data["author_name"].ToString(), comment_author_email = data["author_email"].ToString(), comment_author_IP = data["ip"].ToString(), comment_author_url = data["author_url"].ToString(), comment_content = data["message"].ToString(), comment_date = DateTime.Parse(data["created_at"].ToString()), comment_post_ID = int.Parse(data["thread_key"].ToString()), comment_type = data["type"].ToString() }; Model.Comment parentComment = bLLContext.iBLLSession.ICommentBLL.GetCommentByDuoShuoID(data["parent_id"].ToString()); if (parentComment != null) { comment.comment_parent = parentComment.comment_ID; } bLLContext.iBLLSession.ICommentBLL.Add(comment); Model.Commentmeta commentmeta = new Model.Commentmeta() { comment_ID = comment.comment_ID, meta_key = "post_id",//post_id为多说中评论的ID meta_value = data["post_id"].ToString() }; bLLContext.iBLLSession.ICommentmetaBLL.Add(commentmeta);
不同的数据库结构肯定生成的相应的评论实体是不相同的。可能这儿熟悉wordpress的人能看出一些名堂来了。看出来了还是不要说出来哈。
删除过来的返回数据类似于这种Json格式:
{
log_id: "1210700840xxxxxxxxx",
user_id: "xxxxxxxxx",
action: "delete",
meta: [
"xxxxxxxxx"
],
date: 1443493932
}
官方文档都有说明,这里面的meta就是在多说中,该评论的ID,所以在上面存评论的时候多存了一个commentmeta,就是为了处理删除请求的(当然还包括其他的一些请求)。删除的代码就不贴了。
至此,基本上可以满足将评论实时同步回本地数据库了。