Merge pull request 'matrix msc3575 support' (#2) from msc3575 into main

Reviewed-on: #2
This commit is contained in:
Amarpreet Minhas 2024-02-12 03:36:55 +00:00
commit 900f01166f
2 changed files with 29 additions and 25 deletions

View file

@ -13,6 +13,7 @@ type (
LogLevel string LogLevel string
MatrixIdentityServer string MatrixIdentityServer string
MatrixWellKnownAddress string MatrixWellKnownAddress string
MatrixMsc3575Address string
NostrAddrFee string NostrAddrFee string
} }
) )
@ -26,6 +27,7 @@ func GetConfig() Config {
LogLevel: getEnv("LOG_LEVEL", "INFO"), LogLevel: getEnv("LOG_LEVEL", "INFO"),
MatrixIdentityServer: getEnv("MATRIX_IDENTITY_SERVER", ""), MatrixIdentityServer: getEnv("MATRIX_IDENTITY_SERVER", ""),
MatrixWellKnownAddress: getEnv("MATRIX_WELL_KNOWN_ADDRESS", ""), MatrixWellKnownAddress: getEnv("MATRIX_WELL_KNOWN_ADDRESS", ""),
MatrixMsc3575Address: getEnv("MATRIX_MSC3575_ADDRESS", ""),
NostrAddrFee: getEnv("NOSTR_ADDR_FEE", "0"), NostrAddrFee: getEnv("NOSTR_ADDR_FEE", "0"),
} }
} }

View file

@ -21,9 +21,14 @@ type matrixIdentityServer struct {
BaseUrl string `json:"base_url,omitempty"` BaseUrl string `json:"base_url,omitempty"`
} }
type msc3575address struct {
Url string `json:"url,omitempty"`
}
type matrixClientWellKnown struct { type matrixClientWellKnown struct {
HomeServer *matrixBaseUrl `json:"m.homeserver"` Homeserver *matrixBaseUrl `json:"m.homeserver"`
IdentityServer *matrixIdentityServer `json:"m.identity_server,omitempty"` IdentityServer *matrixIdentityServer `json:"m.identity_server,omitempty"`
Msc3575 *msc3575address `json:"org.matrix.msc3575.proxy,omitempty"`
} }
func MatrixServer(w http.ResponseWriter, req *http.Request) { func MatrixServer(w http.ResponseWriter, req *http.Request) {
@ -46,36 +51,33 @@ func MatrixServer(w http.ResponseWriter, req *http.Request) {
func MatrixClient(w http.ResponseWriter, req *http.Request) { func MatrixClient(w http.ResponseWriter, req *http.Request) {
l := logger.Get() l := logger.Get()
// uses an environment variable for now m := &matrixClientWellKnown{}
wellKnownAddr := config.GetConfig().MatrixWellKnownAddress wellKnownAddr := config.GetConfig().MatrixWellKnownAddress
// no matrix config set
if wellKnownAddr == "" { if wellKnownAddr == "" {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
l.Debug().Str("path", "matrix/client").Msg("matrix well known address unset") l.Debug().Str("path", "matrix/client").Msg("matrix well known address unset")
return return
} }
m.Homeserver = &matrixBaseUrl{
BaseUrl: fmt.Sprintf("https://%s", wellKnownAddr),
}
identityServer := config.GetConfig().MatrixIdentityServer identityServer := config.GetConfig().MatrixIdentityServer
if identityServer == "" { if identityServer != "" {
w.Header().Set("Content-Type", "application/json") m.IdentityServer = &matrixIdentityServer{
w.WriteHeader(http.StatusOK)
baseUrl := &matrixClientWellKnown{
HomeServer: &matrixBaseUrl{
BaseUrl: fmt.Sprintf("https://%s", wellKnownAddr),
},
}
json.NewEncoder(w).Encode(baseUrl)
} else {
identityServer = fmt.Sprintf("https://%s", identityServer)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
baseUrl := &matrixClientWellKnown{
HomeServer: &matrixBaseUrl{
BaseUrl: fmt.Sprintf("https://%s", wellKnownAddr),
},
IdentityServer: &matrixIdentityServer{
BaseUrl: identityServer, BaseUrl: identityServer,
},
}
json.NewEncoder(w).Encode(baseUrl)
} }
} }
msc3575 := config.GetConfig().MatrixMsc3575Address
if msc3575 != "" {
m.Msc3575 = &msc3575address{
Url: msc3575,
}
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(m)
}