package migrate import ( "context" "errors" "fmt" "net/http" "github.com/rs/zerolog" "maunium.net/go/mautrix/bridgev2" "maunium.net/go/mautrix/bridgev2/database" "maunium.net/go/mautrix/bridgev2/networkid" "maunium.net/go/mautrix/event" "maunium.net/go/mautrix/id" ) // moveChatInfo is the chat's info a move has bridgev2 create the portal's new room with. The messenger is // asked first, as bridgev2 asks it for any room it makes. A messenger that cannot describe a chat on // request says so with errors.ErrUnsupported (Messenger reads a thread's info only as threads sync: // infra/matrix/facebook/chat-info-unsupported.patch), and the chat gets the info the bridge already // holds (heldChatInfo). Any other failure, or no answer, is the messenger not answering (XSG_NETWORK), // and the old room is not let go. // // The info carries no backfill: with it bridgev2 would send the chat's latest messages into the new room // unmarked as it makes it (createMatrixRoomInLoop's doForwardBackfill, which stops at the first message // it already has, after sending it a second time) and queue a backfill task. The history is the move's // alone to write, marked. Members are always given: without them bridgev2 asks the messenger again, // and its answer would bring backfill back. // // A group the messenger names empty keeps the name the bridge holds, as the old room shows it: bridgev2 // would make the new room without one, whose encrypted metadata then names nothing, and the app shows // the group unnamed. Instagram names a group it runs over WhatsApp's protocol by the group's WhatsApp // subject (mautrix-meta wrapWAGroupInfo), empty for a group never named there, which the thread sync // named. A DM takes its contact's name, and bridgev2.DefaultChatName takes a name off on purpose. func (m *mover) moveChatInfo(ctx context.Context, login *bridgev2.UserLogin, portal *bridgev2.Portal, oldRoom id.RoomID) (*bridgev2.ChatInfo, error) { info, err := login.Client.GetChatInfo(ctx, portal) if errors.Is(err, errors.ErrUnsupported) { zerolog.Ctx(ctx).Info().Stringer("room_id", oldRoom).Msg("The messenger cannot describe the chat; moving it with the info the bridge holds") if info, err = m.heldChatInfo(ctx, login, portal, oldRoom); err != nil { return nil, err } } else if err != nil || info == nil { zerolog.Ctx(ctx).Err(err).Stringer("room_id", oldRoom).Msg("Failed to get chat info for a move") return nil, failWith(http.StatusBadGateway, "XSG_NETWORK", "The messenger did not answer") } moving := *info moving.CanBackfill = false dm := portal.RoomType == database.RoomTypeDM || (moving.Type != nil && *moving.Type == database.RoomTypeDM) if moving.Name != nil && *moving.Name == "" && moving.Name != bridgev2.DefaultChatName && portal.Name != "" && !dm { moving.Name = nil } if moving.Members == nil { held, err := m.heldChatInfo(ctx, login, portal, oldRoom) if err != nil { return nil, err } moving.Members = held.Members } return &moving, nil } // heldChatInfo reads the old room's members and power levels for chatInfoOf. func (m *mover) heldChatInfo(ctx context.Context, login *bridgev2.UserLogin, portal *bridgev2.Portal, oldRoom id.RoomID) (*bridgev2.ChatInfo, error) { members, err := m.br.Matrix.GetMembers(ctx, oldRoom) if err != nil { return nil, fmt.Errorf("failed to read the old room's members: %w", err) } levels, err := m.br.Matrix.GetPowerLevels(ctx, oldRoom) if err != nil { return nil, fmt.Errorf("failed to read the old room's power levels: %w", err) } // A copy: the state store's levels are shared, and chatInfoOf reads their users. return chatInfoOf(portal, login, members, levels.Clone(), m.br.Matrix.ParseGhostMXID), nil } // chatInfoOf is a chat's info as the bridge holds it. The portal's row keeps its name, topic, picture, // disappearing timer, parent and message-request flag, and bridgev2 makes the room with them when the // info leaves them unset (a name or picture given would be marked custom, and a DM would no longer take // its contact's). The type is the row's, and so is a DM's other user: a DM's member list without one // would clear the portal's (bridgev2 updateOtherUser). The members are those joined in the old room, // which the bridge has kept in line with the messenger: the user as themself and the contacts' ghosts, // each with the level they have there, under the old room's level rules. The bot and the other // identities sharing the connection are not the messenger's members; bridgekit invites the latter. func chatInfoOf(portal *bridgev2.Portal, login *bridgev2.UserLogin, members map[id.UserID]*event.MemberEventContent, levels *event.PowerLevelsEventContent, ghostID func(id.UserID) (networkid.UserID, bool)) *bridgev2.ChatInfo { list := &bridgev2.ChatMemberList{ OtherUserID: portal.OtherUserID, MemberMap: bridgev2.ChatMemberMap{}, PowerLevels: levelRules(levels), } for userID, member := range members { if member == nil || member.Membership != event.MembershipJoin { continue } var key networkid.UserID var sender bridgev2.EventSender if userID == login.UserMXID { // Keyed apart from any ghost: the user's own ghost may be a member too (a chat with oneself). sender = bridgev2.EventSender{IsFromMe: true, SenderLogin: login.ID} } else if ghost, ok := ghostID(userID); ok && ghost != "" { key, sender = ghost, bridgev2.EventSender{Sender: ghost} } else { continue } chatMember := bridgev2.ChatMember{EventSender: sender, Membership: event.MembershipJoin} if levels != nil { if level, ok := levels.Users[userID]; ok { chatMember.PowerLevel = &level } } list.MemberMap[key] = chatMember } roomType := portal.RoomType return &bridgev2.ChatInfo{Members: list, Type: &roomType} } // levelRules are the old room's level rules (every level but the members'), for the new room. func levelRules(levels *event.PowerLevelsEventContent) *bridgev2.PowerLevelOverrides { if levels == nil { return nil } events := make(map[event.Type]int, len(levels.Events)) for eventType, level := range levels.Events { events[ruleType(eventType, level, levels)] = level } usersDefault, eventsDefault := levels.UsersDefault, levels.EventsDefault return &bridgev2.PowerLevelOverrides{ Events: events, UsersDefault: &usersDefault, EventsDefault: &eventsDefault, StateDefault: levels.StateDefaultPtr, Invite: levels.InvitePtr, Kick: levels.KickPtr, Ban: levels.BanPtr, Redact: levels.RedactPtr, } } // ruleType names an event type of the old room's level rules with its class. bridgev2 compares a rule // with the default of its class (state_default for state events, events_default for the rest) and leaves // out one that equals it: a class lost would compare a state event's rule with events_default, and a // name rule of 0 where messages need 0 and state 50 would be left out, needing 50 in the new room. A type // mautrix does not know (m.room.history_visibility, say) gets the class whose default the rule is not, // so the rule is kept as the old room has it; when it is both defaults, leaving it out changes nothing. func ruleType(name string, level int, levels *event.PowerLevelsEventContent) event.Type { eventType := event.NewEventType(name) if eventType.Class == event.UnknownEventType { if level == levels.EventsDefault { eventType.Class = event.StateEventType } else { eventType.Class = event.MessageEventType } } return eventType }