|
| 1 | +local outIndexKey = KEYS[1]; |
| 2 | +local dataKeyTemplate = KEYS[2]; |
| 3 | +local metaKeyTemplate = KEYS[3]; |
| 4 | +local kIndUserTokenTemplate = KEYS[4]; |
| 5 | + |
| 6 | +local exTime = ARGV[6]; |
| 7 | + |
| 8 | +-- hashes |
| 9 | +local kUserAudiences = KEYS[5]; |
| 10 | +local kAliasToId = KEYS[6]; |
| 11 | +local kUsernameToId = KEYS[7]; |
| 12 | +local kSSOToId = KEYS[8]; |
| 13 | + |
| 14 | +-- indexes |
| 15 | +local kIndUsers = KEYS[9]; |
| 16 | +local kIndUsersPublic = KEYS[10]; |
| 17 | + |
| 18 | +-- throttleprefix |
| 19 | +local kThrottlePrefix = KEYS[11]; |
| 20 | + |
| 21 | +-- constant fields names |
| 22 | +local fUserId = ARGV[1] |
| 23 | +local fUserAlias = ARGV[2]; |
| 24 | +local fUserName = ARGV[3]; |
| 25 | + |
| 26 | +local _rawSSOProviders = ARGV[4]; |
| 27 | +local _rawThrottleActions = ARGV[5]; |
| 28 | + |
| 29 | +local throttleActions = cjson.decode(_rawThrottleActions); |
| 30 | +local ssoProviders = cjson.decode(_rawSSOProviders); |
| 31 | + |
| 32 | +local delimiter = "!"; |
| 33 | + |
| 34 | +local function isempty(s) |
| 35 | + return s == nil or s == '' or s == false; |
| 36 | +end |
| 37 | + |
| 38 | +local function toTable(data) |
| 39 | + local result = {}; |
| 40 | + for i = 1, #data, 2 do |
| 41 | + result[data[i]] = data[i + 1] |
| 42 | + end |
| 43 | + return result; |
| 44 | +end |
| 45 | + |
| 46 | +-- key generators |
| 47 | +local function key(...) |
| 48 | + return table.concat(arg, delimiter); |
| 49 | +end |
| 50 | + |
| 51 | +local function decode(strval) |
| 52 | + if type(strval) == "string" then |
| 53 | + return cjson.decode(strval); |
| 54 | + else |
| 55 | + return {} |
| 56 | + end |
| 57 | +end |
| 58 | + |
| 59 | +local function makeKey (userId, template) |
| 60 | + local str = template:gsub('{userid}', userId, 1); |
| 61 | + return str |
| 62 | +end |
| 63 | + |
| 64 | +local function makeMetaDataKey (userId, audience, template) |
| 65 | + local str = template:gsub('{userid}', userId, 1); |
| 66 | + str = str:gsub('{audience}', audience, 1); |
| 67 | + return str; |
| 68 | +end |
| 69 | + |
| 70 | +local function getUserAudiences(userId) |
| 71 | + local strVal = redis.call("HGET", kUserAudiences, userId); |
| 72 | + return decode(strVal) |
| 73 | +end |
| 74 | + |
| 75 | +local function getUserData (userId) |
| 76 | + local key = makeKey(userId, dataKeyTemplate); |
| 77 | + local data = redis.call("HGETALL", key); |
| 78 | + |
| 79 | + if #data > 0 then |
| 80 | + return toTable(data) |
| 81 | + end |
| 82 | + |
| 83 | + return nil; |
| 84 | +end |
| 85 | + |
| 86 | +-- delete logic |
| 87 | +local function deleteUser(userId, userData) |
| 88 | + if userData == nil then |
| 89 | + return; |
| 90 | + end |
| 91 | + |
| 92 | + local alias = userData[fUserAlias]; |
| 93 | + local username = userData[fUserName]; |
| 94 | + |
| 95 | + if isempty(alias) == false then |
| 96 | + redis.call("HDEL", kAliasToId, alias, string.lower(alias)); |
| 97 | + end |
| 98 | + |
| 99 | + --almost impossible but |
| 100 | + if isempty(username) == false then |
| 101 | + redis.call("HDEL", kUsernameToId, username); |
| 102 | + end |
| 103 | + |
| 104 | + for k,v in pairs(ssoProviders) do |
| 105 | + local rawData = userData[v]; |
| 106 | + local provData = decode(rawData); |
| 107 | + if isempty(provData['uid']) == false then |
| 108 | + redis.call("HDEL", kSSOToId, provData['uid']); |
| 109 | + end |
| 110 | + end |
| 111 | + |
| 112 | + -- clean indicies |
| 113 | + redis.call("HDEL", kIndUsersPublic, userId ); |
| 114 | + redis.call("HDEL", kIndUsers, userId); |
| 115 | + |
| 116 | + -- delete user data |
| 117 | + redis.call("DEL", makeKey(userId, dataKeyTemplate)) |
| 118 | + |
| 119 | + -- delete meta |
| 120 | + for k,v in pairs(getUserAudiences(userId)) do |
| 121 | + local metaKey = makeMetaDataKey(userId, v, metaKeyTemplate); |
| 122 | + redis.call("DEL", metaKey); |
| 123 | + end |
| 124 | + |
| 125 | + -- delete USERS_TOKENS |
| 126 | + local userTokensKey = makeKey(userId, kIndUserTokenTemplate); |
| 127 | + redis.call("DEL", userTokensKey); |
| 128 | + |
| 129 | + -- remove audience list |
| 130 | + redis.call("HDEL", kUserAudiences, userId); |
| 131 | + |
| 132 | + -- throttle constants |
| 133 | + for k,v in pairs(throttleActions) do |
| 134 | + local throttleKey = key(kThrottlePrefix, v, userId); |
| 135 | + redis.call("DEL", throttleKey); |
| 136 | + end |
| 137 | + |
| 138 | +end |
| 139 | + |
| 140 | +local function getInactiveUsers() |
| 141 | + return redis.call("ZRANGEBYSCORE", outIndexKey, '-inf', exTime); |
| 142 | +end |
| 143 | + |
| 144 | +local function cleanInactiveUsers(idList) |
| 145 | + for k,v in pairs(idList) do |
| 146 | + redis.call('ZREM', outIndexKey, v); |
| 147 | + end |
| 148 | +end |
| 149 | + |
| 150 | +-- Command body |
| 151 | +local inactiveUsers = getInactiveUsers(); |
| 152 | + |
| 153 | +for k,v in pairs(inactiveUsers) do |
| 154 | + local userData = getUserData(v); |
| 155 | + deleteUser(v, userData); |
| 156 | +end |
| 157 | + |
| 158 | +cleanInactiveUsers(inactiveUsers); |
| 159 | +-- returns count of deleted users |
| 160 | +return { #inactiveUsers } |
0 commit comments