Example #1
0
func GetFriends(w http.ResponseWriter, r *http.Request) {
	var parser store.Parser

	userID := parser.Int(r.FormValue("userID"))
	if parser.Err != nil {
		response.ClientError(w, http.StatusBadRequest)
		return
	}

	users, err := store.GetFriends(userID)
	if err != nil {
		response.ServerError(w, err)
		return
	}

	for i := range users {
		if _, ok := h.connections[users[i].ID()]; ok {
			users[i].SetStatus("Online")
		} else {
			users[i].SetStatus("Offline")
		}
	}

	response.OK(w, users)
}
Example #2
0
func RemoveMember(w http.ResponseWriter, r *http.Request) {
	var parser store.Parser

	projectID := parser.Int(r.FormValue("projectID"))
	userID := parser.Int(r.FormValue("userID"))
	if parser.Err != nil {
		response.ClientError(w, http.StatusBadRequest)
		return
	}

	user := context.Get(r, "user").(store.User)
	if user.ID() != userID {
		response.ClientError(w, http.StatusForbidden)
		return
	}

	if exists, err := store.MemberExists(projectID, userID); err != nil {
		response.ServerError(w, err)
		return
	} else if !exists {
		response.OK(w, nil)
		return
	}

	if err := store.RemoveMember(projectID, userID); err != nil {
		response.ServerError(w, err)
		return
	}

	response.OK(w, nil)
}
Example #3
0
func GetProject(w http.ResponseWriter, r *http.Request) {
	var parser store.Parser

	projectID := parser.Int(r.FormValue("projectID"))
	if parser.Err != nil {
		response.ClientError(w, http.StatusBadRequest)
		return
	}

	p, err := store.GetProject(projectID)
	if err != nil {
		response.ServerError(w, err)
		return
	}

	if p.Author, err = store.GetUser(p.AuthorID); err != nil {
		response.ServerError(w, err)
		return
	}

	user := context.Get(r, "user").(store.User)
	p.IsMember = store.IsMember(projectID, user.ID())

	response.OK(w, p)
}
Example #4
0
func SetFeaturedProject(w http.ResponseWriter, r *http.Request) {
	var parser store.Parser

	const existSQL = `
	SELECT COUNT(*) FROM featured_project
	WHERE project_id = $1`

	projectID := parser.Int(r.FormValue("projectID"))
	if parser.Err != nil {
		response.ClientError(w, http.StatusBadRequest)
		return
	}

	if exists, err := store.Exists(existSQL, projectID); err != nil {
		response.ServerError(w, err)
		return
	} else if exists {
		response.OK(w, nil)
		return
	}

	if err := store.SetFeaturedProject(projectID); err != nil {
		response.ServerError(w, err)
	}

	response.OK(w, nil)
}
Example #5
0
func UpdateMilestone(w http.ResponseWriter, r *http.Request) {
	var parser store.Parser

	projectID := parser.Int(r.FormValue("projectID"))
	milestoneID := parser.Int(r.FormValue("milestoneID"))
	date := parser.Time(r.FormValue("date"))
	if parser.Err != nil {
		response.ClientError(w, http.StatusBadRequest)
		return
	}

	user := context.Get(r, "user").(store.User)
	if !user.IsAuthor(projectID) {
		response.ClientError(w, http.StatusForbidden)
		return
	}

	title := r.FormValue("title")
	description := r.FormValue("description")

	if err := store.UpdateMilestone(store.UpdateMilestoneParams{
		MilestoneID: milestoneID,
		Title:       title,
		Description: description,
		Date:        date,
	}); err != nil {
		response.ServerError(w, err)
		return
	}

	response.OK(w, nil)
}
Example #6
0
func UpdateTask(w http.ResponseWriter, r *http.Request) {
	var parser store.Parser

	projectID := parser.Int(r.FormValue("projectID"))
	if parser.Err != nil {
		response.ServerError(w, parser.Err)
		return
	}

	user := context.Get(r, "user").(store.User)
	if !user.IsMember(projectID) {
		response.ClientError(w, http.StatusForbidden)
		return
	}

	var taskID int64
	if err := store.UpdateTask(store.UpdateTaskParams{
		TaskID:      r.FormValue("taskID"),
		Title:       r.FormValue("title"),
		Description: r.FormValue("description"),
		Tags:        r.Form["tags"],
		StartDate:   r.FormValue("startDate"),
		EndDate:     r.FormValue("endDate"),
	}); err != nil {
		response.ServerError(w, err)
		return
	}

	response.OK(w, taskID)
}
Example #7
0
func PostChat(w http.ResponseWriter, r *http.Request) {
	var parser store.Parser

	userID := parser.Int(r.FormValue("userID"))
	channelID := parser.Int(r.FormValue("channelID"))
	if parser.Err != nil {
		response.ClientError(w, http.StatusBadRequest)
		return
	}

	chatParams := store.PostChatParams{
		UserID:      userID,
		ChannelID:   channelID,
		ChannelType: r.FormValue("channelType"),
		Text:        r.FormValue("text"),
	}

	id, err := store.PostChat(chatParams)
	if err != nil {
		response.ServerError(w, err)
		return
	}

	if err = store.NotifyChat(store.NotifyChatParams{id, chatParams}); err != nil {
		debug.Warn(err)
		return
	}

	response.OK(w, id)
}
Example #8
0
func GetUser(w http.ResponseWriter, r *http.Request) {
	userIDStr := r.FormValue("userID")
	if userIDStr == "me" {
		response.OK(w, store.CurrentUser(r))
		return
	}

	var parser store.Parser
	userID := parser.Int(userIDStr)
	if parser.Err != nil {
		response.ClientError(w, http.StatusBadRequest)
		return
	}

	user := context.Get(r, "user").(store.User)

	otherUser, err := store.GetUserWithParams(userID, store.GetUserParams{user.ID()})
	if err != nil {
		response.ServerError(w, err)
		return
	}

	if otherUser == nil {
		response.ClientError(w, http.StatusNotFound)
		return
	}

	response.OK(w, otherUser)
}
Example #9
0
func UnassignWorker(w http.ResponseWriter, r *http.Request) {
	var parser store.Parser

	taskID := parser.Int(r.FormValue("taskID"))
	userID := parser.Int(r.FormValue("userID"))
	if parser.Err != nil {
		response.ClientError(w, http.StatusBadRequest)
		return
	}

	store.DeleteWorker(taskID, userID)
}
Example #10
0
func UnsetFeaturedProject(w http.ResponseWriter, r *http.Request) {
	var parser store.Parser

	projectID := parser.Int(r.FormValue("projectID"))
	if parser.Err != nil {
		response.ClientError(w, http.StatusBadRequest)
		return
	}

	if err := store.UnsetFeaturedProject(projectID); err != nil {
		response.ServerError(w, err)
		return
	}

	response.OK(w, nil)
}
Example #11
0
func DeleteMilestone(w http.ResponseWriter, r *http.Request) {
	var parser store.Parser

	milestoneID := parser.Int(r.FormValue("milestoneID"))
	if parser.Err != nil {
		response.ClientError(w, http.StatusBadRequest)
		return
	}

	if err := store.DeleteMilestone(milestoneID); err != nil {
		response.ServerError(w, err)
		return
	}

	response.OK(w, nil)
}
Example #12
0
func CompletedProjects(w http.ResponseWriter, r *http.Request) {
	var parser store.Parser

	userID := parser.Int(r.FormValue("userID"))
	if parser.Err != nil {
		response.ClientError(w, http.StatusBadRequest)
		return
	}

	ps, err := store.CompletedProjects(userID)
	if err != nil {
		response.ServerError(w, err)
		return
	}

	response.OK(w, ps)
}
Example #13
0
func LatestTasks(w http.ResponseWriter, r *http.Request) {
	var parser store.Parser

	count := parser.Int(r.FormValue("count"))
	if parser.Err != nil {
		response.ClientError(w, http.StatusBadRequest)
		return
	}

	tasks, err := store.LatestTasks(r.FormValue("title"), count)
	if err != nil {
		response.ServerError(w, err)
		return
	}

	response.OK(w, tasks)
}
func FeaturedProjects(w http.ResponseWriter, r *http.Request) {
	var parser store.Parser

	count := parser.Int(r.FormValue("count"))
	if parser.Err != nil {
		response.ClientError(w, http.StatusBadRequest)
		return
	}

	projects, err := store.FeaturedProjects(count)
	if err != nil {
		response.ServerError(w, err)
		return
	}

	response.OK(w, projects)
}
Example #15
0
func GetMilestones(w http.ResponseWriter, r *http.Request) {
	var parser store.Parser

	projectID := parser.Int(r.FormValue("projectID"))
	if parser.Err != nil {
		response.ClientError(w, http.StatusBadRequest)
		return
	}

	milestones, err := store.GetMilestones(projectID)
	if err != nil {
		response.ServerError(w, err)
		return
	}

	response.OK(w, milestones)
}
Example #16
0
func UpdateProject(w http.ResponseWriter, r *http.Request) {
	var parser store.Parser

	projectID := parser.Int(r.FormValue("projectID"))
	if parser.Err != nil {
		response.ClientError(w, http.StatusBadRequest)
		return
	}

	user := context.Get(r, "user").(store.User)
	if !user.IsAuthor(projectID) {
		response.ClientError(w, http.StatusForbidden)
		return
	}

	for k, v := range r.Form {
		var err error

		if len(v) == 0 {
			continue
		}

		switch k {
		case "title":
			err = store.UpdateProjectTitle(projectID, v[0])
		case "tagline":
			err = store.UpdateProjectTagline(projectID, v[0])
		case "description":
			err = store.UpdateProjectDescription(projectID, v[0])
		}
		if err != nil {
			response.ServerError(w, err)
			return
		}
	}

	if _, err := store.SaveProjectImage(w, r, projectID); err != nil {
		response.ServerError(w, err)
		return
	}

	response.OK(w, nil)
}
Example #17
0
func PersonalizedTasks(w http.ResponseWriter, r *http.Request) {
	var parser store.Parser

	count := parser.Int(r.FormValue("count"))
	if parser.Err != nil {
		response.ClientError(w, http.StatusBadRequest)
		return
	}

	user := context.Get(r, "user").(store.User)

	tasks, err := store.PersonalizedTasks(user.ID(), count)
	if err != nil {
		response.ServerError(w, err)
		return
	}

	response.OK(w, tasks)
}
Example #18
0
func DeleteProject(w http.ResponseWriter, r *http.Request) {
	var parser store.Parser

	projectID := parser.Int(r.FormValue("projectID"))
	if parser.Err != nil {
		response.ClientError(w, http.StatusBadRequest)
		return
	}

	user := context.Get(r, "user").(store.User)
	if !user.IsAuthor(projectID) {
		response.ClientError(w, http.StatusUnauthorized)
		return
	}

	if err := store.DeleteProject(projectID); err != nil {
		response.ServerError(w, err)
		return
	}
}
Example #19
0
func CreateTask(w http.ResponseWriter, r *http.Request) {
	var parser store.Parser

	projectID := parser.Int(r.FormValue("projectID"))
	startDate := parser.Time(r.FormValue("startDate"))
	endDate := parser.Time(r.FormValue("endDate"))
	if parser.Err != nil {
		response.ClientError(w, http.StatusBadRequest)
		return
	}

	if startDate.After(endDate) {
		response.ClientError(w, http.StatusBadRequest)
		return
	}

	user := context.Get(r, "user").(store.User)
	if !user.IsMember(projectID) {
		response.ClientError(w, http.StatusForbidden)
		return
	}

	var taskID int64
	var err error

	if taskID, err = store.CreateTask(store.CreateTaskParams{
		AuthorID:    user.ID(),
		ProjectID:   projectID,
		Title:       r.FormValue("title"),
		Description: r.FormValue("description"),
		Done:        false,
		Tags:        strings.Split(r.FormValue("tags"), ","),
		StartDate:   startDate,
		EndDate:     endDate,
	}); err != nil {
		response.ServerError(w, err)
		return
	}

	response.OK(w, taskID)
}
Example #20
0
func GetTask(w http.ResponseWriter, r *http.Request) {
	var parser store.Parser
	taskID := parser.Int(r.FormValue("taskID"))
	if parser.Err != nil {
		response.ClientError(w, http.StatusBadRequest)
		return
	}

	task, err := store.GetTask(taskID)
	if err != nil {
		response.ServerError(w, err)
		return
	}

	if task == nil {
		response.ClientError(w, http.StatusNotFound)
		return
	}

	response.OK(w, task)
}
Example #21
0
func AssignWorker(w http.ResponseWriter, r *http.Request) {
	var parser store.Parser

	taskID := parser.Int(r.FormValue("taskID"))
	userID := parser.Int(r.FormValue("userID"))
	if parser.Err != nil {
		response.ClientError(w, http.StatusBadRequest)
		return
	}

	user := context.Get(r, "user").(store.User)
	if r.FormValue("toggle") == "true" {
		if err := store.ToggleWorker(taskID, userID, user.ID()); err != nil {
			response.ServerError(w, err)
		}
		response.OK(w, taskID)
		return
	}

	store.InsertWorker(taskID, userID, user.ID())
}
Example #22
0
// id/userID/channelID/channelType
func onNotify(channel, extra string) {
	params := strings.Split(extra, "/")
	if len(params) < 5 {
		return
	}

	var parser store.Parser
	id := parser.Int(params[1])
	userID := parser.Int(params[2])
	channelID := parser.Int(params[3])
	channelType := params[4]
	if parser.Err != nil {
		return
	}

	notification := struct {
		ID          int64  `json:"id"`
		UserID      int64  `json:"userID"`
		ChannelID   int64  `json:"channelID"`
		ChannelType string `json:"channelType"`
	}{
		ID:          id,
		UserID:      userID,
		ChannelID:   channelID,
		ChannelType: channelType,
	}

	switch channelType {
	case "user":
		h.notifyUser(userID, channelID, "newChatMessage", notification)
	case "project":
		h.notifyProject(channelID, "newChatMessage", notification)
	}
}
Example #23
0
func GetChats(w http.ResponseWriter, r *http.Request) {
	if err := r.ParseForm(); err != nil {
		response.ServerError(w, err)
		return
	}

	var parser store.Parser
	var startID int64
	var count int64 = 10

	if v, ok := r.Form["count"]; ok {
		count = parser.Int(v[0])
	}

	if v, ok := r.Form["startID"]; ok {
		startID = parser.Int(v[0])
	}

	channelID := parser.Int(r.FormValue("channelID"))
	if parser.Err != nil {
		response.ClientError(w, http.StatusBadRequest)
		return
	}
	channelType := r.FormValue("channelType")

	user := context.Get(r, "user").(store.User)
	chats, err := store.GetChats(user.ID(), channelID, channelType, startID, count)
	if err != nil {
		response.ServerError(w, err)
		return
	}

	response.OK(w, chats)
}
Example #24
0
func ToggleTaskStatus(w http.ResponseWriter, r *http.Request) {
	var parser store.Parser

	projectID := parser.Int(r.FormValue("projectID"))
	taskID := parser.Int(r.FormValue("taskID"))
	if parser.Err != nil {
		response.ClientError(w, http.StatusBadRequest)
		return
	}

	user := context.Get(r, "user").(store.User)
	if !user.IsMember(projectID) {
		response.ClientError(w, http.StatusForbidden)
		return
	}

	if err := store.ToggleTaskStatus(taskID); err != nil {
		response.ServerError(w, err)
		return
	}

	response.OK(w, taskID)
}
Example #25
0
func RemoveFriend(w http.ResponseWriter, r *http.Request) {
	var parser store.Parser
	otherUserID := parser.Int(r.FormValue("userID"))
	if parser.Err != nil {
		response.ClientError(w, http.StatusBadRequest)
		return
	}

	user := context.Get(r, "user").(store.User)
	if isFriend, err := store.IsFriend(user.ID(), otherUserID); err != nil {
		response.ServerError(w, err)
		return
	} else if !isFriend {
		response.OK(w, nil)
		return
	}

	if err := store.RemoveFriend(user.ID(), otherUserID); err != nil {
		response.ServerError(w, err)
		return
	}

	response.OK(w, nil)
}