mattermost-mobile/app/utils/post/index.ts
Kyriakos Z 675d8495b3
[Gekidou]: MM-39757 - Recent mentions (#5823)
* [Gekidou]: MM-39757 - Recent mentions

* Refactor channel_info to a separate component

* Fixes schema tests

* Fixes channel_info theme color

* Removes RECEIVED_MENTIONS table and model

Removes RECEIVED_MENTIONS table and model and saves recent_mentions in
the SYSTEM table under the recentMentions ID.

* Cleanup recent_mentions handler

* Adds i18n in recent_mentions screen

* Observe changes on the post messages

* Addresses review comments

* Batches records

* Addresses review comments

* Addresses review comments

* Addresses review comments

* Addresses review comments

* Fetches channels and users needed for mentions

Fetching mentions from all teams might result in missing info like user
profiles, and channels missing from the DB.
This commit fetches all missing users and channels.

* Adds empty state for recent mentions

* Prepares all missing models for channels

* Addresses review comments

* Fixes mention keys for recent mentions

User mention keys when asking for mentions should not include general
purpose ones, like @channel, @all, @here.

Fixes ActivityIndicator color in recent mentions screen.

* Removes top margin of mention message

* Addresses review comments

* Fixes group.name undefined
2021-12-16 12:26:43 +02:00

75 lines
3 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {Post} from '@constants';
import {DEFAULT_LOCALE} from '@i18n';
import {displayUsername} from '@utils/user';
import type GroupModel from '@typings/database/models/servers/group';
import type PostModel from '@typings/database/models/servers/post';
import type UserModel from '@typings/database/models/servers/user';
import type {UserMentionKey} from '@typings/global/markdown';
export function areConsecutivePosts(post: PostModel, previousPost: PostModel) {
let consecutive = false;
if (post && previousPost) {
const postFromWebhook = Boolean(post?.props?.from_webhook); // eslint-disable-line camelcase
const prevPostFromWebhook = Boolean(previousPost?.props?.from_webhook); // eslint-disable-line camelcase
const isFromSameUser = previousPost.userId === post.userId;
const isNotSystemMessage = !isSystemMessage(post) && !isSystemMessage(previousPost);
const isInTimeframe = (post.createAt - previousPost.createAt) <= Post.POST_COLLAPSE_TIMEOUT;
const isSameThread = (previousPost.rootId === post.rootId || previousPost.id === post.rootId);
// Were the last post and this post made by the same user within some time?
consecutive = previousPost && (isFromSameUser || isInTimeframe) && !postFromWebhook &&
!prevPostFromWebhook && isNotSystemMessage && isSameThread;
}
return consecutive;
}
export function isFromWebhook(post: PostModel): boolean {
return post.props && post.props.from_webhook === 'true';
}
export function isEdited(post: PostModel): boolean {
return post.editAt > 0;
}
export function isPostEphemeral(post: PostModel): boolean {
return post.type === Post.POST_TYPES.EPHEMERAL || post.type === Post.POST_TYPES.EPHEMERAL_ADD_TO_CHANNEL || post.deleteAt > 0;
}
export function isPostPendingOrFailed(post: PostModel): boolean {
return post.pendingPostId === post.id || post.props.failed;
}
export function isSystemMessage(post: PostModel): boolean {
return Boolean(post.type && post.type?.startsWith(Post.POST_TYPES.SYSTEM_MESSAGE_PREFIX));
}
export function fromAutoResponder(post: PostModel): boolean {
return Boolean(post.type && (post.type === Post.POST_TYPES.SYSTEM_AUTO_RESPONDER));
}
export function postUserDisplayName(post: PostModel, author?: UserModel, teammateNameDisplay?: string, enablePostUsernameOverride = false) {
if (isFromWebhook(post) && post.props?.override_username && enablePostUsernameOverride) {
return post.props.override_username;
}
return displayUsername(author, author?.locale || DEFAULT_LOCALE, teammateNameDisplay, true);
}
export const getMentionKeysForPost = (user: UserModel, post: PostModel, groups: GroupModel[] | null) => {
const keys: UserMentionKey[] = user.mentionKeys;
if (groups?.length) {
for (const group of groups) {
if (group.name && group.name.trim()) {
keys.push({key: `@${group.name}`});
}
}
}
return keys;
};