Skip to content

Commit

Permalink
Prevent new users from sending images & videos in public chats (#5638)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Apr 5, 2024
1 parent 83bbf17 commit 501ee3d
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 7 deletions.
1 change: 1 addition & 0 deletions frontend/app/src/components/register/Register.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
createdUser = {
kind: "created_user",
username,
dateCreated: BigInt(Date.now()),
displayName: undefined,
cryptoAccount: resp.icpAccount,
userId: resp.userId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export const idlFactory = ({ IDL }) => {
const CurrentUserResponse = IDL.Variant({
'Success' : IDL.Record({
'username' : IDL.Text,
'date_created' : TimestampMillis,
'is_platform_operator' : IDL.Bool,
'diamond_membership_status' : DiamondMembershipStatusFull,
'wasm_version' : BuildVersion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ export type Cryptocurrency = { 'InternetComputer' : null } |
export type CurrentUserResponse = {
'Success' : {
'username' : string,
'date_created' : TimestampMillis,
'is_platform_operator' : boolean,
'diamond_membership_status' : DiamondMembershipStatusFull,
'wasm_version' : BuildVersion,
Expand Down
1 change: 1 addition & 0 deletions frontend/openchat-agent/src/services/userIndex/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export function currentUserResponse(candid: ApiCurrentUserResponse): CurrentUser
kind: "created_user",
userId: r.user_id.toString(),
username: r.username,
dateCreated: r.date_created,
displayName: optional(r.display_name, identity),
cryptoAccount: bytesToHexString(r.icp_account),
canisterUpgradeStatus:
Expand Down
2 changes: 1 addition & 1 deletion frontend/openchat-agent/src/utils/caching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import type { CryptocurrencyContent } from "openchat-shared";
import type { PrizeContent } from "openchat-shared";
import type { P2PSwapContent } from "openchat-shared";

const CACHE_VERSION = 99;
const CACHE_VERSION = 100;
const MAX_INDEX = 9999999999;

export type Database = Promise<IDBPDatabase<ChatSchema>>;
Expand Down
4 changes: 2 additions & 2 deletions frontend/openchat-client/src/openchat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1502,7 +1502,7 @@ export class OpenChat extends OpenChatAgentWorker {
return false;
}
} else {
return canSendGroupMessage(chat, mode, permission);
return canSendGroupMessage(this._liveState.user, chat, mode, permission);
}
});
}
Expand All @@ -1523,7 +1523,7 @@ export class OpenChat extends OpenChatAgentWorker {
);
}
} else {
return permittedMessagesInGroup(chat, mode);
return permittedMessagesInGroup(this._liveState.user, chat, mode);
}
}

Expand Down
30 changes: 26 additions & 4 deletions frontend/openchat-client/src/utils/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import type {
OptionalMessagePermissions,
OptionUpdate,
GovernanceProposalsSubtype,
CreatedUser,
} from "openchat-shared";
import {
emptyChatMetrics,
Expand Down Expand Up @@ -1079,32 +1080,42 @@ export function canInviteUsers(chat: ChatSummary): boolean {
}

export function permittedMessagesInGroup(
user: CreatedUser,
chat: MultiUserChat,
mode: "message" | "thread",
): Map<MessagePermission, boolean> {
return new Map(
messagePermissionsList.map((m: MessagePermission) => [
m,
canSendGroupMessage(chat, mode, m),
canSendGroupMessage(user, chat, mode, m),
]),
);
}

const PERMISSIONS_BLOCKED_FOR_NEW_USERS: MessagePermission[] = [
"audio",
"file",
"giphy",
"image",
"video",
];

export function canSendGroupMessage(
user: CreatedUser,
chat: MultiUserChat,
mode: "message" | "thread" | "any",
permission?: MessagePermission,
): boolean {
if (mode === "any") {
return (
canSendGroupMessage(chat, "message", permission) ||
canSendGroupMessage(chat, "thread", permission)
canSendGroupMessage(user, chat, "message", permission) ||
canSendGroupMessage(user, chat, "thread", permission)
);
}

if (permission === undefined) {
return messagePermissionsList.some((mp: MessagePermission) =>
canSendGroupMessage(chat, mode, mp as MessagePermission),
canSendGroupMessage(user, chat, mode, mp as MessagePermission),
);
}

Expand All @@ -1117,6 +1128,17 @@ export function canSendGroupMessage(
return false;
}

if (
chat.public &&
user.diamondStatus.kind === "inactive" &&
PERMISSIONS_BLOCKED_FOR_NEW_USERS.includes(permission)
) {
const isNewUser = Date.now() - Number(user.dateCreated) < 24 * 60 * 60 * 1000; // 1 day
if (isNewUser) {
return false;
}
}

return (
!chat.frozen &&
isPermitted(
Expand Down
2 changes: 2 additions & 0 deletions frontend/openchat-shared/src/domain/user/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export function anonymousUser(): CreatedUser {
return {
kind: "created_user",
username: ANON_USERNAME,
dateCreated: BigInt(0),
displayName: ANON_DISPLAY_NAME, // TODO probably need to translate this
cryptoAccount: "", // TODO - will this be a problem?
userId: ANON_USER_ID,
Expand All @@ -125,6 +126,7 @@ export function anonymousUser(): CreatedUser {
export type CreatedUser = {
kind: "created_user";
username: string;
dateCreated: bigint;
displayName: string | undefined;
cryptoAccount: string;
userId: string;
Expand Down
2 changes: 2 additions & 0 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ then
./scripts/download-all-canister-wasms.sh $WASM_SRC || exit 1
fi

./scripts/download-canister-wasm-dfx.sh event_store

USER_INDEX_CANISTER_ID=$(dfx canister --network $NETWORK id user_index)
GROUP_INDEX_CANISTER_ID=$(dfx canister --network $NETWORK id group_index)
NOTIFICATIONS_INDEX_CANISTER_ID=$(dfx canister --network $NETWORK id notifications_index)
Expand Down

0 comments on commit 501ee3d

Please sign in to comment.