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) }
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) }
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) }
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) }
func CreateMilestone(w http.ResponseWriter, r *http.Request) { var parser store.Parser projectID := parser.Int(r.FormValue("projectID")) 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") var milestoneID int64 var err error if milestoneID, err = store.CreateMilestone(store.CreateMilestoneParams{ ProjectID: projectID, Title: title, Description: description, Date: date, }); err != nil { response.ServerError(w, err) return } response.OK(w, milestoneID) }
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) }
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) }
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) }
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) }
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) }
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) }
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) }
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) }
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) }
// 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) } }
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) }
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) }
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) }
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 } }
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) }
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()) }
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) }
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) }
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) }