Explorar el Código

creating ejabber rooms, adding members

System Administrator hace 1 año
padre
commit
444cd7828e
Se han modificado 2 ficheros con 100 adiciones y 2 borrados
  1. 33 0
      ejabber.py
  2. 67 2
      serializer/orders.py

+ 33 - 0
ejabber.py

@@ -0,0 +1,33 @@
+from core.config import EJ_URL, DEBUG
+import requests
+
+def ej_execute(command, data):
+    """
+    API request to jabber server
+    """
+    url = EJ_URL + command
+    if DEBUG: print(f"[URL]: {url}")
+    res = requests.post(url, json=data)
+    return res
+
+
+def pick_support():
+    return "support"
+
+
+def get_rooms(service, order_num, support, client, provider):
+    """
+    Create names and member list for each room.
+    Returns a dict {room_name: ("member1", "member2", ...)}
+    """
+    # Set room names
+    room_name1 = "_".join(str(attr) for attr in (service, order_num, client, support))
+    room_name2 = "_".join(str(attr) for attr in (service, order_num, client, "with", provider))
+    room_name3 = "_".join(str(attr) for attr in (service, order_num, provider, support))
+    # Set room participants
+    rooms = {
+       room_name1: ("client", "support", "owner"),
+       room_name2: ("client", "provider", "owner"),
+       room_name3: ("provider", "support", "owner"),
+    }
+    return rooms

+ 67 - 2
serializer/orders.py

@@ -1,16 +1,23 @@
 from rest_framework import serializers
-from metaservicesynced.models import Orders
+from metaservicesynced.models import Orders, Client
+from metaservicesynced.ejabber import *
 from tickets.models import Task, TaskList
+from core.config import DEBUG, WEBSERVICE_NAME, EJ_HOST, EJ_SERVICE
 import traceback
+import requests
+    
 
 class OrdersSerializer(serializers.ModelSerializer):
     class Meta:
         model = Orders
         fields = '__all__'
 
+    
     def create(self, validated_data):
         ModelClass = self.Meta.model
 
+
+        
         if "ticket" not in validated_data:
             # Create ticket with minimal required data
             task_data = {
@@ -50,5 +57,63 @@ class OrdersSerializer(serializers.ModelSerializer):
                 )
             )
             raise TypeError(msg)
-        
+        else:
+            if DEBUG: print("[CREATING EJABBER ROOMS]")
+            # Get neccessary data
+            order_num = instance.pk
+            support = pick_support()
+            client = instance.client_id.user.phone_number
+            provider = instance.provider.user_id.phone_number
+
+            for room_name, room_members in get_rooms(WEBSERVICE_NAME, order_num, support, client, provider).items():
+                # Create ejabber rooms
+                data = {
+                    "name": room_name,
+                    "service": EJ_SERVICE,
+                    "host": EJ_HOST,
+                }
+                res = ej_execute("create_room", data)
+                if DEBUG: print(f"[RESPONSE CREATED]: {res.json()}")
+                if DEBUG: print(f"[ROOM]: {room_name}")
+
+                # Set rooms to members_only
+                data = {
+                    "name": room_name,
+                    "service": EJ_SERVICE,
+                    "option": "members_only",
+                    "value": "true"
+                }
+                res = ej_execute("change_room_option", data)
+                if DEBUG: print(f"[SET ROOM TO MEMBERS ONLY]: {res.json()}")
+
+                """
+                Add members to the room
+
+                member_account is a dict that contains members credentials
+                Key - the same thing that is in room_members
+                Value - tuple (jid, ejabber affiliation).
+                Affiliation can be one of: owner, admin, member, outcast, none.
+                owner > admin
+                """
+                members_accounts = {
+                    "owner": ("sports_backend@" + EJ_HOST, "owner"),
+                    "client": (client, "member"),
+                    "support": (support, "member"),
+                    "provider": (provider, "member"),
+                }
+                data = {
+                    "name": room_name,
+                    "service": EJ_SERVICE,
+                    "jid": "sports_backend@" + EJ_HOST,
+                    "affiliation": "owner"
+                }
+                #
+                # REQUIRES TO BE TESTED, RESULTS OF THIS WERE NOT CONFIRMED TO BE CORRECT
+                #
+                # Add all neccessary member to the room
+                for member in room_members:
+                    data["jid"] = members_accounts[member][0]
+                    data["affiliation"] = members_accounts[member][1]
+                    res = ej_execute("set_room_affiliation", data)
+                    if DEBUG: print(f"[ADDED USER]: {res.json()}")
         return instance