welcome message: link @zio with a mention entity
This commit is contained in:
parent
4d781a52bb
commit
d4fe056854
2 changed files with 62 additions and 2 deletions
|
|
@ -10,6 +10,14 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
// officialUpdatesChannelMention is the public @username of the updates channel
|
||||
// linked from the welcome message when the resolved body includes it. It is
|
||||
// carried in a MessageEntityMention wherever it appears in body so clients
|
||||
// render it as a tappable link; the mention itself lives in the default
|
||||
// templates in login_welcome_template.go (or an admin-panel override), not
|
||||
// hardcoded here.
|
||||
const officialUpdatesChannelMention = "@zio"
|
||||
|
||||
// OfficialWelcomeMessage builds the account-visible incoming message sent
|
||||
// from the official system account on every completed sign-in (SignUp and
|
||||
// every subsequent SignIn/SignInWithEmail), regardless of delivery channel.
|
||||
|
|
@ -28,13 +36,42 @@ func OfficialWelcomeMessage(userID int64, body string, date int) (Message, error
|
|||
if userID <= 0 || IsSystemUserID(userID) || body == "" || date < 0 || date > math.MaxInt32 {
|
||||
return Message{}, fmt.Errorf("%w: user=%d date=%d", ErrLoginCodeDeliveryInvalid, userID, date)
|
||||
}
|
||||
return Message{
|
||||
msg := Message{
|
||||
OwnerUserID: userID,
|
||||
Peer: Peer{Type: PeerTypeUser, ID: OfficialSystemUserID},
|
||||
From: Peer{Type: PeerTypeUser, ID: OfficialSystemUserID},
|
||||
Date: date,
|
||||
Body: body,
|
||||
}, nil
|
||||
}
|
||||
if ent, ok := usernameMentionEntity(body, officialUpdatesChannelMention); ok {
|
||||
msg.Entities = []MessageEntity{ent}
|
||||
}
|
||||
return msg, nil
|
||||
}
|
||||
|
||||
// usernameMentionEntity locates mention ("@name") in text and returns a
|
||||
// MessageEntityMention spanning it, with the UTF-16 offset/length clients expect.
|
||||
func usernameMentionEntity(text, mention string) (MessageEntity, bool) {
|
||||
before, _, found := strings.Cut(text, mention)
|
||||
if !found {
|
||||
return MessageEntity{}, false
|
||||
}
|
||||
return MessageEntity{
|
||||
Type: MessageEntityMention,
|
||||
Offset: utf16Len(before),
|
||||
Length: utf16Len(mention),
|
||||
}, true
|
||||
}
|
||||
|
||||
func utf16Len(s string) int {
|
||||
n := 0
|
||||
for _, r := range s {
|
||||
n++
|
||||
if r > 0xffff {
|
||||
n++
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// SignInMethodLabel returns the human-readable method name embedded in
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue