PostStore: Remove db:"-" tag and some other hacks (#19384)
Before gorp, to use sqlx we had to resort to using internal structs without the `db:"-"` tag so that it doesn't interfere with the gorp table generation. Now that it's gone, we can use the original model structs. ```release-note NONE ```
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
e23a7a2311
Коммит
6e97d581e4
@@ -909,12 +909,12 @@ func (s SqlChannelStore) Get(id string, allowFromCache bool) (*model.Channel, er
|
||||
func (s SqlChannelStore) GetPinnedPosts(channelId string) (*model.PostList, error) {
|
||||
pl := model.NewPostList()
|
||||
|
||||
posts := []*postInternal{}
|
||||
posts := []*model.Post{}
|
||||
if err := s.GetReplicaX().Select(&posts, "SELECT *, (SELECT count(Posts.Id) FROM Posts WHERE Posts.RootId = (CASE WHEN p.RootId = '' THEN p.Id ELSE p.RootId END) AND Posts.DeleteAt = 0) as ReplyCount FROM Posts p WHERE IsPinned = true AND ChannelId = ? AND DeleteAt = 0 ORDER BY CreateAt ASC", channelId); err != nil {
|
||||
return nil, errors.Wrap(err, "failed to find Posts")
|
||||
}
|
||||
for _, post := range posts {
|
||||
pl.AddPost(post.ToModel())
|
||||
pl.AddPost(post)
|
||||
pl.AddOrder(post.Id)
|
||||
}
|
||||
return pl, nil
|
||||
|
||||
@@ -35,77 +35,7 @@ type postWithExtra struct {
|
||||
ThreadReplyCount int64
|
||||
IsFollowing *bool
|
||||
ThreadParticipants model.StringArray
|
||||
postInternal
|
||||
}
|
||||
|
||||
// postInternal is an internal struct without the `db:"-"` tags
|
||||
// for sqlx to be able to scan them. This should go away once
|
||||
// we have morph for migrations.
|
||||
// It is named like this to avoid clashes with the variable name "post"
|
||||
type postInternal struct {
|
||||
Id string
|
||||
CreateAt int64
|
||||
UpdateAt int64
|
||||
EditAt int64
|
||||
DeleteAt int64
|
||||
IsPinned bool
|
||||
UserId string
|
||||
ChannelId string
|
||||
RootId string
|
||||
OriginalId string
|
||||
Message string
|
||||
MessageSource string
|
||||
Type string
|
||||
Props model.StringInterface
|
||||
Hashtags string
|
||||
Filenames model.StringArray
|
||||
FileIds model.StringArray
|
||||
PendingPostId string
|
||||
HasReactions bool
|
||||
RemoteId *string
|
||||
ReplyCount int64
|
||||
LastReplyAt int64
|
||||
Participants []*model.User
|
||||
IsFollowing *bool
|
||||
Metadata *model.PostMetadata
|
||||
}
|
||||
|
||||
func (p *postInternal) ToModel() *model.Post {
|
||||
return &model.Post{
|
||||
Id: p.Id,
|
||||
CreateAt: p.CreateAt,
|
||||
UpdateAt: p.UpdateAt,
|
||||
EditAt: p.EditAt,
|
||||
DeleteAt: p.DeleteAt,
|
||||
IsPinned: p.IsPinned,
|
||||
UserId: p.UserId,
|
||||
ChannelId: p.ChannelId,
|
||||
RootId: p.RootId,
|
||||
OriginalId: p.OriginalId,
|
||||
Message: p.Message,
|
||||
MessageSource: p.MessageSource,
|
||||
Type: p.Type,
|
||||
Props: p.Props,
|
||||
Hashtags: p.Hashtags,
|
||||
Filenames: p.Filenames,
|
||||
FileIds: p.FileIds,
|
||||
PendingPostId: p.PendingPostId,
|
||||
HasReactions: p.HasReactions,
|
||||
RemoteId: p.RemoteId,
|
||||
ReplyCount: p.ReplyCount,
|
||||
LastReplyAt: p.LastReplyAt,
|
||||
Participants: p.Participants,
|
||||
IsFollowing: p.IsFollowing,
|
||||
Metadata: p.Metadata,
|
||||
}
|
||||
}
|
||||
|
||||
func sliceToModel(posts []*postInternal) []*model.Post {
|
||||
res := make([]*model.Post, 0, len(posts))
|
||||
for _, p := range posts {
|
||||
res = append(res, p.ToModel())
|
||||
}
|
||||
return res
|
||||
model.Post
|
||||
}
|
||||
|
||||
func (s *SqlPostStore) ClearCaches() {
|
||||
@@ -554,7 +484,7 @@ func (s *SqlPostStore) GetFlaggedPostsForChannel(userId, channelId string, offse
|
||||
func (s *SqlPostStore) getFlaggedPosts(userId, channelId, teamId string, offset int, limit int) (*model.PostList, error) {
|
||||
pl := model.NewPostList()
|
||||
|
||||
posts := []*postInternal{}
|
||||
posts := []*model.Post{}
|
||||
query := `
|
||||
SELECT
|
||||
A.*, (SELECT count(*) FROM Posts WHERE Posts.RootId = (CASE WHEN A.RootId = '' THEN A.Id ELSE A.RootId END) AND Posts.DeleteAt = 0) as ReplyCount
|
||||
@@ -613,7 +543,7 @@ func (s *SqlPostStore) getFlaggedPosts(userId, channelId, teamId string, offset
|
||||
}
|
||||
|
||||
for _, post := range posts {
|
||||
pl.AddPost(post.ToModel())
|
||||
pl.AddPost(post)
|
||||
pl.AddOrder(post.Id)
|
||||
}
|
||||
|
||||
@@ -697,7 +627,7 @@ func (s *SqlPostStore) Get(ctx context.Context, id string, skipFetchThreads, col
|
||||
return nil, store.NewErrInvalidInput("Post", "id", id)
|
||||
}
|
||||
|
||||
var post postInternal
|
||||
var post model.Post
|
||||
postFetchQuery := "SELECT p.*, (SELECT count(*) FROM Posts WHERE Posts.RootId = (CASE WHEN p.RootId = '' THEN p.Id ELSE p.RootId END) AND Posts.DeleteAt = 0) as ReplyCount FROM Posts p WHERE p.Id = ? AND p.DeleteAt = 0"
|
||||
err := s.DBXFromContext(ctx).Get(&post, postFetchQuery, id)
|
||||
if err != nil {
|
||||
@@ -707,7 +637,7 @@ func (s *SqlPostStore) Get(ctx context.Context, id string, skipFetchThreads, col
|
||||
|
||||
return nil, errors.Wrapf(err, "failed to get Post with id=%s", id)
|
||||
}
|
||||
pl.AddPost(post.ToModel())
|
||||
pl.AddPost(&post)
|
||||
pl.AddOrder(id)
|
||||
if !skipFetchThreads {
|
||||
rootId := post.RootId
|
||||
@@ -720,14 +650,14 @@ func (s *SqlPostStore) Get(ctx context.Context, id string, skipFetchThreads, col
|
||||
return nil, errors.Wrapf(err, "invalid rootId with value=%s", rootId)
|
||||
}
|
||||
|
||||
posts := []*postInternal{}
|
||||
posts := []*model.Post{}
|
||||
err = s.GetReplicaX().Select(&posts, "SELECT *, (SELECT count(*) FROM Posts WHERE Posts.RootId = (CASE WHEN p.RootId = '' THEN p.Id ELSE p.RootId END) AND Posts.DeleteAt = 0) as ReplyCount FROM Posts p WHERE (Id = ? OR RootId = ?) AND DeleteAt = 0", rootId, rootId)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to find Posts")
|
||||
}
|
||||
|
||||
for _, p := range posts {
|
||||
pl.AddPost(p.ToModel())
|
||||
pl.AddPost(p)
|
||||
pl.AddOrder(p.Id)
|
||||
}
|
||||
}
|
||||
@@ -755,7 +685,7 @@ func (s *SqlPostStore) GetSingle(id string, inclDeleted bool) (*model.Post, erro
|
||||
return nil, errors.Wrap(err, "getsingleincldeleted_tosql")
|
||||
}
|
||||
|
||||
var post postInternal
|
||||
var post model.Post
|
||||
err = s.GetReplicaX().Get(&post, queryString, args...)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
@@ -764,7 +694,7 @@ func (s *SqlPostStore) GetSingle(id string, inclDeleted bool) (*model.Post, erro
|
||||
|
||||
return nil, errors.Wrapf(err, "failed to get Post with id=%s", id)
|
||||
}
|
||||
return post.ToModel(), nil
|
||||
return &post, nil
|
||||
}
|
||||
|
||||
type etagPosts struct {
|
||||
@@ -1019,16 +949,16 @@ func (s *SqlPostStore) prepareThreadedResponse(posts []*postWithExtra, extended,
|
||||
}
|
||||
|
||||
processPost := func(p *postWithExtra) error {
|
||||
p.postInternal.ReplyCount = p.ThreadReplyCount
|
||||
p.Post.ReplyCount = p.ThreadReplyCount
|
||||
if p.IsFollowing != nil {
|
||||
p.postInternal.IsFollowing = model.NewBool(*p.IsFollowing)
|
||||
p.Post.IsFollowing = model.NewBool(*p.IsFollowing)
|
||||
}
|
||||
for _, userID := range p.ThreadParticipants {
|
||||
participant, ok := usersMap[userID]
|
||||
if !ok {
|
||||
return errors.New("cannot find thread participant with id=" + userID)
|
||||
}
|
||||
p.postInternal.Participants = append(p.postInternal.Participants, participant)
|
||||
p.Post.Participants = append(p.Post.Participants, participant)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1044,8 +974,8 @@ func (s *SqlPostStore) prepareThreadedResponse(posts []*postWithExtra, extended,
|
||||
if err := processPost(posts[idx]); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
post := &posts[idx].postInternal
|
||||
list.AddPost(post.ToModel())
|
||||
post := &posts[idx].Post
|
||||
list.AddPost(post)
|
||||
list.AddOrder(posts[idx].Id)
|
||||
}
|
||||
|
||||
@@ -1174,7 +1104,7 @@ func (s *SqlPostStore) GetPostsSince(options model.GetPostsSinceOptions, allowFr
|
||||
return s.getPostsSinceCollapsedThreads(options)
|
||||
}
|
||||
|
||||
posts := []*postInternal{}
|
||||
posts := []*model.Post{}
|
||||
|
||||
order := "DESC"
|
||||
if options.SortAscending {
|
||||
@@ -1244,7 +1174,7 @@ func (s *SqlPostStore) GetPostsSince(options model.GetPostsSinceOptions, allowFr
|
||||
list := model.NewPostList()
|
||||
|
||||
for _, p := range posts {
|
||||
list.AddPost(p.ToModel())
|
||||
list.AddPost(p)
|
||||
if p.UpdateAt > options.Time {
|
||||
list.AddOrder(p.Id)
|
||||
}
|
||||
@@ -1335,7 +1265,7 @@ func (s *SqlPostStore) getPostsAround(before bool, options model.GetPostsOptions
|
||||
|
||||
offset := options.Page * options.PerPage
|
||||
posts := []*postWithExtra{}
|
||||
parents := []*postInternal{}
|
||||
parents := []*model.Post{}
|
||||
|
||||
var direction string
|
||||
var sort string
|
||||
@@ -1436,7 +1366,7 @@ func (s *SqlPostStore) getPostsAround(before bool, options model.GetPostsOptions
|
||||
}
|
||||
|
||||
for _, p := range parents {
|
||||
list.AddPost(p.ToModel())
|
||||
list.AddPost(p)
|
||||
}
|
||||
|
||||
return list, nil
|
||||
@@ -1544,7 +1474,7 @@ func (s *SqlPostStore) GetPostAfterTime(channelId string, time int64, collapsedT
|
||||
}
|
||||
|
||||
func (s *SqlPostStore) getRootPosts(channelId string, offset int, limit int, skipFetchThreads bool) ([]*model.Post, error) {
|
||||
posts := []*postInternal{}
|
||||
posts := []*model.Post{}
|
||||
var fetchQuery string
|
||||
if skipFetchThreads {
|
||||
fetchQuery = "SELECT p.*, (SELECT COUNT(*) FROM Posts WHERE Posts.RootId = (CASE WHEN p.RootId = '' THEN p.Id ELSE p.RootId END) AND Posts.DeleteAt = 0) as ReplyCount FROM Posts p WHERE ChannelId = ? AND DeleteAt = 0 ORDER BY CreateAt DESC LIMIT ? OFFSET ?"
|
||||
@@ -1555,7 +1485,7 @@ func (s *SqlPostStore) getRootPosts(channelId string, offset int, limit int, ski
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to find Posts")
|
||||
}
|
||||
return sliceToModel(posts), nil
|
||||
return posts, nil
|
||||
}
|
||||
|
||||
func (s *SqlPostStore) getParentsPosts(channelId string, offset int, limit int, skipFetchThreads bool) ([]*model.Post, error) {
|
||||
@@ -1622,7 +1552,7 @@ func (s *SqlPostStore) getParentsPosts(channelId string, offset int, limit int,
|
||||
}
|
||||
|
||||
func (s *SqlPostStore) getParentsPostsPostgreSQL(channelId string, offset int, limit int, skipFetchThreads bool) ([]*model.Post, error) {
|
||||
posts := []*postInternal{}
|
||||
posts := []*model.Post{}
|
||||
replyCountQuery := ""
|
||||
onStatement := "q1.RootId = q2.Id"
|
||||
if skipFetchThreads {
|
||||
@@ -1656,7 +1586,7 @@ func (s *SqlPostStore) getParentsPostsPostgreSQL(channelId string, offset int, l
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to find Posts with channelId=%s", channelId)
|
||||
}
|
||||
return sliceToModel(posts), nil
|
||||
return posts, nil
|
||||
}
|
||||
|
||||
var specialSearchChar = []string{
|
||||
@@ -2166,7 +2096,7 @@ func (s *SqlPostStore) GetPostsByIds(postIds []string) ([]*model.Post, error) {
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "getPostsByIds_tosql")
|
||||
}
|
||||
posts := []*postInternal{}
|
||||
posts := []*model.Post{}
|
||||
|
||||
err = s.GetReplicaX().Select(&posts, query, args...)
|
||||
if err != nil {
|
||||
@@ -2175,7 +2105,7 @@ func (s *SqlPostStore) GetPostsByIds(postIds []string) ([]*model.Post, error) {
|
||||
if len(posts) == 0 {
|
||||
return nil, store.NewErrNotFound("Post", fmt.Sprintf("postIds=%v", postIds))
|
||||
}
|
||||
return sliceToModel(posts), nil
|
||||
return posts, nil
|
||||
}
|
||||
|
||||
func (s *SqlPostStore) GetPostsBatchForIndexing(startTime int64, endTime int64, limit int) ([]*model.PostForIndexing, error) {
|
||||
|
||||
Ссылка в новой задаче
Block a user