Split up getMembershipEventContent()

This commit is contained in:
Tulir Asokan 2018-05-22 17:36:41 +03:00
parent ff5c1292fe
commit c87097627e

View File

@ -78,30 +78,7 @@ func ParseMessage(matrix ifc.MatrixContainer, room *rooms.Room, evt *gomatrix.Ev
return nil return nil
} }
func getMembershipEventContent(room *rooms.Room, evt *gomatrix.Event) (sender string, text tstring.TString) { func getMembershipChangeMessage(evt *gomatrix.Event, membership, prevMembership, senderDisplayname, displayname, prevDisplayname string) (sender string, text tstring.TString) {
member := room.GetMember(evt.Sender)
senderDisplayname := evt.Sender
if member != nil {
senderDisplayname = member.DisplayName
}
membership, _ := evt.Content["membership"].(string)
displayname, _ := evt.Content["displayname"].(string)
if len(displayname) == 0 {
displayname = *evt.StateKey
}
prevMembership := "leave"
prevDisplayname := *evt.StateKey
if evt.Unsigned.PrevContent != nil {
prevMembership, _ = evt.Unsigned.PrevContent["membership"].(string)
prevDisplayname, _ = evt.Unsigned.PrevContent["displayname"].(string)
if len(prevDisplayname) == 0 {
prevDisplayname = *evt.StateKey
}
}
if membership != prevMembership {
switch membership { switch membership {
case "invite": case "invite":
sender = "---" sender = "---"
@ -137,6 +114,34 @@ func getMembershipEventContent(room *rooms.Room, evt *gomatrix.Event) (sender st
text.Colorize(len(senderDisplayname)+len(" banned "), len(displayname), widget.GetHashColor(*evt.StateKey)) text.Colorize(len(senderDisplayname)+len(" banned "), len(displayname), widget.GetHashColor(*evt.StateKey))
text.Colorize(0, len(senderDisplayname), widget.GetHashColor(evt.Sender)) text.Colorize(0, len(senderDisplayname), widget.GetHashColor(evt.Sender))
} }
return
}
func getMembershipEventContent(room *rooms.Room, evt *gomatrix.Event) (sender string, text tstring.TString) {
member := room.GetMember(evt.Sender)
senderDisplayname := evt.Sender
if member != nil {
senderDisplayname = member.DisplayName
}
membership, _ := evt.Content["membership"].(string)
displayname, _ := evt.Content["displayname"].(string)
if len(displayname) == 0 {
displayname = *evt.StateKey
}
prevMembership := "leave"
prevDisplayname := *evt.StateKey
if evt.Unsigned.PrevContent != nil {
prevMembership, _ = evt.Unsigned.PrevContent["membership"].(string)
prevDisplayname, _ = evt.Unsigned.PrevContent["displayname"].(string)
if len(prevDisplayname) == 0 {
prevDisplayname = *evt.StateKey
}
}
if membership != prevMembership {
sender, text = getMembershipChangeMessage(evt, membership, prevMembership, senderDisplayname, displayname, prevDisplayname)
} else if displayname != prevDisplayname { } else if displayname != prevDisplayname {
sender = "---" sender = "---"
text = tstring.NewColorTString(fmt.Sprintf("%s changed their display name to %s.", prevDisplayname, displayname), tcell.ColorGreen) text = tstring.NewColorTString(fmt.Sprintf("%s changed their display name to %s.", prevDisplayname, displayname), tcell.ColorGreen)