You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-11-29 16:43:09 +03:00
fix filtering
This commit is contained in:
@@ -51,21 +51,15 @@ function FilterComponent(filter_json) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks with the filter component matches the given event
|
* Checks with the filter component matches the given event
|
||||||
|
*
|
||||||
|
* Takes a MatrixEvent object
|
||||||
*/
|
*/
|
||||||
FilterComponent.prototype.check = function(event) {
|
FilterComponent.prototype.check = function(event) {
|
||||||
var sender = event.sender;
|
|
||||||
if (!sender) {
|
|
||||||
// Presence events have their 'sender' in content.user_id
|
|
||||||
if (event.content) {
|
|
||||||
sender = event.content.user_id;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.checkFields(
|
return this.checkFields(
|
||||||
event.room_id,
|
event.getRoomId(),
|
||||||
sender,
|
event.getSender(),
|
||||||
event.type,
|
event.getType(),
|
||||||
event.content ? event.content.url !== undefined : false
|
event.getContent() ? event.getContent().url !== undefined : false
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -81,15 +75,16 @@ FilterComponent.prototype.checkFields =
|
|||||||
"types": function(v) { return _matches_wildcard(event_type, v); },
|
"types": function(v) { return _matches_wildcard(event_type, v); },
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var self = this;
|
||||||
Object.keys(literal_keys).forEach(function(name) {
|
Object.keys(literal_keys).forEach(function(name) {
|
||||||
var match_func = literal_keys[name];
|
var match_func = literal_keys[name];
|
||||||
var not_name = "not_" + name;
|
var not_name = "not_" + name;
|
||||||
var disallowed_values = this[not_name];
|
var disallowed_values = self[not_name];
|
||||||
if (disallowed_values.map(match_func)) {
|
if (disallowed_values.map(match_func)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
var allowed_values = this[name];
|
var allowed_values = self[name];
|
||||||
if (allowed_values) {
|
if (allowed_values) {
|
||||||
if (!allowed_values.map(match_func)) {
|
if (!allowed_values.map(match_func)) {
|
||||||
return false;
|
return false;
|
||||||
@@ -108,7 +103,7 @@ FilterComponent.prototype.checkFields =
|
|||||||
};
|
};
|
||||||
|
|
||||||
FilterComponent.prototype.filter = function(events) {
|
FilterComponent.prototype.filter = function(events) {
|
||||||
return events.filter(this.check);
|
return events.filter(this.check, this);
|
||||||
};
|
};
|
||||||
|
|
||||||
FilterComponent.prototype.limit = function() {
|
FilterComponent.prototype.limit = function() {
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ utils.inherits(EventTimelineSet, EventEmitter);
|
|||||||
/**
|
/**
|
||||||
* Get the filter object this timeline list is filtered on
|
* Get the filter object this timeline list is filtered on
|
||||||
*/
|
*/
|
||||||
EventTimeline.prototype.getFilter = function() {
|
EventTimelineSet.prototype.getFilter = function() {
|
||||||
return this._filter;
|
return this._filter;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -79,10 +79,30 @@ EventTimeline.prototype.getFilter = function() {
|
|||||||
* Set the filter object this timeline list is filtered on
|
* Set the filter object this timeline list is filtered on
|
||||||
* (passed to the server when paginating via /messages).
|
* (passed to the server when paginating via /messages).
|
||||||
*/
|
*/
|
||||||
EventTimeline.prototype.setFilter = function(filter) {
|
EventTimelineSet.prototype.setFilter = function(filter) {
|
||||||
this._filter = filter;
|
this._filter = filter;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the list of pending sent events for this timelineSet's room, filtered
|
||||||
|
* by the timelineSet's filter if appropriate.
|
||||||
|
*
|
||||||
|
* @return {module:models/event.MatrixEvent[]} A list of the sent events
|
||||||
|
* waiting for remote echo.
|
||||||
|
*
|
||||||
|
* @throws If <code>opts.pendingEventOrdering</code> was not 'detached'
|
||||||
|
*/
|
||||||
|
EventTimelineSet.prototype.getPendingEvents = function() {
|
||||||
|
if (!this.room) return [];
|
||||||
|
|
||||||
|
if (this._filter) {
|
||||||
|
return this._filter.filterRoomTimeline(this.room.getPendingEvents());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return this.room.getPendingEvents();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the live timeline for this room.
|
* Get the live timeline for this room.
|
||||||
*
|
*
|
||||||
@@ -224,7 +244,7 @@ EventTimelineSet.prototype.addEventsToTimeline = function(events, toStartOfTimel
|
|||||||
|
|
||||||
if (this._filter) {
|
if (this._filter) {
|
||||||
events = this._filter.filterRoomTimeline(events);
|
events = this._filter.filterRoomTimeline(events);
|
||||||
if (!events) {
|
if (!events.length) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -372,7 +392,7 @@ EventTimelineSet.prototype.addEventsToTimeline = function(events, toStartOfTimel
|
|||||||
EventTimelineSet.prototype.addLiveEvent = function(event, duplicateStrategy) {
|
EventTimelineSet.prototype.addLiveEvent = function(event, duplicateStrategy) {
|
||||||
if (this._filter) {
|
if (this._filter) {
|
||||||
var events = this._filter.filterRoomTimeline([event]);
|
var events = this._filter.filterRoomTimeline([event]);
|
||||||
if (!events) {
|
if (!events.length) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -443,7 +463,14 @@ EventTimelineSet.prototype.replaceOrAddEvent = function(localEvent, oldEventId,
|
|||||||
delete this._eventIdToTimeline[oldEventId];
|
delete this._eventIdToTimeline[oldEventId];
|
||||||
this._eventIdToTimeline[newEventId] = existingTimeline;
|
this._eventIdToTimeline[newEventId] = existingTimeline;
|
||||||
} else {
|
} else {
|
||||||
this.addEventToTimeline(localEvent, this._liveTimeline, false);
|
if (this._filter) {
|
||||||
|
if (this._filter.filterRoomTimeline([localEvent]).length) {
|
||||||
|
this.addEventToTimeline(localEvent, this._liveTimeline, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.addEventToTimeline(localEvent, this._liveTimeline, false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -464,11 +464,8 @@ Room.prototype.getOrCreateFilteredTimelineSet = function(filter) {
|
|||||||
if (this._filteredTimelineSets[filter.filterId]) {
|
if (this._filteredTimelineSets[filter.filterId]) {
|
||||||
return this._filteredTimelineSets[filter.filterId];
|
return this._filteredTimelineSets[filter.filterId];
|
||||||
}
|
}
|
||||||
var timelineSet = new EventTimelineSet(
|
var opts = Object.assign({ filter: filter }, this._opts);
|
||||||
this.roomId, this, {
|
var timelineSet = new EventTimelineSet(this.roomId, this, opts);
|
||||||
filter: filter,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
reEmit(this, timelineSet, [ "Room.timeline" ]);
|
reEmit(this, timelineSet, [ "Room.timeline" ]);
|
||||||
this._filteredTimelineSets[filter.filterId] = timelineSet;
|
this._filteredTimelineSets[filter.filterId] = timelineSet;
|
||||||
this._timelineSets.push(timelineSet);
|
this._timelineSets.push(timelineSet);
|
||||||
@@ -607,7 +604,15 @@ Room.prototype.addPendingEvent = function(event, txnId) {
|
|||||||
this._pendingEventList.push(event);
|
this._pendingEventList.push(event);
|
||||||
} else {
|
} else {
|
||||||
for (var i = 0; i < this._timelineSets.length; i++) {
|
for (var i = 0; i < this._timelineSets.length; i++) {
|
||||||
this._timelineSets[i].addEventToTimeline(event, this._timelineSets[i].getLiveTimeline(), false);
|
var timelineSet = this._timelineSets[i];
|
||||||
|
if (timelineSet.getFilter()) {
|
||||||
|
if (this._filter.filterRoomTimeline([event]).length) {
|
||||||
|
timelineSet.addEventToTimeline(event, timelineSet.getLiveTimeline(), false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
timelineSet.addEventToTimeline(event, timelineSet.getLiveTimeline(), false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// notifications are receive-only, so we don't need to worry about this._notifTimelineSet.
|
// notifications are receive-only, so we don't need to worry about this._notifTimelineSet.
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user