forked from wojilu/wojilu
/
MicroblogController.cs
executable file
·298 lines (209 loc) · 9.21 KB
/
MicroblogController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
* Copyright (c) 2010, www.wojilu.com. All rights reserved.
*/
using System;
using System.Collections.Generic;
using wojilu.Web.Mvc;
using wojilu.Web.Mvc.Attr;
using wojilu.Common.Microblogs.Service;
using wojilu.Common.Microblogs.Domain;
using wojilu.Common.Microblogs.Interface;
using wojilu.Members.Users.Interface;
using wojilu.Members.Users.Service;
using wojilu.Members.Users.Domain;
using wojilu.Web.Controller.Common;
using wojilu.Web.Controller.Users;
using wojilu.Common.Microblogs;
namespace wojilu.Web.Controller.Microblogs {
public partial class MicroblogController : ControllerBase {
public IMicroblogService microblogService { get; set; }
public IFollowerService followService { get; set; }
public IVisitorService visitorService { get; set; }
public MicroblogFavoriteService mfService { get; set; }
public MicroblogCommentService commentService { get; set; }
public MicroblogAtService matService { get; set; }
public UserTagService userTagService { get; set; }
public MicroblogController() {
microblogService = new MicroblogService();
followService = new FollowerService();
visitorService = new VisitorService();
mfService = new MicroblogFavoriteService();
commentService = new MicroblogCommentService();
matService = new MicroblogAtService();
userTagService = new UserTagService();
}
public override void Layout() {
User user = ctx.owner.obj as User;
ctx.SetItem( "loadHeader", false );
bindUserInfo( user );
bindUserTags();
set( "cmdFollow", bindCmd( user ) );
List<User> followers = followService.GetRecentFriendsAndFollowers( ctx.owner.Id, 18 );
bindUsers( followers, "users" );
List<User> visitors = visitorService.GetRecent( 9, ctx.owner.Id );
bindUsers( visitors, "visitor" );
set( "moreFollowers", to( new wojilu.Web.Controller.Microblogs.FriendController().FollowerList ) );
set( "moreVisitors", to( new wojilu.Web.Controller.Users.VisitorController().Index ) );
}
public void List() {
Page.Title = ctx.owner.obj.Name + "的微博_" + config.Instance.Site.SiteName;
Page.Keywords = ctx.owner.obj.Name + "的微博";
if (ctx.viewer.HasPrivacyPermission( ctx.owner.obj, UserPermission.Microblog.ToString() ) == false) {
echo( lang( "exVisitNoPermission" ) );
return;
}
set( "user.Name", ctx.owner.obj.Name );
DataPage<Microblog> list = microblogService.GetPageList( ctx.owner.obj.Id, MicroblogAppSetting.Instance.MicroblogPageSize );
List<MicroblogVo> volist = mfService.CheckFavorite( list.Results, ctx.viewer.Id );
ctx.SetItem( "_microblogVoList", volist );
ctx.SetItem( "_showUserFace", false );
load( "blogList", bindBlogs );
set( "page", list.PageBar );
}
public void Show( int id ) {
Microblog blog = microblogService.GetById( id );
if (blog == null) {
echo( lang( "exDataNotFound" ) );
return;
}
// 详细内容:使用通用视图文件
loadCommonView( blog );
// 评论列表
DataPage<MicroblogComment> comments = commentService.GetComments( id, 20 );
IBlock cblock = getBlock( "comments" );
bindComments( cblock, comments.Results );
String pager = comments.PageCount > 1 ? comments.PageBar : "";
set( "page", pager );
// 评论表单
target( new MicroblogCommentsController().SaveReply );
set( "c.RootId", id );
set( "c.ParentId", 0 );
set( "viewer.PicSmall", ctx.viewer.obj.PicSmall );
}
//------------------------------------------------------------------------------------------------
[NonVisit]
public void bindBlogs() {
List<MicroblogVo> list = (List<MicroblogVo>)ctx.GetItem( "_microblogVoList" );
Boolean showUserFace = (Boolean)ctx.GetItem( "_showUserFace" );
IBlock block = getBlock( "list" );
foreach (MicroblogVo blog in list) {
bindOne( block, blog.Microblog, blog.IsFavorite, showUserFace );
block.Next();
}
}
//------------------------------------------------------------------------------------------------
[Login]
public void Forward( int id ) {
Microblog blog = microblogService.GetById( id );
if (blog == null) {
errors.Add( lang( "exDataNotFound" ) );
echoError();
return;
}
IBlock pblock = getBlock( "parent" );
if (blog.ParentId <= 0) {
set( "blog.User", blog.User.Name );
set( "blog.Content", blog.Content );
set( "commentBody", "" );
}
else {
Microblog parent = microblogService.GetById( blog.ParentId );
set( "blog.User", blog.User.Name );
set( "blog.Content", parent.Content );
set( "commentBody", "//@" + blog.User.Name + ":" + blog.Content );
pblock.Set( "blog.ParentUser", parent.User.Name );
pblock.Next();
}
target( Save, id );
}
[HttpPost, DbTransaction]
public void Save( int id ) {
Microblog tblog = microblogService.GetById( id );
int isComment = ctx.PostIsCheck( "isComment" );
int isCommentParent = ctx.PostIsCheck( "isComment" );
String content = ctx.Post( "content" );
if (strUtil.IsNullOrEmpty( content )) content = "转发微博";
Microblog blog = new Microblog();
blog.Content = content;
blog.ParentId = getParentId( tblog ); // 转发微博
blog.User = ctx.viewer.obj as User;
blog.Ip = ctx.Ip;
microblogService.Insert( blog );
// 给对方评论
if (isComment == 1) {
saveComment( tblog, content );
}
// 给原始微博评论
if (isCommentParent == 1) {
saveCommentParent( tblog, content );
}
echoToParent( "转发成功" );
}
private int getParentId( Microblog tblog ) {
if (tblog.ParentId <= 0) return tblog.Id;
return tblog.ParentId;
}
private void saveCommentParent( Microblog tblog, string content ) {
if (tblog.ParentId <= 0) return;
Microblog parent = microblogService.GetById( tblog.ParentId );
saveComment( parent, content );
}
private void saveComment( Microblog tblog, String content ) {
MicroblogComment c = new MicroblogComment();
c.Root = tblog;
c.Content = content;
c.User = ctx.viewer.obj as User;
c.Ip = ctx.Ip;
commentService.InsertComment( c, to( new MicroblogController().Show, tblog.Id ) );
}
[HttpPost, DbTransaction]
public void SaveFavorite( int id ) {
if (ctx.viewer.IsLogin == false) {
echoJsonMsg( "请先登录", false, "" );
return;
}
Microblog blog = microblogService.GetById( id );
if (blog == null) {
errors.Add( lang( "exDataNotFound" ) );
echoError();
return;
}
mfService.SaveFavorite( ctx.viewer.Id, blog );
echoJsonMsg( "ok", true, null );
}
[HttpPost, DbTransaction]
public void CancelFavorite( int id ) {
Microblog blog = microblogService.GetById( id );
if (blog == null) {
errors.Add( lang( "exDataNotFound" ) );
echoError();
return;
}
mfService.CancelFavorite( ctx.viewer.Id, blog );
echoJsonMsg( "ok", true, null );
}
//------------------------------------------------------------------------------------------------
[HttpPost, DbTransaction]
public void Follow() {
if (ctx.viewer.IsLogin == false) errors.Add( "请先登录" );
if (ctx.viewer.IsFollowing( ctx.owner.Id )) errors.Add( "已经关注" );
if (ctx.HasErrors) {
echoText( errors.ErrorsText );
return;
}
followService.Follow( ctx.viewer.Id, ctx.owner.Id );
echoAjaxOk();
}
[HttpPost, DbTransaction]
public void CancelFollow() {
if (ctx.viewer.IsLogin == false) errors.Add( "请先登录" );
if (ctx.viewer.IsFollowing( ctx.owner.Id ) == false) errors.Add( "尚未关注,不可取消" );
if (ctx.HasErrors) {
echoText( errors.ErrorsText );
return;
}
followService.DeleteFollow( ctx.viewer.Id, ctx.owner.Id );
echoAjaxOk();
}
}
}