channels: force pre-history visible when a group gets a public username

New supergroups are created with "chat history for new members" hidden (the
client sets this right after creation, matching official Telegram). The
official server then forces it back to visible when the group is made public;
owpengram's UpdateUsername left the flag alone, leaving public groups in a
state where non-members (and post-join members) see no history at all.

UpdateUsername now clears pre_history_hidden whenever a non-empty username is
assigned, in the same transaction, with a matching admin-log event. Removing
the username leaves the flag untouched, so the creator can hide history again
once the group is private.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Astra 2026-09-09 12:46:56 +01:00
parent c376262cff
commit df15c3ffb3
3 changed files with 90 additions and 1 deletions

View file

@ -241,12 +241,29 @@ func (s *ChannelStore) UpdateUsername(ctx context.Context, req domain.UpdateChan
if err := replacePeerUsernameTx(ctx, tx, peerUsernameTypeChannel, req.ChannelID, username, usernameLower); err != nil {
return domain.Channel{}, err
}
if _, err := tx.Exec(ctx, `UPDATE channels SET username = NULLIF($2,''), updated_at = now() WHERE id = $1`, req.ChannelID, username); err != nil {
// A public group cannot keep pre-history hidden: assigning a username forces
// "chat history for new members" back to visible (matches the official
// server). Removing the username leaves the flag untouched, so the creator
// can hide history again once the group is private.
if _, err := tx.Exec(ctx, `UPDATE channels SET username = NULLIF($2,''), pre_history_hidden = (pre_history_hidden AND $2 = ''), updated_at = now() WHERE id = $1`, req.ChannelID, username); err != nil {
return domain.Channel{}, fmt.Errorf("update channel username: %w", err)
}
if err := markUserChannelMemberIndexPublicTx(ctx, tx, req.ChannelID, username != ""); err != nil {
return domain.Channel{}, err
}
if username != "" && channel.PreHistoryHidden {
if err := s.insertChannelAdminLogTx(ctx, tx, domain.ChannelAdminLogEvent{
ChannelID: req.ChannelID,
UserID: req.UserID,
Date: nowUnix(),
Type: domain.ChannelAdminLogTogglePreHistoryHidden,
PrevBool: true,
NewBool: false,
}); err != nil {
return domain.Channel{}, err
}
channel.PreHistoryHidden = false
}
prevUsername := channel.Username
channel.Username = username
if err := s.insertChannelAdminLogTx(ctx, tx, domain.ChannelAdminLogEvent{