mirror of
https://mau.dev/mautrix/go.git
synced 2026-03-14 14:25:53 +01:00
Hack in authed media download support
This commit is contained in:
parent
9c2d549582
commit
23d716b005
4 changed files with 25 additions and 2 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -291,6 +291,7 @@ func (br *Bridge) ensureConnection() {
|
|||
time.Sleep(10 * time.Second)
|
||||
} else {
|
||||
br.SpecVersions = *versions
|
||||
br.AS.SpecVersions = versions
|
||||
break
|
||||
}
|
||||
}
|
||||
|
|
|
|||
22
client.go
22
client.go
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue