forked from wojilu/wojilu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MicroblogController.cs
executable file
·372 lines (262 loc) · 11.3 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
* 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;
using wojilu.Common.Comments;
using wojilu.Members.Sites.Domain;
using wojilu.Members.Interface;
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 IOpenCommentService 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 OpenCommentService();
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 );
ctx.SetItem( "_showType", "microblog" );
load( "blogList", bindBlogs );
set( "page", list.PageBar );
}
public void Show( int id ) {
Microblog blog = microblogService.GetById( id );
if (blog == null) {
echo( lang( "exDataNotFound" ) );
return;
}
set( "blog.Id", id );
// 详细内容:使用通用视图文件
loadCommonView( blog );
// 评论表单
//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 oBlog, String content ) {
User creator = oBlog.Creator;
IMember owner = User.findById( oBlog.OwnerId );
OpenComment c = new OpenComment();
c.Content = content;
c.TargetUrl = MbLink.ToShowFeed( oBlog.User, oBlog.Id );
c.TargetDataType = oBlog.GetType().FullName;
c.TargetDataId = oBlog.Id;
c.TargetTitle = string.Format( "(微博{0}){1}", oBlog.Created.ToShortDateString(), strUtil.ParseHtml( oBlog.Content, 25 ) );
c.TargetUserId = oBlog.User.Id;
c.OwnerId = owner.Id;
c.AppId = 0;
c.FeedId = oBlog.Id;
c.Ip = oBlog.Ip;
c.Author = creator.Name;
c.ParentId = 0;
c.AtId = 0;
c.Member = creator;
commentService.Create( c );
}
[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 SaveLike( int mid ) {
if (ctx.viewer.IsLogin == false) {
echoJsonMsg( "请先登录", false, "" );
return;
}
if (mid <= 0) {
echoText( "mid=" + mid );
return;
}
Microblog microblog = Microblog.findById( mid );
if (microblog == null) {
echoText( "microblog is null. mid=" + mid );
return;
}
// 检查是否已经like
MicroblogLike flike = MicroblogLike.find( "UserId=" + ctx.viewer.Id + " and MicroblogId=" + mid ).first();
if (flike != null) {
echoText( "您已经赞过" );
return;
}
microblog.Likes = microblog.Likes + 1;
microblog.update();
MicroblogLike microblogLike = new MicroblogLike();
microblogLike.User = ctx.viewer.obj as User;
microblogLike.Microblog = microblog;
microblogLike.Ip = ctx.Ip;
microblogLike.insert();
// target likes
if (strUtil.HasText( microblog.DataType )) {
Type targetType = Entity.GetType( microblog.DataType );
if (targetType != null) {
ILike target = ndb.findById( targetType, microblog.DataId ) as ILike;
if (target != null) {
target.Likes = microblog.Likes;
db.update( target );
}
}
}
echoAjaxOk();
}
//------------------------------------------------------------------------------------------------
[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, ctx.Ip );
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();
}
}
}