Ejemplo n.º 1
0
        /// <summary>
        /// Default constructor for EmsTemplateTests.
        /// </summary>
        public EmsTemplateTests()
        {
            this.PopulateProtectedVariables = true;

            env.Add(LookupContext.PROVIDER_URL, "tibjmsnaming://localhost:7222");
            env.Add(LookupContext.SECURITY_PRINCIPAL, "admin");
            env.Add(LookupContext.SECURITY_CREDENTIALS, "");
            lookupContext = new LookupContext(env);
        }
        private static Iterable<MemberInfo> getMembers(Library typeSystem, TypeInfo type, String name,
            Iterable<TypeInfo> typeArguments, bool useCache, LookupContext context) {
            var nTypeArgs = typeArguments.count();
            foreach (var mi in (useCache) ? getCachedMembers(typeSystem, type, name) : getMembers(typeSystem, type, name)) {
                switch (mi.MemberKind) {
                case Field:
                    if (nTypeArgs == 0 && context.lookupField) {
                        context.lookupField = false;
                        yield return mi;
                    }
                    break;
                    
                case Property:
                    if (nTypeArgs == 0  && context.lookupProperty) {
                        context.lookupProperty = false;
                        yield return mi;
                    }
                    break;
                    
                case Method:
                    if (nTypeArgs > 0) {
                        if (nTypeArgs == mi.Method.GenericArguments.count()) {
                            yield return MemberInfo.getInfo(typeSystem.getGenericMethod(mi.Method, typeArguments));
                        }
                    } else {
                        yield return mi;
                    }
                    break;

                case Type:
                    if (context.lookupClass) {
                        context.lookupClass = false;
                        if (nTypeArgs > 0) {
                            if (nTypeArgs == mi.Type.GenericArguments.count()) {
                                yield return mi;
                            }
                        } else {
                            if (mi.MemberKind == MemberKind.Type && mi.Type.GenericArguments.count() > 0) {
                                mi = MemberInfo.getInfo(mi.Type.RawType);
                            }
                            yield return mi;
                        }
                    }
                    break;
                }
            }
        }
        private static Iterable<MemberInfo> getMembersRecursively(Library typeSystem, TypeInfo scope, TypeInfo type, String name,
                Iterable<TypeInfo> typeArguments, bool useCache, LookupContext context) {
            Iterable<MemberInfo> members;
			switch (type.TypeKind) {
			case Array:
			case LowerBoundedWildcard:
			case UnboundedWildcard:
				type = typeSystem.ObjectType;
				break;
			case UpperBoundedWildcard:
				type = type.WildcardBound;
				break;
			}
            if (type.IsGenericParameter) {
                if (type.GenericParameterBounds.any()) {
                    members = Query.empty();
                    foreach (var t in type.GenericParameterBounds) {
                        members = members.concat(getMembersRecursively(typeSystem, scope, t, name, typeArguments, useCache, context));
                    }
                } else {
                    members = getMembersRecursively(typeSystem, scope, typeSystem.ObjectType, name, typeArguments, useCache, context);
                }
            } else {
                members = getMembers(typeSystem, type, name, typeArguments, useCache, context);
                foreach (var t in type.getBaseTypes()) {
                    members = members.concat(getMembers(typeSystem, t, name, typeArguments, useCache, context));
                }
            }
            return filterMembers(typeSystem, scope, members);
        }
Ejemplo n.º 4
0
        public static async Task RenderMemberList(this Context ctx, LookupContext lookupCtx, IDatabase db, SystemId system, string embedTitle, MemberListOptions opts)
        {
            // We take an IDatabase instead of a IPKConnection so we don't keep the handle open for the entire runtime
            // We wanna release it as soon as the member list is actually *fetched*, instead of potentially minutes later (paginate timeout)
            var members = (await db.Execute(conn => conn.QueryMemberList(system, opts.ToQueryOptions())))
                          .SortByMemberListOptions(opts, lookupCtx)
                          .ToList();

            var itemsPerPage = opts.Type == ListType.Short ? 25 : 5;
            await ctx.Paginate(members.ToAsyncEnumerable(), members.Count, itemsPerPage, embedTitle, Renderer);

            // Base renderer, dispatches based on type
            Task Renderer(EmbedBuilder eb, IEnumerable <ListedMember> page)
            {
                // Add a global footer with the filter/sort string + result count
                eb.Footer(new($"{opts.CreateFilterString()}. {"result".ToQuantity(members.Count)}."));

                // Then call the specific renderers
                if (opts.Type == ListType.Short)
                {
                    ShortRenderer(eb, page);
                }
                else
                {
                    LongRenderer(eb, page);
                }

                return(Task.CompletedTask);
            }

            void ShortRenderer(EmbedBuilder eb, IEnumerable <ListedMember> page)
            {
                // We may end up over the description character limit
                // so run it through a helper that "makes it work" :)
                eb.WithSimpleLineContent(page.Select(m =>
                {
                    if (m.HasProxyTags)
                    {
                        var proxyTagsString = m.ProxyTagsString();
                        if (proxyTagsString.Length > 100) // arbitrary threshold for now, tweak?
                        {
                            proxyTagsString = "tags too long, see member card";
                        }
                        return($"[`{m.Hid}`] **{m.NameFor(ctx)}** *(*{proxyTagsString}*)*");
                    }

                    return($"[`{m.Hid}`] **{m.NameFor(ctx)}**");
                }));
            }

            void LongRenderer(EmbedBuilder eb, IEnumerable <ListedMember> page)
            {
                var zone = ctx.System?.Zone ?? DateTimeZone.Utc;

                foreach (var m in page)
                {
                    var profile = new StringBuilder($"**ID**: {m.Hid}");

                    if (m.DisplayName != null && m.NamePrivacy.CanAccess(lookupCtx))
                    {
                        profile.Append($"\n**Display name**: {m.DisplayName}");
                    }

                    if (m.PronounsFor(lookupCtx) is {} pronouns)
                    {
                        profile.Append($"\n**Pronouns**: {pronouns}");
                    }

                    if (m.BirthdayFor(lookupCtx) != null)
                    {
                        profile.Append($"\n**Birthdate**: {m.BirthdayString}");
                    }

                    if (m.ProxyTags.Count > 0)
                    {
                        profile.Append($"\n**Proxy tags**: {m.ProxyTagsString()}");
                    }

                    if (opts.IncludeMessageCount && m.MessageCountFor(lookupCtx) is {} count&& count > 0)
                    {
                        profile.Append($"\n**Message count:** {count}");
                    }

                    if (opts.IncludeLastMessage && m.MetadataPrivacy.TryGet(lookupCtx, m.LastMessage, out var lastMsg))
                    {
                        profile.Append($"\n**Last message:** {DiscordUtils.SnowflakeToInstant(lastMsg.Value).FormatZoned(zone)}");
                    }

                    if (opts.IncludeLastSwitch && m.MetadataPrivacy.TryGet(lookupCtx, m.LastSwitchTime, out var lastSw))
                    {
                        profile.Append($"\n**Last switched in:** {lastSw.Value.FormatZoned(zone)}");
                    }

                    if (opts.IncludeCreated && m.MetadataPrivacy.TryGet(lookupCtx, m.Created, out var created))
                    {
                        profile.Append($"\n**Created on:** {created.FormatZoned(zone)}");
                    }

                    if (opts.IncludeAvatar && m.AvatarFor(lookupCtx) is {} avatar)
                    {
                        profile.Append($"\n**Avatar URL:** {avatar}");
                    }

                    if (m.DescriptionFor(lookupCtx) is {} desc)
                    {
                        profile.Append($"\n\n{desc}");
                    }

                    if (m.MemberVisibility == PrivacyLevel.Private)
                    {
                        profile.Append("\n*(this member is hidden)*");
                    }

                    eb.Field(new(m.NameFor(ctx), profile.ToString().Truncate(1024)));
                }
            }
        }
Ejemplo n.º 5
0
        public void AddLookupId(TLink link, LookupContext lookupContext)
        {
            var lookupId = _getLookupId(link);

            lookupContext.AddLookupId <TReference, TId>(lookupId);
        }
Ejemplo n.º 6
0
        private static LearnerResults GetOrCreateLearner(long ukprn, long uln, List <LearnerResults> results, LookupContext lookupContext)
        {
            var providerId = lookupContext.GetProviderId(ukprn);
            var learnerId  = lookupContext.GetLearnerId(uln);
            var learner    = results.SingleOrDefault(l => l.ProviderId == providerId && l.LearnerId == learnerId);

            if (learner == null)
            {
                learner = new LearnerResults
                {
                    ProviderId = providerId,
                    LearnerId  = learnerId
                };
                results.Add(learner);
            }
            return(learner);
        }
Ejemplo n.º 7
0
        public static MemberListOptions ParseMemberListOptions(this Context ctx, LookupContext lookupCtx)
        {
            var p = new MemberListOptions();

            // Short or long list? (parse this first, as it can potentially take a positional argument)
            var isFull = ctx.Match("f", "full", "big", "details", "long") || ctx.MatchFlag("f", "full");

            p.Type = isFull ? ListType.Long : ListType.Short;

            // Search query
            if (ctx.HasNext())
            {
                p.Search = ctx.RemainderOrNull();
            }

            // Include description in search?
            if (ctx.MatchFlag("search-description", "filter-description", "in-description", "sd", "description", "desc"))
            {
                p.SearchDescription = true;
            }

            // Sort property (default is by name, but adding a flag anyway, 'cause why not)
            if (ctx.MatchFlag("by-name", "bn"))
            {
                p.SortProperty = SortProperty.Name;
            }
            if (ctx.MatchFlag("by-display-name", "bdn"))
            {
                p.SortProperty = SortProperty.DisplayName;
            }
            if (ctx.MatchFlag("by-id", "bid"))
            {
                p.SortProperty = SortProperty.Hid;
            }
            if (ctx.MatchFlag("by-message-count", "bmc"))
            {
                p.SortProperty = SortProperty.MessageCount;
            }
            if (ctx.MatchFlag("by-created", "bc"))
            {
                p.SortProperty = SortProperty.CreationDate;
            }
            if (ctx.MatchFlag("by-last-fronted", "by-last-front", "by-last-switch", "blf", "bls"))
            {
                p.SortProperty = SortProperty.LastSwitch;
            }
            if (ctx.MatchFlag("by-last-message", "blm", "blp"))
            {
                p.SortProperty = SortProperty.LastMessage;
            }
            if (ctx.MatchFlag("by-birthday", "by-birthdate", "bbd"))
            {
                p.SortProperty = SortProperty.Birthdate;
            }
            if (ctx.MatchFlag("random"))
            {
                p.SortProperty = SortProperty.Random;
            }

            // Sort reverse?
            if (ctx.MatchFlag("r", "rev", "reverse"))
            {
                p.Reverse = true;
            }

            // Privacy filter (default is public only)
            if (ctx.MatchFlag("a", "all"))
            {
                p.PrivacyFilter = null;
            }
            if (ctx.MatchFlag("private-only", "private", "priv"))
            {
                p.PrivacyFilter = PrivacyLevel.Private;
            }
            if (ctx.MatchFlag("public-only", "public", "pub"))
            {
                p.PrivacyFilter = PrivacyLevel.Public;
            }

            // PERM CHECK: If we're trying to access non-public members of another system, error
            if (p.PrivacyFilter != PrivacyLevel.Public && lookupCtx != LookupContext.ByOwner)
            {
                // TODO: should this just return null instead of throwing or something? >.>
                throw new PKError("You cannot look up private members of another system.");
            }

            // Additional fields to include in the search results
            if (ctx.MatchFlag("with-last-switch", "with-last-fronted", "with-last-front", "wls", "wlf"))
            {
                p.IncludeLastSwitch = true;
            }
            if (ctx.MatchFlag("with-last-message", "with-last-proxy", "wlm", "wlp"))
            {
                p.IncludeLastMessage = true;
            }
            if (ctx.MatchFlag("with-message-count", "wmc"))
            {
                p.IncludeMessageCount = true;
            }
            if (ctx.MatchFlag("with-created", "wc"))
            {
                p.IncludeCreated = true;
            }
            if (ctx.MatchFlag("with-avatar", "with-image", "wa", "wi", "ia", "ii", "img"))
            {
                p.IncludeAvatar = true;
            }

            // Always show the sort property, too
            if (p.SortProperty == SortProperty.LastSwitch)
            {
                p.IncludeLastSwitch = true;
            }
            if (p.SortProperty == SortProperty.LastMessage)
            {
                p.IncludeLastMessage = true;
            }
            if (p.SortProperty == SortProperty.MessageCount)
            {
                p.IncludeMessageCount = true;
            }
            if (p.SortProperty == SortProperty.CreationDate)
            {
                p.IncludeCreated = true;
            }

            // Done!
            return(p);
        }
Ejemplo n.º 8
0
        public async Task <DiscordEmbed> CreateMemberEmbed(PKSystem system, PKMember member, DiscordGuild guild, LookupContext ctx)
        {
            // string FormatTimestamp(Instant timestamp) => DateTimeFormats.ZonedDateTimeFormat.Format(timestamp.InZone(system.Zone));

            var name = member.NameFor(ctx);

            if (system.Name != null)
            {
                name = $"{name} ({system.Name})";
            }

            DiscordColor color;

            try
            {
                color = member.Color?.ToDiscordColor() ?? DiscordUtils.Gray;
            }
            catch (ArgumentException)
            {
                // Bad API use can cause an invalid color string
                // TODO: fix that in the API
                // for now we just default to a blank color, yolo
                color = DiscordUtils.Gray;
            }

            var guildSettings = guild != null ? await _db.Execute(c => c.QueryOrInsertMemberGuildConfig(guild.Id, member.Id)) : null;

            var guildDisplayName = guildSettings?.DisplayName;
            var avatar           = guildSettings?.AvatarUrl ?? member.AvatarFor(ctx);

            var proxyTagsStr = string.Join('\n', member.ProxyTags.Select(t => $"`` {t.ProxyString}``"));

            var eb = new DiscordEmbedBuilder()
                     // TODO: add URL of website when that's up
                     .WithAuthor(name, iconUrl: DiscordUtils.WorkaroundForUrlBug(avatar))
                     // .WithColor(member.ColorPrivacy.CanAccess(ctx) ? color : DiscordUtils.Gray)
                     .WithColor(color)
                     .WithFooter($"System ID: {system.Hid} | Member ID: {member.Hid} {(member.MetadataPrivacy.CanAccess(ctx) ? $"| Created on {member.Created.FormatZoned(system)}":"")}");

            var description = "";

            if (member.MemberVisibility == PrivacyLevel.Private)
            {
                description += "*(this member is hidden)*\n";
            }
            if (guildSettings?.AvatarUrl != null)
            {
                if (member.AvatarFor(ctx) != null)
                {
                    description += $"*(this member has a server-specific avatar set; [click here]({member.AvatarUrl}) to see the global avatar)*\n";
                }
                else
                {
                    description += "*(this member has a server-specific avatar set)*\n";
                }
            }
            if (description != "")
            {
                eb.WithDescription(description);
            }

            if (avatar != null)
            {
                eb.WithThumbnail(avatar);
            }

            if (!member.DisplayName.EmptyOrNull() && member.NamePrivacy.CanAccess(ctx))
            {
                eb.AddField("Display Name", member.DisplayName.Truncate(1024), true);
            }
            if (guild != null && guildDisplayName != null)
            {
                eb.AddField($"Server Nickname (for {guild.Name})", guildDisplayName.Truncate(1024), true);
            }
            if (member.BirthdayFor(ctx) != null)
            {
                eb.AddField("Birthdate", member.BirthdayString, true);
            }
            if (member.PronounsFor(ctx) is {} pronouns&& !string.IsNullOrWhiteSpace(pronouns))
            {
                eb.AddField("Pronouns", pronouns.Truncate(1024), true);
            }
            if (member.MessageCountFor(ctx) is {} count&& count > 0)
            {
                eb.AddField("Message Count", member.MessageCount.ToString(), true);
            }
            if (member.HasProxyTags)
            {
                eb.AddField("Proxy Tags", string.Join('\n', proxyTagsStr).Truncate(1024), true);
            }
            // --- For when this gets added to the member object itself or however they get added
            // if (member.LastMessage != null && member.MetadataPrivacy.CanAccess(ctx)) eb.AddField("Last message:" FormatTimestamp(DiscordUtils.SnowflakeToInstant(m.LastMessage.Value)));
            // if (member.LastSwitchTime != null && m.MetadataPrivacy.CanAccess(ctx)) eb.AddField("Last switched in:", FormatTimestamp(member.LastSwitchTime.Value));
            // if (!member.Color.EmptyOrNull() && member.ColorPrivacy.CanAccess(ctx)) eb.AddField("Color", $"#{member.Color}", true);
            if (!member.Color.EmptyOrNull())
            {
                eb.AddField("Color", $"#{member.Color}", true);
            }

            if (member.DescriptionFor(ctx) is {} desc)
            {
                eb.AddField("Description", member.Description.NormalizeLineEndSpacing(), false);
            }

            return(eb.Build());
        }
Ejemplo n.º 9
0
        public Task <DiscordEmbed> CreateFrontPercentEmbed(FrontBreakdown breakdown, DateTimeZone tz, LookupContext ctx)
        {
            var actualPeriod = breakdown.RangeEnd - breakdown.RangeStart;
            var eb           = new DiscordEmbedBuilder()
                               .WithColor(DiscordUtils.Gray)
                               .WithFooter($"Since {breakdown.RangeStart.FormatZoned(tz)} ({actualPeriod.FormatDuration()} ago)");

            var maxEntriesToDisplay = 24; // max 25 fields allowed in embed - reserve 1 for "others"

            // We convert to a list of pairs so we can add the no-fronter value
            // Dictionary doesn't allow for null keys so we instead have a pair with a null key ;)
            var pairs = breakdown.MemberSwitchDurations.ToList();

            if (breakdown.NoFronterDuration != Duration.Zero)
            {
                pairs.Add(new KeyValuePair <PKMember, Duration>(null, breakdown.NoFronterDuration));
            }

            var membersOrdered = pairs.OrderByDescending(pair => pair.Value).Take(maxEntriesToDisplay).ToList();

            foreach (var pair in membersOrdered)
            {
                var frac = pair.Value / actualPeriod;
                eb.AddField(pair.Key?.NameFor(ctx) ?? "*(no fronter)*", $"{frac*100:F0}% ({pair.Value.FormatDuration()})");
            }

            if (membersOrdered.Count > maxEntriesToDisplay)
            {
                eb.AddField("(others)",
                            membersOrdered.Skip(maxEntriesToDisplay)
                            .Aggregate(Duration.Zero, (prod, next) => prod + next.Value)
                            .FormatDuration(), true);
            }

            return(Task.FromResult(eb.Build()));
        }
Ejemplo n.º 10
0
        public async Task <DiscordEmbed> CreateSystemEmbed(DiscordClient client, PKSystem system, LookupContext ctx)
        {
            await using var conn = await _db.Obtain();

            // Fetch/render info for all accounts simultaneously
            var accounts = await conn.GetLinkedAccounts(system.Id);

            var users = await Task.WhenAll(accounts.Select(async uid => (await client.GetUser(uid))?.NameAndMention() ?? $"(deleted account {uid})"));

            var memberCount = await conn.GetSystemMemberCount(system.Id, PrivacyLevel.Public);

            var eb = new DiscordEmbedBuilder()
                     .WithColor(DiscordUtils.Gray)
                     .WithTitle(system.Name ?? null)
                     .WithThumbnail(system.AvatarUrl)
                     .WithFooter($"System ID: {system.Hid} | Created on {system.Created.FormatZoned(system)}");

            var latestSwitch = await _data.GetLatestSwitch(system.Id);

            if (latestSwitch != null && system.FrontPrivacy.CanAccess(ctx))
            {
                var switchMembers = await _data.GetSwitchMembers(latestSwitch).ToListAsync();

                if (switchMembers.Count > 0)
                {
                    eb.AddField("Fronter".ToQuantity(switchMembers.Count(), ShowQuantityAs.None),
                                string.Join(", ", switchMembers.Select(m => m.NameFor(ctx))));
                }
            }

            if (system.Tag != null)
            {
                eb.AddField("Tag", system.Tag.EscapeMarkdown());
            }
            eb.AddField("Linked accounts", string.Join(", ", users).Truncate(1000), true);

            if (system.MemberListPrivacy.CanAccess(ctx))
            {
                if (memberCount > 0)
                {
                    eb.AddField($"Members ({memberCount})", $"(see `pk;system {system.Hid} list` or `pk;system {system.Hid} list full`)", true);
                }
                else
                {
                    eb.AddField($"Members ({memberCount})", "Add one with `pk;member new`!", true);
                }
            }

            if (system.DescriptionFor(ctx) is { } desc)
            {
                eb.AddField("Description", desc.NormalizeLineEndSpacing().Truncate(1024), false);
            }

            return(eb.Build());
        }
Ejemplo n.º 11
0
        public async Task <DiscordEmbed> CreateFronterEmbed(PKSwitch sw, DateTimeZone zone, LookupContext ctx)
        {
            var members = await _data.GetSwitchMembers(sw).ToListAsync();

            var timeSinceSwitch = SystemClock.Instance.GetCurrentInstant() - sw.Timestamp;

            return(new DiscordEmbedBuilder()
                   .WithColor(members.FirstOrDefault()?.Color?.ToDiscordColor() ?? DiscordUtils.Gray)
                   .AddField($"Current {"fronter".ToQuantity(members.Count, ShowQuantityAs.None)}", members.Count > 0 ? string.Join(", ", members.Select(m => m.NameFor(ctx))) : "*(no fronter)*")
                   .AddField("Since", $"{sw.Timestamp.FormatZoned(zone)} ({timeSinceSwitch.FormatDuration()} ago)")
                   .Build());
        }