Hack in authed media download support

This commit is contained in:
Tulir Asokan 2024-06-21 00:09:49 +03:00
commit 23d716b005
4 changed files with 25 additions and 2 deletions

View file

@ -113,6 +113,8 @@ type AppService struct {
txnIDC *TransactionIDCache
SpecVersions *mautrix.RespVersions
Events chan *event.Event
ToDeviceEvents chan *event.Event
DeviceLists chan *mautrix.DeviceLists
@ -307,6 +309,7 @@ func (as *AppService) NewMautrixClient(userID id.UserID) *mautrix.Client {
Log: as.Log.With().Str("as_user_id", userID.String()).Logger(),
Client: as.HTTPClient,
DefaultHTTPRetries: as.DefaultHTTPRetries,
SpecVersions: as.SpecVersions,
}
client.Logger = maulogadapt.ZeroAsMau(&client.Log)
return client

View file

@ -291,6 +291,7 @@ func (br *Bridge) ensureConnection() {
time.Sleep(10 * time.Second)
} else {
br.SpecVersions = *versions
br.AS.SpecVersions = versions
break
}
}

View file

@ -66,6 +66,8 @@ type Client struct {
SyncPresence event.Presence
SpecVersions *RespVersions
StreamSyncMinAge time.Duration
// Number of times that mautrix will retry any HTTP request
@ -852,6 +854,9 @@ func (cli *Client) LogoutAll() (resp *RespLogout, err error) {
func (cli *Client) Versions() (resp *RespVersions, err error) {
urlPath := cli.BuildClientURL("versions")
_, err = cli.MakeRequest("GET", urlPath, nil, &resp)
if err == nil {
cli.SpecVersions = resp
}
return
}
@ -1353,7 +1358,12 @@ func (cli *Client) State(roomID id.RoomID) (stateMap RoomStateMap, err error) {
// GetMediaConfig fetches the configuration of the content repository, such as upload limitations.
func (cli *Client) GetMediaConfig() (resp *RespMediaConfig, err error) {
u := cli.BuildURL(MediaURLPath{"v3", "config"})
var u string
if cli.SpecVersions.ContainsGreaterOrEqual(SpecV111) {
u = cli.BuildClientURL("v1", "media", "config")
} else {
u = cli.BuildURL(MediaURLPath{"v3", "config"})
}
_, err = cli.MakeRequest("GET", u, nil, &resp)
return
}
@ -1449,11 +1459,19 @@ func (cli *Client) downloadContext(ctx context.Context, mxcURL id.ContentURI) (*
if ctxLog.GetLevel() == zerolog.Disabled || ctxLog == zerolog.DefaultContextLogger {
ctx = cli.Log.WithContext(ctx)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, cli.GetDownloadURL(mxcURL), nil)
url := cli.GetDownloadURL(mxcURL)
authedMedia := cli.SpecVersions.ContainsGreaterOrEqual(SpecV111)
if authedMedia {
url = cli.BuildClientURL("v1", "media", "download", mxcURL.Homeserver, mxcURL.FileID)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", cli.UserAgent+" (media downloader)")
if authedMedia {
req.Header.Set("Authorization", "Bearer "+cli.AccessToken)
}
return cli.doMediaRequest(req, cli.DefaultHTTPRetries, 4*time.Second)
}

View file

@ -93,6 +93,7 @@ var (
SpecV15 = MustParseSpecVersion("v1.5")
SpecV16 = MustParseSpecVersion("v1.6")
SpecV17 = MustParseSpecVersion("v1.7")
SpecV111 = MustParseSpecVersion("v1.11")
)
func (svf SpecVersionFormat) String() string {