2017-04-20 23:40:52 +01:00
|
|
|
// Copyright 2017 Vector Creations Ltd
|
Send-to-device support (#1072)
* Groundwork for send-to-device messaging
* Update sample config
* Add unstable routing for now
* Send to device consumer in sync API
* Start the send-to-device consumer
* fix indentation in dendrite-config.yaml
* Create send-to-device database tables, other tweaks
* Add some logic for send-to-device messages, add them into sync stream
* Handle incoming send-to-device messages, count them with EDU stream pos
* Undo changes to test
* pq.Array
* Fix sync
* Logging
* Fix a couple of transaction things, fix client API
* Add send-to-device test, hopefully fix bugs
* Comments
* Refactor a bit
* Fix schema
* Fix queries
* Debug logging
* Fix storing and retrieving of send-to-device messages
* Try to avoid database locks
* Update sync position
* Use latest sync position
* Jiggle about sync a bit
* Fix tests
* Break out the retrieval from the update/delete behaviour
* Comments
* nolint on getResponseWithPDUsForCompleteSync
* Try to line up sync tokens again
* Implement wildcard
* Add all send-to-device tests to whitelist, what could possibly go wrong?
* Only care about wildcard when targeted locally
* Deduplicate transactions
* Handle tokens properly, return immediately if waiting send-to-device messages
* Fix sync
* Update sytest-whitelist
* Fix copyright notice (need to do more of this)
* Comments, copyrights
* Return errors from Do, fix dendritejs
* Review comments
* Comments
* Constructor for TransactionWriter
* defletions
* Update gomatrixserverlib, sytest-blacklist
2020-06-01 17:50:19 +01:00
|
|
|
// Copyright 2017-2018 New Vector Ltd
|
|
|
|
// Copyright 2019-2020 The Matrix.org Foundation C.I.C.
|
2017-04-20 23:40:52 +01:00
|
|
|
//
|
|
|
|
// 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-07 14:32:42 +01:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
Send-to-device support (#1072)
* Groundwork for send-to-device messaging
* Update sample config
* Add unstable routing for now
* Send to device consumer in sync API
* Start the send-to-device consumer
* fix indentation in dendrite-config.yaml
* Create send-to-device database tables, other tweaks
* Add some logic for send-to-device messages, add them into sync stream
* Handle incoming send-to-device messages, count them with EDU stream pos
* Undo changes to test
* pq.Array
* Fix sync
* Logging
* Fix a couple of transaction things, fix client API
* Add send-to-device test, hopefully fix bugs
* Comments
* Refactor a bit
* Fix schema
* Fix queries
* Debug logging
* Fix storing and retrieving of send-to-device messages
* Try to avoid database locks
* Update sync position
* Use latest sync position
* Jiggle about sync a bit
* Fix tests
* Break out the retrieval from the update/delete behaviour
* Comments
* nolint on getResponseWithPDUsForCompleteSync
* Try to line up sync tokens again
* Implement wildcard
* Add all send-to-device tests to whitelist, what could possibly go wrong?
* Only care about wildcard when targeted locally
* Deduplicate transactions
* Handle tokens properly, return immediately if waiting send-to-device messages
* Fix sync
* Update sytest-whitelist
* Fix copyright notice (need to do more of this)
* Comments, copyrights
* Return errors from Do, fix dendritejs
* Review comments
* Comments
* Constructor for TransactionWriter
* defletions
* Update gomatrixserverlib, sytest-blacklist
2020-06-01 17:50:19 +01:00
|
|
|
"context"
|
2020-08-28 13:28:40 +01:00
|
|
|
"fmt"
|
2020-11-20 11:29:02 +00:00
|
|
|
"net"
|
2017-04-07 14:32:42 +01:00
|
|
|
"net/http"
|
2020-11-20 11:29:02 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2017-04-07 14:32:42 +01:00
|
|
|
"time"
|
|
|
|
|
2017-04-10 15:12:18 +01:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
2020-11-20 11:29:02 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal/config"
|
2020-07-30 11:15:46 +01:00
|
|
|
keyapi "github.com/matrix-org/dendrite/keyserver/api"
|
2020-09-04 14:25:01 +01:00
|
|
|
roomserverAPI "github.com/matrix-org/dendrite/roomserver/api"
|
2020-07-30 11:15:46 +01:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/internal"
|
2017-04-20 17:22:44 +01:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/storage"
|
|
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
2020-06-16 14:10:55 +01:00
|
|
|
userapi "github.com/matrix-org/dendrite/userapi/api"
|
2017-07-26 14:53:11 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2017-04-07 14:32:42 +01:00
|
|
|
"github.com/matrix-org/util"
|
2017-10-26 11:34:54 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
2017-04-07 14:32:42 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// RequestPool manages HTTP long-poll connections for /sync
|
|
|
|
type RequestPool struct {
|
2020-06-16 17:05:38 +01:00
|
|
|
db storage.Database
|
2020-11-20 11:29:02 +00:00
|
|
|
cfg *config.SyncAPI
|
2020-06-16 17:05:38 +01:00
|
|
|
userAPI userapi.UserInternalAPI
|
|
|
|
notifier *Notifier
|
2020-07-30 11:15:46 +01:00
|
|
|
keyAPI keyapi.KeyInternalAPI
|
2020-09-04 14:25:01 +01:00
|
|
|
rsAPI roomserverAPI.RoomserverInternalAPI
|
2020-11-20 11:29:02 +00:00
|
|
|
lastseen sync.Map
|
2017-04-10 15:12:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewRequestPool makes a new RequestPool
|
2020-07-30 11:15:46 +01:00
|
|
|
func NewRequestPool(
|
2020-11-20 11:29:02 +00:00
|
|
|
db storage.Database, cfg *config.SyncAPI, n *Notifier,
|
|
|
|
userAPI userapi.UserInternalAPI, keyAPI keyapi.KeyInternalAPI,
|
2020-09-07 14:47:59 +01:00
|
|
|
rsAPI roomserverAPI.RoomserverInternalAPI,
|
2020-07-30 11:15:46 +01:00
|
|
|
) *RequestPool {
|
2020-11-20 11:29:02 +00:00
|
|
|
rp := &RequestPool{db, cfg, userAPI, n, keyAPI, rsAPI, sync.Map{}}
|
|
|
|
go rp.cleanLastSeen()
|
|
|
|
return rp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *RequestPool) cleanLastSeen() {
|
|
|
|
for {
|
|
|
|
rp.lastseen.Range(func(key interface{}, _ interface{}) bool {
|
|
|
|
rp.lastseen.Delete(key)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
time.Sleep(time.Minute)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *RequestPool) updateLastSeen(req *http.Request, device *userapi.Device) {
|
|
|
|
if _, ok := rp.lastseen.LoadOrStore(device.UserID+device.ID, struct{}{}); ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
remoteAddr := req.RemoteAddr
|
|
|
|
if rp.cfg.RealIPHeader != "" {
|
|
|
|
if header := req.Header.Get(rp.cfg.RealIPHeader); header != "" {
|
|
|
|
// TODO: Maybe this isn't great but it will satisfy both X-Real-IP
|
|
|
|
// and X-Forwarded-For (which can be a list where the real client
|
|
|
|
// address is the first listed address). Make more intelligent?
|
|
|
|
addresses := strings.Split(header, ",")
|
|
|
|
if ip := net.ParseIP(addresses[0]); ip != nil {
|
|
|
|
remoteAddr = addresses[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lsreq := &userapi.PerformLastSeenUpdateRequest{
|
|
|
|
UserID: device.UserID,
|
|
|
|
DeviceID: device.ID,
|
|
|
|
RemoteAddr: remoteAddr,
|
|
|
|
}
|
|
|
|
lsres := &userapi.PerformLastSeenUpdateResponse{}
|
|
|
|
go rp.userAPI.PerformLastSeenUpdate(req.Context(), lsreq, lsres) // nolint:errcheck
|
|
|
|
|
|
|
|
rp.lastseen.Store(device.UserID+device.ID, time.Now())
|
2017-04-07 14:32:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// OnIncomingSyncRequest is called when a client makes a /sync request. This function MUST be
|
|
|
|
// called in a dedicated goroutine for this request. This function will block the goroutine
|
|
|
|
// until a response is ready, or it times out.
|
2020-06-16 14:10:55 +01:00
|
|
|
func (rp *RequestPool) OnIncomingSyncRequest(req *http.Request, device *userapi.Device) util.JSONResponse {
|
2018-12-18 17:56:08 +00:00
|
|
|
var syncData *types.Response
|
|
|
|
|
2017-04-07 14:32:42 +01:00
|
|
|
// Extract values from request
|
2020-06-26 15:34:41 +01:00
|
|
|
syncReq, err := newSyncRequest(req, *device, rp.db)
|
2017-04-10 15:12:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
2018-03-13 15:55:45 +00:00
|
|
|
Code: http.StatusBadRequest,
|
2017-04-10 15:12:18 +01:00
|
|
|
JSON: jsonerror.Unknown(err.Error()),
|
|
|
|
}
|
|
|
|
}
|
Send-to-device support (#1072)
* Groundwork for send-to-device messaging
* Update sample config
* Add unstable routing for now
* Send to device consumer in sync API
* Start the send-to-device consumer
* fix indentation in dendrite-config.yaml
* Create send-to-device database tables, other tweaks
* Add some logic for send-to-device messages, add them into sync stream
* Handle incoming send-to-device messages, count them with EDU stream pos
* Undo changes to test
* pq.Array
* Fix sync
* Logging
* Fix a couple of transaction things, fix client API
* Add send-to-device test, hopefully fix bugs
* Comments
* Refactor a bit
* Fix schema
* Fix queries
* Debug logging
* Fix storing and retrieving of send-to-device messages
* Try to avoid database locks
* Update sync position
* Use latest sync position
* Jiggle about sync a bit
* Fix tests
* Break out the retrieval from the update/delete behaviour
* Comments
* nolint on getResponseWithPDUsForCompleteSync
* Try to line up sync tokens again
* Implement wildcard
* Add all send-to-device tests to whitelist, what could possibly go wrong?
* Only care about wildcard when targeted locally
* Deduplicate transactions
* Handle tokens properly, return immediately if waiting send-to-device messages
* Fix sync
* Update sytest-whitelist
* Fix copyright notice (need to do more of this)
* Comments, copyrights
* Return errors from Do, fix dendritejs
* Review comments
* Comments
* Constructor for TransactionWriter
* defletions
* Update gomatrixserverlib, sytest-blacklist
2020-06-01 17:50:19 +01:00
|
|
|
|
2020-03-24 12:20:10 +00:00
|
|
|
logger := util.GetLogger(req.Context()).WithFields(log.Fields{
|
Send-to-device support (#1072)
* Groundwork for send-to-device messaging
* Update sample config
* Add unstable routing for now
* Send to device consumer in sync API
* Start the send-to-device consumer
* fix indentation in dendrite-config.yaml
* Create send-to-device database tables, other tweaks
* Add some logic for send-to-device messages, add them into sync stream
* Handle incoming send-to-device messages, count them with EDU stream pos
* Undo changes to test
* pq.Array
* Fix sync
* Logging
* Fix a couple of transaction things, fix client API
* Add send-to-device test, hopefully fix bugs
* Comments
* Refactor a bit
* Fix schema
* Fix queries
* Debug logging
* Fix storing and retrieving of send-to-device messages
* Try to avoid database locks
* Update sync position
* Use latest sync position
* Jiggle about sync a bit
* Fix tests
* Break out the retrieval from the update/delete behaviour
* Comments
* nolint on getResponseWithPDUsForCompleteSync
* Try to line up sync tokens again
* Implement wildcard
* Add all send-to-device tests to whitelist, what could possibly go wrong?
* Only care about wildcard when targeted locally
* Deduplicate transactions
* Handle tokens properly, return immediately if waiting send-to-device messages
* Fix sync
* Update sytest-whitelist
* Fix copyright notice (need to do more of this)
* Comments, copyrights
* Return errors from Do, fix dendritejs
* Review comments
* Comments
* Constructor for TransactionWriter
* defletions
* Update gomatrixserverlib, sytest-blacklist
2020-06-01 17:50:19 +01:00
|
|
|
"user_id": device.UserID,
|
|
|
|
"device_id": device.ID,
|
|
|
|
"since": syncReq.since,
|
|
|
|
"timeout": syncReq.timeout,
|
|
|
|
"limit": syncReq.limit,
|
2020-03-24 12:20:10 +00:00
|
|
|
})
|
2017-04-07 14:32:42 +01:00
|
|
|
|
2020-11-20 11:29:02 +00:00
|
|
|
rp.updateLastSeen(req, device)
|
|
|
|
|
2017-10-16 13:34:08 +01:00
|
|
|
currPos := rp.notifier.CurrentPosition()
|
|
|
|
|
Send-to-device support (#1072)
* Groundwork for send-to-device messaging
* Update sample config
* Add unstable routing for now
* Send to device consumer in sync API
* Start the send-to-device consumer
* fix indentation in dendrite-config.yaml
* Create send-to-device database tables, other tweaks
* Add some logic for send-to-device messages, add them into sync stream
* Handle incoming send-to-device messages, count them with EDU stream pos
* Undo changes to test
* pq.Array
* Fix sync
* Logging
* Fix a couple of transaction things, fix client API
* Add send-to-device test, hopefully fix bugs
* Comments
* Refactor a bit
* Fix schema
* Fix queries
* Debug logging
* Fix storing and retrieving of send-to-device messages
* Try to avoid database locks
* Update sync position
* Use latest sync position
* Jiggle about sync a bit
* Fix tests
* Break out the retrieval from the update/delete behaviour
* Comments
* nolint on getResponseWithPDUsForCompleteSync
* Try to line up sync tokens again
* Implement wildcard
* Add all send-to-device tests to whitelist, what could possibly go wrong?
* Only care about wildcard when targeted locally
* Deduplicate transactions
* Handle tokens properly, return immediately if waiting send-to-device messages
* Fix sync
* Update sytest-whitelist
* Fix copyright notice (need to do more of this)
* Comments, copyrights
* Return errors from Do, fix dendritejs
* Review comments
* Comments
* Constructor for TransactionWriter
* defletions
* Update gomatrixserverlib, sytest-blacklist
2020-06-01 17:50:19 +01:00
|
|
|
if rp.shouldReturnImmediately(syncReq) {
|
2018-12-18 17:56:08 +00:00
|
|
|
syncData, err = rp.currentSyncForUser(*syncReq, currPos)
|
2017-04-10 15:12:18 +01:00
|
|
|
if err != nil {
|
2020-03-24 12:20:10 +00:00
|
|
|
logger.WithError(err).Error("rp.currentSyncForUser failed")
|
2020-03-02 16:20:44 +00:00
|
|
|
return jsonerror.InternalServerError()
|
2017-04-10 15:12:18 +01:00
|
|
|
}
|
2020-03-24 12:20:10 +00:00
|
|
|
logger.WithField("next", syncData.NextBatch).Info("Responding immediately")
|
2017-04-10 15:12:18 +01:00
|
|
|
return util.JSONResponse{
|
2018-03-13 15:55:45 +00:00
|
|
|
Code: http.StatusOK,
|
2017-10-16 13:34:08 +01:00
|
|
|
JSON: syncData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we wait for the notifier to tell us if something *may* have
|
|
|
|
// happened. We loop in case it turns out that nothing did happen.
|
|
|
|
|
|
|
|
timer := time.NewTimer(syncReq.timeout) // case of timeout=0 is handled above
|
|
|
|
defer timer.Stop()
|
|
|
|
|
2017-10-26 11:34:54 +01:00
|
|
|
userStreamListener := rp.notifier.GetListener(*syncReq)
|
|
|
|
defer userStreamListener.Close()
|
|
|
|
|
2018-12-18 17:56:08 +00:00
|
|
|
// We need the loop in case userStreamListener wakes up even if there isn't
|
|
|
|
// anything to send down. In this case, we'll jump out of the select but
|
|
|
|
// don't want to send anything back until we get some actual content to
|
|
|
|
// respond with, so we skip the return an go back to waiting for content to
|
|
|
|
// be sent down or the request timing out.
|
|
|
|
var hasTimedOut bool
|
2019-07-12 15:59:53 +01:00
|
|
|
sincePos := *syncReq.since
|
2017-10-16 13:34:08 +01:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// Wait for notifier to wake us up
|
2019-07-12 15:59:53 +01:00
|
|
|
case <-userStreamListener.GetNotifyChannel(sincePos):
|
|
|
|
currPos = userStreamListener.GetSyncPosition()
|
|
|
|
sincePos = currPos
|
2017-10-16 13:34:08 +01:00
|
|
|
// Or for timeout to expire
|
|
|
|
case <-timer.C:
|
2018-12-18 17:56:08 +00:00
|
|
|
// We just need to ensure we get out of the select after reaching the
|
|
|
|
// timeout, but there's nothing specific we want to do in this case
|
|
|
|
// apart from that, so we do nothing except stating we're timing out
|
|
|
|
// and need to respond.
|
|
|
|
hasTimedOut = true
|
2017-10-16 13:34:08 +01:00
|
|
|
// Or for the request to be cancelled
|
|
|
|
case <-req.Context().Done():
|
2020-03-24 12:20:10 +00:00
|
|
|
logger.WithError(err).Error("request cancelled")
|
2020-03-02 16:20:44 +00:00
|
|
|
return jsonerror.InternalServerError()
|
2017-10-16 13:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Note that we don't time out during calculation of sync
|
|
|
|
// response. This ensures that we don't waste the hard work
|
|
|
|
// of calculating the sync only to get timed out before we
|
|
|
|
// can respond
|
2018-12-18 17:56:08 +00:00
|
|
|
syncData, err = rp.currentSyncForUser(*syncReq, currPos)
|
2017-10-16 13:34:08 +01:00
|
|
|
if err != nil {
|
2020-03-24 12:20:10 +00:00
|
|
|
logger.WithError(err).Error("rp.currentSyncForUser failed")
|
2020-03-02 16:20:44 +00:00
|
|
|
return jsonerror.InternalServerError()
|
2017-10-16 13:34:08 +01:00
|
|
|
}
|
2018-12-18 17:56:08 +00:00
|
|
|
|
|
|
|
if !syncData.IsEmpty() || hasTimedOut {
|
2020-03-24 12:20:10 +00:00
|
|
|
logger.WithField("next", syncData.NextBatch).WithField("timed_out", hasTimedOut).Info("Responding")
|
2017-10-16 13:34:08 +01:00
|
|
|
return util.JSONResponse{
|
2018-03-13 15:55:45 +00:00
|
|
|
Code: http.StatusOK,
|
2017-10-16 13:34:08 +01:00
|
|
|
JSON: syncData,
|
|
|
|
}
|
2017-04-10 15:12:18 +01:00
|
|
|
}
|
2017-04-07 14:32:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-30 14:52:21 +01:00
|
|
|
func (rp *RequestPool) OnIncomingKeyChangeRequest(req *http.Request, device *userapi.Device) util.JSONResponse {
|
|
|
|
from := req.URL.Query().Get("from")
|
|
|
|
to := req.URL.Query().Get("to")
|
|
|
|
if from == "" || to == "" {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: 400,
|
|
|
|
JSON: jsonerror.InvalidArgumentValue("missing ?from= or ?to="),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fromToken, err := types.NewStreamTokenFromString(from)
|
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: 400,
|
|
|
|
JSON: jsonerror.InvalidArgumentValue("bad 'from' value"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
toToken, err := types.NewStreamTokenFromString(to)
|
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: 400,
|
|
|
|
JSON: jsonerror.InvalidArgumentValue("bad 'to' value"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// work out room joins/leaves
|
|
|
|
res, err := rp.db.IncrementalSync(
|
2020-07-31 14:40:45 +01:00
|
|
|
req.Context(), types.NewResponse(), *device, fromToken, toToken, 10, false,
|
2020-07-30 14:52:21 +01:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(req.Context()).WithError(err).Error("Failed to IncrementalSync")
|
|
|
|
return jsonerror.InternalServerError()
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err = rp.appendDeviceLists(res, device.UserID, fromToken, toToken)
|
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(req.Context()).WithError(err).Error("Failed to appendDeviceLists info")
|
|
|
|
return jsonerror.InternalServerError()
|
|
|
|
}
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: 200,
|
|
|
|
JSON: struct {
|
|
|
|
Changed []string `json:"changed"`
|
|
|
|
Left []string `json:"left"`
|
|
|
|
}{
|
|
|
|
Changed: res.DeviceLists.Changed,
|
|
|
|
Left: res.DeviceLists.Left,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-03 12:29:58 +01:00
|
|
|
// nolint:gocyclo
|
|
|
|
func (rp *RequestPool) currentSyncForUser(req syncRequest, latestPos types.StreamingToken) (*types.Response, error) {
|
|
|
|
res := types.NewResponse()
|
Send-to-device support (#1072)
* Groundwork for send-to-device messaging
* Update sample config
* Add unstable routing for now
* Send to device consumer in sync API
* Start the send-to-device consumer
* fix indentation in dendrite-config.yaml
* Create send-to-device database tables, other tweaks
* Add some logic for send-to-device messages, add them into sync stream
* Handle incoming send-to-device messages, count them with EDU stream pos
* Undo changes to test
* pq.Array
* Fix sync
* Logging
* Fix a couple of transaction things, fix client API
* Add send-to-device test, hopefully fix bugs
* Comments
* Refactor a bit
* Fix schema
* Fix queries
* Debug logging
* Fix storing and retrieving of send-to-device messages
* Try to avoid database locks
* Update sync position
* Use latest sync position
* Jiggle about sync a bit
* Fix tests
* Break out the retrieval from the update/delete behaviour
* Comments
* nolint on getResponseWithPDUsForCompleteSync
* Try to line up sync tokens again
* Implement wildcard
* Add all send-to-device tests to whitelist, what could possibly go wrong?
* Only care about wildcard when targeted locally
* Deduplicate transactions
* Handle tokens properly, return immediately if waiting send-to-device messages
* Fix sync
* Update sytest-whitelist
* Fix copyright notice (need to do more of this)
* Comments, copyrights
* Return errors from Do, fix dendritejs
* Review comments
* Comments
* Constructor for TransactionWriter
* defletions
* Update gomatrixserverlib, sytest-blacklist
2020-06-01 17:50:19 +01:00
|
|
|
|
|
|
|
// See if we have any new tasks to do for the send-to-device messaging.
|
2020-10-02 12:50:58 +01:00
|
|
|
events, updates, deletions, err := rp.db.SendToDeviceUpdatesForSync(req.ctx, req.device.UserID, req.device.ID, *req.since)
|
Send-to-device support (#1072)
* Groundwork for send-to-device messaging
* Update sample config
* Add unstable routing for now
* Send to device consumer in sync API
* Start the send-to-device consumer
* fix indentation in dendrite-config.yaml
* Create send-to-device database tables, other tweaks
* Add some logic for send-to-device messages, add them into sync stream
* Handle incoming send-to-device messages, count them with EDU stream pos
* Undo changes to test
* pq.Array
* Fix sync
* Logging
* Fix a couple of transaction things, fix client API
* Add send-to-device test, hopefully fix bugs
* Comments
* Refactor a bit
* Fix schema
* Fix queries
* Debug logging
* Fix storing and retrieving of send-to-device messages
* Try to avoid database locks
* Update sync position
* Use latest sync position
* Jiggle about sync a bit
* Fix tests
* Break out the retrieval from the update/delete behaviour
* Comments
* nolint on getResponseWithPDUsForCompleteSync
* Try to line up sync tokens again
* Implement wildcard
* Add all send-to-device tests to whitelist, what could possibly go wrong?
* Only care about wildcard when targeted locally
* Deduplicate transactions
* Handle tokens properly, return immediately if waiting send-to-device messages
* Fix sync
* Update sytest-whitelist
* Fix copyright notice (need to do more of this)
* Comments, copyrights
* Return errors from Do, fix dendritejs
* Review comments
* Comments
* Constructor for TransactionWriter
* defletions
* Update gomatrixserverlib, sytest-blacklist
2020-06-01 17:50:19 +01:00
|
|
|
if err != nil {
|
2020-08-28 13:28:40 +01:00
|
|
|
return nil, fmt.Errorf("rp.db.SendToDeviceUpdatesForSync: %w", err)
|
Send-to-device support (#1072)
* Groundwork for send-to-device messaging
* Update sample config
* Add unstable routing for now
* Send to device consumer in sync API
* Start the send-to-device consumer
* fix indentation in dendrite-config.yaml
* Create send-to-device database tables, other tweaks
* Add some logic for send-to-device messages, add them into sync stream
* Handle incoming send-to-device messages, count them with EDU stream pos
* Undo changes to test
* pq.Array
* Fix sync
* Logging
* Fix a couple of transaction things, fix client API
* Add send-to-device test, hopefully fix bugs
* Comments
* Refactor a bit
* Fix schema
* Fix queries
* Debug logging
* Fix storing and retrieving of send-to-device messages
* Try to avoid database locks
* Update sync position
* Use latest sync position
* Jiggle about sync a bit
* Fix tests
* Break out the retrieval from the update/delete behaviour
* Comments
* nolint on getResponseWithPDUsForCompleteSync
* Try to line up sync tokens again
* Implement wildcard
* Add all send-to-device tests to whitelist, what could possibly go wrong?
* Only care about wildcard when targeted locally
* Deduplicate transactions
* Handle tokens properly, return immediately if waiting send-to-device messages
* Fix sync
* Update sytest-whitelist
* Fix copyright notice (need to do more of this)
* Comments, copyrights
* Return errors from Do, fix dendritejs
* Review comments
* Comments
* Constructor for TransactionWriter
* defletions
* Update gomatrixserverlib, sytest-blacklist
2020-06-01 17:50:19 +01:00
|
|
|
}
|
|
|
|
|
2017-04-11 11:52:26 +01:00
|
|
|
// TODO: handle ignored users
|
2020-10-02 12:50:58 +01:00
|
|
|
if req.since.PDUPosition() == 0 && req.since.EDUPosition() == 0 {
|
Send-to-device support (#1072)
* Groundwork for send-to-device messaging
* Update sample config
* Add unstable routing for now
* Send to device consumer in sync API
* Start the send-to-device consumer
* fix indentation in dendrite-config.yaml
* Create send-to-device database tables, other tweaks
* Add some logic for send-to-device messages, add them into sync stream
* Handle incoming send-to-device messages, count them with EDU stream pos
* Undo changes to test
* pq.Array
* Fix sync
* Logging
* Fix a couple of transaction things, fix client API
* Add send-to-device test, hopefully fix bugs
* Comments
* Refactor a bit
* Fix schema
* Fix queries
* Debug logging
* Fix storing and retrieving of send-to-device messages
* Try to avoid database locks
* Update sync position
* Use latest sync position
* Jiggle about sync a bit
* Fix tests
* Break out the retrieval from the update/delete behaviour
* Comments
* nolint on getResponseWithPDUsForCompleteSync
* Try to line up sync tokens again
* Implement wildcard
* Add all send-to-device tests to whitelist, what could possibly go wrong?
* Only care about wildcard when targeted locally
* Deduplicate transactions
* Handle tokens properly, return immediately if waiting send-to-device messages
* Fix sync
* Update sytest-whitelist
* Fix copyright notice (need to do more of this)
* Comments, copyrights
* Return errors from Do, fix dendritejs
* Review comments
* Comments
* Constructor for TransactionWriter
* defletions
* Update gomatrixserverlib, sytest-blacklist
2020-06-01 17:50:19 +01:00
|
|
|
res, err = rp.db.CompleteSync(req.ctx, res, req.device, req.limit)
|
2020-08-28 13:28:40 +01:00
|
|
|
if err != nil {
|
|
|
|
return res, fmt.Errorf("rp.db.CompleteSync: %w", err)
|
|
|
|
}
|
2017-10-16 13:34:08 +01:00
|
|
|
} else {
|
Send-to-device support (#1072)
* Groundwork for send-to-device messaging
* Update sample config
* Add unstable routing for now
* Send to device consumer in sync API
* Start the send-to-device consumer
* fix indentation in dendrite-config.yaml
* Create send-to-device database tables, other tweaks
* Add some logic for send-to-device messages, add them into sync stream
* Handle incoming send-to-device messages, count them with EDU stream pos
* Undo changes to test
* pq.Array
* Fix sync
* Logging
* Fix a couple of transaction things, fix client API
* Add send-to-device test, hopefully fix bugs
* Comments
* Refactor a bit
* Fix schema
* Fix queries
* Debug logging
* Fix storing and retrieving of send-to-device messages
* Try to avoid database locks
* Update sync position
* Use latest sync position
* Jiggle about sync a bit
* Fix tests
* Break out the retrieval from the update/delete behaviour
* Comments
* nolint on getResponseWithPDUsForCompleteSync
* Try to line up sync tokens again
* Implement wildcard
* Add all send-to-device tests to whitelist, what could possibly go wrong?
* Only care about wildcard when targeted locally
* Deduplicate transactions
* Handle tokens properly, return immediately if waiting send-to-device messages
* Fix sync
* Update sytest-whitelist
* Fix copyright notice (need to do more of this)
* Comments, copyrights
* Return errors from Do, fix dendritejs
* Review comments
* Comments
* Constructor for TransactionWriter
* defletions
* Update gomatrixserverlib, sytest-blacklist
2020-06-01 17:50:19 +01:00
|
|
|
res, err = rp.db.IncrementalSync(req.ctx, res, req.device, *req.since, latestPos, req.limit, req.wantFullState)
|
2020-08-28 13:28:40 +01:00
|
|
|
if err != nil {
|
|
|
|
return res, fmt.Errorf("rp.db.IncrementalSync: %w", err)
|
|
|
|
}
|
2017-10-16 13:34:08 +01:00
|
|
|
}
|
|
|
|
|
Upgrade gomatrixserverlib dependency (#808)
* Upgrade gomatrixserverlib dependency
Signed-off-by: Thibaut CHARLES cromfr@gmail.com
* Added missing passing sytest
Signed-off-by: Thibaut CHARLES cromfr@gmail.com
* Fix login using identifier key
Not a full fix, it only really supports logging in with
the localpart of an mxid.
Signed-off-by: Serra Allgood <serra@allgood.dev>
* Replace deprecated prometheus.InstrumentHandler and unsafe time.Ticker
* goimports
* re-add temporarily missing deps?
* Refactor InstrumentHandlerCounter definition
* URL decode args
* Return server names (#833)
* Remove unnecessary map->array processing
* Return server names in room federation directory query
* Knock off a TODO
* Fix /send_join and /send_leave (#821)
Fix the /send_join and /send_leave endpoints, so that they use the v2 endpoints as mandated by MSC1802. Also comment out the SyTest tests that are failing because of lack of support for the v1 endpoints.
* Refuse /send_join without m.room.create (#824)
Signed-off-by: Abhishek Kumar <abhishekkumar2718@gmail.com>
* AS should use the v1 endpoint, rather than r0 (#827)
* docker: Passthrough parameters to dendrite-monolith-server
* Fix copy & paste error (#812)
* Use gomatrixserverlib.Transaction instead of local type (#590) (#811)
* Move files back if linting fails (#810)
* replaced gometalinter description with golangci-lint (#837)
* Amend syncapi SQL queries to return missing columns (#840)
* This commit updates a couple of the syncapi SQL queries to return additional columns that are required/expected by rowsToStreamEvents in output_room_events_table.go.
It's not exactly clear to me yet what transaction_id and session_id do, but these being added n #367 results in state events breaking the /sync endpoint.
This is a temporary fix. We need to come up with a better solution.
* gomatrix to gomatrixserverlib on some weird line change
* Tweaks from @babolivier review comments
* Implement storage interfaces (#841)
* Implement interfaces for federationsender storage
* Implement interfaces for mediaapi storage
* Implement interfaces for publicroomsapi storage
* Implement interfaces for roomserver storage
* Implement interfaces for syncapi storage
* Implement interfaces for keydb storage
* common.PartitionStorer in publicroomsapi interface
* Update copyright notices
* make cmd directory path absolute in build.sh (#830)
* Resync testfile with current sytest pass/fail (#832)
* Resync testfile with current sytest pass/fail
* Add displayname test
* Fall back to postgres when database connection string parsing fails (#842)
* Fall back to postgres when parsing the database connection string for a URI schema fails
* Fix behaviour so that it really tries postgres when URL parsing fails and it complains about unknown schema if it succeeds
* Fix #842
* Fix #842 - again...
* Federation fixes (#845)
* Update gomatrixserverlib to p2p commit 92c0338, other tweaks
* Update gomatrixserverlib to p2p commit e5dcc65
* Rewrite getAuthChain
* Update gomatrixserverlib in go.mod/go.sum
* Correct a couple of package refs for updated gmsl/gomatrix
* Update gomatrixserverlib ref in go.mod/go.sum
* Update getAuthChain comments following @babolivier review
* Add a Sytest blacklist file (#849)
* Add more passing tests to the testfile, add test blacklist file (#848)
* CS API: Support for /messages, fixes for /sync (#847)
* Merge forward
* Tidy up a bit
* TODO: What to do with NextBatch here?
* Replace SyncPosition with PaginationToken throughout syncapi
* Fix PaginationTokens
* Fix lint errors
* Add a couple of missing functions into the syncapi external storage interface
* Some updates based on review comments from @babolivier
* Some updates based on review comments from @babolivier
* argh whitespacing
* Fix opentracing span
* Remove dead code
* Don't overshadow err (fix lint issue)
* Handle extremities after inserting event into topology
* Try insert event topology as ON CONFLICT DO NOTHING
* Prevent OOB error in addRoomDeltaToResponse
* Thwarted by gocyclo again
* Fix NewPaginationTokenFromString, define unit test for it
* Update pagination token test
* Update sytest-whitelist
* Hopefully fix some of the sync batch tokens
* Remove extraneous sync position func
* Revert to topology tokens in addRoomDeltaToResponse etc
* Fix typo
* Remove prevPDUPos as dead now that backwardTopologyPos is used instead
* Fix selectEventsWithEventIDsSQL
* Update sytest-blacklist
* Update sytest-whitelist
* Some fixes for #847 (#850)
* Fix a couple of cases where backfilling events we already had causes panics, hopefully fix ordering of events, update GMSL dependency for backfill URL fixes
* Remove commented out lines from output_room_events_table schema
* Wire up publicroomsapi for roomserver events (#851)
* Wire up publicroomsapi to roomserver events
* Remove parameter that was incorrectly brought over from p2p work
* nolint containsBackwardExtremity for now
* Store our own keys in the keydb (#853)
* Store our own keys in the keydb
The DirectKeyFetcher makes the assumption that you can always reach the key/v2/server endpoint of any server, including our own. We previously haven't bothered to store our own keys in the keydb so this would mean we end up making key requests to ourselves.
In the libp2p world as an example, self-dialling is not possible, therefore this would render it impossible to get our own keys.
This commit adds our own keys into the keydb so that we don't create unnecessarily (and maybe impossible) requests.
* Use golang.org/x/crypto/ed25519 instead of crypto/ed25519 for pre-Go 1.13
* More sync fixes (#854)
* Further sync tweaks
* Remove unnecessary blank line
* getBackwardTopologyPos always returns a usable value
* Revert order fixing
* Implement GET endpoints for account_data in clientapi (#861)
* Implement GET endpoints for account_data in clientapi
* Fix accountDB parameter
* Remove fmt.Println
* Add empty push rules into account data on account creation (#862)
* Handle kind=guest query parameter on /register (#860)
* Handle kind=guest query parameter on /register
* Reorganized imports
* Pass device_id as nil
* Added tests to systest-whitelist
* Update sytest-whitelist
* Blacklist 'displayname updates affect room member events' (#859)
* Room version abstractions (#865)
* Rough first pass at adding room version abstractions
* Define newer room versions
* Update room version metadata
* Fix roomserver/versions
* Try to fix whitespace in roomsSchema
* Implement room version capabilities in CS API (#866)
* Add wiring for querying the roomserver for the default room version
* Try to implement /capabilities for room versions
* Update copyright notices
* Update sytests, add /capabilities endpoint into CS API
* Update sytest-whitelist
* Add GetDefaultRoomVersion
* Fix cases where state package was shadowed
* Fix version formatting
* Update Dockerfile to Go 1.13.6
* oh yes types I remember
* And fix the default too
* Update documentation for Go 1.13 (#867)
* Pass cfg by reference around the codebase (#819)
* Pass cfg by reference around the codebase
* Merge branch 'master' into pass-cfg-by-ref
Co-authored-by: Neil Alexander <neilalexander@users.noreply.github.com>
* Implement missing device management features (#835)
* Implement missing device management features
Signed-off-by: Till Faelligen <tfaelligen@gmail.com>
* Add a little more documentation
* Undo changes
* Use non-anonymous struct to decode devices list
* Update sytest-whitelist
* Update sytest-whitelist
* Update sytest-blacklist
Co-authored-by: Neil Alexander <neilalexander@users.noreply.github.com>
* Adding sslmode: disable to sytest server config (#813)
Co-authored-by: Neil Alexander <neilalexander@users.noreply.github.com>
* Fix AppService bind addrs in test (#805)
Co-authored-by: Neil Alexander <neilalexander@users.noreply.github.com>
* Always defer *sql.Rows.Close and consult with Err (#844)
* Always defer *sql.Rows.Close and consult with Err
database/sql.Rows.Next() makes sure to call Close only after exhausting
result rows which would NOT happen when returning early from a bad Scan.
Close being idempotent makes it a great candidate to get always deferred
regardless of what happens later on the result set.
This change also makes sure call Err() after exhausting Next() and
propagate non-nil results from it as the documentation advises.
Closes #764
Signed-off-by: Kiril Vladimiroff <kiril@vladimiroff.org>
* Override named result parameters in last returns
Signed-off-by: Kiril Vladimiroff <kiril@vladimiroff.org>
* Do the same over new changes that got merged
Signed-off-by: Kiril Vladimiroff <kiril@vladimiroff.org>
Co-authored-by: Neil Alexander <neilalexander@users.noreply.github.com>
* Clean up
Co-authored-by: Serra Allgood <serra@allgood.dev>
Co-authored-by: Andrew Morgan <1342360+anoadragon453@users.noreply.github.com>
Co-authored-by: Neil Alexander <neilalexander@users.noreply.github.com>
Co-authored-by: Brendan Abolivier <github@brendanabolivier.com>
Co-authored-by: Abhishek Kumar <31231064+abhishekkumar2718@users.noreply.github.com>
Co-authored-by: Will Hunt <will@half-shot.uk>
Co-authored-by: S7evinK <tfaelligen@gmail.com>
Co-authored-by: Arshpreet <30545756+arsh-7@users.noreply.github.com>
Co-authored-by: Prateek Sachan <42961174+prateek2211@users.noreply.github.com>
Co-authored-by: Behouba Manassé <behouba@gmail.com>
Co-authored-by: aditsachde <23707194+aditsachde@users.noreply.github.com>
Co-authored-by: Kiril Vladimiroff <kiril@vladimiroff.org>
2020-02-11 15:46:51 +00:00
|
|
|
accountDataFilter := gomatrixserverlib.DefaultEventFilter() // TODO: use filter provided in req instead
|
2020-05-13 12:14:50 +01:00
|
|
|
res, err = rp.appendAccountData(res, req.device.UserID, req, latestPos.PDUPosition(), &accountDataFilter)
|
Send-to-device support (#1072)
* Groundwork for send-to-device messaging
* Update sample config
* Add unstable routing for now
* Send to device consumer in sync API
* Start the send-to-device consumer
* fix indentation in dendrite-config.yaml
* Create send-to-device database tables, other tweaks
* Add some logic for send-to-device messages, add them into sync stream
* Handle incoming send-to-device messages, count them with EDU stream pos
* Undo changes to test
* pq.Array
* Fix sync
* Logging
* Fix a couple of transaction things, fix client API
* Add send-to-device test, hopefully fix bugs
* Comments
* Refactor a bit
* Fix schema
* Fix queries
* Debug logging
* Fix storing and retrieving of send-to-device messages
* Try to avoid database locks
* Update sync position
* Use latest sync position
* Jiggle about sync a bit
* Fix tests
* Break out the retrieval from the update/delete behaviour
* Comments
* nolint on getResponseWithPDUsForCompleteSync
* Try to line up sync tokens again
* Implement wildcard
* Add all send-to-device tests to whitelist, what could possibly go wrong?
* Only care about wildcard when targeted locally
* Deduplicate transactions
* Handle tokens properly, return immediately if waiting send-to-device messages
* Fix sync
* Update sytest-whitelist
* Fix copyright notice (need to do more of this)
* Comments, copyrights
* Return errors from Do, fix dendritejs
* Review comments
* Comments
* Constructor for TransactionWriter
* defletions
* Update gomatrixserverlib, sytest-blacklist
2020-06-01 17:50:19 +01:00
|
|
|
if err != nil {
|
2020-08-28 13:28:40 +01:00
|
|
|
return res, fmt.Errorf("rp.appendAccountData: %w", err)
|
Send-to-device support (#1072)
* Groundwork for send-to-device messaging
* Update sample config
* Add unstable routing for now
* Send to device consumer in sync API
* Start the send-to-device consumer
* fix indentation in dendrite-config.yaml
* Create send-to-device database tables, other tweaks
* Add some logic for send-to-device messages, add them into sync stream
* Handle incoming send-to-device messages, count them with EDU stream pos
* Undo changes to test
* pq.Array
* Fix sync
* Logging
* Fix a couple of transaction things, fix client API
* Add send-to-device test, hopefully fix bugs
* Comments
* Refactor a bit
* Fix schema
* Fix queries
* Debug logging
* Fix storing and retrieving of send-to-device messages
* Try to avoid database locks
* Update sync position
* Use latest sync position
* Jiggle about sync a bit
* Fix tests
* Break out the retrieval from the update/delete behaviour
* Comments
* nolint on getResponseWithPDUsForCompleteSync
* Try to line up sync tokens again
* Implement wildcard
* Add all send-to-device tests to whitelist, what could possibly go wrong?
* Only care about wildcard when targeted locally
* Deduplicate transactions
* Handle tokens properly, return immediately if waiting send-to-device messages
* Fix sync
* Update sytest-whitelist
* Fix copyright notice (need to do more of this)
* Comments, copyrights
* Return errors from Do, fix dendritejs
* Review comments
* Comments
* Constructor for TransactionWriter
* defletions
* Update gomatrixserverlib, sytest-blacklist
2020-06-01 17:50:19 +01:00
|
|
|
}
|
2020-10-02 12:50:58 +01:00
|
|
|
res, err = rp.appendDeviceLists(res, req.device.UserID, *req.since, latestPos)
|
2020-07-30 11:15:46 +01:00
|
|
|
if err != nil {
|
2020-08-28 13:28:40 +01:00
|
|
|
return res, fmt.Errorf("rp.appendDeviceLists: %w", err)
|
2020-08-03 12:29:58 +01:00
|
|
|
}
|
|
|
|
err = internal.DeviceOTKCounts(req.ctx, rp.keyAPI, req.device.UserID, req.device.ID, res)
|
|
|
|
if err != nil {
|
2020-08-28 13:28:40 +01:00
|
|
|
return res, fmt.Errorf("internal.DeviceOTKCounts: %w", err)
|
2020-07-30 11:15:46 +01:00
|
|
|
}
|
Send-to-device support (#1072)
* Groundwork for send-to-device messaging
* Update sample config
* Add unstable routing for now
* Send to device consumer in sync API
* Start the send-to-device consumer
* fix indentation in dendrite-config.yaml
* Create send-to-device database tables, other tweaks
* Add some logic for send-to-device messages, add them into sync stream
* Handle incoming send-to-device messages, count them with EDU stream pos
* Undo changes to test
* pq.Array
* Fix sync
* Logging
* Fix a couple of transaction things, fix client API
* Add send-to-device test, hopefully fix bugs
* Comments
* Refactor a bit
* Fix schema
* Fix queries
* Debug logging
* Fix storing and retrieving of send-to-device messages
* Try to avoid database locks
* Update sync position
* Use latest sync position
* Jiggle about sync a bit
* Fix tests
* Break out the retrieval from the update/delete behaviour
* Comments
* nolint on getResponseWithPDUsForCompleteSync
* Try to line up sync tokens again
* Implement wildcard
* Add all send-to-device tests to whitelist, what could possibly go wrong?
* Only care about wildcard when targeted locally
* Deduplicate transactions
* Handle tokens properly, return immediately if waiting send-to-device messages
* Fix sync
* Update sytest-whitelist
* Fix copyright notice (need to do more of this)
* Comments, copyrights
* Return errors from Do, fix dendritejs
* Review comments
* Comments
* Constructor for TransactionWriter
* defletions
* Update gomatrixserverlib, sytest-blacklist
2020-06-01 17:50:19 +01:00
|
|
|
|
|
|
|
// Before we return the sync response, make sure that we take action on
|
|
|
|
// any send-to-device database updates or deletions that we need to do.
|
|
|
|
// Then add the updates into the sync response.
|
|
|
|
if len(updates) > 0 || len(deletions) > 0 {
|
|
|
|
// Handle the updates and deletions in the database.
|
2020-10-02 12:50:58 +01:00
|
|
|
err = rp.db.CleanSendToDeviceUpdates(context.Background(), updates, deletions, *req.since)
|
Send-to-device support (#1072)
* Groundwork for send-to-device messaging
* Update sample config
* Add unstable routing for now
* Send to device consumer in sync API
* Start the send-to-device consumer
* fix indentation in dendrite-config.yaml
* Create send-to-device database tables, other tweaks
* Add some logic for send-to-device messages, add them into sync stream
* Handle incoming send-to-device messages, count them with EDU stream pos
* Undo changes to test
* pq.Array
* Fix sync
* Logging
* Fix a couple of transaction things, fix client API
* Add send-to-device test, hopefully fix bugs
* Comments
* Refactor a bit
* Fix schema
* Fix queries
* Debug logging
* Fix storing and retrieving of send-to-device messages
* Try to avoid database locks
* Update sync position
* Use latest sync position
* Jiggle about sync a bit
* Fix tests
* Break out the retrieval from the update/delete behaviour
* Comments
* nolint on getResponseWithPDUsForCompleteSync
* Try to line up sync tokens again
* Implement wildcard
* Add all send-to-device tests to whitelist, what could possibly go wrong?
* Only care about wildcard when targeted locally
* Deduplicate transactions
* Handle tokens properly, return immediately if waiting send-to-device messages
* Fix sync
* Update sytest-whitelist
* Fix copyright notice (need to do more of this)
* Comments, copyrights
* Return errors from Do, fix dendritejs
* Review comments
* Comments
* Constructor for TransactionWriter
* defletions
* Update gomatrixserverlib, sytest-blacklist
2020-06-01 17:50:19 +01:00
|
|
|
if err != nil {
|
2020-08-28 13:28:40 +01:00
|
|
|
return res, fmt.Errorf("rp.db.CleanSendToDeviceUpdates: %w", err)
|
Send-to-device support (#1072)
* Groundwork for send-to-device messaging
* Update sample config
* Add unstable routing for now
* Send to device consumer in sync API
* Start the send-to-device consumer
* fix indentation in dendrite-config.yaml
* Create send-to-device database tables, other tweaks
* Add some logic for send-to-device messages, add them into sync stream
* Handle incoming send-to-device messages, count them with EDU stream pos
* Undo changes to test
* pq.Array
* Fix sync
* Logging
* Fix a couple of transaction things, fix client API
* Add send-to-device test, hopefully fix bugs
* Comments
* Refactor a bit
* Fix schema
* Fix queries
* Debug logging
* Fix storing and retrieving of send-to-device messages
* Try to avoid database locks
* Update sync position
* Use latest sync position
* Jiggle about sync a bit
* Fix tests
* Break out the retrieval from the update/delete behaviour
* Comments
* nolint on getResponseWithPDUsForCompleteSync
* Try to line up sync tokens again
* Implement wildcard
* Add all send-to-device tests to whitelist, what could possibly go wrong?
* Only care about wildcard when targeted locally
* Deduplicate transactions
* Handle tokens properly, return immediately if waiting send-to-device messages
* Fix sync
* Update sytest-whitelist
* Fix copyright notice (need to do more of this)
* Comments, copyrights
* Return errors from Do, fix dendritejs
* Review comments
* Comments
* Constructor for TransactionWriter
* defletions
* Update gomatrixserverlib, sytest-blacklist
2020-06-01 17:50:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(events) > 0 {
|
|
|
|
// Add the updates into the sync response.
|
|
|
|
for _, event := range events {
|
|
|
|
res.ToDevice.Events = append(res.ToDevice.Events, event.SendToDeviceEvent)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the next_batch from the sync response and increase the
|
|
|
|
// EDU counter.
|
|
|
|
if pos, perr := types.NewStreamTokenFromString(res.NextBatch); perr == nil {
|
|
|
|
pos.Positions[1]++
|
|
|
|
res.NextBatch = pos.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-03 12:29:58 +01:00
|
|
|
return res, err
|
2017-04-07 14:32:42 +01:00
|
|
|
}
|
2017-07-26 14:53:11 +01:00
|
|
|
|
2020-07-30 11:15:46 +01:00
|
|
|
func (rp *RequestPool) appendDeviceLists(
|
2020-07-30 14:52:21 +01:00
|
|
|
data *types.Response, userID string, since, to types.StreamingToken,
|
2020-07-30 11:15:46 +01:00
|
|
|
) (*types.Response, error) {
|
2020-09-07 12:38:09 +01:00
|
|
|
_, err := internal.DeviceListCatchup(context.Background(), rp.keyAPI, rp.rsAPI, userID, data, since, to)
|
2020-07-30 11:15:46 +01:00
|
|
|
if err != nil {
|
2020-08-28 13:28:40 +01:00
|
|
|
return nil, fmt.Errorf("internal.DeviceListCatchup: %w", err)
|
2020-07-30 11:15:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
2020-06-16 17:05:38 +01:00
|
|
|
// nolint:gocyclo
|
2017-08-02 16:21:35 +01:00
|
|
|
func (rp *RequestPool) appendAccountData(
|
Upgrade gomatrixserverlib dependency (#808)
* Upgrade gomatrixserverlib dependency
Signed-off-by: Thibaut CHARLES cromfr@gmail.com
* Added missing passing sytest
Signed-off-by: Thibaut CHARLES cromfr@gmail.com
* Fix login using identifier key
Not a full fix, it only really supports logging in with
the localpart of an mxid.
Signed-off-by: Serra Allgood <serra@allgood.dev>
* Replace deprecated prometheus.InstrumentHandler and unsafe time.Ticker
* goimports
* re-add temporarily missing deps?
* Refactor InstrumentHandlerCounter definition
* URL decode args
* Return server names (#833)
* Remove unnecessary map->array processing
* Return server names in room federation directory query
* Knock off a TODO
* Fix /send_join and /send_leave (#821)
Fix the /send_join and /send_leave endpoints, so that they use the v2 endpoints as mandated by MSC1802. Also comment out the SyTest tests that are failing because of lack of support for the v1 endpoints.
* Refuse /send_join without m.room.create (#824)
Signed-off-by: Abhishek Kumar <abhishekkumar2718@gmail.com>
* AS should use the v1 endpoint, rather than r0 (#827)
* docker: Passthrough parameters to dendrite-monolith-server
* Fix copy & paste error (#812)
* Use gomatrixserverlib.Transaction instead of local type (#590) (#811)
* Move files back if linting fails (#810)
* replaced gometalinter description with golangci-lint (#837)
* Amend syncapi SQL queries to return missing columns (#840)
* This commit updates a couple of the syncapi SQL queries to return additional columns that are required/expected by rowsToStreamEvents in output_room_events_table.go.
It's not exactly clear to me yet what transaction_id and session_id do, but these being added n #367 results in state events breaking the /sync endpoint.
This is a temporary fix. We need to come up with a better solution.
* gomatrix to gomatrixserverlib on some weird line change
* Tweaks from @babolivier review comments
* Implement storage interfaces (#841)
* Implement interfaces for federationsender storage
* Implement interfaces for mediaapi storage
* Implement interfaces for publicroomsapi storage
* Implement interfaces for roomserver storage
* Implement interfaces for syncapi storage
* Implement interfaces for keydb storage
* common.PartitionStorer in publicroomsapi interface
* Update copyright notices
* make cmd directory path absolute in build.sh (#830)
* Resync testfile with current sytest pass/fail (#832)
* Resync testfile with current sytest pass/fail
* Add displayname test
* Fall back to postgres when database connection string parsing fails (#842)
* Fall back to postgres when parsing the database connection string for a URI schema fails
* Fix behaviour so that it really tries postgres when URL parsing fails and it complains about unknown schema if it succeeds
* Fix #842
* Fix #842 - again...
* Federation fixes (#845)
* Update gomatrixserverlib to p2p commit 92c0338, other tweaks
* Update gomatrixserverlib to p2p commit e5dcc65
* Rewrite getAuthChain
* Update gomatrixserverlib in go.mod/go.sum
* Correct a couple of package refs for updated gmsl/gomatrix
* Update gomatrixserverlib ref in go.mod/go.sum
* Update getAuthChain comments following @babolivier review
* Add a Sytest blacklist file (#849)
* Add more passing tests to the testfile, add test blacklist file (#848)
* CS API: Support for /messages, fixes for /sync (#847)
* Merge forward
* Tidy up a bit
* TODO: What to do with NextBatch here?
* Replace SyncPosition with PaginationToken throughout syncapi
* Fix PaginationTokens
* Fix lint errors
* Add a couple of missing functions into the syncapi external storage interface
* Some updates based on review comments from @babolivier
* Some updates based on review comments from @babolivier
* argh whitespacing
* Fix opentracing span
* Remove dead code
* Don't overshadow err (fix lint issue)
* Handle extremities after inserting event into topology
* Try insert event topology as ON CONFLICT DO NOTHING
* Prevent OOB error in addRoomDeltaToResponse
* Thwarted by gocyclo again
* Fix NewPaginationTokenFromString, define unit test for it
* Update pagination token test
* Update sytest-whitelist
* Hopefully fix some of the sync batch tokens
* Remove extraneous sync position func
* Revert to topology tokens in addRoomDeltaToResponse etc
* Fix typo
* Remove prevPDUPos as dead now that backwardTopologyPos is used instead
* Fix selectEventsWithEventIDsSQL
* Update sytest-blacklist
* Update sytest-whitelist
* Some fixes for #847 (#850)
* Fix a couple of cases where backfilling events we already had causes panics, hopefully fix ordering of events, update GMSL dependency for backfill URL fixes
* Remove commented out lines from output_room_events_table schema
* Wire up publicroomsapi for roomserver events (#851)
* Wire up publicroomsapi to roomserver events
* Remove parameter that was incorrectly brought over from p2p work
* nolint containsBackwardExtremity for now
* Store our own keys in the keydb (#853)
* Store our own keys in the keydb
The DirectKeyFetcher makes the assumption that you can always reach the key/v2/server endpoint of any server, including our own. We previously haven't bothered to store our own keys in the keydb so this would mean we end up making key requests to ourselves.
In the libp2p world as an example, self-dialling is not possible, therefore this would render it impossible to get our own keys.
This commit adds our own keys into the keydb so that we don't create unnecessarily (and maybe impossible) requests.
* Use golang.org/x/crypto/ed25519 instead of crypto/ed25519 for pre-Go 1.13
* More sync fixes (#854)
* Further sync tweaks
* Remove unnecessary blank line
* getBackwardTopologyPos always returns a usable value
* Revert order fixing
* Implement GET endpoints for account_data in clientapi (#861)
* Implement GET endpoints for account_data in clientapi
* Fix accountDB parameter
* Remove fmt.Println
* Add empty push rules into account data on account creation (#862)
* Handle kind=guest query parameter on /register (#860)
* Handle kind=guest query parameter on /register
* Reorganized imports
* Pass device_id as nil
* Added tests to systest-whitelist
* Update sytest-whitelist
* Blacklist 'displayname updates affect room member events' (#859)
* Room version abstractions (#865)
* Rough first pass at adding room version abstractions
* Define newer room versions
* Update room version metadata
* Fix roomserver/versions
* Try to fix whitespace in roomsSchema
* Implement room version capabilities in CS API (#866)
* Add wiring for querying the roomserver for the default room version
* Try to implement /capabilities for room versions
* Update copyright notices
* Update sytests, add /capabilities endpoint into CS API
* Update sytest-whitelist
* Add GetDefaultRoomVersion
* Fix cases where state package was shadowed
* Fix version formatting
* Update Dockerfile to Go 1.13.6
* oh yes types I remember
* And fix the default too
* Update documentation for Go 1.13 (#867)
* Pass cfg by reference around the codebase (#819)
* Pass cfg by reference around the codebase
* Merge branch 'master' into pass-cfg-by-ref
Co-authored-by: Neil Alexander <neilalexander@users.noreply.github.com>
* Implement missing device management features (#835)
* Implement missing device management features
Signed-off-by: Till Faelligen <tfaelligen@gmail.com>
* Add a little more documentation
* Undo changes
* Use non-anonymous struct to decode devices list
* Update sytest-whitelist
* Update sytest-whitelist
* Update sytest-blacklist
Co-authored-by: Neil Alexander <neilalexander@users.noreply.github.com>
* Adding sslmode: disable to sytest server config (#813)
Co-authored-by: Neil Alexander <neilalexander@users.noreply.github.com>
* Fix AppService bind addrs in test (#805)
Co-authored-by: Neil Alexander <neilalexander@users.noreply.github.com>
* Always defer *sql.Rows.Close and consult with Err (#844)
* Always defer *sql.Rows.Close and consult with Err
database/sql.Rows.Next() makes sure to call Close only after exhausting
result rows which would NOT happen when returning early from a bad Scan.
Close being idempotent makes it a great candidate to get always deferred
regardless of what happens later on the result set.
This change also makes sure call Err() after exhausting Next() and
propagate non-nil results from it as the documentation advises.
Closes #764
Signed-off-by: Kiril Vladimiroff <kiril@vladimiroff.org>
* Override named result parameters in last returns
Signed-off-by: Kiril Vladimiroff <kiril@vladimiroff.org>
* Do the same over new changes that got merged
Signed-off-by: Kiril Vladimiroff <kiril@vladimiroff.org>
Co-authored-by: Neil Alexander <neilalexander@users.noreply.github.com>
* Clean up
Co-authored-by: Serra Allgood <serra@allgood.dev>
Co-authored-by: Andrew Morgan <1342360+anoadragon453@users.noreply.github.com>
Co-authored-by: Neil Alexander <neilalexander@users.noreply.github.com>
Co-authored-by: Brendan Abolivier <github@brendanabolivier.com>
Co-authored-by: Abhishek Kumar <31231064+abhishekkumar2718@users.noreply.github.com>
Co-authored-by: Will Hunt <will@half-shot.uk>
Co-authored-by: S7evinK <tfaelligen@gmail.com>
Co-authored-by: Arshpreet <30545756+arsh-7@users.noreply.github.com>
Co-authored-by: Prateek Sachan <42961174+prateek2211@users.noreply.github.com>
Co-authored-by: Behouba Manassé <behouba@gmail.com>
Co-authored-by: aditsachde <23707194+aditsachde@users.noreply.github.com>
Co-authored-by: Kiril Vladimiroff <kiril@vladimiroff.org>
2020-02-11 15:46:51 +00:00
|
|
|
data *types.Response, userID string, req syncRequest, currentPos types.StreamPosition,
|
|
|
|
accountDataFilter *gomatrixserverlib.EventFilter,
|
2017-08-02 16:21:35 +01:00
|
|
|
) (*types.Response, error) {
|
2017-09-22 11:34:54 +01:00
|
|
|
// TODO: Account data doesn't have a sync position of its own, meaning that
|
|
|
|
// account data might be sent multiple time to the client if multiple account
|
|
|
|
// data keys were set between two message. This isn't a huge issue since the
|
|
|
|
// duplicate data doesn't represent a huge quantity of data, but an optimisation
|
|
|
|
// here would be making sure each data is sent only once to the client.
|
2020-12-01 17:01:34 +00:00
|
|
|
if req.since == nil || (req.since.PDUPosition() == 0 && req.since.EDUPosition() == 0) {
|
2017-08-02 16:21:35 +01:00
|
|
|
// If this is the initial sync, we don't need to check if a data has
|
|
|
|
// already been sent. Instead, we send the whole batch.
|
2020-06-18 18:36:03 +01:00
|
|
|
dataReq := &userapi.QueryAccountDataRequest{
|
2020-06-16 17:05:38 +01:00
|
|
|
UserID: userID,
|
2020-06-18 18:36:03 +01:00
|
|
|
}
|
|
|
|
dataRes := &userapi.QueryAccountDataResponse{}
|
|
|
|
if err := rp.userAPI.QueryAccountData(req.ctx, dataReq, dataRes); err != nil {
|
2017-08-02 16:21:35 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-18 18:36:03 +01:00
|
|
|
for datatype, databody := range dataRes.GlobalAccountData {
|
|
|
|
data.AccountData.Events = append(
|
|
|
|
data.AccountData.Events,
|
|
|
|
gomatrixserverlib.ClientEvent{
|
|
|
|
Type: datatype,
|
|
|
|
Content: gomatrixserverlib.RawJSON(databody),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2017-08-02 16:21:35 +01:00
|
|
|
for r, j := range data.Rooms.Join {
|
2020-06-18 18:36:03 +01:00
|
|
|
for datatype, databody := range dataRes.RoomAccountData[r] {
|
|
|
|
j.AccountData.Events = append(
|
|
|
|
j.AccountData.Events,
|
|
|
|
gomatrixserverlib.ClientEvent{
|
|
|
|
Type: datatype,
|
|
|
|
Content: gomatrixserverlib.RawJSON(databody),
|
|
|
|
},
|
|
|
|
)
|
2017-08-02 16:21:35 +01:00
|
|
|
data.Rooms.Join[r] = j
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
2020-05-15 09:41:12 +01:00
|
|
|
r := types.Range{
|
|
|
|
From: req.since.PDUPosition(),
|
|
|
|
To: currentPos,
|
|
|
|
}
|
|
|
|
// If both positions are the same, it means that the data was saved after the
|
|
|
|
// latest room event. In that case, we need to decrement the old position as
|
|
|
|
// results are exclusive of Low.
|
|
|
|
if r.Low() == r.High() {
|
|
|
|
r.From--
|
|
|
|
}
|
|
|
|
|
2017-08-02 16:21:35 +01:00
|
|
|
// Sync is not initial, get all account data since the latest sync
|
2020-01-23 17:51:10 +00:00
|
|
|
dataTypes, err := rp.db.GetAccountDataInRange(
|
2020-05-15 09:41:12 +01:00
|
|
|
req.ctx, userID, r, accountDataFilter,
|
2020-01-23 17:51:10 +00:00
|
|
|
)
|
2017-07-26 14:53:11 +01:00
|
|
|
if err != nil {
|
2020-08-28 13:28:40 +01:00
|
|
|
return nil, fmt.Errorf("rp.db.GetAccountDataInRange: %w", err)
|
2017-07-26 14:53:11 +01:00
|
|
|
}
|
|
|
|
|
2017-08-02 16:21:35 +01:00
|
|
|
if len(dataTypes) == 0 {
|
2020-06-19 13:29:27 +01:00
|
|
|
// TODO: this fixes the sytest but is it the right thing to do?
|
|
|
|
dataTypes[""] = []string{"m.push_rules"}
|
2017-08-02 16:21:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate over the rooms
|
|
|
|
for roomID, dataTypes := range dataTypes {
|
|
|
|
// Request the missing data from the database
|
|
|
|
for _, dataType := range dataTypes {
|
2020-06-18 18:36:03 +01:00
|
|
|
dataReq := userapi.QueryAccountDataRequest{
|
2020-06-16 17:05:38 +01:00
|
|
|
UserID: userID,
|
|
|
|
RoomID: roomID,
|
|
|
|
DataType: dataType,
|
2020-06-18 18:36:03 +01:00
|
|
|
}
|
|
|
|
dataRes := userapi.QueryAccountDataResponse{}
|
|
|
|
err = rp.userAPI.QueryAccountData(req.ctx, &dataReq, &dataRes)
|
2017-08-02 16:21:35 +01:00
|
|
|
if err != nil {
|
2020-06-18 18:36:03 +01:00
|
|
|
continue
|
2017-08-02 16:21:35 +01:00
|
|
|
}
|
2020-06-18 18:36:03 +01:00
|
|
|
if roomID == "" {
|
|
|
|
if globalData, ok := dataRes.GlobalAccountData[dataType]; ok {
|
|
|
|
data.AccountData.Events = append(
|
|
|
|
data.AccountData.Events,
|
|
|
|
gomatrixserverlib.ClientEvent{
|
|
|
|
Type: dataType,
|
|
|
|
Content: gomatrixserverlib.RawJSON(globalData),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if roomData, ok := dataRes.RoomAccountData[roomID][dataType]; ok {
|
|
|
|
joinData := data.Rooms.Join[roomID]
|
|
|
|
joinData.AccountData.Events = append(
|
|
|
|
joinData.AccountData.Events,
|
|
|
|
gomatrixserverlib.ClientEvent{
|
|
|
|
Type: dataType,
|
|
|
|
Content: gomatrixserverlib.RawJSON(roomData),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
data.Rooms.Join[roomID] = joinData
|
|
|
|
}
|
2020-06-16 17:05:38 +01:00
|
|
|
}
|
2017-08-02 16:21:35 +01:00
|
|
|
}
|
2017-07-26 14:53:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|
2019-08-01 05:36:13 +01:00
|
|
|
|
|
|
|
// shouldReturnImmediately returns whether the /sync request is an initial sync,
|
|
|
|
// or timeout=0, or full_state=true, in any of the cases the request should
|
|
|
|
// return immediately.
|
Send-to-device support (#1072)
* Groundwork for send-to-device messaging
* Update sample config
* Add unstable routing for now
* Send to device consumer in sync API
* Start the send-to-device consumer
* fix indentation in dendrite-config.yaml
* Create send-to-device database tables, other tweaks
* Add some logic for send-to-device messages, add them into sync stream
* Handle incoming send-to-device messages, count them with EDU stream pos
* Undo changes to test
* pq.Array
* Fix sync
* Logging
* Fix a couple of transaction things, fix client API
* Add send-to-device test, hopefully fix bugs
* Comments
* Refactor a bit
* Fix schema
* Fix queries
* Debug logging
* Fix storing and retrieving of send-to-device messages
* Try to avoid database locks
* Update sync position
* Use latest sync position
* Jiggle about sync a bit
* Fix tests
* Break out the retrieval from the update/delete behaviour
* Comments
* nolint on getResponseWithPDUsForCompleteSync
* Try to line up sync tokens again
* Implement wildcard
* Add all send-to-device tests to whitelist, what could possibly go wrong?
* Only care about wildcard when targeted locally
* Deduplicate transactions
* Handle tokens properly, return immediately if waiting send-to-device messages
* Fix sync
* Update sytest-whitelist
* Fix copyright notice (need to do more of this)
* Comments, copyrights
* Return errors from Do, fix dendritejs
* Review comments
* Comments
* Constructor for TransactionWriter
* defletions
* Update gomatrixserverlib, sytest-blacklist
2020-06-01 17:50:19 +01:00
|
|
|
func (rp *RequestPool) shouldReturnImmediately(syncReq *syncRequest) bool {
|
|
|
|
if syncReq.since == nil || syncReq.timeout == 0 || syncReq.wantFullState {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
waiting, werr := rp.db.SendToDeviceUpdatesWaiting(context.TODO(), syncReq.device.UserID, syncReq.device.ID)
|
|
|
|
return werr == nil && waiting
|
2019-08-01 05:36:13 +01:00
|
|
|
}
|