2017-04-21 00:40:52 +02:00
|
|
|
// Copyright 2017 Vector Creations Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-04-18 11:32:32 +02:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
2022-04-13 17:41:22 +02:00
|
|
|
"database/sql"
|
2020-05-20 18:30:03 +02:00
|
|
|
"encoding/json"
|
2021-01-19 19:00:42 +01:00
|
|
|
"fmt"
|
2022-04-26 17:58:20 +02:00
|
|
|
"math"
|
2017-04-18 11:32:32 +02:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
2017-05-17 16:38:24 +02:00
|
|
|
|
2020-06-26 16:34:41 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2017-05-17 16:38:24 +02:00
|
|
|
"github.com/matrix-org/util"
|
2021-01-08 17:59:06 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-08-25 14:42:47 +02:00
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/syncapi/storage"
|
|
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
|
|
|
userapi "github.com/matrix-org/dendrite/userapi/api"
|
2017-04-18 11:32:32 +02:00
|
|
|
)
|
|
|
|
|
2019-01-10 11:28:13 +01:00
|
|
|
const defaultSyncTimeout = time.Duration(0)
|
2020-06-26 16:34:41 +02:00
|
|
|
const DefaultTimelineLimit = 20
|
2017-04-18 11:32:32 +02:00
|
|
|
|
2021-01-08 17:59:06 +01:00
|
|
|
func newSyncRequest(req *http.Request, device userapi.Device, syncDB storage.Database) (*types.SyncRequest, error) {
|
2017-04-18 11:32:32 +02:00
|
|
|
timeout := getTimeout(req.URL.Query().Get("timeout"))
|
|
|
|
fullState := req.URL.Query().Get("full_state")
|
|
|
|
wantFullState := fullState != "" && fullState != "false"
|
2020-12-18 12:11:21 +01:00
|
|
|
since, sinceStr := types.StreamingToken{}, req.URL.Query().Get("since")
|
2020-05-13 13:14:50 +02:00
|
|
|
if sinceStr != "" {
|
2020-12-18 12:11:21 +01:00
|
|
|
var err error
|
|
|
|
since, err = types.NewStreamTokenFromString(sinceStr)
|
2020-05-13 13:14:50 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-19 14:29:27 +02:00
|
|
|
}
|
2022-08-25 14:42:47 +02:00
|
|
|
|
|
|
|
// Create a default filter and apply a stored filter on top of it (if specified)
|
2021-01-19 19:00:42 +01:00
|
|
|
filter := gomatrixserverlib.DefaultFilter()
|
2020-05-20 18:30:03 +02:00
|
|
|
filterQuery := req.URL.Query().Get("filter")
|
2020-06-26 16:34:41 +02:00
|
|
|
if filterQuery != "" {
|
|
|
|
if filterQuery[0] == '{' {
|
2021-01-19 19:00:42 +01:00
|
|
|
// Parse the filter from the query string
|
|
|
|
if err := json.Unmarshal([]byte(filterQuery), &filter); err != nil {
|
|
|
|
return nil, fmt.Errorf("json.Unmarshal: %w", err)
|
2020-06-26 16:34:41 +02:00
|
|
|
}
|
|
|
|
} else {
|
2021-01-19 19:00:42 +01:00
|
|
|
// Try to load the filter from the database
|
2020-06-26 16:34:41 +02:00
|
|
|
localpart, _, err := gomatrixserverlib.SplitID('@', device.UserID)
|
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(req.Context()).WithError(err).Error("gomatrixserverlib.SplitID failed")
|
2021-01-19 19:00:42 +01:00
|
|
|
return nil, fmt.Errorf("gomatrixserverlib.SplitID: %w", err)
|
2020-06-26 16:34:41 +02:00
|
|
|
}
|
2022-04-26 17:58:20 +02:00
|
|
|
if err := syncDB.GetFilter(req.Context(), &filter, localpart, filterQuery); err != nil && err != sql.ErrNoRows {
|
2021-01-19 19:00:42 +01:00
|
|
|
util.GetLogger(req.Context()).WithError(err).Error("syncDB.GetFilter failed")
|
|
|
|
return nil, fmt.Errorf("syncDB.GetFilter: %w", err)
|
2020-06-26 16:34:41 +02:00
|
|
|
}
|
2020-05-20 18:30:03 +02:00
|
|
|
}
|
|
|
|
}
|
2021-01-08 17:59:06 +01:00
|
|
|
|
2022-08-25 14:42:47 +02:00
|
|
|
// A loaded filter might have overwritten these values,
|
|
|
|
// so set them after loading the filter.
|
|
|
|
if since.IsEmpty() {
|
|
|
|
// Send as much account data down for complete syncs as possible
|
|
|
|
// by default, otherwise clients do weird things while waiting
|
|
|
|
// for the rest of the data to trickle down.
|
|
|
|
filter.AccountData.Limit = math.MaxInt32
|
|
|
|
filter.Room.AccountData.Limit = math.MaxInt32
|
|
|
|
filter.Room.State.Limit = math.MaxInt32
|
|
|
|
}
|
|
|
|
|
2021-01-08 17:59:06 +01:00
|
|
|
logger := util.GetLogger(req.Context()).WithFields(logrus.Fields{
|
|
|
|
"user_id": device.UserID,
|
|
|
|
"device_id": device.ID,
|
|
|
|
"since": since,
|
|
|
|
"timeout": timeout,
|
2021-01-19 19:00:42 +01:00
|
|
|
"limit": filter.Room.Timeline.Limit,
|
2021-01-08 17:59:06 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
return &types.SyncRequest{
|
|
|
|
Context: req.Context(), //
|
|
|
|
Log: logger, //
|
|
|
|
Device: &device, //
|
|
|
|
Response: types.NewResponse(), // Populated by all streams
|
|
|
|
Filter: filter, //
|
|
|
|
Since: since, //
|
|
|
|
Timeout: timeout, //
|
|
|
|
Rooms: make(map[string]string), // Populated by the PDU stream
|
|
|
|
WantFullState: wantFullState, //
|
2017-04-18 11:32:32 +02:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTimeout(timeoutMS string) time.Duration {
|
|
|
|
if timeoutMS == "" {
|
|
|
|
return defaultSyncTimeout
|
|
|
|
}
|
|
|
|
i, err := strconv.Atoi(timeoutMS)
|
|
|
|
if err != nil {
|
|
|
|
return defaultSyncTimeout
|
|
|
|
}
|
|
|
|
return time.Duration(i) * time.Millisecond
|
|
|
|
}
|