Esempio n. 1
0
        public OutMessage Scope([Protect(UserScope.Admin)] Chat chat, User user, UserScope scope)
        {
            var me        = Context.GetMember(null, chat);
            var target    = Context.GetMember(user, chat);
            var localizer = Context.GetLocalizer();

            if (target == null)
            {
                return(OutMessage.FromCode(localizer["TargetNotFound"]));
            }
            if (!Context.GetUser().IsSuperUser &&
                (me.Scope < target.Scope ||
                 (me.Scope != UserScope.Owner &&
                  target.Scope >= UserScope.Admin &&
                  scope >= UserScope.Admin)))
            {
                return(OutMessage.FromCode(localizer["AccessDenied"]));
            }
            var db = new RepoContext();

            target.Scope = scope;
            db.Update(target);
            db.SaveChanges();
            return(OutMessage.FromCode(localizer["UserScopeUpdated"]));
        }
Esempio n. 2
0
        public async Task UpdateHostTest()
        {
            var e = new WatchEntity
            {
                Emails = Guid.NewGuid().ToString(),
                Host   = Guid.NewGuid().ToString(),
                PingIntervalSeconds = 5
            };
            await RepoContext.Insert(e);

            var e2 = new WatchEntity
            {
                WatchId             = e.WatchId,
                Emails              = Guid.NewGuid().ToString(),
                Host                = Guid.NewGuid().ToString(),
                PingIntervalSeconds = 2
            };

            await RepoContext.Update(e2);

            var e3 = await RepoContext.GetItem(e.WatchId);

            Assert.AreEqual(e.WatchId, e3.WatchId);
            Assert.AreEqual(e2.Emails, e3.Emails, "email does not match");
            Assert.AreEqual(e2.Host, e3.Host);
            Assert.AreEqual(e2.PingIntervalSeconds, e3.PingIntervalSeconds);
        }
Esempio n. 3
0
        public OutMessage Notifications([Protect(UserScope.Admin)] Chat chat, bool value)
        {
            var db        = new RepoContext();
            var localizer = Context.GetLocalizer();

            chat.Notifications = value;
            db.Update(chat);
            db.SaveChanges();

            return(OutMessage.FromCode(value ? localizer["NotificationsEnabled"] : localizer["NotificationsDisabled"]));
        }
Esempio n. 4
0
        public async Task <OutMessage> Init()
        {
            var localizer = Context.GetLocalizer();
            var chat      = Context.GetChat();
            var db        = new RepoContext();

            if (chat != null)
            {
                return(OutMessage.FromCode(localizer["ChatAlreadyInitialized"]));
            }

            chat = new Chat
            {
                Controller    = Controller.Name,
                ChatId        = Message.Chat.Id,
                Language      = Message.Sender.Language,
                Notifications = true
            };
            db.Update(chat);
            db.SaveChanges();

            var user = Context.GetUser();

            var result = $"{localizer["ChatInitialized"]}";

            var info = await Controller.GetChatInfo(chat.ChatId);

            if (info.Owner == 0)
            {
                user.AssignPermissions(chat, UserScope.Owner);
                result += $"\n{user.Username} {localizer["AssignedAsOwner"]}\n{localizer["UnableToAssignOwner"]}";
                return(OutMessage.FromCode(result));
            }

            var uinfo = await Controller.GetUserInfo(info.Owner);

            var owner = User.EnsureCreated(Controller.Name, uinfo);

            owner.AssignPermissions(chat, UserScope.Owner);

            if (owner.Id == user.Id)
            {
                result += $"\n{user.Username} {localizer["AssignedAsOwner"]}";
                return(OutMessage.FromCode(result));
            }

            user.AssignPermissions(chat, UserScope.Admin);

            result += $"\n{user.Username} {localizer["AssignedAsAdmin"]}\n{owner.Username} {localizer["AssignedAsOwner"]}";
            return(OutMessage.FromCode(result));
        }
Esempio n. 5
0
        // public void Info(User user = null)
        //     => this.EnterView("user", user ?? Context.GetUser());

        public OutMessage Mute(Chat chat = null)
        {
            var me        = Context.GetMember(null, chat);
            var localizer = Context.GetLocalizer();

            if (me.Mute > MuteState.None)
            {
                return(OutMessage.FromCode(localizer["YouAlreadyMuted"]));
            }
            var db = new RepoContext();

            me.Mute = MuteState.Muted;
            db.Update(me);
            db.SaveChanges();
            return(OutMessage.FromCode(localizer["YouMuted"]));
        }
Esempio n. 6
0
        public OutMessage Unmute([Protect(UserScope.Moderator)] Chat chat, User user)
        {
            var localizer = Context.GetLocalizer();
            var target    = Context.GetMember(user, chat);

            if (target == null)
            {
                return(OutMessage.FromCode(localizer["TargetNotFound"]));
            }

            var db = new RepoContext();

            target.Mute = MuteState.None;
            db.Update(target);
            db.SaveChanges();
            return(OutMessage.FromCode(localizer["UserUnmuted"]));
        }
Esempio n. 7
0
        public OutMessage Unmute(Chat chat = null)
        {
            var me        = Context.GetMember(null, chat);
            var localizer = Context.GetLocalizer();

            if (me.Mute == MuteState.Blocked)
            {
                return(OutMessage.FromCode(localizer["CantUnmute"]));
            }

            if (me.Mute == MuteState.None)
            {
                return(OutMessage.FromCode(localizer["NotMuted"]));
            }

            var db = new RepoContext();

            me.Mute = MuteState.None;
            db.Update(me);
            db.SaveChanges();
            return(OutMessage.FromCode(localizer["YouUnmuted"]));
        }
Esempio n. 8
0
        public OutMessage CreateChat(string controller, long chatId, User owner = null)
        {
            var db = new RepoContext();

            if (db.Chats.Any(x => x.ChatId == chatId && x.Controller == controller))
            {
                return(OutMessage.FromCode("Already exists"));
            }
            owner ??= Context.GetUser();
            var chat = new Chat {
                Controller = controller, ChatId = chatId
            };
            var member = new ChatMember
            {
                Scope = UserScope.Owner,
                User  = owner,
                Chat  = chat
            };

            db.Update(member);
            db.SaveChanges();
            return(OutMessage.FromCode("Chat created " + chat.Id));
        }