From c06f710b3bb72e7352e136503ceac17cad4a1da6 Mon Sep 17 00:00:00 2001
From: Audric Ackermann <audric@loki.network>
Date: Mon, 1 Feb 2021 13:12:44 +1100
Subject: [PATCH] fix order of added new group in leftPane

---
 ts/components/LeftPane.tsx  |  2 --
 ts/receiver/closedGroups.ts | 23 ++++++++++++-----------
 ts/session/group/index.ts   |  3 +++
 3 files changed, 15 insertions(+), 13 deletions(-)

diff --git a/ts/components/LeftPane.tsx b/ts/components/LeftPane.tsx
index d4790ba31..78f83919a 100644
--- a/ts/components/LeftPane.tsx
+++ b/ts/components/LeftPane.tsx
@@ -6,12 +6,10 @@ import { LeftPaneMessageSection } from './session/LeftPaneMessageSection';
 import { ConversationListItemProps } from './ConversationListItem';
 import { SearchResultsProps } from './SearchResults';
 import { SearchOptions } from '../types/Search';
-import { LeftPaneSectionHeader } from './session/LeftPaneSectionHeader';
 
 import { ConversationType } from '../state/ducks/conversations';
 import { LeftPaneContactSection } from './session/LeftPaneContactSection';
 import { LeftPaneSettingSection } from './session/LeftPaneSettingSection';
-import { SessionIconType } from './session/icon';
 import { SessionTheme } from '../state/ducks/SessionTheme';
 import { DefaultTheme } from 'styled-components';
 import { SessionSettingCategory } from './session/settings/SessionSettings';
diff --git a/ts/receiver/closedGroups.ts b/ts/receiver/closedGroups.ts
index 483e25189..83bdf1fb2 100644
--- a/ts/receiver/closedGroups.ts
+++ b/ts/receiver/closedGroups.ts
@@ -197,16 +197,19 @@ async function handleNewClosedGroup(
     'incoming'
   );
 
-  convo.set('name', name);
-  convo.set('members', members);
-  // mark a closed group as a medium group.
-  // this field is used to poll for this groupPubKey on the swarm nodes, among other things
-  convo.set('is_medium_group', true);
-  convo.set('active_at', Date.now());
-  convo.set('lastJoinedTimestamp', Date.now());
-
   // We only set group admins on group creation
-  convo.set('groupAdmins', admins);
+  const groupDetails = {
+    id: groupId,
+    name: name,
+    members: members,
+    admins,
+    active: true,
+  };
+
+  // be sure to call this before sending the message.
+  // the sending pipeline needs to know from GroupUtils when a message is for a medium group
+  await ClosedGroup.updateOrCreateClosedGroup(groupDetails);
+
   await convo.commit();
   // sanity checks validate this
   // tslint:disable: no-non-null-assertion
@@ -733,8 +736,6 @@ export async function createClosedGroup(
   // be sure to call this before sending the message.
   // the sending pipeline needs to know from GroupUtils when a message is for a medium group
   await ClosedGroup.updateOrCreateClosedGroup(groupDetails);
-  convo.set('lastJoinedTimestamp', Date.now());
-  convo.set('active_at', Date.now());
   await convo.commit();
   convo.updateLastMessage();
 
diff --git a/ts/session/group/index.ts b/ts/session/group/index.ts
index 12262604b..06cbbc981 100644
--- a/ts/session/group/index.ts
+++ b/ts/session/group/index.ts
@@ -258,8 +258,11 @@ export async function updateOrCreateClosedGroup(details: GroupInfo) {
     //   activeAt is null, then this group has been purposefully hidden.
     if (activeAt !== null) {
       updates.active_at = activeAt || Date.now();
+      updates.timestamp = updates.active_at;
     }
     updates.left = false;
+    updates.lastJoinedTimestamp = updates.active_at;
+
   } else {
     updates.left = true;
   }