routePres method

void routePres(
  1. PresMessage pres
)

Process presence change message

Implementation

void routePres(PresMessage pres) {
  TopicSubscription? user;
  switch (pres.what) {
    case 'del':
      // Delete cached messages.
      processDelMessages(pres.clear!, pres.delseq!);
      break;

    case 'on':
    case 'off':
      // Update online status of a subscription.
      user = _users[pres.src];
      if (user != null) {
        user.online = pres.what == 'on';
      } else {
        _loggerService.warn('Presence update for an unknown user' +
            (name ?? '') +
            ' ' +
            (pres.src ?? ''));
      }
      break;

    case 'term':
      // Attachment to topic is terminated probably due to cluster rehashing.
      resetSubscription();
      break;

    case 'acs':
      var userId = pres.src ?? _authService.userId;
      user = _users[userId];

      if (user == null) {
        // Update for an unknown user: notification of a new subscription.
        AccessMode? acs = AccessMode(null).updateAll(pres.dacs);
        if (acs.mode != NONE) {
          user = _cacheManager.getUser(userId ?? '');

          // ignore: unnecessary_null_comparison
          if (user == null) {
            user = TopicSubscription(user: userId, acs: acs);
            getMeta(startMetaQuery().withOneSub(null, userId).build());
          } else {
            user.acs = acs;
          }

          user.updated = DateTime.now();
          processMetaSub([user]);
        }
      } else {
        // Known user
        user.acs!.updateAll(pres.dacs);
        // Update user's access mode.
        processMetaSub([
          TopicSubscription(
              user: userId, updated: DateTime.now(), acs: user.acs)
        ]);
      }

      break;
    default:
      _loggerService.log('Ignored presence update ' + (pres.what ?? ''));
  }

  onPres.add(pres);
}