public void ChangeDbBan(
            int AdminID,
            int UserID,
            int Level,
            long Expires,
            string Reason,
            int ChannelMessageID = 0,
            int ReasonMessageID  = 0
            )
        {
            BanUser baninfo = new BanUser
            {
                UserID           = UserID,
                Ban              = 0,
                Level            = Level,
                Reason           = Reason,
                HistoryID        = 0,
                ChannelMessageID = ChannelMessageID,
                ReasonMessageID  = ReasonMessageID,
                Expires          = Expires
            };

            Temp.bannedUsers[UserID] = baninfo;
            BanHistory banHistory = new BanHistory
            {
                UserID           = UserID,
                Ban              = 0,
                Level            = Level,
                ChannelMessageID = ChannelMessageID,
                ReasonMessageID  = ReasonMessageID,
                AdminID          = AdminID,
                BanTime          = GetTime.GetUnixTime(),
                Reason           = Reason,
                Expires          = Expires
            };

            using (var db = new BlacklistDatabaseContext())
            {
                db.BanHistorys.Add(banHistory);
                try
                {
                    db.BanUsers.Add(baninfo);
                    db.SaveChanges();
                }
                catch (SqliteException)
                {
                    db.BanUsers.Update(baninfo);
                    db.SaveChanges();
                }
                catch (DbUpdateException)
                {
                    db.BanUsers.Update(baninfo);
                    db.SaveChanges();
                }
            }

            new SubscribeBanListCaller().CallGroupsInThread(baninfo);
        }
        public bool UnbanUser(
            int AdminID,
            int UserID,
            string Reason     = null,
            UserInfo userinfo = null
            )
        {
            int ChannelReasonID = 0;

            if (Temp.MainChannelID != 0)
            {
                string banmsg = "";
                if (userinfo == null)
                {
                    UserInfoRequest userinforeq = TgApi.getDefaultApiConnection().getChat(UserID);
                    if (userinforeq.ok)
                    {
                        userinfo = userinforeq.result;
                        banmsg   = userinfo.GetUserTextInfo();
                    }
                    else
                    {
                        banmsg = "User ID : " + UserID;
                    }
                }
                else
                {
                    banmsg = userinfo.GetUserTextInfo();
                }

                banmsg += "\n\n已被解除封鎖";

                if (Reason != null)
                {
                    banmsg += ",原因 : \n" + Reason;
                }

                banmsg += "\n原封鎖原因 : \n" + Temp.GetDatabaseManager().GetUserBanStatus(UserID).Reason + "\n";

                banmsg += "\nOID : " + AdminID + "\n";

                BanUser ban = Temp.GetDatabaseManager().GetUserBanStatus(UserID);
                if (ban.Ban == 1)
                {
                    return(false);
                }

                ChannelReasonID = TgApi.getDefaultApiConnection().sendMessage(Temp.MainChannelID, banmsg).result
                                  .message_id;
            }

            ChangeDbUnban(AdminID, UserID, Reason, ChannelReasonID);
            CNBlacklistApi.PostToAPI(UserID, false, 1, 0, Reason);
            return(true);
        }
        public BanUser GetUserBanStatus(int uid)
        {
            BanUser banuser = null;

            banuser = Temp.bannedUsers.GetValueOrDefault(uid, null);
            if (banuser != null)
            {
                if (GetTime.GetIsExpired(banuser.Expires))
                {
                    banuser.Ban           = 1;
                    Temp.bannedUsers[uid] = banuser;
                }

                return(banuser);
            }

            using (var db = new BlacklistDatabaseContext())
            {
                BanUser bannedUser;
                try
                {
                    bannedUser = db.BanUsers
                                 .Single(users => users.UserID == uid);
                    Temp.bannedUsers.TryAdd(uid, bannedUser);
                }
                catch (InvalidOperationException)
                {
                    bannedUser = new BanUser {
                        Ban = 1
                    };
                    Temp.bannedUsers.TryAdd(uid, bannedUser);
                    return(bannedUser);
                }

                if (GetTime.GetIsExpired(bannedUser.Expires))
                {
                    bannedUser.Ban = 1;
                    db.BanUsers.Update(bannedUser);
                }

                return(bannedUser);
            }
        }
        private void ChangeBanTemp(
            int AdminID,
            int UserID,
            int Level,
            long Expires,
            string Reason)
        {
            BanUser baninfo = new BanUser
            {
                UserID           = UserID,
                Ban              = 0,
                Level            = Level,
                Reason           = Reason,
                HistoryID        = 0,
                ChannelMessageID = 0,
                ReasonMessageID  = 0,
                Expires          = Expires
            };

            using (var db = new BlacklistDatabaseContext())
            {
                try
                {
                    db.BanUsers.Add(baninfo);
                    db.SaveChanges();
                }
                catch (SqliteException)
                {
                    db.BanUsers.Update(baninfo);
                    db.SaveChanges();
                }
                catch (DbUpdateException)
                {
                    db.BanUsers.Update(baninfo);
                    db.SaveChanges();
                }
            }
        }