Skip to content

Commit

Permalink
feat(webhook): call CreateOrUpdateCooccurrence to create cooccurrences
Browse files Browse the repository at this point in the history
  • Loading branch information
MrOrz committed Jan 3, 2024
1 parent cee0edc commit 8400d41
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
8 changes: 5 additions & 3 deletions src/webhook/handlers/askingArticleSubmissionConsent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
searchText,
searchMedia,
createCooccurredSearchResultsCarouselContents,
setMostSimilarArticlesAsCooccurrence,
} from './utils';

// Input should be array of context.msgs idx. Empty if the user does not want to submit.
Expand Down Expand Up @@ -152,16 +153,17 @@ const askingArticleSubmissionConsent: ChatbotPostbackHandler = async ({
);

if (context.msgs.length > 1) {
// Continue with the rest of the messages
// FIXME: implement this

// Search again, this time all messages should be in the database.
// Most similar articles for each respective searched message will be set as cooccurrence.
//
const searchResults = await Promise.all(
context.msgs.map(async (msg) =>
msg.type === 'text'
? searchText(msg.text)
: searchMedia(getLineContentProxyURL(msg.id), userId)
)
);
await setMostSimilarArticlesAsCooccurrence(searchResults, userId);

return {
context,
Expand Down
15 changes: 4 additions & 11 deletions src/webhook/handlers/askingCooccurrence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { msgid, ngettext, t } from 'ttag';
import { FlexSpan } from '@line/bot-sdk';

import ga from 'src/lib/ga';
import gql from 'src/lib/gql';
import { ChatbotPostbackHandler } from 'src/types/chatbotState';

import {
Expand All @@ -16,6 +15,7 @@ import {
getLineContentProxyURL,
createPostbackAction,
createCooccurredSearchResultsCarouselContents,
setMostSimilarArticlesAsCooccurrence,
} from './utils';

const inputSchema = z.enum([POSTBACK_NO, POSTBACK_YES]);
Expand All @@ -26,16 +26,6 @@ const IN_DB_THRESHOLD = 0.8;
/** Postback input type for ASKING_ARTICLE_SOURCE state handler */
export type Input = z.infer<typeof inputSchema>;

export function setCooccurrences(articleIds: string[]) {
return gql`
mutation SetCooccurrences($articleIds: [String!]!) {
CreateOrUpdateCooccurrence(articleIds: $articleIds) {
id
}
}
`({ articleIds });
}

const askingCooccurence: ChatbotPostbackHandler = async ({
context,
postbackData: { state, input: postbackInput },
Expand Down Expand Up @@ -178,6 +168,9 @@ const askingCooccurence: ChatbotPostbackHandler = async ({
};
}

// All messages in DB and thus can be set as cooccurrence.
await setMostSimilarArticlesAsCooccurrence(searchResults, userId);

// Get first few search results for each message, and make at most 10 options
//

Expand Down
23 changes: 23 additions & 0 deletions src/webhook/handlers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1275,3 +1275,26 @@ export function createCooccurredSearchResultsCarouselContents(
)
);
}

/**
* Mark the most similar item in the search of each searched messages as a cooccurrence
*
* @param searchResults - search results from searchMedia() or searchText(), with most similar item
* of each searched items in the first edge.
* @param userId - user that observes this cooccurrence
*/
export function setMostSimilarArticlesAsCooccurrence(
searchResults: (SearchMediaResult | SearchTextResult)[],
userId: string
) {
const articleIds = searchResults.map(
(searchResult) => searchResult.edges[0].node.id
);
return gql`
mutation SetCooccurrences($articleIds: [String!]!) {
CreateOrUpdateCooccurrence(articleIds: $articleIds) {
id
}
}
`({ articleIds }, { userId });
}

0 comments on commit 8400d41

Please sign in to comment.