1 local key_socket_user
= KEYS
[1];
2 local key_user_lock
= KEYS
[2];
3 local key_troupe_users
= KEYS
[3];
4 local key_prev_troupe_users
= KEYS
[4];
6 local user_id
= ARGV
[1];
7 local socket_id
= ARGV
[2];
8 local troupe_id
= ARGV
[3];
9 local known_prev_troupe_id
= ARGV
[4];
10 local is_eyeballs_on
= ARGV
[5];
12 if redis
.call("EXISTS", key_socket_user
) ~= 1 then
13 -- socket does not exist
17 redis
.call("INCR", key_user_lock
);
18 redis
.call("EXPIRE", key_user_lock
, 10);
20 local prev_troupe_id
= redis
.call("HGET", key_socket_user
, "tid")
21 local was_eyeballs_on
= redis
.call("HGET", key_socket_user
, "eb") == "1"
23 local user_in_troupe_count
= -1
24 local user_in_prev_troupe_count
= -1
26 -- Theres a very small chance the troupe has changed this the app server
27 -- queried for the existing value. Double check and don't process
28 -- this request if that's the case
29 if known_prev_troupe_id
~= prev_troupe_id
then
33 -- Eyeballs off the previous troupe
34 if was_eyeballs_on
and prev_troupe_id
then
35 user_in_prev_troupe_count
= redis
.call("ZINCRBY", key_prev_troupe_users
, -1, user_id
)
36 redis
.call("ZREMRANGEBYSCORE", key_prev_troupe_users
, '-inf', '0')
39 if troupe_id
and troupe_id
~= "" then
40 -- If we are reassociating with a new troupe
41 redis
.call("HSET", key_socket_user
, "tid", troupe_id
)
43 if is_eyeballs_on
and user_id
and user_id
~= "" then
44 -- Set eyeballs status for non-anonymous users
45 user_in_troupe_count
= redis
.call("ZINCRBY", key_troupe_users
, 1, user_id
)
46 redis
.call("HSET", key_socket_user
, "eb", 1)
48 -- Clear eyeballs if off or anonymous
49 redis
.call("HDEL", key_socket_user
, "eb")
52 -- No longer associated with a troupe
53 redis
.call("HDEL", key_socket_user
, "tid", "eb")
56 return { 1, user_in_troupe_count
, user_in_prev_troupe_count
}