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

Tracks address activity in middleware #9191

Merged
merged 3 commits into from
Sep 16, 2024
Merged
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
8 changes: 0 additions & 8 deletions common_knowledge/Complex-Functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,6 @@ export async function __updateComment(
});
```

`Repository`

```typescript
// update address last active
address.last_active = new Date();
address.save();
```

`Schemas`

```typescript
Expand Down
4 changes: 0 additions & 4 deletions libs/model/src/comment/CreateComment.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,6 @@ export function CreateComment(): Command<
},
);

// update timestamps
address.last_active = new Date();
await address.save({ transaction });

thread.last_commented_on = new Date();
await thread.save({ transaction });

Expand Down
3 changes: 0 additions & 3 deletions libs/model/src/comment/CreateCommentReaction.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ export function CreateCommentReaction(): Command<
transaction,
});

address.last_active = new Date();
await address.save({ transaction });

return reaction.id;
},
);
Expand Down
4 changes: 0 additions & 4 deletions libs/model/src/comment/UpdateComment.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ export function UpdateComment(): Command<
{ transaction },
);

// update timestamps
address.last_active = new Date();
await address.save({ transaction });

mentions.length &&
(await emitMentions(models, transaction, {
authorAddressId: address.id!,
Expand Down
6 changes: 1 addition & 5 deletions libs/model/src/community/UpdateCommunity.command.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { InvalidInput, type Command } from '@hicommonwealth/core';
import * as schemas from '@hicommonwealth/schemas';
import { ALL_COMMUNITIES, ChainBase } from '@hicommonwealth/shared';
import { ChainBase } from '@hicommonwealth/shared';
import { models } from '../database';
import { AuthContext, isAuthorized } from '../middleware';
import { mustExist } from '../middleware/guards';
import { checkSnapshotObjectExists, commonProtocol } from '../services';

export const UpdateCommunityErrors = {
ReservedId: 'The id is reserved and cannot be used',
NotAdmin: 'Not an admin',
SnapshotOnlyOnEthereum:
'Snapshot data may only be added to chains with Ethereum base',
Expand Down Expand Up @@ -47,9 +46,6 @@ export function UpdateCommunity(): Command<
transactionHash,
} = payload;

if (id === ALL_COMMUNITIES)
throw new InvalidInput(UpdateCommunityErrors.ReservedId);

const community = await models.Community.findOne({
where: { id },
include: [
Expand Down
5 changes: 5 additions & 0 deletions libs/model/src/middleware/authorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ async function buildAuth(
throw new InvalidActor(actor, `User is not ${roles} in the community`);

auth.is_author = auth.address!.id === auth.author_address_id;

// fire and forget address activity tracking
auth.address.last_active = new Date();
void auth.address.save();

return auth;
}

Expand Down
3 changes: 0 additions & 3 deletions libs/model/src/thread/CreateThread.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,6 @@ export function CreateThread(): Command<
},
);

address.last_active = new Date();
await address.save({ transaction });

await models.ThreadSubscription.create(
{
user_id: actor.user.id!,
Expand Down
3 changes: 0 additions & 3 deletions libs/model/src/thread/CreateThreadReaction.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ export function CreateThreadReaction(): Command<
transaction,
});

address.last_active = new Date();
await address.save({ transaction });

return reaction.id;
},
);
Expand Down
16 changes: 1 addition & 15 deletions libs/model/test/community/community-lifecycle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
dispose,
query,
} from '@hicommonwealth/core';
import { ALL_COMMUNITIES, ChainBase, ChainType } from '@hicommonwealth/shared';
import { ChainBase, ChainType } from '@hicommonwealth/shared';
import { Chance } from 'chance';
import { afterAll, assert, beforeAll, describe, expect, test } from 'vitest';
import {
Expand Down Expand Up @@ -241,20 +241,6 @@ describe('Community lifecycle', () => {
assert.equal(updated?.type, 'chain');
});

test('should throw if id is reserved', async () => {
await expect(() =>
command(UpdateCommunity(), {
actor: superAdminActor,
payload: {
...baseRequest,
id: ALL_COMMUNITIES,
namespace: 'tempNamespace',
chain_node_id: 1263,
},
}),
).rejects.toThrow();
});

test('should throw if snapshot not found', async () => {
await expect(() =>
command(UpdateCommunity(), {
Expand Down
10 changes: 10 additions & 0 deletions libs/schemas/src/commands/community.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ export const UpdateCommunity = {
.partial()
.extend({
id: z.string(),
name: z
.string()
.max(255)
.regex(COMMUNITY_NAME_REGEX, {
message: COMMUNITY_NAME_ERROR,
})
.refine((data) => !data.includes(ALL_COMMUNITIES), {
message: `String must not contain '${ALL_COMMUNITIES}'`,
})
.optional(),
featuredTopics: z.array(z.string()).optional(),
snapshot: Snapshot.or(z.array(Snapshot)).optional(),
transactionHash: z.string().optional(),
Expand Down
Loading