diff --git a/src/adapters/db.ts b/src/adapters/db.ts index a940be3..7e96d47 100644 --- a/src/adapters/db.ts +++ b/src/adapters/db.ts @@ -32,9 +32,9 @@ export function createDBComponent(components: Pick let query: SQLStatement if (onlyActive) { - query = SQL`SELECT * FROM friendships WHERE (LOWER(address_requester) = LOWER(${userAddress}) OR LOWER(address_requester) = LOWER(${userAddress})) AND is_active = true` + query = SQL`SELECT * FROM friendships WHERE (address_requester = ${userAddress} OR address_requested = ${userAddress}) AND is_active = true` } else { - query = SQL`SELECT * FROM friendships WHERE (LOWER(address_requester) = LOWER(${userAddress}) OR LOWER(address_requester) = LOWER(${userAddress}))` + query = SQL`SELECT * FROM friendships WHERE (address_requester = ${userAddress} OR address_requested = ${userAddress})` } const generator = pg.streamQuery(query) @@ -46,7 +46,7 @@ export function createDBComponent(components: Pick SQL`WITH friendsA as ( SELECT CASE - WHEN LOWER(address_requester) = LOWER(${userAddress1}) then address_requested + WHEN address_requester = ${userAddress1} then address_requested else address_requester end as address FROM @@ -57,8 +57,8 @@ export function createDBComponent(components: Pick friendships f_a where ( - LOWER(f_a.address_requester) = LOWER(${userAddress1}) - or LOWER(f_a.address_requested) = LOWER(${userAddress1}) + f_a.address_requester = ${userAddress1} + or f_a.address_requested = ${userAddress1} ) and f_a.is_active = true ) as friends_a ) @@ -70,7 +70,7 @@ export function createDBComponent(components: Pick address IN ( SELECT CASE - WHEN LOWER(address_requester) = LOWER(${userAddress2}) then address_requested + WHEN address_requester = ${userAddress2} then address_requested else address_requester end as address_a FROM @@ -81,8 +81,8 @@ export function createDBComponent(components: Pick friendships f_b where ( - LOWER(f_b.address_requester) = LOWER(${userAddress2}) - or LOWER(f_b.address_requested) = LOWER(${userAddress2}) + f_b.address_requester = ${userAddress2} + or f_b.address_requested = ${userAddress2} ) and f_b.is_active = true ) as friends_b );` @@ -95,9 +95,9 @@ export function createDBComponent(components: Pick const query = SQL` SELECT * FROM friendships WHERE - (LOWER(address_requester) = LOWER(${userAddress1}) AND LOWER(address_requested) = LOWER(${userAddress2})) + (address_requester = ${userAddress1} AND address_requested = ${userAddress2}) OR - (LOWER(address_requester) = LOWER(${userAddress2}) AND LOWER(address_requested) = LOWER(${userAddress1})) + (address_requester = ${userAddress2} AND address_requested = ${userAddress1}) ` const results = await pg.query(query) diff --git a/src/adapters/rpcServer.ts b/src/adapters/rpcServer.ts index a05bb2f..908a2e9 100644 --- a/src/adapters/rpcServer.ts +++ b/src/adapters/rpcServer.ts @@ -18,6 +18,7 @@ import { validateNewFriendshipAction } from '../logic/friendships' import emitterToAsyncGenerator from '../utils/emitterToGenerator' +import { normalizeAddress } from '../utils/address' export type IRPCServerComponent = IBaseComponent & { attachUser(user: { transport: Transport; address: string }): void @@ -89,7 +90,7 @@ export default async function createRpcServerComponent( logger.debug(`getting mutual friends ${context.address}<>${request.user!.address}`) let mutualFriends: AsyncGenerator<{ address: string }> | undefined try { - mutualFriends = db.getMutualFriends(context.address, request.user!.address) + mutualFriends = db.getMutualFriends(context.address, normalizeAddress(request.user!.address)) } catch (error) { logger.error(error as any) // throw an error bc there is no sense to create a generator to send an error diff --git a/src/controllers/handlers/ws-handler.ts b/src/controllers/handlers/ws-handler.ts index 2dd140c..abb3dac 100644 --- a/src/controllers/handlers/ws-handler.ts +++ b/src/controllers/handlers/ws-handler.ts @@ -5,6 +5,7 @@ import { WebSocketTransport } from '@dcl/rpc/dist/transports/WebSocket' import future from 'fp-future' import { verify } from '@dcl/platform-crypto-middleware' import { GlobalContext } from '../../types' +import { normalizeAddress } from '../../utils/address' export async function wsHandler(context: IHttpServerComponent.DefaultContext) { const { logs, rpcServer, fetcher } = context.components @@ -57,7 +58,7 @@ export async function wsHandler(context: IHttpServerComponent.DefaultContext ', { address: authchainVerifyResult.auth }) - const address = authchainVerifyResult.auth.toLowerCase() + const address = normalizeAddress(authchainVerifyResult.auth) rpcServer.attachUser({ transport: wsTransport, address }) diff --git a/src/logic/friendships.ts b/src/logic/friendships.ts index 828d432..6e0320c 100644 --- a/src/logic/friendships.ts +++ b/src/logic/friendships.ts @@ -9,6 +9,7 @@ import { FriendshipStatus, SubscriptionEventsEmitter } from '../types' +import { normalizeAddress } from '../utils/address' export function isFriendshipActionValid(from: Action | null, to: Action) { return FRIENDSHIP_ACTION_TRANSITIONS[to].includes(from) @@ -79,27 +80,27 @@ export function parseUpsertFriendshipRequest(request: UpsertFriendshipPayload): case 'accept': return { action: Action.ACCEPT, - user: request.action.accept.user!.address + user: normalizeAddress(request.action.accept.user!.address) } case 'cancel': return { action: Action.CANCEL, - user: request.action.cancel.user!.address + user: normalizeAddress(request.action.cancel.user!.address) } case 'delete': return { action: Action.DELETE, - user: request.action.delete.user!.address + user: normalizeAddress(request.action.delete.user!.address) } case 'reject': return { action: Action.REJECT, - user: request.action.reject.user!.address + user: normalizeAddress(request.action.reject.user!.address) } case 'request': return { action: Action.REQUEST, - user: request.action.request.user!.address, + user: normalizeAddress(request.action.request.user!.address), metadata: request.action.request.message ? { message: request.action.request.message } : null } default: diff --git a/src/utils/address.ts b/src/utils/address.ts new file mode 100644 index 0000000..31abf34 --- /dev/null +++ b/src/utils/address.ts @@ -0,0 +1,3 @@ +export function normalizeAddress(address: string) { + return address.toLowerCase() +}