snapshot of https://github.com/mattermost/mattermost.git (master, 2026-09-16; blobs > 131072B omitted)
Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
Server CI Nightly Race / Build Environment (push) Failing after 1s
Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
Server CI Nightly Race / Build Environment (push) Failing after 1s
This commit is contained in:
193
server/channels/store/sqlstore/status_store.go
Normal file
193
server/channels/store/sqlstore/status_store.go
Normal file
@@ -0,0 +1,193 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
sq "github.com/mattermost/squirrel"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/mattermost/mattermost/server/public/model"
|
||||
"github.com/mattermost/mattermost/server/v8/channels/store"
|
||||
)
|
||||
|
||||
type SqlStatusStore struct {
|
||||
*SqlStore
|
||||
|
||||
statusSelectQuery sq.SelectBuilder
|
||||
}
|
||||
|
||||
func statusSliceColumns() []string {
|
||||
return []string{"UserId", "Status", "Manual", "LastActivityAt", "DNDEndTime", "PrevStatus"}
|
||||
}
|
||||
|
||||
func statusToSlice(st *model.Status) []any {
|
||||
return []any{st.UserId, st.Status, st.Manual, st.LastActivityAt, st.DNDEndTime, st.PrevStatus}
|
||||
}
|
||||
|
||||
func newSqlStatusStore(sqlStore *SqlStore) store.StatusStore {
|
||||
s := SqlStatusStore{
|
||||
SqlStore: sqlStore,
|
||||
}
|
||||
|
||||
s.statusSelectQuery = s.getQueryBuilder().
|
||||
Select(
|
||||
"COALESCE(UserId, '') AS UserId",
|
||||
"COALESCE(Status, '') AS Status",
|
||||
"COALESCE(Manual, FALSE) AS Manual",
|
||||
"COALESCE(LastActivityAt, 0) AS LastActivityAt",
|
||||
"COALESCE(DNDEndTime, 0) AS DNDEndTime",
|
||||
"COALESCE(PrevStatus, '') AS PrevStatus",
|
||||
).
|
||||
From("Status")
|
||||
|
||||
return &s
|
||||
}
|
||||
|
||||
func (s SqlStatusStore) SaveOrUpdate(st *model.Status) error {
|
||||
query := s.getQueryBuilder().
|
||||
Insert("Status").
|
||||
Columns(statusSliceColumns()...).
|
||||
Values(statusToSlice(st)...)
|
||||
|
||||
query = query.SuffixExpr(sq.Expr("ON CONFLICT (userid) DO UPDATE SET Status = EXCLUDED.Status, Manual = EXCLUDED.Manual, LastActivityAt = EXCLUDED.LastActivityAt, DNDEndTime = EXCLUDED.DNDEndTime, PrevStatus = EXCLUDED.PrevStatus"))
|
||||
|
||||
if _, err := s.GetMaster().ExecBuilder(query); err != nil {
|
||||
return errors.Wrap(err, "failed to upsert Status")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s SqlStatusStore) SaveOrUpdateMany(statuses map[string]*model.Status) (retErr error) {
|
||||
if len(statuses) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// If there's only one status, use the existing method
|
||||
if len(statuses) == 1 {
|
||||
for _, st := range statuses {
|
||||
return s.SaveOrUpdate(st)
|
||||
}
|
||||
}
|
||||
|
||||
statusList := make([]*model.Status, 0, len(statuses))
|
||||
for _, st := range statuses {
|
||||
statusList = append(statusList, st)
|
||||
}
|
||||
|
||||
transaction, err := s.GetMaster().Begin()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "begin_transaction")
|
||||
}
|
||||
defer finalizeTransactionX(transaction, &retErr)
|
||||
|
||||
chunks := chunkSlice(statusList, len(statusSliceColumns()), s.SqlStore.getMaxInsertParams())
|
||||
for _, chunk := range chunks {
|
||||
query := s.getQueryBuilder().
|
||||
Insert("Status").
|
||||
Columns(statusSliceColumns()...)
|
||||
for _, st := range chunk {
|
||||
query = query.Values(statusToSlice(st)...)
|
||||
}
|
||||
query = query.SuffixExpr(sq.Expr("ON CONFLICT (userid) DO UPDATE SET Status = EXCLUDED.Status, Manual = EXCLUDED.Manual, LastActivityAt = EXCLUDED.LastActivityAt, DNDEndTime = EXCLUDED.DNDEndTime, PrevStatus = EXCLUDED.PrevStatus"))
|
||||
if _, err := transaction.ExecBuilder(query); err != nil {
|
||||
return errors.Wrap(err, "failed to upsert multiple Status records")
|
||||
}
|
||||
}
|
||||
|
||||
if err := transaction.Commit(); err != nil {
|
||||
return errors.Wrap(err, "commit_transaction")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s SqlStatusStore) Get(userId string) (*model.Status, error) {
|
||||
query := s.statusSelectQuery.Where(sq.Eq{"UserId": userId})
|
||||
|
||||
var status model.Status
|
||||
if err := s.GetReplica().GetBuilder(&status, query); err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, store.NewErrNotFound("Status", fmt.Sprintf("userId=%s", userId))
|
||||
}
|
||||
return nil, errors.Wrapf(err, "failed to get Status with userId=%s", userId)
|
||||
}
|
||||
return &status, nil
|
||||
}
|
||||
|
||||
func (s SqlStatusStore) GetByIds(userIds []string) ([]*model.Status, error) {
|
||||
query := s.statusSelectQuery.Where(sq.Eq{"UserId": userIds})
|
||||
|
||||
statuses := []*model.Status{}
|
||||
err := s.GetReplica().SelectBuilder(&statuses, query)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to find Statuses")
|
||||
}
|
||||
|
||||
return statuses, nil
|
||||
}
|
||||
|
||||
func (s SqlStatusStore) UpdateExpiredDNDStatuses() (_ []*model.Status, err error) {
|
||||
queryString := s.getQueryBuilder().
|
||||
Update("Status").
|
||||
Where(
|
||||
sq.And{
|
||||
sq.Eq{"Status": model.StatusDnd},
|
||||
sq.Gt{"DNDEndTime": 0},
|
||||
sq.LtOrEq{"DNDEndTime": time.Now().UTC().Unix()},
|
||||
},
|
||||
).
|
||||
Set("Status", sq.Expr("PrevStatus")).
|
||||
Set("PrevStatus", model.StatusDnd).
|
||||
Set("DNDEndTime", 0).
|
||||
Set("Manual", false).
|
||||
Suffix("RETURNING *")
|
||||
|
||||
statuses := []*model.Status{}
|
||||
err = s.GetMaster().SelectBuilder(&statuses, queryString)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to find Statuses")
|
||||
}
|
||||
|
||||
return statuses, nil
|
||||
}
|
||||
|
||||
func (s SqlStatusStore) ResetAll() error {
|
||||
if _, err := s.GetMaster().Exec("UPDATE Status SET Status = ? WHERE Manual = false", model.StatusOffline); err != nil {
|
||||
return errors.Wrap(err, "failed to update Statuses")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s SqlStatusStore) GetTotalActiveUsersCount() (int64, error) {
|
||||
time := model.GetMillis() - (1000 * 60 * 60 * 24)
|
||||
query := s.getQueryBuilder().
|
||||
Select("COUNT(UserId)").
|
||||
From("Status").
|
||||
Where(sq.Gt{"LastActivityAt": time})
|
||||
|
||||
var count int64
|
||||
err := s.GetReplica().GetBuilder(&count, query)
|
||||
if err != nil {
|
||||
return count, errors.Wrap(err, "failed to count active users")
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func (s SqlStatusStore) UpdateLastActivityAt(userId string, lastActivityAt int64) error {
|
||||
builder := s.getQueryBuilder().
|
||||
Update("Status").
|
||||
Set("LastActivityAt", lastActivityAt).
|
||||
Where(sq.Eq{"UserId": userId})
|
||||
|
||||
if _, err := s.GetMaster().ExecBuilder(builder); err != nil {
|
||||
return errors.Wrapf(err, "failed to update last activity for userId=%s", userId)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user