Skip to content

Commit

Permalink
refactor(server): create room stream
Browse files Browse the repository at this point in the history
  • Loading branch information
a-wing committed Dec 28, 2023
1 parent 0011777 commit 74292a4
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 54 deletions.
2 changes: 1 addition & 1 deletion server/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewApi(rdb *redis.Client, live777Url string, live777Token string) http.Hand
r.Post("/room/", handle.CreateRoom)
r.Get("/room/{roomId}", handle.ShowRoom)
//r.Patch("/room/{roomId}", handle.UpdateRoom)
//r.Post("/room/{roomId}/stream", handle.CreateRoomStream)
r.Post("/room/{roomId}/stream", handle.CreateRoomStream)
//r.Patch("/room/{roomId}/stream/{streamId}", handle.UpdateRoomStream)

//r.Post("/room/{roomId}/message", handle.CreateMessage)
Expand Down
64 changes: 64 additions & 0 deletions server/api/v1/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package v1

import (
"context"
"net/http"

"woom/server/helper"
"woom/server/model"

"github.com/go-chi/chi/v5"
"github.com/gofrs/uuid/v5"
)

func (h *Handler) helperCreateStreamId() (string, error) {
id, err := uuid.NewV4()
return id.String(), err
}

func (h *Handler) helperCreateRoomStream(r *http.Request, roomId, streamId string) (*model.Stream, error) {
stream := &model.Stream{
// TODO:
Name: "",
// TODO:
Token: "",
Audio: false,
Video: false,
Screen: false,
}

gobStream, err := helper.GobEncode(stream)
if err != nil {
return stream, err
}

if err := h.rdb.HSet(context.TODO(), roomId, streamId, gobStream).Err(); err != nil {
return stream, err
}
return stream, err
}

func (h *Handler) helperShowRoom(r *http.Request) (*model.Room, error) {
roomId := chi.URLParam(r, "roomId")
room := model.Room{
RoomId: roomId,
Streams: map[string]model.Stream{},
}
result, err := h.rdb.HGetAll(context.TODO(), roomId).Result()
if err != nil {
return &room, err
}

for k, v := range result {
if k == model.AdminUniqueKey {
admin := model.RoomAdmin{}
helper.GobDecode(&admin, []byte(v))
room.RoomAdmin = admin
} else {
stream := model.Stream{}
helper.GobDecode(&stream, []byte(v))
room.Streams[k] = stream
}
}
return &room, err
}
54 changes: 8 additions & 46 deletions server/api/v1/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@ import (
"woom/server/helper"
"woom/server/model"

"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
"github.com/gofrs/uuid/v5"
)

const idLength = 9

func (h *Handler) CreateRoom(w http.ResponseWriter, r *http.Request) {
roomId := helper.AddSplitSymbol(helper.GenNumberSecret(idLength))
id, err := uuid.NewV4()
streamId := id.String()
streamId, err := h.helperCreateStreamId()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
Expand All @@ -29,42 +26,24 @@ func (h *Handler) CreateRoom(w http.ResponseWriter, r *http.Request) {
Presenter: "",
Locked: false,
}

stream := model.Stream{
// TODO:
Name: "",
// TODO:
Token: "",
Audio: false,
Video: false,
Screen: false,
}

gobAdmin, err := helper.GobEncode(&admin)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}

gobStream, err := helper.GobEncode(&stream)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}

if err := h.rdb.HSet(context.TODO(), roomId, model.AdminUniqueKey, gobAdmin).Err(); err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}

if err := h.rdb.HSet(context.TODO(), roomId, streamId, gobStream).Err(); err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
//if _, err := h.helperCreateRoomStream(r, roomId, streamId); err != nil {
// w.WriteHeader(http.StatusInternalServerError)
// w.Write([]byte(err.Error()))
// return
//}

room := model.Room{
RoomId: roomId,
RoomAdmin: admin,
Expand All @@ -74,28 +53,11 @@ func (h *Handler) CreateRoom(w http.ResponseWriter, r *http.Request) {
}

func (h *Handler) ShowRoom(w http.ResponseWriter, r *http.Request) {
roomId := chi.URLParam(r, "roomId")
result, err := h.rdb.HGetAll(context.TODO(), roomId).Result()
room, err := h.helperShowRoom(r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}

room := model.Room{
RoomId: roomId,
Streams: map[string]model.Stream{},
}
for k, v := range result {
if k == model.AdminUniqueKey {
admin := model.RoomAdmin{}
helper.GobDecode(&admin, []byte(v))
room.RoomAdmin = admin
} else {
stream := model.Stream{}
helper.GobDecode(&stream, []byte(v))
room.Streams[k] = stream
}
}
render.JSON(w, r, room)
}
25 changes: 25 additions & 0 deletions server/api/v1/stream.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package v1

import (
"net/http"

"github.com/go-chi/render"
)

func (h *Handler) CreateRoomStream(w http.ResponseWriter, r *http.Request) {
room, err := h.helperShowRoom(r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}

streamId, err := h.helperCreateStreamId()
if _, err := h.helperCreateRoomStream(r, room.RoomId, streamId); err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
room.StreamId = streamId
render.JSON(w, r, room)
}
4 changes: 2 additions & 2 deletions webapp/components/join.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ export default function Join() {
<input
className='text-center text-4xl'
placeholder='Enter Meeting id'
value={addSplitSymbol(tmpId)}
onChange={e => setTmpId(delSplitSymbol(e.target.value))}
value={tmpId}
onChange={e => setTmpId(e.target.value)}
maxLength={11}
/>
</div>
Expand Down
2 changes: 1 addition & 1 deletion webapp/components/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default function Layout(props: { meetingId: string }) {

const refresh = async () => {
let res = await fetch(location.origin + `/room/${props.meetingId}`)
const data = await res.json()
const data = (await res.json()).streams
const r = Object.keys(data)
.filter(i => i !== localStreamId)
.filter(i => !!i)
Expand Down
2 changes: 1 addition & 1 deletion webapp/components/member.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function Member() {

const refresh = async () => {
let res = await fetch(location.origin + `/room/${meetingId}`)
setStream(Object.keys(await res.json()).filter(i => i !== localStreamId))
setStream(Object.keys((await res.json()).streams).filter(i => i !== localStreamId))
}

useEffect(() => {
Expand Down
6 changes: 3 additions & 3 deletions webapp/lib/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ function setStreamId(id: string) {
}

async function serverGetStreamId(meetingId: string): Promise<string> {
let res = await fetch(`/room/${meetingId}/stream`, {
let res = await (await fetch(`/room/${meetingId}/stream`, {
method: "POST"
})
return res.text()
})).json()
return res.streamId
}

async function asyncGetStreamId(): Promise<string> {
Expand Down

0 comments on commit 74292a4

Please sign in to comment.