Skip to content

Commit

Permalink
legacyprovision: Added whoami function
Browse files Browse the repository at this point in the history
  • Loading branch information
PurpShell committed Oct 28, 2024
1 parent a3500f3 commit feb9d35
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
29 changes: 29 additions & 0 deletions cmd/mautrix-twitter/legacyprovision.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions cmd/mautrix-twitter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ func main() {
}
m.PostStart = func() {
if m.Matrix.Provisioning != nil {

m.Matrix.Provisioning.Router.HandleFunc("/v1/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)
}
Expand Down
16 changes: 16 additions & 0 deletions pkg/connector/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit feb9d35

Please sign in to comment.