示例#1
0
        public UserForApiContract(User user, string fallbackName, IUserIconFactory iconFactory, UserOptionalFields optionalFields)
            : base(user, fallbackName)
        {
            if (user == null)
            {
                return;
            }

            Active         = user.Active;
            GroupId        = user.GroupId;
            MemberSince    = user.CreateDate;
            VerifiedArtist = user.VerifiedArtist;

            if (optionalFields.HasFlag(UserOptionalFields.KnownLanguages))
            {
                KnownLanguages = user.KnownLanguages.Select(l => new UserKnownLanguageContract(l)).ToArray();
            }

            if (optionalFields.HasFlag(UserOptionalFields.MainPicture) && !string.IsNullOrEmpty(user.Email) && iconFactory != null)
            {
                MainPicture = iconFactory.GetIcons(user, ImageSizes.All);
            }

            if (optionalFields.HasFlag(UserOptionalFields.OldUsernames))
            {
                OldUsernames = user.OldUsernames.Select(n => new OldUsernameContract(n)).ToArray();
            }
        }
示例#2
0
        public async Task SendMessageAsync(WebhookEvents webhookEvent, IUserWithEmail?user, string?title = null, string?url = null, string?description = null, Color?color = null)
        {
            EmbedBuilder CreateEmbedBuilder(IUserWithEmail?user)
            {
                var builder = new EmbedBuilder
                {
                    Color = color,
                };

                if (user is not null)
                {
                    var profileUrl = _entryLinkFactory.GetFullEntryUrl(EntryType.User, user.Id);

                    builder
                    .WithAuthor(name: user.Name, iconUrl: _userIconFactory.GetIcons(user, ImageSizes.Thumb).UrlThumb, url: profileUrl)
                    .WithUrl(profileUrl);
                }

                return(builder);
            }

            var embeds = new[] { CreateEmbedBuilder(user).WithTitle(title).WithUrl(url).WithDescription(description).Build() };

            await _repository.HandleTransactionAsync(async ctx =>
            {
                var webhooks = await ctx.Query <Webhook>().WhereHasWebhookEvent(webhookEvent).ToListAsync();

                var tasks = webhooks.Select(async w =>
                {
                    try
                    {
                        using var client = new DiscordWebhookClient(w.Url);
                        await client.SendMessageAsync(embeds: embeds, username: _brandableStrings.SiteName, avatarUrl: _discordWebhookSettings.AvatarUrl);
                    }
                    catch (Exception x)
                    {
                        s_log.Error(x, "Unable to send message");
                    }
                }).ToList();

                await Task.WhenAll(tasks);
            });
        }