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

Method replyDelSub

server/topic.go:3340–3428  ·  view source on GitHub ↗

Delete subscription.

(sess *Session, asUid types.Uid, msg *ClientComMessage)

Source from the content-addressed store, hash-verified

3338
3339// Delete subscription.
3340func (t *Topic) replyDelSub(sess *Session, asUid types.Uid, msg *ClientComMessage) error {
3341 now := types.TimeNow()
3342 del := msg.Del
3343
3344 asChan, err := t.verifyChannelAccess(msg.Original)
3345 if err != nil {
3346 // User should not be able to address non-channel topic as channel.
3347 sess.queueOut(ErrNotFoundReply(msg, now))
3348 return types.ErrNotFound
3349 }
3350 if asChan {
3351 // Don't allow channel readers to delete self-subscription. Use leave-unsub or del-topic.
3352 sess.queueOut(ErrPermissionDeniedReply(msg, now))
3353 return errors.New("channel access denied: cannot delete subscription")
3354 }
3355
3356 // Get ID of the affected user
3357 uid := types.ParseUserId(del.User)
3358
3359 pud := t.perUser[asUid]
3360 if !(pud.modeGiven & pud.modeWant).IsAdmin() {
3361 err = errors.New("del.sub: permission denied")
3362 } else if uid.IsZero() || uid == asUid {
3363 // Cannot delete self-subscription. User [leave unsub] or [delete topic]
3364 err = errors.New("del.sub: cannot delete self-subscription")
3365 } else if t.cat == types.TopicCatP2P {
3366 // Don't try to delete the other P2P user
3367 err = errors.New("del.sub: cannot apply to a P2P topic")
3368 }
3369
3370 if err != nil {
3371 sess.queueOut(ErrPermissionDeniedReply(msg, now))
3372 return err
3373 }
3374
3375 pud, ok := t.perUser[uid]
3376 if !ok {
3377 sess.queueOut(InfoNoActionReply(msg, now))
3378 return errors.New("del.sub: user not found")
3379 }
3380
3381 // Check if the user being ejected is the owner.
3382 if (pud.modeGiven & pud.modeWant).IsOwner() {
3383 err = errors.New("del.sub: cannot evict topic owner")
3384 } else if !pud.modeWant.IsJoiner() {
3385 // If the user has banned the topic, subscription should not be deleted. Otherwise user may be re-invited
3386 // which defeats the purpose of banning.
3387 err = errors.New("del.sub: cannot delete banned subscription")
3388 }
3389
3390 if err != nil {
3391 sess.queueOut(ErrPermissionDeniedReply(msg, now))
3392 return err
3393 }
3394
3395 // Delete user's subscription from the database
3396 if err := store.Subs.Delete(t.name, uid); err != nil {
3397 if err == types.ErrNotFound {

Callers 1

handleMetaDelMethod · 0.95

Calls 15

verifyChannelAccessMethod · 0.95
notifySubChangeMethod · 0.95
evictUserMethod · 0.95
subsCountMethod · 0.95
markPausedMethod · 0.95
TimeNowFunction · 0.92
ParseUserIdFunction · 0.92
ErrNotFoundReplyFunction · 0.85
ErrPermissionDeniedReplyFunction · 0.85
InfoNoActionReplyFunction · 0.85
ErrUnknownReplyFunction · 0.85
NoErrReplyFunction · 0.85

Tested by

no test coverage detected