diff --git a/keyserver/src/creators/farcaster-channel-tag-creator.js b/keyserver/src/creators/farcaster-channel-tag-creator.js index 3144b832f..15f68bd43 100644 --- a/keyserver/src/creators/farcaster-channel-tag-creator.js +++ b/keyserver/src/creators/farcaster-channel-tag-creator.js @@ -1,163 +1,159 @@ // @flow import uuid from 'uuid'; import { DISABLE_TAGGING_FARCASTER_CHANNEL, farcasterChannelTagBlobHash, } from 'lib/shared/community-utils.js'; import type { CreateOrUpdateFarcasterChannelTagRequest, CreateOrUpdateFarcasterChannelTagResponse, } from 'lib/types/community-types.js'; import { ServerError } from 'lib/utils/errors.js'; import { dbQuery, SQL, MYSQL_DUPLICATE_ENTRY_FOR_KEY_ERROR_CODE, } from '../database/database.js'; import { fetchCommunityInfos } from '../fetchers/community-fetchers.js'; import { uploadBlob, assignHolder, download, deleteBlob, type BlobOperationResult, type BlobDownloadResult, } from '../services/blob.js'; import { Viewer } from '../session/viewer.js'; async function createOrUpdateFarcasterChannelTag( viewer: Viewer, request: CreateOrUpdateFarcasterChannelTagRequest, ): Promise { if (DISABLE_TAGGING_FARCASTER_CHANNEL) { throw new ServerError('internal_error'); } - const [serverCommunityInfos, blobDownload] = await Promise.all([ - fetchCommunityInfos(viewer), + const [communityInfos, blobDownload] = await Promise.all([ + fetchCommunityInfos(viewer, [request.commCommunityID]), getFarcasterChannelTagBlob(request.farcasterChannelID), ]); - const communityInfo = serverCommunityInfos.find( - community => community.id === request.commCommunityID, - ); - - if (!communityInfo) { + if (communityInfos.length !== 1) { throw new ServerError('invalid_parameters'); } if (blobDownload.found) { throw new ServerError('already_in_use'); } const blobHolder = uuid.v4(); const blobResult = await uploadFarcasterChannelTagBlob( request.commCommunityID, request.farcasterChannelID, blobHolder, ); if (!blobResult.success) { if (blobResult.reason === 'HASH_IN_USE') { throw new ServerError('already_in_use'); } else { throw new ServerError('unknown_error'); } } const query = SQL` START TRANSACTION; SELECT farcaster_channel_id, blob_holder INTO @currentFarcasterChannelID, @currentBlobHolder FROM communities WHERE id = ${request.commCommunityID} FOR UPDATE; UPDATE communities SET farcaster_channel_id = ${request.farcasterChannelID}, blob_holder = ${blobHolder} WHERE id = ${request.commCommunityID}; COMMIT; SELECT @currentFarcasterChannelID AS oldFarcasterChannelID, @currentBlobHolder AS oldBlobHolder; `; try { const [transactionResult] = await dbQuery(query, { multipleStatements: true, }); const selectResult = transactionResult.pop(); const [{ oldFarcasterChannelID, oldBlobHolder }] = selectResult; if (oldFarcasterChannelID && oldBlobHolder) { await deleteBlob( { hash: farcasterChannelTagBlobHash(oldFarcasterChannelID), holder: oldBlobHolder, }, true, ); } } catch (error) { await deleteBlob( { hash: farcasterChannelTagBlobHash(request.farcasterChannelID), holder: blobHolder, }, true, ); if (error.errno === MYSQL_DUPLICATE_ENTRY_FOR_KEY_ERROR_CODE) { throw new ServerError('already_in_use'); } throw new ServerError('invalid_parameters'); } return { commCommunityID: request.commCommunityID, farcasterChannelID: request.farcasterChannelID, }; } function getFarcasterChannelTagBlob( secret: string, ): Promise { const hash = farcasterChannelTagBlobHash(secret); return download(hash); } async function uploadFarcasterChannelTagBlob( commCommunityID: string, farcasterChannelID: string, holder: string, ): Promise { const payload = { commCommunityID, farcasterChannelID, }; const payloadString = JSON.stringify(payload); const hash = farcasterChannelTagBlobHash(farcasterChannelID); const blob = new Blob([payloadString]); const uploadResult = await uploadBlob(blob, hash); if (!uploadResult.success) { return uploadResult; } return await assignHolder({ holder, hash }); } export { createOrUpdateFarcasterChannelTag, uploadFarcasterChannelTagBlob }; diff --git a/keyserver/src/fetchers/community-fetchers.js b/keyserver/src/fetchers/community-fetchers.js index 9fb65ac39..92582afdd 100644 --- a/keyserver/src/fetchers/community-fetchers.js +++ b/keyserver/src/fetchers/community-fetchers.js @@ -1,40 +1,47 @@ // @flow import type { ServerCommunityInfo } from 'lib/types/community-types.js'; import { dbQuery, SQL } from '../database/database.js'; import { Viewer } from '../session/viewer.js'; type ServerCommunityInfoWithHolder = $ReadOnly<{ ...ServerCommunityInfo, +blobHolder: ?string, }>; async function fetchCommunityInfos( viewer: Viewer, + communityIDs?: $ReadOnlyArray, ): Promise<$ReadOnlyArray> { if (!viewer.loggedIn) { return []; } const query = SQL` SELECT c.id, c.farcaster_channel_id as farcasterChannelID, c.blob_holder as blobHolder FROM communities c INNER JOIN memberships m ON c.id = m.thread AND m.user = ${viewer.userID} WHERE m.role > 0 `; + if (communityIDs && communityIDs.length > 0) { + query.append(SQL` + AND c.id IN (${communityIDs}) + `); + } + const [result] = await dbQuery(query); const communityInfos = result.map(row => ({ id: row.id.toString(), farcasterChannelID: row.farcasterChannelID, blobHolder: row.blobHolder, })); return communityInfos; } export { fetchCommunityInfos };