From fb2de8d603b8934747d877c5b748e1cf4758f47b Mon Sep 17 00:00:00 2001 From: Grayson Adams Date: Thu, 26 Nov 2026 18:23:49 -0500 Subject: [PATCH 1/4] refactor: move the group name refresh decision into a helper --- pkg/connector/macoun_group_names.go | 26 ++++++++++++++++++++++++++ pkg/connector/sync_controller.go | 8 +------- 2 files changed, 27 insertions(+), 7 deletions(-) create mode 100644 pkg/connector/macoun_group_names.go diff --git a/pkg/connector/macoun_group_names.go b/pkg/connector/macoun_group_names.go new file mode 100644 index 0000000..584fa28 --- /dev/null +++ b/pkg/connector/macoun_group_names.go @@ -0,0 +1,26 @@ +// macoun-matrix - A Matrix-iMessage puppeting bridge (fork of corten-matrix). +// SPDX-License-Identifier: MPL-2.0 +// +// macoun: how the periodic contact sync decides whether a group room's name +// changes. Names the bridge sets are local to the homeserver: bridgev2 drops +// the bridge bot's own m.room.name events before they reach +// HandleMatrixRoomName, so none of this is ever sent to Apple. + +package connector + +// planGroupNameRefresh reports whether refreshGroupPortalNamesFromContacts +// should set newName on a group portal whose room is called current. +// authoritative is resolveGroupName's second result: true for a name a +// participant gave the group, false for one built from the members. +func planGroupNameRefresh(current, newName string, authoritative bool) (rename bool) { + if newName == "" || newName == current { + return false + } + // Don't overwrite an existing portal name with a contact-derived + // fallback: only authoritative sources (user-set iMessage group names + // from the in-memory cache or CloudKit) rename. + if !authoritative && current != "" { + return false + } + return true +} diff --git a/pkg/connector/sync_controller.go b/pkg/connector/sync_controller.go index af15615..a759548 100644 --- a/pkg/connector/sync_controller.go +++ b/pkg/connector/sync_controller.go @@ -1768,13 +1768,7 @@ func (c *IMClient) refreshGroupPortalNamesFromContacts(log zerolog.Logger) { total++ newName, authoritative := c.resolveGroupName(ctx, portalID) - if newName == "" || newName == portal.Name { - continue - } - // Don't overwrite an existing portal name with a contact-derived - // fallback — only authoritative sources (user-set iMessage group - // names from the in-memory cache or CloudKit) should rename. - if !authoritative && portal.Name != "" { + if !planGroupNameRefresh(portal.Name, newName, authoritative) { continue } From 006f0a3db77cdd725ef997c136789d82ba341eca Mon Sep 17 00:00:00 2001 From: Grayson Adams Date: Thu, 26 Nov 2026 18:23:49 -0500 Subject: [PATCH 2/4] test: reproduce a group room stuck on the group chat placeholder --- pkg/connector/macoun_group_names_test.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 pkg/connector/macoun_group_names_test.go diff --git a/pkg/connector/macoun_group_names_test.go b/pkg/connector/macoun_group_names_test.go new file mode 100644 index 0000000..85a75a5 --- /dev/null +++ b/pkg/connector/macoun_group_names_test.go @@ -0,0 +1,24 @@ +// macoun-matrix - A Matrix-iMessage puppeting bridge (fork of corten-matrix). +// SPDX-License-Identifier: MPL-2.0 + +package connector + +import "testing" + +// A group room created before its members were known was named "Group Chat" +// and kept that name after the roster arrived (seen on a live bridge: a +// three-person iMessage group, named "Group Chat" for months +// while its cloud_chat row lists both other members). +func TestGroupNameRefreshReplacesPlaceholder(t *testing.T) { + if !planGroupNameRefresh("Group Chat", "Sam Rivera, Alex Chen", false) { + t.Fatal(`a built name should replace the "Group Chat" placeholder`) + } +} + +// With no members to name the group by, there is no name to give it: the +// room is created unnamed and named once the roster is known. +func TestBuildGroupNameWithoutMembersIsEmpty(t *testing.T) { + if got := (&IMClient{}).buildGroupName(nil); got != "" { + t.Fatalf("buildGroupName(nil) = %q, want empty", got) + } +} From 8cf179ec565e5d0c2d2ed1e6f137516552108ac6 Mon Sep 17 00:00:00 2001 From: Grayson Adams Date: Thu, 26 Nov 2026 18:26:24 -0500 Subject: [PATCH 3/4] fix: name unnamed groups from their members and keep the name current A group room created before its roster was known was named "Group Chat", and the contact refresh never replaced a non-authoritative name once a room had one, so the placeholder stuck. Rooms with no known members now stay unnamed until the roster arrives. The name the bridge builds is recorded in portal metadata, and the refresh replaces a room name only when it is empty, the old placeholder, or that recorded name, so a name the user or a participant chose is kept. Room names the bridge sets are its own events, which bridgev2 never hands to HandleMatrixRoomName, so none reach Apple. --- pkg/connector/client.go | 34 ++++++--- pkg/connector/commands.go | 4 +- pkg/connector/dbmeta.go | 8 ++- pkg/connector/macoun_group_names.go | 90 ++++++++++++++++++++---- pkg/connector/macoun_group_names_test.go | 56 ++++++++++++++- pkg/connector/macoun_groups.go | 1 + pkg/connector/sync_controller.go | 41 +++++++---- 7 files changed, 189 insertions(+), 45 deletions(-) diff --git a/pkg/connector/client.go b/pkg/connector/client.go index e42e154..b96d9e6 100644 --- a/pkg/connector/client.go +++ b/pkg/connector/client.go @@ -7859,9 +7859,19 @@ func (c *IMClient) GetChatInfo(ctx context.Context, portal *bridgev2.Portal) (*b // Setting the name here for existing portals would revert correct // names to stale values from CloudKit or metadata, and produce // unwanted bridge bot "name changed" events. + // A built name is recorded as the bridge's own (GeneratedName), so + // later contact syncs may replace it; see macoun_group_names.go. + var generatedName *string if portal.MXID == "" || portal.Name == "" { - groupName, _ := c.resolveGroupName(ctx, portalID) - chatInfo.Name = &groupName + groupName, authoritative := c.resolveGroupName(ctx, portalID) + if groupName != "" { + chatInfo.Name = &groupName + if !authoritative { + generatedName = &groupName + } else { + generatedName = new(string) + } + } } // Set group photo from cache or CloudKit. @@ -7937,6 +7947,9 @@ func (c *IMClient) GetChatInfo(ctx context.Context, portal *bridgev2.Portal) (*b return changed } } + if generatedName != nil { + chatInfo.ExtraUpdates = recordGeneratedGroupName(*generatedName, chatInfo.ExtraUpdates) + } } else { chatInfo.Type = ptr.Ptr(database.RoomTypeDM) otherUser := makeUserID(portalID) @@ -11906,7 +11919,7 @@ func (c *IMClient) makePortalKey(participants []string, groupName *string, sende // Cache normalized participants in memory so resolveGroupMembers // can find them immediately. The cloud_chat DB write happens async // and may not complete before GetChatInfo is called during portal - // creation, which would cause the group name to resolve as "Group Chat". + // creation, which would leave the new room without a name. if len(participants) > 0 { normalized := make([]string, 0, len(participants)) for _, p := range participants { @@ -12354,15 +12367,14 @@ func (c *IMClient) resolveGroupName(ctx context.Context, portalID string) (name } // 3) Build from contact-resolved member names (fallback, non-authoritative) - members := c.resolveGroupMembers(ctx, portalID) - if len(members) == 0 { - return "Group Chat", false - } - return c.buildGroupName(members), false + // With no members known yet there is nothing to name the group by: the + // result is empty and the room stays unnamed until the roster arrives. + return c.buildGroupName(c.resolveGroupMembers(ctx, portalID)), false } // buildGroupName creates a human-readable group name from member identifiers // by resolving contact names where possible, falling back to phone/email. +// It returns "" when no member other than the user is known. func (c *IMClient) buildGroupName(members []string) string { var names []string for _, memberID := range members { @@ -12395,7 +12407,7 @@ func (c *IMClient) buildGroupName(members []string) string { names = append(names, name) } if len(names) == 0 { - return "Group Chat" + return "" } if len(names) <= 4 { return strings.Join(names, ", ") @@ -14023,7 +14035,9 @@ func (c *IMClient) chatDBInfoToBridgev2(info *imessage.ChatInfo) *bridgev2.ChatI if displayName == "" { displayName = c.buildGroupName(info.Members) } - chatInfo.Name = &displayName + if displayName != "" { + chatInfo.Name = &displayName + } } if parsed.IsGroup { diff --git a/pkg/connector/commands.go b/pkg/connector/commands.go index 15186fd..6439468 100644 --- a/pkg/connector/commands.go +++ b/pkg/connector/commands.go @@ -763,7 +763,7 @@ func fnRestoreChatFromCloudKit(ce *commands.Event, login *bridgev2.UserLogin, cl } } if len(normalized) > 0 { - if built := client.buildGroupName(normalized); built != "" && built != "Group Chat" { + if built := client.buildGroupName(normalized); built != "" { name = built } } @@ -1101,7 +1101,7 @@ func friendlyPortalName(ctx context.Context, bridge *bridgev2.Bridge, client *IM // For group chats, resolve from cloud store (display_name / contact names). isGroup := strings.HasPrefix(portalID, "gid:") || strings.Contains(portalID, ",") if isGroup && client != nil { - if name, _ := client.resolveGroupName(ctx, portalID); name != "" && name != "Group Chat" { + if name, _ := client.resolveGroupName(ctx, portalID); name != "" { return name } } diff --git a/pkg/connector/dbmeta.go b/pkg/connector/dbmeta.go index 2e916fd..9c9a2cb 100644 --- a/pkg/connector/dbmeta.go +++ b/pkg/connector/dbmeta.go @@ -14,8 +14,12 @@ import ( type PortalMetadata struct { ThreadID string `json:"thread_id,omitempty"` SenderGuid string `json:"sender_guid,omitempty"` // Persistent iMessage group UUID - GroupName string `json:"group_name,omitempty"` // iMessage cv_name for outbound routing - IsSms bool `json:"is_sms,omitempty"` // True if this portal routes through SMS + GroupName string `json:"group_name,omitempty"` // iMessage cv_name for outbound routing + IsSms bool `json:"is_sms,omitempty"` // True if this portal routes through SMS + // GeneratedName is the room name the bridge last built from the members + // of an unnamed group. While the room still has it, it is refreshed; see + // macoun_group_names.go. Local to Matrix, never sent to Apple. + GeneratedName string `json:"generated_name,omitempty"` } type GhostMetadata struct{} diff --git a/pkg/connector/macoun_group_names.go b/pkg/connector/macoun_group_names.go index 584fa28..52374aa 100644 --- a/pkg/connector/macoun_group_names.go +++ b/pkg/connector/macoun_group_names.go @@ -5,22 +5,86 @@ // changes. Names the bridge sets are local to the homeserver: bridgev2 drops // the bridge bot's own m.room.name events before they reach // HandleMatrixRoomName, so none of this is ever sent to Apple. +// +// A group nobody has named gets a name built from its members ("Sam +// Rivera, Alex Chen"). The bridge records that name in +// PortalMetadata.GeneratedName, and while the room still carries it, later +// syncs replace it as the roster or contact names change. A name a +// participant gives the group, or one the user sets from Matrix, is not the +// recorded name, so it is never overwritten by a built one. package connector -// planGroupNameRefresh reports whether refreshGroupPortalNamesFromContacts -// should set newName on a group portal whose room is called current. -// authoritative is resolveGroupName's second result: true for a name a -// participant gave the group, false for one built from the members. -func planGroupNameRefresh(current, newName string, authoritative bool) (rename bool) { - if newName == "" || newName == current { - return false +import ( + "context" + + "maunium.net/go/mautrix/bridgev2" +) + +// groupNamePlaceholder is the name earlier builds gave a group whose members +// were not known yet. A room still called this is treated as unnamed. +const groupNamePlaceholder = "Group Chat" + +// planGroupNameRefresh reports what refreshGroupPortalNamesFromContacts does +// with a group portal whose room is called current. newName and +// authoritative are resolveGroupName's results: authoritative is true for a +// name a participant gave the group, false for one built from the members. +// rename means set newName on the room. record means PortalMetadata. +// GeneratedName must be rewritten: to newName when that is a built name, or +// cleared when the group has a participant's name. A room named before +// GeneratedName existed, whose name is exactly what the bridge builds today, +// gets record without rename. +func planGroupNameRefresh(current, newName string, authoritative bool, meta *PortalMetadata) (rename, record bool) { + if newName == "" { + return false, false + } + if newName != current { + if !authoritative && !groupNameIsBridgeBuilt(current, meta) { + // A name someone chose (from Matrix, or a carrier group's + // own name) is not replaced by a built one. + return false, false + } + return true, true + } + want := "" + if !authoritative { + want = newName + } + have := "" + if meta != nil { + have = meta.GeneratedName + } + return false, want != have +} + +// groupNameIsBridgeBuilt reports whether a group room's name is one the bridge +// chose itself and may replace: none, the old placeholder, or the name it +// last built. +func groupNameIsBridgeBuilt(current string, meta *PortalMetadata) bool { + if current == "" || current == groupNamePlaceholder { + return true } - // Don't overwrite an existing portal name with a contact-derived - // fallback: only authoritative sources (user-set iMessage group names - // from the in-memory cache or CloudKit) rename. - if !authoritative && current != "" { - return false + return meta != nil && meta.GeneratedName != "" && current == meta.GeneratedName +} + +// recordGeneratedGroupName returns an ExtraUpdates step that stores the name +// the bridge built (or clears the record when the name is a participant's), +// running next after if one is given. +func recordGeneratedGroupName(generated string, next bridgev2.ExtraUpdater[*bridgev2.Portal]) bridgev2.ExtraUpdater[*bridgev2.Portal] { + return func(ctx context.Context, p *bridgev2.Portal) bool { + changed := false + if next != nil { + changed = next(ctx, p) + } + meta, ok := p.Metadata.(*PortalMetadata) + if !ok { + meta = &PortalMetadata{} + } + if meta.GeneratedName != generated { + meta.GeneratedName = generated + p.Metadata = meta + changed = true + } + return changed } - return true } diff --git a/pkg/connector/macoun_group_names_test.go b/pkg/connector/macoun_group_names_test.go index 85a75a5..551e8a0 100644 --- a/pkg/connector/macoun_group_names_test.go +++ b/pkg/connector/macoun_group_names_test.go @@ -3,15 +3,22 @@ package connector -import "testing" +import ( + "context" + "testing" + + "maunium.net/go/mautrix/bridgev2" + "maunium.net/go/mautrix/bridgev2/database" +) // A group room created before its members were known was named "Group Chat" // and kept that name after the roster arrived (seen on a live bridge: a // three-person iMessage group, named "Group Chat" for months // while its cloud_chat row lists both other members). func TestGroupNameRefreshReplacesPlaceholder(t *testing.T) { - if !planGroupNameRefresh("Group Chat", "Sam Rivera, Alex Chen", false) { - t.Fatal(`a built name should replace the "Group Chat" placeholder`) + rename, record := planGroupNameRefresh("Group Chat", "Sam Rivera, Alex Chen", false, &PortalMetadata{}) + if !rename || !record { + t.Fatalf(`placeholder: rename=%v record=%v, want both true`, rename, record) } } @@ -22,3 +29,46 @@ func TestBuildGroupNameWithoutMembersIsEmpty(t *testing.T) { t.Fatalf("buildGroupName(nil) = %q, want empty", got) } } + +func TestGroupNameRefreshCases(t *testing.T) { + built := &PortalMetadata{GeneratedName: "Alex Chen, Sam Rivera"} + cases := []struct { + name string + current, newName string + authoritative bool + meta *PortalMetadata + wantRename, wantRecord bool + }{ + {"unnamed room gets a built name", "", "Alex Chen", false, nil, true, true}, + {"built name follows the roster", "Alex Chen, Sam Rivera", "Alex Chen, Sam Rivera, Jo Park", false, built, true, true}, + {"name the user chose is kept", "Weekend trip", "Alex Chen, Sam Rivera", false, built, false, false}, + {"carrier group's own name is kept", "Book Club", "Alex Chen, Sam Rivera, Jo Park", false, &PortalMetadata{}, false, false}, + {"participant's name replaces a chosen one", "Weekend trip", "Book Club", true, nil, true, true}, + {"older room already named after its members is recorded", "Alex Chen, Sam Rivera", "Alex Chen, Sam Rivera", false, &PortalMetadata{}, false, true}, + {"recorded and unchanged does nothing", "Alex Chen, Sam Rivera", "Alex Chen, Sam Rivera", false, built, false, false}, + {"participant names the group as the built name: record cleared", "Alex Chen, Sam Rivera", "Alex Chen, Sam Rivera", true, built, false, true}, + {"nothing to build", "Group Chat", "", false, nil, false, false}, + } + for _, tc := range cases { + rename, record := planGroupNameRefresh(tc.current, tc.newName, tc.authoritative, tc.meta) + if rename != tc.wantRename || record != tc.wantRecord { + t.Errorf("%s: rename=%v record=%v, want %v %v", tc.name, rename, record, tc.wantRename, tc.wantRecord) + } + } +} + +func TestRecordGeneratedGroupNameRunsNextAndStores(t *testing.T) { + ran := false + next := func(context.Context, *bridgev2.Portal) bool { ran = true; return false } + p := &bridgev2.Portal{Portal: &database.Portal{Metadata: &PortalMetadata{SenderGuid: "G"}}} + if !recordGeneratedGroupName("Alex Chen", next)(context.Background(), p) { + t.Fatal("storing a new name should report a change") + } + meta := p.Metadata.(*PortalMetadata) + if !ran || meta.GeneratedName != "Alex Chen" || meta.SenderGuid != "G" { + t.Fatalf("ran=%v meta=%+v", ran, meta) + } + if recordGeneratedGroupName("Alex Chen", nil)(context.Background(), p) { + t.Fatal("storing the same name again should report no change") + } +} diff --git a/pkg/connector/macoun_groups.go b/pkg/connector/macoun_groups.go index d454842..9aca497 100644 --- a/pkg/connector/macoun_groups.go +++ b/pkg/connector/macoun_groups.go @@ -675,6 +675,7 @@ func (c *IMClient) CreateGroup(ctx context.Context, params *bridgev2.GroupCreate // GetChatInfo gives unnamed groups. if built := c.buildGroupName(participants); built != "" { info.Name = &built + info.ExtraUpdates = recordGeneratedGroupName(built, info.ExtraUpdates) } } return &bridgev2.CreateChatResponse{PortalKey: portalKey, PortalInfo: info}, nil diff --git a/pkg/connector/sync_controller.go b/pkg/connector/sync_controller.go index a759548..b02f4b3 100644 --- a/pkg/connector/sync_controller.go +++ b/pkg/connector/sync_controller.go @@ -454,7 +454,7 @@ func (c *IMClient) seedDeletedChatsFromRecycleBin(log zerolog.Logger) { } } if len(normalized) > 0 { - if built := c.buildGroupName(normalized); built != "" && built != "Group Chat" { + if built := c.buildGroupName(normalized); built != "" { name = built } } @@ -1738,10 +1738,11 @@ func (c *IMClient) refreshGhostNamesFromContacts(log zerolog.Logger) { log.Info().Int("reconciled", reconciled).Int("total", len(ghosts)).Msg("Reconciled ghost profiles from contacts") } -// refreshGroupPortalNamesFromContacts re-resolves group portal names using -// contact data. Portals created before contacts loaded may have raw phone -// numbers / email addresses as the room name. This also picks up contact -// edits on subsequent periodic syncs. +// refreshGroupPortalNamesFromContacts re-resolves group portal names on every +// contact sync. A participant's name for the group replaces whatever the room +// has; a name built from the members replaces only a name the bridge built +// itself, so contact edits and roster changes reach unnamed groups while a +// name the user chose stays (see planGroupNameRefresh). func (c *IMClient) refreshGroupPortalNamesFromContacts(log zerolog.Logger) { if c.contacts == nil { return @@ -1768,9 +1769,24 @@ func (c *IMClient) refreshGroupPortalNamesFromContacts(log zerolog.Logger) { total++ newName, authoritative := c.resolveGroupName(ctx, portalID) - if !planGroupNameRefresh(portal.Name, newName, authoritative) { + meta, _ := portal.Metadata.(*PortalMetadata) + rename, record := planGroupNameRefresh(portal.Name, newName, authoritative, meta) + if !rename && !record { continue } + info := &bridgev2.ChatInfo{ + // Exclude from timeline so Beeper doesn't render + // contact-based name resolution as a bridge bot message. + ExcludeChangesFromTimeline: true, + } + generated := "" + if !authoritative { + generated = newName + } + info.ExtraUpdates = recordGeneratedGroupName(generated, nil) + if rename { + info.Name = &newName + } c.UserLogin.QueueRemoteEvent(&simplevent.ChatInfoChange{ EventMeta: simplevent.EventMeta{ @@ -1783,16 +1799,11 @@ func (c *IMClient) refreshGroupPortalNamesFromContacts(log zerolog.Logger) { return lc.Str("portal_id", portalID).Str("source", "group_name_refresh") }, }, - ChatInfoChange: &bridgev2.ChatInfoChange{ - ChatInfo: &bridgev2.ChatInfo{ - Name: &newName, - // Exclude from timeline so Beeper doesn't render - // contact-based name resolution as a bridge bot message. - ExcludeChangesFromTimeline: true, - }, - }, + ChatInfoChange: &bridgev2.ChatInfoChange{ChatInfo: info}, }) - updated++ + if rename { + updated++ + } } log.Info().Int("updated", updated).Int("total_groups", total).Msg("Refreshed group portal names from contacts") } From 0766313afd82b6210821f6a23752ccb3f5509539 Mon Sep 17 00:00:00 2001 From: Grayson Adams Date: Thu, 26 Nov 2026 18:39:17 -0500 Subject: [PATCH 4/4] docs: describe how unnamed groups are named --- CHANGELOG.md | 3 +++ docs/bridge-guide.md | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ddb01be..5002fc1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,9 @@ * Fixed members of an iMessage group who never post not appearing in the Matrix room. Every live group message now joins the members on Apple's roster. +* Fixed an iMessage group created before its members were known staying named + "Group Chat". Unnamed groups are named after their members, locally only, + and the name follows roster and contact changes. * Fixed group ghosts being removed from a room whose stored roster was unknown. * Fixed starting a chat through the provisioning API with a bare phone number or email (no `tel:` or `mailto:`) failing with IDS error 6001. diff --git a/docs/bridge-guide.md b/docs/bridge-guide.md index 9f85bc5..3946b57 100644 --- a/docs/bridge-guide.md +++ b/docs/bridge-guide.md @@ -151,6 +151,11 @@ groups cannot: their rooms refuse these changes. group when you send the first message in the new room. - **Rename.** Change the room name. The group is renamed for everyone. A group name cannot be cleared from Matrix. +- **Unnamed groups.** A group nobody has named gets a room name built from its + members' contact names, which follows later roster and contact changes. + That name exists only on your homeserver; it is not sent to the group. Once + a participant names the group, or you rename the room, that name is used + and the built one is not brought back. - **Photo.** Change or remove the room avatar. The picture is cropped to a square and sent as the group photo. - **Add and remove.** Use the room's own invite and remove on a contact's