Skip to content

Commit

Permalink
Merge pull request #37 from mynaparrot/update
Browse files Browse the repository at this point in the history
Few improvements
  • Loading branch information
jibon57 authored Jul 2, 2022
2 parents aff029c + b373b96 commit 8600d35
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 22 deletions.
2 changes: 1 addition & 1 deletion cmd/server/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package main

const Version = "1.1.2"
const Version = "1.1.3"
22 changes: 20 additions & 2 deletions internal/controllers/lti_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controllers

import (
"fmt"
"github.com/goccy/go-json"
"github.com/gofiber/fiber/v2"
"github.com/mynaparrot/plugNmeet/internal/models"
)
Expand Down Expand Up @@ -56,6 +57,13 @@ func HandleLTIV1VerifyHeaderToken(c *fiber.Ctx) error {
c.Locals("name", auth.Name)
c.Locals("isAdmin", auth.IsAdmin)

if auth.LtiCustomParameters != nil {
customParams, err := json.Marshal(auth.LtiCustomParameters)
if err == nil {
c.Locals("customParams", customParams)
}
}

return c.Next()
}

Expand All @@ -75,15 +83,25 @@ func HandleLTIV1IsRoomActive(c *fiber.Ctx) error {

func HandleLTIV1JoinRoom(c *fiber.Ctx) error {
m := models.NewLTIV1Model()
customParams := c.Locals("customParams").([]byte)

token, err := m.LTIV1JoinRoom(&models.LtiClaims{
claim := &models.LtiClaims{
UserId: c.Locals("userId").(string),
Name: c.Locals("name").(string),
IsAdmin: c.Locals("isAdmin").(bool),
RoomId: c.Locals("roomId").(string),
RoomTitle: c.Locals("roomTitle").(string),
})
}

if len(customParams) > 0 {
p := new(models.LtiCustomParameters)
err := json.Unmarshal(customParams, p)
if err == nil {
claim.LtiCustomParameters = p
}
}

token, err := m.LTIV1JoinRoom(claim)
if err != nil {
return c.JSON(fiber.Map{
"status": false,
Expand Down
4 changes: 4 additions & 0 deletions internal/models/auth_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ func (a *authTokenModel) DoGenerateToken(g *GenTokenReq) (string, error) {
Hidden: g.UserInfo.IsHidden,
}

if g.UserInfo.UserId == "RECORDER_BOT" || g.UserInfo.UserId == "RTMP_BOT" {
grant.Recorder = true
}

at.AddGrant(grant).
SetIdentity(g.UserInfo.UserId).
SetName(g.UserInfo.Name).
Expand Down
143 changes: 135 additions & 8 deletions internal/models/lti_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (
"encoding/hex"
"errors"
"fmt"
"github.com/goccy/go-json"
"github.com/gofiber/fiber/v2"
"github.com/jordic/lti"
"github.com/livekit/protocol/livekit"
"github.com/mynaparrot/plugNmeet/internal/config"
"gopkg.in/square/go-jose.v2"
"gopkg.in/square/go-jose.v2/jwt"
"net/url"
"strconv"
"strings"
"time"
)
Expand All @@ -22,11 +24,32 @@ type LTIV1 struct {
}

type LtiClaims struct {
UserId string `json:"user_id"`
Name string `json:"name"`
IsAdmin bool `json:"is_admin"`
RoomId string `json:"room_id"`
RoomTitle string `json:"room_title"`
UserId string `json:"user_id"`
Name string `json:"name"`
IsAdmin bool `json:"is_admin"`
RoomId string `json:"room_id"`
RoomTitle string `json:"room_title"`
LtiCustomParameters *LtiCustomParameters `json:"lti_custom_parameters,omitempty"`
}

type LtiCustomParameters struct {
RoomDuration int64 `json:"room_duration,omitempty"`
AllowPolls *bool `json:"allow_polls,omitempty"`
AllowSharedNotePad *bool `json:"allow_shared_note_pad,omitempty"`
AllowBreakoutRoom *bool `json:"allow_breakout_room,omitempty"`
AllowRecording *bool `json:"allow_recording,omitempty"`
AllowRTMP *bool `json:"allow_rtmp,omitempty"`
AllowViewOtherWebcams *bool `json:"allow_view_other_webcams,omitempty"`
AllowViewOtherParticipants *bool `json:"allow_view_other_users_list,omitempty"`
MuteOnStart *bool `json:"mute_on_start,omitempty"`
LtiCustomDesign *LtiCustomDesign `json:"lti_custom_design,omitempty"`
}

type LtiCustomDesign struct {
PrimaryColor string `json:"primary_color,omitempty"`
SecondaryColor string `json:"secondary_color,omitempty"`
BackgroundColor string `json:"background_color,omitempty"`
CustomLogo string `json:"custom_logo,omitempty"`
}

type LTIV1FetchRecordingsReq struct {
Expand Down Expand Up @@ -59,9 +82,14 @@ func (m *LTIV1) LTIV1Landing(c *fiber.Ctx, requests, signingURL string) error {
return errors.New("either value of user_id or lis_person_contact_email_primary required")
}

name := params.Get("lis_person_name_full")
if name == "" {
name = fmt.Sprintf("%s_%s", "User", userId)
}

claims := &LtiClaims{
UserId: userId,
Name: params.Get("lis_person_name_full"),
Name: name,
IsAdmin: false,
RoomId: m.genHashId(roomId),
RoomTitle: params.Get("context_label"),
Expand All @@ -70,17 +98,81 @@ func (m *LTIV1) LTIV1Landing(c *fiber.Ctx, requests, signingURL string) error {
if strings.Contains(params.Get("roles"), "Instructor") {
claims.IsAdmin = true
}
m.assignCustomParams(params, claims)

j, err := m.ToJWT(claims)
if err != nil {
return err
}

return c.Render("assets/lti/v1", fiber.Map{
vals := fiber.Map{
"Title": claims.RoomTitle,
"Token": j,
"IsAdmin": claims.IsAdmin,
})
}

if claims.LtiCustomParameters.LtiCustomDesign != nil {
design, err := json.Marshal(claims.LtiCustomParameters.LtiCustomDesign)
if err == nil {
vals["CustomDesign"] = string(design)
}
}

return c.Render("assets/lti/v1", vals)
}

func (m *LTIV1) assignCustomParams(params *url.Values, claims *LtiClaims) {
b := new(bool)
customPara := new(LtiCustomParameters)

if params.Get("custom_room_duration") != "" {
duration, _ := strconv.Atoi(params.Get("custom_room_duration"))
customPara.RoomDuration = int64(duration)
}
if params.Get("custom_allow_polls") == "false" {
customPara.AllowPolls = b
}
if params.Get("custom_allow_shared_note_pad") == "false" {
customPara.AllowSharedNotePad = b
}
if params.Get("custom_allow_breakout_room") == "false" {
customPara.AllowBreakoutRoom = b
}
if params.Get("custom_allow_recording") == "false" {
customPara.AllowRecording = b
}
if params.Get("custom_allow_rtmp") == "false" {
customPara.AllowRTMP = b
}
if params.Get("custom_allow_view_other_webcams") == "false" {
customPara.AllowViewOtherWebcams = b
}
if params.Get("custom_allow_view_other_users_list") == "false" {
customPara.AllowViewOtherParticipants = b
}
// this should be last bool
if params.Get("custom_mute_on_start") == "true" {
*b = true
customPara.MuteOnStart = b
}

// custom design
customDesign := new(LtiCustomDesign)
if params.Get("custom_primary_color") != "" {
customDesign.PrimaryColor = params.Get("custom_primary_color")
}
if params.Get("custom_secondary_color") != "" {
customDesign.SecondaryColor = params.Get("custom_secondary_color")
}
if params.Get("custom_background_color") != "" {
customDesign.BackgroundColor = params.Get("custom_background_color")
}
if params.Get("custom_custom_logo") != "" {
customDesign.CustomLogo = params.Get("custom_custom_logo")
}

claims.LtiCustomParameters = customPara
claims.LtiCustomParameters.LtiCustomDesign = customDesign
}

func (m *LTIV1) VerifyAuth(requests, signingURL string) (*url.Values, error) {
Expand Down Expand Up @@ -213,6 +305,41 @@ func (m *LTIV1) createRoomSession(c *LtiClaims) (bool, string, *livekit.Room) {
},
}

if c.LtiCustomParameters != nil {
p := c.LtiCustomParameters
f := req.RoomMetadata.Features

if p.RoomDuration > 0 {
f.RoomDuration = p.RoomDuration
}
if p.MuteOnStart != nil {
f.MuteOnStart = *p.MuteOnStart
}
if p.AllowSharedNotePad != nil {
f.SharedNotePadFeatures.AllowedSharedNotePad = *p.AllowSharedNotePad
}
if p.AllowBreakoutRoom != nil {
f.BreakoutRoomFeatures.IsAllow = *p.AllowBreakoutRoom
}
if p.AllowPolls != nil {
f.AllowPolls = *p.AllowPolls
}
if p.AllowRecording != nil {
f.AllowRecording = *p.AllowRecording
}
if p.AllowRTMP != nil {
f.AllowRTMP = *p.AllowRTMP
}
if p.AllowViewOtherWebcams != nil {
f.AllowViewOtherWebcams = *p.AllowViewOtherWebcams
}
if p.AllowViewOtherParticipants != nil {
f.AllowViewOtherParticipants = *p.AllowViewOtherParticipants
}

req.RoomMetadata.Features = f
}

return m.authModel.CreateRoom(req)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/models/room_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ func (r *RoomService) LoadRoomWithMetadata(roomId string) (*livekit.Room, *RoomM
}

if room.Metadata == "" {
return room, nil, err
return room, nil, errors.New("empty metadata")
}

meta := new(RoomMetadata)
Expand Down
17 changes: 8 additions & 9 deletions internal/models/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,17 @@ func (s *scheduler) StartScheduler() {
go s.subscribeRedisRoomDurationChecker()

s.ticker = time.NewTicker(5 * time.Second)
defer s.ticker.Stop()
s.closeTicker = make(chan bool)

go func() {
for {
select {
case <-s.closeTicker:
return
case <-s.ticker.C:
s.checkRoomWithDuration()
}
for {
select {
case <-s.closeTicker:
return
case <-s.ticker.C:
s.checkRoomWithDuration()
}
}()
}
}

type RedisRoomDurationCheckerReq struct {
Expand Down
2 changes: 1 addition & 1 deletion start.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env bash

go run -race ./cmd/server/*.go
go run ./cmd/server/*.go

0 comments on commit 8600d35

Please sign in to comment.