package migrate import ( "bytes" "encoding/json" ) // item is one event a move writes into the new room. type item struct { // Seq numbers the written events from 1, in the old room's order. It goes into the marker. Seq int // Old is the message or reaction as the old room has it. Old Event // Content is what is written, before its relations are pointed at the new room: a message's // latest edit by the same sender (keeping the original's reply or thread and its album), otherwise // the event's own content. Content json.RawMessage // Reaction is set for an m.reaction. Reaction bool } // plan is what a move writes, worked out from the whole old room in its timeline order. type plan struct { Items []item // Undigestable counts the messages and reactions Counter counts that are left out because // canonical JSON cannot write their content (and the reactions on such messages): the app could // not hash them as the bridge does. Items + Undigestable = Counter's messages + reactions. Undigestable int } type relatesTo struct { RelType string `json:"rel_type"` EventID string `json:"event_id"` InReplyTo *struct { EventID string `json:"event_id"` } `json:"m.in_reply_to"` } func relationOf(content json.RawMessage) relatesTo { var holder struct { RelatesTo relatesTo `json:"m.relates_to"` } _ = json.Unmarshal(content, &holder) return holder.RelatesTo } // buildPlan works out what a move writes from the old room's events, given in timeline order. // It follows Counter: encrypted, state and redacted events are left out, an edit is no message of // its own, and a reaction goes along only with the message it is on. func buildPlan(events []Event) plan { type message struct { evt Event latest json.RawMessage } messages := map[string]*message{} var order []Event for _, evt := range events { if evt.StateKey != nil || evt.Type == "m.room.encrypted" || isRedacted(evt) { continue } relation := relationOf(evt.Content) switch { case messageTypes[evt.Type] && relation.RelType == "m.replace": // The latest edit by the message's own sender is what the message says now (the spec // lets nobody else edit it). Edits come after the message they change. target := messages[relation.EventID] if target == nil || target.evt.Sender != evt.Sender { continue } if newContent := newContentOf(evt.Content); newContent != nil { target.latest = newContent } case messageTypes[evt.Type]: messages[evt.ID] = &message{evt: evt} order = append(order, evt) case evt.Type == "m.reaction" && relation.RelType == "m.annotation" && relation.EventID != "": order = append(order, evt) } } var result plan written := map[string]bool{} leftOut := map[string]bool{} for _, evt := range order { if evt.Type == "m.reaction" { target := relationOf(evt.Content).EventID switch { case leftOut[target]: result.Undigestable++ case !written[target]: // Not on a message Counter counts: it was never going along. case !digestable(evt.Content): result.Undigestable++ default: result.Items = append(result.Items, item{Seq: len(result.Items) + 1, Old: evt, Content: evt.Content, Reaction: true}) } continue } content := evt.Content if latest := messages[evt.ID].latest; latest != nil { content = editedContent(latest, evt.Content) } if !digestable(content) { result.Undigestable++ leftOut[evt.ID] = true continue } written[evt.ID] = true result.Items = append(result.Items, item{Seq: len(result.Items) + 1, Old: evt, Content: content}) } return result } func digestable(content json.RawMessage) bool { _, err := Canonical(content) return err == nil } // newContentOf returns an edit's m.new_content when it is an object. func newContentOf(content json.RawMessage) json.RawMessage { var holder struct { NewContent json.RawMessage `json:"m.new_content"` } if json.Unmarshal(content, &holder) != nil { return nil } trimmed := bytes.TrimSpace(holder.NewContent) if len(trimmed) == 0 || trimmed[0] != '{' { return nil } return holder.NewContent } // keptFromOriginal are marks of the original message an edit's new content may lack and the moved // message keeps: the album a photo came in (a WhatsApp HD copy arrives as an edit of the photo, and // its new content has no album mark). var keptFromOriginal = []string{"com.xessenger.album"} // editedContent is an edit's new content as the moved message: with the reply or thread of the // original message (m.new_content carries no m.relates_to of its own) and the original's marks it // lacks. func editedContent(newContent, original json.RawMessage) json.RawMessage { content, err := decodeObject(newContent) if err != nil { return newContent } delete(content, "m.relates_to") delete(content, "m.new_content") if holder, err := decodeObject(original); err == nil { if relation, ok := holder["m.relates_to"]; ok { content["m.relates_to"] = relation } for _, key := range keptFromOriginal { if value, ok := holder[key]; ok { if _, own := content[key]; !own { content[key] = value } } } } merged, err := json.Marshal(content) if err != nil { return newContent } return merged } // decodeObject decodes a JSON object keeping its numbers as they are written. func decodeObject(raw json.RawMessage) (map[string]any, error) { decoder := json.NewDecoder(bytes.NewReader(raw)) decoder.UseNumber() var value map[string]any if err := decoder.Decode(&value); err != nil { return nil, err } if value == nil { return nil, errNotObject } return value, nil }