subscriptionReply generates a response to a subscription request
(asChan bool, msg *ClientComMessage)
| 1374 | |
| 1375 | // subscriptionReply generates a response to a subscription request |
| 1376 | func (t *Topic) subscriptionReply(asChan bool, msg *ClientComMessage) error { |
| 1377 | // The topic is already initialized by the Hub |
| 1378 | |
| 1379 | msgsub := msg.Sub |
| 1380 | |
| 1381 | // For newly created topics report topic creation time. |
| 1382 | var now time.Time |
| 1383 | if msgsub.Created { |
| 1384 | now = t.updated |
| 1385 | } else { |
| 1386 | now = types.TimeNow() |
| 1387 | } |
| 1388 | |
| 1389 | asUid := types.ParseUserId(msg.AsUser) |
| 1390 | |
| 1391 | if !msgsub.Newsub && (t.cat == types.TopicCatP2P || t.cat == types.TopicCatGrp) { |
| 1392 | // Check if this is a new subscription (P2P & GRP only. SLF, SYS are excluded here). |
| 1393 | pud, found := t.perUser[asUid] |
| 1394 | msgsub.Newsub = !found || pud.deleted |
| 1395 | } |
| 1396 | |
| 1397 | var private any |
| 1398 | var mode string |
| 1399 | if msgsub.Set != nil { |
| 1400 | if msgsub.Set.Sub != nil { |
| 1401 | if msgsub.Set.Sub.User != "" { |
| 1402 | msg.sess.queueOut(ErrMalformedReply(msg, now)) |
| 1403 | return errors.New("user id must not be specified") |
| 1404 | } |
| 1405 | mode = msgsub.Set.Sub.Mode |
| 1406 | } |
| 1407 | |
| 1408 | if msgsub.Set.Desc != nil { |
| 1409 | private = msgsub.Set.Desc.Private |
| 1410 | } |
| 1411 | } |
| 1412 | |
| 1413 | var err error |
| 1414 | var modeChanged *MsgAccessMode |
| 1415 | // Create new subscription or modify an existing one. |
| 1416 | if modeChanged, err = t.thisUserSub(msg.sess, msg, asUid, asChan, mode, private); err != nil { |
| 1417 | return err |
| 1418 | } |
| 1419 | |
| 1420 | hasJoined := true |
| 1421 | if modeChanged != nil { |
| 1422 | if acs, err := types.ParseAcs([]byte(modeChanged.Mode)); err == nil { |
| 1423 | hasJoined = acs.IsJoiner() |
| 1424 | } |
| 1425 | } |
| 1426 | |
| 1427 | if hasJoined { |
| 1428 | // Subscription successfully created. Link topic to session. |
| 1429 | msg.sess.addSub(t.name, &Subscription{ |
| 1430 | broadcast: t.clientMsg, |
| 1431 | done: t.unreg, |
| 1432 | meta: t.meta, |
| 1433 | supd: t.supd, |
no test coverage detected