* Initial commit post input * Fix message posting, add create direct channel and minor fixes * Fix "is typing" and "react to last post" behaviour * Some reordering, better handling of upload error, properly clear draft on send message, and fix minor progress bar misbehavior * Add keyboard listener for shift-enter, add selection between video or photo while attaching, add alert when trying to attach more than you are allowed, add paste functionality, minor fixes and reordering * Add library patch * Fix lint * Address feedback * Address feedback * Add missing negation * Check for group name and fix typo on draft comparisons * Address feedback * Address feedback * Address feedback * Address feedback * Fix several bugs * Remove @app imports * Address feedback * fix post list & post draft layout on iOS * Fix post draft cursor position * Fix file upload route * Allow to pick multiple images using the image picker * accurately get the channel member count * remove android cursor workaround * Remove local const INPUT_LINE_HEIGHT * move getPlaceHolder out of the component * use substring instead of legacy substr for hardward keyboard * Move onAppStateChange above the effects * Fix camera action bottom sheet * no need to memo SendButton * properly use memberCount in sender handler * Refactor how to get memberCount * Fix queryRecentPostsInThread * Remove unused isDirectChannelVisible && isGroupChannelVisible util functions * rename errorBadUser to errorUnkownUser * extract localized strings * use ClientErrorProps instead of ClientError * Minor improvements Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
144 lines
5.1 KiB
TypeScript
144 lines
5.1 KiB
TypeScript
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
import {SYSTEM_IDENTIFIERS} from '@constants/database';
|
|
import General from '@constants/general';
|
|
import DatabaseManager from '@database/manager';
|
|
import {queryRecentCustomStatuses} from '@queries/servers/system';
|
|
import {queryCurrentUser} from '@queries/servers/user';
|
|
|
|
import {addRecentReaction} from './reactions';
|
|
|
|
import type Model from '@nozbe/watermelondb/Model';
|
|
import type UserModel from '@typings/database/models/servers/user';
|
|
|
|
export const setCurrentUserStatusOffline = async (serverUrl: string) => {
|
|
const serverDatabase = DatabaseManager.serverDatabases[serverUrl];
|
|
if (!serverDatabase) {
|
|
return {error: `No database present for ${serverUrl}`};
|
|
}
|
|
|
|
const {database, operator} = serverDatabase;
|
|
|
|
const user = await queryCurrentUser(database);
|
|
if (!user) {
|
|
return {error: `No current user for ${serverUrl}`};
|
|
}
|
|
|
|
user.prepareStatus(General.OFFLINE);
|
|
|
|
try {
|
|
await operator.batchRecords([user]);
|
|
} catch {
|
|
// eslint-disable-next-line no-console
|
|
console.log('FAILED TO BATCH CHANGES FOR SET CURRENT USER STATUS OFFLINE');
|
|
}
|
|
|
|
return null;
|
|
};
|
|
|
|
export const updateLocalCustomStatus = async (serverUrl: string, user: UserModel, customStatus?: UserCustomStatus) => {
|
|
const operator = DatabaseManager.serverDatabases[serverUrl]?.operator;
|
|
if (!operator) {
|
|
return {error: `${serverUrl} database not found`};
|
|
}
|
|
|
|
const models: Model[] = [];
|
|
const currentProps = {...user.props, customStatus: customStatus || {}};
|
|
const userModel = user.prepareUpdate((u: UserModel) => {
|
|
u.props = currentProps;
|
|
});
|
|
|
|
models.push(userModel);
|
|
if (customStatus) {
|
|
const recent = await updateRecentCustomStatuses(serverUrl, customStatus, true);
|
|
if (Array.isArray(recent)) {
|
|
models.push(...recent);
|
|
}
|
|
|
|
if (customStatus.emoji) {
|
|
const recentEmojis = await addRecentReaction(serverUrl, [customStatus.emoji], true);
|
|
if (Array.isArray(recentEmojis)) {
|
|
models.push(...recentEmojis);
|
|
}
|
|
}
|
|
}
|
|
|
|
try {
|
|
await operator.batchRecords(models);
|
|
} catch {
|
|
// eslint-disable-next-line no-console
|
|
console.log('FAILED TO BATCH CHANGES FOR UPDATING CUSTOM STATUS');
|
|
}
|
|
|
|
return {};
|
|
};
|
|
|
|
export const updateRecentCustomStatuses = async (serverUrl: string, customStatus: UserCustomStatus, prepareRecordsOnly = false, remove = false) => {
|
|
const operator = DatabaseManager.serverDatabases[serverUrl]?.operator;
|
|
if (!operator) {
|
|
return {error: `${serverUrl} database not found`};
|
|
}
|
|
|
|
const recent = await queryRecentCustomStatuses(operator.database);
|
|
const recentStatuses = (recent ? recent.value : []) as UserCustomStatus[];
|
|
const index = recentStatuses.findIndex((cs) => (
|
|
cs.emoji === customStatus.emoji &&
|
|
cs.text === customStatus.text &&
|
|
cs.duration === customStatus.duration
|
|
));
|
|
|
|
if (index !== -1) {
|
|
recentStatuses.splice(index, 1);
|
|
}
|
|
|
|
if (!remove) {
|
|
recentStatuses.unshift(customStatus);
|
|
}
|
|
|
|
return operator.handleSystem({
|
|
systems: [{
|
|
id: SYSTEM_IDENTIFIERS.RECENT_CUSTOM_STATUS,
|
|
value: JSON.stringify(recentStatuses),
|
|
}],
|
|
prepareRecordsOnly,
|
|
});
|
|
};
|
|
|
|
export const updateLocalUser = async (
|
|
serverUrl: string,
|
|
userDetails: Partial<UserProfile> & { status?: string},
|
|
) => {
|
|
const database = DatabaseManager.serverDatabases[serverUrl]?.database;
|
|
if (!database) {
|
|
return {error: `${serverUrl} database not found`};
|
|
}
|
|
|
|
try {
|
|
const user = await queryCurrentUser(database);
|
|
if (user) {
|
|
await database.write(async () => {
|
|
await user.update((userRecord: UserModel) => {
|
|
userRecord.authService = userDetails.auth_service ?? user.authService;
|
|
userRecord.email = userDetails.email ?? user.email;
|
|
userRecord.firstName = userDetails.first_name ?? user.firstName;
|
|
userRecord.lastName = userDetails.last_name ?? user.lastName;
|
|
userRecord.lastPictureUpdate = userDetails.last_picture_update ?? user.lastPictureUpdate;
|
|
userRecord.locale = userDetails.locale ?? user.locale;
|
|
userRecord.nickname = userDetails.nickname ?? user.nickname;
|
|
userRecord.notifyProps = userDetails.notify_props ?? user.notifyProps;
|
|
userRecord.position = userDetails?.position ?? user.position;
|
|
userRecord.props = userDetails.props ?? user.props;
|
|
userRecord.roles = userDetails.roles ?? user.roles;
|
|
userRecord.status = userDetails?.status ?? user.status;
|
|
userRecord.timezone = userDetails.timezone ?? user.timezone;
|
|
userRecord.username = userDetails.username ?? user.username;
|
|
});
|
|
});
|
|
}
|
|
} catch (error) {
|
|
return {error};
|
|
}
|
|
|
|
return {};
|
|
};
|