Skip to content

Commit

Permalink
Read account data using specialized routes
Browse files Browse the repository at this point in the history
Now that matrix-org/synapse#4303 is implemented,
we don't need to resort to doing `/sync` to
retrieve account data.

We can do it via the new GET endpoint of the API:
`/user/{user_id}/account_data/{account_dataType}`

This API endpoint landed in Synapse v0.34.1,
so versions prior to that will not be supported anymore.
  • Loading branch information
spantaleev committed Jan 25, 2019
1 parent fe1f295 commit 1add2d2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 66 deletions.
83 changes: 17 additions & 66 deletions src/devture/matrix/corporal/connector/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package connector
import (
"devture/matrix/corporal/avatar"
"devture/matrix/corporal/matrix"
"encoding/json"
"fmt"
"net/http"
"time"
Expand Down Expand Up @@ -223,7 +222,12 @@ func (me *ApiConnector) determineAvatarSourceUriHashByUserAndMxcUri(
return "", nil
}

return value.(string), nil
valueAsString, ok := value.(string)
if !ok {
return "", nil
}

return valueAsString, nil
}

func (me *ApiConnector) GetUserAccountDataContentByType(
Expand All @@ -236,78 +240,25 @@ func (me *ApiConnector) GetUserAccountDataContentByType(
return nil, err
}

// Until there's a way to retrieve user account data
// ( see https://github.com/matrix-org/matrix-doc/issues/1339 ),
// we're forced to do it via a `/sync` request.
//
// This is suboptimal and potentially quite slow,
// as we're fetching a lot of unnecessary data with it.
//
// Below is a `/sync` filter, which asks the server to skip
// some events (presence, room information, etc.).
//
// Still, even with that filter, `/sync` would do too much
// and be potentially slow. The `rooms.join` part of the response
// still contains rooms we've asked to be skipped..
filter := map[string]interface{}{
"presence": map[string]interface{}{
"senders": []string{
"@whatever:whatever",
},
},
"room": map[string]interface{}{
"rooms": []string{
"!whatever:whatever",
},
"state": map[string]interface{}{
"senders": []string{
"@whatever:whatever",
},
},
"account_data": map[string]interface{}{
"senders": []string{
"@whatever:whatever",
},
},
},
"account_data": map[string]interface{}{
"types": []string{
accountDataType,
},
},
}

filterAsBytes, err := json.Marshal(filter)
if err != nil {
return nil, fmt.Errorf("Cannot JSON serialize the filter: %s", err)
}

queryParams := map[string]string{
// Make sure we don't mark the user as online as part of the /sync request
"set_presence": "offline",

"filter": string(filterAsBytes),
}

var resp gomatrix.RespSync

var accountData map[string]interface{}
_, err = client.MakeRequest(
"GET",
client.BuildURLWithQuery([]string{"/sync"}, queryParams),
client.BuildURL(
fmt.Sprintf("/user/%s/account_data/%s", userId, accountDataType),
),
nil,
&resp,
&accountData,
)
if err != nil {
return nil, err
}

for _, event := range resp.AccountData.Events {
if event.Type == accountDataType {
return event.Content, nil
if err != nil {
if matrix.IsErrorWithCode(err, matrix.ErrorNotFound) {
// No such account data
return map[string]interface{}{}, nil
}
return nil, err
}

return map[string]interface{}{}, nil
return accountData, nil
}

func (me *ApiConnector) getJoinedCommunityIdsByUserId(
Expand Down
1 change: 1 addition & 0 deletions src/devture/matrix/corporal/matrix/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const (
ErrorInvalidUsername = "M_INVALID_USERNAME"
ErrorLimitExceeded = "M_LIMIT_EXCEEDED"
ErrorMissingParameter = "M_MISSING_PARAM"
ErrorNotFound = "M_NOT_FOUND"
)

const (
Expand Down

0 comments on commit 1add2d2

Please sign in to comment.