diff --git a/i18n/zh_TW.po b/i18n/zh_TW.po index b78f92d0..b4ac4bbe 100644 --- a/i18n/zh_TW.po +++ b/i18n/zh_TW.po @@ -50,30 +50,30 @@ msgstr "出處" msgid "This reply has no ${ prompt } and it may be biased" msgstr "此回應沒有${prompt},請自行斟酌回應之可信度。" -#: src/webhook/handlers/choosingArticle.js:152 +#: src/webhook/handlers/choosingArticle.js:148 msgid "Volunteer editors have published several replies to this message." msgstr "真的假的查證志工對這則訊息發表了多則看法唷!" -#: src/webhook/handlers/choosingArticle.js:274 +#: src/webhook/handlers/choosingArticle.js:270 msgid "Let's pick one" msgstr "選一則來閱讀吧" -#: src/webhook/handlers/choosingArticle.js:253 +#: src/webhook/handlers/choosingArticle.js:249 msgid "Take a look" msgstr "看他怎麼說" -#: src/webhook/handlers/choosingArticle.js:290 +#: src/webhook/handlers/choosingArticle.js:286 #, javascript-format msgid "Visit ${ articleUrl } for more replies." msgstr "更多回應請到:${ articleUrl }" -#: src/webhook/handlers/initState.js:202 -#: src/webhook/handlers/processImage.js:116 +#: src/webhook/handlers/initState.js:194 +#: src/webhook/handlers/processImage.js:122 msgid "Choose this one" msgstr "選擇這篇" -#: src/webhook/handlers/initState.js:294 -#: src/webhook/handlers/processImage.js:206 +#: src/webhook/handlers/initState.js:286 +#: src/webhook/handlers/processImage.js:212 msgid "" "Internet rumors are often mutated and shared.\n" "Please choose the version that looks the most similar" @@ -116,10 +116,10 @@ msgstr "是" msgid "No" msgstr "否" -#: src/webhook/handlers/initState.js:245 -#: src/webhook/handlers/initState.js:263 -#: src/webhook/handlers/processImage.js:158 -#: src/webhook/handlers/processImage.js:176 +#: src/webhook/handlers/initState.js:237 +#: src/webhook/handlers/initState.js:255 +#: src/webhook/handlers/processImage.js:164 +#: src/webhook/handlers/processImage.js:182 msgid "None of these messages matches mine :(" msgstr "找不到我想查的訊息 QQ" @@ -129,26 +129,26 @@ msgid "${ positive } user considers this helpful" msgid_plural "${ positive } users consider this helpful" msgstr[0] "有 ${positive} 人覺得此回應有幫助" -#: src/webhook/handlers/initState.js:261 -#: src/webhook/handlers/processImage.js:174 +#: src/webhook/handlers/initState.js:253 +#: src/webhook/handlers/processImage.js:180 msgid "Tell us more" msgstr "回報此訊息" -#: src/webhook/handlers/initState.js:277 -#: src/webhook/handlers/processImage.js:189 +#: src/webhook/handlers/initState.js:269 +#: src/webhook/handlers/processImage.js:195 msgid "Please choose the most similar message from the list." msgstr "請從下列選擇您要查的訊息。" -#: src/webhook/handlers/choosingArticle.js:278 +#: src/webhook/handlers/choosingArticle.js:274 msgid "Please take a look at the following replies." msgstr "請從下列選擇您要查看的回應。" -#: src/webhook/handlers/choosingArticle.js:199 +#: src/webhook/handlers/choosingArticle.js:195 #, javascript-format msgid "Someone thinks it ${ typeWords }" msgstr "有人認為它${typeWords}" -#: src/webhook/handlers/initState.js:179 +#: src/webhook/handlers/initState.js:171 #, javascript-format msgid "Looks ${ similarityPercentage }% similar" msgstr "看起來 ${similarityPercentage}% 像" @@ -159,8 +159,7 @@ msgid "" "a few minutes later." msgstr "不好意思!系統可能在忙線中,無法及時處理您傳的訊息。請稍等幾分鐘再試試看唷。" -#: src/webhook/handleInput.js:107 -#: src/webhook/handlePostback.js:79 +#: src/webhook/handlePostback.js:87 msgid "Wrong usage" msgstr "這不是這樣用的" @@ -170,13 +169,13 @@ msgid "" "search sessions do not work now." msgstr "您已經在搜尋新的訊息了,過去查過的訊息的按鈕已經失效囉。" -#: src/webhook/index.js:260 +#: src/webhook/index.js:261 msgid "" "Oops, something is not working. We have cleared your search data, hopefully " "the error will go away. Would you please send us the message from the start?" msgstr "抱歉,chatbot 出錯了。我們已經重設您目前的搜尋狀態,以期能自錯誤狀態中復原。請從頭再傳一次您想詢問的訊息。" -#: src/webhook/handlers/askingArticleSubmissionConsent.js:58 +#: src/webhook/handlers/askingArticleSubmissionConsent.js:74 #, javascript-format msgid "Your submission is now recorded at ${ articleUrl }" msgstr "您回報的訊息已經被收錄至 ${ articleUrl }" @@ -217,35 +216,35 @@ msgid "Share to friends" msgstr "分享給朋友" #: src/webhook/handlers/askingArticleSource.js:23 -#: src/webhook/handlers/askingArticleSubmissionConsent.js:24 +#: src/webhook/handlers/askingArticleSubmissionConsent.js:25 #: src/webhook/handlers/choosingArticle.js:47 #: src/webhook/handlers/choosingReply.js:127 msgid "Please choose from provided options." msgstr "請從上面的選項中做選擇。" -#: src/webhook/handlers/choosingArticle.js:156 +#: src/webhook/handlers/choosingArticle.js:152 #, javascript-format msgid "${ countOfType.RUMOR } of them say it ❌ contains misinformation." msgstr "${ countOfType.RUMOR } 人認為它 ❌ 含有不實訊息。" -#: src/webhook/handlers/choosingArticle.js:159 +#: src/webhook/handlers/choosingArticle.js:155 msgid "${ countOfType.NOT_RUMOR } of them says it ⭕ contains true information." msgstr "${ countOfType.NOT_RUMOR } 人認為它 ⭕ 含有真實訊息。" -#: src/webhook/handlers/choosingArticle.js:164 +#: src/webhook/handlers/choosingArticle.js:160 #, javascript-format msgid "" "${ countOfType.OPINIONATED } of them says it 💬 contains personal " "perspective." msgstr "${ countOfType.OPINIONATED } 人認為它 💬 含有個人意見。" -#: src/webhook/handlers/choosingArticle.js:169 +#: src/webhook/handlers/choosingArticle.js:165 msgid "${ countOfType.NOT_ARTICLE } of them says it ⚠️️ is out of scope of Cofacts." msgstr "${ countOfType.NOT_ARTICLE } 人認為它 ⚠️️ 不在 Cofacts 的查證範圍。" -#: src/webhook/handlers/choosingArticle.js:255 -#: src/webhook/handlers/initState.js:204 -#: src/webhook/handlers/processImage.js:118 +#: src/webhook/handlers/choosingArticle.js:251 +#: src/webhook/handlers/initState.js:196 +#: src/webhook/handlers/processImage.js:124 #, javascript-format msgid "I choose “${ displayTextWhenChosen }”" msgstr "我要選「${displayTextWhenChosen}」" @@ -270,17 +269,13 @@ msgid "There is ${ otherReplyRequestCount } user also waiting for clarification. msgid_plural "There are ${ otherReplyRequestCount } users also waiting for clarification." msgstr[0] "共有 ${ otherReplyRequestCount } 人跟您一樣渴望看到針對這篇訊息的回應。" -#: src/webhook/handlers/initState.js:287 +#: src/webhook/handlers/initState.js:279 #, javascript-format msgid "" "There are some messages that looks similar to \"${ inputSummary }\" you " "have sent to me." msgstr "資料庫裡有幾篇訊息,跟您傳給我的「${ inputSummary }」有些接近。" -#: src/webhook/handlers/initState.js:316 -msgid "We didn't find anything about this image :(" -msgstr "找不到與此圖片相關的資訊 :(" - #: src/liff/lib.js:80 msgid "Please proceed on your mobile phone." msgstr "請在您的手機上繼續操作。" @@ -466,7 +461,7 @@ msgid "" "for more details." msgstr "有人針對您之前曾看過的訊息,寫了新的查證回應唷!" -#: src/webhook/handlers/initState.js:147 +#: src/webhook/handlers/initState.js:139 msgid "(Words found in the hyperlink)" msgstr "(網址裡找到的字)" @@ -838,13 +833,13 @@ msgstr "此訊息不存在。" msgid "The reply does not exist. Maybe it has been deleted by its author." msgstr "此回應不存在。或許已經被作者刪掉囉。" -#: src/webhook/handlers/askingArticleSubmissionConsent.js:82 -#: src/webhook/handlers/choosingArticle.js:327 +#: src/webhook/handlers/askingArticleSubmissionConsent.js:98 +#: src/webhook/handlers/choosingArticle.js:323 msgid "View reported message" msgstr "檢視回報訊息" #: src/webhook/handlers/askingArticleSource.js:49 -#: src/webhook/handlers/choosingArticle.js:339 +#: src/webhook/handlers/choosingArticle.js:335 #: src/webhook/handlers/utils.js:565 msgid "Provide more detail" msgstr "提供更多情報" @@ -857,8 +852,8 @@ msgstr "您可以提供更多關於此訊息的情報給查證志工,讓好心 msgid "Provide detail" msgstr "提供更多情報" -#: src/webhook/handlers/choosingArticle.js:309 -#: src/webhook/handlers/choosingArticle.js:320 +#: src/webhook/handlers/choosingArticle.js:305 +#: src/webhook/handlers/choosingArticle.js:316 msgid "" "This message has already published on Cofacts, and will soon be " "fact-checked by volunteers.\n" @@ -867,8 +862,8 @@ msgstr "" "此訊息已經被收錄至 Cofacts 有待好心人來查證。\n" "請先不要相信這個訊息唷!" -#: src/webhook/handlers/askingArticleSubmissionConsent.js:92 -#: src/webhook/handlers/choosingArticle.js:336 +#: src/webhook/handlers/askingArticleSubmissionConsent.js:108 +#: src/webhook/handlers/choosingArticle.js:332 msgid "In the meantime, you can:" msgstr "接下來您可以:" @@ -938,15 +933,16 @@ msgid "I see. Don’t trust the message just yet!" msgstr "這樣呀。請先不要相信這個訊息唷!" #: src/webhook/handlers/askingArticleSource.js:160 +#: src/webhook/handlers/processImage.js:236 msgid "Do you want someone to fact-check this message?" msgstr "你要請人查查這則訊息嗎?" -#: src/webhook/handlers/askingArticleSubmissionConsent.js:30 +#: src/webhook/handlers/askingArticleSubmissionConsent.js:31 msgid "The message has not been reported and won’t be fact-checked. Thanks anyway!" msgstr "好的,那就不查囉。還是謝謝你!" -#: src/webhook/handlers/askingArticleSubmissionConsent.js:66 -#: src/webhook/handlers/askingArticleSubmissionConsent.js:76 +#: src/webhook/handlers/askingArticleSubmissionConsent.js:82 +#: src/webhook/handlers/askingArticleSubmissionConsent.js:92 msgid "" "The message has now been recorded at Cofacts for volunteers to fact-check. " "Thank you for submitting!" @@ -959,7 +955,7 @@ msgid "" "looking for. But I would still like to help." msgstr "抱歉沒有找到你想要查詢的「${ inputSummary }」。可是我還是想幫你查證。" -#: src/webhook/handlers/initState.js:324 +#: src/webhook/handlers/initState.js:304 #, javascript-format msgid "" "Unfortunately, I currently don’t recognize “${ inputSummary }”, but I would " @@ -1002,7 +998,7 @@ msgid "No, typed it myself" msgstr "否,我自行打字輸入的" #: src/webhook/handlers/choosingArticle.js:69 -#: src/webhook/handlers/initState.js:326 +#: src/webhook/handlers/initState.js:306 msgid "May I ask you a quick question?" msgstr "想先請教您一個問題:" @@ -1010,16 +1006,21 @@ msgstr "想先請教您一個問題:" msgid "Provide better reply" msgstr "提供更好的回應" -#: src/webhook/handlers/processImage.js:64 +#: src/webhook/handlers/processImage.js:66 #, javascript-format msgid "No.${ imgNumber }" msgstr "第 ${ imgNumber } 張" -#: src/webhook/handlers/processImage.js:199 +#: src/webhook/handlers/processImage.js:205 msgid "There are some messages that looks similar to the one you have sent to me." msgstr "資料庫裡有幾篇訊息,跟您傳給我的有些接近。" -#: src/webhook/handlers/processImage.js:71 +#: src/webhook/handlers/processImage.js:78 msgid "Looks ${ scoreInPercent }% similar" msgid_plural "Looks ${ scoreInPercent }% similar" -msgstr[0] "看起來 ${ scoreInPercent }% 像" \ No newline at end of file +msgstr[0] "看起來 ${ scoreInPercent }% 像" + +#: src/webhook/handlers/processImage.js:234 +#. submit +msgid "I am sorry you cannot find the information you are looking for." +msgstr "抱歉沒有找到你想要查詢的訊息。" \ No newline at end of file diff --git a/src/webhook/handlers/__tests__/__snapshots__/askingArticleSubmissionConsent.test.js.snap b/src/webhook/handlers/__tests__/__snapshots__/askingArticleSubmissionConsent.test.js.snap index a356c424..632806d0 100644 --- a/src/webhook/handlers/__tests__/__snapshots__/askingArticleSubmissionConsent.test.js.snap +++ b/src/webhook/handlers/__tests__/__snapshots__/askingArticleSubmissionConsent.test.js.snap @@ -300,3 +300,160 @@ Object { "userId": "userId", } `; + +exports[`should submit image article if user agrees to submit 1`] = ` +Object { + "data": Object { + "foundArticleIds": Array [], + "messageId": "6530038889933", + "searchedText": "", + "sessionId": 1577923200000, + }, + "event": Object { + "input": "__POSTBACK_YES__", + "type": "postback", + }, + "replies": Array [ + Object { + "altText": "The message has now been recorded at Cofacts for volunteers to fact-check. Thank you for submitting!", + "contents": Object { + "body": Object { + "contents": Array [ + Object { + "text": "The message has now been recorded at Cofacts for volunteers to fact-check. Thank you for submitting!", + "type": "text", + "wrap": true, + }, + Object { + "action": Object { + "label": "View reported message", + "type": "uri", + "uri": "https://dev.cofacts.tw/article/new-article-id", + }, + "margin": "md", + "type": "button", + }, + ], + "layout": "vertical", + "type": "box", + }, + "type": "bubble", + }, + "type": "flex", + }, + Object { + "altText": "In the meantime, you can:", + "contents": Object { + "body": Object { + "contents": Array [ + Object { + "text": "In the meantime, you can:", + "type": "text", + "wrap": true, + }, + ], + "layout": "vertical", + "type": "box", + }, + "type": "bubble", + }, + "type": "flex", + }, + Object { + "altText": "Your submission is now recorded at https://dev.cofacts.tw/article/new-article-id", + "contents": Object { + "contents": Array [ + Object { + "body": Object { + "contents": Array [ + Object { + "text": "It would help fact checkers a lot if you provide more detail :)", + "type": "text", + "wrap": true, + }, + ], + "layout": "vertical", + "type": "box", + }, + "footer": Object { + "contents": Array [ + Object { + "action": Object { + "label": "Provide detail", + "type": "uri", + "uri": "https://liff.line.me/1563196602-X6mLdDkW?p=comment&articleId=new-article-id", + }, + "color": "#00B172", + "style": "primary", + "type": "button", + }, + ], + "layout": "vertical", + "spacing": "sm", + "type": "box", + }, + "header": Object { + "contents": Array [ + Object { + "color": "#00B172", + "size": "lg", + "text": "Provide more detail", + "type": "text", + }, + ], + "layout": "vertical", + "paddingBottom": "none", + "type": "box", + }, + "type": "bubble", + }, + Object { + "body": Object { + "contents": Array [ + Object { + "text": "We all get by with a little help from our friends 🌟 Share your question to friends, someone might be able to help!", + "type": "text", + "wrap": true, + }, + ], + "layout": "vertical", + "type": "box", + }, + "footer": Object { + "contents": Array [ + Object { + "action": Object { + "label": "Share on LINE", + "type": "uri", + "uri": "line://msg/text/?Please%20help%20me%20verify%20if%20this%20is%20true%3A%20https%3A%2F%2Fdev.cofacts.tw%2Farticle%2Fnew-article-id", + }, + "color": "#ffb600", + "style": "primary", + "type": "button", + }, + Object { + "action": Object { + "label": "Share on Facebook", + "type": "uri", + "uri": "https://www.facebook.com/dialog/share?openExternalBrowser=1&app_id=719656818195367&display=popup&hashtag=%23ReportedToCofacts&href=https%3A%2F%2Fdev.cofacts.tw%2Farticle%2Fnew-article-id", + }, + "color": "#ffb600", + "style": "primary", + "type": "button", + }, + ], + "layout": "vertical", + "spacing": "sm", + "type": "box", + }, + "type": "bubble", + }, + ], + "type": "carousel", + }, + "type": "flex", + }, + ], + "userId": "userId", +} +`; diff --git a/src/webhook/handlers/__tests__/__snapshots__/processImage.test.js.snap b/src/webhook/handlers/__tests__/__snapshots__/processImage.test.js.snap index 8738f202..2930361c 100644 --- a/src/webhook/handlers/__tests__/__snapshots__/processImage.test.js.snap +++ b/src/webhook/handlers/__tests__/__snapshots__/processImage.test.js.snap @@ -132,6 +132,109 @@ Please choose the version that looks the most similar👇", exports[`one identical article found 1`] = `"__HANDLE_POSTBACK_RESULT__"`; +exports[`should handle image not found 1`] = ` +Object { + "context": Object { + "data": Object { + "messageId": "6530038889933", + "searchedText": "", + "sessionId": 1577836800000, + }, + }, + "replies": Array [ + Object { + "altText": "I am sorry you cannot find the information you are looking for. +Do you want someone to fact-check this message?", + "contents": Object { + "body": Object { + "contents": Array [ + Object { + "text": "I am sorry you cannot find the information you are looking for. +Do you want someone to fact-check this message?", + "type": "text", + "wrap": true, + }, + ], + "layout": "vertical", + "type": "box", + }, + "type": "bubble", + }, + "type": "flex", + }, + Object { + "altText": "Be the first to report the message", + "contents": Object { + "body": Object { + "contents": Array [ + Object { + "contents": Array [ + Object { + "text": "Currently we don’t have this message in our database. If you think it is most likely a rumor, ", + "type": "span", + }, + Object { + "color": "#ffb600", + "text": "press “🆕 Report to database” to make this message public on Cofacts database ", + "type": "span", + "weight": "bold", + }, + Object { + "text": "and have volunteers fact-check it. This way you can help the people who receive the same message in the future.", + "type": "span", + }, + ], + "type": "text", + "wrap": true, + }, + ], + "layout": "vertical", + "paddingAll": "lg", + "spacing": "md", + "type": "box", + }, + "footer": Object { + "contents": Array [ + Object { + "action": Object { + "data": "{\\"input\\":\\"__POSTBACK_YES__\\",\\"sessionId\\":1577836800000,\\"state\\":\\"ASKING_ARTICLE_SUBMISSION_CONSENT\\"}", + "displayText": "🆕 Report to database", + "label": "🆕 Report to database", + "type": "postback", + }, + "color": "#ffb600", + "style": "primary", + "type": "button", + }, + Object { + "action": Object { + "data": "{\\"input\\":\\"__POSTBACK_NO__\\",\\"sessionId\\":1577836800000,\\"state\\":\\"ASKING_ARTICLE_SUBMISSION_CONSENT\\"}", + "displayText": "Don’t report", + "label": "Don’t report", + "type": "postback", + }, + "color": "#333333", + "style": "primary", + "type": "button", + }, + ], + "layout": "vertical", + "spacing": "sm", + "type": "box", + }, + "styles": Object { + "body": Object { + "separator": true, + }, + }, + "type": "bubble", + }, + "type": "flex", + }, + ], +} +`; + exports[`twelve articles found 1`] = ` Object { "context": Object { diff --git a/src/webhook/handlers/__tests__/askingArticleSubmissionConsent.test.js b/src/webhook/handlers/__tests__/askingArticleSubmissionConsent.test.js index 6f723225..a3948ab7 100644 --- a/src/webhook/handlers/__tests__/askingArticleSubmissionConsent.test.js +++ b/src/webhook/handlers/__tests__/askingArticleSubmissionConsent.test.js @@ -118,6 +118,44 @@ it('should submit article if user agrees to submit', async () => { expect(ga.sendMock).toHaveBeenCalledTimes(1); }); +it('should submit image article if user agrees to submit', async () => { + const inputSession = new Date('2020-01-01T18:10:18.314Z').getTime(); + const params = { + data: { + searchedText: '', + messageId: '6530038889933', + foundArticleIds: [], + sessionId: inputSession, + }, + event: { + type: 'postback', + input: POSTBACK_YES, + }, + userId: 'userId', + }; + + MockDate.set('2020-01-02'); + gql.__push({ data: { CreateMediaArticle: { id: 'new-article-id' } } }); + const result = await askingArticleSubmissionConsent(params); + MockDate.reset(); + expect(gql.__finished()).toBe(true); + + expect(result).toMatchSnapshot(); + expect(result.data.sessionId).not.toEqual(inputSession); + expect(ga.eventMock.mock.calls).toMatchInlineSnapshot(` + Array [ + Array [ + Object { + "ea": "Create", + "ec": "Article", + "el": "Yes", + }, + ], + ] + `); + expect(ga.sendMock).toHaveBeenCalledTimes(1); +}); + it('should create a UserArticleLink when creating a Article', async () => { const userId = 'user-id-0'; const params = { diff --git a/src/webhook/handlers/__tests__/processImage.test.js b/src/webhook/handlers/__tests__/processImage.test.js index 3fd5d258..f4e1231a 100644 --- a/src/webhook/handlers/__tests__/processImage.test.js +++ b/src/webhook/handlers/__tests__/processImage.test.js @@ -27,6 +27,7 @@ it('one identical article found', async () => { const event = { type: 'message', timestamp: 1497994016356, + messageId: '6270464463537', message: { type: 'image', id: '6270464463537', @@ -75,6 +76,7 @@ it('one article found (not identical)', async () => { const event = { type: 'message', timestamp: 1497994016356, + messageId: '6270464463537', message: { type: 'image', id: '6270464463537', @@ -123,6 +125,7 @@ it('twelve articles found', async () => { const event = { type: 'message', timestamp: 1497994016356, + messageId: '6530038889933', message: { type: 'image', id: '6530038889933', @@ -141,3 +144,43 @@ it('twelve articles found', async () => { expect(carousel.contents.length).toBeLessThanOrEqual(10); // Flex message carousel 10 bubble limit expect(JSON.stringify(carousel).length).toBeLessThan(50 * 1000); // Flex message carousel 50K limit }); + +it('should handle image not found', async () => { + gql.__push(apiResult.notFound); + const data = { + sessionId: 1497994017447, + }; + const event = { + type: 'message', + timestamp: 1497994016356, + messageId: '6530038889933', + message: { + type: 'image', + id: '6530038889933', + }, + }; + const userId = 'Uc76d8ae9ccd1ada4f06c4e1515d46466'; + MockDate.set('2020-01-01'); + expect(await processImage(data, event, userId)).toMatchSnapshot(); + MockDate.reset(); + expect(gql.__finished()).toBe(true); + expect(ga.eventMock.mock.calls).toMatchInlineSnapshot(` + Array [ + Array [ + Object { + "ea": "MessageType", + "ec": "UserInput", + "el": "image", + }, + ], + Array [ + Object { + "ea": "ArticleSearch", + "ec": "UserInput", + "el": "ArticleNotFound", + }, + ], + ] + `); + expect(ga.sendMock).toHaveBeenCalledTimes(1); +}); diff --git a/src/webhook/handlers/askingArticleSubmissionConsent.js b/src/webhook/handlers/askingArticleSubmissionConsent.js index e23221e5..87a75789 100644 --- a/src/webhook/handlers/askingArticleSubmissionConsent.js +++ b/src/webhook/handlers/askingArticleSubmissionConsent.js @@ -10,6 +10,7 @@ import { createCommentBubble, createArticleShareBubble, createNotificationSettingsBubble, + getLineContentProxyURL, } from './utils'; import UserSettings from 'src/database/models/userSettings'; import UserArticleLink from 'src/database/models/userArticleLink'; @@ -35,26 +36,41 @@ export default async function askingArticleSubmissionConsent(params) { case POSTBACK_YES: { visitor.event({ ec: 'Article', ea: 'Create', el: 'Yes' }); - - const { - data: { CreateArticle }, - } = await gql` - mutation($text: String!) { - CreateArticle(text: $text, reference: { type: LINE }) { - id + let article; + if (data.searchedText && !data.messageId) { + const result = await gql` + mutation($text: String!) { + CreateArticle(text: $text, reference: { type: LINE }) { + id + } + } + `({ text: data.searchedText }, { userId }); + article = result.data.CreateArticle; + } else if (data.messageId) { + const proxyUrl = getLineContentProxyURL(data.messageId); + const result = await gql` + mutation($mediaUrl: String!) { + CreateMediaArticle( + mediaUrl: $mediaUrl + articleType: IMAGE + reference: { type: LINE } + ) { + id + } } - } - `({ text: data.searchedText }, { userId }); + `({ mediaUrl: proxyUrl }, { userId }); + article = result.data.CreateMediaArticle; + } await UserArticleLink.createOrUpdateByUserIdAndArticleId( userId, - CreateArticle.id + article.id ); // Create new session, make article submission button expire after submission data.sessionId = Date.now(); - const articleUrl = getArticleURL(CreateArticle.id); + const articleUrl = getArticleURL(article.id); const articleCreatedMsg = t`Your submission is now recorded at ${articleUrl}`; const { allowNewReplyUpdate } = await UserSettings.findOrInsertByUserId( userId @@ -97,7 +113,7 @@ export default async function askingArticleSubmissionConsent(params) { contents: { type: 'carousel', contents: [ - createCommentBubble(CreateArticle.id), + createCommentBubble(article.id), // Ask user to turn on notification if the user did not turn it on // process.env.NOTIFY_METHOD && diff --git a/src/webhook/handlers/processImage.js b/src/webhook/handlers/processImage.js index e81f15c8..fe605d43 100644 --- a/src/webhook/handlers/processImage.js +++ b/src/webhook/handlers/processImage.js @@ -3,6 +3,8 @@ import { getLineContentProxyURL, createPostbackAction, POSTBACK_NO_ARTICLE_FOUND, + createTextMessage, + createAskArticleSubmissionConsentReply, } from './utils'; import gql from 'src/lib/gql'; import ga from 'src/lib/ga'; @@ -230,8 +232,26 @@ export default async function({ data = {} }, event, userId) { replies = prefixTextArticleFound.concat(textArticleFound); } else { + visitor.event({ + ec: 'UserInput', + ea: 'ArticleSearch', + el: 'ArticleNotFound', + }); + + // Store user messageId into context, which will use for submit new image article + data.searchedText = ''; + data.messageId = event.messageId; + // submit - console.log('Image not found, would you like to submit?'); + replies = [ + createTextMessage({ + text: + t`I am sorry you cannot find the information you are looking for.` + + '\n' + + t`Do you want someone to fact-check this message?`, + }), + createAskArticleSubmissionConsentReply(data.sessionId), + ]; } visitor.send(); return { context: { data }, replies };