Skip to content

Commit

Permalink
🐛 fix: use a URL builder for invitation links
Browse files Browse the repository at this point in the history
  • Loading branch information
rezk2ll committed Jan 14, 2025
1 parent b54e838 commit 7d3c65f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
7 changes: 4 additions & 3 deletions packages/tom-server/src/invitation-api/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from '../types'
import { v7 as uuidv7 } from 'uuid'
import { PATH } from '../routes'
import { buildUrl } from '../../utils'

export default class InvitationService implements IInvitationService {
private readonly EXPIRATION = 24 * 60 * 60 * 1000 // 24 hours
Expand Down Expand Up @@ -110,7 +111,7 @@ export default class InvitationService implements IInvitationService {
await this._storeMatrixInvite(payload, authorization, room_id)
const token = await this._createInvitation(payload)

return `${this.config.base_url}${PATH}/${token}`
return buildUrl(this.config.base_url, `${PATH}/${token}`)
} catch (error) {
this.logger.error(`Failed to generate invitation link`, { error })

Expand Down Expand Up @@ -156,7 +157,7 @@ export default class InvitationService implements IInvitationService {
): Promise<string> => {
try {
const response = await fetch(
`https://${this.config.matrix_server}${this.MATRIX_ROOM_PATH}`,
buildUrl(this.config.matrix_server, this.MATRIX_ROOM_PATH),
{
method: 'POST',
headers: {
Expand Down Expand Up @@ -252,7 +253,7 @@ export default class InvitationService implements IInvitationService {
) => {
try {
await fetch(
`https://${this.config.matrix_server}/${this.MATRIX_INVITE_PATH}`,
buildUrl(this.config.base_url, this.MATRIX_INVITE_PATH),
{
method: 'POST',
headers: {
Expand Down
24 changes: 24 additions & 0 deletions packages/tom-server/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,27 @@ export const tables = {
invitations:
'id varchar(64) PRIMARY KEY, sender varchar(64), recepient varchar(64), medium varchar(64), expiration varchar(64), accessed int, room_id varchar(64)'
}

/**
* Builds a URL from a base URL and a path
*
* @param {string} base - Base URL
* @param {string} path - Path
* @returns {string} - URL
*/
export const buildUrl = (base: string, path: string): string => {
let formattedUrl = base

if (
!formattedUrl.startsWith('https://') &&
!formattedUrl.startsWith('http://')
) {
formattedUrl = `https://${formattedUrl}`
}

const url = new URL(formattedUrl)

url.pathname = path

return url.toString()
}

0 comments on commit 7d3c65f

Please sign in to comment.