2021-06-16 01:33:40 +02:00
|
|
|
import React, { useState } from 'react';
|
2019-11-22 06:16:43 +01:00
|
|
|
|
2020-01-20 05:40:24 +01:00
|
|
|
import { SessionModal } from '../session/SessionModal';
|
2020-07-08 07:46:37 +02:00
|
|
|
import { SessionButton, SessionButtonColor } from '../session/SessionButton';
|
2021-04-22 10:03:58 +02:00
|
|
|
import { ContactType, SessionMemberListItem } from '../session/SessionMemberListItem';
|
2021-06-18 06:20:36 +02:00
|
|
|
import { DefaultTheme, useTheme } from 'styled-components';
|
2021-04-28 05:56:33 +02:00
|
|
|
import { ConversationController } from '../../session/conversations';
|
|
|
|
import { ToastUtils, UserUtils } from '../../session/utils';
|
|
|
|
import { initiateGroupUpdate } from '../../session/group';
|
2021-04-28 06:20:22 +02:00
|
|
|
import { ConversationModel, ConversationTypeEnum } from '../../models/conversation';
|
2021-06-18 06:20:36 +02:00
|
|
|
import { getCompleteUrlForV2ConvoId } from '../../interactions/conversationInteractions';
|
2021-04-28 05:56:33 +02:00
|
|
|
import _ from 'lodash';
|
2021-05-12 02:34:53 +02:00
|
|
|
import { VALIDATION } from '../../session/constants';
|
2021-06-16 01:33:40 +02:00
|
|
|
import { SessionWrapperModal } from '../session/SessionWrapperModal';
|
2021-06-17 08:51:59 +02:00
|
|
|
import { SpacerLG } from '../basic/Text';
|
2021-06-18 06:20:36 +02:00
|
|
|
import { useDispatch } from 'react-redux';
|
|
|
|
import { updateInviteContactModal } from '../../state/ducks/modalDialog';
|
2021-06-16 01:33:40 +02:00
|
|
|
|
2021-06-17 06:55:25 +02:00
|
|
|
type Props = {
|
2021-06-18 06:20:36 +02:00
|
|
|
conversationId: string;
|
2021-06-17 06:55:25 +02:00
|
|
|
};
|
2019-11-22 06:16:43 +01:00
|
|
|
|
2021-06-16 01:33:40 +02:00
|
|
|
const InviteContactsDialogInner = (props: Props) => {
|
2021-06-18 06:20:36 +02:00
|
|
|
const { conversationId } = props;
|
|
|
|
|
|
|
|
const theme = useTheme();
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
|
|
|
const convo = ConversationController.getInstance().get(conversationId);
|
2021-06-17 06:55:25 +02:00
|
|
|
// tslint:disable-next-line: max-func-body-length
|
2019-11-22 06:16:43 +01:00
|
|
|
|
2021-06-17 06:55:25 +02:00
|
|
|
let contacts = ConversationController.getInstance()
|
|
|
|
.getConversations()
|
|
|
|
.filter(d => !!d && !d.isBlocked() && d.isPrivate() && !d.isMe() && !!d.get('active_at'));
|
2021-06-16 01:33:40 +02:00
|
|
|
if (!convo.isPublic()) {
|
|
|
|
// filter our zombies and current members from the list of contact we can add
|
2019-11-22 06:16:43 +01:00
|
|
|
|
2021-06-16 01:33:40 +02:00
|
|
|
const members = convo.get('members') || [];
|
|
|
|
const zombies = convo.get('zombies') || [];
|
2021-06-17 06:55:25 +02:00
|
|
|
contacts = contacts.filter(d => !members.includes(d.id) && !zombies.includes(d.id));
|
2021-06-16 01:33:40 +02:00
|
|
|
}
|
2019-11-22 06:16:43 +01:00
|
|
|
|
2021-06-16 01:33:40 +02:00
|
|
|
const chatName = convo.get('name');
|
|
|
|
// const chatServer = convo.get('server');
|
|
|
|
// const channelId = convo.get('channelId');
|
|
|
|
const isPublicConvo = convo.isPublic();
|
2020-02-04 08:07:31 +01:00
|
|
|
|
2021-06-16 01:33:40 +02:00
|
|
|
const [contactList, setContactList] = useState(
|
|
|
|
contacts.map((d: ConversationModel) => {
|
2019-11-22 06:16:43 +01:00
|
|
|
const lokiProfile = d.getLokiProfile();
|
2021-05-12 02:40:49 +02:00
|
|
|
const nickname = d.getNickname();
|
|
|
|
const name = nickname
|
|
|
|
? nickname
|
|
|
|
: lokiProfile
|
|
|
|
? lokiProfile.displayName
|
|
|
|
: window.i18n('anonymous');
|
2019-11-22 06:16:43 +01:00
|
|
|
|
|
|
|
// TODO: should take existing members into account
|
|
|
|
const existingMember = false;
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: d.id,
|
|
|
|
authorPhoneNumber: d.id,
|
|
|
|
authorProfileName: name,
|
2021-06-16 01:33:40 +02:00
|
|
|
authorAvatarPath: d?.getAvatarPath() || '',
|
2019-11-22 06:16:43 +01:00
|
|
|
selected: false,
|
|
|
|
authorName: name,
|
|
|
|
checkmarked: false,
|
|
|
|
existingMember,
|
|
|
|
};
|
2021-06-16 01:33:40 +02:00
|
|
|
})
|
2021-06-17 06:55:25 +02:00
|
|
|
);
|
2019-11-22 06:16:43 +01:00
|
|
|
|
2021-06-16 01:33:40 +02:00
|
|
|
const closeDialog = () => {
|
|
|
|
window.removeEventListener('keyup', onKeyUp);
|
2021-06-18 06:20:36 +02:00
|
|
|
dispatch(updateInviteContactModal(null));
|
2021-06-17 06:55:25 +02:00
|
|
|
};
|
2019-11-22 06:16:43 +01:00
|
|
|
|
2021-06-16 01:33:40 +02:00
|
|
|
const onClickOK = () => {
|
2021-06-17 06:55:25 +02:00
|
|
|
const selectedContacts = contactList
|
|
|
|
.filter((d: ContactType) => d.checkmarked)
|
|
|
|
.map((d: ContactType) => d.id);
|
2019-11-22 06:16:43 +01:00
|
|
|
|
2021-06-16 01:33:40 +02:00
|
|
|
if (selectedContacts.length > 0) {
|
|
|
|
if (isPublicConvo) {
|
|
|
|
void submitForOpenGroup(selectedContacts);
|
|
|
|
} else {
|
|
|
|
void submitForClosedGroup(selectedContacts);
|
|
|
|
}
|
|
|
|
}
|
2019-11-22 06:16:43 +01:00
|
|
|
|
2021-06-16 01:33:40 +02:00
|
|
|
closeDialog();
|
2021-06-17 06:55:25 +02:00
|
|
|
};
|
2021-05-27 06:35:30 +02:00
|
|
|
|
2021-06-16 01:33:40 +02:00
|
|
|
const onKeyUp = (event: any) => {
|
|
|
|
switch (event.key) {
|
|
|
|
case 'Enter':
|
|
|
|
onClickOK();
|
|
|
|
break;
|
|
|
|
case 'Esc':
|
|
|
|
case 'Escape':
|
|
|
|
closeDialog();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
}
|
2021-06-17 06:55:25 +02:00
|
|
|
};
|
2021-06-16 01:33:40 +02:00
|
|
|
window.addEventListener('keyup', onKeyUp);
|
|
|
|
|
|
|
|
const titleText = `${window.i18n('addingContacts')} ${chatName}`;
|
|
|
|
const cancelText = window.i18n('cancel');
|
|
|
|
const okText = window.i18n('ok');
|
|
|
|
|
|
|
|
const hasContacts = contactList.length !== 0;
|
2021-04-28 05:56:33 +02:00
|
|
|
|
2021-06-16 01:33:40 +02:00
|
|
|
const submitForOpenGroup = async (pubkeys: Array<string>) => {
|
2021-06-17 06:55:25 +02:00
|
|
|
if (convo.isOpenGroupV2()) {
|
2021-04-28 05:56:33 +02:00
|
|
|
const completeUrl = await getCompleteUrlForV2ConvoId(convo.id);
|
|
|
|
const groupInvitation = {
|
|
|
|
serverAddress: completeUrl,
|
|
|
|
serverName: convo.getName(),
|
|
|
|
};
|
|
|
|
pubkeys.forEach(async pubkeyStr => {
|
|
|
|
const privateConvo = await ConversationController.getInstance().getOrCreateAndWait(
|
|
|
|
pubkeyStr,
|
2021-04-28 06:20:22 +02:00
|
|
|
ConversationTypeEnum.PRIVATE
|
2021-04-28 05:56:33 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
if (privateConvo) {
|
|
|
|
void privateConvo.sendMessage('', null, null, null, groupInvitation);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-06-17 06:55:25 +02:00
|
|
|
};
|
2021-04-28 05:56:33 +02:00
|
|
|
|
2021-06-16 01:33:40 +02:00
|
|
|
const submitForClosedGroup = async (pubkeys: Array<string>) => {
|
2021-05-05 05:09:01 +02:00
|
|
|
// closed group chats
|
2021-04-28 05:56:33 +02:00
|
|
|
const ourPK = UserUtils.getOurPubKeyStrFromCache();
|
2021-05-05 05:09:01 +02:00
|
|
|
// we only care about real members. If a member is currently a zombie we have to be able to add him back
|
2021-04-28 05:56:33 +02:00
|
|
|
let existingMembers = convo.get('members') || [];
|
|
|
|
// at least make sure it's an array
|
|
|
|
if (!Array.isArray(existingMembers)) {
|
|
|
|
existingMembers = [];
|
|
|
|
}
|
|
|
|
existingMembers = _.compact(existingMembers);
|
2021-05-05 10:14:59 +02:00
|
|
|
const existingZombies = convo.get('zombies') || [];
|
2021-04-28 05:56:33 +02:00
|
|
|
const newMembers = pubkeys.filter(d => !existingMembers.includes(d));
|
|
|
|
|
|
|
|
if (newMembers.length > 0) {
|
|
|
|
// Do not trigger an update if there is too many members
|
2021-05-05 10:14:59 +02:00
|
|
|
// be sure to include current zombies in this count
|
|
|
|
if (
|
|
|
|
newMembers.length + existingMembers.length + existingZombies.length >
|
2021-05-12 02:34:53 +02:00
|
|
|
VALIDATION.CLOSED_GROUP_SIZE_LIMIT
|
2021-05-05 10:14:59 +02:00
|
|
|
) {
|
2021-04-28 05:56:33 +02:00
|
|
|
ToastUtils.pushTooManyMembers();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const allMembers = _.concat(existingMembers, newMembers, [ourPK]);
|
|
|
|
const uniqMembers = _.uniq(allMembers);
|
|
|
|
|
|
|
|
const groupId = convo.get('id');
|
|
|
|
const groupName = convo.get('name');
|
|
|
|
|
|
|
|
await initiateGroupUpdate(
|
|
|
|
groupId,
|
|
|
|
groupName || window.i18n('unknown'),
|
|
|
|
uniqMembers,
|
|
|
|
undefined
|
|
|
|
);
|
|
|
|
}
|
2021-06-17 06:55:25 +02:00
|
|
|
};
|
2019-11-22 06:16:43 +01:00
|
|
|
|
2021-06-16 01:33:40 +02:00
|
|
|
const renderMemberList = () => {
|
|
|
|
const members = contactList;
|
2021-06-17 06:55:25 +02:00
|
|
|
const selectedContacts = contactList
|
|
|
|
.filter((d: ContactType) => d.checkmarked)
|
|
|
|
.map((d: ContactType) => d.id);
|
2020-02-12 03:53:18 +01:00
|
|
|
|
2020-05-19 04:01:42 +02:00
|
|
|
return members.map((member: ContactType, index: number) => (
|
2020-02-12 03:53:18 +01:00
|
|
|
<SessionMemberListItem
|
|
|
|
member={member}
|
2020-05-21 07:10:54 +02:00
|
|
|
key={index}
|
2020-05-19 04:01:42 +02:00
|
|
|
index={index}
|
2021-01-13 02:36:31 +01:00
|
|
|
isSelected={selectedContacts.some(m => m === member.id)}
|
2020-02-12 03:53:18 +01:00
|
|
|
onSelect={(selectedMember: ContactType) => {
|
2021-06-16 01:33:40 +02:00
|
|
|
onMemberClicked(selectedMember);
|
2020-02-12 03:53:18 +01:00
|
|
|
}}
|
|
|
|
onUnselect={(selectedMember: ContactType) => {
|
2021-06-16 01:33:40 +02:00
|
|
|
onMemberClicked(selectedMember);
|
2020-02-12 03:53:18 +01:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
));
|
2021-06-17 06:55:25 +02:00
|
|
|
};
|
2019-11-22 06:16:43 +01:00
|
|
|
|
2021-06-16 01:33:40 +02:00
|
|
|
const onMemberClicked = (clickedMember: ContactType) => {
|
|
|
|
const updatedContacts = contactList.map((member: ContactType) => {
|
2020-02-12 03:53:18 +01:00
|
|
|
if (member.id === clickedMember.id) {
|
2019-11-22 06:16:43 +01:00
|
|
|
return { ...member, checkmarked: !member.checkmarked };
|
|
|
|
} else {
|
|
|
|
return member;
|
|
|
|
}
|
|
|
|
});
|
2021-06-16 01:33:40 +02:00
|
|
|
setContactList(updatedContacts);
|
2021-06-17 06:55:25 +02:00
|
|
|
};
|
2021-06-16 01:33:40 +02:00
|
|
|
|
|
|
|
return (
|
2021-06-18 06:20:36 +02:00
|
|
|
<SessionWrapperModal title={titleText} onClose={closeDialog}>
|
2021-06-17 08:51:59 +02:00
|
|
|
<SpacerLG />
|
2021-06-16 01:33:40 +02:00
|
|
|
|
|
|
|
<div className="contact-selection-list">{renderMemberList()}</div>
|
|
|
|
{hasContacts ? null : (
|
|
|
|
<>
|
2021-06-17 08:51:59 +02:00
|
|
|
<SpacerLG />
|
2021-06-16 01:33:40 +02:00
|
|
|
<p className="no-contacts">{window.i18n('noContactsToAdd')}</p>
|
2021-06-17 08:51:59 +02:00
|
|
|
<SpacerLG />
|
2021-06-16 01:33:40 +02:00
|
|
|
</>
|
|
|
|
)}
|
|
|
|
|
2021-06-17 08:51:59 +02:00
|
|
|
<SpacerLG />
|
2021-06-16 01:33:40 +02:00
|
|
|
|
|
|
|
<div className="session-modal__button-group">
|
|
|
|
<SessionButton text={cancelText} onClick={closeDialog} />
|
|
|
|
<SessionButton
|
|
|
|
text={okText}
|
|
|
|
disabled={!hasContacts}
|
|
|
|
onClick={onClickOK}
|
|
|
|
buttonColor={SessionButtonColor.Green}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</SessionWrapperModal>
|
|
|
|
);
|
2021-06-17 06:55:25 +02:00
|
|
|
};
|
2020-12-03 02:09:39 +01:00
|
|
|
|
2020-12-03 03:47:32 +01:00
|
|
|
export const InviteContactsDialog = InviteContactsDialogInner;
|