Skip to content

Commit 402c67b

Browse files
committed
feat: delete unconfirmed users lua logic doc
1 parent 0f73d33 commit 402c67b

File tree

2 files changed

+69
-18
lines changed

2 files changed

+69
-18
lines changed

doc/rfcs/inactive_users_cleanup.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,54 @@ When script started:
4242
1. It extracts from `inactive-user` list of `userId` with score range '-inf' to 'currentimestamp - ttl'.
4343
2. Removes all user data including aliases, emails, metadata, tokens, SSO accounts, throttles and cleans all bound indicies
4444
3. Removes processed records from `inactive-user`
45+
46+
### Passed keys(next `KEY.$ind` used):
47+
1. Inactive users list key (USER_ACTIVATE constant) = `local outIndexKey`
48+
2. Template for user data key = `local dataKeyTemplate`
49+
3. Template for user meta data key = `local metaKeyTemplate`
50+
4. Template for user tokens key = `local userTokenTemplate`
51+
5. `user-audience` zset handling key name = `local kUserAudiences`
52+
6. Key for accessing `ALIAS_TO_ID` hash = `local kAliasToId`
53+
7. Key for accessing `USERNAME_TO_ID` hash = `local kUsernameToId`
54+
8. Key for accessing `SSO_TO_ID` hash = `local kSSOToId`
55+
9. Key for accessing general users index `USERS_INDEX` = `local kIndUsers`
56+
10. Key for accessing pulic users index `USERS_PUBLIC_INDEX` = `local kIndUsersPublic`
57+
11. Key prefix from `THROTTLE_PREFIX` constant = `local kThrottlePrefix`
58+
59+
### Passed arguments(next `ARG.$ind` used):
60+
1. UserAlias field name from user data `USERs_ALIAS_FIELD`
61+
2. UserName field name from data `USERS_USERNAME_FIELD`
62+
3. JSON string based on `SSO_PROVIDERS`
63+
4. JSON string based on `THROTTLE_ACTIONS`
64+
5. Timestamp, all users created below this value will be deleted
65+
66+
### How it works:
67+
#### Main
68+
1. Decodes from JSON passed SSO_PROVIDERS, THROTTLE_ACTIONS
69+
2. Gets UserId's from ZSET `KEY.1` where score < `ARG.6`
70+
3. Iterates over list:
71+
1. Forms user data key from `KEY.2` template + userId, and gets values
72+
2. Calls `deleteUser(userid, userdata)`
73+
4. Deletes processed userId's from ZSET `KEY.1`
74+
75+
76+
#### deleteUser(id, userdata)
77+
Logic was adopted from `actions/removeUser.js`. Maybe I'm wrong, but all data deleted here, could be created even for inactive user.
78+
**Except TOKENS?????**
79+
80+
1. Gets `username` and `alias` values from `userData` using provided `ARG.2, ARG.3`
81+
2. If alias not empty, deletes field from `ALIAS_TO_ID`(`KEY.6`) hash
82+
3. If username not empty(sometimes this could happen(((( ), deletes from `USERNAME_TO_ID`(`KEY.7`) hash
83+
4. Iterates over decoded `SSO_PROVIDERS` list, and uses each value for access userData[key],
84+
this field contains json_encoded string with provider options
85+
1. if userData[ssoprovidername] exists, decoding it's value and checking for `uid` value.
86+
2. If `uid` set, deletes this it's value from `SSO_TO_ID`(`KEY.8`)) hash.
87+
5. Deletes userId from `USERS_INDEX`(`KEY.9`), `USERS_PUBLIC_INDEX`(`KEY.10`) sets.
88+
6. Gets Audience list from `USER_AUDIENCE`(`KEY.5`), decodes it from JSON, and iterates over it's values
89+
* forms user meta `key` name from provided template `KEY.3`, userId and `audience`
90+
* deletes meta `key`.
91+
7. Forms user tokens key name from `KEY.4` and deletes it
92+
8. **THROTTLE_PREFIX constant not exists in constants.js, so assuming this left for backward compat with previous version** Iterates over decoded `THROTTLE_ACTIONS` array
93+
* forms Throttle `key` names from userId, `THROTTLE_PREFIX`(`KEY.11`)
94+
* deletes throttle `key`
95+

scripts/deleteInactivated.lua

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
local outIndexKey = KEYS[1];
1+
local inactiveUsersList = KEYS[1];
22
local dataKeyTemplate = KEYS[2];
33
local metaKeyTemplate = KEYS[3];
4-
local kIndUserTokenTemplate = KEYS[4];
5-
6-
local exTime = ARGV[6];
4+
local userTokenTemplate = KEYS[4];
75

86
-- hashes
97
local kUserAudiences = KEYS[5];
@@ -19,15 +17,13 @@ local kIndUsersPublic = KEYS[10];
1917
local kThrottlePrefix = KEYS[11];
2018

2119
-- constant fields names
22-
local fUserId = ARGV[1]
23-
local fUserAlias = ARGV[2];
24-
local fUserName = ARGV[3];
20+
local fUserAlias = ARGV[1];
21+
local fUserName = ARGV[2];
2522

26-
local _rawSSOProviders = ARGV[4];
27-
local _rawThrottleActions = ARGV[5];
23+
local _rawSSOProviders = ARGV[3];
24+
local _rawThrottleActions = ARGV[4];
2825

29-
local throttleActions = cjson.decode(_rawThrottleActions);
30-
local ssoProviders = cjson.decode(_rawSSOProviders);
26+
local exTime = ARGV[5];
3127

3228
local delimiter = "!";
3329

@@ -43,11 +39,6 @@ local function toTable(data)
4339
return result;
4440
end
4541

46-
-- key generators
47-
local function key(...)
48-
return table.concat(arg, delimiter);
49-
end
50-
5142
local function decode(strval)
5243
if type(strval) == "string" then
5344
return cjson.decode(strval);
@@ -56,6 +47,15 @@ local function decode(strval)
5647
end
5748
end
5849

50+
--Let this be here
51+
local throttleActions = decode(_rawThrottleActions);
52+
local ssoProviders = decode(_rawSSOProviders);
53+
54+
-- key generators
55+
local function key(...)
56+
return table.concat(arg, delimiter);
57+
end
58+
5959
local function makeKey (userId, template)
6060
local str = template:gsub('{userid}', userId, 1);
6161
return str
@@ -123,7 +123,7 @@ local function deleteUser(userId, userData)
123123
end
124124

125125
-- delete USERS_TOKENS
126-
local userTokensKey = makeKey(userId, kIndUserTokenTemplate);
126+
local userTokensKey = makeKey(userId, userTokenTemplate);
127127
redis.call("DEL", userTokensKey);
128128

129129
-- remove audience list
@@ -138,7 +138,7 @@ local function deleteUser(userId, userData)
138138
end
139139

140140
local function getInactiveUsers()
141-
return redis.call("ZRANGEBYSCORE", outIndexKey, '-inf', exTime);
141+
return redis.call("ZRANGEBYSCORE", inactiveUsersList, '-inf', exTime);
142142
end
143143

144144
local function cleanInactiveUsers(idList)

0 commit comments

Comments
 (0)