-
Notifications
You must be signed in to change notification settings - Fork 26
/
CreateMediaArticle.js
291 lines (263 loc) · 7.72 KB
/
CreateMediaArticle.js
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import { GraphQLString, GraphQLNonNull } from 'graphql';
import sharp from 'sharp';
import { MediaType, variants } from '@cofacts/media-manager';
import mediaManager, {
IMAGE_PREVIEW,
IMAGE_THUMBNAIL,
} from 'util/mediaManager';
import { assertUser, getContentDefaultStatus } from 'util/user';
import client from 'util/client';
import { getAIResponse } from 'graphql/util';
import { schema } from 'prosemirror-schema-basic';
import Y from 'yjs';
import { EditorState } from 'prosemirror-state';
import { prosemirrorToYDoc } from 'y-prosemirror';
import { ArticleReferenceInput } from 'graphql/models/ArticleReference';
import MutationResult from 'graphql/models/MutationResult';
import { createOrUpdateReplyRequest } from './CreateOrUpdateReplyRequest';
import ArticleTypeEnum from 'graphql/models/ArticleTypeEnum';
const METADATA = {
cacheControl: 'public, max-age=31536000, immutable',
};
const AI_TRANSCRIBER_DESCRIPTION = JSON.stringify({
id: 'ai-transcript',
appId: 'RUMORS_AI',
name: 'AI Transcript',
});
const VALID_ARTICLE_TYPE_TO_MEDIA_TYPE = {
IMAGE: MediaType.image,
VIDEO: MediaType.video,
AUDIO: MediaType.audio,
};
/**
* Upload media of specified article type from the given mediaUrl
*
* @param {object} param
* @param {string} param.mediaUrl
* @param {ArticleTypeEnum} param.articleType
* @returns {Promise<MediaEntry>}
*/
export async function uploadMedia({ mediaUrl, articleType }) {
const mappedMediaType = VALID_ARTICLE_TYPE_TO_MEDIA_TYPE[articleType];
const mediaEntry = await mediaManager.insert({
url: mediaUrl,
getVariantSettings(options) {
const { type, contentType } = options;
// Abort if articleType does not match mediaUrl's file type
//
if (!mappedMediaType || mappedMediaType !== type) {
throw new Error(
`Specified article type is "${articleType}", but the media file is a ${type}.`
);
}
switch (type) {
case MediaType.image:
return [
variants.original(contentType),
{
name: IMAGE_THUMBNAIL,
contentType: 'image/jpeg',
transform: sharp()
.resize({ height: 240, withoutEnlargement: true })
.jpeg({ quality: 60 }),
},
{
name: IMAGE_PREVIEW,
contentType: 'image/webp',
transform: sharp()
.resize({ width: 600, withoutEnlargement: true })
.webp({ quality: 30 }),
},
];
default:
return variants.defaultGetVariantSettings(options);
}
},
onUploadStop(error) {
/* istanbul ignore if */
if (error) {
console.error(`[createNewMediaArticle] onUploadStop error:`, error);
return;
}
mediaEntry.variants.forEach((variant) =>
mediaEntry.getFile(variant).setMetadata(METADATA)
);
},
});
return mediaEntry;
}
/**
* Creates a new article in ElasticSearch,
* or return an article which attachment.hash is similar to mediaUrl
*
* @param {object} param
* @param {MediaEntry} param.mediaEntry
* @param {ArticleTypeEnum} param.articleType
* @param {ArticleReferenceInput} param.reference
* @param {object} user - The user submitting this article
* @returns {Promise<string>} the new article's ID
*/
async function createNewMediaArticle({
mediaEntry,
articleType,
reference: originalReference,
user,
}) {
const attachmentHash = mediaEntry.id;
const text = '';
const now = new Date().toISOString();
const reference = {
...originalReference,
createdAt: now,
userId: user.id,
appId: user.appId,
};
const { body: matchedArticle } = await client.search({
index: 'articles',
type: 'doc',
body: {
query: {
term: {
attachmentHash,
},
},
},
});
if (matchedArticle.hits.total) {
return matchedArticle.hits.hits[0]._id;
}
// use elasticsearch created id
const {
body: { _id: articleId },
} = await client.index({
index: 'articles',
type: 'doc',
body: {
text,
createdAt: now,
updatedAt: now,
userId: user.id,
appId: user.appId,
references: [reference],
articleReplies: [],
articleCategories: [],
normalArticleReplyCount: 0,
normalArticleCategoryCount: 0,
replyRequestCount: 0,
tags: [],
hyperlinks: [],
articleType,
attachmentHash,
status: getContentDefaultStatus(user),
contributors: [],
},
});
return articleId;
}
/**
* @param {string} articleId - For target article
* @param {string} text
* @returns result of article & ydoc operation
*/
export function writeAITranscript(articleId, text) {
// Write aiResponse to articles
const writeToArticleTextPromise = client.update({
index: 'articles',
type: 'doc',
id: articleId,
body: { doc: { text } },
});
// Prosemirror editor state with AI response text
const tempState = EditorState.create({ schema });
const proseMirrorState = tempState.apply(tempState.tr.insertText(text));
// Encode ProseMirror doc node into binary in the same way as Hocuspocus
// @ref https://tiptap.dev/hocuspocus/guides/persistence#faq-in-what-format-should-i-save-my-document
const ydoc = prosemirrorToYDoc(proseMirrorState.doc);
// Setup user mapping
const permanentUserData = new Y.PermanentUserData(ydoc);
permanentUserData.setUserMapping(
ydoc,
ydoc.clientID,
AI_TRANSCRIBER_DESCRIPTION
);
// Create initial version snapshot
const snapshot = Y.snapshot(ydoc);
// Create Y.doc and write to ydoc collection
const createYdocPromise = client.index({
index: 'ydocs',
type: 'doc',
id: articleId,
body: {
ydoc: Buffer.from(Y.encodeStateAsUpdate(ydoc)).toString('base64'),
versions: [
{
createdAt: new Date().toISOString(),
snapshot: Buffer.from(Y.encodeSnapshot(snapshot)).toString('base64'),
},
],
},
});
return Promise.all([writeToArticleTextPromise, createYdocPromise]);
}
export default {
type: MutationResult,
description: 'Create a media article and/or a replyRequest',
args: {
mediaUrl: { type: new GraphQLNonNull(GraphQLString) },
articleType: { type: new GraphQLNonNull(ArticleTypeEnum) },
reference: { type: new GraphQLNonNull(ArticleReferenceInput) },
reason: {
type: GraphQLString,
description: 'The reason why the user want to submit this article',
},
},
async resolve(
rootValue,
{ mediaUrl, articleType, reference, reason },
{ user }
) {
assertUser(user);
const mediaEntry = await uploadMedia({
mediaUrl,
articleType,
});
const aritcleIdPromise = createNewMediaArticle({
mediaEntry,
articleType,
reference,
user,
});
const aiResponsePromise = getAIResponse({
type: 'TRANSCRIPT',
docId: mediaEntry.id,
});
await Promise.all([
// Update reply request
aritcleIdPromise.then((articleId) =>
createOrUpdateReplyRequest({
articleId,
user,
reason,
})
),
// Write AI transcript to article & ydoc
Promise.all([aritcleIdPromise, aiResponsePromise])
.then(([articleId, aiResponse]) => {
if (!aiResponse) {
throw new Error('AI transcript not found');
}
return writeAITranscript(articleId, aiResponse.text);
})
.then(() => {
console.log(
`[CreateMediaArticle] AI transcript for ${mediaEntry.id} applied`
);
})
// It's OK to fail this promise, just log as warning
.catch((e) =>
console.warn(`[CreateMediaArticle] ${mediaEntry.id}:`, e)
),
]);
return { id: await aritcleIdPromise };
},
};