From ca708a752e83abe2c9da65d617cc4ad374582407 Mon Sep 17 00:00:00 2001 From: Rajeh Taher Date: Mon, 28 Oct 2024 13:11:10 +0200 Subject: [PATCH] legacyprovision: Added whoami function --- cmd/mautrix-twitter/legacyprovision.go | 29 ++++++++++++++++++++++++++ cmd/mautrix-twitter/main.go | 2 ++ pkg/connector/client.go | 16 ++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/cmd/mautrix-twitter/legacyprovision.go b/cmd/mautrix-twitter/legacyprovision.go index 78dbf7a..8051f02 100644 --- a/cmd/mautrix-twitter/legacyprovision.go +++ b/cmd/mautrix-twitter/legacyprovision.go @@ -10,6 +10,7 @@ import ( "maunium.net/go/mautrix" "maunium.net/go/mautrix/bridge/status" "maunium.net/go/mautrix/bridgev2" + "maunium.net/go/mautrix/bridgev2/bridgeconfig" "go.mau.fi/mautrix-twitter/pkg/connector" ) @@ -31,6 +32,34 @@ type Response struct { Status string `json:"status"` } +var levelsToNames = map[bridgeconfig.Permissions]string{ + bridgeconfig.PermissionLevelBlock: "block", + bridgeconfig.PermissionLevelRelay: "relay", + bridgeconfig.PermissionLevelCommands: "commands", + bridgeconfig.PermissionLevelUser: "user", + bridgeconfig.PermissionLevelAdmin: "admin", +} + +func legacyProvStatus(w http.ResponseWriter, r *http.Request) { + user := m.Matrix.Provisioning.GetUser(r) + response := map[string]any{ + "permissions": levelsToNames[user.Permissions], + "mxid": user.MXID.String(), + } + + ul := user.GetDefaultLogin() + if ul.ID != "" { // if logged in + twitClient := connector.NewTwitterClient(r.Context(), c, ul) + + currentUser, err := twitClient.GetCurrentUser() + if err == nil { + response["twitter"] = currentUser + } + } + + jsonResponse(w, http.StatusOK, response) +} + func legacyProvLogin(w http.ResponseWriter, r *http.Request) { user := m.Matrix.Provisioning.GetUser(r) ctx := r.Context() diff --git a/cmd/mautrix-twitter/main.go b/cmd/mautrix-twitter/main.go index 1c76d0e..d3559d6 100644 --- a/cmd/mautrix-twitter/main.go +++ b/cmd/mautrix-twitter/main.go @@ -55,6 +55,8 @@ func main() { } m.PostStart = func() { if m.Matrix.Provisioning != nil { + + m.Matrix.Provisioning.Router.HandleFunc("/api/whoami", legacyProvStatus).Methods(http.MethodGet) m.Matrix.Provisioning.Router.HandleFunc("/v1/api/login", legacyProvLogin).Methods(http.MethodPost) m.Matrix.Provisioning.Router.HandleFunc("/v1/api/logout", legacyProvLogout).Methods(http.MethodPost) } diff --git a/pkg/connector/client.go b/pkg/connector/client.go index 7bdad25..a021df8 100644 --- a/pkg/connector/client.go +++ b/pkg/connector/client.go @@ -149,6 +149,22 @@ func (tc *TwitterClient) IsThisUser(_ context.Context, userID networkid.UserID) return networkid.UserID(tc.client.GetCurrentUserID()) == userID } +func (tc *TwitterClient) GetCurrentUser() (user *types.User, err error) { + _, settings, err := tc.client.LoadMessagesPage() + if err != nil { + return nil, err + } + searchResponse, err := tc.client.Search(payload.SearchQuery{ + Query: settings.ScreenName, + ResultType: payload.SEARCH_RESULT_TYPE_USERS, + }) + if err != nil { + return nil, err + } + user = &searchResponse.Users[0] + return +} + func (tc *TwitterClient) GetChatInfo(_ context.Context, portal *bridgev2.Portal) (*bridgev2.ChatInfo, error) { conversationId := string(portal.PortalKey.ID) queryConversationPayload := payload.DmRequestQuery{}.Default()