Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor profiles in remaining queries referencing Profiles #8480

Merged
merged 18 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
32 changes: 5 additions & 27 deletions libs/model/src/models/address.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Role, WalletId, WalletSsoSource } from '@hicommonwealth/shared';
import { Address } from '@hicommonwealth/schemas';
import { WalletId } from '@hicommonwealth/shared';
import Sequelize from 'sequelize';
import { z } from 'zod';
import { decrementProfileCount } from '../utils';
import type { CommunityAttributes, CommunityInstance } from './community';
import { MembershipAttributes } from './membership';
Expand All @@ -8,31 +10,7 @@ import type { SsoTokenInstance } from './sso_token';
import type { ModelInstance } from './types';
import type { UserAttributes, UserInstance } from './user';

export type AddressAttributes = {
address: string;
community_id: string;
verification_token: string;
role: Role;
is_user_default: boolean;
id?: number;
verification_token_expires?: Date;
verified?: Date;
keytype?: string;
block_info?: string;
last_active?: Date;
created_at?: Date;
updated_at?: Date;
user_id?: number;
is_councillor?: boolean;
is_validator?: boolean;
ghost_address?: boolean;
// Cosmos self-custodial wallets only.
// hex is derived from bech32 address using bech32ToHex function in
// packages/commonwealth/shared/utils.ts
hex?: string;
profile_id?: number;
wallet_id?: WalletId;
wallet_sso_source?: WalletSsoSource;
export type AddressAttributes = z.infer<typeof Address> & {
// associations
Community?: CommunityAttributes;
Profile?: ProfileAttributes;
Expand Down Expand Up @@ -162,7 +140,7 @@ export default (

await decrementProfileCount(
sequelize.models,
address.community_id,
address.community_id!,
address.user_id!,
options.transaction!,
);
Expand Down
32 changes: 3 additions & 29 deletions libs/model/src/models/comment.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { EventNames, logger, stats } from '@hicommonwealth/core';
import { Comment } from '@hicommonwealth/schemas';
import Sequelize from 'sequelize';
import { fileURLToPath } from 'url';
import { IDiscordMeta } from '../types';
import { z } from 'zod';
import { emitEvent } from '../utils';
import type { AddressAttributes } from './address';
import { CommentSubscriptionAttributes } from './comment_subscriptions';
import type { CommunityAttributes } from './community';
import type { ReactionAttributes } from './reaction';
Expand All @@ -13,38 +13,12 @@ import type { ModelInstance } from './types';
const __filename = fileURLToPath(import.meta.url);
const log = logger(__filename);

export type CommentAttributes = {
thread_id: number;
address_id: number;
text: string;
plaintext: string;
id?: number;
community_id: string;
parent_id?: string;
version_history?: string[];

// canvas-related columns
canvas_signed_data: string;
canvas_hash: string;

created_by: string;
created_at?: Date;
updated_at?: Date;
deleted_at?: Date;
marked_as_spam_at?: Date;
discord_meta?: IDiscordMeta;
version_history_updated?: boolean;

export type CommentAttributes = z.infer<typeof Comment> & {
// associations
Community?: CommunityAttributes;
Address?: AddressAttributes;
Thread?: ThreadAttributes;
reactions?: ReactionAttributes[];
subscriptions?: CommentSubscriptionAttributes[];

//counts
reaction_count: number;
reaction_weights_sum: number;
};

export type CommentInstance = ModelInstance<CommentAttributes>;
Expand Down
6 changes: 5 additions & 1 deletion libs/model/src/tester/e2eSeeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ export const e2eTestEntities = async function (
email: `test${i - 1}@gmail.com`,
emailVerified: true,
isAdmin: true,
profile: {},
profile: {
name: `testName-${i < 2 ? 1 : 2}`,
avatar_url: `testAvatarUrl-${i < 2 ? 1 : 2}`,
email: `test-${i < 2 ? 1 : 2}@gmail.com`,
},
},
})
)[0],
Expand Down
38 changes: 38 additions & 0 deletions libs/schemas/src/entities/comment.schemas.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,43 @@
import { z } from 'zod';
import { PG_INT } from '../utils';
import { Address } from './user.schemas';

export const Comment = z.object({
thread_id: PG_INT,
address_id: PG_INT,
text: z.string(),
plaintext: z.string(),
id: PG_INT.nullish(),
community_id: z.string(),
parent_id: z.string().nullish(),
version_history: z.array(z.string()).optional(),
version_history_updated: z.boolean().optional(),

canvas_signed_data: z.string(),
canvas_hash: z.string(),

created_by: z.string().nullish(),
created_at: z.any(),
updated_at: z.any(),
deleted_at: z.any(),
marked_as_spam_at: z.any(),

discord_meta: z
.object({
user: z.object({
id: z.string(),
username: z.string(),
}),
channel_id: z.string(),
message_id: z.string(),
})
.nullish(),

reaction_count: PG_INT,
reaction_weights_sum: PG_INT.optional(),

Address: Address.nullish(),
});

export const CommentVersionHistory = z.object({
id: PG_INT.optional(),
Expand Down
35 changes: 0 additions & 35 deletions libs/schemas/src/entities/thread.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,41 +47,6 @@ export const Thread = z.object({
profile_name: z.string().nullish(),
});

export const Comment = z.object({
thread_id: PG_INT,
address_id: PG_INT,
text: z.string(),
plaintext: z.string(),
id: PG_INT.optional(),
community_id: z.string(),
parent_id: z.string().nullish(),
version_history: z.array(z.string()).optional(),

canvas_signed_data: z.string(),
canvas_hash: z.string(),

created_at: z.any(),
updated_at: z.any(),
deleted_at: z.any(),
marked_as_spam_at: z.any(),

discord_meta: z
.object({
user: z.object({
id: z.string(),
username: z.string(),
}),
channel_id: z.string(),
message_id: z.string(),
})
.nullish(),

reaction_count: PG_INT,
reaction_weights_sum: PG_INT.optional(),

Address: Address.optional(),
});

export const ThreadVersionHistory = z.object({
id: PG_INT.optional(),
thread_id: PG_INT,
Expand Down
7 changes: 4 additions & 3 deletions libs/schemas/src/entities/user.schemas.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Roles, WalletId, WalletSsoSource } from '@hicommonwealth/shared';
import { z } from 'zod';
import { PG_INT } from '../utils';

Expand Down Expand Up @@ -66,11 +67,11 @@ export const Address = z.object({
is_validator: z.boolean().optional(),
ghost_address: z.boolean().optional(),
profile_id: PG_INT.nullish().optional(),
wallet_id: z.string().max(255).optional(),
wallet_id: z.nativeEnum(WalletId).optional(),
block_info: z.string().max(255).optional(),
is_user_default: z.boolean().optional(),
role: z.enum(['member', 'admin', 'moderator']).default('member'),
wallet_sso_source: z.string().max(255).optional(),
role: z.enum(Roles).default('member'),
wallet_sso_source: z.nativeEnum(WalletSsoSource).optional(),
hex: z.string().max(64).optional(),
created_at: z.any(),
updated_at: z.any(),
Expand Down
1 change: 1 addition & 0 deletions libs/schemas/src/queries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './feed.schemas';
export * from './pagination';
export * from './subscription.schemas';
export * from './thread.schemas';
export * from './user.schemas';
33 changes: 33 additions & 0 deletions libs/schemas/src/queries/user.schemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { z } from 'zod';
import { Comment } from '../entities/comment.schemas';
import { Tag } from '../entities/tag.schemas';
import { Thread } from '../entities/thread.schemas';
import { Address, UserProfile } from '../entities/user.schemas';

export const GetNewProfileReq = z.object({
profileId: z.string().optional(),
});

export const GetNewProfileResp = z.object({
profile: UserProfile,
totalUpvotes: z.number().int(),
addresses: z.array(Address),
threads: z.array(Thread),
comments: z.array(Comment),
commentThreads: z.array(Thread),
isOwner: z.boolean(),
tags: z.array(Tag),
});

export const GetAddressProfileReq = z.object({
addresses: z.array(z.string()),
communities: z.array(z.string()),
});

export const GetAddressProfileResp = z.object({
profileId: z.number(),
name: z.string(),
address: z.string(),
lastActive: z.date(),
avatarUrl: z.string().optional(),
});
3 changes: 2 additions & 1 deletion libs/shared/src/types/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ThresholdData } from './protocol';

export type Role = 'admin' | 'moderator' | 'member';
export const Roles = ['admin', 'moderator', 'member'] as const;
export type Role = typeof Roles[number];

export enum DefaultPage {
Discussions = 'default_all_discussions_view',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ export type UserProfile = {
};

export function addressToUserProfile(address): UserProfile {
const profile = address?.User?.Profiles[0];
const profile = address?.User?.profile;
if (!profile) {
// @ts-expect-error <StrictNullChecks/>
return undefined;
}

return {
id: profile.id,
id: address.profile_id,
avatarUrl: profile?.avatar_url,
name: profile?.profile_name || profile?.name,
name: profile?.name,
address: address?.address,
lastActive: address?.last_active,
};
Expand Down
23 changes: 12 additions & 11 deletions packages/commonwealth/client/scripts/models/NewProfile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Image } from '../views/components/EditProfile/EditProfile';

import { Image } from '@hicommonwealth/schemas';
import { z } from 'zod';
import { MappedProfile } from '../state/api/profiles/fetchProfileById';
class NewProfile {
private _name: string;
private _email: string;
Expand All @@ -10,7 +11,7 @@ class NewProfile {
private _socials: string[];
private _id: number;
private _isOwner: boolean;
private _backgroundImage: Image;
private _backgroundImage: z.infer<typeof Image>;

get name() {
return this._name;
Expand Down Expand Up @@ -63,17 +64,17 @@ class NewProfile {
id,
is_owner,
background_image,
}) {
}: MappedProfile) {
this._name = profile_name;
this._email = email;
this._website = website;
this._bio = bio;
this._avatarUrl = avatar_url;
this._slug = slug;
this._socials = socials;
this._email = email!;
this._website = website!;
this._bio = bio!;
this._avatarUrl = avatar_url!;
this._slug = slug!;
this._socials = socials!;
this._id = id;
this._isOwner = is_owner;
this._backgroundImage = background_image;
this._backgroundImage = background_image ?? { url: '', imageBehavior: '' };
}

public static fromJSON(json) {
Expand Down
Loading
Loading