implement handleInfoUpdate method

This commit is contained in:
srgooglo 2022-10-20 18:12:03 +02:00
parent e2d4eba699
commit b308eda05d

View File

@ -1,9 +1,10 @@
import { Controller } from "linebridge/dist/server"
import { User, StreamingKey } from "../../models"
import { nanoid } from "nanoid"
import lodash from "lodash"
import axios from "axios"
import { User, StreamingKey, StreamingInfo } from "../../models"
const streamingIngestServer = process.env.STREAMING_INGEST_SERVER
const streamingServerAPIAddress = process.env.STREAMING_API_SERVER
const streamingServerAPIProtocol = streamingServerAPIAddress.startsWith("https") ? "https" : "http"
@ -104,6 +105,42 @@ export default class StreamingController extends Controller {
this.streamings = this.streamings.filter((streaming) => streaming.stream !== stream)
return streaming
},
handleInfoUpdate: async (payload) => {
let info = await StreamingInfo.findOne({
user_id: payload.user_id
}).catch((err) => {
return false
})
const payloadValues = {
title: payload.title,
description: payload.description,
category: payload.category,
thumbnail: payload.thumbnail,
}
if (!info) {
// create new info
info = new StreamingInfo({
user_id: payload.user_id,
...payloadValues
})
}
// merge data
info = lodash.merge(info, {
title: payload.title,
description: payload.description,
category: payload.category,
thumbnail: payload.thumbnail,
})
await info.save()
global.wsInterface.io.emit(`streaming.info_update.${payload.user_id}`, info)
return info
}
}
@ -179,6 +216,31 @@ export default class StreamingController extends Controller {
}
post = {
"/streaming/update_info": {
middlewares: ["withAuthentication"],
fn: async (req, res) => {
const { title, description, category, thumbnail } = req.body
const info = await this.methods.handleInfoUpdate({
user_id: req.user._id.toString(),
title,
description,
category,
thumbnail
}).catch((err) => {
console.error(err)
res.status(500).json({
error: `Cannot update info: ${err.message}`,
})
return null
})
if (info) {
return res.json(info)
}
},
"/streaming/publish": async (req, res) => {
const { app, stream, tcUrl } = req.body
@ -262,4 +324,5 @@ export default class StreamingController extends Controller {
}
}
}
}
}