Skip to content

Commit

Permalink
fix:setting current site
Browse files Browse the repository at this point in the history
  • Loading branch information
cunla committed Jun 29, 2024
1 parent 37b9e3a commit 92fd1ac
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 23 deletions.
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ x-depends-on-defaults: &depends-on-defaults

services:
searchdb:
image: getmeili/meilisearch:v1.7
image: getmeili/meilisearch:v1.8.3
ports:
- "7700:7700"
environment:
Expand Down Expand Up @@ -99,7 +99,7 @@ services:
- wiwik-network

redis:
image: redis/redis-stack-server:7.2.0-v9
image: redis/redis-stack-server:7.2.0-v11
ports:
- "6379:6379"
healthcheck:
Expand Down
34 changes: 21 additions & 13 deletions forum/forum/integrations/slack_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Union, List, Optional
from typing import Union, List, Optional, Dict, Any

from constance import config
from scheduler import job
Expand All @@ -12,7 +12,7 @@
TextObject,
LinkButtonElement,
DividerBlock,
HeaderBlock, MarkdownTextObject,
HeaderBlock, MarkdownTextObject, Block,
)
from slack_sdk.models.views import View
from slack_sdk.signature import SignatureVerifier
Expand Down Expand Up @@ -111,6 +111,17 @@ def _get_permalink(channel: str, message_ts: str) -> Union[str, None]:
return None


@job
def send_message_to_user(user_id: str, message: Dict[str, Any]):
if not config.SLACK_BOT_TOKEN:
return
try:
logger.info(f"sending message to user {user_id}: {message}")
slack_client.chat_postMessage(channel=user_id, **message)
except SlackApiError as e:
logger.warning(f"Got an error: {e.response['error']}")


@job
def slack_post_im_message_to_email(text: str, email: str, notification_text: str = None):
if not config.SLACK_BOT_TOKEN:
Expand All @@ -122,7 +133,7 @@ def slack_post_im_message_to_email(text: str, email: str, notification_text: str
logger.warning(f"slack user for {email} not found, returning")
return
blocks = [
SectionBlock(text=TextObject(text=text, type="mrkdwn")),
SectionBlock(text=MarkdownTextObject(text=text)),
]
slack_client.chat_postMessage(channel=user_id, text=notification_text or text, blocks=blocks, mrkdwn=True)
except SlackApiError as e:
Expand Down Expand Up @@ -270,17 +281,13 @@ def verify_request(request) -> bool:
return verifier.is_valid(body, timestamp, signature)


def questions_message(questions: List[Question]) -> List:
def questions_slack_message(questions: List[Question]) -> Dict[str, Any]:
question_ids = [str(q.id) for q in questions]
logger.debug(f"Sending to slack question IDs ({','.join(question_ids)})")
blocks = list()
blocks: List[Block] = list()
for question in questions:
question_url = get_model_url_with_base("question", question)
blocks.append(
HeaderBlock(
text=question.title,
).to_dict()
)
blocks.append(HeaderBlock(text=question.title, ))
content = question.content[:1000]
content = content.split("\r\n")
num_lines = len(content)
Expand All @@ -291,7 +298,8 @@ def questions_message(questions: List[Question]) -> List:
SectionBlock(
text=MarkdownTextObject(text=content),
accessory=LinkButtonElement(text="Go to question", url=question_url),
).to_dict()
)
)
blocks.append(DividerBlock().to_dict())
return blocks
blocks.append(DividerBlock())
res = dict(blocks=[b.to_dict() for b in blocks])
return res
4 changes: 2 additions & 2 deletions forum/forum/views/common.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.urls import reverse

from wiwik_lib.utils import CURRENT_SITE
from wiwik_lib import utils


def get_model_url(model_name: str, model) -> str:
Expand All @@ -23,4 +23,4 @@ def get_model_url(model_name: str, model) -> str:


def get_model_url_with_base(model_name: str, model) -> str:
return CURRENT_SITE + get_model_url(model_name, model)
return utils.CURRENT_SITE + get_model_url(model_name, model)
4 changes: 2 additions & 2 deletions forum/forum/views/integrations/slack_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ def search_from_slack(request):
results = list(
search.query_method(Question.objects, query).order_by("-has_accepted_answer", "-votes", "-created_at")[:3]
)
res = {"blocks": slack_api.questions_message(results)}
res = slack_api.questions_slack_message(results)
logger.debug(res)
return JsonResponse(data=res, status=200)
return JsonResponse(data=res, status=200, content_type="application/json")
4 changes: 3 additions & 1 deletion forum/wiwik_lib/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from django.apps import AppConfig


logger = logging.getLogger(__package__)


Expand All @@ -10,4 +11,5 @@ class LibConfig(AppConfig):
name = "wiwik_lib"

def ready(self):
pass
from wiwik_lib.utils import set_current_site
set_current_site()
4 changes: 1 addition & 3 deletions forum/wiwik_lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ def paginate_queryset(qs: QuerySet, page: int, per_page: int):

CURRENT_SITE = ""


@checks.register()
def set_current_site(app_configs, **kwargs):
def set_current_site():
messages = []
global CURRENT_SITE
try:
Expand Down

0 comments on commit 92fd1ac

Please sign in to comment.