1 local key_socket_user
= 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_user_sockets
= KEYS
[6];
8 local user_id
= ARGV
[1];
9 local socket_id
= ARGV
[2];
10 local create_time
= ARGV
[3];
11 local mobile_connection
= tonumber(ARGV
[4]);
12 local client_type
= ARGV
[5];
13 local realtime_library
= ARGV
[6];
14 local troupe_id
= ARGV
[7];
15 local oauth_client_id
= ARGV
[8];
16 local token
= ARGV
[9];
17 local unique_client_id
= ARGV
[10];
19 if redis
.call("EXISTS", key_socket_user
) == 1 then
23 redis
.call("INCR", key_user_lock
);
24 redis
.call("EXPIRE", key_user_lock
, 10);
26 redis
.call("HSET", key_socket_user
, "uid", user_id
)
27 redis
.call("HSET", key_socket_user
, "ctime", create_time
)
28 redis
.call("HSET", key_socket_user
, "ct", client_type
)
29 if realtime_library
~= "" then
30 redis
.call("HSET", key_socket_user
, "rl", realtime_library
)
32 redis
.call("HSET", key_socket_user
, "tid", troupe_id
)
33 if oauth_client_id
~= "" then
34 redis
.call("HSET", key_socket_user
, "ocid", oauth_client_id
)
37 redis
.call("HSET", key_socket_user
, "tok", token
)
39 if unique_client_id
~= "" then
40 redis
.call("HSET", key_socket_user
, "ucid", unique_client_id
)
43 local user_socket_count
= -1
45 -- For mobile users, add them to the mobile users collection
46 if mobile_connection
== 1 then
47 redis
.call("HSET", key_socket_user
, "mob", 1)
48 redis
.call("ZINCRBY", key_mobile_users
, 1, user_id
)
50 user_socket_count
= redis
.call("ZINCRBY", key_active_users
, 1, user_id
)
53 redis
.call("SADD", key_user_sockets
, socket_id
)
54 local socket_add_result
= redis
.call("SADD", key_active_sockets
, socket_id
)
56 return { 1, user_socket_count
, socket_add_result
}