Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .snaplet/dataModel.json
Original file line number Diff line number Diff line change
Expand Up @@ -8561,7 +8561,7 @@
"id": "public.projects.tags",
"name": "tags",
"columnName": "tags",
"type": "text[]",
"type": "int8[]",
"isRequired": false,
"kind": "scalar",
"isList": false,
Expand Down Expand Up @@ -9591,7 +9591,7 @@
"id": "public.research.tags",
"name": "tags",
"columnName": "tags",
"type": "text[]",
"type": "int8[]",
"isRequired": false,
"kind": "scalar",
"isList": false,
Expand Down
4 changes: 2 additions & 2 deletions seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ const main = async () => {
...item,
category: categories.find((cat) => cat.type === 'projects')?.id,
created_by: profiles[0].id,
tags: [tags[0].name],
tags: [tags[0].id],
})),
);

Expand All @@ -736,7 +736,7 @@ const main = async () => {
...item,
category: categories.find((cat) => cat.type === 'research')?.id,
created_by: profiles[0].id,
tags: [tags[0].id.toString()],
tags: [tags[0].id],
})),
);

Expand Down
18 changes: 18 additions & 0 deletions shared/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import type { JSONContent } from '@tiptap/core';
import type { Database as GeneratedDatabase } from './database.types';
import type { DBAuthor } from './models/author';
import type { DBCategory } from './models/category';
import type { DiscussionContentType } from './models/common';
import type { DifficultyLevel } from './models/library';
import type { DBMedia, IMediaFile } from './models/media';
import type { Moderation } from './models/moderation';
import type { BadgeAvailability } from './models/profileBadge';
import type { DBDonationSettings, PWAIcons } from './models/tenantSettings';

type Overlay<T, U> = Flatten<Omit<T, keyof U> & U>;
type Flatten<T> = { [K in keyof T]: T[K] };
Expand All @@ -22,6 +24,11 @@ type ColumnOverrides = {
profile_badges: {
available_to: BadgeAvailability | null;
};
profiles: {
photo: DBMedia | null;
cover_images: DBMedia[] | null;
moderation: Moderation | null;
};
questions: {
images: DBMedia[] | null;
};
Expand All @@ -41,9 +48,20 @@ type ColumnOverrides = {
images: DBMedia[] | null;
files: IMediaFile[] | null;
};
map_pins: {
moderation: Moderation;
};
tenant_settings: {
donation_settings: DBDonationSettings | null;
pwa_icons: PWAIcons | null;
};
};

type ReturnOverrides = {
get_comments_with_votes: {
profile: DBAuthor | null;
source_type: DiscussionContentType;
};
get_questions: {
author: DBAuthor | null;
category: DBCategory | null;
Expand Down
16 changes: 8 additions & 8 deletions shared/database.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,7 @@ export type Database = {
previous_slugs: string[] | null;
published_at: string | null;
slug: string;
tags: string[] | null;
tags: number[] | null;
tenant_id: string;
time: string | null;
title: string;
Expand All @@ -1021,7 +1021,7 @@ export type Database = {
previous_slugs?: string[] | null;
published_at?: string | null;
slug: string;
tags?: string[] | null;
tags?: number[] | null;
tenant_id: string;
time?: string | null;
title: string;
Expand All @@ -1048,7 +1048,7 @@ export type Database = {
previous_slugs?: string[] | null;
published_at?: string | null;
slug?: string;
tags?: string[] | null;
tags?: number[] | null;
tenant_id?: string;
time?: string | null;
title?: string;
Expand Down Expand Up @@ -1231,7 +1231,7 @@ export type Database = {
published_at: string | null;
slug: string;
status: Database['public']['Enums']['research_status'] | null;
tags: string[] | null;
tags: number[] | null;
tenant_id: string;
title: string;
total_useful: number | null;
Expand All @@ -1253,7 +1253,7 @@ export type Database = {
published_at?: string | null;
slug: string;
status?: Database['public']['Enums']['research_status'] | null;
tags?: string[] | null;
tags?: number[] | null;
tenant_id: string;
title: string;
total_useful?: number | null;
Expand All @@ -1275,7 +1275,7 @@ export type Database = {
published_at?: string | null;
slug?: string;
status?: Database['public']['Enums']['research_status'] | null;
tags?: string[] | null;
tags?: number[] | null;
tenant_id?: string;
title?: string;
total_useful?: number | null;
Expand Down Expand Up @@ -1822,7 +1822,7 @@ export type Database = {
moderation: string;
modified_at: string;
slug: string;
tags: string[];
tags: number[];
title: string;
total_views: number;
useful_votes_last_week: number;
Expand Down Expand Up @@ -1890,7 +1890,7 @@ export type Database = {
modified_at: string;
slug: string;
status: Database['public']['Enums']['research_status'];
tags: string[];
tags: number[];
title: string;
total_views: number;
update_count: number;
Expand Down
13 changes: 12 additions & 1 deletion shared/models/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,18 @@ export type Collaborator = {

export type ContentType = 'questions' | 'projects' | 'research' | 'news';

export type UsefulContentType = 'questions' | 'projects' | 'research' | 'news' | 'comments';
export const UsefulContentTypes = [
'questions',
'projects',
'research',
'news',
'comments',
] as const;
export type UsefulContentType = (typeof UsefulContentTypes)[number];

export function isUsefulContentType(value: string | undefined): value is UsefulContentType {
return UsefulContentTypes.includes(value as UsefulContentType);
}

export type ContentFormType = 'questions' | 'projects' | 'research' | 'researchUpdate' | 'news';

Expand Down
2 changes: 1 addition & 1 deletion shared/models/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface IDBContentDoc extends IDBDocSB {
readonly total_views?: number | null;
readonly previous_slugs: string[] | null;
readonly slug: string;
readonly tags: number[] | string[] | null;
readonly tags: number[] | null;
readonly useful_count?: number;
}

Expand Down
6 changes: 3 additions & 3 deletions shared/models/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class DBProject implements IDBContentDoc, IDBDownloadable, IDBModeration
file_link: string | null;
files: IMediaFile[] | null;
category_id?: number;
tags: string[] | null;
tags: number[] | null;
is_draft: boolean | null;
time?: string | null;
file_download_count?: number | null;
Expand All @@ -65,7 +65,7 @@ export class DBProject implements IDBContentDoc, IDBDownloadable, IDBModeration
category: obj.category
? { value: obj.category.id.toString(), label: obj.category.name }
: null,
tags: obj.tags?.map(Number) ?? null,
tags: obj.tags,
difficultyLevel: obj.difficulty_level,
files: obj.files,
fileLink: obj.file_link,
Expand Down Expand Up @@ -131,7 +131,7 @@ export class Project implements IContentDoc, IDownloadable, IModeration {
deleted: obj.deleted || false,
category: obj.category ? Category.fromDB(obj.category) : null,
totalViews: obj.total_views || 0,
tagIds: obj.tags?.map((x) => Number(x)),
tagIds: obj.tags ?? undefined,
tags: tags,
difficultyLevel: obj.difficulty_level,
subscriberCount: obj.subscriber_count || 0,
Expand Down
5 changes: 4 additions & 1 deletion shared/models/notificationPreferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ export class NotificationsPreferences {
researchUpdates: boolean;
isUnsubscribed: boolean;

static fromDB(dBNotificationsPreferences: DBNotificationsPreferences) {
static fromDB(
dBNotificationsPreferences: Omit<DBNotificationsPreferences, 'id' | 'user_id'> &
Partial<DBNotificationsPreferences>,
) {
return {
id: dBNotificationsPreferences.id || undefined,
userId: dBNotificationsPreferences.user_id || undefined,
Expand Down
71 changes: 40 additions & 31 deletions shared/models/profile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { JSONContent } from '@tiptap/core';
import type { Json } from '../database';
import type { Comment } from './comment';
import type { SubscribableContentTypes } from './common';
import type { IDBDocSB, IDoc } from './document';
import type { DBMedia, MediaWithPublicUrl } from './media';
import type { IDBModeration, IModeration, Moderation } from './moderation';
Expand All @@ -16,29 +16,37 @@ import type { ResearchUpdate } from './research';
import { Tag } from './tag';
import type { IUserImpact, UserVisitorPreference } from './user';

function parseJsonColumn<T>(value: Json | null): T | null {
if (!value) {
return null;
}

return (typeof value === 'string' ? JSON.parse(value) : value) as T;
}

export class DBProfile {
readonly id: number;
readonly created_at: string;
readonly tags?: DBProfileTagJoin[];
readonly badges?: DBProfileBadgeJoin[];
readonly pin?: DBMapPin;
readonly type?: DBProfileType;
readonly pin?: Pick<DBMapPin, 'id' | 'moderation'>[];
readonly type?: DBProfileType | null;
username: string | null;
display_name: string;
photo: DBMedia | null;
cover_images: DBMedia[] | null;
country: string;
country: string | null;
roles: string[] | null;
visitor_policy: string | null;
visitor_policy: Json | null;
is_blocked_from_messaging: boolean | null;
about: string | null;
impact: string | null;
impact: Json | null;
is_contactable: boolean | null;
last_active: string | null;
website: string | null;
total_views: number;
auth_id: string;
profile_type: number;
total_views: number | null;
auth_id: string | null;
profile_type: number | null;
donations_enabled: boolean;
moderation: Moderation | null;
moderation_feedback: string | null;
Expand Down Expand Up @@ -83,34 +91,32 @@ export class Profile {
coverImages: MediaWithPublicUrl[] | null = null,
authorVotes?: AuthorVotes[],
) {
let impact = null;
let impact: IUserImpact | null = null;

try {
impact = dbProfile.impact ? JSON.parse(dbProfile.impact) : null;
impact = parseJsonColumn<IUserImpact>(dbProfile.impact);
} catch (_) {
console.error('error parsing impact');
}

return new Profile({
id: dbProfile.id,
createdAt: new Date(dbProfile.created_at),
country: dbProfile.country,
country: dbProfile.country ?? '',
displayName: dbProfile.display_name,
username: dbProfile.username,
photo: photo ?? null,
roles: dbProfile.roles || null,
type: dbProfile.type ? ProfileType.fromDB(dbProfile.type) : null,
visitorPolicy: dbProfile.visitor_policy
? (JSON.parse(dbProfile.visitor_policy) as UserVisitorPreference)
: null,
visitorPolicy: parseJsonColumn<UserVisitorPreference>(dbProfile.visitor_policy),
isBlockedFromMessaging: !!dbProfile.is_blocked_from_messaging,
about: dbProfile.about,
coverImages: coverImages ?? null,
impact,
isContactable: dbProfile.is_contactable || null,
lastActive: dbProfile.last_active ? new Date(dbProfile.last_active) : null,
website: dbProfile.website,
totalViews: dbProfile.total_views,
totalViews: dbProfile.total_views ?? 0,
authorUsefulVotes: authorVotes,
donationsEnabled: dbProfile.donations_enabled,
moderation: dbProfile.moderation ?? null,
Expand All @@ -133,23 +139,22 @@ export type ProfileListItem = Pick<
>;

type NotificationContent = News | Comment | Question | ResearchUpdate;
type NotificationSourceContentType = SubscribableContentTypes;

export class DBNotification implements IDBDocSB {
readonly id: number;
readonly title: string;
readonly title: string | null;
readonly action_type: NotificationActionType;
readonly content_id: number;
readonly content_type: NotificationContentType;
readonly created_at: string;
is_read: boolean;
is_read: boolean | null;
modified_at: string | null;
readonly source_content_type: NotificationSourceContentType;
readonly source_content_id: number;
readonly owned_by: DBProfile;
readonly source_content_type: string | null;
readonly source_content_id: number | null;
readonly owned_by?: DBProfile;
readonly owned_by_id: number;
readonly triggered_by: DBProfile;
readonly triggered_by_id: number;
readonly triggered_by?: Pick<DBProfile, 'id' | 'username' | 'photo'> | null;
readonly triggered_by_id: number | null;
readonly tenant_id: string;

constructor(obj: Partial<DBNotification>) {
Expand All @@ -167,8 +172,8 @@ export class Notification implements IDoc {
modifiedAt: Date | null;
ownedById: number;
isRead: boolean;
sourceContentType: NotificationSourceContentType;
sourceContentId: number;
sourceContentType: string | null;
sourceContentId: number | null;

content?: NotificationContent;
ownedBy?: BasicAuthorDetails;
Expand All @@ -181,7 +186,7 @@ export class Notification implements IDoc {
static fromDB(dbNotification: DBNotification) {
return new Notification({
id: dbNotification.id,
title: dbNotification.title,
title: dbNotification.title ?? '',
actionType: dbNotification.action_type,
contentType: dbNotification.content_type,
contentId: dbNotification.content_id,
Expand All @@ -190,9 +195,13 @@ export class Notification implements IDoc {
createdAt: new Date(dbNotification.created_at),
modifiedAt: dbNotification.modified_at ? new Date(dbNotification.modified_at) : null,
ownedById: dbNotification.owned_by_id,
isRead: dbNotification.is_read,
isRead: !!dbNotification.is_read,
triggeredBy: dbNotification.triggered_by
? Profile.fromDB(dbNotification.triggered_by)
? {
id: dbNotification.triggered_by.id,
username: dbNotification.triggered_by.username,
photo: null,
}
: undefined,
ownedBy: dbNotification.owned_by ? Profile.fromDB(dbNotification.owned_by) : undefined,
});
Expand Down Expand Up @@ -565,8 +574,8 @@ export type SubscribedUser = {
profile_created_at: string;
email: string;
is_unsubscribed: boolean;
badge_ids: number[];
roles: string[];
badge_ids?: number[];
roles?: string[];
replies: boolean;
comments: boolean;
research_updates: boolean;
Expand Down
Loading
Loading