Skip to content

Commit

Permalink
Merge branch 'master' into rotorsoft/9051-update-community
Browse files Browse the repository at this point in the history
  • Loading branch information
rotorsoft committed Sep 9, 2024
2 parents e2908a8 + c081ad5 commit 93a2082
Show file tree
Hide file tree
Showing 49 changed files with 896 additions and 398 deletions.
18 changes: 4 additions & 14 deletions libs/model/src/comment/CreateComment.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as schemas from '@hicommonwealth/schemas';
import { models } from '../database';
import { isAuthorized, type AuthContext } from '../middleware';
import { verifyCommentSignature } from '../middleware/canvas';
import { mustExist } from '../middleware/guards';
import { mustBeAuthorizedThread, mustExist } from '../middleware/guards';
import {
emitEvent,
emitMentions,
Expand Down Expand Up @@ -32,25 +32,15 @@ export function CreateComment(): Command<
isAuthorized({ action: schemas.PermissionEnum.CREATE_COMMENT }),
verifyCommentSignature,
],
body: async ({ actor, payload }) => {
const { thread_id, parent_id, ...rest } = payload;
body: async ({ actor, payload, auth }) => {
const { address, thread } = mustBeAuthorizedThread(actor, auth);

const thread = await models.Thread.findOne({ where: { id: thread_id } });
mustExist('Thread', thread);
if (thread.read_only)
throw new InvalidState(CreateCommentErrors.CantCommentOnReadOnly);
if (thread.archived_at)
throw new InvalidState(CreateCommentErrors.ThreadArchived);

const address = await models.Address.findOne({
where: {
community_id: thread.community_id,
user_id: actor.user.id,
address: actor.address,
},
});
mustExist('Community address', address);

const { thread_id, parent_id, ...rest } = payload;
if (parent_id) {
const parent = await models.Comment.findOne({
where: { id: parent_id, thread_id },
Expand Down
24 changes: 3 additions & 21 deletions libs/model/src/comment/CreateCommentReaction.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as schemas from '@hicommonwealth/schemas';
import { models } from '../database';
import { isAuthorized, type AuthContext } from '../middleware';
import { verifyReactionSignature } from '../middleware/canvas';
import { mustExist } from '../middleware/guards';
import { mustBeAuthorizedComment } from '../middleware/guards';
import { getVotingWeight } from '../services/stakeHelper';

export function CreateCommentReaction(): Command<
Expand All @@ -16,27 +16,9 @@ export function CreateCommentReaction(): Command<
isAuthorized({ action: schemas.PermissionEnum.CREATE_COMMENT_REACTION }),
verifyReactionSignature,
],
body: async ({ actor, payload }) => {
const comment = await models.Comment.findOne({
where: { id: payload.comment_id },
include: [
{
model: models.Thread,
required: true,
},
],
});
mustExist('Comment', comment);

body: async ({ payload, actor, auth }) => {
const { address, comment } = mustBeAuthorizedComment(actor, auth);
const thread = comment.Thread!;
const address = await models.Address.findOne({
where: {
community_id: thread.community_id,
user_id: actor.user.id,
address: actor.address,
},
});
mustExist('Community address', address);

const calculated_voting_weight = await getVotingWeight(
thread.community_id,
Expand Down
21 changes: 7 additions & 14 deletions libs/model/src/comment/UpdateComment.command.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { type Command } from '@hicommonwealth/core';
import { InvalidInput, type Command } from '@hicommonwealth/core';
import * as schemas from '@hicommonwealth/schemas';
import { models } from '../database';
import { isAuthorized, type AuthContext } from '../middleware';
import { mustExist } from '../middleware/guards';
import { mustBeAuthorized } from '../middleware/guards';
import {
emitMentions,
findMentionDiff,
Expand All @@ -19,31 +19,24 @@ export function UpdateComment(): Command<
return {
...schemas.UpdateComment,
auth: [isAuthorized({})],
body: async ({ actor, payload }) => {
body: async ({ actor, payload, auth }) => {
const { address } = mustBeAuthorized(actor, auth);
const { comment_id, discord_meta } = payload;

// find by comment_id or discord_meta
const comment = await models.Comment.findOne({
where: comment_id ? { id: comment_id } : { discord_meta },
include: [{ model: models.Thread, required: true }],
});
mustExist('Comment', comment);
const thread = comment.Thread!;
if (!comment) throw new InvalidInput('Comment not found');

const thread = comment.Thread!;
const currentVersion = await models.CommentVersionHistory.findOne({
where: { comment_id: comment.id },
order: [['timestamp', 'DESC']],
});

if (currentVersion?.text !== payload.text) {
const address = await models.Address.findOne({
where: {
community_id: thread.community_id,
user_id: actor.user.id,
address: actor.address,
},
});
mustExist('Community address', address);

const text = sanitizeQuillText(payload.text);
const plaintext = quillToPlain(text);
const mentions = findMentionDiff(
Expand Down
40 changes: 40 additions & 0 deletions libs/model/src/middleware/guards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
InvalidState,
logger,
} from '@hicommonwealth/core';
import { AddressInstance, ThreadInstance } from '../models';
import { AuthContext } from './authorization';

const log = logger(import.meta);

Expand Down Expand Up @@ -56,3 +58,41 @@ export function mustBeSuperAdmin(actor: Actor) {
if (!actor.user.isAdmin)
throw new InvalidActor(actor, 'Must be super administrator');
}

/**
* Address authorization guard
* @param auth auth context
* @returns narrowed auth context
*/
export function mustBeAuthorized(actor: Actor, auth?: AuthContext) {
if (!auth?.address) throw new InvalidActor(actor, 'Not authorized');
return auth as AuthContext & { address: AddressInstance };
}

/**
* Thread authorization guard
* @param auth auth context
* @returns narrowed auth context
*/
export function mustBeAuthorizedThread(actor: Actor, auth?: AuthContext) {
if (!auth?.address) throw new InvalidActor(actor, 'Not authorized');
if (!auth?.thread) throw new InvalidActor(actor, 'Not authorized thread');
return auth as AuthContext & {
address: AddressInstance;
thread: ThreadInstance;
};
}

/**
* Comment authorization guard
* @param auth auth context
* @returns narrowed auth context
*/
export function mustBeAuthorizedComment(actor: Actor, auth?: AuthContext) {
if (!auth?.address) throw new InvalidActor(actor, 'Not authorized');
if (!auth?.comment) throw new InvalidActor(actor, 'Not authorized comment');
return auth as AuthContext & {
address: AddressInstance;
comment: ThreadInstance;
};
}
16 changes: 4 additions & 12 deletions libs/model/src/thread/CreateThread.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { GetActiveContestManagers } from '../contest';
import { models } from '../database';
import { isAuthorized, type AuthContext } from '../middleware';
import { verifyThreadSignature } from '../middleware/canvas';
import { mustExist } from '../middleware/guards';
import { mustBeAuthorized } from '../middleware/guards';
import { tokenBalanceCache } from '../services';
import {
emitMentions,
Expand Down Expand Up @@ -90,7 +90,9 @@ export function CreateThread(): Command<
isAuthorized({ action: schemas.PermissionEnum.CREATE_THREAD }),
verifyThreadSignature,
],
body: async ({ actor, payload }) => {
body: async ({ actor, payload, auth }) => {
const { address } = mustBeAuthorized(actor, auth);

const { community_id, topic_id, kind, url, ...rest } = payload;

if (kind === 'link' && !url?.trim())
Expand All @@ -109,16 +111,6 @@ export function CreateThread(): Command<
checkContestLimits(activeContestManagers, actor.address!);
}

// Loading to update last_active
const address = await models.Address.findOne({
where: {
user_id: actor.user.id,
community_id,
address: actor.address,
},
});
mustExist('Community address', address);

const body = sanitizeQuillText(payload.body);
const plaintext = kind === 'discussion' ? quillToPlain(body) : body;
const mentions = uniqueMentions(parseUserMentions(body));
Expand Down
19 changes: 4 additions & 15 deletions libs/model/src/thread/CreateThreadReaction.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as schemas from '@hicommonwealth/schemas';
import { models } from '../database';
import { isAuthorized, type AuthContext } from '../middleware';
import { verifyReactionSignature } from '../middleware/canvas';
import { mustExist } from '../middleware/guards';
import { mustBeAuthorizedThread } from '../middleware/guards';
import { getVotingWeight } from '../services/stakeHelper';

export const CreateThreadReactionErrors = {
Expand All @@ -22,23 +22,12 @@ export function CreateThreadReaction(): Command<
}),
verifyReactionSignature,
],
body: async ({ actor, payload }) => {
const thread = await models.Thread.findOne({
where: { id: payload.thread_id },
});
mustExist('Thread', thread);
body: async ({ payload, actor, auth }) => {
const { address, thread } = mustBeAuthorizedThread(actor, auth);

if (thread.archived_at)
throw new InvalidState(CreateThreadReactionErrors.ThreadArchived);

const address = await models.Address.findOne({
where: {
user_id: actor.user.id,
community_id: thread.community_id,
address: actor.address,
},
});
mustExist('Community address', address);

const calculated_voting_weight = await getVotingWeight(
thread.community_id,
address.address,
Expand Down
3 changes: 3 additions & 0 deletions libs/schemas/src/commands/comment.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export const UpdateComment = {
input: z.object({
comment_id: PG_INT,
text: z.string().trim().min(1),

// discord integration
thread_id: PG_INT.optional(),
discord_meta: DiscordMetaSchema.optional(),
}),
output: Comment.extend({ community_id: z.string() }),
Expand Down
24 changes: 24 additions & 0 deletions libs/shared/src/commonProtocol/chainConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ export enum ValidChains {
SepoliaBase = 84532,
Sepolia = 11155111,
Blast = 81457,
Linea = 59144,
Optimism = 10,
Mainnet = 1,
Arbitrum = 42161,
}

export const STAKE_ID = 2;
Expand Down Expand Up @@ -39,4 +43,24 @@ export const factoryContracts: {
communityStake: '0xcc752fd15A7Dd0d5301b6A626316E7211352Cf62',
chainId: 8453,
},
[ValidChains.Linea]: {
factory: '0xe3ae9569f4523161742414480f87967e991741bd',
communityStake: '0xcc752fd15a7dd0d5301b6a626316e7211352cf62',
chainId: 59144,
},
[ValidChains.Optimism]: {
factory: '0xe3ae9569f4523161742414480f87967e991741bd',
communityStake: '0xcc752fd15a7dd0d5301b6a626316e7211352cf62',
chainId: 10,
},
[ValidChains.Mainnet]: {
factory: '0x90aa47bf6e754f69ee53f05b5187b320e3118b0f',
communityStake: '0x9ed281e62db1b1d98af90106974891a4c1ca3a47',
chainId: 1,
},
[ValidChains.Arbitrum]: {
factory: '0xE3AE9569f4523161742414480f87967e991741bd',
communityStake: '0xcc752fd15A7Dd0d5301b6A626316E7211352Cf62',
chainId: 42161,
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const featureFlags = {
process.env.FLAG_KNOCK_PUSH_NOTIFICATIONS_ENABLED,
),
farcasterContest: buildFlag(process.env.FLAG_FARCASTER_CONTEST),
newEditor: buildFlag(process.env.FLAG_NEW_EDITOR),
};

export type AvailableFeatureFlag = keyof typeof featureFlags;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Route } from 'react-router-dom';
import { withLayout } from 'views/Layout';
import { RouteFeatureFlags } from './Router';

const EditorPage = lazy(() => import('views/pages/Editor'));
const EditorPage = lazy(() => import('views/pages/EditorPage'));

const DashboardPage = lazy(() => import('views/pages/user_dashboard'));
const CommunitiesPage = lazy(() => import('views/pages/Communities'));
Expand Down Expand Up @@ -113,11 +113,7 @@ const CommonDomainRoutes = ({
contestEnabled,
farcasterContestEnabled,
}: RouteFeatureFlags) => [
<Route
key="/editor"
path="/editor"
element={<EditorPage imageHandler="local" />}
/>,
<Route key="/editor" path="/editor" element={<EditorPage />} />,

<Route
key="/"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
display: flex;
padding: 8px;

user-select: none;

.Item {
display: flex;
margin-top: auto;
Expand Down
Loading

0 comments on commit 93a2082

Please sign in to comment.