Пример #1
0
        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (e.Parameter != null)
            {
                var auditlog = await RESTCalls.GetAuditLog(e.Parameter.ToString());

                LoadingRing.Visibility = Visibility.Collapsed;
                if (auditlog == null)
                {
                    //TODO Add error message
                }
                else
                {
                    Dictionary <string, AuditLogUser> users    = new Dictionary <string, AuditLogUser>();
                    Dictionary <string, Webhook>      webhooks = new Dictionary <string, Webhook>();
                    List <User> usersForMD = new List <User>();

                    if (auditlog.Users != null)
                    {
                        foreach (var user in auditlog.Users)
                        {
                            if (!users.ContainsKey(user.Id))
                            {
                                users.Add(user.Id, user);
                                usersForMD.Add(new User()
                                {
                                    Avatar = user.Avatar, Username = user.Username, Id = user.Id, Discriminator = user.Discriminator,
                                });
                            }
                        }
                    }
                    if (auditlog.Webhooks != null)
                    {
                        foreach (var webhook in auditlog.Webhooks)
                        {
                            webhooks.Add(webhook.Id, webhook);
                        }
                    }
                    if (auditlog.AuditLogEntries != null)
                    {
                        List <SimpleAuditLogEntry> RawEntries = new List <SimpleAuditLogEntry>();
                        foreach (var entry in auditlog.AuditLogEntries)
                        {
                            SimpleAuditLogEntry se = new SimpleAuditLogEntry();
                            se.Action = (AuditLogActionType)entry.ActionType;
                            AuditLogAction action = new AuditLogAction(se.Action, e.Parameter.ToString(), entry.TargetId, users, webhooks, entry.Changes, entry.Options, entry.Reason);
                            string         avatar = "";
                            if (entry.UserId != null)
                            {
                                if (users.ContainsKey(entry.UserId))
                                {
                                    se.Avatar = Common.AvatarString(users[entry.UserId].Avatar,
                                                                    users[entry.UserId].Username);
                                }
                            }

                            se.Glyph = action.Glyph;
                            se.Brush = (SolidColorBrush)Application.Current.Resources[action.Color];
                            se.Users = usersForMD;
                            if (action.Text != null)
                            {
                                se.Text = action.Text.Replace("<user>", "<@" + entry.UserId + ">");
                            }
                            se.Time       = Common.HumanizeDate(Common.SnowflakeToTime(entry.Id).DateTime, null);
                            se.Subactions = action.SubAction;
                            RawEntries.Add(se);
                        }

                        LogItems.ItemsSource = RawEntries;
                        return;

                        List <SimpleAuditLogEntry> GroupedEntries = new List <SimpleAuditLogEntry>();
                        int subtractCount = 1;
                        for (var i = 0; i < RawEntries.Count; i++)
                        {
                            if (i > 0 && RawEntries[i].Action == RawEntries[i - 1].Action &&
                                (RawEntries[i].Action == AuditLogActionType.ChannelOverwriteUpdate ||
                                 RawEntries[i].Action == AuditLogActionType.ChannelUpdate ||
                                 RawEntries[i].Action == AuditLogActionType.EmojiUpdate ||
                                 RawEntries[i].Action == AuditLogActionType.GuildUpdate ||
                                 RawEntries[i].Action == AuditLogActionType.InviteUpdate ||
                                 RawEntries[i].Action == AuditLogActionType.MemberRoleUpdate ||
                                 RawEntries[i].Action == AuditLogActionType.RoleUpdate ||
                                 RawEntries[i].Action == AuditLogActionType.WebhookUpdate ||
                                 RawEntries[i].Action == AuditLogActionType.MessageDelete))
                            {
                                RawEntries[i - subtractCount].Subactions.AddRange(RawEntries[i].Subactions);
                                if (RawEntries[i].Action == AuditLogActionType.MessageDelete)
                                {
                                    RawEntries[i - subtractCount].Text = "Someone deleted x messages by x in #x";
                                    //Change the main message
                                }

                                if (RawEntries[i - subtractCount].Time != RawEntries[i].Time)
                                {
                                    RawEntries[i - subtractCount].Time += " - " + RawEntries[i].Time;
                                }
                            }
                        }
                    }
                }
            }
        }