connector: instantiate http.Client from bridge settings
This commit is contained in:
@@ -43,6 +43,7 @@ type DiscordClient struct {
|
||||
usersFromReady map[string]*discordgo.User
|
||||
UserLogin *bridgev2.UserLogin
|
||||
Session *discordgo.Session
|
||||
client *http.Client
|
||||
|
||||
hasBegunSyncing bool
|
||||
|
||||
@@ -63,6 +64,7 @@ func (d *DiscordConnector) LoadUserLogin(ctx context.Context, login *bridgev2.Us
|
||||
connector: d,
|
||||
UserLogin: login,
|
||||
Session: session,
|
||||
client: d.Bridge.GetHTTPClientSettings().Compile(),
|
||||
}
|
||||
|
||||
login.Client = &cl
|
||||
@@ -263,7 +265,7 @@ func (d *DiscordClient) makeAvatarForGuild(guild *discordgo.Guild) *bridgev2.Ava
|
||||
ID: discordid.MakeAvatarID(guild.Icon),
|
||||
Get: func(ctx context.Context) ([]byte, error) {
|
||||
url := discordgo.EndpointGuildIcon(guild.ID, guild.Icon)
|
||||
return simpleDownload(ctx, url, "guild icon")
|
||||
return d.simpleDownload(ctx, url, "guild icon")
|
||||
},
|
||||
Remove: guild.Icon == "",
|
||||
}
|
||||
@@ -370,13 +372,13 @@ func (d *DiscordClient) bridgeGuild(ctx context.Context, guildID string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func simpleDownload(ctx context.Context, url, thing string) ([]byte, error) {
|
||||
func (d *DiscordClient) simpleDownload(ctx context.Context, url, thing string) ([]byte, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to prepare request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
resp, err := d.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to download %s: %w", thing, err)
|
||||
}
|
||||
|
||||
@@ -77,7 +77,7 @@ func (d *DiscordChatResync) avatar(ctx context.Context) *bridgev2.Avatar {
|
||||
ID: discordid.MakeAvatarID(ch.Icon),
|
||||
Get: func(ctx context.Context) ([]byte, error) {
|
||||
url := discordgo.EndpointGroupIcon(ch.ID, ch.Icon)
|
||||
return simpleDownload(ctx, url, "group dm icon")
|
||||
return d.Client.simpleDownload(ctx, url, "group dm icon")
|
||||
},
|
||||
Remove: ch.Icon == "",
|
||||
}
|
||||
|
||||
@@ -35,13 +35,13 @@ func (d *DiscordClient) IsThisUser(ctx context.Context, userID networkid.UserID)
|
||||
return userID == discordid.UserLoginIDToUserID(d.UserLogin.ID)
|
||||
}
|
||||
|
||||
func makeUserAvatar(u *discordgo.User) *bridgev2.Avatar {
|
||||
func (d *DiscordClient) makeUserAvatar(u *discordgo.User) *bridgev2.Avatar {
|
||||
url := u.AvatarURL("256")
|
||||
|
||||
return &bridgev2.Avatar{
|
||||
ID: discordid.MakeAvatarID(url),
|
||||
Get: func(ctx context.Context) ([]byte, error) {
|
||||
return simpleDownload(ctx, url, "user avatar")
|
||||
return d.simpleDownload(ctx, url, "user avatar")
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -65,7 +65,7 @@ func (d *DiscordClient) GetUserInfo(ctx context.Context, ghost *bridgev2.Ghost)
|
||||
return &bridgev2.UserInfo{
|
||||
Identifiers: []string{fmt.Sprintf("discord:%s", user.ID)},
|
||||
Name: ptr.Ptr(user.DisplayName()),
|
||||
Avatar: makeUserAvatar(user),
|
||||
Avatar: d.makeUserAvatar(user),
|
||||
IsBot: &user.Bot,
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user