Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a filter for URLs in messages #2886

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/filter-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export interface IFilterComponent {
senders?: string[];
not_senders?: string[];
contains_url?: boolean;
message_with_url?: boolean;
limit?: number;
related_by_senders?: Array<RelationType | string>;
related_by_rel_types?: string[];
Expand Down Expand Up @@ -93,11 +94,16 @@ export class FilterComponent {
relationSenders.push(this.userId);
}

// check if a message contains an url by way of regex
const urlCheck = new RegExp("([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_]+@)?"
+ "([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?(/.*)?");

return this.checkFields(
event.getRoomId(),
event.getSender(),
event.getType(),
event.getContent() ? event.getContent().url !== undefined : false,
event.getContent() ? urlCheck.test(event.getContent().body) : false,
relations,
relationSenders,
);
Expand All @@ -115,6 +121,7 @@ export class FilterComponent {
"senders": this.filterJson.senders || null,
"not_senders": this.filterJson.not_senders || [],
"contains_url": this.filterJson.contains_url || null,
"message_with_url": this.filterJson.message_with_url || null,
[FILTER_RELATED_BY_SENDERS.name]: this.filterJson[FILTER_RELATED_BY_SENDERS.name] || [],
[FILTER_RELATED_BY_REL_TYPES.name]: this.filterJson[FILTER_RELATED_BY_REL_TYPES.name] || [],
};
Expand All @@ -126,6 +133,7 @@ export class FilterComponent {
* @param {String} sender the sender of the event being checked
* @param {String} eventType the type of the event being checked
* @param {boolean} containsUrl whether the event contains a content.url field
* @param {boolean} messageWithUrl whether the event contains a content.url field
* @param {boolean} relationTypes whether has aggregated relation of the given type
* @param {boolean} relationSenders whether one of the relation is sent by the user listed
* @return {boolean} true if the event fields match the filter
Expand All @@ -135,6 +143,7 @@ export class FilterComponent {
sender: string,
eventType: string,
containsUrl: boolean,
messageWithUrl: boolean,
relationTypes: Array<RelationType | string>,
relationSenders: string[],
): boolean {
Expand Down Expand Up @@ -170,6 +179,11 @@ export class FilterComponent {
return false;
}

const messageWithUrlFilter = this.filterJson.message_with_url;
if (messageWithUrlFilter !== undefined && messageWithUrlFilter !== messageWithUrl) {
return false;
}

const relationTypesFilter = this.filterJson[FILTER_RELATED_BY_REL_TYPES.name];
if (relationTypesFilter !== undefined) {
if (!this.arrayMatchesFilter(relationTypesFilter, relationTypes)) {
Expand Down