Skip to content

Commit

Permalink
Merge branch 'feature/userstopg' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
yangm97 committed Nov 20, 2018
2 parents 89d0b20 + e4a196c commit dc1b1c5
Show file tree
Hide file tree
Showing 14 changed files with 399 additions and 54 deletions.
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ trim_trailing_whitespace = true
insert_final_newline = true

[*.{yml,yaml}]
indent_size = 2
indent_style = space

[*.sql]
indent_size = 4
indent_style = space
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.env
schema.sql
docker-compose.*.yml
*.sh
*.json
Expand Down
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,11 @@ dev_polling: kill build_polling

dev_webhooks: kill build_webhooks
docker-compose -f docker-compose.yml -f docker/docker-compose.webhooks.yml up

dump_pg:
rm -f schema.sql && docker-compose exec postgres pg_dump -U postgres groupbutler --schema-only > schema.sql

restore_pg:
docker-compose exec postgres dropdb -U postgres groupbutler
docker-compose exec postgres createdb -U postgres groupbutler
docker-compose exec postgres pg_restore -U postgres -C -d groupbutler < schema.sql
16 changes: 16 additions & 0 deletions docker/docker-compose.polling.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ services:
GB_COMMIT: $GB_COMMIT
environment:
REDIS_HOST: redis
POSTGRES_HOST: postgres
POSTGRES_USER: groupbutler
POSTGRES_PASSWORD: password
volumes:
- ./conf:/srv/app/conf
- ./locales:/srv/app/locales
Expand All @@ -22,3 +25,16 @@ services:
image: redis:alpine
ports:
- "6379:6379"

postgres:
image: postgres:alpine
environment:
POSTGRES_PASSWORD: password
ports:
- "5432:5432"
volumes:
- ./schema:/docker-entrypoint-initdb.d
- postgres_data:/var/lib/postgresql/data

volumes:
postgres_data:
17 changes: 17 additions & 0 deletions docker/docker-compose.webhooks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ services:
GB_COMMIT: $GB_COMMIT
environment:
REDIS_HOST: redis
POSTGRES_HOST: postgres
POSTGRES_USER: groupbutler
POSTGRES_PASSWORD: password
volumes:
- ./conf:/usr/local/openresty/nginx/conf
- ./locales:/usr/local/openresty/nginx/locales
Expand All @@ -23,3 +26,17 @@ services:
image: redis:alpine
ports:
- "6379:6379"

postgres:
image: postgres:alpine
environment:
POSTGRES_USER: groupbutler
POSTGRES_PASSWORD: password
ports:
- "5432:5432"
volumes:
- ./schema:/docker-entrypoint-initdb.d
- postgres_data:/var/lib/postgresql/data

volumes:
postgres_data:
2 changes: 1 addition & 1 deletion docker/polling.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ WORKDIR /srv/app

CMD ["polling.lua"]

ARG DEPS_ROCKS="telegram-bot-api lua-resty-socket"
ARG DEPS_ROCKS="telegram-bot-api pgmoon lua-resty-socket"

RUN for ROCK in $DEPS_ROCKS; do luarocks install $ROCK; done

Expand Down
2 changes: 1 addition & 1 deletion docker/webhooks.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ WORKDIR /usr/local/openresty/nginx

HEALTHCHECK --interval=3s --timeout=3s CMD ["healthchecker"] || exit 1

ARG DEPS_OPM="yangm97/lua-telegram-bot-api"
ARG DEPS_OPM="yangm97/lua-telegram-bot-api leafo/pgmoon"
RUN opm install $DEPS_OPM

COPY --from=healthchecker-builder /go/bin/healthchecker /usr/local/bin
Expand Down
3 changes: 1 addition & 2 deletions lua/groupbutler/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ local _M =
},

-- Data
postgres = -- Not implemented
{
postgres = {
host = os.getenv('POSTGRES_HOST') or 'localhost',
port = os.getenv('POSTGRES_PORT') or 5432,
user = os.getenv('POSTGRES_USER') or 'postgres',
Expand Down
58 changes: 26 additions & 32 deletions lua/groupbutler/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ local utilities = require "groupbutler.utilities"
local log = require "groupbutler.logging"
local redis = require "resty.redis"
local plugins = require "groupbutler.plugins"
local message = require "groupbutler.message"
local Message = require "groupbutler.message"
local User = require "groupbutler.user"
local storage = require "groupbutler.storage"
local locale = require "groupbutler.languages"
local i18n = locale.translate
Expand Down Expand Up @@ -37,39 +38,37 @@ function _M:new(update_obj)
return update_obj
end

local function extract_usernames(self, msg)
local red = self.red
if msg.from and msg.from.username then
red:hset('bot:usernames', '@'..msg.from.username:lower(), msg.from.id)
end
if msg.forward_from and msg.forward_from.username then
red:hset('bot:usernames', '@'..msg.forward_from.username:lower(), msg.forward_from.id)
local function inject_message_methods(message, update)
Message:new(message, update)
if message.from then -- Sender is empty for messages sent to channels
User:new(message.from, update):cache()
end
if msg.new_chat_member then
if msg.new_chat_member.username then
red:hset('bot:usernames', '@'..msg.new_chat_member.username:lower(), msg.new_chat_member.id)
end
red:sadd(string.format('chat:%d:members', msg.chat.id), msg.new_chat_member.id)
if message.forward_from then
User:new(message.forward_from, update):cache()
end
if msg.left_chat_member then
if msg.left_chat_member.username then
red:hset('bot:usernames', '@'..msg.left_chat_member.username:lower(), msg.left_chat_member.id)
end

local function add_message_methods(object, update)
local message_objects = {
"message", "edited_message", "channel_post", -- Possible messages inside updates
"reply_to_message", "pinned_message", -- Possible messages inside messages
}
for _, message in pairs(message_objects) do
if type(object[message]) == "table" then
inject_message_methods(object[message], update)
add_message_methods(object[message], update)
end
red:srem(string.format('chat:%d:members', msg.chat.id), msg.left_chat_member.id)
end
if msg.reply_to_message then
extract_usernames(self, msg.reply_to_message)
end
if msg.pinned_message then
extract_usernames(self, msg.pinned_message)
end
end

local function add_methods(update)
add_message_methods(update, update)
end

local function collect_stats(self)
local msg = self.message
local red = self.red
local u = self.u
extract_usernames(self, msg)
local now = os.time(os.date("*t"))
if msg.chat.type ~= 'private' and msg.chat.type ~= 'inline' and msg.from then
red:hset('chat:'..msg.chat.id..':userlast', msg.from.id, now) --last message for each user
Expand Down Expand Up @@ -209,7 +208,6 @@ end

function _M:process()
local u = self.u
local red = self.red
local bot = self.bot

local start_time = u:time_hires()
Expand Down Expand Up @@ -274,9 +272,6 @@ function _M:process()
end
end
if self.message.reply_to_message then
-- Add message methods
message:new(self.message.reply_to_message, self)

self.message.reply = self.message.reply_to_message
if self.message.reply.caption then
self.message.reply.text = self.message.reply.caption
Expand Down Expand Up @@ -308,13 +303,12 @@ function _M:process()

if not self.message.text then self.message.text = self.message.caption or '' end

-- Add message methods
message:new(self.message, self)
add_methods(self)

local retval = on_msg_receive(self, function_key)
u:metric_set("msg_request_duration_sec", u:time_hires() - start_time)
-- print(red:get_reused_times())
red:set_keepalive()
-- print(self.db:get_reused_times())
self.db:set_keepalive()
return retval
end

Expand Down
6 changes: 6 additions & 0 deletions lua/groupbutler/plugins/service.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ function _M:onTextMessage(blocks)

if not msg.service then return end

if blocks[1] == "new_chat_member" then
red:sadd(string.format("chat:%d:members", msg.chat.id), msg.new_chat_member.id)
end
if blocks[1] == "left_chat_member" then
red:srem(string.format("chat:%d:members", msg.chat.id), msg.left_chat_member.id)
end
if blocks[1] == 'new_chat_member'
or blocks[1] == 'left_chat_member' then
local status = red:hget(('chat:%d:settings'):format(msg.chat.id), 'Clean_service_msg')
Expand Down

0 comments on commit dc1b1c5

Please sign in to comment.