-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #643 from podverse/develop
Release v4.13.6
- Loading branch information
Showing
21 changed files
with
4,066 additions
and
48 deletions.
There are no files selected for viewing
3,456 changes: 3,456 additions & 0 deletions
3,456
docs/postman/podverse-api_v4-13-4.postman_collection.json
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
-- Create upDevices table | ||
|
||
CREATE TABLE public."upDevices" ( | ||
"upEndpoint" character varying, | ||
"upPublicKey" character varying NOT NULL, | ||
"upAuthKey" character varying NOT NULL, | ||
"userId" character varying(14) references users(id) ON DELETE CASCADE, | ||
"createdAt" timestamp without time zone DEFAULT now() NOT NULL, | ||
"updatedAt" timestamp without time zone DEFAULT now() NOT NULL, | ||
PRIMARY KEY ("upEndpoint") | ||
); | ||
|
||
ALTER TABLE public."upDevices" OWNER TO postgres; | ||
|
||
CREATE INDEX "IDX_upDevices_userId" ON public."upDevices" USING btree ("userId"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
import { getRepository } from 'typeorm' | ||
import { UPDevice, Notification } from '~/entities' | ||
import { getLoggedInUser } from './user' | ||
import { UPEndpointData } from '~/entities/upDevice' | ||
const createError = require('http-errors') | ||
|
||
export const createUPDevice = async ({ | ||
upEndpoint, | ||
upPublicKey, | ||
upAuthKey, | ||
loggedInUserId | ||
}: { | ||
upEndpoint: string | ||
upPublicKey: string | ||
upAuthKey: string | ||
loggedInUserId: string | ||
}) => { | ||
if (!upEndpoint) { | ||
throw new createError.BadRequest('An upEndpoint must be provided.') | ||
} | ||
|
||
if (!upPublicKey) { | ||
throw new createError.BadRequest('An upPublicKey must be provided.') | ||
} | ||
|
||
if (!upAuthKey) { | ||
throw new createError.BadRequest('An upAuthKey must be provided.') | ||
} | ||
|
||
if (!loggedInUserId) { | ||
throw new createError.BadRequest('A user id must be provided.') | ||
} | ||
|
||
const user = await getLoggedInUser(loggedInUserId) | ||
if (!user) { | ||
throw new createError.NotFound(`User for id ${loggedInUserId} not found.`) | ||
} | ||
|
||
const newUPDevice = new UPDevice() | ||
newUPDevice.upEndpoint = upEndpoint | ||
newUPDevice.upPublicKey = upPublicKey | ||
newUPDevice.upAuthKey = upAuthKey | ||
newUPDevice.user = user | ||
|
||
const repository = getRepository(UPDevice) | ||
await repository.save(newUPDevice) | ||
} | ||
|
||
export const updateUPDevice = async ({ | ||
previousUPEndpoint, | ||
nextUPEndpoint, | ||
upPublicKey, | ||
upAuthKey, | ||
loggedInUserId | ||
}: { | ||
previousUPEndpoint | ||
nextUPEndpoint | ||
upPublicKey | ||
upAuthKey | ||
loggedInUserId | ||
}) => { | ||
if (!previousUPEndpoint) { | ||
throw new createError.BadRequest('A previous upEndpoint must be provided.') | ||
} | ||
|
||
if (!nextUPEndpoint) { | ||
throw new createError.BadRequest('A new upEndpoint must be provided.') | ||
} | ||
|
||
if (!upPublicKey) { | ||
throw new createError.BadRequest('A new upPublicKey must be provided.') | ||
} | ||
|
||
if (!upAuthKey) { | ||
throw new createError.BadRequest('A new upAuthKey must be provided.') | ||
} | ||
|
||
if (!loggedInUserId) { | ||
throw new createError.BadRequest('A user id must be provided.') | ||
} | ||
|
||
const user = await getLoggedInUser(loggedInUserId) | ||
if (!user) { | ||
throw new createError.NotFound(`User for id ${loggedInUserId} not found.`) | ||
} | ||
|
||
const existingUPDevice = await getUPDevice(previousUPEndpoint, loggedInUserId) | ||
const repository = getRepository(UPDevice) | ||
if (!existingUPDevice) { | ||
const newUPDevice = new UPDevice() | ||
newUPDevice.upEndpoint = nextUPEndpoint | ||
newUPDevice.upPublicKey = upPublicKey | ||
newUPDevice.upAuthKey = upAuthKey | ||
newUPDevice.user = user | ||
await repository.save(newUPDevice) | ||
} else { | ||
const newData = { upEndpoint: nextUPEndpoint, upPublicKey, upAuthKey } | ||
await repository.update(previousUPEndpoint, newData) | ||
} | ||
} | ||
|
||
export const deleteUPDevice = async (upEndpoint: string, loggedInUserId: string) => { | ||
if (!upEndpoint) { | ||
throw new createError.BadRequest('An upEndpoint must be provided.') | ||
} | ||
|
||
if (!loggedInUserId) { | ||
throw new createError.BadRequest('A user id must be provided.') | ||
} | ||
|
||
const user = await getLoggedInUser(loggedInUserId) | ||
if (!user) { | ||
throw new createError.NotFound(`User for id ${loggedInUserId} not found.`) | ||
} | ||
|
||
const upDevice = await getUPDevice(upEndpoint, loggedInUserId) | ||
|
||
if (!upDevice) { | ||
throw new createError.NotFound(`upDevice for upEndpoint ${upEndpoint} not found.`) | ||
} | ||
|
||
const repository = getRepository(UPDevice) | ||
await repository.remove(upDevice) | ||
} | ||
|
||
const getUPDevice = async (upEndpoint: string, loggedInUserId: string) => { | ||
if (!upEndpoint) { | ||
throw new createError.BadRequest('An upEndpoint must be provided.') | ||
} | ||
|
||
if (!loggedInUserId) { | ||
throw new createError.BadRequest('A user id must be provided.') | ||
} | ||
|
||
const repository = getRepository(UPDevice) | ||
return repository | ||
.createQueryBuilder('upDevices') | ||
.select('"upDevices"."upEndpoint"', 'upEndpoint') | ||
.where('"upEndpoint" = :upEndpoint', { upEndpoint }) | ||
.andWhere('user.id = :loggedInUserId', { loggedInUserId }) | ||
.leftJoin('upDevices.user', 'user') | ||
.getRawOne() | ||
} | ||
|
||
export const getUPEndpointsForPodcastId = async (podcastId: string) => { | ||
if (!podcastId) { | ||
throw new createError.BadRequest('A podcastId but be provided.') | ||
} | ||
|
||
const repository = getRepository(Notification) | ||
const notifications = await repository | ||
.createQueryBuilder('notifications') | ||
.select('"upDevices"."upEndpoint", "upEndpoint"') | ||
.innerJoin(UPDevice, 'upDevices', 'notifications."userId" = "upDevices"."userId"') | ||
.where('notifications."podcastId" = :podcastId', { podcastId }) | ||
.getRawMany() | ||
|
||
const upEndpoints = notifications.map((upDevice: UPDevice) => upDevice.upEndpoint) | ||
|
||
return upEndpoints | ||
} | ||
|
||
export const getUPDevicesForPodcastId = async (podcastId: string): Promise<UPEndpointData[]> => { | ||
if (!podcastId) { | ||
throw new createError.BadRequest('A podcastId but be provided.') | ||
} | ||
|
||
const repository = getRepository(Notification) | ||
return await repository | ||
.createQueryBuilder('notifications') | ||
.select( | ||
'"upDevices"."upEndpoint" AS "upEndpoint", "upDevices"."upPublicKey" AS "upPublicKey", "upDevices"."upAuthKey" AS "upAuthKey"' | ||
) | ||
.innerJoin(UPDevice, 'upDevices', 'notifications."userId" = "upDevices"."userId"') | ||
.where('notifications."podcastId" = :podcastId', { podcastId }) | ||
.getRawMany() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
|
||
import { Column, CreateDateColumn, Entity, ManyToOne, PrimaryColumn, UpdateDateColumn } from 'typeorm' | ||
import { User } from '~/entities' | ||
|
||
export interface UPEndpointData { | ||
upEndpoint: string | ||
upPublicKey: string | ||
upAuthKey: string | ||
} | ||
|
||
@Entity('upDevices') | ||
export class UPDevice { | ||
@PrimaryColumn() | ||
upEndpoint: string | ||
|
||
// E2EE | ||
@Column() | ||
upPublicKey: string | ||
|
||
// E2EE | ||
@Column() | ||
upAuthKey: string | ||
|
||
@ManyToOne((type) => User, (user) => user.upDevices, { | ||
nullable: false, | ||
onDelete: 'CASCADE' | ||
}) | ||
user: User | ||
|
||
@CreateDateColumn() | ||
createdAt: Date | ||
|
||
@UpdateDateColumn() | ||
updatedAt: Date | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { SendNotificationOptions } from './sendNotificationOptions' | ||
import { sendFcmLiveItemLiveDetectedNotification, sendFcmNewEpisodeDetectedNotification } from './fcmGoogleApi' | ||
import { sendUpLiveItemLiveDetectedNotification, sendUpNewEpisodeDetectedNotification } from './unifiedPush' | ||
|
||
export const sendNewEpisodeDetectedNotification = async (options: SendNotificationOptions) => { | ||
return Promise.all([sendFcmNewEpisodeDetectedNotification(options), sendUpNewEpisodeDetectedNotification(options)]) | ||
} | ||
|
||
export const sendLiveItemLiveDetectedNotification = async (options: SendNotificationOptions) => { | ||
return Promise.all([ | ||
sendFcmLiveItemLiveDetectedNotification(options), | ||
sendUpLiveItemLiveDetectedNotification(options) | ||
]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export interface SendNotificationOptions { | ||
podcastId: string | ||
podcastTitle?: string | ||
episodeTitle?: string | ||
podcastImage?: string | ||
episodeImage?: string | ||
episodeId?: string | ||
} |
Oops, something went wrong.