Пример #1
0
        /// <summary>
        /// 公告详情
        /// </summary>
        /// <param name="c"></param>
        /// <param name="request"></param>
        public int Call_Info(BulletinRequest request)
        {
            BulletinResponse response = new BulletinResponse();

            response.success = true;
            CurrentSession.SendAsync(response);
            return(0);
        }
        public JsonResult CreateBulletin(BulletinRequest request)
        {
            var checkUsername = CheckUsername(request.username);

            if (checkUsername != null)
            {
                return(checkUsername);
            }

            if (!CornerCaseCheckHelper.Check(request.bulletinName, 50, CornerCaseCheckHelper.Title))
            {
                return(JsonReturnHelper.ErrorReturn(402, "Invalid bulletinName."));
            }

            if (!CornerCaseCheckHelper.Check(request.description, 100, CornerCaseCheckHelper.Description))
            {
                return(JsonReturnHelper.ErrorReturn(403, "Invalid Description."));
            }

            if (!CornerCaseCheckHelper.Check(request.projectId, 0, CornerCaseCheckHelper.Id))
            {
                return(JsonReturnHelper.ErrorReturn(401, "Invalid projectId."));
            }

            var project = _meshContext.Projects.FirstOrDefault(u => u.Id == request.projectId);

            if (project == null)
            {
                return(JsonReturnHelper.ErrorReturn(411, "Project does not exist."));
            }

            //Find bulletinBoard of this project
            var bulletinBoard = _meshContext.BulletinBoards.First(b => b.ProjectId == request.projectId);
            var user          = _meshContext.Users.First(u => u.Email == request.username);

            if (_permissionCheck.CheckProjectPermission(request.username, project) != PermissionCheckHelper.ProjectAdmin)
            {
                return(JsonReturnHelper.ErrorReturn(421, "Permission denied."));
            }

            //Check if the bulletin already exists.
            var bulletin =
                _meshContext.Bulletins.FirstOrDefault(b => b.Title == request.bulletinName && b.BoardId == bulletinBoard.Id);

            if (bulletin != null)
            {
                return(JsonReturnHelper.ErrorReturn(411, "Bulletin already exists."));
            }

            //Create the bulletin
            var newBulletin = new Bulletin()
            {
                Title   = request.bulletinName,
                Content = request.description,
                BoardId = bulletinBoard.Id
            };

            //Update feed
            var feedUsers = _meshContext.Develops
                            .Where(d => d.ProjectId == request.projectId)
                            .ToList();

            //Start Transaction to save the bulletin
            using (var transaction = _meshContext.Database.BeginTransaction())
            {
                try
                {
                    _meshContext.Bulletins.Add(newBulletin);
                    _meshContext.SaveChanges();

                    var bulletinId = newBulletin.Id;
                    var feedList   = new List <BulletinFeed>();
                    foreach (var feedUser in feedUsers)
                    {
                        feedList.Add(new BulletinFeed()
                        {
                            UserId     = feedUser.UserId,
                            BulletinId = bulletinId
                        });
                    }

                    _meshContext.AddRange(feedList);
                    _meshContext.SaveChanges();
                    transaction.Commit();
                }
                catch (Exception e)
                {
                    _logger.LogError(e.ToString());
                    return(JsonReturnHelper.ErrorReturn(1, "Unexpected error."));
                }
            }

            return(Json(new
            {
                err_code = 0,
                isSuccess = true,
                msg = "",
                bulletin = new
                {
                    bulletinId = newBulletin.Id,
                    bullentinName = newBulletin.Title,
                    description = newBulletin.Content,
                    createTime = newBulletin.CreatedTime
                }
            }));
        }
        public JsonResult UpdateBulletin(BulletinRequest request)
        {
            var checkUsername = CheckUsername(request.username);

            if (checkUsername != null)
            {
                return(checkUsername);
            }

            if (!CornerCaseCheckHelper.Check(request.bulletinId, 0, CornerCaseCheckHelper.Id))
            {
                return(JsonReturnHelper.ErrorReturn(401, "Invalid bulletinId."));
            }

            if (!CornerCaseCheckHelper.Check(request.bulletinName, 50, CornerCaseCheckHelper.Title))
            {
                return(JsonReturnHelper.ErrorReturn(402, "Invalid bulletinName."));
            }

            if (!CornerCaseCheckHelper.Check(request.description, 100, CornerCaseCheckHelper.Description))
            {
                return(JsonReturnHelper.ErrorReturn(403, "Invalid Description."));
            }

            if (!CornerCaseCheckHelper.Check(request.projectId, 0, CornerCaseCheckHelper.Id))
            {
                return(JsonReturnHelper.ErrorReturn(401, "Invalid projectId."));
            }

            //Check if target bulletin exists
            var bulletin = _meshContext.Bulletins.Find(request.bulletinId);
            var project  = _meshContext.Projects.Find(request.projectId);
            var user     = _meshContext.Users.First(u => u.Email == request.username);

            if (bulletin == null || project == null)
            {
                return(JsonReturnHelper.ErrorReturn(420, "Invalid bulletinId or projectId"));
            }

            //Check if the user is admin of the project
            if (project.AdminId != user.Id)
            {
                return(JsonReturnHelper.ErrorReturn(422, "Permission denied."));
            }

            try
            {
                bulletin.Title   = request.bulletinName;
                bulletin.Content = request.description;
                _meshContext.Bulletins.Update(bulletin);
                _meshContext.SaveChanges();
            }
            catch (Exception e)
            {
                _logger.LogError(e.ToString());
                return(JsonReturnHelper.ErrorReturn(1, "Unexpected error."));
            }

            return(Json(new
            {
                err_code = 0,
                masg = "",
                data = new
                {
                    bulletinId = bulletin.Id,
                    bulletinName = bulletin.Title,
                    description = bulletin.Content,
                    createTime = bulletin.CreatedTime
                }
            }));
        }
Пример #4
0
        public void Handle(IVoltronSession session, BulletinRequest message)
        {
            if (session.IsAnonymous)  //CAS users can't do this.
            {
                return;
            }

            try
            {
                using (var da = DA.Get())
                {
                    switch (message.Type)
                    {
                    case BulletinRequestType.GET_MESSAGES:
                        //when a user logs in they will send this request to recieve all messages after their last recieved message.
                    {
                        var msgs = da.BulletinPosts.GetByNhoodId(message.TargetNHood, 0);
                        session.Write(new BulletinResponse()
                            {
                                Type     = BulletinResponseType.MESSAGES,
                                Messages = msgs.Select(x => ToItem(x)).ToArray()
                            });
                        return;
                    }

                    case BulletinRequestType.DELETE_MESSAGE:
                        //delete bulletin message
                    {
                        //check if either we own this post or we're admin
                        var post = da.BulletinPosts.Get(message.Value);
                        if (post == null)
                        {
                            session.Write(Code(BulletinResponseType.FAIL_MESSAGE_DOESNT_EXIST));
                            return;         //doesn't exist
                        }
                        if (post.avatar_id != session.AvatarId)
                        {
                            //not the owner of this post. are we an admin?
                            var myAva = da.Avatars.Get(session.AvatarId);
                            if (myAva == null || myAva.moderation_level == 0)
                            {
                                session.Write(Code(BulletinResponseType.FAIL_CANT_DELETE));
                                return;
                            }
                        }

                        da.BulletinPosts.SoftDelete(message.Value);
                        session.Write(Code(BulletinResponseType.SUCCESS));
                        return;
                    }

                    case BulletinRequestType.PROMOTE_MESSAGE:
                        //promote message to the mayor channel
                    {
                        //check if either we're mayor of this post's nhood or we're admin
                        //also the post should like, exist.
                        var post = da.BulletinPosts.Get(message.Value);
                        if (post == null)
                        {
                            session.Write(Code(BulletinResponseType.FAIL_MESSAGE_DOESNT_EXIST));
                            return;
                        }
                        if ((post.flags & 1) > 0)
                        {
                            session.Write(Code(BulletinResponseType.FAIL_ALREADY_PROMOTED));
                            return;         //already promoted.
                        }
                        if (post.type != DbBulletinType.community)
                        {
                            session.Write(Code(BulletinResponseType.FAIL_BAD_PERMISSION));
                            return;
                        }
                        var postNhood = da.Neighborhoods.Get((uint)post.neighborhood_id);
                        if (postNhood == null || postNhood.mayor_id != session.AvatarId)
                        {
                            session.Write(Code(BulletinResponseType.FAIL_NOT_MAYOR));
                            return;         //no permission
                        }
                        post.flags |= 1;

                        da.BulletinPosts.SetTypeFlag(message.Value, DbBulletinType.mayor, (int)post.flags);
                        session.Write(Code(BulletinResponseType.SUCCESS));
                        return;
                    }

                    case BulletinRequestType.CAN_POST_MESSAGE:
                    case BulletinRequestType.CAN_POST_SYSTEM_MESSAGE:
                    case BulletinRequestType.POST_SYSTEM_MESSAGE:
                    case BulletinRequestType.POST_MESSAGE:
                    {
                        var type  = (message.Type == BulletinRequestType.POST_SYSTEM_MESSAGE) ? DbBulletinType.system : DbBulletinType.community;
                        var myAva = da.Avatars.Get(session.AvatarId);
                        if (myAva == null)
                        {
                            session.Write(Code(BulletinResponseType.FAIL_UNKNOWN));
                            return;
                        }

                        var now = Epoch.Now;
                        if (now - myAva.move_date < MOVE_LIMIT_PERIOD && myAva.moderation_level == 0)
                        {
                            session.Write(Code(BulletinResponseType.SEND_FAIL_JUST_MOVED)); return;
                        }

                        var   myLotID = da.Roommates.GetAvatarsLots(session.AvatarId)?.FirstOrDefault();
                        DbLot myLot   = (myLotID != null) ? da.Lots.Get(myLotID.lot_id) : null;

                        if (myLot == null || myLot.neighborhood_id != message.TargetNHood ||
                            message.Type == BulletinRequestType.POST_SYSTEM_MESSAGE || message.Type == BulletinRequestType.CAN_POST_SYSTEM_MESSAGE)
                        {
                            //need to live in this nhood to post
                            //if we're an admin we can ignore this
                            if (myAva.moderation_level == 0)
                            {
                                session.Write(Code(BulletinResponseType.SEND_FAIL_NON_RESIDENT));
                                return;
                            }
                        }

                        var postNhood = da.Neighborhoods.Get((uint)message.TargetNHood);
                        if (postNhood == null)
                        {
                            session.Write(Code(BulletinResponseType.FAIL_UNKNOWN));
                            return;
                        }
                        if (session.AvatarId == postNhood.mayor_id && type == DbBulletinType.community)
                        {
                            type = DbBulletinType.mayor;
                        }

                        //are we nhood gameplay banned?

                        var ban = da.Neighborhoods.GetNhoodBan(myAva.user_id);
                        if (ban != null)
                        {
                            session.Write(new BulletinResponse
                                {
                                    Type       = BulletinResponseType.SEND_FAIL_GAMEPLAY_BAN,
                                    Message    = ban.ban_reason,
                                    BanEndDate = ban.end_date
                                });
                            return;
                        }

                        //verify post frequency
                        var last      = da.BulletinPosts.LastUserPost(myAva.user_id, message.TargetNHood);
                        int frequency = 0;
                        switch (type)
                        {
                        case DbBulletinType.mayor:
                            frequency = POST_FREQ_LIMIT_MAYOR; break;

                        case DbBulletinType.community:
                            frequency = POST_FREQ_LIMIT; break;
                        }
                        if (Epoch.Now - (last?.date ?? 0) < frequency)
                        {
                            session.Write(Code(BulletinResponseType.SEND_FAIL_TOO_FREQUENT));
                            return;
                        }

                        if (message.Type == BulletinRequestType.CAN_POST_MESSAGE || message.Type == BulletinRequestType.CAN_POST_SYSTEM_MESSAGE)
                        {
                            session.Write(Code(BulletinResponseType.SUCCESS));
                            return;
                        }

                        int?lotID = null;
                        if (message.LotID != 0)
                        {
                            //verify the lot ID if one is included
                            var lot = da.Lots.GetByLocation(Context.ShardId, message.LotID);
                            if (lot == null)
                            {
                                session.Write(Code(BulletinResponseType.SEND_FAIL_INVALID_LOT));
                                return;
                            }
                            lotID = (int)message.LotID;
                        }

                        if (message.Message.Length == 0 || message.Message.Length > 1000)
                        {
                            session.Write(Code(BulletinResponseType.SEND_FAIL_INVALID_MESSAGE));
                            return;
                        }
                        if (message.Title.Length == 0 || message.Title.Length > 64)
                        {
                            session.Write(Code(BulletinResponseType.SEND_FAIL_INVALID_TITLE));
                            return;
                        }

                        var db = new DbBulletinPost()
                        {
                            avatar_id       = session.AvatarId,
                            date            = Epoch.Now,
                            title           = message.Title,
                            body            = message.Message,
                            flags           = 0,
                            lot_id          = lotID,
                            neighborhood_id = (int)message.TargetNHood,
                            type            = type
                        };
                        try
                        {
                            db.bulletin_id = da.BulletinPosts.Create(db);
                        }
                        catch (Exception e)
                        {
                            LOG.Error(e.ToString());
                            session.Write(Code(BulletinResponseType.FAIL_UNKNOWN));
                            return;
                        }

                        session.Write(new BulletinResponse()
                            {
                                Type     = BulletinResponseType.SEND_SUCCESS,
                                Messages = new BulletinItem[] { ToItem(db) }
                            });
                    }
                    break;
                    }
                }
            }
            catch (Exception e)
            {
                LOG.Error(e.ToString());
                session.Write(Code(BulletinResponseType.FAIL_UNKNOWN));
                return;
            }
        }