fix: remove vcard support, waiting for better implementation

This commit is contained in:
canove
2020-10-07 17:46:57 -03:00
parent ffce591817
commit a21ccbc539
7 changed files with 34 additions and 119 deletions

View File

@@ -0,0 +1,16 @@
import { QueryInterface, DataTypes } from "sequelize";
module.exports = {
up: (queryInterface: QueryInterface) => {
return queryInterface.removeColumn("Messages", "vcardContactId");
},
down: (queryInterface: QueryInterface) => {
return queryInterface.addColumn("Messages", "vcardContactId", {
type: DataTypes.INTEGER,
references: { model: "Contacts", key: "id" },
onUpdate: "CASCADE",
onDelete: "CASCADE"
});
}
};

View File

@@ -1,59 +0,0 @@
interface JCard {
[key: string]: any;
}
interface Meta {
[key: string]: string;
}
const ParseVcardToJson = (input: string): JCard => {
const Re1 = /^(version|fn|title|org):(.+)$/i;
const Re2 = /^([^:;]+);([^:]+):(.+)$/;
const ReKey = /item\d{1,2}\./;
const fields = {} as JCard;
input.split(/\r\n|\r|\n/).forEach(line => {
let results;
let key;
if (Re1.test(line)) {
results = line.match(Re1);
if (results) {
key = results[1].toLowerCase();
const [, , res] = results;
fields[key] = res;
}
} else if (Re2.test(line)) {
results = line.match(Re2);
if (results) {
key = results[1].replace(ReKey, "").toLowerCase();
const meta = {} as Meta;
results[2]
.split(";")
.map((p, i) => {
const match = p.match(/([a-z]+)=(.*)/i);
if (match) {
return [match[1], match[2]];
}
return [`TYPE${i === 0 ? "" : i}`, p];
})
.forEach(p => {
const [, m] = p;
meta[p[0]] = m;
});
if (!fields[key]) fields[key] = [];
fields[key].push({
meta,
value: results[3].split(";")
});
}
}
});
return fields;
};
export default ParseVcardToJson;

View File

@@ -72,13 +72,6 @@ class Message extends Model<Message> {
@BelongsTo(() => Contact, "contactId")
contact: Contact;
@ForeignKey(() => Contact)
@Column
vcardContactId: number;
@BelongsTo(() => Contact, "vcardContactId")
vcardContact: Contact;
}
export default Message;

View File

@@ -7,7 +7,6 @@ interface MessageData {
ticketId: number;
body: string;
contactId?: number;
vcardContactId?: number;
fromMe?: boolean;
read?: boolean;
mediaType?: string;
@@ -29,7 +28,7 @@ const CreateMessageService = async ({
await Message.upsert(messageData);
const message = await Message.findByPk(messageData.id, {
include: ["contact", "vcardContact"]
include: ["contact"]
});
if (!message) {

View File

@@ -44,7 +44,7 @@ const ListMessagesService = async ({
const { count, rows: messages } = await Message.findAndCountAll({
where: whereCondition,
limit,
include: ["contact", "vcardContact"],
include: ["contact"],
offset,
order: [["createdAt", "DESC"]]
});

View File

@@ -19,7 +19,6 @@ import { getIO } from "../../libs/socket";
import { getWbot } from "../../libs/wbot";
import AppError from "../../errors/AppError";
import ShowTicketService from "../TicketServices/ShowTicketService";
import ParseVcardToJson from "../../helpers/ParseVcardToJson";
import CreateMessageService from "../MessageServices/CreateMessageService";
const writeFileAsync = promisify(writeFile);
@@ -181,8 +180,7 @@ const handlMedia = async (
const handleMessage = async (
msg: WbotMessage,
ticket: Ticket,
contact: Contact,
vcardContact?: Contact
contact: Contact
) => {
let newMessage: Message | null;
@@ -193,7 +191,6 @@ const handleMessage = async (
id: msg.id.id,
ticketId: ticket.id,
contactId: msg.fromMe ? undefined : contact.id,
vcardContactId: vcardContact ? vcardContact.id : undefined,
body: msg.body,
fromMe: msg.fromMe,
mediaType: msg.type,
@@ -246,7 +243,6 @@ const wbotMessageListener = (whatsapp: Whatsapp): void => {
try {
let msgContact: WbotContact;
let groupContact: Contact | undefined;
let vcardContact: Contact | undefined;
if (msg.fromMe) {
msgContact = await wbot.getContactById(msg.to);
@@ -259,27 +255,25 @@ const wbotMessageListener = (whatsapp: Whatsapp): void => {
msgContact = await msg.getContact();
}
// if message has an author, its a gruop message.
const chat = await msg.getChat();
if (chat.isGroup) {
let msgGroupContact;
if (msg.fromMe) {
msgGroupContact = await wbot.getContactById(msg.to);
} else {
msgGroupContact = await wbot.getContactById(msg.from);
}
if (msg.author) {
const msgGroupContact = await wbot.getContactById(msg.from);
groupContact = await verifyGroup(msgGroupContact);
}
if (msg.type === "vcard") {
const { tel } = ParseVcardToJson(msg.body);
const vcardWaid = tel[0]?.meta?.waid;
const vcardMsgContact = await wbot.getContactById(`${vcardWaid}@c.us`);
const profilePicUrl = await vcardMsgContact.getProfilePicUrl();
vcardContact = await verifyContact(vcardMsgContact, profilePicUrl);
}
const profilePicUrl = await msgContact.getProfilePicUrl();
const contact = await verifyContact(msgContact, profilePicUrl);
const ticket = await verifyTicket(contact, whatsappId, groupContact);
await handleMessage(msg, ticket, contact, vcardContact);
await handleMessage(msg, ticket, contact);
} catch (err) {
Sentry.captureException(err);
console.log(err);
@@ -311,7 +305,7 @@ const wbotMessageListener = (whatsapp: Whatsapp): void => {
try {
const messageToUpdate = await Message.findByPk(msg.id.id, {
include: ["contact", "vcardContact"]
include: ["contact"]
});
if (!messageToUpdate) {
return;

View File

@@ -14,13 +14,7 @@ import DoneIcon from "@material-ui/icons/Done";
import DoneAllIcon from "@material-ui/icons/DoneAll";
import Paper from "@material-ui/core/Paper";
import {
Avatar,
Card,
CardActions,
CardHeader,
IconButton,
} from "@material-ui/core";
import { IconButton } from "@material-ui/core";
import { Block, ExpandMore } from "@material-ui/icons";
import api from "../../services/api";
@@ -449,27 +443,6 @@ const Ticket = () => {
controls
/>
);
}
if (message.mediaType === "vcard") {
return (
<Card className={classes.vcard} variant="outlined">
<CardHeader
avatar={
<Avatar
aria-label="contact-avatar"
src={message.vcardContact?.profilePicUrl}
/>
}
title={message.vcardContact?.name}
subheader={message.vcardContact?.number}
/>
<CardActions>
{/* <Button size="small" variant="contained">
Send Message
</Button> */}
</CardActions>
</Card>
);
} else {
return (
<a href={message.mediaUrl} target="_blank" rel="noopener noreferrer">
@@ -559,15 +532,14 @@ const Ticket = () => {
{renderDailyTimestamps(message, index)}
{renderMessageDivider(message, index)}
<div className={classes.messageLeft}>
{(message.mediaUrl || message.mediaType === "vcard") &&
checkMessageMedia(message)}
{message.mediaUrl && checkMessageMedia(message)}
{ticket.isGroup && (
<span className={classes.messageContactName}>
{message.contact?.name}
</span>
)}
<div className={classes.textContentItem}>
{message.mediaType !== "vcard" && message.body}
{message.body}
<span className={classes.timestamp}>
{format(parseISO(message.createdAt), "HH:mm")}
</span>