Fixes mentions in message body and resolve them quicker

Relates #1815
pull/1804/head
Audric Ackermann 4 years ago
parent 360cb52680
commit 6e8d25e530
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4

@ -1,12 +1,12 @@
import React from 'react';
import React, { useEffect } from 'react';
import { RenderTextCallbackType } from '../../types/Util';
import classNames from 'classnames';
import { FindMember } from '../../util/findMember';
import { useInterval } from '../../hooks/useInterval';
import { PubKey } from '../../session/types';
import { ConversationModel } from '../../models/conversation';
import { UserUtils } from '../../session/utils';
import { getConversationController } from '../../session/conversations';
import _ from 'lodash';
interface MentionProps {
key: string;
@ -15,29 +15,17 @@ interface MentionProps {
}
const Mention = (props: MentionProps) => {
const [found, setFound] = React.useState<ConversationModel | null>(null);
const [us, setUs] = React.useState(false);
const tryRenameMention = async () => {
if (!found) {
const foundMember = await FindMember.findMember(props.text.slice(1), props.convoId);
if (foundMember) {
const itsUs = UserUtils.isUsFromCache(foundMember.id);
setUs(itsUs);
setFound(foundMember);
// FIXME stop this interval once we found it.
}
}
};
useInterval(() => void tryRenameMention(), 10000);
const foundConvo = getConversationController().get(props.text.slice(1));
let us = false;
if (foundConvo) {
us = UserUtils.isUsFromCache(foundConvo.id);
}
if (found) {
if (foundConvo) {
// TODO: We don't have to search the database of message just to know that the message is for us!
const className = classNames('mention-profile-name', us && 'mention-profile-name-us');
const displayedName = found.getContactProfileNameOrShortenedPubKey();
const displayedName = foundConvo.getContactProfileNameOrShortenedPubKey();
return <span className={className}>{displayedName}</span>;
} else {
return <span className="mention-profile-name">{PubKey.shorten(props.text)}</span>;
@ -73,6 +61,7 @@ export class AddMentions extends React.Component<Props> {
if (!match) {
return renderOther({ text, key: 0 });
}
while (match) {
count++;
const key = count;

@ -901,7 +901,6 @@ export async function lokiOnionFetch(
return retriedResult;
} catch (e) {
window?.log?.warn('onionFetchRetryable failed ', e.message);
// console.warn('error to show to user');
if (e?.errno === 'ENETUNREACH') {
// better handle the no connection state
throw new Error(ERROR_CODE_NO_CONNECT);

@ -1,55 +0,0 @@
import { ConversationModel } from '../models/conversation';
import { getConversationController } from '../session/conversations';
import { MentionsMembersType } from '../state/ducks/conversations';
// tslint:disable: no-unnecessary-class
export class FindMember {
public static async findMember(
pubkey: String,
convoId: string,
clearOurInterval?: any
): Promise<ConversationModel | null> {
let groupMembers;
const groupConvos = getConversationController()
.getConversations()
.filter((d: any) => {
return !d.isPrivate();
});
const thisConvo = groupConvos.find((d: any) => {
return d.id === convoId;
});
if (!thisConvo) {
// If this gets triggered, is is likely because we deleted the conversation
if (clearOurInterval) {
clearOurInterval();
}
return null;
}
if (thisConvo.isPublic()) {
const publicMembers = (await window.inboxStore?.getState()
.mentionsInput) as MentionsMembersType;
const memberConversations = (publicMembers || [])
.map(publicMember => getConversationController().get(publicMember.authorPhoneNumber))
.filter((c: any) => !!c);
groupMembers = memberConversations;
} else {
const privateConvos = getConversationController()
.getConversations()
.filter((d: any) => d.isPrivate());
const members = thisConvo.attributes.members;
if (!members) {
return null;
}
const memberConversations = members
.map((m: any) => privateConvos.find((c: any) => c.id === m))
.filter((c: any) => !!c);
groupMembers = memberConversations;
}
return groupMembers.find(({ id: pn }: any) => pn && pn === pubkey);
}
}
Loading…
Cancel
Save