MCPcopy
hub / github.com/tinode/chat / expandTopicName

Method expandTopicName

server/session.go:1327–1362  ·  view source on GitHub ↗

expandTopicName expands session specific topic name to global name Returns topic: session-specific topic name the message recipient should see routeTo: routable global topic name err: *ServerComMessage with an error to return to the sender

(msg *ClientComMessage)

Source from the content-addressed store, hash-verified

1325// routeTo: routable global topic name
1326// err: *ServerComMessage with an error to return to the sender
1327func (s *Session) expandTopicName(msg *ClientComMessage) (string, *ServerComMessage) {
1328 if msg.Original == "" {
1329 logs.Warn.Println("s.etn: empty topic name", s.sid)
1330 return "", ErrMalformed(msg.Id, "", msg.Timestamp)
1331 }
1332
1333 // Expanded name of the topic to route to i.e. rcptto: or s.subs[routeTo]
1334 var routeTo string
1335 if msg.Original == "me" {
1336 routeTo = msg.AsUser
1337 } else if msg.Original == "fnd" {
1338 routeTo = types.ParseUserId(msg.AsUser).FndName()
1339 } else if msg.Original == "slf" {
1340 routeTo = types.ParseUserId(msg.AsUser).SlfName()
1341 } else if strings.HasPrefix(msg.Original, "usr") {
1342 // p2p topic
1343 uid1 := types.ParseUserId(msg.AsUser)
1344 uid2 := types.ParseUserId(msg.Original)
1345 if uid2.IsZero() {
1346 // Ensure the user id is valid.
1347 logs.Warn.Println("s.etn: failed to parse p2p topic name", s.sid)
1348 return "", ErrMalformed(msg.Id, msg.Original, msg.Timestamp)
1349 } else if uid2 == uid1 {
1350 // Use 'me' to access self-topic.
1351 logs.Warn.Println("s.etn: invalid p2p self-subscription", s.sid)
1352 return "", ErrPermissionDeniedReply(msg, msg.Timestamp)
1353 }
1354 routeTo = uid1.P2PName(uid2)
1355 } else if tmp := types.ChnToGrp(msg.Original); tmp != "" {
1356 routeTo = tmp
1357 } else {
1358 routeTo = msg.Original
1359 }
1360
1361 return routeTo, nil
1362}
1363
1364func (s *Session) serializeAndUpdateStats(msg *ServerComMessage) any {
1365 dataSize, data := s.serialize(msg)

Callers 7

subscribeMethod · 0.95
leaveMethod · 0.95
publishMethod · 0.95
getMethod · 0.95
setMethod · 0.95
delMethod · 0.95
noteMethod · 0.95

Calls 9

ParseUserIdFunction · 0.92
ChnToGrpFunction · 0.92
ErrMalformedFunction · 0.85
ErrPermissionDeniedReplyFunction · 0.85
PrintlnMethod · 0.80
FndNameMethod · 0.80
SlfNameMethod · 0.80
P2PNameMethod · 0.80
IsZeroMethod · 0.45

Tested by

no test coverage detected