[MM-44765] Add API method for getting post's information from permalink (#21518)

* add api for getting post's information

* add information about current user state

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
Konstantinos Pittas
2022-12-05 16:04:44 +02:00
коммит произвёл GitHub
родитель 0c64252c9f
Коммит 3059abdd88
7 изменённых файлов: 398 добавлений и 0 удалений

Просмотреть файл

@@ -712,6 +712,7 @@ type AppIface interface {
GetPostIdAfterTime(channelID string, time int64, collapsedThreads bool) (string, *model.AppError)
GetPostIdBeforeTime(channelID string, time int64, collapsedThreads bool) (string, *model.AppError)
GetPostIfAuthorized(c request.CTX, postID string, session *model.Session, includeDeleted bool) (*model.Post, *model.AppError)
GetPostInfo(c request.CTX, postID string) (*model.PostInfo, *model.AppError)
GetPostThread(postID string, opts model.GetPostsOptions, userID string) (*model.PostList, *model.AppError)
GetPosts(channelID string, offset int, limit int) (*model.PostList, *model.AppError)
GetPostsAfterPost(options model.GetPostsOptions) (*model.PostList, *model.AppError)

Просмотреть файл

@@ -7952,6 +7952,28 @@ func (a *OpenTracingAppLayer) GetPostIfAuthorized(c request.CTX, postID string,
return resultVar0, resultVar1
}
func (a *OpenTracingAppLayer) GetPostInfo(c request.CTX, postID string) (*model.PostInfo, *model.AppError) {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.GetPostInfo")
a.ctx = newCtx
a.app.Srv().Store().SetContext(newCtx)
defer func() {
a.app.Srv().Store().SetContext(origCtx)
a.ctx = origCtx
}()
defer span.Finish()
resultVar0, resultVar1 := a.app.GetPostInfo(c, postID)
if resultVar1 != nil {
span.LogFields(spanlog.Error(resultVar1))
ext.Error.Set(span, true)
}
return resultVar0, resultVar1
}
func (a *OpenTracingAppLayer) GetPostThread(postID string, opts model.GetPostsOptions, userID string) (*model.PostList, *model.AppError) {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.GetPostThread")

Просмотреть файл

@@ -2151,6 +2151,74 @@ func (a *App) CheckPostReminders() {
}
func (a *App) GetPostInfo(c request.CTX, postID string) (*model.PostInfo, *model.AppError) {
userID := c.Session().UserId
post, appErr := a.GetSinglePost(postID, false)
if appErr != nil {
return nil, appErr
}
channel, appErr := a.GetChannel(c, post.ChannelId)
if appErr != nil {
return nil, appErr
}
notFoundError := model.NewAppError("GetPostInfo", "app.post.get.app_error", nil, "", http.StatusNotFound)
var team *model.Team
hasPermissionToAccessTeam := false
if channel.TeamId != "" {
team, appErr = a.GetTeam(channel.TeamId)
if appErr != nil {
return nil, appErr
}
if team.Type == model.TeamOpen {
hasPermissionToAccessTeam = a.HasPermissionToTeam(userID, team.Id, model.PermissionJoinPublicTeams)
} else if team.Type == model.TeamInvite {
hasPermissionToAccessTeam = a.HasPermissionToTeam(userID, team.Id, model.PermissionJoinPrivateTeams)
}
} else {
// This happens in case of DMs and GMs.
hasPermissionToAccessTeam = true
}
if !hasPermissionToAccessTeam {
return nil, notFoundError
}
hasPermissionToAccessChannel := false
if channel.Type == model.ChannelTypeOpen {
hasPermissionToAccessChannel = true
} else if channel.Type == model.ChannelTypePrivate {
hasPermissionToAccessChannel = a.HasPermissionToChannel(c, userID, channel.Id, model.PermissionManagePrivateChannelMembers)
} else if channel.Type == model.ChannelTypeDirect || channel.Type == model.ChannelTypeGroup {
hasPermissionToAccessChannel = a.HasPermissionToChannel(c, userID, channel.Id, model.PermissionReadChannel)
}
if !hasPermissionToAccessChannel {
return nil, notFoundError
}
_, channelMemberErr := a.GetChannelMember(c, channel.Id, userID)
info := model.PostInfo{
ChannelId: channel.Id,
ChannelType: channel.Type,
ChannelDisplayName: channel.DisplayName,
HasJoinedChannel: channelMemberErr == nil,
}
if team != nil {
_, teamMemberErr := a.GetTeamMember(team.Id, userID)
info.TeamId = team.Id
info.TeamType = team.Type
info.TeamDisplayName = team.DisplayName
info.HasJoinedTeam = teamMemberErr == nil
}
return &info, nil
}
func includeEmbedsAndImages(a *App, c request.CTX, topThreadList *model.TopThreadList, userID string) (*model.TopThreadList, error) {
for _, topThread := range topThreadList.Items {
topThread.Post = a.PreparePostForClientWithEmbedsAndImages(c, topThread.Post, false, false, true)