Add information on individual message count

Этот коммит содержится в:
Shivashis Padhi
2022-07-28 16:36:37 +05:30
родитель f007d94155
Коммит 57e25b5d8f
2 изменённых файлов: 65 добавлений и 13 удалений

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

@@ -104,9 +104,16 @@ type DurationPostCount struct {
// Top DMs // Top DMs
type TopDM struct { type TopDM struct {
MessageCount int64 `json:"post_count"` MessageCount int64 `json:"post_count"`
Participants string `json:"-"` OutgoingMessageCount int64 `json:"outgoing_message_count"`
SecondParticipant *TopDMInsightUserInformation `json:"second_participant"` Participants string `json:"-"`
ChannelId string `json:"-"`
SecondParticipant *TopDMInsightUserInformation `json:"second_participant"`
}
type OutgoingMessageQueryResult struct {
ChannelId string
MessageCount int
} }
type TopDMList struct { type TopDMList struct {

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

@@ -2978,7 +2978,7 @@ func (s *SqlPostStore) GetTopDMsForUserSince(userID string, since int64, offset
aggregator = "string_agg(distinct cm.UserId, ',') as Participants" aggregator = "string_agg(distinct cm.UserId, ',') as Participants"
} }
topDMsBuilder := s.getQueryBuilder().Select("count(p.id) as MessageCount", aggregator).FromSelect(channelSelector, "vch"). topDMsBuilder := s.getQueryBuilder().Select("count(p.id) as MessageCount", aggregator, "vch.Id as ChannelId").FromSelect(channelSelector, "vch").
Join("ChannelMembers as cm on cm.ChannelId = vch.Id"). Join("ChannelMembers as cm on cm.ChannelId = vch.Id").
Join("Posts as p on p.ChannelId = vch.Id"). Join("Posts as p on p.ChannelId = vch.Id").
Where(sq.And{ Where(sq.And{
@@ -3012,12 +3012,16 @@ func (s *SqlPostStore) GetTopDMsForUserSince(userID string, since int64, offset
func postProcessTopDMs(s *SqlPostStore, userID string, topDMs []*model.TopDM) ([]*model.TopDM, error) { func postProcessTopDMs(s *SqlPostStore, userID string, topDMs []*model.TopDM) ([]*model.TopDM, error) {
var topDMsFiltered = []*model.TopDM{} var topDMsFiltered = []*model.TopDM{}
var secondParticipantIds []string
var channelIds []string
// identify second participant in a list of participants
for _, topDM := range topDMs { for _, topDM := range topDMs {
participants := strings.Split(topDM.Participants, ",") participants := strings.Split(topDM.Participants, ",")
var secondParticipantId string var secondParticipantId string
if len(participants) == 1 { if len(participants) == 1 {
// chatting to self // channel with self
continue secondParticipantId = "-1"
} else { } else {
// divide message count by 2, because it's counted twice due to channel memberships being 2 for dms. // divide message count by 2, because it's counted twice due to channel memberships being 2 for dms.
topDM.MessageCount = topDM.MessageCount / 2 topDM.MessageCount = topDM.MessageCount / 2
@@ -3028,15 +3032,53 @@ func postProcessTopDMs(s *SqlPostStore, userID string, topDMs []*model.TopDM) ([
secondParticipantId = participants[0] secondParticipantId = participants[0]
} }
} }
// filter topDM out if second user is bot secondParticipantIds = append(secondParticipantIds, secondParticipantId)
users, err := s.User().GetProfileByIds(context.Background(), []string{secondParticipantId}, &store.UserGetByIdsOpts{}, true) channelIds = append(channelIds, topDM.ChannelId)
if err != nil { }
return nil, errors.Wrapf(err, "failed to get second participant information for user-id: %s", topDM.SecondParticipant.Id)
} // get user profiles
if users[0].IsBot { users, err := s.User().GetProfileByIds(context.Background(), secondParticipantIds, &store.UserGetByIdsOpts{}, true)
if err != nil {
return nil, errors.Wrapf(err, "failed to get second participants' information")
}
// get outgoing message count for userId
outgoingMessagesQuery := s.getQueryBuilder().Select("ch.Id as ChannelId, count(p.Id) as MessageCount").From("Channels as ch").
Join("Posts as p on p.ChannelId=ch.Id").Where(sq.Eq{
"ch.Id": channelIds,
"p.UserId": userID,
}).GroupBy("ch.Id")
outgoingMessages := make([]*model.OutgoingMessageQueryResult, 0)
sql, args, err := outgoingMessagesQuery.ToSql()
if err != nil {
return nil, errors.Wrap(err, "GetTopDMsForUserSince_outgoingMessagesQuery_ToSql")
}
err = s.GetReplicaX().Select(&outgoingMessages, sql, args...)
if err != nil {
return nil, errors.Wrapf(err, "failed to find top DMs for user-id: %s", userID)
}
// create map of channelId -> MessageCount
outgoingMessagesMap := make(map[string]int)
for _, outgoingMessage := range outgoingMessages {
outgoingMessagesMap[outgoingMessage.ChannelId] = outgoingMessage.MessageCount
}
// create map of userId -> User
usersMap := make(map[string]*model.User)
for _, user := range users {
usersMap[user.Id] = user
}
for index, topDM := range topDMs {
if secondParticipantIds[index] == "-1" {
continue
}
user := usersMap[secondParticipantIds[index]]
if user.IsBot {
continue continue
} }
user := users[0]
topDM.SecondParticipant = &model.TopDMInsightUserInformation{ topDM.SecondParticipant = &model.TopDMInsightUserInformation{
InsightUserInformation: model.InsightUserInformation{ InsightUserInformation: model.InsightUserInformation{
Id: user.Id, Id: user.Id,
@@ -3048,8 +3090,11 @@ func postProcessTopDMs(s *SqlPostStore, userID string, topDMs []*model.TopDM) ([
}, },
Position: user.Position, Position: user.Position,
} }
topDM.OutgoingMessageCount = int64(outgoingMessagesMap[topDM.ChannelId])
topDMsFiltered = append(topDMsFiltered, topDM) topDMsFiltered = append(topDMsFiltered, topDM)
} }
return topDMsFiltered, nil return topDMsFiltered, nil
} }