Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 1x 8x 8x 1x 7x 6x 1x 5x 5x 5x 5x 1x 4x 4x 4x 4x 4x 1x 1x | /**
* GET /v1/conversations/{conversationId}
*
* One conversation as the caller sees it: the other participant, the
* conversation's status and what the caller can do in it right now
* (`sendState`, see utils/directMessages.js's describeSendState) — what the
* thread needs to show a compose box, a "waiting for acceptance" notice, the
* Accept/Decline/Block actions of a request or a block notice. The requester
* of a declined request still sees it as 'pending': they are never told.
*
* @implements UC-DM-007.1, UC-DM-009.1
*/
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const { DynamoDBDocumentClient, GetCommand } = require('@aws-sdk/lib-dynamodb')
const { conversationStatus, getBlockState, describeSendState } = require('../utils/directMessages')
const client = new DynamoDBClient({})
const docClient = DynamoDBDocumentClient.from(client)
const CORS_HEADERS = {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
}
exports.handler = async (event) => {
try {
const userId = event.requestContext?.authorizer?.claims?.sub
if (!userId) {
return {
statusCode: 401,
headers: CORS_HEADERS,
body: JSON.stringify({ error: 'Unauthorized' })
}
}
const conversationId = event.pathParameters?.conversationId
if (!conversationId) {
return {
statusCode: 400,
headers: CORS_HEADERS,
body: JSON.stringify({ error: 'Missing conversationId' })
}
}
const participantRow = await docClient.send(
new GetCommand({
TableName: process.env.CONVERSATION_PARTICIPANTS_TABLE_NAME,
Key: { userId, conversationId }
})
)
if (!participantRow.Item) {
return {
statusCode: 403,
headers: CORS_HEADERS,
body: JSON.stringify({ error: 'You are not a participant in this conversation' })
}
}
const otherUserId = participantRow.Item.otherUserId
const [conversationResult, otherUserResult, blocks] = await Promise.all([
docClient.send(
new GetCommand({
TableName: process.env.CONVERSATIONS_TABLE_NAME,
Key: { id: conversationId }
})
),
docClient.send(
new GetCommand({
TableName: process.env.USERS_TABLE_NAME,
Key: { id: otherUserId }
})
),
getBlockState(docClient, process.env.USER_BLOCKS_TABLE_NAME, userId, otherUserId)
])
const conversation = conversationResult.Item
if (!conversation) {
return {
statusCode: 404,
headers: CORS_HEADERS,
body: JSON.stringify({ error: 'Conversation not found' })
}
}
const isRequester = conversation.requesterId === userId
let status = conversationStatus(conversation)
if (status === 'declined' && isRequester) status = 'pending'
const otherUser = otherUserResult.Item
return {
statusCode: 200,
headers: CORS_HEADERS,
body: JSON.stringify({
conversationId,
otherUserId,
otherUserNickname: otherUser?.nickname || null,
otherUserName: otherUser?.name || null,
otherUserProfilePicture: otherUser?.profilePicture || null,
status,
isRequester,
isBlockedByCurrentUser: blocks.blockedByUser,
sendState: describeSendState(conversation, userId, blocks)
})
}
} catch (error) {
console.error('[conversations/get] Exception:', error.message, error.stack)
return {
statusCode: 500,
headers: CORS_HEADERS,
body: JSON.stringify({ error: 'Failed to fetch conversation' })
}
}
}
|