forked from Ivasoft/mattermost-mobile
[Gekidou] Login entry point (#5568)
* Login entry point * feedback review * sort imports * Fix model relations * Handle when no current team or current channel has been selected * Fix MFA unit test * update prepareCommonSystemValues arguments
This commit is contained in:
36
app/utils/channel/index.ts
Normal file
36
app/utils/channel/index.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
import {General, Permissions} from '@constants';
|
||||
import {DEFAULT_LOCALE} from '@i18n';
|
||||
import {hasPermission} from '@utils/role';
|
||||
|
||||
export function selectDefaultChannelForTeam(channels: Channel[], memberships: ChannelMembership[], teamId: string, roles?: Role[], locale = DEFAULT_LOCALE) {
|
||||
let channel: Channel|undefined;
|
||||
let canIJoinPublicChannelsInTeam = false;
|
||||
|
||||
if (roles) {
|
||||
canIJoinPublicChannelsInTeam = hasPermission(roles, Permissions.JOIN_PUBLIC_CHANNELS, true);
|
||||
}
|
||||
const defaultChannel = channels?.find((c) => c.name === General.DEFAULT_CHANNEL);
|
||||
const iAmMemberOfTheTeamDefaultChannel = Boolean(defaultChannel && memberships?.find((m) => m.channel_id === defaultChannel.id));
|
||||
const myFirstTeamChannel = channels?.filter((c) => c.team_id === teamId && c.type === General.OPEN_CHANNEL && Boolean(memberships?.find((m) => c.id === m.channel_id))).
|
||||
sort(sortChannelsByDisplayName.bind(null, locale))[0];
|
||||
|
||||
if (iAmMemberOfTheTeamDefaultChannel || canIJoinPublicChannelsInTeam) {
|
||||
channel = defaultChannel;
|
||||
} else {
|
||||
channel = myFirstTeamChannel || defaultChannel;
|
||||
}
|
||||
|
||||
return channel;
|
||||
}
|
||||
|
||||
export function sortChannelsByDisplayName(locale: string, a: Channel, b: Channel): number {
|
||||
// if both channels have the display_name defined
|
||||
if (a.display_name && b.display_name && a.display_name !== b.display_name) {
|
||||
return a.display_name.toLowerCase().localeCompare(b.display_name.toLowerCase(), locale, {numeric: true});
|
||||
}
|
||||
|
||||
return a.name.toLowerCase().localeCompare(b.name.toLowerCase(), locale, {numeric: true});
|
||||
}
|
||||
@@ -1,9 +1,13 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
type SortByCreatAt = (Session | Channel | Team | Post) & {
|
||||
create_at: number;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
export function emptyFunction(e?: any) {
|
||||
// eslint-disable-line no-empty-function, @typescript-eslint/no-unused-vars
|
||||
export function emptyFunction(..._args: any[]) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
// Generates a RFC-4122 version 4 compliant globally unique identifier.
|
||||
@@ -25,3 +29,11 @@ export const generateId = (): string => {
|
||||
});
|
||||
return id;
|
||||
};
|
||||
|
||||
export const sortByNewest = (a: SortByCreatAt, b: SortByCreatAt) => {
|
||||
if (a.create_at > b.create_at) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 1;
|
||||
};
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
export default {
|
||||
initialLoad: 0,
|
||||
channelSwitch: 0,
|
||||
teamSwitch: 0,
|
||||
};
|
||||
@@ -1,11 +1,59 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
import {General} from '@constants';
|
||||
import {General, Preferences} from '@constants';
|
||||
import {DEFAULT_LOCALE, getLocalizedMessage, t} from '@i18n';
|
||||
|
||||
export function getUserIdFromChannelName(userId: string, channelName: string): string {
|
||||
export function displayUsername(user?: UserProfile, locale?: string, teammateDisplayNameSetting?: string, useFallbackUsername = true) {
|
||||
let name = useFallbackUsername ? getLocalizedMessage(locale || DEFAULT_LOCALE, t('channel_loader.someone'), 'Someone') : '';
|
||||
|
||||
if (user) {
|
||||
if (teammateDisplayNameSetting === Preferences.DISPLAY_PREFER_NICKNAME) {
|
||||
name = user.nickname || getFullName(user);
|
||||
} else if (teammateDisplayNameSetting === Preferences.DISPLAY_PREFER_FULL_NAME) {
|
||||
name = getFullName(user);
|
||||
} else {
|
||||
name = user.username;
|
||||
}
|
||||
|
||||
if (!name || name.trim().length === 0) {
|
||||
name = user.username;
|
||||
}
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
export function displayGroupMessageName(users: UserProfile[], locale?: string, teammateDisplayNameSetting?: string, excludeUserId?: string) {
|
||||
const names: string[] = [];
|
||||
const sortUsernames = (a: string, b: string) => {
|
||||
return a.localeCompare(b, locale || DEFAULT_LOCALE, {numeric: true});
|
||||
};
|
||||
|
||||
users.forEach((u) => {
|
||||
if (u.id !== excludeUserId) {
|
||||
names.push(displayUsername(u, locale, teammateDisplayNameSetting));
|
||||
}
|
||||
});
|
||||
|
||||
return names.sort(sortUsernames).join(', ');
|
||||
}
|
||||
|
||||
export function getFullName(user: UserProfile): string {
|
||||
if (user.first_name && user.last_name) {
|
||||
return `${user.first_name} ${user.last_name}`;
|
||||
} else if (user.first_name) {
|
||||
return user.first_name;
|
||||
} else if (user.last_name) {
|
||||
return user.last_name;
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
export function getUserIdFromChannelName(knownUserId: string, channelName: string): string {
|
||||
const ids = channelName.split('__');
|
||||
if (ids[0] === userId) {
|
||||
if (ids[0] === knownUserId) {
|
||||
return ids[1];
|
||||
}
|
||||
return ids[0];
|
||||
|
||||
Reference in New Issue
Block a user