Find and Pass mentioned channel name + claim id on comment_create (#690)

This commit is contained in:
saltrafael 2022-01-14 11:50:09 -03:00 committed by GitHub
parent 0d6cd21457
commit f3892325ce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -5,8 +5,8 @@ import * as PAGES from 'constants/pages';
import { SORT_BY, BLOCK_LEVEL } from 'constants/comment'; import { SORT_BY, BLOCK_LEVEL } from 'constants/comment';
import Lbry from 'lbry'; import Lbry from 'lbry';
import { parseURI, buildURI, isURIEqual } from 'util/lbryURI'; import { parseURI, buildURI, isURIEqual } from 'util/lbryURI';
import { selectClaimsByUri, selectMyChannelClaims } from 'redux/selectors/claims'; import { selectClaimForUri, selectClaimsByUri, selectMyChannelClaims } from 'redux/selectors/claims';
import { doClaimSearch } from 'redux/actions/claims'; import { doResolveUri, doClaimSearch } from 'redux/actions/claims';
import { doToast, doSeeNotifications } from 'redux/actions/notifications'; import { doToast, doSeeNotifications } from 'redux/actions/notifications';
import { import {
selectMyReactsForComment, selectMyReactsForComment,
@ -26,6 +26,13 @@ const isDev = process.env.NODE_ENV !== 'production';
const FETCH_API_FAILED_TO_FETCH = 'Failed to fetch'; const FETCH_API_FAILED_TO_FETCH = 'Failed to fetch';
const PROMISE_FULFILLED = 'fulfilled'; const PROMISE_FULFILLED = 'fulfilled';
const MENTION_REGEX = /(?:^| |\n)@[^\s=&#$@%?:;/"<>%{}|^~[]*(?::[\w]+)?/gm;
declare type MentionedChannel = {
channelName: string,
channelID: string,
};
declare type CommentronErrorMap = { declare type CommentronErrorMap = {
[string]: { [string]: {
commentron: string | RegExp, commentron: string | RegExp,
@ -573,12 +580,35 @@ export function doCommentCreate(
return async (dispatch: Dispatch, getState: GetState) => { return async (dispatch: Dispatch, getState: GetState) => {
const state = getState(); const state = getState();
const activeChannelClaim = selectActiveChannelClaim(state); const activeChannelClaim = selectActiveChannelClaim(state);
let mentionedChannels: Array<MentionedChannel> = [];
if (!activeChannelClaim) { if (!activeChannelClaim) {
console.error('Unable to create comment. No activeChannel is set.'); // eslint-disable-line console.error('Unable to create comment. No activeChannel is set.'); // eslint-disable-line
return; return;
} }
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/matchAll
// $FlowFixMe
const mentionMatches = [...comment.matchAll(MENTION_REGEX)];
if (mentionMatches.length > 0) {
mentionMatches.forEach((match) => {
const matchTerm = match[0];
const mention = matchTerm.substring(matchTerm.indexOf('@'));
const mentionUri = `lbry://${mention}`;
const claim = selectClaimForUri(state, mentionUri);
if (claim) {
mentionedChannels.push({ channelName: claim.name, channelID: claim.claim_id });
} else if (claim === undefined) {
dispatch(doResolveUri(mentionUri))
.then((response) => mentionedChannels.push({ channelName: response.name, channelID: response.claim_id }))
.catch((e) => {});
}
});
}
dispatch({ type: ACTIONS.COMMENT_CREATE_STARTED }); dispatch({ type: ACTIONS.COMMENT_CREATE_STARTED });
let signatureData; let signatureData;
@ -607,6 +637,7 @@ export function doCommentCreate(
signature: signatureData.signature, signature: signatureData.signature,
signing_ts: signatureData.signing_ts, signing_ts: signatureData.signing_ts,
sticker: sticker, sticker: sticker,
mentioned_channels: mentionedChannels,
...(txid ? { support_tx_id: txid } : {}), ...(txid ? { support_tx_id: txid } : {}),
...(payment_intent_id ? { payment_intent_id } : {}), ...(payment_intent_id ? { payment_intent_id } : {}),
...(environment ? { environment } : {}), ...(environment ? { environment } : {}),