pass member and room to editor pills to get avatar url

This commit is contained in:
Bruno Windels 2019-05-17 19:48:05 +01:00
parent 0c0052d06e
commit 710338c01f
4 changed files with 27 additions and 13 deletions

View File

@ -40,16 +40,17 @@ export default class MessageEditor extends React.Component {
constructor(props, context) { constructor(props, context) {
super(props, context); super(props, context);
const room = this.context.matrixClient.getRoom(this.props.event.getRoomId());
const partCreator = new PartCreator( const partCreator = new PartCreator(
() => this._autocompleteRef, () => this._autocompleteRef,
query => this.setState({query}), query => this.setState({query}),
room,
); );
this.model = new EditorModel( this.model = new EditorModel(
parseEvent(this.props.event), parseEvent(this.props.event, room),
partCreator, partCreator,
this._updateEditorState, this._updateEditorState,
); );
const room = this.context.matrixClient.getRoom(this.props.event.getRoomId());
this.state = { this.state = {
autoComplete: null, autoComplete: null,
room, room,

View File

@ -17,11 +17,12 @@ limitations under the License.
import {UserPillPart, RoomPillPart, PlainPart} from "./parts"; import {UserPillPart, RoomPillPart, PlainPart} from "./parts";
export default class AutocompleteWrapperModel { export default class AutocompleteWrapperModel {
constructor(updateCallback, getAutocompleterComponent, updateQuery) { constructor(updateCallback, getAutocompleterComponent, updateQuery, room) {
this._updateCallback = updateCallback; this._updateCallback = updateCallback;
this._getAutocompleterComponent = getAutocompleterComponent; this._getAutocompleterComponent = getAutocompleterComponent;
this._updateQuery = updateQuery; this._updateQuery = updateQuery;
this._query = null; this._query = null;
this._room = room;
} }
onEscape(e) { onEscape(e) {
@ -83,11 +84,12 @@ export default class AutocompleteWrapperModel {
case "@": { case "@": {
const displayName = completion.completion; const displayName = completion.completion;
const userId = completion.completionId; const userId = completion.completionId;
return new UserPillPart(userId, displayName); const member = this._room.getMember(userId);
return new UserPillPart(userId, displayName, member);
} }
case "#": { case "#": {
const displayAlias = completion.completionId; const displayAlias = completion.completionId;
return new RoomPillPart(displayAlias); return new RoomPillPart(displayAlias, this._room);
} }
// also used for emoji completion // also used for emoji completion
default: default:

View File

@ -17,7 +17,7 @@ limitations under the License.
import { MATRIXTO_URL_PATTERN } from '../linkify-matrix'; import { MATRIXTO_URL_PATTERN } from '../linkify-matrix';
import { PlainPart, UserPillPart, RoomPillPart, NewlinePart } from "./parts"; import { PlainPart, UserPillPart, RoomPillPart, NewlinePart } from "./parts";
function parseHtmlMessage(html) { function parseHtmlMessage(html, room) {
const REGEX_MATRIXTO = new RegExp(MATRIXTO_URL_PATTERN); const REGEX_MATRIXTO = new RegExp(MATRIXTO_URL_PATTERN);
// no nodes from parsing here should be inserted in the document, // no nodes from parsing here should be inserted in the document,
// as scripts in event handlers, etc would be executed then. // as scripts in event handlers, etc would be executed then.
@ -37,8 +37,8 @@ function parseHtmlMessage(html) {
const resourceId = pillMatch[1]; // The room/user ID const resourceId = pillMatch[1]; // The room/user ID
const prefix = pillMatch[2]; // The first character of prefix const prefix = pillMatch[2]; // The first character of prefix
switch (prefix) { switch (prefix) {
case "@": return new UserPillPart(resourceId, n.textContent); case "@": return new UserPillPart(resourceId, n.textContent, room.getMember(resourceId));
case "#": return new RoomPillPart(resourceId, n.textContent); case "#": return new RoomPillPart(resourceId, n.textContent, room);
default: return new PlainPart(n.textContent); default: return new PlainPart(n.textContent);
} }
} }
@ -54,10 +54,10 @@ function parseHtmlMessage(html) {
return parts; return parts;
} }
export function parseEvent(event) { export function parseEvent(event, room) {
const content = event.getContent(); const content = event.getContent();
if (content.format === "org.matrix.custom.html") { if (content.format === "org.matrix.custom.html") {
return parseHtmlMessage(content.formatted_body || ""); return parseHtmlMessage(content.formatted_body || "", room);
} else { } else {
const body = content.body || ""; const body = content.body || "";
const lines = body.split("\n"); const lines = body.split("\n");

View File

@ -216,8 +216,9 @@ export class NewlinePart extends BasePart {
} }
export class RoomPillPart extends PillPart { export class RoomPillPart extends PillPart {
constructor(displayAlias) { constructor(displayAlias, room) {
super(displayAlias, displayAlias); super(displayAlias, displayAlias);
this._room = room;
} }
get type() { get type() {
@ -226,6 +227,11 @@ export class RoomPillPart extends PillPart {
} }
export class UserPillPart extends PillPart { export class UserPillPart extends PillPart {
constructor(userId, displayName, member) {
super(userId, displayName);
this._member = member;
}
get type() { get type() {
return "user-pill"; return "user-pill";
} }
@ -256,9 +262,14 @@ export class PillCandidatePart extends PlainPart {
} }
export class PartCreator { export class PartCreator {
constructor(getAutocompleterComponent, updateQuery) { constructor(getAutocompleterComponent, updateQuery, room) {
this._autoCompleteCreator = (updateCallback) => { this._autoCompleteCreator = (updateCallback) => {
return new AutocompleteWrapperModel(updateCallback, getAutocompleterComponent, updateQuery); return new AutocompleteWrapperModel(
updateCallback,
getAutocompleterComponent,
updateQuery,
room,
);
}; };
} }