123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- // Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
- import type { FormValues } from '@shared/components/Form'
- import type { FieldEditorProps } from '@shared/components/Form/fields/FieldEditor/types'
- import { useSessionStore } from '@shared/stores/session'
- import type { ConfigList } from '@shared/types/store'
- import type { ConfidentTake } from '@shared/types/utils'
- import { getInitials } from '@shared/utils/formatter'
- import { isArray, isObject, uniq } from 'lodash-es'
- import type {
- TicketArticleAction,
- TicketArticleActionPlugin,
- TicketArticleType,
- } from './types'
- const replyToTwitterComment = ((
- ticket,
- article,
- { openReplyDialog, getNewArticleBody },
- ) => {
- const articleData: FormValues = {
- articleType: 'twitter status',
- inReplyTo: article.messageId,
- }
- const body = getNewArticleBody('text/plain')
- const recipients = article.from ? [article.from.raw] : []
- if (article.to) recipients.push(article.to.raw)
- const recipientsString = uniq(
- recipients.filter((recipient) => {
- recipient = recipient.trim().toLowerCase()
- if (body.toLowerCase().includes(recipient)) return false
- if (recipient === `@${ticket.preferences?.channel_screen_name}`)
- return false
- return true
- }),
- ).join(' ')
- if (body) articleData.body = `${recipientsString} ${body} `
- else articleData.body = `${recipientsString} `
- openReplyDialog(articleData)
- }) satisfies TicketArticleAction['perform']
- const replyToTwitterDm = ((ticket, article, { openReplyDialog }) => {
- const sender = article.sender?.name
- let to: string | undefined | null
- if (sender === 'Customer') to = article.from?.raw
- else if (sender === 'Agent') to = article.to?.raw
- if (!to) {
- const autorization = article.createdBy.authorizations?.find(
- (a) => a.provider === 'twitter',
- )
- to = autorization?.username || autorization?.uid
- }
- const articleData: FormValues = {
- articleType: 'twitter direct-message',
- body: '',
- to: to ? [to] : [],
- inReplyTo: article.messageId,
- }
- openReplyDialog(articleData)
- }) satisfies TicketArticleAction['perform']
- const getTwitterInitials = (config: ConfigList) => {
- if (config.ui_ticket_zoom_article_twitter_initials) {
- const { user } = useSessionStore()
- if (user) {
- const { firstname, lastname, email } = user
- return `/${getInitials(firstname, lastname, email)}`
- }
- }
- return null
- }
- const actionPlugin: TicketArticleActionPlugin = {
- order: 300,
- addActions(ticket, article) {
- const type = article.type?.name
- if (type !== 'twitter status' && type !== 'twitter direct-message')
- return []
- const action: TicketArticleAction = {
- apps: ['mobile'],
- label: __('Reply'),
- name: type,
- icon: { mobile: 'mobile-reply' },
- view: {
- agent: ['change'],
- },
- perform(ticket, article, options) {
- if (type === 'twitter status')
- return replyToTwitterComment(ticket, article, options)
- return replyToTwitterDm(ticket, article, options)
- },
- }
- return [action]
- },
- addTypes(ticket, { config }) {
- const descriptionType = ticket.createArticleType?.name
- if (
- descriptionType !== 'twitter status' &&
- descriptionType !== 'twitter direct-message'
- )
- return []
- const type: TicketArticleType = {
- apps: ['mobile'],
- value: descriptionType,
- label: __('Twitter'),
- icon: {
- mobile: 'mobile-twitter',
- },
- view: {
- agent: ['change'],
- },
- attributes: [],
- internal: false,
- contentType: 'text/plain',
- updateForm(values) {
- if (!isObject(values.article) || isArray(values.article)) return values
- if (typeof values.article.body === 'string') {
- const initials = getTwitterInitials(config)
- values.article.body += initials ? `\n${initials}` : ''
- }
- return values
- },
- }
- let footer: ConfidentTake<FieldEditorProps, 'meta.footer'>
- if (descriptionType === 'twitter status') {
- footer = {
- maxlength: 280,
- warningLength: 30,
- }
- } else {
- type.attributes = ['to']
- footer = {
- maxlength: 10000,
- warningLength: 500,
- }
- }
- const initials = getTwitterInitials(config)
- if (initials) footer.text = initials
- type.editorMeta = {
- footer,
- }
- return [type]
- },
- }
- export default actionPlugin
|