printWidth: 100

This commit is contained in:
Christopher committed 2021-03-02 21:03:06 +01:00
1 parent d27aeb3b0e
commit 6f4ee009a9
63 files changed
+602 -223

No files matched your search

+75 -58
View File
@@ -1,7 +1,11 @@
import md5 from 'md5';
import events from '../utils/events';
import { Message, MessageGroup, CreateMessageGroupResult } from '../../phone/src/common/typings/messages';
import {
Message,
MessageGroup,
CreateMessageGroupResult,
} from '../../phone/src/common/typings/messages';
import { pool, withTransaction } from './db';
import { getSource, getIdentifier } from './functions';
import { mainLogger } from './sv_logger';
@@ -42,7 +46,11 @@ interface MessageGroupMapping {
* @param groupId - the message group ID to attach this message to
* @param message - content of the message
*/
async function createMessage(userIdentifier: string, groupId: string, message: string): Promise<any> {
async function createMessage(
userIdentifier: string,
groupId: string,
message: string,
): Promise<any> {
const query = `
INSERT INTO npwd_messages
(user_identifier, message, group_id)
@@ -205,26 +213,32 @@ async function getMessageCountByGroup(groupId: string): Promise<number> {
*/
async function getConsolidatedMessageGroups(userIdentifier: string): Promise<MessageGroupMapping> {
const messageGroups = await getMessageGroups(userIdentifier);
return messageGroups.reduce((mapping: MessageGroupMapping, messageGroup: UnformattedMessageGroup) => {
const groupId = messageGroup.group_id;
const displayTerm = messageGroup.display || messageGroup.phone_number || '???';
return messageGroups.reduce(
(mapping: MessageGroupMapping, messageGroup: UnformattedMessageGroup) => {
const groupId = messageGroup.group_id;
const displayTerm = messageGroup.display || messageGroup.phone_number || '???';
if (groupId in mapping) {
mapping[groupId].participants = mapping[groupId].participants.concat(displayTerm);
} else {
mapping[groupId] = {
user_identifier: messageGroup.user_identifier,
avatar: messageGroup.avatar,
label: messageGroup.label,
participants: [displayTerm],
updatedAt: messageGroup.updatedAt ? messageGroup.updatedAt.toString() : null,
};
}
return mapping;
}, {});
if (groupId in mapping) {
mapping[groupId].participants = mapping[groupId].participants.concat(displayTerm);
} else {
mapping[groupId] = {
user_identifier: messageGroup.user_identifier,
avatar: messageGroup.avatar,
label: messageGroup.label,
participants: [displayTerm],
updatedAt: messageGroup.updatedAt ? messageGroup.updatedAt.toString() : null,
};
}
return mapping;
},
{},
);
}
async function getGroupIds(userIdentifier: string, groupMapping: MessageGroupMapping): Promise<string[]> {
async function getGroupIds(
userIdentifier: string,
groupMapping: MessageGroupMapping,
): Promise<string[]> {
const groupIds: string[] = [];
for (const groupId of Object.keys(groupMapping)) {
const isMine = groupMapping[groupId].user_identifier === userIdentifier;
@@ -352,53 +366,56 @@ onNet(events.MESSAGES_FETCH_MESSAGE_GROUPS, async () => {
}
});
onNet(events.MESSAGES_CREATE_MESSAGE_GROUP, async (phoneNumbers: string[], label: string = null) => {
const _source = getSource();
try {
const _identifier = await getIdentifier(_source);
const result = await createMessageGroupsFromPhoneNumbers(_identifier, phoneNumbers, label);
onNet(
events.MESSAGES_CREATE_MESSAGE_GROUP,
async (phoneNumbers: string[], label: string = null) => {
const _source = getSource();
try {
const _identifier = await getIdentifier(_source);
const result = await createMessageGroupsFromPhoneNumbers(_identifier, phoneNumbers, label);
if (result.error && result.duplicate) {
emitNet(events.MESSAGES_ACTION_RESULT, _source, {
message: 'MESSAGES_MESSAGE_GROUP_DUPLICATE',
type: 'error',
});
if (result.error && result.duplicate) {
emitNet(events.MESSAGES_ACTION_RESULT, _source, {
message: 'MESSAGES_MESSAGE_GROUP_DUPLICATE',
type: 'error',
});
// if the phoneNumber is invalid
} else if (result.error && result.phoneNumber) {
emitNet(events.MESSAGES_ACTION_RESULT, _source, {
message: 'MESSAGES_INVALID_PHONE_NUMBER',
type: 'error',
});
// if the phoneNumber is invalid
} else if (result.error && result.phoneNumber) {
emitNet(events.MESSAGES_ACTION_RESULT, _source, {
message: 'MESSAGES_INVALID_PHONE_NUMBER',
type: 'error',
});
// if you are try to add yourself
} else if (result.error && result.mine) {
emitNet(events.MESSAGES_ACTION_RESULT, _source, {
message: 'MESSAGES_MESSAGE_GROUP_CREATE_MINE',
type: 'error',
});
} else if (result.error) {
// if it just fails, welp
emitNet(events.MESSAGES_CREATE_MESSAGE_GROUP_FAILED, _source, result);
emitNet(events.MESSAGES_ACTION_RESULT, _source, {
message: `MESSAGES_MESSAGE_GROUP_CREATE_FAILED:`,
type: 'error',
});
} else {
emitNet(events.MESSAGES_CREATE_MESSAGE_GROUP_SUCCESS, _source, result);
}
} catch (e) {
emitNet(events.MESSAGES_CREATE_MESSAGE_GROUP_FAILED, _source);
// if you are try to add yourself
} else if (result.error && result.mine) {
emitNet(events.MESSAGES_ACTION_RESULT, _source, {
message: 'MESSAGES_MESSAGE_GROUP_CREATE_MINE',
type: 'error',
});
} else if (result.error) {
// if it just fails, welp
emitNet(events.MESSAGES_CREATE_MESSAGE_GROUP_FAILED, _source, result);
emitNet(events.MESSAGES_ACTION_RESULT, _source, {
message: `MESSAGES_MESSAGE_GROUP_CREATE_FAILED:`,
type: 'error',
});
} else {
emitNet(events.MESSAGES_CREATE_MESSAGE_GROUP_SUCCESS, _source, result);
messageLogger.error(`Failed to create message group, ${e.message}`, {
source: _source,
});
}
} catch (e) {
emitNet(events.MESSAGES_CREATE_MESSAGE_GROUP_FAILED, _source);
emitNet(events.MESSAGES_ACTION_RESULT, _source, {
message: `MESSAGES_MESSAGE_GROUP_CREATE_FAILED:`,
type: 'error',
});
messageLogger.error(`Failed to create message group, ${e.message}`, {
source: _source,
});
}
});
},
);
onNet(events.MESSAGES_FETCH_MESSAGES, async (groupId: string) => {
const _source = getSource();