You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

128 lines
4.1 KiB

/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-return */
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-floating-promises */
import dayjs from 'dayjs';
import { knex } from '../../database/knex';
import { SortType } from '../../enums';
export interface User {
id: number;
name: string;
preferred_username?: string;
password_encrypted: string;
email?: string;
avatar?: string;
admin: boolean;
banned: boolean;
published: string;
updated?: string;
show_nsfw: boolean;
theme: string;
default_sort_type: number;
default_listing_type: number;
lang: string;
show_avatars: boolean;
send_notifications_to_email: boolean;
matrix_user_id?: string;
actor_id: string;
bio?: string;
local: boolean;
private_key?: string;
public_key?: string;
last_refreshed_at: string;
sitemod: boolean;
banner?: string;
has_2fa: boolean;
inbox_disabled: boolean;
}
export const getUserList = (): Promise<User[]> => { return knex<User>('user_'); };
export const getUserById = (userId: number): Promise<User|undefined> => {
return knex<User>('user_').where('id', userId).first();
};
export interface UserView {
readonly id: User['id'];
readonly actor_id: User['actor_id'];
readonly name: User['name'];
readonly preferred_username?: User['preferred_username'];
readonly avatar?: User['avatar'];
readonly banner?: User['banner'];
readonly email?: User['email']; // TODO this shouldn't be in this view
readonly matrix_user_id?: User['matrix_user_id'];
readonly bio?: User['bio'];
readonly local: User['local'];
readonly admin: User['admin'];
readonly sitemod: User['sitemod'];
readonly banned: User['banner'];
readonly show_avatars: User['show_avatars']; // TODO this is a setting, probably doesn't need to be here
readonly send_notifications_to_email: User['send_notifications_to_email']; // TODO also never used
readonly published: User['published'];
readonly number_of_posts: number,
readonly post_score: number,
readonly number_of_comments: number,
readonly comment_score: number,
readonly has_2fa: User['has_2fa'];
readonly inbox_disabled: User['inbox_disabled'];
}
export const getUserView = async (
filter: Partial<User>,
omitEmail = false,
sortType?: SortType,
limit?: number,
offset?: number,
): Promise<UserView[]> => {
const users = await knex<UserView>('hexbear.user_view').modify((query) => {
query.where(filter);
switch (sortType as SortType) {
case SortType.Active:
query.orderBy([{ column: 'comment_score', order: 'desc' }, { column: 'published', order: 'desc' }]);
break;
case SortType.Hot:
query.orderBy([{ column: 'comment_score', order: 'desc' }, { column: 'published', order: 'desc' }]);
break;
case SortType.New:
query.orderBy([{ column: 'published', order: 'desc' }]);
break;
case SortType.TopAll:
query.orderBy([{ column: 'comment_score', order: 'desc' }]);
break;
case SortType.TopYear:
query.andWhere('published', '>', dayjs().subtract(1, 'year').toISOString());
query.orderBy([{ column: 'comment_score', order: 'desc' }]);
break;
case SortType.TopMonth:
query.andWhere('published', '>', dayjs().subtract(1, 'month').toISOString());
query.orderBy([{ column: 'comment_score', order: 'desc' }]);
break;
case SortType.TopWeek:
query.andWhere('published', '>', dayjs().subtract(1, 'week').toISOString());
query.orderBy([{ column: 'comment_score', order: 'desc' }]);
break;
case SortType.TopDay:
query.andWhere('published', '>', dayjs().subtract(1, 'day').toISOString());
query.orderBy([{ column: 'comment_score', order: 'desc' }]);
break;
default:
query.orderBy([{ column: 'published', order: 'desc' }]);
}
if (limit) {
query.limit(limit);
}
if (offset) {
query.offset(offset);
}
});
if (omitEmail) {
return users.map((u: UserView) => { return { ...u, email: '' }; });
}
return users;
};