public ActionResult SendGroupMessage(string Token, int GroupID, string Message) { var user = CheckUser(Token); if (user == null) { return(Json(new Base { Code = 500, IsSuccess = false, Info = "AccessToken不正确" })); } var group = DbContext.Groups.Find(GroupID); if ((from gm in @group.GroupMembers where gm.UserID == user.ID select gm).Count() == 0) { return(Json(new Base { Code = 808, IsSuccess = false, Info = "您不是该群的成员" })); } var groupchat = new Entity.GroupChat { GroupID = GroupID, Message = Message, Time = DateTime.Now, UserID = user.ID }; DbContext.GroupChats.Add(groupchat); DbContext.SaveChanges(); var group_users = (from u in DbContext.Users where (from g in DbContext.GroupMembers where g.ID == GroupID && g.UserID == u.ID select g).Count() > 0 select u).ToList(); foreach (var u in group_users) { SignalR.MobileHub.context.Clients.Client(u.Username).onGroupMessage(new GroupChat { ID = groupchat.ID, AvatarURL = Helpers.Gravatar.GetAvatarURL(groupchat.User.Gravatar, 180), Message = groupchat.Message, Time = groupchat.Time, GroupID = groupchat.GroupID, Nickname = groupchat.User.Nickname, UserID = groupchat.UserID }); //Todo: Push to web SignalR.MobileHub.PushTo(u.ID, groupchat.Group.Title + ": " + Message); } return(Json(new Base { Code = 0, IsSuccess = true, Info = "" })); }
public ActionResult SendGroupMessage(string Token, int GroupID, string Message) { var user = CheckUser(Token); if (user == null) return Json(new Base { Code = 500, IsSuccess = false, Info = "AccessToken不正确" }); var group = DbContext.Groups.Find(GroupID); if ((from gm in @group.GroupMembers where gm.UserID == user.ID select gm).Count() == 0) return Json(new Base { Code = 808, IsSuccess = false, Info = "您不是该群的成员" }); var groupchat = new Entity.GroupChat { GroupID = GroupID, Message = Message, Time = DateTime.Now, UserID = user.ID }; DbContext.GroupChats.Add(groupchat); DbContext.SaveChanges(); var group_users = (from u in DbContext.Users where (from g in DbContext.GroupMembers where g.ID == GroupID && g.UserID == u.ID select g).Count() > 0 select u).ToList(); foreach (var u in group_users) { SignalR.MobileHub.context.Clients.Client(u.Username).onGroupMessage(new GroupChat { ID = groupchat.ID, AvatarURL = Helpers.Gravatar.GetAvatarURL(groupchat.User.Gravatar, 180), Message = groupchat.Message, Time = groupchat.Time, GroupID = groupchat.GroupID, Nickname = groupchat.User.Nickname, UserID = groupchat.UserID }); //Todo: Push to web SignalR.MobileHub.PushTo(u.ID, groupchat.Group.Title + ": " + Message); } return Json(new Base { Code = 0, IsSuccess = true, Info = "" }); }