Skip to content

Commit

Permalink
LilSholex 5.7
Browse files Browse the repository at this point in the history
- Added Captioned Meme feature to PersianMeme
- Small improvements on PersianMeme source code
- Using Python 3.11.4 as the base image
- Updated Python Packages
- Updated RabbitMQ, Memcached, and MySQL
- Updated Nginx & OpenSSL
- Small improvements on Nginx configuration
  • Loading branch information
RealNitroZeus committed Jul 21, 2023
1 parent e437e36 commit eed04b6
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 79 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.11.2
FROM python:3.11.4

# Setting working directory
WORKDIR /home/sholex
Expand Down
10 changes: 10 additions & 0 deletions LilSholex/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
SECRETS_PATH = Path('/run/secrets')
with open(SECRETS_PATH / 'secret_key') as secret_file:
SECRET_KEY = secret_file.read().removesuffix('\n')

# PersianMeme
with open(SECRETS_PATH / 'persianmeme_token') as persian_meme_token:
MEME = persian_meme_token.read().removesuffix('\n')
with open(SECRETS_PATH / 'persianmeme_channel') as persian_meme_channel:
Expand All @@ -22,6 +24,14 @@
MEME_HELP_MESSAGES = persian_meme_help_messages.read()
with open(SECRETS_PATH / 'persianmeme_reports') as persian_meme_reports:
MEME_REPORTS_CHANNEL = persian_meme_reports.read().removesuffix('\n')
ID_KEY = 'id:'
EMPTY_CAPTION_KEY = '@ '
SEARCH_CAPTION_KEY = ' @ '
NAMES_KEY = 'names:'
TAGS_KEY = 'tags:'
VOICES_KEY = 'voices:'
VIDEOS_KEY = 'videos:'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
with open(SECRETS_PATH / 'domain') as domain_file:
Expand Down
4 changes: 2 additions & 2 deletions Nginx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM ubuntu:22.04
ARG NGINX_VERSION=nginx-1.23.2
ARG OPENSSL_VERSION=openssl-3.0.7
ARG NGINX_VERSION=nginx-1.24.0
ARG OPENSSL_VERSION=openssl-3.1.0
# Compiling
RUN apt update && DEBIAN_FRONTEND="noninteractive" apt install -y build-essential \
libpcre3 libpcre3-dev zlib1g zlib1g-dev python3 python3-pip wget && apt autoremove -y && apt autoclean
Expand Down
4 changes: 2 additions & 2 deletions conf/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ http {

upstream lilsholex {
zone lilsholex 512K;
server lilsholex:80 max_conns=16 max_fails=5;
server lilsholex:80 max_conns=16;
keepalive 16;
keepalive_time 4h;
keepalive_timeout 199;
Expand All @@ -64,7 +64,7 @@ http {
keepalive 12;
keepalive_requests 100;
keepalive_timeout 20;
server celery_flower:5555 max_fails=5 max_conns=15;
server celery_flower:5555 max_conns=15;
}

# Servers
Expand Down
16 changes: 8 additions & 8 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: "3.8"
services:
db:
image: mysql:8.0.32
image: mysql:8.0.33
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD_FILE: "/run/secrets/db_password"
Expand Down Expand Up @@ -48,7 +48,7 @@ services:
- source: mysql_2
target: /etc/mysql/conf.d/mysql.cnf
rabbitmq:
image: rabbitmq:3.11.10
image: rabbitmq:3.12.2
healthcheck:
test: "rabbitmq-diagnostics -q ping"
interval: 10s
Expand Down Expand Up @@ -87,7 +87,7 @@ services:
- source: rabbitmq
target: /etc/rabbitmq/rabbitmq.conf
nginx:
image: ghcr.io/sholex-team/nginx:4.1
image: ghcr.io/sholex-team/nginx:4.2
networks:
- nginx_lilsholex
ports:
Expand Down Expand Up @@ -145,7 +145,7 @@ services:
- ssl_key
- dhparam
lilsholex:
image: ghcr.io/sholex-team/lilsholex:5.6
image: ghcr.io/sholex-team/lilsholex:5.7
networks:
- db_django
- nginx_lilsholex
Expand Down Expand Up @@ -197,7 +197,7 @@ services:
window: 10s
delay: 5s
celery: &celery_base
image: ghcr.io/sholex-team/lilsholex:5.6
image: ghcr.io/sholex-team/lilsholex:5.7
command: "celery -A LilSholex worker -Q celery -l info -c 2 -E"
networks:
- db_django
Expand Down Expand Up @@ -259,7 +259,7 @@ services:
- internet
- tasks_django
lilsholex_broadcasts:
image: ghcr.io/sholex-team/lilsholex:5.6
image: ghcr.io/sholex-team/lilsholex:5.7
command: "python manage.py process_broadcasts"
networks:
- db_django
Expand Down Expand Up @@ -301,7 +301,7 @@ services:
window: 10s
delay: 5s
memcached:
image: memcached:1.6.19
image: memcached:1.6.21
command: "memcached -m 256"
networks:
- cache_django
Expand Down Expand Up @@ -330,7 +330,7 @@ services:
monitor: 10s
order: start-first
lilsholex_migrations:
image: ghcr.io/sholex-team/lilsholex:5.6
image: ghcr.io/sholex-team/lilsholex:5.7
command: "bash -c 'yes | python manage.py makemigrations persianmeme && \
yes | python manage.py migrate && python manage.py collectstatic --noinput'"
secrets:
Expand Down
4 changes: 2 additions & 2 deletions persianmeme/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.contrib import admin
from persianmeme import models
from . import models
from django.http import HttpResponse, HttpRequest
from django.core.serializers import serialize
from .functions import fake_deny_vote
Expand Down Expand Up @@ -297,7 +297,7 @@ class Message(admin.ModelAdmin):
list_display = ('id', 'sender', 'status')
list_filter = ('status',)
raw_id_fields = ('sender',)
search_fields = ('id', 'sender__user_id', 'sender__user_id', 'sender__chat_id')
search_fields = ('id', 'sender__user_id', 'sender__chat_id')
list_per_page = 15
readonly_fields = ('id',)
fieldsets = (('Information', {'fields': ('id', 'sender')}), ('Status', {'fields': ('status',)}))
Expand Down
16 changes: 8 additions & 8 deletions persianmeme/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,12 @@ def __search_items(self):
return Q(type__in=(types_tuple := (models.MemeType.VOICE, models.MemeType.VIDEO))), \
Q(meme__type__in=types_tuple)

def get_memes(self, query: str, offset: str):
if query.startswith('names:'):
def get_memes(self, query: str, offset: str, caption: str | None):
if query.startswith(settings.NAMES_KEY):
query = query[6:].strip()
queries = Q(name__icontains=query)
type_filter, recent_type_filter = self.__search_items
elif query.startswith('id:') and (target_voice_id := query[3:].strip()).isdigit():
elif query.startswith(settings.ID_KEY) and (target_voice_id := query[3:].strip()).isdigit():
result_maker = make_meme_like_result if self.database.vote else make_meme_result
try:
return ([result_maker(models.Meme.objects.get(
Expand All @@ -192,19 +192,19 @@ def get_memes(self, query: str, offset: str):
Q(sender=self.database)
) | Q(visibility=models.Meme.Visibility.NORMAL)) & Q(
status=models.Meme.Status.ACTIVE)
))], str())
), caption)], str())
except (models.Meme.DoesNotExist, ValueError):
return list(), str()
else:
if is_tags := query.startswith('tags:'):
if is_tags := query.startswith(settings.TAGS_KEY):
query = query[5:].strip()
type_filter, recent_type_filter = self.__search_items
else:
if query.startswith('voices:'):
if query.startswith(settings.VOICES_KEY):
query = query[7:].strip()
type_filter = Q(type=models.MemeType.VOICE)
recent_type_filter = Q(meme__type=models.MemeType.VOICE)
elif query.startswith('videos:'):
elif query.startswith(settings.VIDEOS_KEY):
query = query[7:].strip()
type_filter = Q(type=models.MemeType.VIDEO)
recent_type_filter = Q(meme__type=models.MemeType.VIDEO)
Expand Down Expand Up @@ -249,7 +249,7 @@ def get_memes(self, query: str, offset: str):
if splinted_offset[current_offset] != 'e':
splinted_offset[current_offset] = int(splinted_offset[current_offset])
current_result = result()[splinted_offset[current_offset]:splinted_offset[current_offset] + remaining]
temp_result = [result_maker(meme) for meme in current_result
temp_result = [result_maker(meme, caption) for meme in current_result
if all(meme[0] != target_result['id'] for target_result in results)]
if not (temp_len := len(temp_result)):
splinted_offset[current_offset] = 'e'
Expand Down
46 changes: 34 additions & 12 deletions persianmeme/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,46 +143,68 @@ async def forwarder(chat_id: int):
await asyncio.sleep(0.7)


def make_result(meme: list):
def make_result(meme: list, caption: str | None):
temp_result = {
'id': meme[0],
'title': meme[2]
'id': meme[0]
}
if meme[3] == models.MemeType.VIDEO:
temp_result['type'] = 'video'
temp_result['video_file_id'] = meme[1]
temp_result['description'] = meme[4]
temp_result['title'] = meme[2]
if caption:
temp_result['caption'] = f'<b>{caption}</b>'
temp_result['parse_mode'] = 'HTML'
temp_result['description'] = caption
else:
temp_result['description'] = meme[4]
else:
temp_result['type'] = 'voice'
temp_result['voice_file_id'] = meme[1]
if caption:
temp_result['caption'] = f'<b>{caption}</b>'
temp_result['parse_mode'] = 'HTML'
temp_result['title'] = f'{meme[2]} ({caption})'
else:
temp_result['title'] = meme[2]
return temp_result


def make_like_result(meme: list):
temp_result = make_result(meme)
def make_like_result(meme: list, caption: str | None):
temp_result = make_result(meme, caption)
temp_result['reply_markup'] = {'inline_keyboard': [[
{'text': '👍', 'callback_data': f'up:{meme[0]}'}, {'text': '👎', 'callback_data': f'down:{meme[0]}'}
]]}
return temp_result


def make_meme_result(meme: models.Meme):
def make_meme_result(meme: models.Meme, caption: str | None):
temp_result = {
'id': meme.id,
'title': meme.name
'id': meme.id
}
if meme.type == models.MemeType.VIDEO:
temp_result['type'] = 'video'
temp_result['video_file_id'] = meme.file_id
temp_result['description'] = meme.description
temp_result['title'] = meme.name
if caption:
temp_result['caption'] = f'<b>{caption}</b>'
temp_result['parse_mode'] = 'HTML'
temp_result['description'] = caption
else:
temp_result['description'] = temp_result['description'] = meme.description
else:
temp_result['type'] = 'voice'
temp_result['voice_file_id'] = meme.file_id
if caption:
temp_result['caption'] = f'<b>{caption}</b>'
temp_result['parse_mode'] = 'HTML'
temp_result['title'] = f'{meme.name} ({caption})'
else:
temp_result['title'] = meme.name
return temp_result


def make_meme_like_result(meme: models.Meme):
temp_result = make_meme_result(meme)
def make_meme_like_result(meme: models.Meme, caption: str | None):
temp_result = make_meme_result(meme, caption)
temp_result['reply_markup'] = {'inline_keyboard': [[
{'text': '👍', 'callback_data': f'up:{meme.id}'}, {'text': '👎', 'callback_data': f'down:{meme.id}'}
]]}
Expand Down
13 changes: 11 additions & 2 deletions persianmeme/handlers/inline_query/handlers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from django.conf import settings
from LilSholex.exceptions import RequestInterruption
from persianmeme.classes import User
from persianmeme.functions import answer_inline_query
from persianmeme.models import User as UserClass
from json import dumps


Expand All @@ -24,8 +26,15 @@ def handler(request, inline_query, user_chat_id):
user.send_ad()
user.set_username()
user.database.save()
if user.database.status != 'f':
results, next_offset = user.get_memes(query, offset)
if user.database.status != UserClass.Status.FULL_BANNED:
if len((splinted_query := query.split(settings.SEARCH_CAPTION_KEY, 1))) == 2:
query, caption = splinted_query
elif query.startswith(settings.EMPTY_CAPTION_KEY):
caption = query.removeprefix(settings.EMPTY_CAPTION_KEY)
query = str()
else:
caption = None
results, next_offset = user.get_memes(query, offset, caption)
answer_inline_query(
inline_query_id,
dumps(results),
Expand Down
6 changes: 2 additions & 4 deletions persianmeme/handlers/message/menus/user/menus/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ def handler(message: dict, text: str, message_id: int, user: UserClass):
case 'گزارش تخلف 🛑':
user.database.menu = User.Menu.USER_REPORT_MEME
user.send_message(user_messages['send_target_meme'], keyboards.per_back, message_id)
case 'دیسکورد':
user.send_message(user.translate('discord'), keyboards.discord, message_id)
case 'حمایت مالی 💸':
user.send_message(user.translate('donate'), reply_to_message_id=message_id, parse_mode='Markdown')
case 'گروه عمومی':
user.send_message(user.translate('group'), keyboards.group, message_id)
case 'کانال رای‌گیری 🗳':
user.send_message(user.translate('voting_channel'), keyboards.voting_channel, message_id)
case 'آخرین میم ها 🆕':
user.send_ordered_meme_list(user.database.Ordering.new_meme_id)
case 'ارتباط با مدیریت 📬':
Expand Down
13 changes: 5 additions & 8 deletions persianmeme/keyboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@
], 'resize_keyboard': True}
user = {'keyboard': [
['راهنما 🔰', 'گزارش تخلف 🛑'],
['گروه عمومی', 'دیسکورد', 'حمایت مالی 💸'],
['ویس ها 🔊', 'ویدئو ها 📹'],
['پر استفاده ها ⭐', 'میم های محبوب 👌'],
['آخرین میم ها 🆕'],
['ارتباط با مدیریت 📬', 'تنظیمات ⚙'],
['درخواست حذف میم ✖']
['درخواست حذف میم ✖', 'حمایت مالی 💸'],
['ویس ها 🔊', 'کانال رای‌گیری 🗳', 'ویدئو ها 📹'],
['پر استفاده ها ⭐', 'آخرین میم ها 🆕', 'میم های محبوب 👌'],
['ارتباط با مدیریت 📬', 'تنظیمات ⚙']
], 'resize_keyboard': True}
manage_voices = {'keyboard': [
['ویس های پیشنهادی ✔', 'ویس های شخصی 🔒'], ['پلی لیست ها ▶️'], ['بازگشت 🔙']
Expand All @@ -50,8 +48,7 @@
voice_suggestions = {'keyboard': [
['پیشنهاد ویس 🔥', 'مشاهده ی ویس ها 📝'], ['لغو رای گیری ⏹'], ['بازگشت 🔙']
], 'resize_keyboard': True}
discord = {'inline_keyboard': [[{'text': 'Discord 🎮', 'url': 'https://discord.gg/u8aAzG3SAf'}]]}
group = {'inline_keyboard': [[{'text': 'گروه عمومی 👥', 'url': 'https://t.me/persianmemeofficial'}]]}
voting_channel = {'inline_keyboard': [[{'text': 'کانال رای‌گیری 🗳', 'url': 'https://t.me/persianmemeofficial'}]]}
admin_message = {'inline_keyboard': [[{'text': 'پیام از طرف مدیریت 👆', 'callback_data': 'none'}]]}
manage_playlists = {'keyboard': [
['ایجاد پلی لیست 🆕'], ['مشاهده پلی لیست ها 📝'], ['بازگشت 🔙']
Expand Down
3 changes: 1 addition & 2 deletions persianmeme/translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@
'already_joined_playlist': 'شما در حال حاضر در این پلی لیست عضو هستید !',
'invalid_playlist': 'پلی لیست معتبر نیست ⚠️',
'welcome': 'به ربات Persian Meme خوش آمدید 😁\n\nبرای مشاهده ی راهنمای ربات روی دکمه ی "راهنما 🔰" کلیک کنید ☑️',
'discord': 'با کلیک بر دکمه زیر وارد سرور دیسکورد ما شوید 👇',
'group': 'از طریق دکمه زیر وارد گروه شوید 👇',
'voting_channel': 'از طریق دکمه زیر وارد کانال رای‌گیری شوید 👇',
'pending_message': 'پیام قبلی شما در انتظار مشاهده است ⚠',
'send_message': 'لطفا پیام خود را ارسال کنید .',
'pending_meme': '{0} قبلی شما در انتظار تایید است ❌',
Expand Down
Loading

0 comments on commit eed04b6

Please sign in to comment.