Chore: Convert Fiber models to async Step 1 (#23633)
Co-authored-by: Diego Sampaio <chinello@gmail.com> Co-authored-by: Guilherme Gazzo <guilhermegazzo@gmail.com>pull/23689/head
parent
283d957788
commit
f50609df0b
@ -1,14 +0,0 @@ |
||||
import { WebdavAccounts } from '../../../models/server/raw'; |
||||
|
||||
export async function findWebdavAccountsByUserId({ uid }) { |
||||
return { |
||||
accounts: await WebdavAccounts.findWithUserId(uid, { |
||||
fields: { |
||||
_id: 1, |
||||
username: 1, |
||||
server_url: 1, |
||||
name: 1, |
||||
}, |
||||
}).toArray(), |
||||
}; |
||||
} |
@ -0,0 +1,16 @@ |
||||
import { WebdavAccounts } from '../../../models/server/raw'; |
||||
import { IWebdavAccount } from '../../../../definition/IWebdavAccount'; |
||||
|
||||
export async function findWebdavAccountsByUserId({ uid }: { uid: string }): Promise<{ accounts: IWebdavAccount[] }> { |
||||
return { |
||||
accounts: await WebdavAccounts.findWithUserId(uid, { |
||||
projection: { |
||||
_id: 1, |
||||
username: 1, |
||||
// eslint-disable-next-line @typescript-eslint/camelcase
|
||||
server_url: 1, |
||||
name: 1, |
||||
}, |
||||
}).toArray(), |
||||
}; |
||||
} |
@ -1,3 +0,0 @@ |
||||
import { Roles } from '../../../models'; |
||||
|
||||
export const getRoles = () => Roles.find().fetch(); |
@ -0,0 +1,4 @@ |
||||
import { IRole } from '../../../../definition/IUser'; |
||||
import { Roles } from '../../../models/server/raw'; |
||||
|
||||
export const getRoles = (): IRole[] => Promise.await(Roles.find().toArray()); |
@ -1,3 +0,0 @@ |
||||
import { Roles } from '../../../models'; |
||||
|
||||
export const getUsersInRole = (roleName, scope, options) => Roles.findUsersInRole(roleName, scope, options); |
@ -0,0 +1,14 @@ |
||||
|
||||
|
||||
import { Cursor, FindOneOptions, WithoutProjection } from 'mongodb'; |
||||
|
||||
import { IRole, IUser } from '../../../../definition/IUser'; |
||||
import { Roles } from '../../../models/server/raw'; |
||||
|
||||
export function getUsersInRole(name: IRole['name'], scope?: string): Promise<Cursor<IUser>>; |
||||
|
||||
export function getUsersInRole(name: IRole['name'], scope: string | undefined, options: WithoutProjection<FindOneOptions<IUser>>): Promise<Cursor<IUser>>; |
||||
|
||||
export function getUsersInRole<P>(name: IRole['name'], scope: string | undefined, options: FindOneOptions<P extends IUser ? IUser : P>): Promise<Cursor<P>>; |
||||
|
||||
export function getUsersInRole<P>(name: IRole['name'], scope: string | undefined, options?: any | undefined): Promise<Cursor<IUser | P>> { return Roles.findUsersInRole(name, scope, options); } |
@ -1,13 +1,18 @@ |
||||
import { Meteor } from 'meteor/meteor'; |
||||
|
||||
import { Permissions } from '../../../models/server'; |
||||
import { hasPermission } from '../functions/hasPermission'; |
||||
import { CONSTANTS } from '../../lib'; |
||||
import { Permissions } from '../../../models/server/raw'; |
||||
|
||||
Meteor.methods({ |
||||
'authorization:removeRoleFromPermission'(permissionId, role) { |
||||
async 'authorization:removeRoleFromPermission'(permissionId, role) { |
||||
const uid = Meteor.userId(); |
||||
const permission = Permissions.findOneById(permissionId); |
||||
const permission = await Permissions.findOneById(permissionId); |
||||
|
||||
|
||||
if (!permission) { |
||||
throw new Meteor.Error('error-permission-not-found', 'Permission not found', { method: 'authorization:removeRoleFromPermission' }); |
||||
} |
||||
|
||||
if (!uid || !hasPermission(uid, 'access-permissions') || (permission.level === CONSTANTS.SETTINGS_LEVEL && !hasPermission(uid, 'access-setting-permissions'))) { |
||||
throw new Meteor.Error('error-action-not-allowed', 'Removing permission is not allowed', { |
@ -1,25 +0,0 @@ |
||||
import { Meteor } from 'meteor/meteor'; |
||||
|
||||
import Permissions from '../../../../models/server/models/Permissions'; |
||||
|
||||
Meteor.methods({ |
||||
'permissions/get'(updatedAt) { |
||||
// TODO: should we return this for non logged users?
|
||||
// TODO: we could cache this collection
|
||||
|
||||
const records = Permissions.find().fetch(); |
||||
|
||||
if (updatedAt instanceof Date) { |
||||
return { |
||||
update: records.filter((record) => record._updatedAt > updatedAt), |
||||
remove: Permissions.trashFindDeletedAfter( |
||||
updatedAt, |
||||
{}, |
||||
{ fields: { _id: 1, _deletedAt: 1 } }, |
||||
).fetch(), |
||||
}; |
||||
} |
||||
|
||||
return records; |
||||
}, |
||||
}); |
@ -0,0 +1,28 @@ |
||||
import { Meteor } from 'meteor/meteor'; |
||||
import { check, Match } from 'meteor/check'; |
||||
|
||||
import { Permissions } from '../../../../models/server/raw'; |
||||
|
||||
Meteor.methods({ |
||||
async 'permissions/get'(updatedAt: Date) { |
||||
check(updatedAt, Match.Maybe(Date)); |
||||
|
||||
// TODO: should we return this for non logged users?
|
||||
// TODO: we could cache this collection
|
||||
|
||||
const records = await Permissions.find(updatedAt && { _updatedAt: { $gt: updatedAt } }).toArray(); |
||||
|
||||
if (updatedAt instanceof Date) { |
||||
return { |
||||
update: records, |
||||
remove: await Permissions.trashFindDeletedAfter( |
||||
updatedAt, |
||||
{}, |
||||
{ fields: { _id: 1, _deletedAt: 1 } }, |
||||
).toArray(), |
||||
}; |
||||
} |
||||
|
||||
return records; |
||||
}, |
||||
}); |
@ -1,11 +0,0 @@ |
||||
import { Meteor } from 'meteor/meteor'; |
||||
|
||||
import { Permissions } from '../../models'; |
||||
|
||||
Meteor.startup(() => { |
||||
if (Permissions) { |
||||
if (!Permissions.findOne({ _id: 'auto-translate' })) { |
||||
Permissions.insert({ _id: 'auto-translate', roles: ['admin'] }); |
||||
} |
||||
} |
||||
}); |
@ -0,0 +1,9 @@ |
||||
import { Meteor } from 'meteor/meteor'; |
||||
|
||||
import { Permissions } from '../../models/server/raw'; |
||||
|
||||
Meteor.startup(async () => { |
||||
if (!await Permissions.findOne({ _id: 'auto-translate' })) { |
||||
Permissions.create('auto-translate', ['admin']); |
||||
} |
||||
}); |
@ -1,9 +1,9 @@ |
||||
import { Meteor } from 'meteor/meteor'; |
||||
|
||||
import { CustomSounds } from '../../../models'; |
||||
import { CustomSounds } from '../../../models/server/raw'; |
||||
|
||||
Meteor.methods({ |
||||
listCustomSounds() { |
||||
return CustomSounds.find({}).fetch(); |
||||
async listCustomSounds() { |
||||
return CustomSounds.find({}).toArray(); |
||||
}, |
||||
}); |
||||
|
@ -1,6 +1,7 @@ |
||||
import { Meteor } from 'meteor/meteor'; |
||||
|
||||
import { Permissions } from '../../models'; |
||||
import { Permissions } from '../../models/server/raw'; |
||||
|
||||
|
||||
Meteor.startup(() => { |
||||
// Add permissions for discussion
|
@ -1,9 +1,9 @@ |
||||
import { Meteor } from 'meteor/meteor'; |
||||
|
||||
import { EmojiCustom } from '../../../models'; |
||||
import { EmojiCustom } from '../../../models/server/raw'; |
||||
|
||||
Meteor.methods({ |
||||
listEmojiCustom(options = {}) { |
||||
return EmojiCustom.find(options).fetch(); |
||||
async listEmojiCustom(options = {}) { |
||||
return EmojiCustom.find(options).toArray(); |
||||
}, |
||||
}); |
||||
|
@ -1,69 +0,0 @@ |
||||
import { Settings, Subscriptions, Users } from '../../../models/server'; |
||||
import { STATUS_ENABLED, STATUS_REGISTERING } from '../constants'; |
||||
|
||||
export const getNameAndDomain = (fullyQualifiedName) => fullyQualifiedName.split('@'); |
||||
export const isFullyQualified = (name) => name.indexOf('@') !== -1; |
||||
|
||||
export function isRegisteringOrEnabled() { |
||||
const status = Settings.findOneById('FEDERATION_Status'); |
||||
return [STATUS_ENABLED, STATUS_REGISTERING].includes(status && status.value); |
||||
} |
||||
|
||||
export function updateStatus(status) { |
||||
Settings.updateValueById('FEDERATION_Status', status); |
||||
} |
||||
|
||||
export function updateEnabled(enabled) { |
||||
Settings.updateValueById('FEDERATION_Enabled', enabled); |
||||
} |
||||
|
||||
export const checkRoomType = (room) => room.t === 'p' || room.t === 'd'; |
||||
export const checkRoomDomainsLength = (domains) => domains.length <= (process.env.FEDERATED_DOMAINS_LENGTH || 10); |
||||
|
||||
export const hasExternalDomain = ({ federation }) => { |
||||
// same test as isFederated(room)
|
||||
if (!federation) { |
||||
return false; |
||||
} |
||||
|
||||
return federation.domains |
||||
.some((domain) => domain !== federation.origin); |
||||
}; |
||||
|
||||
export const isLocalUser = ({ federation }, localDomain) => |
||||
!federation || federation.origin === localDomain; |
||||
|
||||
export const getFederatedRoomData = (room) => { |
||||
let hasFederatedUser = false; |
||||
|
||||
let users = null; |
||||
let subscriptions = null; |
||||
|
||||
if (room.t === 'd') { |
||||
// Check if there is a federated user on this room
|
||||
hasFederatedUser = room.usernames.some(isFullyQualified); |
||||
} else { |
||||
// Find all subscriptions of this room
|
||||
subscriptions = Subscriptions.findByRoomIdWhenUsernameExists(room._id).fetch(); |
||||
subscriptions = subscriptions.reduce((acc, s) => { |
||||
acc[s.u._id] = s; |
||||
|
||||
return acc; |
||||
}, {}); |
||||
|
||||
// Get all user ids
|
||||
const userIds = Object.keys(subscriptions); |
||||
|
||||
// Load all the users
|
||||
users = Users.findUsersWithUsernameByIds(userIds).fetch(); |
||||
|
||||
// Check if there is a federated user on this room
|
||||
hasFederatedUser = users.some((u) => isFullyQualified(u.username)); |
||||
} |
||||
|
||||
return { |
||||
hasFederatedUser, |
||||
users, |
||||
subscriptions, |
||||
}; |
||||
}; |
@ -0,0 +1,77 @@ |
||||
import { IRoom, isDirectMessageRoom } from '../../../../definition/IRoom'; |
||||
import { ISubscription } from '../../../../definition/ISubscription'; |
||||
import { IRegisterUser, IUser } from '../../../../definition/IUser'; |
||||
import { Subscriptions, Users } from '../../../models/server'; |
||||
import { Settings } from '../../../models/server/raw'; |
||||
import { STATUS_ENABLED, STATUS_REGISTERING } from '../constants'; |
||||
|
||||
export const getNameAndDomain = (fullyQualifiedName: string): string [] => fullyQualifiedName.split('@'); |
||||
|
||||
export const isFullyQualified = (name: string): boolean => name.indexOf('@') !== -1; |
||||
|
||||
export async function isRegisteringOrEnabled(): Promise<boolean> { |
||||
const value = await Settings.getValueById('FEDERATION_Status'); |
||||
return typeof value === 'string' && [STATUS_ENABLED, STATUS_REGISTERING].includes(value); |
||||
} |
||||
|
||||
export async function updateStatus(status: string): Promise<void> { |
||||
await Settings.updateValueById('FEDERATION_Status', status); |
||||
} |
||||
|
||||
export async function updateEnabled(enabled: boolean): Promise<void> { |
||||
await Settings.updateValueById('FEDERATION_Enabled', enabled); |
||||
} |
||||
|
||||
export const checkRoomType = (room: IRoom): boolean => room.t === 'p' || room.t === 'd'; |
||||
export const checkRoomDomainsLength = (domains: unknown[]): boolean => domains.length <= (process.env.FEDERATED_DOMAINS_LENGTH || 10); |
||||
|
||||
export const hasExternalDomain = ({ federation }: { federation: { origin: string; domains: string[] } }): boolean => { |
||||
// same test as isFederated(room)
|
||||
if (!federation) { |
||||
return false; |
||||
} |
||||
|
||||
return federation.domains |
||||
.some((domain) => domain !== federation.origin); |
||||
}; |
||||
|
||||
export const isLocalUser = ({ federation }: { federation: { origin: string } }, localDomain: string): boolean => |
||||
!federation || federation.origin === localDomain; |
||||
|
||||
export const getFederatedRoomData = (room: IRoom): { |
||||
hasFederatedUser: boolean; |
||||
users: IUser[]; |
||||
subscriptions: { [k: string]: ISubscription } | undefined; |
||||
} => { |
||||
if (isDirectMessageRoom(room)) { |
||||
// Check if there is a federated user on this room
|
||||
|
||||
return { |
||||
users: [], |
||||
hasFederatedUser: room.usernames.some(isFullyQualified), |
||||
subscriptions: undefined, |
||||
}; |
||||
} |
||||
|
||||
// Find all subscriptions of this room
|
||||
const s = Subscriptions.findByRoomIdWhenUsernameExists(room._id).fetch() as ISubscription[]; |
||||
const subscriptions = s.reduce((acc, s) => { |
||||
acc[s.u._id] = s; |
||||
return acc; |
||||
}, {} as { [k: string]: ISubscription }); |
||||
|
||||
// Get all user ids
|
||||
const userIds = Object.keys(subscriptions); |
||||
|
||||
// Load all the users
|
||||
const users: IRegisterUser[] = Users.findUsersWithUsernameByIds(userIds).fetch(); |
||||
|
||||
// Check if there is a federated user on this room
|
||||
const hasFederatedUser = users.some((u) => isFullyQualified(u.username)); |
||||
|
||||
return { |
||||
hasFederatedUser, |
||||
users, |
||||
subscriptions, |
||||
}; |
||||
}; |
@ -1,6 +1,8 @@ |
||||
import { FederationKeys } from '../../../models/server'; |
||||
import { FederationKeys } from '../../../models/server/raw'; |
||||
|
||||
// Create key pair if needed
|
||||
if (!FederationKeys.getPublicKey()) { |
||||
FederationKeys.generateKeys(); |
||||
} |
||||
(async () => { |
||||
if (!await FederationKeys.getPublicKey()) { |
||||
await FederationKeys.generateKeys(); |
||||
} |
||||
})(); |
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue