Skip to content

Commit

Permalink
Revert "Merge tag '361-translation-race-condition' into develop"
Browse files Browse the repository at this point in the history
This reverts commit 5b91848, reversing
changes made to 3b57df1.
  • Loading branch information
yangm97 committed Nov 13, 2018
1 parent 5b91848 commit 89d0b20
Show file tree
Hide file tree
Showing 15 changed files with 51 additions and 247 deletions.
11 changes: 8 additions & 3 deletions lua/groupbutler/languages.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,20 @@ end

local locale = {} -- table with exported functions

do
local directory = "locales"
locale.language = config.lang -- default language

function locale.init(directory)
directory = directory or "locales"

for lang_code in pairs(config.available_languages) do
strings[lang_code] = parse(string.format('%s/%s.po', directory, lang_code))
end
end

function locale.translate(msgid)
return strings[ngx.ctx.language or config.lang][msgid:gsub('^\n', '')] or msgid
return strings[locale.language][msgid:gsub('^\n', '')] or msgid
end

locale.init()

return locale
10 changes: 5 additions & 5 deletions lua/groupbutler/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ local function on_msg_receive(self, callback) -- The fn run whenever a message i
end

-- Set chat language
ngx.ctx.language = red:get('lang:'..msg.chat.id) or config.lang
if not config.available_languages[ngx.ctx.language] then
ngx.ctx.language = config.lang
locale.language = red:get('lang:'..msg.chat.id) or config.lang
if not config.available_languages[locale.language] then
locale.language = config.lang
end

-- Do not process messages from normal groups
Expand Down Expand Up @@ -148,7 +148,7 @@ Unfortunately I can't work in normal groups. If you need me, please ask the crea
if not onm_success then
log.error('An #error occurred (preprocess).\n{err}\n{lang}\n{text}', {
cont=tostring(continue),
lang=ngx.ctx.language,
lang=locale.language,
text=msg.text})
end
if not continue then
Expand Down Expand Up @@ -194,7 +194,7 @@ Unfortunately I can't work in normal groups. If you need me, please ask the crea
end
log.error('An #error occurred.\n{result}\n{lang}\n{text}', {
result=tostring(result),
lang=ngx.ctx.language,
lang=locale.language,
text=msg.text})
return false
end
Expand Down
6 changes: 3 additions & 3 deletions lua/groupbutler/plugins/antispam.lua
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ local function doKeyboard_antispam(self, chat_id)
icon = ''
elseif status == 'del' then icon = '🗑' end
local line = {
{text = i18n(humanizations[field] or field), callback_data = 'antispam:alert:'..field..':'..ngx.ctx.language},
{text = i18n(humanizations[field] or field), callback_data = 'antispam:alert:'..field..':'..locale.language},
{text = icon, callback_data = 'antispam:toggle:'..field..':'..chat_id}
}
table.insert(keyboard.inline_keyboard, line)
Expand All @@ -235,7 +235,7 @@ local function doKeyboard_antispam(self, chat_id)
end

local line = {
{text = 'Warns: '..warns, callback_data = 'antispam:alert:warns:'..ngx.ctx.language},
{text = 'Warns: '..warns, callback_data = 'antispam:alert:warns:'..locale.language},
{text = '', callback_data = 'antispam:toggle:dim:'..chat_id},
{text = '', callback_data = 'antispam:toggle:raise:'..chat_id},
{text = action, callback_data = 'antispam:toggle:action:'..chat_id}
Expand Down Expand Up @@ -272,7 +272,7 @@ function _M:onCallbackQuery(blocks)

if blocks[1] == 'alert' then
if config.available_languages[blocks[3]] then
ngx.ctx.language = blocks[3]
locale.language = blocks[3]
end
local text = get_alert_text(blocks[2])
api:answerCallbackQuery(msg.cb_id, text, true, config.bot_settings.cache_time.alert_help)
Expand Down
4 changes: 2 additions & 2 deletions lua/groupbutler/plugins/defaultpermissions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ local function doKeyboard_permissions(self, chat_id)
line = {
{
text = i18n(humanizations[permission] or permission),
callback_data = 'defpermissions:alert:'..permission..':'..ngx.ctx.language
callback_data = 'defpermissions:alert:'..permission..':'..locale.language
},
{
text = icon,
Expand All @@ -117,7 +117,7 @@ function _M:onCallbackQuery(blocks)
local u = self.u
if blocks[1] == 'alert' then
if config.available_languages[blocks[3]] then
ngx.ctx.language = blocks[3]
locale.language = blocks[3]
end
local text = get_alert_text(blocks[2])
api:answerCallbackQuery(msg.cb_id, text, true, config.bot_settings.cache_time.alert_help)
Expand Down
6 changes: 3 additions & 3 deletions lua/groupbutler/plugins/floodmanager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ local function do_keyboard_flood(self, chat_id)
},
{
{text = '', callback_data = 'flood:dim:'..chat_id},
{text = tostring(num), callback_data = 'flood:alert:num:'..ngx.ctx.language},
{text = tostring(num), callback_data = 'flood:alert:num:'..locale.language},
{text = '', callback_data = 'flood:raise:'..chat_id},
}
}
Expand All @@ -84,7 +84,7 @@ local function do_keyboard_flood(self, chat_id)
exc_status = ''
end
local line = {
{text = translation, callback_data = 'flood:alert:voice:'..ngx.ctx.language},
{text = translation, callback_data = 'flood:alert:voice:'..locale.language},
{text = exc_status, callback_data = 'flood:exc:'..media..':'..chat_id},
}
table.insert(keyboard.inline_keyboard, line)
Expand Down Expand Up @@ -153,7 +153,7 @@ It is also possible to choose which type of messages the antiflood will ignore (

if blocks[1] == 'alert' then
if config.available_languages[blocks[3]] then
ngx.ctx.language = blocks[3]
locale.language = blocks[3]
end
text = get_button_description(blocks[2])
api:answerCallbackQuery(msg.cb_id, text, true, config.bot_settings.cache_time.alert_help)
Expand Down
4 changes: 2 additions & 2 deletions lua/groupbutler/plugins/logchannel.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ local function doKeyboard_logchannel(self, chat_id)
if current_status == 'no' then icon = '☑️' end
table.insert(keyboard.inline_keyboard,
{
{text = event_pretty[event] or event, callback_data = 'logchannel:alert:'..event..':'..ngx.ctx.language},
{text = event_pretty[event] or event, callback_data = 'logchannel:alert:'..event..':'..locale.language},
{text = icon, callback_data = 'logchannel:toggle:'..event..':'..chat_id}
})
end
Expand Down Expand Up @@ -117,7 +117,7 @@ function _M:onCallbackQuery(blocks)
else
if blocks[1] == 'alert' then
if config.available_languages[blocks[3]] then
ngx.ctx.language = blocks[3]
locale.language = blocks[3]
end
local text = get_alert_text(blocks[2])
api:answerCallbackQuery(msg.cb_id, text, true, config.bot_settings.cache_time.alert_help)
Expand Down
4 changes: 2 additions & 2 deletions lua/groupbutler/plugins/mediasettings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ local function doKeyboard_media(self, chat_id)
}
local media_text = media_texts[media] or media
local line = {
{text = media_text, callback_data = 'mediallert:'..ngx.ctx.language},
{text = media_text, callback_data = 'mediallert:'..locale.language},
{text = status, callback_data = 'media:'..media..':'..chat_id}
}
table.insert(keyboard.inline_keyboard, line)
Expand Down Expand Up @@ -128,7 +128,7 @@ When a media is set to delete, the bot will give a warning *only* when this is t
else
if blocks[1] == 'mediallert' then
if config.available_languages[blocks[2]] then
ngx.ctx.language = blocks[2]
locale.language = blocks[2]
end
api:answerCallbackQuery(msg.cb_id, i18n("⚠️ Tap on the right column"), false,
config.bot_settings.cache_time.alert_help)
Expand Down
8 changes: 4 additions & 4 deletions lua/groupbutler/plugins/menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ local function insert_settings_section(keyboard, settings_section, chat_id)

for key, icon in pairs(settings_section) do
local current = {
{text = strings[key] or key, callback_data = 'menu:alert:settings:'..key..':'..ngx.ctx.language},
{text = strings[key] or key, callback_data = 'menu:alert:settings:'..key..':'..locale.language},
{text = icon, callback_data = 'menu:'..key..':'..chat_id}
}
table.insert(keyboard.inline_keyboard, current)
Expand Down Expand Up @@ -225,12 +225,12 @@ local function doKeyboard_menu(self, chat_id)
end
local warn = {
{
{text = i18n('Warns: ')..max, callback_data = 'menu:alert:settings:warnsnum:'..ngx.ctx.language},
{text = i18n('Warns: ')..max, callback_data = 'menu:alert:settings:warnsnum:'..locale.language},
{text = '', callback_data = 'menu:DimWarn:'..chat_id},
{text = '', callback_data = 'menu:RaiseWarn:'..chat_id},
},
{
{text = i18n('Action:'), callback_data = 'menu:alert:settings:warnsact:'..ngx.ctx.language},
{text = i18n('Action:'), callback_data = 'menu:alert:settings:warnsact:'..locale.language},
{text = action, callback_data = 'menu:ActionWarn:'..chat_id}
}
}
Expand Down Expand Up @@ -262,7 +262,7 @@ function _M:onCallbackQuery(blocks)
else
if blocks[2] == 'alert' then
if config.available_languages[blocks[4]] then
ngx.ctx.language = blocks[4]
locale.language = blocks[4]
end
text = get_button_description(blocks[3])
api:answerCallbackQuery(msg.cb_id, text, true, config.bot_settings.cache_time.alert_help)
Expand Down
9 changes: 9 additions & 0 deletions lua/groupbutler/plugins/service.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ function _M:onTextMessage(blocks)
end

if blocks[1] == 'new_chat_member:bot' or blocks[1] == 'migrate_from_chat_id' then
-- set the language
--[[locale.language = red:get(string.format('lang:%d', msg.from.id)) or config.lang
if not config.available_languages[locale.language] then
locale.language = 'en'
end]]
if u:is_blocked_global(msg.from.id) then
api:sendMessage(msg.chat.id, i18n("_You (user ID: %d) are in the blocked list_"):format(msg.from.id), "Markdown")
api:leaveChat(msg.chat.id)
Expand All @@ -45,6 +50,10 @@ function _M:onTextMessage(blocks)
api:leaveChat(msg.chat.id)
return
end
-- save language
--[[if locale.language then
red:set(string.format('lang:%d', msg.chat.id), locale.language)
end]]
u:initGroup(msg.chat.id)
-- send manuals
local text
Expand Down
4 changes: 2 additions & 2 deletions lua/groupbutler/plugins/setlang.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ function _M:onCallbackQuery(blocks)
api:editMessageText(msg.chat.id, msg.message_id, nil, i18n("*List of available languages*:"), "Markdown", nil,
keyboard)
else
ngx.ctx.language = blocks[1]
red:set('lang:'..msg.chat.id, ngx.ctx.language)
locale.language = blocks[1]
red:set('lang:'..msg.chat.id, locale.language)
if (blocks[1] == 'ar_SA' or blocks[1] == 'fa_IR') and msg.chat.type ~= 'private' then
red:hset('chat:'..msg.chat.id..':char', 'Arab', 'allowed')
red:hset('chat:'..msg.chat.id..':char', 'Rtl', 'allowed')
Expand Down
4 changes: 2 additions & 2 deletions lua/groupbutler/utilities.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ local log = require "groupbutler.logging"
local null = require "groupbutler.null"
local i18n = locale.translate
local http, HTTPS, ltn12, time_hires, sleep
if ngx and ngx.now then
if ngx then
http = require "resty.http"
time_hires = ngx.now
sleep = ngx.sleep
Expand Down Expand Up @@ -337,7 +337,7 @@ end

function _M:download_to_file(url, file_path) -- luacheck: ignore 212
log.info("url to download: {url}", {url=url})
if http then
if ngx then
local httpc = http.new()
local ok, err = httpc:request_uri(url)

Expand Down
23 changes: 8 additions & 15 deletions lua/vendor/resty/redis.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,15 @@ local new_tab = (function()
return new_tab
end)()

local socket = (function()
if ngx and ngx.socket then
return ngx.socket
end
return require "resty.socket"
end)()

local null = (function()
if ngx and ngx.null then
return ngx.null
end
local ok, null = pcall(require, "cjson")
if not ok then
null = require "null"
local socket, null = (function()
if not ngx then
local ok, null = pcall(require, "cjson")
if not ok then
null = require "null"
end
return require "resty.socket", null.null
end
return null.null
return ngx.socket, ngx.null
end)()


Expand Down

0 comments on commit 89d0b20

Please sign in to comment.