2021-05-13 14:47:30 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { GroupV2Membership } from '../components/conversation/conversation-details/ConversationDetailsMembershipList';
|
|
|
|
import type {
|
2021-05-13 14:47:30 +00:00
|
|
|
GroupV2PendingMembership,
|
|
|
|
GroupV2RequestingMembership,
|
|
|
|
} from '../components/conversation/conversation-details/PendingInvites';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ConversationType } from '../state/ducks/conversations';
|
2023-08-10 16:43:33 +00:00
|
|
|
import type { ServiceIdString } from '../types/ServiceId';
|
2021-05-13 14:47:30 +00:00
|
|
|
import { isConversationUnregistered } from './isConversationUnregistered';
|
|
|
|
|
2022-11-01 17:34:23 +00:00
|
|
|
export type GroupMemberships = {
|
2022-12-22 00:07:02 +00:00
|
|
|
memberships: ReadonlyArray<GroupV2Membership>;
|
|
|
|
pendingApprovalMemberships: ReadonlyArray<GroupV2RequestingMembership>;
|
|
|
|
pendingMemberships: ReadonlyArray<GroupV2PendingMembership>;
|
2022-11-01 17:34:23 +00:00
|
|
|
};
|
|
|
|
|
2021-05-13 14:47:30 +00:00
|
|
|
export const getGroupMemberships = (
|
|
|
|
{
|
|
|
|
memberships = [],
|
|
|
|
pendingApprovalMemberships = [],
|
|
|
|
pendingMemberships = [],
|
|
|
|
}: Readonly<
|
|
|
|
Pick<
|
|
|
|
ConversationType,
|
|
|
|
'memberships' | 'pendingApprovalMemberships' | 'pendingMemberships'
|
|
|
|
>
|
|
|
|
>,
|
2023-08-10 16:43:33 +00:00
|
|
|
getConversationByServiceId: (
|
|
|
|
serviceId: ServiceIdString
|
|
|
|
) => undefined | ConversationType
|
2022-11-01 17:34:23 +00:00
|
|
|
): GroupMemberships => ({
|
2021-05-13 14:47:30 +00:00
|
|
|
memberships: memberships.reduce(
|
2022-12-22 00:07:02 +00:00
|
|
|
(result: ReadonlyArray<GroupV2Membership>, membership) => {
|
2023-08-16 20:54:39 +00:00
|
|
|
const member = getConversationByServiceId(membership.aci);
|
2021-07-28 18:52:49 +00:00
|
|
|
if (!member) {
|
2021-05-13 14:47:30 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return [...result, { isAdmin: membership.isAdmin, member }];
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
),
|
|
|
|
pendingApprovalMemberships: pendingApprovalMemberships.reduce(
|
2022-12-22 00:07:02 +00:00
|
|
|
(result: ReadonlyArray<GroupV2RequestingMembership>, membership) => {
|
2023-08-16 20:54:39 +00:00
|
|
|
const member = getConversationByServiceId(membership.aci);
|
2021-05-13 14:47:30 +00:00
|
|
|
if (!member || isConversationUnregistered(member)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return [...result, { member }];
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
),
|
|
|
|
pendingMemberships: pendingMemberships.reduce(
|
2022-12-22 00:07:02 +00:00
|
|
|
(result: ReadonlyArray<GroupV2PendingMembership>, membership) => {
|
2023-08-16 20:54:39 +00:00
|
|
|
const member = getConversationByServiceId(membership.serviceId);
|
2021-05-13 14:47:30 +00:00
|
|
|
if (!member || isConversationUnregistered(member)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return [
|
|
|
|
...result,
|
|
|
|
{
|
|
|
|
member,
|
|
|
|
metadata: { addedByUserId: membership.addedByUserId },
|
|
|
|
},
|
|
|
|
];
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
),
|
|
|
|
});
|