You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.4 KiB

5 years ago
import {toSnake,toCamel} from '../utils/case'
import * as cronUtils from './cron'
function chatToJson(c) {
5 years ago
if(!c) return {}
const ch = c.dataValues||c
const chat = JSON.parse(JSON.stringify(ch))
5 years ago
let contactIds = chat.contactIds || null
if(chat.contactIds && typeof chat.contactIds==='string'){
contactIds = JSON.parse(chat.contactIds)
}
delete chat.groupPrivateKey
5 years ago
return toSnake({
...chat,
contactIds
})
}
5 years ago
function messageToJson(msg, chat, contact?) {
5 years ago
if(!msg) return {}
5 years ago
const message = msg.dataValues||msg
let statusMap = message.statusMap || null
if(message.statusMap && typeof message.statusMap==='string'){
statusMap = JSON.parse(message.statusMap)
}
return toSnake({
...message,
statusMap,
chat: chat ? chatToJson(chat) : null,
5 years ago
contact: contact ? contactToJson(contact) : null
5 years ago
})
}
5 years ago
function contactToJson(contact){
if(!contact) return {}
return toSnake(contact.dataValues||contact)
}
5 years ago
const inviteToJson = (invite) => toSnake(invite.dataValues||invite)
const jsonToContact = (json) => toCamel(json)
function subscriptionToJson(subscription, chat) {
const sub = subscription.dataValues || subscription
const { interval, next } = cronUtils.parse(sub.cron)
return toSnake({
...sub,
interval,
next,
chat: chat ? chatToJson(chat) : null,
})
}
export {
messageToJson,
contactToJson,
inviteToJson,
jsonToContact,
chatToJson,
subscriptionToJson,
}