func CreateValetPost(c *Context, post *model.Post) (*model.Post, *model.AppError) { post.Hashtags, _ = model.ParseHashtags(post.Message) post.Filenames = []string{} // no files allowed in valet posts yet if result := <-Srv.Store.User().GetByUsername(c.Session.TeamId, "valet"); result.Err != nil { // if the bot doesn't exist, create it if tresult := <-Srv.Store.Team().Get(c.Session.TeamId); tresult.Err != nil { return nil, tresult.Err } else { post.UserId = (CreateValet(c, tresult.Data.(*model.Team))).Id } } else { post.UserId = result.Data.(*model.User).Id } var rpost *model.Post if result := <-Srv.Store.Post().Save(post); result.Err != nil { return nil, result.Err } else { rpost = result.Data.(*model.Post) } fireAndForgetNotifications(rpost, c.Session.TeamId, c.TeamUrl) return rpost, nil }
func TestPostStoreSave(t *testing.T) { Setup() o1 := model.Post{} o1.ChannelId = model.NewId() o1.UserId = model.NewId() o1.Message = "a" + model.NewId() + "b" if err := (<-store.Post().Save(&o1)).Err; err != nil { t.Fatal("couldn't save item", err) } if err := (<-store.Post().Save(&o1)).Err; err == nil { t.Fatal("shouldn't be able to update from save") } }
func CreatePost(c *Context, post *model.Post, triggerWebhooks bool) (*model.Post, *model.AppError) { var pchan store.StoreChannel if len(post.RootId) > 0 { pchan = Srv.Store.Post().Get(post.RootId) } // Verify the parent/child relationships are correct if pchan != nil { if presult := <-pchan; presult.Err != nil { return nil, model.NewLocAppError("createPost", "api.post.create_post.root_id.app_error", nil, "") } else { list := presult.Data.(*model.PostList) if len(list.Posts) == 0 || !list.IsChannelId(post.ChannelId) { return nil, model.NewLocAppError("createPost", "api.post.create_post.channel_root_id.app_error", nil, "") } if post.ParentId == "" { post.ParentId = post.RootId } if post.RootId != post.ParentId { parent := list.Posts[post.ParentId] if parent == nil { return nil, model.NewLocAppError("createPost", "api.post.create_post.parent_id.app_error", nil, "") } } } } post.CreateAt = 0 post.Hashtags, _ = model.ParseHashtags(post.Message) post.UserId = c.Session.UserId if len(post.Filenames) > 0 { doRemove := false for i := len(post.Filenames) - 1; i >= 0; i-- { path := post.Filenames[i] doRemove = false if model.UrlRegex.MatchString(path) { continue } else if model.PartialUrlRegex.MatchString(path) { matches := model.PartialUrlRegex.FindAllStringSubmatch(path, -1) if len(matches) == 0 || len(matches[0]) < 4 { doRemove = true } channelId := matches[0][1] if channelId != post.ChannelId { doRemove = true } userId := matches[0][2] if userId != post.UserId { doRemove = true } } else { doRemove = true } if doRemove { l4g.Error(utils.T("api.post.create_post.bad_filename.error"), path) post.Filenames = append(post.Filenames[:i], post.Filenames[i+1:]...) } } } var rpost *model.Post if result := <-Srv.Store.Post().Save(post); result.Err != nil { return nil, result.Err } else { rpost = result.Data.(*model.Post) handlePostEventsAndForget(c, rpost, triggerWebhooks) } return rpost, nil }
func CreatePost(c *Context, post *model.Post, doUpdateLastViewed bool) (*model.Post, *model.AppError) { var pchan store.StoreChannel if len(post.RootId) > 0 { pchan = Srv.Store.Post().Get(post.RootId) } // Verify the parent/child relationships are correct if pchan != nil { if presult := <-pchan; presult.Err != nil { return nil, model.NewAppError("createPost", "Invalid RootId parameter", "") } else { list := presult.Data.(*model.PostList) if len(list.Posts) == 0 || !list.IsChannelId(post.ChannelId) { return nil, model.NewAppError("createPost", "Invalid ChannelId for RootId parameter", "") } if post.ParentId == "" { post.ParentId = post.RootId } if post.RootId != post.ParentId { parent := list.Posts[post.ParentId] if parent == nil { return nil, model.NewAppError("createPost", "Invalid ParentId parameter", "") } } } } post.Hashtags, _ = model.ParseHashtags(post.Message) post.UserId = c.Session.UserId if len(post.Filenames) > 0 { doRemove := false for i := len(post.Filenames) - 1; i >= 0; i-- { path := post.Filenames[i] doRemove = false if model.UrlRegex.MatchString(path) { continue } else if model.PartialUrlRegex.MatchString(path) { matches := model.PartialUrlRegex.FindAllStringSubmatch(path, -1) if len(matches) == 0 || len(matches[0]) < 4 { doRemove = true } channelId := matches[0][1] if channelId != post.ChannelId { doRemove = true } userId := matches[0][2] if userId != post.UserId { doRemove = true } } else { doRemove = true } if doRemove { l4g.Error("Bad filename discarded, filename=%v", path) post.Filenames = append(post.Filenames[:i], post.Filenames[i+1:]...) } } } var rpost *model.Post if result := <-Srv.Store.Post().Save(post); result.Err != nil { return nil, result.Err } else if doUpdateLastViewed && (<-Srv.Store.Channel().UpdateLastViewedAt(post.ChannelId, c.Session.UserId)).Err != nil { return nil, result.Err } else { rpost = result.Data.(*model.Post) fireAndForgetNotifications(rpost, c.Session.TeamId, c.GetSiteURL()) } return rpost, nil }
func TestUserUnreadCount(t *testing.T) { Setup() teamId := model.NewId() c1 := model.Channel{} c1.TeamId = teamId c1.DisplayName = "Unread Messages" c1.Name = "unread-messages-" + model.NewId() c1.Type = model.CHANNEL_OPEN c2 := model.Channel{} c2.TeamId = teamId c2.DisplayName = "Unread Direct" c2.Name = "unread-direct-" + model.NewId() c2.Type = model.CHANNEL_DIRECT u1 := &model.User{} u1.Username = "******" + model.NewId() u1.Email = model.NewId() Must(store.User().Save(u1)) Must(store.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u1.Id})) u2 := &model.User{} u2.Email = model.NewId() u2.Username = "******" + model.NewId() Must(store.User().Save(u2)) Must(store.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u2.Id})) if err := (<-store.Channel().Save(&c1)).Err; err != nil { t.Fatal("couldn't save item", err) } m1 := model.ChannelMember{} m1.ChannelId = c1.Id m1.UserId = u1.Id m1.NotifyProps = model.GetDefaultChannelNotifyProps() m2 := model.ChannelMember{} m2.ChannelId = c1.Id m2.UserId = u2.Id m2.NotifyProps = model.GetDefaultChannelNotifyProps() Must(store.Channel().SaveMember(&m1)) Must(store.Channel().SaveMember(&m2)) m1.ChannelId = c2.Id m2.ChannelId = c2.Id if err := (<-store.Channel().SaveDirectChannel(&c2, &m1, &m2)).Err; err != nil { t.Fatal("couldn't save direct channel", err) } p1 := model.Post{} p1.ChannelId = c1.Id p1.UserId = u1.Id p1.Message = "this is a message for @" + u2.Username // Post one message with mention to open channel Must(store.Post().Save(&p1)) Must(store.Channel().IncrementMentionCount(c1.Id, u2.Id)) // Post 2 messages without mention to direct channel p2 := model.Post{} p2.ChannelId = c2.Id p2.UserId = u1.Id p2.Message = "first message" Must(store.Post().Save(&p2)) Must(store.Channel().IncrementMentionCount(c2.Id, u2.Id)) p3 := model.Post{} p3.ChannelId = c2.Id p3.UserId = u1.Id p3.Message = "second message" Must(store.Post().Save(&p3)) Must(store.Channel().IncrementMentionCount(c2.Id, u2.Id)) badge := (<-store.User().GetUnreadCount(u2.Id)).Data.(int64) if badge != 3 { t.Fatal("should have 3 unread messages") } badge = (<-store.User().GetUnreadCountForChannel(u2.Id, c1.Id)).Data.(int64) if badge != 1 { t.Fatal("should have 1 unread messages for that channel") } badge = (<-store.User().GetUnreadCountForChannel(u2.Id, c2.Id)).Data.(int64) if badge != 2 { t.Fatal("should have 2 unread messages for that channel") } }