diff --git a/rocket_connect/instance/models.py b/rocket_connect/instance/models.py index 15870f2..09aa639 100644 --- a/rocket_connect/instance/models.py +++ b/rocket_connect/instance/models.py @@ -100,36 +100,27 @@ def get_managers_channel(self, as_string=True): def get_open_rooms(self): rocket = self.get_rocket_client() rooms = rocket.livechat_rooms(open="true") - if rooms.ok: + if rooms.ok and rooms.json().get("rooms"): return rooms.json()["rooms"] else: return [] - def sync_open_rooms(self, default_connector=None, filter_token=None): - """This method will get the open rooms, filter by a word in token - and recreate the rooms binded to the default connector. - The idea is to help on a migration where the actual open rooms has no - reference at Rocket Connect + def room_sync(self, execute=False): """ - rooms = self.get_open_rooms() - for room in rooms: - if filter_token: - # pass if do not match filter - if filter_token not in room.get("v", {}).get("token"): - pass - else: - LiveChatRoom = apps.get_model("envelope.LiveChatRoom") - room_item, created = LiveChatRoom.objects.get_or_create( - connector=default_connector, - token=room.get("v", {}).get("token"), - room_id=room.get("_id", {}), - ) - room_item.open = True - room_item.save() - if created: - print("ROOM CREATED:", room["v"]["token"]) - else: - print("ROOM UPDATED:", room["v"]["token"]) + Close all open rooms not open in Rocket.Chat + """ + open_rooms = self.get_open_rooms() + open_rooms_id = [r["_id"] for r in open_rooms] + # get all open rooms in connector, except the actually open ones + LiveChatRoom = apps.get_model(app_label="envelope", model_name="LiveChatRoom") + close_rooms = LiveChatRoom.objects.filter( + connector__server=self, open=True + ).exclude(room_id__in=open_rooms_id) + total = close_rooms.count() + if execute: + close_rooms.update(open=False) + response = {"total": total} + return response def force_delivery(self): """ @@ -379,6 +370,21 @@ def connector_status(self): total_visitors=models.Count("room__token", distinct=True), ) + def room_sync(self, execute=False): + """ + Close all open rooms not open in Rocket.Chat + """ + rocket = self.server.get_rocket_client() + open_rooms = rocket.livechat_rooms(open="true").json() + open_rooms_id = [r["_id"] for r in open_rooms["rooms"]] + # get all open rooms in connector, except the actually open ones + close_rooms = self.rooms.filter(open=True).exclude(room_id__in=open_rooms_id) + total = close_rooms.count() + if execute: + close_rooms.update(open=False) + response = {"total": total} + return response + uuid = models.UUIDField(default=uuid.uuid4, editable=False) external_token = models.CharField(max_length=50, default=random_string, unique=True) name = models.CharField( diff --git a/rocket_connect/instance/views.py b/rocket_connect/instance/views.py index 355bc9f..c9ce671 100644 --- a/rocket_connect/instance/views.py +++ b/rocket_connect/instance/views.py @@ -119,6 +119,7 @@ def server_endpoint(request, server_id): @must_be_yours def server_detail_view(request, server_id): server = get_object_or_404(Server.objects, external_token=server_id) + room_sync = None # get server status status = server.status() if request.GET.get("force_connector_delivery"): @@ -147,6 +148,13 @@ def server_detail_view(request, server_id): return redirect(reverse("instance:server_detail", args=[server.external_token])) + if request.GET.get("check-room-sync"): + room_sync = server.room_sync() + if request.GET.get("do-check-room-sync"): + room_sync = server.room_sync(execute=True) + messages.success(request, "Sync Executed!") + room_sync = connector.room_sync() + connectors = ( server.connectors.distinct() .annotate( @@ -170,6 +178,7 @@ def server_detail_view(request, server_id): "server": server, "connectors": connectors, "status": status, + "room_sync": room_sync, } return render(request, "instance/server_detail_view.html", context) @@ -188,6 +197,7 @@ def connector_analyze(request, server_id, connector_id): connector_action_response["status_session"] = connector.status_session() undelivered_messages = None date = None + room_sync = None if request.GET.get("connector_action") == "status_session": connector_action_response["status_session"] = connector.status_session() @@ -244,6 +254,13 @@ def connector_analyze(request, server_id, connector_id): ) ) + if request.GET.get("check-room-sync"): + room_sync = connector.room_sync() + if request.GET.get("do-check-room-sync"): + room_sync = connector.room_sync(execute=True) + messages.success(request, "Sync Executed!") + room_sync = connector.room_sync() + messages_undelivered_by_date = ( connector.messages.filter(delivered=False) .annotate(date=TruncDay("created")) @@ -271,6 +288,7 @@ def connector_analyze(request, server_id, connector_id): "messages_undelivered_by_date": messages_undelivered_by_date, "undelivered_messages": undelivered_messages, "date": date, + "room_sync": room_sync, "connector_action_response": connector_action_response, "config_form": config_form, "base_uri": base_uri, diff --git a/rocket_connect/templates/instance/connector_analyze.html b/rocket_connect/templates/instance/connector_analyze.html index 1a7483f..189b4ed 100644 --- a/rocket_connect/templates/instance/connector_analyze.html +++ b/rocket_connect/templates/instance/connector_analyze.html @@ -15,6 +15,10 @@
+ sync! +
+ {% endif %} ++ sync! +
+ {% endif %} +