Merge pull request 'Fix minor things' (#22) from fix-things into main
Reviewed-on: https://git.verdigado.com/NB-Public/rocketchat2matrix/pulls/22
This commit is contained in:
commit
b1e15242bd
@ -40,7 +40,7 @@ pipeline:
|
||||
image: *node_image
|
||||
commands:
|
||||
- *create_synapse_access_token
|
||||
- npm test
|
||||
- npm test --ci
|
||||
|
||||
node-compile:
|
||||
image: *node_image
|
||||
|
||||
@ -2,6 +2,7 @@ import { expect, jest, test } from '@jest/globals'
|
||||
import axios from 'axios'
|
||||
import { IdMapping } from '../entity/IdMapping'
|
||||
import * as storage from '../helpers/storage'
|
||||
import { SessionOptions } from '../helpers/synapse'
|
||||
import {
|
||||
MatrixRoomPresets,
|
||||
MatrixRoomVisibility,
|
||||
@ -54,7 +55,7 @@ const rcPrivateRoom = {
|
||||
u: roomCreator,
|
||||
}
|
||||
|
||||
const sessionOption = {
|
||||
const sessionOption: SessionOptions = {
|
||||
headers: { Authorization: 'Bearer secretAuthToken' },
|
||||
testingOption: 'there might be other options',
|
||||
}
|
||||
|
||||
@ -6,6 +6,7 @@ import {
|
||||
getMemberships,
|
||||
} from '../helpers/storage'
|
||||
import {
|
||||
SessionOptions,
|
||||
axios,
|
||||
formatUserSessionOptions,
|
||||
getUserSessionOptions,
|
||||
@ -114,7 +115,7 @@ export async function parseMemberships(rcRoom: RcRoom): Promise<void> {
|
||||
|
||||
export async function getCreatorSessionOptions(
|
||||
creatorId: string
|
||||
): Promise<object> {
|
||||
): Promise<SessionOptions | object> {
|
||||
if (creatorId) {
|
||||
try {
|
||||
const creatorSessionOptions = await getUserSessionOptions(creatorId)
|
||||
@ -129,7 +130,7 @@ export async function getCreatorSessionOptions(
|
||||
|
||||
export async function registerRoom(
|
||||
room: MatrixRoom,
|
||||
creatorSessionOptions: object
|
||||
creatorSessionOptions: SessionOptions | object
|
||||
): Promise<string> {
|
||||
return (
|
||||
await axios.post(
|
||||
@ -143,7 +144,7 @@ export async function registerRoom(
|
||||
export async function inviteMember(
|
||||
inviteeId: string,
|
||||
roomId: string,
|
||||
creatorSessionOptions: object
|
||||
creatorSessionOptions: SessionOptions | object
|
||||
): Promise<void> {
|
||||
log.http(`Invite member ${inviteeId}`)
|
||||
await axios.post(
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import { expect, jest, test } from '@jest/globals'
|
||||
import { getUserSessionOptions } from '../helpers/synapse'
|
||||
import { jest, expect, test } from '@jest/globals'
|
||||
import { getCreatorSessionOptions } from './rooms'
|
||||
|
||||
jest.mock('../helpers/synapse')
|
||||
|
||||
@ -10,26 +10,32 @@ const AppDataSource = new DataSource({
|
||||
logging: false,
|
||||
})
|
||||
|
||||
export async function initStorage() {
|
||||
export async function initStorage(): Promise<void> {
|
||||
await AppDataSource.initialize()
|
||||
}
|
||||
|
||||
export function getMapping(id: string, type: number) {
|
||||
export function getMapping(
|
||||
id: string,
|
||||
type: number
|
||||
): Promise<IdMapping | null> {
|
||||
return AppDataSource.manager.findOneBy(IdMapping, {
|
||||
rcId: id,
|
||||
type: type,
|
||||
})
|
||||
}
|
||||
|
||||
export async function save(entity: IdMapping | Membership) {
|
||||
export async function save(entity: IdMapping | Membership): Promise<void> {
|
||||
await AppDataSource.manager.save(entity)
|
||||
}
|
||||
|
||||
export async function getAccessToken(id: string) {
|
||||
export async function getAccessToken(id: string): Promise<string | undefined> {
|
||||
return (await getMapping(id, 0))?.accessToken
|
||||
}
|
||||
|
||||
export async function createMembership(rcRoomId: string, rcUserId: string) {
|
||||
export async function createMembership(
|
||||
rcRoomId: string,
|
||||
rcUserId: string
|
||||
): Promise<void> {
|
||||
const membership = new Membership()
|
||||
membership.rcRoomId = rcRoomId
|
||||
membership.rcUserId = rcUserId
|
||||
@ -37,7 +43,7 @@ export async function createMembership(rcRoomId: string, rcUserId: string) {
|
||||
await save(membership)
|
||||
}
|
||||
|
||||
export async function getMemberships(rcRoomId: string) {
|
||||
export async function getMemberships(rcRoomId: string): Promise<string[]> {
|
||||
return (
|
||||
await AppDataSource.manager.find(Membership, {
|
||||
select: {
|
||||
|
||||
@ -7,6 +7,13 @@ axios.defaults.baseURL = 'http://localhost:8008'
|
||||
axios.defaults.headers.common['Authorization'] = `Bearer ${access_token}`
|
||||
axios.defaults.headers.post['Content-Type'] = 'application/json'
|
||||
|
||||
export interface SessionOptions {
|
||||
headers: {
|
||||
Authorization: string
|
||||
}
|
||||
[others: string]: unknown
|
||||
}
|
||||
|
||||
export { default as axios } from 'axios'
|
||||
export const whoami = () =>
|
||||
new Promise<void>((resolve, reject) => {
|
||||
@ -22,11 +29,13 @@ export const whoami = () =>
|
||||
})
|
||||
})
|
||||
|
||||
export function formatUserSessionOptions(accessToken: string) {
|
||||
export function formatUserSessionOptions(accessToken: string): SessionOptions {
|
||||
return { headers: { Authorization: `Bearer ${accessToken}` } }
|
||||
}
|
||||
|
||||
export async function getUserSessionOptions(id: string) {
|
||||
export async function getUserSessionOptions(
|
||||
id: string
|
||||
): Promise<SessionOptions> {
|
||||
const accessToken = await getAccessToken(id)
|
||||
if (!accessToken) {
|
||||
throw new Error(`Could not retrieve access token for ID ${id}`)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user