Skip to content

Commit

Permalink
Add sync for server and connector, including in UI
Browse files Browse the repository at this point in the history
  • Loading branch information
dudanogueira committed Jun 29, 2022
1 parent 9f74e2b commit 7e2d835
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 25 deletions.
56 changes: 31 additions & 25 deletions rocket_connect/instance/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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(
Expand Down
18 changes: 18 additions & 0 deletions rocket_connect/instance/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down Expand Up @@ -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(
Expand All @@ -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)

Expand All @@ -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()
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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,
Expand Down
27 changes: 27 additions & 0 deletions rocket_connect/templates/instance/connector_analyze.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ <h1>
<i class="fa fa-edit" aria-hidden="true"></i>
</a>
{% endif %}
<a name="" id="" class="btn btn-primary" href="?check-room-sync=1" role="button" title="Sync Rooms">
<i class="fas fa-recycle"></i>
</a>

{{connector}}
<span class="text-{{ connector.enabled|yesno:'success,danger' }}">
{{connector.enabled|yesno:"Active,Inactive" }}
Expand All @@ -25,6 +29,29 @@ <h1>

Endpoint: {{base_uri}}/connector/{{connector.external_token}}/

{% if room_sync %}
<div class="alert alert-warning alert-dismissible fade show" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
<strong>
Unsync Rooms:
</strong>
{{room_sync.total}}
<small>(open here and closed in Rocket.Chat)</small>
{% if room_sync.total %}
<p class="mt-3">
<a name="" id="" class="btn btn-success" href="?check-room-sync=1&do-check-room-sync=1" role="button">sync!</a>
</p>
{% endif %}
</div>

<script>
$(".alert").alert();
</script>

{% endif %}

{% if connector.connector_type == "wppconnect" %}
<div id="wppconnect">
<div class="container">
Expand Down
22 changes: 22 additions & 0 deletions rocket_connect/templates/instance/server_detail_view.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ <h1>
<a name="" id="" class="btn btn-primary" href="{% url 'admin:instance_server_change' server.id %}" role="button">
<i class="fa fa-edit" aria-hidden="true"></i>
</a>
<a name="" id="" class="btn btn-primary" href="?check-room-sync=1" role="button" title="check rooms sync">
<i class="fa fa-recycle" aria-hidden="true"></i>
</a>
{% endif %}
{{server}} <span class="text-{{ server.enabled|yesno:'success,danger' }}">{{ server.enabled|yesno:"Active,Inactive" }}</span>
</h1>
Expand All @@ -37,6 +40,25 @@ <h1>
Add to Rocket.Chat's Omnichannel WebHook Endpoint:<br />
{{server.url}}/server/{{server.external_token}}/
</i>
{% if room_sync %}
<div class="alert alert-warning alert-dismissible fade show" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
<strong> Unsync Rooms:</strong> {{room_sync.total}}
<small>(open here and closed in Rocket.Chat)</small>
{% if room_sync.total %}
<p class="mt-3">
<a name="" id="" class="btn btn-success" href="?check-room-sync=1&do-check-room-sync=1" role="button">sync!</a>
</p>
{% endif %}
</div>

<script>
$(".alert").alert();
</script>

{% endif %}
</div>


Expand Down

0 comments on commit 7e2d835

Please sign in to comment.