Ejemplo n.º 1
0
func GetUsersSubscriptions(c *gin.Context) {
	userId := c.Param("user_id")
	if userId != c.MustGet("request_user_id").(string) {
		c.Error(errors.NewHttp(http.StatusUnauthorized, "Users can only view the subscriptions of themselves"))
		return
	}
	subs, err := db.GetUserSubscriptions(userId)
	if log.Error(err) {
		c.Error(errors.NewISE())
		return
	}
	subResponses := make([]model.SubscriptionResponse, 0)
	for _, sub := range subs {
		section, err := db.GetSectionByNotebookId(sub.NotebookId)
		if log.Error(err) {
			c.Error(errors.NewISE())
			return
		}
		course, err := db.GetCourseByCourseId(section.CourseId)
		if log.Error(err) {
			c.Error(errors.NewISE())
			return
		}
		subResponses = append(subResponses, model.NewSubscriptionResponse(sub, course, section))
	}
	c.JSON(http.StatusOK, subResponses)
}
Ejemplo n.º 2
0
// Status handler for the /status route
func OpenWebsocket(c *gin.Context) {
	userId := c.MustGet("request_user_id").(string)
	noteId := c.Param("note_id")
	in, note, err := db.GetNoteById(noteId)
	if log.Error(err) {
		c.Error(errors.NewISE())
		return
	}
	if !in {
		c.Error(errors.NewHttp(http.StatusNotFound, "The requested note was not found"))
		return
	}
	if userId != note.Owner {
		c.Error(errors.NewHttp(http.StatusUnauthorized, "Only owners can open websockets into their notes"))
		return
	}
	conn, err := websocketUpgrader.Upgrade(c.Writer, c.Request, nil)
	if log.Error(err) {
		c.Error(errors.NewISE())
		return
	}
	log.Info("Opening ws for user %v on %v", userId, note.Id)
	bundle := model.NewContext(userId, noteId)
	WrapWebsocket(conn, bundle)
	ws.ProcessMessages(bundle)
}
Ejemplo n.º 3
0
func RemoveUserSubscription(c *gin.Context) {
	userId := c.Param("user_id")
	if userId != c.MustGet("request_user_id").(string) {
		c.Error(errors.NewHttp(http.StatusUnauthorized, "Users can only delete their own subs"))
		return
	}
	notebookId := c.Param("notebook_id")
	sub := model.DbSubscription{
		UserId:     userId,
		NotebookId: notebookId,
	}
	section, err := db.GetSectionByNotebookId(sub.NotebookId)
	if log.Error(err) {
		c.Error(errors.NewISE())
		return
	}
	course, err := db.GetCourseByCourseId(section.CourseId)
	if log.Error(err) {
		c.Error(errors.NewISE())
		return
	}
	err = db.DeleteSubscription(sub)
	if log.Error(err) {
		c.Error(err)
		return
	}
	c.JSON(http.StatusOK, model.NewSubscriptionResponse(sub, course, section))
}
Ejemplo n.º 4
0
func GetAllSchools(c *gin.Context) {
	schools, err := db.GetAllSchools()
	if log.Error(err) {
		c.Error(errors.NewISE())
		return
	}
	c.JSON(http.StatusOK, schools)
}
Ejemplo n.º 5
0
func EchoWebsocket(c *gin.Context) {
	conn, err := websocketUpgrader.Upgrade(c.Writer, c.Request, nil)
	if log.Error(err) {
		c.Error(errors.NewISE())
		return
	}
	for {
		typ, frame, _ := conn.ReadMessage()
		conn.WriteMessage(typ, frame)
	}
}
Ejemplo n.º 6
0
func GetNotebookNotes(c *gin.Context) {
	userId := c.MustGet("request_user_id").(string)
	notebookId := c.Param("notebook_id")

	// Confirm notebook exists
	_, err := db.GetNotebookById(notebookId)
	if err != nil {
		c.Error(err)
		return
	}

	// Check query params
	filterUserId := c.Query("user")
	filterUnjoined := c.Query("unjoined")

	// And execute those query params
	var notes []model.DbNote
	if filterUserId == "" && filterUnjoined == "" {
		notes, err = db.GetNotesInNotebook(notebookId)
	} else if filterUserId == "" {
		notes, err = db.GetUnjoinedNotesInNotebook(notebookId, userId)
	} else if filterUnjoined == "" {
		notes, err = db.GetNotesInNotebookByUser(notebookId, userId)
	} else {
		c.Error(errors.NewHttp(http.StatusBadRequest, "Cannot provide both unjoined and user parameters lol"))
		return
	}

	if log.Error(err) {
		c.Error(errors.NewISE())
		return
	}

	// Transform the resultant list into hierarchial [topic -> []note]
	topicHash := make(map[string][]model.FullNoteResponse)
	for _, dbnote := range notes {
		if ar, in := topicHash[dbnote.TopicId]; in {
			topicHash[dbnote.TopicId] = append(ar, model.NewFullNoteResponse(dbnote))
		} else {
			topicHash[dbnote.TopicId] = []model.FullNoteResponse{
				model.NewFullNoteResponse(dbnote),
			}
		}
	}
	topicResponses := []model.TopicResponse{}
	for topicid, notelist := range topicHash {
		topicResponses = append(topicResponses, model.TopicResponse{
			Id:    topicid,
			Notes: notelist,
		})
	}
	c.JSON(http.StatusOK, topicResponses)
}
Ejemplo n.º 7
0
func GetUser(c *gin.Context) {
	userId := c.Param("user_id")
	if userId != c.MustGet("request_user_id").(string) {
		c.Error(errors.NewHttp(http.StatusUnauthorized, "Users can only view detailed information about themselves"))
		return
	}
	_, user, err := db.GetUserById(userId)
	if log.Error(err) {
		c.Error(errors.NewISE())
		return
	}
	c.JSON(http.StatusOK, model.NewUserResponse(user))
}
Ejemplo n.º 8
0
func GetSectionsForCourse(c *gin.Context) {
	courseId := c.Param("course_id")
	sections, err := db.GetSectionsForCourse(courseId)
	if log.Error(err) {
		c.Error(errors.NewISE())
		return
	}
	sectionsResponse := make([]model.SectionResponse, 0)
	for _, section := range sections {
		sectionsResponse = append(sectionsResponse, model.SectionResponseWithoutCourse(section))
	}
	c.JSON(http.StatusOK, sectionsResponse)
}
Ejemplo n.º 9
0
func GetCoursesForSchool(c *gin.Context) {
	schoolId := c.Param("school_id")
	courses, err := db.GetCoursesForSchool(schoolId)
	if log.Error(err) {
		c.Error(errors.NewISE())
		return
	}
	courseResponses := make([]model.CourseResponse, 0)
	for _, course := range courses {
		courseResponses = append(courseResponses, model.CourseResponseWithoutSchool(course))
	}
	c.JSON(http.StatusOK, courseResponses)
}
Ejemplo n.º 10
0
func GetSingleSchool(c *gin.Context) {
	schoolId := c.Param("school_id")
	in, school, err := db.GetSchool(schoolId)
	if log.Error(err) {
		c.Error(errors.NewISE())
		return
	}
	if !in {
		c.Error(errors.NewHttp(http.StatusNotFound, "School requested could not be found"))
		return
	}
	c.JSON(http.StatusOK, school)
}
Ejemplo n.º 11
0
func ModifyUserSubscription(c *gin.Context) {
	userId := c.Param("user_id")
	if userId != c.MustGet("request_user_id").(string) {
		c.Error(errors.NewHttp(http.StatusUnauthorized, "Users can only modify subscriptions for themselves"))
		return
	}
	var request model.SubscriptionRequest
	err := c.BindJSON(&request)
	if log.Error(err) {
		c.Error(err)
		return
	}
	sub := model.DbSubscription{
		UserId:     userId,
		NotebookId: request.NotebookId,
		Name: sql.NullString{
			String: request.Name,
			Valid:  true,
		},
	}
	err = db.UpdateSubscription(sub)
	if log.Error(err) {
		c.Error(errors.NewISE())
		return
	}
	section, err := db.GetSectionByNotebookId(sub.NotebookId)
	if log.Error(err) {
		c.Error(errors.NewISE())
		return
	}
	course, err := db.GetCourseByCourseId(section.CourseId)
	if log.Error(err) {
		c.Error(errors.NewISE())
		return
	}
	c.JSON(http.StatusOK, model.NewSubscriptionResponse(sub, course, section))
}
Ejemplo n.º 12
0
func Login(c *gin.Context) {
	var returnCode int
	var request model.LoginRequest

	// Parse the user request
	err := c.BindJSON(&request)
	if log.Error(err) {
		c.Error(err)
		return
	}

	// Right now we assume that the user is logging in with Facebook
	fbUser, err := service.Facebook{}.GetCurrentUser(request.AccessToken)
	if log.Error(err) {
		c.Error(errors.NewHttp(errors.ISE, "Error contacting facebook api"))
		return
	}

	// Then get the user's profile picture
	fbPicture, err := service.Facebook{}.GetProfilePic(request.AccessToken)
	if log.Error(err) {
		c.Error(errors.NewHttp(errors.ISE, "Error contacting facebook api"))
		return
	}

	// See if the user is already a notion user
	in, dbUser, err := db.GetUserByFacebookId(fbUser.Id)
	if log.Error(err) {
		c.Error(errors.NewISE())
		return
	}

	// If they are in the database, we just update their auth token
	if in {
		returnCode = http.StatusAccepted
		dbUser.FbAuthToken = request.AccessToken
		dbUser.FbProfilePic = fbPicture.Data.Url
		err = db.UpdateUser(dbUser)
	} else {
		returnCode = http.StatusCreated
		dbUser = model.DbUser{
			Id:           util.NewId(),
			Name:         fbUser.Name,
			Email:        fbUser.Email,
			Verified:     false,
			AuthMethod:   request.AuthMethod,
			FbUserId:     fbUser.Id,
			FbAuthToken:  request.AccessToken,
			FbProfilePic: fbPicture.Data.Url,
		}
		err = db.CreateUser(dbUser)
	}

	// Error check 'er yo
	if log.Error(err) {
		c.Error(errors.NewISE())
		return
	}

	// Throw back the user object at the requester
	c.JSON(returnCode, model.NewUserResponse(dbUser))

}