Skip to content

Commit

Permalink
add dynamic sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
charles-co committed Sep 9, 2021
1 parent 5fd7a2e commit ecb3db0
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 45 deletions.
9 changes: 7 additions & 2 deletions backend/channel_plugin/apps/channels/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from apps.roles.serializers import RoleSerializer
from django.utils.text import slugify

# from django.conf import settings
# from channel_plugin.info.views import description
from rest_framework import serializers

from channel_plugin.utils.customrequest import Request
Expand Down Expand Up @@ -66,14 +69,16 @@ def validate_name(self, name):
try:
if response[0]["_id"] != self.context.get("_id"):
raise serializers.ValidationError({"error": "Name already exist"})
except: # noqa
except TypeError:
return name
except Exception as e: # noqa
raise serializers.ValidationError({"error": "Name already exist"})
return name

def to_representation(self, instance):
if instance:
instance = dict(instance)
users = instance.pop("users")
users = instance.pop("users", None)
new_users_dict = dict()

if users:
Expand Down
7 changes: 6 additions & 1 deletion backend/channel_plugin/apps/channels/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def channels(self, request, org_id):
serializer.is_valid(raise_exception=True)
channel = serializer.data.get("channel")
result = channel.create(org_id)
result.update({"members": len(result["users"].keys())})
return Response(result, status=status.HTTP_201_CREATED)

@swagger_auto_schema(
Expand All @@ -52,6 +53,8 @@ def channel_all(self, request, org_id):
data = {}
data.update(dict(request.query_params))
result = Request.get(org_id, "channel", data)
for i, channel in enumerate(result):
result[i].update({"members": len(channel["users"].keys())})
return Response(result, status=status.HTTP_200_OK)

@swagger_auto_schema(
Expand All @@ -64,7 +67,8 @@ def channel_all(self, request, org_id):
)
def channel_retrieve(self, request, org_id, channel_id):
data = {"_id": channel_id}
result = Request.get(org_id, "channel", data)
result = Request.get(org_id, "channel", data)[0]
result.update({"members": len(result["users"].keys())})
return Response(result, status=status.HTTP_200_OK)

@swagger_auto_schema(
Expand All @@ -82,6 +86,7 @@ def channel_update(self, request, org_id, channel_id):
serializer.is_valid(raise_exception=True)
payload = serializer.data.get("channel")
result = Request.put(org_id, "channel", payload, object_id=channel_id)
result.update({"members": len(result["users"].keys())})
return Response(result, status=status.HTTP_200_OK)

@action(
Expand Down
107 changes: 65 additions & 42 deletions backend/channel_plugin/channel_plugin/info/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from rest_framework.response import Response
from rest_framework.viewsets import ViewSet

from channel_plugin.utils.customrequest import Request

description = """The Channel Plugin is a feature that helps users create spaces
for conversation and communication on zuri.chat.
Users can also create sub tags in the channels
Expand All @@ -18,6 +20,8 @@
in dedicated spaces called channels.
"""

icons = ["shovel", "cdn.cloudflare.com/445345453345/hello.jpeg", "spear"]


class GetInfoViewset(ViewSet):
@action(
Expand All @@ -35,49 +39,68 @@ def info(self, request):

@action(methods=["GET"], detail=False, url_path="sidebar")
def info_sidebar(self, request):
org_id = request.GET.get("org", "")
user_id = request.GET.get("user", "")
token = request.GET.get("token", "")
org_id = request.query_params.get("org")
user_id = request.query_params.get("user")
data = {
"name": "Channels Plugin",
"description": description,
"plugin_id": settings.PLUGIN_ID,
"organisation_id": org_id,
"user_id": user_id,
"group_name": "Zuri",
"show_group": False,
"joined_rooms": [
{
"title": "general",
"id": "DFGHH-EDDDDS-DFDDF",
"unread": 2,
"members": 23,
"icon": "shovel",
"action": "open",
},
{
"title": "announcements",
"id": "DFGfH-EDDDDS-DFDDF",
"unread": 0,
"badge_type": "info",
"members": 132,
"parent_id": "DFGHH-EDDDDS-DFDDF",
"icon": "spear",
"action": "open",
},
],
"public_rooms": [
{
"title": "jokes",
"id": "DFGfH-EDDDDS-DFDDF",
"unread": 342,
"members": 32,
"icon": "cdn.cloudflare.com/445345453345/hello.jpeg",
"action": "open",
"auto-join": True,
},
],
}
"name": "Channels Plugin",
"description": description,
"plugin_id": settings.PLUGIN_ID
}
if org_id is not None or user_id is not None:
token = request.GET.get("token", "")
channels = Request.get(org_id, "channel")
joined_rooms = list()
public_rooms = list()
if type(channels) == list:
joined_rooms = list(
map(
lambda channel: {
"id": channel.get("_id"),
"title": channel.get("name"),
"members": channel.get("members", len(channel["users"].keys())),
"unread": channel.get("unread", random.randint(0, 50)),
"icon": channel.get(
"icon", icons[random.randint(0, len(icons) - 1)]
),
"action": "open",
},
list(
filter(
lambda channel: user_id in channel["users"].keys(), channels
)
),
)
)
public_rooms = list(
map(
lambda channel: {
"id": channel.get("_id"),
"title": channel.get("name"),
"members": channel.get("members", len(channel["users"].keys())),
"unread": channel.get("unread", random.randint(0, 50)),
"icon": channel.get(
"icon", icons[random.randint(0, len(icons) - 1)]
),
"action": "open",
},
list(
filter(
lambda channel: user_id not in channel["users"].keys()
and not channel.get("private"),
channels,
)
),
)
)

data.update({
"organisation_id": org_id,
"user_id": user_id,
"group_name": "Zuri",
"show_group": False,
"joined_rooms": joined_rooms,
"public_rooms": public_rooms,
})

# AUTHENTICATION SHOULD COME SOMEWHERE HERE, BUT THAT's WHEN WE GET THE DB UP

Expand Down

0 comments on commit ecb3db0

Please sign in to comment.