Jump to content

Channel - Enable/Disable


bakes82

Recommended Posts

bakes82

Well that works I guess. What magic do I need to do for that?  In my code I can return an empty set if it’s not enabled in the plugin config.

Link to comment
Share on other sites

bakes82
17 minutes ago, Luke said:

That's something the server admin does manually by editing users.

That’s not magic, nor automated. Can’t you just add that in real fast. Shouldn’t it be a one liner 😛

Link to comment
Share on other sites

bakes82

How can I get a list of all the channels?

I can set the users policy stuff in the plugin also it looks like, I assume I pass a list of Channel Names to the array of EnabledChannels and Blcoked ones

 

var users = UserManager.Users;
foreach (var user in users)
{
    user.Policy.EnableAllChannels = false;
    user.Policy.BlockedChannels = new[] {Plugin.Instance.PluginConfiguration.ChannelName};
    UserManager.UpdateUser(user);
}
Link to comment
Share on other sites

bakes82

@Luke How do I get a list of all the channels? 

This needs a query .... if I say new InternalitemsQuery it thorws null exception

var channels = ChannelManager.GetChannelsInternal();
Link to comment
Share on other sites

bakes82

@chef Do you know of a way to get Channels maybe from the LibraryManager?  Seems to be the last piece I need.

 

var users = UserManager.Users;

var channels = ChannelManager.GetChannelsInternal(new InternalItemsQuery()
{
    IsFolder = true
}).Where(x => !string.IsNullOrEmpty(x.Name)).ToList();

if (string.IsNullOrEmpty(Plugin.Instance.PluginConfiguration.ChannelName))
{
    foreach (var user in users)
    {
        user.Policy.EnableAllChannels = false;

        var enabledChannels = user.Policy.EnabledChannels.ToList();
        var disabledChannels = user.Policy.BlockedChannels.ToList();

        if (!disabledChannels.Contains(Plugin.Instance.PluginConfiguration.ChannelName))
        {
            disabledChannels.Add(Plugin.Instance.PluginConfiguration.ChannelName);
        }

        if (enabledChannels.Contains(Plugin.Instance.PluginConfiguration.ChannelName))
        {
            enabledChannels.Remove(Plugin.Instance.PluginConfiguration.ChannelName);
        }
        else
        {
            foreach (var chan in channels)
            {
                if (!enabledChannels.Contains(chan.Name))
                {
                    enabledChannels.Add(chan.Name);
                }
            }
        }

        UserManager.UpdateUser(user);
    }
}
else
{
    foreach (var user in users)
    {
        user.Policy.EnableAllChannels = false;

        var enabledChannels = user.Policy.EnabledChannels.ToList();
        var disabledChannels = user.Policy.BlockedChannels.ToList();

        if (disabledChannels.Contains(Plugin.Instance.PluginConfiguration.ChannelName))
        {
            disabledChannels.Remove(Plugin.Instance.PluginConfiguration.ChannelName);
        }

        if (!enabledChannels.Contains(Plugin.Instance.PluginConfiguration.ChannelName))
        {
            enabledChannels.Add(Plugin.Instance.PluginConfiguration.ChannelName);
        }
        else
        {
            foreach (var chan in channels)
            {
                if (!enabledChannels.Contains(chan.Name))
                {
                    enabledChannels.Add(chan.Name);
                }
            }
        }

        UserManager.UpdateUser(user);
    }
}
Link to comment
Share on other sites

bakes82

Looks like this is it:

 

var channels = LibraryManager.GetItemList(new InternalItemsQuery()
{
    Recursive = true,
    IncludeItemTypes = new []{ "Channel" }
}).Where(x => !string.IsNullOrEmpty(x.Name)).ToList();
Link to comment
Share on other sites

bakes82

In case anyone else needs it, the API is horrible, IDs are GUIDs, but then other things use the IDs as strings w/no dashes.  Just ridiculous.

But you can set the users visibility to a channel on/off this way.

var users = UserManager.Users;

var channels = LibraryManager.GetItemList(new InternalItemsQuery()
{
    Recursive = true,
    IncludeItemTypes = new []{ "Channel" }
}).Where(x => !string.IsNullOrEmpty(x.Name)).ToList();

var currentChannel = channels.First(x => x.Name == Plugin.Instance.PluginConfiguration.ChannelName).Id.ToString().Replace("-", string.Empty);

if (!Plugin.Instance.PluginConfiguration.Enabled)
{
    foreach (var user in users)
    {
        user.Policy.EnableAllChannels = false;

        var enabledChannels = user.Policy.EnabledChannels != null ? user.Policy.EnabledChannels.ToList() : new List<string>();
        var disabledChannels = user.Policy.BlockedChannels != null ? user.Policy.BlockedChannels.ToList() : new List<string>();
        
        //Logger.Info("Enabled " + string.Join(",", enabledChannels.Select(x => x.ToString()).ToArray()));
        //Logger.Info("Blocked " + string.Join(",", disabledChannels.Select(x => x.ToString()).ToArray()));

        if (!disabledChannels.Contains(currentChannel))
        {
            disabledChannels.Add(currentChannel);
            //Logger.Info("Added To Disabled " + currentChannel);
        }

        if (enabledChannels.Any())
        {
            if (enabledChannels.Contains(currentChannel))
            {
                enabledChannels.Remove(currentChannel);
            }
        }
        else
        {
            foreach (var channel in channels.Where(x => x.Name != Plugin.Instance.PluginConfiguration.ChannelName))
            {
                enabledChannels.Add(channel.Id.ToString().Replace("-", string.Empty));
            }
        }

        user.Policy.EnabledChannels = enabledChannels.ToArray();
        user.Policy.BlockedChannels = disabledChannels.ToArray();
        
        //Logger.Info("Enabled " + string.Join(",", enabledChannels.Select(x => x.ToString()).ToArray()));
        //Logger.Info("Blocked " + string.Join(",", disabledChannels.Select(x => x.ToString()).ToArray()));

        UserManager.UpdateUser(user);
    }
}
else
{
    foreach (var user in users)
    {
        user.Policy.EnableAllChannels = false;

        var enabledChannels = user.Policy.EnabledChannels != null ? user.Policy.EnabledChannels.ToList() : new List<string>();
        var disabledChannels = user.Policy.BlockedChannels != null ? user.Policy.BlockedChannels.ToList() : new List<string>();
        
        //Logger.Info("Enabled " + string.Join(",", enabledChannels.Select(x => x.ToString()).ToArray()));
        //Logger.Info("Blocked " + string.Join(",", disabledChannels.Select(x => x.ToString()).ToArray()));

        if (disabledChannels.Contains(currentChannel))
        {
            disabledChannels.Remove(currentChannel);
        }
        
        if (enabledChannels.Any())
        {
            if (enabledChannels.Contains(currentChannel))
            {
                continue;
            }

            enabledChannels.Add(currentChannel);
        }
        else
        {
            foreach (var channel in channels)
            {
                enabledChannels.Add(channel.Id.ToString().Replace("-", string.Empty));
            }
        }


        user.Policy.EnabledChannels = enabledChannels.ToArray();
        user.Policy.BlockedChannels = disabledChannels.ToArray();
        
        //Logger.Info("Enabled " + string.Join(",", enabledChannels.Select(x => x.ToString()).ToArray()));
        //Logger.Info("Blocked " + string.Join(",", disabledChannels.Select(x => x.ToString()).ToArray()));

        UserManager.UpdateUser(user);
    }
}
  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...