/// <summary>
 /// Updates or create the user to channel.
 /// </summary>
 /// <param name="iApplication">The application.</param>
 /// <param name="iUser">The user.</param>
 /// <param name="iChannel">The channel.</param>
 /// <returns>The user to channel.</returns>
 public static Entites.UserToChannel UpdateOrCreate(IApplication iApplication, IUser iUser, IChannel iChannel)
 {
     Entites.UserToChannel userToChannel = Get(iUser, iChannel);
     return(userToChannel == null
         ? Create(iApplication, iUser, iChannel)
         : Update(iUser, iChannel, userToChannel));
 }
 /// <summary>
 /// Updates the user to channel with the unique parameters.
 /// </summary>
 /// <param name="iUser">The user.</param>
 /// <param name="iChannel">The channel.</param>
 /// <param name="userToChannel">The user to channel to update.</param>
 /// <returns>The same user to channel updated with the two other arguements' properties.</returns>
 public static Entites.UserToChannel Update(IUser iUser, IChannel iChannel, Entites.UserToChannel userToChannel)
 {
     DAL.UserToChannel.LoadUser(DAL.UserToChannel.LoadChannel(userToChannel));
     userToChannel.User    = User.Update(iUser, userToChannel.User);
     userToChannel.Channel = Channel.Update(iChannel, userToChannel.Channel);
     return(userToChannel);
 }
        /// <summary>
        /// Checks and extracts the GivePoints arguements.
        /// </summary>
        /// <param name="command">The command</param>
        /// <returns>An error message. Null if none.</returns>
        private static string GivePointsAnalyzes(Command command)
        {
            string[] arguements          = command.SplitArguements(' ', 2);
            Entites.UserToChannel sender = command.Message.UserToChannel;

            if (!float.TryParse(arguements[1], out float amount))
            {
                return("Is that supposed to be a number?");
            }
            if (sender.Points < amount)
            {
                return("You only have " + sender.Points + " points. You can't give " + amount + "!");
            }
            if (amount <= 0)
            {
                return("You can't bill people; positive numbers only!");
            }

            var receiver = DAL.User.LoadChannels(
                (Entites.User)
                UserName.GetFromUsername(arguements[0].Substring(1), sender.ApplicationName)?.OwnedBy)
                           .Channels.FirstOrDefault(c => c.ChannelId == sender.ChannelId);

            if (receiver == null)
            {
                return("No one with the username " + arguements[0] + " known.");
            }

            command.Message.UserToChannel.Points -= amount;
            receiver.Points += amount;
            Update(command.Message.UserToChannel);
            Update(receiver);

            return("You gave @" + arguements[0].Substring(1) + " " + arguements[1] + " points.");
        }
 /// <summary>
 /// Counts the number of messages a user sent in a channel.
 /// </summary>
 /// <param name="userToChannelId">The user to channel identifier.</param>
 /// <returns>The number of messages a user sent in a channel</returns>
 public static int CountMessage(int userToChannelId)
 {
     using (TerministratorContext context = new TerministratorContext(true))
     {
         Entites.UserToChannel userToChannel = context.UserToChannel.Find(userToChannelId);
         return(userToChannel == null ? 0 : LoadMessage(userToChannel).Messages.Count);
     }
 }
 /// <summary>
 /// Loads the user reference.
 /// </summary>
 /// <param name="userToChannel">The user to channel.</param>
 /// <returns>The user to channel with the initialized user reference.</returns>
 public static Entites.UserToChannel LoadUser(Entites.UserToChannel userToChannel)
 {
     using (TerministratorContext context = new TerministratorContext(true))
     {
         context.UserToChannel.Attach(userToChannel);
         context.Entry(userToChannel).Reference(p => p.User).Load();
     }
     return(userToChannel);
 }
 /// <summary>
 /// Adds the references of the second arguement in the first one.
 /// </summary>
 /// <param name="userToChannel">The user to channel to add the references in.</param>
 /// <param name="reference">The references.</param>
 /// <returns>The first arguement.</returns>
 private static Entites.UserToChannel AddReferences(Entites.UserToChannel userToChannel,
                                                    Entites.UserToChannel reference)
 {
     userToChannel.Application = reference.Application;
     userToChannel.User        = reference.User;
     userToChannel.Channel     = reference.Channel;
     userToChannel.Privileges  = reference.Privileges;
     return(userToChannel);
 }
Exemple #7
0
 /// <summary>
 /// Creates a new message for a user, in a channel, with a specified text (not in the database).
 /// </summary>
 /// <param name="text">The text.</param>
 /// <param name="userToChannel">The user to channel.</param>
 /// <returns>The newly created message.</returns>
 public static Entites.Message Create(string text, Entites.UserToChannel userToChannel)
 {
     Entites.Message message = new Entites.Message(userToChannel.Application, null, DateTime.Now, userToChannel,
                                                   null);
     message.Texts = new List <Entites.Text> {
         new Entites.Text(text, message.SentOn, message)
     };
     return(message);
 }
 /// <summary>
 /// Creates the specified user to channel in the database.
 /// </summary>
 /// <param name="userToChannel">The user to channel.</param>
 /// <returns>The user to channel witn an updated ID.</returns>
 public static Entites.UserToChannel Create(Entites.UserToChannel userToChannel)
 {
     Entites.UserToChannel reference = ClearReferences(userToChannel);
     using (TerministratorContext context = new TerministratorContext(true))
     {
         userToChannel.UserToChannelId = context.UserToChannel.Add(userToChannel).UserToChannelId;
         context.SaveChanges();
     }
     return(AddReferences(userToChannel, reference));
 }
 /// <summary>
 /// Clears the references of the user to channel.
 /// </summary>
 /// <param name="userToChannel">The user to channel.</param>
 /// <returns>A copy of the user to channel given in entry with only the references.</returns>
 private static Entites.UserToChannel ClearReferences(Entites.UserToChannel userToChannel)
 {
     Entites.UserToChannel reference = new Entites.UserToChannel(userToChannel.Application, userToChannel.User,
                                                                 userToChannel.Channel, DateTime.MinValue, userToChannel.Privileges);
     userToChannel.Application = null;
     userToChannel.User        = null;
     userToChannel.Channel     = null;
     userToChannel.Privileges  = null;
     return(reference);
 }
 /// <summary>
 /// Loads the message collection.
 /// </summary>
 /// <param name="userToChannel">The user to channel.</param>
 /// <returns>The user to channel with the initialized message collection.</returns>
 public static Entites.UserToChannel LoadMessage(Entites.UserToChannel userToChannel)
 {
     using (TerministratorContext context = new TerministratorContext(true))
     {
         userToChannel.Messages = (from c in context.Message
                                   where c.UserToChannelId == userToChannel.UserToChannelId
                                   select c).ToList();
     }
     return(userToChannel);
 }
Exemple #11
0
 /// <summary>
 /// Sends a warning message to the mods about someone whom should be kicked.
 /// </summary>
 /// <param name="userToChannel">The user to channel to warn about.</param>
 private static void SendWarningMessages(Entites.UserToChannel userToChannel)
 {
     foreach (IUser user in userToChannel.Application.GetMods(DAL.UserToChannel.LoadChannel(userToChannel).Channel))
     {
         Entites.Channel c = Channel.GetPrivateChannel(User.GetOrCreate(user));
         if (c != null)
         {
             userToChannel.Application.SendMessage(Message.Create("Warning!", userToChannel));
         }
     }
 }
Exemple #12
0
 /// <summary>
 /// Kicks the specified user from the channel if possible. Otherwise warn the mods.
 /// </summary>
 /// <param name="userToChannel">The user to channel.</param>
 private static void Kick(Entites.UserToChannel userToChannel)
 {
     if (userToChannel.Application.CanKick(userToChannel.Channel))
     {
         userToChannel.Application.Kick(userToChannel.User, userToChannel.Channel);
     }
     else
     {
         SendWarningMessages(userToChannel);
     }
 }
 /// <summary>
 /// Updates the specified user to channel in the database.
 /// </summary>
 /// <param name="userToChannel">The user to channel.</param>
 /// <returns>The same user to channel that was given in entry.</returns>
 public static Entites.UserToChannel Update(Entites.UserToChannel userToChannel)
 {
     using (TerministratorContext context = new TerministratorContext(true))
     {
         Entites.UserToChannel original = context.UserToChannel.Find(userToChannel.UserToChannelId);
         if (original != null)
         {
             original.Points       = userToChannel.Points;
             original.PrivilegesId = userToChannel.PrivilegesId;
             original.NbSilences   = userToChannel.NbSilences;
             original.SilencedTo   = userToChannel.SilencedTo;
             context.SaveChanges();
         }
     }
     return(userToChannel);
 }
 /// <summary>
 /// Loads the privileges reference.
 /// </summary>
 /// <param name="userToChannel">The user to channel.</param>
 /// <returns>The user to channel with the initialized privileges reference.</returns>
 public static Entites.UserToChannel LoadPrivileges(Entites.UserToChannel userToChannel)
 {
     using (TerministratorContext context = new TerministratorContext(true))
     {
         if (userToChannel.Privileges != null)
         {
             userToChannel.Privileges = null;
         }
         if (context.Entry(userToChannel).State == EntityState.Detached)
         {
             context.UserToChannel.Attach(userToChannel);
         }
         context.Entry(userToChannel).Reference(p => p.Privileges).Load();
     }
     return(userToChannel);
 }
        private static string SetPrivilegesAnalyzes(Command command)
        {
            if (!Tools.IsMod(command.Message.UserToChannel))
            {
                return("This command is reserved for moderators.");
            }

            string[] arguements = command.Arguement.Split(new[] { ' ' }, 2);
            string   user;
            string   privilegesName;

            if (arguements.Length > 1)
            {
                user           = arguements[0].Substring(1);
                privilegesName = arguements[1];
            }
            else
            {
                user           = command.Message.UserToChannel.User.CurrentUserName.Username;
                privilegesName = arguements[0];
            }

            Entites.UserToChannel userToChannel =
                Get(DAL.UserName.LoadOwnedBy(DAL.UserName.GetFromUsername(user, command.Message.ApplicationName))
                    .OwnedBy as Entites.User, command.Message.UserToChannel.Channel);
            if (userToChannel == null)
            {
                return($"No one nammed @{user} was found.");
            }

            Entites.Privileges privileges = Privileges.GetPrivileges(command.Message.UserToChannel.Channel, privilegesName);
            if (privileges == null)
            {
                return($"No privileges group nammed {privilegesName} was found.");
            }

            userToChannel.PrivilegesId = privileges.PrivilegesId;
            Update(userToChannel);
            return($"Your privileges group is now set to {privilegesName}.");
        }
Exemple #16
0
 /// <summary>
 /// Determines whether the specified user to channel is mod.
 /// </summary>
 /// <param name="userToChannel">The user to channel.</param>
 /// <returns>
 ///   <c>true</c> if the specified user to channel is mod; otherwise, <c>false</c>.
 /// </returns>
 public static bool IsMod(Entites.UserToChannel userToChannel)
 {
     return
         (userToChannel.Application.GetMods(userToChannel.Channel)
          .Any(x => x.ApplicationId == userToChannel.User.ApplicationId));
 }
 /// <summary>
 /// Gets the message sent by the user in that channel just before the requested date.
 /// </summary>
 /// <param name="userToChannel">The user to channel.</param>
 /// <param name="date">The date.</param>
 /// <returns>The requested message.</returns>
 public static Entites.Message GetMessageBefore(Entites.UserToChannel userToChannel, DateTime date)
 {
     return(DAL.UserToChannel.GetMessageBefore(userToChannel.UserToChannelId, date));
 }
 /// <summary>
 /// Tells if the user to channel exists in the database.
 /// </summary>
 /// <param name="userToChannel">The user identifier.</param>
 /// <returns><c>true</c> if the UserToChannel exist; otherwise <c>false</c>.</returns>
 public static bool Exists(Entites.UserToChannel userToChannel) =>
 Exists(userToChannel.User.IdForApplication, userToChannel.Channel.IdForApplication,
        userToChannel.Channel.Application.ApplicationName);
 /// <summary>
 /// Updates the specified user to channel.
 /// </summary>
 /// <param name="userToChannel">The user to channel.</param>
 /// <returns>The same user to channel.</returns>
 public static Entites.UserToChannel Update(Entites.UserToChannel userToChannel)
 {
     return(DAL.UserToChannel.Update(userToChannel));
 }