1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
<template>
<Modal
size="normal"
:title="t('mail', 'Outbox draft')"
@close="$emit('close')">
<Composer
:from-account="message.accountId"
:to="message.to"
:cc="message.cc"
:bcc="message.bcc"
:subject="message.subject"
:body="outboxBody"
:draft="saveDraft"
:send="sendMessage"
:forwarded-messages="forwardedMessages" />
</Modal>
</template>
<script>
import Modal from '@nextcloud/vue/dist/Components/Modal'
import logger from '../logger'
import { html, plain, toPlain } from '../util/text'
import Composer from './Composer'
import Axios from '@nextcloud/axios'
import { generateUrl } from '@nextcloud/router'
import { translate as t } from '@nextcloud/l10n'
export default {
name: 'OutboxComposer',
components: {
Modal,
Composer,
},
props: {
message: {
type: Object,
required: true,
},
forwardedMessages: {
type: Array,
required: false,
default: () => [],
},
templateMessageId: {
type: Number,
required: false,
default: undefined,
},
},
data() {
return {
original: undefined,
originalBody: undefined,
fetchingTemplateMessage: true,
}
},
computed: {
outboxBody() {
if (this.message.html) {
return html(this.message.text)
}
return plain(this.message.text)
},
},
created() {
this.fetchOriginalMessage()
},
methods: {
stringToRecipients(str) {
if (str === undefined) {
return []
}
return [
{
label: str,
email: str,
},
]
},
async saveDraft(data) {
if (data.draftId === undefined && this.draft) {
logger.debug('draft data does not have a draftId, adding one', {
draft: this.draft,
data,
id: this.draft.databaseId,
})
data.draftId = this.draft.databaseId
}
const dataForServer = {
...data,
body: data.isHtml ? data.body.value : toPlain(data.body).value,
}
await this.$store.dispatch('outbox/updateMessage', { message: dataForServer, id: this.message.id })
},
async sendMessage(data) {
logger.debug('sending message', { data })
const now = new Date().getTime()
const dataForServer = {
accountId: data.account,
sendAt: Math.floor(now / 1000), // JS timestamp is in milliseconds
subject: data.subject,
body: data.isHtml ? data.body.value : toPlain(data.body).value,
isHtml: data.isHtml,
isMdn: false,
inReplyToMessageId: '',
to: data.to,
cc: data.cc,
bcc: data.bcc,
attachmentIds: [],
}
const message = await this.$store.dispatch('outbox/enqueueMessage', {
message: dataForServer,
})
await this.$store.dispatch('outbox/sendMessage', { id: message.id })
},
async fetchOriginalMessage() {
if (this.templateMessageId === undefined) {
this.fetchingTemplateMessage = false
return
}
this.loading = true
this.error = undefined
this.errorMessage = ''
logger.debug(`fetching original message ${this.templateMessageId}`)
try {
const message = await this.$store.dispatch('fetchMessage', this.templateMessageId)
logger.debug('original message fetched', { message })
this.original = message
let body = plain(message.body || '')
if (message.hasHtmlBody) {
logger.debug('original message has HTML body')
const resp = await Axios.get(
generateUrl('/apps/mail/api/messages/{id}/html?plain=true', {
Id: this.templateMessageId,
})
)
body = html(resp.data)
}
this.originalBody = body
} catch (error) {
logger.error('could not load original message ' + this.templateMessageId, { error })
if (error.isError) {
this.errorMessage = t('mail', 'Could not load original message')
this.error = error
this.loading = false
}
} finally {
this.loading = false
}
this.fetchingTemplateMessage = false
},
},
}
</script>
<style lang="scss" scoped>
@media only screen and (max-width: 600px) {
::v-deep .modal-container {
max-width: 80%;
}
}
::v-deep .modal-container {
width: 80%;
min-height: 60%;
}
::v-deep .modal-wrapper .modal-container {
overflow-y: auto !important;
overflow-x: auto !important;
}
</style>
|