Skip to content

Commit

Permalink
optimize sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
charles-co committed Nov 10, 2021
1 parent b161ec8 commit cd05f3e
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 94 deletions.
Empty file.
12 changes: 5 additions & 7 deletions backend/channel_plugin/apps/channels/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def validate_name(self, name):
data = {"name": name.lower()}
response = Request.get(self.context.get("org_id"), "channel", data)
if isinstance(response, list):
raise serializers.ValidationError({"error": "Name already exist"})
raise serializers.ValidationError({"error": "Name already exists"})
return name

def to_representation(self, instance):
Expand All @@ -44,15 +44,13 @@ def to_representation(self, instance):
slug = slugify(instance.get("name"))
channel = Channel(**instance, slug=slug)
user_serializer = UserSerializer(
data={"_id": user_id, "is_admin": True, "notifications": {}}
data={"_id": user_id, "is_admin": True, "notifications": {}}
)

user_serializer.is_valid(raise_exception=True)

channel.users = {
user_id: user_serializer.data
}


channel.users = {user_id: user_serializer.data}

data = {"channel": channel}
return data

Expand Down
89 changes: 3 additions & 86 deletions backend/channel_plugin/channel_plugin/info/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,11 @@
from sentry_sdk import capture_message

from channel_plugin.utils.custome_response import Response as Custom_Response
from channel_plugin.utils.customrequest import AsyncRequest, unread
from channel_plugin.utils.customrequest import AsyncRequest # , unread
from channel_plugin.utils.mixins import AsycViewMixin

from .serializers import InstallSerializer

description = "The Channel Plugin is a feature\
that helps users create spaces for\
conversation and communication on zuri.chat."


class GetInfoViewset(AsycViewMixin, ViewSet):
def get_throttled_message(self, request):
Expand Down Expand Up @@ -60,7 +56,7 @@ async def info(self, request):
"type": "Plugin Information",
"plugin_info": {
"name": "Channels Plugin",
"description": ["Zuri.chat plugin", description],
"description": ["Zuri.chat plugin", settings.DESCRIPTION],
"id": settings.PLUGIN_ID,
},
"scaffold_structure": "Monolith",
Expand Down Expand Up @@ -110,86 +106,7 @@ async def info_sidebar(self, request):
"""
org_id = request.query_params.get("org")
user_id = request.query_params.get("user")
joined_rooms = list()
public_rooms = list()
starred_rooms = list()

data = {
"name": "Channels Plugin",
"description": description,
"plugin_id": settings.PLUGIN_ID,
}
if org_id is not None and user_id is not None:

channels = await AsyncRequest.get(org_id, "channel")

if isinstance(channels, list):
joined_rooms = list(
map(
lambda channel: {
"room_name": channel.get("slug"),
"room_url": f"/channels/message-board/{channel.get('_id')}",
"room_image": "",
"unread": unread(org_id, channel.get("_id")),
},
list(
filter(
lambda channel: user_id in channel["users"].keys()
and not channel.get("default", False)
and not channel["users"][user_id].get("starred", False),
channels,
)
),
)
)
starred_rooms = list(
map(
lambda channel: {
"room_name": channel.get("slug"),
"room_url": f"/channels/message-board/{channel.get('_id')}",
"room_image": "",
"unread": unread(org_id, channel.get("_id")),
},
list(
filter(
lambda channel: user_id in channel["users"].keys()
and not channel.get("default", False)
and channel["users"][user_id].get("starred", False),
channels,
)
),
)
)
public_rooms = list(
map(
lambda channel: {
"room_name": channel.get("slug"),
"room_url": f"/channels/message-board/{channel.get('_id')}",
"room_image": "",
},
list(
filter(
lambda channel: user_id not in channel["users"].keys()
and not channel.get("private")
and not channel.get("default", False),
channels,
)
),
)
)
data.update(
{
"organisation_id": org_id,
"user_id": user_id,
"group_name": "Channel",
"show_group": False,
"category": "channels",
"button_url": "/channels",
"joined_rooms": joined_rooms,
"public_rooms": public_rooms,
"starred_rooms": starred_rooms,
}
)
data = await AsyncRequest.sidebar(org_id, user_id)
# AUTHENTICATION SHOULD COME SOMEWHERE HERE, BUT THAT's WHEN WE GET THE DB UP

return Custom_Response(
Expand Down
103 changes: 103 additions & 0 deletions backend/channel_plugin/channel_plugin/utils/customrequest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import asyncio
import json
import logging
import random
from dataclasses import dataclass
from functools import wraps
from urllib.parse import urlencode

import aiohttp
import requests
from asgiref.sync import sync_to_async
from django.conf import settings
from django.urls import reverse

Expand Down Expand Up @@ -282,6 +285,106 @@ async def delete(org_id, collection_name, data_filter=None, object_id=None):
return await response.json(content_type=None)
return {"error": (await response.json(content_type=None))}

@staticmethod
@change_collection_name
async def sidebar(org_id, user_id, collection_name="channel", token=None):

joined_rooms = list()
public_rooms = list()
starred_rooms = list()

@sync_to_async
def get_response(filter):
res = requests.post(read, data=json.dumps(filter))
data = res.json().get("data", [])
return data if isinstance(data, list) else []

async def get_joined_rooms():
filter = {
"organization_id": org_id,
"collection_name": collection_name,
"plugin_id": settings.PLUGIN_ID,
"filter": {f"users.{user_id}": {"$exists": True}},
}
return await get_response(filter)

async def get_public_rooms():
filter = {
"organization_id": org_id,
"collection_name": collection_name,
"plugin_id": settings.PLUGIN_ID,
"filter": {f"users.{user_id}": {"$exists": False}},
}
return await get_response(filter)

joined_list, public_list = await asyncio.gather(
get_joined_rooms(), get_public_rooms()
)
joined_rooms = list(
map(
lambda channel: {
"room_name": channel.get("slug"),
"room_url": f"/channels/message-board/{channel.get('_id')}",
"room_image": "",
"unread": random.randint(1, 10),
},
list(
filter(
lambda channel: not channel["users"][user_id].get(
"starred", False
),
joined_list,
)
),
)
)

starred_rooms = list(
map(
lambda channel: {
"room_name": channel.get("slug"),
"room_url": f"/channels/message-board/{channel.get('_id')}",
"room_image": "",
"unread": random.randint(1, 10),
},
list(
filter(
lambda channel: channel["users"][user_id].get("starred", False),
joined_list,
)
),
)
)

public_rooms = list(
map(
lambda channel: {
"room_name": channel.get("slug"),
"room_url": f"/channels/message-board/{channel.get('_id')}",
"room_image": "",
},
public_list,
)
)

data.update(
{
"name": "Channels Plugin",
"description": settings.DESCRIPTION,
"plugin_id": settings.PLUGIN_ID,
"organisation_id": org_id,
"user_id": user_id,
"group_name": "Channel",
"show_group": False,
"category": "channels",
"button_url": "/channels",
"joined_rooms": joined_rooms,
"public_rooms": public_rooms,
"starred_rooms": starred_rooms,
}
)
return data


@change_collection_name
def search_db(org_id, channel_id, collection_name, **params):
Expand Down
2 changes: 1 addition & 1 deletion backend/channel_plugin/channel_plugin/utils/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def get_exception_response(self, exc, request):
response.accepted_renderer = request.accepted_renderer
response.accepted_media_type = request.accepted_media_type
response.renderer_context = self.get_renderer_context()

print(f"Response: {response}")
return response

def finalize_response(self, request, response, *args, **kwargs):
Expand Down
4 changes: 4 additions & 0 deletions backend/channel_plugin/config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,10 @@
WRITE_URL = "https://api.zuri.chat/data/write"
DELETE_URL = "https://api.zuri.chat/data/delete"

DESCRIPTION = "The Channel Plugin is a feature\
that helps users create spaces for\
conversation and communication on zuri.chat."

try:
with open("centri.txt", "r") as f:
CENTRIFUGO_API_KEY = f.readline().strip("\n")
Expand Down

0 comments on commit cd05f3e

Please sign in to comment.