Skip to content
Merged
Show file tree
Hide file tree
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
22 changes: 11 additions & 11 deletions app/api/server/v1/channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,17 +387,17 @@ API.v1.addRoute('channels.history', { authRequired: true }, {

const unreads = this.queryParams.unreads || false;

let result;
Meteor.runAsUser(this.userId, () => {
result = Meteor.call('getChannelHistory', {
rid: findResult._id,
latest: latestDate,
oldest: oldestDate,
inclusive,
offset,
count,
unreads,
});
const showThreadMessages = this.queryParams.showThreadMessages !== 'false';

const result = Meteor.call('getChannelHistory', {
rid: findResult._id,
latest: latestDate,
oldest: oldestDate,
inclusive,
offset,
count,
unreads,
showThreadMessages,
});

if (!result) {
Expand Down
14 changes: 11 additions & 3 deletions app/api/server/v1/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,17 @@ API.v1.addRoute('groups.history', { authRequired: true }, {

const unreads = this.queryParams.unreads || false;

let result;
Meteor.runAsUser(this.userId, () => {
result = Meteor.call('getChannelHistory', { rid: findResult.rid, latest: latestDate, oldest: oldestDate, inclusive, offset, count, unreads });
const showThreadMessages = this.queryParams.showThreadMessages !== 'false';

const result = Meteor.call('getChannelHistory', {
rid: findResult.rid,
latest: latestDate,
oldest: oldestDate,
inclusive,
offset,
count,
unreads,
showThreadMessages,
});

if (!result) {
Expand Down
22 changes: 11 additions & 11 deletions app/api/server/v1/im.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,17 +176,17 @@ API.v1.addRoute(['dm.history', 'im.history'], { authRequired: true }, {

const unreads = this.queryParams.unreads || false;

let result;
Meteor.runAsUser(this.userId, () => {
result = Meteor.call('getChannelHistory', {
rid: findResult.room._id,
latest: latestDate,
oldest: oldestDate,
inclusive,
offset,
count,
unreads,
});
const showThreadMessages = this.queryParams.showThreadMessages !== 'false';

const result = Meteor.call('getChannelHistory', {
rid: findResult.room._id,
latest: latestDate,
oldest: oldestDate,
inclusive,
offset,
count,
unreads,
showThreadMessages,
});

if (!result) {
Expand Down
18 changes: 6 additions & 12 deletions app/lib/server/functions/loadMessageHistory.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import { settings } from '../../../settings';
import { Messages, Rooms } from '../../../models';
import { settings } from '../../../settings/server';
import { Messages, Rooms } from '../../../models/server';
import { normalizeMessagesForUser } from '../../../utils/server/lib/normalizeMessagesForUser';

const hideMessagesOfTypeServer = new Set();

settings.get('Hide_System_Messages', function(key, values) {
const hiddenTypes = values.reduce((array, value) => [...array, ...value === 'mute_unmute' ? ['user-muted', 'user-unmuted'] : [value]], []);
hideMessagesOfTypeServer.clear();
hiddenTypes.forEach((item) => hideMessagesOfTypeServer.add(item));
});
import { getHiddenSystemMessages } from '../lib/getHiddenSystemMessages';

export const loadMessageHistory = function loadMessageHistory({ userId, rid, end, limit = 20, ls, showThreadMessages = true }) {
const room = Rooms.findOne(rid, { fields: { sysMes: 1 } });
const room = Rooms.findOneById(rid, { fields: { sysMes: 1 } });

const hiddenMessageTypes = getHiddenSystemMessages(room);

const hiddenMessageTypes = Array.isArray(room && room.sysMes) ? room.sysMes : Array.from(hideMessagesOfTypeServer.values()); // TODO probably remove on chained event system
const options = {
sort: {
ts: -1,
Expand Down
23 changes: 23 additions & 0 deletions app/lib/server/lib/getHiddenSystemMessages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { settings } from '../../../settings/server';
import { IRoom } from '../../../../definition/IRoom';

const hideMessagesOfTypeServer = new Set<string>();

settings.get('Hide_System_Messages', function(_key, values) {
if (!values || !Array.isArray(values)) {
return;
}

const hiddenTypes = values.reduce((array: string[], value: string) => [...array, ...value === 'mute_unmute' ? ['user-muted', 'user-unmuted'] : [value]], []);

hideMessagesOfTypeServer.clear();

hiddenTypes.forEach((item) => hideMessagesOfTypeServer.add(item));
});

// TODO probably remove on chained event system
export function getHiddenSystemMessages(room: IRoom): string[] {
return Array.isArray(room?.sysMes)
? room.sysMes
: [...hideMessagesOfTypeServer];
}
39 changes: 24 additions & 15 deletions app/lib/server/methods/getChannelHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';
import _ from 'underscore';

import { hasPermission } from '../../../authorization';
import { Subscriptions, Messages } from '../../../models';
import { settings } from '../../../settings';
import { hasPermission } from '../../../authorization/server';
import { Subscriptions, Messages } from '../../../models/server';
import { settings } from '../../../settings/server';
import { normalizeMessagesForUser } from '../../../utils/server/lib/normalizeMessagesForUser';
import { getHiddenSystemMessages } from '../lib/getHiddenSystemMessages';

Meteor.methods({
getChannelHistory({ rid, latest, oldest, inclusive, offset = 0, count = 20, unreads }) {
getChannelHistory({ rid, latest, oldest, inclusive, offset = 0, count = 20, unreads, showThreadMessages = true }) {
check(rid, String);

if (!Meteor.userId()) {
Expand Down Expand Up @@ -36,6 +37,8 @@ Meteor.methods({
throw new Meteor.Error('error-invalid-date', 'Invalid date', { method: 'getChannelHistory' });
}

const hiddenMessageTypes = getHiddenSystemMessages(room);

const options = {
sort: {
ts: -1,
Expand All @@ -48,16 +51,9 @@ Meteor.methods({
options.fields = { editedAt: 0 };
}

let records = [];
if (_.isUndefined(oldest) && inclusive) {
records = Messages.findVisibleByRoomIdBeforeTimestampInclusive(rid, latest, options).fetch();
} else if (_.isUndefined(oldest) && !inclusive) {
records = Messages.findVisibleByRoomIdBeforeTimestamp(rid, latest, options).fetch();
} else if (!_.isUndefined(oldest) && inclusive) {
records = Messages.findVisibleByRoomIdBetweenTimestampsInclusive(rid, oldest, latest, options).fetch();
} else {
records = Messages.findVisibleByRoomIdBetweenTimestamps(rid, oldest, latest, options).fetch();
}
const records = _.isUndefined(oldest)
? Messages.findVisibleByRoomIdBeforeTimestampNotContainingTypes(rid, latest, hiddenMessageTypes, options, showThreadMessages, inclusive).fetch()
: Messages.findVisibleByRoomIdBetweenTimestampsNotContainingTypes(rid, oldest, latest, options, showThreadMessages, inclusive).fetch();

const messages = normalizeMessagesForUser(records, fromUserId);

Expand All @@ -68,7 +64,20 @@ Meteor.methods({
if (!_.isUndefined(oldest)) {
const firstMsg = messages[messages.length - 1];
if (!_.isUndefined(firstMsg) && firstMsg.ts > oldest) {
const unreadMessages = Messages.findVisibleByRoomIdBetweenTimestamps(rid, oldest, firstMsg.ts, { limit: 1, sort: { ts: 1 } });
const unreadMessages = Messages.findVisibleByRoomIdBetweenTimestampsNotContainingTypes(
rid,
oldest,
firstMsg.ts,
hiddenMessageTypes,
{
limit: 1,
sort: {
ts: 1,
},
},
showThreadMessages,
);

firstUnread = unreadMessages.fetch()[0];
unreadNotLoaded = unreadMessages.count();
}
Expand Down
54 changes: 5 additions & 49 deletions app/models/server/models/Messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,58 +323,14 @@ export class Messages extends Base {
return this.find(query, options);
}

findVisibleByRoomIdBeforeTimestampInclusive(roomId, timestamp, options) {
findVisibleByRoomIdBeforeTimestampNotContainingTypes(roomId, timestamp, types, options, showThreadMessages = true, inclusive = false) {
const query = {
_hidden: {
$ne: true,
},
rid: roomId,
ts: {
$lte: timestamp,
},
};

return this.find(query, options);
}

findVisibleByRoomIdBetweenTimestamps(roomId, afterTimestamp, beforeTimestamp, options) {
const query = {
_hidden: {
$ne: true,
},
rid: roomId,
ts: {
$gt: afterTimestamp,
$lt: beforeTimestamp,
},
};

return this.find(query, options);
}

findVisibleByRoomIdBetweenTimestampsInclusive(roomId, afterTimestamp, beforeTimestamp, options) {
const query = {
_hidden: {
$ne: true,
},
rid: roomId,
ts: {
$gte: afterTimestamp,
$lte: beforeTimestamp,
},
};

return this.find(query, options);
}

findVisibleByRoomIdBeforeTimestampNotContainingTypes(roomId, timestamp, types, options, showThreadMessages = true) {
const query = {
_hidden: {
$ne: true,
},
rid: roomId,
ts: {
$lt: timestamp,
[inclusive ? '$lte' : '$lt']: timestamp,
},
...!showThreadMessages && {
$or: [{
Expand All @@ -392,15 +348,15 @@ export class Messages extends Base {
return this.find(query, options);
}

findVisibleByRoomIdBetweenTimestampsNotContainingTypes(roomId, afterTimestamp, beforeTimestamp, types, options, showThreadMessages = true) {
findVisibleByRoomIdBetweenTimestampsNotContainingTypes(roomId, afterTimestamp, beforeTimestamp, types, options, showThreadMessages = true, inclusive = false) {
const query = {
_hidden: {
$ne: true,
},
rid: roomId,
ts: {
$gt: afterTimestamp,
$lt: beforeTimestamp,
[inclusive ? '$gte' : '$gt']: afterTimestamp,
[inclusive ? '$lte' : '$lt']: beforeTimestamp,
},
...!showThreadMessages && {
$or: [{
Expand Down
2 changes: 2 additions & 0 deletions definition/IRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export interface IRoom extends IRocketChatRecord {
unread?: number;
alert?: boolean;
hideUnreadStatus?: boolean;

sysMes?: string[];
}

export interface IDirectMessageRoom extends Omit<IRoom, 'default' | 'featured' | 'u' | 'name'> {
Expand Down
4 changes: 2 additions & 2 deletions definition/ISetting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export enum SettingEditor {
EXPRESSION = 'expression'
}

export type SettingValueMultiSelect = Array<{key: string; i18nLabel: string}>
export type SettingValueRoomPick = Array<{_id: string; name: string}> | string
export type SettingValueMultiSelect = string[];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this needed? 👀

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤫

jk 😂 the type was actually wrong.. the value for a multi-select setting is an array of strings.. so I had to fix the type to be able to use it correctly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here is the setting record:

{
    "_id" : "Hide_System_Messages",
    "createdAt" : ISODate("2021-01-11T12:21:35.320Z"),
    "group" : "Message",
    "i18nDescription" : "Hide_System_Messages_Description",
    "i18nLabel" : "Hide_System_Messages",
    "ts" : ISODate("2021-06-14T12:21:41.761Z"),
    "type" : "multiSelect",
    "value" : [ 
        "uj", 
        "ul", 
        "ru"
    ],
    "values" : [ 
        {
            "key" : "uj",
            "i18nLabel" : "Message_HideType_uj"
        }, 
        {
            "key" : "ul",
            "i18nLabel" : "Message_HideType_ul"
        }, 
        {
            "key" : "ru",
            "i18nLabel" : "Message_HideType_ru"
        }, 
        {
            "key" : "au",
            "i18nLabel" : "Message_HideType_au"
        }, 
        {
            "key" : "mute_unmute",
            "i18nLabel" : "Message_HideType_mute_unmute"
        }, 
        {
            "key" : "r",
            "i18nLabel" : "Message_HideType_r"
        }, 
        {
            "key" : "ut",
            "i18nLabel" : "Message_HideType_ut"
        }, 
        {
            "key" : "wm",
            "i18nLabel" : "Message_HideType_wm"
        }, 
        {
            "key" : "rm",
            "i18nLabel" : "Message_HideType_rm"
        }, 
        {
            "key" : "subscription-role-added",
            "i18nLabel" : "Message_HideType_subscription_role_added"
        }, 
        {
            "key" : "subscription-role-removed",
            "i18nLabel" : "Message_HideType_subscription_role_removed"
        }, 
        {
            "key" : "room_archived",
            "i18nLabel" : "Message_HideType_room_archived"
        }, 
        {
            "key" : "room_unarchived",
            "i18nLabel" : "Message_HideType_room_unarchived"
        }, 
        {
            "key" : "room_changed_privacy",
            "i18nLabel" : "Message_HideType_room_changed_privacy"
        }, 
        {
            "key" : "room_changed_avatar",
            "i18nLabel" : "Message_HideType_room_changed_avatar"
        }, 
        {
            "key" : "room_changed_topic",
            "i18nLabel" : "Message_HideType_room_changed_topic"
        }, 
        {
            "key" : "room_e2e_enabled",
            "i18nLabel" : "Message_HideType_room_enabled_encryption"
        }, 
        {
            "key" : "room_e2e_disabled",
            "i18nLabel" : "Message_HideType_room_disabled_encryption"
        }
    ]
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see it 😂 nice nice, let's approve this then 👀

export type SettingValueRoomPick = Array<{_id: string; name: string}> | string;
export type SettingValue = string | boolean | number | SettingValueMultiSelect | undefined;

export interface ISettingSelectOption {
Expand Down