Page Menu
Home
Phabricator (Chris)
Search
Configure Global Search
Log In
Files
F627132
user_tracker.lua
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Authored By
Unknown
Size
2 KB
Referenced Files
None
Subscribers
None
user_tracker.lua
View Options
--[[
mattata v2.0 - User Tracker Middleware
Upserts user and chat information to PostgreSQL with Redis-based debouncing.
Uses a 60s dedup key per user+chat to reduce DB writes by ~95%.
]]
local
user_tracker
=
{}
user_tracker
.
name
=
'user_tracker'
function
user_tracker
.
run
(
ctx
,
message
)
if
not
message
.
from
then
return
ctx
,
true
end
local
user
=
message
.
from
local
user_id
=
user
.
id
local
chat_id
=
message
.
chat
and
message
.
chat
.
id
-- Debounce: skip DB upserts if we've seen this user+chat in the last 60s
local
dedup_key
=
string.format
(
'seen:%s:%s'
,
user_id
,
chat_id
or
'private'
)
local
already_seen
=
ctx
.
redis
.
exists
(
dedup_key
)
if
already_seen
==
1
or
already_seen
==
true
then
-- Still update username->id mapping (cheap Redis SET)
if
user
.
username
then
ctx
.
redis
.
set
(
'username:'
..
user
.
username
:
lower
(),
user_id
)
end
return
ctx
,
true
end
-- Set dedup key with 60s TTL
ctx
.
redis
.
setex
(
dedup_key
,
60
,
'1'
)
-- upsert user to postgresql
local
now
=
os.date
(
'!%Y-%m-%d %H:%M:%S'
)
pcall
(
function
()
ctx
.
db
.
call
(
'sp_upsert_user'
,
{
user_id
,
user
.
username
and
user
.
username
:
lower
()
or
nil
,
user
.
first_name
,
user
.
last_name
,
user
.
language_code
,
user
.
is_bot
or
false
,
now
})
end
)
-- upsert chat to postgresql (for groups)
if
chat_id
and
message
.
chat
.
type
~=
'private'
then
pcall
(
function
()
ctx
.
db
.
call
(
'sp_upsert_chat'
,
{
chat_id
,
message
.
chat
.
title
,
message
.
chat
.
type
,
message
.
chat
.
username
and
message
.
chat
.
username
:
lower
()
or
nil
})
end
)
-- track user<->chat membership
pcall
(
function
()
ctx
.
db
.
call
(
'sp_upsert_chat_member'
,
{
chat_id
,
user_id
,
now
})
end
)
end
-- Keep Redis username->id mapping for quick lookups
if
user
.
username
then
ctx
.
redis
.
set
(
'username:'
..
user
.
username
:
lower
(),
user_id
)
end
return
ctx
,
true
end
return
user_tracker
File Metadata
Details
Attached
Mime Type
application/javascript
Expires
Sun, Nov 15, 1:38 AM (1 d, 20 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
137092
Default Alt Text
user_tracker.lua (2 KB)
Attached To
Mode
R69 mattata
Attached
Detach File
Event Timeline