Gitter migration: Setup redirects (rollout pt. 3)
[gitter.git] / modules / presence / redis-lua / presence-disassociate.lua
blob7e385dc04861434eb105b774bcad90cb2a729694
1 local key_socket = KEYS[1];
2 local key_active_users = KEYS[2];
3 local key_mobile_users = KEYS[3];
4 local key_active_sockets = KEYS[4];
5 local key_user_lock = KEYS[5];
6 local key_troupe_users = KEYS[6];
7 local key_user_sockets = KEYS[7];
9 local user_id = ARGV[1];
10 local socket_id = ARGV[2];
13 -- If the socket doesn't exist, return with a failure code
14 if redis.call("EXISTS", key_socket) == 0 then
15 return { 0 }
16 end
18 redis.call("INCR", key_user_lock);
19 redis.call("EXPIRE", key_user_lock, 10);
21 local user_socket_count
23 if redis.call("HEXISTS", key_socket, "mob") == 1 then
24 -- Decrement the users score in mobile users
25 user_socket_count = -1
26 redis.call("ZINCRBY", key_mobile_users, -1, user_id)
27 redis.call("ZREMRANGEBYSCORE", key_mobile_users, '-inf', 0)
29 else
30 -- Decrement the users score in active users
31 user_socket_count = redis.call("ZINCRBY", key_active_users, -1, user_id)
32 redis.call("ZREMRANGEBYSCORE", key_active_users, '-inf', 0)
34 end
37 local socket_del_result = redis.call("SREM", key_active_sockets, socket_id)
38 redis.call("SREM", key_user_sockets, socket_id)
42 -- Do an eyeballs off as part of the disassociate
43 local user_in_troupe_count = -1
44 local total_in_troupe_count = -1
46 if redis.call("HEXISTS", key_socket, "eb") == 1 then
47 user_in_troupe_count = redis.call("ZINCRBY", key_troupe_users, -1, user_id)
48 redis.call("ZREMRANGEBYSCORE", key_troupe_users, '-inf', '0')
50 total_in_troupe_count = redis.call("ZCARD", key_troupe_users)
51 end
53 redis.call("DEL", key_socket)
57 return { 1, user_socket_count, socket_del_result, user_in_troupe_count, total_in_troupe_count }