Add doc, add checks in fillDisplayName

This commit is contained in:
Brendan Abolivier 2017-09-07 11:38:43 +01:00
parent 79b4edd88e
commit ea703ddc1c
No known key found for this signature in database
GPG key ID: 8EF1500759F70623

View file

@ -79,6 +79,8 @@ func CreateInvitesFrom3PIDInvites(
// createInviteFrom3PIDInvite processes an invite provided by the identity server // createInviteFrom3PIDInvite processes an invite provided by the identity server
// and creates a m.room.member event (with "invite" membership) from it. // and creates a m.room.member event (with "invite" membership) from it.
// Returns an error if there was a problem building the event or fetching the
// necessary data to do so.
func createInviteFrom3PIDInvite( func createInviteFrom3PIDInvite(
req *http.Request, queryAPI api.RoomserverQueryAPI, cfg config.Dendrite, req *http.Request, queryAPI api.RoomserverQueryAPI, cfg config.Dendrite,
inv invite, inv invite,
@ -153,28 +155,40 @@ func createInviteFrom3PIDInvite(
return &event, nil return &event, nil
} }
// fillDisplayName looks in the room's state events for a m.room.third_party_invite
// event with the state key matching a given m.room.member event's content's token.
// If such an event is found, fills the "display_name" attribute of the
// "third_party_invite" structure in the m.room.member event with the display_name
// from the m.room.third_party_invite event.
// Returns an error if there was a problem parsing the m.room.third_party_invite
// event's content or updating the m.room.member event's content.
// Returns nil if no m.room.third_party_invite with a matching token could be
// found. Returning an error isn't necessary in this case as the event will be
// rejected by gomatrixserverlib.
func fillDisplayName( func fillDisplayName(
builder *gomatrixserverlib.EventBuilder, content common.MemberContent, builder *gomatrixserverlib.EventBuilder, content common.MemberContent,
stateEvents []gomatrixserverlib.Event, stateEvents []gomatrixserverlib.Event,
) error { ) error {
// Look for the m.room.third_party_invite event // Look for the m.room.third_party_invite event
var thirdPartyInviteEvent gomatrixserverlib.Event var thirdPartyInviteEvent *gomatrixserverlib.Event
for _, event := range stateEvents { for _, event := range stateEvents {
if event.Type() == "m.room.third_party_invite" { if event.Type() == "m.room.third_party_invite" && *(event.StateKey()) == content.ThirdPartyInvite.Signed.Token {
thirdPartyInviteEvent = event thirdPartyInviteEvent = &event
} }
} }
var thirdPartyInviteContent common.ThirdPartyInviteContent if thirdPartyInviteEvent != nil {
if err := json.Unmarshal(thirdPartyInviteEvent.Content(), &thirdPartyInviteContent); err != nil { var thirdPartyInviteContent common.ThirdPartyInviteContent
return err if err := json.Unmarshal(thirdPartyInviteEvent.Content(), &thirdPartyInviteContent); err != nil {
} return err
}
// Use the m.room.third_party_invite event to fill the "displayname" and // Use the m.room.third_party_invite event to fill the "displayname" and
// update the m.room.member event's content with it // update the m.room.member event's content with it
content.ThirdPartyInvite.DisplayName = thirdPartyInviteContent.DisplayName content.ThirdPartyInvite.DisplayName = thirdPartyInviteContent.DisplayName
if err := builder.SetContent(content); err != nil { if err := builder.SetContent(content); err != nil {
return err return err
}
} }
return nil return nil