2026-03-11 22:06:44 +01:00
|
|
|
// Copyright (C) 2026 saces@c-base.org
|
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
2026-01-31 08:13:53 +01:00
|
|
|
package mxapi
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
|
|
|
|
|
"maunium.net/go/mautrix"
|
|
|
|
|
"maunium.net/go/mautrix/id"
|
|
|
|
|
)
|
|
|
|
|
|
2026-04-29 00:35:12 +02:00
|
|
|
type DiscoverInfo struct {
|
|
|
|
|
Homeserver string `json:"homeserver"`
|
|
|
|
|
UserID id.UserID `json:"user_id"`
|
|
|
|
|
LoginName string `json:"login_name"`
|
|
|
|
|
}
|
2026-01-31 08:13:53 +01:00
|
|
|
|
2026-04-29 00:35:12 +02:00
|
|
|
// Discover tries to guess the homeserver from the given matrix id
|
|
|
|
|
func Discover(userID id.UserID) (di *DiscoverInfo, err error) {
|
|
|
|
|
var tmp_di DiscoverInfo
|
|
|
|
|
tmp_di.LoginName, tmp_di.Homeserver, err = userID.ParseAndValidateRelaxed()
|
2026-01-31 08:13:53 +01:00
|
|
|
if err != nil {
|
2026-04-29 00:35:12 +02:00
|
|
|
return
|
2026-01-31 08:13:53 +01:00
|
|
|
}
|
|
|
|
|
|
2026-04-29 00:35:12 +02:00
|
|
|
wk, err := mautrix.DiscoverClientAPI(context.Background(), tmp_di.Homeserver)
|
2026-01-31 08:13:53 +01:00
|
|
|
if err != nil {
|
2026-04-29 00:35:12 +02:00
|
|
|
return
|
2026-01-31 08:13:53 +01:00
|
|
|
}
|
|
|
|
|
if wk != nil {
|
2026-04-29 00:35:12 +02:00
|
|
|
tmp_di.Homeserver = wk.Homeserver.BaseURL
|
2026-01-31 08:13:53 +01:00
|
|
|
}
|
2026-04-29 00:35:12 +02:00
|
|
|
tmp_di.UserID = userID
|
|
|
|
|
di = &tmp_di
|
|
|
|
|
return
|
2026-01-31 08:13:53 +01:00
|
|
|
}
|