func ServeCreateTopic(c gox.Context) { t := &Topic{} d := c.Request().Data d.AssignTo(t, "param") t.UserID = c.UserID() if len(t.Content) == 0 { c.SendMsg(gox.BadRequest, "missing content") return } if t.ChannelID > 0 { channel, _ := _db.GetChannel(t.ChannelID) if channel == nil { c.SendMsg(gox.NotFound, "channel not found") return } } err := _db.InsertTopic(t) if err != nil { c.SendCode(gox.ServerError) } else { _cache.SetTopic(t) c.SendData(gox.M{"topic": t}) } }
func ServeGetUserTopics(c gox.Context) { d := c.Request().Data userID := d.DGetID("user_id", c.UserID()) sinceID := d.GetID("since_id") size := d.DGetInt("size", DefaultPageSize) switch { case sinceID < 0: c.SendMsg(gox.BadRequest, "invalid since_id") case size < 0: c.SendMsg(gox.BadRequest, "invalid size") case userID <= 0: c.SendMsg(gox.BadRequest, "missing user_id") default: topics, err := _db.GetUserTopics(userID, sinceID, size, 0) if err != nil { c.SendCode(gox.ServerError) } else { var u *accounts.User for _, t := range topics { prepareTopic(t) if u == nil { u = t.User } t.User = nil } c.SendData(gox.M{"topics": topics, "user": u}) } } }
func ServeCreateTopicComment(c gox.Context) { d := c.Request().Data cmt := &Comment{} d.AssignTo(cmt, "param") cmt.UserID = c.UserID() cmt.ItemID = d.GetID("topic_id") cmt.ItemType = ItemTypeTopic if cmt.ItemID == 0 && cmt.ToCommentID == 0 { c.SendMsg(gox.BadRequest, "missing item_id or to_comment_id") return } if len(cmt.Content) == 0 { c.SendMsg(gox.BadRequest, "missing content") return } if cmt.ToCommentID > 0 { cmt.ToComment, _ = GetComment(cmt.ToCommentID) if cmt.ToComment == nil || cmt.ItemType != cmt.ToComment.ItemType { c.SendCode(gox.NotFound) return } cmt.ItemID = cmt.ToComment.ItemID } t, _ := GetTopic(cmt.ItemID) if t == nil { c.SendMsg(gox.NotFound, "topic not found") return } err := _db.InsertComment(cmt) if err != nil { c.SendCode(gox.ServerError) return } prepareComment(cmt, 1) t.CommentsCount++ _cache.SetTopic(t) _cache.SetComment(cmt) if _callbacks.DidRecvComment != nil { _callbacks.DidRecvComment(cmt) } c.SendData(gox.M{"comment": cmt}) }
func ServeGetCommentedTopics(c gox.Context) { d := c.Request().Data page := d.GetInt("page") size := d.DGetInt("size", DefaultPageSize) if page < 0 || size < 0 { c.SendCode(gox.BadRequest) return } topics, err := _db.GetCommentedTopics(c.UserID(), page, size) if err != nil { c.SendCode(gox.ServerError) } else { sendTopics(c, topics) } }
func changeTopicAction(c gox.Context, action int, add bool) { id := c.Request().Data.GetID("topic_id") if id == 0 { c.SendCode(gox.BadRequest) return } t, _ := GetTopic(id) if t == nil { c.SendCode(gox.NotFound) return } var err error if add { err = _db.AddTopicAction(c.UserID(), id, action) } else { err = _db.DelTopicAction(c.UserID(), id, action) } if err != nil { c.SendCode(gox.ServerError) return } if add { switch action { case gox.ActionLike: t.LikesCount++ case gox.ActionFavorite: t.FavoritesCount++ default: break } if _callbacks.DidRecvTopicAction != nil { _callbacks.DidRecvTopicAction(c.UserID(), t, action) } } else { switch action { case gox.ActionLike: t.LikesCount-- if t.LikesCount < 0 { t.LikesCount = 0 } case gox.ActionFavorite: t.FavoritesCount-- if t.FavoritesCount < 0 { t.FavoritesCount = 0 } default: break } } _cache.SetTopic(t) c.SendData(gox.M{"topic": t}) }