Welcome to mirror list, hosted at ThFree Co, Russian Federation.

NewMessageModal.vue « components « src - github.com/nextcloud/mail.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fe37d7584641a6e5e217e6a54416669158ae8306 (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<template>
	<Modal
		size="normal"
		:title="t('mail', 'New message')"
		@close="$emit('close')">
		<Composer v-if="!fetchingTemplateMessage"
			:from-account="composerData.accountId"
			:to="composerData.to"
			:cc="composerData.cc"
			:bcc="composerData.bcc"
			:subject="composerData.subject"
			:body="composerData.body"
			:draft="saveDraft"
			:send="sendMessage"
			:forwarded-messages="forwardedMessages" />
	</Modal>
</template>
<script>
import Modal from '@nextcloud/vue/dist/Components/Modal'
import logger from '../logger'
import { detect, html, plain, toPlain } from '../util/text'
import { saveDraft, sendMessage } from '../service/MessageService'
import Composer from './Composer'
import { showWarning } from '@nextcloud/dialogs'
import Axios from '@nextcloud/axios'
import { generateUrl } from '@nextcloud/router'
import { translate as t } from '@nextcloud/l10n'

export default {
	name: 'NewMessageModal',
	components: {
		Modal,
		Composer,
	},
	props: {
		forwardedMessages: {
			type: Array,
			required: false,
			default: () => [],
		},
		templateMessageId: {
			type: Number,
			required: false,
			default: undefined,
		},
	},
	data() {
		return {
			original: undefined,
			originalBody: undefined,
			fetchingTemplateMessage: true,
		}
	},
	computed: {
		composerData() {
			logger.debug('composing a new message or handling a mailto link', {
				threadId: this.$route.params.threadId,
			})

			let accountId
			// Only preselect an account when we're not in a unified mailbox
			if (this.$route.params.accountId !== 0 && this.$route.params.accountId !== '0') {
				accountId = parseInt(this.$route.params.accountId, 10)
			}
			if (this.templateMessageId !== undefined) {
				if (this.original.attachments.length) {
					showWarning(t('mail', 'Attachments were not copied. Please add them manually.'))
				}

				return {
					accountId: this.original.accountId,
					to: this.original.to,
					cc: this.original.cc,
					subject: this.original.subject,
					body: this.originalBody,
					originalBody: this.originalBody,
				}
			}

			return {
				accountId,
				to: this.stringToRecipients(this.$route.query.to),
				cc: this.stringToRecipients(this.$route.query.cc),
				subject: this.$route.query.subject || '',
				body: this.$route.query.body ? detect(this.$route.query.body) : html(''),
			}
		},
	},
	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,
				to: data.to.map(this.recipientToRfc822).join(', '),
				cc: data.cc.map(this.recipientToRfc822).join(', '),
				bcc: data.bcc.map(this.recipientToRfc822).join(', '),
				body: data.isHtml ? data.body.value : toPlain(data.body).value,
			}
			const { id } = await saveDraft(data.account, dataForServer)

			// Remove old draft envelope
			this.$store.commit('removeEnvelope', { id: data.draftId })
			this.$store.commit('removeMessage', { id: data.draftId })

			// Fetch new draft envelope
			await this.$store.dispatch('fetchEnvelope', id)

			return 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 })

			// Remove old draft envelope
			this.$store.commit('removeEnvelope', { id: data.draftId })
			this.$store.commit('removeMessage', { id: data.draftId })
		},
		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
		},
		recipientToRfc822(recipient) {
			if (recipient.email === recipient.label) {
				// From mailto or sender without proper label
				return recipient.email
			} else if (recipient.label === '') {
				// Invalid label
				return recipient.email
			} else if (recipient.email.search(/^[a-zA-Z]+:/) === 0) {
				// Group integration
				return recipient.email
			} else {
				// Proper layout with label
				return `"${recipient.label}" <${recipient.email}>`
			}
		},
	},
}

</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>