Repository: tinode/chat Branch: master Commit: a9164d34d9de Files: 230 Total size: 2.5 MB Directory structure: gitextract_oztk1vft/ ├── .gitattributes ├── .github/ │ └── ISSUE_TEMPLATE/ │ ├── bug_report.md │ ├── config.yml │ └── feature_request.md ├── CONTRIBUTING.md ├── INSTALL.md ├── LICENSE ├── README.md ├── README_ko.md ├── SECURITY.md ├── build-all.sh ├── build-py-grpc.sh ├── chatbot/ │ ├── LICENSE │ ├── README.md │ ├── csharp/ │ │ └── README.md │ └── python/ │ ├── .gitignore │ ├── README.md │ ├── basic-cookie.sample │ ├── chatbot.py │ ├── quotes.txt │ ├── requirements.txt │ ├── setup.py │ └── token-cookie.sample ├── docker/ │ ├── README.md │ ├── chatbot/ │ │ └── Dockerfile │ ├── docker-compose/ │ │ ├── README.md │ │ ├── cluster.mongodb.yml │ │ ├── cluster.postgres.yml │ │ ├── cluster.rethinkdb.yml │ │ ├── cluster.yml │ │ ├── single-instance.mongodb.yml │ │ ├── single-instance.postgres.yml │ │ ├── single-instance.rethinkdb.yml │ │ └── single-instance.yml │ ├── exporter/ │ │ ├── Dockerfile │ │ └── entrypoint.sh │ └── tinode/ │ ├── Dockerfile │ ├── config.template │ └── entrypoint.sh ├── docker-build.sh ├── docker-release.sh ├── docs/ │ ├── API.md │ ├── CLA.md │ ├── call-establishment.md │ ├── drafty.md │ ├── faq.md │ ├── monitoring.md │ ├── thecard.md │ └── translations.md ├── go.mod ├── go.sum ├── keygen/ │ ├── README.md │ └── keygen.go ├── loadtest/ │ ├── LICENSE │ ├── README.md │ ├── loadtest.scala │ ├── tinode.beam │ ├── tinode.erl │ ├── tinode.scala │ ├── tsung.xml │ └── users.csv ├── monitoring/ │ ├── LICENSE │ ├── README.md │ └── exporter/ │ ├── README.md │ ├── build.sh │ ├── influxdb_exporter.go │ ├── main.go │ ├── prom_exporter.go │ └── scraper.go ├── pbx/ │ ├── README.md │ ├── go-generate.sh │ ├── model.pb.go │ ├── model.proto │ ├── model_grpc.pb.go │ ├── py-generate.sh │ └── py_fix.py ├── py_grpc/ │ ├── .gitignore │ ├── LICENSE │ ├── README.md │ ├── pyproject.toml │ ├── tinode_grpc/ │ │ ├── __init__.py │ │ ├── model_pb2.py │ │ ├── model_pb2.pyi │ │ └── model_pb2_grpc.py │ └── version.py ├── rest-auth/ │ ├── README.md │ ├── auth.py │ ├── dummy_data.json │ └── requirements.txt ├── server/ │ ├── .golangci.yml │ ├── api_key.go │ ├── auth/ │ │ ├── anon/ │ │ │ └── auth_anon.go │ │ ├── auth.go │ │ ├── basic/ │ │ │ └── auth_basic.go │ │ ├── code/ │ │ │ └── auth_code.go │ │ ├── mock_auth/ │ │ │ └── mock_auth.go │ │ ├── rest/ │ │ │ ├── README.md │ │ │ └── auth_rest.go │ │ └── token/ │ │ └── auth_token.go │ ├── calls.go │ ├── cluster.go │ ├── cluster_leader.go │ ├── concurrency/ │ │ ├── goroutinepool.go │ │ └── simplemutex.go │ ├── datamodel.go │ ├── db/ │ │ ├── adapter.go │ │ ├── common/ │ │ │ ├── common.go │ │ │ ├── common_test.go │ │ │ └── test_data/ │ │ │ └── test_data.go │ │ ├── mongodb/ │ │ │ ├── adapter.go │ │ │ ├── blank.go │ │ │ ├── schema.md │ │ │ └── tests/ │ │ │ ├── mongo_test.go │ │ │ └── test.conf │ │ ├── mysql/ │ │ │ ├── adapter.go │ │ │ ├── blank.go │ │ │ ├── schema.sql │ │ │ └── tests/ │ │ │ ├── mysql_test.go │ │ │ └── test.conf │ │ ├── postgres/ │ │ │ ├── adapter.go │ │ │ ├── blank.go │ │ │ ├── schema.sql │ │ │ └── tests/ │ │ │ ├── postgres_test.go │ │ │ └── test.conf │ │ └── rethinkdb/ │ │ ├── adapter.go │ │ ├── blank.go │ │ ├── schema.md │ │ └── tests/ │ │ ├── rethink_test.go │ │ └── test.conf │ ├── drafty/ │ │ ├── drafty.go │ │ ├── drafty_test.go │ │ └── grapheme.go │ ├── hdl_files.go │ ├── hdl_grpc.go │ ├── hdl_longpoll.go │ ├── hdl_websock.go │ ├── http.go │ ├── http_pprof.go │ ├── hub.go │ ├── init_topic.go │ ├── logs/ │ │ └── logs.go │ ├── main.go │ ├── media/ │ │ ├── fs/ │ │ │ └── filesys.go │ │ ├── media.go │ │ ├── media_test.go │ │ └── s3/ │ │ └── s3.go │ ├── pbconverter.go │ ├── plugins.go │ ├── pres.go │ ├── push/ │ │ ├── common/ │ │ │ └── typedef.go │ │ ├── fcm/ │ │ │ ├── README.md │ │ │ ├── payload.go │ │ │ └── push_fcm.go │ │ ├── push.go │ │ ├── stdout/ │ │ │ ├── README.md │ │ │ └── push_stdout.go │ │ └── tnpg/ │ │ ├── README.md │ │ └── push_tnpg.go │ ├── push.go │ ├── ringhash/ │ │ ├── ringhash.go │ │ └── ringhash_test.go │ ├── run-cluster.sh │ ├── sanity-test.sh │ ├── session.go │ ├── session_test.go │ ├── sessionstore.go │ ├── stats.go │ ├── store/ │ │ ├── mock_store/ │ │ │ └── mock_store.go │ │ ├── store.go │ │ └── types/ │ │ ├── types.go │ │ ├── uidgen.go │ │ └── uidgen_test.go │ ├── templ/ │ │ ├── email-password-reset-en.templ │ │ ├── email-password-reset-es.templ │ │ ├── email-password-reset-fr.templ │ │ ├── email-password-reset-pt.templ │ │ ├── email-password-reset-ru.templ │ │ ├── email-password-reset-uk.templ │ │ ├── email-password-reset-vi.templ │ │ ├── email-password-reset-zh-TW.templ │ │ ├── email-password-reset-zh.templ │ │ ├── email-validation-en.templ │ │ ├── email-validation-es.templ │ │ ├── email-validation-fr.templ │ │ ├── email-validation-pt.templ │ │ ├── email-validation-ru.templ │ │ ├── email-validation-uk.templ │ │ ├── email-validation-vi.templ │ │ ├── email-validation-zh-TW.templ │ │ ├── email-validation-zh.templ │ │ ├── sms-universal-en.templ │ │ ├── sms-universal-es.templ │ │ ├── sms-universal-fr.templ │ │ ├── sms-universal-pt.templ │ │ ├── sms-universal-ru.templ │ │ ├── sms-universal-uk.templ │ │ ├── sms-universal-vi.templ │ │ ├── sms-universal-zh-TW.templ │ │ └── sms-universal-zh.templ │ ├── tinode.conf │ ├── topic.go │ ├── topic_proxy.go │ ├── topic_test.go │ ├── user.go │ ├── utils.go │ ├── utils_test.go │ └── validate/ │ ├── email/ │ │ └── validate.go │ ├── tel/ │ │ ├── twilio.go │ │ └── validate.go │ └── validator.go ├── tinode-db/ │ ├── README.md │ ├── credentials.sh │ ├── data.json │ ├── gendb.go │ ├── generate_dataset.py │ ├── main.go │ └── tinode.conf └── tn-cli/ ├── CODE-STRUCTURE.md ├── LICENSE ├── README.md ├── client.py ├── commands.py ├── input_handler.py ├── macros.py ├── requirements.txt ├── sample-macro-script.txt ├── sample-script.txt ├── tn-cli.py ├── tn_globals.py └── utils.py ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitattributes ================================================ model_pb2.py binary model.pb.go binary ================================================ FILE: .github/ISSUE_TEMPLATE/bug_report.md ================================================ --- name: Bug report about: Create a report to help us improve Tinode title: '' labels: 'bug' assignees: '' --- **If you are not reporting a bug, please post to https://groups.google.com/d/forum/tinode instead.** --- ### Subject of the issue Describe your issue here. ### Your environment #### Server-side - [ ] web.tinode.co, api.tinode.co - [ ] sandbox.tinode.co - [ ] Your own setup: * platform (Windows, Mac, Linux etc) * version of Tinode server, e.g. `0.15.2-rc3` * database backend * cluster or standalone #### Client-side - [ ] TinodeWeb/tinodejs: javascript client * Browser make and version. * IMPORTANT! Use `index-dev.html` to reproduce the problem, not `index.html`. - [ ] Tindroid: Android app * Android API level (e.g. 25). * Emulator or hardware, if hardware describe it. - [ ] Tinodios: iOS app * iOS version * Simulator or hardware, if hardware describe it. - [ ] tn-cli * Python version - [ ] Chatbot * Python version - Version of the client, e.g. `0.15.1` - [ ] Your own client. Describe it: * Transport (gRPC, websocket, long polling) * Programming language. * gRPC version, if applicable. ### Steps to reproduce Tell us how to reproduce this issue. ### Expected behaviour Tell us what should happen. ### Actual behaviour Tell us what happens instead. ### Server-side log Copy server-side log here. You may also attach it to the issue as a file. ### Client-side log Copy client-side log here (Android logcat, Javascript console, etc). You may also attach it to the issue as a file. When posting console log from Webapp, please use `index-dev.html`, not `index.html`; `index.html` uses minified javascript which produces unusable logs). ================================================ FILE: .github/ISSUE_TEMPLATE/config.yml ================================================ blank_issues_enabled: false ================================================ FILE: .github/ISSUE_TEMPLATE/feature_request.md ================================================ --- name: Feature request about: Suggest an idea for this project title: '' labels: 'feature request' assignees: '' --- **If you are not requesting a feature, please post to https://groups.google.com/d/forum/tinode instead.** --- **Is your feature request related to a problem? Please describe.** A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] **Describe the solution you'd like** A clear and concise description of what you want to happen. **Describe alternatives you've considered** A clear and concise description of any alternative solutions or features you've considered. **Additional context** Add any other context or screenshots about the feature request here. ================================================ FILE: CONTRIBUTING.md ================================================ # Contributing We're happy you want to contribute! You can help us in different ways: - [Open an issue](https://github.com/tinode/chat/issues) with suggestions for improvements - Fork this repository and submit a pull request - Improve the documentation To submit a pull request, fork the [repository](https://github.com/tinode/chat) and then clone your fork: git clone git@github.com:/chat.git Make your suggested changes, `git push` and then [submit a pull request](https://github.com/tinode/chat/compare/). Note that before we can accept your pull requests, you need to sign our [Contributor License Agreement](docs/CLA.md). ## Why is the Contributor License Agreement necessary? We very much appreciate your wanting to contribute to Tinode Chat, but we need to add you to the contributors list first. Note that the [agreement](docs/CLA.md) is not a transfer of copyright ownership, this simply is a license agreement for contributions. You also do not change your rights to use your own contributions for any other purpose. For some background on why contributor license agreements are necessary, you can read FAQs from many other open source projects: - Django's [CLA FAQ](https://www.djangoproject.com/foundation/cla/faq/) - A [chapter](http://producingoss.com/en/copyright-assignment.html) from Karl Fogel's _Producing Open Source Software_ on CLAs - The [Wikipedia article on CLAs](http://en.wikipedia.org/wiki/Contributor_license_agreement) This is part of the legal framework of the open-source ecosystem that adds some red tape, but protects both the contributor and the company / foundation behind the project. It also gives us the option to relicense the code with a more permissive license in the future. ================================================ FILE: INSTALL.md ================================================ # Installing Tinode The config file [`tinode.conf`](./server/tinode.conf) contains extensive instructions on configuring the server. ## Installing from Binaries 1. Visit the [Releases page](https://github.com/tinode/chat/releases/), choose the latest or otherwise the most suitable release. From the list of binaries download the one for your database (supported: MySQL, PostgreSQL, MongoDB, RethinkDB) and platform (Linux ARM or Intel, Windows, Mac ARM or Intel). Once the binary is downloaded, unpack it to a directory of your choosing, `cd` to that directory. 2. Make sure your database is running. Make sure it's configured to accept connections from `localhost`. In case of MySQL, Tinode will try to connect as `root` without the password. In case of PostgreSQL, Tinode will try connect as `postgres` with the password `postgres`. See notes below (_Building from Source_, section 4) on how to configure Tinode to use a different user or a password. MySQL 5.7 or above is required (use InnoDB, not MyISAM storage engine). MySQL 5.6 or below **will not work**, use of MyISAM **will cause problems**. PostgreSQL 13 or above is required. PostgreSQL 12 or below **will not work**. MongoDB 4.4 or above is required. MongoDB 4.2 and below **will not work**. 3. Run the database initializer `init-db` (or `init-db.exe` on Windows): ``` ./init-db -data=data.json ``` 4. Run the `tinode` (or `tinode.exe` on Windows) server. It will work without any parameters. ``` ./tinode ``` 5. Test your installation by pointing your browser to http://localhost:6060/ ## Docker See [instructions](./docker/README.md) ## Building from Source 1. Install [Go environment](https://golang.org/doc/install). The installation instructions below are for Go 1.18 and newer. Building with the latest Go environment is recommended. 2. OPTIONAL only if you intend to modify the code: Install [protobuf](https://developers.google.com/protocol-buffers/) and [gRPC](https://grpc.io/docs/languages/go/quickstart/) including [code generator](https://developers.google.com/protocol-buffers/docs/reference/go-generated) for Go. 3. Make sure one of the following databases is installed and running: * MySQL 5.7 or above configured with `InnoDB` engine (8.x preferred). MySQL 5.6 or below **will not work**. * PostgreSQL 13 or above. PostgreSQL 12 or below **will not work**. * MongoDB 4.4 or above (8.x preferred). MongoDB 4.2 and below **will not work**. * RethinkDB (deprecated, support will be dropped in 2027 unless RethinkDB team resumes development). 4. Fetch, build Tinode server and tinode-db database initializer: - **MySQL**: ``` go install -tags mysql github.com/tinode/chat/server@latest go install -tags mysql github.com/tinode/chat/tinode-db@latest ``` - **PostgreSQL**: ``` go install -tags postgres github.com/tinode/chat/server@latest go install -tags postgres github.com/tinode/chat/tinode-db@latest ``` - **MongoDB**: ``` go install -tags mongodb github.com/tinode/chat/server@latest go install -tags mongodb github.com/tinode/chat/tinode-db@latest ``` - **RethinkDb**: ``` go install -tags rethinkdb github.com/tinode/chat/server@latest go install -tags rethinkdb github.com/tinode/chat/tinode-db@latest ``` - **All** (bundle all of the above DB adapters): ``` go install -tags "mysql rethinkdb mongodb postgres" github.com/tinode/chat/server@latest go install -tags "mysql rethinkdb mongodb postgres" github.com/tinode/chat/tinode-db@latest ``` The steps above install Tinode binaries at `$GOPATH/bin/`, sorces and supporting files are located at `$GOPATH/pkg/mod/github.com/tinode/chat@vX.XX.X/` where `X.XX.X` is the version you installed, such as `0.19.1`. Note the required **`-tags rethinkdb`**, **`-tags mysql`**, **`-tags mongodb`** or **`-tags postgres`** build option. You may also optionally define `main.buildstamp` for the server by adding a build option, for instance, with a timestamp: ``` go install -tags mysql -ldflags "-X main.buildstamp=`date -u '+%Y%m%dT%H:%M:%SZ'`" github.com/tinode/chat/server@latest ``` The value of `buildstamp` will be sent by the server to the clients. Building with Go 1.17 or below **will fail**! 5. Open `tinode.conf` (located at `$GOPATH/pkg/mod/github.com/tinode/chat@vX.XX.X/server/`). Check that the database connection parameters are correct for your database. If you are using MySQL make sure [DSN](https://github.com/go-sql-driver/mysql#dsn-data-source-name) in `"mysql"` section is appropriate for your MySQL installation. Option `parseTime=true` is required. ```js "mysql": { "dsn": "root@tcp(localhost)/tinode?parseTime=true", "database": "tinode" }, ``` 6. Make sure you specify the adapter name in your `tinode.conf`. E.g. you want to run Tinode with MySQL: ```js "store_config": { ... "use_adapter": "mysql", ... }, ``` 7. Now that you have built the binaries, follow instructions in the _Running a Standalone Server_ section. ## Running a Standalone Server If you followed instructions in the previous section then the Tinode binaries are installed in `$GOPATH/bin/`, the sources and supporting files are located in `$GOPATH/pkg/mod/github.com/tinode/chat@vX.XX.X/`, where `X.XX.X` is the version you installed, for example `0.19.1`. Switch to sources directory (replace `X.XX.X` with your actual version, such as `0.19.1`): ``` cd $GOPATH/pkg/mod/github.com/tinode/chat@vX.XX.X ``` 1. Make sure your database is running: - **MySQL**: https://dev.mysql.com/doc/mysql-startstop-excerpt/5.7/en/mysql-server.html ``` mysql.server start ``` - **PostgreSQL**: https://www.postgresql.org/docs/current/app-pg-ctl.html ``` pg_ctl start ``` - **MongoDB**: https://docs.mongodb.com/manual/administration/install-community/ MongoDB should run as single node replicaset. See https://docs.mongodb.com/manual/administration/replica-set-deployment/ ``` mongod ``` - **RethinkDB**: https://www.rethinkdb.com/docs/start-a-server/ ``` rethinkdb --bind all --daemon ``` 2. Run DB initializer ``` $GOPATH/bin/tinode-db -config=./tinode-db/tinode.conf ``` add `-data=./tinode-db/data.json` flag if you want sample data to be loaded: ``` $GOPATH/bin/tinode-db -config=./tinode-db/tinode.conf -data=./tinode-db/data.json ``` DB initializer needs to be run only once per installation. See [instructions](tinode-db/README.md) for more options. 3. Unpack JS client to a directory, for instance `$HOME/tinode/webapp/` by unzipping `https://github.com/tinode/webapp/archive/master.zip` and `https://github.com/tinode/tinode-js/archive/master.zip` to the same directory. 4. Copy or symlink template directory `./server/templ` to `$GOPATH/bin/templ` ``` ln -s ./server/templ $GOPATH/bin ``` 5. Run the server ``` $GOPATH/bin/server -config=./server/tinode.conf -static_data=$HOME/tinode/webapp/ ``` 6. Test your installation by pointing your browser to [http://localhost:6060/](http://localhost:6060/). The static files from the `-static_data` path are served at web root `/`. You can change this by editing the line `static_mount` in the config file. **Important!** If you are running Tinode alongside another webserver, such as Apache or nginx, keep in mind that you need to launch the webapp from the URL served by Tinode. Otherwise it won't work. ## Running a Cluster - Install and run the database, run DB initializer, unpack JS files, and link or copy template directory as described in the previous section. Both MySQL and RethinkDB supports [cluster](https://www.mysql.com/products/cluster/) [mode](https://www.rethinkdb.com/docs/start-a-server/#a-rethinkdb-cluster-using-multiple-machines). You may consider it for added resiliency. - Cluster expects at least two nodes. A minimum of three nodes is recommended. - The following section configures the cluster. ``` "cluster_config": { // Name of the current node. "self": "", // List of all cluster nodes, including the current one. "nodes": [ {"name": "one", "addr":"localhost:12001"}, {"name": "two", "addr":"localhost:12002"}, {"name": "three", "addr":"localhost:12003"} ], // Configuration of failover feature. Don't change. "failover": { "enabled": true, "heartbeat": 100, "vote_after": 8, "node_fail_after": 16 } } ``` * `self` is the name of the current node. Generally it's more convenient to specify the name of the current node at the command line using `cluster_self` option. Command line value overrides the config file value. If the value is not provided either in the config file or through the command line, the clustering is disabled. * `nodes` defines individual cluster nodes. The sample defines three nodes named `one`, `two`, and `tree` running at the localhost at the specified cluster communication ports. Cluster addresses don't need to be exposed to the outside world. * `failover` is an experimental feature which migrates topics from failed cluster nodes keeping them accessible: * `enabled` turns on failover mode; failover mode requires at least three nodes in the cluster. * `heartbeat` interval in milliseconds between heartbeats sent by the leader node to follower nodes to ensure they are accessible. * `vote_after` number of failed heartbeats before a new leader node is elected. * `node_fail_after` number of heartbeats that a follower node misses before it's considered to be down. If you are testing the cluster with all nodes running on the same host, you also must override the `listen` and `grpc_listen` ports. Here is an example for launching two cluster nodes from the same host using the same config file: ``` $GOPATH/bin/tinode -config=./server/tinode.conf -static_data=./server/webapp/ -listen=:6060 -grpc_listen=:6080 -cluster_self=one & $GOPATH/bin/tinode -config=./server/tinode.conf -static_data=./server/webapp/ -listen=:6061 -grpc_listen=:6081 -cluster_self=two & ``` A bash script [run-cluster.sh](./server/run-cluster.sh) may be found useful. ### Enabling Push Notifications Follow [instructions](./docs/faq.md#q-how-to-setup-push-notifications-with-google-fcm). ### Enabling Video Calls Video calls use [WebRTC](https://en.wikipedia.org/wiki/WebRTC). WebRTC is a peer to peer protocol: once the call is established, the client applications exchange data directly. Direct data exchange is efficient but creates a problem when the parties are not accessible from the internet. WebRTC solves it by means of [ICE](https://en.wikipedia.org/wiki/Interactive_Connectivity_Establishment) servers which implement protocols [TURN(S)](https://en.wikipedia.org/wiki/Traversal_Using_Relays_around_NAT) and [STUN](https://en.wikipedia.org/wiki/STUN) as fallback. Tinode does not provide ICE servers out of the box. You must install and configure (or purchase) your own servers otherwise video and voice calling will not be available. Once you obtain the ICE TURN/STUN configuration from your service provider, add it to `tinode.conf` section `"webrtc"` - `"ice_servers"` (or `"ice_servers_file"`). Also change `"webrtc"` - `"enabled"` to `true`. An example configuration is provided in the `tinode.conf` for illustration only. IT WILL NOT FUNCTION because it uses dummy values instead of actual server addresses. You may find this information useful for choosing the servers: https://gist.github.com/yetithefoot/7592580 ### Note on Running the Server in Background There is [no clean way](https://github.com/golang/go/issues/227) to daemonize a Go process internally. One must use external tools such as shell `&` operator, `systemd`, `launchd`, `SMF`, `daemon tools`, `runit`, etc. to run the process in the background. Specific note for [nohup](https://en.wikipedia.org/wiki/Nohup) users: an `exit` must be issued immediately after `nohup` call to close the foreground session cleanly: ``` nohup $GOPATH/bin/server -config=./server/tinode.conf -static_data=$HOME/tinode/webapp/ & exit ``` Otherwise `SIGHUP` may be received by the server if the shell connection is broken before the ssh session has terminated (indicated by `Connection to XXX.XXX.XXX.XXX port 22: Broken pipe`). In such a case the server will shutdown because `SIGHUP` is intercepted by the server and interpreted as a shutdown request. For more details see https://github.com/tinode/chat/issues/25. ================================================ FILE: LICENSE ================================================ GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The GNU General Public License is a free, copyleft license for software and other kinds of works. The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. To protect your rights, we need to prevent others from denying you these rights or asking you to surrender the rights. Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others. For example, if you distribute copies of such a program, whether gratis or for a fee, you must pass on to the recipients the same freedoms that you received. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. Developers that use the GNU GPL protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License giving you legal permission to copy, distribute and/or modify it. For the developers' and authors' protection, the GPL clearly explains that there is no warranty for this free software. For both users' and authors' sake, the GPL requires that modified versions be marked as changed, so that their problems will not be attributed erroneously to authors of previous versions. Some devices are designed to deny users access to install or run modified versions of the software inside them, although the manufacturer can do so. This is fundamentally incompatible with the aim of protecting users' freedom to change the software. The systematic pattern of such abuse occurs in the area of products for individuals to use, which is precisely where it is most unacceptable. Therefore, we have designed this version of the GPL to prohibit the practice for those products. If such problems arise substantially in other domains, we stand ready to extend this provision to those domains in future versions of the GPL, as needed to protect the freedom of users. Finally, every program is threatened constantly by software patents. States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to avoid the special danger that patents applied to a free program could make it effectively proprietary. To prevent this, the GPL assures that patents cannot be used to render the program non-free. The precise terms and conditions for copying, distribution and modification follow. TERMS AND CONDITIONS 0. Definitions. "This License" refers to version 3 of the GNU General Public License. "Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. "The Program" refers to any copyrightable work licensed under this License. Each licensee is addressed as "you". "Licensees" and "recipients" may be individuals or organizations. To "modify" a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a "modified version" of the earlier work or a work "based on" the earlier work. A "covered work" means either the unmodified Program or a work based on the Program. To "propagate" a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well. To "convey" a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. An interactive user interface displays "Appropriate Legal Notices" to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. 1. Source Code. The "source code" for a work means the preferred form of the work for making modifications to it. "Object code" means any non-source form of a work. A "Standard Interface" means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language. The "System Libraries" of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A "Major Component", in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. The "Corresponding Source" for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work. The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. The Corresponding Source for a work in source code form is that same work. 2. Basic Permissions. All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. 3. Protecting Users' Legal Rights From Anti-Circumvention Law. No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures. 4. Conveying Verbatim Copies. You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. 5. Conveying Modified Source Versions. You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions: a) The work must carry prominent notices stating that you modified it, and giving a relevant date. b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to "keep intact all notices". c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an "aggregate" if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. 6. Conveying Non-Source Forms. You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways: a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange. b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. A "User Product" is either (1) a "consumer product", which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, "normally used" refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product. "Installation Information" for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made. If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM). The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network. Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying. 7. Additional Terms. "Additional permissions" are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions. When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission. Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms: a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or d) Limiting the use for publicity purposes of names of licensors or authors of the material; or e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors. All other non-permissive additional terms are considered "further restrictions" within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying. If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms. Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way. 8. Termination. You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11). However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10. 9. Acceptance Not Required for Having Copies. You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so. 10. Automatic Licensing of Downstream Recipients. Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License. An "entity transaction" is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts. You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it. 11. Patents. A "contributor" is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's "contributor version". A contributor's "essential patent claims" are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, "control" includes the right to grant patent sublicenses in a manner consistent with the requirements of this License. Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version. In the following three paragraphs, a "patent license" is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To "grant" such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party. If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. "Knowingly relying" means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid. If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it. A patent license is "discriminatory" if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007. Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law. 12. No Surrender of Others' Freedom. If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. 13. Use with the GNU Affero General Public License. Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such. 14. Revised Versions of this License. The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation. If the Program specifies that a proxy can decide which future versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version. 15. Disclaimer of Warranty. THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. Limitation of Liability. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 17. Interpretation of Sections 15 and 16. If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Also add information on how to contact you by electronic and paper mail. If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode: Copyright (C) This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, your program's commands might be different; for a GUI interface, you would use an "about box". You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. For more information on this, and how to apply and follow the GNU GPL, see . The GNU General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read . ================================================ FILE: README.md ================================================ # Tinode Instant Messaging Server Instant messaging full stack. Backend in pure [Go](http://golang.org) (license [GPL 3.0](http://www.gnu.org/licenses/gpl-3.0.en.html)), clients for Android (Java), iOS (Swift), and web (ReactJS), as well as [gRPC](https://grpc.io/) client support for C++, C#, Go, Java, Node, PHP, Python, Ruby, Objective-C, etc (all clients licensed under [Apache 2.0](http://www.apache.org/licenses/LICENSE-2.0)). Wire transport is JSON over websocket (long polling is also available) or [protobuf](https://developers.google.com/protocol-buffers/) with gRPC. This is beta-quality software: feature-complete and stable but probably with a few bugs or missing features. Follow [instructions](INSTALL.md) to install and run or use one of the cloud services below. Read [API documentation](docs/API.md). Tinode is *not* XMPP/Jabber. It is *not* compatible with XMPP. It's meant as a replacement for XMPP. On the surface, it's a lot like open source WhatsApp or Telegram. ## Why? The promise of [XMPP](http://xmpp.org/) was to deliver federated instant messaging: anyone would be able to spin up an IM server capable of exchanging messages with any other XMPP server in the world. Unfortunately, XMPP never delivered on this promise. Instant messengers are still a bunch of incompatible walled gardens, similar to what AoL of the late 1990s was to the open Internet. The goal of this project is to deliver on XMPP's original vision: create a modern open platform for federated instant messaging with an emphasis on mobile communication. A secondary goal is to create a decentralized IM platform that is much harder to track and block by the governments. An explicit NON-goal: we are not building yet another Slack replacement. ## Installing and running See [general instructions](./INSTALL.md) or [docker-specific instructions](./docker/README.md). ## Getting support * Read [API documentation](docs/API.md) and [FAQ](docs/faq.md). Read configuration instructions contained in the [`tinode.conf`](./server/tinode.conf) file. * For support, general questions, discussions post to [https://groups.google.com/d/forum/tinode](https://groups.google.com/d/forum/tinode). * For bugs and feature requests [open an issue](https://github.com/tinode/chat/issues/new/choose). * Use https://tinode.co/contact for commercial inquiries. ## Helping out * If you appreciate our work, please help spread the word! Sharing on Reddit, HN, and other communities helps more than you think. * Consider buying paid support: https://tinode.co/support.html * If you are a software developer, send us your pull requests with bug fixes and new features. * If you use the app and discover bugs or missing features, let us know by filing bug reports and feature requests. Vote for existing [feature requests](https://github.com/tinode/chat/issues?q=is%3Aissue+is%3Aopen+sort%3Areactions-%2B1-desc+label%3A%22feature+request%22) you find most valuable. * If you speak a language other than English, [translate](docs/translations.md) the apps into your language. You may also review and improve existing translations. * If you are a UI/UX expert, help us polish the app UI. * Use it: install it for your colleagues or friends at work or at home. ## Public service A [public Tinode service](https://web.tinode.co/) is available. You can use it just like any other instant messenger. Keep in mind that demo accounts present in [sandbox](https://sandbox.tinode.co/) are not available in the public service. You must register an account using valid email in order to use the service. ### Web TinodeWeb, a single page web app, is available at https://web.tinode.co/ ([source](https://github.com/tinode/webapp/)). See screenshots below. ### Android [Tinode for Android](https://play.google.com/store/apps/details?id=co.tinode.tindroidx) a.k.a Tindroid is stable and functional ([source](https://github.com/tinode/tindroid)). See the screenshots below. A [debug APK](https://github.com/tinode/tindroid/releases/latest) is also provided for convenience. ### iOS [Tinode for iOS](https://apps.apple.com/us/app/tinode/id1483763538) a.k.a. Tinodios is stable and functional ([source](https://github.com/tinode/ios)). See the screenshots below. ## Demo/Sandbox A sandboxed demo service is available at https://sandbox.tinode.co/. Log in as one of `alice`, `bob`, `carol`, `dave`, `frank`. Password is `123`, e.g. login for `alice` is `alice123`. You can discover other users by email or phone by prefixing them with `email:` or `tel:` respectively. Emails are `@example.com`, e.g. `alice@example.com`, phones are `+17025550001` through `+17025550009`. When you register a new account you are asked for an email address to send validation code to. For demo purposes you may use `123456` as a universal validation code. The code you get in the email is also valid. ### Sandbox Notes * The sandbox server is reset (all data wiped) every night at 3:15am Pacific time. An error message `User not found or offline` means the server was reset while you were connected. If you see it on the web, reload and relogin. On Android log out and re-login. If the database was changed, delete the app then reinstall. * Sandbox user `Tino` is a [basic chatbot](./chatbot) which responds with a [random quote](http://fortunes.cat-v.org/) to any message. * As generally accepted, when you register a new account you are asked for an email address. The server will send an email with a verification code to that address and you can use it to validate the account. To make things easier for testing, the server will also accept `123456` as a verification code. Remove line `"debug_response": "123456"` from `tinode.conf` to disable this option. * The sandbox server is configured to use [ACME](https://letsencrypt.org/) TLS [implementation](https://godoc.org/golang.org/x/crypto/acme) with hard-coded requirement for [SNI](https://en.wikipedia.org/wiki/Server_Name_Indication). If you are unable to connect then the most likely reason is your TLS client's missing support for SNI. Use a different client. * The default web app loads a single minified javascript bundle and minified CSS. The un-minified version is also available at https://sandbox.tinode.co/index-dev.html * [Docker images](https://hub.docker.com/u/tinode/) with the same demo are available. * You are welcome to test your client software against the sandbox, hack it, etc. No DDoS-ing though please. ## Features ### Supported * Multiple native platforms: * [Android](https://github.com/tinode/tindroid/) (Java) * [iOS](https://github.com/tinode/ios) (Swift) * [Web](https://github.com/tinode/webapp/) (React.js) * Scriptable [command line](tn-cli/) (Python) * User features: * One-on-one and group messaging. * Video and voice calls. Voice messages. * Channels with unlimited number of read-only subscribers. * All chats are synchronized across all devices. * Granular access control with permissions for various actions. * User search/discovery. * Rich formatting of messages markdown-style: \*style\* → **style**, with inline images, videos, file attachments. * Forms and templated responses suitable for chatbots. * Verified/staff/untrusted account markers. * Leave notes to self, bookmark (save) messages. * Message status notifications: message delivery to server; received and read notifications; typing notifications. * Most recent message preview in contact list. * Server-generated presence notifications for people, group chats. * Forwarding and replying to messages. * Editing sent messages. * Pinned chats and messages. * Customizable message backgrounds (wallpapers). * Light/dark/system UI themes. * Administration: * Granular access control with permissions for various actions. * Support for custom authentication backends. * Ability to block unwanted communication server-side. * Anonymous users (important for use cases related to tech support over chat). * Plugins to extend functionality, for example, to support moderation or chatbots. * Scriptable [command-line tool](tn-cli/) for server administration. * Performance, reliability and development: * Sharded clustering with failover. * Storage and out of band transfer of large objects like images or document files using local file system or Amazon S3 (other storage systems can be supported with [media handlers](https://github.com/tinode/chat/blob/master/server/media/media.go#L21)). * JSON or [protobuf version 3](https://developers.google.com/protocol-buffers/) wire protocols. * Bindings for various programming languages: * Javascript with no external dependencies. * Java with dependencies on [Jackson](https://github.com/FasterXML/jackson), [Java-Websocket](https://github.com/TooTallNate/Java-WebSocket), [ICU4J](https://github.com/unicode-org/icu). Suitable for Android but with no Android SDK dependencies. * Swift with no external dependencies. * C/C++, C#, Go, Python, PHP, Ruby and many other languages using [gRPC](https://grpc.io/docs/languages/). * Choice of a database backend. Other databases can be added by writing [adapters](server/db/adapter.go). * MySQL (and MariaDB, Percona as long as they remain SQL and wire protocol compatible) * PostgreSQL * MongoDB * [RethinkDB](http://rethinkdb.com/). Support is deprecated and will be dropped in 2027 because RethinkDB is no longer being developed (unless its development resumes). ### Planned * [Federation](https://en.wikipedia.org/wiki/Federation_(information_technology)). * Location and contacts sharing. * Previews of attached documents, links. * Recording video messages. * Video/audio broadcasting. * Group video/audio calls. * Attaching music/audio other than voice messages. * Different levels of message persistence (from strict persistence to "store until delivered" to purely ephemeral messaging). * Message encryption at rest. * End to end encryption with [OTR](https://en.wikipedia.org/wiki/Off-the-Record_Messaging) for one-on-one messaging and undecided method for group messaging. * Full text search in messages. ### Translations All client software has support for [internationalization](docs/translations.md). The following translations are provided: | Language | Server | Webapp | Android | iOS | | --- | :---: | :---: | :---: | :---: | | English | ✓ | ✓ | ✓ | ✓ | | Arabic | | ✓ | | | | Chinese simplified | ✓ | ✓ | ✓ | ✓ | | Chinese traditional | ✓ | ✓ | ✓ | ✓ | | French | ✓ | ✓ | ✓ | | | German | | ✓ | ✓ | | | Hindi | | | ✓ | | | Italian | | ✓ | ✓ | ✓ | | Korean | | ✓ | ✓ | | | Portuguese | ✓ | | ✓ | | | Romanian | | ✓ | ✓ | | | Russian | ✓ | ✓ | ✓ | ✓ | | Spanish | ✓ | ✓ | ✓ | ✓ | | Thai | | ✓ | | | | Ukrainian | ✓ | ✓ | ✓ | ✓ | | Vietnamese | ✓ | ✓ | | | More translations are [welcome](docs/translations.md). In addition to languages listed above, particularly interested in Bengali, Indonesian, Urdu, Japanese, Turkish, Persian. ## Third-Party ### Projects * [Arango DB adapter](https://github.com/gfxlabs/chat/tree/master/server/db/arango) (outdated) * [DynamoDB adapter](https://github.com/riandyrn/chat/tree/master/server/db/dynamodb) (outdated) ### Licenses * Demo avatars and some other graphics are from https://www.pexels.com/ under [CC0 license](https://www.pexels.com/photo-license/) and https://pixabay.com/ under their [license](https://pixabay.com/service/license/). * Web and Android background patterns are from http://subtlepatterns.com/ under [CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/) license. * Android icons are from https://material.io/tools/icons/ under [Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0.html) license. ## Screenshots ### [Android](https://github.com/tinode/tindroid/)

Android screenshot: list of chats Android screenshot: one conversation Android screenshot: video call

### [iOS](https://github.com/tinode/ios)

iOS screenshot: list of chats iOS screenshot: one conversation iOS screenshot: video call

### [Desktop Web](https://github.com/tinode/webapp/)

Desktop web: full app

### [Mobile Web](https://github.com/tinode/webapp/)

Mobile web: contacts Mobile web: chat Mobile web: topic info

#### SEO Strings Words 'chat' and 'instant messaging' in Chinese, Russian, Persian and a few other languages. * 聊天室 即時通訊 * чат мессенджер * インスタントメッセージ * 인스턴트 메신저 * پیام رسان فوری * تراسل فوري * فوری پیغام رسانی * Nhắn tin tức thời * anlık mesajlaşma sohbet * mensageiro instantâneo * pesan instan * mensajería instantánea * চ্যাট ইন্সট্যান্ট মেসেজিং * चैट त्वरित संदेश * তাৎক্ষণিক বার্তা আদান প্রদান ================================================ FILE: README_ko.md ================================================ # Tinode 인스턴트 메시징 서버 ## This document is outdated. For up to date info use [README.md](./README.md) 인스턴트 메시징 서버. C++, C#, [Go](http://golang.org), Java, Node, PHP, Python, Ruby, Objective-C 등에 대한 [gRPC](https://grpc.io/) 클라이언트 지원은 물론 순수Go(라이선스 [GPL 3.0](http://www.gnu.org/licenses/gpl-3.0.en.html))의 백엔드와 Java,Javasript 및 Swift의 클라이언트 측 바인딩(라이선스 [Apache 2.0](http://www.apache.org/licenses/LICENSE-2.0)). 와이어 전송은 사용자 정의 바인딩을 위해 웹소켓을 통한 JSON(롱 폴링도 가능) 즉, gRPC와 [protobuf](https://developers.google.com/protocol-buffers/). 영구 저장소 [RethinkDB](http://rethinkdb.com/), MySQL 및 MongoDB(실험적). 지원되지 않는 타사 [DynamoDB adapter](https://github.com/riandyrn/chat/tree/master/server/db/dynamodb) adapter도 있습니다. 사용자 정의 어댑터를 작성하여 다른 데이터베이스를 지원할 수 있습니다. Tinode는 XMPP/ Jabber 가 아닙니다. Tinode는 XMPP와 호환되지 않습니다. XMPP를 대체하기 위한 것입니다. 표면적으로는 오픈소스 WhatsApp 또는 Telegram과 매우 유사합니다. 버전 0.16은 베타 급 소프트웨어입니다. 기능은 완전하지만 몇 가지 버그가 있습니다. 아래 클라우드 서비스 중 하나를 설치 및 실행하거나 사용하려면 [지시사항](INSTALL.md)을 따르십시오. [API 설명서](docs/API.md)를 읽으십시오. ## Why? [XMPP](http://xmpp.org/)의 약속은 연합된 인스턴스 메시징을 제공하는 것입니다. 누구나 전세계의 다른 XMPP서버와 메시지를 교환할 수 있는 IM 서버를 가동할 수 있습니다. 불행하게도, XMPP는 이 약속을 이행하지 않았습니다. 인스턴트 메신저들은 1990년대 후반의 AoL공개 인터넷과 비슷한, 양립할 수 없는 벽으로 둘러싸인 정원의 무리들입니다. 이 프로젝트의 목표는 XMPP의 원래 비전인 모바일 통신을 강조하여 연합 인스턴트 메시징을 위한 현대적인 개방형 플랫폼을 만드는 것입니다. 두 번째 목표는 정부가 추적하고 차단하기 훨씬 어려운 분산형 IM플랫폼을 만드는 것입니다. XMPP: XML에 기반한 메시지 지향 통신 프로토콜 IM: Instant Messenger ## 설치 및 실행 [일반 지침](./INSTALL.md) 또는 [도커별 지침](./docker/README.md)을 참조하십시오. ## 지원받기 * [API 설명서](docs/API.md) 및 [FAQ](docs/faq.md)를 읽으십시오. * 지원, 일반적인 질문, 토로은[https://groups.google.com/d/forum/tinode](https://groups.google.com/d/forum/tinode).에 게시하십시오. * 버그 및 기능 요청에 대해서는 [issue](https://github.com/tinode/chat/issues/new)를 여십시오. ## 공공서비스 [Tinode 공공 서비스](https://web.tinode.co/)는 지금 바로 사용할 수 있습니다. 다른 메신저들처럼 사용하면 됩니다. [샌드박스](https://sandbox.tinode.co/)에 있는 데모 계정은 공공 서비스에서 사용할 수 없습니다. 서비스를 이용하려면 유효한 이메일을 사용하여 계정을 등록해야 합니다. ### 웹 Tinode웹은 단일 페이지의 웹으로 https://web.tinode.co/ ([원본](https://github.com/tinode/webapp/))에서 이용이 가능합니다. . 아래에 있는 스크린 샷을 참고하세요. 현재 영어, 중국어 간체, 러시아어를 지원합니다. 더 많은 번역을 환영합니다. ### 안드로이드 Tindroid라고 불리는 [안드로이드 버전의 Tinode](https://play.google.com/store/apps/details?id=co.tinode.tindroidx) 는 안정적으로 가동됩니다. ([원본](https://github.com/tinode/tindroid)). 아래에 있는 스크린 샷을 참고하세요. 편의를 위해 [디버그 APK](https://github.com/tinode/tindroid/releases/latest)도 제공합니다. 현재 영어, 중국어 간체, 러시아어를 지원합니다. 더 많은 번역을 환영합니다. ### iOS Tinodios라고 불리는 [iOS 버전의 Tinode](https://apps.apple.com/app/reference-to-tinodios-here/id123) 안정적으로 가동됩니다.([원본](https://github.com/tinode/ios)). 아래에 있는 스크린샷을 참고하세요. 현재 영어와 중국어 간체를 지원합니다. 더 많은 번역을 환영합니다. ## 데모/샌드박스 샌드박스 데모 버전은 https://sandbox.tinode.co/ 에서 이용 가능합니다. alice, bob, carol, dave, frank 중 하나로 로그인할 수 있습니다. 비밀번호는 <이름>123으로 예를 들어, alice의 비밀번호는 alice123입니다. 사용자 이름을 맨 앞에쓴 <이름>@example.com 형식의 이메일이나 +17025550001 부터 +17025550009의 전화번호를 이용해서 다른 사용자들을 찾을 수 있습니다. 새로운 계정을 등록하면 유효성 검사 코드를 보낼 이메일 주소를 묻는 메시지가 나타납니다. 데모의 목적으로 123456을 범용 유효성 검사 코드로 사용할 수 있습니다. 실제 이메일로 받은 코드도 유효합니다. ### 샌드박스 노트 * 샌드박스 서버는 태평양 표준시 기준 매일 오전 3시 15분에 초기화됩니다(모든 데이터가 지워짐). 사용자를 찾을 수 없습니다 또는 오프라인 같은 오류 메시지는 서버에 연결하는 동안 서버가 초기화 되었음을 의미합니다. 만약 해당 오류 메시지가 표시되면 새로고침 후 다시 로그인 하세요. 안드로이드에서 로그아웃 후 다시 로그인 하세요. 만약 데이터베이스가 변경된 경우에는 앱을 삭제했다가 다시 설치하면 됩니다. * 샌드박스 유저 Tino는 [기본적인 챗봇](./chatbot)으로 모든 메시지에 [임의의 인용구](http://fortunes.cat-v.org/)로 응답합니다. * 일반적으로 새로운 계정을 등록하면 이메일 주소를 묻는 메시지가 표시됩니다. 서버는 유효성 검사 코드가 포함된 메일을 보내며 이를 사용하여 계정을 검증하는 데 사용할 수 있습니다. 테스트를 보다 쉽게 할 수 있도록 서버는 유효성 검사 코드로 123456을 또한 허용합니다. Tinode.conf에서 ”debug_response”: “123456”행을 제거하여 이 옵션을 비활성화 시킬 수 있습니다. * 샌드박스 서버는 [SNI](https://en.wikipedia.org/wiki/Server_Name_Indication)에 대한 하드 코딩된 요구사항과 함께 [ACME](https://letsencrypt.org/) TLS [구현](https://godoc.org/golang.org/x/crypto/acme)을 사용하도록 구성되었습니다. 만약 연결할 수 없을 경우 TLS 클라이언트의 SNI 지원 누락일 가능성이 높습니다. 그 경우 다른 클라이언트를 사용하세요. * 기본 웹 앱은 하나의 축소된 자바스크립트 번들과 축소된 CSS를 가져옵니다. 축소되지 않은 버전은 https://sandbox.tinode.co/index-dev.html 에서도 제공됩니다. * 데모가 같은 [도커 이미지](https://hub.docker.com/u/tinode/)도 사용 가능합니다. * 샌드박스에 대해 소프트웨어를 테스트하고 해킹하는 작업, 기타 작업들을 수행할 수 있습니다. DDos는 절대 사용하지 마세요. ## 특징 ### 지원 기능 * [Android](https://github.com/tinode/tindroid/), [iOS](https://github.com/tinode/ios), [web](https://github.com/tinode/webapp/), 그리고 [command line](tn-cli/) 클라이언트. * 1대1 메시징. * 모든 구성원의 접근 권한을 가진 그룹 메시징을 개별적으로 관리한다. 최대 구성원 수는 설정할 수 있다(기본적으로 128명). * 다양한 작업에 대한 권한을 가진 항목 액세스 제어 * 서버에서 생성한 사용자 및 주제에 대한 존재 알림. * 맞춤형 인증 지원 * failover를 통한 Sharded clustering * 영구 메시지 저장소, 페이지가 지정된 메시지 기록 * 외부 의존성이 없는 javascript 바인딩. * Android SDK dependencies.Java 바인딩(의존성: [Jackson](https://github.com/FasterXML/jackson), [Java-Websocket](https://github.com/TooTallNate/Java-WebSocket)). Android에 적합하지만 Android SDK 종속성이 없음. * TCP 또는 Unix 소켓을 통한 Webocket, long polling, 및 [gRPC](https://grpc.io/). * JSON 또는[protobuf 버전 3](https://developers.google.com/protocol-buffers/) 와이어 프로토콜. * [암호화](https://letsencrypt.org/) 또는 기존 인증서를 내장한 [TLS](https://en.wikipedia.org/wiki/Transport_Layer_Security) 옵션 * 사용자 검색/발견. * 풍부한 메시지 형식, 마크다운 스타일: \*style\* → **style**. * 인라인 이미지 및 첨부 파일. * 챗봇에 적합한 양식 및 템플리트 응답. * 메시지 상태 알림: 서버로 메시지 전달; 수신 및 읽기 알림; 입력 알림. * 클라이언트 측 데이터 캐싱 지원. * 원하지 않는 통신 서버를 차단하는 기능. * 익명 사용자(대화 중 기술 지원 관련 사용 사례에 중요성). * [FCM](https://firebase.google.com/docs/cloud-messaging/) 또는 [TNPG](server/push/tnpg/)를 사용하여 알림을 푸시. * 로컬 파일 시스템 또는 Amazon S3를 사용하여 비디오 파일과 같은 대형 오브젝트의 저장 및 대역 외 전송. * 챗봇을 활성화하기 위해 기능을 확장하는 플러그인. ### 계획 * [연방(연합,연맹)](https://en.wikipedia.org/wiki/Federation_(information_technology)). * 일대일 메시징을 위한 [OTR](https://en.wikipedia.org/wiki/Off-the-Record_Messaging)과 그룹 메시징을 위한 미확정 방법으로 End to end 암호화. * bearer token 액세스 제어를 가진 무제한 회원(또는 수십만 명)의 그룹 메시징. * 자동 예비 시스템. * 메시지 지속성 다른 수준(엄격한 지속성부터 "전달될 때까지 저장"까지, 완전히 짧은 메시징까지). ### 번역 모든 클라이언트 소프트웨어는 국제화를 지원한다. 번역은 영어, 중국어 간체, 러시아어(iOS 제외)에 제공된다. 더 많은 번역을 환영한다. 특히 스페인어, 아랍어, 독일어, 페르시아어, 인도네시아어, 포르투갈어, 힌디어, 벵골어에 관심이 많다. ## 타사 라이선스 * 데모 아바타와 일부 다른 그래픽은 [CC0](https://www.pexels.com/photo-license/) 라이센스에 따라 https://www.pexels.com/에서 제공된다. * 웹 및 안드로이드 배경 패턴은 [CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/) 라이센스에 따라 http://subtlepatterns.com/ 에서 제공된다. * Android 아이콘은 [Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0.html) 라이센스의 https://material.io/tools/icons/ 에서 제공된다. * 일부 iOS 아이콘은 [CC BY-ND 3.0](https://icons8.com/license) 라이센스에 따라 https://icons8.com/ 에서 제공된다. ## 스크린샷 ### [안드로이드](https://github.com/tinode/tindroid/)

Android screenshot: list of chats Android screenshot: one conversation Android screenshot: account settings

### [iOS](https://github.com/tinode/ios)

iOS screenshot: list of chats iOS screenshot: one conversation iOS screenshot: account settings

### [데스크탑 웹](https://github.com/tinode/webapp/)

Desktop web: full app

### [모바일 웹](https://github.com/tinode/webapp/)

Mobile web: contacts Mobile web: chat Mobile web: topic info

#### SEO 문자열 중국어, 러시아어, 페르시아어 및 다른 몇 가지 언어로 '챗'과 '인스턴트 메시징'을 표시한다. * 聊天室 即時通訊 * чат мессенджер * インスタントメッセージ * 인스턴트 메신저 * پیام‌رسانی فوری گپ * تراسل فوري * Nhắn tin tức thời * anlık mesajlaşma sohbet * mensageiro instantâneo * pesan instan * mensajería instantánea ================================================ FILE: SECURITY.md ================================================ # Security Policy ## Reporting a Vulnerability Please report a vulnerability to `security@tinode.co`. ## Do NOT to report: * Firebase initialization tokens. The Firebase tokens are really public: they must be distributed with the client applications and consequently are not private by design. * Exposed `/pprof` and/or `/expvar`. We know they are exposed. It's intentional and harmless. * Exposed Prometheus metrics `/metrics`. Like above, it's intentional and harmless. * DMARC policy is not enabled `p=none`. We know and that's the way we like it for now. * Weak cipher suites (TLS 1.0) at `*.tinode.co`. Yes, we know. Does not look serious/important. ================================================ FILE: build-all.sh ================================================ #!/bin/bash # This script builds and archives binaries and supporting files for mac, linux, and windows. # If directory ./server/static exists, it's asumed to contain TinodeWeb and then it's also # copied and archived. # Supported OSs: mac (darwin), windows, linux. goplat=( darwin darwin windows linux linux ) # CPUs architectures: amd64 and arm64. The same order as OSs. goarc=( amd64 arm64 amd64 amd64 arm64 ) # Number of platform+architectures. buildCount=${#goplat[@]} # Supported database tags dbadapters=( mysql mongodb rethinkdb postgres ) dbtags=( ${dbadapters[@]} alldbs ) for line in $@; do eval "$line" done version=${tag#?} if [ -z "$version" ]; then # Get last git tag as release version. Tag looks like 'v.1.2.3', so strip 'v'. version=`git describe --tags` version=${version#?} fi echo "Releasing $version" GOSRC=.. pushd ${GOSRC}/chat > /dev/null # Prepare directory for the new release rm -fR ./releases/${version} mkdir ./releases/${version} # Tar on Mac is inflexible about directories. Let's just copy release files to # one directory. rm -fR ./releases/tmp mkdir -p ./releases/tmp/templ # Copy templates and database initialization files cp ./server/tinode.conf ./releases/tmp cp ./server/templ/*.templ ./releases/tmp/templ cp ./tinode-db/data.json ./releases/tmp cp ./tinode-db/*.jpg ./releases/tmp cp ./tinode-db/credentials.sh ./releases/tmp # Create directories for and copy TinodeWeb files. if [[ -d ./server/static ]] then mkdir -p ./releases/tmp/static/img mkdir ./releases/tmp/static/img/bkg mkdir ./releases/tmp/static/css mkdir ./releases/tmp/static/audio mkdir ./releases/tmp/static/src mkdir ./releases/tmp/static/umd cp ./server/static/img/*.png ./releases/tmp/static/img cp ./server/static/img/*.svg ./releases/tmp/static/img cp ./server/static/img/*.jpeg ./releases/tmp/static/img cp ./server/static/img/bkg/*.png ./releases/tmp/static/img/bkg cp ./server/static/img/bkg/*.jpg ./releases/tmp/static/img/bkg cp ./server/static/img/bkg/*.json ./releases/tmp/static/img/bkg cp ./server/static/audio/*.m4a ./releases/tmp/static/audio cp ./server/static/css/*.css ./releases/tmp/static/css cp ./server/static/index.html ./releases/tmp/static cp ./server/static/index-dev.html ./releases/tmp/static cp ./server/static/version.js ./releases/tmp/static cp ./server/static/umd/*.js ./releases/tmp/static/umd cp ./server/static/manifest.json ./releases/tmp/static cp ./server/static/service-worker.js ./releases/tmp/static # Create empty FCM client-side config. echo 'const FIREBASE_INIT = {};' > ./releases/tmp/static/firebase-init.js else echo "TinodeWeb not found, skipping" fi for (( i=0; i<${buildCount}; i++ )); do plat="${goplat[$i]}" arc="${goarc[$i]}" # Use .exe file extension for binaries on Windows. ext="" if [ "$plat" = "windows" ]; then ext=".exe" fi # Remove possibly existing keygen from previous build. rm -f ./releases/tmp/keygen rm -f ./releases/tmp/keygen.exe # Keygen is database-independent env GOOS="${plat}" GOARCH="${arc}" go build -ldflags "-s -w" -o ./releases/tmp/keygen${ext} ./keygen > /dev/null for dbtag in "${dbtags[@]}" do echo "Building ${dbtag}-${plat}/${arc}..." # Remove possibly existing binaries from previous build. rm -f ./releases/tmp/tinode rm -f ./releases/tmp/tinode.exe rm -f ./releases/tmp/init-db rm -f ./releases/tmp/init-db.exe # Build tinode server and database initializer for RethinkDb and MySQL. # For 'alldbs' tag, we compile in all available DB adapters. if [ "$dbtag" = "alldbs" ]; then buildtag="${dbadapters[@]}" else buildtag=$dbtag fi env GOOS="${plat}" GOARCH="${arc}" go build \ -ldflags "-s -w -X main.buildstamp=`git describe --tags`" -tags "${buildtag}" \ -o ./releases/tmp/tinode${ext} ./server > /dev/null env GOOS="${plat}" GOARCH="${arc}" go build \ -ldflags "-s -w" -tags "${buildtag}" -o ./releases/tmp/init-db${ext} ./tinode-db > /dev/null # Build archive. All platforms but Windows use tar for archiving. Windows uses zip. if [ "$plat" = "windows" ]; then # Remove possibly existing archive. rm -f ./releases/${version}/tinode-${dbtag}."${plat}-${arc}".zip # Generate a new one pushd ./releases/tmp > /dev/null zip -q -r ../${version}/tinode-${dbtag}."${plat}-${arc}".zip ./* popd > /dev/null else plat2=$plat # Rename 'darwin' tp 'mac' if [ "$plat" = "darwin" ]; then plat2=mac fi # Remove possibly existing archive. rm -f ./releases/${version}/tinode-${dbtag}."${plat2}-${arc}".tar.gz # Generate a new one tar -C ./releases/tmp -zcf ./releases/${version}/tinode-${dbtag}."${plat2}-${arc}".tar.gz . fi done done # Build chatbot release echo "Building python code..." ./build-py-grpc.sh # Release chatbot echo "Packaging chatbot.py..." rm -fR ./releases/tmp mkdir -p ./releases/tmp cp ${GOSRC}/chat/chatbot/python/chatbot.py ./releases/tmp cp ${GOSRC}/chat/chatbot/python/quotes.txt ./releases/tmp cp ${GOSRC}/chat/chatbot/python/requirements.txt ./releases/tmp tar -C ${GOSRC}/chat/releases/tmp -zcf ./releases/${version}/py-chatbot.tar.gz . pushd ./releases/tmp > /dev/null zip -q -r ../${version}/py-chatbot.zip ./* popd > /dev/null # Release tn-cli echo "Packaging tn-cli..." rm -fR ./releases/tmp mkdir -p ./releases/tmp cp ${GOSRC}/chat/tn-cli/*.py ./releases/tmp cp ${GOSRC}/chat/tn-cli/*.txt ./releases/tmp tar -C ${GOSRC}/chat/releases/tmp -zcf ./releases/${version}/tn-cli.tar.gz . pushd ./releases/tmp > /dev/null zip -q -r ../${version}/tn-cli.zip ./* popd > /dev/null # Clean up temporary files rm -fR ./releases/tmp popd > /dev/null ================================================ FILE: build-py-grpc.sh ================================================ #!/bin/bash echo "Packaging python tinode-grpc..." pushd ./pbx > /dev/null # Generate grpc bindings from the proto file. ./py-generate.sh v=3 pushd ../py_grpc > /dev/null # Generate version file from git tags python3 version.py # Generate tinode-grpc package python3 -m build > /dev/null popd > /dev/null popd > /dev/null ================================================ FILE: chatbot/LICENSE ================================================ The code in this folder and nested folders is licensed under Apache 2.0 http://www.apache.org/licenses/LICENSE-2.0 ================================================ FILE: chatbot/README.md ================================================ # Tinode ChatBot Examples * [Python chatbot](python/) * [Karuha](https://github.com/Visecy/Karuha) - third party chatbot framework. * [C# .Net/.NetCore chatbot](https://github.com/tinode/csharpbot) ================================================ FILE: chatbot/csharp/README.md ================================================ # Tinode Chatbot Example for .Net or .NetCore Moved to a separate repo: https://github.com/tinode/csharpbot ================================================ FILE: chatbot/python/.gitignore ================================================ .tn-cookie ================================================ FILE: chatbot/python/README.md ================================================ # Tinode Chatbot This is a simple chatbot for Tinode using [gRPC API](../../pbx/). It's written in Python as a demonstration that the API is language-independent. The chat bot subscribes to events stream using Plugin API and logs in to Tinode server as a regular user over gRPC interface (see `grpc_listen` in [tinode.conf](../../server/tinode.conf) file). The event stream API is used to listen for creation of new accounts. When a new account is created, the bot initiates a p2p topic with the new user. Then it listens for messages sent to the topic and responds to each with a random quote from `quotes.txt` file. Generated files are provided for convenience in a [separate folder](../../py_grpc/tinode_grpc). You may re-generate them if needed: ``` python -m pip install grpcio-tools python -m grpc_tools.protoc -../../pbx --python_out=. --grpc_python_out=. ../../pbx/model.proto ``` Chatbot expects gRPC binding to be provided as `tinode-grpc`. If you want to use them locally, first copy `model_pb2.py` and `model_pb2_grpc.py` to the same folder as `chatbot.py` then find the lines ``` from tinode_grpc import pb from tinode_grpc import pbx ``` in `chatbot.py` and replace them with ``` import model_pb2 as pb import model_pb2_grpc as pbx ``` ## Installing and running ### Using PIP #### Prerequisites [gRPC](https://grpc.io/) requires [python](https://www.python.org/) 2.7 or 3.4 or higher. Make sure [pip](https://pip.pypa.io/en/stable/installing/) 9.0.1 or higher is installed. ``` $ python -m pip install --upgrade pip ``` If you cannot upgrade pip due to a system-owned installation, you can run install it in a `virtualenv`: ``` $ python -m pip install virtualenv $ virtualenv venv $ source venv/bin/activate $ python -m pip install --upgrade pip ``` #### Install dependencies: ``` $ python -m pip install -r requirements.txt ``` On El Capitan OSX, you may get the following error: ``` $ OSError: [Errno 1] Operation not permitted: '/tmp/pip-qwTLbI-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six-1.4.1-py2.7.egg-info' ``` You can work around this using: ``` $ python -m pip install tinode_grpc --ignore-installed ``` ### Run the chatbot Start the [tinode server](../../INSTALL.md) first. Then start the chatbot with credentials of the user you want to be your bot, `alice` in this example: ``` python chatbot.py --login-basic=alice:alice123 ``` If you want to run the bot in the background, start it as ``` nohup python chatbot.py --login-basic=alice:alice123 & ``` Run `python chatbot.py -h` for more options. If you are using python 2, keep in mind that `condition.wait()` [is forever buggy](https://bugs.python.org/issue8844). As a consequence of this bug the bot cannot be terminated with a SIGINT. It has to be stopped with a SIGKILL. You can use cookie file to store credentials. Sample cookie files are provided as `basic-cookie.sample` and `token-cookie.sample`. Once authenticated the bot will store the token in the cookie file, `.tn-cookie` by default. If you have a cookie file with the desired credentials, you can run the bot with no parameters: ``` python chatbot.py ``` If the server is configured to use TLS, i.e. running as `httpS://my-server.example.com/`, the gRPC endpoint also uses the same SSL certificate. In that case add the `--ssl` option when starting the chatbot. If you want the chatbot to connect to the secure server over a local network or under a different name rather than the `my-server.example.com`, for instance as `localhost`, you must specify the SSL domain name to use, otherwise the server will not be able to find the right SSL certificate: ``` python chatbot.py --host=localhost:16060 --ssl --ssl-host=my-server.example.com ``` Quotes are read from `quotes.txt` by default. The file is plain text with one quote per line. ### Using Docker **Warning!** Although the chatbot itself is less than 11KB, the chatbot Docker image is 175MB: the `:slim` Python 3 image is about 140MB, gRPC adds another ~30MB. 1. Follow [instructions](../../docker/README.md) to build and run dockerized Tinode chat server up to and including _step 3_. 2. In _step 4_ run the server adding `--env PLUGIN_PYTHON_CHAT_BOT_ENABLED=true` and `--volume botdata:/botdata` to the command line: 1. **RethinkDB**: ``` $ docker run -p 6060:18080 -d --name tinode-srv --env PLUGIN_PYTHON_CHAT_BOT_ENABLED=true --volume botdata:/botdata --network tinode-net tinode/tinode-rethink:latest ``` 2. **MySQL**: ``` $ docker run -p 6060:18080 -d --name tinode-srv --env PLUGIN_PYTHON_CHAT_BOT_ENABLED=true --volume botdata:/botdata --network tinode-net tinode/tinode-mysql:latest ``` 3. **MongoDB**: ``` $ docker run -p 6060:18080 -d --name tinode-srv --env PLUGIN_PYTHON_CHAT_BOT_ENABLED=true --volume botdata:/botdata --network tinode-net tinode/tinode-mongodb:latest ``` 3. Run the chatbot ``` $ docker run -d --name tino-chatbot --network tinode-net --volume botdata:/botdata tinode/chatbot:latest ``` 4. Test that the bot is functional by pointing your browser to http://localhost:6060/, login and talk to user `Tino`. The user should respond to every message with a random quote. You may replace the `:latest` with a different tag. See all available tags here: * [Tinode-MySQL tags](https://hub.docker.com/r/tinode/tinode-mysql/tags/) * [Tinode-RethinkDB tags](https://hub.docker.com/r/tinode/tinode-rethink/tags/) * [Tinode-MongoDB tags](https://hub.docker.com/r/tinode/tinode-mongodb/tags/) * [Chatbot tags](https://hub.docker.com/r/tinode/chatbot/tags/) In general try to use docker images all with the same tag. ================================================ FILE: chatbot/python/basic-cookie.sample ================================================ {"schema": "basic", "secret": "alice:alice123"} ================================================ FILE: chatbot/python/chatbot.py ================================================ """Python implementation of a Tinode chatbot.""" # For compatibility between python 2 and 3 from __future__ import print_function import argparse import base64 from concurrent import futures from datetime import datetime import json import os try: from importlib.metadata import version except ImportError: # Fallback for Python < 3.8 from importlib_metadata import version import platform try: import Queue as queue except ImportError: import queue import random import signal import sys import time import grpc from google.protobuf.json_format import MessageToDict # Import generated grpc modules from tinode_grpc import pb from tinode_grpc import pbx # For compatibility with python2 if sys.version_info[0] >= 3: unicode = str APP_NAME = "Tino-chatbot" APP_VERSION = "1.2.3" LIB_VERSION = version("tinode_grpc") # Maximum length of string to log. Shorten longer strings. MAX_LOG_LEN = 64 # User ID of the current user botUID = None # Dictionary wich contains lambdas to be executed when server response is received onCompletion = {} # This is needed for gRPC ssl to work correctly. os.environ["GRPC_SSL_CIPHER_SUITES"] = "HIGH+ECDSA" def log(*args): print(datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3], *args) # Add bundle for future execution def add_future(tid, bundle): onCompletion[tid] = bundle # Shorten long strings for logging. def clip_long_string(obj): if isinstance(obj, unicode) or isinstance(obj, str): if len(obj) > MAX_LOG_LEN: return '<' + str(len(obj)) + ' bytes: ' + obj[:12] + '...' + obj[-12:] + '>' return obj elif isinstance(obj, (list, tuple)): return [clip_long_string(item) for item in obj] elif isinstance(obj, dict): return dict((key, clip_long_string(val)) for key, val in obj.items()) else: return obj def to_json(msg): return json.dumps(clip_long_string(MessageToDict(msg))) # Resolve or reject the future def exec_future(tid, code, text, params): bundle = onCompletion.get(tid) if bundle != None: del onCompletion[tid] try: if code >= 200 and code < 400: arg = bundle.get('arg') bundle.get('onsuccess')(arg, params) else: log("Error: {} {} ({})".format(code, text, tid)) onerror = bundle.get('onerror') if onerror: onerror(bundle.get('arg'), {'code': code, 'text': text}) except Exception as err: log("Error handling server response", err) # List of active subscriptions subscriptions = {} def add_subscription(topic): subscriptions[topic] = True def del_subscription(topic): subscriptions.pop(topic, None) def subscription_failed(topic, errcode): if topic == 'me': # Failed 'me' subscription means the bot is disfunctional. if errcode.get('code') == 502: # Cluster unreachable. Break the loop and retry in a few seconds. client_post(None) else: exit(1) def login_error(unused, errcode): # Check for 409 "already authenticated". if errcode.get('code') != 409: exit(1) def server_version(params): if params == None: return log("Server:", params['build'].decode('ascii'), params['ver'].decode('ascii')) def next_id(): next_id.tid += 1 return str(next_id.tid) next_id.tid = 100 # Quotes from the fortune cookie file quotes = [] def next_quote(): idx = random.randrange(0, len(quotes)) # Make sure quotes are not repeated while idx == next_quote.idx: idx = random.randrange(0, len(quotes)) next_quote.idx = idx return quotes[idx] next_quote.idx = 0 # This is the class for the server-side gRPC endpoints class Plugin(pbx.PluginServicer): def Account(self, acc_event, context): action = None if acc_event.action == pb.CREATE: action = "created" # TODO: subscribe to the new user. elif acc_event.action == pb.UPDATE: action = "updated" elif acc_event.action == pb.DELETE: action = "deleted" else: action = "unknown" log("Account", action, ":", acc_event.user_id, acc_event.public) return pb.Unused() queue_out = queue.Queue() def client_generate(): while True: msg = queue_out.get() if msg == None: return log("out:", to_json(msg)) yield msg def client_post(msg): queue_out.put(msg) def client_reset(): # Drain the queue try: while queue_out.get(False) != None: pass except queue.Empty: pass def hello(): tid = next_id() add_future(tid, { 'onsuccess': lambda unused, params: server_version(params), }) return pb.ClientMsg(hi=pb.ClientHi(id=tid, user_agent=APP_NAME + "/" + APP_VERSION + " (" + platform.system() + "/" + platform.release() + "); gRPC-python/" + LIB_VERSION, ver=LIB_VERSION, lang="EN")) def login(cookie_file_name, scheme, secret): tid = next_id() add_future(tid, { 'arg': cookie_file_name, 'onsuccess': lambda fname, params: on_login(fname, params), 'onerror': lambda unused, errcode: login_error(unused, errcode), }) return pb.ClientMsg(login=pb.ClientLogin(id=tid, scheme=scheme, secret=secret)) def subscribe(topic): tid = next_id() add_future(tid, { 'arg': topic, 'onsuccess': lambda topicName, unused: add_subscription(topicName), 'onerror': lambda topicName, errcode: subscription_failed(topicName, errcode), }) return pb.ClientMsg(sub=pb.ClientSub(id=tid, topic=topic)) def leave(topic): tid = next_id() add_future(tid, { 'arg': topic, 'onsuccess': lambda topicName, unused: del_subscription(topicName) }) return pb.ClientMsg(leave=pb.ClientLeave(id=tid, topic=topic)) def publish(topic, text): tid = next_id() return pb.ClientMsg(pub=pb.ClientPub(id=tid, topic=topic, no_echo=True, head={"auto": json.dumps(True).encode('utf-8')}, content=json.dumps(text).encode('utf-8'))) def note_read(topic, seq): return pb.ClientMsg(note=pb.ClientNote(topic=topic, what=pb.READ, seq_id=seq)) def init_server(listen): # Launch plugin server: accept connection(s) from the Tinode server. server = grpc.server(futures.ThreadPoolExecutor(max_workers=16)) pbx.add_PluginServicer_to_server(Plugin(), server) server.add_insecure_port(listen) server.start() log("Plugin server running at '"+listen+"'") return server def init_client(addr, schema, secret, cookie_file_name, secure, ssl_host): log("Connecting to", "secure" if secure else "", "server at", addr, "SNI="+ssl_host if ssl_host else "") channel = None if secure: opts = (('grpc.ssl_target_name_override', ssl_host),) if ssl_host else None channel = grpc.secure_channel(addr, grpc.ssl_channel_credentials(), opts) else: channel = grpc.insecure_channel(addr) # Call the server stream = pbx.NodeStub(channel).MessageLoop(client_generate()) # Session initialization sequence: {hi}, {login}, {sub topic='me'} client_post(hello()) client_post(login(cookie_file_name, schema, secret)) return stream def client_message_loop(stream): try: # Read server responses for msg in stream: log(datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3], "in:", to_json(msg)) if msg.HasField("ctrl"): # Run code on command completion exec_future(msg.ctrl.id, msg.ctrl.code, msg.ctrl.text, msg.ctrl.params) elif msg.HasField("data"): # log("message from:", msg.data.from_user_id) # Protection against the bot talking to self from another session. if msg.data.from_user_id != botUID: # Respond to message. # Mark received message as read client_post(note_read(msg.data.topic, msg.data.seq_id)) # Insert a small delay to prevent accidental DoS self-attack. time.sleep(0.1) # Respond with a witty quote client_post(publish(msg.data.topic, next_quote())) elif msg.HasField("pres"): # log("presence:", msg.pres.topic, msg.pres.what) # Wait for peers to appear online and subscribe to their topics if msg.pres.topic == 'me': if (msg.pres.what == pb.ServerPres.ON or msg.pres.what == pb.ServerPres.MSG) \ and subscriptions.get(msg.pres.src) == None: client_post(subscribe(msg.pres.src)) elif msg.pres.what == pb.ServerPres.OFF and subscriptions.get(msg.pres.src) != None: client_post(leave(msg.pres.src)) else: # Ignore everything else pass except grpc._channel._Rendezvous as err: log("Disconnected:", err) def read_auth_cookie(cookie_file_name): """Read authentication token from a file""" cookie = open(cookie_file_name, 'r') params = json.load(cookie) cookie.close() schema = params.get("schema") secret = None if schema == None: return None, None if schema == 'token': secret = base64.b64decode(params.get('secret').encode('utf-8')) else: secret = params.get('secret').encode('utf-8') return schema, secret def on_login(cookie_file_name, params): global botUID client_post(subscribe('me')) """Save authentication token to file""" if params == None or cookie_file_name == None: return if 'user' in params: botUID = params['user'].decode("ascii")[1:-1] # Protobuf map 'params' is not a python object or dictionary. Convert it. nice = {'schema': 'token'} for key_in in params: if key_in == 'token': key_out = 'secret' else: key_out = key_in nice[key_out] = json.loads(params[key_in].decode('utf-8')) try: cookie = open(cookie_file_name, 'w') json.dump(nice, cookie) cookie.close() except Exception as err: log("Failed to save authentication cookie", err) def load_quotes(file_name): with open(file_name) as f: for line in f: quotes.append(line.strip()) return len(quotes) def run(args): schema = None secret = None if args.login_token: """Use token to login""" schema = 'token' secret = args.login_token.encode('ascii') log("Logging in with token", args.login_token) elif args.login_basic: """Use username:password""" schema = 'basic' secret = args.login_basic.encode('utf-8') log("Logging in with login:password", args.login_basic) else: """Try reading the cookie file""" try: schema, secret = read_auth_cookie(args.login_cookie) log("Logging in with cookie file", args.login_cookie) except Exception as err: log("Failed to read authentication cookie", err) if schema: # Load random quotes from file log("Loaded {} quotes".format(load_quotes(args.quotes))) # Start Plugin server server = init_server(args.listen) # Initialize and launch client client = init_client(args.host, schema, secret, args.login_cookie, args.ssl, args.ssl_host) # Setup closure for graceful termination def exit_gracefully(signo, stack_frame): log("Terminated with signal", signo) server.stop(0) client.cancel() sys.exit(0) # Add signal handlers signal.signal(signal.SIGINT, exit_gracefully) signal.signal(signal.SIGTERM, exit_gracefully) # Run blocking message loop in a cycle to handle # server being down. while True: client_message_loop(client) time.sleep(3) client_reset() client = init_client(args.host, schema, secret, args.login_cookie, args.ssl, args.ssl_host) # Close connections gracefully before exiting server.stop(None) client.cancel() else: log("Error: authentication scheme not defined") if __name__ == '__main__': """Parse command-line arguments. Extract server host name, listen address, authentication scheme""" random.seed() purpose = "Tino, Tinode's chatbot." log(purpose) parser = argparse.ArgumentParser(description=purpose) parser.add_argument('--host', default='localhost:16060', help='address of Tinode server gRPC endpoint') parser.add_argument('--ssl', action='store_true', help='use SSL to connect to the server') parser.add_argument('--ssl-host', help='SSL host name to use instead of default (useful for connecting to localhost)') parser.add_argument('--listen', default='0.0.0.0:40051', help='address to listen on for incoming Plugin API calls') parser.add_argument('--login-basic', help='login using basic authentication username:password') parser.add_argument('--login-token', help='login using token authentication') parser.add_argument('--login-cookie', default='.tn-cookie', help='read credentials from the provided cookie file') parser.add_argument('--quotes', default='quotes.txt', help='file with messages for the chatbot to use, one message per line') args = parser.parse_args() run(args) ================================================ FILE: chatbot/python/quotes.txt ================================================ login:  $3,000,000 1 bulls, 3 cows A Cray is the best machine for simulating the performance of a Cray. A consistent indentation style is the hobgoblin of little minds. A gentleman is one who is never rude unintentionally. -Noel Coward A man must destroy himself before others can destroy him. -Mong Tse A philosopher does not need a torch to gather glow-worms by at mid-day. --Earnest Bramah A song in time is worth a dime. A woman is only a woman, but a good cigar is a smoke. -Rudyard Kipling Admiration is our polite recognition of another's resemblance to ourselves. All the good ones are taken. An atheist is a man with no invisible means of support. Any country with "democratic" in the title isn't. Are we not men? Attend winter sheep meetings. Learning never ends! Be the sea, and see me be. Beware: the light at the end of the tunnel may be New Jersey. Bubble bubble, toil and trouble; cast that float into a double. C'est dommage, mais c'est vrai. Caution: Do not view laser light with remaining eye. Cogito cogito ergo cogito sum. Crazee Edeee, his prices are INSANE!!! Death to all fanatics! Disk crisis, please clean up! Do not meddle in the mouth. Don't be overly suspicious where it's not warranted. Don't let your thoughts get in a rut. The knife which spreads may also cut. Don't worry if it doesn't work right; if everything did, you'd be out of a job. E Pluribus Unix. Either we are alone or we are not. Either way is mind-boggling. Even these days, it's not as easy to go crazy as you think. Everybody should believe in something -- I believe I'll have another drink. Exercise is the Yuppie version of bulemia. Far too noisy, my dear Mozart. Far too many notes. -Emperor Ferdinand. First things first. Why not send for the Nazis right now. Fortran est; non potest legi. Generalizations are useful. The work contained in them can be reckoned as labor God does not play dice. Good day to avoid cops. Crawl to work. Great shot, kid. That was one in a million. Have you done your Christmas chopping yet? -anon. White House Advisor 12/24/81 He was al coltissh, ful of ragerye,/And ful of jargon as a flekke pye. -Chaucer He who listens last is the last one listening. History is a race between education and catastrophe. -H. G. Wells How do I love thee? Hand me my calculator... I am a high-pressure guy, and I didn't take this job to conduct a going-out-of-business sale. - A.A. Penzias I don't even know what street Canada is on. - Al Capone I have the most perfect confidence in your indiscretion. I must have slipped a disk; my pack hurts. I think that I shall never see a billboard as lovely as a tree. -Ogden Nash I'd rather have my mail delivered by Lockheed than ride in a plane built by the Post Office. IOT trap -- core dumped If I had to choose between System V and 4.2, I'd resign. - Peter Honeyman If butterflies had teeth like tigers they would never make it out of the hangar. If it's not broken, don't fix it. If the shoe fits, buy the other one, too. If you don't care where you are, then you ain't lost. If you take the last cup, make a new pot. If your experiment needs statistics, you ought to have done a better experiment. - E. Rutherford In challenging a kzin, a simple scream of rage is sufficient. In this world, truth can wait; she's used to it. It is better to have loved and lost than just to have lost. It is useless to put on your brakes when you're upside down. -Paul Newman It's a small world, but I'd hate to have to paint it. It's hard to love someone who looks down on you because your hands get bloody protecting him. It's not the time between the takes that takes the time - it's changing your mind between the takes that takes the time. -Stage Dept. Join me and I will complete your training. Lando's not a system, he's a man. He's a gambler, scoundrel. You'd like him. Let sleeping wraiths lie. Like winter snow on summer lawn, time past is time gone. Lose a few, lose a few. Macro context switch under way, please do not log out! Man is in doubt to deem himself a god or beast. -Alexander Pope May you live all the days of your life. Mind your own business, Spock. I'm sick of your halfbreed interference. Multilevel standards are like onions. They're smelly and make you cry a lot. -Ron Natalie Never attribute to malice what can be found in scientific american, under computer recreations. New career ideas are worth pursuing. No man's life, liberty or property are safe while the legislature is in session. Non serviam. Nothing of interest ever happened on this day. Oh, so there you are! One scythe fits all. Otto's too so-so to toss soot, too sot to toot SOSs. So? People get it into their heads that this is a democracy. Well it isn't. -gwl Personality is a flimsy thing on which to build an art. -John Cage Populus vult decipi. Promptness is its own reward, if one lives by the clock instead of the sword. Real programmers can't say `lint' without adding `hbaxcu' -Wm Leler Remember the Unknown Buffalo. Rog-O-Matic callidus est. Say "no" to long-sleeved shirts!! Support your right to bare arms! Señor, if you hurry from here, you will wait longer there. -Mexico taxi driver Smoked carp is terrible unless you're out of smoked salmon. Some men are discovered; others are found out. Specialization is for insects. -Robt. A. Heinlein Stop searching. Happiness is right next to you. System going down for 5 minutes -- back up in barrels. Technological unemployment is total today -- 300 years ago we were all farmers. That's the nice thing about standards -- there's so many to choose from. -trb The Luddites always lose. Always. The average legislator is somewhere nearly all the time. -Herb Nore The dawning of the Information Age is bringing about dramatic changes in the fundamental fabric of our civilization. - AA Penzias The first piece of luggage out of the chute doesn't belong to anyone, ever. The hippo has no sting, but the wise man would rather be sat upon by the bee. The meek shall inherit the earth -- they are pronounced "o". The one interesting fact about the Diplodocus is that the accent is on the second syllable. The plural of spouse is spice. The skeletons in the cupboard will all come out in the wash. The universe is laughing behind your back. Them that dishes it out need not fall over every time someone blows hard. There is no fear in love; but perfect love casteth out fear. There'll always be an England - if not it would be necessary to invent one. These widows, sir, are the most perverse creatures in the world. - Joseph Addison. Things will be bright in P.M. A cop will shine a light in your face. This space available. Call 686-7600 for details. Those who in quarrels interpose must often wipe a bloody nose. To be is to be related. To play billiards well is a sign of an ill-spent youth. Toto, I've a feeling we're not in any immediate danger of having just committed suicide! Try a new system or a different approach. Uneasy lies the head that wears a crown. Veni, vidi, maeni, mo, cacha tigrem baedas to, iffi hollers, ledem go, veni, vidi, maeni, mo. Warning: this fortune may change your life. We don't know half of what we know. We retard what we cannot repel, we palliate what we cannot cure. -Johnson We're too close to System Test. What garlic is to salad, insanity is to art. When all else fails, read the instructions. When in trouble or in doubt, run in circles; scream and shout. Whenever I see his fingernails, I thank God I don't have to look at his feet. Whom the gods must destroy they first must drive insane. Without alkaloids, life itself would be impossible. Yes, the red switch. You can tell a man by the company that keeps him. You cannot buy beer; you can only rent it. You have bills. You look strong enough to pull the ears off a Gundark! You should go home. You will never find a more wretched hive of scum and villainy. Your computer account is overdrawn. Please reauthorize. Zero is greater than minus zero, but don't ask by how much. -6600 ref. manual `is false when preceded by its quotation' is false when preceded by its quotation. fortune: not found pic: 5 X 58008 picture shrunk to 0.000603365 X 7 uuxqt cmd (rnews ) status (ucsfcgl!uucp 256) In days of yore, the crab and the crayfish lived in the forest. * Method As described above, see details below. Whatever happens, happens because it must. Boost, don't knock pass 2 error:(file ) more than 100 args? Nepal premier won't resign. init: /dev/console: getty failing, sleeping Sentence without verb. That's the way I got promoted, by eating everything. -pjw Pittsburgh has become a kind of knowledge aircraft carrier, its "top-guns" scattered regularly around the planet. When the music stops, the house of cards collapses and the emperor is found to be wearing no clothes. Never put snow on a frostbitten part. Put a smoke detector in your vacation cottage. Draw up a family fire-escape plan. A mathematician is a machine for turning coffee into theorems. -Paul Erdös I'm TRYING to be a back end! - A Hume There are only 26 calls and most of them are trivial. 162 is unimplemented Incest more common than thought in United States The product classroom is marked pass-fail. ISDN is real and implementable. To dissimulate is to feign not to have what one has. - J Baudrillard Vacuums are nothings. We only mention them to let them know we know they're there. Two wrongs don't make a right; three lefts do. ?12 Machine check during machine check. The downside of having an architecture is wart-for-wart compatibility. - Bob Willard, DEC It doesn't matter if you don't know how your program works, so long as it's parallel -R. O'Keefe sendmail[94] AA00493: SYSERR: net hang reading from coma: Connection timed out during greeting wait with coma Performance doesn't matter if your product is sufficiently feature-rich. --SF system engineer PLEASE LOG IN TO 3B20'S AT 4800 BAUD. SQUASH, do not crush (seen on a vegetable crate) If you get to meet sufficently important people, it's ok to debase yourself. -pjw The system is ready. A watermelon will not ripen in your armpit. Contrary to English and other similar languages, Turkish can be hyphenated with a simple 4 state finite-state machine. nop...session...attach...clone...walk...open... Spare me your sorrow's tears. He is one inch good, one foot evil. Heaven cannot use two suns or a house two masters. To give ground is sometimes the best victory. No word can cut kindness. Let wisdom and virtue be the two wheels of your cart. Willow branches never snap under the weight of snow. Only a monkey tries to catch the full moon in the pond. Don't lug dirt to a hilltop. Don't paint on water or carve on ice. The nail that raises its head is hammered down. Who can tell the he-crow from his mate? He is wise who knows what is enough. His hand was bitten by his own dog. To kill a general first shoot his horse. What is left unsaid is rich as flowers. If you are in a hurry go round-about. Better be ignorant than mistaught. You can't judge widows or horses without handling them. Don't use the ox-cleaver to kill a hen. Two hearts: and only one body. Bread is better than blossoms. Good medicine has often a bitter smack. First among blossoms the cherry: among men the warrior. You can't wrap up the wind or tie down the shadow. You cannot live in the same world with your father's murderer. Even a starving hawk won't lower himself to eat corn. Keep your mouth shut, your eyes open. Some ride in palanquins, some bear palanquins: some weave sandals for palanquin-bearers. Tuning filesystem for rot 0... Two things will make you lose your earrings, and one of them's dancing. -Bonnie Raitt. When the humans are away, the monkeys enter the hut, eat up the maize, and rearrange the furniture. The problem is not getting ksh to execute any particular command, the problem is recognizing that there might be a problem. diff: usage diff [whatever] etc. Intense opportunities for reorganization Stringent ideas in the upper echelon mind Leveraged brainpower at the labs >>>>>>> REMOVE ALL YOUR FILES AND DIRECTORIES NOW! <<<<<<< UX:lp: ERROR: Can't establish contact with the LP print service. Awk is one of the world's greatest collections of surprises. -Doug McIlroy If you think awk is the perfect programming language for the problem, you don't understand the problem yet. -Rob Pike ``Workers of the World, forgive us!'' (a banner in a Moscow counter-rally, Oct 8, 1989) The first step is to determine what the remaining steps are. -Mark Horton If you do something stupid on UNIX you generally get strange behavior. -Doug Gwyn I'd still like you to explain that worm to me - Judge Munson to Robert T. Morris Like raisins in a bread pudding, the moments lie within the body of Henry. A real gentleman never takes bases unless he really has to. There are two proteins involved in DNA synthesis, they are called DNAsynthase 1 and DNAsynthase 3. Drawing on my fine command of the English language, I said nothing. -Robert Benchley Hay, be seedy! He-effigy, hate-shy jaky yellow man, oh peek, you are rusty, you've edible, you ex-wise he! The FSF is not overly concerned about security. - FSF Make: Don't know how your program works OSIfy, v.: To make code impenetrable. Rule 3: If the character is comprised of a container without another radical, then An economic reality of our time: computerized job deskilling. - a book review in Science Estne ebriamen de furfure avenaceo factum? The isomer with the higher dipole moment has the higher physical constants, regardless of the heat content. Van Arkel Rule Other factors being equal, the metal which is most susceptible to failure is that with the lowest boiling point. Mogro-Campero Rule The solid particle erosion rate of annealed face-centered cubic metals is inversely related to their hardness. Finnie-Wolak-Kabil Rule In dichroic crystals, the faster ray is less absorbed. Babinet Rule If you can't stand the heat, get a pool. A rolling stone is a singing rock group. UX:mail: INFO: No mail. Real software has its own 800 support line. - Stu Feldman Marine math: 2 beers times 39 Marines is 49 cases. When times are bad, people feel compelled to overeat. Of the physical pages in use, 3436738592 pages are permanently allocated to VMS. If you carry on, head-to-head, on `solution' you don't get anywhere This is like ignoring both the speed limit and the odometer in your car. It won't get you far. -Kenneth P. Birman /bin/ls: exec header invalid My pile of equipment is bigger than your pile of equipment -- philw Connected to 192.65.218.43. as1: Error: ../bpvvv.c, line 1324: Too many float literals--compile with "-Wb,-nopool" The helpful thought for which you look/Is written somewhere in a book. Is the tool broadly supported or maintained? I'm pulling *something* here. - Dom Marotta I'm drawing a line under the sand. - John Major They bit the wrong chicken's head off with their own teeth and got blood all over their shirt - nls I just want a bare-boned, straight EMACS. - Rae McLellan *** Message content is not printable: delete, write or save it to a file *** 1181258 SAVECORE SEGMENTATION FAULT WHILE TRYING TO SAVE CORE Everything that can ever be invented has been invented -Charles H. Duell, Dan Quayle addressing the United Negro College Fund Here in Nuremberg, information hiding is much more popular in the organization than it is in the software... License Error : The license for this product(SPARCompiler C) has expired My favorite thing about the Internet is that you get to go into the private world of real creeps without having to smell them. - Penn Jillette Music is the pleasure the human soul experiences from counting without being aware that it is counting. -Leibnitz Millenium parties/with loud music, lights and joy;/then, how cold! warning: Hit heuristic-fence-post without finding enclosing function for address 0xfa2e470 War in the Clinton era is just P.R. by other means. - Michael Hirschorn Application Developer's Architecture Guide ! TeX capacity exceeded, sorry [main memory size=263001]. His hand was bitten by his own side in an offhand way. - Mark V. Shaney Sperm bank sued for tossing samples - Headline in Star-Ledger boot: nop...cfs...session...no physical memory Computers come in putty-colored boxes and have AUTOEXEC.BAT files and run screen-savers with flying toasters, and brains do not. - Steven Pinker We have discovered a pervasive nonstationarity. verb = a > b ? 'a': c > d ? 'd': 'c'; It's not only stupid it's wrong! Lucky Numbers 12, 14, 19, 24, 36, 43 The goal of the experimental trials with the artificial heart is to "double the life span of these patients" to 60 days, Lederman said. canlock: corrupted 0xcafebabe Devil Duckie, when you float, it's like I'm bathing in a flaming moat! He lost both Ali-pilaf and Vali-pilaf. He is a man who gives bread. We cut salt and bread together. You can't make stew with cheap meat. Absquotilate it in style, you old skunk,..and show the gentlemen what you can do. Cookie import from '/usr/dhog/lib/cookies' failed: fil Fortunately, Mac OS X supports *that* feature! - Brendan Connell The full documentation for cat is maintained as a Texinfo manual. If the info and cat programs are properly installed at your site, the command "info cat" should give you access to the complete manual. That's nothing.. RMS sent me a .doc file the other day... Notegroups! They kill you! Welcome, rqzgc_mfuv8 Isn't it funny how people say they'll never grow up to be their parents, then one day they look in the mirror and they're moving aircraft carriers into the Gulf region? - the Onion Where would Christianity be if Jesus got eight to fifteen years, with time off for good behavior? -- New York Senator James H. Donovan on capital punishment. Few, few the bird make her nest. To become of bishop miller. The dress don't make the monk. He is mad to bind. He turns as a weath turcocl. After the paunch comes the dance. Linux programmer's mind: don't invalidate the D-cache unless it wasn't enabled Practically noiseless and impossible to explode. - ad for the 1897 Oldsmobile The essence of XML is this: the problem it solves is not hard, and it does not solve the problem well. - Phil Wadler, POPL 2003 If you are idle for more than 1000 hours, the system will log you out. Please save reviews frequently. Setting up your SIP account will allow you to call both other SIP users as well as pstn phones. - Wim Sweldens What's grey? A melted penguin. Linux: the world's best text adventure game. i know what jmk did. he added reentrancy for threads. - boyd, about uintptr I recently visited plan9.bell-labs.com/wiki/plan9 and I would like to offer my web design services. I can help you with your Plan 9 website. - spam gcc is the holy cow of compilers, not the holy grail. - forsyth we live in a world of dogmas, probably trying to fight it with a stronger dogma is a bad idea, but trying to fight it with preumpted objectivism clearly doesn't work. - uriel Lucent is the best place 4 me to work. (Please keep confidential) Subject: There's More to Nevada Than You Think /* keep the code below somewhat more readonable; not used elsewhere */ swapon: /dev/disk/by-uuid/928aaabd-5744-4f0a-b9ef-aa101f286514: Invalid argument - Linux I guess the idea is that ... even though the structures are all different. rusty: kernel pseudo files are not the place for chit-chat ================================================ FILE: chatbot/python/requirements.txt ================================================ futures>=3.2.0; python_version<'3' grpcio>=1.40.0 tinode-grpc>=0.20.0b3 importlib-metadata>=1.0; python_version<'3.8' ================================================ FILE: chatbot/python/setup.py ================================================ import setuptools from subprocess import Popen, PIPE with open('README.md', 'r') as fh: long_description = fh.read() setuptools.setup( name="tinode-chatbot", version=git_version(), author="Tinode Authors", author_email="info@tinode.co", description="Tinode demo chatbot.", long_description=long_description, long_description_content_type="text/markdown", url="https://github.com/tinode/chat", packages=setuptools.find_packages(), install_requires=['grpcio>=1.40.0'], classifiers=( "Programming Language :: Python :: 3", "License :: OSI Approved :: Apache 2.0", "Operating System :: OS Independent", ), ) def git_version(): try: p = Popen(['git', 'describe', '--tags'], stdout=PIPE, stderr=PIPE) p.stderr.close() line = p.stdout.readlines()[0] return line.strip() except: return None ================================================ FILE: chatbot/python/token-cookie.sample ================================================ {"schema": "token", "secret": "mtXWlt9ERZCKsw9aFAABAFGGCnxinE8ruLE21t6SQfck4uBKCIy44kerjmOh4h1+", "expires": "2017-11-18T04:14:02Z"} ================================================ FILE: docker/README.md ================================================ # Using Docker to run Tinode All images are available at https://hub.docker.com/r/tinode/ 1. [Install Docker](https://docs.docker.com/install/) 1.8 or above. The provided dockerfiles are dependent on [Docker networking](https://docs.docker.com/network/) which may not work with the older Docker. 2. Create a bridge network. It's used to connect Tinode container with the database container. ``` $ docker network create tinode-net ``` 3. Decide which database backend you want to use: MySQL, PostgreSQL, MongoDB or RethinkDB. Run the selected database container, attaching it to `tinode-net` network: 1. **MySQL**: If you've decided to use MySQL backend, run the official MySQL Docker container: ``` $ docker run --name mysql --network tinode-net --restart always --env MYSQL_ALLOW_EMPTY_PASSWORD=yes -d mysql:5.7 ``` See [instructions](https://hub.docker.com/_/mysql/) for more options. MySQL 5.7 or above is required. 2. **PostgreSQL**: If you've decided to use PostgreSQL backend, run the official PostgreSQL Docker container: ``` $ docker run --name postgres --network tinode-net --restart always --env POSTGRES_PASSWORD=postgres -d postgres:13 ``` See [instructions](https://hub.docker.com/_/postgres/) for more options. PostgresSQL 13 or above is required. The name `rethinkdb`, `mysql`, `mongodb` or `postgres` in the `--name` assignment is important. It's used by other containers as a database's host name. 3. **MongoDB**: If you've decided to use MongoDB backend, run the official MongoDB Docker container and initialise it as single node replica set (you can change "rs0" if you wish): ``` $ docker run --name mongodb --network tinode-net --restart always -d mongo:latest --replSet "rs0" $ docker exec -it mongodb mongosh # And inside mongo shell: > rs.initiate( {"_id": "rs0", "members": [ {"_id": 0, "host": "mongodb:27017"} ]} ) > quit() ``` See [instructions](https://hub.docker.com/_/mongo/) for more options. MongoDB 4.2 or above is required. 4. **RethinkDB**: If you've decided to use RethinkDB backend, run the official RethinkDB Docker container: ``` $ docker run --name rethinkdb --network tinode-net --restart always -d rethinkdb:2.3 ``` See [instructions](https://hub.docker.com/_/rethinkdb/) for more options. 4. Run the Tinode container for the appropriate database: 1. **MySQL**: ``` $ docker run -p 6060:6060 -d --name tinode-srv --network tinode-net tinode/tinode-mysql:latest ``` 2. **PostgreSQL**: ``` $ docker run -p 6060:6060 -d --name tinode-srv --network tinode-net tinode/tinode-postgres:latest ``` 3. **MongoDB**: ``` $ docker run -p 6060:6060 -d --name tinode-srv --network tinode-net tinode/tinode-mongodb:latest ``` 4. **RethinkDB**: ``` $ docker run -p 6060:6060 -d --name tinode-srv --network tinode-net tinode/tinode-rethinkdb:latest ``` You can also run Tinode with the `tinode/tinode` image (which has all of the above DB adapters compiled in). You will need to specify the database adapter via `STORE_USE_ADAPTER` environment variable. E.g. for `mysql`, the command line will look like ``` $ docker run -p 6060:6060 -d -e STORE_USE_ADAPTER mysql --name tinode-srv --network tinode-net tinode/tinode:latest ``` See [below](#supported-environment-variables) for more options. The port mapping `-p 5678:1234` tells Docker to map container's port 1234 to host's port 5678 making server accessible at http://localhost:5678/. The container will initialize the database with test data on the first run. You may replace `:latest` with a different tag. See all all available tags here: * [MySQL tags](https://hub.docker.com/r/tinode/tinode-mysql/tags/) * [PostgreSQL tags](https://hub.docker.com/r/tinode/tinode-postgresql/tags/) (beta version) * [MongoDB tags](https://hub.docker.com/r/tinode/tinode-mongodb/tags/) * [RethinkDB tags](https://hub.docker.com/r/tinode/tinode-rethink/tags/) * [All bundle tags](https://hub.docker.com/r/tinode/tinode/tags/) 5. Test the installation by pointing your browser to [http://localhost:6060/](http://localhost:6060/). ## Optional ### External config file The container comes with a built-in config file which can be customized with values from the environment variables (see [Supported environment variables](#supported_environment_variables) below). If changes are extensive it may be more convenient to replace the built-in config file with a custom one. In that case map the config file located on your host (e.g. `/users/jdoe/new_tinode.conf`) to container (e.g. `/tinode.conf`) using [Docker volumes](https://docs.docker.com/storage/volumes/) `--volume /users/jdoe/new_tinode.conf:/tinode.conf` then instruct the container to use the new config `--env EXT_CONFIG=/tinode.conf`: ``` $ docker run -p 6060:6060 -d --name tinode-srv --network tinode-net \ --volume /users/jdoe/new_tinode.conf:/tinode.conf \ --env EXT_CONFIG=/tinode.conf \ tinode/tinode-mysql:latest ``` When `EXT_CONFIG` is set, most other environment variables are ignored. Consult [the table](#supported-environment-variables) below for a full list. ### Resetting or upgrading the database The database schema may change from time to time. An error `Invalid database version 101. Expected 103` means the schema has changed and needs to be updated, in this case from version 101 to version 103. You need to either reset or upgrade the database to continue: Shut down the Tinode container and remove it: ``` $ docker stop tinode-srv && docker rm tinode-srv ``` then repeat step 4 adding `--env RESET_DB=true` to reset or `--env UPGRADE_DB=true` to upgrade. Also, the database is automatically created if missing. ### Enable push notifications Tinode uses Google Firebase Cloud Messaging (FCM) to send pushes. Follow [instructions](../docs/faq.md#q-how-to-setup-fcm-push-notifications) for obtaining the required FCM credentials. * Download and save the [FCM service account credentials](https://cloud.google.com/docs/authentication/production) file. * Obtain values for `apiKey`, `messagingSenderId`, `projectId`, `appId`, `messagingVapidKey`. Assuming your Firebase credentials file is named `myproject-1234-firebase-adminsdk-abc12-abcdef012345.json` and it's saved at `/Users/jdoe/`, web API key is `AIRaNdOmX4ULR-X6ranDomzZ2bHdRanDomq2tbQ`, Sender ID `141421356237`, Project ID `myproject-1234`, App ID `1:141421356237:web:abc7de1234fab56cd78abc`, VAPID key (a.k.a. "Web Push certificates") is `83_Or_So_Random_Looking_Characters`, start the container with the following parameters (using MySQL container as an example): ``` $ docker run -p 6060:6060 -d --name tinode-srv --network tinode-net \ -v /Users/jdoe:/config \ --env FCM_CRED_FILE=/config/myproject-1234-firebase-adminsdk-abc12-abcdef012345.json \ --env FCM_API_KEY=AIRaNdOmX4ULR-X6ranDomzZ2bHdRanDomq2tbQ \ --env FCM_APP_ID=1:141421356237:web:abc7de1234fab56cd78abc \ --env FCM_PROJECT_ID=myproject-1234 \ --env FCM_SENDER_ID=141421356237 \ --env FCM_VAPID_KEY=83_Or_So_Random_Looking_Characters \ tinode/tinode-mysql:latest ``` ### Configure video calling Tinode uses [WebRTC](https://webrtc.org/) for video and audio calls. WebRTC needs [Interactive Communication Establishment (ICE)](https://tools.ietf.org/id/draft-ietf-ice-rfc5245bis-13.html) [TURN(S)](https://en.wikipedia.org/wiki/Traversal_Using_Relays_around_NAT) and/or [STUN](https://en.wikipedia.org/wiki/STUN) servers to traverse [NAT](https://en.wikipedia.org/wiki/Network_address_translation), otherwise calls may not work. Tinode does not include TURN(S) or STUN out of the box. You need to obtain and configure your own service. Once you setup your TURN(S) and/or STUN service, save its configuration to a file, for example `/Users/jdoe/turn-config.json` and provide path to this file when starting the container: ``` $ docker run -p 6060:6060 -d --name tinode-srv --network tinode-net \ -v /Users/jdoe:/config \ --env ICE_SERVERS_FILE=/config/turn-config.json \ < ... other config parameters ... > tinode/tinode-mysql:latest ``` The config file uses the following format: ```json [ { "urls": [ "stun:stun.example.com" ] }, { "username": "user-name-to-use-for-authentication-with-the-server", "credential": "your-password", "urls": [ "turn:turn.example.com:80?transport=udp", "turn:turn.example.com:3478?transport=tcp", "turns:turn.example.com:443?transport=tcp", ] } ] ``` [XIRSYS](https://xirsys.com/) offers a free tier for developers. We are in no way affiliated with XIRSYS. We do not endorse or otherwise take any responsibility for your use of their services. ### Run the chatbot See [instructions](../chatbot/python/). The chatbot password is generated only when the database is initialized or reset. It's saved to `/botdata` directory in the container. If you want to keep the data available between container changes, such as image upgrades, make sure the `/botdata` is a mounted volume (i.e. you always launch the container with `--volume botdata:/botdata` option). ## Supported environment variables You can specify the following environment variables when issuing `docker run` command: | Variable | Type | Default | Purpose | | --- | --- | --- | --- | | `ACC_GC_ENABLED`[^2] | bool | `false` | Enable/diable automatic deletion of unfinished account registrations. | | `AUTH_TOKEN_KEY`[^2] | string | `wfaY2RgF2S1OQI/ZlK+LS​rp1KB2jwAdGAIHQ7JZn+Kc=` | base64-encoded 32 random bytes used as salt for authentication tokens. | | `AWS_ACCESS_KEY_ID`[^2] | string | | AWS Access Key ID when using `s3` media handler. | | `AWS_CORS_ORIGINS`[^2] | string | `["*"]` | Allowed origins ([CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin)) URL for downloads. Generally use your server URL and its aliases. | | `AWS_REGION`[^2] | string | | AWS Region when using `s3` media handler | | `AWS_S3_BUCKET`[^2] | string | | Name of the AWS S3 bucket when using `s3` media handler. | | `AWS_S3_ENDPOINT`[^2] | string | | An endpoint URL (hostname only or fully qualified URI) to override the default endpoint; can be of any S3-compatible service, such as `minio-api.x.io` | | `AWS_SECRET_ACCESS_KEY`[^2] | string | | AWS [Secret Access Key](https://aws.amazon.com/blogs/security/wheres-my-secret-access-key/) when using `s3` media handler. | | `CLUSTER_SELF` | string | | Node name if the server is running in a Tinode cluster. | | `DEBUG_EMAIL_VERIFICATION_CODE`[^2] | string | | Enable dummy email verification code, e.g. `123456`. Disabled by default (empty string). | | `DEFAULT_COUNTRY_CODE`[^2] | string | `US` | 2-letter country code to assign to sessions by default when the country isn't specified by the client explicitly and it's impossible to infer it. | | `EXT_CONFIG`[^1] | string | | Path to external config file to use instead of the built-in one. If this parameter is used, most other variables are ignored[^1]. | | `EXT_STATIC_DIR` | string | | Path to external directory containing static data (e.g. Tinode Webapp files). | | `FCM_CRED_FILE`[^2] | string | | Path to JSON file with FCM server-side service account credentials which will be used to send push notifications. | | `FCM_API_KEY` | string | | Firebase API key; required for receiving push notifications in the web client. | | `FCM_APP_ID` | string | | Firebase web app ID; required for receiving push notifications in the web client. | | `FCM_PROJECT_ID` | string | | Firebase project ID; required for receiving push notifications in the web client. | | `FCM_SENDER_ID` | string | | Firebase FCM sender ID; required for receiving push notifications in the web client. | | `FCM_VAPID_KEY` | string | | Also called 'Web Client certificate' in the FCM console; required by the web client to receive push notifications. | | `FCM_INCLUDE_ANDROID_NOTIFICATION`[^2] | boolean | true | If true, pushes a data + notification message, otherwise a data-only message. [More info](https://firebase.google.com/docs/cloud-messaging/concept-options). | | `FCM_MEASUREMENT_ID` | string | | Google Analytics ID of the form `G-123ABCD789`. | | `FS_CORS_ORIGINS`[^2] | string | `["*"]` | Cors origins when media is served from the file system. See `AWS_CORS_ORIGINS` for details. | | `ICE_SERVERS_FILE`[^2] | string | | Path to JSON file with configuration of ICE servers to be used for video calls. | | `MEDIA_HANDLER`[^2] | string | `fs` | Handler of large files, either `fs` or `s3`. | | `MYSQL_DSN`[^2] | string | 'root@tcp(mysql)/tinode?​parseTime=true​&collation=utf8mb4_0900_ai_ci' | MySQL [DSN](https://github.com/go-sql-driver/mysql#dsn-data-source-name). | | `PLUGIN_PYTHON_CHAT_BOT_ENABLED`[^2] | bool | `false` | Enable calling into the plugin provided by Python chatbot. | | `POSTGRES_DSN`[^2] | string | 'postgresql://postgres:postgres@​localhost:5432/tinode?​sslmode=disable​&connect_timeout=10' | PostgreSQL [DSN](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING). | | `RESET_DB` | bool | `false` | Drop and recreate the database. | | `SAMPLE_DATA` | string | _see comment →_ | File with sample data to load. Default `data.json` when resetting or generating new DB, none when upgrading. Use `` (empty string) to disable. | | `SMTP_AUTH_MECHANISM`[^2] | string | `"plain"` | SMTP authentication mechanism to use; one of "login", "cram-md5", "plain". | | `SMTP_DOMAINS`[^2] | string | | White list of email domains; when non-empty, accept registrations with emails from these domains only (email verification). | | `SMTP_HELO_HOST`[^2] | string | _see comment →_ | FQDN to use in SMTP HELO/EHLO command; if missing, the hostname from `SMTP_HOST_URL` is used. | | `SMTP_HOST_URL`[^2] | string | `'http://localhost:6060/'` | URL of the host where the webapp is running (email verification). | | `SMTP_LOGIN`[^2] | string | | Optional login to use for authentication with the SMTP server (email verification). | | `SMTP_PASSWORD`[^2] | string | | Optional password to use for authentication with the SMTP server (email verification). | | `SMTP_PORT`[^2] | number | | Port number of the SMTP server to use for sending verification emails, e.g. `25` or `587`. | | `SMTP_SENDER` | string | | [RFC 5322](https://tools.ietf.org/html/rfc5322) email address to use in the `FROM` field of verification emails, e.g. `'"John Doe" '`. | | `SMTP_SERVER`[^2] | string | | Name of the SMTP server to use for sending verification emails, e.g. `smtp.gmail.com`. If SMTP_SERVER is not defined, email verification will be disabled. | | `STORE_USE_ADAPTER`[^2] | string | | DB adapter name (specify with `tinode/tinode` container only). | | `TEL_HOST_URL`[^2] | string | `'http://localhost:6060/'` | URL of the host where the webapp is running for phone verification. | | `TEL_SENDER`[^2] | string | | Sender name to pass to SMS sending service. | | `TLS_CONTACT_ADDRESS`[^2] | string | | Optional email to use as contact for [LetsEncrypt](https://letsencrypt.org/) certificates, e.g. `jdoe@example.com`. | | `TLS_DOMAIN_NAME`[^2] | string | | If non-empty, enables TLS (http**s**) and configures domain name of your container, e.g. `www.example.com`. In order for TLS to work you have to expose your HTTPS port to the Internet and correctly configure DNS. It WILL FAIL with `localhost` or unroutable IPs. | | `TNPG_AUTH_TOKEN` | string | | Tinode Push Gateway authentication token. | | `TNPG_ORG`[^2] | string | | Tinode Push Gateway organization name as registered at https://console.tinode.co | | `UID_ENCRYPTION_KEY`[^2] | string | `la6YsO+bNX/+XIkOqc5Svw==` | base64-encoded 16 random bytes used as an encryption key for user IDs. | | `UPGRADE_DB` | bool | `false` | Upgrade database schema, if necessary. | | `WAIT_FOR` | string | | If non-empty, waits for the specified database `host:port` to be available before starting the server. | [^1]: If set, variables marked with the footnote `[2]` are ignored. [^2]: Ignored if `EXT_CONFIG` is set. A convenient way to generate a desired number of random bytes and base64-encode them on Linux and Mac: ``` $ openssl rand -base64 ``` ## Metrics Exporter See [monitoring/exporter/README](../monitoring/exporter/README.md) for information on the Exporter. Container is also available as a part of the Tinode docker distribution: `tinode/exporter`. Run it with ``` $ docker run -p 6222:6222 -d --name tinode-exporter --network tinode-net \ --env SERVE_FOR= \ --env TINODE_ADDR= \ ... \ tinode/exporter:latest ``` Available variables: | Variable | Type | Default | Function | | --- | --- | --- | --- | | `SERVE_FOR` | string | `` | Monitoring service: `prometheus` or `influxdb` | | `TINODE_ADDR` | string | `http://localhost/stats/expvar/` | Tinode metrics path | | `INFLUXDB_VERSION` | string | `1.7` | InfluxDB version (`1.7` or `2.0`) | | `INFLUXDB_ORGANIZATION` | string | `org` | InfluxDB organization | | `INFLUXDB_PUSH_INTERVAL` | int | `60` | Exporter metrics push interval in seconds | | `INFLUXDB_PUSH_ADDRESS` | string | `https://mon.tinode.co/intake` | InfluxDB backend url | | `INFLUXDB_AUTH_TOKEN` | string | `` | InfluxDB auth token | | `PROM_NAMESPACE` | string | `tinode` | Prometheus namespace | | `PROM_METRICS_PATH` | string | `/metrics` | Exporter webserver path that Prometheus server scrapes | ================================================ FILE: docker/chatbot/Dockerfile ================================================ # Dockerfile builds an image with a chatbot (Tino) for Tinode. FROM python:3.13-slim ARG VERSION=0.25 ARG LOGIN_AS= ARG TINODE_HOST=tinode-srv:16060 ENV VERSION=$VERSION ARG BINVERS=$VERSION LABEL maintainer="Tinode Team " LABEL name="TinodeChatbot" LABEL version=$VERSION RUN mkdir -p /usr/src/bot WORKDIR /usr/src/bot # Volume with login cookie. Not created automatically. # VOLUME /botdata # Get tarball with the chatbot code and data. ADD https://github.com/tinode/chat/releases/download/v${BINVERS}/py-chatbot.tar.gz . # Unpack chatbot, delete archive RUN tar -xzf py-chatbot.tar.gz \ && rm py-chatbot.tar.gz RUN pip install --no-cache-dir -r requirements.txt # Healthcheck: try to connect to the gRPC port (40051) HEALTHCHECK --interval=1m --timeout=3s --start-period=15s \ CMD python -c "import socket; s=socket.create_connection(('localhost', 40051), timeout=1); s.close()" || exit 1 # Use docker's command line parameter `-e LOGIN_AS=user:password` to login as someone other than Tino. CMD ["/bin/sh", "-c", "python chatbot.py --login-basic=${LOGIN_AS} --login-cookie=/botdata/.tn-cookie --host=${TINODE_HOST} >> /var/log/chatbot.log"] # Plugin port EXPOSE 40051 ================================================ FILE: docker/docker-compose/README.md ================================================ # Docker compose for end-to-end setup. These reference docker-compose files will run Tinode with the MySql backend either as [a single-instance](single-instance.yml) or [a 3-node cluster](cluster.yml) setup. ``` docker-compose -f [-f ] up -d ``` By default, this command starts up a mysql instance, Tinode server(s) and Tinode exporter(s). Tinode server(s) is(are) configured similar to [Tinode Demo/Sandbox](../../README.md#demosandbox) and maps its web port to the host's port 6060 (6061, 6062). Tinode exporter(s) serve(s) metrics for InfluxDB. Reference configuration for the following databases is also available in the override files: * [PostgreSQL 15.2](https://hub.docker.com/_/postgres/tags) * [MongoDB 4.2.3](https://hub.docker.com/_/mongo/tags) * [RethinkDB 2.4.2](https://hub.docker.com/_/rethinkdb/tags) ## Commands ### Full stack To bring up the full stack, you can use the following commands: * MySql: - Single-instance setup: `docker-compose -f single-instance.yml up -d` - Cluster: `docker-compose -f cluster.yml up -d` * PostgreSQL: - Single-instance setup: `docker-compose -f single-instance.yml -f single-instance.postgres.yml up -d` - Cluster: `docker-compose -f cluster.yml -f cluster.postgres.yml up -d` * MongoDb: - Single-instance setup: `docker-compose -f single-instance.yml -f single-instance.mongodb.yml up -d` - Cluster: `docker-compose -f cluster.yml -f cluster.mongodb.yml up -d` * RethinkDb: - Single-instance setup: `docker-compose -f single-instance.yml -f single-instance.rethinkdb.yml up -d` - Cluster: `docker-compose -f cluster.yml -f cluster.rethinkdb.yml up -d` You can run individual/separate components of the setup by providing their names to the `docker-compose` command. E.g. to start the Tinode server in the single-instance MySql setup, ``` docker-compose -f single-instance.yml up -d tinode-0 ``` ### Database resets and/or version upgrades To reset the database or upgrade the database version, you can set `RESET_DB` or `UPGRADE_DB` environment variable to true when starting Tinode with docker-compose. E.g. for upgrading the database in MongoDb cluster setup, use: ``` UPGRADE_DB=true docker-compose -f cluster.yml -f cluster.mongodb.yml up -d tinode-0 ``` For resetting the database in RethinkDb single-instance setup, run: ``` RESET_DB=true docker-compose -f single-instance.yml -f single-instance.rethinkdb.yml up -d tinode-0 ``` ## Troubleshooting Print out and verify your docker-compose configuration by running: ``` docker-compose -f [-f ] config ``` If the Tinode server(s) are failing, you can print the job's stdout/stderr with: ``` docker logs tinode- ``` Additionally, you can examine the jobs `tinode.log` file. To download it from the container, run: ``` docker cp tinode-:/var/log/tinode.log . ``` ================================================ FILE: docker/docker-compose/cluster.mongodb.yml ================================================ version: '3.8' x-mongodb-tinode-env-vars: &mongodb-tinode-env-vars "STORE_USE_ADAPTER": "mongodb" services: db: image: mongo:4.2.3 container_name: mongodb entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ] healthcheck: test: ["CMD", "curl -f http://localhost:28017/ || exit 1"] # Initializes MongoDb replicaset. initdb: image: mongo:4.2.3 container_name: initdb depends_on: - db command: > bash -c "echo 'Starting replica set initialize'; until mongo --host mongodb --eval 'print(\"waited for connection\")'; do sleep 2; done; echo 'Connection finished'; echo 'Creating replica set'; echo \"rs.initiate({'_id': 'rs0', "members": [ {'_id': 0, 'host': 'mongodb:27017'} ]})\" | mongo --host mongodb" tinode-0: environment: << : *mongodb-tinode-env-vars "WAIT_FOR": "mongodb:27017" tinode-1: environment: << : *mongodb-tinode-env-vars tinode-2: environment: << : *mongodb-tinode-env-vars ================================================ FILE: docker/docker-compose/cluster.postgres.yml ================================================ version: '3.8' x-postgres-tinode-env-vars: &postgres-tinode-env-vars "STORE_USE_ADAPTER": "postgres" services: db: image: postgres:15.2 container_name: postgres healthcheck: test: ["CMD-SHELL", "pg_isready"] tinode-0: environment: << : *postgres-tinode-env-vars "WAIT_FOR": "postgres:5432" tinode-1: environment: << : *postgres-tinode-env-vars tinode-2: environment: << : *postgres-tinode-env-vars ================================================ FILE: docker/docker-compose/cluster.rethinkdb.yml ================================================ version: '3.8' x-rethinkdb-tinode-env-vars: &rethinkdb-tinode-env-vars "STORE_USE_ADAPTER": "rethinkdb" services: db: image: rethinkdb:2.4.2 container_name: rethinkdb healthcheck: test: ["CMD", "curl -f http://localhost:8080/ || exit 1"] tinode-0: environment: << : *rethinkdb-tinode-env-vars "WAIT_FOR": "rethinkdb:8080" tinode-1: environment: << : *rethinkdb-tinode-env-vars tinode-2: environment: << : *rethinkdb-tinode-env-vars ================================================ FILE: docker/docker-compose/cluster.yml ================================================ # Reference configuration for a simple 3-node Tinode cluster. # Includes: # * Mysql database # * 3 Tinode servers # * 3 exporters version: '3.8' # Base Tinode template. x-tinode: &tinode-base depends_on: - db image: tinode/tinode:latest restart: always x-exporter: &exporter-base image: tinode/exporter:latest restart: always x-tinode-env-vars: &tinode-env-vars "STORE_USE_ADAPTER": "mysql" "PPROF_URL": "/pprof" # You can provide your own tinode config by setting EXT_CONFIG env var and binding your configuration file to # "EXT_CONFIG": "/etc/tinode/tinode.conf" "WAIT_FOR": "mysql:3306" # Push notifications. # Modify as appropriate. # Tinode Push Gateway configuration. "TNPG_PUSH_ENABLED": "false" # "TNPG_USER": "" # "TNPG_AUTH_TOKEN": "" # FCM specific server configuration. "FCM_PUSH_ENABLED": "false" # "FCM_CRED_FILE": "" # "FCM_INCLUDE_ANDROID_NOTIFICATION": false # # FCM Web client configuration. "FCM_API_KEY": "AIzaSyD6X4ULR-RUsobvs1zZ2bHdJuPz39q2tbQ" "FCM_APP_ID": "1:114126160546:web:aca6ea2981feb81fb44dfb" "FCM_PROJECT_ID": "tinode-1000" "FCM_SENDER_ID": 114126160546 "FCM_VAPID_KEY": "BOgQVPOMzIMXUpsYGpbVkZoEBc0ifKY_f2kSU5DNDGYI6i6CoKqqxDd7w7PJ3FaGRBgVGJffldETumOx831jl58" "FCM_MEASUREMENT_ID": "G-WNJDQR34L3" # iOS app universal links configuration. # "IOS_UNIV_LINKS_APP_ID": "" # Video calls "WEBRTC_ENABLED": "false" # "ICE_SERVERS_FILE": "" x-exporter-env-vars: &exporter-env-vars "TINODE_ADDR": "http://tinode.host:18080/stats/expvar/" # InfluxDB configation: "SERVE_FOR": "influxdb" "INFLUXDB_VERSION": 1.7 "INFLUXDB_ORGANIZATION": "" "INFLUXDB_PUSH_INTERVAL": 30 "INFLUXDB_PUSH_ADDRESS": "https://mon.tinode.co/intake" "INFLUXDB_AUTH_TOKEN": "" # Prometheus configuration: # "SERVE_FOR": "prometheus" # "PROM_NAMESPACE": "tinode" # "PROM_METRICS_PATH": "/metrics" services: db: image: mysql:8.0 container_name: mysql restart: always # Use your own volume. # volumes: # - :/var/lib/mysql environment: - MYSQL_ALLOW_EMPTY_PASSWORD=yes healthcheck: test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"] timeout: 5s retries: 10 security_opt: - seccomp=unconfined # Tinode servers. tinode-0: << : *tinode-base container_name: tinode-0 hostname: tinode-0 # You can mount your volumes as necessary: # volumes: # # E.g. external config (assuming EXT_CONFIG is set). # - :/etc/tinode/tinode.conf # # Logs directory. # - :/var/log ports: - "6060:6060" environment: << : *tinode-env-vars "CLUSTER_SELF": "tinode-0" "RESET_DB": ${RESET_DB:-false} "UPGRADE_DB": ${UPGRADE_DB:-false} tinode-1: << : *tinode-base container_name: tinode-1 hostname: tinode-1 # You can mount your volumes as necessary: # volumes: # # E.g. external config (assuming EXT_CONFIG is set). # - :/etc/tinode/tinode.conf # # Logs directory. # - :/var/log ports: - "6061:6060" environment: << : *tinode-env-vars "CLUSTER_SELF": "tinode-1" # Wait for tinode-0, not the database since # we let tinode-0 perform all database initialization and upgrade work. "WAIT_FOR": "tinode-0:6060" "NO_DB_INIT": "true" tinode-2: << : *tinode-base container_name: tinode-2 hostname: tinode-2 # You can mount your volumes as necessary: # volumes: # # E.g. external config (assuming EXT_CONFIG is set). # - :/etc/tinode/tinode.conf # # Logs directory. # - :/var/log ports: - "6062:6060" environment: << : *tinode-env-vars "CLUSTER_SELF": "tinode-2" # Wait for tinode-0, not the database since # we let tinode-0 perform all database initialization and upgrade work. "WAIT_FOR": "tinode-0:6060" "NO_DB_INIT": "true" # Monitoring. # Exporters are paired with tinode instances. exporter-0: << : *exporter-base container_name: exporter-0 hostname: exporter-0 depends_on: - tinode-0 ports: - 6222:6222 links: - tinode-0:tinode.host environment: << : *exporter-env-vars "INSTANCE": "tinode-0" "WAIT_FOR": "tinode-0:6060" exporter-1: << : *exporter-base container_name: exporter-1 hostname: exporter-1 depends_on: - tinode-1 ports: - 6223:6222 links: - tinode-1:tinode.host environment: << : *exporter-env-vars "INSTANCE": "tinode-1" "WAIT_FOR": "tinode-1:6060" exporter-2: << : *exporter-base container_name: exporter-2 hostname: exporter-2 depends_on: - tinode-2 ports: - 6224:6222 links: - tinode-2:tinode.host environment: << : *exporter-env-vars "INSTANCE": "tinode-2" "WAIT_FOR": "tinode-2:6060" ================================================ FILE: docker/docker-compose/single-instance.mongodb.yml ================================================ version: '3.8' x-mongodb-tinode-env-vars: &mongodb-tinode-env-vars "STORE_USE_ADAPTER": "mongodb" services: db: image: mongo:4.2.3 container_name: mongodb entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ] healthcheck: test: ["CMD", "curl -f http://localhost:28017/ || exit 1"] # Initializes MongoDb replicaset. initdb: image: mongo:4.2.3 container_name: initdb depends_on: - db command: > bash -c "echo 'Starting replica set initialize'; until mongo --host mongodb --eval 'print(\"waited for connection\")'; do sleep 2; done; echo 'Connection finished'; echo 'Creating replica set'; echo \"rs.initiate({'_id': 'rs0', "members": [ {'_id': 0, 'host': 'mongodb:27017'} ]})\" | mongo --host mongodb" tinode-0: environment: << : *mongodb-tinode-env-vars "WAIT_FOR": "mongodb:27017" ================================================ FILE: docker/docker-compose/single-instance.postgres.yml ================================================ version: '3.8' x-postgres-tinode-env-vars: &postgres-tinode-env-vars "STORE_USE_ADAPTER": "postgres" services: db: image: postgres:15.2 container_name: postgres healthcheck: test: ["CMD-SHELL", "pg_isready"] tinode-0: environment: << : *postgres-tinode-env-vars "WAIT_FOR": "postgres:5432" ================================================ FILE: docker/docker-compose/single-instance.rethinkdb.yml ================================================ version: '3.8' x-rethinkdb-tinode-env-vars: &rethinkdb-tinode-env-vars "STORE_USE_ADAPTER": "rethinkdb" services: db: image: rethinkdb:2.4.0 container_name: rethinkdb healthcheck: test: ["CMD", "curl -f http://localhost:8080/ || exit 1"] tinode-0: environment: << : *rethinkdb-tinode-env-vars "WAIT_FOR": "rethinkdb:8080" ================================================ FILE: docker/docker-compose/single-instance.yml ================================================ # Reference configuration for a simple Tinode server. # Includes: # * Mysql database # * Tinode server # * Tinode exporters version: '3.8' # Base Tinode template. x-tinode: &tinode-base depends_on: - db image: tinode/tinode:latest restart: always x-tinode-env-vars: &tinode-env-vars "STORE_USE_ADAPTER": "mysql" "PPROF_URL": "/pprof" # You can provide your own tinode config by setting EXT_CONFIG env var and binding your configuration file to # "EXT_CONFIG": "/etc/tinode/tinode.conf" "WAIT_FOR": "mysql:3306" # Push notifications. # Modify as appropriate. # Tinode Push Gateway configuration. "TNPG_PUSH_ENABLED": "false" # "TNPG_USER": "" # "TNPG_AUTH_TOKEN": "" # FCM specific server configuration. "FCM_PUSH_ENABLED": "false" # "FCM_CRED_FILE": "" # "FCM_INCLUDE_ANDROID_NOTIFICATION": false # # FCM Web client configuration. "FCM_API_KEY": "AIzaSyD6X4ULR-RUsobvs1zZ2bHdJuPz39q2tbQ" "FCM_APP_ID": "1:114126160546:web:aca6ea2981feb81fb44dfb" "FCM_PROJECT_ID": "tinode-1000" "FCM_SENDER_ID": 114126160546 "FCM_VAPID_KEY": "BOgQVPOMzIMXUpsYGpbVkZoEBc0ifKY_f2kSU5DNDGYI6i6CoKqqxDd7w7PJ3FaGRBgVGJffldETumOx831jl58" "FCM_MEASUREMENT_ID": "G-WNJDQR34L3" # iOS app universal links configuration. # "IOS_UNIV_LINKS_APP_ID": "" # Video calls "WEBRTC_ENABLED": "false" # "ICE_SERVERS_FILE": "" x-exporter-env-vars: &exporter-env-vars "TINODE_ADDR": "http://tinode.host:6060/stats/expvar/" # InfluxDB configation: "SERVE_FOR": "influxdb" "INFLUXDB_VERSION": 1.7 "INFLUXDB_ORGANIZATION": "" "INFLUXDB_PUSH_INTERVAL": 30 "INFLUXDB_PUSH_ADDRESS": "https://mon.tinode.co/intake" "INFLUXDB_AUTH_TOKEN": "" # Prometheus configuration: # "SERVE_FOR": "prometheus" # "PROM_NAMESPACE": "tinode" # "PROM_METRICS_PATH": "/metrics" services: db: image: mysql:8.0 container_name: mysql restart: always # Use your own volume. # volumes: # - :/var/lib/mysql environment: - MYSQL_ALLOW_EMPTY_PASSWORD=yes healthcheck: test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"] timeout: 5s retries: 10 security_opt: - seccomp=unconfined # Tinode. tinode-0: << : *tinode-base container_name: tinode-0 hostname: tinode-0 # You can mount your volumes as necessary: # volumes: # # E.g. external config (assuming EXT_CONFIG is set). # - :/etc/tinode/tinode.conf # # Logs directory. # - :/var/log ports: - "6060:6060" environment: << : *tinode-env-vars "RESET_DB": ${RESET_DB:-false} "UPGRADE_DB": ${UPGRADE_DB:-false} # Monitoring. # Exporters are paired with tinode instances. exporter-0: container_name: exporter-0 hostname: exporter-0 depends_on: - tinode-0 image: tinode/exporter:latest restart: always ports: - "6222:6222" links: - tinode-0:tinode.host environment: << : *exporter-env-vars "WAIT_FOR": "tinode-0:6060" ================================================ FILE: docker/exporter/Dockerfile ================================================ FROM alpine:3.14 ARG VERSION=0.16.4 ENV VERSION=$VERSION LABEL maintainer="Tinode Team " LABEL name="TinodeMetricExporter" LABEL version=$VERSION ENV SERVE_FOR="" ENV WAIT_FOR="" ENV TINODE_ADDR=http://localhost/stats/expvar/ ENV INSTANCE="exporter-instance" ENV INFLUXDB_VERSION=1.7 ENV INFLUXDB_ORGANIZATION="org" ENV INFLUXDB_PUSH_INTERVAL=60 ENV INFLUXDB_PUSH_ADDRESS="" ENV INFLUXDB_AUTH_TOKEN="" ENV PROM_NAMESPACE="tinode" ENV PROM_METRICS_PATH="/metrics" WORKDIR /opt/tinode RUN apk add --no-cache bash # Fetch exporter build from Github. ADD https://github.com/tinode/chat/releases/download/v$VERSION/exporter.linux-amd64 ./exporter COPY entrypoint.sh . RUN chmod +x exporter && chmod +x entrypoint.sh ENTRYPOINT ./entrypoint.sh EXPOSE 6222 ================================================ FILE: docker/exporter/entrypoint.sh ================================================ #!/bin/bash # Check if environment variables (provided as argument list) are set. function check_vars() { local varnames=( "$@" ) for varname in "${varnames[@]}" do eval value=\$${varname} if [ -z "$value" ] ; then echo "$varname env var must be specified." exit 1 fi done } # Make sure the system uses /etc/hosts when resolving domain names # (needed for docker-compose's `extra_hosts` param to work correctly). # See https://github.com/gliderlabs/docker-alpine/issues/367, # https://github.com/golang/go/issues/35305 for details. echo "hosts: files dns" > /etc/nsswitch.conf # Accept http requests at. LISTEN_AT=":6222" # Required env vars. common_vars=( TINODE_ADDR INSTANCE SERVE_FOR ) influx_varnames=( INFLUXDB_VERSION INFLUXDB_ORGANIZATION INFLUXDB_PUSH_INTERVAL \ INFLUXDB_PUSH_ADDRESS INFLUXDB_AUTH_TOKEN ) prometheus_varnames=( PROM_NAMESPACE PROM_METRICS_PATH ) check_vars "${common_vars[@]}" # Common arguments. args=("--tinode_addr=${TINODE_ADDR}" "--instance=${INSTANCE}" "--listen_at=${LISTEN_AT}" "--serve_for=${SERVE_FOR}") # Platform-specific arguments. case "$SERVE_FOR" in "prometheus") check_vars "${prometheus_varnames[@]}" args+=("--prom_namespace=${PROM_NAMESPACE}" "--prom_metrics_path=${PROM_METRICS_PATH}") if [ ! -z "$PROM_TIMEOUT" ]; then args+=("--prom_timeout=${PROM_TIMEOUT}") fi ;; "influxdb") check_vars "${influxdb_varnames[@]}" args+=("--influx_db_version=${INFLUXDB_VERSION}" \ "--influx_organization=${INFLUXDB_ORGANIZATION}" \ "--influx_push_interval=${INFLUXDB_PUSH_INTERVAL}" \ "--influx_push_addr=${INFLUXDB_PUSH_ADDRESS}" \ "--influx_auth_token=${INFLUXDB_AUTH_TOKEN}") if [ ! -z "$INFLUXDB_BUCKET" ]; then args+=("--influx_bucket=${INFLUXDB_BUCKET}") fi ;; *) echo "\$SERVE_FOR must be set to either 'prometheus' or 'influxdb'" exit 1 ;; esac # Wait for Tinode server if needed. if [ ! -z "$WAIT_FOR" ] ; then IFS=':' read -ra TND <<< "$WAIT_FOR" if [ ${#TND[@]} -ne 2 ]; then echo "\$WAIT_FOR (${WAIT_FOR}) env var should be in form HOST:PORT" exit 1 fi until nc -z -v -w5 ${TND[0]} ${TND[1]}; do echo "waiting for ${WAIT_FOR}..."; sleep 5; done fi ./exporter "${args[@]}" ================================================ FILE: docker/tinode/Dockerfile ================================================ # Docker file builds an image with a tinode chat server. # # In order to run the image you have to link it to a running database container. For example, to # to use RethinkDB (named 'rethinkdb') and map the port where the tinode server accepts connections: # # $ docker run -p 6060:6060 -d --link rethinkdb \ # --env UID_ENCRYPTION_KEY=base64+encoded+16+bytes= \ # --env API_KEY_SALT=base64+encoded+32+bytes \ # --env AUTH_TOKEN_KEY=base64+encoded+32+bytes \ # tinode-server FROM alpine:3.22 ARG VERSION=0.25 ENV VERSION=$VERSION ARG BINVERS=$VERSION LABEL maintainer="Tinode Team " LABEL name="TinodeChatServer" LABEL version=$VERSION # Build-time options. # Database selector. Builds for MySQL by default. # Alternatively use one of: postgres mongodb rethinkdb for a corresponsing # DB backend or alldbs to build a generic Tinode docker image, for example: # `--build-arg TARGET_DB=postgres` to build for PostgreSQL. ARG TARGET_DB=mysql ENV TARGET_DB=$TARGET_DB # Runtime options. # Specifies the database host:port pair to wait for before running Tinode. # Ignored if empty. ENV WAIT_FOR= # An option to reset database. ENV RESET_DB=false # An option to upgrade database. ENV UPGRADE_DB=false # Option to skip DB initialization when it's missing. ENV NO_DB_INIT=false # Load sample data to database from data.json. ARG SAMPLE_DATA=data.json ENV SAMPLE_DATA=$SAMPLE_DATA # Default country code to use in communication. ENV DEFAULT_COUNTRY_CODE=US # The MySQL DSN connection. ENV MYSQL_DSN='root@tcp(mysql)/tinode?parseTime=true&collation=utf8mb4_0900_ai_ci' # The PostgreSQL DSN connection. ENV POSTGRES_DSN='postgresql://postgres:postgres@localhost:5432/tinode?sslmode=disable&connect_timeout=10' # Disable chatbot plugin by default. ENV PLUGIN_PYTHON_CHAT_BOT_ENABLED=false # Default handler for large files ENV MEDIA_HANDLER=fs # Whitelisted domains for file and S3 large media handler. ENV FS_CORS_ORIGINS='["*"]' ENV AWS_CORS_ORIGINS='["*"]' # AWS S3 parameters ENV AWS_ACCESS_KEY_ID= ENV AWS_SECRET_ACCESS_KEY= ENV AWS_REGION= ENV AWS_S3_BUCKET= ENV AWS_S3_ENDPOINT= # Default externally-visible hostname for email verification. ENV SMTP_HOST_URL='http://localhost:6060' # Email parameters decalarations. ENV SMTP_SERVER= ENV SMTP_PORT= ENV SMTP_SENDER= ENV SMTP_LOGIN= ENV SMTP_PASSWORD= ENV SMTP_AUTH_MECHANISM= ENV SMTP_HELO_HOST= ENV EMAIL_VERIFICATION_REQUIRED= ENV DEBUG_EMAIL_VERIFICATION_CODE= # Whitelist of permitted email domains for email verification (empty list means all domains are permitted) ENV SMTP_DOMAINS='' # Various encryption and salt keys. Replace with your own in production. # Salt used to generate the API key. Don't change it unless you also change the # API key in the webapp & Android. ENV API_KEY_SALT=T713/rYYgW7g4m3vG6zGRh7+FM1t0T8j13koXScOAj4= # Key used to sign authentication tokens. ENV AUTH_TOKEN_KEY=wfaY2RgF2S1OQI/ZlK+LSrp1KB2jwAdGAIHQ7JZn+Kc= # Key to initialize UID generator ENV UID_ENCRYPTION_KEY=la6YsO+bNX/+XIkOqc5Svw== # Disable TLS by default. ENV TLS_ENABLED=false ENV TLS_DOMAIN_NAME= ENV TLS_CONTACT_ADDRESS= # Disable push notifications by default. ENV FCM_PUSH_ENABLED=false # Declare FCM-related vars ENV FCM_API_KEY= ENV FCM_APP_ID= ENV FCM_SENDER_ID= ENV FCM_PROJECT_ID= ENV FCM_VAPID_KEY= ENV FCM_MEASUREMENT_ID= # Enable Android-specific notifications by default. ENV FCM_INCLUDE_ANDROID_NOTIFICATION=true # Disable push notifications via Tinode Push Gateway. ENV TNPG_PUSH_ENABLED=false # Tinode Push Gateway authentication token. ENV TNPG_AUTH_TOKEN= # Tinode Push Gateway organization name as registered at console.tinode.co ENV TNPG_ORG= # Video calls configuration. ENV WEBRTC_ENABLED=false ENV ICE_SERVERS_FILE= # Use the target db by default. # When TARGET_DB is "alldbs", it is the user's responsibility # to set STORE_USE_ADAPTER to the desired db adapter correctly. ENV STORE_USE_ADAPTER=$TARGET_DB # Url path for exposing the server's internal status. E.g. '/status' ENV SERVER_STATUS_PATH='' # Garbage collection of unfinished account registrations. ENV ACC_GC_ENABLED=false # Install root certificates, they are needed for email validator to work # with the TLS SMTP servers like Gmail or Mailjet. Also add bash and grep. RUN apk update && \ apk add --no-cache ca-certificates bash grep WORKDIR /opt/tinode # Copy config template to the container. COPY config.template . COPY entrypoint.sh . # Get the desired Tinode build. ADD https://github.com/tinode/chat/releases/download/v$BINVERS/tinode-$TARGET_DB.linux-amd64.tar.gz . # Unpack the Tinode archive. RUN tar -xzf tinode-$TARGET_DB.linux-amd64.tar.gz \ && rm tinode-$TARGET_DB.linux-amd64.tar.gz # Create directory for chatbot data. RUN mkdir /botdata # Make scripts runnable RUN chmod +x entrypoint.sh RUN chmod +x credentials.sh # Healthcheck: check if port 6060 is open HEALTHCHECK --interval=1m --timeout=3s --start-period=30s \ CMD nc -z localhost 6060 || exit 1 # Generate config from template and run the server. ENTRYPOINT ["./entrypoint.sh"] # HTTP, gRPC, cluster ports EXPOSE 6060 16060 12000-12003 ================================================ FILE: docker/tinode/config.template ================================================ { "listen": ":6060", "api_path": "/", "cache_control": 39600, "static_mount": "/", "grpc_listen": ":16060", "grpc_keepalive_enabled": true, "api_key_salt": "$API_KEY_SALT", "max_message_size": 4194304, "max_subscriber_count": 128, "max_tag_count": 16, "expvar": "/stats/expvar/", "server_status": "$SERVER_STATUS_PATH", "use_x_forwarded_for": true, "default_country_code": "$DEFAULT_COUNTRY_CODE", "media": { "use_handler": "$MEDIA_HANDLER", "max_size": 33554432, "gc_period": 60, "gc_block_size": 100, "handlers": { "fs": { "upload_dir": "uploads", "cache_control": "max-age=86400", "cors_origins": $FS_CORS_ORIGINS }, "s3":{ "access_key_id": "$AWS_ACCESS_KEY_ID", "secret_access_key": "$AWS_SECRET_ACCESS_KEY", "region": "$AWS_REGION", "bucket": "$AWS_S3_BUCKET", "endpoint": "$AWS_S3_ENDPOINT", "presign_ttl": 3600, "cache_control": "max-age=86400", "cors_origins": $AWS_CORS_ORIGINS } } }, "tls": { "enabled": $TLS_ENABLED, "http_redirect": ":80", "strict_max_age": 604800, "autocert": { "cache": "/etc/letsencrypt/live/$TLS_DOMAIN_NAME", "email": "$TLS_CONTACT_ADDRESS", "domains": ["$TLS_DOMAIN_NAME"] } }, "auth_config": { "logical_names": [], "basic": { "add_to_tags": true, "min_login_length": 3, "min_password_length": 6 }, "token": { "expire_in": 1209600, "serial_num": 1, "key": "$AUTH_TOKEN_KEY" }, "code": { "expire_in": 900, "max_retries": 3, "code_length": 6 } }, "store_config": { "uid_key": "$UID_ENCRYPTION_KEY", "max_results": 1024, "use_adapter": "$STORE_USE_ADAPTER", "adapters": { "mysql": { "database": "tinode", "dsn": "$MYSQL_DSN" }, "postgres": { "database": "tinode", "dsn": "$POSTGRES_DSN" }, "rethinkdb": { "database": "tinode", "addresses": "rethinkdb" }, "mongodb": { "database": "tinode", "addresses": "mongodb", "replica_set": "rs0" } } }, "acc_validation": { "email": { "add_to_tags": true, "required": [$EMAIL_VERIFICATION_REQUIRED], "config": { "host_url": "$SMTP_HOST_URL", "smtp_server": "$SMTP_SERVER", "smtp_port": "$SMTP_PORT", "sender": "$SMTP_SENDER", "login": "$SMTP_LOGIN", "sender_password": "$SMTP_PASSWORD", "auth_mechanism": "$SMTP_AUTH_MECHANISM", "smtp_helo_host": "$SMTP_HELO_HOST", "languages": ["en", "es", "fr", "ru", "vi", "zh"], "validation_templ": "./templ/email-validation-{{.Language}}.templ", "reset_secret_templ": "./templ/email-password-reset-{{.Language}}.templ", "max_retries": 3, "domains": [$SMTP_DOMAINS], "debug_response": "$DEBUG_EMAIL_VERIFICATION_CODE" } }, "tel": { "add_to_tags": true, "config": { "host_url": "$TEL_HOST_URL", "languages": ["en", "es", "fr", "pt", "ru", "vi", "zh"], "sender": "$TEL_SENDER", "universal_templ": "./templ/sms-universal-{{.Language}}.templ", "max_retries": 3, "debug_response": "$DEBUG_TEL_VERIFICATION_CODE" } } }, "acc_gc_config": { "enabled": $ACC_GC_ENABLED, "gc_period": 3600, "gc_block_size": 10, "gc_min_account_age": 48 }, "push": [ { "name":"tnpg", "config": { "enabled": $TNPG_PUSH_ENABLED, "token": "$TNPG_AUTH_TOKEN", "org": "$TNPG_ORG" } }, { "name":"fcm", "config": { "enabled": $FCM_PUSH_ENABLED, "project_id": "$FCM_PROJECT_ID", "credentials_file": "$FCM_CRED_FILE", "time_to_live": 3600, "android": { "enabled": $FCM_INCLUDE_ANDROID_NOTIFICATION, "icon": "ic_logo_push", "icon_color": "#3949AB", "click_action": ".MessageActivity", "msg": { "title_loc_key": "new_message", "title": "", "body_loc_key": "", "body": "" }, "sub": { "title_loc_key": "new_chat", "body_loc_key": "" } } } } ], "webrtc": { "enabled": $WEBRTC_ENABLED, "call_establishment_timeout": 30, "ice_servers_file": "$ICE_SERVERS_FILE" }, "cluster_config": { "self": "", "nodes": [ {"name": "tinode-0", "addr": "tinode-0:12000"}, {"name": "tinode-1", "addr": "tinode-1:12001"}, {"name": "tinode-2", "addr": "tinode-2:12002"} ], "failover": { "enabled": true, "heartbeat": 100, "vote_after": 8, "node_fail_after": 16 } }, "plugins": [ { "enabled": $PLUGIN_PYTHON_CHAT_BOT_ENABLED, "name": "python_chat_bot", "timeout": 20000, "filters": { "account": "C" }, "failure_code": 0, "failure_text": null, "service_addr": "tcp://localhost:40051" } ] } ================================================ FILE: docker/tinode/entrypoint.sh ================================================ #!/bin/bash # If EXT_CONFIG is set, use it as a config file. if [ ! -z "$EXT_CONFIG" ] ; then CONFIG="$EXT_CONFIG" # Enable push notifications. if [ ! -z "$FCM_SENDER_ID" ] ; then FCM_PUSH_ENABLED=true fi else CONFIG=working.config # Remove the old config. rm -f working.config # The 'alldbs' is not a valid adapter name. if [ "$TARGET_DB" = "alldbs" ] ; then TARGET_DB= fi # Enable email verification if $SMTP_SERVER is defined. if [ ! -z "$SMTP_SERVER" ] ; then EMAIL_VERIFICATION_REQUIRED='"auth"' fi # Enable TLS (httpS). if [ ! -z "$TLS_DOMAIN_NAME" ] ; then TLS_ENABLED=true fi # Enable push notifications. if [ ! -z "$FCM_CRED_FILE" ] ; then FCM_PUSH_ENABLED=true fi if [ ! -z "$TNPG_AUTH_TOKEN" ] ; then TNPG_PUSH_ENABLED=true fi if [ ! -z "$ICE_SERVERS_FILE" ] ; then WEBRTC_ENABLED=true fi # Generate a new 'working.config' from template and environment while IFS='' read -r line || [[ -n $line ]] ; do while [[ "$line" =~ (\$[A-Z_][A-Z_0-9]*) ]] ; do LHS=${BASH_REMATCH[1]} RHS="$(eval echo "\"$LHS\"")" line=${line//$LHS/"$RHS"} done echo "$line" >> working.config done < config.template fi # If external static dir is defined, use it. # Otherwise, fall back to "./static". if [ ! -z "$EXT_STATIC_DIR" ] ; then STATIC_DIR=$EXT_STATIC_DIR else STATIC_DIR="./static" fi # Do not load data when upgrading database. if [ "$UPGRADE_DB" = "true" ] ; then SAMPLE_DATA= fi # If push notifications are enabled, generate client-side firebase config file. if [ ! -z "$FCM_PUSH_ENABLED" ] || [ ! -z "$TNPG_PUSH_ENABLED" ] ; then # Write client config to $STATIC_DIR/firebase-init.js cat > $STATIC_DIR/firebase-init.js <<- EOM const FIREBASE_INIT = { apiKey: "$FCM_API_KEY", appId: "$FCM_APP_ID", messagingSenderId: "$FCM_SENDER_ID", projectId: "$FCM_PROJECT_ID", messagingVapidKey: "$FCM_VAPID_KEY", measurementId: "$FCM_MEASUREMENT_ID" }; EOM else # Create an empty firebase-init.js echo "" > $STATIC_DIR/firebase-init.js fi if [ ! -z "$IOS_UNIV_LINKS_APP_ID" ] ; then # Write config to $STATIC_DIR/apple-app-site-association config file. # See https://developer.apple.com/library/archive/documentation/General/Conceptual/AppSearch/UniversalLinks.html for details. cat > $STATIC_DIR/apple-app-site-association <<- EOM { "applinks": { "apps": [], "details": [ { "appID": "$IOS_UNIV_LINKS_APP_ID", "paths": [ "*" ] } ] } } EOM fi # Wait for database if needed. if [ ! -z "$WAIT_FOR" ] ; then IFS=':' read -ra DB <<< "$WAIT_FOR" if [ ${#DB[@]} -ne 2 ]; then echo "\$WAIT_FOR (${WAIT_FOR}) env var should be in form HOST:PORT" exit 1 fi until nc -z -v -w5 ${DB[0]} ${DB[1]}; do echo "waiting for ${WAIT_FOR}..."; sleep 3; done fi # Initialize the database if it has not been initialized yet or if data reset/upgrade has been requested. init_stdout=./init-db-stdout.txt ./init-db \ --reset=${RESET_DB} \ --upgrade=${UPGRADE_DB} \ --config=${CONFIG} \ --data=${SAMPLE_DATA} \ --no_init=${NO_DB_INIT} \ 1>${init_stdout} if [ $? -ne 0 ]; then echo "./init-db failed. Quitting." exit 1 fi # If sample data was provided, try to find Tino password. if [ ! -z "$SAMPLE_DATA" ] ; then grep "usr;tino;" $init_stdout > /botdata/tino-password fi if [ -s /botdata/tino-password ] ; then # Convert Tino's authentication credentials into a cookie file. # /botdata/tino-password could be empty if DB was not updated. In such a case the # /botdata/.tn-cookie will not be modified. ./credentials.sh /botdata/.tn-cookie < /botdata/tino-password fi args=("--config=${CONFIG}" "--static_data=$STATIC_DIR" "--cluster_self=$CLUSTER_SELF" "--pprof_url=$PPROF_URL") # Run the tinode server. ./tinode "${args[@]}" 2>> /var/log/tinode.log ================================================ FILE: docker-build.sh ================================================ #!/bin/bash # Build Tinode docker linux/amd64 images. # You may have to install buildx https://docs.docker.com/buildx/working-with-buildx/ # if your build host and target architectures are different (e.g. building on a Mac # with Apple silicon). for line in $@; do eval "$line" done tag=${tag#?} if [ -z "$tag" ]; then echo "Must provide tag as 'tag=v1.2.3'" exit 1 fi # Convert tag into a version ver=( ${tag//./ } ) # if version contains a dash, it's not a full releave, i.e. v0.1.15.5-rc1 if [[ ${ver[2]} != *"-"* ]]; then FULLRELEASE=1 fi # Use buildx if the current platform is not x86. buildcmd='build' if [ `uname -m` != 'x86_64' ]; then buildcmd='buildx build --platform=linux/amd64' fi # If explicit DB is specified, build just one, otherwise build all. if [ "$db" ]; then dbtags=( "$db" ) else dbtags=( mysql postgres mongodb rethinkdb alldbs ) fi # Build an images for various DB backends for dbtag in "${dbtags[@]}" do if [ "$dbtag" == "alldbs" ]; then # For alldbs, container name is tinode/tinode. name="tinode/tinode" else # Otherwise, tinode/tinode-$dbtag. name="tinode/tinode-${dbtag}" fi separator= rmitags="${name}:${ver[0]}.${ver[1]}.${ver[2]}" buildtags="--tag ${name}:${ver[0]}.${ver[1]}.${ver[2]}" if [ -n "$FULLRELEASE" ]; then rmitags="${rmitags} ${name}:latest ${name}:${ver[0]}.${ver[1]}" buildtags="${buildtags} --tag ${name}:latest --tag ${name}:${ver[0]}.${ver[1]}" fi docker rmi ${rmitags} docker ${buildcmd} --build-arg VERSION=$tag --build-arg TARGET_DB=${dbtag} ${buildtags} docker/tinode done if [ "$db" ]; then exit 0 fi # Build chatbot image buildtags="--tag tinode/chatbot:${ver[0]}.${ver[1]}.${ver[2]}" rmitags="tinode/chatbot:${ver[0]}.${ver[1]}.${ver[2]}" if [ -n "$FULLRELEASE" ]; then rmitags="${rmitags} tinode/chatbot:latest tinode/chatbot:${ver[0]}.${ver[1]}" buildtags="${buildtags} --tag tinode/chatbot:latest --tag tinode/chatbot:${ver[0]}.${ver[1]}" fi docker rmi ${rmitags} docker ${buildcmd} --build-arg VERSION=$tag ${buildtags} docker/chatbot # Build exporter image buildtags="--tag tinode/exporter:${ver[0]}.${ver[1]}.${ver[2]}" rmitags="tinode/exporter:${ver[0]}.${ver[1]}.${ver[2]}" if [ -n "$FULLRELEASE" ]; then rmitags="${rmitags} tinode/exporter:latest tinode/exporter:${ver[0]}.${ver[1]}" buildtags="${buildtags} --tag tinode/exporter:latest --tag tinode/exporter:${ver[0]}.${ver[1]}" fi docker rmi ${rmitags} docker ${buildcmd} --build-arg VERSION=$tag ${buildtags} docker/exporter ================================================ FILE: docker-release.sh ================================================ #!/bin/bash # Publish Tinode docker images to hub.docker.com function containerName() { if [ "$1" == "alldbs" ]; then # For alldbs, container name is simply tinode. local name="tinode" else # Otherwise, tinode-$dbtag. local name="tinode-${dbtag}" fi echo $name } for line in $@; do eval "$line" done tag=${tag#?} if [ -z "$tag" ]; then echo "Must provide tag as 'tag=v1.2.3' or 'v1.2.3-abc0'" exit 1 fi # Convert tag into a version ver=( ${tag//./ } ) if [[ ${ver[2]} != *"-"* ]]; then FULLRELEASE=1 fi if [ "$db" ]; then dbtags=( "$db" ) else dbtags=( mysql postgres mongodb rethinkdb alldbs ) fi # Read dockerhub login/password from a separate file source .dockerhub # Login to docker hub docker login -u $user -p $pass # Deploy images for various DB backends for dbtag in "${dbtags[@]}" do name="$(containerName $dbtag)" # Deploy tagged image if [ -n "$FULLRELEASE" ]; then docker push tinode/${name}:latest docker push tinode/${name}:"${ver[0]}.${ver[1]}" fi docker push tinode/${name}:"${ver[0]}.${ver[1]}.${ver[2]}" done if [ "$db" ]; then exit 0 fi # Deploy chatbot images if [ -n "$FULLRELEASE" ]; then docker push tinode/chatbot:latest docker push tinode/chatbot:"${ver[0]}.${ver[1]}" fi docker push tinode/chatbot:"${ver[0]}.${ver[1]}.${ver[2]}" # Deploy exporter images if [ -n "$FULLRELEASE" ]; then docker push tinode/exporter:latest docker push tinode/exporter:"${ver[0]}.${ver[1]}" fi docker push tinode/exporter:"${ver[0]}.${ver[1]}.${ver[2]}" docker logout ================================================ FILE: docs/API.md ================================================ - [Server API](#server-api) - [How it Works?](#how-it-works) - [General Considerations](#general-considerations) - [Connecting to the Server](#connecting-to-the-server) - [gRPC](#grpc) - [WebSocket](#websocket) - [Long Polling](#long-polling) - [Out of Band Large Files](#out-of-band-large-files) - [Running Behind a Reverse Proxy](#running-behind-a-reverse-proxy) - [Users](#users) - [Authentication](#authentication) - [Creating an Account](#creating-an-account) - [Logging in](#logging-in) - [Changing Authentication Parameters](#changing-authentication-parameters) - [Resetting a Password, i.e. "Forgot Password"](#resetting-a-password-ie-forgot-password) - [Suspending a User](#suspending-a-user) - [Credential Validation](#credential-validation) - [Access Control](#access-control) - [Topics](#topics) - [me Topic](#me-topic) - [fnd and Tags: Finding Users and Topics](#fnd-and-tags-finding-users-and-topics) - [Query Language](#query-language) - [Incremental Updates to Queries](#incremental-updates-to-queries) - [Query Rewrite](#query-rewrite) - [Possible Use Cases](#possible-use-cases) - [Peer to Peer Topics](#peer-to-peer-topics) - [Group Topics](#group-topics) - [sys Topic](#sys-topic) - [Using Server-Issued Message IDs](#using-server-issued-message-ids) - [User Agent and Presence Notifications](#user-agent-and-presence-notifications) - [Trusted, Public, Private, Auxiliary Fields](#trusted-public-private-auxiliary-fields) - [Trusted](#trusted) - [Public](#public) - [Private](#private) - [Auxiliary](#auxiliary) - [Format of Content](#format-of-content) - [Out-of-Band Handling of Large Files](#out-of-band-handling-of-large-files) - [Uploading](#uploading) - [Downloading](#downloading) - [Push Notifications](#push-notifications) - [Tinode Push Gateway](#tinode-push-gateway) - [Google FCM](#google-fcm) - [Stdout](#stdout) - [Video Calls](#video-calls) - [Link Previews](#link-previews) - [Messages](#messages) - [Client to Server Messages](#client-to-server-messages) - [{hi}](#hi) - [{acc}](#acc) - [{login}](#login) - [{sub}](#sub) - [{leave}](#leave) - [{pub}](#pub) - [{get}](#get) - [{set}](#set) - [{del}](#del) - [{note}](#note) - [Server to Client Messages](#server-to-client-messages) - [{data}](#data) - [{ctrl}](#ctrl) - [{meta}](#meta) - [{pres}](#pres) - [{info}](#info) # Server API ## How it Works? Tinode is an IM router and a store. Conceptually it loosely follows a [publish-subscribe](https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern) model. Server connects sessions, users, and topics. Session is a network connection between a client application and the server. User represents a human being who connects to the server with a session. Topic is a named communication channel which routes content between sessions. Users and topics are assigned unique IDs. User ID is a string with 'usr' prefix followed by base64-URL-encoded pseudo-random 64-bit number, e.g. `usr2il9suCbuko`. Topic IDs are described below. Clients such as mobile or web applications create sessions by connecting to the server over a websocket or through long polling. Client authentication is required in order to perform most operations. Client authenticates the session by sending a `{login}` packet. See [Authentication](#authentication) section for details. Once authenticated, the client receives a token which is used for authentication later. Multiple simultaneous sessions may be established by the same user. Logging out is not supported (and not needed). Once the session is established, the user can start interacting with other users through topics. The following topic types are available: * `me` is a topic for managing one's profile and receiving notifications about other topics; `me` topic exists for every user. * `fnd` topic is used for finding other users and topics; `fnd` topic also exists for every user. * Peer to peer topic is a communication channel strictly between two users. Each participant sees topic name as the ID of the other participant: 'usr' prefix followed by a base64-URL-encoded numeric part of user ID, e.g. `usr2il9suCbuko`. * Group topic is a channel for multi-user communication. It's named as 'grp' followed by 11 pseudo-random characters, i.e. `grpYiqEXb4QY6s`. Group topics must be explicitly created. Session joins a topic by sending a `{sub}` packet. Packet `{sub}` serves three functions: creating a new topic, subscribing user to a topic, and attaching session to a topic. See [`{sub}`](#sub) section below for details. Once the session has joined the topic, the user may start generating content by sending `{pub}` packets. The content is delivered to other attached sessions as `{data}` packets. The user may query or update topic metadata by sending `{get}` and `{set}` packets. Changes to topic metadata, such as changes in topic description, or when other users join or leave the topic, is reported to live sessions with `{pres}` (presence) packet. The `{pres}` packet is sent either to the topic being affected or to the `me` topic. When user's `me` topic comes online (i.e. an authenticated session attaches to `me` topic), a `{pres}` packet is sent to `me` topics of all other users, who have peer to peer subscriptions with the first user. ## General Considerations Timestamps are always represented as [RFC 3339](http://tools.ietf.org/html/rfc3339)-formatted string with precision up to milliseconds and timezone always set to UTC, e.g. `"2015-10-06T18:07:29.841Z"`. Whenever base64 encoding is mentioned, it means base64 URL encoding with padding characters stripped, see [RFC 4648](http://tools.ietf.org/html/rfc4648). The `{data}` packets have server-issued sequential IDs: base-10 numbers starting at 1 and incrementing by one with every message. They are guaranteed to be unique per topic. In order to connect requests to responses, client may assign message IDs to all packets set to the server. These IDs are strings defined by the client. Client should make them unique at least per session. The client-assigned IDs are not interpreted by the server, they are returned to the client as is. ## Connecting to the Server There are three ways to access the server over the network: websocket, long polling, and [gRPC](https://grpc.io/). When the client establishes a connection to the server over HTTP(S), such as over a websocket or long polling, the server offers the following endpoints: * `/v0/channels` for websocket connections * `/v0/channels/lp` for long polling * `/v0/file/u` for file uploads * `/v0/file/s` for serving files (downloads) `v0` denotes API version (currently zero). Every HTTP(S) request must include the API key. The server checks for the API key in the following order: * HTTP header `X-Tinode-APIKey` * URL query parameter `apikey` (/v0/file/s/abcdefg.jpeg?apikey=...) * Form value `apikey` * Cookie `apikey` A default API key is included with every demo app for convenience. Generate your own key for production using [`keygen` utility](../keygen). Once the connection is opened, the client must issue a `{hi}` message to the server. Server responds with a `{ctrl}` message which indicates either success or an error. The `params` field of the response contains server's protocol version `"params":{"ver":"0.15"}` and may include other values. ### gRPC See definition of the gRPC API in the [proto file](../pbx/model.proto). gRPC API has slightly more functionality than the API described in this document: it allows the `root` user to send messages on behalf of other users as well as delete users. The `bytes` fields in protobuf messages expect JSON-encoded UTF-8 content. For example, a string should be quoted before being converted to bytes as UTF-8: `[]byte("\"some string\"")` (Go), `'"another string"'.encode('utf-8')` (Python 3). ### WebSocket Messages are sent in text frames, one message per frame. Binary frames are reserved for future use. By default server allows connections with any value in the `Origin` header. ### Long Polling Long polling works over `HTTP POST` (preferred) or `GET`. In response to client's very first request server sends a `{ctrl}` message containing `sid` (session ID) in `params`. Long polling client must include `sid` in every subsequent request either in the URL or in the request body. Server allows connections from all origins, i.e. `Access-Control-Allow-Origin: *` ### Out of Band Large Files Large files are sent out of band using `HTTP POST` as `Content-Type: multipart/form-data`. See [below](#out-of-band-handling-of-large-files) for details. ### Running Behind a Reverse Proxy Tinode server can be set up to run behind a reverse proxy, such as NGINX. For efficiency it can accept client connections from Unix sockets by setting `listen` and/or `grpc_listen` config parameters to the path of the Unix socket file, e.g. `unix:/run/tinode.sock`. The server may also be configured to read peer's IP address from `X-Forwarded-For` HTTP header by setting `use_x_forwarded_for` config parameter to `true`. ## Users User is meant to represent a person, an end-user: producer and consumer of messages. Users are generally assigned one of the two authentication levels: authenticated `auth` or anonymous `anon`. The third level `root` is only accessible over `gRPC` where it permits the `root` to send messages on behalf of other users. When a connection is first established, the client application can send either an `{acc}` or a `{login}` message which authenticates the user at one the levels. Each user is assigned a unique ID. The IDs are composed as `usr` followed by base64-encoded 64-bit numeric value, e.g. `usr2il9suCbuko`. Users also have the following properties: * `created`: timestamp when the user record was created * `updated`: timestamp of when user's `public` or `trusted` was last updated * `status`: state of the account * `username`: unique string used in `basic` authentication; username is not accessible to other users * `defacs`: object describing user's default access mode for peer to peer conversations with authenticated and anonymous users; see [Access control](#access-control) for details * `auth`: default access mode for authenticated `auth` users * `anon`: default access for anonymous `anon` users * `trusted`: an application-defined object issued by the system administration. Anyone can read it but only system administrators can change it. * `public`: an application-defined object that describes the user. Anyone can query user for `public` data. * `private`: an application-defined object that is unique to the current user and accessible only by the user. * `tags`: [discovery](#fnd-and-tags-finding-users-and-topics) and credentials. User's account has a state. The following states are defined: * `ok` (normal): the default state which means the account is not restricted in any way and can be used normally; * `susp` (suspended): the user is prevented from accessing the account as well as not found through [search](#fnd-and-tags-finding-users-and-topics); the state can be assigned by the administrator and fully reversible. * `del` (soft-deleted): user is marked as deleted but user's data is retained; un-deleting the user is not currenly supported. * `undef` (undefined): used internally by authenticators; should not be used elsewhere. A user may maintain multiple simultaneous connections (sessions) with the server. Each session is tagged with a client-provided `User Agent` string intended to differentiate client software. Logging out is not supported by design. If an application needs to change the user, it should open a new connection and authenticate it with the new user credentials. ### Authentication Authentication is conceptually similar to [SASL](https://en.wikipedia.org/wiki/Simple_Authentication_and_Security_Layer): it's provided as a set of adapters each implementing a different authentication method. Authenticators are used during account registration [`{acc}`](#acc) and during [`{login}`](#login). The server comes with the following authentication methods out of the box: * `token` provides authentication by a cryptographic token. * `basic` provides authentication by a login-password pair. * `anonymous` is designed for cases where users are temporary, such as handling customer support requests through chat. * `rest` is a [meta-method](../server/auth/rest/) which allows use of external authentication systems by means of JSON RPC. Any other authentication method can be implemented using adapters. The `token` is intended to be the primary means of authentication. Tokens are designed in such a way that token authentication is light weight. For instance, token authenticator generally does not make any database calls, all processing is done in-memory. All other authentication methods are intended to be used only to obtain or refresh the token. Once the token is obtained, subsequent logins should use it. The `basic` authentication scheme expects `secret` to be a base64-encoded string of a string composed of a user name followed by a colon `:` followed by a plan text password. User name in the `basic` scheme must not contain the colon character `:` (ASCII 0x3A). The `anonymous` scheme can be used to create accounts, it cannot be used for logging in: a user creates an account using `anonymous` scheme and obtains a cryptographic token which it uses for subsequent `token` logins. If the token is lost or expired, the user is no longer able to access the account. Compiled-in authenticator names may be changed by using `logical_names` configuration feature. For example, a custom `rest` authenticator may be exposed as `basic` instead of default one or `token` authenticator could be hidden from users. The feature is activated by providing an array of mappings in the config file: `logical_name:actual_name` to rename or `actual_name:` to hide. For instance, to use a `rest` service for basic authentication use `"logical_names": ["basic:rest"]`. #### Creating an Account When a new account is created, the user must inform the server which authentication method will be later used to gain access to this account as well as provide shared secret, if appropriate. Only `basic` and `anonymous` can be used during account creation. The `basic` requires the user to generate and send a unique login and password to the server. The `anonymous` does not exchange secrets. User may optionally set `{acc login=true}` to use the new account for immediate authentication. When `login=false` (or not set), the new account is created but the authentication status of the session which created the account remains unchanged. When `login=true` the server will attempt to authenticate the session with the new account, the `{ctrl}` response to the `{acc}` request will contain the authentication token on success. This is particularly important for the `anonymous` authentication because that's the only time when the authentication token can be retrieved. #### Logging in Logging in is performed by issuing a `{login}` request. Logging in is possible with `basic` and `token` only. Response to any login is a `{ctrl}` message with either a code 200 and a token which can be used in subsequent logins with `token` authentication, or a code 300 request for additional information, such as verifying credentials or responding to a method-dependent challenge in multi-step authentication, or a code 4xx error. Token has server-configured expiration time so it needs to be periodically refreshed. #### Changing Authentication Parameters User may change authentication parameters, such as changing login and password, by issuing an `{acc}` request. Only `basic` authentication currently supports changing parameters: ```js acc: { id: "1a2b3", // string, client-provided message id, optional user: "usr2il9suCbuko", // user being affected by the change, optional token: "XMg...g1Gp8+BO0=", // authentication token if the session // is not yet authenticated, optional. scheme: "basic", // authentication scheme being updated. secret: base64encode("new_username:new_password") // new parameters } ``` In order to change just the password, `username` should be left empty, i.e. `secret: base64encode(":new_password")`. If the session is not authenticated, the request must include a `token`. It can be a regular authentication token obtained during login, or a restricted token received through [Resetting a Password](#resetting-a-password) process. If the session is authenticated, the token must not be included. If the request is authenticated for access level `ROOT`, then the `user` may be set to a valid ID of another user. Otherwise it must be blank (defaulting to the current user) or equal to the ID of the current user. #### Resetting a Password, i.e. "Forgot Password" To reset login or password, (or any other authentication secret, if such action is supported by the authenticator), one sends a `{login}` message with the `scheme` set to `reset` and the `secret` containing a base64-encoded string "`authentication scheme to reset secret for`:`reset method`:`reset method value`". Most basic case of resetting a password by email is ```js login: { id: "1a2b3", scheme: "reset", secret: base64encode("basic:email:jdoe@example.com") } ``` where `jdoe@example.com` is an earlier validated user's email. If the email matches the registration, the server will send a message using specified method and address with instructions for resetting the secret. The email contains a restricted security token which the user can include into an `{acc}` request with the new secret as described in [Changing Authentication Parameters](#changing-authentication-parameters). ### Suspending a User User's account can be suspended by service administrator. Once the account is suspended, the user is no longer able to login and use the service. Only the `root` user may suspend the account. To suspend the account the root user sends the following message: ```js acc: { id: "1a2b3", // string, client-provided message id, optional user: "usr2il9suCbuko", // user being affected by the change status: "susp" } ``` Sending the same message with `status: "ok"` un-suspends the account. A root user may check account status by executing `{get what="desc"}` command against user's `me` topic. ### Credential Validation Server may be optionally configured to require validation of certain credentials associated with the user accounts and authentication scheme. For instance, it's possible to require user to provide a unique email or a phone number, or to solve a captcha as a condition of account registration. The server supports verification of email out of the box with just a configuration change. is mostly functional, verification of phone numbers is not functional because a commercial subscription is needed in order to be able to send text messages (SMS). If certain credentials are required, then user must maintain them in validated state at all times. It means if a required credential has to be changed, the user must first add and validate the new credential and only then remove the old one. Credentials are initially assigned at registration time by sending an `{acc}` message, added using `{set topic="me"}`, deleted using `{del topic="me"}`, and queries by `{get topic="me"}` messages. Credentials are verified by the client by sending either a `{login}` or an `{acc}` message. ### Access Control Access control manages user's access to topics through access control lists (ACLs). The access is assigned individually to each user-topic pair (subscription). Access control is mostly usable for group topics. Its usability for `me` and P2P topics is limited to managing presence notifications and banning uses from initiating or continuing P2P conversations. All channel readers are given the same permissions. User's access to a topic is defined by two sets of permissions: user's desired permissions "want", and permissions granted to user by topic's manager(s) "given". Each permission is represented by a bit in a bitmap. It can be either present or absent. The actual access is determined as a bitwise AND of wanted and given permissions. The permissions are communicated in messages as a set of ASCII characters, where presence of a character means a set permission bit: * No access: `N` is not a permission per se but an indicator that permissions are explicitly cleared/not set. It usually indicates that the default permissions should *not* be applied. * Join: `J`, permission to subscribe to a topic * Read: `R`, permission to receive `{data}` packets * Write: `W`, permission to `{pub}` to topic * Presence: `P`, permission to receive presence updates `{pres}` * Approve: `A`, permission to approve requests to join a topic, remove and ban members; a user with such permission is topic's administrator * Sharing: `S`, permission to invite other people to join the topic * Delete: `D`, permission to hard-delete messages; only owners can completely delete topics * Owner: `O`, user is the topic owner; the owner can assign any other permission to any topic member, change topic description, delete topic; topic may have a single owner only; some topics have no owner When a user subscribes to a topic or starts a chat with another user, the access permissions are either set explicitly or assigned by default `defacs`. Access permissions can be modified by sending `{set}` messages. A client may set explicit permissions in `{sub}` and `{set}` messages. If the permissions are missing or set to an empty string (not `N`!), Tinode will use default permissions `defacs` assigned earlir. If no default permissions are found, the authenticated users in group topics will receive a `JRWPS` access, in P2P topics will get `JRWPA`; anonymous users will receive `N` (no access) which means every subscription request must be explicitly approved by the topic manager. Default access is defined for two categories of users: authenticated and anonymous. The default access value is applied as a "given" permission to all new subscriptions. Topic's default access is established at the topic creation time by `{sub.desc.defacs}` and can be subsequently modified by the owner by sending `{set}` messages. Likewise, user's default access is established at the account creation time by `{acc.desc.defacs}` and can be modified by the user by sending a `{set}` message to `me` topic. ## Topics Topic is a named communication channel for one or more people. Topics have persistent properties. These topic properties can be queried by `{get what="desc"}` message. Topic properties independent of the user making the query: * `created`: timestamp of topic creation time * `updated`: timestamp of when topic's `trusted`, `public`, or `private` was last updated * `touched`: timestamp of the last message sent to the topic * `defacs`: object describing topic's default access mode for authenticated and anonymous users; see [Access control](#access-control) for details * `auth`: default access mode for authenticated users * `anon`: default access for anonymous users * `seq`: integer server-issued sequential ID of the latest `{data}` message sent through the topic * `trusted`: an application-defined object issued by the system administrators. Anyone can read it but only administrators can change it. * `public`: an application-defined object that describes the topic. Anyone who can subscribe to topic can receive topic's `public` data, only topic `owner` can change it. User-dependent topic properties: * `acs`: object describing given user's current access permissions; see [Access control](#access-control) for details * `want`: access permission requested by this user * `given`: access permissions given to this user * `private`: an application-defined object that is unique to the current user (topic subscriber). Topic usually have subscribers. One of the subscribers may be designated as topic owner (`O` access permission) with full access permissions. The list of subscribers can be queries with a `{get what="sub"}` message. The list of subscribers is returned in a `sub` section of a `{meta}` message. ### `me` Topic Topic `me` is automatically created for every user at the account creation time. It serves as means of managing account information, receiving presence notification from people and topics of interest. Topic `me` has no owner. The topic cannot be deleted or unsubscribed from. One can `leave` the topic which will stop all relevant communication and indicate that the user is offline (although the user may still be logged in and may continue to use other topics). Joining or leaving `me` generates a `{pres}` presence update sent to all users who have peer to peer topics with the given user and `P` permissions set. Topic `me` is read-only. `{pub}` messages to `me` are rejected. Message `{get what="desc"}` to `me` is automatically replied with a `{meta}` message containing `desc` section with the topic parameters (see intro to [Topics](#topics) section). The `public` parameters of `me` topic is data that the user wants to show to his/her connections. Changing it changes `public` not just for the `me` topic, but also everywhere where user's `public` is shown, such as `public` of all user's peer to peer topics. Message `{get what="sub"}` to `me` is different from any other topic as it returns the list of topics that the current user is subscribed to as opposite to the expected user's subscription to `me`. * seq: server-issued numeric id of the last message in the topic * recv: seq value self-reported by the current user as received * read: seq value self-reported by the current user as read * seen: for P2P subscriptions, timestamp of user's last presence and User Agent string are reported * when: timestamp when the user was last online * ua: user agent string of the user's client software last used Message `{get what="data"}` to `me` is rejected. Internally the `me` topics are not persisted separately from the users. The `me` topics don't exist in the `topics` table or collection, they are created in memory from the `users` database record. ### `slf` Topic Topic `slf` (self) provides mechanism for storing information, like bookmarks or saved messages. Messages sent to `slf` are accessible only by the user who sent them. This topic is created automatically when the user subscribes to it for the first time. ### `fnd` and Tags: Finding Users and Topics Topic `fnd` is automatically created for every user at the account creation time. It serves as an endpoint for discovering other users and group topics. Users and group topics can be discovered by `tags`. Tags are optionally assigned at the topic or user creation time then can be updated by using `{set what="tags"}` against a `me` or a group topic. A tag is an arbitrary case-insensitive Unicode string (forced to lowercase on the server) up to 96 characters long which may contain characters from `Letter` and `Number` Unicode [classes/categories](https://en.wikipedia.org/wiki/Unicode_character_property#General_Category) as well as any of the following ASCII characters: `_`, `.`, `+`, `-`, `@`, `#`, `!`, `?`. Tag may have a prefix which serves as a namespace. The prefix is a 2-16 character string which starts with a letter [a-z] and may contain lowercase ASCII letters and numbers followed by a colon `:`, ex. prefixed phone tag `tel:+14155551212` or prefixed email tag `email:alice@example.com`. Some prefixed tags are optionally enforced to be unique. In that case only one user or topic may have such a tag. Certain tags may be forced to be immutable to the user, i.e. user's attempts to add or remove an immutable tag will be rejected by the server. The tags are indexed server-side and used in user and topic discovery. Search returns users and topics sorted by the number of matched tags in descending order. In order to find users or topics, a user sets either `public` or `private` parameter of the `fnd` topic to a search query (see [Query language](#query-language)) then issues a `{get topic="fnd" what="sub"}` request. If both `public` and `private` are set, the `public` query is used. The `private` query is persisted across sessions and devices, i.e. all user's sessions see the same `private` query. The value of the `public` query is ephemeral, i.e. it's not saved to database and not shared between user's sessions. The `private` query is intended for large queries which do not change often, such as finding matches for everyone in user's contact list on a mobile phone. The `public` query is intended to be short and specific, such as finding some topic or a user who is not in the contact list. The system responds with a `{meta}` message with the `sub` section listing details of the found users or topics formatted as subscriptions. Topic `fnd` is read-only. `{pub}` messages to `fnd` are rejected. _CURRENTLY UNSUPPORTED_ When a new user registers with tags matching the given query, the `fnd` topic will receive `{pres}` notification for the new user. [Plugins](../pbx) support `Find` service which can be used to replace default search with a custom one. Internally the `fnd` topics are not persisted separately from the users. The `fnd` topics don't exist in the `topics` table or collection, they are created in memory from the `users` database record. #### Query Language Tinode query language is used to define search queries for finding users and topics. The query is a string containing atomic terms separated by spaces or commas. The individual query terms are matched against user's or topic's tags. The individual terms may be written in an RTL language but the query as a whole is parsed left to right. Spaces are treated as the `AND` operator, commas (as well as commas preceded and/or followed by a space) as the `OR` operator. The order of operators is ignored: all `AND` tags are grouped together, all `OR` tags are grouped together. `OR` takes precedence over `AND`: if a tag is preceded of followed by a comma, it's an `OR` tag, otherwise an `AND`. For example, `aaa bbb, ccc` (`aaa AND bbb OR ccc`) is interpreted as `(bbb OR ccc) AND aaa`. Query terms containing spaces must convert spaces to underscores ` ` -> `_`, e.g. `new york` -> `new_york`. **Some examples:** * `flowers`: find topics or users which contain tag `flowers`. * `flowers travel`: find topics or users which contain both tags `flowers` and `travel`. * `flowers, travel`: find topics or users which contain either tag `flowers` or `travel` (or both). * `flowers travel, puppies`: find topics or users which contain `flowers` and either `travel` or `puppies`, i.e. `(travel OR puppies) AND flowers`. * `flowers, travel puppies, kittens`: find topics or users which contain either one of `flowers`, `travel`, `puppies`, or `kittens`, i.e. `flowers OR travel OR puppies OR kittens`. The space between `travel` and `puppies` is treated as `OR` due to `OR` taking precedence over `AND`. #### Incremental Updates to Queries _CURRENTLY UNSUPPORTED_ Queries, particularly `fnd.private` could be arbitrarily large, limited only by the limits on the message size, and by the limits on the query size in the underlying database. Instead of rewriting the entire query to add or remove a term, terms can be added or removed incrementally. The incremental update request is processed left to right. It may contain the same term multiple times, i.e. `-a_tag+a_tag` is a valid request. #### Query Rewrite Finding users by login, phone or email requires query terms to be written with prefixes, i.e. `email:alice@example.com` instead of `alice@example.com`. This may present a problem to end users because it requires them to learn the query language. Tinode solves this problem by implementing _query rewrite_ on the server: if query term (tag) does not contain a prefix, server rewrites it by adding the appropriate prefix. In queries to `fnd.public` the original term is also kept (query `alice@example.com` is rewritten as `email:alice@example.com OR alice@example.com`), in queries to `fnd.private` only the rewritten term is kept (`alice@example.com` is rewritten as `email:alice@example.com`). All terms that look like email, for instance, `alice@example.com` are rewritten to `email:alice@example.com OR alice@example.com`. Terms which look like phone numbers are converted to [E.164](https://en.wikipedia.org/wiki/E.164) and also rewritten as `tel:+14155551212 OR +14155551212`. In addition, in queries to `fnd.public` all other unprefixed terms which look like logins are rewritten as logins: `alice` -> `basic:alice OR alice`. As described above, tags which look like phone numbers are converted to E.164 format. Such conversion requires an ISO 3166-1 alpha-2 country code. The following logic is used when converting phone number tags to E.164: * If the tag already contains a country calling code, it's used as is: `+1(415)555-1212` -> `+14155551212`. * If the tag has no prefix, country code is taken from the locale value set by the client in `lang` field of the `{hi}` message. * If client has not provided the code in the `hi.lang`, the country code is taken from `default_country_code` field of the `tinode.conf`. * If no `default_country_code` is set in `tinode.conf`, `US` country code is used. #### Possible Use Cases * Restricting users to organisations. An immutable tag(s) may be assigned to the user which denotes the organisation the user belongs to. When the user searches for other users or topics, the search can be restricted to always contain the tag. This approach can be used to segment users into organisations with limited visibility into each other. * Search by geographical location. Client software may periodically assign a [geohash](https://en.wikipedia.org/wiki/Geohash) tag to the user based on current location. Searching for users in a given area would mean matching on geohash tags. * Search by numerical range, such as age range. The approach is similar to geohashing. The entire range of numbers is covered by the smallest possible power of 2, for instance the range of human ages is covered by 27=128 years. The entire range is split in two halves: the range 0-63 is denoted by 0, 64-127 by 1. The operation is repeated with each subrange, i.e. 0-31 is 00, 32-63 is 01, 0-15 is 000, 32-47 is 010. Once completed, the age 30 will belong to the following ranges: 0 (0-63), 00 (0-31), 001 (16-31), 0011 (24-31), 00111 (28-31), 001111 (30-31), 0011110 (30). A 30 y.o. user is assigned a few tags to indicate the age, i.e. `age:00111`, `age:001111`, and `age:0011110`. Technically, all 7 tags may be assigned but usually it's impractical. To query for anyone in the age range 28-35 convert the range into a minimal number of tags: `age:00111` (28-31), `age:01000` (32-35). This query will match the 30 y.o. user by tag `age:00111`. ### Peer to Peer Topics Peer to peer (P2P) topics represent communication channels between strictly two users. The name of the topic is different for each of the two participants. Each of them sees the name of the topic as the user ID of the other participant: `usr` followed by base64 URL-encoded ID of the user. For example, if two users `usrOj0B3-gSBSs` and `usrIU_LOVwRNsc` start a P2P topic, the first one will see it as `usrIU_LOVwRNsc`, the second as `usrOj0B3-gSBSs`. The P2P topic has no owner. A P2P topic is created by one user subscribing to topic with the name equal to the ID of the other user. For instance, user `usrOj0B3-gSBSs` can establish a P2P topic with user `usrIU_LOVwRNsc` by sending a `{sub topic="usrIU_LOVwRNsc"}`. Tinode will respond with a `{ctrl}` packet with the name of the newly created topic as described above. The other user will receive a `{pres}` message on `me` topic with updated access permissions. Internally, P2P topics are stored as `p2p` followed by base64 URL encoded concatenation of two 64-bit user IDs, with the lower numeric value ID first: `p2pm7PvMGmdcx_uVkDRaSTbwA`. The 'public' parameter of P2P topics is user-dependent. For instance a P2P topic between users A and B would show user A's 'public' to user B and vice versa. If a user updates 'public', all user's P2P topics will automatically update 'public' too. The 'private' parameter of a P2P topic is defined by each participant individually as with any other topic type. ### Group Topics Group topic represents a communication channel between multiple users. The name of a group topic is `grp` or `chn` followed by a string of characters from base64 URL-encoding set. No other assumptions can be made about internal structure or length of the group name. Group topics support limited number of subscribers (controlled by a `max_subscriber_count` parameter in configuration file) with access permissions of each subscriber managed individually. Group topics may also be enabled to support any number of read-only users - `readers`. All `readers` have the same access permissions. Group topics with enabled `readers` are called `channels`. A group topic is created by sending a `{sub}` message with the topic field set to string `new` or `nch` optionally followed by any characters, e.g. `new` or `newAbC123` are equivalent. Tinode will respond with a `{ctrl}` message with the name of the newly created topic, i.e. `{sub topic="new"}` is replied with `{ctrl topic="grpmiKBkQVXnm3P"}`. If topic creation fails, the error is reported on the original topic name, i.e. `new` or `newAbC123`. The user who created the topic becomes topic owner. Ownership can be transferred to another user with a `{set}` message but one user must remain the owner at all times. A `channel` topic is different from the non-channel group topic in the following ways: * Channel topic is created by sending `{sub topic="nch"}`. Sending `{sub topic="new"}` will create a group topic without enabling channel functionality. * Sending `{sub topic="chnAbC123"}` will create a `reader` subscription to a channel. A non-channel topic will reject such subscription request. * When searching for topics using [`fnd`](#fnd-and-tags-finding-users-and-topics), channels will show addresses with `chn` prefixes, non-channel topic will show with `grp` prefixes. * Messages received by readers on channels have no `From` field. Normal subscribers will receive messages with `From` containing ID of the sender. * Default permissions for a channel and non-channel group topics are different: channel group topic grants no permissions at all. * A subscriber joining or leaving the topic (regular or channel-enabled) generates a `{pres}` message to all other subscribers who are currently in the joined state with the topic and have appropriate permissions. Reader joining or leaving the channel generates no `{pres}` message. ### `sys` Topic The `sys` topic serves as an always available channel of communication with the system administrators. A normal non-root user cannot subscribe to `sys` but can publish to it without subscription. Existing clients use this channel to report abuse by sending a Drafty-formatted `{pub}` message with the report as JSON attachment. A root user can subscribe to `sys` topic. Once subscribed, the root user will receive messages sent to `sys` topic by other users. ## Using Server-Issued Message IDs Tinode provides basic support for client-side caching of `{data}` messages in the form of server-issued sequential message IDs. The client may request the last message id from the topic by issuing a `{get what="desc"}` message. If the returned ID is greater than the ID of the latest received message, the client knows that the topic has unread messages and their count. The client may fetch these messages using `{get what="data"}` message. The client may also paginate history retrieval by using message IDs. ## User Agent and Presence Notifications A user is reported as being online when one or more of user's sessions are attached to the `me` topic. Client-side software identifies itself to the server using `ua` (user agent) field of the `{login}` message. The _user agent_ is published in `{meta}` and `{pres}` messages in the following way: * When user's first session attaches to `me`, the _user agent_ from that session is broadcast in the `{pres what="on" ua="..."}` message. * When multiple user sessions are attached to `me`, the _user agent_ of the session where the most recent action has happened is reported in `{pres what="ua" ua="..."}`; the 'action' in this context means any message sent by the client. To avoid potentially excessive traffic, user agent changes are broadcast no more frequently than once a minute. * When user's last session detaches from `me`, the _user agent_ from that session is recorded together with the timestamp; the user agent is broadcast in the `{pres what="off" ua="..."}` message and subsequently reported as the last online timestamp and user agent. An empty `ua=""` _user agent_ is not reported. I.e. if user attaches to `me` with non-empty _user agent_ then does so with an empty one, the change is not reported. An empty _user agent_ may be disallowed in the future. ## Trusted, Public, Private, Auxiliary Fields Topics have `trusted`, `public`, `aux` fields, subscriptions have `private` fields. The primary difference between these fields is in access control: * `trusted`: writable by `ROOT` users, readable by anyone. * `public`: writable by the `owner` or the user, readable by anyone. * `aux`: writable by topic administrators, readable by subscribers. * `private`: readable and writable only by the user who created the subscription. Generally, the fields are application-defined. The server does not enforce any particular structure of these fields except for `fnd` topic. At the same time, client software should use the same format for interoperability reasons. The following sections describe the format of these fields as they are implemented by all official clients. Although it's not yet enforced, if a third-party application defines custom keys, the key names should start with an `x-` followed by the application's fully qualified domain name, e.g. `x-example.com-value: "abc"`. The fields should contain primitive types only, i.e. `string`, `boolean`, `number`, or `null`. ### Trusted The format of the optional `trusted` field in group and peer to peer topics is a set of key-value pairs; `fnd` and `sys` topics do not have the `trusted`. The field is writable by `ROOT` users, readable by anyone who has access to the topic or user. The following optional keys are currently defined: ```js trusted: { verified: true, // boolean, an indicator of a verified/trustworthy user or topic. staff: true, // boolean, an indicator that the user or topic // is a part of/belongs to the server administration. danger: true // boolean, an indicator that the user or topic are untrustworthy. } ``` ### Public The format of the `public` field in group, peer to peer, systems topics is expected to be [theCard](./thecard.md). The field is writable by by the user for users, the topic owner for topics. The field is readable by anyone who has access to topic or user. The `fnd` topic expects `public` to be a string representing a [search query](#query-language)). ### Private The format of the `private` field in group and peer to peer topics is a set of key-value pairs. The field is writable and readable by the user only. The following keys are currently defined: ```js private: { comment: "some comment", // string, optional user comment about a topic or a peer user arch: true, // boolean, indicator that the topic is archived by the user, i.e. // should not be shown in the UI with other non-archived topics. accepted: "JRWS", // string, 'given' mode accepted by the user. tpins: ["grpmiKBkQVXnm3P", "usrIU_LOVwRNsc"] // array of topic IDs to pin to the top of // the contacts list; 'me' topic only. } ``` The `fnd` topic expects `private` to be a string representing a [search query](#query-language)). ### Auxiliary The format of the `aux` field is a set of key-value pairs. The `aux` is writable by topic admins and readable by all topic subscribers. The following keys are currently defined: ```js aux: { pins: [1001, 23456] // array of integer message IDs to pin to the top of the message list. } ``` ## Format of Content Format of `content` field in `{pub}` and `{data}` is application-defined and as such the server does not enforce any particular structure of the field. At the same time, client software should use the same format for interoperability reasons. Currently the following two types of `content` are supported: * Plain text * [Drafty](./drafty.md) If Drafty is used, a message header `"head": {"mime": "text/x-drafty"}` must be set. ## Out-of-Band Handling of Large Files Large files create problems when sent in-band for multiple reasons: * limits on database storage as in-band messages are stored in database fields * in-band messages must be downloaded completely as a part of downloading chat history Tinode provides two endpoints for handling large files: `/v0/file/u` for uploading files and `v0/file/s` for downloading. The endpoints require the client to provide both [API key](#connecting-to-the-server) and login credentials. The server checks credentials in the following order: **Login credentials** * HTTP header `Authorization` (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Authorization) * URL query parameters `auth` and `secret` (/v0/file/s/abcdefg.jpeg?auth=...&secret=...) * Form values `auth` and `secret` * Cookies `auth` and `secret` ### Uploading To upload a file first create an RFC 2388 multipart request then send it to the server using HTTP POST. The server responds to the request either with a `307 Temporary Redirect` with the new upload URL, or with a `200 OK` and a `{ctrl}` message in response body: ```js ctrl: { params: { url: "/v0/file/s/mfHLxDWFhfU.pdf" }, code: 200, text: "ok", ts: "2018-07-06T18:47:51.265Z" } ``` If `307 Temporary Redirect` is returned, the client must retry the upload at the provided URL. The URL returned in `307` response should be used for just this one upload. All subsequent uploads should try the default URL first. The `ctrl.params.url` contains the path to the uploaded file at the current server. It could be either the full path like `/v0/file/s/mfHLxDWFhfU.pdf`, a relative path like `./mfHLxDWFhfU.pdf`, or just the file name `mfHLxDWFhfU.pdf`. Anything but the full path is interpreted against the default *download* endpoint `/v0/file/s/`. For instance, if `mfHLxDWFhfU.pdf` is returned then the file is located at `http(s)://current-tinode-server/v0/file/s/mfHLxDWFhfU.pdf`. Once the URL of the file is received, either immediately or after following the redirect, the client may use the URL to send a `{pub}` message with the uploaded file as an attachment, or, if the file is an image, as an avatar image for a topic or user profile (see [theCard](./thecard.md)). For example, the URL can be used in a [Drafty](./drafty.md)-formatted `pub.content` field: ```js { pub: { id: "121103", topic: "grpnG99YhENiQU", head: { mime: "text/x-drafty" }, content: { ent: [ { data: { mime: "image/jpeg", name: "roses-are-red.jpg", ref: "/v0/file/s/sJOD_tZDPz0.jpg", size: 437265 }, tp: "EX" } ], fmt: [ { at: -1, key:0, len:1 } ] } }, extra: { attachments: ["/v0/file/s/sJOD_tZDPz0.jpg"] } } ``` It's important to list the used URLs in the `extra: attachments[...]` field. Tinode server uses this field to maintain the uploaded file's use counter. Once the counter drops to zero for the given file (for instance, because a message with the shared URL was deleted or because the client failed to include the URL in the `extra.attachments` field), the server will garbage collect the file. Only relative URLs should be used. Absolute URLs in the `extra.attachments` field are ignored. The URL value is expected to be the `ctrl.params.url` returned in response to upload. ### Downloading The serving endpoint `/v0/file/s` serves files in response to HTTP GET requests. The client must evaluate relative URLs against this endpoint, i.e. if it receives a URL `mfHLxDWFhfU.pdf` or `./mfHLxDWFhfU.pdf` it should interpret it as a path `/v0/file/s/mfHLxDWFhfU.pdf` at the current Tinode HTTP server. _Important!_ As a security measure, the client should not send security credentials if the download URL is absolute and leads to another server. ## Push Notifications Tinode uses compile-time adapters for handling push notifications. The server comes with [Tinode Push Gateway](../server/push/tnpg/), [Google FCM](https://firebase.google.com/docs/cloud-messaging/), and `stdout` adapters. Tinode Push Gateway and Google FCM support Android with [Play Services](https://developers.google.com/android/guides/overview) (may not be supported by some Chinese phones), iOS devices and all major web browsers excluding Safari. The `stdout` adapter does not actually send push notifications. It's mostly useful for debugging, testing and logging. Other types of push notifications such as [TPNS](https://intl.cloud.tencent.com/product/tpns) can be handled by writing appropriate adapters. If you are writing a custom plugin, the notification payload is the following: ```js { topic: "grpnG99YhENiQU", // Topic which received the message. xfrom: "usr2il9suCbuko", // ID of the user who sent the message. ts: "2019-01-06T18:07:30.038Z", // message timestamp in RFC3339 format. seq: "1234", // sequential ID of the message (integer value sent as text). mime: "text/x-drafty", // optional message MIME-Type. content: "Lorem ipsum dolor sit amet, consectetur adipisci", // The first 80 characters of the message content as plain text. } ``` ### Tinode Push Gateway Tinode Push Gateway (TNPG) is a proprietary Tinode service which sends push notifications on behalf of Tinode. Internally it uses Google FCM and as such supports the same platforms as FCM. The main advantage of using TNPG over FCM is simplicity of configuration: mobile clients do not need to be recompiled, all is needed is a [configuration update](../server/push/tnpg/) on a server. ### Google FCM [Google FCM](https://firebase.google.com/docs/cloud-messaging/) supports Android with [Play Services](https://developers.google.com/android/guides/overview), iPhone and iPad devices, and all major web browsers excluding Safari. In order to use FCM mobile clients (iOS, Android) must be recompiled with credentials obtained from Google. See [instructions](../server/push/fcm/) for details. ### Stdout The `stdout` adapter is mostly useful for debugging and logging. It writes push payload to `STDOUT` where it can be redirected to file or read by some other process. ## Video Calls [See separate document](call-establishment.md). ## Link Previews Tinode provides an optional service which helps client applications generate link (URL) previews for inclusion into messages. The enpoint of this service (if enabled) is located at `/v0/urlpreview`. The service takes a single parameter `url`: ``` /v0/urlpreview?url=https%3A%2F%2Ftinode.co ``` The first several kilobytes of the document at the given URL is fetched by issuing an HTTP(S) GET request. If the returned document has content-type `text/html`, the HTML is parsed for page title, description, and image URL. The result is formatted as JSON and returned as ```json {"title": "Page title", "description": "This is a page description", "image_url": "https://tinode.co/img/logo64x64.png"} ``` The link preview service requires authentication. It's exactly the same as authentication for [Out of Band Large Files](#out-of-band-handling-of-large-files). ## Messages A message is a logically associated set of data. Messages are passed as JSON-formatted UTF-8 text. All client to server messages may have an optional `id` field. It's set by the client as means to receive an acknowledgement from the server that the message was received and processed. The `id` is expected to be a session-unique string but it can be any string. The server does not attempt to interpret it other than to check JSON validity. The `id` is returned unchanged by the server when it replies to the client message. Server requires strictly valid JSON, including double quotes around field names. For brevity the notation below omits double quotes around field names as well as outer curly brackets. Examples use `//` comments only for expressiveness. The comments cannot be used in actual communication with the server. For messages that update application-defined data, such as `{set}` `private` or `public` fields, when server-side data needs to be cleared, use a string with a single Unicode DEL character "␡" (`\u2421`). I.e. sending `"public": null` will not clear the field, but sending `"public": "␡"` will. Any unrecognized fields are silently ignored by the server. ### Client to Server Messages Every client to server message contains the main payload described in the sections below and an optional top-level field `extra`: ```js { abc: { ... }, // Main payload, see sections below. extra: { attachments: ["/v0/file/s/sJOD_tZDPz0.jpg"], // Array of out-of-band attachments which have to be exempted from GC. obo: "usr2il9suCbuko", // Alternative user ID set by the root user (obo = On Behalf Of). authlevel: "auth" // Altered authentication level set by the root user. } } ``` The `attachments` array lists URLs of files uploaded out of band. Such listing increments use counter of these files. Once the use counter drops to 0, the files will be automatically deleted. The `obo` (On Behalf Of) can be set by the `root` user. If the `obo` is set, the server will treat the message as if it came from the specified user as opposite to the actual sender. The `authlevel` is supplementary to the `obo` and permits setting custom authentication level for the user. A an `"auth"` level is used if the field is unset. #### `{hi}` Handshake message client uses to inform the server of its version and user agent. This message must be the first that the client sends to the server. Server responds with a `{ctrl}` which contains server build `build`, wire protocol version `ver`, session ID `sid` in case of long polling, as well as server constraints, all in `ctrl.params`. ```js hi: { id: "1a2b3", // string, client-provided message id, optional ver: "0.15.8-rc2", // string, version of the wire protocol supported by the client, required ua: "JS/1.0 (Windows 10)", // string, user agent identifying client software, // optional dev: "L1iC2...dNtk2", // string, unique value which identifies this specific // connected device for the purpose of push notifications; not // interpreted by the server. // see [Push notifications support](#push-notifications-support); optional platf: "android", // string, underlying OS for the purpose of push notifications, one of // "android", "ios", "web"; if missing, the server will try its best to // detect the platform from the user agent string; optional lang: "en-US" // human language of the client device; optional } ``` The user agent `ua` is expected to follow [RFC 7231 section 5.5.3](http://tools.ietf.org/html/rfc7231#section-5.5.3) recommendation but the format is not enforced. The message can be sent more than once to update `ua`, `dev` and `lang` values. If sent more than once, the `ver` field of the second and subsequent messages must be either unchanged or not set. #### `{acc}` Message `{acc}` creates users or updates `tags` or authentication credentials `scheme` and `secret` of exiting users. To create a new user set `user` to the string `new` optionally followed by any character sequence, e.g. `newr15gsr`. Either authenticated or anonymous session can send an `{acc}` message to create a new user. To update authentication data or validate a credential of the current user leave `user` unset. The `{acc}` message **cannot** be used to modify `desc` or `cred` of an existing user. Update user's `me` topic instead. ```js acc: { id: "1a2b3", // string, client-provided message id, optional user: "newABC123", // string, "new" optionally followed by any characters to create a new user, // default: current user, optional token: "XMgS...8+BO0=", // string, authentication token to use for the request if the // session is not authenticated, optional // Temporary authentication parameters for one-off actions, like password reset. tmpscheme: "code", // name of the temp wuth scheme tmpsecret: "XMgS...8+BO0=", // temp auth secret status: "ok", // change user's status; no default value, optional. authlevel: "auth", // authentication level of the user when UserID is set and not equal // to the current user; Either "", "auth" or "anon"; default: "" scheme: "basic", // authentication scheme for this account, required; // "basic" and "anon" are currently supported for account creation. secret: base64encode("username:password"), // string, base64 encoded secret for the chosen // authentication scheme; to delete a scheme use a string with a single DEL // Unicode character "\u2421"; "token" and "basic" cannot be deleted login: true, // boolean, use the newly created account to authenticate current session, // i.e. create account and immediately use it to login. tags: ["alice johnson",... ], // array of tags for user discovery; see 'fnd' topic for // details, optional (if missing, user will not be discoverable other than // by login) cred: [ // account credentials which require verification, such as email or phone number. { meth: "email", // string, verification method, e.g. "email", "tel", "recaptcha", etc. val: "alice@example.com", // string, credential to verify such as email or phone resp: "178307", // string, verification response, optional params: { ... } // parameters, specific to the verification method, optional }, ... ], desc: { // object, user initialisation data closely matching that of table // initialisation; used only when creating an account; optional defacs: { auth: "JRWS", // string, default access mode for peer to peer conversations // between this user and other authenticated users anon: "N" // string, default access mode for peer to peer conversations // between this user and anonymous (un-authenticated) users }, // Default access mode for user's peer to peer topics public: { ... }, // application-defined payload to describe user, // available to everyone private: { ... } // private application-defined payload available only to user // through 'me' topic } } ``` Server responds with a `{ctrl}` message with `params` containing details of the new user account such as user ID and, in case of `login: true`, authentication token. If `desc.defacs` is missing, the server will assign server-default access permissions to new account. The only supported authentication schemes for account creation are `basic` and `anonymous`. #### `{login}` Login is used to authenticate the current session. ```js login: { id: "1a2b3", // string, client-provided message id, optional scheme: "basic", // string, authentication scheme; "basic", // "token", and "reset" are currently supported secret: base64encode("username:password"), // string, base64-encoded secret for the chosen // authentication scheme, required cred: [ { meth: "email", // string, verification method, e.g. "email", "tel", "captcha", etc, required resp: "178307" // string, verification response, required }, ... ], // response to a request for credential verification, optional } ``` Server responds to a `{login}` packet with a `{ctrl}` message. The `params` of the message contains the id of the logged in user as `user`. The `token` contains an encrypted string which can be used for authentication. Expiration time of the token is passed as `expires`. #### `{sub}` The `{sub}` packet serves the following functions: * creating a new topic * subscribing user to an existing topic * attaching session to a previously subscribed topic * fetching topic data User creates a new group topic by sending `{sub}` packet with the `topic` field set to `new12321` (regular topic) or `nch12321` (channel) where `12321` denotes any string including an empty string. Server will create a topic and respond back to the session with the name of the newly created topic. User creates a new peer to peer topic by sending `{sub}` packet with `topic` set to peer's user ID. The user is always subscribed to and the session is attached to the newly created topic. If the user had no relationship with the topic, sending `{sub}` packet creates it. Subscribing means to establish a relationship between session's user and the topic where no relationship existed in the past. Joining (attaching to) a topic means for the session to start consuming content from the topic. Server automatically differentiates between subscribing and joining/attaching based on context: if the user had no prior relationship with the topic, the server subscribes the user then attaches the current session to the topic. If relationship existed, the server only attaches the session to the topic. When subscribing, the server checks user's access permissions against topic's access control list. It may grant immediate access, deny access, may generate a request for approval from topic managers. Server replies to the `{sub}` with a `{ctrl}`. The `{sub}` message may include a `get` and `set` fields which mirror `{get}` and `{set}` messages. If included, server will treat them as a subsequent `{set}` and `{get}` messages on the same topic. If the `get` is set, the reply may include `{meta}` and `{data}` messages. ```js sub: { id: "1a2b3", // string, client-provided message id, optional topic: "me", // topic to be subscribed or attached to bkg: true, // request to attach to topic is issued by an automated agent, server should delay sending // presence notifications because the agent is expected to disconnect very quickly // Object with topic initialisation data, new topics & new // subscriptions only, mirrors {set} message set: { // New topic parameters, mirrors {set desc} desc: { defacs: { auth: "JRWS", // string, default access for new authenticated subscribers anon: "N" // string, default access for new anonymous (un-authenticated) // subscribers }, // Default access mode for the new topic trusted: { ... }, // application-defined payload assigned by the system administration public: { ... }, // application-defined payload to describe topic private: { ... } // per-user private application-defined content }, // object, optional // Subscription parameters, mirrors {set sub}. 'sub.user' must be blank sub: { mode: "JRWS", // string, requested access mode, optional; // default: server-defined }, // object, optional tags: [ // array of strings, update to tags (see fnd topic description), optional. "email:alice@example.com", "tel:1234567890" ], cred: { // update to credentials, optional. meth: "email", // string, verification method, e.g. "email", "tel", "recaptcha", etc. val: "alice@example.com", // string, credential to verify such as email or phone resp: "178307", // string, verification response, optional params: { ... } // parameters, specific to the verification method, optional }, aux: { ... } // update auxiliary data. }, get: { // Metadata to request from the topic; space-separated list, valid strings // are "desc", "sub", "data", "tags"; default: request nothing; unknown strings are // ignored; see {get what} for details what: "desc sub data", // string, optional // Optional parameters for {get what="desc"} desc: { ims: "2015-10-06T18:07:30.038Z" // timestamp, "if modified since" - return // public and private values only if at least one of them has been // updated after the stated timestamp, optional }, // Optional parameters for {get what="sub"} sub: { ims: "2015-10-06T18:07:30.038Z", // timestamp, "if modified since" - return // only those subscriptions which have been modified after the stated // timestamp, optional user: "usr2il9suCbuko", // string, return results for a single user, // any topic other than 'me', optional topic: "usr2il9suCbuko", // string, return results for a single topic, // 'me' topic only, optional limit: 20 // integer, limit the number of returned objects }, // Optional parameters for {get what="data"}, see {get what="data"} for details data: { since: 123, // integer, load messages with server-issued IDs greater or equal // to this (inclusive/closed), optional before: 321, // integer, load messages with server-issued sequential IDs less // than this (exclusive/open), optional limit: 20, // integer, limit the number of returned objects, // default: 32, optional } // object, optional } } ``` See [Trusted, Public, and Private Fields](#trusted-public-and-private-fields) for `trusted`, `private`, and `public` format considerations. #### `{leave}` This is a counterpart to `{sub}` message. It also serves two functions: * leaving the topic without unsubscribing (`unsub=false`) * unsubscribing (`unsub=true`) Server responds to `{leave}` with a `{ctrl}` packet. Leaving without unsubscribing affects just the current session. Leaving with unsubscribing will affect all user's sessions. ```js leave: { id: "1a2b3", // string, client-provided message id, optional topic: "grp1XUtEhjv6HND", // string, topic to leave, unsubscribe, or // delete, required unsub: true // boolean, leave and unsubscribe, optional, default: false } ``` #### `{pub}` The message is used to distribute content to topic subscribers. ```js pub: { id: "1a2b3", // string, client-provided message id, optional topic: "grp1XUtEhjv6HND", // string, topic to publish to, required noecho: false, // boolean, suppress echo (see below), optional head: { key: "value", ... }, // set of string key-value pairs, optional content: { ... } // object, application-defined content to publish // to topic subscribers, required } ``` Topic subscribers receive the `content` in the [`{data}`](#data) message. By default the originating session gets a copy of `{data}` like any other session currently attached to the topic. If for some reason the originating session does not want to receive the copy of the data it just published, set `noecho` to `true`. See [Format of Content](#format-of-content) for `content` format considerations. The following values are currently defined for the `head` field: * `attachments`: an array of paths indicating media attached to this message `["/v0/file/s/sJOD_tZDPz0.jpg"]`. * `auto`: `true` when the message was sent automatically, i.e. by a chatbot or an auto-responder. * `forwarded`: an indicator that the message is a forwarded message, a unique ID of the original message, `"grp1XUtEhjv6HND:123"`. * `mentions`: an array of user IDs mentioned (`@alice`) in the message: `["usr1XUtEhjv6HND", "usr2il9suCbuko"]`. * `mime`: MIME-type of the message content, `"text/x-drafty"`; a `null` or a missing value is interpreted as `"text/plain"`. * `replace`: an indicator that the message is a correction/replacement for another message, a topic-unique ID of the message being updated/replaced, `":123"` * `reply`: an indicator that the message is a reply to another message, a unique ID of the original message, `"grp1XUtEhjv6HND:123"`. * `sender`: a user ID of the sender added by the server when the message is sent on behalf of another user, `"usr1XUtEhjv6HND"`. * `thread`: an indicator that the message is a part of a conversation thread, a topic-unique ID of the first message in the thread, `":123"`; `thread` is intended for tagging a flat list of messages as opposite to creating a tree. * `webrtc`: a string representing the state of the video call the message represents. Possible values: * `"started"`: call has been initiated and being established * `"accepted"`: call has been accepted and established * `"finished"`: previously successfully established call has been ended * `"missed"`: call timed out before getting established * `"declined"`: call was hung up by the callee before getting established * `"busy"`: the call was declined due to the callee being in another call. * `"disconnected"`: call was terminated by the server for other reasons (e.g. due to an error) * `webrtc-duration`: a number representing a video call duration (in milliseconds). Application-specific fields should start with an `x--`. Although the server does not enforce this rule yet, it may start doing so in the future. The unique message ID should be formed as `:` whenever possible, such as `"grp1XUtEhjv6HND:123"`. If the topic is omitted, i.e. `":123"`, it's assumed to be the current topic. #### `{get}` Query topic for metadata, such as description or a list of subscribers, or query message history. The requester must be [subscribed and attached](#sub) to the topic to receive the full response. Some limited `desc` and `sub` information is available without being attached. ```js get: { id: "1a2b3", // string, client-provided message id, optional topic: "grp1XUtEhjv6HND", // string, name of topic to request data from what: "sub desc data del cred", // string, space-separated list of parameters to query; // unknown values are ignored; required // Optional parameters for {get what="desc"} desc: { ims: "2015-10-06T18:07:30.038Z" // timestamp, "if modified since" - return // public and private values only if at least one of them has been // updated after the stated timestamp, optional }, // Optional parameters for {get what="sub"} sub: { ims: "2015-10-06T18:07:30.038Z", // timestamp, "if modified since" - return // public and private values only if at least one of them has been // updated after the stated timestamp, optional user: "usr2il9suCbuko", // string, return results for a single user, // any topic other than 'me', optional topic: "usr2il9suCbuko", // string, return results for a single topic, // 'me' topic only, optional limit: 20 // integer, limit the number of returned objects }, // Optional parameters for {get what="data"} data: { since: 123, // integer, load messages with server-issued IDs greater or equal // to this (inclusive/closed), optional before: 321, // integer, load messages with server-issed sequential IDs less // than this (exclusive/open), optional limit: 20, // integer, limit the number of returned objects, default: 32, // optional }, // Optional parameters for {get what="del"} del: { since: 5, // integer, load deleted ranges with the delete transaction IDs greater // or equal to this (inclusive/closed), optional before: 12, // integer, load deleted ranges with the delete transaction IDs less // than this (exclusive/open), optional limit: 25, // integer, limit the number of returned objects, default: 32, // optional } } ``` * `{get what="desc"}` Query topic description. Server responds with a `{meta}` message containing requested data. See `{meta}` for details. If `ims` is specified and data has not been updated, the message will skip `trusted`, `public`, and `private` fields. Limited information is available without [attaching](#sub) to topic first. See [Trusted, Public, and Private Fields](#trusted-public-and-private-fields) for `trusted`, `private`, and `public` format considerations. * `{get what="sub"}` Get a list of subscribers. Server responds with a `{meta}` message containing a list of subscribers. See `{meta}` for details. For `me` topic the request returns a list of user's subscriptions. If `ims` is specified and data has not been updated, responds with a `{ctrl}` "not modified" message. Only user's own subscription is returned without [attaching](#sub) to topic first. * `{get what="tags"}` Query indexed tags. Server responds with a `{meta}` message containing an array of string tags. See `{meta}` and `fnd` topic for details. Supported only for `me` and group topics. * `{get what="data"}` Query message history. Server sends `{data}` messages matching parameters provided in the `data` field of the query. The `id` field of the data messages is not provided as it's common for data messages. When all `{data}` messages are transmitted, a `{ctrl}` message is sent. * `{get what="del"}` Query message deletion history. Server responds with a `{meta}` message containing a list of deleted message ranges. * `{get what="cred"}` Query [credentials](#credentail-validation). Server responds with a `{meta}` message containing an array of credentials. Supported for `me` topic only. * `{get what="aux"}` Query auxiliary topic data. Server responds with a `{meta}` message containing an object with auxiliary key-value pairs. #### `{set}` Update topic metadata, delete messages or topic. The requester is generally expected to be [subscribed and attached](#sub) to the topic. Only `desc.private` and requester's `sub.mode` can be updated without attaching first. ```js set: { id: "1a2b3", // string, client-provided message id, optional topic: "grp1XUtEhjv6HND", // string, name of topic to update, required // Optional payload to update topic description desc: { defacs: { // new default access mode auth: "JRWP", // access permissions for authenticated users anon: "JRW" // access permissions for anonymous users }, trusted: { ... }, // application-defined payload assigned by the system administration public: { ... }, // application-defined payload to describe topic private: { ... } // per-user private application-defined content }, // Optional payload to update subscription(s) sub: { user: "usr2il9suCbuko", // string, user affected by this request; // default (empty) means current user mode: "JRWP" // string, access mode change, either given ('user' // is defined) or requested ('user' undefined) }, // object, payload for what == "sub" // Optional update to tags (see fnd topic description) tags: [ // array of strings "email:alice@example.com", "tel:1234567890" ], cred: { // Optional update to credentials. meth: "email", // string, verification method, e.g. "email", "tel", "recaptcha", etc. val: "alice@example.com", // string, credential to verify such as email or phone resp: "178307", // string, verification response, optional params: { ... } // parameters, specific to the verification method, optional }, aux: { ... } // application-defined key-value pairs } ``` #### `{del}` Delete messages, subscriptions, topics, users. ```js del: { id: "1a2b3", // string, client-provided message id, optional topic: "grp1XUtEhjv6HND", // string, topic affected, required for "topic", "sub", // "msg" what: "msg", // string, one of "topic", "sub", "msg", "user", "cred"; what // to delete - the entire topic, a subscription, some or all messages, // a user, a credential; optional, default: "msg" hard: false, // boolean, request to hard-delete vs mark as deleted; in case of // what="msg" delete for all users vs current user only; // optional, default: false delseq: [{low: 123, hi: 125}, {low: 156}], // array of ranges of message IDs // to delete, inclusive-exclusive, i.e. [low, hi), optional user: "usr2il9suCbuko" // string, user being deleted (what="user") or whose // subscription is being deleted (what="sub"), optional cred: { // credential to delete ('me' topic only). meth: "email", // string, verification method, e.g. "email", "tel", etc. val: "alice@example.com" // string, credential being deleted } } ``` `what="msg"` User can soft-delete `hard=false` (default) or hard-delete `hard=true` messages. Soft-deleting messages hides them from the requesting user but does not delete them from storage. An `R` permission is required to soft-delete messages. Hard-deleting messages deletes message content from storage (`head`, `content`) leaving a message stub. It affects all users. A `D` permission is needed to hard-delete messages. Messages can be deleted in bulk by specifying one or more message ID ranges in `delseq` parameter. Each delete operation is assigned a unique `delete ID`. The greatest `delete ID` is reported back in the `clear` of the `{meta}` message. `what="sub"` Deleting a subscription removes specified user from topic subscribers. It requires an `A` permission. A user cannot delete own subscription. A `{leave}` should be used instead. If the subscription is soft-deleted (default), it's marked as deleted without actually deleting a record from storage. `what="topic"` Deleting a topic deletes the topic including all subscriptions, and all messages. Only the owner can delete a topic. `what="user"` Deleting a user is a very heavy operation. Use caution. `what="cred"` Delete credential. Validated credentials and those with no attempts at validation are hard-deleted. Credentials with failed attempts at validation are soft-deleted which prevents their reuse by the same user. #### `{note}` Client-generated ephemeral notification for forwarding to other clients currently attached to the topic, such as typing notifications or delivery receipts. The message is "fire and forget": not stored to disk per se and not acknowledged by the server. Messages deemed invalid are silently dropped. The `{note.recv}` and `{note.read}` do alter persistent state on the server. The value is stored and reported back in the corresponding fields of the `{meta.sub}` message. ```js note: { topic: "grp1XUtEhjv6HND", // string, topic to notify, required what: "kp", // string, action type of the notification. seq: 123, // integer, ID of the message being acknowledged, required for // 'recv' & 'read'. unread: 10, // integer, client-reported total count of unread messages, optional. event: "ringing", // string, subaction; surrently used only by video/audio calls, // when what="call". payload: { // object, required payload for 'call' and 'data'. ... } } ``` The following actions types are currently defined: * call: a video call status update. * data: a generic packet of structured data, usually a form response. * kp: key press, i.e. a typing notification. The client should use it to indicate that the user is composing a new message. * kpa: audio message is in the process of recording. * kpv: video message is in the process of recording. * read: a `{data}` message is seen (read) by the user. It implies `recv` as well. * recv: a `{data}` message is received by the client software but may not yet seen by user. The `read` and `recv` notifications may optionally include `unread` value which is the total count of unread messages as determined by this client. The per-user `unread` count is maintained by the server: it's incremented when new `{data}` messages are sent to user and reset to the values reported by the `{note unread=...}` message. The `unread` value is never decremented by the server. The value is included in push notifications to be shown on a badge on iOS:

Tinode iOS icon with a pill counter

### Server to Client Messages Messages to a session generated in response to a specific request contain an `id` field equal to the id of the originating message. The `id` is not interpreted by the server. Most server to client messages have a `ts` field which is a timestamp when the message was generated by the server. #### `{data}` Content published in the topic. These messages are the only messages persisted in database; `{data}` messages are broadcast to all topic subscribers with an `R` permission. ```js data: { topic: "grp1XUtEhjv6HND", // string, topic which distributed this message, // always present from: "usr2il9suCbuko", // string, id of the user who published the // message; could be missing if the message was // generated by the server head: { key: "value", ... }, // set of string key-value pairs, passed // unchanged from {pub}, optional ts: "2015-10-06T18:07:30.038Z", // string, timestamp seq: 123, // integer, server-issued sequential ID content: { ... } // object, application-defined content exactly as published // by the user in the {pub} message } ``` Data messages have a `seq` field which holds a sequential numeric ID generated by the server. The IDs are guaranteed to be unique within a topic. IDs start from 1 and sequentially increment with every successful [`{pub}`](#pub) message received by the topic. See [Format of Content](#format-of-content) for `content` format considerations. See [`{pub}`](#pub) message for the possible values of the `head` field. #### `{ctrl}` Generic response indicating an error or a success condition. The message is sent to the originating session. ```js ctrl: { id: "1a2b3", // string, client-provided message id, optional topic: "grp1XUtEhjv6HND", // string, topic name, if this is a response in context // of a topic, optional code: 200, // integer, code indicating success or failure of the request, follows // the HTTP status codes model, always present text: "OK", // string, text with more details about the result, always present params: { ... }, // object, generic response parameters, context-dependent, // optional ts: "2015-10-06T18:07:30.038Z", // string, timestamp } ``` #### `{meta}` Information about topic metadata or subscribers, sent in response to `{get}`, `{set}` or `{sub}` message to the originating session. ```js meta: { id: "1a2b3", // string, client-provided message id, optional topic: "grp1XUtEhjv6HND", // string, topic name, if this is a response in // context of a topic, optional ts: "2015-10-06T18:07:30.038Z", // string, timestamp desc: { created: "2015-10-24T10:26:09.716Z", updated: "2015-10-24T10:26:09.716Z", status: "ok", // account status; included for `me` topic only, and only if // the request is sent by a root-authenticated session. defacs: { // topic's default access permissions; present only if the current //user has 'S' permission auth: "JRWP", // default access for authenticated users anon: "N" // default access for anonymous users }, acs: { // user's actual access permissions want: "JRWP", // string, requested access permission given: "JRWP", // string, granted access permission mode: "JRWP" // string, combination of want and given }, seq: 123, // integer, server-issued id of the last {data} message read: 112, // integer, ID of the message user claims through {note} message // to have read, optional recv: 115, // integer, like 'read', but received, optional clear: 12, // integer, in case some messages were deleted, the greatest ID // of a deleted message, optional trusted: { ... }, // application-defined payload writable by the system // administration, readable by all public: { ... }, // application-defined data writable by topic owner, // readable by all private: { ... } // application-defined data that's available to the current // user only }, // object, topic description, optional sub: [ // array of objects, topic subscribers or user's subscriptions, optional { user: "usr2il9suCbuko", // string, ID of the user this subscription // describes, absent when querying 'me'. updated: "2015-10-24T10:26:09.716Z", // timestamp of the last change in the // subscription, present only for // requester's own subscriptions touched: "2017-11-02T09:13:55.530Z", // timestamp of the last message in the // topic (may also include other events // in the future, such as new subscribers) acs: { // user's access permissions want: "JRWP", // string, requested access permission, present for user's own // subscriptions and when the requester is topic's manager or owner given: "JRWP", // string, granted access permission, optional exactly as 'want' mode: "JRWP" // string, combination of want and given }, read: 112, // integer, ID of the message user claims through {note} message // to have read, optional. recv: 315, // integer, like 'read', but received, optional. clear: 12, // integer, in case some messages were deleted, the greatest ID // of a deleted message, optional. trusted: { ... }, // application-defined payload assigned by the system // administration public: { ... }, // application-defined user's 'public' object, absent when // querying P2P topics. private: { ... } // application-defined user's 'private' object. online: true, // boolean, current online status of the user; if this is a // group or a p2p topic, it's user's online status in the topic, // i.e. if the user is attached and listening to messages; if this // is a response to a 'me' query, it tells if the topic is // online; p2p is considered online if the other party is // online, not necessarily attached to topic; a group topic // is considered online if it has at least one active // subscriber. // The following fields are present only when querying 'me' topic topic: "grp1XUtEhjv6HND", // string, topic this subscription describes seq: 321, // integer, server-issued id of the last {data} message // The following field is present only when querying 'me' topic and the // topic described is a P2P topic seen: { // object, if this is a P2P topic, info on when the peer was last //online when: "2015-10-24T10:26:09.716Z", // timestamp ua: "Tinode/1.0 (Android 5.1)" // string, user agent of peer's client } }, ... ], tags: [ // array of tags that the topic or user (in case of "me" topic) is indexed by "email:alice@example.com", "tel:+1234567890", "flowers" ], cred: [ // array of user's credentials { meth: "email", // string, validation method val: "alice@example.com", // string, credential value done: true // validation status }, ... ], del: { clear: 3, // ID of the latest applicable 'delete' transaction delseq: [{low: 15}, {low: 22, hi: 28}, ...], // ranges of IDs of deleted messages }, aux: { ... } // application-defined key-value pairs writable by topic managers, // readable by topic subscribers. } ``` #### `{pres}` Tinode uses `{pres}` message to inform clients of important events. A separate [document](https://docs.google.com/spreadsheets/d/e/2PACX-1vStUDHb7DPrD8tF5eANLu4YIjRkqta8KOhLvcj2precsjqR40eDHvJnnuuS3bw-NcWsP1QKc7GSTYuX/pubhtml?gid=1959642482&single=true) explains all possible use cases. ```js pres: { topic: "me", // string, topic which receives the notification, always present src: "grp1XUtEhjv6HND", // string, topic or user affected by the change, always present what: "on", // string, action type, what's changed, always present seq: 123, // integer, "what" is "msg", a server-issued ID of the message, // optional clear: 15, // integer, "what" is "del", an update to the delete transaction ID. delseq: [{low: 123}, {low: 126, hi: 136}], // array of ranges, "what" is "del", // ranges of IDs of deleted messages, optional ua: "Tinode/1.0 (Android 2.2)", // string, a User Agent string identifying the client // software if "what" is "on" or "ua", optional act: "usr2il9suCbuko", // string, user who performed the action, optional tgt: "usrRkDVe0PYDOo", // string, user affected by the action, optional acs: {want: "+AS-D", given: "+S"} // object, changes to access mode, "what" is "acs", // optional } ``` The following action types are currently defined: * on: topic or user came online * off: topic or user went offline * ua: user agent changed, for example user was logged in with one client, then logged in with another * upd: topic description has changed * tags: topic tags have changed * aux: topic aux data has changed * acs: access permissions have changed * gone: topic is no longer available, for example, it was deleted or you were unsubscribed from it * term: subscription to topic has been terminated, you may try to resubscribe * msg: a new message is available * read: one or more messages have been read by the recipient * recv: one or more messages have been received by the recipient * del: messages were deleted The `{pres}` messages are purely transient: they are not stored and no attempt is made to deliver them later if the destination is temporarily unavailable. Timestamp is not present in `{pres}` messages. #### `{info}` Forwarded client-generated notification `{note}`. Server guarantees that the message complies with this specification and that content of `topic` and `from` fields is correct. The other content is copied from the `{note}` message verbatim and may potentially be incorrect or misleading if the originator so desires. ```js info: { topic: "grp1XUtEhjv6HND", // string, topic affected, always present src: "usrRkDVe0PYDOo", // string, topic where the even has occurred; // present only when "topic": "me" from: "usr2il9suCbuko", // string, id of the user who published the // message, always present what: "read", // string, one of "kp", "recv", "read", "data", see client-side {note}, // always present seq: 123, // integer, ID of the message that client has acknowledged, // guaranteed 0 < read <= recv <= {ctrl.params.seq}; present for recv & // read event: "ringing", // string, used by video/audio calls payload: { ... } // object, arbitrary payload, used by video calls } ``` ================================================ FILE: docs/CLA.md ================================================ # Tinode Individual Contributor License Agreement In order to clarify the intellectual property license granted with Contributions from any person or entity, [Tinode LLC](https://tinode.co) must have a Contributor License Agreement ("CLA") on file that has been signed by each Contributor, indicating agreement to the license terms below. This license is for your protection as a Contributor as well as the protection of Tinode LLC; it does not change your rights to use your own Contributions for any other purpose. You accept and agree to the following terms and conditions for Your present and future Contributions submitted to Tinode. Except for the license granted herein to Tinode LLC and recipients of software distributed by Tinode LLC, You reserve all right, title, and interest in and to Your Contributions. 1. Definitions. "You" (or "Your") shall mean the copyright owner or legal entity authorized by the copyright owner that is making this Agreement with Tinode LLC. For legal entities, the entity making a Contribution and all other entities that control, are controlled by, or are under common control with that entity are considered to be a single Contributor. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "Contribution" shall mean any original work of authorship, including any modifications or additions to an existing work, that is intentionally submitted by You to Tinode LLC for inclusion in, or documentation of, any of the products owned or managed by Tinode LLC (the "Work"). For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to Tinode LLC or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, Tinode LLC for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by You as "Not a Contribution." 2. Grant of Copyright License. Subject to the terms and conditions of this Agreement, You hereby grant to Tinode LLC and to recipients of software distributed by Tinode LLC a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare derivative works of, publicly display, publicly perform, sublicense, and distribute Your Contributions and such derivative works. 3. Grant of Patent License. Subject to the terms and conditions of this Agreement, You hereby grant to Tinode LLC and to recipients of software distributed by Tinode LLC a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by You that are necessarily infringed by Your Contribution(s) alone or by combination of Your Contribution(s) with the Work to which such Contribution(s) was submitted. If any entity institutes patent litigation against You or any other entity (including a cross-claim or counterclaim in a lawsuit) alleging that your Contribution, or the Work to which you have contributed, constitutes direct or contributory patent infringement, then any patent licenses granted to that entity under this Agreement for that Contribution or Work shall terminate as of the date such litigation is filed. 4. You represent that you are legally entitled to grant the above license. If your employer(s) has rights to intellectual property that you create that includes your Contributions, you represent that you have received permission to make Contributions on behalf of that employer, that your employer has waived such rights for your Contributions to Tinode, or that your employer has executed a separate Corporate CLA with Tinode LLC. 5. You represent that each of Your Contributions is Your original creation (see section 7 for submissions on behalf of others). You represent that Your Contribution submissions include complete details of any third-party license or other restriction (including, but not limited to, related patents and trademarks) of which you are personally aware and which are associated with any part of Your Contributions. 6. You are not expected to provide support for Your Contributions, except to the extent You desire to provide support. You may provide support for free, for a fee, or not at all. Unless required by applicable law or agreed to in writing, You provide Your Contributions on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON- INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. 7. Should You wish to submit work that is not Your original creation, You may submit it to Tinode separately from any Contribution, identifying the complete details of its source and of any license or other restriction (including, but not limited to, related patents, trademarks, and license agreements) of which you are personally aware, and conspicuously marking the work as "Submitted on behalf of a third-party: [named here]". 8. You agree to notify Tinode LLC of any facts or circumstances of which you become aware that would make these representations inaccurate in any respect. --- ## [SIGN NOW](https://docs.google.com/forms/d/e/1FAIpQLSfmtJDHzFOJTzIv5jZ-gHRxVU0ysTdIMJakv1xgUUCu_RGeKQ/formResponse) ================================================ FILE: docs/call-establishment.md ================================================ # Video Call Establishment Flow Tinode supports peer to peer video calls over [WebRTC](https://webrtc.org/). The diagram below illustrates a call establishment flow between two users `Alice` and `Bob`. The flow is conceptually similar to [SIP](https://en.wikipedia.org/wiki/Session_Initiation_Protocol), but uses native Tinode messages for transport. Notes: - All communication is proxied by the Tinode server. - Client-to-server events are dispatched in `{note}` messages with the call's `topic` and `seq` fields set. - Server-to-client data are routed in `{info}` messages on `me` topic with the call's `src` (call topic) and `seq` fields set (and/or in data push notifications). - It's assumed that both Alice and Bob may have multiple devices. ## Details ### Call phases The flow may be broken down into 4 phases: * Steps 1-5: call initiation * Steps 6-7: call acceptance * Steps 8-15: metadata exchange * Steps 16-17: call termination ```mermaid sequenceDiagram participant A as Alice participant S as Tinode Server participant B as Bob rect rgb(212, 242, 255) Note over A: Alice initiates a call A->>S: 1. {pub head:webrtc=started} S->>A: 2. {ctrl params:seq=123} S->>+B: 3. {info seq=123 event=invite} S-->>B: or {data seq=123 head:webrtc=started}
push notification B->>-S: 4. {note seq=123 event=ringing} S->>A: 5. {info seq=123 event=ringing} end Note over S: Bob's client ringing
waiting for Bob to accept rect rgb(212, 242, 255) Note over B: Bob accepts the call B->>S: 6. {note seq=123 event=accept} S->>A: 7a. {info seq=123 event=accept} S->>B: 7b. {info seq=123 event=accept} S-->>B: {data seq=124 head:webrtc=accepted,replace=123} S-->>A: {data seq=124 head:webrtc=accepted,replace=123} end Note over S: Call accepted, peer metadata exchange A->>S: 8. {note seq=123 event=offer} S->>+B: 9. {info seq=123 event=offer} B->>-S: 10. {note seq=123 event=answer} S->>A: 11. {info seq=123 event=answer} rect rgb(212, 242, 255) Note over S: ICE candidate exchange loop A->>S: 12. {note seq=123 event=ice-candidate} S->>B: 13. {info seq=123 event=ice-candidate} B->>S: 14. {note seq=123 event=ice-candidate} S->>A: 15. {info seq=123 event=ice-candidate} end end Note over S: Call established
conversation in progress rect rgb(212, 242, 255) Note over S: Call termination alt A->>S: 16a. {note seq=123 event=hang-up} B->>S: 16b. {note seq=123 event=hang-up} end alt S->>B: 17a. {info seq=123 event=hang-up} S->>A: 17b. {info seq=123 event=hang-up} end S-->>B: {data seq=125 head:webrtc=finished,replace=123} S-->>A: {data seq=125 head:webrtc=finished,replace=123} end ``` ### Call Establishment & Termination steps #### Call initiation 1. `Alice` initiates a call by posting a video call message (with `webrtc=started` header) 2. Server replies with a `{ctrl}` message containing the `seq` id of the call. 3. Server routes an `invite` event message to `Bob` (all clients). - Additionally, server sends data push notifications containing a `webrtc=started` field to `Bob`. - Upon receiving either of the above, `Bob` displays the incoming call UI. 4. `Bob` replies with a `ringing` event. 5. Server relays the `ringing` event to `Alice`. The latter now plays the ringing sound. - Note that `Alice` may receive multiple `ringing` events as each separate instance of `Bob` acknowldges receipt of the call invitation separately. - `Alice` and server will wait for up to a server configured timeout for `Bob` to accept the call and then hang up. - At this point, the call is officially **initiated**. #### Call acceptance 6. `Bob` accepts the call by sending an `accept` event. 7. (a) and (b): Server routes `accept` event to `Alice` and `Bob`. - Additionally, the server broadcasts a replacement for the call data message with `webrtc=accepted` header. - Push notifications for the replacement message are sent as well. - `Bob`'s sessions except the one that accepted the call may silently dismiss the incoming call UI. - At this point, the call is officially **accepted**. #### Metadata exchange 8. `Alice` sends an `offer` event containing an SDP payload. 9. Server routes the `offer` to `Bob`. 10. Upon receiving the `offer`, `Bob` replies with an `answer` event containing an SDP payload. 11. Server forwards `Bob`'s `answer` event to `Alice`. Steps 12-15 are Ice candidate exchange between `Alice` and `Bob`. At this point the call is officially **established**. `Alice` and `Bob` can see and hear each other. #### Call termination 16. `Alice` sends a `hang-up` event to server. 17. Server routes a `hang-up` event to `Bob`. Additionally, the server broadcasts a replacement for the call data message with `webrtc=finished` header. Push notifications for the replacement message are sent as well. ================================================ FILE: docs/drafty.md ================================================ # Drafty: Rich Message Format Drafty is a text format used by Tinode to style messages. The intent of Drafty is to be expressive just enough without opening too many possibilities for security issues. One may think of it as JSON-encapsulated [markdown](https://en.wikipedia.org/wiki/Markdown). Drafty is influenced by FB's [draft.js](https://draftjs.org/) specification. As of the time of this writing [Javascript](https://github.com/tinode/tinode-js/blob/master/src/drafty.js), [Java](https://github.com/tinode/tindroid/blob/master/tinodesdk/src/main/java/co/tinode/tinodesdk/model/Drafty.java) and [Swift](https://github.com/tinode/ios/blob/master/TinodeSDK/model/Drafty.swift) implementations exist. A [Go implementation](https://github.com/tinode/chat/blob/master/server/drafty/drafty.go) can convert Drafy to plain text and previews. ## Example > this is **bold**, `code` and _italic_, ~~strike~~
> combined **bold and _italic_**
> an url: https://www.example.com/abc#fragment and another _[https://web.tinode.co](https://web.tinode.co)_
> this is a [@mention](#) and a [#hashtag](#) in a string
> second [#hashtag](#)
Sample Drafty-JSON representation of the text above: ```js { "txt": "this is bold, code and italic, strike combined bold and italic an url: https://www.example.com/abc#fragment and another www.tinode.co this is a @mention and a #hashtag in a string second #hashtag", "fmt": [ { "at":8, "len":4,"tp":"ST" },{ "at":14, "len":4, "tp":"CO" },{ "at":23, "len":6, "tp":"EM"}, { "at":31, "len":6, "tp":"DL" },{ "tp":"BR", "len":1, "at":37 },{ "at":56, "len":6, "tp":"EM" }, { "at":47, "len":15, "tp":"ST" },{ "tp":"BR", "len":1, "at":62 },{ "at":120, "len":13, "tp":"EM" }, { "at":71, "len":36, "key":0 },{ "at":120, "len":13, "key":1 },{ "tp":"BR", "len":1, "at":133 }, { "at":144, "len":8, "key":2 },{ "at":159, "len":8, "key":3 },{ "tp":"BR", "len":1, "at":179 }, { "at":187, "len":8, "key":3 },{ "tp":"BR", "len":1, "at":195 } ], "ent": [ { "tp":"LN", "data":{ "url":"https://www.example.com/abc#fragment" } }, { "tp":"LN", "data":{ "url":"http://www.tinode.co" } }, { "tp":"MN", "data":{ "val":"mention" } }, { "tp":"HT", "data":{ "val":"hashtag" } } ] } ``` ## Structure Drafty object has three fields: plain text `txt`, inline markup `fmt`, and entities `ent`. ### Plain Text The message to be sent is converted to plain Unicode text with all markup stripped and kept in `txt` field. In general, a valid Drafy may contain just the `txt` field. ### Inline Formatting `fmt` Inline formatting is an array of individual styles in the `fmt` field. Each style is represented by an object with at least `at` and `len` fields. The `at` value means 0-based offset into `txt`, `len` is the number of characters to apply formatting to. The third value of style is either `tp` or `key`. If `tp` is provided, it means the style is a basic text decoration: * `BR`: line break. * `CO`: code or monotyped text, possibly with different background: `monotype`. * `DL`: deleted or strikethrough text: ~~strikethrough~~. * `EM`: emphasized text, usually represented as italic: _italic_. * `FM`: form / set of fields; may also be represented as an entity. * `HD`: hidden content. * `HL`: highlighted text, such as text in a different color or with a different background; the color cannot be specified. * `RW`: logical grouping of formats, a row; may also be represented as an entity. * `ST`: strong or bold text: **bold**. If key is provided, it's a 0-based index into the `ent` array which contains extended style parameters such as an image or an URL: * `AU`: embedded audio. * `BN`: interactive button. * `EX`: generic attachment. * `FM`: form / set of fields; may also be represented as a basic decoration. * `HT`: hashtag, e.g. [#hashtag](#). * `IM`: inline image. * `LN`: link (URL) [https://api.tinode.co](https://api.tinode.co). * `MN`: mention such as [@tinode](#). * `RW`: logical grouping of formats, a row; may also be represented as a basic decoration. * `VC`: video (and audio) calls. * `VD`: inline video. Examples: * `{ "at":8, "len":4, "tp":"ST"}`: apply formatting `ST` (strong/bold) to 4 characters starting at offset 8 into `txt`. * `{ "at":144, "len":8, "key":2 }`: insert entity `ent[2]` into position 144, the entity spans 8 characters. * `{ "at":-1, "len":0, "key":4 }`: show the `ent[4]` as a file attachment, don't apply any styling to text. The clients should be able to handle missing `at`, `key`, and `len` values. Missing values are assumed to be equal to `0`. The indexes `at` and `len` are measured in [Unicode code points](https://developer.mozilla.org/en-US/docs/Glossary/Code_point), not bytes or characters. The behavior of multi-codepoint glyphs such as emojis with Fitzpatrick skin tone modifiers, variation selectors, or grouped with `ZWJ` is currently undefined. #### `FM`: a form, an ordered set or fields Form provides means to add paragraph-level formatting to a logical group of elements. It may be represented as a text decoration or as an entity.
Do you agree?
Yes
No
```js { "txt": "Do you agree? Yes No", "fmt": [ {"len": 20, "tp": "FM"}, // missing 'at' is zero: "at": 0 {"len": 13, "tp": "ST"} {"at": 13, "len": 1, "tp": "BR"}, {"at": 14, "len": 3}, // missing 'key' is zero: "key": 0 {"at": 17, "len": 1, "tp": "BR"}, {"at": 18, "len": 2, "key": 1}, ], "ent": [ {"tp": "BN", "data": {"name": "yes", "act": "pub", "val": "oh yes!"}}, {"tp": "BN", "data": {"name": "no", "act": "pub"}} ] } ``` If a `Yes` button is pressed in the example above, the client is expected to send a message to the server with the following content: ```js { "txt": "Yes", "fmt": [{ "at":-1 }], "ent": [{ "tp": "EX", "data": { "mime": "text/x-drafty-fr", // drafty form-response. "val": { "seq": 15, // seq id of the message containing the form. "resp": {"yes": "oh yes!"} } } }] } ``` The form may be optionally represented as an entity: ```js { "tp": "FM", "data": { "su": true } } ``` The `data.su` describes how interactive form elements behave after the click. An `"su": true` indicates that the form is `single use`: the form should change after the first interaction to show that it's no longer accepting input. ### Entities `ent` In general, an entity is a text decoration which requires additional (possibly large) data. An entity is represented by an object with two fields: `tp` indicates type of the entity, `data` is type-dependent styling information. Unknown fields are ignored. #### `AU`: embedded audio record `AU` is an audio record. The `data` contains the following fields: ```js { "tp": "AU", "data": { "mime": "audio/aac", "val": "Rt53jUU...iVBORw0KGgoA==", "ref": "/v0/file/s/e769gvt1ILE.m4v", "preview": "Aw4JKBkAAAAKMSM...vHxgcJhsgESAY" "duration": 180000, "name": "ding_dong.m4a", "size": 595496 } } ``` * `mime`: data type, such as 'audio/ogg'. * `val`: optional in-band audio data: base64-encoded audio bits. * `ref`: optional reference to out-of-band audio data. Either `val` or `ref` must be present. * `preview`: base64-encoded array of bytes to generate a visual preview; each byte is an amplitude bar. * `duration`: duration of the record in milliseconds. * `name`: optional name of the original file. * `size`: optional size of the file in bytes. To create a message with just a single audio record and no text, use the following Drafty: ```js { txt: " ", fmt: [{len: 1}], ent: [{tp: "AU", data: {}]} } ``` _IMPORTANT Security Consideration_: the `val` and `ref` fields may contain malicious payload. The client should restrict URL scheme in the `ref` field to `http` and `https` only. The client should present content of `val` field to the user only if it's correctly converted to an audio. #### `BN`: interactive button `BN` offers an option to send data to a server, either origin server or another one. The `data` contains the following fields: ```js { "tp": "BN", "data": { "name": "confirmation", "act": "url", "val": "some-value", "ref": "https://www.example.com/path/?foo=bar" } } ``` * `act`: type of action in response to button click: * `pub`: send a Drafty-formatted `{pub}` message to the current topic with the form data as an attachment: ```js { "tp":"EX", "data":{ "mime":"text/x-drafty-fr", "val": { "seq": 3, "resp": { "confirmation": "some-value" } } } } ``` * `url`: issue an `HTTP GET` request to the URL from the `data.ref` field. The following query parameters are appended to the URL: `=`, `uid=`, `topic=`, `seq=`. * `note`: send a `{note}` message to the current topic with `what` set to `data` (not currently implemented, contact us if you need it). ```js { "what": "data", "data": { "mime": "text/x-drafty-fr", "val": { "seq": 3, "resp": { "confirmation": "some-value" } } } ``` * `name`: optional name of the button which is reported back to the server. * `val`: additional opaque data. * `ref`: the URL for the `url` action. If the `name` is provided but `val` is not, `val` is assumed to be `1`. If `name` is undefined then nether `name` nor `val` are sent. The button in the example above will send an HTTP GET to https://www.example.com/path/?foo=bar&confirmation=some-value&uid=usrFsk73jYRR&topic=grpnG99YhENiQU&seq=3 assuming the current user ID is `usrFsk73jYRR`, the topic is `grpnG99YhENiQU`, and the sequence ID of message with the button is `3`. _IMPORTANT Security Consideration_: the client should restrict URL scheme in the `ref` field to `http` and `https` only. #### `EX`: file attachment `EX` is an attachment which the client should not try to interpret. The `data` contains the following fields: ```js { "tp": "EX", "data": { "mime", "text/plain", "val", "Q3l0aG9uPT0w...PT00LjAuMAo=", "ref": "/v0/file/s/abcdef12345.txt", "name", "requirements.txt", "size": 1234 } } ``` * `mime`: data type, such as 'application/octet-stream'. * `val`: optional in-band base64-encoded file data. * `ref`: optional reference to out-of-band file data. Either `val` or `ref` must be present. * `name`: optional name of the original file. * `size`: optional size of the file in bytes. To generate a message with the file attachment shown as a downloadable file, use the following format: ```js { at: -1, len: 0, key: } ``` _IMPORTANT Security Consideration_: the `ref` fields may contain malicious payload. The client should restrict URL scheme in the `ref` field to `http` and `https` only. #### `IM`: inline image or attached image with inline preview `IM` is an image. The `data` contains the following fields: ```js { "tp": "IM", "data": { "mime": "image/png", "val": "Rt53jUU...iVBORw0KGgoA==", "ref": "/v0/file/s/abcdef12345.jpg", "width": 512, "height": 512, "name": "sample_image.png", "size": 123456 } } ``` * `mime`: data type, such as 'image/jpeg'. * `val`: optional in-band image data: base64-encoded image bits. * `ref`: optional reference to out-of-band image data. Either `val` or `ref` must be present. * `width`, `height`: linear dimensions of the image in pixels. * `name`: optional name of the original file. * `size`: optional size of the file in bytes. To create a message with just a single image and no text, use the following Drafty: ```js { txt: " ", fmt: [{len: 1}], ent: [{tp: "IM", data: {}]} } ``` _IMPORTANT Security Consideration_: the `val` and `ref` fields may contain malicious payload. The client should restrict URL scheme in the `ref` field to `http` and `https` only. The client should present content of `val` field to the user only if it's correctly converted to an image. #### `LN`: link (URL) `LN` is an URL. The `data` contains a single `url` field: `{ "tp": "LN", "data": { "url": "https://www.example.com/abc#fragment" } }` The `url` could be any valid URL that the client knows how to interpret, for instance it could be email or phone URL too: `email:alice@example.com` or `tel:+17025550001`. _IMPORTANT Security Consideration_: the `url` field may be maliciously constructed. The client should disable certain URL schemes such as `javascript:` and `data:`. #### `MN`: mention such as [@alice](#) Mention `data` contains a single `val` field with ID of the mentioned user: ```js { "tp":"MN", "data":{ "val":"usrFsk73jYRR" } } ``` #### `HT`: hashtag, e.g. [#tinode](#) Hashtag `data` contains a single `val` field with the hashtag value which the client software needs to interpret, for instance it could be a search term: ```js { "tp":"HT", "data":{ "val":"tinode" } } ``` #### `VC`: video call control message Video call `data` contains current state of the call and its duration: ```js { "tp": "VC", "data": { "duration": 10000, "state": "disconnected", "incoming": false, "aonly": true } } ``` * `duration`: call duration in milliseconds. * `state`: surrent call state; supported states: * `accepted`: a call is established (ongoing). * `busy`: a call cannot be established because the callee is already in another call. * `finished`: a previously establied call has successfully finished. * `disconnected`: the call is dropped, for example because of an error. * `missed`: the call is missed, i.e. the callee didn't pick up the phone. * `declined`: the call is declined, i.e. the callee hung up before picking up. * `incoming`: true if the call is incoming, otherwise the call is outgoing. * `aonly`: true if this is an audio-only call (no video). The `VC` may also be represented as a format `"fmt": [{"len": 1, "tp": "VC"}]` with no entity. In such a case all call information is contained in the `head` fields of the enclosing message. #### `VD`: video with preview `VD` represents a video recording. The `data` contains the following fields: ```js { "tp": "VD", "data": { "mime": "video/webm", "ref": "/v0/file/s/abcdef12345.webm", "preview": "AsTrsU...k86n00Ggo==" "preref": "/v0/file/s/abcdef54321.jpeg", "premime": "image/jpeg", "width": 640, "height": 360, "duration": 32000, "name": " bigbuckbunny.webm", "size": 1234567 } } ``` * `mime`: data type of the video, such as 'video/webm'. * `val`: optional in-band video data: base64-encoded video bits, usually not present (null). * `ref`: optional reference to an out-of-band video data. Either `val` or `ref` must be present. * `preview`: optional base64-encoded screencapture image from the video (poster). * `preref`: optional reference to an out-of-band screencapture image from the video (poster). * `premime`: data type of the optional screencapture image (poster); assumed 'image/jpeg' if missing. * `width`, `height`: linear dimensions of the video and poster in pixels. * `duration`: duration of the video in milliseconds. * `name`: optional name of the original file. * `size`: optional size of the file in bytes. To create a message with just a single video and no text, use the following Drafty: ```js { txt: " ", fmt: [{len: 1}], ent: [{tp: "VD", data: {}]} } ``` _IMPORTANT Security Consideration_: the `val`, `ref`, `preview` fields may contain malicious payload. The client should restrict URL scheme in the `ref` and `preview` fields to `http` and `https` only. The client should present content of `val` field to the user only if it's correctly converted to a video. ================================================ FILE: docs/faq.md ================================================ # Frequently Asked Questions ### Q: Where can I find server logs when running in Docker?
**A**: The log is in the container at `/var/log/tinode.log`. Attach to a running container with command ``` docker exec -it name-of-the-running-container /bin/bash ``` Then, for instance, see the log with `tail -50 /var/log/tinode.log` If the container has stopped already, you can copy the log out of the container (saving it to `./tinode.log`): ``` docker cp name-of-the-container:/var/log/tinode.log ./tinode.log ``` Alternatively, you can instruct the docker container to save the logs to a directory on the host by mapping a host directory to `/var/log/` in the container. Add `-v /where/to/save/logs:/var/log` to the `docker run` command. ### Q: What are the options for enabling push notifications?
**A**: You can use [Tinode Push Gateway (TNPG)](https://github.com/tinode/chat/tree/master/server/push/tnpg) or you can use [Google FCM](https://firebase.google.com/docs/cloud-messaging): * _Tinode Push Gateway_ uses Tinode servers to send pushes on your behalf. It requires minumum setup: your server sends request to TNPG, which forwards it to Google FCM or Apple APNS. * _Google FCM_ does not rely on Tinode infrastructure for pushes but requires you to build and release your own mobile apps (iOS and Android). ### Q: How to setup push notifications with Tinode Push Gateway?
**A**: Enabling TNPG push notifications requires two steps: * register at [console.tinode.co](https://console.tinode.co) and obtain a TNPG token. * configure server with the token. See detailed instructions [here](../server/push/tnpg/). ### Q: How to setup push notifications with Google FCM?
**A**: This option requires you to build and release your own mobile apps. If you do not want to do it, use the TNPG option above. Enabling FCM push notifications requires the following steps: * enable push sending from the server. * enable receiving pushes in the clients. #### Server and TinodeWeb 1. Create a project at https://firebase.google.com/ if you have not done so already. 2. Follow instructions at https://cloud.google.com/iam/docs/creating-managing-service-account-keys to download the credentials file. 3. Update the server config [`tinode.conf`](../server/tinode.conf#L255), section `"push"` -> `"name": "fcm"`. Do _ONE_ of the following: * _Either_ enter the path to the downloaded credentials file into `"credentials_file"`. * _OR_ copy the file contents to `"credentials"`.

Remove the other entry. I.e. if you have updated `"credentials_file"`, remove `"credentials"` and vice versa. 4. Update [TinodeWeb](/tinode/webapp/) config [`firebase-init.js`](https://github.com/tinode/webapp/blob/master/firebase-init.js): update `apiKey`, `messagingSenderId`, `projectId`, `appId`, `messagingVapidKey`. See more info at https://github.com/tinode/webapp/#push_notifications #### iOS and Android 1. If you are using an Android client, add `google-services.json` to [Tindroid](/tinode/tindroid/) by following instructions at https://developers.google.com/android/guides/google-services-plugin and recompile the client. You may also optionally submit it to Google Play Store. See more info at https://github.com/tinode/tindroid/#push_notifications 2. If you are using an iOS client, add `GoogleService-Info.plist` to [Tinodios](/tinode/ios/) by following instructions at https://firebase.google.com/docs/cloud-messaging/ios/client) and recompile the client. You may optionally submit the app to Apple AppStore. See more info at https://github.com/tinode/ios/#push_notifications ### Q: How to add new users?
**A**: There are three ways to create accounts: * A user can create a new account using one of the applications (web, Android, iOS). * A new account can be created using [tn-cli](../tn-cli/) (`acc` command or `useradd` macro). The process can be scripted. * If the user already exists in an external database, the Tinode account can be automatically created on the first login using the [rest authenticator](../server/auth/rest/). ### Q: How do I make my installation private?
**A**: If you want to restrict registrations to only those people whom you approve, then the simplest way is to restrict Tinode registrations to an email domain you control: register a custom domain, set up a catch-all email forwarding service at your domain registrar (usually free). Then use your domain name in Tinode config (`"acc_validation" -> "email" -> "domains"`, for example `"domains": ["my-domain.com"]`). You will receive registration emails at your catch-all email box and you will be able to forward validation codes to your users manually. Alternatively, if you have a lot of users, you can use [rest authenticator](../server/auth/rest/). ### Q: How to create a `root` user?
**A**: Starting with Tinode version 0.18 the `root` access can be granted to a user by running the following command: ```sh ./tinode-db -auth=ROOT -uid=usrAbcDef123 -scheme=basic ``` Starting with 0.21 you can use a simpler command: ```sh ./tinode-db -make_root=usrAbcDef123 ``` Where `usrAbcDef123` is the ID of the user to update. In version 0.17 and older the `root` access can be granted to a user only by executing a database query. First create or choose the user you want to promote to `root` then execute the query: * RethinkDB: ```js r.db('tinode').table('auth').get('basic:login-of-the-user-to-make-root').update({authLvl: 30}) ``` * MySQL, PostgreSQL: ```sql USE 'tinode'; UPDATE auth SET authlvl=30 WHERE uname='basic:login-of-the-user-to-make-root'; ``` * MongoDB: ```js db.getCollection('auth').updateOne({_id: 'basic:login-of-the-user-to-make-root'}, {$set: {authlvl: 30}}) ``` The test database has a stock user `xena` which has root access. ### Q: Once the number of network connections reaches about 1000 per node, all kinds of problems start. Is this a bug?
**A**: It is likely not a bug. To ensure good server performance Linux limits the total number of open file descriptors (live network connections, open files) for each process at the kernel level. The default limit is usually 1024. There are other possible restrictions on the number of file descriptors. The problems you are experiencing are likely caused by exceeding one of the Linux-imposed limits. Please seek assistance of a system administrator. ### Q: What is the difference between a group topic and a channel?
**A**: Channel is a special case of a group topic. Normal group topics allow limited number of subscribers (128 by default). Each subscriber can be managed individually: invited, removed, banned, promoted to administrator or owner, other access permissions can be personally adjusted. Group topics with enabled channel functionality additionally permit an unlimited number of `readers`. The readers have read-only access to the topic, they cannot be managed individually, cannot be invited or removed, they cannot post messages. Readers do not generate presence notifications when joining or un-joining the topic and do not receive presence notifications from normal group members. Readers receive channel messages with `From` field set to `null`, i.e. they do not know who personally posted any given message to the channel. Readers cannot delete channel messages. ### Q: What is the proper way to format gRPC {pub content}?
**A**: The gPRC sends `content` field of a `{pub}` message as a byte array while the client applications expect it to be valid JSON. Consequently, you have to format the field to be valid JSON before passing it to gRPC. For example, to send a plain text `Hello world` message you have to send a quoted string `"Hello world"`. In most cases the string you pass to the gRPC call would look like `"\"Hello world\""` or `'"Hello world"'`. ### Q: How to fix PostgreSQL initialization failing with 'missing database' error?
**A**: PostgreSQL has a (mis)feature: a DB connection must always select a database. If the connection tries to use a database which does not exist (even with intent to create it), the connection fails. When Tinode is started for the first time, it tries to create a database, usually `tinode` (see `tinode.conf`, `"store_config": {"adapters": {"postgres": {"DBName": "tinode"}}}`. The database `tinode` obviously does not exist, so Tinode connection falls back to 'default' database which has the same name as the name of the connecting PostgreSQL user. The default configuration specifies user as `postgres` (`"User": "postgres"`), the database `postgres` always exists, so the connection succeeds and everything works as expected. But if you change the user to anything other than `postgres`, let's say `tinodeadmin`, then trouble starts: the database with the name `tinodeadmin` does not exist and the connection fails. If you want to change the user name to anything other than `postgres`, then you must create either a database `tinode` (or whatever you named your Tinode database) or an empty database with the same name as your user `tinodeadmin`. For example: ``` $ psql postgres=# create database tinode; exit ``` ================================================ FILE: docs/monitoring.md ================================================ # Monitoring Tinode server Tinode server can optionally expose runtime statistics as a json document at a configurable HTTP(S) endpoint. The feature is enabled by adding a string parameter `expvar` to the config file. The value of the `expvar` is the URL path pointing to the location where the variables are served. In addition to the config file, the feature can be enabled from the command line by adding an `--expvar` parameter. The feature is disabled if the value of `expvar` is an empty string `""` or a dash `"-"`. A non-blank value of the command line parameter overrides the config file value. The feature is enabled in the default config file to publish stats at `/debug/vars`. As of the time of this writing the following stats are published: * `memstats`: Go's memory statistics as described at https://golang.org/pkg/runtime/#MemStats * `cmdline`: server's command line parameters as an array of strings. * `TotalSessions`: the count of all sessions which were created during server's life time. * `LiveSessions`: the number of sessions currently live, regardless of authentication status. * `TotalTopics`: the count of all topics activated during servers's life time. * `LiveTopics`: the number of currently active topics. ================================================ FILE: docs/thecard.md ================================================ # theCard: Person/Topic Description Format Tinode uses `theCard` to store and transmit descriptions of people and topics. The format is conceptually similar to [vCard](https://www.rfc-editor.org/rfc/rfc6350.txt) 3.0. When `JSON` is used to represent `theCard` data, it does it differently than [jCard](https://tools.ietf.org/html/rfc7095). `theCard` and `jCard` are incompatible. The main difference is that `theCard` uses objects to represent logically related data while `jCard` uses ordered arrays. `theCard` is structured as an object: ```js { fn: "John Doe", // string, formatted name of the person or topic. photo: { // object, avatar photo; either 'data' or 'ref' must be present, all other fields are optional. type: "jpeg", // string, MIME type but with 'image/' dropped. data: "Rt53jUU...iVBORw0KGgoA==", // string, base64-encoded binary image data ref: "https://api.tinode.co/file/s/abcdef12345.jpg", // string, URL of the image. width: 512, // integer, image width in pixels. height: 512, // integer, image height in pixels. size: 123456 // integer, image size in bytes. }, note: "Some notes", // string, description of a person or a topic. // // None of the following fields are implemented by any known client: // n: { // object, person's structured name. surname: "Miner", // surname or last or family name. given: "Coal", // first or given name. additional: "Diamond", // additional name, such as middle name or patronymic. prefix: "Dr.", // prefix, such as honorary title or gender designation. suffix: "Jr.", // suffix, such as 'Jr' or 'II'. }, org: { // object, organization the person or topic belongs to. fn: "Most Evil Corp", // string, formatted name of the organisation. title: "CEO", // string, person's job title at the organisation. }, comm: [ // array of objects defining means of communication with the the person or topic. { des: ["home", "voice"], // contact designation, optional. proto: "tel", // communication protocol, required value: "+17025551234" // phone number. }, { des: ["work"], proto: "email", value: "alice@example.com", // email address }, { des: ["other"], proto: "tinode", value: "tinode:topic/usrRkDVe0PYDOo", // tinode ID URI, may include server address. }, { proto: "http", // should be used for either http or https website addresses. value: "https://tinode.co", // actual address of a website. }, ... ], bday: { // object, person's birthday. y: 1970, // integer, year m: 1, // integer, month 1..12 d: 15 // integer, day 1..31 }, } ``` All fields are optional. Tinode clients currently use only `fn`, `photo`, `org`, `note`, `comm` fields. If other fields are needed in the future, then they will be adopted from the correspondent [vCard](https://www.rfc-editor.org/rfc/rfc6350.txt) fields. ================================================ FILE: docs/translations.md ================================================ # Localizing Tinode **IMPORTANT!** Please use `devel` branches for translations. ## Server The server sends emails or SMS to users upon creation of a new account and when the user requests to reset the password: * [/server/templ/email-validation-en.templ](../server/templ/email-validation-en.templ) * [/server/templ/email-password-reset-en.templ](../server/templ/email-password-reset-en.templ) * [/server/templ/sms-validation-en.templ](../server/templ/sms-validation-en.templ) Create a copy of the files naming them `email-password-reset-XX.teml`, `email-validation-XX.templ`, `sms-validation-XX.templ` where `XX` is the [ISO-631-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) code of the new language. Translate the content and send a pull request with the new files. If you don't know how to create a pull request then just sent the translated files in any way you can. ## Webapp The translations are located in two places: [/src/i18n/](https://github.com/tinode/webapp/tree/devel/src/i18n/) and [/service-worker.js](https://github.com/tinode/webapp/blob/devel/service-worker.js#L11). In order to add a translation, copy `/src/i18n/en.json` to a file named `/src/i18n/XX.json` where `XX` is the [BCP-47](https://tools.ietf.org/rfc/bcp/bcp47.txt) code of the new language. If in doubt how to choose the BCP-47 language code, use a two letter [ISO-631-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes). Only the `"translation":` line has to be translated, the `"defaultMessage"`, `"description"` etc should NOT be translated, they serve as help only, i.e.: ```js "action_block_contact": { "translation": "Bloquear contacto", // <<<---- Only this string needs to be translated "defaultMessage": "Block Contact", // This is the default message in English "description": "Flat button [Block Contact]", // This is an explanation where/how the string is used. "missing": false, "obsolete": false }, ``` When translating the `service-worker.js`, just add the strings directly to the file. Only two strings need to be translated "New message" and "New chat": ```js const i18n = { ... 'XX': { 'new_message': "New message", 'new_chat': "New chat", }, ... ``` Please send a pull request with the new files. If you don't know how to create a pull request just sent the files in any way you can. ## Android A single file needs to be translated: [/tinode/tindroid/app/src/main/res/values/strings.xml](https://github.com/tinode/tindroid/blob/devel/app/src/main/res/values/strings.xml) Create a new directory `values-XX` in [app/src/main/res](https://github.com/tinode/tindroid/tree/devel/app/src/main/res), where `XX` is a two-letter [ISO-631-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) code , optionally followed by a two letter [ISO 3166-1-alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) region code (preceded by lowercase r). For example `values-pt.xml` would contain Portuguese translations while `values-pt-rBR.xml` is for _Brazilian_ Portuguese translations. Make a copy of the file with English strings, place it to the new directory. Translate all the strings not marked with `translatable="false"` (the strings with `translatable="false"` don't need to be included at all) then send a pull request with the new file. If you don't know how to create a pull request then just sent the file in any way you can. ## iOS Unfortunately iOS localization process is very convoluted and generally requires `Xcode` which runs only on Mac. Unless you are familiar with the iOS development, please create a [feature request](https://github.com/tinode/ios/issues/new?assignees=&labels=&template=feature_request.md&title=) for the desired language and we will send you the file for translation. If you feel brave enough, you can translate the [following .xliff file](https://github.com/tinode/ios/blob/devel/Localizations/en.xcloc/Localized%20Contents/en.xliff) and then send it to us in any way you can. Translate all the strings between the `` tags: ```xml Action failed: %@ Se ha producido un error al realizar la acción: %@ Toast notification ``` If you are familiar with Xcode and localization for iOS, the exported localizations are located at [/Localizations](https://github.com/tinode/ios/tree/devel/Localizations). ================================================ FILE: go.mod ================================================ module github.com/tinode/chat go 1.24.0 require ( firebase.google.com/go v3.13.0+incompatible github.com/aws/aws-sdk-go v1.55.7 github.com/go-sql-driver/mysql v1.9.3 github.com/golang/mock v1.6.0 github.com/google/go-cmp v0.7.0 github.com/gorilla/handlers v1.5.2 github.com/gorilla/websocket v1.5.3 github.com/jackc/pgconn v1.14.3 github.com/jackc/pgx/v4 v4.18.3 github.com/jmoiron/sqlx v1.4.0 github.com/nyaruka/phonenumbers v1.6.3 github.com/prometheus/client_golang v1.22.0 github.com/prometheus/common v0.65.0 github.com/rivo/uniseg v0.4.7 github.com/tinode/jsonco v1.0.0 github.com/tinode/snowflake v1.0.0 go.mongodb.org/mongo-driver v1.17.4 golang.org/x/crypto v0.45.0 golang.org/x/oauth2 v0.30.0 golang.org/x/text v0.31.0 google.golang.org/api v0.241.0 google.golang.org/grpc v1.73.0 google.golang.org/protobuf v1.36.6 gopkg.in/rethinkdb/rethinkdb-go.v6 v6.2.2 ) require ( cel.dev/expr v0.24.0 // indirect cloud.google.com/go/auth v0.16.2 // indirect cloud.google.com/go/auth/oauth2adapt v0.2.8 // indirect cloud.google.com/go/monitoring v1.24.2 // indirect filippo.io/edwards25519 v1.1.0 // indirect github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.29.0 // indirect github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.53.0 // indirect github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.53.0 // indirect github.com/cncf/xds/go v0.0.0-20250501225837-2ac532fd4443 // indirect github.com/envoyproxy/go-control-plane/envoy v1.32.4 // indirect github.com/envoyproxy/protoc-gen-validate v1.2.1 // indirect github.com/go-jose/go-jose/v4 v4.1.1 // indirect github.com/go-logr/logr v1.4.3 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/golang-jwt/jwt/v5 v5.2.2 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 // indirect github.com/spiffe/go-spiffe/v2 v2.5.0 // indirect github.com/zeebo/errs v1.4.0 // indirect go.opentelemetry.io/auto/sdk v1.1.0 // indirect go.opentelemetry.io/contrib/detectors/gcp v1.37.0 // indirect go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.62.0 // indirect go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.62.0 // indirect go.opentelemetry.io/otel v1.37.0 // indirect go.opentelemetry.io/otel/metric v1.37.0 // indirect go.opentelemetry.io/otel/sdk v1.37.0 // indirect go.opentelemetry.io/otel/sdk/metric v1.37.0 // indirect go.opentelemetry.io/otel/trace v1.37.0 // indirect golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b // indirect golang.org/x/net v0.47.0 // indirect ) require ( cloud.google.com/go v0.121.3 // indirect cloud.google.com/go/compute/metadata v0.7.0 // indirect cloud.google.com/go/firestore v1.18.0 // indirect cloud.google.com/go/iam v1.5.2 // indirect cloud.google.com/go/longrunning v0.6.7 // indirect cloud.google.com/go/storage v1.55.0 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/felixge/httpsnoop v1.0.4 // indirect github.com/golang/protobuf v1.5.4 // indirect github.com/golang/snappy v1.0.0 // indirect github.com/google/s2a-go v0.1.9 // indirect github.com/google/uuid v1.6.0 // indirect github.com/googleapis/enterprise-certificate-proxy v0.3.6 // indirect github.com/googleapis/gax-go/v2 v2.15.0 // indirect github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed // indirect github.com/jackc/chunkreader/v2 v2.0.1 // indirect github.com/jackc/pgio v1.0.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgproto3/v2 v2.3.3 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect github.com/jackc/pgtype v1.14.4 // indirect github.com/jackc/puddle v1.3.0 // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect github.com/klauspost/compress v1.18.0 // indirect github.com/montanaflynn/stats v0.7.1 // indirect github.com/opentracing/opentracing-go v1.2.0 // indirect github.com/prometheus/client_model v0.6.2 // indirect github.com/prometheus/procfs v0.17.0 // indirect github.com/sirupsen/logrus v1.9.3 // indirect github.com/twilio/twilio-go v1.26.5 github.com/xdg-go/pbkdf2 v1.0.0 // indirect github.com/xdg-go/scram v1.1.2 // indirect github.com/xdg-go/stringprep v1.0.4 // indirect github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect golang.org/x/sync v0.18.0 // indirect golang.org/x/sys v0.38.0 // indirect golang.org/x/time v0.12.0 // indirect google.golang.org/appengine v1.6.8 // indirect google.golang.org/genproto v0.0.0-20250707201910-8d1bb00bc6a7 // indirect google.golang.org/genproto/googleapis/api v0.0.0-20250707201910-8d1bb00bc6a7 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20250707201910-8d1bb00bc6a7 // indirect gopkg.in/cenkalti/backoff.v2 v2.2.1 // indirect ) ================================================ FILE: go.sum ================================================ cel.dev/expr v0.24.0 h1:56OvJKSH3hDGL0ml5uSxZmz3/3Pq4tJ+fb1unVLAFcY= cel.dev/expr v0.24.0/go.mod h1:hLPLo1W4QUmuYdA72RBX06QTs6MXw941piREPl3Yfiw= cloud.google.com/go v0.121.3 h1:84RD+hQXNdY5Sw/MWVAx5O9Aui/rd5VQ9HEcdN19afo= cloud.google.com/go v0.121.3/go.mod h1:6vWF3nJWRrEUv26mMB3FEIU/o1MQNVPG1iHdisa2SJc= cloud.google.com/go/auth v0.16.2 h1:QvBAGFPLrDeoiNjyfVunhQ10HKNYuOwZ5noee0M5df4= cloud.google.com/go/auth v0.16.2/go.mod h1:sRBas2Y1fB1vZTdurouM0AzuYQBMZinrUYL8EufhtEA= cloud.google.com/go/auth/oauth2adapt v0.2.8 h1:keo8NaayQZ6wimpNSmW5OPc283g65QNIiLpZnkHRbnc= cloud.google.com/go/auth/oauth2adapt v0.2.8/go.mod h1:XQ9y31RkqZCcwJWNSx2Xvric3RrU88hAYYbjDWYDL+c= cloud.google.com/go/compute/metadata v0.7.0 h1:PBWF+iiAerVNe8UCHxdOt6eHLVc3ydFeOCw78U8ytSU= cloud.google.com/go/compute/metadata v0.7.0/go.mod h1:j5MvL9PprKL39t166CoB1uVHfQMs4tFQZZcKwksXUjo= cloud.google.com/go/firestore v1.18.0 h1:cuydCaLS7Vl2SatAeivXyhbhDEIR8BDmtn4egDhIn2s= cloud.google.com/go/firestore v1.18.0/go.mod h1:5ye0v48PhseZBdcl0qbl3uttu7FIEwEYVaWm0UIEOEU= cloud.google.com/go/iam v1.5.2 h1:qgFRAGEmd8z6dJ/qyEchAuL9jpswyODjA2lS+w234g8= cloud.google.com/go/iam v1.5.2/go.mod h1:SE1vg0N81zQqLzQEwxL2WI6yhetBdbNQuTvIKCSkUHE= cloud.google.com/go/logging v1.13.0 h1:7j0HgAp0B94o1YRDqiqm26w4q1rDMH7XNRU34lJXHYc= cloud.google.com/go/logging v1.13.0/go.mod h1:36CoKh6KA/M0PbhPKMq6/qety2DCAErbhXT62TuXALA= cloud.google.com/go/longrunning v0.6.7 h1:IGtfDWHhQCgCjwQjV9iiLnUta9LBCo8R9QmAFsS/PrE= cloud.google.com/go/longrunning v0.6.7/go.mod h1:EAFV3IZAKmM56TyiE6VAP3VoTzhZzySwI/YI1s/nRsY= cloud.google.com/go/monitoring v1.24.2 h1:5OTsoJ1dXYIiMiuL+sYscLc9BumrL3CarVLL7dd7lHM= cloud.google.com/go/monitoring v1.24.2/go.mod h1:x7yzPWcgDRnPEv3sI+jJGBkwl5qINf+6qY4eq0I9B4U= cloud.google.com/go/storage v1.55.0 h1:NESjdAToN9u1tmhVqhXCaCwYBuvEhZLLv0gBr+2znf0= cloud.google.com/go/storage v1.55.0/go.mod h1:ztSmTTwzsdXe5syLVS0YsbFxXuvEmEyZj7v7zChEmuY= cloud.google.com/go/trace v1.11.6 h1:2O2zjPzqPYAHrn3OKl029qlqG6W8ZdYaOWRyr8NgMT4= cloud.google.com/go/trace v1.11.6/go.mod h1:GA855OeDEBiBMzcckLPE2kDunIpC72N+Pq8WFieFjnI= filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= firebase.google.com/go v3.13.0+incompatible h1:3TdYC3DDi6aHn20qoRkxwGqNgdjtblwVAyRLQwGn/+4= firebase.google.com/go v3.13.0+incompatible/go.mod h1:xlah6XbEyW6tbfSklcfe5FHJIwjt8toICdV5Wh9ptHs= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.29.0 h1:UQUsRi8WTzhZntp5313l+CHIAT95ojUI2lpP/ExlZa4= github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.29.0/go.mod h1:Cz6ft6Dkn3Et6l2v2a9/RpN7epQ1GtDlO6lj8bEcOvw= github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.53.0 h1:owcC2UnmsZycprQ5RfRgjydWhuoxg71LUfyiQdijZuM= github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.53.0/go.mod h1:ZPpqegjbE99EPKsu3iUWV22A04wzGPcAY/ziSIQEEgs= github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/cloudmock v0.53.0 h1:4LP6hvB4I5ouTbGgWtixJhgED6xdf67twf9PoY96Tbg= github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/cloudmock v0.53.0/go.mod h1:jUZ5LYlw40WMd07qxcQJD5M40aUxrfwqQX1g7zxYnrQ= github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.53.0 h1:Ron4zCA/yk6U7WOBXhTJcDpsUBG9npumK6xw2auFltQ= github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.53.0/go.mod h1:cSgYe11MCNYunTnRXrKiR/tHc0eoKjICUuWpNZoVCOo= github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs= github.com/aws/aws-sdk-go v1.55.7 h1:UJrkFq7es5CShfBwlWAC8DA077vp8PyVbQd3lqLiztE= github.com/aws/aws-sdk-go v1.55.7/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU= github.com/beevik/etree v1.1.0/go.mod h1:r8Aw8JqVegEf0w2fDnATrX9VpkMcyFeM0FhwO62wh+A= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/bitly/go-hostpool v0.1.0 h1:XKmsF6k5el6xHG3WPJ8U0Ku/ye7njX7W81Ng7O2ioR0= github.com/bitly/go-hostpool v0.1.0/go.mod h1:4gOCgp6+NZnVqlKyZ/iBZFTAJKembaVENUpMkpg42fw= github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4YnC6+E63dPcxHo2sUxDIu8g3QgEJdRY= github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cncf/xds/go v0.0.0-20250501225837-2ac532fd4443 h1:aQ3y1lwWyqYPiWZThqv1aFbZMiM9vblcSArJRf2Irls= github.com/cncf/xds/go v0.0.0-20250501225837-2ac532fd4443/go.mod h1:W+zGtBO5Y1IgJhy4+A9GOqVhqLpfZi+vwmdNXUehLA8= github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I= github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ= github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/envoyproxy/go-control-plane v0.13.4 h1:zEqyPVyku6IvWCFwux4x9RxkLOMUL+1vC9xUFv5l2/M= github.com/envoyproxy/go-control-plane v0.13.4/go.mod h1:kDfuBlDVsSj2MjrLEtRWtHlsWIFcGyB2RMO44Dc5GZA= github.com/envoyproxy/go-control-plane/envoy v1.32.4 h1:jb83lalDRZSpPWW2Z7Mck/8kXZ5CQAFYVjQcdVIr83A= github.com/envoyproxy/go-control-plane/envoy v1.32.4/go.mod h1:Gzjc5k8JcJswLjAx1Zm+wSYE20UrLtt7JZMWiWQXQEw= github.com/envoyproxy/go-control-plane/ratelimit v0.1.0 h1:/G9QYbddjL25KvtKTv3an9lx6VBE2cnb8wp1vEGNYGI= github.com/envoyproxy/go-control-plane/ratelimit v0.1.0/go.mod h1:Wk+tMFAFbCXaJPzVVHnPgRKdUdwW/KdbRt94AzgRee4= github.com/envoyproxy/protoc-gen-validate v1.2.1 h1:DEo3O99U8j4hBFwbJfrz9VtgcDfUKS7KJ7spH3d86P8= github.com/envoyproxy/protoc-gen-validate v1.2.1/go.mod h1:d/C80l/jxXLdfEIhX1W2TmLfsJ31lvEjwamM4DxlWXU= github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg= github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/go-jose/go-jose/v4 v4.1.1 h1:JYhSgy4mXXzAdF3nUx3ygx347LRXJRrpgyU3adRmkAI= github.com/go-jose/go-jose/v4 v4.1.1/go.mod h1:BdsZGqgdO3b6tTc6LSE56wcDbMMLuPsw5d4ZD5f94kA= github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY= github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI= github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= github.com/go-sql-driver/mysql v1.9.3 h1:U/N249h2WzJ3Ukj8SowVFjdtZKfu9vlLZxjPXV1aweo= github.com/go-sql-driver/mysql v1.9.3/go.mod h1:qn46aNg1333BRMNU69Lq93t8du/dwxI64Gl8i5p1WMU= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/gofrs/uuid v4.0.0+incompatible h1:1SD/1F5pU8p29ybwgQSwpQk+mwdRrXCYuPhW6m+TnJw= github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/golang-jwt/jwt/v5 v5.2.2 h1:Rl4B7itRWVtYIHFrSNd7vhTiz9UpLdi6gZhZ3wEeDy8= github.com/golang-jwt/jwt/v5 v5.2.2/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc= github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.4/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= github.com/golang/snappy v1.0.0 h1:Oy607GVXHs7RtbggtPBnr2RmDArIsAefDwvrdWvRhGs= github.com/golang/snappy v1.0.0/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= github.com/google/martian/v3 v3.3.3 h1:DIhPTQrbPkgs2yJYdXU/eNACCG5DVQjySNRNlflZ9Fc= github.com/google/martian/v3 v3.3.3/go.mod h1:iEPrYcgCF7jA9OtScMFQyAlZZ4YXTKEtJ1E6RWzmBA0= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/google/s2a-go v0.1.9 h1:LGD7gtMgezd8a/Xak7mEWL0PjoTQFvpRudN895yqKW0= github.com/google/s2a-go v0.1.9/go.mod h1:YA0Ei2ZQL3acow2O62kdp9UlnvMmU7kA6Eutn0dXayM= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/googleapis/enterprise-certificate-proxy v0.3.6 h1:GW/XbdyBFQ8Qe+YAmFU9uHLo7OnF5tL52HFAgMmyrf4= github.com/googleapis/enterprise-certificate-proxy v0.3.6/go.mod h1:MkHOF77EYAE7qfSuSS9PU6g4Nt4e11cnsDUowfwewLA= github.com/googleapis/gax-go/v2 v2.15.0 h1:SyjDc1mGgZU5LncH8gimWo9lW1DtIfPibOG81vgd/bo= github.com/googleapis/gax-go/v2 v2.15.0/go.mod h1:zVVkkxAQHa1RQpg9z2AUCMnKhi0Qld9rcmyfL1OZhoc= github.com/gorilla/handlers v1.5.2 h1:cLTUSsNkgcwhgRqvCNmdbRWG0A3N4F+M2nWKdScwyEE= github.com/gorilla/handlers v1.5.2/go.mod h1:dX+xVpaxdSw+q0Qek8SSsl3dfMk3jNddUkMzo0GtH0w= github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed h1:5upAirOpQc1Q53c0bnx2ufif5kANL7bfZWcc6VJWJd8= github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed/go.mod h1:tMWxXQ9wFIaZeTI9F+hmhFiGpFmhOHzyShyFUhRm0H4= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo= github.com/jackc/chunkreader/v2 v2.0.0/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk= github.com/jackc/chunkreader/v2 v2.0.1 h1:i+RDz65UE+mmpjTfyz0MoVTnzeYxroil2G82ki7MGG8= github.com/jackc/chunkreader/v2 v2.0.1/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk= github.com/jackc/pgconn v0.0.0-20190420214824-7e0022ef6ba3/go.mod h1:jkELnwuX+w9qN5YIfX0fl88Ehu4XC3keFuOJJk9pcnA= github.com/jackc/pgconn v0.0.0-20190824142844-760dd75542eb/go.mod h1:lLjNuW/+OfW9/pnVKPazfWOgNfH2aPem8YQ7ilXGvJE= github.com/jackc/pgconn v0.0.0-20190831204454-2fabfa3c18b7/go.mod h1:ZJKsE/KZfsUgOEh9hBm+xYTstcNHg7UPMVJqRfQxq4s= github.com/jackc/pgconn v1.8.0/go.mod h1:1C2Pb36bGIP9QHGBYCjnyhqu7Rv3sGshaQUvmfGIB/o= github.com/jackc/pgconn v1.9.0/go.mod h1:YctiPyvzfU11JFxoXokUOOKQXQmDMoJL9vJzHH8/2JY= github.com/jackc/pgconn v1.9.1-0.20210724152538-d89c8390a530/go.mod h1:4z2w8XhRbP1hYxkpTuBjTS3ne3J48K83+u0zoyvg2pI= github.com/jackc/pgconn v1.14.3 h1:bVoTr12EGANZz66nZPkMInAV/KHD2TxH9npjXXgiB3w= github.com/jackc/pgconn v1.14.3/go.mod h1:RZbme4uasqzybK2RK5c65VsHxoyaml09lx3tXOcO/VM= github.com/jackc/pgio v1.0.0 h1:g12B9UwVnzGhueNavwioyEEpAmqMe1E/BN9ES+8ovkE= github.com/jackc/pgio v1.0.0/go.mod h1:oP+2QK2wFfUWgr+gxjoBH9KGBb31Eio69xUb0w5bYf8= github.com/jackc/pgmock v0.0.0-20190831213851-13a1b77aafa2/go.mod h1:fGZlG77KXmcq05nJLRkk0+p82V8B8Dw8KN2/V9c/OAE= github.com/jackc/pgmock v0.0.0-20201204152224-4fe30f7445fd/go.mod h1:hrBW0Enj2AZTNpt/7Y5rr2xe/9Mn757Wtb2xeBzPv2c= github.com/jackc/pgmock v0.0.0-20210724152146-4ad1a8207f65 h1:DadwsjnMwFjfWc9y5Wi/+Zz7xoE5ALHsRQlOctkOiHc= github.com/jackc/pgmock v0.0.0-20210724152146-4ad1a8207f65/go.mod h1:5R2h2EEX+qri8jOWMbJCtaPWkrrNc7OHwsp2TCqp7ak= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= github.com/jackc/pgproto3 v1.1.0/go.mod h1:eR5FA3leWg7p9aeAqi37XOTgTIbkABlvcPB3E5rlc78= github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190420180111-c116219b62db/go.mod h1:bhq50y+xrl9n5mRYyCBFKkpRVTLYJVWeCc+mEAI3yXA= github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190609003834-432c2951c711/go.mod h1:uH0AWtUmuShn0bcesswc4aBTWGvw0cAxIJp+6OB//Wg= github.com/jackc/pgproto3/v2 v2.0.0-rc3/go.mod h1:ryONWYqW6dqSg1Lw6vXNMXoBJhpzvWKnT95C46ckYeM= github.com/jackc/pgproto3/v2 v2.0.0-rc3.0.20190831210041-4c03ce451f29/go.mod h1:ryONWYqW6dqSg1Lw6vXNMXoBJhpzvWKnT95C46ckYeM= github.com/jackc/pgproto3/v2 v2.0.6/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA= github.com/jackc/pgproto3/v2 v2.1.1/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA= github.com/jackc/pgproto3/v2 v2.3.3 h1:1HLSx5H+tXR9pW3in3zaztoEwQYRC9SQaYUHjTSUOag= github.com/jackc/pgproto3/v2 v2.3.3/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA= github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b/go.mod h1:vsD4gTJCa9TptPL8sPkXrLZ+hDuNrZCnj29CQpr4X1E= github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= github.com/jackc/pgtype v0.0.0-20190421001408-4ed0de4755e0/go.mod h1:hdSHsc1V01CGwFsrv11mJRHWJ6aifDLfdV3aVjFF0zg= github.com/jackc/pgtype v0.0.0-20190824184912-ab885b375b90/go.mod h1:KcahbBH1nCMSo2DXpzsoWOAfFkdEtEJpPbVLq8eE+mc= github.com/jackc/pgtype v0.0.0-20190828014616-a8802b16cc59/go.mod h1:MWlu30kVJrUS8lot6TQqcg7mtthZ9T0EoIBFiJcmcyw= github.com/jackc/pgtype v1.8.1-0.20210724151600-32e20a603178/go.mod h1:C516IlIV9NKqfsMCXTdChteoXmwgUceqaLfjg2e3NlM= github.com/jackc/pgtype v1.14.0/go.mod h1:LUMuVrfsFfdKGLw+AFFVv6KtHOFMwRgDDzBt76IqCA4= github.com/jackc/pgtype v1.14.4 h1:fKuNiCumbKTAIxQwXfB/nsrnkEI6bPJrrSiMKgbJ2j8= github.com/jackc/pgtype v1.14.4/go.mod h1:aKeozOde08iifGosdJpz9MBZonJOUJxqNpPBcMJTlVA= github.com/jackc/pgx/v4 v4.0.0-20190420224344-cc3461e65d96/go.mod h1:mdxmSJJuR08CZQyj1PVQBHy9XOp5p8/SHH6a0psbY9Y= github.com/jackc/pgx/v4 v4.0.0-20190421002000-1b8f0016e912/go.mod h1:no/Y67Jkk/9WuGR0JG/JseM9irFbnEPbuWV2EELPNuM= github.com/jackc/pgx/v4 v4.0.0-pre1.0.20190824185557-6972a5742186/go.mod h1:X+GQnOEnf1dqHGpw7JmHqHc1NxDoalibchSk9/RWuDc= github.com/jackc/pgx/v4 v4.12.1-0.20210724153913-640aa07df17c/go.mod h1:1QD0+tgSXP7iUjYm9C1NxKhny7lq6ee99u/z+IHFcgs= github.com/jackc/pgx/v4 v4.18.2/go.mod h1:Ey4Oru5tH5sB6tV7hDmfWFahwF15Eb7DNXlRKx2CkVw= github.com/jackc/pgx/v4 v4.18.3 h1:dE2/TrEsGX3RBprb3qryqSV9Y60iZN1C6i8IrmW9/BA= github.com/jackc/pgx/v4 v4.18.3/go.mod h1:Ey4Oru5tH5sB6tV7hDmfWFahwF15Eb7DNXlRKx2CkVw= github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/jackc/puddle v1.3.0 h1:eHK/5clGOatcjX3oWGBO/MpxpbHzSwud5EWTSCI+MX0= github.com/jackc/puddle v1.3.0/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= github.com/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o= github.com/jmoiron/sqlx v1.4.0/go.mod h1:ZrZ7UsYB/weZdl2Bxg6jCRO9c3YHl8r3ahlKmRT4JLY= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo= github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/localtunnel/go-localtunnel v0.0.0-20170326223115-8a804488f275 h1:IZycmTpoUtQK3PD60UYBwjaCUHUP7cML494ao9/O8+Q= github.com/localtunnel/go-localtunnel v0.0.0-20170326223115-8a804488f275/go.mod h1:zt6UU74K6Z6oMOYJbJzYpYucqdcQwSMPBEdSvGiaUMw= github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ= github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE= github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/nyaruka/phonenumbers v1.6.3 h1:JU7Q30+UM/03/vto6Q4EiZfEuRpTVyXMqImIbI942Qw= github.com/nyaruka/phonenumbers v1.6.3/go.mod h1:7gjs+Lchqm49adhAKB5cdcng5ZXgt6x7Jgvi0ZorUtU= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.12.0/go.mod h1:oUhWkIvk5aDxtKvDDuw8gItl8pKl42LzjC9KZE0HfGg= github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= github.com/onsi/gomega v1.9.0/go.mod h1:Ho0h+IUsWyvy1OpqCwxlQ/21gkhVunqlU8fDGcoTdcA= github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs= github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 h1:GFCKgmp0tecUJ0sJuv4pzYCqS9+RGSn52M3FUwPs+uo= github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10/go.mod h1:t/avpk3KcrXxUnYOhZhMXJlSEyie6gQbtLq5NM3loB8= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_golang v1.22.0 h1:rb93p9lokFEsctTys46VnV1kLCDpVZ0a/Y92Vm0Zc6Q= github.com/prometheus/client_golang v1.22.0/go.mod h1:R7ljNsLXhuQXYZYtw6GAE9AZg8Y7vEW5scdCXrWRXC0= github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk= github.com/prometheus/client_model v0.6.2/go.mod h1:y3m2F6Gdpfy6Ut/GBsUqTWZqCUvMVzSfMLjcu6wAwpE= github.com/prometheus/common v0.65.0 h1:QDwzd+G1twt//Kwj/Ww6E9FQq1iVMmODnILtW1t2VzE= github.com/prometheus/common v0.65.0/go.mod h1:0gZns+BLRQ3V6NdaerOhMbwwRbNh9hkGINtQAsP5GS8= github.com/prometheus/procfs v0.17.0 h1:FuLQ+05u4ZI+SS/w9+BWEM2TXiHKsUQ9TADiRH7DuK0= github.com/prometheus/procfs v0.17.0/go.mod h1:oPQLaDAMRbA+u8H5Pbfq+dl3VDAvHxMUOVhe0wYB2zw= github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII= github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o= github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU= github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4= github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ= github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= github.com/sirupsen/logrus v1.0.6/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc= github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/spiffe/go-spiffe/v2 v2.5.0 h1:N2I01KCUkv1FAjZXJMwh95KK1ZIQLYbPfhaxw8WS0hE= github.com/spiffe/go-spiffe/v2 v2.5.0/go.mod h1:P+NxobPc6wXhVtINNtFjNWGBTreew1GBUCwT2wPmb7g= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/tinode/jsonco v1.0.0 h1:zVcpjzDvjuA1G+HLrckI5EiiRyq9jgV3x37OQl6e5FE= github.com/tinode/jsonco v1.0.0/go.mod h1:Bnavu3302Qfn2pILMNwASkelodgeew3IvDrbdzU84u8= github.com/tinode/snowflake v1.0.0 h1:YciQ9ZKn1TrnvpS8yZErt044XJaxWVtR9aMO9rOZVOE= github.com/tinode/snowflake v1.0.0/go.mod h1:5JiaCe3o7QdDeyRcAeZBGVghwRS+ygt2CF/hxmAoptQ= github.com/twilio/twilio-go v1.26.5 h1:K105kKOyoulPsW1uB6lPrjGf+j5rAEGgDh1ZXtqznWc= github.com/twilio/twilio-go v1.26.5/go.mod h1:FpgNWMoD8CFnmukpKq9RNpUSGXC0BwnbeKZj2YHlIkw= github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c= github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY= github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4= github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8= github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM= github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 h1:ilQV1hzziu+LLM3zUTJ0trRztfwgjqKnBWNtSRkbmwM= github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78/go.mod h1:aL8wCCfTfSfmXjznFBSZNN13rSJjlIOI1fUNAtF7rmI= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/zeebo/errs v1.4.0 h1:XNdoD/RRMKP7HD0UhJnIzUy74ISdGGxURlYG8HSWSfM= github.com/zeebo/errs v1.4.0/go.mod h1:sgbWHsvVuTPHcqJJGQ1WhI5KbWlHYz+2+2C/LSEtCw4= github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q= go.mongodb.org/mongo-driver v1.17.4 h1:jUorfmVzljjr0FLzYQsGP8cgN/qzzxlY9Vh0C9KFXVw= go.mongodb.org/mongo-driver v1.17.4/go.mod h1:Hy04i7O2kC4RS06ZrhPRqj/u4DTYkFDAAccj+rVKqgQ= go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA= go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A= go.opentelemetry.io/contrib/detectors/gcp v1.37.0 h1:B+WbN9RPsvobe6q4vP6KgM8/9plR/HNjgGBrfcOlweA= go.opentelemetry.io/contrib/detectors/gcp v1.37.0/go.mod h1:K5zQ3TT7p2ru9Qkzk0bKtCql0RGkPj9pRjpXgZJZ+rU= go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.62.0 h1:rbRJ8BBoVMsQShESYZ0FkvcITu8X8QNwJogcLUmDNNw= go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.62.0/go.mod h1:ru6KHrNtNHxM4nD/vd6QrLVWgKhxPYgblq4VAtNawTQ= go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.62.0 h1:Hf9xI/XLML9ElpiHVDNwvqI0hIFlzV8dgIr35kV1kRU= go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.62.0/go.mod h1:NfchwuyNoMcZ5MLHwPrODwUF1HWCXWrL31s8gSAdIKY= go.opentelemetry.io/otel v1.37.0 h1:9zhNfelUvx0KBfu/gb+ZgeAfAgtWrfHJZcAqFC228wQ= go.opentelemetry.io/otel v1.37.0/go.mod h1:ehE/umFRLnuLa/vSccNq9oS1ErUlkkK71gMcN34UG8I= go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.36.0 h1:rixTyDGXFxRy1xzhKrotaHy3/KXdPhlWARrCgK+eqUY= go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.36.0/go.mod h1:dowW6UsM9MKbJq5JTz2AMVp3/5iW5I/TStsk8S+CfHw= go.opentelemetry.io/otel/metric v1.37.0 h1:mvwbQS5m0tbmqML4NqK+e3aDiO02vsf/WgbsdpcPoZE= go.opentelemetry.io/otel/metric v1.37.0/go.mod h1:04wGrZurHYKOc+RKeye86GwKiTb9FKm1WHtO+4EVr2E= go.opentelemetry.io/otel/sdk v1.37.0 h1:ItB0QUqnjesGRvNcmAcU0LyvkVyGJ2xftD29bWdDvKI= go.opentelemetry.io/otel/sdk v1.37.0/go.mod h1:VredYzxUvuo2q3WRcDnKDjbdvmO0sCzOvVAiY+yUkAg= go.opentelemetry.io/otel/sdk/metric v1.37.0 h1:90lI228XrB9jCMuSdA0673aubgRobVZFhbjxHHspCPc= go.opentelemetry.io/otel/sdk/metric v1.37.0/go.mod h1:cNen4ZWfiD37l5NhS+Keb5RXVWZWpRE+9WyVCpbo5ps= go.opentelemetry.io/otel/trace v1.37.0 h1:HLdcFNbRQBE2imdSEgm/kwqmQj1Or1l/7bW6mxVK7z4= go.opentelemetry.io/otel/trace v1.37.0/go.mod h1:TlgrlQ+PtQO5XFerSPUYG0JSgGyryXewPGyayAWSBS0= go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4= go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU= go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA= go.uber.org/zap v1.9.1/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= go.uber.org/zap v1.13.0/go.mod h1:zwrFLgMcdUuIBviXEYEH1YKNaOBnKXsx2IPda5bBwHM= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190411191339-88737f569e3a/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE= golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20201203163018-be400aefbc4c/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= golang.org/x/crypto v0.20.0/go.mod h1:Xwo95rrVNIoSMx9wa1JroENMToLWn3RNVrTBpLHgZPQ= golang.org/x/crypto v0.45.0 h1:jMBrvKuj23MTlT0bQEOBcAE0mjg8mK9RXFhRH6nyF3Q= golang.org/x/crypto v0.45.0/go.mod h1:XTGrrkGJve7CYK7J8PEww4aY7gM3qMCElcJQ8n8JdX4= golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b h1:M2rDM6z3Fhozi9O7NWsxAkg/yqS/lQJ6PmkyIV3YP+o= golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b/go.mod h1:3//PLf8L/X+8b4vuAfHzxeRUl04Adcb341+IGKfnqS8= golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= golang.org/x/net v0.47.0 h1:Mx+4dIFzqraBXUugkia1OOvlD6LemFo1ALMHjrXDOhY= golang.org/x/net v0.47.0/go.mod h1:/jNxtkgq5yWUGYkaZGqo27cfGZ1c5Nen03aYrrKpVRU= golang.org/x/oauth2 v0.30.0 h1:dnDm7JmhM45NNpd8FDDeLhK6FwqbOf4MLCM9zb1BOHI= golang.org/x/oauth2 v0.30.0/go.mod h1:B++QgG3ZKulg6sRPGD/mqlHQs5rB3Ml9erfeDY7xKlU= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.18.0 h1:kr88TuHDroi+UVf+0hZnirlk8o8T+4MrK6mr60WkH/I= golang.org/x/sync v0.18.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.38.0 h1:3yZWxaJjBmCWXqhN1qh02AkOnCQ1poK6oF+a7xWL6Gc= golang.org/x/sys v0.38.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/text v0.31.0 h1:aC8ghyu4JhP8VojJ2lEHBnochRno1sgL6nEi9WGFGMM= golang.org/x/text v0.31.0/go.mod h1:tKRAlv61yKIjGGHX/4tP1LTbc13YSec1pxVEWXzfoeM= golang.org/x/time v0.12.0 h1:ScB/8o8olJvc+CQPWrK3fPZNfh7qgwCrY0zJmoEQLSE= golang.org/x/time v0.12.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190425163242-31fd60d6bfdc/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= golang.org/x/tools v0.0.0-20190823170909-c4a336ef6a2f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20200103221440-774c71fcf114/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/api v0.241.0 h1:QKwqWQlkc6O895LchPEDUSYr22Xp3NCxpQRiWTB6avE= google.golang.org/api v0.241.0/go.mod h1:cOVEm2TpdAGHL2z+UwyS+kmlGr3bVWQQ6sYEqkKje50= google.golang.org/appengine v1.6.8 h1:IhEN5q69dyKagZPYMSdIjS2HqprW324FRQZJcGqPAsM= google.golang.org/appengine v1.6.8/go.mod h1:1jJ3jBArFh5pcgW8gCtRJnepW8FzD1V44FJffLiz/Ds= google.golang.org/genproto v0.0.0-20250707201910-8d1bb00bc6a7 h1:FGOcxvKlJgRBVbXeugjljCfCgfKWhC42FBoYmTCWVBs= google.golang.org/genproto v0.0.0-20250707201910-8d1bb00bc6a7/go.mod h1:249YoW4b1INqFTEop2T4aJgiO7UBYJrpejsaLvjWfI8= google.golang.org/genproto/googleapis/api v0.0.0-20250707201910-8d1bb00bc6a7 h1:FiusG7LWj+4byqhbvmB+Q93B/mOxJLN2DTozDuZm4EU= google.golang.org/genproto/googleapis/api v0.0.0-20250707201910-8d1bb00bc6a7/go.mod h1:kXqgZtrWaf6qS3jZOCnCH7WYfrvFjkC51bM8fz3RsCA= google.golang.org/genproto/googleapis/rpc v0.0.0-20250707201910-8d1bb00bc6a7 h1:pFyd6EwwL2TqFf8emdthzeX+gZE1ElRq3iM8pui4KBY= google.golang.org/genproto/googleapis/rpc v0.0.0-20250707201910-8d1bb00bc6a7/go.mod h1:qQ0YXyHHx3XkvlzUtpXDkS29lDSafHMZBAZDc03LQ3A= google.golang.org/grpc v1.73.0 h1:VIWSmpI2MegBtTuFt5/JWy2oXxtjJ/e89Z70ImfD2ok= google.golang.org/grpc v1.73.0/go.mod h1:50sbHOUqWoCQGI8V2HQLJM0B+LMlIUjNSZmow7EVBQc= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY= google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY= gopkg.in/airbrake/gobrake.v2 v2.0.9/go.mod h1:/h5ZAUhDkGaJfjzjKLSjv6zCL6O0LLBxU4K+aSYdM/U= gopkg.in/cenkalti/backoff.v2 v2.2.1 h1:eJ9UAg01/HIHG987TwxvnzK2MgxXq97YY6rYDpY9aII= gopkg.in/cenkalti/backoff.v2 v2.2.1/go.mod h1:S0QdOvT2AlerfSBkp0O+dk+bbIMaNbEmVk876gPCthU= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2/go.mod h1:Xk6kEKp8OKb+X14hQBKWaSkCsqBpgog8nAV2xsGOxlo= gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:aPpfJ7XW+gOuirDoZ8gHhLh3kZ1B08FtV2bbmy7Jv3s= gopkg.in/rethinkdb/rethinkdb-go.v6 v6.2.2 h1:tczPZjdz6soV2thcuq1IFOuNLrBUGonFyUXBbIWXWis= gopkg.in/rethinkdb/rethinkdb-go.v6 v6.2.2/go.mod h1:c7Wo0IjB7JL9B9Avv0UZKorYJCUhiergpj3u1WtGT1E= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= ================================================ FILE: keygen/README.md ================================================ # keygen: API key generator A command-line utility to generate an API key for [Tinode server](../server/) **Parameters:** * `sequence`: Sequential number of the API key. This value can be used to reject previously issued keys. * `isroot`: Currently unused. Intended to designate key of a system administrator. * `validate`: Key to validate: check previously issued key for validity. * `salt`: [HMAC](https://en.wikipedia.org/wiki/HMAC) salt, 32 random bytes base64 standard encoded; must be present for key validation; optional when generating the key: if missing, a cryptographically-strong salt will be automatically generated. ## Usage The API key is used to provide some protection from automatic scraping of server API and for identification of client applications. * `API key` is used on the client side. * `HMAC salt` is used on the server side to verify the API key. Run the generator: ```sh ./keygen ``` Sample output: ```text API key v1 seq1 [ordinary]: AQAAAAABAACGOIyP2vh5avSff5oVvMpk HMAC salt: TC0Jzr8f28kAspXrb4UYccJUJ63b7CSA16n1qMxxGpw= ``` Copy `HMAC salt` to `api_key_salt` parameter in your server [config file](https://github.com/tinode/chat/blob/master/server/tinode.conf). Copy `API key` to the client applications: * TinodeWeb: `API_KEY` in [config.js](https://github.com/tinode/webapp/blob/master/src/config.js) * Tindroid: `API_KEY` in [Cache.java](https://github.com/tinode/tindroid/blob/master/app/src/main/java/co/tinode/tindroid/Cache.java) * Tinodious: `kApiKey` in [SharedUtils.swift](https://github.com/tinode/ios/blob/master/TinodiosDB/SharedUtils.swift) Rebuild the clients after changing the API key. ================================================ FILE: keygen/keygen.go ================================================ package main import ( "bytes" "crypto/hmac" "crypto/md5" "crypto/rand" "encoding/base64" "encoding/binary" "flag" "fmt" "log" "os" ) // Generate API key // Composition: // // [1:algorithm version][4:deprecated (used to be application ID)][2:key sequence][1:isRoot][16:signature] = 24 bytes // // convertible to base64 without padding. // All integers are little-endian. func main() { version := flag.Int("sequence", 1, "Sequential number of the API key") isRoot := flag.Int("isroot", 0, "Is this a root API key?") apikey := flag.String("validate", "", "API key to validate") hmacSalt := flag.String("salt", "", "HMAC salt, 32 random bytes base64-encoded") flag.Parse() if *apikey != "" { if *hmacSalt == "" { log.Println("Error: must provide HMAC salt for key validation") os.Exit(1) } os.Exit(validate(*apikey, *hmacSalt)) } else { os.Exit(generate(*version, *isRoot, *hmacSalt)) } } const ( // APIKEY_VERSION is algorithm version. APIKEY_VERSION = 1 // APIKEY_APPID is deprecated. APIKEY_APPID = 4 // APIKEY_SEQUENCE key serial number. APIKEY_SEQUENCE = 2 // APIKEY_WHO is a Root user designator. APIKEY_WHO = 1 // APIKEY_SIGNATURE is cryptographic signature. APIKEY_SIGNATURE = 16 // APIKEY_LENGTH is total length of the key. APIKEY_LENGTH = APIKEY_VERSION + APIKEY_APPID + APIKEY_SEQUENCE + APIKEY_WHO + APIKEY_SIGNATURE ) func generate(sequence, isRoot int, hmacSaltB64 string) int { var data [APIKEY_LENGTH]byte var hmacSalt []byte if hmacSaltB64 == "" { hmacSalt = make([]byte, 32) _, err := rand.Read(hmacSalt) if err != nil { log.Println("Error: Failed to generate HMAC salt", err) return 1 } } else { var err error hmacSalt, err = base64.URLEncoding.DecodeString(hmacSaltB64) if err != nil { // Try standard base64 decoding hmacSalt, err = base64.StdEncoding.DecodeString(hmacSaltB64) } if err != nil { log.Println("Error: Failed to decode HMAC salt", err) return 1 } } // Make sure the salt is base64std encoded: tinode.conf requires std encoding. hmacSaltB64 = base64.StdEncoding.EncodeToString(hmacSalt) // [1:algorithm version][4:appid][2:key sequence][1:isRoot] data[0] = 1 // default algorithm // deprecated binary.LittleEndian.PutUint32(data[APIKEY_VERSION:], uint32(0)) binary.LittleEndian.PutUint16(data[APIKEY_VERSION+APIKEY_APPID:], uint16(sequence)) data[APIKEY_VERSION+APIKEY_APPID+APIKEY_SEQUENCE] = uint8(isRoot) hasher := hmac.New(md5.New, hmacSalt) hasher.Write(data[:APIKEY_VERSION+APIKEY_APPID+APIKEY_SEQUENCE+APIKEY_WHO]) signature := hasher.Sum(nil) copy(data[APIKEY_VERSION+APIKEY_APPID+APIKEY_SEQUENCE+APIKEY_WHO:], signature) var strIsRoot string if isRoot == 1 { strIsRoot = "ROOT" } else { strIsRoot = "ordinary" } fmt.Printf("API key v%d seq%d [%s]: %s\nHMAC salt: %s\n", 1, sequence, strIsRoot, base64.URLEncoding.EncodeToString(data[:]), hmacSaltB64) return 0 } func validate(apikey string, hmacSaltB64 string) int { var version uint8 var deprecated uint32 var sequence uint16 var isRoot uint8 var strIsRoot string hmacSalt, err := base64.URLEncoding.DecodeString(hmacSaltB64) if err != nil { // Try standard base64 decoding. hmacSalt, err = base64.StdEncoding.DecodeString(hmacSaltB64) } if err != nil { log.Println("Error: Failed to decode HMAC salt", err) return 1 } if declen := base64.URLEncoding.DecodedLen(len(apikey)); declen != APIKEY_LENGTH { log.Printf("Error: Invalid key length %d, expecting %d", declen, APIKEY_LENGTH) return 1 } data, err := base64.URLEncoding.DecodeString(apikey) if err != nil { log.Println("Error: Failed to decode key as base64-URL-encoded", err) return 1 } buf := bytes.NewReader(data) binary.Read(buf, binary.LittleEndian, &version) if version != 1 { log.Println("Error: Unknown signature algorithm ", version) return 1 } hasher := hmac.New(md5.New, hmacSalt) hasher.Write(data[:APIKEY_VERSION+APIKEY_APPID+APIKEY_SEQUENCE+APIKEY_WHO]) if signature := hasher.Sum(nil); !bytes.Equal(data[APIKEY_VERSION+APIKEY_APPID+APIKEY_SEQUENCE+APIKEY_WHO:], signature) { log.Println("Error: Invalid signature ", data, signature) return 1 } // [1:algorithm version][4:deprecated][2:key sequence][1:isRoot] binary.Read(buf, binary.LittleEndian, &deprecated) binary.Read(buf, binary.LittleEndian, &sequence) binary.Read(buf, binary.LittleEndian, &isRoot) if isRoot == 1 { strIsRoot = "ROOT" } else { strIsRoot = "ordinary" } fmt.Printf("Valid v%d seq%d, [%s]\n", version, sequence, strIsRoot) return 0 } ================================================ FILE: loadtest/LICENSE ================================================ Code in this folder is licensed under Apache 2.0 http://www.apache.org/licenses/LICENSE-2.0 ================================================ FILE: loadtest/README.md ================================================ # Tinode Load Testing Content of this directory is for running rudimentary load tests of Tinode server. You need this only if you want to run your own load tests. ## Tsung The `tsung.xml` is a configuration for [Tsung](http://tsung.erlang-projects.org/). The `tinode.beam` is an erlang binary required by the test to generate base64-encoded user-password pairs. The `tinode.erl` is the source for `tinode.beam` (`erlc tinode.erl` -> `tinode.beam`). [Install Tsung](http://tsung.erlang-projects.org/user_manual/installation.html), then run the test ``` tsung -f ./tsung.xml start ``` ## Gatling A similar loadtest scenario is also available in Gatling. The configuration file is `loadtest.scala`. Run it with (after [installing Gatling](https://gatling.io/docs/current/installation/)): ``` gatling.sh -sf . -rsf . -rd "na" -s tinode.Loadtest ``` Currently, three tests are available: * `tinode.Loadtest`: after connecting to server, retrieves user's subscriptions, and publishes a few messages to them one by one. * `tinode.MeLoadtest`: attempts to max out `me` topic connections. * `tinode.SingleTopicLoadtest`: connects to and publishes messages to the specified topic (typically, a group topic). The script supports passing params via the `JAVA_OPTS` envvar. Parameter name | Default value | Description -------------- | ------------- | ------------- `num_sessions` | 10000 | Total number of sessions to connect to the server `ramp` | 300 | Time period in seconds over which to ramp up the load (`0` to `num_sessions`). `publish_count` | 10 | Number of messages that a user will publish to a topic it subscribes to. `publish_interval` | 100 | Maximum period of time a user will wait between publishing subsequent messages to a topic. `accounts` | users.csv | `tinode.Loadtest` and `tinode.SingleTopicLoadtest` only: Path to CSV file containing user accounts to use in loadtest (in format `username,password[,token]` (`token` field is optional). `topic` | | `tinode.SingleTopicLoadtest` only: topic name to send load to. `username` | | `tinode.MeLoadtest` only: user to subscribe to `me` topic. `password` | | `tinode.MeLoadtest` only: user password. Examples: ```shell JAVA_OPTS="-Daccounts=users.csv -Dnum_sessions=100 -Dramp=10" gatling.sh -sf . -rsf . -rd "na" -s tinode.Loadtest ``` Ramps up load to 100 sessions listed in `users.csv` file over 10 seconds. ```shell JAVA_OPTS="-Dusername=user1 -Dpassword=user1123 -Dnum_sessions=10000 -Dramp=600" gatling.sh -sf . -rsf . -rd "na" -s tinode.MeLoadtest ``` Connects 10000 sessions to `me` topic for `user1` with password `user1123` over 600 seconds. ```shell JAVA_OPTS="-Dtopic=grpYOrcDwORhPg -Daccounts=users.csv -Dnum_sessions=10000 -Dramp=1000 -Dpublish_count=2 -Dpublish_interval=300" gatling.sh -sf . -rsf . -rd "na" -s tinode.SingleTopicLoadtest ``` Connects 10000 users (specified in `users.csv` file) to `grpYOrcDwORhPg` topic over 1000 seconds. Each user will publish 2 messages with interval up to 300 seconds. This will be eventually packaged into a docker container. ### Experiments We have tested our single-server Tinode synthetic setup with 50000 accounts on a standard `t3.xlarge` AWS box (4 vCPUs, 16GiB, 5Gbps network) with the `mysql` backend. As the load increases, before starting to drop: * The server can sustain 50000 concurrently connected sessions. * An individual group topic was able to sustain 1500 concurrent sessions. ================================================ FILE: loadtest/loadtest.scala ================================================ package tinode import java.util.Base64 import java.util.concurrent.ConcurrentHashMap import scala.collection.JavaConverters._ import scala.collection._ import scala.concurrent.duration._ import io.gatling.core.Predef._ import io.gatling.http.Predef._ class Loadtest extends TinodeBase { // Input file can be set with the "accounts" java option. // E.g. JAVA_OPTS="-Daccounts=/tmp/z.csv" gatling.sh -sf . -rsf . -rd "na" -s tinode.Loadtest val feeder = csv(System.getProperty("accounts", "users.csv")).random val scn = scenario("WebSocket") .exec(ws("Connect WS").connect("/v0/channels?apikey=AQEAAAABAAD_rAp4DJh05a1HAwFT3A6K")) .exec(session => session.set("id", "tn-" + session.userId)) .pause(1) .exec(hello) .pause(1) .feed(feeder) .doIfOrElse({session => val uname = session("username").as[String] var token = session("token").asOption[String] if (token == None) { token = tokenCache.get(uname) } token == None }) { loginBasic } { loginToken } .exitHereIfFailed .exec(subMe) .exitHereIfFailed .exec(getSubs) .exitHereIfFailed .doIf({session => session.attributes.contains("subs") }) { exec { session => // Shuffle subscriptions. val subs = session("subs").as[Vector[String]] val shuffled = scala.util.Random.shuffle(subs.toList) session.set("subs", shuffled) } .foreach("${subs}", "sub") { exec(subTopic) .exitHereIfFailed .pause(0, 2) .doIfOrElse({session => val topic = session("sub").as[String] !topic.startsWith("chn") }) { publish } { pause(5) } .exec(leaveTopic) .pause(0, 3) } } .exec(ws("close-ws").close) setUp(scn.inject(rampUsers(numSessions) during (rampPeriod.seconds))).protocols(httpProtocol) } class MeLoadtest extends TinodeBase { val username = System.getProperty("username", "user0") val password = System.getProperty("password", "user0123") val scn = scenario("WebSocket") .exec(ws("Connect WS").connect("/v0/channels?apikey=AQEAAAABAAD_rAp4DJh05a1HAwFT3A6K")) .exec(session => session.set("id", "tn-" + session.userId)) .exec(session => session.set("username", username)) .exec(session => session.set("password", password)) .pause(1) .exec(hello) .pause(1) .doIfOrElse({session => val uname = session("username").as[String] val token = tokenCache.get(username) token == None }) { loginBasic } { loginToken } .exitHereIfFailed .exec(subMe) .exitHereIfFailed .exec(getSubs) .exitHereIfFailed .pause(1000) .exec(ws("close-ws").close) setUp(scn.inject(rampUsers(numSessions) during (rampPeriod.seconds))).protocols(httpProtocol) } class SingleTopicLoadtest extends TinodeBase { // Input file can be set with the "accounts" java option. // E.g. JAVA_OPTS="-Daccounts=/tmp/z.csv" gatling.sh -sf . -rsf . -rd "na" -s tinode.Loadtest val feeder = csv(System.getProperty("accounts", "users.csv")).random val topic = System.getProperty("topic", "TOPIC_NAME") val scn = scenario("WebSocket") .exec(ws("Connect WS").connect("/v0/channels?apikey=AQEAAAABAAD_rAp4DJh05a1HAwFT3A6K")) .exec(session => session.set("id", "tn-" + session.userId)) .pause(1) .exec(hello) .pause(1) .feed(feeder) .doIfOrElse({session => val uname = session("username").as[String] var token = session("token").asOption[String] if (token == None) { token = tokenCache.get(uname) } token == None }) { loginBasic } { loginToken } .exitHereIfFailed .exec(subMe) .exitHereIfFailed .exec(getSubs) .exitHereIfFailed .doIf({session => session.attributes.contains("subs") }) { exec(session => session.set("sub", topic)) .exec(subTopic) .exitHereIfFailed .pause(0, 10) .exec(publish) .pause(15) .exec(leaveTopic) .pause(0, 3) } .exec(ws("close-ws").close) setUp(scn.inject(rampUsers(numSessions) during (rampPeriod.seconds))).protocols(httpProtocol) } ================================================ FILE: loadtest/tinode.erl ================================================ %% Support module for Tinode load testing with Tsung. %% Compile using erlc then copy resulting .beam to %% /usr/local/lib/erlang/lib/tsung-1.7.0/ebin/ %% Alternatively you can just leave it in the current %% directory. -module(tinode). -export([rand_user_secret/1, shuffle/1, cache_token/2, read_token/1]). %% Produces a secret for use in basic login. rand_user_secret({Pid, DynData}) -> base64:encode_to_string(get_rand_secret()). %% Unexported. Picks a random user from a pre-defined list. get_rand_secret() -> case rand:uniform(6) of 1 -> "alice:alice123"; 2 -> "bob:bob123"; 3 -> "carol:carol123"; 4 -> "dave:dave123"; 5 -> "eve:eve123"; 6 -> "frank:frank123" end. %% Shuffles a list randomly. shuffle(L) -> RandomList=[{rand:uniform(), X} || X <- L], [X || {_,X} <- lists:sort(RandomList)]. %% Reads previously cached auth token for the specified user. read_token(Uid) -> {ok, LogDir} = application:get_env(tsung_controller, log_dir_real), case file:read_file(filename:join(LogDir, Uid)) of {ok, Data} -> string:trim(Data); {error, _} -> "" end. %% Saves auth token for the specified user in the log directory. cache_token(Uid, Token) -> {ok, LogDir} = application:get_env(tsung_controller, log_dir_real), {ok, File} = file:open(filename:join(LogDir, Uid), [write]), file:write(File, Token), file:close(File), ok. ================================================ FILE: loadtest/tinode.scala ================================================ package tinode import java.util.Base64 import java.util.concurrent.ConcurrentHashMap import scala.collection.JavaConverters._ import scala.collection._ import scala.concurrent.duration._ import io.gatling.core.Predef._ import io.gatling.http.Predef._ class TinodeBase extends Simulation { val httpProtocol = http .baseUrl("http://localhost:6060") .wsBaseUrl("ws://localhost:6060") // Auth tokens to share between sessions. val tokenCache : concurrent.Map[String, String] = new ConcurrentHashMap() asScala // Total number of messages to publish to a topic. val publishCount = Integer.getInteger("publish_count", 10).toInt // Maximum interval between publishing messages to a topic. val publishInterval = Integer.getInteger("publish_interval", 100).toInt // Total number of sessions. val numSessions = Integer.getInteger("num_sessions", 10000) // Ramp up period (0 to numSessions) in seconds. val rampPeriod = java.lang.Long.getLong("ramp", 300L) val hello = exitBlockOnFail { exec { ws("hi").sendText( """{"hi":{"id":"afabb3","ver":"0.22.8","ua":"Gatling-Loadtest/1.0; gatling/1.7.0"}}""" ) .await(15 seconds)( ws.checkTextMessage("hi") .matching(jsonPath("$.ctrl").find.exists) ) } } val loginBasic = exitBlockOnFail { exec { session => val uname = session("username").as[String] val password = session("password").as[String] val secret = new String(java.util.Base64.getEncoder.encode((uname + ":" + password).getBytes())) session.set("secret", secret) } .exec { ws("login").sendText( """{"login":{"id":"${id}-login","scheme":"basic","secret":"${secret}"}}""" ) .await(15 seconds)( ws.checkTextMessage("login-ctrl") .matching(jsonPath("$.ctrl").find.exists) .check(jsonPath("$.ctrl.params.token").saveAs("token")) ) } .exec { session => val uname = session("username").as[String] val token = session("token").as[String] tokenCache.put(uname, token) session } } val loginToken = exitBlockOnFail { exec { session => val uname = session("username").as[String] var token = session("token").asOption[String] if (token == None) { token = tokenCache.get(uname) } session.set("token", token.getOrElse("")) } .exec { ws("login-token").sendText( """{"login":{"id":"${id}-login2","scheme":"token","secret":"${token}"}}""" ) .await(15 seconds)( ws.checkTextMessage("login-ctrl") .matching(jsonPath("$.ctrl").find.exists) ) } } val subMe = exitBlockOnFail { exec { ws("sub-me").sendText( """{"sub":{"id":"${id}-sub-me","topic":"me","get":{"what":"desc"}}}""" ) .await(15 seconds)( ws.checkTextMessage("sub-me-desc") .matching(jsonPath("$.ctrl").find.exists) .check(jsonPath("$.ctrl.code").ofType[Int].in(200 to 299)) ) } } val subTopic = exitBlockOnFail { exec { ws("sub-topic").sendText( """{"sub":{"id":"${id}-sub-${sub}","topic":"${sub}","get":{"what":"desc sub data del"}}}""" ) .await(15 seconds)( ws.checkTextMessage("sub-topic-ctrl") .matching(jsonPath("$.ctrl").find.exists) .check(jsonPath("$.ctrl.code").ofType[Int].in(200 to 299)) ) } } val publish = exitBlockOnFail { exec { repeat(publishCount, "i") { exec { ws("pub-topic").sendText( """{"pub":{"id":"${id}-pub-${sub}-${i}","topic":"${sub}","content":"This is a Gatling test ${i}"}}""" ) .await(15 seconds)( ws.checkTextMessage("pub-topic-ctrl") .matching(jsonPath("$.ctrl").find.exists) .check(jsonPath("$.ctrl.code").ofType[Int].in(200 to 299)) ) } .pause(0, publishInterval) } } } val getSubs = exitBlockOnFail { exec { ws("get-subs").sendText( """{"get":{"id":"${id}-get-subs","topic":"me","what":"sub"}}""" ) .await(15 seconds)( ws.checkTextMessage("save-subs") .matching(jsonPath("$.meta.sub").find.exists) .check(jsonPath("$.meta.sub[*].topic").findAll.saveAs("subs")) ) } } val leaveTopic = exitBlockOnFail { exec { ws("leave-topic").sendText( """{"leave":{"id":"${id}-leave-${sub}","topic":"${sub}"}}""" ) .await(15 seconds)( ws.checkTextMessage("sub-topic-ctrl") .matching(jsonPath("$.ctrl").find.exists) ) } } } ================================================ FILE: loadtest/tsung.xml ================================================ {"hi":{"id":"%%_baseid%%01","ver":"0.15","ua":"Tsung-Loadtest/1.0; tsung/1.7.0"}} {"ctrl":.*"code":200.*} {"login":{"id":"%%_baseid%%02","scheme":"token","secret":"%%_token%%"}} {"ctrl":.*"code":200.*} {"login":{"id":"%%_baseid%%02","scheme":"basic","secret":"%%_secret%%"}} {"sub":{"id":"%%_baseid%%03","topic":"me","get":{"what":"desc"}}} {"get":{"id":"%%_baseid%%04","topic":"me","what":"sub"}} {"sub":{"id":"%%_baseid%%%%_topicx%%%%_ctr%%","topic":"%%_topicx%%","get":{"what":"desc sub data del"}}} {"pub":{"id":"%%_baseid%%%%_topicx%%%%_ctr%%%%_counter%%","topic":"%%_topicx%%","content":"This is a Tsung test %%_baseid%% %%_counter%%"}} {"leave":{"id":"%%_baseid%%%%_topicx%%%%_ctr%%","topic":"%%_topicx%%"}} ================================================ FILE: loadtest/users.csv ================================================ username,password alice,alice123 bob,bob123 carol,carol123 dave,dave123 eve,eve123 frank,frank123 ================================================ FILE: monitoring/LICENSE ================================================ Code in this folder and nested folders is licensed under Apache 2.0 http://www.apache.org/licenses/LICENSE-2.0 ================================================ FILE: monitoring/README.md ================================================ # Monitoring Support This directory contains code related to monitoring Tinode server. Supported monitoring services are * [Prometheus](https://prometheus.io/) * [InfluxDB](https://www.influxdata.com/) See [exporter/README](./exporter/README.md) for more details. ================================================ FILE: monitoring/exporter/README.md ================================================ # Tinode Metric Exporter This is a simple service which reads JSON monitoring data exposed by Tinode server using [expvar](https://golang.org/pkg/expvar/) and re-publishes it in other formats. Currently the supported formats are: * [InfluxDB](https://www.influxdata.com/) [exporter](https://docs.influxdata.com/influxdb/v1.7/tools/api/#write-http-endpoint) **pushes** data to its target backend. This is the default mode. * [Prometheus](https://prometheus.io/) [exporter](https://prometheus.io/docs/instrumenting/exporters/) which exports data in [prometheus format](https://prometheus.io/docs/concepts/data_model/). The Prometheus monitoring service is expected to **pull/scrape** data from the exporter. ## Usage Exporters are intended to run next to (pair with) Tinode servers: one Exporter per one Tinode server, i.e. a single Exporter provides metrics from a single Tinode server. ## Configuration The exporters are configured by command-line flags: ### Common flags * `serve_for` specifies which monitoring service the Exporter will gather metrics for; accepted values: `influxdb`, `prometheus`; default: `influxdb`. * `tinode_addr` is the address where the Tinode instance publishes `expvar` data to scrape; default: `http://localhost:6060/stats/expvar`. * `listen_at` is the hostname to bind to for serving the metrics; default: `:6222`. * `instance` is the Exporter instance name (it may be exported to the upstream backend); default: `exporter`. * `metric_list` is a comma-separated list of metrics to export; default: `Version,LiveTopics,TotalTopics,LiveSessions,ClusterLeader,TotalClusterNodes,LiveClusterNodes,memstats.Alloc`. ### InfluxDB * `influx_push_addr` is the address of InfluxDB target server where the data gets sent; default: `http://localhost:9999/write`. * `influx_db_version` is the version of InfluxDB (only 1.7 and 2.0 are supported); default: `1.7`. * `influx_organization` specifies InfluxDB organization to push metrics as; default: `test`; * `influx_bucket` is the name of InfluxDB storage bucket to store data in (used only in InfluxDB 2.0); default: `test`. * `influx_auth_token` - InfluxDB authentication token; no default value. * `influx_push_interval` - InfluxDB push interval in seconds; default: `30`. #### Example Run InfluxDB Exporter as ``` ./exporter \ --serve_for=influxdb \ --tinode_addr=http://localhost:6060/stats/expvar \ --listen_at=:6222 \ --instance=exp-0 \ --influx_push_addr=http://my-influxdb-backend.net/write \ --influx_db_version=1.7 \ --influx_organization=myOrg \ --influx_auth_token=myAuthToken123 \ --influx_push_interval=30 ``` This exporter will push the collected metrics to the specified backend once every 30 seconds. ### Prometheus * `prom_namespace` is a prefix to use for metrics names. If you are monitoring multiple tinode instances you may want to use different namespaces; default: `tinode`. * `prom_metrics_path` is the path under which to expose the metrics for scraping; default: `/metrics`. * `prom_timeout` is the Tinode connection timeout in seconds in response to Prometheus scrapes; default: `15`. #### Example Run Prometheus Exporter as ``` ./exporter \ --serve_for=prometheus \ --tinode_addr=http://localhost:6060/stats/expvar \ --listen_at=:6222 \ --instance=exp-0 \ --prom_namespace=tinode \ --prom_metrics_path=/metrics \ --prom_timeout=15 ``` This exporter will serve data at path /metrics, on port 6222. Once running, configure your Prometheus monitoring installation to collect data from this exporter. ================================================ FILE: monitoring/exporter/build.sh ================================================ #!/bin/bash # This scripts build and archives binaries and supporting files. # Supported OSs: mac (darwin), windows, linux. goplat=( darwin darwin windows linux ) # CPUs architectures: amd64 and arm64. The same order as OSs. goarc=( amd64 arm64 amd64 amd64 ) # Number of platform+architectures. buildCount=${#goplat[@]} for line in $@; do eval "$line" done # Strip 'v' prefix as in v0.16.4 -> 0.16.4. version=${tag#?} if [ -z "$version" ]; then # Get last git tag as release version. Tag looks like 'v.1.2.3', so strip 'v'. version=`git describe --tags` version=${version#?} fi echo "Releasing exporter $version" GOSRC=${GOPATH}/src/github.com/tinode pushd ${GOSRC}/chat > /dev/null # Make sure earlier builds are deleted. rm -f ./releases/${version}/exporter* for (( i=0; i<${buildCount}; i++ )); do plat="${goplat[$i]}" arc="${goarc[$i]}" echo "Building ${plat}/${arc}..." # Remove possibly existing binaries from earlier builds. rm -f ./releases/tmp/exporter* # Environment to cros-compile for the platform. env GOOS="${plat}" GOARCH="${arc}" go build \ -ldflags "-s -w -X main.buildstamp=`git describe --tags`" \ -o ./releases/tmp/exporter ./monitoring/exporter > /dev/null # Build archive. All platforms but Windows use tar for archiving. Windows uses zip. if [ "$plat" = "windows" ]; then # Just copy the binary with .exe appended. cp ./releases/tmp/exporter ./releases/${version}/exporter."${plat}-${arc}".exe else plat2=$plat # Rename 'darwin' tp 'mac' if [ "$plat" = "darwin" ]; then plat2=mac fi # Just copy the binary. cp ./releases/tmp/exporter ./releases/${version}/exporter."${plat2}-${arc}" fi done popd > /dev/null ================================================ FILE: monitoring/exporter/influxdb_exporter.go ================================================ package main import ( "bytes" "fmt" "io" "log" "net/http" "net/url" "strings" "time" ) // InfluxDBExporter collects metrics from a Tinode server and pushes them to InfluxDB. type InfluxDBExporter struct { targetAddress string organization string bucket string tokenHeader string instance string scraper *Scraper } // NewInfluxDBExporter returns an initialized InfluxDB exporter. func NewInfluxDBExporter(influxDBVersion, pushBaseAddress, organization, bucket, token, instance string, scraper *Scraper) *InfluxDBExporter { targetAddress := formPushTargetAddress(influxDBVersion, pushBaseAddress, organization, bucket) tokenHeader := formAuthorizationHeaderValue(influxDBVersion, token) return &InfluxDBExporter{ targetAddress: targetAddress, organization: organization, bucket: bucket, tokenHeader: tokenHeader, instance: instance, scraper: scraper, } } // Push scrapes metrics from Tinode server and pushes these metrics to InfluxDB. func (e *InfluxDBExporter) Push() error { metrics, err := e.scraper.CollectRaw() if err != nil { return err } b := new(bytes.Buffer) ts := time.Now().UnixNano() for k, v := range metrics { switch val := v.(type) { case float64: fmt.Fprintf(b, "%s,instance=%s value=%f %d\n", k, e.instance, val, ts) case *histogram: fmt.Fprintf(b, "%s,instance=%s count=%d %d\n", k, e.instance, val.count, ts) fmt.Fprintf(b, "%s,instance=%s sum=%f %d\n", k, e.instance, val.sum, ts) for bucket, count := range val.buckets { fmt.Fprintf(b, "%s,instance=%s le=%f,value=%d %d\n", k, e.instance, bucket, count, ts) } default: log.Panicln("Invalid metric type: ", v) } } req, err := http.NewRequest("POST", e.targetAddress, b) if err != nil { return err } req.Header.Add("Authorization", e.tokenHeader) resp, err := http.DefaultClient.Do(req) if err != nil { return err } defer resp.Body.Close() if resp.StatusCode >= 400 { var body string if rb, err := io.ReadAll(resp.Body); err != nil { body = err.Error() } else { body = strings.TrimSpace(string(rb)) } return fmt.Errorf("HTTP %s: %s", resp.Status, body) } return nil } func formPushTargetAddress(influxDBVersion, baseAddr, organization, bucket string) string { url, err := url.ParseRequestURI(baseAddr) if err != nil { log.Fatal("Invalid push_addr", err) } // Url format // - in 2.0: /api/v2/write?org=organization&bucket=bucket // - in 1.7: /write?db=organization organizationParamName := "org" bucketParamName := "bucket" if influxDBVersion == "1.7" { organizationParamName = "db" // Concept of explicit bucket in 1.7 is absent. bucketParamName = "" } q := url.Query() q.Add(organizationParamName, organization) if bucketParamName != "" { q.Add(bucketParamName, bucket) } url.RawQuery = q.Encode() return url.String() } func formAuthorizationHeaderValue(influxDBVersion, token string) string { // Authorization header has value // - in 2.0: Token // - in 1.7: Bearer if influxDBVersion == "2.0" { return fmt.Sprintf("Token %s", token) } return fmt.Sprintf("Bearer %s", token) } ================================================ FILE: monitoring/exporter/main.go ================================================ package main import ( "flag" "fmt" "log" "net/http" "strings" "time" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/common/version" ) type monitoringService int const ( promService monitoringService = 1 influxService monitoringService = 2 ) const ( // Minimum interval between InfluxDB pushes in seconds. minPushInterval = 10 ) type promHTTPLogger struct{} func (l promHTTPLogger) Println(v ...interface{}) { log.Println(v...) } func parseMetricList(list string) []string { metrics := strings.Split(list, ",") for i, m := range metrics { metrics[i] = strings.TrimSpace(m) } return metrics } // Build version number defined by the compiler: // // -ldflags "-X main.buildstamp=value_to_assign_to_buildstamp" // // Reported to clients in response to {hi} message. // For instance, to define the buildstamp as a timestamp of when the server was built add a // flag to compiler command line: // // -ldflags "-X main.buildstamp=`date -u '+%Y%m%dT%H:%M:%SZ'`" // // or to set it to git tag: // // -ldflags "-X main.buildstamp=`git describe --tags`" var buildstamp = "undef" func main() { log.Printf("Tinode metrics exporter.") var ( serveFor = flag.String("serve_for", "influxdb", "Monitoring service to gather metrics for. Available: influxdb, prometheus.") tinodeAddr = flag.String("tinode_addr", "http://localhost:6060/stats/expvar", "Address of the Tinode instance to scrape.") listenAt = flag.String("listen_at", ":6222", "Host name and port to listen for incoming requests on.") metricList = flag.String("metric_list", "Version,LiveTopics,TotalTopics,LiveSessions,ClusterLeader,TotalClusterNodes,LiveClusterNodes,memstats.Alloc", "Comma-separated list of numeric metrics to scrape and export.") histoMetricList = flag.String("histo_metric_list", "RequestLatency,OutgoingMessageSize", "Comma-separated list of histogram metrics to scrape and export.") instance = flag.String("instance", "exporter", "Exporter instance name.") // Prometheus-specific arguments. promNamespace = flag.String("prom_namespace", "tinode", "Prometheus namespace for metrics '_...'") promMetricsPath = flag.String("prom_metrics_path", "/metrics", "Path under which to expose metrics for Prometheus scrapes.") promTimeout = flag.Int("prom_timeout", 15, "Tinode connection timeout in seconds in response to Prometheus scrapes.") // InfluxDB-specific arguments. influxPushAddr = flag.String("influx_push_addr", "http://localhost:9999/write", "Address of InfluxDB target server where the data gets sent.") influxDBVersion = flag.String("influx_db_version", "1.7", "Version of InfluxDB (only 1.7 and 2.0 are supported).") influxOrganization = flag.String("influx_organization", "test", "InfluxDB organization to push metrics as.") influxBucket = flag.String("influx_bucket", "test", "InfluxDB storage bucket to store data in (used only in InfluxDB 2.0).") influxAuthToken = flag.String("influx_auth_token", "", "InfluxDB authentication token.") influxPushInterval = flag.Int("influx_push_interval", 30, "InfluxDB push interval in seconds.") ) flag.Parse() var service monitoringService if *serveFor == "prometheus" { service = promService } else if *serveFor == "influxdb" { service = influxService } else { log.Fatal("Invalid monitoring service:" + *serveFor + "; must be either \"prometheus\" or \"influxdb\"") } // Validate flags. switch service { case promService: if *promMetricsPath == "/" { log.Fatal("Serving metrics from / is not supported") } case influxService: if *influxOrganization == "" { log.Fatal("Must specify --influx_organization") } if *influxAuthToken == "" { log.Fatal("Must specify --influx_auth_token") } if *influxBucket == "" { log.Fatal("Must specify --influx_bucket") } if *influxDBVersion != "1.7" && *influxDBVersion != "2.0" { log.Fatal("The --influx_db_version must be either 1.7 or 2.0") } if *influxPushInterval > 0 && *influxPushInterval < minPushInterval { *influxPushInterval = minPushInterval log.Println("The --influx_push_interval is too low, reset to", minPushInterval) } } // Index page at web root. http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { var servingPath string switch service { case promService: servingPath = "

Prometheus exporter path: Metrics

" case influxService: servingPath = "

InfluxDB push path: Push

" } w.Write([]byte(`Tinode Exporter

Tinode Exporter

Server type` + *serveFor + `

` + servingPath + `

Build

` + version.Info() + ` ` + version.BuildContext() + `
`)) }) metrics := parseMetricList(*metricList) histoMetrics := parseMetricList(*histoMetricList) scraper := Scraper{address: *tinodeAddr, simpleMetrics: metrics, histogramMetrics: histoMetrics} var serverTypeString string // Create exporters. switch service { case promService: serverTypeString = *serveFor promExporter := NewPromExporter(*tinodeAddr, *promNamespace, time.Duration(*promTimeout)*time.Second, &scraper) registry := prometheus.NewRegistry() registry.MustRegister(promExporter) http.Handle(*promMetricsPath, promhttp.InstrumentMetricHandler( registry, promhttp.HandlerFor( registry, promhttp.HandlerOpts{ ErrorLog: &promHTTPLogger{}, Timeout: time.Duration(*promTimeout) * time.Second, }, ), ), ) case influxService: serverTypeString = fmt.Sprintf("%s, version %s", *serveFor, *influxDBVersion) influxDBExporter := NewInfluxDBExporter(*influxDBVersion, *influxPushAddr, *influxOrganization, *influxBucket, *influxAuthToken, *instance, &scraper) if *influxPushInterval > 0 { go func() { interval := time.Duration(*influxPushInterval) * time.Second ch := time.Tick(interval) for { if _, ok := <-ch; ok { if err := influxDBExporter.Push(); err != nil { log.Println("InfluxDB push failed:", err) } } else { return } } }() } else { log.Println("InfluxDB push interval is zero. Will not push data automatically.") } // Forces a data push. http.HandleFunc("/push", func(w http.ResponseWriter, r *http.Request) { var msg string if err := influxDBExporter.Push(); err == nil { msg = "HTTP 200 OK" } else { msg = err.Error() } w.Write([]byte(`Tinode Push

Tinode Push

` + msg + `
`)) }) } log.Println("Reading Tinode expvar from", *tinodeAddr) log.Printf("Exporter running at %s. Server type %s", *listenAt, serverTypeString) log.Fatalln(http.ListenAndServe(*listenAt, nil)) } ================================================ FILE: monitoring/exporter/prom_exporter.go ================================================ package main import ( "log" "time" "github.com/prometheus/client_golang/prometheus" ) // PromExporter collects metrics in Prometheus format from a Tinode server. type PromExporter struct { address string timeout time.Duration namespace string scraper *Scraper up *prometheus.Desc version *prometheus.Desc topicsLive *prometheus.Desc topicsTotal *prometheus.Desc sessionsLive *prometheus.Desc sessionsTotal *prometheus.Desc numGoroutines *prometheus.Desc incomingMessagesWebsockTotal *prometheus.Desc outgoingMessagesWebsockTotal *prometheus.Desc incomingMessagesLongpollTotal *prometheus.Desc outgoingMessagesLongpollTotal *prometheus.Desc incomingMessagesGrpcTotal *prometheus.Desc outgoingMessagesGrpcTotal *prometheus.Desc fileDownloadsTotal *prometheus.Desc fileUploadsTotal *prometheus.Desc ctrlCodesTotal2xx *prometheus.Desc ctrlCodesTotal3xx *prometheus.Desc ctrlCodesTotal4xx *prometheus.Desc ctrlCodesTotal5xx *prometheus.Desc clusterLeader *prometheus.Desc clusterSize *prometheus.Desc clusterNodesLive *prometheus.Desc malloced *prometheus.Desc requestLatencyMsCount *prometheus.Desc outgoingMessageBytesCount *prometheus.Desc } // NewPromExporter returns an initialized Prometheus exporter. func NewPromExporter(server, namespace string, timeout time.Duration, scraper *Scraper) *PromExporter { return &PromExporter{ address: server, timeout: timeout, namespace: namespace, scraper: scraper, up: prometheus.NewDesc( prometheus.BuildFQName(namespace, "", "up"), "If tinode instance is reachable.", nil, nil, ), version: prometheus.NewDesc( prometheus.BuildFQName(namespace, "", "version"), "The version of this tinode instance.", nil, nil, ), topicsLive: prometheus.NewDesc( prometheus.BuildFQName(namespace, "", "topics_live_count"), "Number of currently active topics.", nil, nil, ), topicsTotal: prometheus.NewDesc( prometheus.BuildFQName(namespace, "", "topics_total"), "Total number of topics used during instance lifetime.", nil, nil, ), sessionsLive: prometheus.NewDesc( prometheus.BuildFQName(namespace, "", "sessions_live_count"), "Number of currently active sessions.", nil, nil, ), sessionsTotal: prometheus.NewDesc( prometheus.BuildFQName(namespace, "", "sessions_total"), "Total number of sessions since instance start.", nil, nil, ), numGoroutines: prometheus.NewDesc( prometheus.BuildFQName(namespace, "", "num_goroutines"), "Number of currently spawned goroutines.", nil, nil, ), incomingMessagesWebsockTotal: prometheus.NewDesc( prometheus.BuildFQName(namespace, "", "incoming_messages_websock_total"), "Total number of incoming messages via websocket.", nil, nil, ), outgoingMessagesWebsockTotal: prometheus.NewDesc( prometheus.BuildFQName(namespace, "", "outgoing_messages_websock_total"), "Total number of outgoiing messages via websocket.", nil, nil, ), incomingMessagesLongpollTotal: prometheus.NewDesc( prometheus.BuildFQName(namespace, "", "incoming_messages_longpoll_total"), "Total number of incoming messages via longpoll.", nil, nil, ), outgoingMessagesLongpollTotal: prometheus.NewDesc( prometheus.BuildFQName(namespace, "", "outgoing_messages_longpoll_total"), "Total number of outgoiing messages via longpoll.", nil, nil, ), incomingMessagesGrpcTotal: prometheus.NewDesc( prometheus.BuildFQName(namespace, "", "incoming_messages_grpc_total"), "Total number of incoming messages via grpc.", nil, nil, ), outgoingMessagesGrpcTotal: prometheus.NewDesc( prometheus.BuildFQName(namespace, "", "outgoing_messages_grpc_total"), "Total number of outgoiing messages via grpc.", nil, nil, ), fileDownloadsTotal: prometheus.NewDesc( prometheus.BuildFQName(namespace, "", "file_downloads_total"), "Total number of large file downloads.", nil, nil, ), fileUploadsTotal: prometheus.NewDesc( prometheus.BuildFQName(namespace, "", "file_uploads_total"), "Total number of large file uploads.", nil, nil, ), ctrlCodesTotal2xx: prometheus.NewDesc( prometheus.BuildFQName(namespace, "", "ctrl_codes_total_2xx"), "Total number of 2xx ctrl response codes.", nil, nil, ), ctrlCodesTotal3xx: prometheus.NewDesc( prometheus.BuildFQName(namespace, "", "ctrl_codes_total_3xx"), "Total number of 3xx ctrl response codes.", nil, nil, ), ctrlCodesTotal4xx: prometheus.NewDesc( prometheus.BuildFQName(namespace, "", "ctrl_codes_total_4xx"), "Total number of 4xx ctrl response codes.", nil, nil, ), ctrlCodesTotal5xx: prometheus.NewDesc( prometheus.BuildFQName(namespace, "", "ctrl_codes_total_5xx"), "Total number of 5xx ctrl response codes.", nil, nil, ), clusterLeader: prometheus.NewDesc( prometheus.BuildFQName(namespace, "", "cluster_leader"), "If this cluster node is the cluster leader.", nil, nil, ), clusterSize: prometheus.NewDesc( prometheus.BuildFQName(namespace, "", "cluster_size"), "Configured number of cluster nodes.", nil, nil, ), clusterNodesLive: prometheus.NewDesc( prometheus.BuildFQName(namespace, "", "cluster_nodes_live"), "Number of cluster nodes believed to be live by the current node.", nil, nil, ), malloced: prometheus.NewDesc( prometheus.BuildFQName(namespace, "", "malloced_bytes"), "Number of bytes of memory allocated and in use.", nil, nil, ), requestLatencyMsCount: prometheus.NewDesc( prometheus.BuildFQName(namespace, "", "request_latency_ms_count"), "Request latency histogram (in ms).", nil, nil, ), outgoingMessageBytesCount: prometheus.NewDesc( prometheus.BuildFQName(namespace, "", "outgoing_message_bytes"), "Response size histogram (in bytes).", nil, nil, ), } } // Describe describes all the metrics exported by the memcached exporter. It // implements prometheus.Collector. func (e *PromExporter) Describe(ch chan<- *prometheus.Desc) { ch <- e.up ch <- e.version ch <- e.topicsLive ch <- e.topicsTotal ch <- e.sessionsLive ch <- e.sessionsTotal ch <- e.numGoroutines ch <- e.incomingMessagesWebsockTotal ch <- e.outgoingMessagesWebsockTotal ch <- e.incomingMessagesLongpollTotal ch <- e.outgoingMessagesLongpollTotal ch <- e.incomingMessagesGrpcTotal ch <- e.outgoingMessagesGrpcTotal ch <- e.fileDownloadsTotal ch <- e.fileUploadsTotal ch <- e.ctrlCodesTotal2xx ch <- e.ctrlCodesTotal3xx ch <- e.ctrlCodesTotal4xx ch <- e.ctrlCodesTotal5xx ch <- e.clusterLeader ch <- e.clusterSize ch <- e.clusterNodesLive ch <- e.malloced ch <- e.requestLatencyMsCount ch <- e.outgoingMessageBytesCount } // Collect fetches statistics from the configured Tinode instance, and // delivers them as Prometheus metrics. It implements prometheus.Collector. func (e *PromExporter) Collect(ch chan<- prometheus.Metric) { up := float64(1) if stats, err := e.scraper.Scrape(); err != nil { log.Println("Failed to fetch or parse response", err) up = 0 } else { if err := e.parseStats(ch, stats); err != nil { up = 0 } } ch <- prometheus.MustNewConstMetric(e.up, prometheus.GaugeValue, up) } func (e *PromExporter) parseStats(ch chan<- prometheus.Metric, stats map[string]interface{}) error { err := firstError( e.parseAndUpdate(ch, e.version, prometheus.GaugeValue, stats, "Version"), e.parseAndUpdate(ch, e.topicsLive, prometheus.GaugeValue, stats, "LiveTopics"), e.parseAndUpdate(ch, e.topicsTotal, prometheus.CounterValue, stats, "TotalTopics"), e.parseAndUpdate(ch, e.sessionsLive, prometheus.GaugeValue, stats, "LiveSessions"), e.parseAndUpdate(ch, e.sessionsTotal, prometheus.CounterValue, stats, "TotalSessions"), e.parseAndUpdate(ch, e.numGoroutines, prometheus.GaugeValue, stats, "NumGoroutines"), e.parseAndUpdate(ch, e.incomingMessagesWebsockTotal, prometheus.CounterValue, stats, "IncomingMessagesWebsockTotal"), e.parseAndUpdate(ch, e.outgoingMessagesWebsockTotal, prometheus.CounterValue, stats, "OutgoingMessagesWebsockTotal"), e.parseAndUpdate(ch, e.incomingMessagesLongpollTotal, prometheus.CounterValue, stats, "IncomingMessagesLongpollTotal"), e.parseAndUpdate(ch, e.outgoingMessagesLongpollTotal, prometheus.CounterValue, stats, "OutgoingMessagesLongpollTotal"), e.parseAndUpdate(ch, e.incomingMessagesGrpcTotal, prometheus.CounterValue, stats, "IncomingMessagesGrpcTotal"), e.parseAndUpdate(ch, e.outgoingMessagesGrpcTotal, prometheus.CounterValue, stats, "OutgoingMessagesGrpcTotal"), e.parseAndUpdate(ch, e.fileDownloadsTotal, prometheus.CounterValue, stats, "FileDownloadsTotal"), e.parseAndUpdate(ch, e.fileUploadsTotal, prometheus.CounterValue, stats, "FileUploadsTotal"), e.parseAndUpdate(ch, e.ctrlCodesTotal2xx, prometheus.CounterValue, stats, "CtrlCodesTotal2xx"), e.parseAndUpdate(ch, e.ctrlCodesTotal3xx, prometheus.CounterValue, stats, "CtrlCodesTotal3xx"), e.parseAndUpdate(ch, e.ctrlCodesTotal4xx, prometheus.CounterValue, stats, "CtrlCodesTotal4xx"), e.parseAndUpdate(ch, e.ctrlCodesTotal5xx, prometheus.CounterValue, stats, "CtrlCodesTotal5xx"), e.parseAndUpdate(ch, e.clusterLeader, prometheus.GaugeValue, stats, "ClusterLeader"), e.parseAndUpdate(ch, e.clusterSize, prometheus.GaugeValue, stats, "TotalClusterNodes"), e.parseAndUpdate(ch, e.clusterNodesLive, prometheus.GaugeValue, stats, "LiveClusterNodes"), e.parseAndUpdate(ch, e.malloced, prometheus.GaugeValue, stats, "memstats.Alloc"), e.parseAndUpdateHisto(ch, e.requestLatencyMsCount, stats, "RequestLatency"), e.parseAndUpdateHisto(ch, e.outgoingMessageBytesCount, stats, "OutgoingMessageSize"), ) return err } func (e *PromExporter) parseAndUpdate(ch chan<- prometheus.Metric, desc *prometheus.Desc, valueType prometheus.ValueType, stats map[string]interface{}, key string) error { v, err := parseNumeric(stats, key) if err != nil { return err } ch <- prometheus.MustNewConstMetric(desc, valueType, v) return nil } func (e *PromExporter) parseAndUpdateHisto(ch chan<- prometheus.Metric, desc *prometheus.Desc, stats map[string]interface{}, key string) error { h, err := parseHisto(stats, key) if err != nil { return err } ch <- prometheus.MustNewConstHistogram(desc, h.count, h.sum, h.buckets) return nil } func firstError(errs ...error) error { for _, v := range errs { if v != nil { return v } } return nil } ================================================ FILE: monitoring/exporter/scraper.go ================================================ package main import ( "encoding/json" "errors" "log" "net/http" "strings" ) // Scraper collects metrics from a tinode server. type Scraper struct { // Target Tinode server address. address string // List of simple numeric metrics to scrape. simpleMetrics []string // List of histogram metrics to scrape. histogramMetrics []string } // Histogram struct. type histogram struct { count uint64 sum float64 buckets map[float64]uint64 } var errKeyNotFound = errors.New("key not found") var errMalformed = errors.New("input malformed") // CollectRaw gathers all metrics from the configured Tinode instance, // and returns them as a map. func (s *Scraper) CollectRaw() (map[string]interface{}, error) { stats, err := s.Scrape() if err != nil { log.Println("Failed to fetch or parse response", err) return nil, err } metrics, err := s.parseStatsRaw(stats) if err != nil { return nil, err } metrics["up"] = 1.0 return metrics, nil } // Scrape fetches the data from Tinode server using HTTP GET then decodes the response. func (s *Scraper) Scrape() (map[string]interface{}, error) { resp, err := http.Get(s.address) if err != nil { log.Println("Failed to connect to server", err) return nil, err } defer resp.Body.Close() var stats map[string]interface{} err = json.NewDecoder(resp.Body).Decode(&stats) return stats, err } func (s *Scraper) parseStatsRaw(stats map[string]interface{}) (map[string]interface{}, error) { metrics := make(map[string]interface{}) for _, key := range s.simpleMetrics { if val, err := parseNumeric(stats, key); err == nil { metrics[key] = val } else { return nil, err } } for _, key := range s.histogramMetrics { if val, err := parseHisto(stats, key); err == nil { metrics[key] = val } else { return nil, err } } return metrics, nil } // Extracts a simple histogram from `stats` and returns a cumulative histogram // corresponding to the simple histogram. // Returns: (count, sum, buckets, error) tuple. func parseHisto(stats map[string]interface{}, key string) (*histogram, error) { // Histogram is presented as a json with the predefined fields: count, sum, count_per_bucket, bounds. count, err := parseNumeric(stats, key+".count") if err != nil { return nil, err } sum, err := parseNumeric(stats, key+".sum") if err != nil { return nil, err } buckets, err := parseList(stats, key+".count_per_bucket") if err != nil { return nil, err } bounds, err := parseList(stats, key+".bounds") if err != nil { return nil, err } n := len(buckets) if n != len(bounds)+1 { return nil, errMalformed } result := make(map[float64]uint64) s := uint64(0) for i, v := range bounds { s += uint64(buckets[i]) result[v] = s } return &histogram{count: uint64(count), sum: sum, buckets: result}, nil } // Extracts a list of numerics from `stats` for the given path. func parseList(stats map[string]interface{}, path string) ([]float64, error) { value, err := parseMetric(stats, path) if err != nil { return nil, err } listval, ok := value.([]interface{}) if !ok { log.Println("Value at path is not a float64 array:", path, value) return nil, errMalformed } result := []float64{} for _, v := range listval { result = append(result, v.(float64)) } return result, nil } // Extracts a numeric from `stats` for the given path. func parseNumeric(stats map[string]interface{}, path string) (float64, error) { value, err := parseMetric(stats, path) if err != nil { return 0, err } floatval, ok := value.(float64) if !ok { log.Println("Value at path is not a float64:", path, value) return 0, errKeyNotFound } return floatval, nil } // Extracts a metric from `stats` for the given path. func parseMetric(stats map[string]interface{}, path string) (interface{}, error) { parts := strings.Split(path, ".") var value interface{} var found bool value = stats for i := 0; i < len(parts); i++ { subset, ok := value.(map[string]interface{}) if !ok { log.Println("Invalid key path:", path) return 0, errKeyNotFound } value, found = subset[parts[i]] if !found { log.Println("Invalid key path:", path, "(", parts[i], ")") return 0, errKeyNotFound } } return value, nil } ================================================ FILE: pbx/README.md ================================================ # Protocol Buffer and gRPC definitions Definitions for Tinode [gRPC](https://grpc.io/) client and plugins. Tinode gRPC clients must implement rpc service `Node`, Tinode plugins `Plugin`. Generated `Go` and `Python` code is included. For a sample `Python` implementation of a command line client see [tn-cli](../tn-cli/). For a partial plugin implementation see [chatbot](../chatbot/). If you want to make changes, you have to install protobuffers tool chain and gRPC: ``` $ python -m pip install grpcio grpcio-tools googleapis-common-protos ``` To generate `Go` bindings add the following comment to your code and run `go generate` (your actual path to `/pbx` may be different): ``` //go:generate protoc --proto_path=../pbx --go_out=plugins=grpc:../pbx ../pbx/model.proto ``` To generate `Python` bindings: ``` python -m grpc_tools.protoc -I../pbx --python_out=. --grpc_python_out=. ../pbx/model.proto ``` ================================================ FILE: pbx/go-generate.sh ================================================ #!/bin/bash protoc --go_out=../pbx --go_opt=paths=source_relative --go-grpc_out=../pbx --go-grpc_opt=paths=source_relative model.proto ================================================ FILE: pbx/model.pb.go ================================================ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.27.1 // protoc v3.21.12 // source: model.proto package pbx import ( protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" reflect "reflect" sync "sync" ) const ( // Verify that this generated code is sufficiently up-to-date. _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) // Verify that runtime/protoimpl is sufficiently up-to-date. _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) ) // Authentication level type AuthLevel int32 const ( AuthLevel_NONE AuthLevel = 0 AuthLevel_ANON AuthLevel = 10 AuthLevel_AUTH AuthLevel = 20 AuthLevel_ROOT AuthLevel = 30 ) // Enum value maps for AuthLevel. var ( AuthLevel_name = map[int32]string{ 0: "NONE", 10: "ANON", 20: "AUTH", 30: "ROOT", } AuthLevel_value = map[string]int32{ "NONE": 0, "ANON": 10, "AUTH": 20, "ROOT": 30, } ) func (x AuthLevel) Enum() *AuthLevel { p := new(AuthLevel) *p = x return p } func (x AuthLevel) String() string { return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) } func (AuthLevel) Descriptor() protoreflect.EnumDescriptor { return file_model_proto_enumTypes[0].Descriptor() } func (AuthLevel) Type() protoreflect.EnumType { return &file_model_proto_enumTypes[0] } func (x AuthLevel) Number() protoreflect.EnumNumber { return protoreflect.EnumNumber(x) } // Deprecated: Use AuthLevel.Descriptor instead. func (AuthLevel) EnumDescriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{0} } type InfoNote int32 const ( // Invalid value. The name must be globally unique. InfoNote_X1 InfoNote = 0 InfoNote_READ InfoNote = 1 InfoNote_RECV InfoNote = 2 InfoNote_KP InfoNote = 3 InfoNote_CALL InfoNote = 4 ) // Enum value maps for InfoNote. var ( InfoNote_name = map[int32]string{ 0: "X1", 1: "READ", 2: "RECV", 3: "KP", 4: "CALL", } InfoNote_value = map[string]int32{ "X1": 0, "READ": 1, "RECV": 2, "KP": 3, "CALL": 4, } ) func (x InfoNote) Enum() *InfoNote { p := new(InfoNote) *p = x return p } func (x InfoNote) String() string { return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) } func (InfoNote) Descriptor() protoreflect.EnumDescriptor { return file_model_proto_enumTypes[1].Descriptor() } func (InfoNote) Type() protoreflect.EnumType { return &file_model_proto_enumTypes[1] } func (x InfoNote) Number() protoreflect.EnumNumber { return protoreflect.EnumNumber(x) } // Deprecated: Use InfoNote.Descriptor instead. func (InfoNote) EnumDescriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{1} } type CallEvent int32 const ( // Invalid value. The name must be globally unique. CallEvent_X2 CallEvent = 0 CallEvent_ACCEPT CallEvent = 1 CallEvent_ANSWER CallEvent = 2 CallEvent_HANG_UP CallEvent = 3 CallEvent_ICE_CANDIDATE CallEvent = 4 CallEvent_INVITE CallEvent = 5 CallEvent_OFFER CallEvent = 6 CallEvent_RINGING CallEvent = 7 ) // Enum value maps for CallEvent. var ( CallEvent_name = map[int32]string{ 0: "X2", 1: "ACCEPT", 2: "ANSWER", 3: "HANG_UP", 4: "ICE_CANDIDATE", 5: "INVITE", 6: "OFFER", 7: "RINGING", } CallEvent_value = map[string]int32{ "X2": 0, "ACCEPT": 1, "ANSWER": 2, "HANG_UP": 3, "ICE_CANDIDATE": 4, "INVITE": 5, "OFFER": 6, "RINGING": 7, } ) func (x CallEvent) Enum() *CallEvent { p := new(CallEvent) *p = x return p } func (x CallEvent) String() string { return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) } func (CallEvent) Descriptor() protoreflect.EnumDescriptor { return file_model_proto_enumTypes[2].Descriptor() } func (CallEvent) Type() protoreflect.EnumType { return &file_model_proto_enumTypes[2] } func (x CallEvent) Number() protoreflect.EnumNumber { return protoreflect.EnumNumber(x) } // Deprecated: Use CallEvent.Descriptor instead. func (CallEvent) EnumDescriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{2} } // Plugin response codes type RespCode int32 const ( // Instruct Tinode server to continue with default processing of the client request. RespCode_CONTINUE RespCode = 0 // Drop the request as if the client did not send it RespCode_DROP RespCode = 1 // Send the the provided srvmsg response to the client. ServerResp must contain non-zero // srvmsg. RespCode_RESPOND RespCode = 2 // Replace client's original request with the provided clmsg request then continue with // processing. ServerResp must contain non-zero clmsg. RespCode_REPLACE RespCode = 3 ) // Enum value maps for RespCode. var ( RespCode_name = map[int32]string{ 0: "CONTINUE", 1: "DROP", 2: "RESPOND", 3: "REPLACE", } RespCode_value = map[string]int32{ "CONTINUE": 0, "DROP": 1, "RESPOND": 2, "REPLACE": 3, } ) func (x RespCode) Enum() *RespCode { p := new(RespCode) *p = x return p } func (x RespCode) String() string { return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) } func (RespCode) Descriptor() protoreflect.EnumDescriptor { return file_model_proto_enumTypes[3].Descriptor() } func (RespCode) Type() protoreflect.EnumType { return &file_model_proto_enumTypes[3] } func (x RespCode) Number() protoreflect.EnumNumber { return protoreflect.EnumNumber(x) } // Deprecated: Use RespCode.Descriptor instead. func (RespCode) EnumDescriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{3} } type Crud int32 const ( Crud_CREATE Crud = 0 Crud_UPDATE Crud = 1 Crud_DELETE Crud = 2 ) // Enum value maps for Crud. var ( Crud_name = map[int32]string{ 0: "CREATE", 1: "UPDATE", 2: "DELETE", } Crud_value = map[string]int32{ "CREATE": 0, "UPDATE": 1, "DELETE": 2, } ) func (x Crud) Enum() *Crud { p := new(Crud) *p = x return p } func (x Crud) String() string { return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) } func (Crud) Descriptor() protoreflect.EnumDescriptor { return file_model_proto_enumTypes[4].Descriptor() } func (Crud) Type() protoreflect.EnumType { return &file_model_proto_enumTypes[4] } func (x Crud) Number() protoreflect.EnumNumber { return protoreflect.EnumNumber(x) } // Deprecated: Use Crud.Descriptor instead. func (Crud) EnumDescriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{4} } // What to delete, either "msg" to delete messages (default) or "topic" to delete the topic or "sub" // to delete a subscription to topic. type ClientDel_What int32 const ( // Invalid value. The name must be globally unique. ClientDel_X0 ClientDel_What = 0 ClientDel_MSG ClientDel_What = 1 ClientDel_TOPIC ClientDel_What = 2 ClientDel_SUB ClientDel_What = 3 ClientDel_USER ClientDel_What = 4 ClientDel_CRED ClientDel_What = 5 ) // Enum value maps for ClientDel_What. var ( ClientDel_What_name = map[int32]string{ 0: "X0", 1: "MSG", 2: "TOPIC", 3: "SUB", 4: "USER", 5: "CRED", } ClientDel_What_value = map[string]int32{ "X0": 0, "MSG": 1, "TOPIC": 2, "SUB": 3, "USER": 4, "CRED": 5, } ) func (x ClientDel_What) Enum() *ClientDel_What { p := new(ClientDel_What) *p = x return p } func (x ClientDel_What) String() string { return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) } func (ClientDel_What) Descriptor() protoreflect.EnumDescriptor { return file_model_proto_enumTypes[5].Descriptor() } func (ClientDel_What) Type() protoreflect.EnumType { return &file_model_proto_enumTypes[5] } func (x ClientDel_What) Number() protoreflect.EnumNumber { return protoreflect.EnumNumber(x) } // Deprecated: Use ClientDel_What.Descriptor instead. func (ClientDel_What) EnumDescriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{18, 0} } type ServerPres_What int32 const ( // Invalid value. The name must be globally unique. ServerPres_X3 ServerPres_What = 0 ServerPres_ON ServerPres_What = 1 ServerPres_OFF ServerPres_What = 2 ServerPres_UA ServerPres_What = 3 ServerPres_UPD ServerPres_What = 4 ServerPres_GONE ServerPres_What = 5 ServerPres_ACS ServerPres_What = 6 ServerPres_TERM ServerPres_What = 7 ServerPres_MSG ServerPres_What = 8 ServerPres_READ ServerPres_What = 9 ServerPres_RECV ServerPres_What = 10 ServerPres_DEL ServerPres_What = 11 ServerPres_TAGS ServerPres_What = 12 ServerPres_AUX ServerPres_What = 13 ) // Enum value maps for ServerPres_What. var ( ServerPres_What_name = map[int32]string{ 0: "X3", 1: "ON", 2: "OFF", 3: "UA", 4: "UPD", 5: "GONE", 6: "ACS", 7: "TERM", 8: "MSG", 9: "READ", 10: "RECV", 11: "DEL", 12: "TAGS", 13: "AUX", } ServerPres_What_value = map[string]int32{ "X3": 0, "ON": 1, "OFF": 2, "UA": 3, "UPD": 4, "GONE": 5, "ACS": 6, "TERM": 7, "MSG": 8, "READ": 9, "RECV": 10, "DEL": 11, "TAGS": 12, "AUX": 13, } ) func (x ServerPres_What) Enum() *ServerPres_What { p := new(ServerPres_What) *p = x return p } func (x ServerPres_What) String() string { return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) } func (ServerPres_What) Descriptor() protoreflect.EnumDescriptor { return file_model_proto_enumTypes[6].Descriptor() } func (ServerPres_What) Type() protoreflect.EnumType { return &file_model_proto_enumTypes[6] } func (x ServerPres_What) Number() protoreflect.EnumNumber { return protoreflect.EnumNumber(x) } // Deprecated: Use ServerPres_What.Descriptor instead. func (ServerPres_What) EnumDescriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{28, 0} } // Dummy placeholder message. type Unused struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields } func (x *Unused) Reset() { *x = Unused{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[0] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *Unused) String() string { return protoimpl.X.MessageStringOf(x) } func (*Unused) ProtoMessage() {} func (x *Unused) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[0] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use Unused.ProtoReflect.Descriptor instead. func (*Unused) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{0} } // Topic default access mode type DefaultAcsMode struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Auth string `protobuf:"bytes,1,opt,name=auth,proto3" json:"auth,omitempty"` Anon string `protobuf:"bytes,2,opt,name=anon,proto3" json:"anon,omitempty"` } func (x *DefaultAcsMode) Reset() { *x = DefaultAcsMode{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[1] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *DefaultAcsMode) String() string { return protoimpl.X.MessageStringOf(x) } func (*DefaultAcsMode) ProtoMessage() {} func (x *DefaultAcsMode) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[1] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use DefaultAcsMode.ProtoReflect.Descriptor instead. func (*DefaultAcsMode) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{1} } func (x *DefaultAcsMode) GetAuth() string { if x != nil { return x.Auth } return "" } func (x *DefaultAcsMode) GetAnon() string { if x != nil { return x.Anon } return "" } // Actual access mode type AccessMode struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields // Access mode requested by the user Want string `protobuf:"bytes,1,opt,name=want,proto3" json:"want,omitempty"` // Access mode granted to the user by the admin Given string `protobuf:"bytes,2,opt,name=given,proto3" json:"given,omitempty"` } func (x *AccessMode) Reset() { *x = AccessMode{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[2] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *AccessMode) String() string { return protoimpl.X.MessageStringOf(x) } func (*AccessMode) ProtoMessage() {} func (x *AccessMode) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[2] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use AccessMode.ProtoReflect.Descriptor instead. func (*AccessMode) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{2} } func (x *AccessMode) GetWant() string { if x != nil { return x.Want } return "" } func (x *AccessMode) GetGiven() string { if x != nil { return x.Given } return "" } // SetSub: payload in set.sub request to update current subscription or invite another user, {sub.what} == "sub" type SetSub struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields // User affected by this request. Default (empty): current user UserId string `protobuf:"bytes,1,opt,name=user_id,json=userId,proto3" json:"user_id,omitempty"` // Access mode change, either Given or Want depending on context Mode string `protobuf:"bytes,2,opt,name=mode,proto3" json:"mode,omitempty"` } func (x *SetSub) Reset() { *x = SetSub{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[3] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *SetSub) String() string { return protoimpl.X.MessageStringOf(x) } func (*SetSub) ProtoMessage() {} func (x *SetSub) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[3] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use SetSub.ProtoReflect.Descriptor instead. func (*SetSub) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{3} } func (x *SetSub) GetUserId() string { if x != nil { return x.UserId } return "" } func (x *SetSub) GetMode() string { if x != nil { return x.Mode } return "" } // Credentials such as email or phone number type ClientCred struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields // Credential type, i.e. `email` or `tel`. Method string `protobuf:"bytes,1,opt,name=method,proto3" json:"method,omitempty"` // Value to verify, i.e. `user@example.com` or `+18003287448` Value string `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"` // Verification response Response string `protobuf:"bytes,3,opt,name=response,proto3" json:"response,omitempty"` // Request parameters, such as preferences or country code. Params map[string][]byte `protobuf:"bytes,4,rep,name=params,proto3" json:"params,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` } func (x *ClientCred) Reset() { *x = ClientCred{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[4] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *ClientCred) String() string { return protoimpl.X.MessageStringOf(x) } func (*ClientCred) ProtoMessage() {} func (x *ClientCred) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[4] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use ClientCred.ProtoReflect.Descriptor instead. func (*ClientCred) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{4} } func (x *ClientCred) GetMethod() string { if x != nil { return x.Method } return "" } func (x *ClientCred) GetValue() string { if x != nil { return x.Value } return "" } func (x *ClientCred) GetResponse() string { if x != nil { return x.Response } return "" } func (x *ClientCred) GetParams() map[string][]byte { if x != nil { return x.Params } return nil } // SetDesc: C2S in set.what == "desc" and sub.init message type SetDesc struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields DefaultAcs *DefaultAcsMode `protobuf:"bytes,1,opt,name=default_acs,json=defaultAcs,proto3" json:"default_acs,omitempty"` Public []byte `protobuf:"bytes,2,opt,name=public,proto3" json:"public,omitempty"` Private []byte `protobuf:"bytes,3,opt,name=private,proto3" json:"private,omitempty"` Trusted []byte `protobuf:"bytes,4,opt,name=trusted,proto3" json:"trusted,omitempty"` } func (x *SetDesc) Reset() { *x = SetDesc{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[5] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *SetDesc) String() string { return protoimpl.X.MessageStringOf(x) } func (*SetDesc) ProtoMessage() {} func (x *SetDesc) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[5] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use SetDesc.ProtoReflect.Descriptor instead. func (*SetDesc) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{5} } func (x *SetDesc) GetDefaultAcs() *DefaultAcsMode { if x != nil { return x.DefaultAcs } return nil } func (x *SetDesc) GetPublic() []byte { if x != nil { return x.Public } return nil } func (x *SetDesc) GetPrivate() []byte { if x != nil { return x.Private } return nil } func (x *SetDesc) GetTrusted() []byte { if x != nil { return x.Trusted } return nil } type SeqRange struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Low int32 `protobuf:"varint,1,opt,name=low,proto3" json:"low,omitempty"` Hi int32 `protobuf:"varint,2,opt,name=hi,proto3" json:"hi,omitempty"` } func (x *SeqRange) Reset() { *x = SeqRange{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[6] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *SeqRange) String() string { return protoimpl.X.MessageStringOf(x) } func (*SeqRange) ProtoMessage() {} func (x *SeqRange) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[6] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use SeqRange.ProtoReflect.Descriptor instead. func (*SeqRange) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{6} } func (x *SeqRange) GetLow() int32 { if x != nil { return x.Low } return 0 } func (x *SeqRange) GetHi() int32 { if x != nil { return x.Hi } return 0 } type GetOpts struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields // Timestamp in milliseconds since epoch 01/01/1970 IfModifiedSince int64 `protobuf:"varint,1,opt,name=if_modified_since,json=ifModifiedSince,proto3" json:"if_modified_since,omitempty"` // Limit search to this user ID User string `protobuf:"bytes,2,opt,name=user,proto3" json:"user,omitempty"` // Limit search results to one topic; Topic string `protobuf:"bytes,3,opt,name=topic,proto3" json:"topic,omitempty"` // Load messages with seq id equal or greater than this SinceId int32 `protobuf:"varint,4,opt,name=since_id,json=sinceId,proto3" json:"since_id,omitempty"` // Load messages with seq id lower than this BeforeId int32 `protobuf:"varint,5,opt,name=before_id,json=beforeId,proto3" json:"before_id,omitempty"` // Maximum number of results to return Limit int32 `protobuf:"varint,6,opt,name=limit,proto3" json:"limit,omitempty"` // Load messages by id or ranges of ids Ranges []*SeqRange `protobuf:"bytes,7,rep,name=ranges,proto3" json:"ranges,omitempty"` } func (x *GetOpts) Reset() { *x = GetOpts{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[7] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *GetOpts) String() string { return protoimpl.X.MessageStringOf(x) } func (*GetOpts) ProtoMessage() {} func (x *GetOpts) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[7] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use GetOpts.ProtoReflect.Descriptor instead. func (*GetOpts) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{7} } func (x *GetOpts) GetIfModifiedSince() int64 { if x != nil { return x.IfModifiedSince } return 0 } func (x *GetOpts) GetUser() string { if x != nil { return x.User } return "" } func (x *GetOpts) GetTopic() string { if x != nil { return x.Topic } return "" } func (x *GetOpts) GetSinceId() int32 { if x != nil { return x.SinceId } return 0 } func (x *GetOpts) GetBeforeId() int32 { if x != nil { return x.BeforeId } return 0 } func (x *GetOpts) GetLimit() int32 { if x != nil { return x.Limit } return 0 } func (x *GetOpts) GetRanges() []*SeqRange { if x != nil { return x.Ranges } return nil } type GetQuery struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields What string `protobuf:"bytes,1,opt,name=what,proto3" json:"what,omitempty"` // Parameters of "desc" request Desc *GetOpts `protobuf:"bytes,2,opt,name=desc,proto3" json:"desc,omitempty"` // Parameters of "sub" request Sub *GetOpts `protobuf:"bytes,3,opt,name=sub,proto3" json:"sub,omitempty"` // Parameters of "data" request Data *GetOpts `protobuf:"bytes,4,opt,name=data,proto3" json:"data,omitempty"` } func (x *GetQuery) Reset() { *x = GetQuery{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[8] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *GetQuery) String() string { return protoimpl.X.MessageStringOf(x) } func (*GetQuery) ProtoMessage() {} func (x *GetQuery) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[8] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use GetQuery.ProtoReflect.Descriptor instead. func (*GetQuery) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{8} } func (x *GetQuery) GetWhat() string { if x != nil { return x.What } return "" } func (x *GetQuery) GetDesc() *GetOpts { if x != nil { return x.Desc } return nil } func (x *GetQuery) GetSub() *GetOpts { if x != nil { return x.Sub } return nil } func (x *GetQuery) GetData() *GetOpts { if x != nil { return x.Data } return nil } type SetQuery struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields // Topic metadata, new topic & new subscriptions only Desc *SetDesc `protobuf:"bytes,1,opt,name=desc,proto3" json:"desc,omitempty"` // Subscription parameters Sub *SetSub `protobuf:"bytes,2,opt,name=sub,proto3" json:"sub,omitempty"` // Indexable tags Tags []string `protobuf:"bytes,3,rep,name=tags,proto3" json:"tags,omitempty"` // Credential being updated. Cred *ClientCred `protobuf:"bytes,4,opt,name=cred,proto3" json:"cred,omitempty"` // Auxiliary data. Aux map[string][]byte `protobuf:"bytes,5,rep,name=aux,proto3" json:"aux,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` } func (x *SetQuery) Reset() { *x = SetQuery{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[9] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *SetQuery) String() string { return protoimpl.X.MessageStringOf(x) } func (*SetQuery) ProtoMessage() {} func (x *SetQuery) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[9] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use SetQuery.ProtoReflect.Descriptor instead. func (*SetQuery) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{9} } func (x *SetQuery) GetDesc() *SetDesc { if x != nil { return x.Desc } return nil } func (x *SetQuery) GetSub() *SetSub { if x != nil { return x.Sub } return nil } func (x *SetQuery) GetTags() []string { if x != nil { return x.Tags } return nil } func (x *SetQuery) GetCred() *ClientCred { if x != nil { return x.Cred } return nil } func (x *SetQuery) GetAux() map[string][]byte { if x != nil { return x.Aux } return nil } // Client handshake type ClientHi struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` UserAgent string `protobuf:"bytes,2,opt,name=user_agent,json=userAgent,proto3" json:"user_agent,omitempty"` Ver string `protobuf:"bytes,3,opt,name=ver,proto3" json:"ver,omitempty"` DeviceId string `protobuf:"bytes,4,opt,name=device_id,json=deviceId,proto3" json:"device_id,omitempty"` Lang string `protobuf:"bytes,5,opt,name=lang,proto3" json:"lang,omitempty"` Platform string `protobuf:"bytes,6,opt,name=platform,proto3" json:"platform,omitempty"` Background bool `protobuf:"varint,7,opt,name=background,proto3" json:"background,omitempty"` } func (x *ClientHi) Reset() { *x = ClientHi{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[10] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *ClientHi) String() string { return protoimpl.X.MessageStringOf(x) } func (*ClientHi) ProtoMessage() {} func (x *ClientHi) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[10] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use ClientHi.ProtoReflect.Descriptor instead. func (*ClientHi) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{10} } func (x *ClientHi) GetId() string { if x != nil { return x.Id } return "" } func (x *ClientHi) GetUserAgent() string { if x != nil { return x.UserAgent } return "" } func (x *ClientHi) GetVer() string { if x != nil { return x.Ver } return "" } func (x *ClientHi) GetDeviceId() string { if x != nil { return x.DeviceId } return "" } func (x *ClientHi) GetLang() string { if x != nil { return x.Lang } return "" } func (x *ClientHi) GetPlatform() string { if x != nil { return x.Platform } return "" } func (x *ClientHi) GetBackground() bool { if x != nil { return x.Background } return false } // User creation message {acc} type ClientAcc struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` // User being created or updated UserId string `protobuf:"bytes,2,opt,name=user_id,json=userId,proto3" json:"user_id,omitempty"` // The initial authentication scheme the account can use Scheme string `protobuf:"bytes,3,opt,name=scheme,proto3" json:"scheme,omitempty"` // Shared secret Secret []byte `protobuf:"bytes,4,opt,name=secret,proto3" json:"secret,omitempty"` // Authenticate session with the newly created account Login bool `protobuf:"varint,5,opt,name=login,proto3" json:"login,omitempty"` // Indexable tags for user discovery Tags []string `protobuf:"bytes,6,rep,name=tags,proto3" json:"tags,omitempty"` // User initialization data when creating a new user, otherwise ignored Desc *SetDesc `protobuf:"bytes,7,opt,name=desc,proto3" json:"desc,omitempty"` // Credentials for verification. Cred []*ClientCred `protobuf:"bytes,8,rep,name=cred,proto3" json:"cred,omitempty"` // Authentication token used for resetting a password. Token []byte `protobuf:"bytes,9,opt,name=token,proto3" json:"token,omitempty"` // Account state: normal ("ok"), suspended State string `protobuf:"bytes,10,opt,name=state,proto3" json:"state,omitempty"` // AuthLevel AuthLevel AuthLevel `protobuf:"varint,11,opt,name=auth_level,json=authLevel,proto3,enum=pbx.AuthLevel" json:"auth_level,omitempty"` // Temporary auth params for one-off actions like password reset. TmpScheme string `protobuf:"bytes,12,opt,name=tmp_scheme,json=tmpScheme,proto3" json:"tmp_scheme,omitempty"` TmpSecret []byte `protobuf:"bytes,13,opt,name=tmp_secret,json=tmpSecret,proto3" json:"tmp_secret,omitempty"` } func (x *ClientAcc) Reset() { *x = ClientAcc{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[11] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *ClientAcc) String() string { return protoimpl.X.MessageStringOf(x) } func (*ClientAcc) ProtoMessage() {} func (x *ClientAcc) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[11] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use ClientAcc.ProtoReflect.Descriptor instead. func (*ClientAcc) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{11} } func (x *ClientAcc) GetId() string { if x != nil { return x.Id } return "" } func (x *ClientAcc) GetUserId() string { if x != nil { return x.UserId } return "" } func (x *ClientAcc) GetScheme() string { if x != nil { return x.Scheme } return "" } func (x *ClientAcc) GetSecret() []byte { if x != nil { return x.Secret } return nil } func (x *ClientAcc) GetLogin() bool { if x != nil { return x.Login } return false } func (x *ClientAcc) GetTags() []string { if x != nil { return x.Tags } return nil } func (x *ClientAcc) GetDesc() *SetDesc { if x != nil { return x.Desc } return nil } func (x *ClientAcc) GetCred() []*ClientCred { if x != nil { return x.Cred } return nil } func (x *ClientAcc) GetToken() []byte { if x != nil { return x.Token } return nil } func (x *ClientAcc) GetState() string { if x != nil { return x.State } return "" } func (x *ClientAcc) GetAuthLevel() AuthLevel { if x != nil { return x.AuthLevel } return AuthLevel_NONE } func (x *ClientAcc) GetTmpScheme() string { if x != nil { return x.TmpScheme } return "" } func (x *ClientAcc) GetTmpSecret() []byte { if x != nil { return x.TmpSecret } return nil } // Login {login} message type ClientLogin struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` // Authentication scheme Scheme string `protobuf:"bytes,2,opt,name=scheme,proto3" json:"scheme,omitempty"` // Shared secret Secret []byte `protobuf:"bytes,3,opt,name=secret,proto3" json:"secret,omitempty"` // Credentials for verification. Cred []*ClientCred `protobuf:"bytes,4,rep,name=cred,proto3" json:"cred,omitempty"` } func (x *ClientLogin) Reset() { *x = ClientLogin{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[12] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *ClientLogin) String() string { return protoimpl.X.MessageStringOf(x) } func (*ClientLogin) ProtoMessage() {} func (x *ClientLogin) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[12] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use ClientLogin.ProtoReflect.Descriptor instead. func (*ClientLogin) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{12} } func (x *ClientLogin) GetId() string { if x != nil { return x.Id } return "" } func (x *ClientLogin) GetScheme() string { if x != nil { return x.Scheme } return "" } func (x *ClientLogin) GetSecret() []byte { if x != nil { return x.Secret } return nil } func (x *ClientLogin) GetCred() []*ClientCred { if x != nil { return x.Cred } return nil } // Subscription request {sub} message type ClientSub struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` Topic string `protobuf:"bytes,2,opt,name=topic,proto3" json:"topic,omitempty"` // mirrors {set} SetQuery *SetQuery `protobuf:"bytes,3,opt,name=set_query,json=setQuery,proto3" json:"set_query,omitempty"` // mirrors {get} GetQuery *GetQuery `protobuf:"bytes,4,opt,name=get_query,json=getQuery,proto3" json:"get_query,omitempty"` } func (x *ClientSub) Reset() { *x = ClientSub{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[13] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *ClientSub) String() string { return protoimpl.X.MessageStringOf(x) } func (*ClientSub) ProtoMessage() {} func (x *ClientSub) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[13] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use ClientSub.ProtoReflect.Descriptor instead. func (*ClientSub) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{13} } func (x *ClientSub) GetId() string { if x != nil { return x.Id } return "" } func (x *ClientSub) GetTopic() string { if x != nil { return x.Topic } return "" } func (x *ClientSub) GetSetQuery() *SetQuery { if x != nil { return x.SetQuery } return nil } func (x *ClientSub) GetGetQuery() *GetQuery { if x != nil { return x.GetQuery } return nil } // Unsubscribe {leave} request message type ClientLeave struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` Topic string `protobuf:"bytes,2,opt,name=topic,proto3" json:"topic,omitempty"` Unsub bool `protobuf:"varint,3,opt,name=unsub,proto3" json:"unsub,omitempty"` } func (x *ClientLeave) Reset() { *x = ClientLeave{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[14] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *ClientLeave) String() string { return protoimpl.X.MessageStringOf(x) } func (*ClientLeave) ProtoMessage() {} func (x *ClientLeave) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[14] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use ClientLeave.ProtoReflect.Descriptor instead. func (*ClientLeave) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{14} } func (x *ClientLeave) GetId() string { if x != nil { return x.Id } return "" } func (x *ClientLeave) GetTopic() string { if x != nil { return x.Topic } return "" } func (x *ClientLeave) GetUnsub() bool { if x != nil { return x.Unsub } return false } // ClientPub is client's request to publish data to topic subscribers {pub} type ClientPub struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` Topic string `protobuf:"bytes,2,opt,name=topic,proto3" json:"topic,omitempty"` NoEcho bool `protobuf:"varint,3,opt,name=no_echo,json=noEcho,proto3" json:"no_echo,omitempty"` Head map[string][]byte `protobuf:"bytes,4,rep,name=head,proto3" json:"head,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` Content []byte `protobuf:"bytes,5,opt,name=content,proto3" json:"content,omitempty"` } func (x *ClientPub) Reset() { *x = ClientPub{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[15] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *ClientPub) String() string { return protoimpl.X.MessageStringOf(x) } func (*ClientPub) ProtoMessage() {} func (x *ClientPub) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[15] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use ClientPub.ProtoReflect.Descriptor instead. func (*ClientPub) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{15} } func (x *ClientPub) GetId() string { if x != nil { return x.Id } return "" } func (x *ClientPub) GetTopic() string { if x != nil { return x.Topic } return "" } func (x *ClientPub) GetNoEcho() bool { if x != nil { return x.NoEcho } return false } func (x *ClientPub) GetHead() map[string][]byte { if x != nil { return x.Head } return nil } func (x *ClientPub) GetContent() []byte { if x != nil { return x.Content } return nil } // Query topic state {get} type ClientGet struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` Topic string `protobuf:"bytes,2,opt,name=topic,proto3" json:"topic,omitempty"` Query *GetQuery `protobuf:"bytes,3,opt,name=query,proto3" json:"query,omitempty"` } func (x *ClientGet) Reset() { *x = ClientGet{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[16] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *ClientGet) String() string { return protoimpl.X.MessageStringOf(x) } func (*ClientGet) ProtoMessage() {} func (x *ClientGet) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[16] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use ClientGet.ProtoReflect.Descriptor instead. func (*ClientGet) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{16} } func (x *ClientGet) GetId() string { if x != nil { return x.Id } return "" } func (x *ClientGet) GetTopic() string { if x != nil { return x.Topic } return "" } func (x *ClientGet) GetQuery() *GetQuery { if x != nil { return x.Query } return nil } // Update topic state {set} type ClientSet struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` Topic string `protobuf:"bytes,2,opt,name=topic,proto3" json:"topic,omitempty"` Query *SetQuery `protobuf:"bytes,3,opt,name=query,proto3" json:"query,omitempty"` } func (x *ClientSet) Reset() { *x = ClientSet{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[17] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *ClientSet) String() string { return protoimpl.X.MessageStringOf(x) } func (*ClientSet) ProtoMessage() {} func (x *ClientSet) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[17] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use ClientSet.ProtoReflect.Descriptor instead. func (*ClientSet) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{17} } func (x *ClientSet) GetId() string { if x != nil { return x.Id } return "" } func (x *ClientSet) GetTopic() string { if x != nil { return x.Topic } return "" } func (x *ClientSet) GetQuery() *SetQuery { if x != nil { return x.Query } return nil } // ClientDel delete messages or topic type ClientDel struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` Topic string `protobuf:"bytes,2,opt,name=topic,proto3" json:"topic,omitempty"` What ClientDel_What `protobuf:"varint,3,opt,name=what,proto3,enum=pbx.ClientDel_What" json:"what,omitempty"` // Delete messages by id or range of ids DelSeq []*SeqRange `protobuf:"bytes,4,rep,name=del_seq,json=delSeq,proto3" json:"del_seq,omitempty"` // User ID of the subscription to delete UserId string `protobuf:"bytes,5,opt,name=user_id,json=userId,proto3" json:"user_id,omitempty"` // Credential to delete. Cred *ClientCred `protobuf:"bytes,6,opt,name=cred,proto3" json:"cred,omitempty"` // Request to hard-delete messages for all users, if such option is available. Hard bool `protobuf:"varint,7,opt,name=hard,proto3" json:"hard,omitempty"` } func (x *ClientDel) Reset() { *x = ClientDel{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[18] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *ClientDel) String() string { return protoimpl.X.MessageStringOf(x) } func (*ClientDel) ProtoMessage() {} func (x *ClientDel) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[18] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use ClientDel.ProtoReflect.Descriptor instead. func (*ClientDel) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{18} } func (x *ClientDel) GetId() string { if x != nil { return x.Id } return "" } func (x *ClientDel) GetTopic() string { if x != nil { return x.Topic } return "" } func (x *ClientDel) GetWhat() ClientDel_What { if x != nil { return x.What } return ClientDel_X0 } func (x *ClientDel) GetDelSeq() []*SeqRange { if x != nil { return x.DelSeq } return nil } func (x *ClientDel) GetUserId() string { if x != nil { return x.UserId } return "" } func (x *ClientDel) GetCred() *ClientCred { if x != nil { return x.Cred } return nil } func (x *ClientDel) GetHard() bool { if x != nil { return x.Hard } return false } // ClientNote is a client-generated notification for topic subscribers type ClientNote struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Topic string `protobuf:"bytes,1,opt,name=topic,proto3" json:"topic,omitempty"` // what is being reported: "recv" - message received, "read" - message read, // "kp" - typing notification, "call" - voice/video call What InfoNote `protobuf:"varint,2,opt,name=what,proto3,enum=pbx.InfoNote" json:"what,omitempty"` // Server-issued message ID being reported SeqId int32 `protobuf:"varint,3,opt,name=seq_id,json=seqId,proto3" json:"seq_id,omitempty"` // Client's count of unread messages to report back to the server. Used in push notifications on iOS. Unread int32 `protobuf:"varint,4,opt,name=unread,proto3" json:"unread,omitempty"` // Call event. Event CallEvent `protobuf:"varint,5,opt,name=event,proto3,enum=pbx.CallEvent" json:"event,omitempty"` // Arbitrary json payload (used in video calls). Payload []byte `protobuf:"bytes,6,opt,name=payload,proto3" json:"payload,omitempty"` } func (x *ClientNote) Reset() { *x = ClientNote{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[19] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *ClientNote) String() string { return protoimpl.X.MessageStringOf(x) } func (*ClientNote) ProtoMessage() {} func (x *ClientNote) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[19] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use ClientNote.ProtoReflect.Descriptor instead. func (*ClientNote) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{19} } func (x *ClientNote) GetTopic() string { if x != nil { return x.Topic } return "" } func (x *ClientNote) GetWhat() InfoNote { if x != nil { return x.What } return InfoNote_X1 } func (x *ClientNote) GetSeqId() int32 { if x != nil { return x.SeqId } return 0 } func (x *ClientNote) GetUnread() int32 { if x != nil { return x.Unread } return 0 } func (x *ClientNote) GetEvent() CallEvent { if x != nil { return x.Event } return CallEvent_X2 } func (x *ClientNote) GetPayload() []byte { if x != nil { return x.Payload } return nil } type ClientExtra struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Attachments []string `protobuf:"bytes,1,rep,name=attachments,proto3" json:"attachments,omitempty"` // Root user may send messages on behalf of other users. OnBehalfOf string `protobuf:"bytes,2,opt,name=on_behalf_of,json=onBehalfOf,proto3" json:"on_behalf_of,omitempty"` AuthLevel AuthLevel `protobuf:"varint,3,opt,name=auth_level,json=authLevel,proto3,enum=pbx.AuthLevel" json:"auth_level,omitempty"` } func (x *ClientExtra) Reset() { *x = ClientExtra{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[20] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *ClientExtra) String() string { return protoimpl.X.MessageStringOf(x) } func (*ClientExtra) ProtoMessage() {} func (x *ClientExtra) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[20] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use ClientExtra.ProtoReflect.Descriptor instead. func (*ClientExtra) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{20} } func (x *ClientExtra) GetAttachments() []string { if x != nil { return x.Attachments } return nil } func (x *ClientExtra) GetOnBehalfOf() string { if x != nil { return x.OnBehalfOf } return "" } func (x *ClientExtra) GetAuthLevel() AuthLevel { if x != nil { return x.AuthLevel } return AuthLevel_NONE } type ClientMsg struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields // Types that are assignable to Message: // *ClientMsg_Hi // *ClientMsg_Acc // *ClientMsg_Login // *ClientMsg_Sub // *ClientMsg_Leave // *ClientMsg_Pub // *ClientMsg_Get // *ClientMsg_Set // *ClientMsg_Del // *ClientMsg_Note Message isClientMsg_Message `protobuf_oneof:"Message"` // Additional message parameters. Extra *ClientExtra `protobuf:"bytes,13,opt,name=extra,proto3" json:"extra,omitempty"` } func (x *ClientMsg) Reset() { *x = ClientMsg{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[21] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *ClientMsg) String() string { return protoimpl.X.MessageStringOf(x) } func (*ClientMsg) ProtoMessage() {} func (x *ClientMsg) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[21] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use ClientMsg.ProtoReflect.Descriptor instead. func (*ClientMsg) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{21} } func (m *ClientMsg) GetMessage() isClientMsg_Message { if m != nil { return m.Message } return nil } func (x *ClientMsg) GetHi() *ClientHi { if x, ok := x.GetMessage().(*ClientMsg_Hi); ok { return x.Hi } return nil } func (x *ClientMsg) GetAcc() *ClientAcc { if x, ok := x.GetMessage().(*ClientMsg_Acc); ok { return x.Acc } return nil } func (x *ClientMsg) GetLogin() *ClientLogin { if x, ok := x.GetMessage().(*ClientMsg_Login); ok { return x.Login } return nil } func (x *ClientMsg) GetSub() *ClientSub { if x, ok := x.GetMessage().(*ClientMsg_Sub); ok { return x.Sub } return nil } func (x *ClientMsg) GetLeave() *ClientLeave { if x, ok := x.GetMessage().(*ClientMsg_Leave); ok { return x.Leave } return nil } func (x *ClientMsg) GetPub() *ClientPub { if x, ok := x.GetMessage().(*ClientMsg_Pub); ok { return x.Pub } return nil } func (x *ClientMsg) GetGet() *ClientGet { if x, ok := x.GetMessage().(*ClientMsg_Get); ok { return x.Get } return nil } func (x *ClientMsg) GetSet() *ClientSet { if x, ok := x.GetMessage().(*ClientMsg_Set); ok { return x.Set } return nil } func (x *ClientMsg) GetDel() *ClientDel { if x, ok := x.GetMessage().(*ClientMsg_Del); ok { return x.Del } return nil } func (x *ClientMsg) GetNote() *ClientNote { if x, ok := x.GetMessage().(*ClientMsg_Note); ok { return x.Note } return nil } func (x *ClientMsg) GetExtra() *ClientExtra { if x != nil { return x.Extra } return nil } type isClientMsg_Message interface { isClientMsg_Message() } type ClientMsg_Hi struct { Hi *ClientHi `protobuf:"bytes,1,opt,name=hi,proto3,oneof"` } type ClientMsg_Acc struct { Acc *ClientAcc `protobuf:"bytes,2,opt,name=acc,proto3,oneof"` } type ClientMsg_Login struct { Login *ClientLogin `protobuf:"bytes,3,opt,name=login,proto3,oneof"` } type ClientMsg_Sub struct { Sub *ClientSub `protobuf:"bytes,4,opt,name=sub,proto3,oneof"` } type ClientMsg_Leave struct { Leave *ClientLeave `protobuf:"bytes,5,opt,name=leave,proto3,oneof"` } type ClientMsg_Pub struct { Pub *ClientPub `protobuf:"bytes,6,opt,name=pub,proto3,oneof"` } type ClientMsg_Get struct { Get *ClientGet `protobuf:"bytes,7,opt,name=get,proto3,oneof"` } type ClientMsg_Set struct { Set *ClientSet `protobuf:"bytes,8,opt,name=set,proto3,oneof"` } type ClientMsg_Del struct { Del *ClientDel `protobuf:"bytes,9,opt,name=del,proto3,oneof"` } type ClientMsg_Note struct { Note *ClientNote `protobuf:"bytes,10,opt,name=note,proto3,oneof"` } func (*ClientMsg_Hi) isClientMsg_Message() {} func (*ClientMsg_Acc) isClientMsg_Message() {} func (*ClientMsg_Login) isClientMsg_Message() {} func (*ClientMsg_Sub) isClientMsg_Message() {} func (*ClientMsg_Leave) isClientMsg_Message() {} func (*ClientMsg_Pub) isClientMsg_Message() {} func (*ClientMsg_Get) isClientMsg_Message() {} func (*ClientMsg_Set) isClientMsg_Message() {} func (*ClientMsg_Del) isClientMsg_Message() {} func (*ClientMsg_Note) isClientMsg_Message() {} // Credentials type ServerCred struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields // Credential type, i.e. `email` or `tel`. Method string `protobuf:"bytes,1,opt,name=method,proto3" json:"method,omitempty"` // Value to verify, i.e. `user@example.com` or `+18003287448` Value string `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"` // Indicator that the credential is validated Done bool `protobuf:"varint,3,opt,name=done,proto3" json:"done,omitempty"` } func (x *ServerCred) Reset() { *x = ServerCred{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[22] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *ServerCred) String() string { return protoimpl.X.MessageStringOf(x) } func (*ServerCred) ProtoMessage() {} func (x *ServerCred) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[22] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use ServerCred.ProtoReflect.Descriptor instead. func (*ServerCred) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{22} } func (x *ServerCred) GetMethod() string { if x != nil { return x.Method } return "" } func (x *ServerCred) GetValue() string { if x != nil { return x.Value } return "" } func (x *ServerCred) GetDone() bool { if x != nil { return x.Done } return false } // Topic description, S2C in Meta message type TopicDesc struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields CreatedAt int64 `protobuf:"varint,1,opt,name=created_at,json=createdAt,proto3" json:"created_at,omitempty"` UpdatedAt int64 `protobuf:"varint,2,opt,name=updated_at,json=updatedAt,proto3" json:"updated_at,omitempty"` TouchedAt int64 `protobuf:"varint,3,opt,name=touched_at,json=touchedAt,proto3" json:"touched_at,omitempty"` Defacs *DefaultAcsMode `protobuf:"bytes,4,opt,name=defacs,proto3" json:"defacs,omitempty"` Acs *AccessMode `protobuf:"bytes,5,opt,name=acs,proto3" json:"acs,omitempty"` SeqId int32 `protobuf:"varint,6,opt,name=seq_id,json=seqId,proto3" json:"seq_id,omitempty"` ReadId int32 `protobuf:"varint,7,opt,name=read_id,json=readId,proto3" json:"read_id,omitempty"` RecvId int32 `protobuf:"varint,8,opt,name=recv_id,json=recvId,proto3" json:"recv_id,omitempty"` DelId int32 `protobuf:"varint,9,opt,name=del_id,json=delId,proto3" json:"del_id,omitempty"` Public []byte `protobuf:"bytes,10,opt,name=public,proto3" json:"public,omitempty"` Private []byte `protobuf:"bytes,11,opt,name=private,proto3" json:"private,omitempty"` State string `protobuf:"bytes,12,opt,name=state,proto3" json:"state,omitempty"` StateAt int64 `protobuf:"varint,13,opt,name=state_at,json=stateAt,proto3" json:"state_at,omitempty"` Trusted []byte `protobuf:"bytes,14,opt,name=trusted,proto3" json:"trusted,omitempty"` IsChan bool `protobuf:"varint,17,opt,name=is_chan,json=isChan,proto3" json:"is_chan,omitempty"` // 17! Online bool `protobuf:"varint,18,opt,name=online,proto3" json:"online,omitempty"` // P2P only: other user's last online timestamp & user agent LastSeenTime int64 `protobuf:"varint,15,opt,name=last_seen_time,json=lastSeenTime,proto3" json:"last_seen_time,omitempty"` LastSeenUserAgent string `protobuf:"bytes,16,opt,name=last_seen_user_agent,json=lastSeenUserAgent,proto3" json:"last_seen_user_agent,omitempty"` } func (x *TopicDesc) Reset() { *x = TopicDesc{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[23] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *TopicDesc) String() string { return protoimpl.X.MessageStringOf(x) } func (*TopicDesc) ProtoMessage() {} func (x *TopicDesc) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[23] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use TopicDesc.ProtoReflect.Descriptor instead. func (*TopicDesc) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{23} } func (x *TopicDesc) GetCreatedAt() int64 { if x != nil { return x.CreatedAt } return 0 } func (x *TopicDesc) GetUpdatedAt() int64 { if x != nil { return x.UpdatedAt } return 0 } func (x *TopicDesc) GetTouchedAt() int64 { if x != nil { return x.TouchedAt } return 0 } func (x *TopicDesc) GetDefacs() *DefaultAcsMode { if x != nil { return x.Defacs } return nil } func (x *TopicDesc) GetAcs() *AccessMode { if x != nil { return x.Acs } return nil } func (x *TopicDesc) GetSeqId() int32 { if x != nil { return x.SeqId } return 0 } func (x *TopicDesc) GetReadId() int32 { if x != nil { return x.ReadId } return 0 } func (x *TopicDesc) GetRecvId() int32 { if x != nil { return x.RecvId } return 0 } func (x *TopicDesc) GetDelId() int32 { if x != nil { return x.DelId } return 0 } func (x *TopicDesc) GetPublic() []byte { if x != nil { return x.Public } return nil } func (x *TopicDesc) GetPrivate() []byte { if x != nil { return x.Private } return nil } func (x *TopicDesc) GetState() string { if x != nil { return x.State } return "" } func (x *TopicDesc) GetStateAt() int64 { if x != nil { return x.StateAt } return 0 } func (x *TopicDesc) GetTrusted() []byte { if x != nil { return x.Trusted } return nil } func (x *TopicDesc) GetIsChan() bool { if x != nil { return x.IsChan } return false } func (x *TopicDesc) GetOnline() bool { if x != nil { return x.Online } return false } func (x *TopicDesc) GetLastSeenTime() int64 { if x != nil { return x.LastSeenTime } return 0 } func (x *TopicDesc) GetLastSeenUserAgent() string { if x != nil { return x.LastSeenUserAgent } return "" } // MsgTopicSub: topic subscription details, sent in Meta message type TopicSub struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields UpdatedAt int64 `protobuf:"varint,1,opt,name=updated_at,json=updatedAt,proto3" json:"updated_at,omitempty"` DeletedAt int64 `protobuf:"varint,2,opt,name=deleted_at,json=deletedAt,proto3" json:"deleted_at,omitempty"` Online bool `protobuf:"varint,3,opt,name=online,proto3" json:"online,omitempty"` Acs *AccessMode `protobuf:"bytes,4,opt,name=acs,proto3" json:"acs,omitempty"` ReadId int32 `protobuf:"varint,5,opt,name=read_id,json=readId,proto3" json:"read_id,omitempty"` RecvId int32 `protobuf:"varint,6,opt,name=recv_id,json=recvId,proto3" json:"recv_id,omitempty"` Public []byte `protobuf:"bytes,7,opt,name=public,proto3" json:"public,omitempty"` Trusted []byte `protobuf:"bytes,16,opt,name=trusted,proto3" json:"trusted,omitempty"` // 16! Private []byte `protobuf:"bytes,8,opt,name=private,proto3" json:"private,omitempty"` // Uid of the subscribed user UserId string `protobuf:"bytes,9,opt,name=user_id,json=userId,proto3" json:"user_id,omitempty"` // Topic name of this subscription Topic string `protobuf:"bytes,10,opt,name=topic,proto3" json:"topic,omitempty"` TouchedAt int64 `protobuf:"varint,11,opt,name=touched_at,json=touchedAt,proto3" json:"touched_at,omitempty"` // ID of the last {data} message in a topic SeqId int32 `protobuf:"varint,12,opt,name=seq_id,json=seqId,proto3" json:"seq_id,omitempty"` // Messages are deleted up to this ID DelId int32 `protobuf:"varint,13,opt,name=del_id,json=delId,proto3" json:"del_id,omitempty"` // Other user's last online timestamp & user agent LastSeenTime int64 `protobuf:"varint,14,opt,name=last_seen_time,json=lastSeenTime,proto3" json:"last_seen_time,omitempty"` LastSeenUserAgent string `protobuf:"bytes,15,opt,name=last_seen_user_agent,json=lastSeenUserAgent,proto3" json:"last_seen_user_agent,omitempty"` } func (x *TopicSub) Reset() { *x = TopicSub{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[24] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *TopicSub) String() string { return protoimpl.X.MessageStringOf(x) } func (*TopicSub) ProtoMessage() {} func (x *TopicSub) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[24] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use TopicSub.ProtoReflect.Descriptor instead. func (*TopicSub) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{24} } func (x *TopicSub) GetUpdatedAt() int64 { if x != nil { return x.UpdatedAt } return 0 } func (x *TopicSub) GetDeletedAt() int64 { if x != nil { return x.DeletedAt } return 0 } func (x *TopicSub) GetOnline() bool { if x != nil { return x.Online } return false } func (x *TopicSub) GetAcs() *AccessMode { if x != nil { return x.Acs } return nil } func (x *TopicSub) GetReadId() int32 { if x != nil { return x.ReadId } return 0 } func (x *TopicSub) GetRecvId() int32 { if x != nil { return x.RecvId } return 0 } func (x *TopicSub) GetPublic() []byte { if x != nil { return x.Public } return nil } func (x *TopicSub) GetTrusted() []byte { if x != nil { return x.Trusted } return nil } func (x *TopicSub) GetPrivate() []byte { if x != nil { return x.Private } return nil } func (x *TopicSub) GetUserId() string { if x != nil { return x.UserId } return "" } func (x *TopicSub) GetTopic() string { if x != nil { return x.Topic } return "" } func (x *TopicSub) GetTouchedAt() int64 { if x != nil { return x.TouchedAt } return 0 } func (x *TopicSub) GetSeqId() int32 { if x != nil { return x.SeqId } return 0 } func (x *TopicSub) GetDelId() int32 { if x != nil { return x.DelId } return 0 } func (x *TopicSub) GetLastSeenTime() int64 { if x != nil { return x.LastSeenTime } return 0 } func (x *TopicSub) GetLastSeenUserAgent() string { if x != nil { return x.LastSeenUserAgent } return "" } type DelValues struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields DelId int32 `protobuf:"varint,1,opt,name=del_id,json=delId,proto3" json:"del_id,omitempty"` DelSeq []*SeqRange `protobuf:"bytes,2,rep,name=del_seq,json=delSeq,proto3" json:"del_seq,omitempty"` } func (x *DelValues) Reset() { *x = DelValues{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[25] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *DelValues) String() string { return protoimpl.X.MessageStringOf(x) } func (*DelValues) ProtoMessage() {} func (x *DelValues) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[25] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use DelValues.ProtoReflect.Descriptor instead. func (*DelValues) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{25} } func (x *DelValues) GetDelId() int32 { if x != nil { return x.DelId } return 0 } func (x *DelValues) GetDelSeq() []*SeqRange { if x != nil { return x.DelSeq } return nil } // {ctrl} message type ServerCtrl struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` Topic string `protobuf:"bytes,2,opt,name=topic,proto3" json:"topic,omitempty"` Code int32 `protobuf:"varint,3,opt,name=code,proto3" json:"code,omitempty"` Text string `protobuf:"bytes,4,opt,name=text,proto3" json:"text,omitempty"` Params map[string][]byte `protobuf:"bytes,5,rep,name=params,proto3" json:"params,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` } func (x *ServerCtrl) Reset() { *x = ServerCtrl{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[26] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *ServerCtrl) String() string { return protoimpl.X.MessageStringOf(x) } func (*ServerCtrl) ProtoMessage() {} func (x *ServerCtrl) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[26] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use ServerCtrl.ProtoReflect.Descriptor instead. func (*ServerCtrl) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{26} } func (x *ServerCtrl) GetId() string { if x != nil { return x.Id } return "" } func (x *ServerCtrl) GetTopic() string { if x != nil { return x.Topic } return "" } func (x *ServerCtrl) GetCode() int32 { if x != nil { return x.Code } return 0 } func (x *ServerCtrl) GetText() string { if x != nil { return x.Text } return "" } func (x *ServerCtrl) GetParams() map[string][]byte { if x != nil { return x.Params } return nil } // {data} message type ServerData struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Topic string `protobuf:"bytes,1,opt,name=topic,proto3" json:"topic,omitempty"` // ID of the user who originated the message as {pub}, could be empty if sent by the system FromUserId string `protobuf:"bytes,2,opt,name=from_user_id,json=fromUserId,proto3" json:"from_user_id,omitempty"` // Timestamp when the message was sent. Timestamp int64 `protobuf:"varint,7,opt,name=timestamp,proto3" json:"timestamp,omitempty"` // Timestamp when the message was deleted or 0. Milliseconds since the epoch 01/01/1970 DeletedAt int64 `protobuf:"varint,3,opt,name=deleted_at,json=deletedAt,proto3" json:"deleted_at,omitempty"` SeqId int32 `protobuf:"varint,4,opt,name=seq_id,json=seqId,proto3" json:"seq_id,omitempty"` Head map[string][]byte `protobuf:"bytes,5,rep,name=head,proto3" json:"head,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` Content []byte `protobuf:"bytes,6,opt,name=content,proto3" json:"content,omitempty"` } func (x *ServerData) Reset() { *x = ServerData{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[27] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *ServerData) String() string { return protoimpl.X.MessageStringOf(x) } func (*ServerData) ProtoMessage() {} func (x *ServerData) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[27] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use ServerData.ProtoReflect.Descriptor instead. func (*ServerData) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{27} } func (x *ServerData) GetTopic() string { if x != nil { return x.Topic } return "" } func (x *ServerData) GetFromUserId() string { if x != nil { return x.FromUserId } return "" } func (x *ServerData) GetTimestamp() int64 { if x != nil { return x.Timestamp } return 0 } func (x *ServerData) GetDeletedAt() int64 { if x != nil { return x.DeletedAt } return 0 } func (x *ServerData) GetSeqId() int32 { if x != nil { return x.SeqId } return 0 } func (x *ServerData) GetHead() map[string][]byte { if x != nil { return x.Head } return nil } func (x *ServerData) GetContent() []byte { if x != nil { return x.Content } return nil } // {pres} message type ServerPres struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Topic string `protobuf:"bytes,1,opt,name=topic,proto3" json:"topic,omitempty"` Src string `protobuf:"bytes,2,opt,name=src,proto3" json:"src,omitempty"` What ServerPres_What `protobuf:"varint,3,opt,name=what,proto3,enum=pbx.ServerPres_What" json:"what,omitempty"` UserAgent string `protobuf:"bytes,4,opt,name=user_agent,json=userAgent,proto3" json:"user_agent,omitempty"` SeqId int32 `protobuf:"varint,5,opt,name=seq_id,json=seqId,proto3" json:"seq_id,omitempty"` DelId int32 `protobuf:"varint,6,opt,name=del_id,json=delId,proto3" json:"del_id,omitempty"` DelSeq []*SeqRange `protobuf:"bytes,7,rep,name=del_seq,json=delSeq,proto3" json:"del_seq,omitempty"` TargetUserId string `protobuf:"bytes,8,opt,name=target_user_id,json=targetUserId,proto3" json:"target_user_id,omitempty"` ActorUserId string `protobuf:"bytes,9,opt,name=actor_user_id,json=actorUserId,proto3" json:"actor_user_id,omitempty"` Acs *AccessMode `protobuf:"bytes,10,opt,name=acs,proto3" json:"acs,omitempty"` } func (x *ServerPres) Reset() { *x = ServerPres{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[28] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *ServerPres) String() string { return protoimpl.X.MessageStringOf(x) } func (*ServerPres) ProtoMessage() {} func (x *ServerPres) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[28] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use ServerPres.ProtoReflect.Descriptor instead. func (*ServerPres) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{28} } func (x *ServerPres) GetTopic() string { if x != nil { return x.Topic } return "" } func (x *ServerPres) GetSrc() string { if x != nil { return x.Src } return "" } func (x *ServerPres) GetWhat() ServerPres_What { if x != nil { return x.What } return ServerPres_X3 } func (x *ServerPres) GetUserAgent() string { if x != nil { return x.UserAgent } return "" } func (x *ServerPres) GetSeqId() int32 { if x != nil { return x.SeqId } return 0 } func (x *ServerPres) GetDelId() int32 { if x != nil { return x.DelId } return 0 } func (x *ServerPres) GetDelSeq() []*SeqRange { if x != nil { return x.DelSeq } return nil } func (x *ServerPres) GetTargetUserId() string { if x != nil { return x.TargetUserId } return "" } func (x *ServerPres) GetActorUserId() string { if x != nil { return x.ActorUserId } return "" } func (x *ServerPres) GetAcs() *AccessMode { if x != nil { return x.Acs } return nil } // {meta} message type ServerMeta struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` Topic string `protobuf:"bytes,2,opt,name=topic,proto3" json:"topic,omitempty"` Desc *TopicDesc `protobuf:"bytes,3,opt,name=desc,proto3" json:"desc,omitempty"` Sub []*TopicSub `protobuf:"bytes,4,rep,name=sub,proto3" json:"sub,omitempty"` Del *DelValues `protobuf:"bytes,5,opt,name=del,proto3" json:"del,omitempty"` Tags []string `protobuf:"bytes,6,rep,name=tags,proto3" json:"tags,omitempty"` Cred []*ServerCred `protobuf:"bytes,7,rep,name=cred,proto3" json:"cred,omitempty"` Aux map[string][]byte `protobuf:"bytes,8,rep,name=aux,proto3" json:"aux,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` } func (x *ServerMeta) Reset() { *x = ServerMeta{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[29] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *ServerMeta) String() string { return protoimpl.X.MessageStringOf(x) } func (*ServerMeta) ProtoMessage() {} func (x *ServerMeta) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[29] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use ServerMeta.ProtoReflect.Descriptor instead. func (*ServerMeta) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{29} } func (x *ServerMeta) GetId() string { if x != nil { return x.Id } return "" } func (x *ServerMeta) GetTopic() string { if x != nil { return x.Topic } return "" } func (x *ServerMeta) GetDesc() *TopicDesc { if x != nil { return x.Desc } return nil } func (x *ServerMeta) GetSub() []*TopicSub { if x != nil { return x.Sub } return nil } func (x *ServerMeta) GetDel() *DelValues { if x != nil { return x.Del } return nil } func (x *ServerMeta) GetTags() []string { if x != nil { return x.Tags } return nil } func (x *ServerMeta) GetCred() []*ServerCred { if x != nil { return x.Cred } return nil } func (x *ServerMeta) GetAux() map[string][]byte { if x != nil { return x.Aux } return nil } // {info} message: server-side copy of ClientNote with From and optional Src added. type ServerInfo struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Topic string `protobuf:"bytes,1,opt,name=topic,proto3" json:"topic,omitempty"` FromUserId string `protobuf:"bytes,2,opt,name=from_user_id,json=fromUserId,proto3" json:"from_user_id,omitempty"` What InfoNote `protobuf:"varint,3,opt,name=what,proto3,enum=pbx.InfoNote" json:"what,omitempty"` SeqId int32 `protobuf:"varint,4,opt,name=seq_id,json=seqId,proto3" json:"seq_id,omitempty"` Src string `protobuf:"bytes,5,opt,name=src,proto3" json:"src,omitempty"` Event CallEvent `protobuf:"varint,6,opt,name=event,proto3,enum=pbx.CallEvent" json:"event,omitempty"` Payload []byte `protobuf:"bytes,7,opt,name=payload,proto3" json:"payload,omitempty"` } func (x *ServerInfo) Reset() { *x = ServerInfo{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[30] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *ServerInfo) String() string { return protoimpl.X.MessageStringOf(x) } func (*ServerInfo) ProtoMessage() {} func (x *ServerInfo) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[30] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use ServerInfo.ProtoReflect.Descriptor instead. func (*ServerInfo) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{30} } func (x *ServerInfo) GetTopic() string { if x != nil { return x.Topic } return "" } func (x *ServerInfo) GetFromUserId() string { if x != nil { return x.FromUserId } return "" } func (x *ServerInfo) GetWhat() InfoNote { if x != nil { return x.What } return InfoNote_X1 } func (x *ServerInfo) GetSeqId() int32 { if x != nil { return x.SeqId } return 0 } func (x *ServerInfo) GetSrc() string { if x != nil { return x.Src } return "" } func (x *ServerInfo) GetEvent() CallEvent { if x != nil { return x.Event } return CallEvent_X2 } func (x *ServerInfo) GetPayload() []byte { if x != nil { return x.Payload } return nil } // Cumulative message type ServerMsg struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields // Types that are assignable to Message: // *ServerMsg_Ctrl // *ServerMsg_Data // *ServerMsg_Pres // *ServerMsg_Meta // *ServerMsg_Info Message isServerMsg_Message `protobuf_oneof:"Message"` // DEPRECATED. Will be removed soon. // When response is sent to Root, send internal topic name too. // // Deprecated: Do not use. Topic string `protobuf:"bytes,6,opt,name=topic,proto3" json:"topic,omitempty"` } func (x *ServerMsg) Reset() { *x = ServerMsg{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[31] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *ServerMsg) String() string { return protoimpl.X.MessageStringOf(x) } func (*ServerMsg) ProtoMessage() {} func (x *ServerMsg) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[31] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use ServerMsg.ProtoReflect.Descriptor instead. func (*ServerMsg) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{31} } func (m *ServerMsg) GetMessage() isServerMsg_Message { if m != nil { return m.Message } return nil } func (x *ServerMsg) GetCtrl() *ServerCtrl { if x, ok := x.GetMessage().(*ServerMsg_Ctrl); ok { return x.Ctrl } return nil } func (x *ServerMsg) GetData() *ServerData { if x, ok := x.GetMessage().(*ServerMsg_Data); ok { return x.Data } return nil } func (x *ServerMsg) GetPres() *ServerPres { if x, ok := x.GetMessage().(*ServerMsg_Pres); ok { return x.Pres } return nil } func (x *ServerMsg) GetMeta() *ServerMeta { if x, ok := x.GetMessage().(*ServerMsg_Meta); ok { return x.Meta } return nil } func (x *ServerMsg) GetInfo() *ServerInfo { if x, ok := x.GetMessage().(*ServerMsg_Info); ok { return x.Info } return nil } // Deprecated: Do not use. func (x *ServerMsg) GetTopic() string { if x != nil { return x.Topic } return "" } type isServerMsg_Message interface { isServerMsg_Message() } type ServerMsg_Ctrl struct { Ctrl *ServerCtrl `protobuf:"bytes,1,opt,name=ctrl,proto3,oneof"` } type ServerMsg_Data struct { Data *ServerData `protobuf:"bytes,2,opt,name=data,proto3,oneof"` } type ServerMsg_Pres struct { Pres *ServerPres `protobuf:"bytes,3,opt,name=pres,proto3,oneof"` } type ServerMsg_Meta struct { Meta *ServerMeta `protobuf:"bytes,4,opt,name=meta,proto3,oneof"` } type ServerMsg_Info struct { Info *ServerInfo `protobuf:"bytes,5,opt,name=info,proto3,oneof"` } func (*ServerMsg_Ctrl) isServerMsg_Message() {} func (*ServerMsg_Data) isServerMsg_Message() {} func (*ServerMsg_Pres) isServerMsg_Message() {} func (*ServerMsg_Meta) isServerMsg_Message() {} func (*ServerMsg_Info) isServerMsg_Message() {} type ServerResp struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Status RespCode `protobuf:"varint,1,opt,name=status,proto3,enum=pbx.RespCode" json:"status,omitempty"` Srvmsg *ServerMsg `protobuf:"bytes,2,opt,name=srvmsg,proto3" json:"srvmsg,omitempty"` Clmsg *ClientMsg `protobuf:"bytes,3,opt,name=clmsg,proto3" json:"clmsg,omitempty"` } func (x *ServerResp) Reset() { *x = ServerResp{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[32] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *ServerResp) String() string { return protoimpl.X.MessageStringOf(x) } func (*ServerResp) ProtoMessage() {} func (x *ServerResp) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[32] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use ServerResp.ProtoReflect.Descriptor instead. func (*ServerResp) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{32} } func (x *ServerResp) GetStatus() RespCode { if x != nil { return x.Status } return RespCode_CONTINUE } func (x *ServerResp) GetSrvmsg() *ServerMsg { if x != nil { return x.Srvmsg } return nil } func (x *ServerResp) GetClmsg() *ClientMsg { if x != nil { return x.Clmsg } return nil } // Context message type Session struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields SessionId string `protobuf:"bytes,1,opt,name=session_id,json=sessionId,proto3" json:"session_id,omitempty"` UserId string `protobuf:"bytes,2,opt,name=user_id,json=userId,proto3" json:"user_id,omitempty"` AuthLevel AuthLevel `protobuf:"varint,3,opt,name=auth_level,json=authLevel,proto3,enum=pbx.AuthLevel" json:"auth_level,omitempty"` RemoteAddr string `protobuf:"bytes,4,opt,name=remote_addr,json=remoteAddr,proto3" json:"remote_addr,omitempty"` UserAgent string `protobuf:"bytes,5,opt,name=user_agent,json=userAgent,proto3" json:"user_agent,omitempty"` DeviceId string `protobuf:"bytes,6,opt,name=device_id,json=deviceId,proto3" json:"device_id,omitempty"` Language string `protobuf:"bytes,7,opt,name=language,proto3" json:"language,omitempty"` } func (x *Session) Reset() { *x = Session{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[33] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *Session) String() string { return protoimpl.X.MessageStringOf(x) } func (*Session) ProtoMessage() {} func (x *Session) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[33] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use Session.ProtoReflect.Descriptor instead. func (*Session) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{33} } func (x *Session) GetSessionId() string { if x != nil { return x.SessionId } return "" } func (x *Session) GetUserId() string { if x != nil { return x.UserId } return "" } func (x *Session) GetAuthLevel() AuthLevel { if x != nil { return x.AuthLevel } return AuthLevel_NONE } func (x *Session) GetRemoteAddr() string { if x != nil { return x.RemoteAddr } return "" } func (x *Session) GetUserAgent() string { if x != nil { return x.UserAgent } return "" } func (x *Session) GetDeviceId() string { if x != nil { return x.DeviceId } return "" } func (x *Session) GetLanguage() string { if x != nil { return x.Language } return "" } type ClientReq struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Msg *ClientMsg `protobuf:"bytes,1,opt,name=msg,proto3" json:"msg,omitempty"` Sess *Session `protobuf:"bytes,2,opt,name=sess,proto3" json:"sess,omitempty"` } func (x *ClientReq) Reset() { *x = ClientReq{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[34] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *ClientReq) String() string { return protoimpl.X.MessageStringOf(x) } func (*ClientReq) ProtoMessage() {} func (x *ClientReq) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[34] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use ClientReq.ProtoReflect.Descriptor instead. func (*ClientReq) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{34} } func (x *ClientReq) GetMsg() *ClientMsg { if x != nil { return x.Msg } return nil } func (x *ClientReq) GetSess() *Session { if x != nil { return x.Sess } return nil } type SearchQuery struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields UserId string `protobuf:"bytes,1,opt,name=user_id,json=userId,proto3" json:"user_id,omitempty"` Query string `protobuf:"bytes,2,opt,name=query,proto3" json:"query,omitempty"` } func (x *SearchQuery) Reset() { *x = SearchQuery{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[35] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *SearchQuery) String() string { return protoimpl.X.MessageStringOf(x) } func (*SearchQuery) ProtoMessage() {} func (x *SearchQuery) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[35] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use SearchQuery.ProtoReflect.Descriptor instead. func (*SearchQuery) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{35} } func (x *SearchQuery) GetUserId() string { if x != nil { return x.UserId } return "" } func (x *SearchQuery) GetQuery() string { if x != nil { return x.Query } return "" } type SearchFound struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Status RespCode `protobuf:"varint,1,opt,name=status,proto3,enum=pbx.RespCode" json:"status,omitempty"` // New search query If status == REPLACE, otherwise unset. Query string `protobuf:"bytes,2,opt,name=query,proto3" json:"query,omitempty"` // Search results. Result []*TopicSub `protobuf:"bytes,3,rep,name=result,proto3" json:"result,omitempty"` } func (x *SearchFound) Reset() { *x = SearchFound{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[36] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *SearchFound) String() string { return protoimpl.X.MessageStringOf(x) } func (*SearchFound) ProtoMessage() {} func (x *SearchFound) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[36] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use SearchFound.ProtoReflect.Descriptor instead. func (*SearchFound) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{36} } func (x *SearchFound) GetStatus() RespCode { if x != nil { return x.Status } return RespCode_CONTINUE } func (x *SearchFound) GetQuery() string { if x != nil { return x.Query } return "" } func (x *SearchFound) GetResult() []*TopicSub { if x != nil { return x.Result } return nil } type TopicEvent struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Action Crud `protobuf:"varint,1,opt,name=action,proto3,enum=pbx.Crud" json:"action,omitempty"` Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"` Desc *TopicDesc `protobuf:"bytes,3,opt,name=desc,proto3" json:"desc,omitempty"` } func (x *TopicEvent) Reset() { *x = TopicEvent{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[37] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *TopicEvent) String() string { return protoimpl.X.MessageStringOf(x) } func (*TopicEvent) ProtoMessage() {} func (x *TopicEvent) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[37] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use TopicEvent.ProtoReflect.Descriptor instead. func (*TopicEvent) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{37} } func (x *TopicEvent) GetAction() Crud { if x != nil { return x.Action } return Crud_CREATE } func (x *TopicEvent) GetName() string { if x != nil { return x.Name } return "" } func (x *TopicEvent) GetDesc() *TopicDesc { if x != nil { return x.Desc } return nil } type AccountEvent struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Action Crud `protobuf:"varint,1,opt,name=action,proto3,enum=pbx.Crud" json:"action,omitempty"` UserId string `protobuf:"bytes,2,opt,name=user_id,json=userId,proto3" json:"user_id,omitempty"` DefaultAcs *DefaultAcsMode `protobuf:"bytes,3,opt,name=default_acs,json=defaultAcs,proto3" json:"default_acs,omitempty"` Public []byte `protobuf:"bytes,4,opt,name=public,proto3" json:"public,omitempty"` // Indexable tags for user discovery Tags []string `protobuf:"bytes,8,rep,name=tags,proto3" json:"tags,omitempty"` } func (x *AccountEvent) Reset() { *x = AccountEvent{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[38] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *AccountEvent) String() string { return protoimpl.X.MessageStringOf(x) } func (*AccountEvent) ProtoMessage() {} func (x *AccountEvent) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[38] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use AccountEvent.ProtoReflect.Descriptor instead. func (*AccountEvent) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{38} } func (x *AccountEvent) GetAction() Crud { if x != nil { return x.Action } return Crud_CREATE } func (x *AccountEvent) GetUserId() string { if x != nil { return x.UserId } return "" } func (x *AccountEvent) GetDefaultAcs() *DefaultAcsMode { if x != nil { return x.DefaultAcs } return nil } func (x *AccountEvent) GetPublic() []byte { if x != nil { return x.Public } return nil } func (x *AccountEvent) GetTags() []string { if x != nil { return x.Tags } return nil } type SubscriptionEvent struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Action Crud `protobuf:"varint,1,opt,name=action,proto3,enum=pbx.Crud" json:"action,omitempty"` Topic string `protobuf:"bytes,2,opt,name=topic,proto3" json:"topic,omitempty"` UserId string `protobuf:"bytes,3,opt,name=user_id,json=userId,proto3" json:"user_id,omitempty"` DelId int32 `protobuf:"varint,4,opt,name=del_id,json=delId,proto3" json:"del_id,omitempty"` ReadId int32 `protobuf:"varint,5,opt,name=read_id,json=readId,proto3" json:"read_id,omitempty"` RecvId int32 `protobuf:"varint,6,opt,name=recv_id,json=recvId,proto3" json:"recv_id,omitempty"` Mode *AccessMode `protobuf:"bytes,7,opt,name=mode,proto3" json:"mode,omitempty"` Private []byte `protobuf:"bytes,8,opt,name=private,proto3" json:"private,omitempty"` } func (x *SubscriptionEvent) Reset() { *x = SubscriptionEvent{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[39] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *SubscriptionEvent) String() string { return protoimpl.X.MessageStringOf(x) } func (*SubscriptionEvent) ProtoMessage() {} func (x *SubscriptionEvent) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[39] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use SubscriptionEvent.ProtoReflect.Descriptor instead. func (*SubscriptionEvent) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{39} } func (x *SubscriptionEvent) GetAction() Crud { if x != nil { return x.Action } return Crud_CREATE } func (x *SubscriptionEvent) GetTopic() string { if x != nil { return x.Topic } return "" } func (x *SubscriptionEvent) GetUserId() string { if x != nil { return x.UserId } return "" } func (x *SubscriptionEvent) GetDelId() int32 { if x != nil { return x.DelId } return 0 } func (x *SubscriptionEvent) GetReadId() int32 { if x != nil { return x.ReadId } return 0 } func (x *SubscriptionEvent) GetRecvId() int32 { if x != nil { return x.RecvId } return 0 } func (x *SubscriptionEvent) GetMode() *AccessMode { if x != nil { return x.Mode } return nil } func (x *SubscriptionEvent) GetPrivate() []byte { if x != nil { return x.Private } return nil } type MessageEvent struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Action Crud `protobuf:"varint,1,opt,name=action,proto3,enum=pbx.Crud" json:"action,omitempty"` Msg *ServerData `protobuf:"bytes,2,opt,name=msg,proto3" json:"msg,omitempty"` } func (x *MessageEvent) Reset() { *x = MessageEvent{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[40] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *MessageEvent) String() string { return protoimpl.X.MessageStringOf(x) } func (*MessageEvent) ProtoMessage() {} func (x *MessageEvent) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[40] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use MessageEvent.ProtoReflect.Descriptor instead. func (*MessageEvent) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{40} } func (x *MessageEvent) GetAction() Crud { if x != nil { return x.Action } return Crud_CREATE } func (x *MessageEvent) GetMsg() *ServerData { if x != nil { return x.Msg } return nil } type Auth struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Scheme string `protobuf:"bytes,1,opt,name=scheme,proto3" json:"scheme,omitempty"` Secret string `protobuf:"bytes,2,opt,name=secret,proto3" json:"secret,omitempty"` } func (x *Auth) Reset() { *x = Auth{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[41] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *Auth) String() string { return protoimpl.X.MessageStringOf(x) } func (*Auth) ProtoMessage() {} func (x *Auth) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[41] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use Auth.ProtoReflect.Descriptor instead. func (*Auth) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{41} } func (x *Auth) GetScheme() string { if x != nil { return x.Scheme } return "" } func (x *Auth) GetSecret() string { if x != nil { return x.Secret } return "" } // File description. type FileMeta struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` MimeType string `protobuf:"bytes,2,opt,name=mime_type,json=mimeType,proto3" json:"mime_type,omitempty"` Etag string `protobuf:"bytes,3,opt,name=etag,proto3" json:"etag,omitempty"` Size int64 `protobuf:"varint,4,opt,name=size,proto3" json:"size,omitempty"` } func (x *FileMeta) Reset() { *x = FileMeta{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[42] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *FileMeta) String() string { return protoimpl.X.MessageStringOf(x) } func (*FileMeta) ProtoMessage() {} func (x *FileMeta) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[42] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use FileMeta.ProtoReflect.Descriptor instead. func (*FileMeta) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{42} } func (x *FileMeta) GetName() string { if x != nil { return x.Name } return "" } func (x *FileMeta) GetMimeType() string { if x != nil { return x.MimeType } return "" } func (x *FileMeta) GetEtag() string { if x != nil { return x.Etag } return "" } func (x *FileMeta) GetSize() int64 { if x != nil { return x.Size } return 0 } // File upload request. type FileUpReq struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields // Request ID. Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` // Request authentication credentials. Auth *Auth `protobuf:"bytes,2,opt,name=auth,proto3" json:"auth,omitempty"` // The topic this upload belongs to. Topic string `protobuf:"bytes,3,opt,name=topic,proto3" json:"topic,omitempty"` // Uploaded metadata. Meta *FileMeta `protobuf:"bytes,4,opt,name=meta,proto3" json:"meta,omitempty"` // File bytes being uploaded. Content []byte `protobuf:"bytes,5,opt,name=content,proto3" json:"content,omitempty"` } func (x *FileUpReq) Reset() { *x = FileUpReq{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[43] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *FileUpReq) String() string { return protoimpl.X.MessageStringOf(x) } func (*FileUpReq) ProtoMessage() {} func (x *FileUpReq) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[43] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use FileUpReq.ProtoReflect.Descriptor instead. func (*FileUpReq) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{43} } func (x *FileUpReq) GetId() string { if x != nil { return x.Id } return "" } func (x *FileUpReq) GetAuth() *Auth { if x != nil { return x.Auth } return nil } func (x *FileUpReq) GetTopic() string { if x != nil { return x.Topic } return "" } func (x *FileUpReq) GetMeta() *FileMeta { if x != nil { return x.Meta } return nil } func (x *FileUpReq) GetContent() []byte { if x != nil { return x.Content } return nil } // Response to file upload. type FileUpResp struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields // Response ID. Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` // Response code. Code int32 `protobuf:"varint,2,opt,name=code,proto3" json:"code,omitempty"` // Response text. Text string `protobuf:"bytes,3,opt,name=text,proto3" json:"text,omitempty"` Meta *FileMeta `protobuf:"bytes,4,opt,name=meta,proto3" json:"meta,omitempty"` // New upload location. RedirUrl string `protobuf:"bytes,5,opt,name=redir_url,json=redirUrl,proto3" json:"redir_url,omitempty"` } func (x *FileUpResp) Reset() { *x = FileUpResp{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[44] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *FileUpResp) String() string { return protoimpl.X.MessageStringOf(x) } func (*FileUpResp) ProtoMessage() {} func (x *FileUpResp) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[44] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use FileUpResp.ProtoReflect.Descriptor instead. func (*FileUpResp) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{44} } func (x *FileUpResp) GetId() string { if x != nil { return x.Id } return "" } func (x *FileUpResp) GetCode() int32 { if x != nil { return x.Code } return 0 } func (x *FileUpResp) GetText() string { if x != nil { return x.Text } return "" } func (x *FileUpResp) GetMeta() *FileMeta { if x != nil { return x.Meta } return nil } func (x *FileUpResp) GetRedirUrl() string { if x != nil { return x.RedirUrl } return "" } // File download request. type FileDownReq struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields // Request ID Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` // Request authentication credentials. Auth *Auth `protobuf:"bytes,2,opt,name=auth,proto3" json:"auth,omitempty"` // File URI to download. Uri string `protobuf:"bytes,3,opt,name=uri,proto3" json:"uri,omitempty"` // ETag IfModified string `protobuf:"bytes,4,opt,name=if_modified,json=ifModified,proto3" json:"if_modified,omitempty"` } func (x *FileDownReq) Reset() { *x = FileDownReq{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[45] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *FileDownReq) String() string { return protoimpl.X.MessageStringOf(x) } func (*FileDownReq) ProtoMessage() {} func (x *FileDownReq) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[45] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use FileDownReq.ProtoReflect.Descriptor instead. func (*FileDownReq) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{45} } func (x *FileDownReq) GetId() string { if x != nil { return x.Id } return "" } func (x *FileDownReq) GetAuth() *Auth { if x != nil { return x.Auth } return nil } func (x *FileDownReq) GetUri() string { if x != nil { return x.Uri } return "" } func (x *FileDownReq) GetIfModified() string { if x != nil { return x.IfModified } return "" } // Response to file download. type FileDownResp struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields // Response ID. Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` // Response code. Code int32 `protobuf:"varint,2,opt,name=code,proto3" json:"code,omitempty"` // Response text. Text string `protobuf:"bytes,3,opt,name=text,proto3" json:"text,omitempty"` Meta *FileMeta `protobuf:"bytes,4,opt,name=meta,proto3" json:"meta,omitempty"` // File location. RedirUrl string `protobuf:"bytes,5,opt,name=redir_url,json=redirUrl,proto3" json:"redir_url,omitempty"` // File bytes. Content []byte `protobuf:"bytes,6,opt,name=content,proto3" json:"content,omitempty"` } func (x *FileDownResp) Reset() { *x = FileDownResp{} if protoimpl.UnsafeEnabled { mi := &file_model_proto_msgTypes[46] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *FileDownResp) String() string { return protoimpl.X.MessageStringOf(x) } func (*FileDownResp) ProtoMessage() {} func (x *FileDownResp) ProtoReflect() protoreflect.Message { mi := &file_model_proto_msgTypes[46] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use FileDownResp.ProtoReflect.Descriptor instead. func (*FileDownResp) Descriptor() ([]byte, []int) { return file_model_proto_rawDescGZIP(), []int{46} } func (x *FileDownResp) GetId() string { if x != nil { return x.Id } return "" } func (x *FileDownResp) GetCode() int32 { if x != nil { return x.Code } return 0 } func (x *FileDownResp) GetText() string { if x != nil { return x.Text } return "" } func (x *FileDownResp) GetMeta() *FileMeta { if x != nil { return x.Meta } return nil } func (x *FileDownResp) GetRedirUrl() string { if x != nil { return x.RedirUrl } return "" } func (x *FileDownResp) GetContent() []byte { if x != nil { return x.Content } return nil } var File_model_proto protoreflect.FileDescriptor var file_model_proto_rawDesc = []byte{ 0x0a, 0x0b, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x03, 0x70, 0x62, 0x78, 0x22, 0x08, 0x0a, 0x06, 0x55, 0x6e, 0x75, 0x73, 0x65, 0x64, 0x22, 0x38, 0x0a, 0x0e, 0x44, 0x65, 0x66, 0x61, 0x75, 0x6c, 0x74, 0x41, 0x63, 0x73, 0x4d, 0x6f, 0x64, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x61, 0x75, 0x74, 0x68, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x61, 0x75, 0x74, 0x68, 0x12, 0x12, 0x0a, 0x04, 0x61, 0x6e, 0x6f, 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x61, 0x6e, 0x6f, 0x6e, 0x22, 0x36, 0x0a, 0x0a, 0x41, 0x63, 0x63, 0x65, 0x73, 0x73, 0x4d, 0x6f, 0x64, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x77, 0x61, 0x6e, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x77, 0x61, 0x6e, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x67, 0x69, 0x76, 0x65, 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x67, 0x69, 0x76, 0x65, 0x6e, 0x22, 0x35, 0x0a, 0x06, 0x53, 0x65, 0x74, 0x53, 0x75, 0x62, 0x12, 0x17, 0x0a, 0x07, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x75, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x6d, 0x6f, 0x64, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6d, 0x6f, 0x64, 0x65, 0x22, 0xc6, 0x01, 0x0a, 0x0a, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x43, 0x72, 0x65, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x6d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x6d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x33, 0x0a, 0x06, 0x70, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x18, 0x04, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x1b, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x43, 0x72, 0x65, 0x64, 0x2e, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x06, 0x70, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x1a, 0x39, 0x0a, 0x0b, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x22, 0x8b, 0x01, 0x0a, 0x07, 0x53, 0x65, 0x74, 0x44, 0x65, 0x73, 0x63, 0x12, 0x34, 0x0a, 0x0b, 0x64, 0x65, 0x66, 0x61, 0x75, 0x6c, 0x74, 0x5f, 0x61, 0x63, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x13, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x44, 0x65, 0x66, 0x61, 0x75, 0x6c, 0x74, 0x41, 0x63, 0x73, 0x4d, 0x6f, 0x64, 0x65, 0x52, 0x0a, 0x64, 0x65, 0x66, 0x61, 0x75, 0x6c, 0x74, 0x41, 0x63, 0x73, 0x12, 0x16, 0x0a, 0x06, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x06, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x12, 0x18, 0x0a, 0x07, 0x70, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, 0x70, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x74, 0x72, 0x75, 0x73, 0x74, 0x65, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, 0x74, 0x72, 0x75, 0x73, 0x74, 0x65, 0x64, 0x22, 0x2c, 0x0a, 0x08, 0x53, 0x65, 0x71, 0x52, 0x61, 0x6e, 0x67, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x6c, 0x6f, 0x77, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x03, 0x6c, 0x6f, 0x77, 0x12, 0x0e, 0x0a, 0x02, 0x68, 0x69, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x02, 0x68, 0x69, 0x22, 0xd4, 0x01, 0x0a, 0x07, 0x47, 0x65, 0x74, 0x4f, 0x70, 0x74, 0x73, 0x12, 0x2a, 0x0a, 0x11, 0x69, 0x66, 0x5f, 0x6d, 0x6f, 0x64, 0x69, 0x66, 0x69, 0x65, 0x64, 0x5f, 0x73, 0x69, 0x6e, 0x63, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0f, 0x69, 0x66, 0x4d, 0x6f, 0x64, 0x69, 0x66, 0x69, 0x65, 0x64, 0x53, 0x69, 0x6e, 0x63, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x75, 0x73, 0x65, 0x72, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x75, 0x73, 0x65, 0x72, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x12, 0x19, 0x0a, 0x08, 0x73, 0x69, 0x6e, 0x63, 0x65, 0x5f, 0x69, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, 0x05, 0x52, 0x07, 0x73, 0x69, 0x6e, 0x63, 0x65, 0x49, 0x64, 0x12, 0x1b, 0x0a, 0x09, 0x62, 0x65, 0x66, 0x6f, 0x72, 0x65, 0x5f, 0x69, 0x64, 0x18, 0x05, 0x20, 0x01, 0x28, 0x05, 0x52, 0x08, 0x62, 0x65, 0x66, 0x6f, 0x72, 0x65, 0x49, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x6c, 0x69, 0x6d, 0x69, 0x74, 0x18, 0x06, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x6c, 0x69, 0x6d, 0x69, 0x74, 0x12, 0x25, 0x0a, 0x06, 0x72, 0x61, 0x6e, 0x67, 0x65, 0x73, 0x18, 0x07, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0d, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x53, 0x65, 0x71, 0x52, 0x61, 0x6e, 0x67, 0x65, 0x52, 0x06, 0x72, 0x61, 0x6e, 0x67, 0x65, 0x73, 0x22, 0x82, 0x01, 0x0a, 0x08, 0x47, 0x65, 0x74, 0x51, 0x75, 0x65, 0x72, 0x79, 0x12, 0x12, 0x0a, 0x04, 0x77, 0x68, 0x61, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x77, 0x68, 0x61, 0x74, 0x12, 0x20, 0x0a, 0x04, 0x64, 0x65, 0x73, 0x63, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x47, 0x65, 0x74, 0x4f, 0x70, 0x74, 0x73, 0x52, 0x04, 0x64, 0x65, 0x73, 0x63, 0x12, 0x1e, 0x0a, 0x03, 0x73, 0x75, 0x62, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x47, 0x65, 0x74, 0x4f, 0x70, 0x74, 0x73, 0x52, 0x03, 0x73, 0x75, 0x62, 0x12, 0x20, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x47, 0x65, 0x74, 0x4f, 0x70, 0x74, 0x73, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x22, 0xe6, 0x01, 0x0a, 0x08, 0x53, 0x65, 0x74, 0x51, 0x75, 0x65, 0x72, 0x79, 0x12, 0x20, 0x0a, 0x04, 0x64, 0x65, 0x73, 0x63, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x53, 0x65, 0x74, 0x44, 0x65, 0x73, 0x63, 0x52, 0x04, 0x64, 0x65, 0x73, 0x63, 0x12, 0x1d, 0x0a, 0x03, 0x73, 0x75, 0x62, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0b, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x53, 0x65, 0x74, 0x53, 0x75, 0x62, 0x52, 0x03, 0x73, 0x75, 0x62, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x61, 0x67, 0x73, 0x18, 0x03, 0x20, 0x03, 0x28, 0x09, 0x52, 0x04, 0x74, 0x61, 0x67, 0x73, 0x12, 0x23, 0x0a, 0x04, 0x63, 0x72, 0x65, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x43, 0x72, 0x65, 0x64, 0x52, 0x04, 0x63, 0x72, 0x65, 0x64, 0x12, 0x28, 0x0a, 0x03, 0x61, 0x75, 0x78, 0x18, 0x05, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x16, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x53, 0x65, 0x74, 0x51, 0x75, 0x65, 0x72, 0x79, 0x2e, 0x41, 0x75, 0x78, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x03, 0x61, 0x75, 0x78, 0x1a, 0x36, 0x0a, 0x08, 0x41, 0x75, 0x78, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x22, 0xb8, 0x01, 0x0a, 0x08, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x48, 0x69, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x1d, 0x0a, 0x0a, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x61, 0x67, 0x65, 0x6e, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x75, 0x73, 0x65, 0x72, 0x41, 0x67, 0x65, 0x6e, 0x74, 0x12, 0x10, 0x0a, 0x03, 0x76, 0x65, 0x72, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x76, 0x65, 0x72, 0x12, 0x1b, 0x0a, 0x09, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65, 0x5f, 0x69, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65, 0x49, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x6c, 0x61, 0x6e, 0x67, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6c, 0x61, 0x6e, 0x67, 0x12, 0x1a, 0x0a, 0x08, 0x70, 0x6c, 0x61, 0x74, 0x66, 0x6f, 0x72, 0x6d, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x70, 0x6c, 0x61, 0x74, 0x66, 0x6f, 0x72, 0x6d, 0x12, 0x1e, 0x0a, 0x0a, 0x62, 0x61, 0x63, 0x6b, 0x67, 0x72, 0x6f, 0x75, 0x6e, 0x64, 0x18, 0x07, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0a, 0x62, 0x61, 0x63, 0x6b, 0x67, 0x72, 0x6f, 0x75, 0x6e, 0x64, 0x22, 0xee, 0x02, 0x0a, 0x09, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x41, 0x63, 0x63, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x17, 0x0a, 0x07, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x75, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x06, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x18, 0x05, 0x20, 0x01, 0x28, 0x08, 0x52, 0x05, 0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x61, 0x67, 0x73, 0x18, 0x06, 0x20, 0x03, 0x28, 0x09, 0x52, 0x04, 0x74, 0x61, 0x67, 0x73, 0x12, 0x20, 0x0a, 0x04, 0x64, 0x65, 0x73, 0x63, 0x18, 0x07, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x53, 0x65, 0x74, 0x44, 0x65, 0x73, 0x63, 0x52, 0x04, 0x64, 0x65, 0x73, 0x63, 0x12, 0x23, 0x0a, 0x04, 0x63, 0x72, 0x65, 0x64, 0x18, 0x08, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x43, 0x72, 0x65, 0x64, 0x52, 0x04, 0x63, 0x72, 0x65, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x18, 0x09, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x12, 0x14, 0x0a, 0x05, 0x73, 0x74, 0x61, 0x74, 0x65, 0x18, 0x0a, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x73, 0x74, 0x61, 0x74, 0x65, 0x12, 0x2d, 0x0a, 0x0a, 0x61, 0x75, 0x74, 0x68, 0x5f, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x18, 0x0b, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x0e, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x41, 0x75, 0x74, 0x68, 0x4c, 0x65, 0x76, 0x65, 0x6c, 0x52, 0x09, 0x61, 0x75, 0x74, 0x68, 0x4c, 0x65, 0x76, 0x65, 0x6c, 0x12, 0x1d, 0x0a, 0x0a, 0x74, 0x6d, 0x70, 0x5f, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x65, 0x18, 0x0c, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x74, 0x6d, 0x70, 0x53, 0x63, 0x68, 0x65, 0x6d, 0x65, 0x12, 0x1d, 0x0a, 0x0a, 0x74, 0x6d, 0x70, 0x5f, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0x18, 0x0d, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x09, 0x74, 0x6d, 0x70, 0x53, 0x65, 0x63, 0x72, 0x65, 0x74, 0x22, 0x72, 0x0a, 0x0b, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x06, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0x12, 0x23, 0x0a, 0x04, 0x63, 0x72, 0x65, 0x64, 0x18, 0x04, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x43, 0x72, 0x65, 0x64, 0x52, 0x04, 0x63, 0x72, 0x65, 0x64, 0x22, 0x89, 0x01, 0x0a, 0x09, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x53, 0x75, 0x62, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x12, 0x2a, 0x0a, 0x09, 0x73, 0x65, 0x74, 0x5f, 0x71, 0x75, 0x65, 0x72, 0x79, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0d, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x53, 0x65, 0x74, 0x51, 0x75, 0x65, 0x72, 0x79, 0x52, 0x08, 0x73, 0x65, 0x74, 0x51, 0x75, 0x65, 0x72, 0x79, 0x12, 0x2a, 0x0a, 0x09, 0x67, 0x65, 0x74, 0x5f, 0x71, 0x75, 0x65, 0x72, 0x79, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0d, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x47, 0x65, 0x74, 0x51, 0x75, 0x65, 0x72, 0x79, 0x52, 0x08, 0x67, 0x65, 0x74, 0x51, 0x75, 0x65, 0x72, 0x79, 0x22, 0x49, 0x0a, 0x0b, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x4c, 0x65, 0x61, 0x76, 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x12, 0x14, 0x0a, 0x05, 0x75, 0x6e, 0x73, 0x75, 0x62, 0x18, 0x03, 0x20, 0x01, 0x28, 0x08, 0x52, 0x05, 0x75, 0x6e, 0x73, 0x75, 0x62, 0x22, 0xcb, 0x01, 0x0a, 0x09, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x50, 0x75, 0x62, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x12, 0x17, 0x0a, 0x07, 0x6e, 0x6f, 0x5f, 0x65, 0x63, 0x68, 0x6f, 0x18, 0x03, 0x20, 0x01, 0x28, 0x08, 0x52, 0x06, 0x6e, 0x6f, 0x45, 0x63, 0x68, 0x6f, 0x12, 0x2c, 0x0a, 0x04, 0x68, 0x65, 0x61, 0x64, 0x18, 0x04, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x18, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x50, 0x75, 0x62, 0x2e, 0x48, 0x65, 0x61, 0x64, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x04, 0x68, 0x65, 0x61, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x1a, 0x37, 0x0a, 0x09, 0x48, 0x65, 0x61, 0x64, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x22, 0x56, 0x0a, 0x09, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x47, 0x65, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x12, 0x23, 0x0a, 0x05, 0x71, 0x75, 0x65, 0x72, 0x79, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0d, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x47, 0x65, 0x74, 0x51, 0x75, 0x65, 0x72, 0x79, 0x52, 0x05, 0x71, 0x75, 0x65, 0x72, 0x79, 0x22, 0x56, 0x0a, 0x09, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x53, 0x65, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x12, 0x23, 0x0a, 0x05, 0x71, 0x75, 0x65, 0x72, 0x79, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0d, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x53, 0x65, 0x74, 0x51, 0x75, 0x65, 0x72, 0x79, 0x52, 0x05, 0x71, 0x75, 0x65, 0x72, 0x79, 0x22, 0x95, 0x02, 0x0a, 0x09, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x44, 0x65, 0x6c, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x12, 0x27, 0x0a, 0x04, 0x77, 0x68, 0x61, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x13, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x44, 0x65, 0x6c, 0x2e, 0x57, 0x68, 0x61, 0x74, 0x52, 0x04, 0x77, 0x68, 0x61, 0x74, 0x12, 0x26, 0x0a, 0x07, 0x64, 0x65, 0x6c, 0x5f, 0x73, 0x65, 0x71, 0x18, 0x04, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0d, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x53, 0x65, 0x71, 0x52, 0x61, 0x6e, 0x67, 0x65, 0x52, 0x06, 0x64, 0x65, 0x6c, 0x53, 0x65, 0x71, 0x12, 0x17, 0x0a, 0x07, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x75, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x23, 0x0a, 0x04, 0x63, 0x72, 0x65, 0x64, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x43, 0x72, 0x65, 0x64, 0x52, 0x04, 0x63, 0x72, 0x65, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x68, 0x61, 0x72, 0x64, 0x18, 0x07, 0x20, 0x01, 0x28, 0x08, 0x52, 0x04, 0x68, 0x61, 0x72, 0x64, 0x22, 0x3f, 0x0a, 0x04, 0x57, 0x68, 0x61, 0x74, 0x12, 0x06, 0x0a, 0x02, 0x58, 0x30, 0x10, 0x00, 0x12, 0x07, 0x0a, 0x03, 0x4d, 0x53, 0x47, 0x10, 0x01, 0x12, 0x09, 0x0a, 0x05, 0x54, 0x4f, 0x50, 0x49, 0x43, 0x10, 0x02, 0x12, 0x07, 0x0a, 0x03, 0x53, 0x55, 0x42, 0x10, 0x03, 0x12, 0x08, 0x0a, 0x04, 0x55, 0x53, 0x45, 0x52, 0x10, 0x04, 0x12, 0x08, 0x0a, 0x04, 0x43, 0x52, 0x45, 0x44, 0x10, 0x05, 0x22, 0xb4, 0x01, 0x0a, 0x0a, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x4e, 0x6f, 0x74, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x12, 0x21, 0x0a, 0x04, 0x77, 0x68, 0x61, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x0d, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x49, 0x6e, 0x66, 0x6f, 0x4e, 0x6f, 0x74, 0x65, 0x52, 0x04, 0x77, 0x68, 0x61, 0x74, 0x12, 0x15, 0x0a, 0x06, 0x73, 0x65, 0x71, 0x5f, 0x69, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x73, 0x65, 0x71, 0x49, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x75, 0x6e, 0x72, 0x65, 0x61, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x75, 0x6e, 0x72, 0x65, 0x61, 0x64, 0x12, 0x24, 0x0a, 0x05, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x0e, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x43, 0x61, 0x6c, 0x6c, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x52, 0x05, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x22, 0x80, 0x01, 0x0a, 0x0b, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x45, 0x78, 0x74, 0x72, 0x61, 0x12, 0x20, 0x0a, 0x0b, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x09, 0x52, 0x0b, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x73, 0x12, 0x20, 0x0a, 0x0c, 0x6f, 0x6e, 0x5f, 0x62, 0x65, 0x68, 0x61, 0x6c, 0x66, 0x5f, 0x6f, 0x66, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a, 0x6f, 0x6e, 0x42, 0x65, 0x68, 0x61, 0x6c, 0x66, 0x4f, 0x66, 0x12, 0x2d, 0x0a, 0x0a, 0x61, 0x75, 0x74, 0x68, 0x5f, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x0e, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x41, 0x75, 0x74, 0x68, 0x4c, 0x65, 0x76, 0x65, 0x6c, 0x52, 0x09, 0x61, 0x75, 0x74, 0x68, 0x4c, 0x65, 0x76, 0x65, 0x6c, 0x22, 0xb2, 0x03, 0x0a, 0x09, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x4d, 0x73, 0x67, 0x12, 0x1f, 0x0a, 0x02, 0x68, 0x69, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0d, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x48, 0x69, 0x48, 0x00, 0x52, 0x02, 0x68, 0x69, 0x12, 0x22, 0x0a, 0x03, 0x61, 0x63, 0x63, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x41, 0x63, 0x63, 0x48, 0x00, 0x52, 0x03, 0x61, 0x63, 0x63, 0x12, 0x28, 0x0a, 0x05, 0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x10, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x48, 0x00, 0x52, 0x05, 0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x12, 0x22, 0x0a, 0x03, 0x73, 0x75, 0x62, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x53, 0x75, 0x62, 0x48, 0x00, 0x52, 0x03, 0x73, 0x75, 0x62, 0x12, 0x28, 0x0a, 0x05, 0x6c, 0x65, 0x61, 0x76, 0x65, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x10, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x4c, 0x65, 0x61, 0x76, 0x65, 0x48, 0x00, 0x52, 0x05, 0x6c, 0x65, 0x61, 0x76, 0x65, 0x12, 0x22, 0x0a, 0x03, 0x70, 0x75, 0x62, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x50, 0x75, 0x62, 0x48, 0x00, 0x52, 0x03, 0x70, 0x75, 0x62, 0x12, 0x22, 0x0a, 0x03, 0x67, 0x65, 0x74, 0x18, 0x07, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x47, 0x65, 0x74, 0x48, 0x00, 0x52, 0x03, 0x67, 0x65, 0x74, 0x12, 0x22, 0x0a, 0x03, 0x73, 0x65, 0x74, 0x18, 0x08, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x53, 0x65, 0x74, 0x48, 0x00, 0x52, 0x03, 0x73, 0x65, 0x74, 0x12, 0x22, 0x0a, 0x03, 0x64, 0x65, 0x6c, 0x18, 0x09, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x44, 0x65, 0x6c, 0x48, 0x00, 0x52, 0x03, 0x64, 0x65, 0x6c, 0x12, 0x25, 0x0a, 0x04, 0x6e, 0x6f, 0x74, 0x65, 0x18, 0x0a, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x4e, 0x6f, 0x74, 0x65, 0x48, 0x00, 0x52, 0x04, 0x6e, 0x6f, 0x74, 0x65, 0x12, 0x26, 0x0a, 0x05, 0x65, 0x78, 0x74, 0x72, 0x61, 0x18, 0x0d, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x10, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x45, 0x78, 0x74, 0x72, 0x61, 0x52, 0x05, 0x65, 0x78, 0x74, 0x72, 0x61, 0x42, 0x09, 0x0a, 0x07, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x22, 0x4e, 0x0a, 0x0a, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x43, 0x72, 0x65, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x6d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x6d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x6f, 0x6e, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x08, 0x52, 0x04, 0x64, 0x6f, 0x6e, 0x65, 0x22, 0x9d, 0x04, 0x0a, 0x09, 0x54, 0x6f, 0x70, 0x69, 0x63, 0x44, 0x65, 0x73, 0x63, 0x12, 0x1d, 0x0a, 0x0a, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x5f, 0x61, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x09, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x41, 0x74, 0x12, 0x1d, 0x0a, 0x0a, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64, 0x5f, 0x61, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x09, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64, 0x41, 0x74, 0x12, 0x1d, 0x0a, 0x0a, 0x74, 0x6f, 0x75, 0x63, 0x68, 0x65, 0x64, 0x5f, 0x61, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x09, 0x74, 0x6f, 0x75, 0x63, 0x68, 0x65, 0x64, 0x41, 0x74, 0x12, 0x2b, 0x0a, 0x06, 0x64, 0x65, 0x66, 0x61, 0x63, 0x73, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x13, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x44, 0x65, 0x66, 0x61, 0x75, 0x6c, 0x74, 0x41, 0x63, 0x73, 0x4d, 0x6f, 0x64, 0x65, 0x52, 0x06, 0x64, 0x65, 0x66, 0x61, 0x63, 0x73, 0x12, 0x21, 0x0a, 0x03, 0x61, 0x63, 0x73, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x41, 0x63, 0x63, 0x65, 0x73, 0x73, 0x4d, 0x6f, 0x64, 0x65, 0x52, 0x03, 0x61, 0x63, 0x73, 0x12, 0x15, 0x0a, 0x06, 0x73, 0x65, 0x71, 0x5f, 0x69, 0x64, 0x18, 0x06, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x73, 0x65, 0x71, 0x49, 0x64, 0x12, 0x17, 0x0a, 0x07, 0x72, 0x65, 0x61, 0x64, 0x5f, 0x69, 0x64, 0x18, 0x07, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x72, 0x65, 0x61, 0x64, 0x49, 0x64, 0x12, 0x17, 0x0a, 0x07, 0x72, 0x65, 0x63, 0x76, 0x5f, 0x69, 0x64, 0x18, 0x08, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x72, 0x65, 0x63, 0x76, 0x49, 0x64, 0x12, 0x15, 0x0a, 0x06, 0x64, 0x65, 0x6c, 0x5f, 0x69, 0x64, 0x18, 0x09, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x64, 0x65, 0x6c, 0x49, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x18, 0x0a, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x06, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x12, 0x18, 0x0a, 0x07, 0x70, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65, 0x18, 0x0b, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, 0x70, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x73, 0x74, 0x61, 0x74, 0x65, 0x18, 0x0c, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x73, 0x74, 0x61, 0x74, 0x65, 0x12, 0x19, 0x0a, 0x08, 0x73, 0x74, 0x61, 0x74, 0x65, 0x5f, 0x61, 0x74, 0x18, 0x0d, 0x20, 0x01, 0x28, 0x03, 0x52, 0x07, 0x73, 0x74, 0x61, 0x74, 0x65, 0x41, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x74, 0x72, 0x75, 0x73, 0x74, 0x65, 0x64, 0x18, 0x0e, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, 0x74, 0x72, 0x75, 0x73, 0x74, 0x65, 0x64, 0x12, 0x17, 0x0a, 0x07, 0x69, 0x73, 0x5f, 0x63, 0x68, 0x61, 0x6e, 0x18, 0x11, 0x20, 0x01, 0x28, 0x08, 0x52, 0x06, 0x69, 0x73, 0x43, 0x68, 0x61, 0x6e, 0x12, 0x16, 0x0a, 0x06, 0x6f, 0x6e, 0x6c, 0x69, 0x6e, 0x65, 0x18, 0x12, 0x20, 0x01, 0x28, 0x08, 0x52, 0x06, 0x6f, 0x6e, 0x6c, 0x69, 0x6e, 0x65, 0x12, 0x24, 0x0a, 0x0e, 0x6c, 0x61, 0x73, 0x74, 0x5f, 0x73, 0x65, 0x65, 0x6e, 0x5f, 0x74, 0x69, 0x6d, 0x65, 0x18, 0x0f, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0c, 0x6c, 0x61, 0x73, 0x74, 0x53, 0x65, 0x65, 0x6e, 0x54, 0x69, 0x6d, 0x65, 0x12, 0x2f, 0x0a, 0x14, 0x6c, 0x61, 0x73, 0x74, 0x5f, 0x73, 0x65, 0x65, 0x6e, 0x5f, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x61, 0x67, 0x65, 0x6e, 0x74, 0x18, 0x10, 0x20, 0x01, 0x28, 0x09, 0x52, 0x11, 0x6c, 0x61, 0x73, 0x74, 0x53, 0x65, 0x65, 0x6e, 0x55, 0x73, 0x65, 0x72, 0x41, 0x67, 0x65, 0x6e, 0x74, 0x22, 0xd4, 0x03, 0x0a, 0x08, 0x54, 0x6f, 0x70, 0x69, 0x63, 0x53, 0x75, 0x62, 0x12, 0x1d, 0x0a, 0x0a, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64, 0x5f, 0x61, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x09, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64, 0x41, 0x74, 0x12, 0x1d, 0x0a, 0x0a, 0x64, 0x65, 0x6c, 0x65, 0x74, 0x65, 0x64, 0x5f, 0x61, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x09, 0x64, 0x65, 0x6c, 0x65, 0x74, 0x65, 0x64, 0x41, 0x74, 0x12, 0x16, 0x0a, 0x06, 0x6f, 0x6e, 0x6c, 0x69, 0x6e, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x08, 0x52, 0x06, 0x6f, 0x6e, 0x6c, 0x69, 0x6e, 0x65, 0x12, 0x21, 0x0a, 0x03, 0x61, 0x63, 0x73, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x41, 0x63, 0x63, 0x65, 0x73, 0x73, 0x4d, 0x6f, 0x64, 0x65, 0x52, 0x03, 0x61, 0x63, 0x73, 0x12, 0x17, 0x0a, 0x07, 0x72, 0x65, 0x61, 0x64, 0x5f, 0x69, 0x64, 0x18, 0x05, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x72, 0x65, 0x61, 0x64, 0x49, 0x64, 0x12, 0x17, 0x0a, 0x07, 0x72, 0x65, 0x63, 0x76, 0x5f, 0x69, 0x64, 0x18, 0x06, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x72, 0x65, 0x63, 0x76, 0x49, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x18, 0x07, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x06, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x12, 0x18, 0x0a, 0x07, 0x74, 0x72, 0x75, 0x73, 0x74, 0x65, 0x64, 0x18, 0x10, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, 0x74, 0x72, 0x75, 0x73, 0x74, 0x65, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x70, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65, 0x18, 0x08, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, 0x70, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65, 0x12, 0x17, 0x0a, 0x07, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x18, 0x09, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x75, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x18, 0x0a, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x12, 0x1d, 0x0a, 0x0a, 0x74, 0x6f, 0x75, 0x63, 0x68, 0x65, 0x64, 0x5f, 0x61, 0x74, 0x18, 0x0b, 0x20, 0x01, 0x28, 0x03, 0x52, 0x09, 0x74, 0x6f, 0x75, 0x63, 0x68, 0x65, 0x64, 0x41, 0x74, 0x12, 0x15, 0x0a, 0x06, 0x73, 0x65, 0x71, 0x5f, 0x69, 0x64, 0x18, 0x0c, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x73, 0x65, 0x71, 0x49, 0x64, 0x12, 0x15, 0x0a, 0x06, 0x64, 0x65, 0x6c, 0x5f, 0x69, 0x64, 0x18, 0x0d, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x64, 0x65, 0x6c, 0x49, 0x64, 0x12, 0x24, 0x0a, 0x0e, 0x6c, 0x61, 0x73, 0x74, 0x5f, 0x73, 0x65, 0x65, 0x6e, 0x5f, 0x74, 0x69, 0x6d, 0x65, 0x18, 0x0e, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0c, 0x6c, 0x61, 0x73, 0x74, 0x53, 0x65, 0x65, 0x6e, 0x54, 0x69, 0x6d, 0x65, 0x12, 0x2f, 0x0a, 0x14, 0x6c, 0x61, 0x73, 0x74, 0x5f, 0x73, 0x65, 0x65, 0x6e, 0x5f, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x61, 0x67, 0x65, 0x6e, 0x74, 0x18, 0x0f, 0x20, 0x01, 0x28, 0x09, 0x52, 0x11, 0x6c, 0x61, 0x73, 0x74, 0x53, 0x65, 0x65, 0x6e, 0x55, 0x73, 0x65, 0x72, 0x41, 0x67, 0x65, 0x6e, 0x74, 0x22, 0x4a, 0x0a, 0x09, 0x44, 0x65, 0x6c, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x73, 0x12, 0x15, 0x0a, 0x06, 0x64, 0x65, 0x6c, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x64, 0x65, 0x6c, 0x49, 0x64, 0x12, 0x26, 0x0a, 0x07, 0x64, 0x65, 0x6c, 0x5f, 0x73, 0x65, 0x71, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0d, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x53, 0x65, 0x71, 0x52, 0x61, 0x6e, 0x67, 0x65, 0x52, 0x06, 0x64, 0x65, 0x6c, 0x53, 0x65, 0x71, 0x22, 0xca, 0x01, 0x0a, 0x0a, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x43, 0x74, 0x72, 0x6c, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x65, 0x78, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x74, 0x65, 0x78, 0x74, 0x12, 0x33, 0x0a, 0x06, 0x70, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x18, 0x05, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x1b, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x43, 0x74, 0x72, 0x6c, 0x2e, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x06, 0x70, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x1a, 0x39, 0x0a, 0x0b, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x22, 0x9a, 0x02, 0x0a, 0x0a, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x44, 0x61, 0x74, 0x61, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x12, 0x20, 0x0a, 0x0c, 0x66, 0x72, 0x6f, 0x6d, 0x5f, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a, 0x66, 0x72, 0x6f, 0x6d, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x1c, 0x0a, 0x09, 0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x18, 0x07, 0x20, 0x01, 0x28, 0x03, 0x52, 0x09, 0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x12, 0x1d, 0x0a, 0x0a, 0x64, 0x65, 0x6c, 0x65, 0x74, 0x65, 0x64, 0x5f, 0x61, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x09, 0x64, 0x65, 0x6c, 0x65, 0x74, 0x65, 0x64, 0x41, 0x74, 0x12, 0x15, 0x0a, 0x06, 0x73, 0x65, 0x71, 0x5f, 0x69, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x73, 0x65, 0x71, 0x49, 0x64, 0x12, 0x2d, 0x0a, 0x04, 0x68, 0x65, 0x61, 0x64, 0x18, 0x05, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x19, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x44, 0x61, 0x74, 0x61, 0x2e, 0x48, 0x65, 0x61, 0x64, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x04, 0x68, 0x65, 0x61, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x1a, 0x37, 0x0a, 0x09, 0x48, 0x65, 0x61, 0x64, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x22, 0xc9, 0x03, 0x0a, 0x0a, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x50, 0x72, 0x65, 0x73, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x12, 0x10, 0x0a, 0x03, 0x73, 0x72, 0x63, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x73, 0x72, 0x63, 0x12, 0x28, 0x0a, 0x04, 0x77, 0x68, 0x61, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x14, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x50, 0x72, 0x65, 0x73, 0x2e, 0x57, 0x68, 0x61, 0x74, 0x52, 0x04, 0x77, 0x68, 0x61, 0x74, 0x12, 0x1d, 0x0a, 0x0a, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x61, 0x67, 0x65, 0x6e, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x75, 0x73, 0x65, 0x72, 0x41, 0x67, 0x65, 0x6e, 0x74, 0x12, 0x15, 0x0a, 0x06, 0x73, 0x65, 0x71, 0x5f, 0x69, 0x64, 0x18, 0x05, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x73, 0x65, 0x71, 0x49, 0x64, 0x12, 0x15, 0x0a, 0x06, 0x64, 0x65, 0x6c, 0x5f, 0x69, 0x64, 0x18, 0x06, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x64, 0x65, 0x6c, 0x49, 0x64, 0x12, 0x26, 0x0a, 0x07, 0x64, 0x65, 0x6c, 0x5f, 0x73, 0x65, 0x71, 0x18, 0x07, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0d, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x53, 0x65, 0x71, 0x52, 0x61, 0x6e, 0x67, 0x65, 0x52, 0x06, 0x64, 0x65, 0x6c, 0x53, 0x65, 0x71, 0x12, 0x24, 0x0a, 0x0e, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x5f, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x18, 0x08, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0c, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x22, 0x0a, 0x0d, 0x61, 0x63, 0x74, 0x6f, 0x72, 0x5f, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x18, 0x09, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x61, 0x63, 0x74, 0x6f, 0x72, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x21, 0x0a, 0x03, 0x61, 0x63, 0x73, 0x18, 0x0a, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x41, 0x63, 0x63, 0x65, 0x73, 0x73, 0x4d, 0x6f, 0x64, 0x65, 0x52, 0x03, 0x61, 0x63, 0x73, 0x22, 0x86, 0x01, 0x0a, 0x04, 0x57, 0x68, 0x61, 0x74, 0x12, 0x06, 0x0a, 0x02, 0x58, 0x33, 0x10, 0x00, 0x12, 0x06, 0x0a, 0x02, 0x4f, 0x4e, 0x10, 0x01, 0x12, 0x07, 0x0a, 0x03, 0x4f, 0x46, 0x46, 0x10, 0x02, 0x12, 0x06, 0x0a, 0x02, 0x55, 0x41, 0x10, 0x03, 0x12, 0x07, 0x0a, 0x03, 0x55, 0x50, 0x44, 0x10, 0x04, 0x12, 0x08, 0x0a, 0x04, 0x47, 0x4f, 0x4e, 0x45, 0x10, 0x05, 0x12, 0x07, 0x0a, 0x03, 0x41, 0x43, 0x53, 0x10, 0x06, 0x12, 0x08, 0x0a, 0x04, 0x54, 0x45, 0x52, 0x4d, 0x10, 0x07, 0x12, 0x07, 0x0a, 0x03, 0x4d, 0x53, 0x47, 0x10, 0x08, 0x12, 0x08, 0x0a, 0x04, 0x52, 0x45, 0x41, 0x44, 0x10, 0x09, 0x12, 0x08, 0x0a, 0x04, 0x52, 0x45, 0x43, 0x56, 0x10, 0x0a, 0x12, 0x07, 0x0a, 0x03, 0x44, 0x45, 0x4c, 0x10, 0x0b, 0x12, 0x08, 0x0a, 0x04, 0x54, 0x41, 0x47, 0x53, 0x10, 0x0c, 0x12, 0x07, 0x0a, 0x03, 0x41, 0x55, 0x58, 0x10, 0x0d, 0x22, 0xb6, 0x02, 0x0a, 0x0a, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x4d, 0x65, 0x74, 0x61, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x12, 0x22, 0x0a, 0x04, 0x64, 0x65, 0x73, 0x63, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x54, 0x6f, 0x70, 0x69, 0x63, 0x44, 0x65, 0x73, 0x63, 0x52, 0x04, 0x64, 0x65, 0x73, 0x63, 0x12, 0x1f, 0x0a, 0x03, 0x73, 0x75, 0x62, 0x18, 0x04, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0d, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x54, 0x6f, 0x70, 0x69, 0x63, 0x53, 0x75, 0x62, 0x52, 0x03, 0x73, 0x75, 0x62, 0x12, 0x20, 0x0a, 0x03, 0x64, 0x65, 0x6c, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x44, 0x65, 0x6c, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x73, 0x52, 0x03, 0x64, 0x65, 0x6c, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x61, 0x67, 0x73, 0x18, 0x06, 0x20, 0x03, 0x28, 0x09, 0x52, 0x04, 0x74, 0x61, 0x67, 0x73, 0x12, 0x23, 0x0a, 0x04, 0x63, 0x72, 0x65, 0x64, 0x18, 0x07, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x43, 0x72, 0x65, 0x64, 0x52, 0x04, 0x63, 0x72, 0x65, 0x64, 0x12, 0x2a, 0x0a, 0x03, 0x61, 0x75, 0x78, 0x18, 0x08, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x18, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x4d, 0x65, 0x74, 0x61, 0x2e, 0x41, 0x75, 0x78, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x03, 0x61, 0x75, 0x78, 0x1a, 0x36, 0x0a, 0x08, 0x41, 0x75, 0x78, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x22, 0xd0, 0x01, 0x0a, 0x0a, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x49, 0x6e, 0x66, 0x6f, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x12, 0x20, 0x0a, 0x0c, 0x66, 0x72, 0x6f, 0x6d, 0x5f, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a, 0x66, 0x72, 0x6f, 0x6d, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x21, 0x0a, 0x04, 0x77, 0x68, 0x61, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x0d, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x49, 0x6e, 0x66, 0x6f, 0x4e, 0x6f, 0x74, 0x65, 0x52, 0x04, 0x77, 0x68, 0x61, 0x74, 0x12, 0x15, 0x0a, 0x06, 0x73, 0x65, 0x71, 0x5f, 0x69, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x73, 0x65, 0x71, 0x49, 0x64, 0x12, 0x10, 0x0a, 0x03, 0x73, 0x72, 0x63, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x73, 0x72, 0x63, 0x12, 0x24, 0x0a, 0x05, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x0e, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x43, 0x61, 0x6c, 0x6c, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x52, 0x05, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x18, 0x07, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x22, 0xf3, 0x01, 0x0a, 0x09, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x4d, 0x73, 0x67, 0x12, 0x25, 0x0a, 0x04, 0x63, 0x74, 0x72, 0x6c, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x43, 0x74, 0x72, 0x6c, 0x48, 0x00, 0x52, 0x04, 0x63, 0x74, 0x72, 0x6c, 0x12, 0x25, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x44, 0x61, 0x74, 0x61, 0x48, 0x00, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x12, 0x25, 0x0a, 0x04, 0x70, 0x72, 0x65, 0x73, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x50, 0x72, 0x65, 0x73, 0x48, 0x00, 0x52, 0x04, 0x70, 0x72, 0x65, 0x73, 0x12, 0x25, 0x0a, 0x04, 0x6d, 0x65, 0x74, 0x61, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x4d, 0x65, 0x74, 0x61, 0x48, 0x00, 0x52, 0x04, 0x6d, 0x65, 0x74, 0x61, 0x12, 0x25, 0x0a, 0x04, 0x69, 0x6e, 0x66, 0x6f, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x49, 0x6e, 0x66, 0x6f, 0x48, 0x00, 0x52, 0x04, 0x69, 0x6e, 0x66, 0x6f, 0x12, 0x18, 0x0a, 0x05, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x42, 0x02, 0x18, 0x01, 0x52, 0x05, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x42, 0x09, 0x0a, 0x07, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x22, 0x81, 0x01, 0x0a, 0x0a, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x12, 0x25, 0x0a, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x0d, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x43, 0x6f, 0x64, 0x65, 0x52, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x12, 0x26, 0x0a, 0x06, 0x73, 0x72, 0x76, 0x6d, 0x73, 0x67, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x4d, 0x73, 0x67, 0x52, 0x06, 0x73, 0x72, 0x76, 0x6d, 0x73, 0x67, 0x12, 0x24, 0x0a, 0x05, 0x63, 0x6c, 0x6d, 0x73, 0x67, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x4d, 0x73, 0x67, 0x52, 0x05, 0x63, 0x6c, 0x6d, 0x73, 0x67, 0x22, 0xe9, 0x01, 0x0a, 0x07, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x12, 0x1d, 0x0a, 0x0a, 0x73, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x73, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x49, 0x64, 0x12, 0x17, 0x0a, 0x07, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x75, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x2d, 0x0a, 0x0a, 0x61, 0x75, 0x74, 0x68, 0x5f, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x0e, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x41, 0x75, 0x74, 0x68, 0x4c, 0x65, 0x76, 0x65, 0x6c, 0x52, 0x09, 0x61, 0x75, 0x74, 0x68, 0x4c, 0x65, 0x76, 0x65, 0x6c, 0x12, 0x1f, 0x0a, 0x0b, 0x72, 0x65, 0x6d, 0x6f, 0x74, 0x65, 0x5f, 0x61, 0x64, 0x64, 0x72, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a, 0x72, 0x65, 0x6d, 0x6f, 0x74, 0x65, 0x41, 0x64, 0x64, 0x72, 0x12, 0x1d, 0x0a, 0x0a, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x61, 0x67, 0x65, 0x6e, 0x74, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x75, 0x73, 0x65, 0x72, 0x41, 0x67, 0x65, 0x6e, 0x74, 0x12, 0x1b, 0x0a, 0x09, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65, 0x5f, 0x69, 0x64, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65, 0x49, 0x64, 0x12, 0x1a, 0x0a, 0x08, 0x6c, 0x61, 0x6e, 0x67, 0x75, 0x61, 0x67, 0x65, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x6c, 0x61, 0x6e, 0x67, 0x75, 0x61, 0x67, 0x65, 0x22, 0x4f, 0x0a, 0x09, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x52, 0x65, 0x71, 0x12, 0x20, 0x0a, 0x03, 0x6d, 0x73, 0x67, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x4d, 0x73, 0x67, 0x52, 0x03, 0x6d, 0x73, 0x67, 0x12, 0x20, 0x0a, 0x04, 0x73, 0x65, 0x73, 0x73, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x04, 0x73, 0x65, 0x73, 0x73, 0x22, 0x3c, 0x0a, 0x0b, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x51, 0x75, 0x65, 0x72, 0x79, 0x12, 0x17, 0x0a, 0x07, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x75, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x71, 0x75, 0x65, 0x72, 0x79, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x71, 0x75, 0x65, 0x72, 0x79, 0x22, 0x71, 0x0a, 0x0b, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x46, 0x6f, 0x75, 0x6e, 0x64, 0x12, 0x25, 0x0a, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x0d, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x43, 0x6f, 0x64, 0x65, 0x52, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x12, 0x14, 0x0a, 0x05, 0x71, 0x75, 0x65, 0x72, 0x79, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x71, 0x75, 0x65, 0x72, 0x79, 0x12, 0x25, 0x0a, 0x06, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x18, 0x03, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0d, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x54, 0x6f, 0x70, 0x69, 0x63, 0x53, 0x75, 0x62, 0x52, 0x06, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x22, 0x67, 0x0a, 0x0a, 0x54, 0x6f, 0x70, 0x69, 0x63, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x12, 0x21, 0x0a, 0x06, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x09, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x43, 0x72, 0x75, 0x64, 0x52, 0x06, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x22, 0x0a, 0x04, 0x64, 0x65, 0x73, 0x63, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x54, 0x6f, 0x70, 0x69, 0x63, 0x44, 0x65, 0x73, 0x63, 0x52, 0x04, 0x64, 0x65, 0x73, 0x63, 0x22, 0xac, 0x01, 0x0a, 0x0c, 0x41, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x12, 0x21, 0x0a, 0x06, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x09, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x43, 0x72, 0x75, 0x64, 0x52, 0x06, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x17, 0x0a, 0x07, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x75, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x34, 0x0a, 0x0b, 0x64, 0x65, 0x66, 0x61, 0x75, 0x6c, 0x74, 0x5f, 0x61, 0x63, 0x73, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x13, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x44, 0x65, 0x66, 0x61, 0x75, 0x6c, 0x74, 0x41, 0x63, 0x73, 0x4d, 0x6f, 0x64, 0x65, 0x52, 0x0a, 0x64, 0x65, 0x66, 0x61, 0x75, 0x6c, 0x74, 0x41, 0x63, 0x73, 0x12, 0x16, 0x0a, 0x06, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x06, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x61, 0x67, 0x73, 0x18, 0x08, 0x20, 0x03, 0x28, 0x09, 0x52, 0x04, 0x74, 0x61, 0x67, 0x73, 0x22, 0xed, 0x01, 0x0a, 0x11, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x12, 0x21, 0x0a, 0x06, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x09, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x43, 0x72, 0x75, 0x64, 0x52, 0x06, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x12, 0x17, 0x0a, 0x07, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x75, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x15, 0x0a, 0x06, 0x64, 0x65, 0x6c, 0x5f, 0x69, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x64, 0x65, 0x6c, 0x49, 0x64, 0x12, 0x17, 0x0a, 0x07, 0x72, 0x65, 0x61, 0x64, 0x5f, 0x69, 0x64, 0x18, 0x05, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x72, 0x65, 0x61, 0x64, 0x49, 0x64, 0x12, 0x17, 0x0a, 0x07, 0x72, 0x65, 0x63, 0x76, 0x5f, 0x69, 0x64, 0x18, 0x06, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x72, 0x65, 0x63, 0x76, 0x49, 0x64, 0x12, 0x23, 0x0a, 0x04, 0x6d, 0x6f, 0x64, 0x65, 0x18, 0x07, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x41, 0x63, 0x63, 0x65, 0x73, 0x73, 0x4d, 0x6f, 0x64, 0x65, 0x52, 0x04, 0x6d, 0x6f, 0x64, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x70, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65, 0x18, 0x08, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, 0x70, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65, 0x22, 0x54, 0x0a, 0x0c, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x12, 0x21, 0x0a, 0x06, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x09, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x43, 0x72, 0x75, 0x64, 0x52, 0x06, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x21, 0x0a, 0x03, 0x6d, 0x73, 0x67, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x44, 0x61, 0x74, 0x61, 0x52, 0x03, 0x6d, 0x73, 0x67, 0x22, 0x36, 0x0a, 0x04, 0x41, 0x75, 0x74, 0x68, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0x22, 0x63, 0x0a, 0x08, 0x46, 0x69, 0x6c, 0x65, 0x4d, 0x65, 0x74, 0x61, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x1b, 0x0a, 0x09, 0x6d, 0x69, 0x6d, 0x65, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x6d, 0x69, 0x6d, 0x65, 0x54, 0x79, 0x70, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x65, 0x74, 0x61, 0x67, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x65, 0x74, 0x61, 0x67, 0x12, 0x12, 0x0a, 0x04, 0x73, 0x69, 0x7a, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x03, 0x52, 0x04, 0x73, 0x69, 0x7a, 0x65, 0x22, 0x8d, 0x01, 0x0a, 0x09, 0x46, 0x69, 0x6c, 0x65, 0x55, 0x70, 0x52, 0x65, 0x71, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x1d, 0x0a, 0x04, 0x61, 0x75, 0x74, 0x68, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x41, 0x75, 0x74, 0x68, 0x52, 0x04, 0x61, 0x75, 0x74, 0x68, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x12, 0x21, 0x0a, 0x04, 0x6d, 0x65, 0x74, 0x61, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0d, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x46, 0x69, 0x6c, 0x65, 0x4d, 0x65, 0x74, 0x61, 0x52, 0x04, 0x6d, 0x65, 0x74, 0x61, 0x12, 0x18, 0x0a, 0x07, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x22, 0x84, 0x01, 0x0a, 0x0a, 0x46, 0x69, 0x6c, 0x65, 0x55, 0x70, 0x52, 0x65, 0x73, 0x70, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x65, 0x78, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x74, 0x65, 0x78, 0x74, 0x12, 0x21, 0x0a, 0x04, 0x6d, 0x65, 0x74, 0x61, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0d, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x46, 0x69, 0x6c, 0x65, 0x4d, 0x65, 0x74, 0x61, 0x52, 0x04, 0x6d, 0x65, 0x74, 0x61, 0x12, 0x1b, 0x0a, 0x09, 0x72, 0x65, 0x64, 0x69, 0x72, 0x5f, 0x75, 0x72, 0x6c, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x72, 0x65, 0x64, 0x69, 0x72, 0x55, 0x72, 0x6c, 0x22, 0x6f, 0x0a, 0x0b, 0x46, 0x69, 0x6c, 0x65, 0x44, 0x6f, 0x77, 0x6e, 0x52, 0x65, 0x71, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x1d, 0x0a, 0x04, 0x61, 0x75, 0x74, 0x68, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x41, 0x75, 0x74, 0x68, 0x52, 0x04, 0x61, 0x75, 0x74, 0x68, 0x12, 0x10, 0x0a, 0x03, 0x75, 0x72, 0x69, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x75, 0x72, 0x69, 0x12, 0x1f, 0x0a, 0x0b, 0x69, 0x66, 0x5f, 0x6d, 0x6f, 0x64, 0x69, 0x66, 0x69, 0x65, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a, 0x69, 0x66, 0x4d, 0x6f, 0x64, 0x69, 0x66, 0x69, 0x65, 0x64, 0x22, 0xa0, 0x01, 0x0a, 0x0c, 0x46, 0x69, 0x6c, 0x65, 0x44, 0x6f, 0x77, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x65, 0x78, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x74, 0x65, 0x78, 0x74, 0x12, 0x21, 0x0a, 0x04, 0x6d, 0x65, 0x74, 0x61, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0d, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x46, 0x69, 0x6c, 0x65, 0x4d, 0x65, 0x74, 0x61, 0x52, 0x04, 0x6d, 0x65, 0x74, 0x61, 0x12, 0x1b, 0x0a, 0x09, 0x72, 0x65, 0x64, 0x69, 0x72, 0x5f, 0x75, 0x72, 0x6c, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x72, 0x65, 0x64, 0x69, 0x72, 0x55, 0x72, 0x6c, 0x12, 0x18, 0x0a, 0x07, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2a, 0x33, 0x0a, 0x09, 0x41, 0x75, 0x74, 0x68, 0x4c, 0x65, 0x76, 0x65, 0x6c, 0x12, 0x08, 0x0a, 0x04, 0x4e, 0x4f, 0x4e, 0x45, 0x10, 0x00, 0x12, 0x08, 0x0a, 0x04, 0x41, 0x4e, 0x4f, 0x4e, 0x10, 0x0a, 0x12, 0x08, 0x0a, 0x04, 0x41, 0x55, 0x54, 0x48, 0x10, 0x14, 0x12, 0x08, 0x0a, 0x04, 0x52, 0x4f, 0x4f, 0x54, 0x10, 0x1e, 0x2a, 0x38, 0x0a, 0x08, 0x49, 0x6e, 0x66, 0x6f, 0x4e, 0x6f, 0x74, 0x65, 0x12, 0x06, 0x0a, 0x02, 0x58, 0x31, 0x10, 0x00, 0x12, 0x08, 0x0a, 0x04, 0x52, 0x45, 0x41, 0x44, 0x10, 0x01, 0x12, 0x08, 0x0a, 0x04, 0x52, 0x45, 0x43, 0x56, 0x10, 0x02, 0x12, 0x06, 0x0a, 0x02, 0x4b, 0x50, 0x10, 0x03, 0x12, 0x08, 0x0a, 0x04, 0x43, 0x41, 0x4c, 0x4c, 0x10, 0x04, 0x2a, 0x6f, 0x0a, 0x09, 0x43, 0x61, 0x6c, 0x6c, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x12, 0x06, 0x0a, 0x02, 0x58, 0x32, 0x10, 0x00, 0x12, 0x0a, 0x0a, 0x06, 0x41, 0x43, 0x43, 0x45, 0x50, 0x54, 0x10, 0x01, 0x12, 0x0a, 0x0a, 0x06, 0x41, 0x4e, 0x53, 0x57, 0x45, 0x52, 0x10, 0x02, 0x12, 0x0b, 0x0a, 0x07, 0x48, 0x41, 0x4e, 0x47, 0x5f, 0x55, 0x50, 0x10, 0x03, 0x12, 0x11, 0x0a, 0x0d, 0x49, 0x43, 0x45, 0x5f, 0x43, 0x41, 0x4e, 0x44, 0x49, 0x44, 0x41, 0x54, 0x45, 0x10, 0x04, 0x12, 0x0a, 0x0a, 0x06, 0x49, 0x4e, 0x56, 0x49, 0x54, 0x45, 0x10, 0x05, 0x12, 0x09, 0x0a, 0x05, 0x4f, 0x46, 0x46, 0x45, 0x52, 0x10, 0x06, 0x12, 0x0b, 0x0a, 0x07, 0x52, 0x49, 0x4e, 0x47, 0x49, 0x4e, 0x47, 0x10, 0x07, 0x2a, 0x3c, 0x0a, 0x08, 0x52, 0x65, 0x73, 0x70, 0x43, 0x6f, 0x64, 0x65, 0x12, 0x0c, 0x0a, 0x08, 0x43, 0x4f, 0x4e, 0x54, 0x49, 0x4e, 0x55, 0x45, 0x10, 0x00, 0x12, 0x08, 0x0a, 0x04, 0x44, 0x52, 0x4f, 0x50, 0x10, 0x01, 0x12, 0x0b, 0x0a, 0x07, 0x52, 0x45, 0x53, 0x50, 0x4f, 0x4e, 0x44, 0x10, 0x02, 0x12, 0x0b, 0x0a, 0x07, 0x52, 0x45, 0x50, 0x4c, 0x41, 0x43, 0x45, 0x10, 0x03, 0x2a, 0x2a, 0x0a, 0x04, 0x43, 0x72, 0x75, 0x64, 0x12, 0x0a, 0x0a, 0x06, 0x43, 0x52, 0x45, 0x41, 0x54, 0x45, 0x10, 0x00, 0x12, 0x0a, 0x0a, 0x06, 0x55, 0x50, 0x44, 0x41, 0x54, 0x45, 0x10, 0x01, 0x12, 0x0a, 0x0a, 0x06, 0x44, 0x45, 0x4c, 0x45, 0x54, 0x45, 0x10, 0x02, 0x32, 0xaf, 0x01, 0x0a, 0x04, 0x4e, 0x6f, 0x64, 0x65, 0x12, 0x33, 0x0a, 0x0b, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x4c, 0x6f, 0x6f, 0x70, 0x12, 0x0e, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x4d, 0x73, 0x67, 0x1a, 0x0e, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x4d, 0x73, 0x67, 0x22, 0x00, 0x28, 0x01, 0x30, 0x01, 0x12, 0x37, 0x0a, 0x10, 0x4c, 0x61, 0x72, 0x67, 0x65, 0x46, 0x69, 0x6c, 0x65, 0x52, 0x65, 0x63, 0x65, 0x69, 0x76, 0x65, 0x12, 0x0e, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x46, 0x69, 0x6c, 0x65, 0x55, 0x70, 0x52, 0x65, 0x71, 0x1a, 0x0f, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x46, 0x69, 0x6c, 0x65, 0x55, 0x70, 0x52, 0x65, 0x73, 0x70, 0x22, 0x00, 0x28, 0x01, 0x12, 0x39, 0x0a, 0x0e, 0x4c, 0x61, 0x72, 0x67, 0x65, 0x46, 0x69, 0x6c, 0x65, 0x53, 0x65, 0x72, 0x76, 0x65, 0x12, 0x10, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x46, 0x69, 0x6c, 0x65, 0x44, 0x6f, 0x77, 0x6e, 0x52, 0x65, 0x71, 0x1a, 0x11, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x46, 0x69, 0x6c, 0x65, 0x44, 0x6f, 0x77, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x22, 0x00, 0x30, 0x01, 0x32, 0x9f, 0x02, 0x0a, 0x06, 0x50, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x12, 0x2d, 0x0a, 0x08, 0x46, 0x69, 0x72, 0x65, 0x48, 0x6f, 0x73, 0x65, 0x12, 0x0e, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x52, 0x65, 0x71, 0x1a, 0x0f, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x22, 0x00, 0x12, 0x2c, 0x0a, 0x04, 0x46, 0x69, 0x6e, 0x64, 0x12, 0x10, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x51, 0x75, 0x65, 0x72, 0x79, 0x1a, 0x10, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x46, 0x6f, 0x75, 0x6e, 0x64, 0x22, 0x00, 0x12, 0x2b, 0x0a, 0x07, 0x41, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x11, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x41, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x1a, 0x0b, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x55, 0x6e, 0x75, 0x73, 0x65, 0x64, 0x22, 0x00, 0x12, 0x27, 0x0a, 0x05, 0x54, 0x6f, 0x70, 0x69, 0x63, 0x12, 0x0f, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x54, 0x6f, 0x70, 0x69, 0x63, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x1a, 0x0b, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x55, 0x6e, 0x75, 0x73, 0x65, 0x64, 0x22, 0x00, 0x12, 0x35, 0x0a, 0x0c, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x16, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x1a, 0x0b, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x55, 0x6e, 0x75, 0x73, 0x65, 0x64, 0x22, 0x00, 0x12, 0x2b, 0x0a, 0x07, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x11, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x1a, 0x0b, 0x2e, 0x70, 0x62, 0x78, 0x2e, 0x55, 0x6e, 0x75, 0x73, 0x65, 0x64, 0x22, 0x00, 0x42, 0x1c, 0x5a, 0x1a, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x74, 0x69, 0x6e, 0x6f, 0x64, 0x65, 0x2f, 0x63, 0x68, 0x61, 0x74, 0x2f, 0x70, 0x62, 0x78, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( file_model_proto_rawDescOnce sync.Once file_model_proto_rawDescData = file_model_proto_rawDesc ) func file_model_proto_rawDescGZIP() []byte { file_model_proto_rawDescOnce.Do(func() { file_model_proto_rawDescData = protoimpl.X.CompressGZIP(file_model_proto_rawDescData) }) return file_model_proto_rawDescData } var file_model_proto_enumTypes = make([]protoimpl.EnumInfo, 7) var file_model_proto_msgTypes = make([]protoimpl.MessageInfo, 53) var file_model_proto_goTypes = []interface{}{ (AuthLevel)(0), // 0: pbx.AuthLevel (InfoNote)(0), // 1: pbx.InfoNote (CallEvent)(0), // 2: pbx.CallEvent (RespCode)(0), // 3: pbx.RespCode (Crud)(0), // 4: pbx.Crud (ClientDel_What)(0), // 5: pbx.ClientDel.What (ServerPres_What)(0), // 6: pbx.ServerPres.What (*Unused)(nil), // 7: pbx.Unused (*DefaultAcsMode)(nil), // 8: pbx.DefaultAcsMode (*AccessMode)(nil), // 9: pbx.AccessMode (*SetSub)(nil), // 10: pbx.SetSub (*ClientCred)(nil), // 11: pbx.ClientCred (*SetDesc)(nil), // 12: pbx.SetDesc (*SeqRange)(nil), // 13: pbx.SeqRange (*GetOpts)(nil), // 14: pbx.GetOpts (*GetQuery)(nil), // 15: pbx.GetQuery (*SetQuery)(nil), // 16: pbx.SetQuery (*ClientHi)(nil), // 17: pbx.ClientHi (*ClientAcc)(nil), // 18: pbx.ClientAcc (*ClientLogin)(nil), // 19: pbx.ClientLogin (*ClientSub)(nil), // 20: pbx.ClientSub (*ClientLeave)(nil), // 21: pbx.ClientLeave (*ClientPub)(nil), // 22: pbx.ClientPub (*ClientGet)(nil), // 23: pbx.ClientGet (*ClientSet)(nil), // 24: pbx.ClientSet (*ClientDel)(nil), // 25: pbx.ClientDel (*ClientNote)(nil), // 26: pbx.ClientNote (*ClientExtra)(nil), // 27: pbx.ClientExtra (*ClientMsg)(nil), // 28: pbx.ClientMsg (*ServerCred)(nil), // 29: pbx.ServerCred (*TopicDesc)(nil), // 30: pbx.TopicDesc (*TopicSub)(nil), // 31: pbx.TopicSub (*DelValues)(nil), // 32: pbx.DelValues (*ServerCtrl)(nil), // 33: pbx.ServerCtrl (*ServerData)(nil), // 34: pbx.ServerData (*ServerPres)(nil), // 35: pbx.ServerPres (*ServerMeta)(nil), // 36: pbx.ServerMeta (*ServerInfo)(nil), // 37: pbx.ServerInfo (*ServerMsg)(nil), // 38: pbx.ServerMsg (*ServerResp)(nil), // 39: pbx.ServerResp (*Session)(nil), // 40: pbx.Session (*ClientReq)(nil), // 41: pbx.ClientReq (*SearchQuery)(nil), // 42: pbx.SearchQuery (*SearchFound)(nil), // 43: pbx.SearchFound (*TopicEvent)(nil), // 44: pbx.TopicEvent (*AccountEvent)(nil), // 45: pbx.AccountEvent (*SubscriptionEvent)(nil), // 46: pbx.SubscriptionEvent (*MessageEvent)(nil), // 47: pbx.MessageEvent (*Auth)(nil), // 48: pbx.Auth (*FileMeta)(nil), // 49: pbx.FileMeta (*FileUpReq)(nil), // 50: pbx.FileUpReq (*FileUpResp)(nil), // 51: pbx.FileUpResp (*FileDownReq)(nil), // 52: pbx.FileDownReq (*FileDownResp)(nil), // 53: pbx.FileDownResp nil, // 54: pbx.ClientCred.ParamsEntry nil, // 55: pbx.SetQuery.AuxEntry nil, // 56: pbx.ClientPub.HeadEntry nil, // 57: pbx.ServerCtrl.ParamsEntry nil, // 58: pbx.ServerData.HeadEntry nil, // 59: pbx.ServerMeta.AuxEntry } var file_model_proto_depIdxs = []int32{ 54, // 0: pbx.ClientCred.params:type_name -> pbx.ClientCred.ParamsEntry 8, // 1: pbx.SetDesc.default_acs:type_name -> pbx.DefaultAcsMode 13, // 2: pbx.GetOpts.ranges:type_name -> pbx.SeqRange 14, // 3: pbx.GetQuery.desc:type_name -> pbx.GetOpts 14, // 4: pbx.GetQuery.sub:type_name -> pbx.GetOpts 14, // 5: pbx.GetQuery.data:type_name -> pbx.GetOpts 12, // 6: pbx.SetQuery.desc:type_name -> pbx.SetDesc 10, // 7: pbx.SetQuery.sub:type_name -> pbx.SetSub 11, // 8: pbx.SetQuery.cred:type_name -> pbx.ClientCred 55, // 9: pbx.SetQuery.aux:type_name -> pbx.SetQuery.AuxEntry 12, // 10: pbx.ClientAcc.desc:type_name -> pbx.SetDesc 11, // 11: pbx.ClientAcc.cred:type_name -> pbx.ClientCred 0, // 12: pbx.ClientAcc.auth_level:type_name -> pbx.AuthLevel 11, // 13: pbx.ClientLogin.cred:type_name -> pbx.ClientCred 16, // 14: pbx.ClientSub.set_query:type_name -> pbx.SetQuery 15, // 15: pbx.ClientSub.get_query:type_name -> pbx.GetQuery 56, // 16: pbx.ClientPub.head:type_name -> pbx.ClientPub.HeadEntry 15, // 17: pbx.ClientGet.query:type_name -> pbx.GetQuery 16, // 18: pbx.ClientSet.query:type_name -> pbx.SetQuery 5, // 19: pbx.ClientDel.what:type_name -> pbx.ClientDel.What 13, // 20: pbx.ClientDel.del_seq:type_name -> pbx.SeqRange 11, // 21: pbx.ClientDel.cred:type_name -> pbx.ClientCred 1, // 22: pbx.ClientNote.what:type_name -> pbx.InfoNote 2, // 23: pbx.ClientNote.event:type_name -> pbx.CallEvent 0, // 24: pbx.ClientExtra.auth_level:type_name -> pbx.AuthLevel 17, // 25: pbx.ClientMsg.hi:type_name -> pbx.ClientHi 18, // 26: pbx.ClientMsg.acc:type_name -> pbx.ClientAcc 19, // 27: pbx.ClientMsg.login:type_name -> pbx.ClientLogin 20, // 28: pbx.ClientMsg.sub:type_name -> pbx.ClientSub 21, // 29: pbx.ClientMsg.leave:type_name -> pbx.ClientLeave 22, // 30: pbx.ClientMsg.pub:type_name -> pbx.ClientPub 23, // 31: pbx.ClientMsg.get:type_name -> pbx.ClientGet 24, // 32: pbx.ClientMsg.set:type_name -> pbx.ClientSet 25, // 33: pbx.ClientMsg.del:type_name -> pbx.ClientDel 26, // 34: pbx.ClientMsg.note:type_name -> pbx.ClientNote 27, // 35: pbx.ClientMsg.extra:type_name -> pbx.ClientExtra 8, // 36: pbx.TopicDesc.defacs:type_name -> pbx.DefaultAcsMode 9, // 37: pbx.TopicDesc.acs:type_name -> pbx.AccessMode 9, // 38: pbx.TopicSub.acs:type_name -> pbx.AccessMode 13, // 39: pbx.DelValues.del_seq:type_name -> pbx.SeqRange 57, // 40: pbx.ServerCtrl.params:type_name -> pbx.ServerCtrl.ParamsEntry 58, // 41: pbx.ServerData.head:type_name -> pbx.ServerData.HeadEntry 6, // 42: pbx.ServerPres.what:type_name -> pbx.ServerPres.What 13, // 43: pbx.ServerPres.del_seq:type_name -> pbx.SeqRange 9, // 44: pbx.ServerPres.acs:type_name -> pbx.AccessMode 30, // 45: pbx.ServerMeta.desc:type_name -> pbx.TopicDesc 31, // 46: pbx.ServerMeta.sub:type_name -> pbx.TopicSub 32, // 47: pbx.ServerMeta.del:type_name -> pbx.DelValues 29, // 48: pbx.ServerMeta.cred:type_name -> pbx.ServerCred 59, // 49: pbx.ServerMeta.aux:type_name -> pbx.ServerMeta.AuxEntry 1, // 50: pbx.ServerInfo.what:type_name -> pbx.InfoNote 2, // 51: pbx.ServerInfo.event:type_name -> pbx.CallEvent 33, // 52: pbx.ServerMsg.ctrl:type_name -> pbx.ServerCtrl 34, // 53: pbx.ServerMsg.data:type_name -> pbx.ServerData 35, // 54: pbx.ServerMsg.pres:type_name -> pbx.ServerPres 36, // 55: pbx.ServerMsg.meta:type_name -> pbx.ServerMeta 37, // 56: pbx.ServerMsg.info:type_name -> pbx.ServerInfo 3, // 57: pbx.ServerResp.status:type_name -> pbx.RespCode 38, // 58: pbx.ServerResp.srvmsg:type_name -> pbx.ServerMsg 28, // 59: pbx.ServerResp.clmsg:type_name -> pbx.ClientMsg 0, // 60: pbx.Session.auth_level:type_name -> pbx.AuthLevel 28, // 61: pbx.ClientReq.msg:type_name -> pbx.ClientMsg 40, // 62: pbx.ClientReq.sess:type_name -> pbx.Session 3, // 63: pbx.SearchFound.status:type_name -> pbx.RespCode 31, // 64: pbx.SearchFound.result:type_name -> pbx.TopicSub 4, // 65: pbx.TopicEvent.action:type_name -> pbx.Crud 30, // 66: pbx.TopicEvent.desc:type_name -> pbx.TopicDesc 4, // 67: pbx.AccountEvent.action:type_name -> pbx.Crud 8, // 68: pbx.AccountEvent.default_acs:type_name -> pbx.DefaultAcsMode 4, // 69: pbx.SubscriptionEvent.action:type_name -> pbx.Crud 9, // 70: pbx.SubscriptionEvent.mode:type_name -> pbx.AccessMode 4, // 71: pbx.MessageEvent.action:type_name -> pbx.Crud 34, // 72: pbx.MessageEvent.msg:type_name -> pbx.ServerData 48, // 73: pbx.FileUpReq.auth:type_name -> pbx.Auth 49, // 74: pbx.FileUpReq.meta:type_name -> pbx.FileMeta 49, // 75: pbx.FileUpResp.meta:type_name -> pbx.FileMeta 48, // 76: pbx.FileDownReq.auth:type_name -> pbx.Auth 49, // 77: pbx.FileDownResp.meta:type_name -> pbx.FileMeta 28, // 78: pbx.Node.MessageLoop:input_type -> pbx.ClientMsg 50, // 79: pbx.Node.LargeFileReceive:input_type -> pbx.FileUpReq 52, // 80: pbx.Node.LargeFileServe:input_type -> pbx.FileDownReq 41, // 81: pbx.Plugin.FireHose:input_type -> pbx.ClientReq 42, // 82: pbx.Plugin.Find:input_type -> pbx.SearchQuery 45, // 83: pbx.Plugin.Account:input_type -> pbx.AccountEvent 44, // 84: pbx.Plugin.Topic:input_type -> pbx.TopicEvent 46, // 85: pbx.Plugin.Subscription:input_type -> pbx.SubscriptionEvent 47, // 86: pbx.Plugin.Message:input_type -> pbx.MessageEvent 38, // 87: pbx.Node.MessageLoop:output_type -> pbx.ServerMsg 51, // 88: pbx.Node.LargeFileReceive:output_type -> pbx.FileUpResp 53, // 89: pbx.Node.LargeFileServe:output_type -> pbx.FileDownResp 39, // 90: pbx.Plugin.FireHose:output_type -> pbx.ServerResp 43, // 91: pbx.Plugin.Find:output_type -> pbx.SearchFound 7, // 92: pbx.Plugin.Account:output_type -> pbx.Unused 7, // 93: pbx.Plugin.Topic:output_type -> pbx.Unused 7, // 94: pbx.Plugin.Subscription:output_type -> pbx.Unused 7, // 95: pbx.Plugin.Message:output_type -> pbx.Unused 87, // [87:96] is the sub-list for method output_type 78, // [78:87] is the sub-list for method input_type 78, // [78:78] is the sub-list for extension type_name 78, // [78:78] is the sub-list for extension extendee 0, // [0:78] is the sub-list for field type_name } func init() { file_model_proto_init() } func file_model_proto_init() { if File_model_proto != nil { return } if !protoimpl.UnsafeEnabled { file_model_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*Unused); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*DefaultAcsMode); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*AccessMode); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*SetSub); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*ClientCred); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*SetDesc); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[6].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*SeqRange); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[7].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*GetOpts); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[8].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*GetQuery); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[9].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*SetQuery); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[10].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*ClientHi); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[11].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*ClientAcc); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[12].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*ClientLogin); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[13].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*ClientSub); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[14].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*ClientLeave); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[15].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*ClientPub); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[16].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*ClientGet); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[17].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*ClientSet); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[18].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*ClientDel); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[19].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*ClientNote); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[20].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*ClientExtra); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[21].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*ClientMsg); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[22].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*ServerCred); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[23].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*TopicDesc); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[24].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*TopicSub); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[25].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*DelValues); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[26].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*ServerCtrl); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[27].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*ServerData); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[28].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*ServerPres); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[29].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*ServerMeta); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[30].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*ServerInfo); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[31].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*ServerMsg); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[32].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*ServerResp); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[33].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*Session); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[34].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*ClientReq); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[35].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*SearchQuery); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[36].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*SearchFound); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[37].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*TopicEvent); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[38].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*AccountEvent); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[39].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*SubscriptionEvent); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[40].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*MessageEvent); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[41].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*Auth); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[42].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*FileMeta); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[43].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*FileUpReq); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[44].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*FileUpResp); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[45].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*FileDownReq); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } file_model_proto_msgTypes[46].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*FileDownResp); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } } file_model_proto_msgTypes[21].OneofWrappers = []interface{}{ (*ClientMsg_Hi)(nil), (*ClientMsg_Acc)(nil), (*ClientMsg_Login)(nil), (*ClientMsg_Sub)(nil), (*ClientMsg_Leave)(nil), (*ClientMsg_Pub)(nil), (*ClientMsg_Get)(nil), (*ClientMsg_Set)(nil), (*ClientMsg_Del)(nil), (*ClientMsg_Note)(nil), } file_model_proto_msgTypes[31].OneofWrappers = []interface{}{ (*ServerMsg_Ctrl)(nil), (*ServerMsg_Data)(nil), (*ServerMsg_Pres)(nil), (*ServerMsg_Meta)(nil), (*ServerMsg_Info)(nil), } type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_model_proto_rawDesc, NumEnums: 7, NumMessages: 53, NumExtensions: 0, NumServices: 2, }, GoTypes: file_model_proto_goTypes, DependencyIndexes: file_model_proto_depIdxs, EnumInfos: file_model_proto_enumTypes, MessageInfos: file_model_proto_msgTypes, }.Build() File_model_proto = out.File file_model_proto_rawDesc = nil file_model_proto_goTypes = nil file_model_proto_depIdxs = nil } ================================================ FILE: pbx/model.proto ================================================ syntax = "proto3"; package pbx; option go_package = "github.com/tinode/chat/pbx"; // This is the methods that needs to be implemented by a gRPC client. service Node { // Client sends a stream of ClientMsg, server responds with a stream of ServerMsg rpc MessageLoop(stream ClientMsg) returns (stream ServerMsg) {} // Large file upload: a request with a stream of chunks. rpc LargeFileReceive(stream FileUpReq) returns (FileUpResp) {} // Large file file download: a response with a stream of chunks. rpc LargeFileServe(FileDownReq) returns (stream FileDownResp) {} } // Plugin interface. service Plugin { // This plugin method is called by Tinode server for every message received from the clients. The // method returns a ServerResp message. ServerResp.status tells Tinode server what to do next. // See possible values for ServerResp.status in RespCode below. rpc FireHose(ClientReq) returns (ServerResp) {} // An alteranative user and topic discovery mechanism. // A search request issued on a 'fnd' topic. This method is called to generate an alternative result set. rpc Find(SearchQuery) returns (SearchFound) {} // The following methods are for the Tinode server to report individual events. // Account created, updated or deleted rpc Account(AccountEvent) returns (Unused) {} // Topic created, updated [or deleted -- not supported yet] rpc Topic(TopicEvent) returns (Unused) {} // Subscription created, updated or deleted rpc Subscription(SubscriptionEvent) returns (Unused) {} // Message published or deleted rpc Message(MessageEvent) returns (Unused) {} } // Dummy placeholder message. message Unused { } // Common client/server messages // Authentication level enum AuthLevel { NONE = 0; ANON = 10; AUTH = 20; ROOT = 30; } // Client messages // Topic default access mode message DefaultAcsMode { string auth = 1; string anon = 2; } // Actual access mode message AccessMode { // Access mode requested by the user string want = 1; // Access mode granted to the user by the admin string given = 2; } // SetSub: payload in set.sub request to update current subscription or invite another user, {sub.what} == "sub" message SetSub { // User affected by this request. Default (empty): current user string user_id = 1; // Access mode change, either Given or Want depending on context string mode = 2; } // Credentials such as email or phone number message ClientCred { // Credential type, i.e. `email` or `tel`. string method = 1; // Value to verify, i.e. `user@example.com` or `+18003287448` string value = 2; // Verification response string response = 3; // Request parameters, such as preferences or country code. map params = 4; } // SetDesc: C2S in set.what == "desc" and sub.init message message SetDesc { DefaultAcsMode default_acs = 1; bytes public = 2; bytes private = 3; bytes trusted = 4; } message SeqRange { int32 low = 1; int32 hi = 2; } message GetOpts { // Timestamp in milliseconds since epoch 01/01/1970 int64 if_modified_since = 1; // Limit search to this user ID string user = 2; // Limit search results to one topic; string topic = 3; // Load messages with seq id equal or greater than this int32 since_id = 4; // Load messages with seq id lower than this int32 before_id = 5; // Maximum number of results to return int32 limit = 6; // Load messages by id or ranges of ids repeated SeqRange ranges = 7; } message GetQuery { string what = 1; // Parameters of "desc" request GetOpts desc = 2; // Parameters of "sub" request GetOpts sub = 3; // Parameters of "data" request GetOpts data = 4; } message SetQuery { // Topic metadata, new topic & new subscriptions only SetDesc desc = 1; // Subscription parameters SetSub sub = 2; // Indexable tags repeated string tags = 3; // Credential being updated. ClientCred cred = 4; // Auxiliary data. map aux = 5; } // Client handshake message ClientHi { string id = 1; string user_agent = 2; string ver = 3; string device_id = 4; string lang = 5; string platform = 6; bool background = 7; } // User creation message {acc} message ClientAcc { string id = 1; // User being created or updated string user_id = 2; // The initial authentication scheme the account can use string scheme = 3; // Shared secret bytes secret = 4; // Authenticate session with the newly created account bool login = 5; // Indexable tags for user discovery repeated string tags = 6; // User initialization data when creating a new user, otherwise ignored SetDesc desc = 7; // Credentials for verification. repeated ClientCred cred = 8; // Authentication token used for resetting a password. bytes token = 9; // Account state: normal ("ok"), suspended string state = 10; // AuthLevel AuthLevel auth_level = 11; // Temporary auth params for one-off actions like password reset. string tmp_scheme = 12; bytes tmp_secret = 13; } // Login {login} message message ClientLogin { string id = 1; // Authentication scheme string scheme = 2; // Shared secret bytes secret = 3; // Credentials for verification. repeated ClientCred cred = 4; } // Subscription request {sub} message message ClientSub { string id = 1; string topic = 2; // mirrors {set} SetQuery set_query = 3; // mirrors {get} GetQuery get_query = 4; } // Unsubscribe {leave} request message message ClientLeave { string id = 1; string topic = 2; bool unsub = 3; } // ClientPub is client's request to publish data to topic subscribers {pub} message ClientPub { string id = 1; string topic = 2; bool no_echo = 3; map head = 4; bytes content = 5; } // Query topic state {get} message ClientGet { string id = 1; string topic = 2; GetQuery query = 3; } // Update topic state {set} message ClientSet { string id = 1; string topic = 2; SetQuery query = 3; } // ClientDel delete messages or topic message ClientDel { string id = 1; string topic = 2; // What to delete, either "msg" to delete messages (default) or "topic" to delete the topic or "sub" // to delete a subscription to topic. enum What { // Invalid value. The name must be globally unique. X0 = 0; MSG = 1; TOPIC = 2; SUB = 3; USER = 4; CRED = 5; } What what = 3; // Delete messages by id or range of ids repeated SeqRange del_seq = 4; // User ID of the subscription to delete string user_id = 5; // Credential to delete. ClientCred cred = 6; // Request to hard-delete messages for all users, if such option is available. bool hard = 7; } enum InfoNote { // Invalid value. The name must be globally unique. X1 = 0; READ = 1; RECV = 2; KP = 3; CALL = 4; } enum CallEvent { // Invalid value. The name must be globally unique. X2 = 0; ACCEPT = 1; ANSWER = 2; HANG_UP = 3; ICE_CANDIDATE = 4; INVITE = 5; OFFER = 6; RINGING = 7; } // ClientNote is a client-generated notification for topic subscribers message ClientNote { string topic = 1; // what is being reported: "recv" - message received, "read" - message read, // "kp" - typing notification, "call" - voice/video call InfoNote what = 2; // Server-issued message ID being reported int32 seq_id = 3; // Client's count of unread messages to report back to the server. Used in push notifications on iOS. int32 unread = 4; // Call event. CallEvent event = 5; // Arbitrary json payload (used in video calls). bytes payload = 6; } message ClientExtra { repeated string attachments = 1; // Root user may send messages on behalf of other users. string on_behalf_of = 2; AuthLevel auth_level = 3; } message ClientMsg { oneof Message { ClientHi hi = 1; ClientAcc acc = 2; ClientLogin login = 3; ClientSub sub = 4; ClientLeave leave = 5; ClientPub pub = 6; ClientGet get = 7; ClientSet set = 8; ClientDel del = 9; ClientNote note = 10; } // Additional message parameters. ClientExtra extra = 13; } // ************************ // Server response messages // Credentials message ServerCred { // Credential type, i.e. `email` or `tel`. string method = 1; // Value to verify, i.e. `user@example.com` or `+18003287448` string value = 2; // Indicator that the credential is validated bool done = 3; } // Topic description, S2C in Meta message message TopicDesc { int64 created_at = 1; int64 updated_at = 2; int64 touched_at = 3; DefaultAcsMode defacs = 4; AccessMode acs = 5; int32 seq_id = 6; int32 read_id = 7; int32 recv_id = 8; int32 del_id = 9; bytes public = 10; bytes private = 11; string state = 12; int64 state_at = 13; bytes trusted = 14; bool is_chan = 17; // 17! bool online = 18; // P2P only: other user's last online timestamp & user agent int64 last_seen_time = 15; string last_seen_user_agent = 16; } // MsgTopicSub: topic subscription details, sent in Meta message message TopicSub { int64 updated_at = 1; int64 deleted_at = 2; bool online = 3; AccessMode acs = 4; int32 read_id = 5; int32 recv_id = 6; bytes public = 7; bytes trusted = 16; // 16! bytes private = 8; // Response to non-'me' topic // Uid of the subscribed user string user_id = 9; // 'me' topic only // Topic name of this subscription string topic = 10; int64 touched_at = 11; // ID of the last {data} message in a topic int32 seq_id = 12; // Messages are deleted up to this ID int32 del_id = 13; // P2P topics only: // Other user's last online timestamp & user agent int64 last_seen_time = 14; string last_seen_user_agent = 15; } message DelValues { int32 del_id = 1; repeated SeqRange del_seq = 2; } // {ctrl} message message ServerCtrl { string id = 1; string topic = 2; int32 code = 3; string text = 4; map params = 5; } // {data} message message ServerData { string topic = 1; // ID of the user who originated the message as {pub}, could be empty if sent by the system string from_user_id = 2; // Timestamp when the message was sent. int64 timestamp = 7; // Timestamp when the message was deleted or 0. Milliseconds since the epoch 01/01/1970 int64 deleted_at = 3; int32 seq_id = 4; map head = 5; bytes content = 6; } // {pres} message message ServerPres { string topic = 1; string src = 2; enum What { // Invalid value. The name must be globally unique. X3 = 0; ON = 1; OFF = 2; UA = 3; UPD = 4; GONE = 5; ACS = 6; TERM = 7; MSG = 8; READ = 9; RECV = 10; DEL = 11; TAGS = 12; AUX = 13; } What what = 3; string user_agent = 4; int32 seq_id = 5; int32 del_id = 6; repeated SeqRange del_seq = 7; string target_user_id = 8; string actor_user_id = 9; AccessMode acs = 10; } // {meta} message message ServerMeta { string id = 1; string topic = 2; TopicDesc desc = 3; repeated TopicSub sub = 4; DelValues del = 5; repeated string tags = 6; repeated ServerCred cred = 7; map aux = 8; } // {info} message: server-side copy of ClientNote with From and optional Src added. message ServerInfo { string topic = 1; string from_user_id = 2; InfoNote what = 3; int32 seq_id = 4; string src = 5; CallEvent event = 6; bytes payload = 7; } // Cumulative message message ServerMsg { oneof Message { ServerCtrl ctrl = 1; ServerData data = 2; ServerPres pres = 3; ServerMeta meta = 4; ServerInfo info = 5; } // DEPRECATED. Will be removed soon. // When response is sent to Root, send internal topic name too. string topic = 6 [deprecated = true]; } // Plugin response codes enum RespCode { // Instruct Tinode server to continue with default processing of the client request. CONTINUE = 0; // Drop the request as if the client did not send it DROP = 1; // Send the the provided srvmsg response to the client. ServerResp must contain non-zero // srvmsg. RESPOND = 2; // Replace client's original request with the provided clmsg request then continue with // processing. ServerResp must contain non-zero clmsg. REPLACE = 3; } message ServerResp { RespCode status = 1; ServerMsg srvmsg = 2; ClientMsg clmsg = 3; } // Context message message Session { string session_id = 1; string user_id = 2; AuthLevel auth_level = 3; string remote_addr = 4; string user_agent = 5; string device_id = 6; string language = 7; } message ClientReq { ClientMsg msg = 1; Session sess = 2; } // Search message SearchQuery { string user_id = 1; string query = 2; } message SearchFound { RespCode status = 1; // New search query If status == REPLACE, otherwise unset. string query = 2; // Search results. repeated TopicSub result = 3; } // CRUD event messages enum Crud { CREATE = 0; UPDATE = 1; DELETE = 2; } message TopicEvent { Crud action = 1; string name = 2; TopicDesc desc = 3; } message AccountEvent { Crud action = 1; string user_id = 2; DefaultAcsMode default_acs = 3; bytes public = 4; // Indexable tags for user discovery repeated string tags = 8; } message SubscriptionEvent { Crud action = 1; string topic = 2; string user_id = 3; int32 del_id = 4; int32 read_id = 5; int32 recv_id = 6; AccessMode mode = 7; bytes private = 8; } message MessageEvent { Crud action = 1; ServerData msg = 2; } // Large file handling. message Auth { string scheme = 1; string secret = 2; } // File description. message FileMeta { string name = 1; string mime_type = 2; string etag = 3; int64 size = 4; } // File upload request. message FileUpReq { // Request ID. string id = 1; // Request authentication credentials. Auth auth = 2; // The topic this upload belongs to. string topic = 3; // Uploaded metadata. FileMeta meta = 4; // File bytes being uploaded. bytes content = 5; } // Response to file upload. message FileUpResp { // Response ID. string id = 1; // Response code. int32 code = 2; // Response text. string text = 3; FileMeta meta = 4; // New upload location. string redir_url = 5; } // File download request. message FileDownReq { // Request ID string id = 1; // Request authentication credentials. Auth auth = 2; // File URI to download. string uri = 3; // ETag string if_modified = 4; } // Response to file download. message FileDownResp { // Response ID. string id = 1; // Response code. int32 code = 2; // Response text. string text = 3; FileMeta meta = 4; // File location. string redir_url = 5; // File bytes. bytes content = 6; } ================================================ FILE: pbx/model_grpc.pb.go ================================================ // Code generated by protoc-gen-go-grpc. DO NOT EDIT. // versions: // - protoc-gen-go-grpc v1.2.0 // - protoc v3.21.12 // source: model.proto package pbx import ( context "context" grpc "google.golang.org/grpc" codes "google.golang.org/grpc/codes" status "google.golang.org/grpc/status" ) // This is a compile-time assertion to ensure that this generated file // is compatible with the grpc package it is being compiled against. // Requires gRPC-Go v1.32.0 or later. const _ = grpc.SupportPackageIsVersion7 // NodeClient is the client API for Node service. // // For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. type NodeClient interface { // Client sends a stream of ClientMsg, server responds with a stream of ServerMsg MessageLoop(ctx context.Context, opts ...grpc.CallOption) (Node_MessageLoopClient, error) // Large file upload: a request with a stream of chunks. LargeFileReceive(ctx context.Context, opts ...grpc.CallOption) (Node_LargeFileReceiveClient, error) // Large file file download: a response with a stream of chunks. LargeFileServe(ctx context.Context, in *FileDownReq, opts ...grpc.CallOption) (Node_LargeFileServeClient, error) } type nodeClient struct { cc grpc.ClientConnInterface } func NewNodeClient(cc grpc.ClientConnInterface) NodeClient { return &nodeClient{cc} } func (c *nodeClient) MessageLoop(ctx context.Context, opts ...grpc.CallOption) (Node_MessageLoopClient, error) { stream, err := c.cc.NewStream(ctx, &Node_ServiceDesc.Streams[0], "/pbx.Node/MessageLoop", opts...) if err != nil { return nil, err } x := &nodeMessageLoopClient{stream} return x, nil } type Node_MessageLoopClient interface { Send(*ClientMsg) error Recv() (*ServerMsg, error) grpc.ClientStream } type nodeMessageLoopClient struct { grpc.ClientStream } func (x *nodeMessageLoopClient) Send(m *ClientMsg) error { return x.ClientStream.SendMsg(m) } func (x *nodeMessageLoopClient) Recv() (*ServerMsg, error) { m := new(ServerMsg) if err := x.ClientStream.RecvMsg(m); err != nil { return nil, err } return m, nil } func (c *nodeClient) LargeFileReceive(ctx context.Context, opts ...grpc.CallOption) (Node_LargeFileReceiveClient, error) { stream, err := c.cc.NewStream(ctx, &Node_ServiceDesc.Streams[1], "/pbx.Node/LargeFileReceive", opts...) if err != nil { return nil, err } x := &nodeLargeFileReceiveClient{stream} return x, nil } type Node_LargeFileReceiveClient interface { Send(*FileUpReq) error CloseAndRecv() (*FileUpResp, error) grpc.ClientStream } type nodeLargeFileReceiveClient struct { grpc.ClientStream } func (x *nodeLargeFileReceiveClient) Send(m *FileUpReq) error { return x.ClientStream.SendMsg(m) } func (x *nodeLargeFileReceiveClient) CloseAndRecv() (*FileUpResp, error) { if err := x.ClientStream.CloseSend(); err != nil { return nil, err } m := new(FileUpResp) if err := x.ClientStream.RecvMsg(m); err != nil { return nil, err } return m, nil } func (c *nodeClient) LargeFileServe(ctx context.Context, in *FileDownReq, opts ...grpc.CallOption) (Node_LargeFileServeClient, error) { stream, err := c.cc.NewStream(ctx, &Node_ServiceDesc.Streams[2], "/pbx.Node/LargeFileServe", opts...) if err != nil { return nil, err } x := &nodeLargeFileServeClient{stream} if err := x.ClientStream.SendMsg(in); err != nil { return nil, err } if err := x.ClientStream.CloseSend(); err != nil { return nil, err } return x, nil } type Node_LargeFileServeClient interface { Recv() (*FileDownResp, error) grpc.ClientStream } type nodeLargeFileServeClient struct { grpc.ClientStream } func (x *nodeLargeFileServeClient) Recv() (*FileDownResp, error) { m := new(FileDownResp) if err := x.ClientStream.RecvMsg(m); err != nil { return nil, err } return m, nil } // NodeServer is the server API for Node service. // All implementations must embed UnimplementedNodeServer // for forward compatibility type NodeServer interface { // Client sends a stream of ClientMsg, server responds with a stream of ServerMsg MessageLoop(Node_MessageLoopServer) error // Large file upload: a request with a stream of chunks. LargeFileReceive(Node_LargeFileReceiveServer) error // Large file file download: a response with a stream of chunks. LargeFileServe(*FileDownReq, Node_LargeFileServeServer) error mustEmbedUnimplementedNodeServer() } // UnimplementedNodeServer must be embedded to have forward compatible implementations. type UnimplementedNodeServer struct { } func (UnimplementedNodeServer) MessageLoop(Node_MessageLoopServer) error { return status.Errorf(codes.Unimplemented, "method MessageLoop not implemented") } func (UnimplementedNodeServer) LargeFileReceive(Node_LargeFileReceiveServer) error { return status.Errorf(codes.Unimplemented, "method LargeFileReceive not implemented") } func (UnimplementedNodeServer) LargeFileServe(*FileDownReq, Node_LargeFileServeServer) error { return status.Errorf(codes.Unimplemented, "method LargeFileServe not implemented") } func (UnimplementedNodeServer) mustEmbedUnimplementedNodeServer() {} // UnsafeNodeServer may be embedded to opt out of forward compatibility for this service. // Use of this interface is not recommended, as added methods to NodeServer will // result in compilation errors. type UnsafeNodeServer interface { mustEmbedUnimplementedNodeServer() } func RegisterNodeServer(s grpc.ServiceRegistrar, srv NodeServer) { s.RegisterService(&Node_ServiceDesc, srv) } func _Node_MessageLoop_Handler(srv interface{}, stream grpc.ServerStream) error { return srv.(NodeServer).MessageLoop(&nodeMessageLoopServer{stream}) } type Node_MessageLoopServer interface { Send(*ServerMsg) error Recv() (*ClientMsg, error) grpc.ServerStream } type nodeMessageLoopServer struct { grpc.ServerStream } func (x *nodeMessageLoopServer) Send(m *ServerMsg) error { return x.ServerStream.SendMsg(m) } func (x *nodeMessageLoopServer) Recv() (*ClientMsg, error) { m := new(ClientMsg) if err := x.ServerStream.RecvMsg(m); err != nil { return nil, err } return m, nil } func _Node_LargeFileReceive_Handler(srv interface{}, stream grpc.ServerStream) error { return srv.(NodeServer).LargeFileReceive(&nodeLargeFileReceiveServer{stream}) } type Node_LargeFileReceiveServer interface { SendAndClose(*FileUpResp) error Recv() (*FileUpReq, error) grpc.ServerStream } type nodeLargeFileReceiveServer struct { grpc.ServerStream } func (x *nodeLargeFileReceiveServer) SendAndClose(m *FileUpResp) error { return x.ServerStream.SendMsg(m) } func (x *nodeLargeFileReceiveServer) Recv() (*FileUpReq, error) { m := new(FileUpReq) if err := x.ServerStream.RecvMsg(m); err != nil { return nil, err } return m, nil } func _Node_LargeFileServe_Handler(srv interface{}, stream grpc.ServerStream) error { m := new(FileDownReq) if err := stream.RecvMsg(m); err != nil { return err } return srv.(NodeServer).LargeFileServe(m, &nodeLargeFileServeServer{stream}) } type Node_LargeFileServeServer interface { Send(*FileDownResp) error grpc.ServerStream } type nodeLargeFileServeServer struct { grpc.ServerStream } func (x *nodeLargeFileServeServer) Send(m *FileDownResp) error { return x.ServerStream.SendMsg(m) } // Node_ServiceDesc is the grpc.ServiceDesc for Node service. // It's only intended for direct use with grpc.RegisterService, // and not to be introspected or modified (even as a copy) var Node_ServiceDesc = grpc.ServiceDesc{ ServiceName: "pbx.Node", HandlerType: (*NodeServer)(nil), Methods: []grpc.MethodDesc{}, Streams: []grpc.StreamDesc{ { StreamName: "MessageLoop", Handler: _Node_MessageLoop_Handler, ServerStreams: true, ClientStreams: true, }, { StreamName: "LargeFileReceive", Handler: _Node_LargeFileReceive_Handler, ClientStreams: true, }, { StreamName: "LargeFileServe", Handler: _Node_LargeFileServe_Handler, ServerStreams: true, }, }, Metadata: "model.proto", } // PluginClient is the client API for Plugin service. // // For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. type PluginClient interface { // This plugin method is called by Tinode server for every message received from the clients. The // method returns a ServerResp message. ServerResp.status tells Tinode server what to do next. // See possible values for ServerResp.status in RespCode below. FireHose(ctx context.Context, in *ClientReq, opts ...grpc.CallOption) (*ServerResp, error) // An alteranative user and topic discovery mechanism. // A search request issued on a 'fnd' topic. This method is called to generate an alternative result set. Find(ctx context.Context, in *SearchQuery, opts ...grpc.CallOption) (*SearchFound, error) // Account created, updated or deleted Account(ctx context.Context, in *AccountEvent, opts ...grpc.CallOption) (*Unused, error) // Topic created, updated [or deleted -- not supported yet] Topic(ctx context.Context, in *TopicEvent, opts ...grpc.CallOption) (*Unused, error) // Subscription created, updated or deleted Subscription(ctx context.Context, in *SubscriptionEvent, opts ...grpc.CallOption) (*Unused, error) // Message published or deleted Message(ctx context.Context, in *MessageEvent, opts ...grpc.CallOption) (*Unused, error) } type pluginClient struct { cc grpc.ClientConnInterface } func NewPluginClient(cc grpc.ClientConnInterface) PluginClient { return &pluginClient{cc} } func (c *pluginClient) FireHose(ctx context.Context, in *ClientReq, opts ...grpc.CallOption) (*ServerResp, error) { out := new(ServerResp) err := c.cc.Invoke(ctx, "/pbx.Plugin/FireHose", in, out, opts...) if err != nil { return nil, err } return out, nil } func (c *pluginClient) Find(ctx context.Context, in *SearchQuery, opts ...grpc.CallOption) (*SearchFound, error) { out := new(SearchFound) err := c.cc.Invoke(ctx, "/pbx.Plugin/Find", in, out, opts...) if err != nil { return nil, err } return out, nil } func (c *pluginClient) Account(ctx context.Context, in *AccountEvent, opts ...grpc.CallOption) (*Unused, error) { out := new(Unused) err := c.cc.Invoke(ctx, "/pbx.Plugin/Account", in, out, opts...) if err != nil { return nil, err } return out, nil } func (c *pluginClient) Topic(ctx context.Context, in *TopicEvent, opts ...grpc.CallOption) (*Unused, error) { out := new(Unused) err := c.cc.Invoke(ctx, "/pbx.Plugin/Topic", in, out, opts...) if err != nil { return nil, err } return out, nil } func (c *pluginClient) Subscription(ctx context.Context, in *SubscriptionEvent, opts ...grpc.CallOption) (*Unused, error) { out := new(Unused) err := c.cc.Invoke(ctx, "/pbx.Plugin/Subscription", in, out, opts...) if err != nil { return nil, err } return out, nil } func (c *pluginClient) Message(ctx context.Context, in *MessageEvent, opts ...grpc.CallOption) (*Unused, error) { out := new(Unused) err := c.cc.Invoke(ctx, "/pbx.Plugin/Message", in, out, opts...) if err != nil { return nil, err } return out, nil } // PluginServer is the server API for Plugin service. // All implementations must embed UnimplementedPluginServer // for forward compatibility type PluginServer interface { // This plugin method is called by Tinode server for every message received from the clients. The // method returns a ServerResp message. ServerResp.status tells Tinode server what to do next. // See possible values for ServerResp.status in RespCode below. FireHose(context.Context, *ClientReq) (*ServerResp, error) // An alteranative user and topic discovery mechanism. // A search request issued on a 'fnd' topic. This method is called to generate an alternative result set. Find(context.Context, *SearchQuery) (*SearchFound, error) // Account created, updated or deleted Account(context.Context, *AccountEvent) (*Unused, error) // Topic created, updated [or deleted -- not supported yet] Topic(context.Context, *TopicEvent) (*Unused, error) // Subscription created, updated or deleted Subscription(context.Context, *SubscriptionEvent) (*Unused, error) // Message published or deleted Message(context.Context, *MessageEvent) (*Unused, error) mustEmbedUnimplementedPluginServer() } // UnimplementedPluginServer must be embedded to have forward compatible implementations. type UnimplementedPluginServer struct { } func (UnimplementedPluginServer) FireHose(context.Context, *ClientReq) (*ServerResp, error) { return nil, status.Errorf(codes.Unimplemented, "method FireHose not implemented") } func (UnimplementedPluginServer) Find(context.Context, *SearchQuery) (*SearchFound, error) { return nil, status.Errorf(codes.Unimplemented, "method Find not implemented") } func (UnimplementedPluginServer) Account(context.Context, *AccountEvent) (*Unused, error) { return nil, status.Errorf(codes.Unimplemented, "method Account not implemented") } func (UnimplementedPluginServer) Topic(context.Context, *TopicEvent) (*Unused, error) { return nil, status.Errorf(codes.Unimplemented, "method Topic not implemented") } func (UnimplementedPluginServer) Subscription(context.Context, *SubscriptionEvent) (*Unused, error) { return nil, status.Errorf(codes.Unimplemented, "method Subscription not implemented") } func (UnimplementedPluginServer) Message(context.Context, *MessageEvent) (*Unused, error) { return nil, status.Errorf(codes.Unimplemented, "method Message not implemented") } func (UnimplementedPluginServer) mustEmbedUnimplementedPluginServer() {} // UnsafePluginServer may be embedded to opt out of forward compatibility for this service. // Use of this interface is not recommended, as added methods to PluginServer will // result in compilation errors. type UnsafePluginServer interface { mustEmbedUnimplementedPluginServer() } func RegisterPluginServer(s grpc.ServiceRegistrar, srv PluginServer) { s.RegisterService(&Plugin_ServiceDesc, srv) } func _Plugin_FireHose_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { in := new(ClientReq) if err := dec(in); err != nil { return nil, err } if interceptor == nil { return srv.(PluginServer).FireHose(ctx, in) } info := &grpc.UnaryServerInfo{ Server: srv, FullMethod: "/pbx.Plugin/FireHose", } handler := func(ctx context.Context, req interface{}) (interface{}, error) { return srv.(PluginServer).FireHose(ctx, req.(*ClientReq)) } return interceptor(ctx, in, info, handler) } func _Plugin_Find_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { in := new(SearchQuery) if err := dec(in); err != nil { return nil, err } if interceptor == nil { return srv.(PluginServer).Find(ctx, in) } info := &grpc.UnaryServerInfo{ Server: srv, FullMethod: "/pbx.Plugin/Find", } handler := func(ctx context.Context, req interface{}) (interface{}, error) { return srv.(PluginServer).Find(ctx, req.(*SearchQuery)) } return interceptor(ctx, in, info, handler) } func _Plugin_Account_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { in := new(AccountEvent) if err := dec(in); err != nil { return nil, err } if interceptor == nil { return srv.(PluginServer).Account(ctx, in) } info := &grpc.UnaryServerInfo{ Server: srv, FullMethod: "/pbx.Plugin/Account", } handler := func(ctx context.Context, req interface{}) (interface{}, error) { return srv.(PluginServer).Account(ctx, req.(*AccountEvent)) } return interceptor(ctx, in, info, handler) } func _Plugin_Topic_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { in := new(TopicEvent) if err := dec(in); err != nil { return nil, err } if interceptor == nil { return srv.(PluginServer).Topic(ctx, in) } info := &grpc.UnaryServerInfo{ Server: srv, FullMethod: "/pbx.Plugin/Topic", } handler := func(ctx context.Context, req interface{}) (interface{}, error) { return srv.(PluginServer).Topic(ctx, req.(*TopicEvent)) } return interceptor(ctx, in, info, handler) } func _Plugin_Subscription_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { in := new(SubscriptionEvent) if err := dec(in); err != nil { return nil, err } if interceptor == nil { return srv.(PluginServer).Subscription(ctx, in) } info := &grpc.UnaryServerInfo{ Server: srv, FullMethod: "/pbx.Plugin/Subscription", } handler := func(ctx context.Context, req interface{}) (interface{}, error) { return srv.(PluginServer).Subscription(ctx, req.(*SubscriptionEvent)) } return interceptor(ctx, in, info, handler) } func _Plugin_Message_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { in := new(MessageEvent) if err := dec(in); err != nil { return nil, err } if interceptor == nil { return srv.(PluginServer).Message(ctx, in) } info := &grpc.UnaryServerInfo{ Server: srv, FullMethod: "/pbx.Plugin/Message", } handler := func(ctx context.Context, req interface{}) (interface{}, error) { return srv.(PluginServer).Message(ctx, req.(*MessageEvent)) } return interceptor(ctx, in, info, handler) } // Plugin_ServiceDesc is the grpc.ServiceDesc for Plugin service. // It's only intended for direct use with grpc.RegisterService, // and not to be introspected or modified (even as a copy) var Plugin_ServiceDesc = grpc.ServiceDesc{ ServiceName: "pbx.Plugin", HandlerType: (*PluginServer)(nil), Methods: []grpc.MethodDesc{ { MethodName: "FireHose", Handler: _Plugin_FireHose_Handler, }, { MethodName: "Find", Handler: _Plugin_Find_Handler, }, { MethodName: "Account", Handler: _Plugin_Account_Handler, }, { MethodName: "Topic", Handler: _Plugin_Topic_Handler, }, { MethodName: "Subscription", Handler: _Plugin_Subscription_Handler, }, { MethodName: "Message", Handler: _Plugin_Message_Handler, }, }, Streams: []grpc.StreamDesc{}, Metadata: "model.proto", } ================================================ FILE: pbx/py-generate.sh ================================================ #!/bin/bash # Generate python gRPC bindings for Tinode. A command line parameter v=XX will use specified python version, # i.e. ./generate-python.sh v=3 will use python3. for line in $@; do eval "$line" done python="python${v}" # This generates python gRPC bindings for Tinode. $python -m grpc_tools.protoc -I../pbx --python_out=../py_grpc/tinode_grpc --grpc_python_out=../py_grpc/tinode_grpc --pyi_out=../py_grpc/tinode_grpc ../pbx/model.proto # Bindings are incompatible with Python packaging system. This is a fix. $python py_fix.py ================================================ FILE: pbx/py_fix.py ================================================ # grpc-tools generates python 2 file which does not work with # python3 packaging system. This is a fix. model_pb2_grpc = "../py_grpc/tinode_grpc/model_pb2_grpc.py" with open(model_pb2_grpc, "r") as fh: content = fh.read().replace("\nimport model_pb2 as model__pb2", "\nfrom . import model_pb2 as model__pb2") with open(model_pb2_grpc,"w") as fh: fh.write(content) ================================================ FILE: py_grpc/.gitignore ================================================ # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] *$py.class /build/ /dist/ /tinode_grpc/GIT_VERSION *.egg-info/ ================================================ FILE: py_grpc/LICENSE ================================================ The code in this folder is licensed under Apache 2.0 http://www.apache.org/licenses/LICENSE-2.0 ================================================ FILE: py_grpc/README.md ================================================ # Generated Protocol Buffer and gRPC files for [Tinode](https://github.com/tinode) Generated Python code for [gRPC](https://grpc.io/) client and plugins. gRPC clients must implement rpc service `Node`, plugins must implement `Plugin`. For a sample implementation of a command line client see [tn-cli](https://github.com/tinode/chat/tree/master/tn-cli/). For a partial plugin implementation see [chatbot](https://github.com/tinode/chat/tree/master/chatbot). ## Installing Install the package by executing ``` pip install tinode_grpc ``` ## Generating files Don't modify included files directly. If you want to make changes, you have to install protobuffers tool chain and gRPC then generate the Python bindings from [`pbx/model.proto`](https://github.com/tinode/chat/tree/master/pbx/model.proto) (your path to `model.proto` may be different): ``` python -m grpc_tools.protoc -I../pbx --python_out=. --pyi_out=. --grpc_python_out=. ../pbx/model.proto ``` The generated `model_pb2_grpc.py` imports `model_pb2.py` as a module instead of a package which is incompatible with python3 packaging system. Use `../pbx/py_fix.py` to apply a fix. This is only needed if you want to repackage the generated files. ================================================ FILE: py_grpc/pyproject.toml ================================================ [build-system] requires = ["setuptools>=45", "wheel"] build-backend = "setuptools.build_meta" [project] name = "tinode_grpc" description = "Tinode gRPC bindings." authors = [ {name = "Tinode Authors", email = "info@tinode.co"}, ] license = "Apache-2.0" readme = "README.md" keywords = ["chat", "messaging", "messenger", "im", "tinode"] classifiers = [ "Programming Language :: Python :: 2", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", "Operating System :: OS Independent", "Topic :: Communications :: Chat", "Intended Audience :: Developers", ] dependencies = [ "protobuf>=3.6.1", "grpcio>=1.19.0", ] dynamic = ["version"] [project.urls] Homepage = "https://github.com/tinode/chat" Repository = "https://github.com/tinode/chat" Issues = "https://github.com/tinode/chat/issues" [tool.setuptools] packages = ["tinode_grpc"] [tool.setuptools.package-data] "*" = ["GIT_VERSION", "*.pyi"] # Alternative version handling if you want to keep reading from GIT_VERSION file [tool.setuptools.dynamic] version = {file = "tinode_grpc/GIT_VERSION"} ================================================ FILE: py_grpc/tinode_grpc/__init__.py ================================================ from . import model_pb2 as pb from . import model_pb2_grpc as pbx ================================================ FILE: py_grpc/tinode_grpc/model_pb2.py ================================================ # -*- coding: utf-8 -*- # Generated by the protocol buffer compiler. DO NOT EDIT! # source: model.proto """Generated protocol buffer code.""" from google.protobuf import descriptor as _descriptor from google.protobuf import descriptor_pool as _descriptor_pool from google.protobuf import symbol_database as _symbol_database from google.protobuf.internal import builder as _builder # @@protoc_insertion_point(imports) _sym_db = _symbol_database.Default() DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x0bmodel.proto\x12\x03pbx\"\x08\n\x06Unused\",\n\x0e\x44\x65\x66\x61ultAcsMode\x12\x0c\n\x04\x61uth\x18\x01 \x01(\t\x12\x0c\n\x04\x61non\x18\x02 \x01(\t\")\n\nAccessMode\x12\x0c\n\x04want\x18\x01 \x01(\t\x12\r\n\x05given\x18\x02 \x01(\t\"\'\n\x06SetSub\x12\x0f\n\x07user_id\x18\x01 \x01(\t\x12\x0c\n\x04mode\x18\x02 \x01(\t\"\x99\x01\n\nClientCred\x12\x0e\n\x06method\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\t\x12\x10\n\x08response\x18\x03 \x01(\t\x12+\n\x06params\x18\x04 \x03(\x0b\x32\x1b.pbx.ClientCred.ParamsEntry\x1a-\n\x0bParamsEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\x0c:\x02\x38\x01\"e\n\x07SetDesc\x12(\n\x0b\x64\x65\x66\x61ult_acs\x18\x01 \x01(\x0b\x32\x13.pbx.DefaultAcsMode\x12\x0e\n\x06public\x18\x02 \x01(\x0c\x12\x0f\n\x07private\x18\x03 \x01(\x0c\x12\x0f\n\x07trusted\x18\x04 \x01(\x0c\"#\n\x08SeqRange\x12\x0b\n\x03low\x18\x01 \x01(\x05\x12\n\n\x02hi\x18\x02 \x01(\x05\"\x94\x01\n\x07GetOpts\x12\x19\n\x11if_modified_since\x18\x01 \x01(\x03\x12\x0c\n\x04user\x18\x02 \x01(\t\x12\r\n\x05topic\x18\x03 \x01(\t\x12\x10\n\x08since_id\x18\x04 \x01(\x05\x12\x11\n\tbefore_id\x18\x05 \x01(\x05\x12\r\n\x05limit\x18\x06 \x01(\x05\x12\x1d\n\x06ranges\x18\x07 \x03(\x0b\x32\r.pbx.SeqRange\"k\n\x08GetQuery\x12\x0c\n\x04what\x18\x01 \x01(\t\x12\x1a\n\x04\x64\x65sc\x18\x02 \x01(\x0b\x32\x0c.pbx.GetOpts\x12\x19\n\x03sub\x18\x03 \x01(\x0b\x32\x0c.pbx.GetOpts\x12\x1a\n\x04\x64\x61ta\x18\x04 \x01(\x0b\x32\x0c.pbx.GetOpts\"\xbe\x01\n\x08SetQuery\x12\x1a\n\x04\x64\x65sc\x18\x01 \x01(\x0b\x32\x0c.pbx.SetDesc\x12\x18\n\x03sub\x18\x02 \x01(\x0b\x32\x0b.pbx.SetSub\x12\x0c\n\x04tags\x18\x03 \x03(\t\x12\x1d\n\x04\x63red\x18\x04 \x01(\x0b\x32\x0f.pbx.ClientCred\x12#\n\x03\x61ux\x18\x05 \x03(\x0b\x32\x16.pbx.SetQuery.AuxEntry\x1a*\n\x08\x41uxEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\x0c:\x02\x38\x01\"~\n\x08\x43lientHi\x12\n\n\x02id\x18\x01 \x01(\t\x12\x12\n\nuser_agent\x18\x02 \x01(\t\x12\x0b\n\x03ver\x18\x03 \x01(\t\x12\x11\n\tdevice_id\x18\x04 \x01(\t\x12\x0c\n\x04lang\x18\x05 \x01(\t\x12\x10\n\x08platform\x18\x06 \x01(\t\x12\x12\n\nbackground\x18\x07 \x01(\x08\"\x8a\x02\n\tClientAcc\x12\n\n\x02id\x18\x01 \x01(\t\x12\x0f\n\x07user_id\x18\x02 \x01(\t\x12\x0e\n\x06scheme\x18\x03 \x01(\t\x12\x0e\n\x06secret\x18\x04 \x01(\x0c\x12\r\n\x05login\x18\x05 \x01(\x08\x12\x0c\n\x04tags\x18\x06 \x03(\t\x12\x1a\n\x04\x64\x65sc\x18\x07 \x01(\x0b\x32\x0c.pbx.SetDesc\x12\x1d\n\x04\x63red\x18\x08 \x03(\x0b\x32\x0f.pbx.ClientCred\x12\r\n\x05token\x18\t \x01(\x0c\x12\r\n\x05state\x18\n \x01(\t\x12\"\n\nauth_level\x18\x0b \x01(\x0e\x32\x0e.pbx.AuthLevel\x12\x12\n\ntmp_scheme\x18\x0c \x01(\t\x12\x12\n\ntmp_secret\x18\r \x01(\x0c\"X\n\x0b\x43lientLogin\x12\n\n\x02id\x18\x01 \x01(\t\x12\x0e\n\x06scheme\x18\x02 \x01(\t\x12\x0e\n\x06secret\x18\x03 \x01(\x0c\x12\x1d\n\x04\x63red\x18\x04 \x03(\x0b\x32\x0f.pbx.ClientCred\"j\n\tClientSub\x12\n\n\x02id\x18\x01 \x01(\t\x12\r\n\x05topic\x18\x02 \x01(\t\x12 \n\tset_query\x18\x03 \x01(\x0b\x32\r.pbx.SetQuery\x12 \n\tget_query\x18\x04 \x01(\x0b\x32\r.pbx.GetQuery\"7\n\x0b\x43lientLeave\x12\n\n\x02id\x18\x01 \x01(\t\x12\r\n\x05topic\x18\x02 \x01(\t\x12\r\n\x05unsub\x18\x03 \x01(\x08\"\x9d\x01\n\tClientPub\x12\n\n\x02id\x18\x01 \x01(\t\x12\r\n\x05topic\x18\x02 \x01(\t\x12\x0f\n\x07no_echo\x18\x03 \x01(\x08\x12&\n\x04head\x18\x04 \x03(\x0b\x32\x18.pbx.ClientPub.HeadEntry\x12\x0f\n\x07\x63ontent\x18\x05 \x01(\x0c\x1a+\n\tHeadEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\x0c:\x02\x38\x01\"D\n\tClientGet\x12\n\n\x02id\x18\x01 \x01(\t\x12\r\n\x05topic\x18\x02 \x01(\t\x12\x1c\n\x05query\x18\x03 \x01(\x0b\x32\r.pbx.GetQuery\"D\n\tClientSet\x12\n\n\x02id\x18\x01 \x01(\t\x12\r\n\x05topic\x18\x02 \x01(\t\x12\x1c\n\x05query\x18\x03 \x01(\x0b\x32\r.pbx.SetQuery\"\xe8\x01\n\tClientDel\x12\n\n\x02id\x18\x01 \x01(\t\x12\r\n\x05topic\x18\x02 \x01(\t\x12!\n\x04what\x18\x03 \x01(\x0e\x32\x13.pbx.ClientDel.What\x12\x1e\n\x07\x64\x65l_seq\x18\x04 \x03(\x0b\x32\r.pbx.SeqRange\x12\x0f\n\x07user_id\x18\x05 \x01(\t\x12\x1d\n\x04\x63red\x18\x06 \x01(\x0b\x32\x0f.pbx.ClientCred\x12\x0c\n\x04hard\x18\x07 \x01(\x08\"?\n\x04What\x12\x06\n\x02X0\x10\x00\x12\x07\n\x03MSG\x10\x01\x12\t\n\x05TOPIC\x10\x02\x12\x07\n\x03SUB\x10\x03\x12\x08\n\x04USER\x10\x04\x12\x08\n\x04\x43RED\x10\x05\"\x88\x01\n\nClientNote\x12\r\n\x05topic\x18\x01 \x01(\t\x12\x1b\n\x04what\x18\x02 \x01(\x0e\x32\r.pbx.InfoNote\x12\x0e\n\x06seq_id\x18\x03 \x01(\x05\x12\x0e\n\x06unread\x18\x04 \x01(\x05\x12\x1d\n\x05\x65vent\x18\x05 \x01(\x0e\x32\x0e.pbx.CallEvent\x12\x0f\n\x07payload\x18\x06 \x01(\x0c\"\\\n\x0b\x43lientExtra\x12\x13\n\x0b\x61ttachments\x18\x01 \x03(\t\x12\x14\n\x0con_behalf_of\x18\x02 \x01(\t\x12\"\n\nauth_level\x18\x03 \x01(\x0e\x32\x0e.pbx.AuthLevel\"\xf5\x02\n\tClientMsg\x12\x1b\n\x02hi\x18\x01 \x01(\x0b\x32\r.pbx.ClientHiH\x00\x12\x1d\n\x03\x61\x63\x63\x18\x02 \x01(\x0b\x32\x0e.pbx.ClientAccH\x00\x12!\n\x05login\x18\x03 \x01(\x0b\x32\x10.pbx.ClientLoginH\x00\x12\x1d\n\x03sub\x18\x04 \x01(\x0b\x32\x0e.pbx.ClientSubH\x00\x12!\n\x05leave\x18\x05 \x01(\x0b\x32\x10.pbx.ClientLeaveH\x00\x12\x1d\n\x03pub\x18\x06 \x01(\x0b\x32\x0e.pbx.ClientPubH\x00\x12\x1d\n\x03get\x18\x07 \x01(\x0b\x32\x0e.pbx.ClientGetH\x00\x12\x1d\n\x03set\x18\x08 \x01(\x0b\x32\x0e.pbx.ClientSetH\x00\x12\x1d\n\x03\x64\x65l\x18\t \x01(\x0b\x32\x0e.pbx.ClientDelH\x00\x12\x1f\n\x04note\x18\n \x01(\x0b\x32\x0f.pbx.ClientNoteH\x00\x12\x1f\n\x05\x65xtra\x18\r \x01(\x0b\x32\x10.pbx.ClientExtraB\t\n\x07Message\"9\n\nServerCred\x12\x0e\n\x06method\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\t\x12\x0c\n\x04\x64one\x18\x03 \x01(\x08\"\xf6\x02\n\tTopicDesc\x12\x12\n\ncreated_at\x18\x01 \x01(\x03\x12\x12\n\nupdated_at\x18\x02 \x01(\x03\x12\x12\n\ntouched_at\x18\x03 \x01(\x03\x12#\n\x06\x64\x65\x66\x61\x63s\x18\x04 \x01(\x0b\x32\x13.pbx.DefaultAcsMode\x12\x1c\n\x03\x61\x63s\x18\x05 \x01(\x0b\x32\x0f.pbx.AccessMode\x12\x0e\n\x06seq_id\x18\x06 \x01(\x05\x12\x0f\n\x07read_id\x18\x07 \x01(\x05\x12\x0f\n\x07recv_id\x18\x08 \x01(\x05\x12\x0e\n\x06\x64\x65l_id\x18\t \x01(\x05\x12\x0e\n\x06public\x18\n \x01(\x0c\x12\x0f\n\x07private\x18\x0b \x01(\x0c\x12\r\n\x05state\x18\x0c \x01(\t\x12\x10\n\x08state_at\x18\r \x01(\x03\x12\x0f\n\x07trusted\x18\x0e \x01(\x0c\x12\x0f\n\x07is_chan\x18\x11 \x01(\x08\x12\x0e\n\x06online\x18\x12 \x01(\x08\x12\x16\n\x0elast_seen_time\x18\x0f \x01(\x03\x12\x1c\n\x14last_seen_user_agent\x18\x10 \x01(\t\"\xbe\x02\n\x08TopicSub\x12\x12\n\nupdated_at\x18\x01 \x01(\x03\x12\x12\n\ndeleted_at\x18\x02 \x01(\x03\x12\x0e\n\x06online\x18\x03 \x01(\x08\x12\x1c\n\x03\x61\x63s\x18\x04 \x01(\x0b\x32\x0f.pbx.AccessMode\x12\x0f\n\x07read_id\x18\x05 \x01(\x05\x12\x0f\n\x07recv_id\x18\x06 \x01(\x05\x12\x0e\n\x06public\x18\x07 \x01(\x0c\x12\x0f\n\x07trusted\x18\x10 \x01(\x0c\x12\x0f\n\x07private\x18\x08 \x01(\x0c\x12\x0f\n\x07user_id\x18\t \x01(\t\x12\r\n\x05topic\x18\n \x01(\t\x12\x12\n\ntouched_at\x18\x0b \x01(\x03\x12\x0e\n\x06seq_id\x18\x0c \x01(\x05\x12\x0e\n\x06\x64\x65l_id\x18\r \x01(\x05\x12\x16\n\x0elast_seen_time\x18\x0e \x01(\x03\x12\x1c\n\x14last_seen_user_agent\x18\x0f \x01(\t\";\n\tDelValues\x12\x0e\n\x06\x64\x65l_id\x18\x01 \x01(\x05\x12\x1e\n\x07\x64\x65l_seq\x18\x02 \x03(\x0b\x32\r.pbx.SeqRange\"\x9f\x01\n\nServerCtrl\x12\n\n\x02id\x18\x01 \x01(\t\x12\r\n\x05topic\x18\x02 \x01(\t\x12\x0c\n\x04\x63ode\x18\x03 \x01(\x05\x12\x0c\n\x04text\x18\x04 \x01(\t\x12+\n\x06params\x18\x05 \x03(\x0b\x32\x1b.pbx.ServerCtrl.ParamsEntry\x1a-\n\x0bParamsEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\x0c:\x02\x38\x01\"\xcf\x01\n\nServerData\x12\r\n\x05topic\x18\x01 \x01(\t\x12\x14\n\x0c\x66rom_user_id\x18\x02 \x01(\t\x12\x11\n\ttimestamp\x18\x07 \x01(\x03\x12\x12\n\ndeleted_at\x18\x03 \x01(\x03\x12\x0e\n\x06seq_id\x18\x04 \x01(\x05\x12\'\n\x04head\x18\x05 \x03(\x0b\x32\x19.pbx.ServerData.HeadEntry\x12\x0f\n\x07\x63ontent\x18\x06 \x01(\x0c\x1a+\n\tHeadEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\x0c:\x02\x38\x01\"\xf6\x02\n\nServerPres\x12\r\n\x05topic\x18\x01 \x01(\t\x12\x0b\n\x03src\x18\x02 \x01(\t\x12\"\n\x04what\x18\x03 \x01(\x0e\x32\x14.pbx.ServerPres.What\x12\x12\n\nuser_agent\x18\x04 \x01(\t\x12\x0e\n\x06seq_id\x18\x05 \x01(\x05\x12\x0e\n\x06\x64\x65l_id\x18\x06 \x01(\x05\x12\x1e\n\x07\x64\x65l_seq\x18\x07 \x03(\x0b\x32\r.pbx.SeqRange\x12\x16\n\x0etarget_user_id\x18\x08 \x01(\t\x12\x15\n\ractor_user_id\x18\t \x01(\t\x12\x1c\n\x03\x61\x63s\x18\n \x01(\x0b\x32\x0f.pbx.AccessMode\"\x86\x01\n\x04What\x12\x06\n\x02X3\x10\x00\x12\x06\n\x02ON\x10\x01\x12\x07\n\x03OFF\x10\x02\x12\x06\n\x02UA\x10\x03\x12\x07\n\x03UPD\x10\x04\x12\x08\n\x04GONE\x10\x05\x12\x07\n\x03\x41\x43S\x10\x06\x12\x08\n\x04TERM\x10\x07\x12\x07\n\x03MSG\x10\x08\x12\x08\n\x04READ\x10\t\x12\x08\n\x04RECV\x10\n\x12\x07\n\x03\x44\x45L\x10\x0b\x12\x08\n\x04TAGS\x10\x0c\x12\x07\n\x03\x41UX\x10\r\"\xfe\x01\n\nServerMeta\x12\n\n\x02id\x18\x01 \x01(\t\x12\r\n\x05topic\x18\x02 \x01(\t\x12\x1c\n\x04\x64\x65sc\x18\x03 \x01(\x0b\x32\x0e.pbx.TopicDesc\x12\x1a\n\x03sub\x18\x04 \x03(\x0b\x32\r.pbx.TopicSub\x12\x1b\n\x03\x64\x65l\x18\x05 \x01(\x0b\x32\x0e.pbx.DelValues\x12\x0c\n\x04tags\x18\x06 \x03(\t\x12\x1d\n\x04\x63red\x18\x07 \x03(\x0b\x32\x0f.pbx.ServerCred\x12%\n\x03\x61ux\x18\x08 \x03(\x0b\x32\x18.pbx.ServerMeta.AuxEntry\x1a*\n\x08\x41uxEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\x0c:\x02\x38\x01\"\x9b\x01\n\nServerInfo\x12\r\n\x05topic\x18\x01 \x01(\t\x12\x14\n\x0c\x66rom_user_id\x18\x02 \x01(\t\x12\x1b\n\x04what\x18\x03 \x01(\x0e\x32\r.pbx.InfoNote\x12\x0e\n\x06seq_id\x18\x04 \x01(\x05\x12\x0b\n\x03src\x18\x05 \x01(\t\x12\x1d\n\x05\x65vent\x18\x06 \x01(\x0e\x32\x0e.pbx.CallEvent\x12\x0f\n\x07payload\x18\x07 \x01(\x0c\"\xce\x01\n\tServerMsg\x12\x1f\n\x04\x63trl\x18\x01 \x01(\x0b\x32\x0f.pbx.ServerCtrlH\x00\x12\x1f\n\x04\x64\x61ta\x18\x02 \x01(\x0b\x32\x0f.pbx.ServerDataH\x00\x12\x1f\n\x04pres\x18\x03 \x01(\x0b\x32\x0f.pbx.ServerPresH\x00\x12\x1f\n\x04meta\x18\x04 \x01(\x0b\x32\x0f.pbx.ServerMetaH\x00\x12\x1f\n\x04info\x18\x05 \x01(\x0b\x32\x0f.pbx.ServerInfoH\x00\x12\x11\n\x05topic\x18\x06 \x01(\tB\x02\x18\x01\x42\t\n\x07Message\"j\n\nServerResp\x12\x1d\n\x06status\x18\x01 \x01(\x0e\x32\r.pbx.RespCode\x12\x1e\n\x06srvmsg\x18\x02 \x01(\x0b\x32\x0e.pbx.ServerMsg\x12\x1d\n\x05\x63lmsg\x18\x03 \x01(\x0b\x32\x0e.pbx.ClientMsg\"\xa0\x01\n\x07Session\x12\x12\n\nsession_id\x18\x01 \x01(\t\x12\x0f\n\x07user_id\x18\x02 \x01(\t\x12\"\n\nauth_level\x18\x03 \x01(\x0e\x32\x0e.pbx.AuthLevel\x12\x13\n\x0bremote_addr\x18\x04 \x01(\t\x12\x12\n\nuser_agent\x18\x05 \x01(\t\x12\x11\n\tdevice_id\x18\x06 \x01(\t\x12\x10\n\x08language\x18\x07 \x01(\t\"D\n\tClientReq\x12\x1b\n\x03msg\x18\x01 \x01(\x0b\x32\x0e.pbx.ClientMsg\x12\x1a\n\x04sess\x18\x02 \x01(\x0b\x32\x0c.pbx.Session\"-\n\x0bSearchQuery\x12\x0f\n\x07user_id\x18\x01 \x01(\t\x12\r\n\x05query\x18\x02 \x01(\t\"Z\n\x0bSearchFound\x12\x1d\n\x06status\x18\x01 \x01(\x0e\x32\r.pbx.RespCode\x12\r\n\x05query\x18\x02 \x01(\t\x12\x1d\n\x06result\x18\x03 \x03(\x0b\x32\r.pbx.TopicSub\"S\n\nTopicEvent\x12\x19\n\x06\x61\x63tion\x18\x01 \x01(\x0e\x32\t.pbx.Crud\x12\x0c\n\x04name\x18\x02 \x01(\t\x12\x1c\n\x04\x64\x65sc\x18\x03 \x01(\x0b\x32\x0e.pbx.TopicDesc\"\x82\x01\n\x0c\x41\x63\x63ountEvent\x12\x19\n\x06\x61\x63tion\x18\x01 \x01(\x0e\x32\t.pbx.Crud\x12\x0f\n\x07user_id\x18\x02 \x01(\t\x12(\n\x0b\x64\x65\x66\x61ult_acs\x18\x03 \x01(\x0b\x32\x13.pbx.DefaultAcsMode\x12\x0e\n\x06public\x18\x04 \x01(\x0c\x12\x0c\n\x04tags\x18\x08 \x03(\t\"\xb0\x01\n\x11SubscriptionEvent\x12\x19\n\x06\x61\x63tion\x18\x01 \x01(\x0e\x32\t.pbx.Crud\x12\r\n\x05topic\x18\x02 \x01(\t\x12\x0f\n\x07user_id\x18\x03 \x01(\t\x12\x0e\n\x06\x64\x65l_id\x18\x04 \x01(\x05\x12\x0f\n\x07read_id\x18\x05 \x01(\x05\x12\x0f\n\x07recv_id\x18\x06 \x01(\x05\x12\x1d\n\x04mode\x18\x07 \x01(\x0b\x32\x0f.pbx.AccessMode\x12\x0f\n\x07private\x18\x08 \x01(\x0c\"G\n\x0cMessageEvent\x12\x19\n\x06\x61\x63tion\x18\x01 \x01(\x0e\x32\t.pbx.Crud\x12\x1c\n\x03msg\x18\x02 \x01(\x0b\x32\x0f.pbx.ServerData\"&\n\x04\x41uth\x12\x0e\n\x06scheme\x18\x01 \x01(\t\x12\x0e\n\x06secret\x18\x02 \x01(\t\"G\n\x08\x46ileMeta\x12\x0c\n\x04name\x18\x01 \x01(\t\x12\x11\n\tmime_type\x18\x02 \x01(\t\x12\x0c\n\x04\x65tag\x18\x03 \x01(\t\x12\x0c\n\x04size\x18\x04 \x01(\x03\"m\n\tFileUpReq\x12\n\n\x02id\x18\x01 \x01(\t\x12\x17\n\x04\x61uth\x18\x02 \x01(\x0b\x32\t.pbx.Auth\x12\r\n\x05topic\x18\x03 \x01(\t\x12\x1b\n\x04meta\x18\x04 \x01(\x0b\x32\r.pbx.FileMeta\x12\x0f\n\x07\x63ontent\x18\x05 \x01(\x0c\"d\n\nFileUpResp\x12\n\n\x02id\x18\x01 \x01(\t\x12\x0c\n\x04\x63ode\x18\x02 \x01(\x05\x12\x0c\n\x04text\x18\x03 \x01(\t\x12\x1b\n\x04meta\x18\x04 \x01(\x0b\x32\r.pbx.FileMeta\x12\x11\n\tredir_url\x18\x05 \x01(\t\"T\n\x0b\x46ileDownReq\x12\n\n\x02id\x18\x01 \x01(\t\x12\x17\n\x04\x61uth\x18\x02 \x01(\x0b\x32\t.pbx.Auth\x12\x0b\n\x03uri\x18\x03 \x01(\t\x12\x13\n\x0bif_modified\x18\x04 \x01(\t\"w\n\x0c\x46ileDownResp\x12\n\n\x02id\x18\x01 \x01(\t\x12\x0c\n\x04\x63ode\x18\x02 \x01(\x05\x12\x0c\n\x04text\x18\x03 \x01(\t\x12\x1b\n\x04meta\x18\x04 \x01(\x0b\x32\r.pbx.FileMeta\x12\x11\n\tredir_url\x18\x05 \x01(\t\x12\x0f\n\x07\x63ontent\x18\x06 \x01(\x0c*3\n\tAuthLevel\x12\x08\n\x04NONE\x10\x00\x12\x08\n\x04\x41NON\x10\n\x12\x08\n\x04\x41UTH\x10\x14\x12\x08\n\x04ROOT\x10\x1e*8\n\x08InfoNote\x12\x06\n\x02X1\x10\x00\x12\x08\n\x04READ\x10\x01\x12\x08\n\x04RECV\x10\x02\x12\x06\n\x02KP\x10\x03\x12\x08\n\x04\x43\x41LL\x10\x04*o\n\tCallEvent\x12\x06\n\x02X2\x10\x00\x12\n\n\x06\x41\x43\x43\x45PT\x10\x01\x12\n\n\x06\x41NSWER\x10\x02\x12\x0b\n\x07HANG_UP\x10\x03\x12\x11\n\rICE_CANDIDATE\x10\x04\x12\n\n\x06INVITE\x10\x05\x12\t\n\x05OFFER\x10\x06\x12\x0b\n\x07RINGING\x10\x07*<\n\x08RespCode\x12\x0c\n\x08\x43ONTINUE\x10\x00\x12\x08\n\x04\x44ROP\x10\x01\x12\x0b\n\x07RESPOND\x10\x02\x12\x0b\n\x07REPLACE\x10\x03**\n\x04\x43rud\x12\n\n\x06\x43REATE\x10\x00\x12\n\n\x06UPDATE\x10\x01\x12\n\n\x06\x44\x45LETE\x10\x02\x32\xaf\x01\n\x04Node\x12\x33\n\x0bMessageLoop\x12\x0e.pbx.ClientMsg\x1a\x0e.pbx.ServerMsg\"\x00(\x01\x30\x01\x12\x37\n\x10LargeFileReceive\x12\x0e.pbx.FileUpReq\x1a\x0f.pbx.FileUpResp\"\x00(\x01\x12\x39\n\x0eLargeFileServe\x12\x10.pbx.FileDownReq\x1a\x11.pbx.FileDownResp\"\x00\x30\x01\x32\x9f\x02\n\x06Plugin\x12-\n\x08\x46ireHose\x12\x0e.pbx.ClientReq\x1a\x0f.pbx.ServerResp\"\x00\x12,\n\x04\x46ind\x12\x10.pbx.SearchQuery\x1a\x10.pbx.SearchFound\"\x00\x12+\n\x07\x41\x63\x63ount\x12\x11.pbx.AccountEvent\x1a\x0b.pbx.Unused\"\x00\x12\'\n\x05Topic\x12\x0f.pbx.TopicEvent\x1a\x0b.pbx.Unused\"\x00\x12\x35\n\x0cSubscription\x12\x16.pbx.SubscriptionEvent\x1a\x0b.pbx.Unused\"\x00\x12+\n\x07Message\x12\x11.pbx.MessageEvent\x1a\x0b.pbx.Unused\"\x00\x42\x1cZ\x1agithub.com/tinode/chat/pbxb\x06proto3') _globals = globals() _builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals) _builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'model_pb2', _globals) if _descriptor._USE_C_DESCRIPTORS == False: DESCRIPTOR._options = None DESCRIPTOR._serialized_options = b'Z\032github.com/tinode/chat/pbx' _CLIENTCRED_PARAMSENTRY._options = None _CLIENTCRED_PARAMSENTRY._serialized_options = b'8\001' _SETQUERY_AUXENTRY._options = None _SETQUERY_AUXENTRY._serialized_options = b'8\001' _CLIENTPUB_HEADENTRY._options = None _CLIENTPUB_HEADENTRY._serialized_options = b'8\001' _SERVERCTRL_PARAMSENTRY._options = None _SERVERCTRL_PARAMSENTRY._serialized_options = b'8\001' _SERVERDATA_HEADENTRY._options = None _SERVERDATA_HEADENTRY._serialized_options = b'8\001' _SERVERMETA_AUXENTRY._options = None _SERVERMETA_AUXENTRY._serialized_options = b'8\001' _SERVERMSG.fields_by_name['topic']._options = None _SERVERMSG.fields_by_name['topic']._serialized_options = b'\030\001' _globals['_AUTHLEVEL']._serialized_start=6379 _globals['_AUTHLEVEL']._serialized_end=6430 _globals['_INFONOTE']._serialized_start=6432 _globals['_INFONOTE']._serialized_end=6488 _globals['_CALLEVENT']._serialized_start=6490 _globals['_CALLEVENT']._serialized_end=6601 _globals['_RESPCODE']._serialized_start=6603 _globals['_RESPCODE']._serialized_end=6663 _globals['_CRUD']._serialized_start=6665 _globals['_CRUD']._serialized_end=6707 _globals['_UNUSED']._serialized_start=20 _globals['_UNUSED']._serialized_end=28 _globals['_DEFAULTACSMODE']._serialized_start=30 _globals['_DEFAULTACSMODE']._serialized_end=74 _globals['_ACCESSMODE']._serialized_start=76 _globals['_ACCESSMODE']._serialized_end=117 _globals['_SETSUB']._serialized_start=119 _globals['_SETSUB']._serialized_end=158 _globals['_CLIENTCRED']._serialized_start=161 _globals['_CLIENTCRED']._serialized_end=314 _globals['_CLIENTCRED_PARAMSENTRY']._serialized_start=269 _globals['_CLIENTCRED_PARAMSENTRY']._serialized_end=314 _globals['_SETDESC']._serialized_start=316 _globals['_SETDESC']._serialized_end=417 _globals['_SEQRANGE']._serialized_start=419 _globals['_SEQRANGE']._serialized_end=454 _globals['_GETOPTS']._serialized_start=457 _globals['_GETOPTS']._serialized_end=605 _globals['_GETQUERY']._serialized_start=607 _globals['_GETQUERY']._serialized_end=714 _globals['_SETQUERY']._serialized_start=717 _globals['_SETQUERY']._serialized_end=907 _globals['_SETQUERY_AUXENTRY']._serialized_start=865 _globals['_SETQUERY_AUXENTRY']._serialized_end=907 _globals['_CLIENTHI']._serialized_start=909 _globals['_CLIENTHI']._serialized_end=1035 _globals['_CLIENTACC']._serialized_start=1038 _globals['_CLIENTACC']._serialized_end=1304 _globals['_CLIENTLOGIN']._serialized_start=1306 _globals['_CLIENTLOGIN']._serialized_end=1394 _globals['_CLIENTSUB']._serialized_start=1396 _globals['_CLIENTSUB']._serialized_end=1502 _globals['_CLIENTLEAVE']._serialized_start=1504 _globals['_CLIENTLEAVE']._serialized_end=1559 _globals['_CLIENTPUB']._serialized_start=1562 _globals['_CLIENTPUB']._serialized_end=1719 _globals['_CLIENTPUB_HEADENTRY']._serialized_start=1676 _globals['_CLIENTPUB_HEADENTRY']._serialized_end=1719 _globals['_CLIENTGET']._serialized_start=1721 _globals['_CLIENTGET']._serialized_end=1789 _globals['_CLIENTSET']._serialized_start=1791 _globals['_CLIENTSET']._serialized_end=1859 _globals['_CLIENTDEL']._serialized_start=1862 _globals['_CLIENTDEL']._serialized_end=2094 _globals['_CLIENTDEL_WHAT']._serialized_start=2031 _globals['_CLIENTDEL_WHAT']._serialized_end=2094 _globals['_CLIENTNOTE']._serialized_start=2097 _globals['_CLIENTNOTE']._serialized_end=2233 _globals['_CLIENTEXTRA']._serialized_start=2235 _globals['_CLIENTEXTRA']._serialized_end=2327 _globals['_CLIENTMSG']._serialized_start=2330 _globals['_CLIENTMSG']._serialized_end=2703 _globals['_SERVERCRED']._serialized_start=2705 _globals['_SERVERCRED']._serialized_end=2762 _globals['_TOPICDESC']._serialized_start=2765 _globals['_TOPICDESC']._serialized_end=3139 _globals['_TOPICSUB']._serialized_start=3142 _globals['_TOPICSUB']._serialized_end=3460 _globals['_DELVALUES']._serialized_start=3462 _globals['_DELVALUES']._serialized_end=3521 _globals['_SERVERCTRL']._serialized_start=3524 _globals['_SERVERCTRL']._serialized_end=3683 _globals['_SERVERCTRL_PARAMSENTRY']._serialized_start=269 _globals['_SERVERCTRL_PARAMSENTRY']._serialized_end=314 _globals['_SERVERDATA']._serialized_start=3686 _globals['_SERVERDATA']._serialized_end=3893 _globals['_SERVERDATA_HEADENTRY']._serialized_start=1676 _globals['_SERVERDATA_HEADENTRY']._serialized_end=1719 _globals['_SERVERPRES']._serialized_start=3896 _globals['_SERVERPRES']._serialized_end=4270 _globals['_SERVERPRES_WHAT']._serialized_start=4136 _globals['_SERVERPRES_WHAT']._serialized_end=4270 _globals['_SERVERMETA']._serialized_start=4273 _globals['_SERVERMETA']._serialized_end=4527 _globals['_SERVERMETA_AUXENTRY']._serialized_start=865 _globals['_SERVERMETA_AUXENTRY']._serialized_end=907 _globals['_SERVERINFO']._serialized_start=4530 _globals['_SERVERINFO']._serialized_end=4685 _globals['_SERVERMSG']._serialized_start=4688 _globals['_SERVERMSG']._serialized_end=4894 _globals['_SERVERRESP']._serialized_start=4896 _globals['_SERVERRESP']._serialized_end=5002 _globals['_SESSION']._serialized_start=5005 _globals['_SESSION']._serialized_end=5165 _globals['_CLIENTREQ']._serialized_start=5167 _globals['_CLIENTREQ']._serialized_end=5235 _globals['_SEARCHQUERY']._serialized_start=5237 _globals['_SEARCHQUERY']._serialized_end=5282 _globals['_SEARCHFOUND']._serialized_start=5284 _globals['_SEARCHFOUND']._serialized_end=5374 _globals['_TOPICEVENT']._serialized_start=5376 _globals['_TOPICEVENT']._serialized_end=5459 _globals['_ACCOUNTEVENT']._serialized_start=5462 _globals['_ACCOUNTEVENT']._serialized_end=5592 _globals['_SUBSCRIPTIONEVENT']._serialized_start=5595 _globals['_SUBSCRIPTIONEVENT']._serialized_end=5771 _globals['_MESSAGEEVENT']._serialized_start=5773 _globals['_MESSAGEEVENT']._serialized_end=5844 _globals['_AUTH']._serialized_start=5846 _globals['_AUTH']._serialized_end=5884 _globals['_FILEMETA']._serialized_start=5886 _globals['_FILEMETA']._serialized_end=5957 _globals['_FILEUPREQ']._serialized_start=5959 _globals['_FILEUPREQ']._serialized_end=6068 _globals['_FILEUPRESP']._serialized_start=6070 _globals['_FILEUPRESP']._serialized_end=6170 _globals['_FILEDOWNREQ']._serialized_start=6172 _globals['_FILEDOWNREQ']._serialized_end=6256 _globals['_FILEDOWNRESP']._serialized_start=6258 _globals['_FILEDOWNRESP']._serialized_end=6377 _globals['_NODE']._serialized_start=6710 _globals['_NODE']._serialized_end=6885 _globals['_PLUGIN']._serialized_start=6888 _globals['_PLUGIN']._serialized_end=7175 # @@protoc_insertion_point(module_scope) ================================================ FILE: py_grpc/tinode_grpc/model_pb2.pyi ================================================ from google.protobuf.internal import containers as _containers from google.protobuf.internal import enum_type_wrapper as _enum_type_wrapper from google.protobuf import descriptor as _descriptor from google.protobuf import message as _message from typing import ClassVar as _ClassVar, Iterable as _Iterable, Mapping as _Mapping, Optional as _Optional, Union as _Union DESCRIPTOR: _descriptor.FileDescriptor class AuthLevel(int, metaclass=_enum_type_wrapper.EnumTypeWrapper): __slots__ = [] NONE: _ClassVar[AuthLevel] ANON: _ClassVar[AuthLevel] AUTH: _ClassVar[AuthLevel] ROOT: _ClassVar[AuthLevel] class InfoNote(int, metaclass=_enum_type_wrapper.EnumTypeWrapper): __slots__ = [] X1: _ClassVar[InfoNote] READ: _ClassVar[InfoNote] RECV: _ClassVar[InfoNote] KP: _ClassVar[InfoNote] CALL: _ClassVar[InfoNote] class CallEvent(int, metaclass=_enum_type_wrapper.EnumTypeWrapper): __slots__ = [] X2: _ClassVar[CallEvent] ACCEPT: _ClassVar[CallEvent] ANSWER: _ClassVar[CallEvent] HANG_UP: _ClassVar[CallEvent] ICE_CANDIDATE: _ClassVar[CallEvent] INVITE: _ClassVar[CallEvent] OFFER: _ClassVar[CallEvent] RINGING: _ClassVar[CallEvent] class RespCode(int, metaclass=_enum_type_wrapper.EnumTypeWrapper): __slots__ = [] CONTINUE: _ClassVar[RespCode] DROP: _ClassVar[RespCode] RESPOND: _ClassVar[RespCode] REPLACE: _ClassVar[RespCode] class Crud(int, metaclass=_enum_type_wrapper.EnumTypeWrapper): __slots__ = [] CREATE: _ClassVar[Crud] UPDATE: _ClassVar[Crud] DELETE: _ClassVar[Crud] NONE: AuthLevel ANON: AuthLevel AUTH: AuthLevel ROOT: AuthLevel X1: InfoNote READ: InfoNote RECV: InfoNote KP: InfoNote CALL: InfoNote X2: CallEvent ACCEPT: CallEvent ANSWER: CallEvent HANG_UP: CallEvent ICE_CANDIDATE: CallEvent INVITE: CallEvent OFFER: CallEvent RINGING: CallEvent CONTINUE: RespCode DROP: RespCode RESPOND: RespCode REPLACE: RespCode CREATE: Crud UPDATE: Crud DELETE: Crud class Unused(_message.Message): __slots__ = [] def __init__(self) -> None: ... class DefaultAcsMode(_message.Message): __slots__ = ["auth", "anon"] AUTH_FIELD_NUMBER: _ClassVar[int] ANON_FIELD_NUMBER: _ClassVar[int] auth: str anon: str def __init__(self, auth: _Optional[str] = ..., anon: _Optional[str] = ...) -> None: ... class AccessMode(_message.Message): __slots__ = ["want", "given"] WANT_FIELD_NUMBER: _ClassVar[int] GIVEN_FIELD_NUMBER: _ClassVar[int] want: str given: str def __init__(self, want: _Optional[str] = ..., given: _Optional[str] = ...) -> None: ... class SetSub(_message.Message): __slots__ = ["user_id", "mode"] USER_ID_FIELD_NUMBER: _ClassVar[int] MODE_FIELD_NUMBER: _ClassVar[int] user_id: str mode: str def __init__(self, user_id: _Optional[str] = ..., mode: _Optional[str] = ...) -> None: ... class ClientCred(_message.Message): __slots__ = ["method", "value", "response", "params"] class ParamsEntry(_message.Message): __slots__ = ["key", "value"] KEY_FIELD_NUMBER: _ClassVar[int] VALUE_FIELD_NUMBER: _ClassVar[int] key: str value: bytes def __init__(self, key: _Optional[str] = ..., value: _Optional[bytes] = ...) -> None: ... METHOD_FIELD_NUMBER: _ClassVar[int] VALUE_FIELD_NUMBER: _ClassVar[int] RESPONSE_FIELD_NUMBER: _ClassVar[int] PARAMS_FIELD_NUMBER: _ClassVar[int] method: str value: str response: str params: _containers.ScalarMap[str, bytes] def __init__(self, method: _Optional[str] = ..., value: _Optional[str] = ..., response: _Optional[str] = ..., params: _Optional[_Mapping[str, bytes]] = ...) -> None: ... class SetDesc(_message.Message): __slots__ = ["default_acs", "public", "private", "trusted"] DEFAULT_ACS_FIELD_NUMBER: _ClassVar[int] PUBLIC_FIELD_NUMBER: _ClassVar[int] PRIVATE_FIELD_NUMBER: _ClassVar[int] TRUSTED_FIELD_NUMBER: _ClassVar[int] default_acs: DefaultAcsMode public: bytes private: bytes trusted: bytes def __init__(self, default_acs: _Optional[_Union[DefaultAcsMode, _Mapping]] = ..., public: _Optional[bytes] = ..., private: _Optional[bytes] = ..., trusted: _Optional[bytes] = ...) -> None: ... class SeqRange(_message.Message): __slots__ = ["low", "hi"] LOW_FIELD_NUMBER: _ClassVar[int] HI_FIELD_NUMBER: _ClassVar[int] low: int hi: int def __init__(self, low: _Optional[int] = ..., hi: _Optional[int] = ...) -> None: ... class GetOpts(_message.Message): __slots__ = ["if_modified_since", "user", "topic", "since_id", "before_id", "limit", "ranges"] IF_MODIFIED_SINCE_FIELD_NUMBER: _ClassVar[int] USER_FIELD_NUMBER: _ClassVar[int] TOPIC_FIELD_NUMBER: _ClassVar[int] SINCE_ID_FIELD_NUMBER: _ClassVar[int] BEFORE_ID_FIELD_NUMBER: _ClassVar[int] LIMIT_FIELD_NUMBER: _ClassVar[int] RANGES_FIELD_NUMBER: _ClassVar[int] if_modified_since: int user: str topic: str since_id: int before_id: int limit: int ranges: _containers.RepeatedCompositeFieldContainer[SeqRange] def __init__(self, if_modified_since: _Optional[int] = ..., user: _Optional[str] = ..., topic: _Optional[str] = ..., since_id: _Optional[int] = ..., before_id: _Optional[int] = ..., limit: _Optional[int] = ..., ranges: _Optional[_Iterable[_Union[SeqRange, _Mapping]]] = ...) -> None: ... class GetQuery(_message.Message): __slots__ = ["what", "desc", "sub", "data"] WHAT_FIELD_NUMBER: _ClassVar[int] DESC_FIELD_NUMBER: _ClassVar[int] SUB_FIELD_NUMBER: _ClassVar[int] DATA_FIELD_NUMBER: _ClassVar[int] what: str desc: GetOpts sub: GetOpts data: GetOpts def __init__(self, what: _Optional[str] = ..., desc: _Optional[_Union[GetOpts, _Mapping]] = ..., sub: _Optional[_Union[GetOpts, _Mapping]] = ..., data: _Optional[_Union[GetOpts, _Mapping]] = ...) -> None: ... class SetQuery(_message.Message): __slots__ = ["desc", "sub", "tags", "cred", "aux"] class AuxEntry(_message.Message): __slots__ = ["key", "value"] KEY_FIELD_NUMBER: _ClassVar[int] VALUE_FIELD_NUMBER: _ClassVar[int] key: str value: bytes def __init__(self, key: _Optional[str] = ..., value: _Optional[bytes] = ...) -> None: ... DESC_FIELD_NUMBER: _ClassVar[int] SUB_FIELD_NUMBER: _ClassVar[int] TAGS_FIELD_NUMBER: _ClassVar[int] CRED_FIELD_NUMBER: _ClassVar[int] AUX_FIELD_NUMBER: _ClassVar[int] desc: SetDesc sub: SetSub tags: _containers.RepeatedScalarFieldContainer[str] cred: ClientCred aux: _containers.ScalarMap[str, bytes] def __init__(self, desc: _Optional[_Union[SetDesc, _Mapping]] = ..., sub: _Optional[_Union[SetSub, _Mapping]] = ..., tags: _Optional[_Iterable[str]] = ..., cred: _Optional[_Union[ClientCred, _Mapping]] = ..., aux: _Optional[_Mapping[str, bytes]] = ...) -> None: ... class ClientHi(_message.Message): __slots__ = ["id", "user_agent", "ver", "device_id", "lang", "platform", "background"] ID_FIELD_NUMBER: _ClassVar[int] USER_AGENT_FIELD_NUMBER: _ClassVar[int] VER_FIELD_NUMBER: _ClassVar[int] DEVICE_ID_FIELD_NUMBER: _ClassVar[int] LANG_FIELD_NUMBER: _ClassVar[int] PLATFORM_FIELD_NUMBER: _ClassVar[int] BACKGROUND_FIELD_NUMBER: _ClassVar[int] id: str user_agent: str ver: str device_id: str lang: str platform: str background: bool def __init__(self, id: _Optional[str] = ..., user_agent: _Optional[str] = ..., ver: _Optional[str] = ..., device_id: _Optional[str] = ..., lang: _Optional[str] = ..., platform: _Optional[str] = ..., background: bool = ...) -> None: ... class ClientAcc(_message.Message): __slots__ = ["id", "user_id", "scheme", "secret", "login", "tags", "desc", "cred", "token", "state", "auth_level", "tmp_scheme", "tmp_secret"] ID_FIELD_NUMBER: _ClassVar[int] USER_ID_FIELD_NUMBER: _ClassVar[int] SCHEME_FIELD_NUMBER: _ClassVar[int] SECRET_FIELD_NUMBER: _ClassVar[int] LOGIN_FIELD_NUMBER: _ClassVar[int] TAGS_FIELD_NUMBER: _ClassVar[int] DESC_FIELD_NUMBER: _ClassVar[int] CRED_FIELD_NUMBER: _ClassVar[int] TOKEN_FIELD_NUMBER: _ClassVar[int] STATE_FIELD_NUMBER: _ClassVar[int] AUTH_LEVEL_FIELD_NUMBER: _ClassVar[int] TMP_SCHEME_FIELD_NUMBER: _ClassVar[int] TMP_SECRET_FIELD_NUMBER: _ClassVar[int] id: str user_id: str scheme: str secret: bytes login: bool tags: _containers.RepeatedScalarFieldContainer[str] desc: SetDesc cred: _containers.RepeatedCompositeFieldContainer[ClientCred] token: bytes state: str auth_level: AuthLevel tmp_scheme: str tmp_secret: bytes def __init__(self, id: _Optional[str] = ..., user_id: _Optional[str] = ..., scheme: _Optional[str] = ..., secret: _Optional[bytes] = ..., login: bool = ..., tags: _Optional[_Iterable[str]] = ..., desc: _Optional[_Union[SetDesc, _Mapping]] = ..., cred: _Optional[_Iterable[_Union[ClientCred, _Mapping]]] = ..., token: _Optional[bytes] = ..., state: _Optional[str] = ..., auth_level: _Optional[_Union[AuthLevel, str]] = ..., tmp_scheme: _Optional[str] = ..., tmp_secret: _Optional[bytes] = ...) -> None: ... class ClientLogin(_message.Message): __slots__ = ["id", "scheme", "secret", "cred"] ID_FIELD_NUMBER: _ClassVar[int] SCHEME_FIELD_NUMBER: _ClassVar[int] SECRET_FIELD_NUMBER: _ClassVar[int] CRED_FIELD_NUMBER: _ClassVar[int] id: str scheme: str secret: bytes cred: _containers.RepeatedCompositeFieldContainer[ClientCred] def __init__(self, id: _Optional[str] = ..., scheme: _Optional[str] = ..., secret: _Optional[bytes] = ..., cred: _Optional[_Iterable[_Union[ClientCred, _Mapping]]] = ...) -> None: ... class ClientSub(_message.Message): __slots__ = ["id", "topic", "set_query", "get_query"] ID_FIELD_NUMBER: _ClassVar[int] TOPIC_FIELD_NUMBER: _ClassVar[int] SET_QUERY_FIELD_NUMBER: _ClassVar[int] GET_QUERY_FIELD_NUMBER: _ClassVar[int] id: str topic: str set_query: SetQuery get_query: GetQuery def __init__(self, id: _Optional[str] = ..., topic: _Optional[str] = ..., set_query: _Optional[_Union[SetQuery, _Mapping]] = ..., get_query: _Optional[_Union[GetQuery, _Mapping]] = ...) -> None: ... class ClientLeave(_message.Message): __slots__ = ["id", "topic", "unsub"] ID_FIELD_NUMBER: _ClassVar[int] TOPIC_FIELD_NUMBER: _ClassVar[int] UNSUB_FIELD_NUMBER: _ClassVar[int] id: str topic: str unsub: bool def __init__(self, id: _Optional[str] = ..., topic: _Optional[str] = ..., unsub: bool = ...) -> None: ... class ClientPub(_message.Message): __slots__ = ["id", "topic", "no_echo", "head", "content"] class HeadEntry(_message.Message): __slots__ = ["key", "value"] KEY_FIELD_NUMBER: _ClassVar[int] VALUE_FIELD_NUMBER: _ClassVar[int] key: str value: bytes def __init__(self, key: _Optional[str] = ..., value: _Optional[bytes] = ...) -> None: ... ID_FIELD_NUMBER: _ClassVar[int] TOPIC_FIELD_NUMBER: _ClassVar[int] NO_ECHO_FIELD_NUMBER: _ClassVar[int] HEAD_FIELD_NUMBER: _ClassVar[int] CONTENT_FIELD_NUMBER: _ClassVar[int] id: str topic: str no_echo: bool head: _containers.ScalarMap[str, bytes] content: bytes def __init__(self, id: _Optional[str] = ..., topic: _Optional[str] = ..., no_echo: bool = ..., head: _Optional[_Mapping[str, bytes]] = ..., content: _Optional[bytes] = ...) -> None: ... class ClientGet(_message.Message): __slots__ = ["id", "topic", "query"] ID_FIELD_NUMBER: _ClassVar[int] TOPIC_FIELD_NUMBER: _ClassVar[int] QUERY_FIELD_NUMBER: _ClassVar[int] id: str topic: str query: GetQuery def __init__(self, id: _Optional[str] = ..., topic: _Optional[str] = ..., query: _Optional[_Union[GetQuery, _Mapping]] = ...) -> None: ... class ClientSet(_message.Message): __slots__ = ["id", "topic", "query"] ID_FIELD_NUMBER: _ClassVar[int] TOPIC_FIELD_NUMBER: _ClassVar[int] QUERY_FIELD_NUMBER: _ClassVar[int] id: str topic: str query: SetQuery def __init__(self, id: _Optional[str] = ..., topic: _Optional[str] = ..., query: _Optional[_Union[SetQuery, _Mapping]] = ...) -> None: ... class ClientDel(_message.Message): __slots__ = ["id", "topic", "what", "del_seq", "user_id", "cred", "hard"] class What(int, metaclass=_enum_type_wrapper.EnumTypeWrapper): __slots__ = [] X0: _ClassVar[ClientDel.What] MSG: _ClassVar[ClientDel.What] TOPIC: _ClassVar[ClientDel.What] SUB: _ClassVar[ClientDel.What] USER: _ClassVar[ClientDel.What] CRED: _ClassVar[ClientDel.What] X0: ClientDel.What MSG: ClientDel.What TOPIC: ClientDel.What SUB: ClientDel.What USER: ClientDel.What CRED: ClientDel.What ID_FIELD_NUMBER: _ClassVar[int] TOPIC_FIELD_NUMBER: _ClassVar[int] WHAT_FIELD_NUMBER: _ClassVar[int] DEL_SEQ_FIELD_NUMBER: _ClassVar[int] USER_ID_FIELD_NUMBER: _ClassVar[int] CRED_FIELD_NUMBER: _ClassVar[int] HARD_FIELD_NUMBER: _ClassVar[int] id: str topic: str what: ClientDel.What del_seq: _containers.RepeatedCompositeFieldContainer[SeqRange] user_id: str cred: ClientCred hard: bool def __init__(self, id: _Optional[str] = ..., topic: _Optional[str] = ..., what: _Optional[_Union[ClientDel.What, str]] = ..., del_seq: _Optional[_Iterable[_Union[SeqRange, _Mapping]]] = ..., user_id: _Optional[str] = ..., cred: _Optional[_Union[ClientCred, _Mapping]] = ..., hard: bool = ...) -> None: ... class ClientNote(_message.Message): __slots__ = ["topic", "what", "seq_id", "unread", "event", "payload"] TOPIC_FIELD_NUMBER: _ClassVar[int] WHAT_FIELD_NUMBER: _ClassVar[int] SEQ_ID_FIELD_NUMBER: _ClassVar[int] UNREAD_FIELD_NUMBER: _ClassVar[int] EVENT_FIELD_NUMBER: _ClassVar[int] PAYLOAD_FIELD_NUMBER: _ClassVar[int] topic: str what: InfoNote seq_id: int unread: int event: CallEvent payload: bytes def __init__(self, topic: _Optional[str] = ..., what: _Optional[_Union[InfoNote, str]] = ..., seq_id: _Optional[int] = ..., unread: _Optional[int] = ..., event: _Optional[_Union[CallEvent, str]] = ..., payload: _Optional[bytes] = ...) -> None: ... class ClientExtra(_message.Message): __slots__ = ["attachments", "on_behalf_of", "auth_level"] ATTACHMENTS_FIELD_NUMBER: _ClassVar[int] ON_BEHALF_OF_FIELD_NUMBER: _ClassVar[int] AUTH_LEVEL_FIELD_NUMBER: _ClassVar[int] attachments: _containers.RepeatedScalarFieldContainer[str] on_behalf_of: str auth_level: AuthLevel def __init__(self, attachments: _Optional[_Iterable[str]] = ..., on_behalf_of: _Optional[str] = ..., auth_level: _Optional[_Union[AuthLevel, str]] = ...) -> None: ... class ClientMsg(_message.Message): __slots__ = ["hi", "acc", "login", "sub", "leave", "pub", "get", "set", "note", "extra"] HI_FIELD_NUMBER: _ClassVar[int] ACC_FIELD_NUMBER: _ClassVar[int] LOGIN_FIELD_NUMBER: _ClassVar[int] SUB_FIELD_NUMBER: _ClassVar[int] LEAVE_FIELD_NUMBER: _ClassVar[int] PUB_FIELD_NUMBER: _ClassVar[int] GET_FIELD_NUMBER: _ClassVar[int] SET_FIELD_NUMBER: _ClassVar[int] DEL_FIELD_NUMBER: _ClassVar[int] NOTE_FIELD_NUMBER: _ClassVar[int] EXTRA_FIELD_NUMBER: _ClassVar[int] hi: ClientHi acc: ClientAcc login: ClientLogin sub: ClientSub leave: ClientLeave pub: ClientPub get: ClientGet set: ClientSet note: ClientNote extra: ClientExtra def __init__(self, hi: _Optional[_Union[ClientHi, _Mapping]] = ..., acc: _Optional[_Union[ClientAcc, _Mapping]] = ..., login: _Optional[_Union[ClientLogin, _Mapping]] = ..., sub: _Optional[_Union[ClientSub, _Mapping]] = ..., leave: _Optional[_Union[ClientLeave, _Mapping]] = ..., pub: _Optional[_Union[ClientPub, _Mapping]] = ..., get: _Optional[_Union[ClientGet, _Mapping]] = ..., set: _Optional[_Union[ClientSet, _Mapping]] = ..., note: _Optional[_Union[ClientNote, _Mapping]] = ..., extra: _Optional[_Union[ClientExtra, _Mapping]] = ..., **kwargs) -> None: ... class ServerCred(_message.Message): __slots__ = ["method", "value", "done"] METHOD_FIELD_NUMBER: _ClassVar[int] VALUE_FIELD_NUMBER: _ClassVar[int] DONE_FIELD_NUMBER: _ClassVar[int] method: str value: str done: bool def __init__(self, method: _Optional[str] = ..., value: _Optional[str] = ..., done: bool = ...) -> None: ... class TopicDesc(_message.Message): __slots__ = ["created_at", "updated_at", "touched_at", "defacs", "acs", "seq_id", "read_id", "recv_id", "del_id", "public", "private", "state", "state_at", "trusted", "is_chan", "online", "last_seen_time", "last_seen_user_agent"] CREATED_AT_FIELD_NUMBER: _ClassVar[int] UPDATED_AT_FIELD_NUMBER: _ClassVar[int] TOUCHED_AT_FIELD_NUMBER: _ClassVar[int] DEFACS_FIELD_NUMBER: _ClassVar[int] ACS_FIELD_NUMBER: _ClassVar[int] SEQ_ID_FIELD_NUMBER: _ClassVar[int] READ_ID_FIELD_NUMBER: _ClassVar[int] RECV_ID_FIELD_NUMBER: _ClassVar[int] DEL_ID_FIELD_NUMBER: _ClassVar[int] PUBLIC_FIELD_NUMBER: _ClassVar[int] PRIVATE_FIELD_NUMBER: _ClassVar[int] STATE_FIELD_NUMBER: _ClassVar[int] STATE_AT_FIELD_NUMBER: _ClassVar[int] TRUSTED_FIELD_NUMBER: _ClassVar[int] IS_CHAN_FIELD_NUMBER: _ClassVar[int] ONLINE_FIELD_NUMBER: _ClassVar[int] LAST_SEEN_TIME_FIELD_NUMBER: _ClassVar[int] LAST_SEEN_USER_AGENT_FIELD_NUMBER: _ClassVar[int] created_at: int updated_at: int touched_at: int defacs: DefaultAcsMode acs: AccessMode seq_id: int read_id: int recv_id: int del_id: int public: bytes private: bytes state: str state_at: int trusted: bytes is_chan: bool online: bool last_seen_time: int last_seen_user_agent: str def __init__(self, created_at: _Optional[int] = ..., updated_at: _Optional[int] = ..., touched_at: _Optional[int] = ..., defacs: _Optional[_Union[DefaultAcsMode, _Mapping]] = ..., acs: _Optional[_Union[AccessMode, _Mapping]] = ..., seq_id: _Optional[int] = ..., read_id: _Optional[int] = ..., recv_id: _Optional[int] = ..., del_id: _Optional[int] = ..., public: _Optional[bytes] = ..., private: _Optional[bytes] = ..., state: _Optional[str] = ..., state_at: _Optional[int] = ..., trusted: _Optional[bytes] = ..., is_chan: bool = ..., online: bool = ..., last_seen_time: _Optional[int] = ..., last_seen_user_agent: _Optional[str] = ...) -> None: ... class TopicSub(_message.Message): __slots__ = ["updated_at", "deleted_at", "online", "acs", "read_id", "recv_id", "public", "trusted", "private", "user_id", "topic", "touched_at", "seq_id", "del_id", "last_seen_time", "last_seen_user_agent"] UPDATED_AT_FIELD_NUMBER: _ClassVar[int] DELETED_AT_FIELD_NUMBER: _ClassVar[int] ONLINE_FIELD_NUMBER: _ClassVar[int] ACS_FIELD_NUMBER: _ClassVar[int] READ_ID_FIELD_NUMBER: _ClassVar[int] RECV_ID_FIELD_NUMBER: _ClassVar[int] PUBLIC_FIELD_NUMBER: _ClassVar[int] TRUSTED_FIELD_NUMBER: _ClassVar[int] PRIVATE_FIELD_NUMBER: _ClassVar[int] USER_ID_FIELD_NUMBER: _ClassVar[int] TOPIC_FIELD_NUMBER: _ClassVar[int] TOUCHED_AT_FIELD_NUMBER: _ClassVar[int] SEQ_ID_FIELD_NUMBER: _ClassVar[int] DEL_ID_FIELD_NUMBER: _ClassVar[int] LAST_SEEN_TIME_FIELD_NUMBER: _ClassVar[int] LAST_SEEN_USER_AGENT_FIELD_NUMBER: _ClassVar[int] updated_at: int deleted_at: int online: bool acs: AccessMode read_id: int recv_id: int public: bytes trusted: bytes private: bytes user_id: str topic: str touched_at: int seq_id: int del_id: int last_seen_time: int last_seen_user_agent: str def __init__(self, updated_at: _Optional[int] = ..., deleted_at: _Optional[int] = ..., online: bool = ..., acs: _Optional[_Union[AccessMode, _Mapping]] = ..., read_id: _Optional[int] = ..., recv_id: _Optional[int] = ..., public: _Optional[bytes] = ..., trusted: _Optional[bytes] = ..., private: _Optional[bytes] = ..., user_id: _Optional[str] = ..., topic: _Optional[str] = ..., touched_at: _Optional[int] = ..., seq_id: _Optional[int] = ..., del_id: _Optional[int] = ..., last_seen_time: _Optional[int] = ..., last_seen_user_agent: _Optional[str] = ...) -> None: ... class DelValues(_message.Message): __slots__ = ["del_id", "del_seq"] DEL_ID_FIELD_NUMBER: _ClassVar[int] DEL_SEQ_FIELD_NUMBER: _ClassVar[int] del_id: int del_seq: _containers.RepeatedCompositeFieldContainer[SeqRange] def __init__(self, del_id: _Optional[int] = ..., del_seq: _Optional[_Iterable[_Union[SeqRange, _Mapping]]] = ...) -> None: ... class ServerCtrl(_message.Message): __slots__ = ["id", "topic", "code", "text", "params"] class ParamsEntry(_message.Message): __slots__ = ["key", "value"] KEY_FIELD_NUMBER: _ClassVar[int] VALUE_FIELD_NUMBER: _ClassVar[int] key: str value: bytes def __init__(self, key: _Optional[str] = ..., value: _Optional[bytes] = ...) -> None: ... ID_FIELD_NUMBER: _ClassVar[int] TOPIC_FIELD_NUMBER: _ClassVar[int] CODE_FIELD_NUMBER: _ClassVar[int] TEXT_FIELD_NUMBER: _ClassVar[int] PARAMS_FIELD_NUMBER: _ClassVar[int] id: str topic: str code: int text: str params: _containers.ScalarMap[str, bytes] def __init__(self, id: _Optional[str] = ..., topic: _Optional[str] = ..., code: _Optional[int] = ..., text: _Optional[str] = ..., params: _Optional[_Mapping[str, bytes]] = ...) -> None: ... class ServerData(_message.Message): __slots__ = ["topic", "from_user_id", "timestamp", "deleted_at", "seq_id", "head", "content"] class HeadEntry(_message.Message): __slots__ = ["key", "value"] KEY_FIELD_NUMBER: _ClassVar[int] VALUE_FIELD_NUMBER: _ClassVar[int] key: str value: bytes def __init__(self, key: _Optional[str] = ..., value: _Optional[bytes] = ...) -> None: ... TOPIC_FIELD_NUMBER: _ClassVar[int] FROM_USER_ID_FIELD_NUMBER: _ClassVar[int] TIMESTAMP_FIELD_NUMBER: _ClassVar[int] DELETED_AT_FIELD_NUMBER: _ClassVar[int] SEQ_ID_FIELD_NUMBER: _ClassVar[int] HEAD_FIELD_NUMBER: _ClassVar[int] CONTENT_FIELD_NUMBER: _ClassVar[int] topic: str from_user_id: str timestamp: int deleted_at: int seq_id: int head: _containers.ScalarMap[str, bytes] content: bytes def __init__(self, topic: _Optional[str] = ..., from_user_id: _Optional[str] = ..., timestamp: _Optional[int] = ..., deleted_at: _Optional[int] = ..., seq_id: _Optional[int] = ..., head: _Optional[_Mapping[str, bytes]] = ..., content: _Optional[bytes] = ...) -> None: ... class ServerPres(_message.Message): __slots__ = ["topic", "src", "what", "user_agent", "seq_id", "del_id", "del_seq", "target_user_id", "actor_user_id", "acs"] class What(int, metaclass=_enum_type_wrapper.EnumTypeWrapper): __slots__ = [] X3: _ClassVar[ServerPres.What] ON: _ClassVar[ServerPres.What] OFF: _ClassVar[ServerPres.What] UA: _ClassVar[ServerPres.What] UPD: _ClassVar[ServerPres.What] GONE: _ClassVar[ServerPres.What] ACS: _ClassVar[ServerPres.What] TERM: _ClassVar[ServerPres.What] MSG: _ClassVar[ServerPres.What] READ: _ClassVar[ServerPres.What] RECV: _ClassVar[ServerPres.What] DEL: _ClassVar[ServerPres.What] TAGS: _ClassVar[ServerPres.What] AUX: _ClassVar[ServerPres.What] X3: ServerPres.What ON: ServerPres.What OFF: ServerPres.What UA: ServerPres.What UPD: ServerPres.What GONE: ServerPres.What ACS: ServerPres.What TERM: ServerPres.What MSG: ServerPres.What READ: ServerPres.What RECV: ServerPres.What DEL: ServerPres.What TAGS: ServerPres.What AUX: ServerPres.What TOPIC_FIELD_NUMBER: _ClassVar[int] SRC_FIELD_NUMBER: _ClassVar[int] WHAT_FIELD_NUMBER: _ClassVar[int] USER_AGENT_FIELD_NUMBER: _ClassVar[int] SEQ_ID_FIELD_NUMBER: _ClassVar[int] DEL_ID_FIELD_NUMBER: _ClassVar[int] DEL_SEQ_FIELD_NUMBER: _ClassVar[int] TARGET_USER_ID_FIELD_NUMBER: _ClassVar[int] ACTOR_USER_ID_FIELD_NUMBER: _ClassVar[int] ACS_FIELD_NUMBER: _ClassVar[int] topic: str src: str what: ServerPres.What user_agent: str seq_id: int del_id: int del_seq: _containers.RepeatedCompositeFieldContainer[SeqRange] target_user_id: str actor_user_id: str acs: AccessMode def __init__(self, topic: _Optional[str] = ..., src: _Optional[str] = ..., what: _Optional[_Union[ServerPres.What, str]] = ..., user_agent: _Optional[str] = ..., seq_id: _Optional[int] = ..., del_id: _Optional[int] = ..., del_seq: _Optional[_Iterable[_Union[SeqRange, _Mapping]]] = ..., target_user_id: _Optional[str] = ..., actor_user_id: _Optional[str] = ..., acs: _Optional[_Union[AccessMode, _Mapping]] = ...) -> None: ... class ServerMeta(_message.Message): __slots__ = ["id", "topic", "desc", "sub", "tags", "cred", "aux"] class AuxEntry(_message.Message): __slots__ = ["key", "value"] KEY_FIELD_NUMBER: _ClassVar[int] VALUE_FIELD_NUMBER: _ClassVar[int] key: str value: bytes def __init__(self, key: _Optional[str] = ..., value: _Optional[bytes] = ...) -> None: ... ID_FIELD_NUMBER: _ClassVar[int] TOPIC_FIELD_NUMBER: _ClassVar[int] DESC_FIELD_NUMBER: _ClassVar[int] SUB_FIELD_NUMBER: _ClassVar[int] DEL_FIELD_NUMBER: _ClassVar[int] TAGS_FIELD_NUMBER: _ClassVar[int] CRED_FIELD_NUMBER: _ClassVar[int] AUX_FIELD_NUMBER: _ClassVar[int] id: str topic: str desc: TopicDesc sub: _containers.RepeatedCompositeFieldContainer[TopicSub] tags: _containers.RepeatedScalarFieldContainer[str] cred: _containers.RepeatedCompositeFieldContainer[ServerCred] aux: _containers.ScalarMap[str, bytes] def __init__(self, id: _Optional[str] = ..., topic: _Optional[str] = ..., desc: _Optional[_Union[TopicDesc, _Mapping]] = ..., sub: _Optional[_Iterable[_Union[TopicSub, _Mapping]]] = ..., tags: _Optional[_Iterable[str]] = ..., cred: _Optional[_Iterable[_Union[ServerCred, _Mapping]]] = ..., aux: _Optional[_Mapping[str, bytes]] = ..., **kwargs) -> None: ... class ServerInfo(_message.Message): __slots__ = ["topic", "from_user_id", "what", "seq_id", "src", "event", "payload"] TOPIC_FIELD_NUMBER: _ClassVar[int] FROM_USER_ID_FIELD_NUMBER: _ClassVar[int] WHAT_FIELD_NUMBER: _ClassVar[int] SEQ_ID_FIELD_NUMBER: _ClassVar[int] SRC_FIELD_NUMBER: _ClassVar[int] EVENT_FIELD_NUMBER: _ClassVar[int] PAYLOAD_FIELD_NUMBER: _ClassVar[int] topic: str from_user_id: str what: InfoNote seq_id: int src: str event: CallEvent payload: bytes def __init__(self, topic: _Optional[str] = ..., from_user_id: _Optional[str] = ..., what: _Optional[_Union[InfoNote, str]] = ..., seq_id: _Optional[int] = ..., src: _Optional[str] = ..., event: _Optional[_Union[CallEvent, str]] = ..., payload: _Optional[bytes] = ...) -> None: ... class ServerMsg(_message.Message): __slots__ = ["ctrl", "data", "pres", "meta", "info", "topic"] CTRL_FIELD_NUMBER: _ClassVar[int] DATA_FIELD_NUMBER: _ClassVar[int] PRES_FIELD_NUMBER: _ClassVar[int] META_FIELD_NUMBER: _ClassVar[int] INFO_FIELD_NUMBER: _ClassVar[int] TOPIC_FIELD_NUMBER: _ClassVar[int] ctrl: ServerCtrl data: ServerData pres: ServerPres meta: ServerMeta info: ServerInfo topic: str def __init__(self, ctrl: _Optional[_Union[ServerCtrl, _Mapping]] = ..., data: _Optional[_Union[ServerData, _Mapping]] = ..., pres: _Optional[_Union[ServerPres, _Mapping]] = ..., meta: _Optional[_Union[ServerMeta, _Mapping]] = ..., info: _Optional[_Union[ServerInfo, _Mapping]] = ..., topic: _Optional[str] = ...) -> None: ... class ServerResp(_message.Message): __slots__ = ["status", "srvmsg", "clmsg"] STATUS_FIELD_NUMBER: _ClassVar[int] SRVMSG_FIELD_NUMBER: _ClassVar[int] CLMSG_FIELD_NUMBER: _ClassVar[int] status: RespCode srvmsg: ServerMsg clmsg: ClientMsg def __init__(self, status: _Optional[_Union[RespCode, str]] = ..., srvmsg: _Optional[_Union[ServerMsg, _Mapping]] = ..., clmsg: _Optional[_Union[ClientMsg, _Mapping]] = ...) -> None: ... class Session(_message.Message): __slots__ = ["session_id", "user_id", "auth_level", "remote_addr", "user_agent", "device_id", "language"] SESSION_ID_FIELD_NUMBER: _ClassVar[int] USER_ID_FIELD_NUMBER: _ClassVar[int] AUTH_LEVEL_FIELD_NUMBER: _ClassVar[int] REMOTE_ADDR_FIELD_NUMBER: _ClassVar[int] USER_AGENT_FIELD_NUMBER: _ClassVar[int] DEVICE_ID_FIELD_NUMBER: _ClassVar[int] LANGUAGE_FIELD_NUMBER: _ClassVar[int] session_id: str user_id: str auth_level: AuthLevel remote_addr: str user_agent: str device_id: str language: str def __init__(self, session_id: _Optional[str] = ..., user_id: _Optional[str] = ..., auth_level: _Optional[_Union[AuthLevel, str]] = ..., remote_addr: _Optional[str] = ..., user_agent: _Optional[str] = ..., device_id: _Optional[str] = ..., language: _Optional[str] = ...) -> None: ... class ClientReq(_message.Message): __slots__ = ["msg", "sess"] MSG_FIELD_NUMBER: _ClassVar[int] SESS_FIELD_NUMBER: _ClassVar[int] msg: ClientMsg sess: Session def __init__(self, msg: _Optional[_Union[ClientMsg, _Mapping]] = ..., sess: _Optional[_Union[Session, _Mapping]] = ...) -> None: ... class SearchQuery(_message.Message): __slots__ = ["user_id", "query"] USER_ID_FIELD_NUMBER: _ClassVar[int] QUERY_FIELD_NUMBER: _ClassVar[int] user_id: str query: str def __init__(self, user_id: _Optional[str] = ..., query: _Optional[str] = ...) -> None: ... class SearchFound(_message.Message): __slots__ = ["status", "query", "result"] STATUS_FIELD_NUMBER: _ClassVar[int] QUERY_FIELD_NUMBER: _ClassVar[int] RESULT_FIELD_NUMBER: _ClassVar[int] status: RespCode query: str result: _containers.RepeatedCompositeFieldContainer[TopicSub] def __init__(self, status: _Optional[_Union[RespCode, str]] = ..., query: _Optional[str] = ..., result: _Optional[_Iterable[_Union[TopicSub, _Mapping]]] = ...) -> None: ... class TopicEvent(_message.Message): __slots__ = ["action", "name", "desc"] ACTION_FIELD_NUMBER: _ClassVar[int] NAME_FIELD_NUMBER: _ClassVar[int] DESC_FIELD_NUMBER: _ClassVar[int] action: Crud name: str desc: TopicDesc def __init__(self, action: _Optional[_Union[Crud, str]] = ..., name: _Optional[str] = ..., desc: _Optional[_Union[TopicDesc, _Mapping]] = ...) -> None: ... class AccountEvent(_message.Message): __slots__ = ["action", "user_id", "default_acs", "public", "tags"] ACTION_FIELD_NUMBER: _ClassVar[int] USER_ID_FIELD_NUMBER: _ClassVar[int] DEFAULT_ACS_FIELD_NUMBER: _ClassVar[int] PUBLIC_FIELD_NUMBER: _ClassVar[int] TAGS_FIELD_NUMBER: _ClassVar[int] action: Crud user_id: str default_acs: DefaultAcsMode public: bytes tags: _containers.RepeatedScalarFieldContainer[str] def __init__(self, action: _Optional[_Union[Crud, str]] = ..., user_id: _Optional[str] = ..., default_acs: _Optional[_Union[DefaultAcsMode, _Mapping]] = ..., public: _Optional[bytes] = ..., tags: _Optional[_Iterable[str]] = ...) -> None: ... class SubscriptionEvent(_message.Message): __slots__ = ["action", "topic", "user_id", "del_id", "read_id", "recv_id", "mode", "private"] ACTION_FIELD_NUMBER: _ClassVar[int] TOPIC_FIELD_NUMBER: _ClassVar[int] USER_ID_FIELD_NUMBER: _ClassVar[int] DEL_ID_FIELD_NUMBER: _ClassVar[int] READ_ID_FIELD_NUMBER: _ClassVar[int] RECV_ID_FIELD_NUMBER: _ClassVar[int] MODE_FIELD_NUMBER: _ClassVar[int] PRIVATE_FIELD_NUMBER: _ClassVar[int] action: Crud topic: str user_id: str del_id: int read_id: int recv_id: int mode: AccessMode private: bytes def __init__(self, action: _Optional[_Union[Crud, str]] = ..., topic: _Optional[str] = ..., user_id: _Optional[str] = ..., del_id: _Optional[int] = ..., read_id: _Optional[int] = ..., recv_id: _Optional[int] = ..., mode: _Optional[_Union[AccessMode, _Mapping]] = ..., private: _Optional[bytes] = ...) -> None: ... class MessageEvent(_message.Message): __slots__ = ["action", "msg"] ACTION_FIELD_NUMBER: _ClassVar[int] MSG_FIELD_NUMBER: _ClassVar[int] action: Crud msg: ServerData def __init__(self, action: _Optional[_Union[Crud, str]] = ..., msg: _Optional[_Union[ServerData, _Mapping]] = ...) -> None: ... class Auth(_message.Message): __slots__ = ["scheme", "secret"] SCHEME_FIELD_NUMBER: _ClassVar[int] SECRET_FIELD_NUMBER: _ClassVar[int] scheme: str secret: str def __init__(self, scheme: _Optional[str] = ..., secret: _Optional[str] = ...) -> None: ... class FileMeta(_message.Message): __slots__ = ["name", "mime_type", "etag", "size"] NAME_FIELD_NUMBER: _ClassVar[int] MIME_TYPE_FIELD_NUMBER: _ClassVar[int] ETAG_FIELD_NUMBER: _ClassVar[int] SIZE_FIELD_NUMBER: _ClassVar[int] name: str mime_type: str etag: str size: int def __init__(self, name: _Optional[str] = ..., mime_type: _Optional[str] = ..., etag: _Optional[str] = ..., size: _Optional[int] = ...) -> None: ... class FileUpReq(_message.Message): __slots__ = ["id", "auth", "topic", "meta", "content"] ID_FIELD_NUMBER: _ClassVar[int] AUTH_FIELD_NUMBER: _ClassVar[int] TOPIC_FIELD_NUMBER: _ClassVar[int] META_FIELD_NUMBER: _ClassVar[int] CONTENT_FIELD_NUMBER: _ClassVar[int] id: str auth: Auth topic: str meta: FileMeta content: bytes def __init__(self, id: _Optional[str] = ..., auth: _Optional[_Union[Auth, _Mapping]] = ..., topic: _Optional[str] = ..., meta: _Optional[_Union[FileMeta, _Mapping]] = ..., content: _Optional[bytes] = ...) -> None: ... class FileUpResp(_message.Message): __slots__ = ["id", "code", "text", "meta", "redir_url"] ID_FIELD_NUMBER: _ClassVar[int] CODE_FIELD_NUMBER: _ClassVar[int] TEXT_FIELD_NUMBER: _ClassVar[int] META_FIELD_NUMBER: _ClassVar[int] REDIR_URL_FIELD_NUMBER: _ClassVar[int] id: str code: int text: str meta: FileMeta redir_url: str def __init__(self, id: _Optional[str] = ..., code: _Optional[int] = ..., text: _Optional[str] = ..., meta: _Optional[_Union[FileMeta, _Mapping]] = ..., redir_url: _Optional[str] = ...) -> None: ... class FileDownReq(_message.Message): __slots__ = ["id", "auth", "uri", "if_modified"] ID_FIELD_NUMBER: _ClassVar[int] AUTH_FIELD_NUMBER: _ClassVar[int] URI_FIELD_NUMBER: _ClassVar[int] IF_MODIFIED_FIELD_NUMBER: _ClassVar[int] id: str auth: Auth uri: str if_modified: str def __init__(self, id: _Optional[str] = ..., auth: _Optional[_Union[Auth, _Mapping]] = ..., uri: _Optional[str] = ..., if_modified: _Optional[str] = ...) -> None: ... class FileDownResp(_message.Message): __slots__ = ["id", "code", "text", "meta", "redir_url", "content"] ID_FIELD_NUMBER: _ClassVar[int] CODE_FIELD_NUMBER: _ClassVar[int] TEXT_FIELD_NUMBER: _ClassVar[int] META_FIELD_NUMBER: _ClassVar[int] REDIR_URL_FIELD_NUMBER: _ClassVar[int] CONTENT_FIELD_NUMBER: _ClassVar[int] id: str code: int text: str meta: FileMeta redir_url: str content: bytes def __init__(self, id: _Optional[str] = ..., code: _Optional[int] = ..., text: _Optional[str] = ..., meta: _Optional[_Union[FileMeta, _Mapping]] = ..., redir_url: _Optional[str] = ..., content: _Optional[bytes] = ...) -> None: ... ================================================ FILE: py_grpc/tinode_grpc/model_pb2_grpc.py ================================================ # Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT! """Client and server classes corresponding to protobuf-defined services.""" import grpc from . import model_pb2 as model__pb2 class NodeStub(object): """This is the methods that needs to be implemented by a gRPC client. """ def __init__(self, channel): """Constructor. Args: channel: A grpc.Channel. """ self.MessageLoop = channel.stream_stream( '/pbx.Node/MessageLoop', request_serializer=model__pb2.ClientMsg.SerializeToString, response_deserializer=model__pb2.ServerMsg.FromString, ) self.LargeFileReceive = channel.stream_unary( '/pbx.Node/LargeFileReceive', request_serializer=model__pb2.FileUpReq.SerializeToString, response_deserializer=model__pb2.FileUpResp.FromString, ) self.LargeFileServe = channel.unary_stream( '/pbx.Node/LargeFileServe', request_serializer=model__pb2.FileDownReq.SerializeToString, response_deserializer=model__pb2.FileDownResp.FromString, ) class NodeServicer(object): """This is the methods that needs to be implemented by a gRPC client. """ def MessageLoop(self, request_iterator, context): """Client sends a stream of ClientMsg, server responds with a stream of ServerMsg """ context.set_code(grpc.StatusCode.UNIMPLEMENTED) context.set_details('Method not implemented!') raise NotImplementedError('Method not implemented!') def LargeFileReceive(self, request_iterator, context): """Large file upload: a request with a stream of chunks. """ context.set_code(grpc.StatusCode.UNIMPLEMENTED) context.set_details('Method not implemented!') raise NotImplementedError('Method not implemented!') def LargeFileServe(self, request, context): """Large file file download: a response with a stream of chunks. """ context.set_code(grpc.StatusCode.UNIMPLEMENTED) context.set_details('Method not implemented!') raise NotImplementedError('Method not implemented!') def add_NodeServicer_to_server(servicer, server): rpc_method_handlers = { 'MessageLoop': grpc.stream_stream_rpc_method_handler( servicer.MessageLoop, request_deserializer=model__pb2.ClientMsg.FromString, response_serializer=model__pb2.ServerMsg.SerializeToString, ), 'LargeFileReceive': grpc.stream_unary_rpc_method_handler( servicer.LargeFileReceive, request_deserializer=model__pb2.FileUpReq.FromString, response_serializer=model__pb2.FileUpResp.SerializeToString, ), 'LargeFileServe': grpc.unary_stream_rpc_method_handler( servicer.LargeFileServe, request_deserializer=model__pb2.FileDownReq.FromString, response_serializer=model__pb2.FileDownResp.SerializeToString, ), } generic_handler = grpc.method_handlers_generic_handler( 'pbx.Node', rpc_method_handlers) server.add_generic_rpc_handlers((generic_handler,)) # This class is part of an EXPERIMENTAL API. class Node(object): """This is the methods that needs to be implemented by a gRPC client. """ @staticmethod def MessageLoop(request_iterator, target, options=(), channel_credentials=None, call_credentials=None, insecure=False, compression=None, wait_for_ready=None, timeout=None, metadata=None): return grpc.experimental.stream_stream(request_iterator, target, '/pbx.Node/MessageLoop', model__pb2.ClientMsg.SerializeToString, model__pb2.ServerMsg.FromString, options, channel_credentials, insecure, call_credentials, compression, wait_for_ready, timeout, metadata) @staticmethod def LargeFileReceive(request_iterator, target, options=(), channel_credentials=None, call_credentials=None, insecure=False, compression=None, wait_for_ready=None, timeout=None, metadata=None): return grpc.experimental.stream_unary(request_iterator, target, '/pbx.Node/LargeFileReceive', model__pb2.FileUpReq.SerializeToString, model__pb2.FileUpResp.FromString, options, channel_credentials, insecure, call_credentials, compression, wait_for_ready, timeout, metadata) @staticmethod def LargeFileServe(request, target, options=(), channel_credentials=None, call_credentials=None, insecure=False, compression=None, wait_for_ready=None, timeout=None, metadata=None): return grpc.experimental.unary_stream(request, target, '/pbx.Node/LargeFileServe', model__pb2.FileDownReq.SerializeToString, model__pb2.FileDownResp.FromString, options, channel_credentials, insecure, call_credentials, compression, wait_for_ready, timeout, metadata) class PluginStub(object): """Plugin interface. """ def __init__(self, channel): """Constructor. Args: channel: A grpc.Channel. """ self.FireHose = channel.unary_unary( '/pbx.Plugin/FireHose', request_serializer=model__pb2.ClientReq.SerializeToString, response_deserializer=model__pb2.ServerResp.FromString, ) self.Find = channel.unary_unary( '/pbx.Plugin/Find', request_serializer=model__pb2.SearchQuery.SerializeToString, response_deserializer=model__pb2.SearchFound.FromString, ) self.Account = channel.unary_unary( '/pbx.Plugin/Account', request_serializer=model__pb2.AccountEvent.SerializeToString, response_deserializer=model__pb2.Unused.FromString, ) self.Topic = channel.unary_unary( '/pbx.Plugin/Topic', request_serializer=model__pb2.TopicEvent.SerializeToString, response_deserializer=model__pb2.Unused.FromString, ) self.Subscription = channel.unary_unary( '/pbx.Plugin/Subscription', request_serializer=model__pb2.SubscriptionEvent.SerializeToString, response_deserializer=model__pb2.Unused.FromString, ) self.Message = channel.unary_unary( '/pbx.Plugin/Message', request_serializer=model__pb2.MessageEvent.SerializeToString, response_deserializer=model__pb2.Unused.FromString, ) class PluginServicer(object): """Plugin interface. """ def FireHose(self, request, context): """This plugin method is called by Tinode server for every message received from the clients. The method returns a ServerResp message. ServerResp.status tells Tinode server what to do next. See possible values for ServerResp.status in RespCode below. """ context.set_code(grpc.StatusCode.UNIMPLEMENTED) context.set_details('Method not implemented!') raise NotImplementedError('Method not implemented!') def Find(self, request, context): """An alteranative user and topic discovery mechanism. A search request issued on a 'fnd' topic. This method is called to generate an alternative result set. """ context.set_code(grpc.StatusCode.UNIMPLEMENTED) context.set_details('Method not implemented!') raise NotImplementedError('Method not implemented!') def Account(self, request, context): """The following methods are for the Tinode server to report individual events. Account created, updated or deleted """ context.set_code(grpc.StatusCode.UNIMPLEMENTED) context.set_details('Method not implemented!') raise NotImplementedError('Method not implemented!') def Topic(self, request, context): """Topic created, updated [or deleted -- not supported yet] """ context.set_code(grpc.StatusCode.UNIMPLEMENTED) context.set_details('Method not implemented!') raise NotImplementedError('Method not implemented!') def Subscription(self, request, context): """Subscription created, updated or deleted """ context.set_code(grpc.StatusCode.UNIMPLEMENTED) context.set_details('Method not implemented!') raise NotImplementedError('Method not implemented!') def Message(self, request, context): """Message published or deleted """ context.set_code(grpc.StatusCode.UNIMPLEMENTED) context.set_details('Method not implemented!') raise NotImplementedError('Method not implemented!') def add_PluginServicer_to_server(servicer, server): rpc_method_handlers = { 'FireHose': grpc.unary_unary_rpc_method_handler( servicer.FireHose, request_deserializer=model__pb2.ClientReq.FromString, response_serializer=model__pb2.ServerResp.SerializeToString, ), 'Find': grpc.unary_unary_rpc_method_handler( servicer.Find, request_deserializer=model__pb2.SearchQuery.FromString, response_serializer=model__pb2.SearchFound.SerializeToString, ), 'Account': grpc.unary_unary_rpc_method_handler( servicer.Account, request_deserializer=model__pb2.AccountEvent.FromString, response_serializer=model__pb2.Unused.SerializeToString, ), 'Topic': grpc.unary_unary_rpc_method_handler( servicer.Topic, request_deserializer=model__pb2.TopicEvent.FromString, response_serializer=model__pb2.Unused.SerializeToString, ), 'Subscription': grpc.unary_unary_rpc_method_handler( servicer.Subscription, request_deserializer=model__pb2.SubscriptionEvent.FromString, response_serializer=model__pb2.Unused.SerializeToString, ), 'Message': grpc.unary_unary_rpc_method_handler( servicer.Message, request_deserializer=model__pb2.MessageEvent.FromString, response_serializer=model__pb2.Unused.SerializeToString, ), } generic_handler = grpc.method_handlers_generic_handler( 'pbx.Plugin', rpc_method_handlers) server.add_generic_rpc_handlers((generic_handler,)) # This class is part of an EXPERIMENTAL API. class Plugin(object): """Plugin interface. """ @staticmethod def FireHose(request, target, options=(), channel_credentials=None, call_credentials=None, insecure=False, compression=None, wait_for_ready=None, timeout=None, metadata=None): return grpc.experimental.unary_unary(request, target, '/pbx.Plugin/FireHose', model__pb2.ClientReq.SerializeToString, model__pb2.ServerResp.FromString, options, channel_credentials, insecure, call_credentials, compression, wait_for_ready, timeout, metadata) @staticmethod def Find(request, target, options=(), channel_credentials=None, call_credentials=None, insecure=False, compression=None, wait_for_ready=None, timeout=None, metadata=None): return grpc.experimental.unary_unary(request, target, '/pbx.Plugin/Find', model__pb2.SearchQuery.SerializeToString, model__pb2.SearchFound.FromString, options, channel_credentials, insecure, call_credentials, compression, wait_for_ready, timeout, metadata) @staticmethod def Account(request, target, options=(), channel_credentials=None, call_credentials=None, insecure=False, compression=None, wait_for_ready=None, timeout=None, metadata=None): return grpc.experimental.unary_unary(request, target, '/pbx.Plugin/Account', model__pb2.AccountEvent.SerializeToString, model__pb2.Unused.FromString, options, channel_credentials, insecure, call_credentials, compression, wait_for_ready, timeout, metadata) @staticmethod def Topic(request, target, options=(), channel_credentials=None, call_credentials=None, insecure=False, compression=None, wait_for_ready=None, timeout=None, metadata=None): return grpc.experimental.unary_unary(request, target, '/pbx.Plugin/Topic', model__pb2.TopicEvent.SerializeToString, model__pb2.Unused.FromString, options, channel_credentials, insecure, call_credentials, compression, wait_for_ready, timeout, metadata) @staticmethod def Subscription(request, target, options=(), channel_credentials=None, call_credentials=None, insecure=False, compression=None, wait_for_ready=None, timeout=None, metadata=None): return grpc.experimental.unary_unary(request, target, '/pbx.Plugin/Subscription', model__pb2.SubscriptionEvent.SerializeToString, model__pb2.Unused.FromString, options, channel_credentials, insecure, call_credentials, compression, wait_for_ready, timeout, metadata) @staticmethod def Message(request, target, options=(), channel_credentials=None, call_credentials=None, insecure=False, compression=None, wait_for_ready=None, timeout=None, metadata=None): return grpc.experimental.unary_unary(request, target, '/pbx.Plugin/Message', model__pb2.MessageEvent.SerializeToString, model__pb2.Unused.FromString, options, channel_credentials, insecure, call_credentials, compression, wait_for_ready, timeout, metadata) ================================================ FILE: py_grpc/version.py ================================================ # Convert git tag like "v0.15.5-rc5-3-g2084bd63" to PEP 440 version like "0.15.5rc5.post3" from subprocess import check_output command = 'git describe --tags' def git_version(): line = check_output(command.split()).decode('utf-8').strip() if line.startswith("v"): line = line[1:] if '-rc' in line: line = line.replace('-rc', 'rc') if '-beta' in line: line = line.replace('-beta', 'b') if '-alpha' in line: line = line.replace('-alpha', 'a') if '-' in line: parts = line.split('-') line = parts[0] + '.post' + parts[1] return line if __name__ == '__main__': with open('tinode_grpc/GIT_VERSION','w+') as fh: fh.write(git_version()) ================================================ FILE: rest-auth/README.md ================================================ # Example of a REST authenticator server. This is an example of a server-side [REST authenticator](../server/auth/rest/). It's a basic Python script meant to be run as a web server. It implements the required endpoints. It responds to all requests with dummy data. The service uses [Flask](http://flask.pocoo.org/), so make sure it's installed: ``` pip install flask ``` Run the service as ``` python auth.py ``` ================================================ FILE: rest-auth/auth.py ================================================ #!/usr/bin/python # Sample Tinode REST/JSON-RPC authentication service. # See https://github.com/tinode/chat/rest-auth for details. from flask import Flask, jsonify, make_response, request import base64 import json dummy_data = {} app = Flask(__name__) def parse_secret(ecoded_secret): secret = base64.b64decode(ecoded_secret) return secret.split(':') @app.route('/') def index(): return 'Sample Tinode REST/JSON-RPC authentication service. '+\ 'See https://github.com/tinode/chat/rest-auth/ for details.' @app.route('/add', methods=['POST']) def add(): return jsonify({'err': 'unsupported'}) @app.route('/auth', methods=['POST']) def auth(): if not request.json: return jsonify({'err': 'malformed'}) uname, password = parse_secret(request.json.get('secret')) if uname in dummy_data: if dummy_data[uname]['password'] != password: # Wrong password return jsonify({'err': 'failed'}) if 'uid' in dummy_data[uname]: # We have uname -> uid mapping return jsonify({ 'rec': { 'uid': dummy_data[uname]['uid'], 'authlvl': dummy_data[uname]['authlvl'], 'features': dummy_data[uname]['features'] } }) else: # This is the first login. Tell Tinode to create a new account. return jsonify({ 'rec': { 'authlvl': dummy_data[uname]['authlvl'], 'tags': dummy_data[uname]['tags'], 'features': dummy_data[uname]['features'] }, 'newacc': { 'auth': dummy_data[uname]['auth'], 'anon': dummy_data[uname]['anon'], 'public': dummy_data[uname]['public'], 'private': dummy_data[uname]['private'] } }) return jsonify({'err': 'unsupported'}) else: return jsonify({'err': 'not found'}) @app.route('/checkunique', methods=['POST']) def checkunique(): return jsonify({'err': 'unsupported'}) @app.route('/del', methods=['POST']) def xdel(): return jsonify({'err': 'unsupported'}) @app.route('/gen', methods=['POST']) def gen(): return jsonify({'err': 'unsupported'}) @app.route('/link', methods=['POST']) def link(): if not request.json: return jsonify({'err': 'malformed'}) rec = request.json.get('rec', None) secret = request.json.get('secret', '') if not rec or not rec['uid'] or not secret: return jsonify({'err': 'malformed'}) # Save the link account <-> secret to database. uname, password = parse_secret(secret) if uname not in dummy_data: # Unknown user name return jsonify({'err': 'not found'}) if 'uid' in dummy_data[uname]: # Already linked return jsonify({'err': 'duplicate value'}) # Save updated data to file dummy_data[uname]['uid'] = rec['uid'] with open('dummy_data.json', 'w') as outfile: json.dump(dummy_data, outfile, indent=2, sort_keys=True) # Success return jsonify({}) @app.route('/upd', methods=['POST']) def upd(): return jsonify({'err': 'unsupported'}) @app.route('/rtagns', methods=['POST']) def rtags(): # Return dummy namespace "rest" and "email", let client check logins by regular expression. return jsonify({'strarr': ['rest', 'email'], 'byteval': base64.b64encode('^[a-z0-9_]{3,8}$')}) @app.errorhandler(404) def not_found(error): return make_response(jsonify({'err': 'not found'}), 404) @app.errorhandler(405) def not_found(error): return make_response(jsonify({'err': 'method not allowed'}), 405) if __name__ == '__main__': # Load previously saved dummy data. Dummy data contains # tinode user id <-> user name mapping and data for account creation. with open('dummy_data.json') as infile: dummy_data = json.load(infile) app.run(debug=True) ================================================ FILE: rest-auth/dummy_data.json ================================================ { "alice": { "anon": "N", "auth": "JRWPA", "authlvl": "auth", "features": "V", "password": "alice123", "private": "email:bob@example.com,email:carol@example.com,email:dave@example.com,email:eve@example.com,email:frank@example.com", "public": { "fn": "Alice Johnson", "photo": { "data": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAAgACADASIAAhEBAxEB/8QAGQAAAwEBAQAAAAAAAAAAAAAAAQUGAwQI/8QAKRAAAgEDAgQFBQAAAAAAAAAAAQIDAAQRBTEGIUFREhMUYYEiQnGCo//EABYBAQEBAAAAAAAAAAAAAAAAAAABAv/EAB4RAAICAgMBAQAAAAAAAAAAAAECABEDIRNB4YGR/9oADAMBAAIRAxEAPwD1TQ5YoZ5ikvFNzJBpywWsvl3d3ItvEw3Utuw91UM361CaFzSKXYKO475GjSLhS9kvdKX1bq95bs1vO22XQ4Jx0zgN+CKd53oDYuHQoxU9QNvjoaj9Tj1DU+MFGny28cOlw/U00RkBmk7AMMFUH9asCcVhDDFG80kcaI0z+Nyox4jgLk9zgAfAoy3qbxZeMlgN1X75cl9GF7pXFdxbahJbyDU4/URtDEY18yMBXGCx5lSh3+01Y965praKWeCWREaSFiY2ZQSpIwSD05Ej5rUNge+9FFakzZeQhq3W/nlT/9k=", "type": "jpeg" } }, "tags": [ "email:alice@example.com" ] }, "bob": { "anon": "N", "auth": "JRWPA", "authlvl": "auth", "features": "V", "password": "bob123", "private": "email:alice@example.com,email:carol@example.com,email:dave@example.com,email:eve@example.com,email:frank@example.com", "public": { "fn": "Bob Smith", "photo": { "data": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAAgACADASIAAhEBAxEB/8QAGgABAAEFAAAAAAAAAAAAAAAABQMAAQIECP/EACkQAAEDAgQFBAMAAAAAAAAAAAECAwQAEQUSITETIjJBgUJRUnFhofD/xAAXAQEBAQEAAAAAAAAAAAAAAAAAAgED/8QAHxEAAQUBAAIDAAAAAAAAAAAAEQABAgMSMXGhE2Hw/9oADAMBAAIRAxEAPwDqisb+4FWvv4obFC65i8RlD7rLamXVnIQLkKQBuPyaicsMQudk8RIPPacBvtVeKEw5x5M5yOp9choNhZUoDMg32JAA286U3SE9MUrntiFHbTXqIoTForMrHYKJLaFpDDx5hf1N07/CtSbAizVIMuOy8UdPEQFW+r1lsNsPCm2v5IZB536dFxG2oWLJjYblDRbUp1pJ5UG4ym3Ym6vvxT99NRUEWMxFa4cdpDSPigAD9VOe9zSuGGH5kprww9NxvC//2Q==", "type": "jpeg" } }, "tags": [ "email:bob@example.com" ] }, "carol": { "anon": "N", "auth": "JRWPA", "authlvl": "auth", "features": "V", "password": "carol123", "private": "email:alice@example.com,email:bob@example.com,email:dave@example.com,email:eve@example.com,email:frank@example.com", "public": { "fn": "Carol Xmas", "photo": { "data": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAAgACADASIAAhEBAxEB/8QAGQAAAwEBAQAAAAAAAAAAAAAAAQUGBAMI/8QAJRAAAQQCAQMEAwAAAAAAAAAAAQIDBAUAERITIVEGMUFxFCJh/8QAFQEBAQAAAAAAAAAAAAAAAAAAAQL/xAAdEQEAAgEFAQAAAAAAAAAAAAABABECAxIhMcFB/9oADAMBAAIRAxEAPwD1RrAO31hyPkuzL71JPr4816DXVnBEhUfiHXnlpC+HIg8UhBSTrSiVDuAP2cNPdfNBywWpYYcU1Ncuv6iVWE2WlWilMlSVdP6ISD3/AKT7feNsEBobjB8ZH0zyK71vfwJJ6blk43YRSo9nQGW2VpT5KS0CR4cTlhmC0qoFqwGbKHGlsg8g2+0HEg+dH5ysMjGx6SvfIJNgcSVFOxyA2RnTFlVS1tQlxNVXxIYcIK/x2Q3y17b0O+M8lr51Gf/Z", "type": "jpeg" } }, "tags": [ "email:carol@example.com" ] }, "dave": { "anon": "N", "auth": "JRWPA", "authlvl": "auth", "features": "V", "password": "dave123", "private": "email:alice@example.com,email:bob@example.com,email:carol@example.com,email:eve@example.com,email:frank@example.com", "public": { "fn": "Dave Goliathsson", "photo": { "data": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAAgACADASIAAhEBAxEB/8QAGQAAAwADAAAAAAAAAAAAAAAAAAQFAgMI/8QAIxAAAgIBAwQDAQAAAAAAAAAAAQIAAxEEITEFEhNBYXOxof/EABcBAQEBAQAAAAAAAAAAAAAAAAECAAP/xAAcEQACAwADAQAAAAAAAAAAAAABAgADERIxwUH/2gAMAwEAAhEDEQA/AOqMQ9wkxksu6lfX57URKkICEcktn18CcbH45g3YgSpCT6bbKtSlF9nkDglHIwduQcbShKRwwhCIVbdX1X01frx6LXaPTXv33UVWPjGXQE4kWKzYV+Hwj2Ii5ZdT1GrxN3JSGLMOMnYD9/kozBFWtQqAADgATZGusrpPZmM//9k=", "type": "jpeg" } }, "tags": [ "email:dave@example.com" ] }, "eve": { "anon": "N", "auth": "JRWPA", "authlvl": "auth", "features": "V", "password": "eve123", "private": "email:alice@example.com,email:bob@example.com,email:carol@example.com,email:dave@example.com,email:frank@example.com", "public": { "fn": "Eve Adams", "photo": { "data": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAAgACADASIAAhEBAxEB/8QAGQAAAwEBAQAAAAAAAAAAAAAAAQUGAgQI/8QAIxAAAQQCAAcBAQAAAAAAAAAAAgEDBAUAEQYSEyExYXFBkf/EABUBAQEAAAAAAAAAAAAAAAAAAAIB/8QAGxEAAwEBAQEBAAAAAAAAAAAAAQIRABIhA3H/2gAMAwEAAhEDEQA/APVKZlfiZrymSt6EiVxJAhtzpMZgor7pIwSIpEJtIm9ov4RYkXozJE6Ms1R81h/MV1dYcIjIp8yTzJrT5oqD80iY0yEAHzRgAfDcFyUv4EWfxhVtzY7MhsYMgkF0EJEXqM9++VZeMXWVRXWhtLZQIkvp7QFfZFzl351tO3hP5iRuTcvk/DXGtqYFdzrXw2I5Hrm6QIO9eN6zvL1rF1dS1taZnXwIcUyTREwyLaqnvSYyX3kJptuLmm2/u//Z", "type": "jpeg" } }, "tags": [ "email:eve@example.com" ] }, "frank": { "anon": "N", "auth": "JRWPA", "authlvl": "auth", "features": "V", "password": "frank123", "private": "email:alice@example.com,email:bob@example.com,email:carol@example.com,email:dave@example.com,email:eve@example.com", "public": { "fn": "Frank Singer", "photo": { "data": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAAgACADASIAAhEBAxEB/8QAGQAAAgMBAAAAAAAAAAAAAAAAAAUCAwQI/8QAJxAAAQQBAgUEAwAAAAAAAAAAAQACAwQREjEFEyFBgQYUU2FxcpH/xAAXAQADAQAAAAAAAAAAAAAAAAAAAQID/8QAHBEBAAMBAQADAAAAAAAAAAAAAQACEQMSUaHh/9oADAMBAAIRAxEAPwDqfHQoIwcdkZ3SXibZZeMVK0diaKJ0ErzyyBkh0YHb7KYazPpfxXc39joeFJYaVR9bUXWZ5tXyuBx+MBbUMdVTUyBCQ8TrR2vUNBkzGvb7aY4P7RJ605VLomGZspa3WGlodjqAdxnwP4nVx2T15nSvl+T6ZCpSgqauRG1mrfHdat0I8qV2XWpUwJ//2Q==", "type": "jpeg" } }, "tags": [ "email:frank@example.com" ] }, "xena": { "anon": "N", "auth": "JRWPA", "authlvl": "root", "features": "V", "password": "xena123", "private": "", "public": { "fn": "Xena Peaceful Peasant", "photo": { "data": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAAgACADASIAAhEBAxEB/8QAGAABAQEBAQAAAAAAAAAAAAAABgAEBQj/xAArEAABAwMCBAQHAAAAAAAAAAABAgMEAAUREjEGISJhNEFRcRQjJTJCQ4L/xAAUAQEAAAAAAAAAAAAAAAAAAAAA/8QAFBEBAAAAAAAAAAAAAAAAAAAAAP/aAAwDAQACEQMRAD8A9U1VUeukuRar/EffcUu1TMRVg7R3s/LX7LzoPfR6mgQ1Ue4fmSbvOl3FLhTavDxG88ncHre9ieSeyc/lyQ0FRTi36y43ww2fGtlyaobtRQcHHopZ6R/RH20rrM1GYakOvtstIfeADiwkBS8bZPnjJoOBwbIcYadsM7R8baghoEJwHmP1OgbcwMEDZST5YpRWZUZgy0yiy2ZSUFsO6RqCSQSnO+MgHHatNB//2Q==", "type": "jpeg" } }, "tags": [ "email:xena@example.com" ] } } ================================================ FILE: rest-auth/requirements.txt ================================================ flask>=1.1.0 ================================================ FILE: server/.golangci.yml ================================================ linters-settings: govet: check-shadowing: true disable: - composites golint: min-confidence: 0 gocyclo: min-complexity: 10 maligned: suggest-new: true dupl: threshold: 100 goconst: min-len: 2 min-occurrences: 2 misspell: locale: US lll: line-length: 140 gocritic: enabled-tags: - performance - style - experimental disabled-checks: - wrapperFunc - commentFormatting # https://github.com/go-critic/go-critic/issues/755 linters: enable-all: true disable: - errcheck - maligned - prealloc - gosec - gochecknoglobals # options for analysis running run: # list of build tags, all linters use it. Default is empty list. build-tags: - mysql - rethinkdb - mongodb ================================================ FILE: server/api_key.go ================================================ /****************************************************************************** * * Description : * * Authentication * *****************************************************************************/ package main import ( "bytes" "crypto/hmac" "crypto/md5" "encoding/base64" "github.com/tinode/chat/server/logs" ) // Singned AppID. Composition: // // [1:algorithm version][4:appid][2:key sequence][1:isRoot][16:signature] = 24 bytes // // convertible to base64 without padding. All integers are little-endian. // Definitions for byte lengths of key's parts. const ( // apikeyVersion is the version of this API scheme. apikeyVersion = 1 // apikeyAppID is deprecated and will be removed in the future. apikeyAppID = 4 // apikeySequence is the serial number of the key. apikeySequence = 2 // apikeyWho indicates if the key grants root privileges. apikeyWho = 1 // apikeySignature is key's cryptographic (HMAC) signature. apikeySignature = 16 // apikeyLength is the length of the key in bytes. apikeyLength = apikeyVersion + apikeyAppID + apikeySequence + apikeyWho + apikeySignature ) // Client signature validation // // key: client's secret key // // Returns application id, key type. func checkAPIKey(apikey string) (isValid, isRoot bool) { if declen := base64.URLEncoding.DecodedLen(len(apikey)); declen != apikeyLength { return } data, err := base64.URLEncoding.DecodeString(apikey) if err != nil { logs.Warn.Println("failed to decode.base64 appid ", err) return } if data[0] != 1 { logs.Warn.Println("unknown appid signature algorithm ", data[0]) return } hasher := hmac.New(md5.New, globals.apiKeySalt) hasher.Write(data[:apikeyVersion+apikeyAppID+apikeySequence+apikeyWho]) check := hasher.Sum(nil) if !bytes.Equal(data[apikeyVersion+apikeyAppID+apikeySequence+apikeyWho:], check) { logs.Warn.Println("invalid apikey signature") return } isRoot = (data[apikeyVersion+apikeyAppID+apikeySequence] == 1) isValid = true return } ================================================ FILE: server/auth/anon/auth_anon.go ================================================ // Package anon provides authentication without credentials. Most useful for customer support. // Anonymous authentication is used only at the account creation time. package anon import ( "encoding/json" "errors" "time" "github.com/tinode/chat/server/auth" "github.com/tinode/chat/server/store" "github.com/tinode/chat/server/store/types" ) // authenticator is the singleton instance of the anonymous authorizer. type authenticator struct { name string } // Init is a noop, always returns success. func (a *authenticator) Init(_ json.RawMessage, name string) error { if name == "" { return errors.New("auth_anonymous: authenticator name cannot be blank") } if a.name != "" { return errors.New("auth_anonymous: already initialized as " + a.name + "; " + name) } a.name = name return nil } // IsInitialized returns true if the handler is initialized. func (a *authenticator) IsInitialized() bool { return a.name != "" } // AddRecord checks authLevel and assigns default LevelAnon. Otherwise it // just reports success. func (authenticator) AddRecord(rec *auth.Rec, secret []byte, remoteAddr string) (*auth.Rec, error) { if rec.AuthLevel == auth.LevelNone { rec.AuthLevel = auth.LevelAnon } rec.State = types.StateOK return rec, nil } // UpdateRecord is a noop. Just report success. func (authenticator) UpdateRecord(rec *auth.Rec, secret []byte, remoteAddr string) (*auth.Rec, error) { return rec, nil } // Authenticate is not supported. This authenticator is used only at account creation time. func (authenticator) Authenticate(secret []byte, remoteAddr string) (*auth.Rec, []byte, error) { return nil, nil, types.ErrUnsupported } // AsTag is not supported, will produce an empty string. func (authenticator) AsTag(token string) string { return "" } // IsUnique for a noop. Anonymous login does not use secret, any secret is fine. func (authenticator) IsUnique(secret []byte, remoteAddr string) (bool, error) { return true, nil } // GenSecret always fails. func (authenticator) GenSecret(rec *auth.Rec) ([]byte, time.Time, error) { return nil, time.Time{}, types.ErrUnsupported } // DelRecords is a noop which always succeeds. func (authenticator) DelRecords(uid types.Uid) error { return nil } // RestrictedTags returns tag namespaces restricted by this authenticator (none for anonymous). func (authenticator) RestrictedTags() ([]string, error) { return nil, nil } // GetResetParams returns authenticator parameters passed to password reset handler // (none for anonymous). func (authenticator) GetResetParams(uid types.Uid) (map[string]any, error) { return nil, nil } const realName = "anonymous" // GetRealName returns the hardcoded name of the authenticator. func (authenticator) GetRealName() string { return realName } func init() { store.RegisterAuthScheme(realName, &authenticator{}) } ================================================ FILE: server/auth/auth.go ================================================ // Package auth provides interfaces and types required for implementing an authenticaor. package auth import ( "encoding/json" "errors" "strconv" "time" "github.com/tinode/chat/server/store/types" ) // Level is the type for authentication levels. type Level int // Authentication levels const ( // LevelNone is undefined/not authenticated LevelNone Level = iota * 10 // LevelAnon is anonymous user/light authentication LevelAnon // LevelAuth is fully authenticated user LevelAuth // LevelRoot is a superuser (currently unused) LevelRoot ) // String implements Stringer interface: gets human-readable name for a numeric authentication level. func (a Level) String() string { s, err := a.MarshalText() if err != nil { return "unkn" } return string(s) } // ParseAuthLevel parses authentication level from a string. func ParseAuthLevel(name string) Level { switch name { case "anon", "ANON": return LevelAnon case "auth", "AUTH": return LevelAuth case "root", "ROOT": return LevelRoot default: return LevelNone } } // MarshalText converts Level to a slice of bytes with the name of the level. func (a Level) MarshalText() ([]byte, error) { switch a { case LevelNone: return []byte(""), nil case LevelAnon: return []byte("anon"), nil case LevelAuth: return []byte("auth"), nil case LevelRoot: return []byte("root"), nil default: return nil, errors.New("auth.Level: invalid level value") } } // UnmarshalText parses authentication level from a string. func (a *Level) UnmarshalText(b []byte) error { switch string(b) { case "": *a = LevelNone return nil case "anon", "ANON": *a = LevelAnon return nil case "auth", "AUTH": *a = LevelAuth return nil case "root", "ROOT": *a = LevelRoot return nil default: return errors.New("auth.Level: unrecognized") } } // MarshalJSON converts Level to a quoted string. func (a Level) MarshalJSON() ([]byte, error) { res, err := a.MarshalText() if err != nil { return nil, err } return append(append([]byte{'"'}, res...), '"'), nil } // UnmarshalJSON reads Level from a quoted string. func (a *Level) UnmarshalJSON(b []byte) error { if b[0] != '"' || b[len(b)-1] != '"' { return errors.New("syntax error") } return a.UnmarshalText(b[1 : len(b)-1]) } // Feature is a bitmap of authenticated features, such as validated/not validated. type Feature uint16 const ( // FeatureValidated bit is set if user's credentials are already validated (V). FeatureValidated Feature = 1 << iota // FeatureNoLogin is set if the token should not be used to permanently authenticate a session (L). FeatureNoLogin ) // MarshalText converts Feature to ASCII byte slice. func (f Feature) MarshalText() ([]byte, error) { res := []byte{} for i, chr := range []byte{'V', 'L'} { if (f & (1 << uint(i))) != 0 { res = append(res, chr) } } return res, nil } // UnmarshalText parses Feature string as byte slice. func (f *Feature) UnmarshalText(b []byte) error { var f0 int var err error if len(b) > 0 { if b[0] >= '0' && b[0] <= '9' { f0, err = strconv.Atoi(string(b)) } else { Loop: for i := range b { switch b[i] { case 'V', 'v': f0 |= int(FeatureValidated) case 'L', 'l': f0 |= int(FeatureNoLogin) default: err = errors.New("Feature: invalid character '" + string(b[i]) + "'") break Loop } } } } *f = Feature(f0) return err } // String Featureto a string representation. func (f Feature) String() string { res, err := f.MarshalText() if err != nil { return "" } return string(res) } // MarshalJSON converts Feature to a quoted string. func (f Feature) MarshalJSON() ([]byte, error) { res, err := f.MarshalText() if err != nil { return nil, err } return append(append([]byte{'"'}, res...), '"'), nil } // UnmarshalJSON reads Feature from a quoted string or an integer. func (f *Feature) UnmarshalJSON(b []byte) error { if b[0] == '"' && b[len(b)-1] == '"' { return f.UnmarshalText(b[1 : len(b)-1]) } return f.UnmarshalText(b) } // Duration is identical to time.Duration except it can be sanely unmarshallend from JSON. type Duration time.Duration // UnmarshalJSON handles the cases where duration is specified in JSON as a "5000s" string or just plain seconds. func (d *Duration) UnmarshalJSON(b []byte) error { var v any if err := json.Unmarshal(b, &v); err != nil { return err } switch value := v.(type) { case float64: *d = Duration(time.Duration(value) * time.Second) return nil case string: d0, err := time.ParseDuration(value) if err != nil { return err } *d = Duration(d0) return nil default: return errors.New("invalid duration") } } // Rec is an authentication record. type Rec struct { // User ID. Uid types.Uid `json:"uid,omitempty"` // Authentication level. AuthLevel Level `json:"authlvl,omitempty"` // Lifetime of this record. Lifetime Duration `json:"lifetime,omitempty"` // Bitmap of features. Currently 'validated'/'not validated' only. Features Feature `json:"features,omitempty"` // Tags generated by this authentication record. Tags []string `json:"tags,omitempty"` // User account state received or read by the authenticator. State types.ObjState // Credential 'method:value' associated with this record. Credential string `json:"cred,omitempty"` // Authenticator may request the server to create a new account. // These are the account parameters which can be used for creating the account. DefAcs *types.DefaultAccess `json:"defacs,omitempty"` Public any `json:"public,omitempty"` Private any `json:"private,omitempty"` } // AuthHandler is the interface which auth providers must implement. type AuthHandler interface { // Init initializes the handler taking config string and logical name as parameters. Init(jsonconf json.RawMessage, name string) error // IsInitialized returns true if the handler is initialized. IsInitialized() bool // AddRecord adds persistent authentication record to the database. // Returns: updated auth record, error AddRecord(rec *Rec, secret []byte, remoteAddr string) (*Rec, error) // UpdateRecord updates existing record with new credentials. // Returns updated auth record, error. UpdateRecord(rec *Rec, secret []byte, remoteAddr string) (*Rec, error) // Authenticate: given a user-provided authentication secret (such as "login:password"), either // return user's record (ID, time when the secret expires, etc), or issue a challenge to // continue the authentication process to the next step, or return an error code. // The remoteAddr (i.e. the IP address of the client) can be used by custom authenticators for // additional validation. The stock authenticators don't use it. // store.Users.GetAuthRecord("scheme", "unique") // Returns: user auth record, challenge, error. Authenticate(secret []byte, remoteAddr string) (*Rec, []byte, error) // AsTag converts search token into prefixed tag or an empty string if it // cannot be represented as a prefixed tag. AsTag(token string) string // IsUnique verifies if the provided secret can be considered unique by the auth scheme // E.g. if login is unique. It also may check for policy compliance, i.e. not too short, etc. IsUnique(secret []byte, remoteAddr string) (bool, error) // GenSecret generates a new secret, if appropriate. GenSecret(rec *Rec) ([]byte, time.Time, error) // DelRecords deletes (or disables) all authentication records for the given user. DelRecords(uid types.Uid) error // RestrictedTags returns the tag namespaces (prefixes) which are restricted by this authenticator. RestrictedTags() ([]string, error) // GetResetParams returns authenticator parameters passed to password reset handler // for the provided user id. // Returns: map of params. GetResetParams(uid types.Uid) (map[string]any, error) // GetRealName returns the hardcoded name of the authenticator. GetRealName() string } ================================================ FILE: server/auth/basic/auth_basic.go ================================================ // Package basic is an authenticator by login-password. package basic import ( "encoding/json" "errors" "regexp" "strings" "time" "github.com/tinode/chat/server/auth" "github.com/tinode/chat/server/store" "github.com/tinode/chat/server/store/types" "golang.org/x/crypto/bcrypt" ) // Define default constraints on login and password const ( defaultMinLoginLength = 2 defaultMaxLoginLength = 32 defaultMinPasswordLength = 3 ) // Token suitable as a login: starts and ends with a Unicode letter (class L) or number (class N), // contains Unicode letters, numbers, dot and underscore. var loginPattern = regexp.MustCompile(`^[\pL\pN][_.\pL\pN]*[\pL\pN]+$`) // authenticator is the type to map authentication methods to. type authenticator struct { name string addToTags bool minPasswordLength int minLoginLength int } func (a *authenticator) checkLoginPolicy(uname string) error { rlogin := []rune(uname) if len(rlogin) < a.minLoginLength || len(rlogin) > defaultMaxLoginLength || !loginPattern.MatchString(uname) { return types.ErrPolicy } return nil } func (a *authenticator) checkPasswordPolicy(password string) error { if len([]rune(password)) < a.minPasswordLength { return types.ErrPolicy } return nil } func parseSecret(bsecret []byte) (uname, password string, err error) { secret := string(bsecret) splitAt := strings.Index(secret, ":") if splitAt < 0 { err = types.ErrMalformed return } uname = strings.ToLower(secret[:splitAt]) password = secret[splitAt+1:] return } // Init initializes the basic authenticator. func (a *authenticator) Init(jsonconf json.RawMessage, name string) error { if name == "" { return errors.New("auth_basic: authenticator name cannot be blank") } if a.name != "" { return errors.New("auth_basic: already initialized as " + a.name + "; " + name) } type configType struct { // AddToTags indicates that the user name should be used as a searchable tag. AddToTags bool `json:"add_to_tags"` MinPasswordLength int `json:"min_password_length"` MinLoginLength int `json:"min_login_length"` } var config configType if err := json.Unmarshal(jsonconf, &config); err != nil { return errors.New("auth_basic: failed to parse config: " + err.Error() + "(" + string(jsonconf) + ")") } a.name = name a.addToTags = config.AddToTags a.minPasswordLength = config.MinPasswordLength if a.minPasswordLength <= 0 { a.minPasswordLength = defaultMinPasswordLength } a.minLoginLength = config.MinLoginLength if a.minLoginLength > defaultMaxLoginLength { return errors.New("auth_basic: min_login_length exceeds the limit") } if a.minLoginLength <= 0 { a.minLoginLength = defaultMinLoginLength } return nil } // IsInitialized returns true if the handler is initialized. func (a *authenticator) IsInitialized() bool { return a.name != "" } // AddRecord adds a basic authentication record to DB. func (a *authenticator) AddRecord(rec *auth.Rec, secret []byte, remoteAddr string) (*auth.Rec, error) { uname, password, err := parseSecret(secret) if err != nil { return nil, err } if err = a.checkLoginPolicy(uname); err != nil { return nil, err } if err = a.checkPasswordPolicy(password); err != nil { return nil, err } passhash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) if err != nil { return nil, err } var expires time.Time if rec.Lifetime > 0 { expires = time.Now().Add(time.Duration(rec.Lifetime)).UTC().Round(time.Millisecond) } authLevel := rec.AuthLevel if authLevel == auth.LevelNone { authLevel = auth.LevelAuth } err = store.Users.AddAuthRecord(rec.Uid, authLevel, a.name, uname, passhash, expires) if err != nil { return nil, err } rec.AuthLevel = authLevel if a.addToTags { rec.Tags = append(rec.Tags, a.name+":"+uname) } return rec, nil } // UpdateRecord updates password for basic authentication. func (a *authenticator) UpdateRecord(rec *auth.Rec, secret []byte, remoteAddr string) (*auth.Rec, error) { uname, password, err := parseSecret(secret) if err != nil { return nil, err } login, authLevel, _, _, err := store.Users.GetAuthRecord(rec.Uid, a.name) if err != nil { return nil, err } // User does not have a record. if login == "" { return nil, types.ErrNotFound } if uname == "" || uname == login { // User is changing just the password. uname = login } else if err = a.checkLoginPolicy(uname); err != nil { return nil, err } else if uid, _, _, _, err := store.Users.GetAuthUniqueRecord(a.name, uname); err != nil { return nil, err } else if !uid.IsZero() { // The (new) user name already exists. Report an error. return nil, types.ErrDuplicate } if err = a.checkPasswordPolicy(password); err != nil { return nil, err } passhash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) if err != nil { return nil, types.ErrInternal } var expires time.Time if rec.Lifetime > 0 { expires = types.TimeNow().Add(time.Duration(rec.Lifetime)) } err = store.Users.UpdateAuthRecord(rec.Uid, authLevel, a.name, uname, passhash, expires) if err != nil { return nil, err } // Remove old tag from the list of tags oldTag := a.name + ":" + login for i, tag := range rec.Tags { if tag == oldTag { rec.Tags[i] = rec.Tags[len(rec.Tags)-1] rec.Tags = rec.Tags[:len(rec.Tags)-1] break } } // Add new tag rec.Tags = append(rec.Tags, a.name+":"+uname) return rec, nil } // Authenticate checks login and password. func (a *authenticator) Authenticate(secret []byte, remoteAddr string) (*auth.Rec, []byte, error) { uname, password, err := parseSecret(secret) if err != nil { return nil, nil, err } uid, authLvl, passhash, expires, err := store.Users.GetAuthUniqueRecord(a.name, uname) if err != nil { return nil, nil, err } if uid.IsZero() { // Invalid login. return nil, nil, types.ErrFailed } if !expires.IsZero() && expires.Before(time.Now()) { // The record has expired return nil, nil, types.ErrExpired } err = bcrypt.CompareHashAndPassword(passhash, []byte(password)) if err != nil { // Invalid password return nil, nil, types.ErrFailed } var lifetime time.Duration if !expires.IsZero() { lifetime = time.Until(expires) } return &auth.Rec{ Uid: uid, AuthLevel: authLvl, Lifetime: auth.Duration(lifetime), Features: 0, State: types.StateUndefined}, nil, nil } // AsTag convert search token into a prefixed tag, if possible. func (a *authenticator) AsTag(token string) string { if !a.addToTags { return "" } if err := a.checkLoginPolicy(token); err != nil { return "" } return a.name + ":" + token } // IsUnique checks login uniqueness and policy compliance. func (a *authenticator) IsUnique(secret []byte, remoteAddr string) (bool, error) { uname, _, err := parseSecret(secret) if err != nil { return false, err } if err := a.checkLoginPolicy(uname); err != nil { return false, err } uid, _, _, _, err := store.Users.GetAuthUniqueRecord(a.name, uname) if err != nil { return false, err } if uid.IsZero() { return true, nil } return false, types.ErrDuplicate } // GenSecret is not supported, generates an error. func (authenticator) GenSecret(rec *auth.Rec) ([]byte, time.Time, error) { return nil, time.Time{}, types.ErrUnsupported } // DelRecords deletes saved authentication records of the given user. func (a *authenticator) DelRecords(uid types.Uid) error { return store.Users.DelAuthRecords(uid, a.name) } // RestrictedTags returns tag namespaces (prefixes) restricted by this adapter. func (a *authenticator) RestrictedTags() ([]string, error) { var prefix []string if a.addToTags { prefix = []string{a.name} } return prefix, nil } // GetResetParams returns authenticator parameters passed to password reset handler. func (a *authenticator) GetResetParams(uid types.Uid) (map[string]any, error) { login, _, _, _, err := store.Users.GetAuthRecord(uid, a.name) if err != nil { return nil, err } // User does not have a record matching the authentication scheme. if login == "" { return nil, types.ErrNotFound } params := make(map[string]any) params["login"] = login return params, nil } const realName = "basic" // GetRealName returns the hardcoded name of the authenticator. func (authenticator) GetRealName() string { return realName } func init() { store.RegisterAuthScheme(realName, &authenticator{}) } ================================================ FILE: server/auth/code/auth_code.go ================================================ // Package code implements temporary no-login authentication by short numeric code. package code import ( "crypto/rand" "encoding/json" "errors" "math/big" "strconv" "strings" "time" "github.com/tinode/chat/server/auth" "github.com/tinode/chat/server/logs" "github.com/tinode/chat/server/store" "github.com/tinode/chat/server/store/types" ) // authenticator is a singleton instance of the authenticator. type authenticator struct { name string codeLength int maxCodeValue *big.Int lifetime time.Duration maxRetries int } // Init initializes the authenticator: parses the config and sets internal state. func (ca *authenticator) Init(jsonconf json.RawMessage, name string) error { if name == "" { return errors.New("auth_code: authenticator name cannot be blank") } if ca.name != "" { return errors.New("auth_code: already initialized as " + ca.name + "; " + name) } type configType struct { // Length of the security code. CodeLength int `json:"code_length"` // Code expiration time in seconds. ExpireIn int `json:"expire_in"` // Maximum number of verification attempts per code. MaxRetries int `json:"max_retries"` } var config configType if err := json.Unmarshal(jsonconf, &config); err != nil { return errors.New("auth_code: failed to parse config: " + err.Error() + "(" + string(jsonconf) + ")") } if config.ExpireIn <= 0 { return errors.New("auth_code: invalid expiration period") } if config.CodeLength < 4 { return errors.New("auth_code: invalid code length") } if config.MaxRetries < 1 { return errors.New("auth_code: invalid retries count") } ca.name = name ca.codeLength = config.CodeLength ca.maxCodeValue = big.NewInt(0).Exp(big.NewInt(10), big.NewInt(int64(ca.codeLength)), nil) ca.lifetime = time.Duration(config.ExpireIn) * time.Second ca.maxRetries = config.MaxRetries return nil } // IsInitialized returns true if the handler is initialized. func (ca *authenticator) IsInitialized() bool { return ca.name != "" } // AddRecord is not supported, will produce an error. func (authenticator) AddRecord(rec *auth.Rec, secret []byte, remoteAddr string) (*auth.Rec, error) { return nil, types.ErrUnsupported } // UpdateRecord is not supported, will produce an error. func (authenticator) UpdateRecord(rec *auth.Rec, secret []byte, remoteAddr string) (*auth.Rec, error) { return nil, types.ErrUnsupported } // Authenticate checks validity of provided short code. // The secret is structured as ::, "123456:email:alice@example.com". func (ca *authenticator) Authenticate(secret []byte, remoteAddr string) (*auth.Rec, []byte, error) { parts := strings.SplitN(string(secret), ":", 2) if len(parts) != 2 { return nil, nil, types.ErrMalformed } code, cred := parts[0], parts[1] key := sanitizeKey(realName + "_" + cred) value, err := store.PCache.Get(key) if err != nil { if err == types.ErrNotFound { err = types.ErrFailed } return nil, nil, err } // code:count:uid parts = strings.Split(value, ":") if len(parts) != 3 { return nil, nil, types.ErrInternal } count, err := strconv.Atoi(parts[1]) if err != nil { return nil, nil, types.ErrInternal } if count >= ca.maxRetries { return nil, nil, types.ErrFailed } if parts[0] != code { // Update count of attempts. If the update fails, the error is ignored. store.PCache.Upsert(key, parts[0]+":"+strconv.Itoa(count+1)+":"+parts[2], false) return nil, nil, types.ErrFailed } // Success. Remove no longer needed entry. The error is ignored here. if err = store.PCache.Delete(key); err != nil { logs.Warn.Println("code_auth: error deleting key", key, err) } return &auth.Rec{ Uid: types.ParseUid(parts[2]), AuthLevel: auth.LevelNone, Lifetime: auth.Duration(ca.lifetime), Features: auth.FeatureNoLogin, State: types.StateUndefined, Credential: cred}, nil, nil } // GenSecret generates a new code. func (ca *authenticator) GenSecret(rec *auth.Rec) ([]byte, time.Time, error) { // Run garbage collection. store.PCache.Expire(realName+"_", time.Now().UTC().Add(-ca.lifetime)) // Generate random code. code, err := rand.Int(rand.Reader, ca.maxCodeValue) if err != nil { return nil, time.Time{}, types.ErrInternal } // Convert the code to fixed length string. resp := strconv.FormatInt(code.Int64(), 10) resp = strings.Repeat("0", ca.codeLength-len(resp)) + resp if rec.Lifetime == 0 { rec.Lifetime = auth.Duration(ca.lifetime) } else if rec.Lifetime < 0 { return nil, time.Time{}, types.ErrExpired } // Save "code:counter:uid" to the database. The key is code_. if err = store.PCache.Upsert(sanitizeKey(realName+"_"+rec.Credential), resp+":0:"+rec.Uid.String(), true); err != nil { return nil, time.Time{}, err } expires := time.Now().Add(time.Duration(rec.Lifetime)).UTC().Round(time.Millisecond) return []byte(resp), expires, nil } // AsTag is not supported, will produce an empty string. func (authenticator) AsTag(token string) string { return "" } // IsUnique is not supported, will produce an error. func (authenticator) IsUnique(secret []byte, remoteAddr string) (bool, error) { return false, types.ErrUnsupported } // DelRecords adds disabled user ID to a stop list. func (authenticator) DelRecords(uid types.Uid) error { return nil } // RestrictedTags returns tag namespaces restricted by this authenticator (none for short code). func (authenticator) RestrictedTags() ([]string, error) { return nil, nil } // GetResetParams returns authenticator parameters passed to password reset handler // (none for short code). func (authenticator) GetResetParams(uid types.Uid) (map[string]any, error) { return nil, nil } // Replace all occurrences of '%' with '/' to ensure SQL LIKE query works correctly. func sanitizeKey(key string) string { return strings.ReplaceAll(key, "%", "/") } const realName = "code" // GetRealName returns the hardcoded name of the authenticator. func (authenticator) GetRealName() string { return realName } func init() { store.RegisterAuthScheme(realName, &authenticator{}) } ================================================ FILE: server/auth/mock_auth/mock_auth.go ================================================ // Code generated by MockGen. DO NOT EDIT. // Source: auth/auth.go // Package mock_auth is a generated GoMock package. package mock_auth import ( json "encoding/json" reflect "reflect" time "time" gomock "github.com/golang/mock/gomock" auth "github.com/tinode/chat/server/auth" types "github.com/tinode/chat/server/store/types" ) // MockAuthHandler is a mock of AuthHandler interface. type MockAuthHandler struct { ctrl *gomock.Controller recorder *MockAuthHandlerMockRecorder } // MockAuthHandlerMockRecorder is the mock recorder for MockAuthHandler. type MockAuthHandlerMockRecorder struct { mock *MockAuthHandler } // NewMockAuthHandler creates a new mock instance. func NewMockAuthHandler(ctrl *gomock.Controller) *MockAuthHandler { mock := &MockAuthHandler{ctrl: ctrl} mock.recorder = &MockAuthHandlerMockRecorder{mock} return mock } // EXPECT returns an object that allows the caller to indicate expected use. func (m *MockAuthHandler) EXPECT() *MockAuthHandlerMockRecorder { return m.recorder } // AddRecord mocks base method. func (m *MockAuthHandler) AddRecord(rec *auth.Rec, secret []byte, remoteAddr string) (*auth.Rec, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "AddRecord", rec, secret, remoteAddr) ret0, _ := ret[0].(*auth.Rec) ret1, _ := ret[1].(error) return ret0, ret1 } // AddRecord indicates an expected call of AddRecord. func (mr *MockAuthHandlerMockRecorder) AddRecord(rec, secret, remoteAddr interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AddRecord", reflect.TypeOf((*MockAuthHandler)(nil).AddRecord), rec, secret, remoteAddr) } // AsTag mocks base method. func (m *MockAuthHandler) AsTag(token string) string { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "AsTag", token) ret0, _ := ret[0].(string) return ret0 } // AsTag indicates an expected call of AsTag. func (mr *MockAuthHandlerMockRecorder) AsTag(token interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AsTag", reflect.TypeOf((*MockAuthHandler)(nil).AsTag), token) } // Authenticate mocks base method. func (m *MockAuthHandler) Authenticate(secret []byte, remoteAddr string) (*auth.Rec, []byte, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Authenticate", secret, remoteAddr) ret0, _ := ret[0].(*auth.Rec) ret1, _ := ret[1].([]byte) ret2, _ := ret[2].(error) return ret0, ret1, ret2 } // Authenticate indicates an expected call of Authenticate. func (mr *MockAuthHandlerMockRecorder) Authenticate(secret, remoteAddr interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Authenticate", reflect.TypeOf((*MockAuthHandler)(nil).Authenticate), secret, remoteAddr) } // DelRecords mocks base method. func (m *MockAuthHandler) DelRecords(uid types.Uid) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "DelRecords", uid) ret0, _ := ret[0].(error) return ret0 } // DelRecords indicates an expected call of DelRecords. func (mr *MockAuthHandlerMockRecorder) DelRecords(uid interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DelRecords", reflect.TypeOf((*MockAuthHandler)(nil).DelRecords), uid) } // GenSecret mocks base method. func (m *MockAuthHandler) GenSecret(rec *auth.Rec) ([]byte, time.Time, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GenSecret", rec) ret0, _ := ret[0].([]byte) ret1, _ := ret[1].(time.Time) ret2, _ := ret[2].(error) return ret0, ret1, ret2 } // GenSecret indicates an expected call of GenSecret. func (mr *MockAuthHandlerMockRecorder) GenSecret(rec interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GenSecret", reflect.TypeOf((*MockAuthHandler)(nil).GenSecret), rec) } // GetRealName mocks base method. func (m *MockAuthHandler) GetRealName() string { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetRealName") ret0, _ := ret[0].(string) return ret0 } // GetRealName indicates an expected call of GetRealName. func (mr *MockAuthHandlerMockRecorder) GetRealName() *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetRealName", reflect.TypeOf((*MockAuthHandler)(nil).GetRealName)) } // GetResetParams mocks base method. func (m *MockAuthHandler) GetResetParams(uid types.Uid) (map[string]any, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetResetParams", uid) ret0, _ := ret[0].(map[string]any) ret1, _ := ret[1].(error) return ret0, ret1 } // GetResetParams indicates an expected call of GetResetParams. func (mr *MockAuthHandlerMockRecorder) GetResetParams(uid interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetResetParams", reflect.TypeOf((*MockAuthHandler)(nil).GetResetParams), uid) } // Init mocks base method. func (m *MockAuthHandler) Init(jsonconf json.RawMessage, name string) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Init", jsonconf, name) ret0, _ := ret[0].(error) return ret0 } // Init indicates an expected call of Init. func (mr *MockAuthHandlerMockRecorder) Init(jsonconf, name interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Init", reflect.TypeOf((*MockAuthHandler)(nil).Init), jsonconf, name) } // IsInitialized mocks base method. func (m *MockAuthHandler) IsInitialized() bool { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "IsInitialized") ret0, _ := ret[0].(bool) return ret0 } // IsInitialized indicates an expected call of IsInitialized. func (mr *MockAuthHandlerMockRecorder) IsInitialized() *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "IsInitialized", reflect.TypeOf((*MockAuthHandler)(nil).IsInitialized)) } // IsUnique mocks base method. func (m *MockAuthHandler) IsUnique(secret []byte, remoteAddr string) (bool, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "IsUnique", secret, remoteAddr) ret0, _ := ret[0].(bool) ret1, _ := ret[1].(error) return ret0, ret1 } // IsUnique indicates an expected call of IsUnique. func (mr *MockAuthHandlerMockRecorder) IsUnique(secret, remoteAddr interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "IsUnique", reflect.TypeOf((*MockAuthHandler)(nil).IsUnique), secret, remoteAddr) } // RestrictedTags mocks base method. func (m *MockAuthHandler) RestrictedTags() ([]string, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "RestrictedTags") ret0, _ := ret[0].([]string) ret1, _ := ret[1].(error) return ret0, ret1 } // RestrictedTags indicates an expected call of RestrictedTags. func (mr *MockAuthHandlerMockRecorder) RestrictedTags() *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RestrictedTags", reflect.TypeOf((*MockAuthHandler)(nil).RestrictedTags)) } // UpdateRecord mocks base method. func (m *MockAuthHandler) UpdateRecord(rec *auth.Rec, secret []byte, remoteAddr string) (*auth.Rec, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "UpdateRecord", rec, secret, remoteAddr) ret0, _ := ret[0].(*auth.Rec) ret1, _ := ret[1].(error) return ret0, ret1 } // UpdateRecord indicates an expected call of UpdateRecord. func (mr *MockAuthHandlerMockRecorder) UpdateRecord(rec, secret, remoteAddr interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateRecord", reflect.TypeOf((*MockAuthHandler)(nil).UpdateRecord), rec, secret, remoteAddr) } ================================================ FILE: server/auth/rest/README.md ================================================ # REST or JSON-RPC authenticator This authenticator permits authentication of Tinode users and creation of Tinode accounts using a separate process as a source of truth. For instance, if accounts are managed by corporate LDAP, this service allows handling of Tinode authentication using the same LDAP service. This authenticator calls a designated authentication service over HTTP(S) POST. A skeleton implementation of a server is provided for reference at [rest-auth](../../../rest-auth/). The requests may be handled either by a single endpoint or by separate per-request endpoints. Request and response payloads are formatted as JSON. Some of the request or response fields are context-dependent and may be skipped. - [REST or JSON-RPC authenticator](#rest-or-json-rpc-authenticator) - [Configuration](#configuration) - [Request](#request) - [Response](#response) - [Recognized error responses](#recognized-error-responses) - [The server must implement the following endpoints:](#the-server-must-implement-the-following-endpoints) - [`add` Add new authentication record](#add-add-new-authentication-record) - [Sample request](#sample-request) - [Sample response (rec values may change)](#sample-response-rec-values-may-change) - [`auth` Request for authentication](#auth-request-for-authentication) - [Sample request](#sample-request) - [Sample response when the account already exists (optional challenge included)](#sample-response-when-the-account-already-exists-optional-challenge-included) - [Sample response when the account needs to be created by Tinode](#sample-response-when-the-account-needs-to-be-created-by-tinode) - [`checkunique` Checks if provided authentication record is unique.](#checkunique-checks-if-provided-authentication-record-is-unique) - [Sample request](#sample-request) - [Sample response](#sample-response) - [`del` Requests to delete authentication record.](#del-requests-to-delete-authentication-record) - [Sample request](#sample-request) - [Sample response](#sample-response) - [`gen` Generate authentication secret.](#gen-generate-authentication-secret) - [Sample request](#sample-request) - [Sample response](#sample-response) - [`link` Requests server to link new account ID to authentication record.](#link-requests-server-to-link-new-account-id-to-authentication-record) - [Sample request](#sample-request) - [Sample response](#sample-response) - [`upd` Update authentication record.](#upd-update-authentication-record) - [Sample request](#sample-request) - [Sample response](#sample-response) - [`rtagns` Get a list of restricted tag namespaces.](#rtagns-get-a-list-of-restricted-tag-namespaces) - [Sample request](#sample-request) - [Sample response](#sample-response) ## Configuration Add the following section to the `auth_config` in [tinode.conf](../../tinode.conf): ```js ... "auth_config": { ... "rest": { // ServerUrl is the URL of the authentication server to call. The URL must be absolute: // it must include the scheme, such as http or https, and the host name. "server_url": "http://127.0.0.1:5000/", // Authentication server is allowed to create new accounts. "allow_new_accounts": true, // Use separate endpoints, i.e. add request name to serverUrl path when making requests: // http://127.0.0.1:5000/add "use_separate_endpoints": true }, ... }, ``` If you want to use your authenticator **instead** of stock `basic` (login-password) authentication, add logical renaming and disable `rest` at the original name: ```js ... "auth_config": { "logical_names": ["basic:rest", "rest:"], "rest": { ... }, ... }, ... ``` ## Request ```js { "endpoint": "auth", // string, one of the endpoints as described below, optional. "secret": "Ym9iOmJvYjEyMw==", // authentication secret as provided by the client, // base64-encoded bytes, optional. "addr": "2001:0db8:85a3:0000:0000:8a2e:0370:7334", // string, IPv4 or IPv6 address of // the client making the request, optional. "rec": { // authentication record, optional. { "uid": "LELEQHDWbgY", // user ID, int64 base64-encoded "authlvl": "auth", // authentication level "lifetime": "10000s", // lifetime of this record in seconds or as time.Duration string; // see https://golang.org/pkg/time/#Duration for format. "features": 1, // bitmap of features as integer or as a string of feature characters: // "validated" (V) or "no login" (L)". "tags": ["email:alice@example.com"], // Tags associated with this authentication record. "state": "ok", // optional account state. } } } ``` ## Response ```js { "err": "internal", // string, error message in case of an error. "rec": { // authentication record. ... // the same as `request.rec` }, "byteval": "Ym9iOmJvYjEyMw==", // array of bytes, optional "ts": "2018-12-04T15:17:02.627Z", // time stamp, optional "boolval": true, // boolean value, optional "strarr": ["abc", "def"], // array of strings, optoional "newacc": { // data to use for creating a new account. // Default access mode "auth": "JRWPS", "anon": "N", "public": {...}, // user's public data, see /docs/API.md#trusted-public-and-private-fields "trusted": {...}, // user's trusted data, see /docs/API.md#trusted-public-and-private-fields "private": {...} // user's private data, see /docs/API.md#trusted-public-and-private-fields } } ``` ## Recognized error responses The error is returned as json: ```json { "err": "error-message" } ``` See [here](../../store/types/types.go#L24) for an up to date list of supported error messages. * "internal": database failure or other internal catch-all failure. * "malformed": request cannot be parsed or otherwise wrong. * "failed": authentication failed (wrong login or password, etc). * "duplicate value": duplicate credential, i.e. attempt to create a record with a non-unique login. * "unsupported": the operation is not supported. * "expired": the secret has expired. * "policy": policy violation, e.g. password too weak. * "credentials": credentials like email or captcha must be validated. * "not found": the object was not found. * "denied": the operation is not permitted. ## The server must implement the following endpoints: ### `add` Add new authentication record This endpoint requests server to add a new authentication record. This endpoint is generally used for account creation. If accounts are managed externally, it's likely to be unused and should generally return an error `"unsupported"`. #### Sample request ```json { "endpoint": "add", "secret": "Ym9iOmJvYjEyMw==", "addr": "111.22.33.44", "rec": { "uid": "LELEQHDWbgY", "lifetime": "10000s", "features": 2, "tags": ["email:alice@example.com"] } } ``` #### Sample response (rec values may change) ```json { "rec": { "uid": "LELEQHDWbgY", "authlvl": "auth", "lifetime": "5000s", "features": 1, "tags": ["email:alice@example.com", "uname:alice"] } } ``` ### `auth` Request for authentication Request to authenticate a user. Client (Tinode) provides a secret, authentication server responds with a user record. If this is a very first login and the server manages the accounts, the server may return `newacc` object which will be used by client (Tinode) to create the account. The server may optionally return a challenge as `byteval`. #### Sample request ```json { "endpoint": "auth", "secret": "Ym9iOmJvYjEyMw==", "addr": "111.22.33.44" } ``` #### Sample response when the account already exists (optional challenge included) ```json { "rec": { "uid": "LELEQHDWbgY", "authlvl": "auth", "state": "ok" }, "byteval": "9X6m3tWeBEMlDxlcFAABAAEAbVs" } ``` #### Sample response when the account needs to be created by Tinode ```js { "rec": { "state": "suspended", // Or "ok". "authlvl": "auth", "lifetime": "5000s", "features": 1, "tags": ["email:alice@example.com", "uname:alice"] }, "newacc": { "auth": "JRWPS", "anon": "N", "public": {/* see /docs/API.md#trusted-public-and-private-fields */}, "trusted": {/* see /docs/API.md#trusted-public-and-private-fields */}, "private": {/* see /docs/API.md#trusted-public-and-private-fields */} } } ``` ### `checkunique` Checks if provided authentication record is unique. Request is used for account creation. If accounts are managed by the server, the server should respond with an error `"unsupported"`. #### Sample request ```json { "endpoint": "checkunique", "secret": "Ym9iOmJvYjEyMw==", "addr": "111.22.33.44" } ``` #### Sample response ```json { "boolval": true } ``` ### `del` Requests to delete authentication record. If accounts are managed by the server, the server should respond with an error `"unsupported"`. #### Sample request ```json { "endpoint": "del", "rec": { "uid": "LELEQHDWbgY", } } ``` #### Sample response ```json {} ``` ### `gen` Generate authentication secret. If accounts are managed by the server, the server should respond with an error `"unsupported"`. #### Sample request ```json { "endpoint": "gen", "rec": { "uid": "LELEQHDWbgY", "authlvl": "auth", } } ``` #### Sample response ```json { "byteval": "9X6m3tWeBEMlDxlcFAABAAEAbVs", "ts": "2018-12-04T15:17:02.627Z", } ``` ### `link` Requests server to link new account ID to authentication record. If server requested Tinode to create a new account, this endpoint is used to link the new Tinode user ID with the server's authentication record. If linking is successful, the server should respond with a non-empty json. #### Sample request ```json { "endpoint": "link", "secret": "Ym9iOmJvYjEyMw==", "rec": { "uid": "LELEQHDWbgY", "authlvl": "auth", }, } ``` #### Sample response ```json {} ``` ### `upd` Update authentication record. If accounts are managed by the server, the server should respond with an error `"unsupported"`. #### Sample request ```json { "endpoint": "upd", "secret": "Ym9iOmJvYjEyMw==", "addr": "111.22.33.44", "rec": { "uid": "LELEQHDWbgY", "authlvl": "auth", } } ``` #### Sample response ```json {} ``` ### `rtagns` Get a list of restricted tag namespaces. Server may enforce certain tag namespaces (tag prefixes) to be restricted, i.e. not editable by the user. These are also used in Tinode discovery mechanism (e.g. searching for users, contact sync). See [API docs](/docs/API.md#fnd-and-tags-finding-users-and-topics) for details. The server may optionally provide a regular expression to validate search tokens before rewriting them as prefixed tags. I.e. if server allows only logins of 3-8 ASCII letters and numbers then the regexp could be `^[a-z0-9_]{3,8}$` which is base64-encoded as `XlthLXowLTlfXXszLDh9JA==`. #### Sample request ```json { "endpoint": "rtagns", } ``` #### Sample response ```json { "strarr": ["basic", "email", "tel"], "byteval": "XlthLXowLTlfXXszLDh9JA==" } ``` ================================================ FILE: server/auth/rest/auth_rest.go ================================================ // Package rest provides authentication by calling a separate process over REST API (technically JSON RPC, not REST). package rest import ( "bytes" "encoding/json" "errors" "io" "net/http" "net/url" "regexp" "strings" "time" "github.com/tinode/chat/server/auth" "github.com/tinode/chat/server/logs" "github.com/tinode/chat/server/store" "github.com/tinode/chat/server/store/types" ) // authenticator is the type to map authentication methods to. type authenticator struct { // Logical name of this authenticator name string // URL of the server serverUrl string // Authenticator may add new accounts to local database. allowNewAccounts bool // Use separate endpoints, i.e. add request name to serverUrl path when making requests. useSeparateEndpoints bool // Cache of restricted tag prefixes (namespaces). rTagNS []string // Optional regex pattern for checking tokens. reToken *regexp.Regexp } // Request to the server. type request struct { Endpoint string `json:"endpoint"` Name string `json:"name"` Record *auth.Rec `json:"rec,omitempty"` Secret []byte `json:"secret,omitempty"` RemoteAddr string `json:"addr,omitempty"` } // User initialization data when creating a new user. type newAccount struct { // Default access mode Auth string `json:"auth,omitempty"` Anon string `json:"anon,omitempty"` // User's Public data Public any `json:"public,omitempty"` // User's Trusted data Trusted any `json:"trusted,omitempty"` // Per-subscription private data Private any `json:"private,omitempty"` } // Response from the server. type response struct { // Error message in case of an error. Err string `json:"err,omitempty"` // Optional auth record Record *auth.Rec `json:"rec,omitempty"` // Optional byte slice ByteVal []byte `json:"byteval,omitempty"` // Optional time value TimeVal time.Time `json:"ts,omitempty"` // Boolean value BoolVal bool `json:"boolval,omitempty"` // String slice value StrSliceVal []string `json:"strarr,omitempty"` // Account creation data NewAcc *newAccount `json:"newacc,omitempty"` } // Init initializes the handler. func (a *authenticator) Init(jsonconf json.RawMessage, name string) error { if name == "" { return errors.New("auth_rest: authenticator name cannot be blank") } if a.name != "" { return errors.New("auth_rest: already initialized as " + a.name + "; " + name) } type configType struct { // ServerUrl is the URL of the server to call. ServerUrl string `json:"server_url"` // Server may create new accounts. AllowNewAccounts bool `json:"allow_new_accounts"` // Use separate endpoints, i.e. add request name to serverUrl path when making requests. UseSeparateEndpoints bool `json:"use_separate_endpoints"` } var config configType err := json.Unmarshal(jsonconf, &config) if err != nil { return errors.New("auth_rest: failed to parse config: " + err.Error() + "(" + string(jsonconf) + ")") } serverUrl, err := url.Parse(config.ServerUrl) if err != nil || !serverUrl.IsAbs() { return errors.New("auth_rest: invalid server_url '" + string(jsonconf) + "'") } if !strings.HasSuffix(serverUrl.Path, "/") { serverUrl.Path += "/" } a.name = name a.serverUrl = serverUrl.String() a.allowNewAccounts = config.AllowNewAccounts a.useSeparateEndpoints = config.UseSeparateEndpoints return nil } // IsInitialized returns true if the handler is initialized. func (a *authenticator) IsInitialized() bool { return a.name != "" } // Execute HTTP POST to the server at the specified endpoint and with the provided payload. func (a *authenticator) callEndpoint(endpoint string, rec *auth.Rec, secret []byte, remoteAddr string) (*response, error) { // Convert payload to json. req := &request{Endpoint: endpoint, Name: a.name, Record: rec, Secret: secret, RemoteAddr: remoteAddr} content, err := json.Marshal(req) if err != nil { return nil, err } urlToCall := a.serverUrl if a.useSeparateEndpoints { epUrl, _ := url.Parse(a.serverUrl) epUrl.Path += endpoint urlToCall = epUrl.String() } // Send payload to server using default HTTP client. post, err := http.Post(urlToCall, "application/json", bytes.NewBuffer(content)) if err != nil { return nil, err } defer post.Body.Close() // Check HTTP status response. Must be 2xx. if post.StatusCode < http.StatusOK || post.StatusCode >= http.StatusMultipleChoices { return nil, errors.New("unexpected HTTP response " + post.Status) } // Read response. body, err := io.ReadAll(post.Body) if err != nil { return nil, err } // Parse response. var resp response err = json.Unmarshal(body, &resp) if err != nil { return nil, err } if resp.Err != "" { return nil, types.StoreError(resp.Err) } return &resp, nil } // AddRecord adds persistent authentication record to the database. // Returns: updated auth record, error func (a *authenticator) AddRecord(rec *auth.Rec, secret []byte, remoteAddr string) (*auth.Rec, error) { resp, err := a.callEndpoint("add", rec, secret, remoteAddr) if err != nil { return nil, err } return resp.Record, nil } // UpdateRecord updates existing record with new credentials. func (a *authenticator) UpdateRecord(rec *auth.Rec, secret []byte, remoteAddr string) (*auth.Rec, error) { _, err := a.callEndpoint("upd", rec, secret, remoteAddr) return rec, err } // Authenticate: get user record by provided secret func (a *authenticator) Authenticate(secret []byte, remoteAddr string) (*auth.Rec, []byte, error) { resp, err := a.callEndpoint("auth", nil, secret, remoteAddr) if err != nil { return nil, nil, err } // Auth record not found. if resp.Record == nil { logs.Warn.Println("rest_auth: invalid response: missing Record") return nil, nil, types.ErrInternal } // Check if server provided a user ID. If not, create a new account in the local database. if resp.Record.Uid.IsZero() && a.allowNewAccounts { if resp.NewAcc == nil { return nil, nil, types.ErrNotFound } // Create account, get UID, report UID back to the server. user := types.User{ State: resp.Record.State, Public: resp.NewAcc.Public, Trusted: resp.NewAcc.Trusted, Tags: resp.Record.Tags, } user.Access.Auth.UnmarshalText([]byte(resp.NewAcc.Auth)) user.Access.Anon.UnmarshalText([]byte(resp.NewAcc.Anon)) _, err = store.Users.Create(&user, resp.NewAcc.Private) if err != nil { return nil, nil, err } // Report the new UID to the server. resp.Record.Uid = user.Uid() _, err = a.callEndpoint("link", resp.Record, secret, "") if err != nil { store.Users.Delete(resp.Record.Uid, true) return nil, nil, err } } return resp.Record, resp.ByteVal, nil } // AsTag converts search token into prefixed tag or an empty string if it // cannot be represented as a prefixed tag. func (a *authenticator) AsTag(token string) string { if len(a.rTagNS) > 0 { if a.reToken != nil && !a.reToken.MatchString(token) { return "" } // No validation or passed validation. return a.rTagNS[0] + ":" + token } return "" } // IsUnique verifies if the provided secret can be considered unique by the auth // scheme as well as policy compliance. E.g. if login is unique and not too short/long. func (a *authenticator) IsUnique(secret []byte, remoteAddr string) (bool, error) { resp, err := a.callEndpoint("checkunique", nil, secret, remoteAddr) if err != nil { return false, err } return resp.BoolVal, err } // GenSecret generates a new secret, if appropriate. func (a *authenticator) GenSecret(rec *auth.Rec) ([]byte, time.Time, error) { resp, err := a.callEndpoint("gen", rec, nil, "") if err != nil { return nil, time.Time{}, err } return resp.ByteVal, resp.TimeVal, err } // DelRecords deletes all authentication records for the given user. func (a *authenticator) DelRecords(uid types.Uid) error { logs.Info.Println("DelRecords, initialized=", a.name != "") _, err := a.callEndpoint("del", &auth.Rec{Uid: uid}, nil, "") return err } // RestrictedTags returns tag namespaces (prefixes, such as prefix:login) restricted by the server. func (a *authenticator) RestrictedTags() ([]string, error) { if a.rTagNS != nil { // Using cached prefixes. ns := make([]string, len(a.rTagNS)) // Returning a copy to prevent accidental modification of server-provided tags. copy(ns, a.rTagNS) return ns, nil } // First time use, fetch prefixes from the server. resp, err := a.callEndpoint("rtagns", nil, nil, "") if err != nil { return nil, err } // Save valid result to cache. a.rTagNS = resp.StrSliceVal if len(resp.ByteVal) > 0 { a.reToken, err = regexp.Compile(string(resp.ByteVal)) if err != nil { logs.Warn.Println("rest_auth: invalid token regexp", string(resp.ByteVal)) } } return resp.StrSliceVal, nil } // GetResetParams returns authenticator parameters passed to password reset handler // (none for rest). func (authenticator) GetResetParams(uid types.Uid) (map[string]any, error) { // TODO: route request to the server. return nil, nil } const realName = "rest" // GetRealName returns the hardcoded name of the authenticator. func (authenticator) GetRealName() string { return realName } func init() { store.RegisterAuthScheme(realName, &authenticator{}) } ================================================ FILE: server/auth/token/auth_token.go ================================================ // Package token implements authentication by HMAC-signed security token. package token import ( "bytes" "crypto/hmac" "crypto/sha256" "encoding/binary" "encoding/json" "errors" "time" "github.com/tinode/chat/server/auth" "github.com/tinode/chat/server/store" "github.com/tinode/chat/server/store/types" ) // authenticator is a singleton instance of the authenticator. type authenticator struct { name string hmacSalt []byte lifetime time.Duration serialNumber int } // tokenLayout defines positioning of various bytes in token. // [8:UID][4:expires][2:authLevel][2:serial-number][2:feature-bits][32:signature] = 50 bytes type tokenLayout struct { // User ID. Uid uint64 // Token expiration time. Expires uint32 // User's authentication level. AuthLevel uint16 // Serial number - to invalidate all tokens if needed. SerialNumber uint16 // Bitmap with feature bits. Features uint16 } // Init initializes the authenticator: parses the config and sets salt, serial number and lifetime. func (ta *authenticator) Init(jsonconf json.RawMessage, name string) error { if name == "" { return errors.New("auth_token: authenticator name cannot be blank") } if ta.name != "" { return errors.New("auth_token: already initialized as " + ta.name + "; " + name) } type configType struct { // Key for signing tokens Key []byte `json:"key"` // Datatabase or other serial number, to invalidate all issued tokens at once. SerialNum int `json:"serial_num"` // Token expiration time ExpireIn int `json:"expire_in"` } var config configType if err := json.Unmarshal(jsonconf, &config); err != nil { return errors.New("auth_token: failed to parse config: " + err.Error() + "(" + string(jsonconf) + ")") } if len(config.Key) < sha256.Size { return errors.New("auth_token: the key is missing or too short") } if config.ExpireIn <= 0 { return errors.New("auth_token: invalid expiration value") } ta.name = name ta.hmacSalt = config.Key ta.lifetime = time.Duration(config.ExpireIn) * time.Second ta.serialNumber = config.SerialNum return nil } // IsInitialized returns true if the handler is initialized. func (ta *authenticator) IsInitialized() bool { return ta.name != "" } // AddRecord is not supported, will produce an error. func (authenticator) AddRecord(rec *auth.Rec, secret []byte, remoteAddr string) (*auth.Rec, error) { return nil, types.ErrUnsupported } // UpdateRecord is not supported, will produce an error. func (authenticator) UpdateRecord(rec *auth.Rec, secret []byte, remoteAddr string) (*auth.Rec, error) { return nil, types.ErrUnsupported } // Authenticate checks validity of provided token. func (ta *authenticator) Authenticate(token []byte, remoteAddr string) (*auth.Rec, []byte, error) { var tl tokenLayout dataSize := binary.Size(&tl) if len(token) < dataSize+sha256.Size { // Token is too short return nil, nil, types.ErrMalformed } buf := bytes.NewBuffer(token) err := binary.Read(buf, binary.LittleEndian, &tl) if err != nil { return nil, nil, types.ErrMalformed } hbuf := new(bytes.Buffer) binary.Write(hbuf, binary.LittleEndian, &tl) // Check signature. hasher := hmac.New(sha256.New, ta.hmacSalt) hasher.Write(hbuf.Bytes()) if !hmac.Equal(token[dataSize:dataSize+sha256.Size], hasher.Sum(nil)) { return nil, nil, types.ErrFailed } // Check authentication level for validity. if auth.Level(tl.AuthLevel) > auth.LevelRoot { return nil, nil, types.ErrMalformed } // Check serial number. if int(tl.SerialNumber) != ta.serialNumber { return nil, nil, types.ErrFailed } // Check token expiration time. expires := time.Unix(int64(tl.Expires), 0).UTC() if expires.Before(time.Now().Add(1 * time.Second)) { return nil, nil, types.ErrExpired } return &auth.Rec{ Uid: types.Uid(tl.Uid), AuthLevel: auth.Level(tl.AuthLevel), Lifetime: auth.Duration(time.Until(expires)), Features: auth.Feature(tl.Features), State: types.StateUndefined}, nil, nil } // GenSecret generates a new token. func (ta *authenticator) GenSecret(rec *auth.Rec) ([]byte, time.Time, error) { if rec.Lifetime == 0 { rec.Lifetime = auth.Duration(ta.lifetime) } else if rec.Lifetime < 0 { return nil, time.Time{}, types.ErrExpired } expires := time.Now().Add(time.Duration(rec.Lifetime)).UTC().Round(time.Millisecond) tl := tokenLayout{ Uid: uint64(rec.Uid), Expires: uint32(expires.Unix()), AuthLevel: uint16(rec.AuthLevel), SerialNumber: uint16(ta.serialNumber), Features: uint16(rec.Features), } buf := new(bytes.Buffer) binary.Write(buf, binary.LittleEndian, &tl) hasher := hmac.New(sha256.New, ta.hmacSalt) hasher.Write(buf.Bytes()) binary.Write(buf, binary.LittleEndian, hasher.Sum(nil)) return buf.Bytes(), expires, nil } // AsTag is not supported, will produce an empty string. func (authenticator) AsTag(token string) string { return "" } // IsUnique is not supported, will produce an error. func (authenticator) IsUnique(token []byte, remoteAddr string) (bool, error) { return false, types.ErrUnsupported } // DelRecords adds disabled user ID to a stop list. func (authenticator) DelRecords(uid types.Uid) error { return nil } // RestrictedTags returns tag namespaces restricted by this authenticator (none for token). func (authenticator) RestrictedTags() ([]string, error) { return nil, nil } // GetResetParams returns authenticator parameters passed to password reset handler // (none for token). func (authenticator) GetResetParams(uid types.Uid) (map[string]any, error) { return nil, nil } const realName = "token" // GetRealName returns the hardcoded name of the authenticator. func (authenticator) GetRealName() string { return realName } func init() { store.RegisterAuthScheme(realName, &authenticator{}) } ================================================ FILE: server/calls.go ================================================ /****************************************************************************** * * Description : * Video call handling (establishment, metadata exhange and termination). * *****************************************************************************/ package main import ( "encoding/json" "errors" "fmt" "os" "strconv" "time" "github.com/tinode/chat/server/logs" "github.com/tinode/chat/server/store/types" jcr "github.com/tinode/jsonco" ) // Video call constants. const ( // Events for call between users A and B. // // B has received the call but hasn't picked it up yet. constCallEventRinging = "ringing" // B has accepted the call. constCallEventAccept = "accept" // WebRTC SDP & ICE data exchange events. constCallEventOffer = "offer" constCallEventAnswer = "answer" constCallEventIceCandidate = "ice-candidate" // Call finished by either side or server. constCallEventHangUp = "hang-up" // Message headers representing call states. // Call is established. constCallMsgAccepted = "accepted" // Previously establied call has successfully finished. constCallMsgFinished = "finished" // Call is dropped (e.g. because of an error). constCallMsgDisconnected = "disconnected" // Call is missed (the callee didn't pick up the phone). constCallMsgMissed = "missed" // Call is declined (the callee hung up before picking up). constCallMsgDeclined = "declined" ) type callConfig struct { // Enable video/voice calls. Enabled bool `json:"enabled"` // Timeout in seconds before a call is dropped if not answered. CallEstablishmentTimeout int `json:"call_establishment_timeout"` // ICE servers. ICEServers []iceServer `json:"ice_servers"` // Alternative config as an external file. ICEServersFile string `json:"ice_servers_file"` } // ICE server config. type iceServer struct { Username string `json:"username,omitempty"` Credential string `json:"credential,omitempty"` CredentialType string `json:"credential_type,omitempty"` Urls []string `json:"urls,omitempty"` } // callPartyData describes a video call participant. type callPartyData struct { // ID of the call participant (asUid); not necessarily the session owner. uid types.Uid // True if this session/user initiated the call. isOriginator bool // Call party session. sess *Session } // videoCall describes video call that's being established or in progress. type videoCall struct { // Call participants (session sid -> callPartyData). parties map[string]callPartyData // Call message seq ID. seq int // Call message content. content any // Call message content mime type. contentMime any // Time when the call was accepted. acceptedAt time.Time } // callPartySession returns a session to be stored in the call party data. func callPartySession(sess *Session) *Session { if sess.isProxy() { // We are on the topic host node. Make a copy of the ephemeral proxy session. callSess := &Session{ proto: PROXY, // Multiplexing session which actually handles the communication. multi: sess.multi, // Local parameters specific to this session. sid: sess.sid, userAgent: sess.userAgent, remoteAddr: sess.remoteAddr, lang: sess.lang, countryCode: sess.countryCode, proxyReq: ProxyReqCall, background: sess.background, uid: sess.uid, } return callSess } return sess } func initVideoCalls(jsconfig json.RawMessage) error { var config callConfig if len(jsconfig) == 0 { return nil } if err := json.Unmarshal([]byte(jsconfig), &config); err != nil { return fmt.Errorf("failed to parse config: %w", err) } if !config.Enabled { logs.Info.Println("Video calls disabled") return nil } if len(config.ICEServers) > 0 { globals.iceServers = config.ICEServers } else if config.ICEServersFile != "" { var iceConfig []iceServer file, err := os.Open(config.ICEServersFile) if err != nil { return fmt.Errorf("failed to read ICE config: %w", err) } jr := jcr.New(file) if err = json.NewDecoder(jr).Decode(&iceConfig); err != nil { switch jerr := err.(type) { case *json.UnmarshalTypeError: lnum, cnum, _ := jr.LineAndChar(jerr.Offset) return fmt.Errorf("unmarshall error in ICE config in %s at %d:%d (offset %d bytes): %w", jerr.Field, lnum, cnum, jerr.Offset, jerr) case *json.SyntaxError: lnum, cnum, _ := jr.LineAndChar(jerr.Offset) return fmt.Errorf("syntax error in config file at %d:%d (offset %d bytes): %w", lnum, cnum, jerr.Offset, jerr) default: return fmt.Errorf("failed to parse config file: %w", err) } } file.Close() globals.iceServers = iceConfig } if len(globals.iceServers) == 0 { return errors.New("no valid ICE cervers found") } globals.callEstablishmentTimeout = config.CallEstablishmentTimeout if globals.callEstablishmentTimeout <= 0 { globals.callEstablishmentTimeout = defaultCallEstablishmentTimeout } logs.Info.Println("Video calls enabled with", len(globals.iceServers), "ICE servers") return nil } // Add webRTC-related headers to message Head. The original Head may already contain some entries, // like 'sender', preserve them. func (call *videoCall) messageHead(head map[string]any, newState string, duration int) map[string]any { if head == nil { head = map[string]any{} } head["replace"] = ":" + strconv.Itoa(call.seq) head["webrtc"] = newState if duration > 0 { head["webrtc-duration"] = duration } else { delete(head, "webrtc-duration") } if call.contentMime != nil { head["mime"] = call.contentMime } return head } // Generates server info message template for the video call event. func (call *videoCall) infoMessage(event string) *ServerComMessage { return &ServerComMessage{ Info: &MsgServerInfo{ What: "call", Event: event, SeqId: call.seq, }, } } // Returns Uid and session of the present video call originator // if a call is being established or in progress. func (t *Topic) getCallOriginator() (types.Uid, *Session) { if t.currentCall == nil { return types.ZeroUid, nil } for _, p := range t.currentCall.parties { if p.isOriginator { return p.uid, p.sess } } return types.ZeroUid, nil } // Handles video call invite (initiation) // (in response to msg = {pub head=[mime: application/x-tinode-webrtc]}). func (t *Topic) handleCallInvite(msg *ClientComMessage, asUid types.Uid) { // Call being establshed. t.currentCall = &videoCall{ parties: make(map[string]callPartyData), seq: t.lastID, content: msg.Pub.Content, contentMime: msg.Pub.Head["mime"], } t.currentCall.parties[msg.sess.sid] = callPartyData{ uid: asUid, isOriginator: true, sess: callPartySession(msg.sess), } // Wait for constCallEstablishmentTimeout for the other side to accept the call. t.callEstablishmentTimer.Reset(time.Duration(globals.callEstablishmentTimeout) * time.Second) } // Handles events on existing video call (acceptance, termination, metadata exchange). // (in response to msg = {note what=call}). func (t *Topic) handleCallEvent(msg *ClientComMessage) { if t.currentCall == nil { // Must initiate call first. logs.Warn.Printf("topic[%s]: No call in progress", t.name) return } if t.isInactive() { // Topic is paused or being deleted. return } call := msg.Note if t.currentCall.seq != call.SeqId { // Call not found. logs.Info.Printf("topic[%s]: invalid seq id - current call (%d) vs received (%d)", t.name, t.currentCall.seq, call.SeqId) return } asUid := types.ParseUserId(msg.AsUser) if _, userFound := t.perUser[asUid]; !userFound { // User not found in topic. logs.Warn.Printf("topic[%s]: could not find user %s", t.name, asUid.UserId()) return } switch call.Event { case constCallEventRinging, constCallEventAccept: // Invariants: // 1. Call has been initiated but not been established yet. if len(t.currentCall.parties) != 1 { return } originatorUid, originator := t.getCallOriginator() if originator == nil { // No originator session: terminating. t.terminateCallInProgress(false) return } // 2. These events may only arrive from the callee. if originator.sid == msg.sess.sid || originatorUid == asUid { return } // Prepare a {info} message to forward to the call originator. forwardMsg := t.currentCall.infoMessage(call.Event) forwardMsg.Info.From = msg.AsUser forwardMsg.Info.Topic = t.original(originatorUid) if call.Event == constCallEventAccept { // The call has been accepted. // Send a replacement {data} message to the topic. msgCopy := *msg msgCopy.AsUser = originatorUid.UserId() replaceWith := constCallMsgAccepted var origHead map[string]any if msgCopy.Pub != nil { origHead = msgCopy.Pub.Head } // else fetch the original message from store and use its head. head := t.currentCall.messageHead(origHead, replaceWith, 0) if err := t.saveAndBroadcastMessage(&msgCopy, originatorUid, false, nil, head, t.currentCall.content); err != nil { return } // Add callee data to t.currentCall. t.currentCall.parties[msg.sess.sid] = callPartyData{ uid: asUid, isOriginator: false, sess: callPartySession(msg.sess), } t.currentCall.acceptedAt = time.Now() // Notify other clients that the call has been accepted. t.infoCallSubsOffline(msg.AsUser, asUid, call.Event, t.currentCall.seq, call.Payload, msg.sess.sid, false) t.callEstablishmentTimer.Stop() } originator.queueOut(forwardMsg) case constCallEventOffer, constCallEventAnswer, constCallEventIceCandidate: // Invariants: // 1. Call has been estabslied (2 participants). if len(t.currentCall.parties) != 2 { logs.Warn.Printf("topic[%s]: call participants expected 2 vs found %d", t.name, len(t.currentCall.parties)) return } // 2. Event is coming from a call participant session. if _, ok := t.currentCall.parties[msg.sess.sid]; !ok { logs.Warn.Printf("topic[%s]: call event from non-party session %s", t.name, msg.sess.sid) return } // Call metadata exchange. Either side of the call may send these events. // Simply forward them to the other session. var otherUid types.Uid var otherEnd *Session for sid, p := range t.currentCall.parties { if sid != msg.sess.sid { otherUid = p.uid otherEnd = p.sess break } } if otherEnd == nil { logs.Warn.Printf("topic[%s]: could not find call peer for session %s", t.name, msg.sess.sid) return } // All is good. Send {info} message to the otherEnd. forwardMsg := t.currentCall.infoMessage(call.Event) forwardMsg.Info.From = msg.AsUser forwardMsg.Info.Topic = t.original(otherUid) forwardMsg.Info.Payload = call.Payload otherEnd.queueOut(forwardMsg) case constCallEventHangUp: switch len(t.currentCall.parties) { case 2: // If it's a call in progress, hangup may arrive only from a call participant session. if _, ok := t.currentCall.parties[msg.sess.sid]; !ok { return } case 1: // Call hasn't been established yet. originatorUid, originator := t.getCallOriginator() // Hangup may come from either the originating session or // any callee user session. if asUid == originatorUid && originator.sid != msg.sess.sid { return } default: break } t.maybeEndCallInProgress(msg.AsUser, msg, false) default: logs.Warn.Printf("topic[%s]: video call (seq %d) received unexpected call event: %s", t.name, t.currentCall.seq, call.Event) } } // Ends current call in response to a client hangup request (msg). func (t *Topic) maybeEndCallInProgress(from string, msg *ClientComMessage, callDidTimeout bool) { if t.currentCall == nil { return } t.callEstablishmentTimer.Stop() originatorUid, _ := t.getCallOriginator() var replaceWith string var callDuration int64 if from != "" && len(t.currentCall.parties) == 2 { // This is a call in progress. replaceWith = constCallMsgFinished callDuration = time.Since(t.currentCall.acceptedAt).Milliseconds() } else { if from != "" { // User originated hang-up. if from == originatorUid.UserId() { // Originator/caller requested event. replaceWith = constCallMsgMissed } else { // Callee requested event. replaceWith = constCallMsgDeclined } } else { // Server initiated disconnect. // Call hasn't been established. Just drop it. if callDidTimeout { replaceWith = constCallMsgMissed } else { replaceWith = constCallMsgDisconnected } } } // Send a message indicating the call has ended. msgCopy := *msg msgCopy.AsUser = originatorUid.UserId() var origHead map[string]any if msgCopy.Pub != nil { origHead = msgCopy.Pub.Head } // else fetch the original message from store and use its head. head := t.currentCall.messageHead(origHead, replaceWith, int(callDuration)) if err := t.saveAndBroadcastMessage(&msgCopy, originatorUid, false, nil, head, t.currentCall.content); err != nil { logs.Err.Printf("topic[%s]: failed to write finalizing message for call seq id %d - '%s'", t.name, t.currentCall.seq, err) } // Send {info} hangup event to the subscribed sessions. t.broadcastToSessions(t.currentCall.infoMessage(constCallEventHangUp)) // Let all other sessions know the call is over. for tgt := range t.perUser { t.infoCallSubsOffline(from, tgt, constCallEventHangUp, t.currentCall.seq, nil, "", true) } t.currentCall = nil } // Server initiated call termination. func (t *Topic) terminateCallInProgress(callDidTimeout bool) { if t.currentCall == nil { return } uid, sess := t.getCallOriginator() if sess == nil || uid.IsZero() { // Just drop the call. logs.Warn.Printf("topic[%s]: video call seq %d has no originator, terminating.", t.name, t.currentCall.seq) t.currentCall = nil return } // Dummy hangup request. dummy := &ClientComMessage{ Original: t.original(uid), RcptTo: uid.UserId(), AsUser: uid.UserId(), Timestamp: types.TimeNow(), sess: sess, } logs.Info.Printf("topic[%s]: terminating call seq %d, timeout: %t", t.name, t.currentCall.seq, callDidTimeout) t.maybeEndCallInProgress("", dummy, callDidTimeout) } ================================================ FILE: server/cluster.go ================================================ package main import ( "encoding/gob" "encoding/json" "errors" "net" "net/rpc" "sort" "sync" "sync/atomic" "time" "github.com/tinode/chat/server/auth" "github.com/tinode/chat/server/concurrency" "github.com/tinode/chat/server/logs" "github.com/tinode/chat/server/push" rh "github.com/tinode/chat/server/ringhash" "github.com/tinode/chat/server/store/types" ) const ( // Network connection timeout. clusterNetworkTimeout = 3 * time.Second // Default timeout before attempting to reconnect to a node. clusterDefaultReconnectTime = 200 * time.Millisecond // Number of replicas in ringhash. clusterHashReplicas = 20 // Buffer size for sending requests from proxy to master. clusterProxyToMasterBuffer = 64 // Expand buffer size by this value for nodes over the basic 3-node setup. clusterProxyToMasterBufferPerNode = 16 // Timeout for attempting to enqueue a proxy-to-master request when the buffer is full. clusterP2MTimeout = 20 * time.Millisecond // Buffer size for receiving responses from other nodes, per node. clusterRpcCompletionBuffer = 64 ) // ProxyReqType is the type of proxy requests. type ProxyReqType int // Individual request types. const ( ProxyReqNone ProxyReqType = iota ProxyReqJoin // {sub}. ProxyReqLeave // {leave} ProxyReqMeta // {meta set|get} ProxyReqBroadcast // {pub}, {note} ProxyReqBgSession ProxyReqMeUserAgent ProxyReqCall // Used in video call proxy sessions for routing call events. ) type clusterNodeConfig struct { Name string `json:"name"` Addr string `json:"addr"` } type clusterConfig struct { // List of all members of the cluster, including this member Nodes []clusterNodeConfig `json:"nodes"` // Name of this cluster node ThisName string `json:"self"` // Deprecated: this field is no longer used. NumProxyEventGoRoutines int `json:"-"` // Failover configuration Failover *clusterFailoverConfig } // ClusterNode is a client's connection to another node. type ClusterNode struct { lock sync.Mutex // RPC endpoint endpoint *rpc.Client // True if the endpoint is believed to be connected connected bool // True if a go routine is trying to reconnect the node reconnecting bool // TCP address in the form host:port address string // Name of the node name string // Fingerprint of the node: unique value which changes when the node restarts. fingerprint int64 // A number of times this node has failed in a row failCount int // Channel for shutting down the runner; buffered, 1. done chan bool // IDs of multiplexing sessions belonging to this node. msess map[string]struct{} // Default channel for receiving responses to RPC calls issued by this node. // Buffered, clusterRpcCompletionBuffer * number_of_nodes. rpcDone chan *rpc.Call // Channel for sending proxy to master requests; buffered, clusterProxyToMasterBuffer. p2mSender chan *ClusterReq } func (n *ClusterNode) asyncRpcLoop() { for call := range n.rpcDone { n.handleRpcResponse(call) } } func (n *ClusterNode) p2mSenderLoop() { for req := range n.p2mSender { if req == nil { // Stop return } if err := n.proxyToMaster(req); err != nil { logs.Warn.Println("p2mSenderLoop: call failed", n.name, err) } } } // ClusterSess is a basic info on a remote session where the message was created. type ClusterSess struct { // IP address of the client. For long polling this is the IP of the last poll RemoteAddr string // User agent, a string provived by an authenticated client in {login} packet UserAgent string // ID of the current user or 0 Uid types.Uid // User's authentication level AuthLvl auth.Level // Protocol version of the client: ((major & 0xff) << 8) | (minor & 0xff) Ver int // Human language of the client Lang string // Country of the client CountryCode string // Device ID DeviceID string // Device platform: "web", "ios", "android" Platform string // Session ID Sid string // Background session Background bool } // ClusterSessUpdate represents a request to update a session. // User Agent change or background session comes to foreground. type ClusterSessUpdate struct { // User this session represents. Uid types.Uid // Session id. Sid string // Session user agent. UserAgent string } // ClusterReq is either a Proxy to Master or Topic Proxy to Topic Master or intra-cluster routing request message. type ClusterReq struct { // Name of the node sending this request Node string // Ring hash signature of the node sending this request // Signature must match the signature of the receiver, otherwise the // Cluster is desynchronized. Signature string // Fingerprint of the node sending this request. // Fingerprint changes when the node is restarted. Fingerprint int64 // Type of request. ReqType ProxyReqType // Client message. Set for C2S requests. CliMsg *ClientComMessage // Message to be routed. Set for intra-cluster route requests. SrvMsg *ServerComMessage // Expanded (routable) topic name RcptTo string // Originating session Sess *ClusterSess // True when the topic proxy is gone. Gone bool } // ClusterRoute is intra-cluster routing request message. type ClusterRoute struct { // Name of the node sending this request Node string // Ring hash signature of the node sending this request // Signature must match the signature of the receiver, otherwise the // Cluster is desynchronized. Signature string // Fingerprint of the node sending this request. // Fingerprint changes when the node is restarted. Fingerprint int64 // Message to be routed. Set for intra-cluster route requests. SrvMsg *ServerComMessage // Originating session Sess *ClusterSess } // ClusterResp is a Master to Proxy response message. type ClusterResp struct { // Server message with the response. SrvMsg *ServerComMessage // Originating session ID to forward response to, if any. OrigSid string // Expanded (routable) topic name RcptTo string // Parameters sent back by the topic master in response a topic proxy request. // Original request type. OrigReqType ProxyReqType } // ClusterPing is used to detect node restarts. type ClusterPing struct { // Name of the node sending this request. Node string // Fingerprint of the node sending this request. // Fingerprint changes when the node restarts. Fingerprint int64 } // Handle outbound node communication: read messages from the channel, forward to remote nodes. // FIXME(gene): this will drain the outbound queue in case of a failure: all unprocessed messages will be dropped. // Maybe it's a good thing, maybe not. func (n *ClusterNode) reconnect() { var reconnTicker *time.Ticker // Avoid parallel reconnection threads. n.lock.Lock() if n.reconnecting { n.lock.Unlock() return } n.reconnecting = true n.lock.Unlock() count := 0 for { // Attempt to reconnect right away if conn, err := net.DialTimeout("tcp", n.address, clusterNetworkTimeout); err == nil { if reconnTicker != nil { reconnTicker.Stop() } n.lock.Lock() n.endpoint = rpc.NewClient(conn) n.connected = true n.reconnecting = false n.lock.Unlock() statsInc("LiveClusterNodes", 1) logs.Info.Println("cluster: connected to", n.name) // Send this node credentials to the new node. var unused bool n.call("Cluster.Ping", &ClusterPing{ Node: globals.cluster.thisNodeName, Fingerprint: globals.cluster.fingerprint, }, &unused) return } else if count == 0 { reconnTicker = time.NewTicker(clusterDefaultReconnectTime) } count++ select { case <-reconnTicker.C: // Wait for timer to try to reconnect again. Do nothing if the timer is inactive. case <-n.done: // Shutting down logs.Info.Println("cluster: shutdown started at node", n.name) reconnTicker.Stop() if n.endpoint != nil { n.endpoint.Close() } n.lock.Lock() n.connected = false n.reconnecting = false n.lock.Unlock() logs.Info.Println("cluster: shut down completed at node", n.name) return } } } func (n *ClusterNode) call(proc string, req, resp any) error { if !n.connected { return errors.New("cluster: node '" + n.name + "' not connected") } if err := n.endpoint.Call(proc, req, resp); err != nil { logs.Warn.Println("cluster: call failed", n.name, err) n.lock.Lock() if n.connected { n.endpoint.Close() n.connected = false statsInc("LiveClusterNodes", -1) go n.reconnect() } n.lock.Unlock() return err } return nil } func (n *ClusterNode) handleRpcResponse(call *rpc.Call) { if call.Error != nil { logs.Warn.Printf("cluster: %s call failed: %s", call.ServiceMethod, call.Error) n.lock.Lock() if n.connected { n.endpoint.Close() n.connected = false statsInc("LiveClusterNodes", -1) go n.reconnect() } n.lock.Unlock() } } func (n *ClusterNode) callAsync(proc string, req, resp any, done chan *rpc.Call) *rpc.Call { if done != nil && cap(done) == 0 { logs.Err.Panic("cluster: RPC done channel is unbuffered") } if !n.connected { call := &rpc.Call{ ServiceMethod: proc, Args: req, Reply: resp, Error: errors.New("cluster: node '" + n.name + "' not connected"), Done: done, } if done != nil { done <- call } return call } var responseChan chan *rpc.Call if done != nil { // Make a separate response callback if we need to notify the caller. myDone := make(chan *rpc.Call, 1) go func() { call := <-myDone n.handleRpcResponse(call) if done != nil { done <- call } }() responseChan = myDone } else { responseChan = n.rpcDone } call := n.endpoint.Go(proc, req, resp, responseChan) return call } // proxyToMaster forwards request from topic proxy to topic master. func (n *ClusterNode) proxyToMaster(msg *ClusterReq) error { msg.Node = globals.cluster.thisNodeName var rejected bool err := n.call("Cluster.TopicMaster", msg, &rejected) if err == nil && rejected { err = errors.New("cluster: topic master node out of sync") } return err } // proxyToMaster forwards request from topic proxy to topic master. func (n *ClusterNode) proxyToMasterAsync(msg *ClusterReq) error { select { case n.p2mSender <- msg: return nil default: } // Buffer is full. Wait briefly before giving up. timer := time.NewTimer(clusterP2MTimeout) defer timer.Stop() select { case n.p2mSender <- msg: return nil case <-timer.C: return errors.New("cluster: load exceeded") } } // masterToProxyAsync forwards response from topic master to topic proxy // in a fire-and-forget manner. func (n *ClusterNode) masterToProxyAsync(msg *ClusterResp) error { var unused bool if c := n.callAsync("Cluster.TopicProxy", msg, &unused, nil); c.Error != nil { return c.Error } return nil } // route routes server message within the cluster. func (n *ClusterNode) route(msg *ClusterRoute) error { var unused bool return n.call("Cluster.Route", msg, &unused) } // Cluster is the representation of the cluster. type Cluster struct { // Cluster nodes with RPC endpoints (excluding current node). nodes map[string]*ClusterNode // Name of the local node thisNodeName string // Fingerprint of the local node fingerprint int64 // Resolved address to listed on listenOn string // Socket for inbound connections inbound *net.TCPListener // Ring hash for mapping topic names to nodes ring *rh.Ring // Failover parameters. Could be nil if failover is not enabled fo *clusterFailover // Thread pool to use for running proxy session (write) event processing logic. // The number of proxy sessions grows as O(number of topics x number of cluster nodes). // In large Tinode deployments (10s of thousands of topics, tens of nodes), // running a separate event processing goroutine for each proxy session // leads to a rather large memory usage and excessive scheduling overhead. proxyEventQueue *concurrency.GoRoutinePool } func (n *ClusterNode) stopMultiplexingSession(msess *Session) { if msess == nil { return } msess.stopSession(nil) n.lock.Lock() delete(n.msess, msess.sid) n.lock.Unlock() } // TopicMaster is a gRPC endpoint which receives requests sent by proxy topic to master topic. func (c *Cluster) TopicMaster(msg *ClusterReq, rejected *bool) error { *rejected = false node := c.nodes[msg.Node] if node == nil { logs.Warn.Println("cluster TopicMaster: request from an unknown node", msg.Node) return nil } // Master maintains one multiplexing session per proxy topic per node. // Except channel topics: // * one multiplexing session for channel subscriptions. // * one multiplexing session for group subscriptions. var msid string if msg.CliMsg != nil && types.IsChannel(msg.CliMsg.Original) { // If it's a channel request, use channel name. msid = msg.CliMsg.Original } else { msid = msg.RcptTo } // Append node name. msid += "-" + msg.Node msess := globals.sessionStore.Get(msid) if msg.Gone { // Proxy topic is gone. Tear down the local auxiliary session. // If it was the last session, master topic will shut down as well. node.stopMultiplexingSession(msess) if t := globals.hub.topicGet(msg.RcptTo); t != nil && t.isChan { // If it's a channel topic, also stop the "chnX-" local auxiliary session. msidChn := types.GrpToChn(t.name) + "-" + msg.Node node.stopMultiplexingSession(globals.sessionStore.Get(msidChn)) } return nil } if msg.Signature != c.ring.Signature() { logs.Warn.Println("cluster TopicMaster: session signature mismatch", msg.RcptTo) *rejected = true return nil } // Create a new multiplexing session if needed. if msess == nil { // If the session is not found, create it. var count int msess, count = globals.sessionStore.NewSession(node, msid) node.lock.Lock() node.msess[msid] = struct{}{} node.lock.Unlock() logs.Info.Println("cluster: multiplexing session started", msid, count) msess.proxiedTopic = msg.RcptTo } // This is a local copy of a remote session. var sess *Session // Sess is nil for user agent changes and deferred presence notification requests. if msg.Sess != nil { // We only need some session info. No need to copy everything. sess = &Session{ proto: PROXY, // Multiplexing session which actually handles the communication. multi: msess, // Local parameters specific to this session. sid: msg.Sess.Sid, userAgent: msg.Sess.UserAgent, remoteAddr: msg.Sess.RemoteAddr, lang: msg.Sess.Lang, countryCode: msg.Sess.CountryCode, proxyReq: msg.ReqType, background: msg.Sess.Background, uid: msg.Sess.Uid, } } if msg.CliMsg != nil { msg.CliMsg.sess = sess msg.CliMsg.init = true } switch msg.ReqType { case ProxyReqJoin: select { case globals.hub.join <- msg.CliMsg: default: // Reply with a 500 to the user. sess.queueOut(ErrUnknownReply(msg.CliMsg, msg.CliMsg.Timestamp)) logs.Warn.Println("cluster: join req failed - hub.join queue full, topic ", msg.CliMsg.RcptTo, "; orig sid ", sess.sid) } case ProxyReqLeave: if t := globals.hub.topicGet(msg.RcptTo); t != nil { t.unreg <- msg.CliMsg } else { logs.Warn.Println("cluster: leave request for unknown topic", msg.RcptTo) } case ProxyReqMeta: if t := globals.hub.topicGet(msg.RcptTo); t != nil { select { case t.meta <- msg.CliMsg: default: sess.queueOut(ErrUnknownReply(msg.CliMsg, msg.CliMsg.Timestamp)) logs.Warn.Println("cluster: meta req failed - topic.meta queue full, topic ", msg.CliMsg.RcptTo, "; orig sid ", sess.sid) } } else { logs.Warn.Println("cluster: meta request for unknown topic", msg.RcptTo) } case ProxyReqBroadcast: select { case globals.hub.routeCli <- msg.CliMsg: default: logs.Err.Println("cluster: route req failed - hub.route queue full") } case ProxyReqBgSession, ProxyReqMeUserAgent: // sess could be nil if t := globals.hub.topicGet(msg.RcptTo); t != nil { if t.supd == nil { logs.Err.Panicln("cluster: invalid topic category in session update", t.name, msg.ReqType) } su := &sessionUpdate{} if msg.ReqType == ProxyReqBgSession { su.sess = sess } else { su.userAgent = sess.userAgent } t.supd <- su } else { logs.Warn.Println("cluster: session update for unknown topic", msg.RcptTo, msg.ReqType) } default: logs.Warn.Println("cluster: unknown request type", msg.ReqType, msg.RcptTo) *rejected = true } return nil } // TopicProxy is a gRPC endpoint at topic proxy which receives topic master responses. func (Cluster) TopicProxy(msg *ClusterResp, unused *bool) error { // This cluster member received a response from the topic master to be forwarded to the topic. // Find appropriate topic, send the message to it. if t := globals.hub.topicGet(msg.RcptTo); t != nil { msg.SrvMsg.uid = types.ParseUserId(msg.SrvMsg.AsUser) select { case t.proxy <- msg: default: logs.Warn.Printf("cluster: proxy channel full, topic %s", msg.RcptTo) } } else { logs.Warn.Println("cluster: master response for unknown topic", msg.RcptTo) } return nil } // Route endpoint receives intra-cluster messages destined for the nodes hosting the topic. // Called by Hub.route channel consumer for messages send without attaching to topic first. func (c *Cluster) Route(msg *ClusterRoute, rejected *bool) error { logError := func(err string) { sid := "" if msg.Sess != nil { sid = msg.Sess.Sid } logs.Warn.Println(err, sid) *rejected = true } *rejected = false if msg.Signature != c.ring.Signature() { logError("cluster Route: session signature mismatch") return nil } if msg.SrvMsg == nil { // TODO: maybe panic here. logError("cluster Route: nil server message") return nil } select { case globals.hub.routeSrv <- msg.SrvMsg: default: logError("cluster Route: server busy") } return nil } // User cache & push notifications management. These are calls received by the Master from Proxy. // The Proxy expects no payload to be returned by the master. // UserCacheUpdate endpoint receives updates to user's cached values as well as sends push notifications. func (c *Cluster) UserCacheUpdate(msg *UserCacheReq, rejected *bool) error { if msg.Gone { // User is deleted. Evict all user's sessions. globals.sessionStore.EvictUser(msg.UserId, "") if globals.cluster.isRemoteTopic(msg.UserId.UserId()) { // No need to delete user's cache if user is remote. return nil } } usersRequestFromCluster(msg) return nil } // Ping is a gRPC endpoint which receives ping requests from peer nodes.Used to detect node restarts. func (c *Cluster) Ping(ping *ClusterPing, unused *bool) error { node := c.nodes[ping.Node] if node == nil { logs.Warn.Println("cluster Ping from unknown node", ping.Node) return nil } if node.fingerprint == 0 { // This is the first connection to remote node. node.fingerprint = ping.Fingerprint } else if node.fingerprint != ping.Fingerprint { // Remote node restarted. node.fingerprint = ping.Fingerprint c.invalidateProxySubs(ping.Node) c.gcProxySessionsForNode(ping.Node) } return nil } // Sends user cache update to user's Master node where the cache actually resides. // The request is extected to contain users who reside at remote nodes only. func (c *Cluster) routeUserReq(req *UserCacheReq) error { // Index requests by cluster node. reqByNode := make(map[string]*UserCacheReq) if req.PushRcpt != nil { // Request to send push notifications. Create separate packets for each affected cluster node. for uid, recipient := range req.PushRcpt.To { n := c.nodeForTopic(uid.UserId()) if n == nil { return errors.New("attempt to update user at a non-existent node (1)") } r := reqByNode[n.name] if r == nil { r = &UserCacheReq{ PushRcpt: &push.Receipt{ Payload: req.PushRcpt.Payload, To: make(map[types.Uid]push.Recipient), }, Node: c.thisNodeName, } } r.PushRcpt.To[uid] = recipient reqByNode[n.name] = r } } else if len(req.UserIdList) > 0 { // Request to add/remove some users from cache. for _, uid := range req.UserIdList { n := c.nodeForTopic(uid.UserId()) if n == nil { return errors.New("attempt to update user at a non-existent node (2)") } r := reqByNode[n.name] if r == nil { r = &UserCacheReq{Node: c.thisNodeName, Inc: req.Inc} } r.UserIdList = append(r.UserIdList, uid) reqByNode[n.name] = r } } else if req.Gone { // Message that the user is deleted is sent to all nodes. r := &UserCacheReq{Node: c.thisNodeName, UserIdList: req.UserIdList, Gone: true} for _, n := range c.nodes { reqByNode[n.name] = r } } if len(reqByNode) > 0 { for nodeName, r := range reqByNode { n := c.nodes[nodeName] var rejected bool err := n.call("Cluster.UserCacheUpdate", r, &rejected) if rejected { err = errors.New("master node out of sync") } if err != nil { return err } } return nil } // Update to cached values. n := c.nodeForTopic(req.UserId.UserId()) if n == nil { return errors.New("attempt to update user at a non-existent node (3)") } req.Node = c.thisNodeName var rejected bool err := n.call("Cluster.UserCacheUpdate", req, &rejected) if rejected { err = errors.New("master node out of sync") } return err } // Given topic name, find appropriate cluster node to route message to. func (c *Cluster) nodeForTopic(topic string) *ClusterNode { key := c.ring.Get(topic) if key == c.thisNodeName { logs.Err.Println("cluster: request to route to self") // Do not route to self return nil } node := c.nodes[key] if node == nil { logs.Warn.Println("cluster: no node for topic", topic, key) } return node } // isRemoteTopic checks if the given topic is handled by this node or a remote node. func (c *Cluster) isRemoteTopic(topic string) bool { if c == nil { // Cluster not initialized, all topics are local return false } return c.ring.Get(topic) != c.thisNodeName } // genLocalTopicName is just like genTopicName(), but the generated name belongs to the current cluster node. func (c *Cluster) genLocalTopicName() string { topic := genTopicName() if c == nil { // Cluster not initialized, all topics are local return topic } // TODO: if cluster is large it may become too inefficient. for c.ring.Get(topic) != c.thisNodeName { topic = genTopicName() } return topic } // isPartitioned checks if the cluster is partitioned due to network or other failure and if the // current node is a part of the smaller partition. func (c *Cluster) isPartitioned() bool { if c == nil || c.fo == nil { // Cluster not initialized or failover disabled therefore not partitioned. return false } c.fo.activeNodesLock.RLock() result := (len(c.nodes)+1)/2 >= len(c.fo.activeNodes) c.fo.activeNodesLock.RUnlock() return result } func (c *Cluster) makeClusterReq(reqType ProxyReqType, msg *ClientComMessage, topic string, sess *Session) *ClusterReq { req := &ClusterReq{ Node: c.thisNodeName, Signature: c.ring.Signature(), Fingerprint: c.fingerprint, ReqType: reqType, RcptTo: topic, } var uid types.Uid if msg != nil { req.CliMsg = msg uid = types.ParseUserId(req.CliMsg.AsUser) } if sess != nil { if uid.IsZero() { uid = sess.uid } req.Sess = &ClusterSess{ Uid: uid, AuthLvl: sess.authLvl, RemoteAddr: sess.remoteAddr, UserAgent: sess.userAgent, Ver: sess.ver, Lang: sess.lang, CountryCode: sess.countryCode, DeviceID: sess.deviceID, Platform: sess.platf, Sid: sess.sid, Background: sess.background, } } return req } // Forward client request message from the Topic Proxy to the Topic Master (cluster node which owns the topic). func (c *Cluster) routeToTopicMaster(reqType ProxyReqType, msg *ClientComMessage, topic string, sess *Session) error { if c == nil { // Cluster may be nil due to shutdown. return nil } if sess != nil && reqType != ProxyReqLeave { if atomic.LoadInt32(&sess.terminating) > 0 { // The session is terminating. // Do not forward any requests except "leave" to the topic master. return nil } } req := c.makeClusterReq(reqType, msg, topic, sess) // Find the cluster node which owns the topic, then forward to it. n := c.nodeForTopic(topic) if n == nil { return errors.New("node for topic not found") } return n.proxyToMasterAsync(req) } // Forward server response message to the node that owns topic. func (c *Cluster) routeToTopicIntraCluster(topic string, msg *ServerComMessage, sess *Session) error { if c == nil { // Cluster may be nil due to shutdown. return nil } n := c.nodeForTopic(topic) if n == nil { return errors.New("node for topic not found (intra)") } route := &ClusterRoute{ Node: c.thisNodeName, Signature: c.ring.Signature(), Fingerprint: c.fingerprint, SrvMsg: msg, } if sess != nil { route.Sess = &ClusterSess{Sid: sess.sid} } return n.route(route) } // Topic proxy terminated. Inform remote Master node that the proxy is gone. func (c *Cluster) topicProxyGone(topicName string) error { if c == nil { // Cluster may be nil due to shutdown. return nil } // Find the cluster node which owns the topic, then forward to it. n := c.nodeForTopic(topicName) if n == nil { return errors.New("node for topic not found") } req := c.makeClusterReq(ProxyReqLeave, nil, topicName, nil) req.Gone = true return n.proxyToMasterAsync(req) } // Returns snowflake worker id. func clusterInit(configString json.RawMessage, self *string) int { if globals.cluster != nil { logs.Err.Fatal("Cluster already initialized.") } // Registering variables even if it's a standalone server. Otherwise monitoring software will // complain about missing vars. // 1 if this node is cluster leader, 0 otherwise statsRegisterInt("ClusterLeader") // Total number of nodes configured statsRegisterInt("TotalClusterNodes") // Number of nodes currently believed to be up. statsRegisterInt("LiveClusterNodes") // This is a standalone server, not initializing if len(configString) == 0 { logs.Info.Println("Cluster: running as a standalone server.") return 1 } var config clusterConfig if err := json.Unmarshal(configString, &config); err != nil { logs.Err.Fatal(err) } thisName := *self if thisName == "" { thisName = config.ThisName } // Name of the current node is not specified: clustering disabled. if thisName == "" { logs.Info.Println("Cluster: running as a standalone server.") return 1 } gob.Register([]any{}) gob.Register(map[string]any{}) gob.Register(map[string]int{}) gob.Register(map[string]string{}) gob.Register(MsgAccessMode{}) if config.NumProxyEventGoRoutines != 0 { logs.Warn.Println("Cluster config: field num_proxy_event_goroutines is deprecated.") } globals.cluster = &Cluster{ thisNodeName: thisName, fingerprint: time.Now().Unix(), nodes: make(map[string]*ClusterNode), proxyEventQueue: concurrency.NewGoRoutinePool(len(config.Nodes) * 5), } var nodeNames []string for _, host := range config.Nodes { nodeNames = append(nodeNames, host.Name) if host.Name == thisName { globals.cluster.listenOn = host.Addr // Don't create a cluster member for this local instance continue } globals.cluster.nodes[host.Name] = &ClusterNode{ address: host.Addr, name: host.Name, done: make(chan bool, 1), msess: make(map[string]struct{}), } } if len(globals.cluster.nodes) == 0 { // Cluster needs at least two nodes. logs.Err.Fatal("Cluster: invalid cluster size: 1") } if len(globals.cluster.nodes)%2 == 1 { // Even number of cluster nodes (self + odd number). logs.Warn.Println("Cluster: use odd number of cluster nodes") } if !globals.cluster.failoverInit(config.Failover) { globals.cluster.rehash(nil) } sort.Strings(nodeNames) workerId := sort.SearchStrings(nodeNames, thisName) + 1 statsSet("TotalClusterNodes", int64(len(globals.cluster.nodes)+1)) return workerId } // Proxied session is being closed at the Master node. func (sess *Session) closeRPC() { if sess.isMultiplex() { logs.Info.Println("cluster: session proxy closed", sess.sid) } } // Start accepting connections. func (c *Cluster) start() { addr, err := net.ResolveTCPAddr("tcp", c.listenOn) if err != nil { logs.Err.Fatal(err) } c.inbound, err = net.ListenTCP("tcp", addr) if err != nil { logs.Err.Fatal(err) } var bufferSize = clusterProxyToMasterBuffer if len(c.nodes) > 2 { // Expand the buffer for larger (>3 node) clusters. bufferSize += clusterProxyToMasterBufferPerNode * (len(c.nodes) - 2) } for _, n := range c.nodes { go n.reconnect() n.rpcDone = make(chan *rpc.Call, len(c.nodes)*clusterRpcCompletionBuffer) n.p2mSender = make(chan *ClusterReq, bufferSize) go n.asyncRpcLoop() go n.p2mSenderLoop() } if c.fo != nil { go c.run() } err = rpc.Register(c) if err != nil { logs.Err.Fatal(err) } go rpc.Accept(c.inbound) logs.Info.Printf("Cluster of %d nodes initialized, node '%s' is listening on [%s]", len(globals.cluster.nodes)+1, globals.cluster.thisNodeName, c.listenOn) } func (c *Cluster) shutdown() { if globals.cluster == nil { return } for _, n := range c.nodes { close(n.rpcDone) close(n.p2mSender) } globals.cluster.proxyEventQueue.Stop() globals.cluster = nil c.inbound.Close() if c.fo != nil { c.fo.done <- true } for _, n := range c.nodes { n.done <- true } logs.Info.Println("Cluster shut down") } // Recalculate the ring hash using provided list of nodes or only nodes in a non-failed state. // Returns the list of nodes used for ring hash. func (c *Cluster) rehash(nodes []string) []string { ring := rh.New(clusterHashReplicas, nil) var ringKeys []string if nodes == nil { for _, node := range c.nodes { ringKeys = append(ringKeys, node.name) } ringKeys = append(ringKeys, c.thisNodeName) } else { ringKeys = append(ringKeys, nodes...) } ring.Add(ringKeys...) c.ring = ring return ringKeys } // invalidateProxySubs iterates over sessions proxied on this node and for each session // sends "{pres term}" informing that the topic subscription (attachment) was lost: // - Called immediately after Cluster.rehash() for all relocated topics (forNode == ""). // - Called for topics hosted at a specific node when a node restart is detected. // TODO: consider resubscribing to topics instead of forcing sessions to resubscribe. func (c *Cluster) invalidateProxySubs(forNode string) { sessions := make(map[*Session][]string) globals.hub.topics.Range(func(_, v any) bool { topic := v.(*Topic) if !topic.isProxy { // Topic isn't a proxy. return true } if forNode == "" { if topic.masterNode == c.ring.Get(topic.name) { // The topic hasn't moved. Continue. return true } } else if topic.masterNode != forNode { // The topic is hosted at a different node than the restarted node. return true } for s, psd := range topic.sessions { // FIXME: 'me' topic must be the last one in the list for each topic. sessions[s] = append(sessions[s], topicNameForUser(topic.name, psd.uid, psd.isChanSub)) } return true }) for s, topicsToTerminate := range sessions { s.presTermDirect(topicsToTerminate) } } // gcProxySessions terminates orphaned proxy sessions at a master node for all lost nodes (allNodes minus activeNodes). // The session is orphaned when the origin node is gone. func (c *Cluster) gcProxySessions(activeNodes []string) { allNodes := []string{c.thisNodeName} for name := range c.nodes { allNodes = append(allNodes, name) } _, failedNodes, _ := stringSliceDelta(allNodes, activeNodes) for _, node := range failedNodes { // Iterate sessions of a failed node c.gcProxySessionsForNode(node) } } // gcProxySessionsForNode terminates orphaned proxy sessions at a master node for the given node. // For example, a remote node is restarted or the cluster is rehashed without the node. func (c *Cluster) gcProxySessionsForNode(node string) { n := c.nodes[node] n.lock.Lock() msess := n.msess n.msess = make(map[string]struct{}) n.lock.Unlock() for sid := range msess { if sess := globals.sessionStore.Get(sid); sess != nil { sess.stop <- nil } } } // clusterWriteLoop implements write loop for multiplexing (proxy) session at a node which hosts master topic. // The session is a multiplexing session, i.e. it handles requests for multiple sessions at origin. func (sess *Session) clusterWriteLoop(forTopic string) { terminate := true defer func() { if terminate { sess.closeRPC() globals.sessionStore.Delete(sess) sess.inflightReqs = nil sess.unsubAll() } }() for { select { case msg, ok := <-sess.send: if !ok || sess.clnode.endpoint == nil { // channel closed return } srvMsg := msg.(*ServerComMessage) response := &ClusterResp{SrvMsg: srvMsg} if srvMsg.sess == nil { response.OrigSid = "*" } else { response.OrigReqType = srvMsg.sess.proxyReq response.OrigSid = srvMsg.sess.sid srvMsg.AsUser = srvMsg.sess.uid.UserId() switch srvMsg.sess.proxyReq { case ProxyReqJoin, ProxyReqLeave, ProxyReqMeta, ProxyReqBgSession, ProxyReqMeUserAgent, ProxyReqCall: // Do nothing case ProxyReqBroadcast, ProxyReqNone: if srvMsg.Data != nil || srvMsg.Pres != nil || srvMsg.Info != nil { response.OrigSid = "*" } else if srvMsg.Ctrl == nil { logs.Warn.Println("cluster: request type not set in clusterWriteLoop", sess.sid, srvMsg.describe(), "src_sid:", srvMsg.sess.sid) } default: logs.Err.Panicln("cluster: unknown request type in clusterWriteLoop", srvMsg.sess.proxyReq) } } srvMsg.RcptTo = forTopic response.RcptTo = forTopic if err := sess.clnode.masterToProxyAsync(response); err != nil { logs.Warn.Printf("cluster: response to proxy failed \"%s\": %s", sess.sid, err.Error()) return } case msg := <-sess.stop: if msg == nil { // Terminating multiplexing session. return } // There are two cases of msg != nil: // * user is being deleted // * node shutdown // In both cases the msg does not need to be forwarded to the proxy. case <-sess.detach: return default: terminate = false return } } } ================================================ FILE: server/cluster_leader.go ================================================ package main import ( "math/rand" "net/rpc" "sync" "time" "github.com/tinode/chat/server/logs" ) // Cluster methods related to leader node election. Based on ideas from Raft protocol. // The leader node issues heartbeats to follower nodes. If the follower node fails enough // times, the leader node annouces it dead and initiates rehashing: it regenerates ring hash with // only live nodes and communicates the new list of nodes to followers. They in turn do their // rehashing using the new list. When the dead node is revived, rehashing happens again. // Failover config. type clusterFailover struct { // Current leader leader string // Current election term term int // Hearbeat interval heartBeat time.Duration // Vote timeout: the number of missed heartbeats before a new election is initiated. voteTimeout int // The list of nodes the leader considers active activeNodes []string activeNodesLock sync.RWMutex // The number of heartbeats a node can fail before being declared dead nodeFailCountLimit int // Channel for processing leader health checks. healthCheck chan *ClusterHealth // Channel for processing election votes. electionVote chan *ClusterVote // Channel for stopping the failover runner. done chan bool } type clusterFailoverConfig struct { // Failover is enabled Enabled bool `json:"enabled"` // Time in milliseconds between heartbeats Heartbeat int `json:"heartbeat"` // Number of failed heartbeats before a leader election is initiated. VoteAfter int `json:"vote_after"` // Number of failures before a node is considered dead NodeFailAfter int `json:"node_fail_after"` } // ClusterHealth is content of a leader's health check of a follower node. type ClusterHealth struct { // Name of the leader node Leader string // Election term Term int // Ring hash signature that represents the cluster Signature string // Names of nodes currently active in the cluster Nodes []string } // ClusterVoteRequest is a request from a leader candidate to a node to vote for the candidate. type ClusterVoteRequest struct { // Candidate node which issued this request Node string // Election term Term int } // ClusterVoteResponse is a vote from a node. type ClusterVoteResponse struct { // Actual vote Result bool // Node's term after the vote Term int } // ClusterVote is a vote request and a response in leader election. type ClusterVote struct { req *ClusterVoteRequest resp chan ClusterVoteResponse } func (c *Cluster) failoverInit(config *clusterFailoverConfig) bool { if config == nil || !config.Enabled { return false } if len(c.nodes) < 2 { logs.Err.Printf("cluster: failover disabled; need at least 3 nodes, got %d", len(c.nodes)+1) return false } // Generate ring hash on the assumption that all nodes are alive and well. // This minimizes rehashing during normal operations. var activeNodes []string for _, node := range c.nodes { activeNodes = append(activeNodes, node.name) } activeNodes = append(activeNodes, c.thisNodeName) c.rehash(activeNodes) // Random heartbeat ticker: 0.75 * config.HeartBeat + random(0, 0.5 * config.HeartBeat). // The PRNG is initialized in main.go. hb := time.Duration(config.Heartbeat) * time.Millisecond hb = (hb >> 1) + (hb >> 2) + time.Duration(rand.Intn(int(hb>>1))) c.fo = &clusterFailover{ activeNodes: activeNodes, heartBeat: hb, voteTimeout: config.VoteAfter, nodeFailCountLimit: config.NodeFailAfter, healthCheck: make(chan *ClusterHealth, config.VoteAfter), electionVote: make(chan *ClusterVote, len(c.nodes)), done: make(chan bool, 1), } logs.Info.Println("cluster: failover mode enabled") return true } // Health is called by the leader node to assert leadership and check status // of the followers. func (c *Cluster) Health(health *ClusterHealth, unused *bool) error { select { case c.fo.healthCheck <- health: default: } return nil } // Vote processes request for a vote from a candidate. func (c *Cluster) Vote(vreq *ClusterVoteRequest, response *ClusterVoteResponse) error { respChan := make(chan ClusterVoteResponse, 1) c.fo.electionVote <- &ClusterVote{ req: vreq, resp: respChan, } *response = <-respChan return nil } // Cluster leader checks health of follower nodes. func (c *Cluster) sendHealthChecks() { rehash := false for _, node := range c.nodes { unused := false err := node.call("Cluster.Health", &ClusterHealth{ Leader: c.thisNodeName, Term: c.fo.term, Signature: c.ring.Signature(), Nodes: c.fo.activeNodes, }, &unused) if err != nil { node.failCount++ if node.failCount == c.fo.nodeFailCountLimit { // Node failed too many times rehash = true } } else { if node.failCount >= c.fo.nodeFailCountLimit { // Node has recovered rehash = true } node.failCount = 0 } } if rehash { activeNodes := []string{c.thisNodeName} for _, node := range c.nodes { if node.failCount < c.fo.nodeFailCountLimit { activeNodes = append(activeNodes, node.name) } } c.fo.activeNodesLock.Lock() c.fo.activeNodes = activeNodes c.fo.activeNodesLock.Unlock() c.rehash(activeNodes) c.invalidateProxySubs("") c.gcProxySessions(activeNodes) logs.Info.Println("cluster: initiating failover rehash for nodes", activeNodes) globals.hub.rehash <- true } } func (c *Cluster) electLeader() { // Increment the term (voting for myself in this term) and clear the leader c.fo.term++ c.fo.leader = "" // Make sure the current node does not report itself as a leader. statsSet("ClusterLeader", 0) logs.Info.Println("cluster: leading new election for term", c.fo.term) nodeCount := len(c.nodes) // Number of votes needed to elect the leader expectVotes := (nodeCount+1)>>1 + 1 done := make(chan *rpc.Call, nodeCount) // Send async requests for votes to other nodes for _, node := range c.nodes { response := ClusterVoteResponse{} node.callAsync("Cluster.Vote", &ClusterVoteRequest{ Node: c.thisNodeName, Term: c.fo.term, }, &response, done) } // Number of votes received (1 vote for self) voteCount := 1 timeout := time.NewTimer(c.fo.heartBeat>>1 + c.fo.heartBeat) // Wait for one of the following // 1. More than half of the nodes voting in favor // 2. All nodes responded. // 3. Timeout. for i := 0; i < nodeCount && voteCount < expectVotes; { select { case call := <-done: if call.Error == nil { if call.Reply.(*ClusterVoteResponse).Result { // Vote in my favor voteCount++ } else if c.fo.term < call.Reply.(*ClusterVoteResponse).Term { // Vote against me. Abandon vote: this node's term is behind the cluster i = nodeCount voteCount = 0 } } i++ case <-timeout.C: // break the loop i = nodeCount } } if voteCount >= expectVotes { // Current node elected as the leader. c.fo.leader = c.thisNodeName statsSet("ClusterLeader", 1) logs.Info.Printf("'%s' elected self as a new leader", c.thisNodeName) } } // Go routine that processes calls related to leader election and maintenance. func (c *Cluster) run() { ticker := time.NewTicker(c.fo.heartBeat) // Count of missed health checks from the leader. missed := 0 // Don't rehash immediately on the first missed health check. If this node just came online, leader will // account it on the next check. Otherwise it will be rehashing twice. rehashSkipped := false for { select { case <-ticker.C: if c.fo.leader == c.thisNodeName { // I'm the leader, send the health checks to followers. c.sendHealthChecks() } else { // Increment the number of missed health checks from the leader. // The counter will be reset to zero when a health check is received. missed++ if missed >= c.fo.voteTimeout { // Leader is gone, initiate election of a new leader. missed = 0 c.electLeader() } } case health := <-c.fo.healthCheck: // Health check from the leader. if health.Term < c.fo.term { // This is a health check from a stale leader. Ignore. logs.Warn.Println("cluster: health check from a stale leader", health.Term, c.fo.term, health.Leader, c.fo.leader) continue } if health.Term > c.fo.term { c.fo.term = health.Term c.fo.leader = health.Leader logs.Info.Printf("cluster: leader '%s' elected", c.fo.leader) } else if health.Leader != c.fo.leader { if c.fo.leader != "" { // Wrong leader. It's a bug, should never happen! logs.Err.Printf("cluster: wrong leader '%s' while expecting '%s'; term %d", health.Leader, c.fo.leader, health.Term) } else { logs.Info.Printf("cluster: leader set to '%s'", health.Leader) } c.fo.leader = health.Leader } // This is a health check from a leader, consequently this node is not the leader. statsSet("ClusterLeader", 0) missed = 0 if health.Signature != c.ring.Signature() { if rehashSkipped { logs.Info.Println("cluster: rehashing at a request of", health.Leader, health.Nodes, health.Signature, c.ring.Signature()) c.rehash(health.Nodes) c.invalidateProxySubs("") c.gcProxySessions(health.Nodes) rehashSkipped = false globals.hub.rehash <- true } else { rehashSkipped = true } } case vreq := <-c.fo.electionVote: if c.fo.term < vreq.req.Term { // This is a new election. This node has not voted yet. Vote for the requestor and // clear the current leader. logs.Info.Printf("Voting YES for %s, my term %d, vote term %d", vreq.req.Node, c.fo.term, vreq.req.Term) c.fo.term = vreq.req.Term c.fo.leader = "" // Election means these is no leader yet. statsSet("ClusterLeader", 0) vreq.resp <- ClusterVoteResponse{Result: true, Term: c.fo.term} } else { // This node has voted already or stale election, reject. logs.Info.Printf("Voting NO for %s, my term %d, vote term %d", vreq.req.Node, c.fo.term, vreq.req.Term) vreq.resp <- ClusterVoteResponse{Result: false, Term: c.fo.term} } case <-c.fo.done: return } } } ================================================ FILE: server/concurrency/goroutinepool.go ================================================ // Package concurrency is a very simple implementation of a mutex with channels. // Provides TryLock functionality absent in Go's regular sync.Mutex. // See https://github.com/golang/go/issues/6123 for details. package concurrency // Task represents a work task to be run on the specified thread pool. type Task func() // GoRoutinePool is a pull of Go routines with associated locking mechanism. type GoRoutinePool struct { // Work queue. work chan Task // Counter to control the number of already allocated/running goroutines. sem chan struct{} // Exit knob. stop chan struct{} } // NewGoRoutinePool allocates a new thread pool with `numWorkers` goroutines. func NewGoRoutinePool(numWorkers int) *GoRoutinePool { return &GoRoutinePool{ work: make(chan Task), sem: make(chan struct{}, numWorkers), stop: make(chan struct{}, numWorkers), } } // Schedule enqueus a closure to run on the GoRoutinePool's goroutines. func (p *GoRoutinePool) Schedule(task Task) { select { case p.work <- task: case p.sem <- struct{}{}: go p.worker(task) } } // Stop sends a stop signal to all running goroutines. func (p *GoRoutinePool) Stop() { numWorkers := cap(p.sem) for range numWorkers { p.stop <- struct{}{} } } // Thread pool worker goroutine. func (p *GoRoutinePool) worker(task Task) { defer func() { <-p.sem }() for { task() select { case task = <-p.work: case <-p.stop: return } } } ================================================ FILE: server/concurrency/simplemutex.go ================================================ package concurrency // SimpleMutex is a channel used for locking. type SimpleMutex chan struct{} // NewSimpleMutex creates and returns a new SimpleMutex object. func NewSimpleMutex() SimpleMutex { return make(SimpleMutex, 1) } // Lock acquires a lock on the mutex. func (s SimpleMutex) Lock() { s <- struct{}{} } // TryLock attempts to acquire a lock on the mutex. // Returns true if the lock has been acquired, false otherwise. func (s SimpleMutex) TryLock() bool { select { case s <- struct{}{}: return true default: return false } } // Unlock releases the mutex. func (s SimpleMutex) Unlock() { <-s } ================================================ FILE: server/datamodel.go ================================================ package main /****************************************************************************** * * Description : * * Wire protocol structures * *****************************************************************************/ import ( "encoding/json" "net/http" "strconv" "strings" "time" "github.com/tinode/chat/server/store/types" ) // MsgGetOpts defines Get query parameters. type MsgGetOpts struct { // Optional User ID to return result(s) for one user. User string `json:"user,omitempty"` // Optional topic name to return result(s) for one topic. Topic string `json:"topic,omitempty"` // Return results modified since this timespamp. IfModifiedSince *time.Time `json:"ims,omitempty"` // Load messages/ranges with IDs equal or greater than this (inclusive or closed). SinceId int `json:"since,omitempty"` // Load messages/ranges with IDs lower than this (exclusive or open). BeforeId int `json:"before,omitempty"` // Limit the number of messages loaded. Limit int `json:"limit,omitempty"` // Fetch messages with IDs in these ranges. IdRanges []MsgRange `json:"ranges,omitempty"` } // MsgGetQuery is a topic metadata or data query. type MsgGetQuery struct { What string `json:"what"` // Parameters of "desc" request: IfModifiedSince Desc *MsgGetOpts `json:"desc,omitempty"` // Parameters of "sub" request: User, Topic, IfModifiedSince, Limit. Sub *MsgGetOpts `json:"sub,omitempty"` // Parameters of "data" request: Since, Before, Limit. Data *MsgGetOpts `json:"data,omitempty"` // Parameters of "del" request: Since, Before, Limit. Del *MsgGetOpts `json:"del,omitempty"` } // MsgSetSub is a payload in set.sub request to update current subscription or invite another user, {sub.what} == "sub". type MsgSetSub struct { // User affected by this request. Default (empty): current user. User string `json:"user,omitempty"` // Access mode change, either Given or Want depending on context. Mode string `json:"mode,omitempty"` } // MsgSetDesc is a C2S in set.what == "desc", acc, sub message. type MsgSetDesc struct { // Default access mode. DefaultAcs *MsgDefaultAcsMode `json:"defacs,omitempty"` // Description of the user or topic. Public any `json:"public,omitempty"` // Trusted (system-provided) user or topic data. Trusted any `json:"trusted,omitempty"` // Per-subscription private data. Private any `json:"private,omitempty"` } // MsgCredClient is an account credential such as email or phone number. type MsgCredClient struct { // Credential type, i.e. `email` or `tel`. Method string `json:"meth,omitempty"` // Value to verify, i.e. `user@example.com` or `+18003287448` Value string `json:"val,omitempty"` // Verification response Response string `json:"resp,omitempty"` // Request parameters, such as preferences. Passed to valiator without interpretation. Params map[string]any `json:"params,omitempty"` } // MsgSetQuery is an update to topic or user metadata: description, subscriptions, tags, credentials. type MsgSetQuery struct { // Topic/user description, new object & new subscriptions only Desc *MsgSetDesc `json:"desc,omitempty"` // Subscription parameters Sub *MsgSetSub `json:"sub,omitempty"` // Indexable tags for user discovery Tags []string `json:"tags,omitempty"` // Update to account credentials. Cred *MsgCredClient `json:"cred,omitempty"` // Update auxiliary data Aux map[string]any } // MsgRange is either an individual ID (HiId=0) or a randge of IDs, low end inclusive (closed), // high-end exclusive (open): [LowId .. HiId), e.g. 1..5 -> 1, 2, 3, 4. type MsgRange struct { LowId int `json:"low,omitempty"` HiId int `json:"hi,omitempty"` } /**************************************************************** * Client to Server (C2S) messages. ****************************************************************/ // MsgClientHi is a handshake {hi} message. type MsgClientHi struct { // Message Id Id string `json:"id,omitempty"` // User agent UserAgent string `json:"ua,omitempty"` // Protocol version, i.e. "0.13" Version string `json:"ver,omitempty"` // Client's unique device ID DeviceID string `json:"dev,omitempty"` // ISO 639-1 human language of the connected device Lang string `json:"lang,omitempty"` // Platform code: ios, android, web. Platform string `json:"platf,omitempty"` // Session is initially in non-iteractive, i.e. issued by a service. Presence notifications are delayed. Background bool `json:"bkg,omitempty"` } // MsgClientAcc is an {acc} message for creating or updating a user account. type MsgClientAcc struct { // Message Id Id string `json:"id,omitempty"` // "newXYZ" to create a new user or UserId to update a user; default: current user. User string `json:"user,omitempty"` // Temporary authentication parameters for one-off actions, like password reset. TmpScheme string `json:"tmpscheme,omitempty"` TmpSecret []byte `json:"tmpsecret,omitempty"` // Account state: normal, suspended. State string `json:"status,omitempty"` // Authentication level of the user when UserID is set and not equal to the current user. // Either "", "auth" or "anon". Default: "" AuthLevel string `json:"authlevel,omitempty"` // The initial authentication scheme the account can use Scheme string `json:"scheme,omitempty"` // Shared secret Secret []byte `json:"secret,omitempty"` // Authenticate session with the newly created account Login bool `json:"login,omitempty"` // Indexable tags for user discovery Tags []string `json:"tags,omitempty"` // User initialization data when creating a new user, otherwise ignored Desc *MsgSetDesc `json:"desc,omitempty"` // Credentials to verify (email or phone or captcha) Cred []MsgCredClient `json:"cred,omitempty"` } // MsgClientLogin is a login {login} message. type MsgClientLogin struct { // Message Id Id string `json:"id,omitempty"` // Authentication scheme Scheme string `json:"scheme,omitempty"` // Shared secret Secret []byte `json:"secret"` // Credntials being verified (email or phone or captcha etc.) Cred []MsgCredClient `json:"cred,omitempty"` } // MsgClientSub is a subscription request {sub} message. type MsgClientSub struct { Id string `json:"id,omitempty"` Topic string `json:"topic"` // Mirrors {set}. Set *MsgSetQuery `json:"set,omitempty"` // Mirrors {get}. Get *MsgGetQuery `json:"get,omitempty"` // Intra-cluster fields. // True if this subscription created a new topic. // In case of p2p topics, it's true if the other user's subscription was // created (as a part of new topic creation or just alone). Created bool `json:"-"` // True if this is a new subscription. Newsub bool `json:"-"` } const ( constMsgMetaDesc = 1 << iota constMsgMetaSub constMsgMetaData constMsgMetaTags constMsgMetaDel constMsgMetaCred constMsgMetaAux ) const ( constMsgDelTopic = iota + 1 constMsgDelMsg constMsgDelSub constMsgDelUser constMsgDelCred ) func parseMsgClientMeta(params string) int { var bits int parts := strings.SplitN(params, " ", 8) for _, p := range parts { switch p { case "desc": bits |= constMsgMetaDesc case "sub": bits |= constMsgMetaSub case "data": bits |= constMsgMetaData case "tags": bits |= constMsgMetaTags case "del": bits |= constMsgMetaDel case "cred": bits |= constMsgMetaCred case "aux": bits |= constMsgMetaAux default: // ignore unknown } } return bits } func parseMsgClientDel(params string) int { switch params { case "", "msg": return constMsgDelMsg case "topic": return constMsgDelTopic case "sub": return constMsgDelSub case "user": return constMsgDelUser case "cred": return constMsgDelCred default: // ignore } return 0 } // MsgDefaultAcsMode is a topic default access mode. type MsgDefaultAcsMode struct { Auth string `json:"auth,omitempty"` Anon string `json:"anon,omitempty"` } // MsgClientLeave is an unsubscribe {leave} request message. type MsgClientLeave struct { Id string `json:"id,omitempty"` Topic string `json:"topic"` Unsub bool `json:"unsub,omitempty"` } // MsgClientPub is client's request to publish data to topic subscribers {pub}. type MsgClientPub struct { Id string `json:"id,omitempty"` Topic string `json:"topic"` NoEcho bool `json:"noecho,omitempty"` Head map[string]any `json:"head,omitempty"` Content any `json:"content"` } // MsgClientGet is a query of topic state {get}. type MsgClientGet struct { Id string `json:"id,omitempty"` Topic string `json:"topic"` MsgGetQuery } // MsgClientSet is an update of topic state {set}. type MsgClientSet struct { Id string `json:"id,omitempty"` Topic string `json:"topic"` MsgSetQuery } // MsgClientDel delete messages or topic {del}. type MsgClientDel struct { Id string `json:"id,omitempty"` Topic string `json:"topic,omitempty"` // What to delete: // * "msg" to delete messages (default) // * "topic" to delete the topic // * "sub" to delete a subscription to topic. // * "user" to delete or disable user. // * "cred" to delete credential (email or phone) What string `json:"what"` // Delete messages with these IDs (either one by one or a set of ranges) DelSeq []MsgRange `json:"delseq,omitempty"` // User ID of the user or subscription to delete User string `json:"user,omitempty"` // Credential to delete Cred *MsgCredClient `json:"cred,omitempty"` // Request to hard-delete objects (i.e. delete messages for all users), if such option is available. Hard bool `json:"hard,omitempty"` } // MsgClientNote is a client-generated notification for topic subscribers {note}. type MsgClientNote struct { // There is no Id -- server will not akn {ping} packets, they are "fire and forget" Topic string `json:"topic"` // what is being reported: "recv" - message received, "read" - message read, "kp" - typing notification What string `json:"what"` // Server-issued message ID being reported SeqId int `json:"seq,omitempty"` // Client's count of unread messages to report back to the server. Used in push notifications on iOS. Unread int `json:"unread,omitempty"` // Call event. Event string `json:"event,omitempty"` // Arbitrary json payload (used in video calls). Payload json.RawMessage `json:"payload,omitempty"` } // MsgClientExtra is not a stand-alone message but extra data which augments the main payload. type MsgClientExtra struct { // Array of out-of-band attachments which have to be exempted from GC. Attachments []string `json:"attachments,omitempty"` // Alternative user ID set by the root user (obo = On Behalf Of). AsUser string `json:"obo,omitempty"` // Altered authentication level set by the root user. AuthLevel string `json:"authlevel,omitempty"` } // ClientComMessage is a wrapper for client messages. type ClientComMessage struct { Hi *MsgClientHi `json:"hi"` Acc *MsgClientAcc `json:"acc"` Login *MsgClientLogin `json:"login"` Sub *MsgClientSub `json:"sub"` Leave *MsgClientLeave `json:"leave"` Pub *MsgClientPub `json:"pub"` Get *MsgClientGet `json:"get"` Set *MsgClientSet `json:"set"` Del *MsgClientDel `json:"del"` Note *MsgClientNote `json:"note"` // Optional data. Extra *MsgClientExtra `json:"extra"` // Internal fields, routed only within the cluster. // Message ID denormalized Id string `json:"-"` // Un-routable (original) topic name denormalized from XXX.Topic. Original string `json:"-"` // Routable (expanded) topic name. RcptTo string `json:"-"` // Sender's UserId as string. AsUser string `json:"-"` // Sender's authentication level. AuthLvl int `json:"-"` // Denormalized 'what' field of meta messages (set, get, del). MetaWhat int `json:"-"` // Timestamp when this message was received by the server. Timestamp time.Time `json:"-"` // Originating session to send an aknowledgement to. sess *Session // The message is initialized (true) as opposite to being used as a wrapper for session. init bool } /**************************************************************** * Server to client messages. ****************************************************************/ // MsgLastSeenInfo contains info on user's appearance online - when & user agent. type MsgLastSeenInfo struct { // Timestamp of user's last appearance online. When *time.Time `json:"when,omitempty"` // User agent of the device when the user was last online. UserAgent string `json:"ua,omitempty"` } func (src *MsgLastSeenInfo) describe() string { return "'" + src.UserAgent + "' @ " + src.When.String() } // MsgCredServer is an account credential such as email or phone number. type MsgCredServer struct { // Credential type, i.e. `email` or `tel`. Method string `json:"meth,omitempty"` // Credential value, i.e. `user@example.com` or `+18003287448` Value string `json:"val,omitempty"` // Indicates that the credential is validated. Done bool `json:"done,omitempty"` } // MsgAccessMode is a definition of access mode. type MsgAccessMode struct { // Access mode requested by the user Want string `json:"want,omitempty"` // Access mode granted to the user by the admin Given string `json:"given,omitempty"` // Cumulative access mode want & given Mode string `json:"mode,omitempty"` } func (src *MsgAccessMode) describe() string { var s string if src.Want != "" { s = "w=" + src.Want } if src.Given != "" { s += " g=" + src.Given } if src.Mode != "" { s += " m=" + src.Mode } return strings.TrimSpace(s) } // MsgTopicDesc is a topic description, S2C in Meta message. type MsgTopicDesc struct { CreatedAt *time.Time `json:"created,omitempty"` UpdatedAt *time.Time `json:"updated,omitempty"` // Timestamp of the last message TouchedAt *time.Time `json:"touched,omitempty"` // Account state, 'me' topic only. State string `json:"state,omitempty"` // If the group topic is online. Online bool `json:"online,omitempty"` // If the topic can be accessed as a channel IsChan bool `json:"chan,omitempty"` // P2P other user's last online timestamp & user agent LastSeen *MsgLastSeenInfo `json:"seen,omitempty"` DefaultAcs *MsgDefaultAcsMode `json:"defacs,omitempty"` // Actual access mode Acs *MsgAccessMode `json:"acs,omitempty"` // Max message ID SeqId int `json:"seq,omitempty"` ReadSeqId int `json:"read,omitempty"` RecvSeqId int `json:"recv,omitempty"` // Id of the last delete operation as seen by the requesting user DelId int `json:"clear,omitempty"` SubCnt int `json:"subcnt,omitempty"` Public any `json:"public,omitempty"` Trusted any `json:"trusted,omitempty"` // Per-subscription private data Private any `json:"private,omitempty"` } func (src *MsgTopicDesc) describe() string { var s string if src.State != "" { s = " state=" + src.State } s += " online=" + strconv.FormatBool(src.Online) if src.Acs != nil { s += " acs={" + src.Acs.describe() + "}" } if src.SeqId != 0 { s += " seq=" + strconv.Itoa(src.SeqId) } if src.ReadSeqId != 0 { s += " read=" + strconv.Itoa(src.ReadSeqId) } if src.RecvSeqId != 0 { s += " recv=" + strconv.Itoa(src.RecvSeqId) } if src.DelId != 0 { s += " clear=" + strconv.Itoa(src.DelId) } if src.SubCnt != 0 { s += " subcnt=" + strconv.Itoa(src.SubCnt) } if src.Public != nil { s += " pub='...'" } if src.Trusted != nil { s += " trst='...'" } if src.Private != nil { s += " priv='...'" } return s } // MsgTopicSub is topic subscription details, sent in Meta message. type MsgTopicSub struct { // Fields common to all subscriptions // Timestamp when the subscription was last updated UpdatedAt *time.Time `json:"updated,omitempty"` // Timestamp when the subscription was deleted DeletedAt *time.Time `json:"deleted,omitempty"` // If the subscriber/topic is online Online bool `json:"online,omitempty"` // Access mode. Topic admins receive the full info, non-admins receive just the cumulative mode // Acs.Mode = want & given. The field is not a pointer because at least one value is always assigned. Acs MsgAccessMode `json:"acs,omitempty"` // ID of the message reported by the given user as read ReadSeqId int `json:"read,omitempty"` // ID of the message reported by the given user as received RecvSeqId int `json:"recv,omitempty"` // Topic's public data Public any `json:"public,omitempty"` // Topic's trusted public data Trusted any `json:"trusted,omitempty"` // User's own private data per topic Private any `json:"private,omitempty"` // Response to non-'me' topic // Uid of the subscribed user User string `json:"user,omitempty"` // The following sections makes sense only in context of getting // user's own subscriptions ('me' topic response) // Topic name of this subscription Topic string `json:"topic,omitempty"` // Timestamp of the last message in the topic. TouchedAt *time.Time `json:"touched,omitempty"` // ID of the last {data} message in a topic SeqId int `json:"seq,omitempty"` // Id of the latest Delete operation DelId int `json:"clear,omitempty"` // Number of subscribers, group topics only. SubCnt int `json:"subcnt,omitempty"` // P2P topics in 'me' {get subs} response: // Other user's last online timestamp & user agent LastSeen *MsgLastSeenInfo `json:"seen,omitempty"` } func (src *MsgTopicSub) describe() string { s := src.Topic + ":" + src.User + " online=" + strconv.FormatBool(src.Online) + " acs=" + src.Acs.describe() if src.SeqId != 0 { s += " seq=" + strconv.Itoa(src.SeqId) } if src.ReadSeqId != 0 { s += " read=" + strconv.Itoa(src.ReadSeqId) } if src.RecvSeqId != 0 { s += " recv=" + strconv.Itoa(src.RecvSeqId) } if src.DelId != 0 { s += " clear=" + strconv.Itoa(src.DelId) } if src.SubCnt != 0 { s += " subcnt=" + strconv.Itoa(src.SubCnt) } if src.Public != nil { s += " pub='...'" } if src.Trusted != nil { s += " trst='...'" } if src.Private != nil { s += " priv='...'" } if src.LastSeen != nil { s += " seen={" + src.LastSeen.describe() + "}" } return s } // MsgDelValues describes request to delete messages. type MsgDelValues struct { DelId int `json:"clear,omitempty"` DelSeq []MsgRange `json:"delseq,omitempty"` } // MsgServerCtrl is a server control message {ctrl}. type MsgServerCtrl struct { Id string `json:"id,omitempty"` Topic string `json:"topic,omitempty"` Params any `json:"params,omitempty"` Code int `json:"code"` Text string `json:"text,omitempty"` Timestamp time.Time `json:"ts"` } // Deep-shallow copy. func (src *MsgServerCtrl) copy() *MsgServerCtrl { if src == nil { return nil } dst := *src return &dst } func (src *MsgServerCtrl) describe() string { return src.Topic + " id=" + src.Id + " code=" + strconv.Itoa(src.Code) + " txt=" + src.Text } // MsgServerData is a server {data} message. type MsgServerData struct { Topic string `json:"topic"` // ID of the user who originated the message as {pub}, could be empty if sent by the system From string `json:"from,omitempty"` Timestamp time.Time `json:"ts"` DeletedAt *time.Time `json:"deleted,omitempty"` SeqId int `json:"seq"` Head map[string]any `json:"head,omitempty"` Content any `json:"content"` } // Deep-shallow copy. func (src *MsgServerData) copy() *MsgServerData { if src == nil { return nil } dst := *src return &dst } func (src *MsgServerData) describe() string { s := src.Topic + " from=" + src.From + " seq=" + strconv.Itoa(src.SeqId) if src.DeletedAt != nil { s += " deleted" } else { if src.Head != nil { s += " head=..." } s += " content='...'" } return s } // MsgServerPres is presence notification {pres} (authoritative update). type MsgServerPres struct { Topic string `json:"topic"` Src string `json:"src,omitempty"` What string `json:"what"` UserAgent string `json:"ua,omitempty"` SeqId int `json:"seq,omitempty"` DelId int `json:"clear,omitempty"` DelSeq []MsgRange `json:"delseq,omitempty"` AcsTarget string `json:"tgt,omitempty"` AcsActor string `json:"act,omitempty"` // Acs or a delta Acs. Need to marshal it to json under a name different than 'acs' // to allow different handling on the client Acs *MsgAccessMode `json:"dacs,omitempty"` // UNroutable params. All marked with `json:"-"` to exclude from json marshaling. // They are still serialized for intra-cluster communication. // Flag to break the reply loop WantReply bool `json:"-"` // Additional access mode filters when sending to topic's online members. Both filter conditions must be true. // send only to those who have this access mode. FilterIn int `json:"-"` // skip those who have this access mode. FilterOut int `json:"-"` // When sending to 'me', skip sessions subscribed to this topic. SkipTopic string `json:"-"` // Send to sessions of a single user only. SingleUser string `json:"-"` // Exclude sessions of a single user. ExcludeUser string `json:"-"` } // Deep-shallow copy. func (src *MsgServerPres) copy() *MsgServerPres { if src == nil { return nil } dst := *src return &dst } func (src *MsgServerPres) describe() string { s := src.Topic if src.Src != "" { s += " src=" + src.Src } if src.What != "" { s += " what=" + src.What } if src.UserAgent != "" { s += " ua=" + src.UserAgent } if src.SeqId != 0 { s += " seq=" + strconv.Itoa(src.SeqId) } if src.DelId != 0 { s += " clear=" + strconv.Itoa(src.DelId) } if src.DelSeq != nil { s += " delseq" } if src.AcsTarget != "" { s += " tgt=" + src.AcsTarget } if src.AcsActor != "" { s += " actor=" + src.AcsActor } if src.Acs != nil { s += " dacs=" + src.Acs.describe() } return s } // MsgServerMeta is a topic metadata {meta} update. type MsgServerMeta struct { Id string `json:"id,omitempty"` Topic string `json:"topic"` Timestamp *time.Time `json:"ts,omitempty"` // Topic description Desc *MsgTopicDesc `json:"desc,omitempty"` // Subscriptions as an array of objects Sub []MsgTopicSub `json:"sub,omitempty"` // Delete ID and the ranges of IDs of deleted messages Del *MsgDelValues `json:"del,omitempty"` // User discovery tags Tags []string `json:"tags,omitempty"` // Account credentials, 'me' only. Cred []*MsgCredServer `json:"cred,omitempty"` // Auxiliary data Aux map[string]any `json:"aux,omitempty"` } // Deep-shallow copy of meta message. Deep copy of Id and Topic fields, shallow copy of payload. func (src *MsgServerMeta) copy() *MsgServerMeta { if src == nil { return nil } dst := *src return &dst } func (src *MsgServerMeta) describe() string { s := src.Topic + " id=" + src.Id if src.Desc != nil { s += " desc={" + src.Desc.describe() + "}" } if src.Sub != nil { var x []string for _, sub := range src.Sub { x = append(x, sub.describe()) } s += " sub=[{" + strings.Join(x, "},{") + "}]" } if src.Del != nil { x, _ := json.Marshal(src.Del) s += " del={" + string(x) + "}" } if src.Tags != nil { s += " tags=[" + strings.Join(src.Tags, ",") + "]" } if src.Cred != nil { x, _ := json.Marshal(src.Cred) s += " cred=[" + string(x) + "]" } if src.Aux != nil { x, _ := json.Marshal(src.Aux) s += " aux=[" + string(x) + "]" } return s } // MsgServerInfo is the server-side copy of MsgClientNote with From and optionally Src added (non-authoritative). type MsgServerInfo struct { // Topic to send event to. Topic string `json:"topic"` // Topic where the even has occurred (set only when Topic='me'). Src string `json:"src,omitempty"` // ID of the user who originated the message. From string `json:"from,omitempty"` // The event being reported: "rcpt" - message received, "read" - message read, "kp" - typing notification, "call" - video call. What string `json:"what"` // Server-issued message ID being reported. SeqId int `json:"seq,omitempty"` // Call event. Event string `json:"event,omitempty"` // Arbitrary json payload (used by video calls). Payload json.RawMessage `json:"payload,omitempty"` // UNroutable params. All marked with `json:"-"` to exclude from json marshaling. // They are still serialized for intra-cluster communication. // When sending to 'me', skip sessions subscribed to this topic. SkipTopic string `json:"-"` } // Deep copy. func (src *MsgServerInfo) copy() *MsgServerInfo { if src == nil { return nil } dst := *src return &dst } // Basic description. func (src *MsgServerInfo) describe() string { s := src.Topic if src.Src != "" { s += " src=" + src.Src } s += " what=" + src.What + " from=" + src.From if src.SeqId > 0 { s += " seq=" + strconv.Itoa(src.SeqId) } if len(src.Payload) > 0 { s += " payload=<..." + strconv.Itoa(len(src.Payload)) + " bytes ...>" } return s } // ServerComMessage is a wrapper for server-side messages. type ServerComMessage struct { Ctrl *MsgServerCtrl `json:"ctrl,omitempty"` Data *MsgServerData `json:"data,omitempty"` Meta *MsgServerMeta `json:"meta,omitempty"` Pres *MsgServerPres `json:"pres,omitempty"` Info *MsgServerInfo `json:"info,omitempty"` // Internal fields. // MsgServerData has no Id field, copying it here for use in {ctrl} aknowledgements Id string `json:"-"` // Routable (expanded) name of the topic. RcptTo string `json:"-"` // User ID of the sender of the original message. AsUser string `json:"-"` // Timestamp for consistency of timestamps in {ctrl} messages // (corresponds to originating client message receipt timestamp). Timestamp time.Time `json:"-"` // Originating session to send an aknowledgement to. Could be nil. sess *Session // Session ID to skip when sendng packet to sessions. Used to skip sending to original session. // Could be either empty. SkipSid string `json:"-"` // User id affected by this message. uid types.Uid } // Deep-shallow copy of ServerComMessage. Deep copy of service fields, // shallow copy of session and payload. func (src *ServerComMessage) copy() *ServerComMessage { if src == nil { return nil } dst := &ServerComMessage{ Id: src.Id, RcptTo: src.RcptTo, AsUser: src.AsUser, Timestamp: src.Timestamp, sess: src.sess, SkipSid: src.SkipSid, uid: src.uid, } dst.Ctrl = src.Ctrl.copy() dst.Data = src.Data.copy() dst.Meta = src.Meta.copy() dst.Pres = src.Pres.copy() dst.Info = src.Info.copy() return dst } func (src *ServerComMessage) describe() string { if src == nil { return "-" } switch { case src.Ctrl != nil: return "{ctrl " + src.Ctrl.describe() + "}" case src.Data != nil: return "{data " + src.Data.describe() + "}" case src.Meta != nil: return "{meta " + src.Meta.describe() + "}" case src.Pres != nil: return "{pres " + src.Pres.describe() + "}" case src.Info != nil: return "{info " + src.Info.describe() + "}" default: return "{nil}" } } // Generators of server-side error messages {ctrl}. // NoErr indicates successful completion (200). func NoErr(id, topic string, ts time.Time) *ServerComMessage { return NoErrParams(id, topic, ts, nil) } // NoErrExplicitTs indicates successful completion with explicit server and incoming request timestamps (200). func NoErrExplicitTs(id, topic string, serverTs, incomingReqTs time.Time) *ServerComMessage { return NoErrParamsExplicitTs(id, topic, serverTs, incomingReqTs, nil) } // NoErrReply indicates successful completion as a reply to a client message (200). func NoErrReply(msg *ClientComMessage, ts time.Time) *ServerComMessage { return NoErrExplicitTs(msg.Id, msg.Original, ts, msg.Timestamp) } // NoErrParams indicates successful completion with additional parameters (200). func NoErrParams(id, topic string, ts time.Time, params any) *ServerComMessage { return NoErrParamsExplicitTs(id, topic, ts, ts, params) } // NoErrParamsExplicitTs indicates successful completion with additional parameters // and explicit server and incoming request timestamps (200). func NoErrParamsExplicitTs(id, topic string, serverTs, incomingReqTs time.Time, params any) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusOK, // 200 Text: "ok", Topic: topic, Params: params, Timestamp: serverTs, }, Id: id, Timestamp: incomingReqTs, } } // NoErrParamsReply indicates successful completion with additional parameters // and explicit server and incoming request timestamps (200). func NoErrParamsReply(msg *ClientComMessage, ts time.Time, params any) *ServerComMessage { return NoErrParamsExplicitTs(msg.Id, msg.Original, ts, msg.Timestamp, params) } // NoErrCreated indicated successful creation of an object (201). func NoErrCreated(id, topic string, ts time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusCreated, // 201 Text: "created", Topic: topic, Timestamp: ts, }, Id: id, Timestamp: ts, } } // NoErrAccepted indicates request was accepted but not processed yet (202). func NoErrAccepted(id, topic string, ts time.Time) *ServerComMessage { return NoErrAcceptedExplicitTs(id, topic, ts, ts) } // NoErrAcceptedExplicitTs indicates request was accepted but not processed yet // with explicit server and incoming request timestamps (202). func NoErrAcceptedExplicitTs(id, topic string, serverTs, incomingReqTs time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusAccepted, // 202 Text: "accepted", Topic: topic, Timestamp: serverTs, }, Id: id, Timestamp: incomingReqTs, } } // NoContentParams indicates request was processed but resulted in no content (204). func NoContentParams(id, topic string, serverTs, incomingReqTs time.Time, params any) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusNoContent, // 204 Text: "no content", Topic: topic, Params: params, Timestamp: serverTs, }, Id: id, Timestamp: incomingReqTs, } } // NoContentParamsReply indicates request was processed but resulted in no content // in response to a client request (204). func NoContentParamsReply(msg *ClientComMessage, ts time.Time, params any) *ServerComMessage { return NoContentParams(msg.Id, msg.Original, ts, msg.Timestamp, params) } // NoErrEvicted indicates that the user was disconnected from topic for no fault of the user (205). func NoErrEvicted(id, topic string, ts time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusResetContent, // 205 Text: "evicted", Topic: topic, Timestamp: ts, }, Id: id, } } // NoErrShutdown means user was disconnected from topic because system shutdown is in progress (205). func NoErrShutdown(ts time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Code: http.StatusResetContent, // 205 Text: "server shutdown", Timestamp: ts, }, } } // NoErrDeliveredParams means requested content has been delivered (208). func NoErrDeliveredParams(id, topic string, ts time.Time, params any) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusAlreadyReported, // 208 Text: "delivered", Topic: topic, Params: params, Timestamp: ts, }, Id: id, } } // 3xx // InfoValidateCredentials requires user to confirm credentials before going forward (300). func InfoValidateCredentials(id string, ts time.Time) *ServerComMessage { return InfoValidateCredentialsExplicitTs(id, ts, ts) } // InfoValidateCredentialsExplicitTs requires user to confirm credentials before going forward // with explicit server and incoming request timestamps (300). func InfoValidateCredentialsExplicitTs(id string, serverTs, incomingReqTs time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusMultipleChoices, // 300 Text: "validate credentials", Timestamp: serverTs, }, Id: id, Timestamp: incomingReqTs, } } // InfoChallenge requires user to respond to presented challenge before login can be completed (300). func InfoChallenge(id string, ts time.Time, challenge []byte) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusMultipleChoices, // 300 Text: "challenge", Params: map[string]any{"challenge": challenge}, Timestamp: ts, }, Id: id, Timestamp: ts, } } // InfoAuthReset is sent in response to request to reset authentication when it was completed // but login was not performed (301). func InfoAuthReset(id string, ts time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusMovedPermanently, // 301 Text: "auth reset", Timestamp: ts, }, Id: id, Timestamp: ts, } } // InfoUseOther is a response to a subscription request redirecting client to another topic (303). func InfoUseOther(id, topic, other string, serverTs, incomingReqTs time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusSeeOther, // 303 Text: "use other", Topic: topic, Params: map[string]string{"topic": other}, Timestamp: serverTs, }, Id: id, Timestamp: incomingReqTs, } } // InfoUseOtherReply is a response to a subscription request redirecting client to another topic (303). func InfoUseOtherReply(msg *ClientComMessage, other string, ts time.Time) *ServerComMessage { return InfoUseOther(msg.Id, msg.Original, other, ts, msg.Timestamp) } // InfoAlreadySubscribed response means request to subscribe was ignored because user is already subscribed (304). func InfoAlreadySubscribed(id, topic string, ts time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusNotModified, // 304 Text: "already subscribed", Topic: topic, Timestamp: ts, }, Id: id, Timestamp: ts, } } // InfoNotJoined response means request to leave was ignored because user was not subscribed (304). func InfoNotJoined(id, topic string, ts time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusNotModified, // 304 Text: "not joined", Topic: topic, Timestamp: ts, }, Id: id, Timestamp: ts, } } // InfoNoAction response means request was ignored because the object was already in the desired state // with explicit server and incoming request timestamps (304). func InfoNoAction(id, topic string, serverTs, incomingReqTs time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusNotModified, // 304 Text: "no action", Topic: topic, Timestamp: serverTs, }, Id: id, Timestamp: incomingReqTs, } } // InfoNoActionReply response means request was ignored because the object was already in the desired state // in response to a client request (304). func InfoNoActionReply(msg *ClientComMessage, ts time.Time) *ServerComMessage { return InfoNoAction(msg.Id, msg.Original, ts, msg.Timestamp) } // InfoNotModified response means update request was a noop (304). func InfoNotModified(id, topic string, ts time.Time) *ServerComMessage { return InfoNotModifiedExplicitTs(id, topic, ts, ts) } // InfoNotModifiedReply response means update request was a noop // in response to a client request (304). func InfoNotModifiedReply(msg *ClientComMessage, ts time.Time) *ServerComMessage { return InfoNotModifiedExplicitTs(msg.Id, msg.Original, ts, msg.Timestamp) } // InfoNotModifiedExplicitTs response means update request was a noop // with explicit server and incoming request timestamps (304). func InfoNotModifiedExplicitTs(id, topic string, serverTs, incomingReqTs time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusNotModified, // 304 Text: "not modified", Topic: topic, Timestamp: serverTs, }, Id: id, Timestamp: incomingReqTs, } } // InfoFound redirects to a new resource (307). func InfoFound(id, topic string, ts time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusTemporaryRedirect, // 307 Text: "found", Topic: topic, Timestamp: ts, }, Id: id, Timestamp: ts, } } // 4xx Errors // ErrMalformed request malformed (400). func ErrMalformed(id, topic string, ts time.Time) *ServerComMessage { return ErrMalformedExplicitTs(id, topic, ts, ts) } // ErrMalformedReply request malformed // in response to a client request (400). func ErrMalformedReply(msg *ClientComMessage, ts time.Time) *ServerComMessage { return ErrMalformedExplicitTs(msg.Id, msg.Original, ts, msg.Timestamp) } // ErrMalformedExplicitTs request malformed with explicit server and incoming request timestamps (400). func ErrMalformedExplicitTs(id, topic string, serverTs, incomingReqTs time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusBadRequest, // 400 Text: "malformed", Topic: topic, Timestamp: serverTs, }, Id: id, Timestamp: incomingReqTs, } } // ErrAuthRequired authentication required - user must authenticate first (401). func ErrAuthRequired(id, topic string, serverTs, incomingReqTs time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusUnauthorized, // 401 Text: "authentication required", Topic: topic, Timestamp: serverTs, }, Id: id, Timestamp: incomingReqTs, } } // ErrAuthRequiredReply authentication required - user must authenticate first // in response to a client request (401). func ErrAuthRequiredReply(msg *ClientComMessage, ts time.Time) *ServerComMessage { return ErrAuthRequired(msg.Id, msg.Original, ts, msg.Timestamp) } // ErrAuthFailed authentication failed // with explicit server and incoming request timestamps (401). func ErrAuthFailed(id, topic string, serverTs, incomingReqTs time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusUnauthorized, // 401 Text: "authentication failed", Topic: topic, Timestamp: serverTs, }, Id: id, Timestamp: incomingReqTs, } } // ErrAuthUnknownScheme authentication scheme is unrecognized or invalid (401). func ErrAuthUnknownScheme(id, topic string, ts time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusUnauthorized, // 401 Text: "unknown authentication scheme", Topic: topic, Timestamp: ts, }, Id: id, Timestamp: ts, } } // ErrPermissionDenied user is authenticated but operation is not permitted (403). func ErrPermissionDenied(id, topic string, ts time.Time) *ServerComMessage { return ErrPermissionDeniedExplicitTs(id, topic, ts, ts) } // ErrPermissionDeniedExplicitTs user is authenticated but operation is not permitted // with explicit server and incoming request timestamps (403). func ErrPermissionDeniedExplicitTs(id, topic string, serverTs, incomingReqTs time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusForbidden, // 403 Text: "permission denied", Topic: topic, Timestamp: serverTs, }, Id: id, Timestamp: incomingReqTs, } } // ErrPermissionDeniedReply user is authenticated but operation is not permitted // with explicit server and incoming request timestamps in response to a client request (403). func ErrPermissionDeniedReply(msg *ClientComMessage, ts time.Time) *ServerComMessage { return ErrPermissionDeniedExplicitTs(msg.Id, msg.Original, ts, msg.Timestamp) } // ErrAPIKeyRequired valid API key is required (403). func ErrAPIKeyRequired(ts time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Code: http.StatusForbidden, Text: "valid API key required", Timestamp: ts, }, } } // ErrSessionNotFound valid API key is required (403). func ErrSessionNotFound(ts time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Code: http.StatusForbidden, Text: "invalid or expired session", Timestamp: ts, }, } } // ErrTopicNotFound topic is not found // with explicit server and incoming request timestamps (404). func ErrTopicNotFound(id, topic string, serverTs, incomingReqTs time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusNotFound, Text: "topic not found", // 404 Topic: topic, Timestamp: serverTs, }, Id: id, Timestamp: incomingReqTs, } } // ErrTopicNotFoundReply topic is not found // with explicit server and incoming request timestamps // in response to a client request (404). func ErrTopicNotFoundReply(msg *ClientComMessage, ts time.Time) *ServerComMessage { return ErrTopicNotFound(msg.Id, msg.Original, ts, msg.Timestamp) } // ErrUserNotFound user is not found // with explicit server and incoming request timestamps (404). func ErrUserNotFound(id, topic string, serverTs, incomingReqTs time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusNotFound, // 404 Text: "user not found", Topic: topic, Timestamp: serverTs, }, Id: id, Timestamp: incomingReqTs, } } // ErrUserNotFoundReply user is not found // with explicit server and incoming request timestamps in response to a client request (404). func ErrUserNotFoundReply(msg *ClientComMessage, ts time.Time) *ServerComMessage { return ErrUserNotFound(msg.Id, msg.Original, ts, msg.Timestamp) } // ErrNotFound is an error for missing objects other than user or topic (404). func ErrNotFound(id, topic string, ts time.Time) *ServerComMessage { return ErrNotFoundExplicitTs(id, topic, ts, ts) } // ErrNotFoundExplicitTs is an error for missing objects other than user or topic // with explicit server and incoming request timestamps (404). func ErrNotFoundExplicitTs(id, topic string, serverTs, incomingReqTs time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusNotFound, // 404 Text: "not found", Topic: topic, Timestamp: serverTs, }, Id: id, Timestamp: incomingReqTs, } } // ErrNotFoundReply is an error for missing objects other than user or topic // with explicit server and incoming request timestamps in response to a client request (404). func ErrNotFoundReply(msg *ClientComMessage, ts time.Time) *ServerComMessage { return ErrNotFoundExplicitTs(msg.Id, msg.Original, ts, msg.Timestamp) } // ErrOperationNotAllowed a valid operation is not permitted in this context (405). func ErrOperationNotAllowed(id, topic string, ts time.Time) *ServerComMessage { return ErrOperationNotAllowedExplicitTs(id, topic, ts, ts) } // ErrOperationNotAllowedExplicitTs a valid operation is not permitted in this context // with explicit server and incoming request timestamps (405). func ErrOperationNotAllowedExplicitTs(id, topic string, serverTs, incomingReqTs time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusMethodNotAllowed, // 405 Text: "operation or method not allowed", Topic: topic, Timestamp: serverTs, }, Id: id, Timestamp: incomingReqTs, } } // ErrOperationNotAllowedReply a valid operation is not permitted in this context // with explicit server and incoming request timestamps (405). func ErrOperationNotAllowedReply(msg *ClientComMessage, ts time.Time) *ServerComMessage { return ErrOperationNotAllowedExplicitTs(msg.Id, msg.Original, ts, msg.Timestamp) } // ErrInvalidResponse indicates that the client's response in invalid // with explicit server and incoming request timestamps (406). func ErrInvalidResponse(id, topic string, serverTs, incomingReqTs time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusNotAcceptable, // 406 Text: "invalid response", Topic: topic, Timestamp: serverTs, }, Id: id, Timestamp: incomingReqTs, } } // ErrDisconnected indicates that client disconnected or failed to send data in a timely // manner (408). func ErrDisconnected(id, topic string, ts time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusRequestTimeout, // 408 Text: "disconnected", Topic: topic, Timestamp: ts, }, Id: id, Timestamp: ts, } } // ErrAlreadyAuthenticated invalid attempt to authenticate an already authenticated session. // Switching users is not supported (409). func ErrAlreadyAuthenticated(id, topic string, ts time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusConflict, // 409 Text: "already authenticated", Topic: topic, Timestamp: ts, }, Id: id, Timestamp: ts, } } // ErrDuplicateCredential attempt to create a duplicate credential // with explicit server and incoming request timestamps (409). func ErrDuplicateCredential(id, topic string, serverTs, incomingReqTs time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusConflict, // 409 Text: "duplicate credential", Topic: topic, Timestamp: serverTs, }, Id: id, Timestamp: incomingReqTs, } } // ErrAttachFirst must attach to topic first in response to a client message (409). func ErrAttachFirst(msg *ClientComMessage, ts time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: msg.Id, Code: http.StatusConflict, // 409 Text: "must attach first", Topic: msg.Original, Timestamp: ts, }, Id: msg.Id, Timestamp: msg.Timestamp, } } // ErrAlreadyExists the object already exists (409). func ErrAlreadyExists(id, topic string, ts time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusConflict, // 409 Text: "already exists", Topic: topic, Timestamp: ts, }, Id: id, Timestamp: ts, } } // ErrCommandOutOfSequence invalid sequence of comments, i.e. attempt to {sub} before {hi} (409). func ErrCommandOutOfSequence(id, unused string, ts time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusConflict, // 409 Text: "command out of sequence", Timestamp: ts, }, Id: id, Timestamp: ts, } } // ErrGone topic deleted or user banned (410). func ErrGone(id, topic string, ts time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusGone, // 410 Text: "gone", Topic: topic, Timestamp: ts, }, Id: id, Timestamp: ts, } } // ErrTooLarge packet or request size exceeded the limit (413). func ErrTooLarge(id, topic string, ts time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusRequestEntityTooLarge, // 413 Text: "too large", Topic: topic, Timestamp: ts, }, Id: id, Timestamp: ts, } } // ErrPolicy request violates a policy (e.g. password is too weak or too many subscribers) (422). func ErrPolicy(id, topic string, ts time.Time) *ServerComMessage { return ErrPolicyExplicitTs(id, topic, ts, ts) } // ErrPolicyExplicitTs request violates a policy (e.g. password is too weak or too many subscribers) // with explicit server and incoming request timestamps (422). func ErrPolicyExplicitTs(id, topic string, serverTs, incomingReqTs time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusUnprocessableEntity, // 422 Text: "policy violation", Topic: topic, Timestamp: serverTs, }, Id: id, Timestamp: incomingReqTs, } } // ErrPolicyReply request violates a policy (e.g. password is too weak or too many subscribers) // with explicit server and incoming request timestamps in response to a client request (422). func ErrPolicyReply(msg *ClientComMessage, ts time.Time) *ServerComMessage { return ErrPolicyExplicitTs(msg.Id, msg.Original, ts, msg.Timestamp) } // ErrCallBusyExplicitTs indicates a "busy" reply to a video call request (486). func ErrCallBusyExplicitTs(id, topic string, serverTs, incomingReqTs time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: 486, // Busy here. Text: "busy here", Topic: topic, Timestamp: serverTs, }, Id: id, Timestamp: incomingReqTs, } } // ErrCallBusyReply indicates a "busy" reply in response to a video call request (486) func ErrCallBusyReply(msg *ClientComMessage, ts time.Time) *ServerComMessage { return ErrCallBusyExplicitTs(msg.Id, msg.Original, ts, msg.Timestamp) } // ErrUnknown database or other server error (500). func ErrUnknown(id, topic string, ts time.Time) *ServerComMessage { return ErrUnknownExplicitTs(id, topic, ts, ts) } // ErrUnknownExplicitTs database or other server error with explicit server and incoming request timestamps (500). func ErrUnknownExplicitTs(id, topic string, serverTs, incomingReqTs time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusInternalServerError, // 500 Text: "internal error", Topic: topic, Timestamp: serverTs, }, Id: id, Timestamp: incomingReqTs, } } // ErrUnknownReply database or other server error in response to a client request (500). func ErrUnknownReply(msg *ClientComMessage, ts time.Time) *ServerComMessage { return ErrUnknownExplicitTs(msg.Id, msg.Original, ts, msg.Timestamp) } // ErrNotImplemented feature not implemented with explicit server and incoming request timestamps (501). // TODO: consider changing status code to 4XX. func ErrNotImplemented(id, topic string, serverTs, incomingReqTs time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusNotImplemented, // 501 Text: "not implemented", Topic: topic, Timestamp: serverTs, }, Id: id, Timestamp: incomingReqTs, } } // ErrNotImplementedReply feature not implemented error in response to a client request (501). func ErrNotImplementedReply(msg *ClientComMessage, ts time.Time) *ServerComMessage { return ErrNotImplemented(msg.Id, msg.Original, ts, msg.Timestamp) } // ErrClusterUnreachableReply in-cluster communication has failed error as response to a client request (502). func ErrClusterUnreachableReply(msg *ClientComMessage, ts time.Time) *ServerComMessage { return ErrClusterUnreachableExplicitTs(msg.Id, msg.Original, ts, msg.Timestamp) } // ErrClusterUnreachable in-cluster communication has failed error with explicit server and // incoming request timestamps (502). func ErrClusterUnreachableExplicitTs(id, topic string, serverTs, incomingReqTs time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusBadGateway, // 502 Text: "cluster unreachable", Topic: topic, Timestamp: serverTs, }, Id: id, Timestamp: incomingReqTs, } } // ErrServiceUnavailableReply server overloaded error in response to a client request (503). func ErrServiceUnavailableReply(msg *ClientComMessage, ts time.Time) *ServerComMessage { return ErrServiceUnavailableExplicitTs(msg.Id, msg.Original, ts, msg.Timestamp) } // ErrServiceUnavailableExplicitTs server overloaded error with explicit server and // incoming request timestamps (503). func ErrServiceUnavailableExplicitTs(id, topic string, serverTs, incomingReqTs time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusServiceUnavailable, // 503 Text: "service unavailable", Topic: topic, Timestamp: serverTs, }, Id: id, Timestamp: incomingReqTs, } } // ErrLocked operation rejected because the topic is being deleted (503). func ErrLocked(id, topic string, ts time.Time) *ServerComMessage { return ErrLockedExplicitTs(id, topic, ts, ts) } // ErrLockedReply operation rejected because the topic is being deleted in response // to a client request (503). func ErrLockedReply(msg *ClientComMessage, ts time.Time) *ServerComMessage { return ErrLockedExplicitTs(msg.Id, msg.Original, ts, msg.Timestamp) } // ErrLockedExplicitTs operation rejected because the topic is being deleted // with explicit server and incoming request timestamps (503). func ErrLockedExplicitTs(id, topic string, serverTs, incomingReqTs time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusServiceUnavailable, // 503 Text: "locked", Topic: topic, Timestamp: serverTs, }, Id: id, Timestamp: incomingReqTs, } } // ErrVersionNotSupported invalid (too low) protocol version (505). func ErrVersionNotSupported(id string, ts time.Time) *ServerComMessage { return &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: http.StatusHTTPVersionNotSupported, // 505 Text: "version not supported", Timestamp: ts, }, Id: id, Timestamp: ts, } } ================================================ FILE: server/db/adapter.go ================================================ // Package adapter contains the interfaces to be implemented by the database adapter package adapter import ( "encoding/json" "time" "github.com/tinode/chat/server/auth" t "github.com/tinode/chat/server/store/types" ) // Adapter is the interface that must be implemented by a database // adapter. The current schema supports a single connection by database type. type Adapter interface { // General // Open and configure the adapter Open(config json.RawMessage) error // Close the adapter Close() error // IsOpen checks if the adapter is ready for use IsOpen() bool // GetDbVersion returns current database version. GetDbVersion() (int, error) // CheckDbVersion checks if the actual database version matches adapter version. CheckDbVersion() error // GetName returns the name of the adapter GetName() string // SetMaxResults configures how many results can be returned in a single DB call. SetMaxResults(val int) error // CreateDb creates the database optionally dropping an existing database first. CreateDb(reset bool) error // UpgradeDb upgrades database to the current adapter version. UpgradeDb() error // Version returns adapter version Version() int // DB connection stats object. Stats() any // User management // UserCreate creates user record UserCreate(user *t.User) error // UserGet returns record for a given user ID UserGet(uid t.Uid) (*t.User, error) // UserGetAll returns user records for a given list of user IDs UserGetAll(ids ...t.Uid) ([]t.User, error) // UserDelete deletes user record UserDelete(uid t.Uid, hard bool) error // UserUpdate updates user record UserUpdate(uid t.Uid, update map[string]any) error // UserUpdateTags adds, removes, or resets user's tags UserUpdateTags(uid t.Uid, add, remove, reset []string) ([]string, error) // UserGetByCred returns user ID for the given validated credential. UserGetByCred(method, value string) (t.Uid, error) // UserUnreadCount returns the total number of unread messages in all topics with // the R permission. If read fails, the counts are still returned with the original // user IDs but with the unread count undefined and non-nil error. UserUnreadCount(ids ...t.Uid) (map[t.Uid]int, error) // UserGetUnvalidated returns a list of no more than 'limit' uids who never logged in, // have no validated credentials and which haven't been updated since 'lastUpdatedBefore'. UserGetUnvalidated(lastUpdatedBefore time.Time, limit int) ([]t.Uid, error) // Credential management // CredUpsert adds or updates a credential record. Returns true if record was inserted, false if updated. CredUpsert(cred *t.Credential) (bool, error) // CredGetActive returns the currently active credential record for the given method. CredGetActive(uid t.Uid, method string) (*t.Credential, error) // CredGetAll returns credential records for the given user and method, validated only or all. CredGetAll(uid t.Uid, method string, validatedOnly bool) ([]t.Credential, error) // CredDel deletes credentials for the given method/value. If method is empty, deletes all // user's credentials. CredDel(uid t.Uid, method, value string) error // CredConfirm marks given credential as validated. CredConfirm(uid t.Uid, method string) error // CredFail increments count of failed validation attepmts for the given credentials. CredFail(uid t.Uid, method string) error // Authentication management for the basic authentication scheme // AuthGetUniqueRecord returns authentication record for a given unique value i.e. login. AuthGetUniqueRecord(unique string) (t.Uid, auth.Level, []byte, time.Time, error) // AuthGetRecord returns authentication record given user ID and method. AuthGetRecord(user t.Uid, scheme string) (string, auth.Level, []byte, time.Time, error) // AuthAddRecord creates new authentication record AuthAddRecord(user t.Uid, scheme, unique string, authLvl auth.Level, secret []byte, expires time.Time) error // AuthDelScheme deletes an existing authentication scheme for the user. AuthDelScheme(user t.Uid, scheme string) error // AuthDelAllRecords deletes all records of a given user. AuthDelAllRecords(uid t.Uid) (int, error) // AuthUpdRecord modifies an authentication record. Only non-default/non-zero values are updated. AuthUpdRecord(user t.Uid, scheme, unique string, authLvl auth.Level, secret []byte, expires time.Time) error // Topic management // TopicCreate creates a topic TopicCreate(topic *t.Topic) error // TopicCreateP2P creates a p2p topic TopicCreateP2P(initiator, invited *t.Subscription) error // TopicGet loads a single topic by name, if it exists. If the topic does not exist the call returns (nil, nil) TopicGet(topic string) (*t.Topic, error) // TopicsForUser loads subscriptions for a given user. Reads public value. // When the 'opts.IfModifiedSince' query is not nil the subscriptions with UpdatedAt > opts.IfModifiedSince // are returned, where UpdatedAt can be either a subscription, a topic, or a user update timestamp. // This is need in order to support paginagion of subscriptions: get subscriptions page by page // from the oldest updates to most recent: // 1. Client already has subscriptions with the latest update timestamp X. // 2. Client asks for N updated subscriptions since X. The server returns N with updates between X and Y. // 3. Client goes to step 1 with X := Y. TopicsForUser(uid t.Uid, keepDeleted bool, opts *t.QueryOpt) ([]t.Subscription, error) // UsersForTopic loads users' subscriptions for a given topic. Public is loaded. UsersForTopic(topic string, keepDeleted bool, opts *t.QueryOpt) ([]t.Subscription, error) // OwnTopics loads a slice of topic names where the user is the owner. OwnTopics(uid t.Uid) ([]string, error) // ChannelsForUser loads a slice of topic names where the user is a channel reader and notifications (P) are enabled. ChannelsForUser(uid t.Uid) ([]string, error) // TopicShare creates topic subscriptions. TopicShare(topic string, subs []*t.Subscription) error // TopicDelete deletes topic, subscriptions, messages. TopicDelete(topic string, isChan, hard bool) error // TopicUpdateOnMessage increments Topic's or User's SeqId value and updates TouchedAt timestamp. TopicUpdateOnMessage(topic string, msg *t.Message) error // TopicUpdateSubCnt refreshes denormalized topic subscriber count. TopicUpdateSubCnt(topic string) error // TopicUpdate updates topic record. TopicUpdate(topic string, update map[string]any) error // TopicOwnerChange updates topic's owner TopicOwnerChange(topic string, newOwner t.Uid) error // Topic subscriptions // SubscriptionGet reads a subscription of a user to a topic SubscriptionGet(topic string, user t.Uid, keepDeleted bool) (*t.Subscription, error) // SubsForUser loads all subscriptions of a given user. Does NOT load Public or Private values, // does not load deleted subscriptions. SubsForUser(user t.Uid) ([]t.Subscription, error) // SubsForTopic gets a list of subscriptions to a given topic.. Does NOT load Public value. SubsForTopic(topic string, keepDeleted bool, opts *t.QueryOpt) ([]t.Subscription, error) // SubsUpdate updates pasrt of a subscription object. Pass nil for fields which don't need to be updated SubsUpdate(topic string, user t.Uid, update map[string]any) error // SubsDelete deletes a single subscription SubsDelete(topic string, user t.Uid) error // Search // Find searches for users or topics given a list of tags. // - caller is the user or topic who is doing the searching, it will be skipped from results. // - prefix if present will cause match rank highest in the results. // - req is a list of required tag sets. Each set is a list of tags. The search will return // all users/topics which have at least one tag from each set. // - opt is a list of optional tags; if present the result will rank higher. // - activeOnly if true will return only active subscriptions. Find(caller, prefix string, req [][]string, opt []string, activeOnly bool) ([]t.Subscription, error) // FindOne returns topic or user which matches the given tag. FindOne(tag string) (string, error) // Messages // MessageSave saves message to database MessageSave(msg *t.Message) error // MessageGetAll returns messages matching the query MessageGetAll(topic string, forUser t.Uid, opts *t.QueryOpt) ([]t.Message, error) // MessageDeleteList marks messages as deleted. // Soft- or Hard- is defined by forUser value: forUser.IsZero == true is hard. MessageDeleteList(topic string, toDel *t.DelMessage) error // MessageGetDeleted returns a list of deleted message Ids. MessageGetDeleted(topic string, forUser t.Uid, opts *t.QueryOpt) ([]t.DelMessage, error) // Devices (for push notifications) // DeviceUpsert creates or updates a device record DeviceUpsert(uid t.Uid, dev *t.DeviceDef) error // DeviceGetAll returns all devices for a given set of users DeviceGetAll(uid ...t.Uid) (map[t.Uid][]t.DeviceDef, int, error) // DeviceDelete deletes a device record DeviceDelete(uid t.Uid, deviceID string) error // File upload records. The files are stored outside of the database. // FileStartUpload initializes a file upload. FileStartUpload(fd *t.FileDef) error // FileFinishUpload marks file upload as completed, successfully or otherwise. FileFinishUpload(fd *t.FileDef, success bool, size int64) (*t.FileDef, error) // FileGet fetches a record of a specific file FileGet(fid string) (*t.FileDef, error) // FileDeleteUnused deletes records where UseCount is zero. If olderThan is non-zero, deletes // unused records with UpdatedAt before olderThan. // Returns array of FileDef.Location of deleted filerecords so actual files can be deleted too. FileDeleteUnused(olderThan time.Time, limit int) ([]string, error) // FileLinkAttachments connects given topic or message to the file record IDs from the list. FileLinkAttachments(topic string, userId, msgId t.Uid, fids []string) error // Persistent cache management. // PCacheGet reads a persistent cache entry. PCacheGet(key string) (string, error) // PCacheUpsert creates or updates a persistent cache entry. PCacheUpsert(key string, value string, failOnDuplicate bool) error // PCacheDelete deletes a single persistent cache entry. PCacheDelete(key string) error // PCacheExpire expires older entries with the specified key prefix. PCacheExpire(keyPrefix string, olderThan time.Time) error // Testing // GetTestDB returns a currently open database connection. GetTestDB() any } ================================================ FILE: server/db/common/common.go ================================================ // Package common contains utility methods used by all adapters. package common import ( "encoding/json" "sort" "strconv" "strings" "time" "github.com/tinode/chat/server/auth" "github.com/tinode/chat/server/store" t "github.com/tinode/chat/server/store/types" ) type AuthRecord struct { Unique string `json:"unique" bson:"_id"` UserId string `json:"userid"` Scheme string `json:"scheme"` AuthLvl auth.Level `json:"authLvl"` Secret []byte `json:"secret"` Expires time.Time `json:"expires"` } // SelectEarliestUpdatedSubs selects no more than the given number of subscriptions from the // given slice satisfying the query. When the number of subscriptions is greater than the limit, // the subscriptions with the earliest timestamp are selected. func SelectEarliestUpdatedSubs(subs []t.Subscription, opts *t.QueryOpt, maxResults int) []t.Subscription { limit := maxResults ims := time.Time{} if opts != nil { if opts.Limit > 0 && opts.Limit < limit { limit = opts.Limit } if opts.IfModifiedSince != nil { ims = *opts.IfModifiedSince } } // No cache management and the number of results is below the limit: return all. if ims.IsZero() && len(subs) <= limit { return subs } // Now that we fetched potentially more subscriptions than needed, we got to take those with the oldest modifications. // Sorting in ascending order by modification time. sort.Slice(subs, func(i, j int) bool { return subs[i].LastModified().Before(subs[j].LastModified()) }) if !ims.IsZero() { // Keep only those subscriptions which are newer than ims. at := sort.Search(len(subs), func(i int) bool { return subs[i].LastModified().After(ims) }) subs = subs[at:] } // Trim slice at the limit. if len(subs) > limit { subs = subs[:limit] } return subs } // SelectLatestTime picks the latest update timestamp out of the two. func SelectLatestTime(t1, t2 time.Time) time.Time { if t1.Before(t2) { // Subscription has not changed recently, use user's update timestamp. return t2 } return t1 } // RangesToSql converts a slice of ranges to SQL BETWEEN or IN() constraint and arguments. func RangesToSql(in []t.Range) (string, []any) { if len(in) > 1 || in[0].Hi == 0 { var args []any for _, r := range in { if r.Hi == 0 { args = append(args, r.Low) } else { for i := r.Low; i < r.Hi; i++ { args = append(args, i) } } } return "IN (?" + strings.Repeat(",?", len(args)-1) + ")", args } // Optimizing for a special case of single range low..hi. // SQL's BETWEEN is inclusive-inclusive thus decrement Hi by 1. return "BETWEEN ? AND ?", []any{in[0].Low, in[0].Hi - 1} } // DisjunctionSql converts a slice of disjunctions to SQL HAVING clause and arguments. func DisjunctionSql(req [][]string, fieldName string) (string, []any) { var args []any counts := make([]string, 0, len(req)) for _, reqDisjunction := range req { // At least one of the tags must be present. if len(reqDisjunction) == 0 { continue } counts = append(counts, "COUNT("+fieldName+" IN (?"+strings.Repeat(",?", len(reqDisjunction)-1)+") OR NULL)>=1") for _, tag := range reqDisjunction { args = append(args, tag) } } return "HAVING " + strings.Join(counts, " AND ") + " ", args } // FilterFoundTags keeps only those tags in setTags that are present in the index. func FilterFoundTags(setTags t.StringSlice, index map[string]struct{}) []string { foundTags := make([]string, 0, 1) for _, tag := range setTags { if _, ok := index[tag]; ok { foundTags = append(foundTags, tag) } } return foundTags } // Convert to JSON before storing to JSON field. func ToJSON(src any) []byte { if src == nil { return nil } jval, _ := json.Marshal(src) return jval } // Deserialize JSON data from DB. func FromJSON(src any) any { if src == nil { return nil } if bb, ok := src.([]byte); ok { var out any json.Unmarshal(bb, &out) return out } return nil } // Convert update to a list of columns and arguments. func UpdateByMap(update map[string]any) (cols []string, args []any) { for col, arg := range update { col = strings.ToLower(col) if col == "public" || col == "trusted" || col == "private" || col == "aux" { arg = ToJSON(arg) } cols = append(cols, col+"=?") args = append(args, arg) } return } // If Tags field is updated, get the tags so tags table cab be updated too. func ExtractTags(update map[string]any) []string { var tags []string if val := update["Tags"]; val != nil { tags, _ = val.(t.StringSlice) } return []string(tags) } // EncodeUidString takes decoded string representation of int64, produce UID. // UIDs are stored as decoded int64 values. func EncodeUidString(str string) t.Uid { unum, _ := strconv.ParseInt(str, 10, 64) return store.EncodeUid(unum) } // DecodeUidString takes UID as string, converts it to int64 representation. // UIDs are stored as decoded int64 values. func DecodeUidString(str string) int64 { uid := t.ParseUid(str) return store.DecodeUid(uid) } ================================================ FILE: server/db/common/common_test.go ================================================ package common import ( "encoding/json" "reflect" "strings" "testing" "time" "github.com/tinode/chat/server/store/types" ) func genTestData() []types.Subscription { var testData = []types.Subscription{ {ObjHeader: types.ObjHeader{Id: "1", UpdatedAt: time.Date(2021, time.June, 1, 1, 11, 0, 0, time.Local)}}, {ObjHeader: types.ObjHeader{Id: "2", UpdatedAt: time.Date(2021, time.June, 2, 2, 12, 0, 0, time.Local)}}, {ObjHeader: types.ObjHeader{Id: "3", UpdatedAt: time.Date(2021, time.June, 3, 3, 13, 0, 0, time.Local)}}, {ObjHeader: types.ObjHeader{Id: "4", UpdatedAt: time.Date(2021, time.June, 4, 4, 14, 0, 0, time.Local)}}, {ObjHeader: types.ObjHeader{Id: "5", UpdatedAt: time.Date(2021, time.June, 5, 5, 15, 0, 0, time.Local)}}, {ObjHeader: types.ObjHeader{Id: "6", UpdatedAt: time.Date(2021, time.June, 6, 6, 16, 0, 0, time.Local)}}, {ObjHeader: types.ObjHeader{Id: "7", UpdatedAt: time.Date(2021, time.June, 7, 7, 17, 0, 0, time.Local)}}, {ObjHeader: types.ObjHeader{Id: "8", UpdatedAt: time.Date(2021, time.June, 8, 8, 18, 0, 0, time.Local)}}, {ObjHeader: types.ObjHeader{Id: "9", UpdatedAt: time.Date(2021, time.June, 9, 9, 19, 0, 0, time.Local)}}, {ObjHeader: types.ObjHeader{Id: "10", UpdatedAt: time.Date(2021, time.June, 10, 10, 20, 0, 0, time.Local)}}, } // TouchedAt is either greater or equal to UpdatedAt. testData[0].SetTouchedAt(time.Date(2021, time.June, 1, 1, 11, 0, 0, time.Local)) // 1 testData[1].SetTouchedAt(time.Date(2021, time.June, 4, 4, 12, 0, 0, time.Local)) // 3 testData[2].SetTouchedAt(time.Date(2021, time.June, 4, 2, 13, 0, 0, time.Local)) // 2 testData[3].SetTouchedAt(time.Date(2021, time.June, 4, 4, 14, 0, 0, time.Local)) // 4 testData[4].SetTouchedAt(time.Date(2021, time.June, 7, 5, 15, 0, 0, time.Local)) // 6 testData[5].SetTouchedAt(time.Date(2021, time.June, 6, 6, 16, 0, 0, time.Local)) // 5 testData[6].SetTouchedAt(time.Date(2021, time.June, 7, 7, 17, 0, 0, time.Local)) // 7 testData[7].SetTouchedAt(time.Date(2021, time.June, 9, 8, 18, 0, 0, time.Local)) // 8 testData[8].SetTouchedAt(time.Date(2021, time.June, 10, 11, 19, 0, 0, time.Local)) // 10 testData[9].SetTouchedAt(time.Date(2021, time.June, 10, 10, 20, 0, 0, time.Local)) // 9 return testData } func TestSelectEarliestUpdatedSubs(t *testing.T) { getOrder := func(subs []types.Subscription) string { var order []string for i := range subs { order = append(order, subs[i].Id) } return strings.Join(order, ",") } subs := SelectEarliestUpdatedSubs(genTestData(), nil, 100) // No sorting when returning the full set. expectedOrder := "1,2,3,4,5,6,7,8,9,10" sortOrder := getOrder(subs) if sortOrder != expectedOrder { t.Error("Wrong results returned. Expected:", expectedOrder, "; Got:", sortOrder) } // Sorted, oldest 9 results. subs = SelectEarliestUpdatedSubs(genTestData(), nil, 9) expectedOrder = "1,3,2,4,6,5,7,8,10" sortOrder = getOrder(subs) if sortOrder != expectedOrder { t.Error("Limited query returned wrong results. Expected:", expectedOrder, "; Got:", sortOrder) } // Sorted, oldest 9 results. subs = SelectEarliestUpdatedSubs(genTestData(), &types.QueryOpt{Limit: 20}, 9) expectedOrder = "1,3,2,4,6,5,7,8,10" sortOrder = getOrder(subs) if sortOrder != expectedOrder { t.Error("Limited query (2) returned wrong results. Expected:", expectedOrder, "; Got:", sortOrder) } // Sorted, oldest 9 results. subs = SelectEarliestUpdatedSubs(genTestData(), &types.QueryOpt{Limit: 9}, 20) expectedOrder = "1,3,2,4,6,5,7,8,10" sortOrder = getOrder(subs) if sortOrder != expectedOrder { t.Error("Limited query (3) returned wrong results. Expected:", expectedOrder, "; Got:", sortOrder) } ims := time.Date(2021, time.June, 7, 8, 16, 15, 0, time.Local) subs = SelectEarliestUpdatedSubs(genTestData(), &types.QueryOpt{Limit: 6, IfModifiedSince: &ims}, 20) expectedOrder = "8,10,9" sortOrder = getOrder(subs) if sortOrder != expectedOrder { t.Error("Date & count limited query returned wrong results. Expected:", expectedOrder, "; Got:", sortOrder) } ims = time.Date(2021, time.June, 4, 4, 13, 15, 0, time.Local) subs = SelectEarliestUpdatedSubs(genTestData(), &types.QueryOpt{Limit: 3, IfModifiedSince: &ims}, 20) expectedOrder = "4,6,5" sortOrder = getOrder(subs) if sortOrder != expectedOrder { t.Error("Count & date limited query returned wrong results. Expected:", expectedOrder, "; Got:", sortOrder) } } func TestSelectLatestTime(t *testing.T) { t1 := time.Date(2021, time.June, 1, 10, 0, 0, 0, time.UTC) t2 := time.Date(2021, time.June, 2, 10, 0, 0, 0, time.UTC) // t1 is before t2, should return t2 result := SelectLatestTime(t1, t2) if !result.Equal(t2) { t.Errorf("Expected %v, got %v", t2, result) } // t2 is after t1, should return t2 result = SelectLatestTime(t2, t1) if !result.Equal(t2) { t.Errorf("Expected %v, got %v", t2, result) } // Equal times, should return either one (in this case t1) result = SelectLatestTime(t1, t1) if !result.Equal(t1) { t.Errorf("Expected %v, got %v", t1, result) } } func TestRangesToSql(t *testing.T) { // Test single range with Hi = 0 (IN clause) ranges := []types.Range{{Low: 5, Hi: 0}} sql, args := RangesToSql(ranges) expectedSql := "IN (?)" expectedArgs := []any{5} if sql != expectedSql { t.Errorf("Expected SQL '%s', got '%s'", expectedSql, sql) } if !reflect.DeepEqual(args, expectedArgs) { t.Errorf("Expected args %v, got %v", expectedArgs, args) } // Test single range with Hi > 0 (BETWEEN clause) ranges = []types.Range{{Low: 5, Hi: 8}} sql, args = RangesToSql(ranges) expectedSql = "BETWEEN ? AND ?" expectedArgs = []any{5, 7} // Hi-1 for BETWEEN if sql != expectedSql { t.Errorf("Expected SQL '%s', got '%s'", expectedSql, sql) } if !reflect.DeepEqual(args, expectedArgs) { t.Errorf("Expected args %v, got %v", expectedArgs, args) } // Test multiple ranges (IN clause) ranges = []types.Range{{Low: 1, Hi: 3}, {Low: 5, Hi: 0}, {Low: 8, Hi: 10}} sql, args = RangesToSql(ranges) expectedSql = "IN (?,?,?,?,?)" expectedArgs = []any{1, 2, 5, 8, 9} if sql != expectedSql { t.Errorf("Expected SQL '%s', got '%s'", expectedSql, sql) } if !reflect.DeepEqual(args, expectedArgs) { t.Errorf("Expected args %v, got %v", expectedArgs, args) } } func TestDisjunctionSql(t *testing.T) { // Test single disjunction req := [][]string{{"tag1", "tag2", "tag3"}} sql, args := DisjunctionSql(req, "tagname") expectedSql := "HAVING COUNT(tagname IN (?,?,?) OR NULL)>=1 " expectedArgs := []any{"tag1", "tag2", "tag3"} if sql != expectedSql { t.Errorf("Expected SQL '%s', got '%s'", expectedSql, sql) } if !reflect.DeepEqual(args, expectedArgs) { t.Errorf("Expected args %v, got %v", expectedArgs, args) } // Test multiple disjunctions req = [][]string{{"tag1", "tag2"}, {"tag3"}, {"tag4", "tag5"}} sql, args = DisjunctionSql(req, "fieldname") expectedSql = "HAVING COUNT(fieldname IN (?,?) OR NULL)>=1 AND COUNT(fieldname IN (?) OR NULL)>=1 AND COUNT(fieldname IN (?,?) OR NULL)>=1 " expectedArgs = []any{"tag1", "tag2", "tag3", "tag4", "tag5"} if sql != expectedSql { t.Errorf("Expected SQL '%s', got '%s'", expectedSql, sql) } if !reflect.DeepEqual(args, expectedArgs) { t.Errorf("Expected args %v, got %v", expectedArgs, args) } // Test empty disjunctions (should be skipped) req = [][]string{{"tag1"}, {}, {"tag2"}} sql, args = DisjunctionSql(req, "fieldname") expectedSql = "HAVING COUNT(fieldname IN (?) OR NULL)>=1 AND COUNT(fieldname IN (?) OR NULL)>=1 " expectedArgs = []any{"tag1", "tag2"} if sql != expectedSql { t.Errorf("Expected SQL '%s', got '%s'", expectedSql, sql) } if !reflect.DeepEqual(args, expectedArgs) { t.Errorf("Expected args %v, got %v", expectedArgs, args) } } func TestFilterFoundTags(t *testing.T) { setTags := types.StringSlice{"tag1", "tag2", "tag3", "tag4", "tag5"} index := map[string]struct{}{ "tag1": {}, "tag3": {}, "tag5": {}, "tag6": {}, // Not in setTags } result := FilterFoundTags(setTags, index) expected := []string{"tag1", "tag3", "tag5"} if !reflect.DeepEqual(result, expected) { t.Errorf("Expected %v, got %v", expected, result) } // Test with empty index emptyIndex := map[string]struct{}{} result = FilterFoundTags(setTags, emptyIndex) expected = []string{} if !reflect.DeepEqual(result, expected) { t.Errorf("Expected %v, got %v", expected, result) } // Test with empty setTags emptyTags := types.StringSlice{} result = FilterFoundTags(emptyTags, index) expected = []string{} if !reflect.DeepEqual(result, expected) { t.Errorf("Expected %v, got %v", expected, result) } } func TestToJSON(t *testing.T) { // Test with nil result := ToJSON(nil) if result != nil { t.Errorf("Expected nil, got %v", result) } // Test with string input := "test string" result = ToJSON(input) expected := []byte(`"test string"`) if !reflect.DeepEqual(result, expected) { t.Errorf("Expected %v, got %v", expected, result) } // Test with map input2 := map[string]any{"key": "value", "number": 42} result = ToJSON(input2) // Parse back to verify var parsed map[string]any if err := json.Unmarshal(result, &parsed); err != nil { t.Errorf("Failed to unmarshal result: %v", err) } if parsed["key"] != "value" || parsed["number"] != float64(42) { t.Errorf("JSON conversion failed, got %v", parsed) } } func TestFromJSON(t *testing.T) { // Test with nil result := FromJSON(nil) if result != nil { t.Errorf("Expected nil, got %v", result) } // Test with valid JSON bytes input := []byte(`{"key": "value", "number": 42}`) result = FromJSON(input) if resultMap, ok := result.(map[string]any); ok { if resultMap["key"] != "value" || resultMap["number"] != float64(42) { t.Errorf("JSON deserialization failed, got %v", resultMap) } } else { t.Errorf("Expected map[string]any, got %T", result) } // Test with invalid JSON bytes invalidInput := []byte(`{invalid json}`) result = FromJSON(invalidInput) if result != nil { t.Errorf("Expected nil for invalid JSON, got %v", result) } // Test with non-byte slice stringInput := "not bytes" result = FromJSON(stringInput) if result != nil { t.Errorf("Expected nil for non-byte input, got %v", result) } } func TestUpdateByMap(t *testing.T) { update := map[string]any{ "Name": "John Doe", "Age": 30, "Public": map[string]string{"avatar": "url"}, "Private": map[string]string{"email": "john@example.com"}, "Trusted": map[string]bool{"verified": true}, "UpdatedAt": time.Now(), } cols, args := UpdateByMap(update) // Check that we have the right number of columns and args if len(cols) != len(args) || len(cols) != len(update) { t.Errorf("Expected %d columns and args, got %d cols and %d args", len(update), len(cols), len(args)) } // Verify column format for _, col := range cols { if !strings.Contains(col, "=?") { t.Errorf("Column should contain '=?', got %s", col) } } // Check that JSON fields are properly handled foundPublic := false foundPrivate := false foundTrusted := false for i, col := range cols { if strings.HasPrefix(col, "public=?") { foundPublic = true // Should be JSON bytes if _, ok := args[i].([]byte); !ok { t.Errorf("Public field should be []byte, got %T", args[i]) } } if strings.HasPrefix(col, "private=?") { foundPrivate = true if _, ok := args[i].([]byte); !ok { t.Errorf("Private field should be []byte, got %T", args[i]) } } if strings.HasPrefix(col, "trusted=?") { foundTrusted = true if _, ok := args[i].([]byte); !ok { t.Errorf("Trusted field should be []byte, got %T", args[i]) } } } if !foundPublic || !foundPrivate || !foundTrusted { t.Error("Missing JSON fields in output") } } func TestExtractTags(t *testing.T) { // Test with Tags field present update := map[string]any{ "Name": "John", "Tags": types.StringSlice{"tag1", "tag2", "tag3"}, "Age": 30, } tags := ExtractTags(update) expected := []string{"tag1", "tag2", "tag3"} if !reflect.DeepEqual(tags, expected) { t.Errorf("Expected %v, got %v", expected, tags) } // Test with no Tags field update = map[string]any{ "Name": "John", "Age": 30, } tags = ExtractTags(update) expected = nil if !reflect.DeepEqual(tags, expected) { t.Errorf("Expected %+v, got %+v", expected, tags) } // Test with nil Tags field update = map[string]any{ "Name": "John", "Tags": nil, "Age": 30, } tags = ExtractTags(update) expected = nil if !reflect.DeepEqual(tags, expected) { t.Errorf("Expected %+v, got %+v", expected, tags) } // Test with wrong type for Tags field update = map[string]any{ "Name": "John", "Tags": "not a slice", "Age": 30, } tags = ExtractTags(update) expected = nil if !reflect.DeepEqual(tags, expected) { t.Errorf("Expected %+v, got %+v", expected, tags) } } ================================================ FILE: server/db/common/test_data/test_data.go ================================================ package test_data import ( "time" "github.com/tinode/chat/server/auth" "github.com/tinode/chat/server/db/common" "github.com/tinode/chat/server/store/types" ) type TestData struct { UGen *types.UidGenerator Users []*types.User Creds []*types.Credential Recs []common.AuthRecord Topics []*types.Topic Subs []*types.Subscription Msgs []*types.Message Devs []*types.DeviceDef Files []*types.FileDef // Tags: add, remove, reset Tags [][]string Now time.Time } func initUsers(now time.Time) []*types.User { users := make([]*types.User, 0, 3) users = append(users, &types.User{ ObjHeader: types.ObjHeader{ Id: "3ysxkod5hNM", }, UserAgent: "SomeAgent v1.2.3", Tags: []string{"alice"}, }) users = append(users, &types.User{ ObjHeader: types.ObjHeader{ Id: "9AVDamaNCRY", }, UserAgent: "Tinode Web v111.222.333", Tags: []string{"bob"}, }) users = append(users, &types.User{ ObjHeader: types.ObjHeader{ Id: "0QLrX3WPS2o", }, UserAgent: "Tindroid v1.2.3", Tags: []string{"carol"}, }) for _, user := range users { // Initialize timestamps. user.InitTimes() // Assign user.id from user.Id. user.Uid() } deletedAt := now.Add(10 * time.Minute) users[2].State = types.StateDeleted users[2].StateAt = &deletedAt return users } func initCreds(now time.Time, users []*types.User) []*types.Credential { creds := make([]*types.Credential, 0, 6) creds = append(creds, &types.Credential{ // 0 User: users[0].Id, Method: "email", Value: "alice@test.example.com", Done: true, }) creds = append(creds, &types.Credential{ // 1 User: users[1].Id, Method: "email", Value: "bob@test.example.com", Done: true, }) creds = append(creds, &types.Credential{ // 2 User: users[1].Id, Method: "email", Value: "bob@test.example.com", }) creds = append(creds, &types.Credential{ // 3 User: users[2].Id, Method: "tel", Value: "+998991112233", }) creds = append(creds, &types.Credential{ // 4 User: users[2].Id, Method: "tel", Value: "+998993332211", Done: true, }) creds = append(creds, &types.Credential{ // 5 User: users[2].Id, Method: "email", Value: "asdf@example.com", }) for _, cred := range creds { cred.InitTimes() } creds[3].CreatedAt = now.Add(-10 * time.Minute) creds[3].UpdatedAt = now.Add(-10 * time.Minute) return creds } func initAuthRecords(now time.Time, users []*types.User) []common.AuthRecord { recs := make([]common.AuthRecord, 0, 2) recs = append(recs, common.AuthRecord{ Unique: "basic:alice", UserId: users[0].Id, Scheme: "basic", AuthLvl: auth.LevelAuth, Secret: []byte{'a', 'l', 'i', 'c', 'e'}, Expires: now.Add(24 * time.Hour), }) recs = append(recs, common.AuthRecord{ Unique: "basic:bob", UserId: users[1].Id, Scheme: "basic", AuthLvl: auth.LevelAuth, Secret: []byte{'b', 'o', 'b'}, Expires: now.Add(24 * time.Hour), }) return recs } func initTopics(now time.Time, users []*types.User) []*types.Topic { topics := make([]*types.Topic, 0, 5) topics = append(topics, &types.Topic{ ObjHeader: types.ObjHeader{ Id: "grpgRXf0rU4uR4", CreatedAt: now.Add(10 * time.Minute), UpdatedAt: now, }, TouchedAt: now, Owner: users[0].Id, SeqId: 111, Tags: []string{"travel", "zxcv"}, SubCnt: 2, }) topics = append(topics, &types.Topic{ ObjHeader: types.ObjHeader{ Id: "p2p9AVDamaNCRbfKzGSh3mE0w", CreatedAt: now, UpdatedAt: now, }, TouchedAt: now, SeqId: 12, }) topics = append(topics, &types.Topic{ ObjHeader: types.ObjHeader{ Id: "p2pxQLrX3WPS2rfKzGSh3mE0w", CreatedAt: now, UpdatedAt: now, }, TouchedAt: now, SeqId: 15, }) topics = append(topics, &types.Topic{ ObjHeader: types.ObjHeader{ Id: "p2pE1iE7I9JN5ESv44HiLbj1A", CreatedAt: now, UpdatedAt: now, }, TouchedAt: now, SeqId: 555, }) topics = append(topics, &types.Topic{ ObjHeader: types.ObjHeader{ Id: "p2pQvr1xwKU01LfKzGSh3mE0w", CreatedAt: now, UpdatedAt: now, }, TouchedAt: now, SeqId: 333, }) return topics } func initSubs(now time.Time, users []*types.User, topics []*types.Topic) []*types.Subscription { subs := make([]*types.Subscription, 0, 6) subs = append(subs, &types.Subscription{ ObjHeader: types.ObjHeader{ CreatedAt: now, UpdatedAt: now.Add(10 * time.Minute), }, User: users[0].Id, Topic: topics[0].Id, RecvSeqId: 5, ReadSeqId: 1, ModeWant: 255, ModeGiven: 255, }) subs = append(subs, &types.Subscription{ ObjHeader: types.ObjHeader{ CreatedAt: now, UpdatedAt: now.Add(15 * time.Minute), }, User: users[1].Id, Topic: topics[0].Id, RecvSeqId: 6, ReadSeqId: 3, ModeWant: 47, ModeGiven: 47, }) subs = append(subs, &types.Subscription{ ObjHeader: types.ObjHeader{ CreatedAt: now.Add(-10 * time.Hour), UpdatedAt: now.Add(-10 * time.Hour), }, User: users[0].Id, Topic: topics[1].Id, RecvSeqId: 9, ReadSeqId: 5, ModeWant: 47, ModeGiven: 47, }) subs = append(subs, &types.Subscription{ ObjHeader: types.ObjHeader{ CreatedAt: now, UpdatedAt: now.Add(20 * time.Minute), }, User: users[1].Id, Topic: topics[1].Id, RecvSeqId: 9, ReadSeqId: 5, ModeWant: 47, ModeGiven: 47, }) subs = append(subs, &types.Subscription{ ObjHeader: types.ObjHeader{ CreatedAt: now, UpdatedAt: now.Add(30 * time.Minute), }, User: users[2].Id, Topic: topics[2].Id, RecvSeqId: 0, ReadSeqId: 0, ModeWant: 47, ModeGiven: 47, }) subs = append(subs, &types.Subscription{ ObjHeader: types.ObjHeader{ CreatedAt: now, UpdatedAt: now.Add(40 * time.Minute), }, User: users[2].Id, Topic: topics[3].Id, RecvSeqId: 555, ReadSeqId: 455, ModeWant: 47, ModeGiven: 47, }) for _, sub := range subs { sub.SetTouchedAt(now) } return subs } func initMessages(users []*types.User, topics []*types.Topic) []*types.Message { msgs := make([]*types.Message, 0, 6) msgs = append(msgs, &types.Message{ SeqId: 1, Topic: topics[0].Id, From: users[0].Id, Content: "msg1", }) msgs = append(msgs, &types.Message{ SeqId: 2, Topic: topics[0].Id, From: users[2].Id, Content: "msg2", DeletedFor: []types.SoftDelete{{ User: users[0].Id, DelId: 1}}, }) msgs = append(msgs, &types.Message{ SeqId: 3, Topic: topics[0].Id, From: users[0].Id, Content: "msg31", }) msgs = append(msgs, &types.Message{ SeqId: 1, Topic: topics[1].Id, From: users[1].Id, Content: "msg1", }) msgs = append(msgs, &types.Message{ SeqId: 5, Topic: topics[1].Id, From: users[1].Id, Content: "msg2", }) msgs = append(msgs, &types.Message{ SeqId: 11, Topic: topics[1].Id, From: users[0].Id, Content: "msg3", }) for i, msg := range msgs { msg.InitTimes() msg.SetUid(types.Uid(i + 1)) } return msgs } func initDevices(now time.Time) []*types.DeviceDef { devs := make([]*types.DeviceDef, 0, 2) devs = append(devs, &types.DeviceDef{ DeviceId: "2934ujfoviwj09ntf094", Platform: "Android", LastSeen: now, Lang: "en_EN", }) devs = append(devs, &types.DeviceDef{ DeviceId: "pogpjb023b09gfdmp", Platform: "iOS", LastSeen: now, Lang: "en_EN", }) return devs } func initFileDefs(now time.Time, users []*types.User) []*types.FileDef { files := make([]*types.FileDef, 0, 2) files = append(files, &types.FileDef{ ObjHeader: types.ObjHeader{ CreatedAt: now, UpdatedAt: now, }, Status: types.UploadStarted, User: users[0].Id, MimeType: "application/pdf", Location: "uploads/qwerty.pdf", Size: 123456, }) files = append(files, &types.FileDef{ ObjHeader: types.ObjHeader{ CreatedAt: now.Add(60 * time.Minute), UpdatedAt: now.Add(60 * time.Minute), }, Status: types.UploadStarted, User: users[0].Id, Location: "uploads/asdf.txt", Size: 654321, }) files[0].SetUid(types.Uid(1001)) files[1].SetUid(types.Uid(1002)) return files } func initTags() [][]string { // Tags must be lowercase and non-repeating. addTags := []string{"tag1", "alice"} removeTags := []string{"alice", "tag1", "tag2"} resetTags := []string{"alice", "tag111", "tag333"} return [][]string{addTags, removeTags, resetTags} } func InitTestData() *TestData { // Use fixed timestamp to make tests more predictable var now = time.Date(2021, time.June, 12, 11, 39, 24, 15, time.Local).UTC().Round(time.Millisecond) var uGen = &types.UidGenerator{} if err := uGen.Init(11, []byte("testtesttesttest")); err != nil { return nil } var users = initUsers(now) var topics = initTopics(now, users) return &TestData{ UGen: uGen, Users: users, Creds: initCreds(now, users), Recs: initAuthRecords(now, users), Topics: topics, Subs: initSubs(now, users, topics), Msgs: initMessages(users, topics), Devs: initDevices(now), Files: initFileDefs(now, users), Tags: initTags(), Now: now, } } ================================================ FILE: server/db/mongodb/adapter.go ================================================ //go:build mongodb // Package mongodb is a database adapter for MongoDB. package mongodb import ( "context" "crypto/tls" "encoding/json" "errors" "slices" "sort" "strconv" "strings" "time" "github.com/tinode/chat/server/auth" "github.com/tinode/chat/server/db/common" "github.com/tinode/chat/server/store" t "github.com/tinode/chat/server/store/types" b "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" mdb "go.mongodb.org/mongo-driver/mongo" mdbopts "go.mongodb.org/mongo-driver/mongo/options" ) // adapter holds MongoDB connection data. type adapter struct { conn *mdb.Client db *mdb.Database dbName string // Maximum number of records to return maxResults int // Maximum number of message records to return maxMessageResults int version int ctx context.Context useTransactions bool } const ( adpVersion = 116 adapterName = "mongodb" defaultHost = "localhost:27017" defaultDatabase = "tinode" defaultMaxResults = 1024 // This is capped by the Session's send queue limit (128). defaultMaxMessageResults = 100 defaultAuthMechanism = "SCRAM-SHA-256" defaultAuthSource = "admin" ) // See https://godoc.org/go.mongodb.org/mongo-driver/mongo/options#ClientOptions for explanations. type configType struct { // Connection string URI https://www.mongodb.com/docs/manual/reference/connection-string/ Uri string `json:"uri,omitempty"` Addresses any `json:"addresses,omitempty"` ConnectTimeout int `json:"timeout,omitempty"` // Options separately from ClientOptions (custom options): Database string `json:"database,omitempty"` ReplicaSet string `json:"replica_set,omitempty"` AuthMechanism string `json:"auth_mechanism,omitempty"` AuthSource string `json:"auth_source,omitempty"` Username string `json:"username,omitempty"` Password string `json:"password,omitempty"` UseTLS bool `json:"tls,omitempty"` TlsCertFile string `json:"tls_cert_file,omitempty"` TlsPrivateKey string `json:"tls_private_key,omitempty"` InsecureSkipVerify bool `json:"tls_skip_verify,omitempty"` // The only version supported at this time is "1". APIVersion mdbopts.ServerAPIVersion `json:"api_version,omitempty"` } func (a *adapter) maybeStartTransaction(sess mdb.Session) error { if a.useTransactions { return sess.StartTransaction() } return nil } func (a *adapter) maybeCommitTransaction(ctx context.Context, sess mdb.Session) error { if a.useTransactions { return sess.CommitTransaction(ctx) } return nil } // Open initializes mongodb session func (a *adapter) Open(jsonconfig json.RawMessage) error { if a.conn != nil { return errors.New("adapter mongodb is already connected") } if len(jsonconfig) < 2 { return errors.New("adapter mongodb missing config") } var err error var config configType if err = json.Unmarshal(jsonconfig, &config); err != nil { return errors.New("adapter mongodb failed to parse config: " + err.Error()) } var opts mdbopts.ClientOptions if config.Addresses == nil { opts.SetHosts([]string{defaultHost}) } else if host, ok := config.Addresses.(string); ok { opts.SetHosts([]string{host}) } else if ihosts, ok := config.Addresses.([]any); ok && len(ihosts) > 0 { hosts := make([]string, len(ihosts)) for i, ih := range ihosts { h, ok := ih.(string) if !ok || h == "" { return errors.New("adapter mongodb invalid config.Addresses value") } hosts[i] = h } opts.SetHosts(hosts) } else { return errors.New("adapter mongodb failed to parse config.Addresses") } if config.Database == "" { a.dbName = defaultDatabase } else { a.dbName = config.Database } if config.ReplicaSet != "" { opts.SetReplicaSet(config.ReplicaSet) a.useTransactions = true } else { // Retriable writes are not supported in a standalone instance. opts.SetRetryWrites(false) } if config.Username != "" { if config.AuthMechanism == "" { config.AuthMechanism = defaultAuthMechanism } if config.AuthSource == "" { config.AuthSource = defaultAuthSource } var passwordSet bool if config.Password != "" { passwordSet = true } opts.SetAuth( mdbopts.Credential{ AuthMechanism: config.AuthMechanism, AuthSource: config.AuthSource, Username: config.Username, Password: config.Password, PasswordSet: passwordSet, }) } if config.UseTLS { tlsConfig := tls.Config{ InsecureSkipVerify: config.InsecureSkipVerify, } if config.TlsCertFile != "" { cert, err := tls.LoadX509KeyPair(config.TlsCertFile, config.TlsPrivateKey) if err != nil { return err } tlsConfig.Certificates = append(tlsConfig.Certificates, cert) } opts.SetTLSConfig(&tlsConfig) } if a.maxResults <= 0 { a.maxResults = defaultMaxResults } if a.maxMessageResults <= 0 { a.maxMessageResults = defaultMaxMessageResults } // Connection string URI overrides any other options configured earlier. if config.Uri != "" { opts.ApplyURI(config.Uri) } if config.APIVersion != "" { opts.SetServerAPIOptions(mdbopts.ServerAPI(config.APIVersion)) } // Make sure the options are sane. if err = opts.Validate(); err != nil { return err } a.ctx = context.Background() a.conn, err = mdb.Connect(a.ctx, &opts) a.db = a.conn.Database(a.dbName) if err != nil { return err } a.version = -1 return nil } // Close the adapter func (a *adapter) Close() error { var err error if a.conn != nil { err = a.conn.Disconnect(a.ctx) a.conn = nil a.version = -1 } return err } // IsOpen checks if the adapter is ready for use func (a *adapter) IsOpen() bool { return a.conn != nil } // GetDbVersion returns current database version. func (a *adapter) GetDbVersion() (int, error) { if a.version > 0 { return a.version, nil } var result struct { Key string `bson:"_id"` Value int } if err := a.db.Collection("kvmeta").FindOne(a.ctx, b.M{"_id": "version"}).Decode(&result); err != nil { if err == mdb.ErrNoDocuments { err = errors.New("Database not initialized") } return -1, err } a.version = result.Value return result.Value, nil } func (a *adapter) updateDbVersion(v int) error { a.version = -1 _, err := a.db.Collection("kvmeta").UpdateOne(a.ctx, b.M{"_id": "version"}, b.M{"$set": b.M{"value": v}}, ) return err } // CheckDbVersion checks if the actual database version matches adapter version. func (a *adapter) CheckDbVersion() error { version, err := a.GetDbVersion() if err != nil { return err } if version != adpVersion { return errors.New("Invalid database version " + strconv.Itoa(version) + ". Expected " + strconv.Itoa(adpVersion)) } return nil } // Version returns adapter version func (a *adapter) Version() int { return adpVersion } // DB connection stats object. func (a *adapter) Stats() any { if a.db == nil { return nil } var result b.M if err := a.db.RunCommand(a.ctx, b.D{{"serverStatus", 1}}, nil).Decode(&result); err != nil { return nil } return result["connections"] } // GetName returns the name of the adapter func (a *adapter) GetName() string { return adapterName } // SetMaxResults configures how many results can be returned in a single DB call. func (a *adapter) SetMaxResults(val int) error { if val <= 0 { a.maxResults = defaultMaxResults } else { a.maxResults = val } return nil } // CreateDb creates the database optionally dropping an existing database first. func (a *adapter) CreateDb(reset bool) error { if reset { if err := a.db.Drop(a.ctx); err != nil { return err } } else if a.isDbInitialized() { return errors.New("Database already initialized") } // Collections (tables) do not need to be explicitly created since MongoDB creates them with first write operation indexes := []struct { Collection string Field string IndexOpts mdb.IndexModel }{ // Users // Index on 'user.state' for finding suspended and soft-deleted users. { Collection: "users", Field: "state", }, // Index on 'user.tags' array so user can be found by tags. { Collection: "users", Field: "tags", }, // Index for 'user.devices.deviceid' to ensure Device ID uniqueness across users. // Partial filter set to avoid unique constraint for null values (when user object have no devices). { Collection: "users", IndexOpts: mdb.IndexModel{ Keys: b.M{"devices.deviceid": 1}, Options: mdbopts.Index(). SetUnique(true). SetPartialFilterExpression(b.M{"devices.deviceid": b.M{"$exists": true}}), }, }, // Index on lastSeen and updatedat for deleting stale user accounts. { Collection: "users", IndexOpts: mdb.IndexModel{Keys: b.D{{"lastseen", 1}, {"updatedat", 1}}}, }, // User authentication records {_id, userid, secret} // Should be able to access user's auth records by user id { Collection: "auth", Field: "userid", }, // Subscription to a topic. The primary key is a topic:user string { Collection: "subscriptions", Field: "user", }, { Collection: "subscriptions", Field: "topic", }, // Topics stored in database // Index on 'owner' field for deleting users. { Collection: "topics", Field: "owner", }, // Index on 'state' for finding suspended and soft-deleted topics. { Collection: "topics", Field: "state", }, // Index on 'topic.tags' array so topics can be found by tags. // These tags are not unique as opposite to 'user.tags'. { Collection: "topics", Field: "tags", }, // Stored message // Compound index of 'topic - seqid' for selecting messages in a topic. { Collection: "messages", IndexOpts: mdb.IndexModel{Keys: b.D{{"topic", 1}, {"seqid", 1}}}, }, // Compound index of hard-deleted messages { Collection: "messages", IndexOpts: mdb.IndexModel{Keys: b.D{{"topic", 1}, {"delid", 1}}}, }, // Compound multi-index of soft-deleted messages: each message gets multiple compound index entries like // [topic, user1, delid1], [topic, user2, delid2],... { Collection: "messages", IndexOpts: mdb.IndexModel{Keys: b.D{{"topic", 1}, {"deletedfor.user", 1}, {"deletedfor.delid", 1}}}, }, // Log of deleted messages // Compound index of 'topic - delid' { Collection: "dellog", IndexOpts: mdb.IndexModel{Keys: b.D{{"topic", 1}, {"delid", 1}}}, }, // User credentials - contact information such as "email:jdoe@example.com" or "tel:+18003287448": // Id: "method:credential" like "email:jdoe@example.com". See types.Credential. // Index on 'credentials.user' to be able to query credentials by user id. { Collection: "credentials", Field: "user", }, // Records of file uploads. See types.FileDef. // Index on 'fileuploads.usecount' to be able to delete unused records at once. { Collection: "fileuploads", Field: "usecount", }, } var err error for _, idx := range indexes { if idx.Field != "" { _, err = a.db.Collection(idx.Collection).Indexes().CreateOne(a.ctx, mdb.IndexModel{Keys: b.M{idx.Field: 1}}) } else { _, err = a.db.Collection(idx.Collection).Indexes().CreateOne(a.ctx, idx.IndexOpts) } if err != nil { return err } } // Collection "kvmeta" with metadata key-value pairs. // Key in "_id" field. // Record current DB version. if _, err := a.db.Collection("kvmeta").InsertOne(a.ctx, map[string]any{"_id": "version", "value": adpVersion}); err != nil { return err } // Create system topic 'sys'. return createSystemTopic(a) } // UpgradeDb upgrades database to the current adapter version. func (a *adapter) UpgradeDb() error { bumpVersion := func(a *adapter, x int) error { if err := a.updateDbVersion(x); err != nil { return err } _, err := a.GetDbVersion() return err } _, err := a.GetDbVersion() if err != nil { return err } if a.version == 110 { // Perform database upgrade from versions 110 to version 111. // Users // Reset previously unused field State to value StateOK. if _, err := a.db.Collection("users").UpdateMany(a.ctx, b.M{}, b.M{"$set": b.M{"state": t.StateOK}}); err != nil { return err } // Add StatusDeleted to all deleted users as indicated by DeletedAt not being null. if _, err := a.db.Collection("users").UpdateMany(a.ctx, b.M{"deletedat": b.M{"$ne": nil}}, b.M{"$set": b.M{"state": t.StateDeleted}}); err != nil { return err } // Rename DeletedAt into StateAt. Update only those rows which have defined DeletedAt. if _, err := a.db.Collection("users").UpdateMany(a.ctx, b.M{"deletedat": b.M{"$exists": true}}, b.M{"$rename": b.M{"deletedat": "stateat"}}); err != nil { return err } // Drop secondary index DeletedAt. if _, err := a.db.Collection("users").Indexes().DropOne(a.ctx, "deletedat_1"); err != nil { return err } // Create secondary index on State for finding suspended and soft-deleted topics. if _, err = a.db.Collection("users").Indexes().CreateOne(a.ctx, mdb.IndexModel{Keys: b.M{"state": 1}}); err != nil { return err } // Topics // Add StateDeleted to all topics with DeletedAt not null. if _, err := a.db.Collection("topics").UpdateMany(a.ctx, b.M{"deletedat": b.M{"$ne": nil}}, b.M{"$set": b.M{"state": t.StateDeleted}}); err != nil { return err } // Set StateOK for all other topics. if _, err := a.db.Collection("topics").UpdateMany(a.ctx, b.M{"state": b.M{"$exists": false}}, b.M{"$set": b.M{"state": t.StateOK}}); err != nil { return err } // Rename DeletedAt into StateAt. Update only those rows which have defined DeletedAt. if _, err := a.db.Collection("topics").UpdateMany(a.ctx, b.M{"deletedat": b.M{"$exists": true}}, b.M{"$rename": b.M{"deletedat": "stateat"}}); err != nil { return err } // Create secondary index on State for finding suspended and soft-deleted topics. if _, err = a.db.Collection("topics").Indexes().CreateOne(a.ctx, mdb.IndexModel{Keys: b.M{"state": 1}}); err != nil { return err } if err := bumpVersion(a, 111); err != nil { return err } } if a.version == 111 { // Just bump the version to keep in line with MySQL. if err := bumpVersion(a, 112); err != nil { return err } } if a.version == 112 { // Create secondary index on Users(lastseen,updatedat) for deleting stale user accounts. if _, err = a.db.Collection("users").Indexes().CreateOne(a.ctx, mdb.IndexModel{Keys: b.D{{"lastseen", 1}, {"updatedat", 1}}}); err != nil { return err } if err := bumpVersion(a, 113); err != nil { return err } } if a.version < 116 { // Version 114: topics.aux added, fileuploads.etag added. // Version 115: SQL indexes added. // Version 116: topics.subcnt added. if err := bumpVersion(a, 116); err != nil { return err } } if a.version != adpVersion { return errors.New("Failed to perform database upgrade to version " + strconv.Itoa(adpVersion) + ". DB is still at " + strconv.Itoa(a.version)) } return nil } // Create system topic 'sys'. func createSystemTopic(a *adapter) error { now := t.TimeNow() _, err := a.db.Collection("topics").InsertOne(a.ctx, &t.Topic{ ObjHeader: t.ObjHeader{ Id: "sys", CreatedAt: now, UpdatedAt: now}, TouchedAt: now, Access: t.DefaultAccess{Auth: t.ModeNone, Anon: t.ModeNone}, Public: map[string]any{"fn": "System"}, }) return err } // User management // UserCreate creates user record func (a *adapter) UserCreate(usr *t.User) error { if _, err := a.db.Collection("users").InsertOne(a.ctx, &usr); err != nil { return err } return nil } // UserGet fetches a single user by user id. If user is not found it returns (nil, nil) func (a *adapter) UserGet(id t.Uid) (*t.User, error) { var user t.User filter := b.M{"_id": id.String(), "state": b.M{"$ne": t.StateDeleted}} if err := a.db.Collection("users").FindOne(a.ctx, filter).Decode(&user); err != nil { if err == mdb.ErrNoDocuments { // User not found return nil, nil } else { return nil, err } } user.Public = unmarshalBsonD(user.Public) user.Trusted = unmarshalBsonD(user.Trusted) return &user, nil } // UserGetAll returns user records for a given list of user IDs func (a *adapter) UserGetAll(ids ...t.Uid) ([]t.User, error) { uids := make([]any, len(ids)) for i, id := range ids { uids[i] = id.String() } var users []t.User filter := b.M{"_id": b.M{"$in": uids}, "state": b.M{"$ne": t.StateDeleted}} cur, err := a.db.Collection("users").Find(a.ctx, filter) if err != nil { return nil, err } defer cur.Close(a.ctx) for cur.Next(a.ctx) { var user t.User if err := cur.Decode(&user); err != nil { return nil, err } user.Public = unmarshalBsonD(user.Public) user.Trusted = unmarshalBsonD(user.Trusted) users = append(users, user) } return users, nil } // UserDelete deletes specified user: wipes completely (hard-delete) or marks as deleted. func (a *adapter) UserDelete(uid t.Uid, hard bool) error { ownFilter := b.M{"owner": uid.String()} // In case of hard delete, delete all topics, even those which were // soft-deleted previsously. if !hard { ownFilter["state"] = b.M{"$ne": t.StateDeleted} } forUser := uid.String() // Select topics where the user is the owner. ownTopics, err := a.topicNamesForUser("topics", ownFilter, "_id", true) if err != nil { return err } ownTopicsFilter := b.M{"topic": b.M{"$in": ownTopics}} var sess mdb.Session if sess, err = a.conn.StartSession(); err != nil { return err } defer sess.EndSession(a.ctx) if err = a.maybeStartTransaction(sess); err != nil { return err } if err = mdb.WithSession(a.ctx, sess, func(sc mdb.SessionContext) error { if hard { // No need to delete user's devices: devices are stored in user's record and will be deleted with it. // Delete user's subscriptions in all topics and decrement subcnt in topic. if err = a.subsDelete(sc, b.M{"user": forUser}, true); err != nil { return err } // Delete user's dellog entries in all topics. err = a.clearUserDellog(sc, forUser) if err != nil { return err } // Can't delete user's messages in all topics because we cannot notify topics of such deletion. // Just leave the messages there marked as sent by "not found" user. // Delete topics where the user is the owner: if len(ownTopics) > 0 { // 1. Delete dellog // 2. Decrement fileuploads. // 3. Delete all messages. // 4. Delete subscriptions. // Delete dellog for topics owned by the user. _, err = a.db.Collection("dellog").DeleteMany(sc, ownTopicsFilter) if err != nil { return err } // Decrement fileuploads UseCounter // First get array of attachments IDs that were used in messages of topics from topicIds // Then decrement the usecount field of these file records err = a.decFileUseCounter(sc, "messages", ownTopicsFilter) if err != nil { return err } // Decrement use counter for topic avatars. err = a.decFileUseCounter(sc, "topics", b.M{"_id": b.M{"$in": ownTopics}}) if err != nil { return err } // Delete messages _, err = a.db.Collection("messages").DeleteMany(sc, ownTopicsFilter) if err != nil { return err } // Delete subscriptions for all users where the user is the owner of the topic. _, err = a.db.Collection("subscriptions").DeleteMany(sc, ownTopicsFilter) if err != nil { return err } // No need to delete topic tags: they are stored in topic record and will be deleted with it. // And finally delete the topics. if _, err = a.db.Collection("topics").DeleteMany(sc, b.M{"owner": forUser}); err != nil { return err } } // Delete user's authentication records. if _, err = a.authDelAllRecords(sc, uid); err != nil { return err } // Delete credentials. if err = a.credDel(sc, uid, "", ""); err != nil && err != t.ErrNotFound { return err } // Delete avatar (decrement use counter). if err = a.decFileUseCounter(sc, "users", b.M{"_id": forUser}); err != nil { return err } // No need to delete user's tags: they are stored in user's record and will be deleted with it. // And finally delete the user. if _, err = a.db.Collection("users").DeleteOne(sc, b.M{"_id": forUser}); err != nil { return err } } else { // Disable user's subscriptions. if err = a.subsDelete(sc, b.M{"user": forUser}, false); err != nil { return err } now := t.TimeNow() disable := b.M{"$set": b.M{"updatedat": now, "state": t.StateDeleted, "stateat": now}} if len(ownTopics) > 0 { // Disable subscriptions for topics where the user is the owner. if _, err = a.db.Collection("subscriptions").UpdateMany(sc, ownTopicsFilter, disable); err != nil { return err } // Disable group topics where the user is the owner. if _, err = a.db.Collection("topics").UpdateMany(sc, b.M{"_id": b.M{"$in": ownTopics}}, b.M{"$set": b.M{ "updatedat": now, "touchedat": now, "state": t.StateDeleted, "stateat": now, }}); err != nil { return err } } // Disable p2p topics with the user. p2pTopics, err := a.p2pTopicsForUser(uid) if err != nil { return err } if len(p2pTopics) > 0 { if _, err = a.db.Collection("topics").UpdateMany(sc, b.M{"_id": b.M{"$in": p2pTopics}}, b.M{"$set": b.M{ "updatedat": now, "touchedat": now, "state": t.StateDeleted, "stateat": now, }}); err != nil { return err } // Disable subscription to user's disabled p2p topics. if _, err = a.db.Collection("subscriptions").UpdateMany(sc, b.M{"topic": b.M{"$in": p2pTopics}}, disable); err != nil { return err } } // Finally disable the user. if _, err = a.db.Collection("users").UpdateMany(sc, b.M{"_id": forUser}, disable); err != nil { return err } } // Finally commit all changes return a.maybeCommitTransaction(sc, sess) }); err != nil { return err } return err } // topicStateForUser is called by UserUpdate when the update contains state change. // Soft-deleted topics remain soft-deleted. func (a *adapter) topicStateForUser(uid t.Uid, now time.Time, update any) error { state, ok := update.(t.ObjState) if !ok { return t.ErrMalformed } if now.IsZero() { now = t.TimeNow() } // Change state of all topics where the user is the owner. if _, err := a.db.Collection("topics").UpdateMany(a.ctx, b.M{"owner": uid.String(), "state": b.M{"$ne": t.StateDeleted}}, b.M{"$set": b.M{"state": state, "stateat": now}}); err != nil { return err } // Change state of p2p topics with the user (p2p topic's owner is blank) // Get list of p2p topics with the user. p2pTopics, err := a.p2pTopicsForUser(uid) if err != nil { return err } if len(p2pTopics) > 0 { if _, err := a.db.Collection("topics").UpdateMany(a.ctx, b.M{"_id": b.M{"$in": p2pTopics}, "state": b.M{"$ne": t.StateDeleted}}, b.M{"$set": b.M{"state": state, "stateat": now}}); err != nil { return err } } // Subscriptions don't need to be updated: // subscriptions of a disabled user are not disabled and still can be manipulated. return nil } // UserUpdate updates user record func (a *adapter) UserUpdate(uid t.Uid, update map[string]any) error { // Convert field names from CamelCase to lowercase. update = normalizeUpdateMap(update) _, err := a.db.Collection("users").UpdateOne(a.ctx, b.M{"_id": uid.String()}, b.M{"$set": update}) if err != nil { return err } if state, ok := update["state"]; ok { now, _ := update["stateat"].(time.Time) err = a.topicStateForUser(uid, now, state) } // Tags are stored in the same record, no need to update them separately. return err } // UserUpdateTags adds, removes, or resets user's tags. func (a *adapter) UserUpdateTags(uid t.Uid, add, remove, reset []string) ([]string, error) { var newTags t.StringSlice // Compare to nil vs checking for zero length: zero length reset is valid. if reset != nil { // Replace tags with the new value newTags = reset } else { var user t.User err := a.db.Collection("users").FindOne(a.ctx, b.M{"_id": uid.String()}).Decode(&user) if err != nil { return nil, err } // Mutate the tag list. newTags = user.Tags if len(add) > 0 { newTags = union(newTags, add) } if len(remove) > 0 { newTags = diff(newTags, remove) } } return newTags, a.UserUpdate(uid, map[string]any{"tags": newTags}) } // UserGetByCred returns user ID for the given validated credential. func (a *adapter) UserGetByCred(method, value string) (t.Uid, error) { var userId map[string]string err := a.db.Collection("credentials").FindOne(a.ctx, b.M{"_id": method + ":" + value}, mdbopts.FindOne().SetProjection(b.M{"user": 1, "_id": 0}), ).Decode(&userId) if err != nil { if err == mdb.ErrNoDocuments { return t.ZeroUid, nil } return t.ZeroUid, err } return t.ParseUid(userId["user"]), nil } // UserUnreadCount returns the total number of unread messages in all topics with // the R permission. If read fails, the counts are still returned with the original // user IDs but with the unread count undefined and non-nil error. // Does not count unread messages in channels although it probably should. func (a *adapter) UserUnreadCount(ids ...t.Uid) (map[t.Uid]int, error) { uids := make([]string, len(ids)) counts := make(map[t.Uid]int, len(ids)) for i, id := range ids { uids[i] = id.String() // Ensure all original uids are always present. counts[id] = 0 } /* Query: db.subscriptions.aggregate([ { $match: { user: { $in: ["KnElfSSA21U", "0ZcCQmwI2RI"] } } }, { $lookup: { from: "topics", localField: "topic", foreignField: "_id", as: "fromTopics"} }, { $match: { fromTopics: { $not: {$size: 0} }}}, { $replaceRoot: { newRoot: { $mergeObjects: [ {$arrayElemAt: [ "$fromTopics", 0 ]} , "$$ROOT" ] } } }, { $match: { deletedat: { $exists: false }, state: { $ne: t.StateDeleted }, modewant: { $bitsAllSet: [ t.ModeRead ] }, modegiven: { $bitsAllSet: [ t.ModeRead ] } } }, { $project: { _id: 0, user: 1, readseqid: 1, seqid: 1} }, { $group: { _id: "$user", unreadCount: { $sum: { $subtract: [ "$seqid", "$readseqid" ] } } } } ]) Result: { "_id" : "KnElfSSA21U", "unreadCount" : 0 } { "_id" : "0ZcCQmwI2RI", "unreadCount" : 7 } */ pipeline := b.A{ b.M{"$match": b.M{"user": b.M{"$in": uids}}}, // Join documents from two collection. // FIXME: this does not work for channels as localField[topic] is not the same as foreignField[_id]. b.M{"$lookup": b.M{ "from": "topics", "localField": "topic", "foreignField": "_id", "as": "fromTopics"}, }, // Remove users with no subscriptions. b.M{"$match": b.M{"fromTopics": b.M{"$not": b.M{"$size": 0}}}}, // Merge two documents into one b.M{"$replaceRoot": b.M{"newRoot": b.M{"$mergeObjects": b.A{b.M{"$arrayElemAt": b.A{"$fromTopics", 0}}, "$$ROOT"}}}}, // Keep only those records which affect the result. b.M{"$match": b.M{ "deletedat": b.M{"$exists": false}, "state": b.M{"$ne": t.StateDeleted}, // Filter by access mode "modewant": b.M{"$bitsAllSet": b.A{t.ModeRead}}, "modegiven": b.M{"$bitsAllSet": b.A{t.ModeRead}}}}, // Remove unused fields. b.M{"$project": b.M{"_id": 0, "user": 1, "readseqid": 1, "seqid": 1}}, // GROUP BY user. b.M{"$group": b.M{"_id": "$user", "unreadCount": b.M{"$sum": b.M{"$subtract": b.A{"$seqid", "$readseqid"}}}}}, } cur, err := a.db.Collection("subscriptions").Aggregate(a.ctx, pipeline) if err != nil { return counts, err } defer cur.Close(a.ctx) for cur.Next(a.ctx) { var oneCount struct { Id string `bson:"_id"` UnreadCount int `bson:"unreadCount"` } cur.Decode(&oneCount) counts[t.ParseUid(oneCount.Id)] = oneCount.UnreadCount } return counts, nil } // UserGetUnvalidated returns a list of uids which have never logged in, have no // validated credentials and haven't been updated since lastUpdatedBefore. func (a *adapter) UserGetUnvalidated(lastUpdatedBefore time.Time, limit int) ([]t.Uid, error) { /* Query: [ // .. WHERE lastseen IS NULL AND updatedat [{x: 1, a: 1}, {x: 1, a: 2}] {$unwind: {path: "$fcred"}}, // SELECT _id, CASE WHEN done THEN 1 ELSE 0 END {$project: { _id: 1, completed: { $cond: { if: "$fcred.done", then: 1, else: 0 } }, }}, // GROUP BY _id {$group: { _id: "$_id", completed: { $sum: "$completed" } } }, // HAVING completed=0 {$match: { completed: 0 }}, // SELECT _id {$project: { _id: "$_id" }}, {$limit: 10} ] */ pipeline := b.A{ b.M{"$match": b.M{ "$and": b.A{ b.M{"lastseen": primitive.Null{}}, b.M{"updatedat": b.M{"$lt": lastUpdatedBefore}}, }, }}, b.M{"$lookup": b.D{ {"from", "credentials"}, {"localField", "_id"}, {"foreignField", "user"}, {"as", "fcred"}}, }, b.M{"$unwind": b.M{"path": "$fcred"}}, b.M{"$project": b.D{ {"_id", 1}, {"completed", b.M{ "$cond": b.D{{"if", "$fcred.done"}, {"then", 1}, {"else", 0}}}, }}}, b.M{"$group": b.D{{"_id", "$_id"}, {"completed", b.M{"$sum": "$completed"}}}}, b.M{"$match": b.M{"completed": 0}}, b.M{"$project": b.M{"_id": "$_id"}}, b.M{"$limit": limit}, } cur, err := a.db.Collection("users").Aggregate(a.ctx, pipeline) if err != nil { return nil, err } defer cur.Close(a.ctx) var uids []t.Uid for cur.Next(a.ctx) { var oneUser struct { Id string `bson:"_id"` } if err := cur.Decode(&oneUser); err != nil { return nil, err } uid := t.ParseUid(oneUser.Id) if uid.IsZero() { return nil, errors.New("failed to decode user id") } uids = append(uids, uid) } return uids, err } // Credential management // CredUpsert adds or updates a validation record. Returns true if inserted, false if updated. // 1. if credential is validated: // 1.1 Hard-delete unconfirmed equivalent record, if exists. // 1.2 Insert new. Report error if duplicate. // 2. if credential is not validated: // 2.1 Check if validated equivalent exist. If so, report an error. // 2.2 Soft-delete all unvalidated records of the same method. // 2.3 Undelete existing credential. Return if successful. // 2.4 Insert new credential record. func (a *adapter) CredUpsert(cred *t.Credential) (bool, error) { credCollection := a.db.Collection("credentials") cred.Id = cred.Method + ":" + cred.Value if !cred.Done { // Check if the same credential is already validated. var result1 t.Credential err := credCollection.FindOne(a.ctx, b.M{"_id": cred.Id}).Decode(&result1) if result1 != (t.Credential{}) { // Someone has already validated this credential. return false, t.ErrDuplicate } if err != nil && err != mdb.ErrNoDocuments { // if no result -> continue return false, err } // Soft-delete all unvalidated records of this user and method. _, err = credCollection.UpdateMany(a.ctx, b.M{"user": cred.User, "method": cred.Method, "done": false}, b.M{"$set": b.M{"deletedat": t.TimeNow()}}) if err != nil { return false, err } // If credential is not confirmed, it should not block others // from attempting to validate it: make index user-unique instead of global-unique. cred.Id = cred.User + ":" + cred.Id // Check if this credential has already been added by the user. var result2 t.Credential err = credCollection.FindOne(a.ctx, b.M{"_id": cred.Id}).Decode(&result2) if result2 != (t.Credential{}) { _, err = credCollection.UpdateOne(a.ctx, b.M{"_id": cred.Id}, b.M{ "$unset": b.M{"deletedat": ""}, "$set": b.M{"updatedat": cred.UpdatedAt, "resp": cred.Resp}}) if err != nil { return false, err } // The record was updated, all is fine. return false, nil } if err != nil && err != mdb.ErrNoDocuments { return false, err } } else { // Hard-delete potentially present unvalidated credential. _, err := credCollection.DeleteOne(a.ctx, b.M{"_id": cred.User + ":" + cred.Id}) if err != nil { return false, err } } // Insert a new record. _, err := credCollection.InsertOne(a.ctx, cred) if isDuplicateErr(err) { return true, t.ErrDuplicate } return true, err } // CredGetActive returns the currently active credential record for the given method. func (a *adapter) CredGetActive(uid t.Uid, method string) (*t.Credential, error) { var cred t.Credential filter := b.M{ "user": uid.String(), "deletedat": b.M{"$exists": false}, "method": method, "done": false} if err := a.db.Collection("credentials").FindOne(a.ctx, filter).Decode(&cred); err != nil { if err == mdb.ErrNoDocuments { // Cred not found err = nil } return nil, err } return &cred, nil } // CredGetAll returns credential records for the given user and method, validated only or all. func (a *adapter) CredGetAll(uid t.Uid, method string, validatedOnly bool) ([]t.Credential, error) { filter := b.M{"user": uid.String()} if method != "" { filter["method"] = method } if validatedOnly { filter["done"] = true } else { filter["deletedat"] = b.M{"$exists": false} } cur, err := a.db.Collection("credentials").Find(a.ctx, filter) if err != nil { return nil, err } defer cur.Close(a.ctx) var credentials []t.Credential if err := cur.All(a.ctx, &credentials); err != nil { return nil, err } return credentials, nil } // CredDel deletes credentials for the given method/value. If method is empty, deletes all // user's credentials. func (a *adapter) credDel(ctx context.Context, uid t.Uid, method, value string) error { credCollection := a.db.Collection("credentials") filter := b.M{"user": uid.String()} if method != "" { filter["method"] = method if value != "" { filter["value"] = value } } else { res, err := credCollection.DeleteMany(ctx, filter) if err == nil { if res.DeletedCount == 0 { err = t.ErrNotFound } } return err } // Hard-delete all confirmed values or values with no attempts at confirmation. hardDeleteFilter := copyBsonMap(filter) hardDeleteFilter["$or"] = b.A{ b.M{"done": true}, b.M{"retries": 0}} if res, err := credCollection.DeleteMany(ctx, hardDeleteFilter); err != nil { return err } else if res.DeletedCount > 0 { return nil } // Soft-delete all other values. res, err := credCollection.UpdateMany(ctx, filter, b.M{"$set": b.M{"deletedat": t.TimeNow()}}) if err == nil { if res.ModifiedCount == 0 { err = t.ErrNotFound } } return err } func (a *adapter) CredDel(uid t.Uid, method, value string) error { return a.credDel(a.ctx, uid, method, value) } // CredConfirm marks given credential as validated. func (a *adapter) CredConfirm(uid t.Uid, method string) error { cred, err := a.CredGetActive(uid, method) if err != nil { return err } cred.Done = true cred.UpdatedAt = t.TimeNow() if _, err = a.CredUpsert(cred); err != nil { return err } _, _ = a.db.Collection("credentials").DeleteOne(a.ctx, b.M{"_id": uid.String() + ":" + cred.Method + ":" + cred.Value}) return nil } // CredFail increments count of failed validation attepmts for the given credentials. func (a *adapter) CredFail(uid t.Uid, method string) error { filter := b.M{ "user": uid.String(), "deletedat": b.M{"$exists": false}, "method": method, "done": false} update := b.M{ "$inc": b.M{"retries": 1}, "$set": b.M{"updatedat": t.TimeNow()}} _, err := a.db.Collection("credentials").UpdateOne(a.ctx, filter, update) return err } // Authentication management for the basic authentication scheme // AuthGetUniqueRecord returns authentication record for a given unique value i.e. login. func (a *adapter) AuthGetUniqueRecord(unique string) (t.Uid, auth.Level, []byte, time.Time, error) { var record struct { UserId string AuthLvl auth.Level Secret []byte Expires time.Time } filter := b.M{"_id": unique} findOpts := mdbopts.FindOne().SetProjection(b.M{ "userid": 1, "authlvl": 1, "secret": 1, "expires": 1, }) if err := a.db.Collection("auth").FindOne(a.ctx, filter, findOpts).Decode(&record); err != nil { if err == mdb.ErrNoDocuments { return t.ZeroUid, 0, nil, time.Time{}, nil } return t.ZeroUid, 0, nil, time.Time{}, err } return t.ParseUid(record.UserId), record.AuthLvl, record.Secret, record.Expires, nil } // AuthGetRecord returns authentication record given user ID and method. func (a *adapter) AuthGetRecord(uid t.Uid, scheme string) (string, auth.Level, []byte, time.Time, error) { var record struct { Id string `bson:"_id"` AuthLvl auth.Level Secret []byte Expires time.Time } filter := b.M{"userid": uid.String(), "scheme": scheme} findOpts := mdbopts.FindOne().SetProjection(b.M{ "authlvl": 1, "secret": 1, "expires": 1, }) err := a.db.Collection("auth").FindOne(a.ctx, filter, findOpts).Decode(&record) if err != nil { if err == mdb.ErrNoDocuments { err = t.ErrNotFound } return "", 0, nil, time.Time{}, err } return record.Id, record.AuthLvl, record.Secret, record.Expires, nil } // AuthAddRecord creates new authentication record func (a *adapter) AuthAddRecord(uid t.Uid, scheme, unique string, authLvl auth.Level, secret []byte, expires time.Time) error { authRecord := b.M{ "_id": unique, "userid": uid.String(), "scheme": scheme, "authlvl": authLvl, "secret": secret, "expires": expires} if _, err := a.db.Collection("auth").InsertOne(a.ctx, authRecord); err != nil { if isDuplicateErr(err) { return t.ErrDuplicate } return err } return nil } // AuthDelScheme deletes an existing authentication scheme for the user. func (a *adapter) AuthDelScheme(uid t.Uid, scheme string) error { _, err := a.db.Collection("auth").DeleteOne(a.ctx, b.M{ "userid": uid.String(), "scheme": scheme}) return err } func (a *adapter) authDelAllRecords(ctx context.Context, uid t.Uid) (int, error) { res, err := a.db.Collection("auth").DeleteMany(ctx, b.M{"userid": uid.String()}) return int(res.DeletedCount), err } // AuthDelAllRecords deletes all records of a given user. func (a *adapter) AuthDelAllRecords(uid t.Uid) (int, error) { return a.authDelAllRecords(a.ctx, uid) } // AuthUpdRecord modifies an authentication record. func (a *adapter) AuthUpdRecord(uid t.Uid, scheme, unique string, authLvl auth.Level, secret []byte, expires time.Time) error { // The primary key is immutable. If '_id' has changed, we have to replace the old record with a new one: // 1. Check if '_id' has changed. // 2. If not, execute update by '_id' // 3. If yes, first insert the new record (it may fail due to dublicate '_id') then delete the old one. var err error var record common.AuthRecord findOpts := mdbopts.FindOne().SetProjection(b.M{"_id": 1}) filter := b.M{"userid": uid.String(), "scheme": scheme} if err = a.db.Collection("auth").FindOne(a.ctx, filter, findOpts).Decode(&record); err != nil { if err == mdb.ErrNoDocuments { err = t.ErrNotFound } return err } if record.Unique == unique { upd := b.M{ "authlvl": authLvl, } if len(secret) > 0 { upd["secret"] = secret } if !expires.IsZero() { upd["expires"] = expires } _, err = a.db.Collection("auth").UpdateOne(a.ctx, b.M{"_id": unique}, b.M{"$set": upd}) } else { // Unique has changed. Insert-Delete. // FIXME: use transaction. if len(secret) == 0 { secret = record.Secret } if expires.IsZero() { expires = record.Expires } err = a.AuthAddRecord(uid, scheme, unique, authLvl, secret, expires) if err == nil { // Delete the old record. Not much can be done with the error. a.db.Collection("auth").DeleteOne(a.ctx, b.M{"_id": record.Unique}) } } return err } // Topic management func (a *adapter) undeleteSubscription(sub *t.Subscription) error { _, err := a.db.Collection("subscriptions").UpdateOne(a.ctx, b.M{"_id": sub.Id}, b.M{ "$unset": b.M{"deletedat": ""}, "$set": b.M{ "updatedat": sub.UpdatedAt, "createdat": sub.CreatedAt, "modegiven": sub.ModeGiven, "modewant": sub.ModeWant, "delid": 0, "readseqid": 0, "recvseqid": 0}}) return err } // TopicCreate creates a topic func (a *adapter) TopicCreate(topic *t.Topic) error { _, err := a.db.Collection("topics").InsertOne(a.ctx, &topic) return err } // TopicCreateP2P creates a p2p topic. func (a *adapter) TopicCreateP2P(initiator, invited *t.Subscription) error { initiator.Id = initiator.Topic + ":" + initiator.User // Don't care if the initiator changes own subscription replOpts := mdbopts.Replace().SetUpsert(true) _, err := a.db.Collection("subscriptions").ReplaceOne(a.ctx, b.M{"_id": initiator.Id}, initiator, replOpts) if err != nil { return err } // If the second subscription exists, don't overwrite it. Just make sure it's not deleted. invited.Id = invited.Topic + ":" + invited.User _, err = a.db.Collection("subscriptions").InsertOne(a.ctx, invited) if err != nil { // Is this a duplicate subscription? if !isDuplicateErr(err) { // It's a genuine DB error return err } // Undelete the second subsription if it exists: remove DeletedAt, update CreatedAt and UpdatedAt, // update ModeGiven. err = a.undeleteSubscription(invited) if err != nil { return err } } topic := &t.Topic{ ObjHeader: t.ObjHeader{Id: initiator.Topic}, TouchedAt: initiator.GetTouchedAt(), } topic.ObjHeader.MergeTimes(&initiator.ObjHeader) return a.TopicCreate(topic) } // TopicGet loads a single topic by name, if it exists. If the topic does not exist the call returns (nil, nil) func (a *adapter) TopicGet(topic string) (*t.Topic, error) { var tt = new(t.Topic) if err := a.db.Collection("topics").FindOne(a.ctx, b.M{"_id": topic}).Decode(tt); err != nil { if err == mdb.ErrNoDocuments { return nil, nil } return nil, err } if t.GetTopicCat(topic) == t.TopicCatGrp { // Topic found, get subsription count. subCnt, err := a.subscriptionCount(topic) if err != nil { return nil, err } if int(subCnt) != tt.SubCnt { // Update the topic with the correct subscription count. tt.SubCnt = int(subCnt) err = a.topicUpdate(topic, b.M{"subcnt": tt.SubCnt}) if err != nil { return nil, err } } } tt.Public = unmarshalBsonD(tt.Public) tt.Trusted = unmarshalBsonD(tt.Trusted) return tt, nil } // TopicsForUser loads user's contact list: p2p and grp topics, except for 'me' & 'fnd' subscriptions. // Reads and denormalizes Public & Trusted values. func (a *adapter) TopicsForUser(uid t.Uid, keepDeleted bool, opts *t.QueryOpt) ([]t.Subscription, error) { // Fetch all user's subscriptions. filter := b.M{"user": uid.String()} if !keepDeleted { // Filter out rows with defined deletedat filter["deletedat"] = b.M{"$exists": false} } limit := 0 ims := time.Time{} if opts != nil { if opts.Topic != "" { filter["topic"] = opts.Topic } // Apply the limit only when the client does not manage the cache (or cold start). // Otherwise have to get all subscriptions and do a manual join with users/topics. if opts.IfModifiedSince == nil { if opts.Limit > 0 && opts.Limit < a.maxResults { limit = opts.Limit } else { limit = a.maxResults } } else { ims = *opts.IfModifiedSince } } else { limit = a.maxResults } var findOpts *mdbopts.FindOptions if limit > 0 { findOpts = mdbopts.Find().SetLimit(int64(limit)) } cur, err := a.db.Collection("subscriptions").Find(a.ctx, filter, findOpts) if err != nil { return nil, err } // Must close the cursor manually as we will be reusing it. // Fetch subscriptions. Two queries are needed: users table (me & p2p) and topics table (p2p and grp). // Prepare a list of Separate subscriptions to users vs topics join := make(map[string]t.Subscription) // Keeping these to make a join with table for .private and .access topq := make([]string, 0, 16) usrq := make([]string, 0, 16) for cur.Next(a.ctx) { var sub t.Subscription if err = cur.Decode(&sub); err != nil { break } tname := sub.Topic sub.User = uid.String() tcat := t.GetTopicCat(tname) if tcat == t.TopicCatMe || tcat == t.TopicCatFnd { // Skip 'me' or 'fnd' subscription. Don't skip 'sys'. continue } else if tcat == t.TopicCatP2P { // P2P subscription, find the other user to get user.Public uid1, uid2, _ := t.ParseP2P(sub.Topic) if uid1 == uid { usrq = append(usrq, uid2.String()) sub.SetWith(uid2.UserId()) } else { usrq = append(usrq, uid1.String()) sub.SetWith(uid1.UserId()) } topq = append(topq, tname) } else if tcat == t.TopicCatGrp { // Maybe convert channel name to topic name. tname = t.ChnToGrp(tname) } // No special handling needed for 'slf', 'sys' subscriptions. topq = append(topq, tname) sub.Private = unmarshalBsonD(sub.Private) join[tname] = sub } cur.Close(a.ctx) if err != nil { return nil, err } var subs []t.Subscription if len(join) == 0 { return subs, nil } if len(topq) > 0 { // Fetch grp & p2p topics filter = b.M{"_id": b.M{"$in": topq}} if !keepDeleted { filter["state"] = b.M{"$ne": t.StateDeleted} } if !ims.IsZero() { // Use cache timestamp if provided: get newer entries only. filter["touchedat"] = b.M{"$gt": ims} findOpts = nil if limit > 0 && limit < len(topq) { // No point in fetching more than the requested limit. findOpts = mdbopts.Find().SetSort(b.D{{"touchedat", 1}}).SetLimit(int64(limit)) } } cur, err = a.db.Collection("topics").Find(a.ctx, filter, findOpts) if err != nil { return nil, err } for cur.Next(a.ctx) { var top t.Topic if err = cur.Decode(&top); err != nil { break } sub := join[top.Id] // Check if sub.UpdatedAt needs to be adjusted to earlier or later time. sub.UpdatedAt = common.SelectLatestTime(sub.UpdatedAt, top.UpdatedAt) sub.SetState(top.State) sub.SetTouchedAt(top.TouchedAt) sub.SetSeqId(top.SeqId) if t.GetTopicCat(sub.Topic) == t.TopicCatGrp { sub.SetSubCnt(top.SubCnt) sub.SetPublic(unmarshalBsonD(top.Public)) sub.SetTrusted(unmarshalBsonD(top.Trusted)) } // Put back the updated value of a p2p subsription, will process further below join[top.Id] = sub } cur.Close(a.ctx) if err != nil { return nil, err } } // Fetch p2p users and join to p2p tables if len(usrq) > 0 { filter = b.M{"_id": b.M{"$in": usrq}} if !keepDeleted { filter["state"] = b.M{"$ne": t.StateDeleted} } // Ignoring ims: we need all users to get LastSeen and UserAgent. cur, err = a.db.Collection("users").Find(a.ctx, filter, findOpts) if err != nil { return nil, err } for cur.Next(a.ctx) { var usr2 t.User if err = cur.Decode(&usr2); err != nil { break } joinOn := uid.P2PName(t.ParseUid(usr2.Id)) if sub, ok := join[joinOn]; ok { sub.UpdatedAt = common.SelectLatestTime(sub.UpdatedAt, usr2.UpdatedAt) sub.SetState(usr2.State) sub.SetPublic(unmarshalBsonD(usr2.Public)) sub.SetTrusted(unmarshalBsonD(usr2.Trusted)) sub.SetDefaultAccess(usr2.Access.Auth, usr2.Access.Anon) sub.SetLastSeenAndUA(usr2.LastSeen, usr2.UserAgent) join[joinOn] = sub } } cur.Close(a.ctx) if err != nil { return nil, err } } subs = make([]t.Subscription, 0, len(join)) for _, sub := range join { subs = append(subs, sub) } return common.SelectEarliestUpdatedSubs(subs, opts, a.maxResults), nil } // UsersForTopic loads users' subscriptions for a given topic (not channel readers). // Public & Trusted are loaded. func (a *adapter) UsersForTopic(topic string, keepDeleted bool, opts *t.QueryOpt) ([]t.Subscription, error) { tcat := t.GetTopicCat(topic) // Fetch all subscribed users. The number of users is not large. filter := b.M{"topic": topic} if !keepDeleted && tcat != t.TopicCatP2P { // Filter out rows with DeletedAt being not null. // P2P topics must load all subscriptions otherwise it will be impossible // to swap Public values. filter["deletedat"] = b.M{"$exists": false} } limit := a.maxResults var oneUser t.Uid if opts != nil { // Ignore IfModifiedSince - we must return all entries // Those unmodified will be stripped of Public, Trusted & Private. if !opts.User.IsZero() { if tcat != t.TopicCatP2P { filter["user"] = opts.User.String() } oneUser = opts.User } if opts.Limit > 0 && opts.Limit < limit { limit = opts.Limit } } cur, err := a.db.Collection("subscriptions").Find(a.ctx, filter, mdbopts.Find().SetLimit(int64(limit))) if err != nil { return nil, err } // Fetch subscriptions. var subs []t.Subscription join := make(map[string]t.Subscription) usrq := make([]any, 0, 16) for cur.Next(a.ctx) { var sub t.Subscription if err = cur.Decode(&sub); err != nil { break } join[sub.User] = sub usrq = append(usrq, sub.User) } cur.Close(a.ctx) if err != nil { return nil, err } // Fetch users by a list of subscriptions. if len(usrq) > 0 { subs = make([]t.Subscription, 0, len(usrq)) cur, err = a.db.Collection("users").Find(a.ctx, b.M{ "_id": b.M{"$in": usrq}, "state": b.M{"$ne": t.StateDeleted}}) if err != nil { return nil, err } for cur.Next(a.ctx) { var usr2 t.User if err = cur.Decode(&usr2); err != nil { break } if sub, ok := join[usr2.Id]; ok { sub.ObjHeader.MergeTimes(&usr2.ObjHeader) sub.Private = unmarshalBsonD(sub.Private) sub.SetPublic(unmarshalBsonD(usr2.Public)) sub.SetTrusted(unmarshalBsonD(usr2.Trusted)) sub.SetLastSeenAndUA(usr2.LastSeen, usr2.UserAgent) subs = append(subs, sub) } } cur.Close(a.ctx) if err != nil { return nil, err } } if t.GetTopicCat(topic) == t.TopicCatP2P && len(subs) > 0 { // Swap public values & lastSeen of P2P topics as expected. if len(subs) == 1 { // User is deleted. Nothing we can do. subs[0].SetPublic(nil) subs[0].SetTrusted(nil) subs[0].SetLastSeenAndUA(nil, "") } else { tmp := subs[0].GetPublic() subs[0].SetPublic(subs[1].GetPublic()) subs[1].SetPublic(tmp) tmp = subs[0].GetTrusted() subs[0].SetTrusted(subs[1].GetTrusted()) subs[1].SetTrusted(tmp) lastSeen := subs[0].GetLastSeen() userAgent := subs[0].GetUserAgent() subs[0].SetLastSeenAndUA(subs[1].GetLastSeen(), subs[1].GetUserAgent()) subs[1].SetLastSeenAndUA(lastSeen, userAgent) } // Remove deleted and unneeded subscriptions if !keepDeleted || !oneUser.IsZero() { var xsubs []t.Subscription for i := range subs { if (subs[i].DeletedAt != nil && !keepDeleted) || (!oneUser.IsZero() && subs[i].Uid() != oneUser) { continue } xsubs = append(xsubs, subs[i]) } subs = xsubs } } return subs, nil } // topicNamesForUser reads topic names from the 'field' of 'collection' using 'filter'. // If includeChan is true, for group topics also add the corresponding channel name. func (a *adapter) topicNamesForUser(collection string, filter b.M, field string, includeChan bool) ([]string, error) { cur, err := a.db.Collection(collection).Find(a.ctx, filter, mdbopts.Find().SetProjection(b.M{field: 1})) if err != nil { return nil, err } defer cur.Close(a.ctx) var names []string for cur.Next(a.ctx) { var res map[string]string if err = cur.Decode(&res); err != nil { break } names = append(names, res[field]) // If the name is a group topic, also add the channel name if requested. if includeChan { if channel := t.GrpToChn(res[field]); channel != "" { names = append(names, channel) } } } return names, err } func (a *adapter) p2pTopicsForUser(uid t.Uid) ([]string, error) { return a.topicNamesForUser("subscriptions", b.M{ "user": uid.String(), "deletedat": b.M{"$exists": false}, "topic": b.M{"$regex": primitive.Regex{Pattern: "^p2p"}}}, "topic", false) } // OwnTopics loads a slice of topic names where the user is the owner. func (a *adapter) OwnTopics(uid t.Uid) ([]string, error) { return a.topicNamesForUser("topics", b.M{"owner": uid.String(), "state": b.M{"$ne": t.StateDeleted}}, "_id", false) } // ChannelsForUser loads a slice of topic names where the user is a channel reader and notifications (P) are enabled. func (a *adapter) ChannelsForUser(uid t.Uid) ([]string, error) { return a.topicNamesForUser("subscriptions", b.M{ "user": uid.String(), "deletedat": b.M{"$exists": false}, "topic": b.M{"$regex": primitive.Regex{Pattern: "^chn"}}, "modewant": b.M{"$bitsAllSet": b.A{t.ModePres}}, "modegiven": b.M{"$bitsAllSet": b.A{t.ModePres}}}, "topic", false) } // TopicShare creates topic subscriptions. func (a *adapter) TopicShare(topic string, shares []*t.Subscription) error { // Assign Ids. for _, sub := range shares { sub.Id = sub.Topic + ":" + sub.User } // Subscription could have been marked as deleted (DeletedAt != nil). If it's marked // as deleted, unmark by clearing the DeletedAt field of the old subscription and // updating times and ModeGiven. for _, sub := range shares { _, err := a.db.Collection("subscriptions").InsertOne(a.ctx, sub) if err != nil { if isDuplicateErr(err) { if err = a.undeleteSubscription(sub); err != nil { return err } } else { return err } } } if topic != "" { // Update topic's subscription count. // The error is ignored because the subscriptions have been created already. a.db.Collection("topics").UpdateOne(a.ctx, b.M{"_id": topic}, b.M{"$inc": b.M{"subcnt": len(shares)}}) } return nil } // TopicDelete deletes topic, subscriptions, messages. func (a *adapter) TopicDelete(topic string, isChan, hard bool) error { filter := b.M{} if isChan { // If the topic is a channel, must try to delete subscriptions under both grpXXX and chnXXX names. filter["$or"] = b.A{ b.M{"topic": topic}, b.M{"topic": t.GrpToChn(topic)}, } } else { filter["topic"] = topic } err := a.subsDelete(a.ctx, filter, hard) if err != nil { return err } filter = b.M{"_id": topic} if hard { if err = a.decFileUseCounter(a.ctx, "topics", filter); err != nil { return err } if err = a.MessageDeleteList(topic, nil); err != nil { return err } _, err = a.db.Collection("topics").DeleteOne(a.ctx, filter) } else { _, err = a.db.Collection("topics").UpdateOne(a.ctx, filter, b.M{"$set": b.M{ "state": t.StateDeleted, "stateat": t.TimeNow(), }}) } return err } // TopicUpdateOnMessage increments Topic's or User's SeqId value and updates TouchedAt timestamp. func (a *adapter) TopicUpdateOnMessage(topic string, msg *t.Message) error { return a.topicUpdate(topic, b.M{"seqid": msg.SeqId, "touchedat": msg.CreatedAt}) } func (a *adapter) subscriptionCount(topic string) (int64, error) { // Get count of non-deleted subscriptions to the topic. return a.db.Collection("subscriptions").CountDocuments(a.ctx, b.M{ "topic": b.M{"$in": b.A{topic, t.GrpToChn(topic)}}, "deletedat": b.M{"$exists": false}, }) } // TopicUpdateSubCnt updates subscriber count denormalized in topic. func (a *adapter) TopicUpdateSubCnt(topic string) error { // Get count of non-deleted subscriptions to the topic. // UPDATE ... SET=(SELECT ...) is not supported in MongoDB, so we have to do it in two queries. count, err := a.subscriptionCount(topic) if err != nil { return err } return a.topicUpdate(topic, b.M{"subcnt": count}) } // TopicUpdate updates topic record. func (a *adapter) TopicUpdate(topic string, update map[string]any) error { if t, u := update["TouchedAt"], update["UpdatedAt"]; t == nil && u != nil { update["TouchedAt"] = u } return a.topicUpdate(topic, normalizeUpdateMap(update)) } // TopicOwnerChange updates topic's owner func (a *adapter) TopicOwnerChange(topic string, newOwner t.Uid) error { return a.topicUpdate(topic, map[string]any{"owner": newOwner.String()}) } func (a *adapter) topicUpdate(topic string, update map[string]any) error { _, err := a.db.Collection("topics").UpdateOne(a.ctx, b.M{"_id": topic}, b.M{"$set": update}) return err } // Topic subscriptions // SubscriptionGet reads a subscription of a user to a topic. func (a *adapter) SubscriptionGet(topic string, user t.Uid, keepDeleted bool) (*t.Subscription, error) { sub := new(t.Subscription) filter := b.M{"_id": topic + ":" + user.String()} if !keepDeleted { filter["deletedat"] = b.M{"$exists": false} } err := a.db.Collection("subscriptions").FindOne(a.ctx, filter).Decode(sub) if err != nil { if err == mdb.ErrNoDocuments { return nil, nil } return nil, err } return sub, nil } // SubsForUser loads all subscriptions of a given user. It does NOT load Public, Trusted or Private values, // does not load deleted subs. func (a *adapter) SubsForUser(user t.Uid) ([]t.Subscription, error) { filter := b.M{"user": user.String(), "deletedat": b.M{"$exists": false}} cur, err := a.db.Collection("subscriptions").Find(a.ctx, filter) if err != nil { return nil, err } defer cur.Close(a.ctx) var subs []t.Subscription for cur.Next(a.ctx) { var ss t.Subscription if err := cur.Decode(&ss); err != nil { return nil, err } ss.Private = nil subs = append(subs, ss) } return subs, cur.Err() } // SubsForTopic fetches all subsciptions for a topic. Does NOT load Public value and does not load channel readers. // The difference between UsersForTopic vs SubsForTopic is that the former loads user.public+trusted, // the latter does not. func (a *adapter) SubsForTopic(topic string, keepDeleted bool, opts *t.QueryOpt) ([]t.Subscription, error) { filter := b.M{"topic": topic} if !keepDeleted { filter["deletedat"] = b.M{"$exists": false} } limit := a.maxResults if opts != nil { // Ignore IfModifiedSince - we must return all entries // Those unmodified will be stripped of Public, Trusted & Private. if !opts.User.IsZero() { filter["user"] = opts.User.String() } if opts.Limit > 0 && opts.Limit < limit { limit = opts.Limit } } findOpts := new(mdbopts.FindOptions).SetLimit(int64(limit)) cur, err := a.db.Collection("subscriptions").Find(a.ctx, filter, findOpts) if err != nil { return nil, err } defer cur.Close(a.ctx) var subs []t.Subscription for cur.Next(a.ctx) { var ss t.Subscription if err := cur.Decode(&ss); err != nil { return nil, err } ss.Private = unmarshalBsonD(ss.Private) subs = append(subs, ss) } return subs, cur.Err() } // SubsUpdate updates part of a subscription object. Pass nil for fields which don't need to be updated func (a *adapter) SubsUpdate(topic string, user t.Uid, update map[string]any) error { // Convert CamelCase field names to lowercase. update = normalizeUpdateMap(update) filter := b.M{} if !user.IsZero() { // Update one topic subscription filter["_id"] = topic + ":" + user.String() } else { // Update all topic subscriptions filter["topic"] = topic } _, err := a.db.Collection("subscriptions").UpdateOne(a.ctx, filter, b.M{"$set": update}) return err } // SubsDelete marks at most one subscription as deleted (soft-deleting). func (a *adapter) SubsDelete(topic string, user t.Uid) error { var sess mdb.Session var err error if sess, err = a.conn.StartSession(); err != nil { return err } defer sess.EndSession(a.ctx) if err = a.maybeStartTransaction(sess); err != nil { return err } forUser := user.String() return mdb.WithSession(a.ctx, sess, func(sc mdb.SessionContext) error { if err := a.subsDelete(sc, b.M{"_id": topic + ":" + forUser}, false); err != nil { return err } // Channel readers cannot delete messages. if !t.IsChannel(topic) { // Delete user's dellog entries. if _, err := a.db.Collection("dellog").DeleteMany(sc, b.M{"topic": topic, "deletedfor": forUser}); err != nil { return err } // Delete user's markings of soft-deleted messages filter := b.M{"topic": topic, "deletedfor.user": forUser} if _, err := a.db.Collection("messages"). UpdateMany(sc, filter, b.M{"$pull": b.M{"deletedfor": b.M{"user": forUser}}}); err != nil { return err } } if t.GetTopicCat(topic) == t.TopicCatGrp { // Decrement topic subscription count (only one subscription is deleted). if err := a.topicUpdate(topic, b.M{"subcnt": -1}); err != nil { return err } } // Commit changes. return a.maybeCommitTransaction(sc, sess) }) } // clearUserDellog deletes all dellog entries and deletedfor markings of a given user. func (a *adapter) clearUserDellog(sc mdb.SessionContext, forUser string) error { topics, err := a.db.Collection("subscriptions").Distinct(sc, "topic", b.M{"user": forUser, "deletedat": b.M{"$exists": false}}) if err != nil { return err } // No need to convert channel names to group names: // channel readers cannot delete messages. if len(topics) > 0 { // Delete user's dellog entries. if _, err = a.db.Collection("dellog").DeleteMany(sc, b.M{"topic": b.M{"$in": topics}, "deletedfor": forUser}); err != nil { return err } // Delete user's markings of soft-deleted messages filter := b.M{"topic": b.M{"$in": topics}, "deletedfor.user": forUser} if _, err = a.db.Collection("messages"). UpdateMany(sc, filter, b.M{"$pull": b.M{"deletedfor": b.M{"user": forUser}}}); err != nil { return err } } return nil } // Delete/mark deleted subscriptions and decrement subcnt in topic. func (a *adapter) subsDelete(ctx context.Context, filter b.M, hard bool) error { // First, decrement subscription count in all affected topics. // Doing it in two steps because MongoDB does not support an equivalent of // 'UPDATE .. LEFT JOIN ...'. filterWithDeletedAt := copyBsonMap(filter) filterWithDeletedAt["deletedat"] = b.M{"$exists": false} cur, err := a.db.Collection("subscriptions").Find(ctx, filterWithDeletedAt, mdbopts.Find().SetProjection(b.D{{"topic", 1}, {"_id", 0}})) if err != nil { return err } defer cur.Close(ctx) var topics []string for cur.Next(ctx) { var result struct { Topic string `bson:"topic"` } if err = cur.Decode(&result); err != nil { return err } if t.IsChannel(result.Topic) { // Convert channel name to group name. topics = append(topics, t.ChnToGrp(result.Topic)) } topics = append(topics, result.Topic) } if err = cur.Err(); err != nil { return err } if len(topics) > 0 { // Decrement subscription count in affected topics. a.db.Collection("topics").UpdateMany(ctx, b.M{"_id": b.M{"$in": topics}}, b.M{"$inc": b.M{"subcnt": -1}}) } // Now delete or mark deleted the subscriptions. if hard { _, err = a.db.Collection("subscriptions").DeleteMany(ctx, filter) } else { now := t.TimeNow() _, err = a.db.Collection("subscriptions").UpdateMany(ctx, filterWithDeletedAt, b.M{"$set": b.M{"updatedat": now, "deletedat": now}}) } return err } // Find searches for contacts and topics given a list of tags. func (a *adapter) Find(caller, prefPrefix string, req [][]string, opt []string, activeOnly bool) ([]t.Subscription, error) { /* // MongoDB aggregation pipeline using unionWith. [ { $match: { tags: { $in: ["basic:alice", "travel"] } } }, { $unionWith: { coll: "topics", pipeline: [ { $match: { tags: { $in: ["basic:alice", "travel"] } } } ] } }, { $project: { _id: 1, access: 1, createdat: 1, updatedat: 1, usebt: 1, public: 1, trusted: 1, tags: 1, _source: 1 } }, { $addFields: { matchedCount: { $sum: { $map: { input: { $setIntersection: [ "$tags", [ "alias:aliassa", "basic:alice", "travel" ] ] }, as: "tag", in: { $cond: { if: { $regexMatch: { input: "$$tag", regex: "^alias:"} }, then: 20, else: 1 } } } }}}}, { $match: { $expr: { $ne: [ { $size: { $setIntersection: [ "$tags", ["basic:alice", "travel"] ] } }, 0 ] } } }, { $sort: { matchedCount: -1 } }, { $limit: 20 } ] // Alternative approach using $facet for (supposedly) better performance: [ { $facet: { users: [ { $match: { tags: { $in: [ "alias:alice", "basic:alice", "travel" ] } } }, { $project: { _id: 1, access: 1, createdat: 1, updatedat: 1, usebt: 1, public: 1, trusted: 1, tags: 1 } } ], topics: [ { $lookup: { from: "topics", pipeline: [ { $match: { tags: { $in: [ "alias:alice", "basic:alice", "travel" ] } } }, { $project: { _id: 1, access: 1, createdat: 1, updatedat: 1, usebt: 1, public: 1, trusted: 1, tags: 1 } } } ], as: "topicDocs" }}, { $unwind: "$topicDocs" }, { $replaceRoot: { newRoot: "$topicDocs" } } ] } }, { $project: { combined: { $concatArrays: ["$users", "$topics"] } } }, { $unwind: "$combined" }, { $replaceRoot: { newRoot: "$combined" } }, { $group: { _id: "$_id", doc: { $first: "$$ROOT" } } }, { $replaceRoot: { newRoot: "$doc" } }, { $addFields: { matchedCount: { $sum: { $map: { input: { $setIntersection: [ "$tags", [ "alias:alice", "basic:alice", "travel" ] ] }, as: "tag", in: { $cond: { if: { $regexMatch: { input: "$$tag", regex: "^alias:" } }, then: 20, else: 1 } } } } } } }, { $match: { $expr: { $ne: [ { $size: { $setIntersection: [ "$tags", [ "alias:alice", "basic:alice", "travel" ] ] } }, 0 ] } } }, { $sort: { matchedCount: -1 } }, { $limit: 20 } ] */ index := make(map[string]struct{}) allReq := t.FlattenDoubleSlice(req) var allTags []any for _, tag := range append(allReq, opt...) { allTags = append(allTags, tag) index[tag] = struct{}{} } matchOn := b.M{"tags": b.M{"$in": allTags}} if activeOnly { matchOn["state"] = b.M{"$eq": t.StateOK} } projectFields := b.M{"_id": 1, "createdat": 1, "updatedat": 1, "usebt": 1, "access": 1, "subcnt": 1, "public": 1, "trusted": 1, "tags": 1} pipeline := b.A{ // Stage 1: $facet b.M{ "$facet": b.D{ {"users", b.A{ b.M{"$match": matchOn}, b.M{"$project": projectFields}, }}, {"topics", b.A{ b.M{"$lookup": b.D{ {"from", "topics"}, {"pipeline", b.A{ b.M{"$match": matchOn}, b.M{"$project": projectFields}, }}, {"as", "topicDocs"}, }}, b.M{"$unwind": "$topicDocs"}, b.M{"$replaceRoot": b.M{"newRoot": "$topicDocs"}}, }}, }, }, // Stage 2: $project b.M{"$project": b.M{"combined": b.M{"$concatArrays": b.A{"$users", "$topics"}}}}, // Stage 3: $unwind b.M{"$unwind": "$combined"}, // Stage 4: $replaceRoot b.M{"$replaceRoot": b.M{"newRoot": "$combined"}}, // Stage 5: $group b.M{"$group": b.D{{"_id", "$_id"}, {"doc", b.M{"$first": "$$ROOT"}}}}, // Stage 6: $replaceRoot b.M{"$replaceRoot": b.M{"newRoot": "$doc"}}, // Stage 7: $addFields b.M{"$addFields": b.M{"matchedCount": b.M{"$sum": b.M{"$map": b.D{ {"input", b.M{"$setIntersection": b.A{"$tags", allTags}}}, {"as", "tag"}, {"in", b.D{ {"$cond", b.D{ {"if", b.M{"$regexMatch": b.D{ {"input", "$$tag"}, {"regex", "^alias:"}, }, }}, {"then", 20}, {"else", 1}, }}}}}, }}}}, } // Ensure required tags are present. for _, reqDisjunction := range req { if len(reqDisjunction) == 0 { continue } var reqTags []any for _, tag := range reqDisjunction { reqTags = append(reqTags, tag) } // Filter out documents where 'tags' intersection with 'reqTags' is an empty array. pipeline = append(pipeline, b.M{"$match": b.M{"$expr": b.M{"$ne": b.A{b.M{"$size": b.M{"$setIntersection": b.A{"$tags", reqTags}}}, 0}}}}) } pipeline = append(pipeline, // Stage 9: $sort b.M{"$sort": b.D{{"matchedCount", -1}, {"subcnt", -1}}}, // Stage 10: $limit b.M{"$limit": a.maxResults}, ) cur, err := a.db.Collection("users").Aggregate(a.ctx, pipeline) if err != nil { return nil, err } defer cur.Close(a.ctx) var subs []t.Subscription for cur.Next(a.ctx) { var topic t.Topic var sub t.Subscription if err = cur.Decode(&topic); err != nil { break } if topic.UseBt { // This is a channel, convert grp to chn name: all channel-capable // topics should appear as channels in search results. sub.Topic = t.GrpToChn(topic.Id) } else { if uid := t.ParseUid(topic.Id); !uid.IsZero() { topic.Id = uid.UserId() if topic.Id == caller { // Skip the caller. continue } } sub.Topic = topic.Id } sub.CreatedAt = topic.CreatedAt sub.UpdatedAt = topic.UpdatedAt sub.SetSubCnt(topic.SubCnt) sub.SetPublic(unmarshalBsonD(topic.Public)) sub.SetTrusted(unmarshalBsonD(topic.Trusted)) sub.SetDefaultAccess(topic.Access.Auth, topic.Access.Anon) // Indicating that the mode is not set, not 'N'. sub.ModeGiven = t.ModeUnset sub.ModeWant = t.ModeUnset sub.Private = common.FilterFoundTags(topic.Tags, index) subs = append(subs, sub) } if err == nil { err = cur.Err() } return subs, err } // FindOne returns the first topic or user which matches the given tag. func (a *adapter) FindOne(tag string) (string, error) { // Part of the pipeline identical for users and topics collections. commonPipe := b.A{b.M{"$match": b.M{"tags": tag}}, b.M{"$project": b.M{"_id": 1}}} // Must create a copy of commonPipe so the original commonPipe can be used unmodified in $unionWith. pipeline := append(slices.Clone(commonPipe), b.M{"$unionWith": b.M{"coll": "topics", "pipeline": commonPipe}}, b.M{"$limit": 1}) cur, err := a.db.Collection("users").Aggregate(a.ctx, pipeline) if err != nil { return "", err } defer cur.Close(a.ctx) var found string if cur.Next(a.ctx) { entry := map[string]any{} if err = cur.Decode(&entry); err != nil { return "", err } if id, ok := entry["_id"].(string); ok { if user := t.ParseUid(id); !user.IsZero() { found = user.UserId() } else { found = id } } } return found, cur.Err() } // Messages // MessageSave saves message to database func (a *adapter) MessageSave(msg *t.Message) error { _, err := a.db.Collection("messages").InsertOne(a.ctx, msg) return err } // MessageGetAll returns messages matching the query. func (a *adapter) MessageGetAll(topic string, forUser t.Uid, opts *t.QueryOpt) ([]t.Message, error) { var limit = a.maxMessageResults var lower, upper int requester := forUser.String() if opts != nil { if opts.Since > 0 { lower = opts.Since } if opts.Before > 0 { upper = opts.Before } if opts.Limit > 0 && opts.Limit < limit { limit = opts.Limit } } filter := b.M{ "topic": topic, "delid": b.M{"$exists": false}, "deletedfor.user": b.M{"$ne": requester}, } if upper == 0 { filter["seqid"] = b.M{"$gte": lower} } else { filter["seqid"] = b.M{"$gte": lower, "$lt": upper} } findOpts := mdbopts.Find().SetSort(b.D{{"topic", -1}, {"seqid", -1}}) findOpts.SetLimit(int64(limit)) cur, err := a.db.Collection("messages").Find(a.ctx, filter, findOpts) if err != nil { return nil, err } defer cur.Close(a.ctx) var msgs []t.Message for cur.Next(a.ctx) { var msg t.Message if err = cur.Decode(&msg); err != nil { return nil, err } msg.Content = unmarshalBsonD(msg.Content) msgs = append(msgs, msg) } return msgs, nil } func (a *adapter) messagesHardDelete(topic string) error { var err error // TODO: handle file uploads filter := b.M{"topic": topic} if _, err = a.db.Collection("dellog").DeleteMany(a.ctx, filter); err != nil { return err } if err = a.decFileUseCounter(a.ctx, "messages", filter); err != nil { return err } if _, err = a.db.Collection("messages").DeleteMany(a.ctx, filter); err != nil { return err } return err } // rangeToFilter is Mongo's equivalent of common.RangeToSql. func rangeToFilter(delRanges []t.Range, filter b.M) b.M { if len(delRanges) > 1 || delRanges[0].Hi == 0 { rangeFilter := b.A{} for _, rng := range delRanges { if rng.Hi == 0 { rangeFilter = append(rangeFilter, b.M{"seqid": rng.Low}) } else { rangeFilter = append(rangeFilter, b.M{"seqid": b.M{"$gte": rng.Low, "$lt": rng.Hi}}) } } filter["$or"] = rangeFilter } else { filter["seqid"] = b.M{"$gte": delRanges[0].Low, "$lt": delRanges[0].Hi} } return filter } // MessageDeleteList marks messages as deleted. // Soft- or Hard- is defined by forUser value: forUSer.IsZero == true is hard. func (a *adapter) MessageDeleteList(topic string, toDel *t.DelMessage) error { var err error if toDel == nil { // No filter: delete all messages. return a.messagesHardDelete(topic) } // Only some messages are being deleted delRanges := toDel.SeqIdRanges filter := b.M{ "topic": topic, // Skip already hard-deleted messages. "delid": b.M{"$exists": false}, } // Mongo's equivalent of common.RangeToSql rangeToFilter(delRanges, filter) if toDel.DeletedFor == "" { // Hard-deleting messages requires updates to the messages table. // We are asked to delete messages no older than newerThan. if newerThan := toDel.GetNewerThan(); newerThan != nil { filter["createdat"] = b.M{"$gt": newerThan} } pipeline := b.A{ b.M{"$match": filter}, b.M{"$project": b.M{"seqid": 1}}, } // Find the actual IDs still present in the database. cur, err := a.db.Collection("messages").Aggregate(a.ctx, pipeline) if err != nil { return err } defer cur.Close(a.ctx) var seqIDs []int for cur.Next(a.ctx) { var result struct { SeqID int `bson:"seqid"` } if err = cur.Decode(&result); err != nil { return err } seqIDs = append(seqIDs, result.SeqID) } if len(seqIDs) == 0 { // Nothing to delete. No need to make a log entry. All done. return nil } // Recalculate the actual ranges to delete. sort.Ints(seqIDs) delRanges = t.SliceToRanges(seqIDs) // Compose a new query with the new ranges. filter = b.M{ "topic": topic, } rangeToFilter(delRanges, filter) if err = a.decFileUseCounter(a.ctx, "messages", filter); err != nil { return err } // Hard-delete individual messages. Message is not deleted but all fields with content // are replaced with nulls. _, err = a.db.Collection("messages").UpdateMany(a.ctx, filter, b.M{"$set": b.M{ "deletedat": t.TimeNow(), "delid": toDel.DelId, "from": "", "head": nil, "content": nil, "attachments": nil}}) } else { // Soft-deleting: adding DelId to DeletedFor // Skip messages already soft-deleted for the current user filter["deletedfor.user"] = b.M{"$ne": toDel.DeletedFor} _, err = a.db.Collection("messages").UpdateMany(a.ctx, filter, b.M{"$addToSet": b.M{ "deletedfor": &t.SoftDelete{ User: toDel.DeletedFor, DelId: toDel.DelId, }}}) } // Make log entries. Needed for both hard- and soft-deleting. _, err = a.db.Collection("dellog").InsertOne(a.ctx, toDel) return err } // MessageGetDeleted returns a list of deleted message Ids. func (a *adapter) MessageGetDeleted(topic string, forUser t.Uid, opts *t.QueryOpt) ([]t.DelMessage, error) { var limit = a.maxResults var lower, upper int if opts != nil { if opts.Since > 0 { lower = opts.Since } if opts.Before > 0 { upper = opts.Before } if opts.Limit > 0 && opts.Limit < limit { limit = opts.Limit } } filter := b.M{ "topic": topic, "$or": b.A{ b.M{"deletedfor": forUser.String()}, b.M{"deletedfor": ""}, }} if upper == 0 { filter["delid"] = b.M{"$gte": lower} } else { filter["delid"] = b.M{"$gte": lower, "$lt": upper} } findOpts := mdbopts.Find(). SetSort(b.D{{"topic", 1}, {"delid", 1}}). SetLimit(int64(limit)) cur, err := a.db.Collection("dellog").Find(a.ctx, filter, findOpts) if err != nil { return nil, err } defer cur.Close(a.ctx) var dmsgs []t.DelMessage if err = cur.All(a.ctx, &dmsgs); err != nil { return nil, err } return dmsgs, nil } // Devices (for push notifications). // DeviceUpsert creates or updates a device record. func (a *adapter) DeviceUpsert(uid t.Uid, dev *t.DeviceDef) error { userId := uid.String() var user t.User err := a.db.Collection("users").FindOne(a.ctx, b.M{ "_id": userId, "devices.deviceid": dev.DeviceId}).Decode(&user) if err == nil && user.Id != "" { // current user owns this device // ArrayFilter used to avoid adding another (duplicate) device object. Update that device data updOpts := mdbopts.Update().SetArrayFilters(mdbopts.ArrayFilters{ Filters: []any{b.M{"dev.deviceid": dev.DeviceId}}}) _, err = a.db.Collection("users").UpdateOne(a.ctx, b.M{"_id": userId}, b.M{"$set": b.M{ "devices.$[dev].platform": dev.Platform, "devices.$[dev].lastseen": dev.LastSeen, "devices.$[dev].lang": dev.Lang}}, updOpts) return err } else if err == mdb.ErrNoDocuments { // device is free or owned by other user err = a.deviceInsert(userId, dev) if isDuplicateErr(err) { // Other user owns this device. // We need to delete this device from that user and then insert again if _, err = a.db.Collection("users").UpdateOne(a.ctx, b.M{"devices.deviceid": dev.DeviceId}, b.M{"$pull": b.M{"devices": b.M{"deviceid": dev.DeviceId}}}); err != nil { return err } return a.deviceInsert(userId, dev) } if err != nil { return err } return nil } return err } // deviceInsert adds device object to user.devices array func (a *adapter) deviceInsert(userId string, dev *t.DeviceDef) error { filter := b.M{"_id": userId} _, err := a.db.Collection("users").UpdateOne(a.ctx, filter, b.M{"$push": b.M{"devices": dev}}) if err != nil && strings.Contains(err.Error(), "must be an array") { // field 'devices' is not array. Make it array with 'dev' as its first element _, err = a.db.Collection("users").UpdateOne(a.ctx, filter, b.M{"$set": b.M{"devices": []any{dev}}}) } return err } // DeviceGetAll returns all devices for a given set of users. func (a *adapter) DeviceGetAll(uids ...t.Uid) (map[t.Uid][]t.DeviceDef, int, error) { ids := make([]any, len(uids)) for i, id := range uids { ids[i] = id.String() } filter := b.M{"_id": b.M{"$in": ids}} findOpts := mdbopts.Find().SetProjection(b.M{"_id": 1, "devices": 1}) cur, err := a.db.Collection("users").Find(a.ctx, filter, findOpts) if err != nil { return nil, 0, err } defer cur.Close(a.ctx) result := make(map[t.Uid][]t.DeviceDef) count := 0 var uid t.Uid for cur.Next(a.ctx) { var row struct { Id string `bson:"_id"` Devices []t.DeviceDef } if err = cur.Decode(&row); err != nil { return nil, 0, err } if len(row.Devices) > 0 { if err := uid.UnmarshalText([]byte(row.Id)); err != nil { continue } result[uid] = row.Devices count++ } } return result, count, cur.Err() } // DeviceDelete deletes a device record (push token). func (a *adapter) DeviceDelete(uid t.Uid, deviceID string) error { var err error filter := b.M{"_id": uid.String()} update := b.M{} if deviceID == "" { update["$set"] = b.M{"devices": []any{}} } else { update["$pull"] = b.M{"devices": b.M{"deviceid": deviceID}} } _, err = a.db.Collection("users").UpdateOne(a.ctx, filter, update) return err } // File upload records. The files are stored outside of the database. // FileStartUpload initializes a file upload func (a *adapter) FileStartUpload(fd *t.FileDef) error { _, err := a.db.Collection("fileuploads").InsertOne(a.ctx, fd) return err } // FileFinishUpload marks file upload as completed, successfully or otherwise. func (a *adapter) FileFinishUpload(fd *t.FileDef, success bool, size int64) (*t.FileDef, error) { now := t.TimeNow() if success { // Mark upload as completed. if _, err := a.db.Collection("fileuploads").UpdateOne(a.ctx, b.M{"_id": fd.Id}, b.M{"$set": b.M{ "updatedat": now, "status": t.UploadCompleted, "size": size, "etag": fd.ETag, "location": fd.Location, }}); err != nil { return nil, err } fd.Status = t.UploadCompleted fd.Size = size } else { // Remove record: it's now useless. if _, err := a.db.Collection("fileuploads").DeleteOne(a.ctx, b.M{"_id": fd.Id}); err != nil { return nil, err } fd.Status = t.UploadFailed fd.Size = 0 } fd.UpdatedAt = now return fd, nil } // FileGet fetches a record of a specific file func (a *adapter) FileGet(fid string) (*t.FileDef, error) { var fd t.FileDef err := a.db.Collection("fileuploads").FindOne(a.ctx, b.M{"_id": fid}).Decode(&fd) if err != nil { if err == mdb.ErrNoDocuments { return nil, nil } return nil, err } return &fd, nil } // FileDeleteUnused deletes records where UseCount is zero. If olderThan is non-zero, deletes // unused records with UpdatedAt before olderThan. // Returns array of FileDef.Location of deleted filerecords so actual files can be deleted too. func (a *adapter) FileDeleteUnused(olderThan time.Time, limit int) ([]string, error) { findOpts := mdbopts.Find() filter := b.M{"$or": b.A{ b.M{"usecount": 0}, b.M{"usecount": b.M{"$exists": false}}}} if !olderThan.IsZero() { filter["updatedat"] = b.M{"$lt": olderThan} } if limit > 0 { findOpts.SetLimit(int64(limit)) } findOpts.SetProjection(b.M{"location": 1, "_id": 0}) cur, err := a.db.Collection("fileuploads").Find(a.ctx, filter, findOpts) if err != nil { return nil, err } defer cur.Close(a.ctx) var locations []string for cur.Next(a.ctx) { var result map[string]string if err := cur.Decode(&result); err != nil { return nil, err } locations = append(locations, result["location"]) } _, err = a.db.Collection("fileuploads").DeleteMany(a.ctx, filter) return locations, err } // Given a filter query against 'messages' collection, decrement corresponding use counter in 'fileuploads' table. func (a *adapter) decFileUseCounter(ctx context.Context, collection string, msgFilter b.M) error { // Copy msgFilter filter := b.M{} for k, v := range msgFilter { filter[k] = v } filter["attachments"] = b.M{"$exists": true} fileIds, err := a.db.Collection(collection).Distinct(ctx, "attachments", filter) if err != nil { return err } if len(fileIds) > 0 { _, err = a.db.Collection("fileuploads").UpdateMany(ctx, b.M{"_id": b.M{"$in": fileIds}}, b.M{"$inc": b.M{"usecount": -1}}) } return err } // FileLinkAttachments connects given topic or message to the file record IDs from the list. func (a *adapter) FileLinkAttachments(topic string, userId, msgId t.Uid, fids []string) error { if len(fids) == 0 || (topic == "" && userId.IsZero() && msgId.IsZero()) { return t.ErrMalformed } now := t.TimeNow() var err error if msgId.IsZero() { // Only one link per user or topic is permitted. fids = fids[0:1] // Topics and users and mutable. Must unlink the previous attachments first. var table string var linkId string if topic != "" { table = "topics" linkId = topic } else { table = "users" linkId = userId.String() } // Find the old attachment. var attachments map[string][]string findOpts := mdbopts.FindOne().SetProjection(b.M{"attachments": 1, "_id": 0}) err = a.db.Collection(table).FindOne(a.ctx, b.M{"_id": linkId}, findOpts).Decode(&attachments) if err != nil { return err } if len(attachments["attachments"]) > 0 { // Decrement the use count of old attachment. if _, err = a.db.Collection("fileuploads").UpdateOne(a.ctx, b.M{"_id": attachments["attachments"][0]}, b.M{ "$set": b.M{"updatedat": now}, "$inc": b.M{"usecount": -1}, }, ); err != nil { return err } } _, err = a.db.Collection(table).UpdateOne(a.ctx, b.M{"_id": linkId}, b.M{"$set": b.M{"updatedat": now, "attachments": fids}}) if err != nil { return err } } else { _, err = a.db.Collection("messages").UpdateOne(a.ctx, b.M{"_id": msgId.String()}, b.M{"$set": b.M{"updatedat": now, "attachments": fids}}) if err != nil { return err } } ids := make([]any, len(fids)) for i, id := range fids { ids[i] = id } _, err = a.db.Collection("fileuploads").UpdateMany(a.ctx, b.M{"_id": b.M{"$in": ids}}, b.M{ "$set": b.M{"updatedat": now}, "$inc": b.M{"usecount": 1}, }, ) return err } // PCacheGet reads a persistet cache entry. func (a *adapter) PCacheGet(key string) (string, error) { var value map[string]string findOpts := mdbopts.FindOneOptions{Projection: b.M{"value": 1, "_id": 0}} if err := a.db.Collection("kvmeta").FindOne(a.ctx, b.M{"_id": key}, &findOpts).Decode(&value); err != nil { if err == mdb.ErrNoDocuments { err = t.ErrNotFound } return "", err } return value["value"], nil } // PCacheUpsert creates or updates a persistent cache entry. func (a *adapter) PCacheUpsert(key string, value string, failOnDuplicate bool) error { if strings.Contains(key, "^") { // Do not allow ^ in keys: it interferes with $match query. return t.ErrMalformed } collection := a.db.Collection("kvmeta") doc := b.M{ "value": value, } if failOnDuplicate { doc["_id"] = key doc["createdat"] = t.TimeNow() _, err := collection.InsertOne(a.ctx, doc) if mdb.IsDuplicateKeyError(err) { err = t.ErrDuplicate } return err } res := collection.FindOneAndUpdate(a.ctx, b.M{"_id": key}, b.M{"$set": doc}, mdbopts.FindOneAndUpdate().SetUpsert(true).SetReturnDocument(mdbopts.After)) return res.Err() } // PCacheDelete deletes one persistent cache entry. func (a *adapter) PCacheDelete(key string) error { _, err := a.db.Collection("kvmeta").DeleteOne(a.ctx, b.M{"_id": key}) return err } // PCacheExpire expires old entries with the given key prefix. func (a *adapter) PCacheExpire(keyPrefix string, olderThan time.Time) error { if keyPrefix == "" { return t.ErrMalformed } _, err := a.db.Collection("kvmeta").DeleteMany(a.ctx, b.M{"createdat": b.M{"$lt": olderThan}, "_id": primitive.Regex{Pattern: "^" + keyPrefix}}) return err } // GetTestDB returns a currently open database connection. func (a *adapter) GetTestDB() any { return a.db } func (a *adapter) isDbInitialized() bool { var result map[string]int findOpts := mdbopts.FindOneOptions{Projection: b.M{"value": 1, "_id": 0}} if err := a.db.Collection("kvmeta").FindOne(a.ctx, b.M{"_id": "version"}, &findOpts).Decode(&result); err != nil { return false } return true } // GetTestAdapter returns an adapter object. Useful for running tests. func GetTestAdapter() *adapter { return &adapter{} } func init() { store.RegisterAdapter(&adapter{}) } func contains(s []string, e string) bool { for _, a := range s { if a == e { return true } } return false } func union(userTags, addTags []string) []string { for _, tag := range addTags { if !contains(userTags, tag) { userTags = append(userTags, tag) } } return userTags } func diff(userTags, removeTags []string) []string { var result []string for _, tag := range userTags { if !contains(removeTags, tag) { result = append(result, tag) } } return result } // normalizeUpdateMap turns keys that hardcoded as CamelCase into lowercase (MongoDB uses lowercase by default) func normalizeUpdateMap(update map[string]any) map[string]any { result := make(map[string]any, len(update)) for key, value := range update { result[strings.ToLower(key)] = value } return result } // Recursive unmarshalling of bson.D type. // Mongo drivers unmarshalling into 'any' creates bson.D object for maps and bson.A object for slices. // We need to manually unmarshal them into correct types: map[string]any and []any respectively. func unmarshalBsonD(bsonObj any) any { if obj, ok := bsonObj.(b.D); ok && len(obj) != 0 { result := make(map[string]any) for key, val := range obj.Map() { result[key] = unmarshalBsonD(val) } return result } else if obj, ok := bsonObj.(primitive.Binary); ok { // primitive.Binary is a struct type with Subtype and Data fields. We need only Data ([]byte) return obj.Data } else if obj, ok := bsonObj.(b.A); ok { // in case of array of bson.D objects var result []any for _, elem := range obj { result = append(result, unmarshalBsonD(elem)) } return result } // Just return value as is return bsonObj } func copyBsonMap(mp b.M) b.M { result := b.M{} for k, v := range mp { result[k] = v } return result } func isDuplicateErr(err error) bool { if err == nil { return false } msg := err.Error() return strings.Contains(msg, "duplicate key error") } ================================================ FILE: server/db/mongodb/blank.go ================================================ //go:build !mongodb // +build !mongodb // This file is needed for conditional compilation. It's used when // the build tag 'mongodb' is not defined. Otherwise the adapter.go // is compiled. package mongodb ================================================ FILE: server/db/mongodb/schema.md ================================================ # MongoDB Database Schema ## Database `tinode` ### Table `users` Stores user accounts Fields: * `_id` user id, primary key * `createdat` timestamp when the user was created * `updatedat` timestamp when user metadata was updated * `access` user's default access level for peer-to-peer topics * `auth`, `anon` default permissions for authenticated and anonymous users * `public` application-defined data * `state` account state: normal (ok), suspended, soft-deleted * `stateat` timestamp when the state was last updated or NULL * `lastseen` timestamp when the user was last online * `useragent` client User-Agent used when last online * `tags` unique strings for user discovery * `devices` client devices for push notifications * `deviceid` device registration ID * `platform` device platform string (iOS, Android, Web) * `lastseen` last logged in * `lang` device language, ISO code Indexes: * `_id` primary key * `tags` multikey-index (indexed array) * `deletedat` index * `deviceids` multikey-index of push notification tokens Sample: ```json { "access": { "anon": 0 , "auth": 47 } , "createdat": "2019-10-11T12:13:14.522Z" , "state": 0, "stateat": null , "devices": null , "_id": "7yUCHniegrM" , "lastseen": "2019-10-11T12:13:14.522Z" , "public": { "fn": "Alice Johnson" , "photo": { "data": Binary('/9j/4AAQSkZJRgAB...'), "type": "jpg" } } , "state": 1 , "tags": [ "email:alice@example.com" , "tel:17025550001" ] , "updatedat": "2019-10-11T12:13:14.522Z", "useragent": "TinodeWeb/0.13 (MacIntel) tinodejs/0.13" } ``` ### Table `auth` Stores authentication secrets Fields: * `_id` unique string which identifies this record, primary key; defined as "_authentication scheme_':'_some unique value per scheme_" * `userid` ID of the user who owns the record * `secret` shared secret, for instance bcrypt of password * `authLvl` authentication level * `expires` timestamp when the records expires Indexes: * `_id` primary key * `userid` index Sample: ```json { "_id": "basic:alice" , "authLvl": 20 , "expires": "2019-10-11T12:13:14.522Z" , "secret": Binary('/9j/RgAB...'), "userid": "7yUCHniegrM" } ``` ### Table `topics` The table stores topics. Fields: * `_id` name of the topic, primary key * `createdat` topic creation time * `updatedat` timestamp of the last change to topic metadata * `access` stores topic's default access permissions * `auth`, `anon` permissions for authenticated and anonymous users respectively * `owner` ID of the user who owns the topic * `public` application-defined data * `state` topic state: normal (ok), suspended, soft-deleted * `stateat` timestamp when the state was last updated or NULL * `seqid` sequential ID of the last message * `delid` topic-sequential ID of the deletion operation * `usebt` currently unused Indexes: * `_id` primary key * `owner` index * `tags` multikey index Sample: ```json { "access": { "anon": 64 , "auth": 64 } , "delid": 0, "createdat": "2019-10-11T12:13:14.522Z", "lastmessageat": "2019-10-11T12:13:14.522Z" , "id": "p2pavVGHLCBbKrvJQIeeJ6Csw" , "owner": "v2JyG4OLSoA" , "public": { "fn": "Travel, travel, travel" , "photo": { "data": Binary('/9j/RgAB...') , "type": "jpg" } } , "seqid": 14, "state": 0, "stateat": null, "updatedat": "2019-10-11T12:13:14.522Z" , "usebt": false } ``` ### Table `subscriptions` The table stores relationships between users and topics. Fields: * `_id` used for object retrieval * `createdat` timestamp when the user was created * `updatedat` timestamp when user metadata was updated * `deletedat` currently unused * `readseqid` id of the message last read by the user * `recvseqid` id of the message last received by user device * `delid` topic-sequential ID of the soft-deletion operation * `topic` name of the topic subscribed to * `user` subscriber's user ID * `modewant` access mode that user wants when accessing the topic * `modegiven` access mode granted to user by the topic * `private` application-defined data, accessible by the user only Indexes: * `_id` primary key composed as "_topic name_':'_user ID_" * `user` index * `topic` index Sample: ```json { "_id": "grpjajVKrHn0PU:v2JyG4OLSoA" , "createdat": "2019-10-11T12:13:14.522Z" , "updatedat": "2019-10-11T12:13:14.522Z" , "deletedat": null , "user": "v2JyG4OLSoA", "topic": "grpjajVKrHn0PU" , "recvseqid": 0 , "readseqid": 0 , "modewant": 47 , "modegiven": 47 , "private": "Kirgudu" , "state": 0 } ``` ### Table `messages` The table stores `{data}` messages Fields: * `_id` currently unused, primary key * `createdat` timestamp when the message was created * `updatedat` initially equal to CreatedAt, for deleted messages equal to DeletedAt * `deletedfor` array of user IDs which soft-deleted the message * `delid` topic-sequential ID of the soft-deletion operation * `user` ID of the user who soft-deleted the message * `from` ID of the user who generated this message * `topic` which received this message * `seqid` messages ID - sequential number of the message in the topic * `head` message headers * `attachments` denormalized IDs of files attached to the message * `content` application-defined message payload Indexes: * `_id` primary key Sample: ```json { "_id": "LLXKEe9W4Bs" , "createdat": "2019-10-11T12:13:14.522Z" , "updatedat": "2019-10-11T12:13:14.522Z", "deletedfor": [ { "delid": 1 , "user": "wTI0jO9rEqY" } ] , "seqid": 3 , "topic": "p2pJhbJnya8z5PBMjSM72sSpg", "from": "wTI0jO9rEqY" , "head": { "mime": "text/x-drafty" } , "content": { "fmt": [ { "len": 6 , "tp": "ST" } ] , "txt": "Hello!" } } ``` ### Table `dellog` The table stores records of message deletions Fields: * `_id` currently unused, primary key * `createdat` timestamp when the record was created * `updatedat` timestamp equal to CreatedAt * `delid` topic-sequential ID of the deletion operation. * `deletedfor` ID of the user for soft-deletions, blank string for hard-deletions * `topic` affected topic * `seqidranges` array of ranges of deleted message IDs (see `messages.seqid`) Indexes: * `_id` primary key * `topic_delid` compound index `["Topic", "DelId"]` Sample: ```json { "_id": "9LfrjW349Rc", "createdat": "2019-10-11T12:13:14.522Z", "updatedat": "2019-10-11T12:13:14.522Z", "topic": "grpGx7fpjQwVC0", "delid": 18, "deletedfor": "xY-YHx09-WI", "seqidranges": [ { "low": 20, "hi": 25 } ] } ``` ### Table `credentials` The tables stores user credentials used for validation. * `_id` credential, primary key * `createdat` timestamp when the record was created * `updatedat` timestamp when the last validation attempt was performed (successful or not). * `method` validation method * `done` indicator if the credential is validated * `resp` expected validation response * `retries` number of failed attempts at validation * `user` id of the user who owns this credential * `value` value of the credential Indexes: * `_id` Primary key composed either as `user`:`method`:`value` for unconfirmed credentials or as `method`:`value` for confirmed. * `user` Index Sample: ```json { "Id": "tel:+17025550001", "CreatedAt": "2019-10-11T12:13:14.522Z", "UpdatedAt": "2019-10-11T12:13:14.522Z", "Method": "tel" , "Done": true , "Resp": "123456" , "Retries": 0 , "User": "k3srBRk9RYw" , "Value": "+17025550001" } ``` ### Table `fileuploads` The table stores records of uploaded files. The files themselves are stored outside of the database. * `_id` unique user-visible file name, primary key * `createdat` timestamp when the record was created * `updatedat` timestamp of when th upload has cmpleted or failed * `user` id of the user who uploaded this file. * `location` actual location of the file on the server. * `mimetype` file content type as a [Mime](https://en.wikipedia.org/wiki/MIME) string. * `size` size of the file in bytes. Could be 0 if upload has not completed yet. * `usecount` count of messages referencing this file. * `status` upload status: 0 pending, 1 completed, -1 failed. Indexes: * `_id` file name, primary key * `user` index * `usecount` index Sample: ```json { "_id": "sFmjlQ_kA6A" , "createdat": "2019-10-11T12:13:14.522Z" , "updatedat": "2019-10-11T12:13:14.522Z" , "location": "uploads/sFmjlQ_kA6A" , "mimetype": "image/jpeg" , "size": 54961090 , "usecount": 3, "status": 1 , "user": "7j-RR1V7O3Y" } ``` ================================================ FILE: server/db/mongodb/tests/mongo_test.go ================================================ // To test another db backend: // 1) Create GetAdapter function inside your db backend adapter package (like one inside mongodb adapter) // 2) Uncomment your db backend package ('backend' named package) // 3) Write own initConnectionToDb and 'db' variable // 4) Replace mongodb specific db queries inside test to your own queries. // 5) Run. package tests import ( "bytes" "context" "encoding/json" "flag" "fmt" "log" "os" "reflect" "testing" "time" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" adapter "github.com/tinode/chat/server/db" jcr "github.com/tinode/jsonco" b "go.mongodb.org/mongo-driver/bson" mdb "go.mongodb.org/mongo-driver/mongo" mdbopts "go.mongodb.org/mongo-driver/mongo/options" "github.com/tinode/chat/server/db/common" "github.com/tinode/chat/server/db/common/test_data" backend "github.com/tinode/chat/server/db/mongodb" "github.com/tinode/chat/server/logs" "github.com/tinode/chat/server/store/types" ) type configType struct { // If Reset=true test will recreate database every time it runs Reset bool `json:"reset_db_data"` // Configurations for individual adapters. Adapters map[string]json.RawMessage `json:"adapters"` } var config configType var adp adapter.Adapter var db *mdb.Database var ctx context.Context var testData *test_data.TestData func TestCreateDb(t *testing.T) { if err := adp.CreateDb(config.Reset); err != nil { t.Fatal(err) } } // ================== Create tests ================================ func TestUserCreate(t *testing.T) { for _, user := range testData.Users { if err := adp.UserCreate(user); err != nil { t.Error(err) } } count, err := db.Collection("users").CountDocuments(ctx, b.M{}) if err != nil { t.Error(err) } if count == 0 { t.Error("No users created!") } } func TestCredUpsert(t *testing.T) { // Test just inserts: for i := 0; i < 2; i++ { inserted, err := adp.CredUpsert(testData.Creds[i]) if err != nil { t.Fatal(err) } if !inserted { t.Error("Should be inserted, but updated") } } // Test duplicate: _, err := adp.CredUpsert(testData.Creds[1]) if err != types.ErrDuplicate { t.Error("Should return duplicate error but got", err) } _, err = adp.CredUpsert(testData.Creds[2]) if err != types.ErrDuplicate { t.Error("Should return duplicate error but got", err) } // Test add new unvalidated credentials inserted, err := adp.CredUpsert(testData.Creds[3]) if err != nil { t.Fatal(err) } if !inserted { t.Error("Should be inserted, but updated") } inserted, err = adp.CredUpsert(testData.Creds[3]) if err != nil { t.Fatal(err) } if inserted { t.Error("Should be updated, but inserted") } // Just insert other creds (used in other tests) for _, cred := range testData.Creds[4:] { _, err = adp.CredUpsert(cred) if err != nil { t.Fatal(err) } } } func TestAuthAddRecord(t *testing.T) { for _, rec := range testData.Recs { err := adp.AuthAddRecord(types.ParseUserId("usr"+rec.UserId), rec.Scheme, rec.Unique, rec.AuthLvl, rec.Secret, rec.Expires) if err != nil { t.Fatal(err) } } //Test duplicate err := adp.AuthAddRecord(types.ParseUserId("usr"+testData.Users[0].Id), testData.Recs[0].Scheme, testData.Recs[0].Unique, testData.Recs[0].AuthLvl, testData.Recs[0].Secret, testData.Recs[0].Expires) if err != types.ErrDuplicate { t.Fatal("Should be duplicate error but got", err) } } func TestTopicCreate(t *testing.T) { err := adp.TopicCreate(testData.Topics[0]) if err != nil { t.Error(err) } for _, tpc := range testData.Topics[3:] { err = adp.TopicCreate(tpc) if err != nil { t.Error(err) } } } func TestTopicCreateP2P(t *testing.T) { err := adp.TopicCreateP2P(testData.Subs[2], testData.Subs[3]) if err != nil { t.Fatal(err) } oldModeGiven := testData.Subs[2].ModeGiven testData.Subs[2].ModeGiven = 255 err = adp.TopicCreateP2P(testData.Subs[4], testData.Subs[2]) if err != nil { t.Fatal(err) } var got types.Subscription err = db.Collection("subscriptions").FindOne(ctx, b.M{"_id": testData.Subs[2].Id}).Decode(&got) if err != nil { t.Fatal(err) } if got.ModeGiven == oldModeGiven { t.Error("ModeGiven update failed") } } func TestTopicShare(t *testing.T) { if err := adp.TopicShare(testData.Subs[0].Topic, testData.Subs); err != nil { t.Fatal(err) } } func TestMessageSave(t *testing.T) { for _, msg := range testData.Msgs { err := adp.MessageSave(msg) if err != nil { t.Fatal(err) } } // Some messages are soft deleted, but it's ignored by adp.MessageSave for _, msg := range testData.Msgs { if len(msg.DeletedFor) > 0 { for _, del := range msg.DeletedFor { toDel := types.DelMessage{ Topic: msg.Topic, DeletedFor: del.User, DelId: del.DelId, SeqIdRanges: []types.Range{{Low: msg.SeqId}}, } adp.MessageDeleteList(msg.Topic, &toDel) } } } } func TestFileStartUpload(t *testing.T) { for _, f := range testData.Files { err := adp.FileStartUpload(f) if err != nil { t.Fatal(err) } } } // ================== Read tests ================================== func TestUserGet(t *testing.T) { // Test not found got, err := adp.UserGet(types.ParseUserId("dummyuserid")) if err == nil && got != nil { t.Error("user should be nil.") } got, err = adp.UserGet(types.ParseUserId("usr" + testData.Users[0].Id)) if err != nil { t.Fatal(err) } if !reflect.DeepEqual(got, testData.Users[0]) { t.Error(mismatchErrorString("User", got, testData.Users[0])) } } func TestUserGetAll(t *testing.T) { // Test not found got, err := adp.UserGetAll(types.ParseUserId("dummyuserid"), types.ParseUserId("otherdummyid")) if err != nil { t.Fatal(err) } if got != nil { t.Error("result users should be nil.") } got, err = adp.UserGetAll(types.ParseUserId("usr"+testData.Users[0].Id), types.ParseUserId("usr"+testData.Users[1].Id)) if err != nil { t.Fatal(err) } if len(got) != 2 { t.Fatal(mismatchErrorString("resultUsers length", len(got), 2)) } for i, usr := range got { if !reflect.DeepEqual(&usr, testData.Users[i]) { t.Error(mismatchErrorString("User", &usr, testData.Users[i])) } } } func TestUserGetByCred(t *testing.T) { // Test not found got, err := adp.UserGetByCred("foo", "bar") if err != nil { t.Fatal(err) } if got != types.ZeroUid { t.Error("result uid should be ZeroUid") } got, _ = adp.UserGetByCred(testData.Creds[0].Method, testData.Creds[0].Value) if got != types.ParseUserId("usr"+testData.Creds[0].User) { t.Error(mismatchErrorString("Uid", got, types.ParseUserId("usr"+testData.Creds[0].User))) } } func TestCredGetActive(t *testing.T) { got, err := adp.CredGetActive(types.ParseUserId("usr"+testData.Users[2].Id), "tel") if err != nil { t.Error(err) } if !reflect.DeepEqual(got, testData.Creds[3]) { t.Error(mismatchErrorString("Credential", got, testData.Creds[3])) } // Test not found got, err = adp.CredGetActive(types.ParseUserId("dummyusrid"), "") if err != nil { t.Error(err) } if got != nil { t.Error("result should be nil, got", got) } } func TestCredGetAll(t *testing.T) { got, err := adp.CredGetAll(types.ParseUserId("usr"+testData.Users[2].Id), "", false) if err != nil { t.Fatal(err) } if len(got) != 3 { t.Error(mismatchErrorString("Credentials length", len(got), 3)) } got, _ = adp.CredGetAll(types.ParseUserId("usr"+testData.Users[2].Id), "tel", false) if len(got) != 2 { t.Error(mismatchErrorString("Credentials length", len(got), 2)) } got, _ = adp.CredGetAll(types.ParseUserId("usr"+testData.Users[2].Id), "", true) if len(got) != 1 { t.Error(mismatchErrorString("Credentials length", len(got), 1)) } got, _ = adp.CredGetAll(types.ParseUserId("usr"+testData.Users[2].Id), "tel", true) if len(got) != 1 { t.Error(mismatchErrorString("Credentials length", len(got), 1)) } } func TestAuthGetUniqueRecord(t *testing.T) { uid, authLvl, secret, expires, err := adp.AuthGetUniqueRecord("basic:alice") if err != nil { t.Fatal(err) } if uid != types.ParseUserId("usr"+testData.Recs[0].UserId) || authLvl != testData.Recs[0].AuthLvl || !bytes.Equal(secret, testData.Recs[0].Secret) || expires != testData.Recs[0].Expires { got := fmt.Sprintf("%v %v %v %v", uid, authLvl, secret, expires) want := fmt.Sprintf("%v %v %v %v", testData.Recs[0].UserId, testData.Recs[0].AuthLvl, testData.Recs[0].Secret, testData.Recs[0].Expires) t.Error(mismatchErrorString("Auth record", got, want)) } // Test not found uid, _, _, _, err = adp.AuthGetUniqueRecord("qwert:asdfg") if err == nil && !uid.IsZero() { t.Error("Auth record found but shouldn't. Uid:", uid.String()) } } func TestAuthGetRecord(t *testing.T) { recId, authLvl, secret, expires, err := adp.AuthGetRecord(types.ParseUserId("usr"+testData.Recs[0].UserId), "basic") if err != nil { t.Fatal(err) } if recId != testData.Recs[0].Unique || authLvl != testData.Recs[0].AuthLvl || !bytes.Equal(secret, testData.Recs[0].Secret) || expires != testData.Recs[0].Expires { got := fmt.Sprintf("%v %v %v %v", recId, authLvl, secret, expires) want := fmt.Sprintf("%v %v %v %v", testData.Recs[0].Unique, testData.Recs[0].AuthLvl, testData.Recs[0].Secret, testData.Recs[0].Expires) t.Error(mismatchErrorString("Auth record", got, want)) } // Test not found recId, _, _, _, err = adp.AuthGetRecord(types.ParseUserId("dummyuserid"), "scheme") if err != types.ErrNotFound { t.Error("Auth record found but shouldn't. recId:", recId) } } func TestTopicGet(t *testing.T) { got, err := adp.TopicGet(testData.Topics[0].Id) if err != nil { t.Fatal(err) } if !reflect.DeepEqual(got, testData.Topics[0]) { t.Error(mismatchErrorString("Topic", got, testData.Topics[0])) } // Test not found got, err = adp.TopicGet("asdfasdfasdf") if err != nil { t.Fatal(err) } if got != nil { t.Error("Topic should be nil but got:", got) } } func TestTopicsForUser(t *testing.T) { qOpts := types.QueryOpt{ Topic: testData.Topics[1].Id, Limit: 999, } gotSubs, err := adp.TopicsForUser(types.ParseUserId("usr"+testData.Users[0].Id), false, &qOpts) if err != nil { t.Fatal(err) } if len(gotSubs) != 1 { t.Error(mismatchErrorString("Subs length", len(gotSubs), 1)) } gotSubs, err = adp.TopicsForUser(types.ParseUserId("usr"+testData.Users[1].Id), true, nil) if err != nil { t.Fatal(err) } if len(gotSubs) != 2 { t.Error(mismatchErrorString("Subs length (2)", len(gotSubs), 2)) } qOpts.Topic = "" ims := testData.Now.Add(15 * time.Minute) qOpts.IfModifiedSince = &ims gotSubs, err = adp.TopicsForUser(types.ParseUserId("usr"+testData.Users[0].Id), false, &qOpts) if err != nil { t.Fatal(err) } if len(gotSubs) != 1 { t.Error(mismatchErrorString("Subs length (IMS)", len(gotSubs), 1)) } // time.Now() is correct (as opposite to testData.Now) // Topic is modified using time.Now(). ims = time.Now().Add(15 * time.Minute) gotSubs, err = adp.TopicsForUser(types.ParseUserId("usr"+testData.Users[0].Id), false, &qOpts) if err != nil { t.Fatal(err) } if len(gotSubs) != 0 { t.Error(mismatchErrorString("Subs length (IMS 2)", len(gotSubs), 0)) } } func TestUsersForTopic(t *testing.T) { qOpts := types.QueryOpt{ User: types.ParseUserId("usr" + testData.Users[0].Id), Limit: 999, } gotSubs, err := adp.UsersForTopic(testData.Topics[0].Id, false, &qOpts) if err != nil { t.Fatal(err) } if len(gotSubs) != 1 { t.Error(mismatchErrorString("Subs length", len(gotSubs), 1)) } gotSubs, err = adp.UsersForTopic(testData.Topics[0].Id, true, nil) if err != nil { t.Fatal(err) } if len(gotSubs) != 2 { t.Error(mismatchErrorString("Subs length", len(gotSubs), 2)) } gotSubs, err = adp.UsersForTopic(testData.Topics[1].Id, false, nil) if err != nil { t.Fatal(err) } if len(gotSubs) != 2 { t.Error(mismatchErrorString("Subs length", len(gotSubs), 2)) } } func TestOwnTopics(t *testing.T) { gotSubs, err := adp.OwnTopics(types.ParseUserId("usr" + testData.Users[0].Id)) if err != nil { t.Fatal(err) } if len(gotSubs) != 1 { t.Fatalf("Got topic length %v instead of %v", len(gotSubs), 1) } if gotSubs[0] != testData.Topics[0].Id { t.Errorf("Got topic %v instead of %v", gotSubs[0], testData.Topics[0].Id) } } func TestSubscriptionGet(t *testing.T) { got, err := adp.SubscriptionGet(testData.Topics[0].Id, types.ParseUserId("usr"+testData.Users[0].Id), false) if err != nil { t.Error(err) } opts := cmpopts.IgnoreUnexported(types.Subscription{}, types.ObjHeader{}) if !cmp.Equal(got, testData.Subs[0], opts) { t.Error(mismatchErrorString("Subs", got, testData.Subs[0])) } // Test not found got, err = adp.SubscriptionGet("dummytopic", types.ParseUserId("dummyuserid"), false) if err != nil { t.Error(err) } if got != nil { t.Error("result sub should be nil.") } } func TestSubsForUser(t *testing.T) { gotSubs, err := adp.SubsForUser(types.ParseUserId("usr" + testData.Users[0].Id)) if err != nil { t.Error(err) } if len(gotSubs) != 2 { t.Error(mismatchErrorString("Subs length", len(gotSubs), 1)) } // Test not found gotSubs, err = adp.SubsForUser(types.ParseUserId("usr12345678")) if err != nil { t.Error(err) } if len(gotSubs) != 0 { t.Error(mismatchErrorString("Subs length", len(gotSubs), 0)) } } func TestSubsForTopic(t *testing.T) { qOpts := types.QueryOpt{ User: types.ParseUserId("usr" + testData.Users[0].Id), Limit: 999, } gotSubs, err := adp.SubsForTopic(testData.Topics[0].Id, false, &qOpts) if err != nil { t.Error(err) } if len(gotSubs) != 1 { t.Error(mismatchErrorString("Subs length", len(gotSubs), 1)) } // Test not found gotSubs, err = adp.SubsForTopic("dummytopicid", false, nil) if err != nil { t.Error(err) } if len(gotSubs) != 0 { t.Error(mismatchErrorString("Subs length", len(gotSubs), 0)) } } func TestFind(t *testing.T) { reqTags := [][]string{{"alice", "bob", "carol", "travel", "qwer", "asdf", "zxcv"}} gotSubs, err := adp.Find("usr"+testData.Users[2].Id, "", reqTags, nil, true) if err != nil { t.Error(err) } if len(gotSubs) != 3 { t.Error(mismatchErrorString("result length", len(gotSubs), 3)) } } func TestMessageGetAll(t *testing.T) { opts := types.QueryOpt{ Since: 1, Before: 2, Limit: 999, } gotMsgs, err := adp.MessageGetAll(testData.Topics[0].Id, types.ParseUserId("usr"+testData.Users[0].Id), &opts) if err != nil { t.Fatal(err) } if len(gotMsgs) != 1 { t.Error(mismatchErrorString("Messages length opts", len(gotMsgs), 1)) } gotMsgs, _ = adp.MessageGetAll(testData.Topics[0].Id, types.ParseUserId("usr"+testData.Users[0].Id), nil) if len(gotMsgs) != 2 { t.Error(mismatchErrorString("Messages length no opts", len(gotMsgs), 2)) } gotMsgs, _ = adp.MessageGetAll(testData.Topics[0].Id, types.ZeroUid, nil) if len(gotMsgs) != 3 { t.Error(mismatchErrorString("Messages length zero uid", len(gotMsgs), 3)) } } func TestFileGet(t *testing.T) { // General test done during TestFileFinishUpload(). // Test not found got, err := adp.FileGet("dummyfileid") if err != nil { if got != nil { t.Error("File found but shouldn't:", got) } } } // ================== Update tests ================================ func TestUserUpdate(t *testing.T) { update := map[string]any{ "UserAgent": "Test Agent v0.11", "UpdatedAt": testData.Now.Add(30 * time.Minute), } err := adp.UserUpdate(types.ParseUserId("usr"+testData.Users[0].Id), update) if err != nil { t.Fatal(err) } var got types.User err = db.Collection("users").FindOne(ctx, b.M{"_id": testData.Users[0].Id}).Decode(&got) if err != nil { t.Fatal(err) } if got.UserAgent != "Test Agent v0.11" { t.Error(mismatchErrorString("UserAgent", got.UserAgent, "Test Agent v0.11")) } if got.UpdatedAt.Equal(got.CreatedAt) { t.Error("UpdatedAt field not updated") } } func TestUserUpdateTags(t *testing.T) { addTags := testData.Tags[0] removeTags := testData.Tags[1] resetTags := testData.Tags[2] got, err := adp.UserUpdateTags(types.ParseUserId("usr"+testData.Users[0].Id), addTags, nil, nil) if err != nil { t.Fatal(err) } want := []string{"alice", "tag1"} if !reflect.DeepEqual(got, want) { t.Error(mismatchErrorString("Tags", got, want)) } got, _ = adp.UserUpdateTags(types.ParseUserId("usr"+testData.Users[0].Id), nil, removeTags, nil) want = nil if !reflect.DeepEqual(got, want) { t.Error(mismatchErrorString("Tags", got, want)) } got, _ = adp.UserUpdateTags(types.ParseUserId("usr"+testData.Users[0].Id), nil, nil, resetTags) want = []string{"alice", "tag111", "tag333"} if !reflect.DeepEqual(got, want) { t.Error(mismatchErrorString("Tags", got, want)) } got, _ = adp.UserUpdateTags(types.ParseUserId("usr"+testData.Users[0].Id), addTags, removeTags, nil) want = []string{"tag111", "tag333"} if !reflect.DeepEqual(got, want) { t.Error(mismatchErrorString("Tags", got, want)) } got, _ = adp.UserUpdateTags(types.ParseUserId("usr"+testData.Users[0].Id), addTags, removeTags, nil) want = []string{"tag111", "tag333"} if !reflect.DeepEqual(got, want) { t.Error(mismatchErrorString("Tags", got, want)) } } func TestCredFail(t *testing.T) { err := adp.CredFail(types.ParseUserId("usr"+testData.Creds[3].User), "tel") if err != nil { t.Error(err) } // Check if fields updated var got types.Credential _ = db.Collection("credentials").FindOne(ctx, b.M{ "user": testData.Creds[3].User, "method": "tel", "value": testData.Creds[3].Value}).Decode(&got) if got.Retries != 1 { t.Error(mismatchErrorString("Retries count", got.Retries, 1)) } if got.UpdatedAt.Equal(got.CreatedAt) { t.Error("UpdatedAt field not updated") } } func TestCredConfirm(t *testing.T) { err := adp.CredConfirm(types.ParseUserId("usr"+testData.Creds[3].User), "tel") if err != nil { t.Fatal(err) } // Test fields are updated var got types.Credential err = db.Collection("credentials").FindOne(ctx, b.M{ "user": testData.Creds[3].User, "method": "tel", "value": testData.Creds[3].Value}).Decode(&got) if err != nil { t.Fatal(err) } if got.UpdatedAt.Equal(got.CreatedAt) { t.Error("Credential not updated correctly") } // and uncomfirmed credential deleted err = db.Collection("credentials").FindOne(ctx, b.M{"_id": testData.Creds[3].User + ":" + got.Method + ":" + got.Value}).Decode(&got) if err != mdb.ErrNoDocuments { t.Error("Uncomfirmed credential not deleted") } } func TestAuthUpdRecord(t *testing.T) { rec := testData.Recs[1] newSecret := []byte{'s', 'e', 'c', 'r', 'e', 't'} err := adp.AuthUpdRecord(types.ParseUserId("usr"+rec.UserId), rec.Scheme, rec.Unique, rec.AuthLvl, newSecret, rec.Expires) if err != nil { t.Fatal(err) } var got common.AuthRecord err = db.Collection("auth").FindOne(ctx, b.M{"_id": rec.Unique}).Decode(&got) if err != nil { t.Fatal(err) } if bytes.Equal(got.Secret, rec.Secret) { t.Error(mismatchErrorString("Secret", got.Secret, rec.Secret)) } // Test with auth ID (unique) change newId := "basic:bob12345" err = adp.AuthUpdRecord(types.ParseUserId("usr"+rec.UserId), rec.Scheme, newId, rec.AuthLvl, newSecret, rec.Expires) if err != nil { t.Fatal(err) } // Test if old ID deleted err = db.Collection("auth").FindOne(ctx, b.M{"_id": rec.Unique}).Decode(&got) if err == nil || err != mdb.ErrNoDocuments { t.Errorf("Unique not changed. Got error: %v; ID: %v", err, got.Unique) } if bytes.Equal(got.Secret, rec.Secret) { t.Error(mismatchErrorString("Secret", got.Secret, rec.Secret)) } if bytes.Equal(got.Secret, rec.Secret) { t.Error(mismatchErrorString("Secret", got.Secret, rec.Secret)) } } func TestTopicUpdateOnMessage(t *testing.T) { msg := types.Message{ ObjHeader: types.ObjHeader{ CreatedAt: testData.Now.Add(33 * time.Minute), }, SeqId: 66, } err := adp.TopicUpdateOnMessage(testData.Topics[2].Id, &msg) if err != nil { t.Fatal(err) } var got types.Topic err = db.Collection("topics").FindOne(ctx, b.M{"_id": testData.Topics[2].Id}).Decode(&got) if err != nil { t.Fatal(err) } if got.TouchedAt != msg.CreatedAt || got.SeqId != msg.SeqId { t.Error(mismatchErrorString("TouchedAt", got.TouchedAt, msg.CreatedAt)) t.Error(mismatchErrorString("SeqId", got.SeqId, msg.SeqId)) } } func TestTopicUpdate(t *testing.T) { update := map[string]any{ "UpdatedAt": testData.Now.Add(55 * time.Minute), } err := adp.TopicUpdate(testData.Topics[0].Id, update) if err != nil { t.Fatal(err) } var got types.Topic _ = db.Collection("topics").FindOne(ctx, b.M{"_id": testData.Topics[0].Id}).Decode(&got) if got.UpdatedAt != update["UpdatedAt"] { t.Error(mismatchErrorString("UpdatedAt", got.UpdatedAt, update["UpdatedAt"])) } } func TestTopicOwnerChange(t *testing.T) { err := adp.TopicOwnerChange(testData.Topics[0].Id, types.ParseUserId("usr"+testData.Users[1].Id)) if err != nil { t.Fatal(err) } var got types.Topic _ = db.Collection("topics").FindOne(ctx, b.M{"_id": testData.Topics[0].Id}).Decode(&got) if got.Owner != testData.Users[1].Id { t.Error(mismatchErrorString("Owner", got.Owner, testData.Users[1].Id)) } } func TestSubsUpdate(t *testing.T) { update := map[string]any{ "UpdatedAt": testData.Now.Add(22 * time.Minute), } err := adp.SubsUpdate(testData.Topics[0].Id, types.ParseUserId("usr"+testData.Users[0].Id), update) if err != nil { t.Fatal(err) } var got types.Subscription _ = db.Collection("subscriptions").FindOne(ctx, b.M{"_id": testData.Topics[0].Id + ":" + testData.Users[0].Id}).Decode(&got) if got.UpdatedAt != update["UpdatedAt"] { t.Error(mismatchErrorString("UpdatedAt", got.UpdatedAt, update["UpdatedAt"])) } err = adp.SubsUpdate(testData.Topics[1].Id, types.ZeroUid, update) if err != nil { t.Fatal(err) } _ = db.Collection("subscriptions").FindOne(ctx, b.M{"topic": testData.Topics[1].Id}).Decode(&got) if got.UpdatedAt != update["UpdatedAt"] { t.Error(mismatchErrorString("UpdatedAt", got.UpdatedAt, update["UpdatedAt"])) } } func TestSubsDelete(t *testing.T) { err := adp.SubsDelete(testData.Topics[1].Id, types.ParseUserId("usr"+testData.Users[0].Id)) if err != nil { t.Fatal(err) } var got types.Subscription _ = db.Collection("subscriptions").FindOne(ctx, b.M{"_id": testData.Topics[1].Id + ":" + testData.Users[0].Id}).Decode(&got) if got.DeletedAt == nil { t.Error(mismatchErrorString("DeletedAt", got.DeletedAt, nil)) } } func TestDeviceUpsert(t *testing.T) { err := adp.DeviceUpsert(types.ParseUserId("usr"+testData.Users[0].Id), testData.Devs[0]) if err != nil { t.Fatal(err) } var got types.User err = db.Collection("users").FindOne(ctx, b.M{"_id": testData.Users[0].Id}).Decode(&got) if err != nil { t.Error(err) } if !reflect.DeepEqual(got.DeviceArray[0], testData.Devs[0]) { t.Error(mismatchErrorString("Device", got.DeviceArray[0], testData.Devs[0])) } // Test update testData.Devs[0].Platform = "Web" err = adp.DeviceUpsert(types.ParseUserId("usr"+testData.Users[0].Id), testData.Devs[0]) if err != nil { t.Fatal(err) } err = db.Collection("users").FindOne(ctx, b.M{"_id": testData.Users[0].Id}).Decode(&got) if err != nil { t.Error(err) } if got.DeviceArray[0].Platform != "Web" { t.Error("Device not updated.", got.DeviceArray[0]) } // Test add same device to another user err = adp.DeviceUpsert(types.ParseUserId("usr"+testData.Users[1].Id), testData.Devs[0]) if err != nil { t.Fatal(err) } err = db.Collection("users").FindOne(ctx, b.M{"_id": testData.Users[1].Id}).Decode(&got) if err != nil { t.Error(err) } if got.DeviceArray[0].Platform != "Web" { t.Error("Device not updated.", got.DeviceArray[0]) } err = adp.DeviceUpsert(types.ParseUserId("usr"+testData.Users[2].Id), testData.Devs[1]) if err != nil { t.Error(err) } } func TestMessageAttachments(t *testing.T) { fids := []string{testData.Files[0].Id, testData.Files[1].Id} err := adp.FileLinkAttachments("", types.ZeroUid, types.ParseUid(testData.Msgs[1].Id), fids) if err != nil { t.Fatal(err) } var got map[string][]string findOpts := mdbopts.FindOne().SetProjection(b.M{"attachments": 1, "_id": 0}) err = db.Collection("messages").FindOne(ctx, b.M{"_id": testData.Msgs[1].Id}, findOpts).Decode(&got) if err != nil { t.Fatal(err) } if !reflect.DeepEqual(got["attachments"], fids) { t.Error(mismatchErrorString("Attachments", got["attachments"], fids)) } var got2 map[string]int findOpts = mdbopts.FindOne().SetProjection(b.M{"usecount": 1, "_id": 0}) err = db.Collection("fileuploads").FindOne(ctx, b.M{"_id": testData.Files[0].Id}, findOpts).Decode(&got2) if err != nil { t.Fatal(err) } if got2["usecount"] != 1 { t.Error(mismatchErrorString("UseCount", got2["usecount"], 1)) } } func TestFileFinishUpload(t *testing.T) { got, err := adp.FileFinishUpload(testData.Files[0], true, 22222) if err != nil { t.Fatal(err) } if got.Status != types.UploadCompleted { t.Error(mismatchErrorString("Status", got.Status, types.UploadCompleted)) } if got.Size != 22222 { t.Error(mismatchErrorString("Size", got.Size, 22222)) } } // ================== Other tests ================================= func TestDeviceGetAll(t *testing.T) { uid0 := types.ParseUserId("usr" + testData.Users[0].Id) uid1 := types.ParseUserId("usr" + testData.Users[1].Id) uid2 := types.ParseUserId("usr" + testData.Users[2].Id) gotDevs, count, err := adp.DeviceGetAll(uid0, uid1, uid2) if err != nil { t.Fatal(err) } if count != 2 { t.Fatal(mismatchErrorString("count", count, 2)) } if !reflect.DeepEqual(gotDevs[uid1][0], *testData.Devs[0]) { t.Error(mismatchErrorString("Device", gotDevs[uid1][0], *testData.Devs[0])) } if !reflect.DeepEqual(gotDevs[uid2][0], *testData.Devs[1]) { t.Error(mismatchErrorString("Device", gotDevs[uid2][0], *testData.Devs[1])) } } func TestDeviceDelete(t *testing.T) { err := adp.DeviceDelete(types.ParseUserId("usr"+testData.Users[1].Id), testData.Devs[0].DeviceId) if err != nil { t.Fatal(err) } var got types.User err = db.Collection("users").FindOne(ctx, b.M{"_id": testData.Users[1].Id}).Decode(&got) if err != nil { t.Fatal(err) } if len(got.DeviceArray) != 0 { t.Error("Device not deleted:", got.DeviceArray) } err = adp.DeviceDelete(types.ParseUserId("usr"+testData.Users[2].Id), "") if err != nil { t.Fatal(err) } err = db.Collection("users").FindOne(ctx, b.M{"_id": testData.Users[2].Id}).Decode(&got) if err != nil { t.Fatal(err) } if len(got.DeviceArray) != 0 { t.Error("Device not deleted:", got.DeviceArray) } } // ================== Persistent Cache tests ====================== func TestPCacheUpsert(t *testing.T) { err := adp.PCacheUpsert("test_key", "test_value", false) if err != nil { t.Fatal(err) } // Test duplicate with failOnDuplicate = true err = adp.PCacheUpsert("test_key2", "test_value2", true) if err != nil { t.Fatal(err) } err = adp.PCacheUpsert("test_key2", "new_value", true) if err != types.ErrDuplicate { t.Error("Expected duplicate error") } } func TestPCacheGet(t *testing.T) { value, err := adp.PCacheGet("test_key") if err != nil { t.Fatal(err) } if value != "test_value" { t.Error(mismatchErrorString("Cache value", value, "test_value")) } // Test not found _, err = adp.PCacheGet("nonexistent") if err != types.ErrNotFound { t.Error("Expected not found error") } } func TestPCacheDelete(t *testing.T) { err := adp.PCacheDelete("test_key") if err != nil { t.Fatal(err) } // Verify deleted _, err = adp.PCacheGet("test_key") if err != types.ErrNotFound { t.Error("Key should be deleted") } } func TestPCacheExpire(t *testing.T) { // Insert some test keys with prefix adp.PCacheUpsert("prefix_key1", "value1", false) adp.PCacheUpsert("prefix_key2", "value2", false) // Expire keys older than now (should delete all test keys) err := adp.PCacheExpire("prefix_", time.Now().Add(1*time.Minute)) if err != nil { t.Fatal(err) } } // ================== Delete tests ================================ func TestCredDel(t *testing.T) { err := adp.CredDel(types.ParseUserId("usr"+testData.Users[0].Id), "email", "alice@test.example.com") if err != nil { t.Fatal(err) } var got []map[string]any cur, err := db.Collection("credentials").Find(ctx, b.M{"method": "email", "value": "alice@test.example.com"}) if err != nil { t.Fatal(err) } if err = cur.All(ctx, &got); err != nil { t.Fatal(err) } if len(got) != 0 { t.Error("Got result but shouldn't", got) } err = adp.CredDel(types.ParseUserId("usr"+testData.Users[1].Id), "", "") if err != nil { t.Fatal(err) } cur, err = db.Collection("credentials").Find(ctx, b.M{"user": testData.Users[1].Id}) if err != nil { t.Fatal(err) } if err = cur.All(ctx, &got); err != nil { t.Fatal(err) } if len(got) != 0 { t.Error("Got result but shouldn't", got) } } func TestAuthDelScheme(t *testing.T) { // tested during TestAuthUpdRecord } func TestAuthDelAllRecords(t *testing.T) { delCount, err := adp.AuthDelAllRecords(types.ParseUserId("usr" + testData.Recs[0].UserId)) if err != nil { t.Fatal(err) } if delCount != 1 { t.Error(mismatchErrorString("delCount", delCount, 1)) } // With dummy user delCount, _ = adp.AuthDelAllRecords(types.ParseUserId("dummyuserid")) if delCount != 0 { t.Error(mismatchErrorString("delCount", delCount, 0)) } } func TestSubsDelForUser(t *testing.T) { // Tested during TestUserDelete (both hard and soft deletions) } func TestMessageDeleteList(t *testing.T) { toDel := types.DelMessage{ ObjHeader: types.ObjHeader{ Id: testData.UGen.GetStr(), CreatedAt: testData.Now, UpdatedAt: testData.Now, }, Topic: testData.Topics[1].Id, DeletedFor: testData.Users[2].Id, DelId: 1, SeqIdRanges: []types.Range{{Low: 9}, {Low: 3, Hi: 7}}, } err := adp.MessageDeleteList(toDel.Topic, &toDel) if err != nil { t.Fatal(err) } var got []types.Message cur, err := db.Collection("messages").Find(ctx, b.M{"topic": toDel.Topic}) if err != nil { t.Fatal(err) } if err = cur.All(ctx, &got); err != nil { t.Fatal(err) } for _, msg := range got { if msg.SeqId == 1 && msg.DeletedFor != nil { t.Error("Message with SeqID=1 should not be deleted") } if msg.SeqId == 5 && msg.DeletedFor == nil { t.Error("Message with SeqID=5 should be deleted") } if msg.SeqId == 11 && msg.DeletedFor != nil { t.Error("Message with SeqID=11 should not be deleted") } } // toDel = types.DelMessage{ ObjHeader: types.ObjHeader{ Id: testData.UGen.GetStr(), CreatedAt: testData.Now, UpdatedAt: testData.Now, }, Topic: testData.Topics[0].Id, DelId: 3, SeqIdRanges: []types.Range{{Low: 1, Hi: 3}}, } err = adp.MessageDeleteList(toDel.Topic, &toDel) if err != nil { t.Fatal(err) } cur, err = db.Collection("messages").Find(ctx, b.M{"topic": toDel.Topic}) if err != nil { t.Fatal(err) } if err = cur.All(ctx, &got); err != nil { t.Fatal(err) } for _, msg := range got { if msg.Content != nil && msg.SeqId != 3 { t.Error("Message not deleted:", msg.SeqId) } } err = adp.MessageDeleteList(testData.Topics[0].Id, nil) if err != nil { t.Fatal(err) } cur, err = db.Collection("messages").Find(ctx, b.M{"topic": testData.Topics[0].Id}) if err != nil { t.Fatal(err) } if err = cur.All(ctx, &got); err != nil { t.Fatal(err) } if len(got) != 0 { t.Error("Result should be empty:", got) } } func TestTopicDelete(t *testing.T) { err := adp.TopicDelete(testData.Topics[1].Id, false, false) if err != nil { t.Fatal() } var got types.Topic cur, err := db.Collection("topics").Find(ctx, b.M{"topic": testData.Topics[1].Id}) if err != nil { t.Fatal(err) } for cur.Next(ctx) { if err = cur.Decode(&got); err != nil { t.Error(err) } if got.State != types.StateDeleted { t.Error("Soft delete failed:", got) } } err = adp.TopicDelete(testData.Topics[0].Id, true, true) if err != nil { t.Fatal() } var got2 []types.Topic cur, err = db.Collection("topics").Find(ctx, b.M{"topic": testData.Topics[0].Id}) if err != nil { t.Fatal(err) } if err = cur.All(ctx, &got2); err != nil { t.Fatal(err) } if len(got2) != 0 { t.Error("Hard delete failed:", got2) } } func TestFileDeleteUnused(t *testing.T) { // time.Now() is correct (as opposite to testData.Now): // the FileFinishUpload uses time.Now() as a timestamp. locs, err := adp.FileDeleteUnused(time.Now().Add(1*time.Minute), 999) if err != nil { t.Fatal(err) } if len(locs) != 2 { t.Error(mismatchErrorString("Locations length", len(locs), 2)) } } func TestUserDelete(t *testing.T) { err := adp.UserDelete(types.ParseUserId("usr"+testData.Users[0].Id), false) if err != nil { t.Fatal(err) } var got types.User err = db.Collection("users").FindOne(ctx, b.M{"_id": testData.Users[0].Id}).Decode(&got) if err != nil { t.Fatal(err) } if got.State != types.StateDeleted { t.Error("User soft delete failed", got) } err = adp.UserDelete(types.ParseUserId("usr"+testData.Users[1].Id), true) if err != nil { t.Fatal(err) } err = db.Collection("users").FindOne(ctx, b.M{"_id": testData.Users[1].Id}).Decode(&got) if err != mdb.ErrNoDocuments { t.Error("User hard delete failed", err) } } func TestUserUnreadCount(t *testing.T) { uids := []types.Uid{ types.ParseUserId("usr" + testData.Users[1].Id), types.ParseUserId("usr" + testData.Users[2].Id), } expected := map[types.Uid]int{uids[0]: 0, uids[1]: 166} counts, err := adp.UserUnreadCount(uids...) if err != nil { t.Fatal(err) } if len(counts) != 2 { t.Error(mismatchErrorString("UnreadCount length", len(counts), 2)) } for uid, unread := range counts { if expected[uid] != unread { t.Error(mismatchErrorString("UnreadCount", unread, expected[uid])) } } // Test not found (even if the account is not found, the call must return one record). uid := types.ParseUserId("dummyuserid") counts, err = adp.UserUnreadCount(uid) if err != nil { t.Fatal(err) } if len(counts) != 1 { t.Error(mismatchErrorString("UnreadCount length (dummy)", len(counts), 1)) } if counts[uid] != 0 { t.Error(mismatchErrorString("Non-zero UnreadCount (dummy)", counts[uid], 0)) } } // ================== Other tests ================================= func TestMessageGetDeleted(t *testing.T) { qOpts := types.QueryOpt{ Since: 1, Before: 10, Limit: 999, } got, err := adp.MessageGetDeleted(testData.Topics[1].Id, types.ParseUserId("usr"+testData.Users[2].Id), &qOpts) if err != nil { t.Fatal(err) } if len(got) != 1 { t.Error(mismatchErrorString("result length", len(got), 1)) } } // ================================================================ func mismatchErrorString(key string, got, want any) string { return fmt.Sprintf("%s mismatch:\nGot = %+v\nWant = %+v", key, got, want) } func init() { logs.Init(os.Stderr, "stdFlags") adp = backend.GetTestAdapter() conffile := flag.String("config", "./test.conf", "config of the database connection") if file, err := os.Open(*conffile); err != nil { log.Fatal("Failed to read config file:", err) } else if err = json.NewDecoder(jcr.New(file)).Decode(&config); err != nil { log.Fatal("Failed to parse config file:", err) } if adp == nil { log.Fatal("Database adapter is missing") } if adp.IsOpen() { log.Print("Connection is already opened") } err := adp.Open(config.Adapters[adp.GetName()]) if err != nil { log.Fatal(err) } db = adp.GetTestDB().(*mdb.Database) testData = test_data.InitTestData() if testData == nil { log.Fatal("Failed to initialize test data") } } ================================================ FILE: server/db/mongodb/tests/test.conf ================================================ { "reset_db_data": true, "adapters": { "mongodb": { "database": "tinode_test", //"replica_set": "rs0", "addresses": "localhost:27017", //"username": "tinode_test", //"password": "tinode_test", } } } ================================================ FILE: server/db/mysql/adapter.go ================================================ //go:build mysql // +build mysql // Package mysql is a database adapter for MySQL. package mysql import ( "context" "database/sql" "encoding/json" "errors" "hash/fnv" "sort" "strconv" "strings" "time" ms "github.com/go-sql-driver/mysql" "github.com/jmoiron/sqlx" "github.com/tinode/chat/server/auth" "github.com/tinode/chat/server/db/common" "github.com/tinode/chat/server/store" t "github.com/tinode/chat/server/store/types" ) // adapter holds MySQL connection data. type adapter struct { db *sqlx.DB dsn string dbName string // Maximum number of records to return maxResults int // Maximum number of message records to return maxMessageResults int version int // Single query timeout. sqlTimeout time.Duration // DB transaction timeout. txTimeout time.Duration } const ( adpVersion = 116 adapterName = "mysql" defaultDSN = "root:@tcp(localhost:3306)/tinode?parseTime=true" defaultDatabase = "tinode" defaultMaxResults = 1024 // This is capped by the Session's send queue limit (128). defaultMaxMessageResults = 100 // If DB request timeout is specified, // we allocate txTimeoutMultiplier times more time for transactions. txTimeoutMultiplier = 1.5 ) type configType struct { // DB connection settings. // See https://pkg.go.dev/github.com/go-sql-driver/mysql#Config // for the full list of fields. ms.Config // Deprecated. DSN string `json:"dsn,omitempty"` // Connection pool settings. // // Maximum number of open connections to the database. MaxOpenConns int `json:"max_open_conns,omitempty"` // Maximum number of connections in the idle connection pool. MaxIdleConns int `json:"max_idle_conns,omitempty"` // Maximum amount of time a connection may be reused (in seconds). ConnMaxLifetime int `json:"conn_max_lifetime,omitempty"` // DB request timeout (in seconds). // If 0 (or negative), no timeout is applied. SqlTimeout int `json:"sql_timeout,omitempty"` } func (a *adapter) getContext() (context.Context, context.CancelFunc) { if a.sqlTimeout > 0 { return context.WithTimeout(context.Background(), a.sqlTimeout) } return context.Background(), nil } func (a *adapter) getContextForTx() (context.Context, context.CancelFunc) { if a.txTimeout > 0 { return context.WithTimeout(context.Background(), a.txTimeout) } return context.Background(), nil } // Open initializes database session func (a *adapter) Open(jsonconfig json.RawMessage) error { if a.db != nil { return errors.New("mysql adapter is already connected") } if len(jsonconfig) < 2 { return errors.New("adapter mysql missing config") } var err error defaultCfg := ms.NewConfig() config := configType{Config: *defaultCfg} if err = json.Unmarshal(jsonconfig, &config); err != nil { return errors.New("mysql adapter failed to parse config: " + err.Error()) } if dsn := config.FormatDSN(); dsn != defaultCfg.FormatDSN() { // MySql config is specified. Use it. a.dbName = config.DBName a.dsn = dsn if config.DSN != "" { return errors.New("mysql config: conflicting config and DSN are provided") } } else { // Otherwise, use DSN to configure database connection. // Note: this method is deprecated. if config.DSN != "" { // Remove optional schema. a.dsn = strings.TrimPrefix(config.DSN, "mysql://") } else { a.dsn = defaultDSN } // Parse out the database name from the DSN. if cfg, err := ms.ParseDSN(a.dsn); err == nil { a.dbName = cfg.DBName } else { return err } } if a.dbName == "" { a.dbName = defaultDatabase } if a.maxResults <= 0 { a.maxResults = defaultMaxResults } if a.maxMessageResults <= 0 { a.maxMessageResults = defaultMaxMessageResults } // This just initializes the driver but does not open the network connection. a.db, err = sqlx.Open("mysql", a.dsn) if err != nil { return err } // Actually opening the network connection. err = a.db.Ping() if isMissingDb(err) { // Ignore missing database here. If we are initializing the database // missing DB is OK. err = nil } if err == nil { if config.MaxOpenConns > 0 { a.db.SetMaxOpenConns(config.MaxOpenConns) } if config.MaxIdleConns > 0 { a.db.SetMaxIdleConns(config.MaxIdleConns) } if config.ConnMaxLifetime > 0 { a.db.SetConnMaxLifetime(time.Duration(config.ConnMaxLifetime) * time.Second) } if config.SqlTimeout > 0 { a.sqlTimeout = time.Duration(config.SqlTimeout) * time.Second // We allocate txTimeoutMultiplier times sqlTimeout for transactions. a.txTimeout = time.Duration(float64(config.SqlTimeout)*txTimeoutMultiplier) * time.Second } } return err } // Close closes the underlying database connection func (a *adapter) Close() error { var err error if a.db != nil { err = a.db.Close() a.db = nil a.version = -1 } return err } // IsOpen returns true if connection to database has been established. It does not check if // connection is actually live. func (a *adapter) IsOpen() bool { return a.db != nil } // GetDbVersion returns current database version. func (a *adapter) GetDbVersion() (int, error) { if a.version > 0 { return a.version, nil } ctx, cancel := a.getContext() if cancel != nil { defer cancel() } var vers int err := a.db.GetContext(ctx, &vers, "SELECT `value` FROM kvmeta WHERE `key`='version'") if err != nil { if isMissingDb(err) || isMissingTable(err) || err == sql.ErrNoRows { err = errors.New("Database not initialized") } return -1, err } a.version = vers return vers, nil } func (a *adapter) updateDbVersion(v int) error { ctx, cancel := a.getContext() if cancel != nil { defer cancel() } a.version = -1 if _, err := a.db.ExecContext(ctx, "UPDATE kvmeta SET `value`=? WHERE `key`='version'", v); err != nil { return err } return nil } // CheckDbVersion checks whether the actual DB version matches the expected version of this adapter. func (a *adapter) CheckDbVersion() error { version, err := a.GetDbVersion() if err != nil { return err } if version != adpVersion { return errors.New("Invalid database version " + strconv.Itoa(version) + ". Expected " + strconv.Itoa(adpVersion)) } return nil } // Version returns adapter version. func (adapter) Version() int { return adpVersion } // DB connection stats object. func (a *adapter) Stats() any { if a.db == nil { return nil } return a.db.Stats() } // GetName returns string that adapter uses to register itself with store. func (a *adapter) GetName() string { return adapterName } // SetMaxResults configures how many results can be returned in a single DB call. func (a *adapter) SetMaxResults(val int) error { if val <= 0 { a.maxResults = defaultMaxResults } else { a.maxResults = val } return nil } // CreateDb initializes the storage. func (a *adapter) CreateDb(reset bool) error { var err error var tx *sql.Tx // Can't use an existing connection because it's configured with a database name which may not exist. // Don't care if it does not close cleanly. a.db.Close() // This DSN has been parsed before and produced no error, not checking for errors here. cfg, _ := ms.ParseDSN(a.dsn) // Clear database name cfg.DBName = "" a.db, err = sqlx.Open("mysql", cfg.FormatDSN()) if err != nil { return err } if tx, err = a.db.Begin(); err != nil { return err } defer func() { if err != nil { // FIXME: This is useless: MySQL auto-commits on every CREATE TABLE. // Maybe DROP DATABASE instead. tx.Rollback() } }() if reset { if _, err = tx.Exec("DROP DATABASE IF EXISTS " + a.dbName); err != nil { return err } } if _, err = tx.Exec("CREATE DATABASE " + a.dbName + " CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci"); err != nil { return err } if _, err = tx.Exec("USE " + a.dbName); err != nil { return err } if _, err = tx.Exec( `CREATE TABLE users( id BIGINT NOT NULL, createdat DATETIME(3) NOT NULL, updatedat DATETIME(3) NOT NULL, state SMALLINT NOT NULL DEFAULT 0, stateat DATETIME(3), access JSON, lastseen DATETIME, useragent VARCHAR(255) DEFAULT '', public JSON, trusted JSON, tags JSON, PRIMARY KEY(id), INDEX users_state_stateat(state, stateat), INDEX users_lastseen_updatedat(lastseen, updatedat) )`); err != nil { return err } // Indexed user tags. if _, err = tx.Exec( `CREATE TABLE usertags( id INT NOT NULL AUTO_INCREMENT, userid BIGINT NOT NULL, tag VARCHAR(96) NOT NULL, PRIMARY KEY(id), FOREIGN KEY(userid) REFERENCES users(id), INDEX usertags_tag(tag), UNIQUE INDEX usertags_userid_tag(userid, tag) )`); err != nil { return err } // Indexed devices. Normalized into a separate table. if _, err = tx.Exec( `CREATE TABLE devices( id INT NOT NULL AUTO_INCREMENT, userid BIGINT NOT NULL, hash CHAR(16) NOT NULL, deviceid TEXT NOT NULL, platform VARCHAR(32), lastseen DATETIME NOT NULL, lang VARCHAR(8), PRIMARY KEY(id), FOREIGN KEY(userid) REFERENCES users(id), UNIQUE INDEX devices_hash(hash) )`); err != nil { return err } // Authentication records for the basic authentication scheme. if _, err = tx.Exec( `CREATE TABLE auth( id INT NOT NULL AUTO_INCREMENT, uname VARCHAR(32) NOT NULL, userid BIGINT NOT NULL, scheme VARCHAR(16) NOT NULL, authlvl INT NOT NULL, secret VARCHAR(255) NOT NULL, expires DATETIME, PRIMARY KEY(id), FOREIGN KEY(userid) REFERENCES users(id), UNIQUE INDEX auth_userid_scheme(userid, scheme), UNIQUE INDEX auth_uname(uname) )`); err != nil { return err } // Topics if _, err = tx.Exec( `CREATE TABLE topics( id INT NOT NULL AUTO_INCREMENT, createdat DATETIME(3) NOT NULL, updatedat DATETIME(3) NOT NULL, state SMALLINT NOT NULL DEFAULT 0, stateat DATETIME(3), touchedat DATETIME(3), name CHAR(25) NOT NULL, usebt TINYINT DEFAULT 0, owner BIGINT NOT NULL DEFAULT 0, access JSON, seqid INT NOT NULL DEFAULT 0, delid INT DEFAULT 0, subcnt INT DEFAULT 0, public JSON, trusted JSON, tags JSON, aux JSON, PRIMARY KEY(id), UNIQUE INDEX topics_name(name), INDEX topics_owner(owner), INDEX topics_state_stateat(state, stateat), INDEX topics_name_state_seqid(name, state, seqid) )`); err != nil { return err } // Create system topic 'sys'. if err = createSystemTopic(tx); err != nil { return err } // Indexed topic tags. if _, err = tx.Exec( `CREATE TABLE topictags( id INT NOT NULL AUTO_INCREMENT, topic CHAR(25) NOT NULL, tag VARCHAR(96) NOT NULL, PRIMARY KEY(id), FOREIGN KEY(topic) REFERENCES topics(name), INDEX topictags_tag(tag), UNIQUE INDEX topictags_topic_tag(topic, tag) )`); err != nil { return err } // Subscriptions if _, err = tx.Exec( `CREATE TABLE subscriptions( id INT NOT NULL AUTO_INCREMENT, createdat DATETIME(3) NOT NULL, updatedat DATETIME(3) NOT NULL, deletedat DATETIME(3), userid BIGINT NOT NULL, topic CHAR(25) NOT NULL, delid INT DEFAULT 0, recvseqid INT DEFAULT 0, readseqid INT DEFAULT 0, modewant CHAR(8), modegiven CHAR(8), private JSON, PRIMARY KEY(id), FOREIGN KEY(userid) REFERENCES users(id), UNIQUE INDEX subscriptions_topic_userid(topic, userid), INDEX subscriptions_topic(topic), INDEX subscriptions_deletedat(deletedat), INDEX subscriptions_userid_topic_deletedat(userid, topic, deletedat) )`); err != nil { return err } // Messages if _, err = tx.Exec( `CREATE TABLE messages( id INT NOT NULL AUTO_INCREMENT, createdat DATETIME(3) NOT NULL, updatedat DATETIME(3) NOT NULL, deletedat DATETIME(3), delid INT DEFAULT 0, seqid INT NOT NULL, topic CHAR(25) NOT NULL,` + "`from` BIGINT NOT NULL," + `head JSON, content JSON, PRIMARY KEY(id), FOREIGN KEY(topic) REFERENCES topics(name), UNIQUE INDEX messages_topic_seqid(topic, seqid) )`); err != nil { return err } // Deletion log if _, err = tx.Exec( `CREATE TABLE dellog( id INT NOT NULL AUTO_INCREMENT, topic CHAR(25) NOT NULL, deletedfor BIGINT NOT NULL DEFAULT 0, delid INT NOT NULL, low INT NOT NULL, hi INT NOT NULL, PRIMARY KEY(id), FOREIGN KEY(topic) REFERENCES topics(name), INDEX dellog_topic_delid_deletedfor(topic,delid,deletedfor), INDEX dellog_topic_deletedfor_low_hi(topic,deletedfor,low,hi), INDEX dellog_deletedfor(deletedfor) )`); err != nil { return err } // User credentials if _, err = tx.Exec( `CREATE TABLE credentials( id INT NOT NULL AUTO_INCREMENT, createdat DATETIME(3) NOT NULL, updatedat DATETIME(3) NOT NULL, deletedat DATETIME(3), method VARCHAR(16) NOT NULL, value VARCHAR(128) NOT NULL, synthetic VARCHAR(192) NOT NULL, userid BIGINT NOT NULL, resp VARCHAR(255), done TINYINT NOT NULL DEFAULT 0, retries INT NOT NULL DEFAULT 0, PRIMARY KEY(id), FOREIGN KEY(userid) REFERENCES users(id), UNIQUE credentials_uniqueness(synthetic) )`); err != nil { return err } // Records of uploaded files. // Don't add FOREIGN KEY on userid. It's not needed and it will break user deletion. if _, err = tx.Exec( `CREATE TABLE fileuploads( id BIGINT NOT NULL, createdat DATETIME(3) NOT NULL, updatedat DATETIME(3) NOT NULL, userid BIGINT, status INT NOT NULL, mimetype VARCHAR(255) NOT NULL, size BIGINT NOT NULL, etag VARCHAR(128), location VARCHAR(2048) NOT NULL, PRIMARY KEY(id), INDEX fileuploads_status(status) )`); err != nil { return err } // Links between uploaded files and the topics, users or messages they are attached to. if _, err = tx.Exec( `CREATE TABLE filemsglinks( id INT NOT NULL AUTO_INCREMENT, createdat DATETIME(3) NOT NULL, fileid BIGINT NOT NULL, msgid INT, topic CHAR(25), userid BIGINT, PRIMARY KEY(id), FOREIGN KEY(fileid) REFERENCES fileuploads(id) ON DELETE CASCADE, FOREIGN KEY(msgid) REFERENCES messages(id) ON DELETE CASCADE, FOREIGN KEY(topic) REFERENCES topics(name) ON DELETE CASCADE, FOREIGN KEY(userid) REFERENCES users(id) ON DELETE CASCADE )`); err != nil { return err } if _, err = tx.Exec( `CREATE TABLE kvmeta(` + "`key` VARCHAR(64) NOT NULL," + "createdat DATETIME(3)," + "`value` TEXT," + "PRIMARY KEY(`key`)," + "INDEX kvmeta_createdat_key(createdat, `key`)" + `)`); err != nil { return err } if _, err = tx.Exec("INSERT INTO kvmeta(`key`, `value`) VALUES('version',?)", adpVersion); err != nil { return err } return tx.Commit() } // UpgradeDb upgrades the database, if necessary. func (a *adapter) UpgradeDb() error { bumpVersion := func(a *adapter, x int) error { if err := a.updateDbVersion(x); err != nil { return err } _, err := a.GetDbVersion() return err } if _, err := a.GetDbVersion(); err != nil { return err } if a.version == 106 { // Perform database upgrade from version 106 to version 107. if _, err := a.db.Exec("CREATE UNIQUE INDEX usertags_userid_tag ON usertags(userid, tag)"); err != nil { return err } if _, err := a.db.Exec("CREATE UNIQUE INDEX topictags_topic_tag ON topictags(topic, tag)"); err != nil { return err } if _, err := a.db.Exec("ALTER TABLE credentials ADD deletedat DATETIME(3) AFTER updatedat"); err != nil { return err } if err := bumpVersion(a, 107); err != nil { return err } } if a.version == 107 { // Perform database upgrade from version 107 to version 108. // Replace default user access JRWPA with JRWPAS. if _, err := a.db.Exec(`UPDATE users SET access=JSON_REPLACE(access, '$.Auth', 'JRWPAS') WHERE CAST(JSON_EXTRACT(access, '$.Auth') AS CHAR) LIKE '"JRWPA"'`); err != nil { return err } if err := bumpVersion(a, 108); err != nil { return err } } if a.version == 108 { // Perform database upgrade from version 108 to version 109. tx, err := a.db.Begin() if err != nil { return err } if err = createSystemTopic(tx); err != nil { tx.Rollback() return err } if err = tx.Commit(); err != nil { return err } if err = bumpVersion(a, 109); err != nil { return err } } if a.version == 109 { // Perform database upgrade from version 109 to version 110. if _, err := a.db.Exec("UPDATE topics SET touchedat=updatedat WHERE touchedat IS NULL"); err != nil { return err } if err := bumpVersion(a, 110); err != nil { return err } } if a.version == 110 { // Users if _, err := a.db.Exec("ALTER TABLE users MODIFY state SMALLINT NOT NULL DEFAULT 0 AFTER updatedat"); err != nil { return err } if _, err := a.db.Exec("ALTER TABLE users CHANGE deletedat stateat DATETIME(3)"); err != nil { return err } if _, err := a.db.Exec("ALTER TABLE users DROP INDEX users_deletedat"); err != nil { return err } // Add status to formerly soft-deleted users. if _, err := a.db.Exec("UPDATE users SET state=? WHERE stateat IS NOT NULL", t.StateDeleted); err != nil { return err } if _, err := a.db.Exec("ALTER TABLE users ADD INDEX users_state(state)"); err != nil { return err } // Topics if _, err := a.db.Exec("ALTER TABLE topics ADD state SMALLINT NOT NULL DEFAULT 0 AFTER updatedat"); err != nil { return err } if _, err := a.db.Exec("ALTER TABLE topics CHANGE deletedat stateat DATETIME(3)"); err != nil { return err } // Add status to formerly soft-deleted topics. if _, err := a.db.Exec("UPDATE topics SET state=? WHERE stateat IS NOT NULL", t.StateDeleted); err != nil { return err } if _, err := a.db.Exec("ALTER TABLE topics ADD INDEX topics_state(state)"); err != nil { return err } // Subscriptions if _, err := a.db.Exec("ALTER TABLE subscriptions ADD INDEX topics_deletedat(deletedat)"); err != nil { return err } if err := bumpVersion(a, 111); err != nil { return err } } if a.version == 111 { // Perform database upgrade from version 111 to version 112. if _, err := a.db.Exec("ALTER TABLE users ADD trusted JSON AFTER public"); err != nil { return err } if _, err := a.db.Exec("ALTER TABLE topics ADD trusted JSON AFTER public"); err != nil { return err } // Remove NOT NULL constraint, so an avatar upload can be done at registration. if _, err := a.db.Exec("ALTER TABLE fileuploads MODIFY userid BIGINT"); err != nil { return err } if _, err := a.db.Exec("ALTER TABLE fileuploads ADD INDEX fileuploads_status(status)"); err != nil { return err } // Remove NOT NULL constraint to enable links to users and topics. if _, err := a.db.Exec("ALTER TABLE filemsglinks MODIFY msgid INT"); err != nil { return err } if _, err := a.db.Exec("ALTER TABLE filemsglinks ADD topic CHAR(25)"); err != nil { return err } if _, err := a.db.Exec("ALTER TABLE filemsglinks ADD userid BIGINT"); err != nil { return err } if _, err := a.db.Exec("ALTER TABLE filemsglinks ADD FOREIGN KEY(topic) REFERENCES topics(name) ON DELETE CASCADE"); err != nil { return err } if _, err := a.db.Exec("ALTER TABLE filemsglinks ADD FOREIGN KEY(userid) REFERENCES users(id) ON DELETE CASCADE"); err != nil { return err } if err := bumpVersion(a, 112); err != nil { return err } } if a.version == 112 { // Perform database upgrade from version 112 to version 113. // Index for deleting unvalidated accounts. if _, err := a.db.Exec("ALTER TABLE users ADD INDEX users_lastseen_updatedat(lastseen,updatedat)"); err != nil { return err } // Add timestamp to kvmeta. if _, err := a.db.Exec("ALTER TABLE kvmeta MODIFY `key` VARCHAR(64) NOT NULL"); err != nil { return err } // Add timestamp to kvmeta. if _, err := a.db.Exec("ALTER TABLE kvmeta ADD createdat DATETIME(3) AFTER `key`"); err != nil { return err } // Add compound index on the new field and key (could be searched by key prefix). if _, err := a.db.Exec("ALTER TABLE kvmeta ADD INDEX kvmeta_createdat_key(createdat, `key`)"); err != nil { return err } if err := bumpVersion(a, 113); err != nil { return err } } if a.version == 113 { // Perform database upgrade from version 113 to version 114. if _, err := a.db.Exec("ALTER TABLE topics ADD aux JSON"); err != nil { return err } if _, err := a.db.Exec("ALTER TABLE fileuploads ADD etag VARCHAR(128) AFTER size"); err != nil { return err } if err := bumpVersion(a, 114); err != nil { return err } } if a.version == 114 { // Perform database upgrade from version 114 to version 115. // Find relevant subscriptions for given users efficiently, and use the join key too. if _, err := a.db.Exec("CREATE INDEX idx_subs_user_topic_del ON subscriptions(userid, topic, deletedat)"); err != nil { return err } // Optimizes join; state filters; seqid supports the SUM operation. if _, err := a.db.Exec("CREATE INDEX idx_topics_name_state_seqid ON topics(name, state, seqid)"); err != nil { return err } if err := bumpVersion(a, 115); err != nil { return err } } if a.version == 115 { // Perform database upgrade from version 115 to version 116. // Add subscriber count column to the topics table. if _, err := a.db.Exec("ALTER TABLE topics ADD COLUMN subcnt INT DEFAULT 0 AFTER delid"); err != nil { return err } if err := bumpVersion(a, 116); err != nil { return err } } if a.version != adpVersion { return errors.New("Failed to perform database upgrade to version " + strconv.Itoa(adpVersion) + ". DB is still at " + strconv.Itoa(a.version)) } return nil } // Create system topic 'sys'. func createSystemTopic(tx *sql.Tx) error { now := t.TimeNow() query := `INSERT INTO topics(createdat,updatedat,state,touchedat,name,access,public) VALUES(?,?,?,?,'sys','{"Auth": "N","Anon": "N"}','{"fn": "System"}')` _, err := tx.Exec(query, now, now, t.StateOK, now) return err } func addTags(tx *sqlx.Tx, table, keyName string, keyVal any, tags []string, ignoreDups bool) error { if len(tags) == 0 { return nil } insert, err := tx.Prepare("INSERT INTO " + table + "(" + keyName + ",tag) VALUES(?,?)") if err != nil { return err } for _, tag := range tags { if _, err = insert.Exec(keyVal, tag); err != nil { if isDupe(err) { if ignoreDups { err = nil continue } return t.ErrDuplicate } return err } } return nil } func removeTags(tx *sqlx.Tx, table, keyName string, keyVal any, tags []string) error { if len(tags) == 0 { return nil } var args []any for _, tag := range tags { args = append(args, tag) } query, args, _ := sqlx.In("DELETE FROM "+table+" WHERE "+keyName+"=? AND tag IN (?)", keyVal, args) _, err := tx.Exec(tx.Rebind(query), args...) return err } // UserCreate creates a new user. Returns error and true if error is due to duplicate user name, // false for any other error func (a *adapter) UserCreate(user *t.User) error { ctx, cancel := a.getContextForTx() if cancel != nil { defer cancel() } tx, err := a.db.BeginTxx(ctx, nil) if err != nil { return err } defer func() { if err != nil { tx.Rollback() } }() decoded_uid := store.DecodeUid(user.Uid()) if _, err = tx.Exec("INSERT INTO users(id,createdat,updatedat,state,access,public,trusted,tags) VALUES(?,?,?,?,?,?,?,?)", decoded_uid, user.CreatedAt, user.UpdatedAt, user.State, user.Access, common.ToJSON(user.Public), common.ToJSON(user.Trusted), user.Tags); err != nil { return err } // Save user's tags to a separate table to make user findable. if err = addTags(tx, "usertags", "userid", decoded_uid, user.Tags, false); err != nil { return err } return tx.Commit() } // Add user's authentication record func (a *adapter) AuthAddRecord(uid t.Uid, scheme, unique string, authLvl auth.Level, secret []byte, expires time.Time) error { var exp *time.Time if !expires.IsZero() { exp = &expires } ctx, cancel := a.getContext() if cancel != nil { defer cancel() } if _, err := a.db.ExecContext(ctx, "INSERT INTO auth(uname,userid,scheme,authLvl,secret,expires) VALUES(?,?,?,?,?,?)", unique, store.DecodeUid(uid), scheme, authLvl, secret, exp); err != nil { if isDupe(err) { return t.ErrDuplicate } return err } return nil } // AuthDelScheme deletes an existing authentication scheme for the user. func (a *adapter) AuthDelScheme(user t.Uid, scheme string) error { ctx, cancel := a.getContext() if cancel != nil { defer cancel() } _, err := a.db.ExecContext(ctx, "DELETE FROM auth WHERE userid=? AND scheme=?", store.DecodeUid(user), scheme) return err } // AuthDelAllRecords deletes all authentication records for the user. func (a *adapter) AuthDelAllRecords(user t.Uid) (int, error) { ctx, cancel := a.getContext() if cancel != nil { defer cancel() } res, err := a.db.ExecContext(ctx, "DELETE FROM auth WHERE userid=?", store.DecodeUid(user)) if err != nil { return 0, err } count, _ := res.RowsAffected() return int(count), nil } // Update user's authentication unique, secret, auth level. func (a *adapter) AuthUpdRecord(uid t.Uid, scheme, unique string, authLvl auth.Level, secret []byte, expires time.Time) error { params := []string{"authLvl=?"} args := []any{authLvl} if unique != "" { params = append(params, "uname=?") args = append(args, unique) } if len(secret) > 0 { params = append(params, "secret=?") args = append(args, secret) } if !expires.IsZero() { params = append(params, "expires=?") args = append(args, expires) } args = append(args, store.DecodeUid(uid), scheme) ctx, cancel := a.getContext() if cancel != nil { defer cancel() } sql := "UPDATE auth SET " + strings.Join(params, ",") + " WHERE userid=? AND scheme=?" resp, err := a.db.ExecContext(ctx, sql, args...) if isDupe(err) { return t.ErrDuplicate } if count, _ := resp.RowsAffected(); count <= 0 { return t.ErrNotFound } return err } // Retrieve user's authentication record func (a *adapter) AuthGetRecord(uid t.Uid, scheme string) (string, auth.Level, []byte, time.Time, error) { var expires time.Time var record struct { Uname string Authlvl auth.Level Secret []byte Expires *time.Time } ctx, cancel := a.getContext() if cancel != nil { defer cancel() } if err := a.db.GetContext(ctx, &record, "SELECT uname,secret,expires,authlvl FROM auth WHERE userid=? AND scheme=?", store.DecodeUid(uid), scheme); err != nil { if err == sql.ErrNoRows { // Nothing found - use standard error. err = t.ErrNotFound } return "", 0, nil, expires, err } if record.Expires != nil { expires = *record.Expires } return record.Uname, record.Authlvl, record.Secret, expires, nil } // Retrieve user's authentication record func (a *adapter) AuthGetUniqueRecord(unique string) (t.Uid, auth.Level, []byte, time.Time, error) { var expires time.Time var record struct { Userid int64 Authlvl auth.Level Secret []byte Expires *time.Time } ctx, cancel := a.getContext() if cancel != nil { defer cancel() } if err := a.db.GetContext(ctx, &record, "SELECT userid,secret,expires,authlvl FROM auth WHERE uname=?", unique); err != nil { if err == sql.ErrNoRows { // Nothing found - clear the error err = nil } return t.ZeroUid, 0, nil, expires, err } if record.Expires != nil { expires = *record.Expires } return store.EncodeUid(record.Userid), record.Authlvl, record.Secret, expires, nil } // UserGet fetches a single user by user id. If user is not found it returns (nil, nil) func (a *adapter) UserGet(uid t.Uid) (*t.User, error) { ctx, cancel := a.getContext() if cancel != nil { defer cancel() } var user t.User err := a.db.GetContext(ctx, &user, "SELECT * FROM users WHERE id=? AND state!=?", store.DecodeUid(uid), t.StateDeleted) if err == nil { user.SetUid(uid) user.Public = common.FromJSON(user.Public) user.Trusted = common.FromJSON(user.Trusted) return &user, nil } if err == sql.ErrNoRows { // Clear the error if user does not exist or marked as soft-deleted. return nil, nil } return nil, err } func (a *adapter) UserGetAll(ids ...t.Uid) ([]t.User, error) { uids := make([]any, len(ids)) for i, id := range ids { if id.IsZero() { continue } uids[i] = store.DecodeUid(id) } users := []t.User{} ctx, cancel := a.getContext() if cancel != nil { defer cancel() } q, uids, _ := sqlx.In("SELECT * FROM users WHERE id IN (?) AND state!=?", uids, t.StateDeleted) rows, err := a.db.QueryxContext(ctx, a.db.Rebind(q), uids...) if err != nil { return nil, err } defer rows.Close() for rows.Next() { var user t.User if err = rows.StructScan(&user); err != nil { users = nil break } user.SetUid(common.EncodeUidString(user.Id)) user.Public = common.FromJSON(user.Public) user.Trusted = common.FromJSON(user.Trusted) users = append(users, user) } if err == nil { err = rows.Err() } return users, err } // UserDelete deletes specified user: wipes completely (hard-delete) or marks as deleted. // TODO: report when the user is not found. func (a *adapter) UserDelete(uid t.Uid, hard bool) error { query := "SELECT name FROM topics WHERE owner=?" args := []any{store.DecodeUid(uid)} // In case of hard delete, delete all topics, even those which were // soft-deleted previsously. if !hard { query += " AND state!=?" args = append(args, t.StateDeleted) } // Get a list of topic names owned by the user (as 'grp' and 'chn'). ownTopics, err := a.topicNamesForUser(query, true, args...) if err != nil { return err } ctx, cancel := a.getContextForTx() if cancel != nil { defer cancel() } tx, err := a.db.BeginTxx(ctx, nil) if err != nil { return err } defer func() { if err != nil { tx.Rollback() } }() now := t.TimeNow() decoded_uid := store.DecodeUid(uid) if hard { // Delete user's devices // t.ErrNotFound = user has no devices. if err = deviceDelete(tx, uid, ""); err != nil && err != t.ErrNotFound { return err } // Delete user's subscriptions in all topics. if err = subsDelForUser(tx, decoded_uid, true); err != nil { return err } // Delete records of messages soft-deleted for the user in all topics. if _, err = tx.Exec("DELETE FROM dellog WHERE deletedfor=?", decoded_uid); err != nil { return err } // Can't delete user's messages in all topics because we cannot notify topics of such deletion. // Just leave the messages there marked as sent by "not found" user. // Delete topics where the user is the owner. if len(ownTopics) > 0 { // First delete all messages in those topics. if _, err = tx.Exec("DELETE dellog FROM dellog JOIN topics ON topics.name=dellog.topic WHERE topics.owner=?", decoded_uid); err != nil { return err } // Deletion of messages will cascade to filemsglinks and so to fileuploads. if _, err = tx.Exec("DELETE messages FROM messages JOIN topics ON topics.name=messages.topic WHERE topics.owner=?", decoded_uid); err != nil { return err } // Delete subscriptions for all users where the user is the owner of the topic. sql, args, _ := sqlx.In("DELETE FROM subscriptions AS s WHERE topic IN (?)", ownTopics) if _, err = tx.Exec(tx.Rebind(sql), args); err != nil { return err } // Delete topic tags. if _, err = tx.Exec("DELETE tt FROM topictags AS tt JOIN topics AS t ON t.name=tt.topic WHERE t.owner=?", decoded_uid); err != nil { return err } // And finally delete the topics. if _, err = tx.Exec("DELETE FROM topics WHERE owner=?", decoded_uid); err != nil { return err } } // Delete user's authentication records. if _, err = tx.Exec("DELETE FROM auth WHERE userid=?", decoded_uid); err != nil { return err } // Delete all credentials. if err = credDel(tx, uid, "", ""); err != nil && err != t.ErrNotFound { return err } if _, err = tx.Exec("DELETE FROM usertags WHERE userid=?", decoded_uid); err != nil { return err } if _, err = tx.Exec("DELETE FROM users WHERE id=?", decoded_uid); err != nil { return err } } else { // Disable all user's subscriptions. That includes p2p subscriptions. No need to delete them. if err = subsDelForUser(tx, decoded_uid, false); err != nil { return err } if len(ownTopics) > 0 { // Disable all subscriptions to topics where the user is the owner. sql, args, _ := sqlx.In("UPDATE subscriptions SET updatedat=?,deletedat=? WHERE topic IN (?)", now, now, ownTopics) if _, err = tx.Exec(tx.Rebind(sql), args...); err != nil { return err } } // Disable group topics where the user is the owner. if _, err = tx.Exec("UPDATE topics SET updatedat=?,touchedat=?,state=?,stateat=? WHERE owner=?", now, now, t.StateDeleted, now, decoded_uid); err != nil { return err } // Disable p2p topics with the user (p2p topic's owner is 0). if _, err = tx.Exec("UPDATE topics AS t JOIN subscriptions AS s ON t.name=s.topic "+ "SET t.updatedat=?,t.touchedat=?,t.state=?,t.stateat=? "+ "WHERE t.owner=0 AND s.userid=? AND t.name LIKE 'p2p%'", now, now, t.StateDeleted, now, decoded_uid); err != nil { return err } // Disable the other user's subscription to a disabled p2p topic. if _, err = tx.Exec("UPDATE subscriptions AS s_one JOIN subscriptions AS s_two "+ "ON s_one.topic=s_two.topic "+ "SET s_two.updatedat=?, s_two.deletedat=? WHERE s_one.userid=? AND s_one.topic LIKE 'p2p%'", now, now, decoded_uid); err != nil { return err } // Finally disable user. if _, err = tx.Exec("UPDATE users SET updatedat=?,state=?,stateat=? WHERE id=?", now, t.StateDeleted, now, decoded_uid); err != nil { return err } } return tx.Commit() } // topicStateForUser is called by UserUpdate when the update contains state change. // Soft-deleted topics remain soft-deleted. func (a *adapter) topicStateForUser(tx *sqlx.Tx, decoded_uid int64, now time.Time, update any) error { var err error state, ok := update.(t.ObjState) if !ok { return t.ErrMalformed } if now.IsZero() { now = t.TimeNow() } // Change state of all topics where the user is the owner. if _, err = tx.Exec("UPDATE topics SET state=?, stateat=? WHERE owner=? AND state!=?", state, now, decoded_uid, t.StateDeleted); err != nil { return err } // Change state of p2p topics with the user (p2p topic's owner is 0) if _, err = tx.Exec("UPDATE topics JOIN subscriptions ON topics.name=subscriptions.topic "+ "SET topics.state=?, topics.stateat=? WHERE topics.owner=0 AND subscriptions.userid=? AND topics.state!=?", state, now, decoded_uid, t.StateDeleted); err != nil { return err } // Subscriptions don't need to be updated: // subscriptions of a disabled user are not disabled and still can be manipulated. return nil } // UserUpdate updates user object. func (a *adapter) UserUpdate(uid t.Uid, update map[string]any) error { ctx, cancel := a.getContextForTx() if cancel != nil { defer cancel() } tx, err := a.db.BeginTxx(ctx, nil) if err != nil { return err } defer func() { if err != nil { tx.Rollback() } }() cols, args := common.UpdateByMap(update) decoded_uid := store.DecodeUid(uid) args = append(args, decoded_uid) _, err = tx.Exec("UPDATE users SET "+strings.Join(cols, ",")+" WHERE id=?", args...) if err != nil { return err } if state, ok := update["State"]; ok { now, _ := update["StateAt"].(time.Time) err = a.topicStateForUser(tx, decoded_uid, now, state) if err != nil { return err } } // Tags are also stored in a separate table if tags := common.ExtractTags(update); tags != nil { // First delete all user tags _, err = tx.Exec("DELETE FROM usertags WHERE userid=?", decoded_uid) if err != nil { return err } // Now insert new tags err = addTags(tx, "usertags", "userid", decoded_uid, tags, false) if err != nil { return err } } return tx.Commit() } // UserUpdateTags adds, removes, or resets user's tags. func (a *adapter) UserUpdateTags(uid t.Uid, add, remove, reset []string) ([]string, error) { ctx, cancel := a.getContextForTx() if cancel != nil { defer cancel() } tx, err := a.db.BeginTxx(ctx, nil) if err != nil { return nil, err } defer func() { if err != nil { tx.Rollback() } }() decoded_uid := store.DecodeUid(uid) if reset != nil { // Delete all tags first if resetting. _, err = tx.Exec("DELETE FROM usertags WHERE userid=?", decoded_uid) if err != nil { return nil, err } add = reset remove = nil } // Now insert new tags. Ignore duplicates if resetting. err = addTags(tx, "usertags", "userid", decoded_uid, add, reset == nil) if err != nil { return nil, err } // Delete tags. err = removeTags(tx, "usertags", "userid", decoded_uid, remove) if err != nil { return nil, err } var allTags []string err = tx.Select(&allTags, "SELECT tag FROM usertags WHERE userid=?", decoded_uid) if err != nil { return nil, err } _, err = tx.Exec("UPDATE users SET tags=? WHERE id=?", t.StringSlice(allTags), decoded_uid) if err != nil { return nil, err } return allTags, tx.Commit() } // UserGetByCred returns user ID for the given validated credential. func (a *adapter) UserGetByCred(method, value string) (t.Uid, error) { ctx, cancel := a.getContext() if cancel != nil { defer cancel() } var decoded_uid int64 err := a.db.GetContext(ctx, &decoded_uid, "SELECT userid FROM credentials WHERE synthetic=?", method+":"+value) if err == nil { return store.EncodeUid(decoded_uid), nil } if err == sql.ErrNoRows { // Clear the error if user does not exist return t.ZeroUid, nil } return t.ZeroUid, err } // UserUnreadCount returns the total number of unread messages in all topics with // the R permission. If read fails, the counts are still returned with the original // user IDs but with the unread count undefined and non-nil error. // UserUnreadCount does not count unread messages in channels although it should. func (a *adapter) UserUnreadCount(ids ...t.Uid) (map[t.Uid]int, error) { uids := make([]any, len(ids)) counts := make(map[t.Uid]int, len(ids)) for i, id := range ids { uids[i] = store.DecodeUid(id) // Ensure all original uids are always present. counts[id] = 0 } ctx, cancel := a.getContext() if cancel != nil { defer cancel() } // FIXME: support channels (for channels subscriptions.topic != topics.name). q, args, _ := sqlx.In("SELECT s.userid, SUM(t.seqid)-SUM(s.readseqid) AS unreadcount FROM topics AS t, subscriptions AS s "+ "WHERE s.userid IN (?) AND t.name=s.topic AND s.deletedat IS NULL AND t.state!=? AND "+ "INSTR(s.modewant, 'R')>0 AND INSTR(s.modegiven, 'R')>0 GROUP BY s.userid", uids, int(t.StateDeleted)) rows, err := a.db.QueryxContext(ctx, a.db.Rebind(q), args...) if err != nil { return counts, err } defer rows.Close() var userId int64 var unreadCount int for rows.Next() { if err = rows.Scan(&userId, &unreadCount); err != nil { break } counts[store.EncodeUid(userId)] = unreadCount } if err == nil { err = rows.Err() } return counts, err } // UserGetUnvalidated returns a list of uids which have never logged in, have no // validated credentials and haven't been updated since lastUpdatedBefore. func (a *adapter) UserGetUnvalidated(lastUpdatedBefore time.Time, limit int) ([]t.Uid, error) { var uids []t.Uid ctx, cancel := a.getContext() if cancel != nil { defer cancel() } rows, err := a.db.QueryxContext(ctx, "SELECT u.id, IFNULL(SUM(c.done),0) AS total FROM users AS u "+ "LEFT JOIN credentials AS c ON u.id=c.userid WHERE u.lastseen IS NULL AND u.updatedat 0 && opts.Limit < a.maxResults { limit = opts.Limit } else { limit = a.maxResults } } else { ims = *opts.IfModifiedSince } } else { limit = a.maxResults } if limit > 0 { q += " LIMIT ?" args = append(args, limit) } ctx, cancel := a.getContext() if cancel != nil { defer cancel() } rows, err := a.db.QueryxContext(ctx, q, args...) if err != nil { return nil, err } // Must close rows manually as we will be reusing it. // Fetch subscriptions. Two queries are needed: users table (p2p) and topics table (grp). // Prepare a list of separate subscriptions to users vs topics join := make(map[string]t.Subscription) // Keeping these to make a join with table for .private and .access topq := make([]any, 0, 16) usrq := make([]any, 0, 16) for rows.Next() { var sub t.Subscription if err = rows.StructScan(&sub); err != nil { break } tname := sub.Topic sub.User = uid.String() tcat := t.GetTopicCat(tname) if tcat == t.TopicCatMe || tcat == t.TopicCatFnd { // One of 'me', 'fnd' subscriptions, skip. // Don't skip 'sys' subscription. continue } else if tcat == t.TopicCatP2P { // P2P subscription, find the other user to get user.Public and user.Trusted. uid1, uid2, _ := t.ParseP2P(tname) if uid1 == uid { usrq = append(usrq, store.DecodeUid(uid2)) sub.SetWith(uid2.UserId()) } else { usrq = append(usrq, store.DecodeUid(uid1)) sub.SetWith(uid1.UserId()) } } else if tcat == t.TopicCatGrp { // Maybe convert channel name to group topic name. tname = t.ChnToGrp(tname) } // No special handling needed for 'slf', 'sys' subscriptions. topq = append(topq, tname) sub.Private = common.FromJSON(sub.Private) join[tname] = sub } if err == nil { err = rows.Err() } rows.Close() if err != nil { return nil, err } var subs []t.Subscription if len(join) == 0 { return subs, nil } // Fetch grp topics and join to subscriptions. if len(topq) > 0 { q = "SELECT updatedat,state,touchedat,name AS id,usebt,access,seqid,delid,subcnt,public,trusted " + "FROM topics WHERE name IN (?)" q, args, _ = sqlx.In(q, topq) if !keepDeleted { // Optionally skip deleted topics. q += " AND state!=?" args = append(args, t.StateDeleted) } if !ims.IsZero() { // Use cache timestamp if provided: get newer entries only. q += " AND touchedat>?" args = append(args, ims) if limit > 0 && limit < len(topq) { // No point in fetching more than the requested limit. q += " ORDER BY touchedat LIMIT ?" args = append(args, limit) } } ctx2, cancel2 := a.getContext() if cancel2 != nil { defer cancel2() } rows, err = a.db.QueryxContext(ctx2, a.db.Rebind(q), args...) if err != nil { return nil, err } var top t.Topic for rows.Next() { if err = rows.StructScan(&top); err != nil { break } sub := join[top.Id] // Check if sub.UpdatedAt needs to be adjusted to earlier or later time. sub.UpdatedAt = common.SelectLatestTime(sub.UpdatedAt, top.UpdatedAt) sub.SetState(top.State) sub.SetTouchedAt(top.TouchedAt) sub.SetSeqId(top.SeqId) if t.GetTopicCat(sub.Topic) == t.TopicCatGrp { sub.SetSubCnt(top.SubCnt) sub.SetPublic(common.FromJSON(top.Public)) sub.SetTrusted(common.FromJSON(top.Trusted)) } // Put back the updated value of a subsription, will process further below join[top.Id] = sub } if err == nil { err = rows.Err() } rows.Close() if err != nil { return nil, err } } // Fetch p2p users and join to p2p subscriptions. if len(usrq) > 0 { q = "SELECT id,updatedat,state,access,lastseen,useragent,public,trusted " + "FROM users WHERE id IN (?)" q, args, _ = sqlx.In(q, usrq) if !keepDeleted { // Optionally skip deleted users. q += " AND state!=?" args = append(args, t.StateDeleted) } // Ignoring ims: we need all users to get LastSeen and UserAgent. ctx3, cancel3 := a.getContext() if cancel3 != nil { defer cancel3() } rows, err = a.db.QueryxContext(ctx3, a.db.Rebind(q), args...) if err != nil { return nil, err } for rows.Next() { var usr2 t.User if err = rows.StructScan(&usr2); err != nil { break } joinOn := uid.P2PName(common.EncodeUidString(usr2.Id)) if sub, ok := join[joinOn]; ok { sub.UpdatedAt = common.SelectLatestTime(sub.UpdatedAt, usr2.UpdatedAt) sub.SetState(usr2.State) sub.SetPublic(common.FromJSON(usr2.Public)) sub.SetTrusted(common.FromJSON(usr2.Trusted)) sub.SetDefaultAccess(usr2.Access.Auth, usr2.Access.Anon) sub.SetLastSeenAndUA(usr2.LastSeen, usr2.UserAgent) join[joinOn] = sub } } if err == nil { err = rows.Err() } rows.Close() if err != nil { return nil, err } } subs = make([]t.Subscription, 0, len(join)) for _, sub := range join { subs = append(subs, sub) } return common.SelectEarliestUpdatedSubs(subs, opts, a.maxResults), nil } // UsersForTopic loads users subscribed to the given topic (not channel readers). // The difference between UsersForTopic vs SubsForTopic is that the former loads user.Public, // the latter does not. func (a *adapter) UsersForTopic(topic string, keepDeleted bool, opts *t.QueryOpt) ([]t.Subscription, error) { tcat := t.GetTopicCat(topic) // Fetch all subscribed users. The number of users is not large. q := `SELECT s.createdat,s.updatedat,s.deletedat,s.userid,s.topic,s.delid,s.recvseqid, s.readseqid,s.modewant,s.modegiven,u.public,u.trusted,u.lastseen,u.useragent,s.private FROM subscriptions AS s JOIN users AS u ON s.userid=u.id WHERE s.topic=?` args := []any{topic} if !keepDeleted { // Filter out rows with users deleted q += " AND u.state!=?" args = append(args, t.StateDeleted) // For p2p topics we must load all subscriptions including deleted. // Otherwise it will be impossible to swipe Public values. if tcat != t.TopicCatP2P { // Filter out deleted subscriptions. q += " AND s.deletedat IS NULL" } } limit := a.maxResults var oneUser t.Uid if opts != nil { // Ignore IfModifiedSince: loading all entries because a topic cannot have too many subscribers. // Those unmodified will be stripped of Public & Private. if !opts.User.IsZero() { // For p2p topics we have to fetch both users otherwise public cannot be swapped. if tcat != t.TopicCatP2P { q += " AND s.userid=?" args = append(args, store.DecodeUid(opts.User)) } oneUser = opts.User } if opts.Limit > 0 && opts.Limit < limit { limit = opts.Limit } } q += " LIMIT ?" args = append(args, limit) ctx, cancel := a.getContext() if cancel != nil { defer cancel() } rows, err := a.db.QueryxContext(ctx, q, args...) if err != nil { return nil, err } defer rows.Close() // Fetch subscriptions. var sub t.Subscription var subs []t.Subscription var lastSeen sql.NullTime var userAgent string var public, trusted any for rows.Next() { if err = rows.Scan( &sub.CreatedAt, &sub.UpdatedAt, &sub.DeletedAt, &sub.User, &sub.Topic, &sub.DelId, &sub.RecvSeqId, &sub.ReadSeqId, &sub.ModeWant, &sub.ModeGiven, &public, &trusted, &lastSeen, &userAgent, &sub.Private); err != nil { break } sub.User = common.EncodeUidString(sub.User).String() sub.Private = common.FromJSON(sub.Private) sub.SetPublic(common.FromJSON(public)) sub.SetTrusted(common.FromJSON(trusted)) sub.SetLastSeenAndUA(&lastSeen.Time, userAgent) subs = append(subs, sub) } if err == nil { err = rows.Err() } if err == nil && tcat == t.TopicCatP2P && len(subs) > 0 { // Swap public & lastSeen values of P2P topics as expected. if len(subs) == 1 { // The other user is deleted, nothing we can do. subs[0].SetPublic(nil) subs[0].SetTrusted(nil) subs[0].SetLastSeenAndUA(nil, "") } else { tmp := subs[0].GetPublic() subs[0].SetPublic(subs[1].GetPublic()) subs[1].SetPublic(tmp) tmp = subs[0].GetTrusted() subs[0].SetTrusted(subs[1].GetTrusted()) subs[1].SetTrusted(tmp) lastSeen := subs[0].GetLastSeen() userAgent = subs[0].GetUserAgent() subs[0].SetLastSeenAndUA(subs[1].GetLastSeen(), subs[1].GetUserAgent()) subs[1].SetLastSeenAndUA(lastSeen, userAgent) } // Remove deleted and unneeded subscriptions if !keepDeleted || !oneUser.IsZero() { var xsubs []t.Subscription for i := range subs { if (subs[i].DeletedAt != nil && !keepDeleted) || (!oneUser.IsZero() && subs[i].Uid() != oneUser) { continue } xsubs = append(xsubs, subs[i]) } subs = xsubs } } return subs, err } // topicNamesForUser reads a slice of strings using provided query. // if includeChan is true, the query is expected to add channel names as well as group topic names. func (a *adapter) topicNamesForUser(sqlQuery string, includeChan bool, args ...any) ([]string, error) { ctx, cancel := a.getContext() if cancel != nil { defer cancel() } rows, err := a.db.QueryxContext(ctx, sqlQuery, args...) if err != nil { return nil, err } defer rows.Close() var names []string for rows.Next() { var name string if err = rows.Scan(&name); err != nil { break } names = append(names, name) // If the name is a group topic, also add the channel name if requested. if includeChan { if channel := t.GrpToChn(name); channel != "" { names = append(names, channel) } } } if err == nil { err = rows.Err() } return names, err } // OwnTopics loads a slice of topic names where the user is the owner. func (a *adapter) OwnTopics(uid t.Uid) ([]string, error) { return a.topicNamesForUser("SELECT name FROM topics WHERE owner=? AND state!=?", false, store.DecodeUid(uid), t.StateDeleted) } // ChannelsForUser loads a slice of topic names where the user is a channel reader and notifications (P) are enabled. func (a *adapter) ChannelsForUser(uid t.Uid) ([]string, error) { return a.topicNamesForUser("SELECT topic FROM subscriptions WHERE userid=? AND topic LIKE 'chn%' "+ "AND INSTR(modewant,'P')>0 AND INSTR(modegiven,'P')>0 AND deletedat IS NULL", false, store.DecodeUid(uid)) } // TopicShare adds subscriptions to a topic and increments the topic's subcnt. func (a *adapter) TopicShare(topic string, shares []*t.Subscription) error { ctx, cancel := a.getContextForTx() if cancel != nil { defer cancel() } tx, err := a.db.BeginTxx(ctx, nil) if err != nil { return err } defer func() { if err != nil { tx.Rollback() } }() for _, sub := range shares { err = createSubscription(tx, sub, true) if err != nil { return err } } if topic != "" { // Update topic's subscription count. if _, err = tx.Exec("UPDATE topics SET subcnt=subcnt+? WHERE name=?", len(shares), topic); err != nil { return err } } return tx.Commit() } // TopicDelete deletes topic, subscriptions, messages. func (a *adapter) TopicDelete(topic string, isChan, hard bool) error { ctx, cancel := a.getContextForTx() if cancel != nil { defer cancel() } tx, err := a.db.BeginTxx(ctx, nil) if err != nil { return err } defer func() { if err != nil { tx.Rollback() } }() // If the topic is a channel, must try to delete subscriptions under both grpXXX and chnXXX names. args := []any{topic} if isChan { args = append(args, t.GrpToChn(topic)) } if hard { // Delete subscriptions. If this is a channel, delete both group subscriptions and channel subscriptions. q, args, _ := sqlx.In("DELETE FROM subscriptions WHERE topic IN (?)", args) if _, err = tx.Exec(tx.Rebind(q), args...); err != nil { return err } if err = messageDeleteList(tx, topic, nil); err != nil { return err } if _, err = tx.Exec("DELETE FROM topictags WHERE topic=?", topic); err != nil { return err } if _, err = tx.Exec("DELETE FROM topics WHERE name=?", topic); err != nil { return err } } else { now := t.TimeNow() q, args, _ := sqlx.In("UPDATE subscriptions SET updatedat=?,deletedat=? WHERE topic IN (?)", now, now, args) if _, err = tx.Exec(tx.Rebind(q), args...); err != nil { return err } if _, err = tx.Exec("UPDATE topics SET updatedat=?,touchedat=?,state=?,stateat=? WHERE name=?", now, now, t.StateDeleted, now, topic); err != nil { return err } } return tx.Commit() } // TopicUpdateOnMessage updates topic's seqid and touchedat when a new message is posted. func (a *adapter) TopicUpdateOnMessage(topic string, msg *t.Message) error { ctx, cancel := a.getContext() if cancel != nil { defer cancel() } _, err := a.db.ExecContext(ctx, "UPDATE topics SET seqid=?,touchedat=? WHERE name=?", msg.SeqId, msg.CreatedAt, topic) return err } // TopicUpdateSubCnt updates subscriber count denormalized in topic. func (a *adapter) TopicUpdateSubCnt(topic string) error { ctx, cancel := a.getContext() if cancel != nil { defer cancel() } _, err := a.db.ExecContext(ctx, "UPDATE topics SET subcnt=(SELECT COUNT(*) FROM subscriptions WHERE topic IN (?,?) AND deletedat IS NULL) WHERE name=?", topic, t.GrpToChn(topic), topic) return err } // TopicUpdate updates topic's fields given in the update map. // If update contains UpdatedAt but not TouchedAt, TouchedAt is set to Updated func (a *adapter) TopicUpdate(topic string, update map[string]any) error { ctx, cancel := a.getContextForTx() if cancel != nil { defer cancel() } tx, err := a.db.BeginTxx(ctx, nil) if err != nil { return err } defer func() { if err != nil { tx.Rollback() } }() if t, u := update["TouchedAt"], update["UpdatedAt"]; t == nil && u != nil { update["TouchedAt"] = u } cols, args := common.UpdateByMap(update) args = append(args, topic) _, err = tx.Exec("UPDATE topics SET "+strings.Join(cols, ",")+" WHERE name=?", args...) if err != nil { return err } // Tags are also stored in a separate table if tags := common.ExtractTags(update); tags != nil { // First delete all user tags _, err = tx.Exec("DELETE FROM topictags WHERE topic=?", topic) if err != nil { return err } // Now insert new tags err = addTags(tx, "topictags", "topic", topic, tags, false) if err != nil { return err } } return tx.Commit() } func (a *adapter) TopicOwnerChange(topic string, newOwner t.Uid) error { ctx, cancel := a.getContext() if cancel != nil { defer cancel() } _, err := a.db.ExecContext(ctx, "UPDATE topics SET owner=? WHERE name=?", store.DecodeUid(newOwner), topic) return err } // Get a subscription of a user to a topic. func (a *adapter) SubscriptionGet(topic string, user t.Uid, keepDeleted bool) (*t.Subscription, error) { ctx, cancel := a.getContext() if cancel != nil { defer cancel() } query := `SELECT createdat,updatedat,deletedat,userid AS user,topic,delid,recvseqid, readseqid,modewant,modegiven,private FROM subscriptions WHERE topic=? AND userid=?` if !keepDeleted { query += " AND deletedat IS NULL" } var sub t.Subscription err := a.db.GetContext(ctx, &sub, query, topic, store.DecodeUid(user)) if err != nil { if err == sql.ErrNoRows { // Nothing found - clear the error err = nil } return nil, err } sub.User = user.String() sub.Private = common.FromJSON(sub.Private) return &sub, nil } // SubsForUser loads all user's subscriptions. Does NOT load Public or Private values and does // not load deleted subscriptions. func (a *adapter) SubsForUser(forUser t.Uid) ([]t.Subscription, error) { q := `SELECT createdat,updatedat,deletedat,userid AS user,topic,delid,recvseqid, readseqid,modewant,modegiven FROM subscriptions WHERE userid=? AND deletedat IS NULL` args := []any{store.DecodeUid(forUser)} ctx, cancel := a.getContext() if cancel != nil { defer cancel() } rows, err := a.db.QueryxContext(ctx, q, args...) if err != nil { return nil, err } defer rows.Close() var subs []t.Subscription var sub t.Subscription for rows.Next() { if err = rows.StructScan(&sub); err != nil { break } sub.User = forUser.String() subs = append(subs, sub) } if err == nil { err = rows.Err() } return subs, err } // SubsForTopic fetches all subsciptions for a topic. Does NOT load Public value and does not load channel readers. // The difference between UsersForTopic vs SubsForTopic is that the former loads user.public+trusted, // the latter does not. func (a *adapter) SubsForTopic(topic string, keepDeleted bool, opts *t.QueryOpt) ([]t.Subscription, error) { q := `SELECT createdat,updatedat,deletedat,userid AS user,topic,delid,recvseqid, readseqid,modewant,modegiven,private FROM subscriptions WHERE topic=?` args := []any{topic} if !keepDeleted { // Filter out deleted rows. q += " AND deletedat IS NULL" } limit := a.maxResults if opts != nil { // Ignore IfModifiedSince - we must return all entries // Those unmodified will be stripped of Public & Private. if !opts.User.IsZero() { q += " AND userid=?" args = append(args, store.DecodeUid(opts.User)) } if opts.Limit > 0 && opts.Limit < limit { limit = opts.Limit } } q += " LIMIT ?" args = append(args, limit) ctx, cancel := a.getContext() if cancel != nil { defer cancel() } rows, err := a.db.QueryxContext(ctx, q, args...) if err != nil { return nil, err } defer rows.Close() var subs []t.Subscription var sub t.Subscription for rows.Next() { if err = rows.StructScan(&sub); err != nil { break } sub.User = common.EncodeUidString(sub.User).String() sub.Private = common.FromJSON(sub.Private) subs = append(subs, sub) } if err == nil { err = rows.Err() } return subs, err } // SubsUpdate updates one or multiple subscriptions to a topic. func (a *adapter) SubsUpdate(topic string, user t.Uid, update map[string]any) error { ctx, cancel := a.getContextForTx() if cancel != nil { defer cancel() } tx, err := a.db.BeginTxx(ctx, nil) if err != nil { return err } defer func() { if err != nil { tx.Rollback() } }() cols, args := common.UpdateByMap(update) q := "UPDATE subscriptions SET " + strings.Join(cols, ",") + " WHERE topic=?" args = append(args, topic) if !user.IsZero() { // Update just one topic subscription q += " AND userid=?" args = append(args, store.DecodeUid(user)) } if _, err = tx.Exec(q, args...); err != nil { return err } return tx.Commit() } // SubsDelete marks at most one subscription as deleted (soft-deleting). func (a *adapter) SubsDelete(topic string, user t.Uid) error { ctx, cancel := a.getContextForTx() if cancel != nil { defer cancel() } tx, err := a.db.BeginTxx(ctx, nil) if err != nil { return err } defer func() { if err != nil { tx.Rollback() } }() decoded_id := store.DecodeUid(user) now := t.TimeNow() // Mark subscription as deleted. res, err := tx.ExecContext(ctx, "UPDATE subscriptions SET updatedat=?,deletedat=? WHERE topic=? AND userid=? AND deletedat IS NULL", now, now, topic, decoded_id) if err != nil { return err } affected, err := res.RowsAffected() if err == nil && affected == 0 { // ensure tx.Rollback() above is ran err = t.ErrNotFound return err } // Channel readers cannot delete messages. if !t.IsChannel(topic) { // Remove records of messages soft-deleted by this user. _, err = tx.Exec("DELETE FROM dellog WHERE topic=? AND deletedfor=?", topic, decoded_id) if err != nil { return err } } if t.GetTopicCat(topic) == t.TopicCatGrp { // Decrement topic subscription count (only one subscription is deleted). _, err = tx.Exec("UPDATE topics SET subcnt=subcnt-1 WHERE name=?", t.ChnToGrp(topic)) if err != nil { return err } } return tx.Commit() } // subsDelForUser marks user's subscriptions as deleted. func subsDelForUser(tx *sqlx.Tx, decoded_uid int64, hard bool) error { // Decrement subscription count for all topics the user is subscribed to. rows, err := tx.Query("SELECT topic FROM subscriptions WHERE userid=? AND deletedat IS NULL", decoded_uid) if err != nil { return err } var topics []any for rows.Next() { var name string if err = rows.Scan(&name); err != nil { break } if t.IsChannel(name) { // Convert channel name to group name. name = t.ChnToGrp(name) } topics = append(topics, name) } if err == nil { err = rows.Err() } rows.Close() if err != nil { return err } if len(topics) > 0 { sql, args, err := sqlx.In("UPDATE topics SET subcnt=subcnt-1 WHERE name IN (?)", topics) _, err = tx.Exec(tx.Rebind(sql), args...) if err != nil { return err } } if hard { _, err = tx.Exec("DELETE FROM subscriptions WHERE userid=?", decoded_uid) } else { now := t.TimeNow() _, err = tx.Exec("UPDATE subscriptions SET updatedat=?,deletedat=? WHERE userid=? AND deletedat IS NULL", now, now, decoded_uid) } return err } // SubsDelForUser marks user's subscriptions as deleted. func (a *adapter) SubsDelForUser(user t.Uid, hard bool) error { ctx, cancel := a.getContextForTx() if cancel != nil { defer cancel() } tx, err := a.db.BeginTxx(ctx, nil) if err != nil { return err } defer func() { if err != nil { tx.Rollback() } }() if err = subsDelForUser(tx, store.DecodeUid(user), hard); err != nil { return err } return tx.Commit() } // Find returns a list of users or group topics who match given tags, such as "email:jdoe@example.com" or "tel:+18003287448". func (a *adapter) Find(caller, promoPrefix string, req [][]string, opt []string, activeOnly bool) ([]t.Subscription, error) { var args []any stateConstraint := "" if activeOnly { args = append(args, t.StateOK) stateConstraint = "u.state=? AND " } index := make(map[string]struct{}) allReq := t.FlattenDoubleSlice(req) for _, tag := range append(allReq, opt...) { args = append(args, tag) index[tag] = struct{}{} } var matcher string if promoPrefix != "" { // The max number of tags is 16. Using 20 to make sure one prefix match is greater than all non-prefix matches. matcher = "SUM(CASE WHEN LOCATE('" + promoPrefix + "', tg.tag)=1 THEN 20 ELSE 1 END)" } else { matcher = "COUNT(*)" } query := "SELECT u.id,u.createdat,u.updatedat,0,u.access,0 AS subcnt,u.public,u.trusted,u.tags," + matcher + " AS matches " + "FROM users AS u JOIN usertags AS tg ON tg.userid=u.id " + "WHERE " + stateConstraint + "tg.tag IN (?" + strings.Repeat(",?", len(allReq)+len(opt)-1) + ") " + "GROUP BY u.id,u.createdat,u.updatedat,u.access,u.public,u.trusted,u.tags " if len(allReq) > 0 { q, a := common.DisjunctionSql(req, "tg.tag") query += q args = append(args, a...) } query += "UNION ALL " if activeOnly { args = append(args, t.StateOK) stateConstraint = "t.state=? AND " } for _, tag := range append(allReq, opt...) { args = append(args, tag) } query += "SELECT t.name AS topic,t.createdat,t.updatedat,t.usebt,t.access,t.subcnt,t.public,t.trusted,t.tags," + matcher + " AS matches " + "FROM topics AS t JOIN topictags AS tg ON t.name=tg.topic " + "WHERE " + stateConstraint + "tg.tag IN (?" + strings.Repeat(",?", len(allReq)+len(opt)-1) + ") " + "GROUP BY t.name,t.createdat,t.updatedat,t.usebt,t.access,t.subcnt,t.public,t.trusted,t.tags " if len(allReq) > 0 { q, a := common.DisjunctionSql(req, "tg.tag") query += q args = append(args, a...) } query += "ORDER BY matches DESC, subcnt DESC LIMIT ?" args = append(args, a.maxResults) ctx, cancel := a.getContext() if cancel != nil { defer cancel() } // Get users matched by tags, sort by number of matches from high to low. rows, err := a.db.QueryxContext(ctx, query, args...) if err != nil { return nil, err } defer rows.Close() // Read results as subscriptions. var public, trusted any var access t.DefaultAccess var subcnt int var setTags t.StringSlice var ignored int var isChan bool var sub t.Subscription var subs []t.Subscription for rows.Next() { if err = rows.Scan(&sub.Topic, &sub.CreatedAt, &sub.UpdatedAt, &isChan, &access, &subcnt, &public, &trusted, &setTags, &ignored); err != nil { subs = nil break } if id, err := strconv.ParseInt(sub.Topic, 10, 64); err == nil { sub.Topic = store.EncodeUid(id).UserId() if sub.Topic == caller { // Skip the caller. continue } } if isChan { // This is a channel, convert grp to chn name: all channel-capable // topics should appear as channels in search results. sub.Topic = t.GrpToChn(sub.Topic) } sub.SetSubCnt(subcnt) sub.SetPublic(common.FromJSON(public)) sub.SetTrusted(common.FromJSON(trusted)) sub.SetDefaultAccess(access.Auth, access.Anon) // Indicating that the mode is not set, not 'N'. sub.ModeGiven = t.ModeUnset sub.ModeWant = t.ModeUnset sub.Private = common.FilterFoundTags(setTags, index) subs = append(subs, sub) } if err == nil { err = rows.Err() } return subs, err } // FindOne returns the first topic or user which matches the given tag. func (a *adapter) FindOne(tag string) (string, error) { var args []any query := "SELECT t.name AS topic FROM topics AS t LEFT JOIN topictags AS tt ON t.name=tt.topic " + "WHERE tt.tag=?" args = append(args, tag) query += " UNION ALL " query += "SELECT u.id AS topic FROM users AS u LEFT JOIN usertags AS ut ON ut.userid=u.id " + "WHERE ut.tag=?" args = append(args, tag) // LIMIT is applied to all resultant rows. query += " LIMIT 1" ctx, cancel := a.getContext() if cancel != nil { defer cancel() } rows, err := a.db.QueryxContext(ctx, query, args...) if err != nil { return "", err } defer rows.Close() var found string if rows.Next() { if err = rows.Scan(&found); err != nil { return "", err } // Check if the found value is a topic name or a user ID. // User IDs are returned as decoded decimal strings. if id, err := strconv.ParseInt(found, 10, 64); err == nil { found = store.EncodeUid(id).UserId() } } if err == nil { err = rows.Err() } return found, err } // Messages func (a *adapter) MessageSave(msg *t.Message) error { ctx, cancel := a.getContext() if cancel != nil { defer cancel() } // store assignes message ID, but we don't use it. Message IDs are not used anywhere. // Using a sequential ID provided by the database. res, err := a.db.ExecContext(ctx, "INSERT INTO messages(createdAt,updatedAt,seqid,topic,`from`,head,content) VALUES(?,?,?,?,?,?,?)", msg.CreatedAt, msg.UpdatedAt, msg.SeqId, msg.Topic, store.DecodeUid(t.ParseUid(msg.From)), msg.Head, common.ToJSON(msg.Content)) if err == nil { id, _ := res.LastInsertId() // Replacing ID given by store by ID given by the DB. msg.SetUid(t.Uid(id)) } return err } // MessageGetAll returns messages matching the query. func (a *adapter) MessageGetAll(topic string, forUser t.Uid, opts *t.QueryOpt) ([]t.Message, error) { var limit = a.maxMessageResults args := []any{store.DecodeUid(forUser), topic} seqIdConstraint := "" if opts != nil { seqIdConstraint = "AND m.seqid " if len(opts.IdRanges) > 0 { constr, newargs := common.RangesToSql(opts.IdRanges) seqIdConstraint += constr args = append(args, newargs...) } else { seqIdConstraint += "BETWEEN ? AND ?" if opts.Since > 0 { args = append(args, opts.Since) } else { args = append(args, 0) } if opts.Before > 1 { // MySQL BETWEEN is inclusive-inclusive, Tinode API requires inclusive-exclusive, thus -1 args = append(args, opts.Before-1) } else { args = append(args, 1<<31-1) } } if opts.Limit > 0 && opts.Limit < limit { limit = opts.Limit } } args = append(args, limit) ctx, cancel := a.getContext() if cancel != nil { defer cancel() } rows, err := a.db.QueryxContext( ctx, "SELECT m.createdat,m.updatedat,m.deletedat,m.delid,m.seqid,m.topic,m.`from`,m.head,m.content"+ " FROM messages AS m LEFT JOIN dellog AS d"+ " ON d.topic=m.topic AND m.seqid BETWEEN d.low AND d.hi-1 AND d.deletedfor=?"+ " WHERE m.delid=0 AND m.topic=? "+seqIdConstraint+" AND d.deletedfor IS NULL"+ " ORDER BY m.seqid DESC LIMIT ?", args...) if err != nil { return nil, err } defer rows.Close() msgs := make([]t.Message, 0, limit) for rows.Next() { var msg t.Message if err = rows.StructScan(&msg); err != nil { break } msg.From = common.EncodeUidString(msg.From).String() msg.Content = common.FromJSON(msg.Content) msgs = append(msgs, msg) } if err == nil { err = rows.Err() } return msgs, err } // Get ranges of deleted messages func (a *adapter) MessageGetDeleted(topic string, forUser t.Uid, opts *t.QueryOpt) ([]t.DelMessage, error) { var limit = a.maxResults var lower = 0 var upper = 1<<31 - 1 if opts != nil { if opts.Since > 0 { lower = opts.Since } if opts.Before > 1 { // DelRange is inclusive-exclusive, while BETWEEN is inclusive-inclisive. upper = opts.Before - 1 } if opts.Limit > 0 && opts.Limit < limit { limit = opts.Limit } } // Fetch log of deletions ctx, cancel := a.getContext() if cancel != nil { defer cancel() } rows, err := a.db.QueryxContext(ctx, "SELECT topic,deletedfor,delid,low,hi FROM dellog WHERE topic=? AND delid BETWEEN ? AND ?"+ " AND (deletedFor=0 OR deletedFor=?)"+ " ORDER BY delid LIMIT ?", topic, lower, upper, store.DecodeUid(forUser), limit) if err != nil { return nil, err } defer rows.Close() var dellog struct { Topic string Deletedfor int64 Delid int Low int Hi int } var dmsgs []t.DelMessage var dmsg t.DelMessage for rows.Next() { if err = rows.StructScan(&dellog); err != nil { dmsgs = nil break } if dellog.Delid != dmsg.DelId { if dmsg.DelId > 0 { dmsgs = append(dmsgs, dmsg) } dmsg.DelId = dellog.Delid dmsg.Topic = dellog.Topic if dellog.Deletedfor > 0 { dmsg.DeletedFor = store.EncodeUid(dellog.Deletedfor).String() } else { dmsg.DeletedFor = "" } dmsg.SeqIdRanges = nil } if dellog.Hi <= dellog.Low+1 { dellog.Hi = 0 } dmsg.SeqIdRanges = append(dmsg.SeqIdRanges, t.Range{Low: dellog.Low, Hi: dellog.Hi}) } if err == nil { err = rows.Err() } if err == nil { if dmsg.DelId > 0 { dmsgs = append(dmsgs, dmsg) } } return dmsgs, err } func messageDeleteList(tx *sqlx.Tx, topic string, toDel *t.DelMessage) error { var err error if toDel == nil { // Whole topic is being deleted, thus also deleting all messages. _, err = tx.Exec("DELETE FROM dellog WHERE topic=?", topic) if err == nil { _, err = tx.Exec("DELETE FROM messages WHERE topic=?", topic) } // filemsglinks will be deleted because of ON DELETE CASCADE return err } // Only some messages are being deleted. delRanges := toDel.SeqIdRanges if toDel.DeletedFor == "" { // Hard-deleting messages requires updates to the messages table. where := "m.topic=?" args := []any{topic} if len(delRanges) > 0 { rSql, rArgs := common.RangesToSql(delRanges) where += " AND m.seqid " + rSql args = append(args, rArgs...) } where += " AND m.deletedat IS NULL" // We are asked to delete messages no older than newerThan. if newerThan := toDel.GetNewerThan(); newerThan != nil { where += " AND m.createdat>?" args = append(args, newerThan) } // Find the actual IDs still present in the database. var seqIDs []int err = tx.Select(&seqIDs, "SELECT seqid FROM messages AS m WHERE "+where, args...) if err != nil { return err } if len(seqIDs) == 0 { // Nothing to delete. No need to make a log entry. All done. return nil } // Recalculate the actual ranges to delete. sort.Ints(seqIDs) delRanges = t.SliceToRanges(seqIDs) // Compose a new query with the new ranges. where = "m.topic=?" args = []any{topic} rSql, rArgs := common.RangesToSql(delRanges) where += " AND m.seqid " + rSql args = append(args, rArgs...) // No need to add anything else: deletedat etc is already accounted for. _, err = tx.Exec("DELETE fml.* FROM filemsglinks AS fml INNER JOIN messages AS m ON m.id=fml.msgid WHERE "+ where, args...) if err != nil { return err } // Instead of deleting messages, clear all content. _, err = tx.Exec("UPDATE messages AS m SET m.deletedat=?,m.delId=?,m.`from`=0,m.head=NULL,m.content=NULL WHERE "+ where, append([]any{t.TimeNow(), toDel.DelId}, args...)...) if err != nil { return err } } // Now make log entries. Needed for both hard- and soft-deleting. var insert *sql.Stmt if insert, err = tx.Prepare( "INSERT INTO dellog(topic,deletedfor,delid,low,hi) VALUES(?,?,?,?,?)"); err != nil { return err } forUser := common.DecodeUidString(toDel.DeletedFor) for _, rng := range delRanges { if rng.Hi == 0 { // Dellog must contain valid Low and *Hi*. rng.Hi = rng.Low + 1 } // A log entry for each range. if _, err = insert.Exec(topic, forUser, toDel.DelId, rng.Low, rng.Hi); err != nil { break } } return err } // MessageDeleteList deletes messages in the given topic with seqIds from the list. func (a *adapter) MessageDeleteList(topic string, toDel *t.DelMessage) (err error) { ctx, cancel := a.getContextForTx() if cancel != nil { defer cancel() } tx, err := a.db.BeginTxx(ctx, nil) if err != nil { return err } defer func() { if err != nil { tx.Rollback() } }() if err = messageDeleteList(tx, topic, toDel); err != nil { return err } return tx.Commit() } func deviceHasher(deviceID string) string { // Generate custom key as [64-bit hash of device id] to ensure predictable // length of the key hasher := fnv.New64() hasher.Write([]byte(deviceID)) return strconv.FormatUint(uint64(hasher.Sum64()), 16) } // Device management for push notifications. // DeviceUpsert creates or updates a device record. func (a *adapter) DeviceUpsert(uid t.Uid, def *t.DeviceDef) error { hash := deviceHasher(def.DeviceId) ctx, cancel := a.getContextForTx() if cancel != nil { defer cancel() } tx, err := a.db.BeginTxx(ctx, nil) if err != nil { return err } defer func() { if err != nil { tx.Rollback() } }() // Ensure uniqueness of the device ID: delete all records of the device ID _, err = tx.Exec("DELETE FROM devices WHERE hash=?", hash) if err != nil { return err } // Actually add/update DeviceId for the new user _, err = tx.Exec("INSERT INTO devices(userid, hash, deviceId, platform, lastseen, lang) VALUES(?,?,?,?,?,?)", store.DecodeUid(uid), hash, def.DeviceId, def.Platform, def.LastSeen, def.Lang) if err != nil { return err } return tx.Commit() } // DeviceGetAll returns all devices for a given set of users. func (a *adapter) DeviceGetAll(uids ...t.Uid) (map[t.Uid][]t.DeviceDef, int, error) { var unums []any for _, uid := range uids { unums = append(unums, store.DecodeUid(uid)) } q, unums, _ := sqlx.In("SELECT userid,deviceid,platform,lastseen,lang FROM devices WHERE userid IN (?)", unums) ctx, cancel := a.getContext() if cancel != nil { defer cancel() } rows, err := a.db.QueryxContext(ctx, q, unums...) if err != nil { return nil, 0, err } defer rows.Close() var device struct { Userid int64 Deviceid string Platform string Lastseen time.Time Lang string } result := make(map[t.Uid][]t.DeviceDef) count := 0 for rows.Next() { if err = rows.StructScan(&device); err != nil { break } uid := store.EncodeUid(device.Userid) udev := result[uid] udev = append(udev, t.DeviceDef{ DeviceId: device.Deviceid, Platform: device.Platform, LastSeen: device.Lastseen, Lang: device.Lang, }) result[uid] = udev count++ } if err == nil { err = rows.Err() } return result, count, err } func deviceDelete(tx *sqlx.Tx, uid t.Uid, deviceID string) error { var err error var res sql.Result if deviceID == "" { res, err = tx.Exec("DELETE FROM devices WHERE userid=?", store.DecodeUid(uid)) } else { res, err = tx.Exec("DELETE FROM devices WHERE userid=? AND hash=?", store.DecodeUid(uid), deviceHasher(deviceID)) } if err == nil { if count, _ := res.RowsAffected(); count == 0 { err = t.ErrNotFound } } return err } // DeviceDelete deletes a device record (push token). func (a *adapter) DeviceDelete(uid t.Uid, deviceID string) error { ctx, cancel := a.getContextForTx() if cancel != nil { defer cancel() } tx, err := a.db.BeginTxx(ctx, nil) if err != nil { return err } defer func() { if err != nil { tx.Rollback() } }() err = deviceDelete(tx, uid, deviceID) if err != nil { return err } return tx.Commit() } // Credential management // CredUpsert adds or updates a validation record. Returns true if inserted, false if updated. // 1. if credential is validated: // 1.1 Hard-delete unconfirmed equivalent record, if exists. // 1.2 Insert new. Report error if duplicate. // 2. if credential is not validated: // 2.1 Check if validated equivalent exist. If so, report an error. // 2.2 Soft-delete all unvalidated records of the same method. // 2.3 Undelete existing credential. Return if successful. // 2.4 Insert new credential record. func (a *adapter) CredUpsert(cred *t.Credential) (bool, error) { var err error ctx, cancel := a.getContextForTx() if cancel != nil { defer cancel() } tx, err := a.db.BeginTxx(ctx, nil) if err != nil { return false, err } defer func() { if err != nil { tx.Rollback() } }() now := t.TimeNow() userId := common.DecodeUidString(cred.User) // Enforce uniqueness: if credential is confirmed, "method:value" must be unique. // if credential is not yet confirmed, "userid:method:value" is unique. synth := cred.Method + ":" + cred.Value if !cred.Done { // Check if this credential is already validated. var done bool err = tx.Get(&done, "SELECT done FROM credentials WHERE synthetic=?", synth) if err == nil { // Assign err to ensure closing of a transaction. err = t.ErrDuplicate return false, err } if err != sql.ErrNoRows { return false, err } // We are going to insert new record. synth = cred.User + ":" + synth // Adding new unvalidated credential. Deactivate all unvalidated records of this user and method. _, err = tx.Exec("UPDATE credentials SET deletedat=? WHERE userid=? AND method=? AND done=FALSE", now, userId, cred.Method) if err != nil { return false, err } // Assume that the record exists and try to update it: undelete, update timestamp and response value. res, err := tx.Exec("UPDATE credentials SET updatedat=?,deletedat=NULL,resp=?,done=FALSE WHERE synthetic=?", cred.UpdatedAt, cred.Resp, synth) if err != nil { return false, err } // If record was updated, then all is fine. if numrows, _ := res.RowsAffected(); numrows > 0 { return false, tx.Commit() } } else { // Hard-deleting unconformed record if it exists. _, err = tx.Exec("DELETE FROM credentials WHERE synthetic=?", cred.User+":"+synth) if err != nil { return false, err } } // Add new record. _, err = tx.Exec("INSERT INTO credentials(createdat,updatedat,method,value,synthetic,userid,resp,done) "+ "VALUES(?,?,?,?,?,?,?,?)", cred.CreatedAt, cred.UpdatedAt, cred.Method, cred.Value, synth, userId, cred.Resp, cred.Done) if err != nil { if isDupe(err) { return true, t.ErrDuplicate } return true, err } return true, tx.Commit() } // credDel deletes given validation method or all methods of the given user. // 1. If user is being deleted, hard-delete all records (method == "") // 2. If one value is being deleted: // 2.1 Delete it if it's valiated or if there were no attempts at validation // (otherwise it could be used to circumvent the limit on validation attempts). // 2.2 In that case mark it as soft-deleted. func credDel(tx *sqlx.Tx, uid t.Uid, method, value string) error { constraints := " WHERE userid=?" args := []any{store.DecodeUid(uid)} if method != "" { constraints += " AND method=?" args = append(args, method) if value != "" { constraints += " AND value=?" args = append(args, value) } } var err error var res sql.Result if method == "" { // Case 1 res, err = tx.Exec("DELETE FROM credentials"+constraints, args...) if err == nil { if count, _ := res.RowsAffected(); count == 0 { err = t.ErrNotFound } } return err } // Case 2.1 res, err = tx.Exec("DELETE FROM credentials"+constraints+" AND (done=TRUE OR retries=0)", args...) if err != nil { return err } if count, _ := res.RowsAffected(); count > 0 { return nil } // Case 2.2 args = append([]any{t.TimeNow()}, args...) res, err = tx.Exec("UPDATE credentials SET deletedat=?"+constraints, args...) if err == nil { if count, _ := res.RowsAffected(); count >= 0 { err = t.ErrNotFound } } return err } // CredDel deletes either credentials of the given user. If method is blank all // credentials are removed. If value is blank all credentials of the given the // method are removed. func (a *adapter) CredDel(uid t.Uid, method, value string) error { ctx, cancel := a.getContextForTx() if cancel != nil { defer cancel() } tx, err := a.db.BeginTxx(ctx, nil) if err != nil { return err } defer func() { if err != nil { tx.Rollback() } }() err = credDel(tx, uid, method, value) if err != nil { return err } return tx.Commit() } // CredConfirm marks given credential method as confirmed. func (a *adapter) CredConfirm(uid t.Uid, method string) error { ctx, cancel := a.getContext() if cancel != nil { defer cancel() } res, err := a.db.ExecContext( ctx, "UPDATE credentials SET updatedat=?,done=TRUE,synthetic=CONCAT(method,':',value) "+ "WHERE userid=? AND method=? AND deletedat IS NULL AND done=FALSE", t.TimeNow(), store.DecodeUid(uid), method) if err != nil { if isDupe(err) { return t.ErrDuplicate } return err } if numrows, _ := res.RowsAffected(); numrows < 1 { return t.ErrNotFound } return nil } // CredFail increments failure count of the given validation method. func (a *adapter) CredFail(uid t.Uid, method string) error { ctx, cancel := a.getContext() if cancel != nil { defer cancel() } _, err := a.db.ExecContext(ctx, "UPDATE credentials SET updatedat=?,retries=retries+1 WHERE userid=? AND method=? AND done=FALSE", t.TimeNow(), store.DecodeUid(uid), method) return err } // CredGetActive returns currently active unvalidated credential of the given user and method. func (a *adapter) CredGetActive(uid t.Uid, method string) (*t.Credential, error) { ctx, cancel := a.getContext() if cancel != nil { defer cancel() } var cred t.Credential err := a.db.GetContext(ctx, &cred, "SELECT createdat,updatedat,method,value,resp,done,retries "+ "FROM credentials WHERE userid=? AND deletedat IS NULL AND method=? AND done=FALSE", store.DecodeUid(uid), method) if err != nil { if err == sql.ErrNoRows { err = nil } return nil, err } cred.User = uid.String() return &cred, nil } // CredGetAll returns credential records for the given user and method, all or validated only. func (a *adapter) CredGetAll(uid t.Uid, method string, validatedOnly bool) ([]t.Credential, error) { query := "SELECT createdat,updatedat,method,value,resp,done,retries FROM credentials WHERE userid=? AND deletedat IS NULL" args := []any{store.DecodeUid(uid)} if method != "" { query += " AND method=?" args = append(args, method) } if validatedOnly { query += " AND done=TRUE" } ctx, cancel := a.getContext() if cancel != nil { defer cancel() } var credentials []t.Credential err := a.db.SelectContext(ctx, &credentials, query, args...) if err != nil { return nil, err } user := uid.String() for i := range credentials { credentials[i].User = user } return credentials, err } // FileUploads // FileStartUpload initializes a file upload func (a *adapter) FileStartUpload(fd *t.FileDef) error { ctx, cancel := a.getContext() if cancel != nil { defer cancel() } var user any if fd.User != "" { user = store.DecodeUid(t.ParseUid(fd.User)) } else { user = 0 } _, err := a.db.ExecContext(ctx, "INSERT INTO fileuploads(id,createdat,updatedat,userid,status,mimetype,size,etag,location) "+ "VALUES(?,?,?,?,?,?,?,?,?)", store.DecodeUid(fd.Uid()), fd.CreatedAt, fd.UpdatedAt, user, fd.Status, fd.MimeType, fd.Size, fd.ETag, fd.Location) return err } // FileFinishUpload marks file upload as completed, successfully or otherwise func (a *adapter) FileFinishUpload(fd *t.FileDef, success bool, size int64) (*t.FileDef, error) { ctx, cancel := a.getContext() if cancel != nil { defer cancel() } tx, err := a.db.BeginTxx(ctx, nil) if err != nil { return nil, err } defer func() { if err != nil { tx.Rollback() } }() now := t.TimeNow() if success { _, err = tx.ExecContext(ctx, "UPDATE fileuploads SET updatedat=?,status=?,size=?,etag=?,location=? WHERE id=?", now, t.UploadCompleted, size, fd.ETag, fd.Location, store.DecodeUid(fd.Uid())) if err != nil { return nil, err } fd.Status = t.UploadCompleted fd.Size = size } else { // Deleting the record: there is no value in keeping it in the DB. _, err = tx.ExecContext(ctx, "DELETE FROM fileuploads WHERE id=?", store.DecodeUid(fd.Uid())) if err != nil { return nil, err } fd.Status = t.UploadFailed fd.Size = 0 } fd.UpdatedAt = now return fd, tx.Commit() } // FileGet fetches a record of a specific file func (a *adapter) FileGet(fid string) (*t.FileDef, error) { id := t.ParseUid(fid) if id.IsZero() { return nil, t.ErrMalformed } ctx, cancel := a.getContext() if cancel != nil { defer cancel() } var fd t.FileDef err := a.db.GetContext(ctx, &fd, "SELECT id,createdat,updatedat,userid AS user,status,mimetype,size,IFNULL(etag,'') AS etag,location "+ "FROM fileuploads WHERE id=?", store.DecodeUid(id)) if err == sql.ErrNoRows { return nil, nil } if err != nil { return nil, err } fd.Id = common.EncodeUidString(fd.Id).String() fd.User = common.EncodeUidString(fd.User).String() return &fd, nil } // FileDeleteUnused deletes records where UseCount is zero. If olderThan is non-zero, deletes // unused records with UpdatedAt before olderThan. // Returns array of FileDef.Location of deleted filerecords so actual files can be deleted too. func (a *adapter) FileDeleteUnused(olderThan time.Time, limit int) ([]string, error) { ctx, cancel := a.getContextForTx() if cancel != nil { defer cancel() } tx, err := a.db.BeginTxx(ctx, nil) if err != nil { return nil, err } defer func() { if err != nil { tx.Rollback() } }() // Garbage collecting entries which as either marked as deleted, or lack message references, or have no user assigned. query := "SELECT fu.id,fu.location FROM fileuploads AS fu LEFT JOIN filemsglinks AS fml ON fml.fileid=fu.id " + "WHERE fml.id IS NULL" var args []any if !olderThan.IsZero() { query += " AND fu.updatedat 0 { query += " LIMIT ?" args = append(args, limit) } rows, err := tx.Query(query, args...) if err != nil { return nil, err } defer rows.Close() var locations []string var ids []any for rows.Next() { var id int var loc string if err = rows.Scan(&id, &loc); err != nil { break } if loc != "" { locations = append(locations, loc) } ids = append(ids, id) } if err == nil { err = rows.Err() } if err != nil { return nil, err } if len(ids) > 0 { query, ids, _ = sqlx.In("DELETE FROM fileuploads WHERE id IN (?)", ids) _, err = tx.Exec(query, ids...) if err != nil { return nil, err } } return locations, tx.Commit() } // FileLinkAttachments connects given topic or message to the file record IDs from the list. func (a *adapter) FileLinkAttachments(topic string, userId, msgId t.Uid, fids []string) error { if len(fids) == 0 || (topic == "" && msgId.IsZero() && userId.IsZero()) { return t.ErrMalformed } now := t.TimeNow() var args []any var linkId any var linkBy string if !msgId.IsZero() { linkBy = "msgid" linkId = int64(msgId) } else if topic != "" { linkBy = "topic" linkId = topic // Only one attachment per topic is permitted at this time. fids = fids[0:1] } else { linkBy = "userid" linkId = store.DecodeUid(userId) // Only one attachment per user is permitted at this time. fids = fids[0:1] } // Decoded ids var dids []any for _, fid := range fids { id := t.ParseUid(fid) if id.IsZero() { return t.ErrMalformed } dids = append(dids, store.DecodeUid(id)) } for _, id := range dids { // createdat,fileid,[msgid|topic|userid] args = append(args, now, id, linkId) } ctx, cancel := a.getContextForTx() if cancel != nil { defer cancel() } tx, err := a.db.BeginTxx(ctx, nil) if err != nil { return err } defer func() { if err != nil { tx.Rollback() } }() // Unlink earlier uploads on the same topic or user allowing them to be garbage-collected. if msgId.IsZero() { sql := "DELETE FROM filemsglinks WHERE " + linkBy + "=?" _, err = tx.Exec(sql, linkId) if err != nil { return err } } sql := "INSERT INTO filemsglinks(createdat,fileid," + linkBy + ") VALUES (?,?,?)" _, err = tx.Exec(sql+strings.Repeat(",(?,?,?)", len(dids)-1), args...) if err != nil { return err } return tx.Commit() } // PCacheGet reads a persistet cache entry. func (a *adapter) PCacheGet(key string) (string, error) { ctx, cancel := a.getContext() if cancel != nil { defer cancel() } var value string if err := a.db.GetContext(ctx, &value, "SELECT `value` FROM kvmeta WHERE `key`=? LIMIT 1", key); err != nil { if err == sql.ErrNoRows { return "", t.ErrNotFound } return "", err } return value, nil } // PCacheUpsert creates or updates a persistent cache entry. func (a *adapter) PCacheUpsert(key string, value string, failOnDuplicate bool) error { if strings.Contains(key, "%") { // Do not allow % in keys: it interferes with LIKE query. return t.ErrMalformed } ctx, cancel := a.getContext() if cancel != nil { defer cancel() } var action string if failOnDuplicate { action = "INSERT" } else { action = "REPLACE" } _, err := a.db.ExecContext(ctx, action+" INTO kvmeta(`key`,createdat,`value`) VALUES(?,?,?)", key, t.TimeNow(), value) if isDupe(err) { return t.ErrDuplicate } return err } // PCacheDelete deletes one persistent cache entry. func (a *adapter) PCacheDelete(key string) error { ctx, cancel := a.getContext() if cancel != nil { defer cancel() } _, err := a.db.ExecContext(ctx, "DELETE FROM kvmeta WHERE `key`=?", key) return err } // PCacheExpire expires old entries with the given key prefix. func (a *adapter) PCacheExpire(keyPrefix string, olderThan time.Time) error { if keyPrefix == "" { return t.ErrMalformed } ctx, cancel := a.getContext() if cancel != nil { defer cancel() } _, err := a.db.ExecContext(ctx, "DELETE FROM kvmeta WHERE `key` LIKE ? AND createdat 0 { for _, del := range msg.DeletedFor { toDel := types.DelMessage{ Topic: msg.Topic, DeletedFor: del.User, DelId: del.DelId, SeqIdRanges: []types.Range{{Low: msg.SeqId}}, } adp.MessageDeleteList(msg.Topic, &toDel) } } } } func TestFileStartUpload(t *testing.T) { for _, f := range testData.Files { err := adp.FileStartUpload(f) if err != nil { t.Fatal(err) } } } // ================== Read tests ================================== func TestUserGet(t *testing.T) { // Test not found got, err := adp.UserGet(dummyUid1) if err == nil && got != nil { t.Error("user should be nil.") } got, err = adp.UserGet(types.ParseUserId("usr" + testData.Users[0].Id)) if err != nil { t.Fatal(err) } // User agent is not stored when creating a user. Make sure it's the same. got.UserAgent = testData.Users[0].UserAgent got.CreatedAt = testData.Users[0].CreatedAt got.UpdatedAt = testData.Users[0].UpdatedAt if !reflect.DeepEqual(got, testData.Users[0]) { t.Error(mismatchErrorString("User", got, testData.Users[0])) } } func TestUserGetAll(t *testing.T) { // Test not found (dummy UIDs). got, err := adp.UserGetAll(dummyUid1, dummyUid2) if err != nil { t.Fatal(err) } if len(got) > 0 { t.Error("result users should be zero length, got", len(got)) } got, err = adp.UserGetAll(types.ParseUserId("usr"+testData.Users[0].Id), types.ParseUserId("usr"+testData.Users[1].Id)) if err != nil { t.Fatal(err) } if len(got) != 2 { t.Fatal(mismatchErrorString("resultUsers length", len(got), 2)) } for i, usr := range got { // User agent is not compared. usr.UserAgent = testData.Users[i].UserAgent usr.CreatedAt = testData.Users[i].CreatedAt usr.UpdatedAt = testData.Users[i].UpdatedAt if !reflect.DeepEqual(&usr, testData.Users[i]) { t.Error(mismatchErrorString("User", &usr, testData.Users[i])) } } } func TestUserGetByCred(t *testing.T) { // Test not found got, err := adp.UserGetByCred("foo", "bar") if err != nil { t.Fatal(err) } if got != types.ZeroUid { t.Error("result uid should be ZeroUid") } got, _ = adp.UserGetByCred(testData.Creds[0].Method, testData.Creds[0].Value) if got != types.ParseUserId("usr"+testData.Creds[0].User) { t.Error(mismatchErrorString("Uid", got, types.ParseUserId("usr"+testData.Creds[0].User))) } } func TestCredGetActive(t *testing.T) { got, err := adp.CredGetActive(types.ParseUserId("usr"+testData.Users[2].Id), "tel") if err != nil { t.Error(err) } if !reflect.DeepEqual(got, testData.Creds[3]) { t.Error(mismatchErrorString("Credential", got, testData.Creds[3])) } // Test not found got, err = adp.CredGetActive(dummyUid1, "") if err != nil { t.Error(err) } if got != nil { t.Error("result should be nil, but got", got) } } func TestCredGetAll(t *testing.T) { got, err := adp.CredGetAll(types.ParseUserId("usr"+testData.Users[2].Id), "", false) if err != nil { t.Fatal(err) } if len(got) != 3 { t.Error(mismatchErrorString("Credentials length", len(got), 3)) } got, _ = adp.CredGetAll(types.ParseUserId("usr"+testData.Users[2].Id), "tel", false) if len(got) != 2 { t.Error(mismatchErrorString("Credentials length", len(got), 2)) } got, _ = adp.CredGetAll(types.ParseUserId("usr"+testData.Users[2].Id), "", true) if len(got) != 1 { t.Error(mismatchErrorString("Credentials length", len(got), 1)) } got, _ = adp.CredGetAll(types.ParseUserId("usr"+testData.Users[2].Id), "tel", true) if len(got) != 1 { t.Error(mismatchErrorString("Credentials length", len(got), 1)) } } func TestAuthGetUniqueRecord(t *testing.T) { uid, authLvl, secret, expires, err := adp.AuthGetUniqueRecord("basic:alice") if err != nil { t.Fatal(err) } if uid != types.ParseUserId("usr"+testData.Recs[0].UserId) || authLvl != testData.Recs[0].AuthLvl || !reflect.DeepEqual(secret, testData.Recs[0].Secret) || expires != testData.Recs[0].Expires { got := fmt.Sprintf("%v %v %v %v", uid, authLvl, secret, expires) want := fmt.Sprintf("%v %v %v %v", testData.Recs[0].UserId, testData.Recs[0].AuthLvl, testData.Recs[0].Secret, testData.Recs[0].Expires) t.Error(mismatchErrorString("Auth record", got, want)) } // Test not found uid, _, _, _, err = adp.AuthGetUniqueRecord("qwert:asdfg") if err == nil && !uid.IsZero() { t.Error("Auth record found but shouldn't. Uid:", uid.String()) } } func TestAuthGetRecord(t *testing.T) { recId, authLvl, secret, expires, err := adp.AuthGetRecord(types.ParseUserId("usr"+testData.Recs[0].UserId), "basic") if err != nil { t.Fatal(err) } if recId != testData.Recs[0].Unique || authLvl != testData.Recs[0].AuthLvl || !reflect.DeepEqual(secret, testData.Recs[0].Secret) || expires != testData.Recs[0].Expires { got := fmt.Sprintf("%v %v %v %v", recId, authLvl, secret, expires) want := fmt.Sprintf("%v %v %v %v", testData.Recs[0].Unique, testData.Recs[0].AuthLvl, testData.Recs[0].Secret, testData.Recs[0].Expires) t.Error(mismatchErrorString("Auth record", got, want)) } // Test not found recId, _, _, _, err = adp.AuthGetRecord(types.Uid(123), "scheme") if err != types.ErrNotFound { t.Error("Auth record found but shouldn't. recId:", recId) } } func TestTopicGet(t *testing.T) { got, err := adp.TopicGet(testData.Topics[0].Id) if err != nil { t.Fatal(err) } if !reflect.DeepEqual(got, testData.Topics[0]) { t.Error(mismatchErrorString("Topic", got, testData.Topics[0])) } // Test not found got, err = adp.TopicGet("asdfasdfasdf") if err != nil { t.Fatal(err) } if got != nil { t.Error("Topic should be nil but got:", got) } } func TestTopicsForUser(t *testing.T) { qOpts := types.QueryOpt{ Topic: "p2p9AVDamaNCRbfKzGSh3mE0w", Limit: 999, } gotSubs, err := adp.TopicsForUser(types.ParseUserId("usr"+testData.Users[0].Id), false, &qOpts) if err != nil { t.Fatal(err) } if len(gotSubs) != 1 { t.Error(mismatchErrorString("Subs length", len(gotSubs), 1)) } gotSubs, err = adp.TopicsForUser(types.ParseUserId("usr"+testData.Users[1].Id), true, nil) if err != nil { t.Fatal(err) } if len(gotSubs) != 2 { t.Error(mismatchErrorString("Subs length (2)", len(gotSubs), 2)) } qOpts.Topic = "" ims := testData.Now.Add(15 * time.Minute) qOpts.IfModifiedSince = &ims gotSubs, err = adp.TopicsForUser(types.ParseUserId("usr"+testData.Users[0].Id), false, &qOpts) if err != nil { t.Fatal(err) } if len(gotSubs) != 1 { t.Error(mismatchErrorString("Subs length (IMS)", len(gotSubs), 1)) } ims = time.Now().Add(15 * time.Minute) gotSubs, err = adp.TopicsForUser(types.ParseUserId("usr"+testData.Users[0].Id), false, &qOpts) if err != nil { t.Fatal(err) } if len(gotSubs) != 0 { t.Error(mismatchErrorString("Subs length (IMS 2)", len(gotSubs), 0)) } } func TestUsersForTopic(t *testing.T) { qOpts := types.QueryOpt{ User: types.ParseUserId("usr" + testData.Users[0].Id), Limit: 999, } gotSubs, err := adp.UsersForTopic("grpgRXf0rU4uR4", false, &qOpts) if err != nil { t.Fatal(err) } if len(gotSubs) != 1 { t.Error(mismatchErrorString("Subs length", len(gotSubs), 1)) } gotSubs, err = adp.UsersForTopic("grpgRXf0rU4uR4", true, nil) if err != nil { t.Fatal(err) } if len(gotSubs) != 2 { t.Error(mismatchErrorString("Subs length", len(gotSubs), 2)) } gotSubs, err = adp.UsersForTopic("p2p9AVDamaNCRbfKzGSh3mE0w", false, nil) if err != nil { t.Fatal(err) } if len(gotSubs) != 2 { t.Error(mismatchErrorString("Subs length", len(gotSubs), 2)) } } func TestOwnTopics(t *testing.T) { gotSubs, err := adp.OwnTopics(types.ParseUserId("usr" + testData.Users[0].Id)) if err != nil { t.Fatal(err) } if len(gotSubs) != 1 { t.Fatalf("Got topic length %v instead of %v", len(gotSubs), 1) } if gotSubs[0] != testData.Topics[0].Id { t.Errorf("Got topic %v instead of %v", gotSubs[0], testData.Topics[0].Id) } } func TestSubscriptionGet(t *testing.T) { got, err := adp.SubscriptionGet(testData.Topics[0].Id, types.ParseUserId("usr"+testData.Users[0].Id), false) if err != nil { t.Error(err) } if diff := cmp.Diff(got, testData.Subs[0], cmpopts.IgnoreUnexported(types.Subscription{}, types.ObjHeader{})); diff != "" { t.Error(mismatchErrorString("Subs", diff, "")) } // Test not found got, err = adp.SubscriptionGet("dummytopic", dummyUid1, false) if err != nil { t.Error(err) } if got != nil { t.Error("result sub should be nil.") } } func TestSubsForUser(t *testing.T) { gotSubs, err := adp.SubsForUser(types.ParseUserId("usr" + testData.Users[0].Id)) if err != nil { t.Error(err) } if len(gotSubs) != 2 { t.Error(mismatchErrorString("Subs length", len(gotSubs), 1)) } // Test not found gotSubs, err = adp.SubsForUser(types.ParseUserId("usr12345678")) if err != nil { t.Error(err) } if len(gotSubs) != 0 { t.Error(mismatchErrorString("Subs length", len(gotSubs), 0)) } } func TestSubsForTopic(t *testing.T) { qOpts := types.QueryOpt{ User: types.ParseUserId("usr" + testData.Users[0].Id), Limit: 999, } gotSubs, err := adp.SubsForTopic(testData.Topics[0].Id, false, &qOpts) if err != nil { t.Error(err) } if len(gotSubs) != 1 { t.Error(mismatchErrorString("Subs length", len(gotSubs), 1)) } // Test not found gotSubs, err = adp.SubsForTopic("dummytopicid", false, nil) if err != nil { t.Error(err) } if len(gotSubs) != 0 { t.Error(mismatchErrorString("Subs length", len(gotSubs), 0)) } } func TestFind(t *testing.T) { reqTags := [][]string{{"alice", "bob", "carol", "travel", "qwer", "asdf", "zxcv"}} got, err := adp.Find("usr"+testData.Users[2].Id, "", reqTags, nil, true) if err != nil { t.Error(err) } if len(got) != 3 { t.Error(mismatchErrorString("result length", len(got), 3)) } } func TestMessageGetAll(t *testing.T) { opts := types.QueryOpt{ Since: 1, Before: 2, Limit: 999, } gotMsgs, err := adp.MessageGetAll(testData.Topics[0].Id, types.ParseUserId("usr"+testData.Users[0].Id), &opts) if err != nil { t.Fatal(err) } if len(gotMsgs) != 1 { t.Error(mismatchErrorString("Messages length opts", len(gotMsgs), 1)) } gotMsgs, _ = adp.MessageGetAll(testData.Topics[0].Id, types.ParseUserId("usr"+testData.Users[0].Id), nil) if len(gotMsgs) != 2 { t.Fatalf("%+v", gotMsgs) t.Error(mismatchErrorString("Messages length no opts", len(gotMsgs), 2)) } gotMsgs, _ = adp.MessageGetAll(testData.Topics[0].Id, types.ZeroUid, nil) if len(gotMsgs) != 3 { t.Error(mismatchErrorString("Messages length zero uid", len(gotMsgs), 3)) } } func TestFileGet(t *testing.T) { // General test done during TestFileFinishUpload(). // Test not found got, err := adp.FileGet("dummyfileid") if err != nil { if got != nil { t.Error("File found but shouldn't:", got) } } } // ================== Update tests ================================ func TestUserUpdate(t *testing.T) { update := map[string]any{ "UserAgent": "Test Agent v0.11", "UpdatedAt": testData.Now.Add(30 * time.Minute), } err := adp.UserUpdate(types.ParseUserId("usr"+testData.Users[0].Id), update) if err != nil { t.Fatal(err) } var got struct { UserAgent string UpdatedAt time.Time CreatedAt time.Time } err = db.QueryRow("SELECT useragent, updatedat, createdat FROM users WHERE id=?", decodeUid(testData.Users[0].Id)). Scan(&got.UserAgent, &got.UpdatedAt, &got.CreatedAt) if err != nil { t.Fatal(err) } if got.UserAgent != "Test Agent v0.11" { t.Error(mismatchErrorString("UserAgent", got.UserAgent, "Test Agent v0.11")) } if got.UpdatedAt.Equal(got.CreatedAt) { t.Error("UpdatedAt field not updated") } } func TestUserUpdateTags(t *testing.T) { addTags := testData.Tags[0] removeTags := testData.Tags[1] resetTags := testData.Tags[2] uid := types.ParseUserId("usr" + testData.Users[0].Id) got, err := adp.UserUpdateTags(uid, addTags, nil, nil) if err != nil { t.Fatal(err) } want := []string{"alice", "tag1"} if !reflect.DeepEqual(got, want) { t.Error(mismatchErrorString("Tags", got, want)) } got, err = adp.UserUpdateTags(uid, nil, removeTags, nil) if err != nil { t.Fatal(err) } want = nil if !reflect.DeepEqual(got, want) { t.Error(mismatchErrorString("Tags", got, want)) } got, err = adp.UserUpdateTags(uid, nil, nil, resetTags) if err != nil { t.Fatal(err) } want = []string{"alice", "tag111", "tag333"} if !reflect.DeepEqual(got, want) { t.Error(mismatchErrorString("Tags", got, want)) } got, err = adp.UserUpdateTags(uid, addTags, removeTags, nil) if err != nil { t.Fatal(err) } want = []string{"tag111", "tag333"} if !reflect.DeepEqual(got, want) { t.Error(mismatchErrorString("Tags", got, want)) } got, err = adp.UserUpdateTags(uid, addTags, removeTags, nil) if err != nil { t.Fatal(err) } want = []string{"tag111", "tag333"} if !reflect.DeepEqual(got, want) { t.Error(mismatchErrorString("Tags", got, want)) } } func TestCredFail(t *testing.T) { err := adp.CredFail(types.ParseUserId("usr"+testData.Creds[3].User), "tel") if err != nil { t.Error(err) } // Check if fields updated var got struct { Retries int UpdatedAt time.Time CreatedAt time.Time } err = db.QueryRow("SELECT retries, updatedat, createdat FROM credentials WHERE userid=? AND method=? AND value=?", decodeUid(testData.Creds[3].User), "tel", testData.Creds[3].Value).Scan(&got.Retries, &got.UpdatedAt, &got.CreatedAt) if err != nil { t.Fatal(err) } if got.Retries != 1 { t.Error(mismatchErrorString("Retries count", got.Retries, 1)) } if got.UpdatedAt.Equal(got.CreatedAt) { t.Error("UpdatedAt field not updated") } } func TestCredConfirm(t *testing.T) { err := adp.CredConfirm(types.ParseUserId("usr"+testData.Creds[3].User), "tel") if err != nil { t.Fatal(err) } // Test fields are updated var got struct { UpdatedAt time.Time CreatedAt time.Time Done bool } err = db.QueryRow("SELECT updatedat, createdat, done FROM credentials WHERE userid=? AND method=? AND value=?", decodeUid(testData.Creds[3].User), "tel", testData.Creds[3].Value).Scan(&got.UpdatedAt, &got.CreatedAt, &got.Done) if err != nil { t.Fatal(err) } if got.UpdatedAt.Equal(got.CreatedAt) { t.Error("Credential not updated correctly") } if !got.Done { t.Error("Credential should be marked as done") } } func TestAuthUpdRecord(t *testing.T) { rec := testData.Recs[1] newSecret := []byte{'s', 'e', 'c', 'r', 'e', 't'} err := adp.AuthUpdRecord(types.ParseUserId("usr"+rec.UserId), rec.Scheme, rec.Unique, rec.AuthLvl, newSecret, rec.Expires) if err != nil { t.Fatal(err) } var got []byte err = db.QueryRow("SELECT secret FROM auth WHERE uname=?", rec.Unique).Scan(&got) if err != nil { t.Fatal(err) } if reflect.DeepEqual(got, rec.Secret) { t.Error(mismatchErrorString("Secret", got, rec.Secret)) } // Test with auth ID (unique) change newId := "basic:bob12345" err = adp.AuthUpdRecord(types.ParseUserId("usr"+rec.UserId), rec.Scheme, newId, rec.AuthLvl, newSecret, rec.Expires) if err != nil { t.Fatal(err) } // Test if old ID deleted var count int err = db.QueryRow("SELECT COUNT(*) FROM auth WHERE uname=?", rec.Unique).Scan(&count) if err != nil { t.Fatal(err) } if count != 0 { t.Error("Old auth record not deleted") } } func TestTopicUpdateOnMessage(t *testing.T) { msg := types.Message{ ObjHeader: types.ObjHeader{ CreatedAt: testData.Now.Add(33 * time.Minute), }, SeqId: 66, } err := adp.TopicUpdateOnMessage(testData.Topics[2].Id, &msg) if err != nil { t.Fatal(err) } var got struct { TouchedAt time.Time SeqId int } err = db.QueryRow("SELECT touchedat, seqid FROM topics WHERE name=?", testData.Topics[2].Id). Scan(&got.TouchedAt, &got.SeqId) if err != nil { t.Fatal(err) } if got.TouchedAt != msg.CreatedAt || got.SeqId != msg.SeqId { t.Error(mismatchErrorString("TouchedAt", got.TouchedAt, msg.CreatedAt)) t.Error(mismatchErrorString("SeqId", got.SeqId, msg.SeqId)) } } func TestTopicUpdate(t *testing.T) { update := map[string]any{ "UpdatedAt": testData.Now.Add(55 * time.Minute), } err := adp.TopicUpdate(testData.Topics[0].Id, update) if err != nil { t.Fatal(err) } var got time.Time err = db.QueryRow("SELECT updatedat FROM topics WHERE name=?", testData.Topics[0].Id).Scan(&got) if err != nil { t.Fatal(err) } if got != update["UpdatedAt"] { t.Error(mismatchErrorString("UpdatedAt", got, update["UpdatedAt"])) } } func TestTopicOwnerChange(t *testing.T) { err := adp.TopicOwnerChange(testData.Topics[0].Id, types.ParseUserId("usr"+testData.Users[1].Id)) if err != nil { t.Fatal(err) } var got int64 err = db.QueryRow("SELECT owner FROM topics WHERE name=?", testData.Topics[0].Id).Scan(&got) if err != nil { t.Fatal(err) } expectedOwner := decodeUid(testData.Users[1].Id) // Assuming user ID conversion if got != expectedOwner { t.Error(mismatchErrorString("Owner", got, expectedOwner)) } } func TestSubsUpdate(t *testing.T) { update := map[string]any{ "UpdatedAt": testData.Now.Add(22 * time.Minute), } err := adp.SubsUpdate(testData.Topics[0].Id, types.ParseUserId("usr"+testData.Users[0].Id), update) if err != nil { t.Fatal(err) } var got time.Time err = db.QueryRow("SELECT updatedat FROM subscriptions WHERE topic=? AND userid=?", testData.Topics[0].Id, decodeUid(testData.Users[0].Id)).Scan(&got) if err != nil { t.Fatal(err) } if got != update["UpdatedAt"] { t.Error(mismatchErrorString("UpdatedAt", got, update["UpdatedAt"])) } err = adp.SubsUpdate(testData.Topics[1].Id, types.ZeroUid, update) if err != nil { t.Fatal(err) } err = db.QueryRow("SELECT updatedat FROM subscriptions WHERE topic=? LIMIT 1", testData.Topics[1].Id).Scan(&got) if err != nil { t.Fatal(err) } if got != update["UpdatedAt"] { t.Error(mismatchErrorString("UpdatedAt", got, update["UpdatedAt"])) } } func TestSubsDelete(t *testing.T) { err := adp.SubsDelete(testData.Topics[1].Id, types.ParseUserId("usr"+testData.Users[0].Id)) if err != nil { t.Fatal(err) } var deletedat sql.NullTime err = db.QueryRow("SELECT deletedat FROM subscriptions WHERE topic=? AND userid=?", testData.Topics[1].Id, decodeUid(testData.Users[0].Id)).Scan(&deletedat) if err != nil { t.Fatal(err) } if !deletedat.Valid { t.Error("DeletedAt should not be null") } } func TestDeviceUpsert(t *testing.T) { err := adp.DeviceUpsert(types.ParseUserId("usr"+testData.Users[0].Id), testData.Devs[0]) if err != nil { t.Fatal(err) } var got struct { DeviceId string Platform string } err = db.QueryRow("SELECT deviceid, platform FROM devices WHERE userid=? LIMIT 1", decodeUid(testData.Users[0].Id)).Scan(&got.DeviceId, &got.Platform) if err != nil { t.Fatal(err) } if got.DeviceId != testData.Devs[0].DeviceId || got.Platform != testData.Devs[0].Platform { t.Error(mismatchErrorString("Device", got, testData.Devs[0])) } // Test update testData.Devs[0].Platform = "Web" err = adp.DeviceUpsert(types.ParseUserId("usr"+testData.Users[0].Id), testData.Devs[0]) if err != nil { t.Fatal(err) } err = db.QueryRow("SELECT platform FROM devices WHERE userid=? AND deviceid=?", decodeUid(testData.Users[0].Id), testData.Devs[0].DeviceId).Scan(&got.Platform) if err != nil { t.Fatal(err) } if got.Platform != "Web" { t.Error("Device not updated.", got.Platform) } // Test add same device to another user err = adp.DeviceUpsert(types.ParseUserId("usr"+testData.Users[1].Id), testData.Devs[0]) if err != nil { t.Fatal(err) } err = db.QueryRow("SELECT platform FROM devices WHERE userid=? AND deviceid=?", decodeUid(testData.Users[1].Id), testData.Devs[0].DeviceId).Scan(&got.Platform) if err != nil { t.Fatal(err) } if got.Platform != "Web" { t.Error("Device not updated.", got.Platform) } err = adp.DeviceUpsert(types.ParseUserId("usr"+testData.Users[2].Id), testData.Devs[1]) if err != nil { t.Error(err) } } func TestFileFinishUpload(t *testing.T) { got, err := adp.FileFinishUpload(testData.Files[0], true, 22222) if err != nil { t.Fatal(err) } if got.Status != types.UploadCompleted { t.Error(mismatchErrorString("Status", got.Status, types.UploadCompleted)) } if got.Size != 22222 { t.Error(mismatchErrorString("Size", got.Size, 22222)) } } func TestMessageAttachments(t *testing.T) { fids := []string{testData.Files[0].Id, testData.Files[1].Id} err := adp.FileLinkAttachments("", types.ZeroUid, types.ParseUid(testData.Msgs[1].Id), fids) if err != nil { t.Fatal(err) } // Check if attachments were linked (this would require checking filemsglinks table) var count int if err = db.QueryRow("SELECT COUNT(*) FROM filemsglinks WHERE msgid=?", types.ParseUid(testData.Msgs[1].Id)).Scan(&count); err != nil { t.Fatal(err) } if count != len(fids) { t.Error(mismatchErrorString("Attachments count", count, len(fids))) } } // ================== Other tests ================================= func TestDeviceGetAll(t *testing.T) { uid0 := types.ParseUserId("usr" + testData.Users[0].Id) uid1 := types.ParseUserId("usr" + testData.Users[1].Id) uid2 := types.ParseUserId("usr" + testData.Users[2].Id) gotDevs, count, err := adp.DeviceGetAll(uid0, uid1, uid2) if err != nil { t.Fatal(err) } if count != 2 { t.Fatal(mismatchErrorString("count", count, 2)) } if !reflect.DeepEqual(gotDevs[uid1][0], *testData.Devs[0]) { t.Error(mismatchErrorString("Device", gotDevs[uid1][0], *testData.Devs[0])) } if !reflect.DeepEqual(gotDevs[uid2][0], *testData.Devs[1]) { t.Error(mismatchErrorString("Device", gotDevs[uid2][0], *testData.Devs[1])) } } func TestDeviceDelete(t *testing.T) { err := adp.DeviceDelete(types.ParseUserId("usr"+testData.Users[1].Id), testData.Devs[0].DeviceId) if err != nil { t.Fatal(err) } var count int err = db.QueryRow("SELECT COUNT(*) FROM devices WHERE userid=?", testData.Users[1].Id).Scan(&count) if err != nil { t.Fatal(err) } if count != 0 { t.Error("Device not deleted:", count) } err = adp.DeviceDelete(types.ParseUserId("usr"+testData.Users[2].Id), "") if err != nil { t.Fatal(err) } err = db.QueryRow("SELECT COUNT(*) FROM devices WHERE userid=?", testData.Users[2].Id).Scan(&count) if err != nil { t.Fatal(err) } if count != 0 { t.Error("Device not deleted:", count) } } // ================== Persistent Cache tests ====================== func TestPCacheUpsert(t *testing.T) { err := adp.PCacheUpsert("test_key", "test_value", false) if err != nil { t.Fatal(err) } // Test duplicate with failOnDuplicate = true err = adp.PCacheUpsert("test_key2", "test_value2", true) if err != nil { t.Fatal(err) } err = adp.PCacheUpsert("test_key2", "new_value", true) if err != types.ErrDuplicate { t.Error("Expected duplicate error") } } func TestPCacheGet(t *testing.T) { value, err := adp.PCacheGet("test_key") if err != nil { t.Fatal(err) } if value != "test_value" { t.Error(mismatchErrorString("Cache value", value, "test_value")) } // Test not found _, err = adp.PCacheGet("nonexistent") if err != types.ErrNotFound { t.Error("Expected not found error") } } func TestPCacheDelete(t *testing.T) { err := adp.PCacheDelete("test_key") if err != nil { t.Fatal(err) } // Verify deleted _, err = adp.PCacheGet("test_key") if err != types.ErrNotFound { t.Error("Key should be deleted") } } func TestPCacheExpire(t *testing.T) { // Insert some test keys with prefix adp.PCacheUpsert("prefix_key1", "value1", false) adp.PCacheUpsert("prefix_key2", "value2", false) // Expire keys older than now (should delete all test keys) err := adp.PCacheExpire("prefix_", time.Now().Add(1*time.Minute)) if err != nil { t.Fatal(err) } } // ================== Delete tests ================================ func TestCredDel(t *testing.T) { err := adp.CredDel(types.ParseUserId("usr"+testData.Users[0].Id), "email", "alice@test.example.com") if err != nil { t.Fatal(err) } var count int err = db.QueryRow("SELECT COUNT(*) FROM credentials WHERE method='email' AND value='alice@test.example.com'").Scan(&count) if err != nil { t.Fatal(err) } if count != 0 { t.Error("Got result but shouldn't", count) } err = adp.CredDel(types.ParseUserId("usr"+testData.Users[1].Id), "", "") if err != nil { t.Fatal(err) } err = db.QueryRow("SELECT COUNT(*) FROM credentials WHERE userid=?", testData.Users[1].Id).Scan(&count) if err != nil { t.Fatal(err) } if count != 0 { t.Error("Got result but shouldn't", count) } } func TestAuthDelScheme(t *testing.T) { // tested during TestAuthUpdRecord } func TestAuthDelAllRecords(t *testing.T) { delCount, err := adp.AuthDelAllRecords(types.ParseUserId("usr" + testData.Recs[0].UserId)) if err != nil { t.Fatal(err) } if delCount != 1 { t.Error(mismatchErrorString("delCount", delCount, 1)) } // With dummy user delCount, _ = adp.AuthDelAllRecords(dummyUid1) if delCount != 0 { t.Error(mismatchErrorString("delCount", delCount, 0)) } } func TestSubsDelForUser(t *testing.T) { // Tested during TestUserDelete (both hard and soft deletions) } func TestMessageDeleteList(t *testing.T) { toDel := types.DelMessage{ ObjHeader: types.ObjHeader{ Id: testData.UGen.GetStr(), CreatedAt: testData.Now, UpdatedAt: testData.Now, }, Topic: testData.Topics[1].Id, DeletedFor: testData.Users[2].Id, DelId: 1, SeqIdRanges: []types.Range{{Low: 9}, {Low: 3, Hi: 7}}, } err := adp.MessageDeleteList(toDel.Topic, &toDel) if err != nil { t.Fatal(err) } // Check messages in dellog var count int err = db.QueryRow("SELECT COUNT(*) FROM dellog WHERE topic=? AND deletedfor=?", toDel.Topic, decodeUid(toDel.DeletedFor)).Scan(&count) if err != nil { t.Fatal(err) } if count == 0 { t.Error("No dellog entries created") } // Hard delete test toDel = types.DelMessage{ ObjHeader: types.ObjHeader{ Id: testData.UGen.GetStr(), CreatedAt: testData.Now, UpdatedAt: testData.Now, }, Topic: testData.Topics[0].Id, DelId: 3, SeqIdRanges: []types.Range{{Low: 1, Hi: 3}}, } err = adp.MessageDeleteList(toDel.Topic, &toDel) if err != nil { t.Fatal(err) } // Check if messages content was cleared err = db.QueryRow("SELECT COUNT(*) FROM messages WHERE topic=? AND content IS NOT NULL", toDel.Topic).Scan(&count) if err != nil { t.Fatal(err) } if count > 1 { t.Errorf("Messages not properly deleted %d, %s", count, toDel.Topic) } err = adp.MessageDeleteList(testData.Topics[0].Id, nil) if err != nil { t.Fatal(err) } err = db.QueryRow("SELECT COUNT(*) FROM messages WHERE topic=?", testData.Topics[0].Id).Scan(&count) if err != nil { t.Fatal(err) } if count != 0 { t.Error("Result should be empty:", count) } } func TestTopicDelete(t *testing.T) { err := adp.TopicDelete(testData.Topics[1].Id, false, false) if err != nil { t.Fatal(err) } var state int err = db.QueryRow("SELECT state FROM topics WHERE name=?", testData.Topics[1].Id).Scan(&state) if err != nil { t.Fatal(err) } if state != int(types.StateDeleted) { t.Error("Soft delete failed:", state) } err = adp.TopicDelete(testData.Topics[0].Id, false, true) if err != nil { t.Fatal(err) } var count int err = db.QueryRow("SELECT COUNT(*) FROM topics WHERE name=?", testData.Topics[0].Id).Scan(&count) if err != nil { t.Fatal(err) } if count != 0 { t.Error("Hard delete failed:", count) } } func TestFileDeleteUnused(t *testing.T) { locs, err := adp.FileDeleteUnused(time.Now().Add(1*time.Minute), 999) if err != nil { t.Fatal(err) } if len(locs) != 2 { t.Error(mismatchErrorString("Locations length", len(locs), 2)) } } func TestUserDelete(t *testing.T) { err := adp.UserDelete(types.ParseUserId("usr"+testData.Users[0].Id), false) if err != nil { t.Fatal(err) } var state int err = db.QueryRow("SELECT state FROM users WHERE id=?", decodeUid(testData.Users[0].Id)).Scan(&state) if err != nil { t.Fatal(err) } if state != int(types.StateDeleted) { t.Error("User soft delete failed", state) } err = adp.UserDelete(types.ParseUserId("usr"+testData.Users[1].Id), true) if err != nil { t.Fatal(err) } var count int err = db.QueryRow("SELECT COUNT(*) FROM users WHERE id=?", testData.Users[1].Id).Scan(&count) if err != nil { t.Fatal(err) } if count != 0 { t.Error("User hard delete failed") } } // ================== Other tests ================================= func TestUserUnreadCount(t *testing.T) { uids := []types.Uid{ types.ParseUserId("usr" + testData.Users[1].Id), types.ParseUserId("usr" + testData.Users[2].Id), } expected := map[types.Uid]int{uids[0]: 0, uids[1]: 166} counts, err := adp.UserUnreadCount(uids...) if err != nil { t.Fatal(err) } if len(counts) != 2 { t.Error(mismatchErrorString("UnreadCount length", len(counts), 2)) } for uid, unread := range counts { if expected[uid] != unread { t.Error(mismatchErrorString("UnreadCount", unread, expected[uid])) } } // Test not found (even if the account is not found, the call must return one record). counts, err = adp.UserUnreadCount(dummyUid1) if err != nil { t.Fatal(err) } if len(counts) != 1 { t.Error(mismatchErrorString("UnreadCount length (dummy)", len(counts), 1)) } if counts[dummyUid1] != 0 { t.Error(mismatchErrorString("Non-zero UnreadCount (dummy)", counts[dummyUid1], 0)) } } func TestMessageGetDeleted(t *testing.T) { qOpts := types.QueryOpt{ Since: 1, Before: 10, Limit: 999, } got, err := adp.MessageGetDeleted(testData.Topics[1].Id, types.ParseUserId("usr"+testData.Users[2].Id), &qOpts) if err != nil { t.Fatal(err) } if len(got) != 1 { t.Error(mismatchErrorString("result length", len(got), 1)) } } // ================================================================ func mismatchErrorString(key string, got, want any) string { return fmt.Sprintf("%s mismatch:\nGot = %+v\nWant = %+v", key, got, want) } func init() { logs.Init(os.Stderr, "stdFlags") adp = backend.GetTestAdapter() conffile := flag.String("config", "./test.conf", "config of the database connection") if file, err := os.Open(*conffile); err != nil { log.Fatal("Failed to read config file:", err) } else if err = json.NewDecoder(jcr.New(file)).Decode(&config); err != nil { log.Fatal("Failed to parse config file:", err) } if adp == nil { log.Fatal("Database adapter is missing") } if adp.IsOpen() { log.Print("Connection is already opened") } err := adp.Open(config.Adapters[adp.GetName()]) if err != nil { log.Fatal(err) } db = adp.GetTestDB().(*sqlx.DB) testData = test_data.InitTestData() if testData == nil { log.Fatal("Failed to initialize test data") } store.SetTestUidGenerator(*testData.UGen) } ================================================ FILE: server/db/mysql/tests/test.conf ================================================ { "reset_db_data": true, "adapters": { "mysql": { "dsn": "root@tcp(localhost:3306)/tinode_test?parseTime=true&collation=utf8mb4_unicode_ci", // Name of the main database. "database": "tinode_test" } } } ================================================ FILE: server/db/postgres/adapter.go ================================================ //go:build postgres // +build postgres // Package postgres is a database adapter for PostgreSQL. package postgres import ( "context" "encoding/json" "errors" "fmt" "hash/fnv" "log" "net/url" "reflect" "sort" "strconv" "strings" "time" "github.com/jackc/pgconn" "github.com/jackc/pgx/v4" "github.com/jackc/pgx/v4/pgxpool" "github.com/jmoiron/sqlx" "github.com/tinode/chat/server/auth" "github.com/tinode/chat/server/db/common" "github.com/tinode/chat/server/store" t "github.com/tinode/chat/server/store/types" ) // adapter holds MySQL connection data. type adapter struct { db *pgxpool.Pool poolConfig *pgxpool.Config dsn string dbName string // Maximum number of records to return maxResults int // Maximum number of message records to return maxMessageResults int version int // Single query timeout. sqlTimeout time.Duration // DB transaction timeout. txTimeout time.Duration } const ( adpVersion = 116 adapterName = "postgres" defaultMaxResults = 1024 // This is capped by the Session's send queue limit (128). defaultMaxMessageResults = 100 // If DB request timeout is specified, // we allocate txTimeoutMultiplier times more time for transactions. txTimeoutMultiplier = 1.5 ) type configType struct { // DB connection settings: // Using fields User string `json:"user,omitempty"` Passwd string `json:"passwd,omitempty"` Host string `json:"host,omitempty"` Port string `json:"port,omitempty"` DBName string `json:"dbname,omitempty"` // Deprecated. DSN string `json:"dsn,omitempty"` // Connection pool settings. // // Maximum number of open connections to the database. MaxOpenConns int `json:"max_open_conns,omitempty"` // Maximum number of connections in the idle connection pool. MaxIdleConns int `json:"max_idle_conns,omitempty"` // Maximum amount of time a connection may be reused (in seconds). ConnMaxLifetime int `json:"conn_max_lifetime,omitempty"` // SSL mode determines how SSL connections are handled. // Supported values: // - "disable": No SSL connection (default) // - "require": Require SSL connection but don't verify server certificate // - "verify-ca": Require SSL and verify that the server certificate is issued by a trusted CA // - "verify-full": Require SSL and verify that the server certificate matches the server hostname // - "prefer": Try SSL first, fallback to non-SSL if SSL fails // - "allow": Try non-SSL first, fallback to SSL if non-SSL fails SSLMode string `json:"ssl_mode,omitempty"` // DB request timeout (in seconds). // If 0 (or negative), no timeout is applied. SqlTimeout int `json:"sql_timeout,omitempty"` } func (a *adapter) getContext() (context.Context, context.CancelFunc) { if a.sqlTimeout > 0 { return context.WithTimeout(context.Background(), a.sqlTimeout) } return context.Background(), nil } func (a *adapter) getContextForTx() (context.Context, context.CancelFunc) { if a.txTimeout > 0 { return context.WithTimeout(context.Background(), a.txTimeout) } return context.Background(), nil } // Open initializes database session func (a *adapter) Open(jsonconfig json.RawMessage) error { if a.db != nil { return errors.New("postgres adapter is already connected") } if len(jsonconfig) < 2 { return errors.New("postgres adapter missing config") } var err error var config configType ctx := context.Background() if err = json.Unmarshal(jsonconfig, &config); err != nil { return errors.New("postgres adapter failed to parse config: " + err.Error()) } if config.DSN != "" { a.dsn = config.DSN if uri, err := url.Parse(a.dsn); err == nil { a.dbName = strings.TrimPrefix(uri.Path, "/") } else { return err } } else { if a.dsn, err = setConnStr(config); err != nil { return err } a.dbName = config.DBName } if a.maxResults <= 0 { a.maxResults = defaultMaxResults } if a.maxMessageResults <= 0 { a.maxMessageResults = defaultMaxMessageResults } if a.poolConfig, err = pgxpool.ParseConfig(a.dsn); err != nil { return errors.New("postgres adapter failed to parse DSN: " + err.Error()) } // ConnectConfig creates a new Pool and immediately establishes one connection. a.db, err = pgxpool.ConnectConfig(ctx, a.poolConfig) if isMissingDb(err) { // Missing DB is OK if we are initializing the database. // Since tinode DB does not exist, connect without specifying the DB name. a.poolConfig.ConnConfig.Database = "" a.db, err = pgxpool.ConnectConfig(ctx, a.poolConfig) } if err != nil { return err } // Actually opening the network connection if one was not opened earlier. if a.poolConfig.LazyConnect { err = a.db.Ping(ctx) } if err == nil { if config.MaxOpenConns > 0 { a.poolConfig.MaxConns = int32(config.MaxOpenConns) } if config.MaxIdleConns > 0 { a.poolConfig.MinConns = int32(config.MaxIdleConns) } if config.ConnMaxLifetime > 0 { a.poolConfig.MaxConnLifetime = time.Duration(config.ConnMaxLifetime) * time.Second } if config.SqlTimeout > 0 { a.sqlTimeout = time.Duration(config.SqlTimeout) * time.Second // We allocate txTimeoutMultiplier times sqlTimeout for transactions. a.txTimeout = time.Duration(float64(config.SqlTimeout)*txTimeoutMultiplier) * time.Second } } return err } // Close closes the underlying database connection func (a *adapter) Close() error { if a.db != nil { a.db.Close() a.db = nil a.version = -1 } return nil } // IsOpen returns true if connection to database has been established. It does not check if // connection is actually live. func (a *adapter) IsOpen() bool { return a.db != nil } // GetDbVersion returns current database version. func (a *adapter) GetDbVersion() (int, error) { if a.version > 0 { return a.version, nil } ctx, cancel := a.getContext() if cancel != nil { defer cancel() } var vers string err := a.db.QueryRow(ctx, "SELECT value FROM kvmeta WHERE key='version'").Scan(&vers) if err != nil { if isMissingDb(err) || isMissingTable(err) || err == pgx.ErrNoRows { err = errors.New("Database not initialized") } return -1, err } a.version, _ = strconv.Atoi(vers) return a.version, nil } func (a *adapter) updateDbVersion(v int) error { ctx, cancel := a.getContext() if cancel != nil { defer cancel() } a.version = -1 if _, err := a.db.Exec(ctx, `UPDATE kvmeta SET "value"=$1 WHERE "key"='version'`, strconv.Itoa(v)); err != nil { return err } return nil } // CheckDbVersion checks whether the actual DB version matches the expected version of this adapter. func (a *adapter) CheckDbVersion() error { version, err := a.GetDbVersion() if err != nil { return err } if version != adpVersion { return errors.New("Invalid database version " + strconv.Itoa(version) + ". Expected " + strconv.Itoa(adpVersion)) } return nil } // Version returns adapter version. func (adapter) Version() int { return adpVersion } // DB connection stats object. func (a *adapter) Stats() any { if a.db == nil { return nil } return a.db.Stat() } // GetName returns string that adapter uses to register itself with store. func (a *adapter) GetName() string { return adapterName } // SetMaxResults configures how many results can be returned in a single DB call. func (a *adapter) SetMaxResults(val int) error { if val <= 0 { a.maxResults = defaultMaxResults } else { a.maxResults = val } return nil } // CreateDb initializes the storage. func (a *adapter) CreateDb(reset bool) error { var err error var tx pgx.Tx ctx, cancel := a.getContext() if cancel != nil { defer cancel() } // Can't use an existing connection because it's configured with a database name which may not exist. // Don't care if it does not close cleanly. if a.db != nil { a.db.Close() } // Create default database name a.poolConfig.ConnConfig.Database = "postgres" a.db, err = pgxpool.ConnectConfig(ctx, a.poolConfig) if err != nil { return err } if reset { if _, err = a.db.Exec(ctx, fmt.Sprintf("DROP DATABASE IF EXISTS %s;", a.dbName)); err != nil { return err } } if _, err = a.db.Exec(ctx, fmt.Sprintf("CREATE DATABASE %s WITH ENCODING utf8;", a.dbName)); err != nil { return err } a.poolConfig.ConnConfig.Database = a.dbName a.db, err = pgxpool.ConnectConfig(ctx, a.poolConfig) if err != nil { return err } if tx, err = a.db.Begin(ctx); err != nil { return err } defer func() { if err != nil { // FIXME: This is useless: MySQL auto-commits on every CREATE TABLE. // Maybe DROP DATABASE instead. tx.Rollback(ctx) } }() // Indexed users. if _, err := tx.Exec(ctx, `CREATE TABLE users( id BIGINT NOT NULL, createdat TIMESTAMP(3) NOT NULL, updatedat TIMESTAMP(3) NOT NULL, state SMALLINT NOT NULL DEFAULT 0, stateat TIMESTAMP(3), access JSON, lastseen TIMESTAMP, useragent VARCHAR(255) DEFAULT '', public JSON, trusted JSON, tags JSON, PRIMARY KEY(id) ); CREATE INDEX users_state_stateat ON users(state, stateat); CREATE INDEX users_lastseen_updatedat ON users(lastseen, updatedat);`); err != nil { return err } // Indexed user tags. if _, err = tx.Exec(ctx, `CREATE TABLE usertags( id SERIAL NOT NULL, userid BIGINT NOT NULL, tag VARCHAR(96) NOT NULL, PRIMARY KEY(id), FOREIGN KEY(userid) REFERENCES users(id) ); CREATE INDEX usertags_tag ON usertags(tag); CREATE UNIQUE INDEX usertags_userid_tag ON usertags(userid, tag);`); err != nil { return err } // Indexed devices. Normalized into a separate table. if _, err = tx.Exec(ctx, `CREATE TABLE devices( id SERIAL NOT NULL, userid BIGINT NOT NULL, hash CHAR(16) NOT NULL, deviceid TEXT NOT NULL, platform VARCHAR(32), lastseen TIMESTAMP NOT NULL, lang VARCHAR(8), PRIMARY KEY(id), FOREIGN KEY(userid) REFERENCES users(id) ); CREATE UNIQUE INDEX devices_hash ON devices(hash);`); err != nil { return err } // Authentication records for the basic authentication scheme. if _, err = tx.Exec(ctx, `CREATE TABLE auth( id SERIAL NOT NULL, uname VARCHAR(32) NOT NULL, userid BIGINT NOT NULL, scheme VARCHAR(16) NOT NULL, authlvl INT NOT NULL, secret VARCHAR(255) NOT NULL, expires TIMESTAMP, PRIMARY KEY(id), FOREIGN KEY(userid) REFERENCES users(id) ); CREATE UNIQUE INDEX auth_userid_scheme ON auth(userid, scheme); CREATE UNIQUE INDEX auth_uname ON auth(uname);`); err != nil { return err } // Topics if _, err = tx.Exec(ctx, `CREATE TABLE topics( id SERIAL NOT NULL, createdat TIMESTAMP(3) NOT NULL, updatedat TIMESTAMP(3) NOT NULL, state SMALLINT NOT NULL DEFAULT 0, stateat TIMESTAMP(3), touchedat TIMESTAMP(3), name VARCHAR(25) NOT NULL, usebt BOOLEAN DEFAULT FALSE, owner BIGINT NOT NULL DEFAULT 0, access JSON, seqid INT NOT NULL DEFAULT 0, delid INT DEFAULT 0, subcnt INT DEFAULT 0, public JSON, trusted JSON, tags JSON, aux JSON, PRIMARY KEY(id) ); CREATE UNIQUE INDEX topics_name ON topics(name); CREATE INDEX topics_owner ON topics(owner); CREATE INDEX topics_state_stateat ON topics(state, stateat); CREATE INDEX topics_name_state_seqid ON topics(name, state, seqid);`); err != nil { return err } // Create system topic 'sys'. if err = createSystemTopic(tx); err != nil { return err } // Indexed topic tags. if _, err = tx.Exec(ctx, `CREATE TABLE topictags( id SERIAL NOT NULL, topic VARCHAR(25) NOT NULL, tag VARCHAR(96) NOT NULL, PRIMARY KEY(id), FOREIGN KEY(topic) REFERENCES topics(name) ); CREATE INDEX topictags_tag ON topictags(tag); CREATE UNIQUE INDEX topictags_topic_tag ON topictags(topic, tag);`); err != nil { return err } // Subscriptions if _, err = tx.Exec(ctx, `CREATE TABLE subscriptions( id SERIAL NOT NULL, createdat TIMESTAMP(3) NOT NULL, updatedat TIMESTAMP(3) NOT NULL, deletedat TIMESTAMP(3), userid BIGINT NOT NULL, topic VARCHAR(25) NOT NULL, delid INT DEFAULT 0, recvseqid INT DEFAULT 0, readseqid INT DEFAULT 0, modewant VARCHAR(8), modegiven VARCHAR(8), private JSON, PRIMARY KEY(id), FOREIGN KEY(userid) REFERENCES users(id) ); CREATE UNIQUE INDEX subscriptions_topic_userid ON subscriptions(topic, userid); CREATE INDEX subscriptions_topic ON subscriptions(topic); CREATE INDEX subscriptions_deletedat ON subscriptions(deletedat); CREATE INDEX subscriptions_userid_topic_deletedat ON subscriptions(userid, topic, deletedat);`); err != nil { return err } // Messages if _, err = tx.Exec(ctx, `CREATE TABLE messages( id SERIAL NOT NULL, createdat TIMESTAMP(3) NOT NULL, updatedat TIMESTAMP(3) NOT NULL, deletedat TIMESTAMP(3), delid INT DEFAULT 0, seqid INT NOT NULL, topic VARCHAR(25) NOT NULL, "from" BIGINT NOT NULL, head JSON, content JSON, PRIMARY KEY(id), FOREIGN KEY(topic) REFERENCES topics(name) ); CREATE UNIQUE INDEX messages_topic_seqid ON messages(topic, seqid);`); err != nil { return err } // Deletion log if _, err = tx.Exec(ctx, `CREATE TABLE dellog( id SERIAL NOT NULL, topic VARCHAR(25) NOT NULL, deletedfor BIGINT NOT NULL DEFAULT 0, delid INT NOT NULL, low INT NOT NULL, hi INT NOT NULL, PRIMARY KEY(id), FOREIGN KEY(topic) REFERENCES topics(name) ); CREATE INDEX dellog_topic_delid_deletedfor ON dellog(topic,delid,deletedfor); CREATE INDEX dellog_topic_deletedfor_low_hi ON dellog(topic,deletedfor,low,hi); CREATE INDEX dellog_deletedfor ON dellog(deletedfor);`); err != nil { return err } // User credentials if _, err = tx.Exec(ctx, `CREATE TABLE credentials( id SERIAL NOT NULL, createdat TIMESTAMP(3) NOT NULL, updatedat TIMESTAMP(3) NOT NULL, deletedat TIMESTAMP(3), method VARCHAR(16) NOT NULL, value VARCHAR(128) NOT NULL, synthetic VARCHAR(192) NOT NULL, userid BIGINT NOT NULL, resp VARCHAR(255), done BOOLEAN NOT NULL DEFAULT FALSE, retries INT NOT NULL DEFAULT 0, PRIMARY KEY(id), FOREIGN KEY(userid) REFERENCES users(id) ); CREATE UNIQUE INDEX credentials_uniqueness ON credentials(synthetic);`); err != nil { return err } // Records of uploaded files. // Don't add FOREIGN KEY on userid. It's not needed and it will break user deletion. if _, err = tx.Exec(ctx, `CREATE TABLE fileuploads( id BIGINT NOT NULL, createdat TIMESTAMP(3) NOT NULL, updatedat TIMESTAMP(3) NOT NULL, userid BIGINT, status INT NOT NULL, mimetype VARCHAR(255) NOT NULL, size BIGINT NOT NULL, etag VARCHAR(128), location VARCHAR(2048) NOT NULL, PRIMARY KEY(id) ); CREATE INDEX fileuploads_status ON fileuploads(status);`); err != nil { return err } // Links between uploaded files and the topics, users or messages they are attached to. if _, err = tx.Exec(ctx, `CREATE TABLE filemsglinks( id SERIAL NOT NULL, createdat TIMESTAMP(3) NOT NULL, fileid BIGINT NOT NULL, msgid INT, topic VARCHAR(25), userid BIGINT, PRIMARY KEY(id), FOREIGN KEY(fileid) REFERENCES fileuploads(id) ON DELETE CASCADE, FOREIGN KEY(msgid) REFERENCES messages(id) ON DELETE CASCADE, FOREIGN KEY(topic) REFERENCES topics(name) ON DELETE CASCADE, FOREIGN KEY(userid) REFERENCES users(id) ON DELETE CASCADE );`); err != nil { return err } if _, err = tx.Exec(ctx, `CREATE TABLE kvmeta( "key" VARCHAR(64) NOT NULL, createdat TIMESTAMP(3), "value" TEXT, PRIMARY KEY("key") ); CREATE INDEX kvmeta_createdat_key ON kvmeta(createdat, "key");`); err != nil { return err } if _, err = tx.Exec(ctx, `INSERT INTO kvmeta("key", "value") VALUES($1, $2)`, "version", strconv.Itoa(adpVersion)); err != nil { return err } return tx.Commit(ctx) } // UpgradeDb upgrades the database, if necessary. func (a *adapter) UpgradeDb() error { bumpVersion := func(a *adapter, x int) error { if err := a.updateDbVersion(x); err != nil { return err } _, err := a.GetDbVersion() return err } if _, err := a.GetDbVersion(); err != nil { return err } ctx, cancel := a.getContext() if cancel != nil { defer cancel() } if a.version == 112 { // Perform database upgrade from version 112 to version 113. // Index for deleting unvalidated accounts. if _, err := a.db.Exec(ctx, "CREATE INDEX users_lastseen_updatedat ON users(lastseen,updatedat)"); err != nil { return err } // Allow lnger kvmeta keys. if _, err := a.db.Exec(ctx, `ALTER TABLE kvmeta ALTER COLUMN "key" TYPE VARCHAR(64)`); err != nil { return err } if _, err := a.db.Exec(ctx, `ALTER TABLE kvmeta ALTER COLUMN "key" SET NOT NULL`); err != nil { return err } // Add timestamp to kvmeta. if _, err := a.db.Exec(ctx, `ALTER TABLE kvmeta ADD COLUMN createdat TIMESTAMP(3)`); err != nil { return err } // Add compound index on the new field and key (could be searched by key prefix). if _, err := a.db.Exec(ctx, `CREATE INDEX kvmeta_createdat_key ON kvmeta(createdat, "key")`); err != nil { return err } if err := bumpVersion(a, 113); err != nil { return err } } if a.version == 113 { // Perform database upgrade from version 113 to version 114. if _, err := a.db.Exec(ctx, "ALTER TABLE topics ADD COLUMN aux JSON"); err != nil { return err } if _, err := a.db.Exec(ctx, "ALTER TABLE fileuploads ADD COLUMN etag VARCHAR(128)"); err != nil { return err } if err := bumpVersion(a, 114); err != nil { return err } } if a.version == 114 { // Perform database upgrade from version 114 to version 115. // Find relevant subscriptions for given users efficiently, and use the join key too. if _, err := a.db.Exec(ctx, "CREATE INDEX idx_subs_user_topic_del ON subscriptions(userid, topic, deletedat)"); err != nil { return err } // Optimizes join; state filters; seqid supports the SUM operation. if _, err := a.db.Exec(ctx, "CREATE INDEX idx_topics_name_state_seqid ON topics(name, state, seqid)"); err != nil { return err } if err := bumpVersion(a, 115); err != nil { return err } } if a.version == 115 { // Perform database upgrade from version 115 to version 116. // Add subscriber count column to the topics table. if _, err := a.db.Exec(ctx, "ALTER TABLE topics ADD subcnt INT DEFAULT 0"); err != nil { return err } if err := bumpVersion(a, 116); err != nil { return err } } if a.version != adpVersion { return errors.New("Failed to perform database upgrade to version " + strconv.Itoa(adpVersion) + ". DB is still at " + strconv.Itoa(a.version)) } return nil } func createSystemTopic(tx pgx.Tx) error { now := t.TimeNow() query := `INSERT INTO topics(createdat,updatedat,state,touchedat,name,access,public) VALUES($1,$2,$3,$4,'sys','{"Auth": "N","Anon": "N"}','{"fn": "System"}')` _, err := tx.Exec(context.Background(), query, now, now, t.StateOK, now) return err } func addTags(ctx context.Context, tx pgx.Tx, table, keyName string, keyVal any, tags []string, ignoreDups bool) error { if len(tags) == 0 { return nil } //addTags(ctx, tx, "usertags", "userid", decoded_uid, add, reset == nil) sql := "INSERT INTO " + table + " (" + keyName + ",tag) VALUES($1,$2)" if ignoreDups { sql += " ON CONFLICT DO NOTHING" } for _, tag := range tags { if _, err := tx.Exec(ctx, sql, keyVal, tag); err != nil { if isDupe(err) { return t.ErrDuplicate } return err } } return nil } func removeTags(ctx context.Context, tx pgx.Tx, table, keyName string, keyVal any, tags []string) error { if len(tags) == 0 { return nil } sql, args := expandQuery("DELETE FROM "+table+" WHERE "+keyName+"=? AND tag IN (?)", keyVal, tags) _, err := tx.Exec(ctx, sql, args...) return err } // UserCreate creates a new user. Returns error and true if error is due to duplicate user name, // false for any other error func (a *adapter) UserCreate(user *t.User) error { ctx, cancel := a.getContextForTx() if cancel != nil { defer cancel() } tx, err := a.db.BeginTx(ctx, pgx.TxOptions{}) if err != nil { return err } defer func() { if err != nil { tx.Rollback(ctx) } }() decoded_uid := store.DecodeUid(user.Uid()) if _, err = tx.Exec(ctx, "INSERT INTO users(id,createdat,updatedat,state,access,public,trusted,tags) VALUES($1,$2,$3,$4,$5,$6,$7,$8);", decoded_uid, user.CreatedAt, user.UpdatedAt, user.State, user.Access, common.ToJSON(user.Public), common.ToJSON(user.Trusted), user.Tags); err != nil { return err } // Save user's tags to a separate table to make user findable. if err = addTags(ctx, tx, "usertags", "userid", decoded_uid, user.Tags, false); err != nil { return err } return tx.Commit(ctx) } // Add user's authentication record func (a *adapter) AuthAddRecord(uid t.Uid, scheme, unique string, authLvl auth.Level, secret []byte, expires time.Time) error { var exp *time.Time if !expires.IsZero() { exp = &expires } ctx, cancel := a.getContext() if cancel != nil { defer cancel() } if _, err := a.db.Exec(ctx, "INSERT INTO auth(uname,userid,scheme,authLvl,secret,expires) VALUES($1,$2,$3,$4,$5,$6)", unique, store.DecodeUid(uid), scheme, authLvl, secret, exp); err != nil { if isDupe(err) { return t.ErrDuplicate } return err } return nil } // AuthDelScheme deletes an existing authentication scheme for the user. func (a *adapter) AuthDelScheme(user t.Uid, scheme string) error { ctx, cancel := a.getContext() if cancel != nil { defer cancel() } _, err := a.db.Exec(ctx, "DELETE FROM auth WHERE userid=$1 AND scheme=$2", store.DecodeUid(user), scheme) return err } // AuthDelAllRecords deletes all authentication records for the user. func (a *adapter) AuthDelAllRecords(user t.Uid) (int, error) { ctx, cancel := a.getContext() if cancel != nil { defer cancel() } res, err := a.db.Exec(ctx, "DELETE FROM auth WHERE userid=$1", store.DecodeUid(user)) if err != nil { return 0, err } count := res.RowsAffected() return int(count), nil } // Update user's authentication unique, secret, auth level. func (a *adapter) AuthUpdRecord(uid t.Uid, scheme, unique string, authLvl auth.Level, secret []byte, expires time.Time) error { parapg := []string{"authLvl=?"} args := []any{authLvl} if unique != "" { parapg = append(parapg, "uname=?") args = append(args, unique) } if len(secret) > 0 { parapg = append(parapg, "secret=?") args = append(args, secret) } if !expires.IsZero() { parapg = append(parapg, "expires=?") args = append(args, expires) } args = append(args, store.DecodeUid(uid), scheme) ctx, cancel := a.getContext() if cancel != nil { defer cancel() } sql, args := expandQuery("UPDATE auth SET "+strings.Join(parapg, ",")+" WHERE userid=? AND scheme=?", args...) resp, err := a.db.Exec(ctx, sql, args...) if isDupe(err) { return t.ErrDuplicate } if count := resp.RowsAffected(); count <= 0 { return t.ErrNotFound } return err } // Retrieve user's authentication record func (a *adapter) AuthGetRecord(uid t.Uid, scheme string) (string, auth.Level, []byte, time.Time, error) { var expires time.Time var record struct { Uname string Authlvl auth.Level Secret []byte Expires *time.Time } ctx, cancel := a.getContext() if cancel != nil { defer cancel() } if err := a.db.QueryRow(ctx, "SELECT uname,secret,expires,authlvl FROM auth WHERE userid=$1 AND scheme=$2", store.DecodeUid(uid), scheme).Scan( &record.Uname, &record.Secret, &record.Expires, &record.Authlvl); err != nil { if err == pgx.ErrNoRows { // Nothing found - use standard error. err = t.ErrNotFound } return "", 0, nil, expires, err } if record.Expires != nil { expires = *record.Expires } return record.Uname, record.Authlvl, record.Secret, expires, nil } // Retrieve user's authentication record func (a *adapter) AuthGetUniqueRecord(unique string) (t.Uid, auth.Level, []byte, time.Time, error) { var expires time.Time var record struct { Userid int64 Authlvl auth.Level Secret []byte Expires *time.Time } ctx, cancel := a.getContext() if cancel != nil { defer cancel() } if err := a.db.QueryRow(ctx, "SELECT userid,secret,expires,authlvl FROM auth WHERE uname=$1", unique).Scan( &record.Userid, &record.Secret, &record.Expires, &record.Authlvl); err != nil { if err == pgx.ErrNoRows { // Nothing found - clear the error err = nil } return t.ZeroUid, 0, nil, expires, err } if record.Expires != nil { expires = *record.Expires } return store.EncodeUid(record.Userid), record.Authlvl, record.Secret, expires, nil } // UserGet fetches a single user by user id. If user is not found it returns (nil, nil) func (a *adapter) UserGet(uid t.Uid) (*t.User, error) { ctx, cancel := a.getContext() if cancel != nil { defer cancel() } var user t.User var id int64 row, err := a.db.Query(ctx, "SELECT * FROM users WHERE id=$1 AND state!=$2", store.DecodeUid(uid), t.StateDeleted) if err != nil { return nil, err } defer row.Close() if !row.Next() { // Nothing found: user does not exist or marked as soft-deleted return nil, nil } err = row.Scan(&id, &user.CreatedAt, &user.UpdatedAt, &user.State, &user.StateAt, &user.Access, &user.LastSeen, &user.UserAgent, &user.Public, &user.Trusted, &user.Tags) if err == nil { user.SetUid(uid) return &user, nil } return nil, err } func (a *adapter) UserGetAll(ids ...t.Uid) ([]t.User, error) { uids := make([]any, len(ids)) for i, id := range ids { uids[i] = store.DecodeUid(id) } users := []t.User{} ctx, cancel := a.getContext() if cancel != nil { defer cancel() } rows, err := a.db.Query(ctx, "SELECT * FROM users WHERE id = ANY ($1) AND state!=$2", uids, t.StateDeleted) if err != nil { return nil, err } defer rows.Close() for rows.Next() { var user t.User var id int64 if err = rows.Scan(&id, &user.CreatedAt, &user.UpdatedAt, &user.State, &user.StateAt, &user.Access, &user.LastSeen, &user.UserAgent, &user.Public, &user.Trusted, &user.Tags); err != nil { users = nil break } user.SetUid(store.EncodeUid(id)) users = append(users, user) } if err == nil { err = rows.Err() } return users, err } // UserDelete deletes specified user: wipes completely (hard-delete) or marks as deleted. // TODO: report when the user is not found. func (a *adapter) UserDelete(uid t.Uid, hard bool) error { query := "SELECT name FROM topics WHERE owner=$1" args := []any{store.DecodeUid(uid)} // In case of hard delete, delete all topics, even those which were // soft-deleted previsously. if !hard { query += " AND state!=$2" args = append(args, t.StateDeleted) } // Get a list of topic names owned by the user (as 'grp' and 'chn'). ownTopics, err := a.topicNamesForUser(query, false, args...) if err != nil { return err } ctx, cancel := a.getContextForTx() if cancel != nil { defer cancel() } tx, err := a.db.BeginTx(ctx, pgx.TxOptions{}) if err != nil { return err } defer func() { if err != nil { tx.Rollback(ctx) } }() now := t.TimeNow() decoded_uid := store.DecodeUid(uid) if hard { // Delete user's devices // t.ErrNotFound = user has no devices. if err = deviceDelete(ctx, tx, uid, ""); err != nil && err != t.ErrNotFound { return err } // Delete user's subscriptions in all topics. if err = subsDelForUser(ctx, tx, decoded_uid, true); err != nil { return err } // Delete records of messages soft-deleted for the user. if _, err = tx.Exec(ctx, "DELETE FROM dellog WHERE deletedfor=$1", decoded_uid); err != nil { return err } // Can't delete user's messages in all topics because we cannot notify topics of such deletion. // Just leave the messages there marked as sent by "not found" user. // Delete topics where the user is the owner. if len(ownTopics) > 0 { // First delete all messages in those topics. if _, err = tx.Exec(ctx, "DELETE FROM dellog USING topics WHERE topics.name=dellog.topic AND topics.owner=$1", decoded_uid); err != nil { return err } // Deletion of messages will cascade to filemsglinks and so to fileuploads. if _, err = tx.Exec(ctx, "DELETE FROM messages USING topics WHERE topics.name=messages.topic AND topics.owner=$1", decoded_uid); err != nil { return err } // Delete subscriptions for all users where the user is the owner of the topic. sql, args, _ := sqlx.In("DELETE FROM subscriptions AS s WHERE topic IN (?)", ownTopics) if _, err = tx.Exec(ctx, sqlx.Rebind(sqlx.DOLLAR, sql), args...); err != nil { return err } // Delete topic tags. if _, err = tx.Exec(ctx, "DELETE FROM topictags USING topics WHERE topics.name=topictags.topic AND topics.owner=$1", decoded_uid); err != nil { return err } // And finally delete the topics. if _, err = tx.Exec(ctx, "DELETE FROM topics WHERE owner=$1", decoded_uid); err != nil { return err } } // Delete user's authentication records. if _, err = tx.Exec(ctx, "DELETE FROM auth WHERE userid=$1", decoded_uid); err != nil { return err } // Delete all credentials. if err = credDel(ctx, tx, uid, "", ""); err != nil && err != t.ErrNotFound { return err } if _, err = tx.Exec(ctx, "DELETE FROM usertags WHERE userid=$1", decoded_uid); err != nil { return err } if _, err = tx.Exec(ctx, "DELETE FROM users WHERE id=$1", decoded_uid); err != nil { return err } } else { // Disable all user's subscriptions. That includes p2p subscriptions. No need to delete them. if err = subsDelForUser(ctx, tx, decoded_uid, false); err != nil { return err } if len(ownTopics) > 0 { // Disable all subscriptions to topics where the user is the owner. sql, args, _ := sqlx.In("UPDATE subscriptions SET updatedat=?,deletedat=? WHERE topic IN (?)", now, now, ownTopics) if _, err = tx.Exec(ctx, sqlx.Rebind(sqlx.DOLLAR, sql), args...); err != nil { return err } // Disable group topics where the user is the owner. if _, err = tx.Exec(ctx, "UPDATE topics SET updatedat=$1,touchedat=$1,state=$2,stateat=$1 WHERE owner=$3", now, t.StateDeleted, decoded_uid); err != nil { return err } } // Disable p2p topics with the user (p2p topic's owner is 0). if _, err = tx.Exec(ctx, "UPDATE topics SET updatedat=$1,touchedat=$1,state=$2,stateat=$1 "+ "FROM subscriptions WHERE topics.name=subscriptions.topic "+ "AND topics.owner=0 AND subscriptions.userid=$3", now, t.StateDeleted, decoded_uid); err != nil { return err } // Disable the other user's subscription to a disabled p2p topic. if _, err = tx.Exec(ctx, "UPDATE subscriptions AS s_one SET updatedat=$1,deletedat=$1 "+ "FROM subscriptions AS s_two WHERE s_one.topic=s_two.topic "+ "AND s_two.userid=$2 AND s_two.topic LIKE 'p2p%'", now, decoded_uid); err != nil { return err } // Disable user. if _, err = tx.Exec(ctx, "UPDATE users SET updatedat=$1,state=$2,stateat=$1 WHERE id=$3", now, t.StateDeleted, decoded_uid); err != nil { return err } } return tx.Commit(ctx) } // topicStateForUser is called by UserUpdate when the update contains state change. // Soft-deleted topics remain soft-deleted. func (a *adapter) topicStateForUser(ctx context.Context, tx pgx.Tx, decoded_uid int64, now time.Time, update any) error { var err error state, ok := update.(t.ObjState) if !ok { return t.ErrMalformed } if now.IsZero() { now = t.TimeNow() } // Change state of all topics where the user is the owner. if _, err = tx.Exec(ctx, "UPDATE topics SET state=$1, stateat=$2 WHERE owner=$3 AND state!=$4", state, now, decoded_uid, t.StateDeleted); err != nil { return err } // Change state of p2p topics with the user (p2p topic's owner is 0) if _, err = tx.Exec(ctx, "UPDATE topics SET state=$1, stateat=$2 "+ "FROM subscriptions WHERE topics.name=subscriptions.topic AND "+ "topics.owner=0 AND subscriptions.userid=$3 AND topics.state!=$4", state, now, decoded_uid, t.StateDeleted); err != nil { return err } // Subscriptions don't need to be updated: // subscriptions of a disabled user are not disabled and still can be manipulated. return nil } // UserUpdate updates user object. func (a *adapter) UserUpdate(uid t.Uid, update map[string]any) error { ctx, cancel := a.getContextForTx() if cancel != nil { defer cancel() } tx, err := a.db.BeginTx(ctx, pgx.TxOptions{}) if err != nil { return err } defer func() { if err != nil { tx.Rollback(ctx) } }() cols, args := common.UpdateByMap(update) decoded_uid := store.DecodeUid(uid) args = append(args, decoded_uid) sql, args := expandQuery("UPDATE users SET "+strings.Join(cols, ",")+" WHERE id=?", args...) _, err = tx.Exec(ctx, sql, args...) if err != nil { return err } if state, ok := update["State"]; ok { now, _ := update["StateAt"].(time.Time) err = a.topicStateForUser(ctx, tx, decoded_uid, now, state) if err != nil { return err } } // Tags are also stored in a separate table if tags := common.ExtractTags(update); tags != nil { // First delete all user tags _, err = tx.Exec(ctx, "DELETE FROM usertags WHERE userid=$1", decoded_uid) if err != nil { return err } // Now insert new tags err = addTags(ctx, tx, "usertags", "userid", decoded_uid, tags, false) if err != nil { return err } } return tx.Commit(ctx) } func tempFetchTags(ctx context.Context, tx pgx.Tx, decoded_uid int64) ([]string, error) { var allTags []string rows, err := tx.Query(ctx, "SELECT tag FROM usertags WHERE userid=$1", decoded_uid) if err != nil { return nil, err } defer rows.Close() for rows.Next() { var tag string rows.Scan(&tag) allTags = append(allTags, tag) } return allTags, nil } // UserUpdateTags adds or resets user's tags func (a *adapter) UserUpdateTags(uid t.Uid, add, remove, reset []string) ([]string, error) { ctx, cancel := a.getContextForTx() if cancel != nil { defer cancel() } tx, err := a.db.BeginTx(ctx, pgx.TxOptions{}) if err != nil { return nil, err } defer func() { if err != nil { tx.Rollback(ctx) } }() decoded_uid := store.DecodeUid(uid) if reset != nil { // Delete all tags first if resetting. _, err = tx.Exec(ctx, "DELETE FROM usertags WHERE userid=$1", decoded_uid) if err != nil { return nil, err } add = reset remove = nil } // Now insert new tags. Ignore duplicates if resetting. err = addTags(ctx, tx, "usertags", "userid", decoded_uid, add, reset == nil) if err != nil { return nil, err } // Delete tags. err = removeTags(ctx, tx, "usertags", "userid", decoded_uid, remove) if err != nil { return nil, err } var allTags []string rows, err := tx.Query(ctx, "SELECT tag FROM usertags WHERE userid=$1", decoded_uid) if err != nil { return nil, err } defer rows.Close() for rows.Next() { var tag string rows.Scan(&tag) allTags = append(allTags, tag) } _, err = tx.Exec(ctx, "UPDATE users SET tags=$1 WHERE id=$2", t.StringSlice(allTags), decoded_uid) if err != nil { return nil, err } return allTags, tx.Commit(ctx) } // UserGetByCred returns user ID for the given validated credential. func (a *adapter) UserGetByCred(method, value string) (t.Uid, error) { ctx, cancel := a.getContext() if cancel != nil { defer cancel() } var decoded_uid int64 err := a.db.QueryRow(ctx, "SELECT userid FROM credentials WHERE synthetic=$1", method+":"+value).Scan(&decoded_uid) if err == nil { return store.EncodeUid(decoded_uid), nil } if err == pgx.ErrNoRows { // Clear the error if user does not exist return t.ZeroUid, nil } return t.ZeroUid, err } // UserUnreadCount returns the total number of unread messages in all topics with // the R permission. If read fails, the counts are still returned with the original // user IDs but with the unread count undefined and non-nil error. // UserUnreadCount does not count unread messages in channels although it should. func (a *adapter) UserUnreadCount(ids ...t.Uid) (map[t.Uid]int, error) { uids := make([]any, len(ids)) counts := make(map[t.Uid]int, len(ids)) for i, id := range ids { uids[i] = store.DecodeUid(id) // Ensure all original uids are always present. counts[id] = 0 } ctx, cancel := a.getContext() if cancel != nil { defer cancel() } // FIXME: support channels. query, uids := expandQuery("SELECT s.userid, SUM(t.seqid)-SUM(s.readseqid) AS unreadcount FROM topics AS t, subscriptions AS s "+ "WHERE s.userid IN (?) AND t.name=s.topic AND s.deletedat IS NULL AND t.state!=? AND "+ "POSITION('R' IN s.modewant)>0 AND POSITION('R' IN s.modegiven)>0 GROUP BY s.userid", uids, t.StateDeleted) rows, err := a.db.Query(ctx, query, uids...) if err != nil { return counts, err } defer rows.Close() var userId int64 var unreadCount int for rows.Next() { if err = rows.Scan(&userId, &unreadCount); err != nil { break } counts[store.EncodeUid(userId)] = unreadCount } if err == nil { err = rows.Err() } return counts, err } // UserGetUnvalidated returns a list of uids which have never logged in, have no // validated credentials and haven't been updated since lastUpdatedBefore. func (a *adapter) UserGetUnvalidated(lastUpdatedBefore time.Time, limit int) ([]t.Uid, error) { var uids []t.Uid ctx, cancel := a.getContext() if cancel != nil { defer cancel() } rows, err := a.db.Query(ctx, "SELECT u.id, COALESCE(SUM(CASE WHEN c.done THEN 1 ELSE 0 END), 0) AS total "+ "FROM users u LEFT JOIN credentials c ON u.id = c.userid "+ "WHERE u.lastseen IS NULL AND u.updatedat < $1 GROUP BY u.id, u.updatedat "+ "HAVING COALESCE(SUM(CASE WHEN c.done THEN 1 ELSE 0 END), 0) = 0 ORDER BY u.updatedat ASC LIMIT $2", lastUpdatedBefore, limit) if err != nil { return nil, err } defer rows.Close() for rows.Next() { var userId int64 var unused int if err = rows.Scan(&userId, &unused); err != nil { break } uids = append(uids, store.EncodeUid(userId)) } if err == nil { err = rows.Err() } return uids, err } // ***************************** func (a *adapter) topicCreate(ctx context.Context, tx pgx.Tx, topic *t.Topic) error { _, err := tx.Exec(ctx, "INSERT INTO topics(createdat,updatedat,touchedat,state,name,usebt,owner,access,public,trusted,tags,aux) "+ "VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12)", topic.CreatedAt, topic.UpdatedAt, topic.TouchedAt, topic.State, topic.Id, topic.UseBt, store.DecodeUid(t.ParseUid(topic.Owner)), topic.Access, common.ToJSON(topic.Public), common.ToJSON(topic.Trusted), topic.Tags, common.ToJSON(topic.Aux)) if err != nil { return err } // Save topic's tags to a separate table to make topic findable. return addTags(ctx, tx, "topictags", "topic", topic.Id, topic.Tags, false) } // TopicCreate saves topic object to database. func (a *adapter) TopicCreate(topic *t.Topic) error { ctx, cancel := a.getContextForTx() if cancel != nil { defer cancel() } tx, err := a.db.BeginTx(ctx, pgx.TxOptions{}) if err != nil { return err } defer func() { if err != nil { tx.Rollback(ctx) } }() err = a.topicCreate(ctx, tx, topic) if err != nil { return err } return tx.Commit(ctx) } // If undelete = true - update subscription on duplicate key, otherwise ignore the duplicate. func createSubscription(ctx context.Context, tx pgx.Tx, sub *t.Subscription, undelete bool) error { isOwner := (sub.ModeGiven & sub.ModeWant).IsOwner() jpriv := common.ToJSON(sub.Private) decoded_uid := store.DecodeUid(t.ParseUid(sub.User)) _, err2 := tx.Exec(ctx, "SAVEPOINT createSub") if err2 != nil { log.Println("Error: Failed to create savepoint: ", err2.Error()) } _, err := tx.Exec(ctx, "INSERT INTO subscriptions(createdat,updatedat,deletedat,userid,topic,modeWant,modeGiven,private) "+ "VALUES($1,$2,NULL,$3,$4,$5,$6,$7)", sub.CreatedAt, sub.UpdatedAt, decoded_uid, sub.Topic, sub.ModeWant.String(), sub.ModeGiven.String(), jpriv) if err != nil && isDupe(err) { _, err2 = tx.Exec(ctx, "ROLLBACK TO SAVEPOINT createSub") if err2 != nil { log.Println("Error: Failed to rollback savepoint: ", err2.Error()) } if undelete { _, err = tx.Exec(ctx, "UPDATE subscriptions SET createdat=$1,updatedat=$2,deletedat=NULL,modeWant=$3,modeGiven=$4,"+ "delid=0,recvseqid=0,readseqid=0 WHERE topic=$5 AND userid=$6", sub.CreatedAt, sub.UpdatedAt, sub.ModeWant.String(), sub.ModeGiven.String(), sub.Topic, decoded_uid) } else { _, err = tx.Exec(ctx, "UPDATE subscriptions SET createdat=$1,updatedat=$2,deletedat=NULL,modeWant=$3,modeGiven=$4,"+ "delid=0,recvseqid=0,readseqid=0,private=$5 WHERE topic=$6 AND userid=$7", sub.CreatedAt, sub.UpdatedAt, sub.ModeWant.String(), sub.ModeGiven.String(), jpriv, sub.Topic, decoded_uid) } } else { _, err2 = tx.Exec(ctx, "RELEASE SAVEPOINT createSub") if err2 != nil { log.Println("Error: Failed to release savepoint: ", err2.Error()) } } if err == nil && isOwner { _, err = tx.Exec(ctx, "UPDATE topics SET owner=$1 WHERE name=$2", decoded_uid, sub.Topic) } return err } // TopicCreateP2P given two users creates a p2p topic func (a *adapter) TopicCreateP2P(initiator, invited *t.Subscription) error { ctx, cancel := a.getContextForTx() if cancel != nil { defer cancel() } tx, err := a.db.BeginTx(ctx, pgx.TxOptions{}) if err != nil { return err } defer func() { if err != nil { tx.Rollback(ctx) } }() err = createSubscription(ctx, tx, initiator, false) if err != nil { return err } err = createSubscription(ctx, tx, invited, true) if err != nil { return err } topic := &t.Topic{ObjHeader: t.ObjHeader{Id: initiator.Topic}} topic.ObjHeader.MergeTimes(&initiator.ObjHeader) topic.TouchedAt = initiator.GetTouchedAt() err = a.topicCreate(ctx, tx, topic) if err != nil { return err } return tx.Commit(ctx) } // TopicGet loads a single topic by name, if it exists. If the topic does not exist the call returns (nil, nil) func (a *adapter) TopicGet(topic string) (*t.Topic, error) { ctx, cancel := a.getContextForTx() if cancel != nil { defer cancel() } // Fetch topic by name var tt = new(t.Topic) var owner int64 err := a.db.QueryRow(ctx, "SELECT createdat,updatedat,state,stateat,touchedat,name AS id,usebt,access,owner,seqid,delid,subcnt,public,trusted,tags,aux "+ "FROM topics WHERE name=$1", topic).Scan(&tt.CreatedAt, &tt.UpdatedAt, &tt.State, &tt.StateAt, &tt.TouchedAt, &tt.Id, &tt.UseBt, &tt.Access, &owner, &tt.SeqId, &tt.DelId, &tt.SubCnt, &tt.Public, &tt.Trusted, &tt.Tags, &tt.Aux) if err != nil { if err == pgx.ErrNoRows { // Nothing found - clear the error err = nil } return nil, err } if t.GetTopicCat(topic) == t.TopicCatGrp { // Topic found, get subsription count. Try both topic and channel names. var subCnt int if err = a.db.QueryRow(ctx, "SELECT COUNT(*) FROM subscriptions WHERE topic IN ($1,$2) AND deletedat IS NULL", topic, t.GrpToChn(topic)). Scan(&subCnt); err != nil { return nil, err } if subCnt != tt.SubCnt { // Update the topic with the correct subscription count. tt.SubCnt = subCnt if _, err = a.db.Exec(ctx, "UPDATE topics SET subcnt=$1 WHERE name=$2", subCnt, topic); err != nil { return nil, err } } } tt.Owner = store.EncodeUid(owner).String() return tt, err } // TopicsForUser loads user's contact list: p2p and grp topics, except for 'me' & 'fnd' subscriptions. // Reads and denormalizes Public value. func (a *adapter) TopicsForUser(uid t.Uid, keepDeleted bool, opts *t.QueryOpt) ([]t.Subscription, error) { // Fetch ALL user's subscriptions, even those which has not been modified recently. // We are going to use these subscriptions to fetch topics and users which may have been modified recently. q := `SELECT createdat,updatedat,deletedat,topic,delid,recvseqid, readseqid,modewant,modegiven,private FROM subscriptions WHERE userid=?` args := []any{store.DecodeUid(uid)} if !keepDeleted { // Filter out deleted rows. q += " AND deletedat IS NULL" } limit := 0 ims := time.Time{} if opts != nil { if opts.Topic != "" { q += " AND topic=?" args = append(args, opts.Topic) } // Apply the limit only when the client does not manage the cache (or cold start). // Otherwise have to get all subscriptions and do a manual join with users/topics. if opts.IfModifiedSince == nil { if opts.Limit > 0 && opts.Limit < a.maxResults { limit = opts.Limit } else { limit = a.maxResults } } else { ims = *opts.IfModifiedSince } } else { limit = a.maxResults } if limit > 0 { q += " LIMIT ?" args = append(args, limit) } q, args = expandQuery(q, args...) ctx, cancel := a.getContext() if cancel != nil { defer cancel() } rows, err := a.db.Query(ctx, q, args...) if err != nil { return nil, err } // Must close rows manually as we will be reusing it. // Fetch subscriptions. Two queries are needed: users table (p2p) and topics table (grp). // Prepare a list of separate subscriptions to users vs topics join := make(map[string]t.Subscription) // Keeping these to make a join with table for .private and .access topq := make([]any, 0, 16) usrq := make([]any, 0, 16) for rows.Next() { var sub t.Subscription var modeWant, modeGiven []byte if err = rows.Scan(&sub.CreatedAt, &sub.UpdatedAt, &sub.DeletedAt, &sub.Topic, &sub.DelId, &sub.RecvSeqId, &sub.ReadSeqId, &modeWant, &modeGiven, &sub.Private); err != nil { break } sub.ModeWant.Scan(modeWant) sub.ModeGiven.Scan(modeGiven) tname := sub.Topic sub.User = uid.String() tcat := t.GetTopicCat(tname) if tcat == t.TopicCatMe || tcat == t.TopicCatFnd { // One of 'me', 'fnd' subscriptions, skip. // Don't skip 'sys' subscription. continue } else if tcat == t.TopicCatP2P { // P2P subscription, find the other user to get user.Public and user.Trusted. uid1, uid2, _ := t.ParseP2P(tname) if uid1 == uid { usrq = append(usrq, store.DecodeUid(uid2)) sub.SetWith(uid2.UserId()) } else { usrq = append(usrq, store.DecodeUid(uid1)) sub.SetWith(uid1.UserId()) } } else if tcat == t.TopicCatGrp { // Maybe convert channel name to topic name. tname = t.ChnToGrp(tname) } // No special handling needed for 'slf', 'sys' subscriptions. topq = append(topq, tname) sub.Private = common.FromJSON(sub.Private) join[tname] = sub } if err == nil { err = rows.Err() } rows.Close() if err != nil { return nil, err } var subs []t.Subscription if len(join) == 0 { return subs, nil } // Fetch grp topics and join to subscriptions. if len(topq) > 0 { q = "SELECT updatedat,state,touchedat,name AS id,usebt,access,seqid,delid,subcnt,public,trusted " + "FROM topics WHERE name IN (?)" newargs := []any{topq} if !keepDeleted { // Optionally skip deleted topics. q += " AND state!=?" newargs = append(newargs, t.StateDeleted) } if !ims.IsZero() { // Use cache timestamp if provided: get newer entries only. q += " AND touchedat>?" newargs = append(newargs, ims) if limit > 0 && limit < len(topq) { // No point in fetching more than the requested limit. q += " ORDER BY touchedat LIMIT ?" newargs = append(newargs, limit) } } q, newargs = expandQuery(q, newargs...) ctx2, cancel2 := a.getContext() if cancel2 != nil { defer cancel2() } rows, err = a.db.Query(ctx2, q, newargs...) if err != nil { return nil, err } var top t.Topic for rows.Next() { if err = rows.Scan(&top.UpdatedAt, &top.State, &top.TouchedAt, &top.Id, &top.UseBt, &top.Access, &top.SeqId, &top.DelId, &top.SubCnt, &top.Public, &top.Trusted); err != nil { break } sub := join[top.Id] // Check if sub.UpdatedAt needs to be adjusted to earlier or later time. sub.UpdatedAt = common.SelectLatestTime(sub.UpdatedAt, top.UpdatedAt) sub.SetState(top.State) sub.SetTouchedAt(top.TouchedAt) sub.SetSeqId(top.SeqId) if t.GetTopicCat(sub.Topic) == t.TopicCatGrp { sub.SetSubCnt(top.SubCnt) sub.SetPublic(top.Public) sub.SetTrusted(top.Trusted) } // Put back the updated value of a subsription, will process further below join[top.Id] = sub } if err == nil { err = rows.Err() } rows.Close() if err != nil { return nil, err } } // Fetch p2p users and join to p2p subscriptions. if len(usrq) > 0 { q = "SELECT id,updatedat,state,access,lastseen,useragent,public,trusted " + "FROM users WHERE id IN (?)" newargs := []any{usrq} if !keepDeleted { // Optionally skip deleted users. q += " AND state!=?" newargs = append(newargs, t.StateDeleted) } // Ignoring ipg: we need all users to get LastSeen and UserAgent. q, newargs = expandQuery(q, newargs...) ctx3, cancel3 := a.getContext() if cancel3 != nil { defer cancel3() } rows, err = a.db.Query(ctx3, q, newargs...) if err != nil { return nil, err } for rows.Next() { var usr2 t.User var id int64 if err = rows.Scan(&id, &usr2.UpdatedAt, &usr2.State, &usr2.Access, &usr2.LastSeen, &usr2.UserAgent, &usr2.Public, &usr2.Trusted); err != nil { break } usr2.Id = store.EncodeUid(id).String() joinOn := uid.P2PName(t.ParseUid(usr2.Id)) if sub, ok := join[joinOn]; ok { sub.UpdatedAt = common.SelectLatestTime(sub.UpdatedAt, usr2.UpdatedAt) sub.SetState(usr2.State) sub.SetPublic(usr2.Public) sub.SetTrusted(usr2.Trusted) sub.SetDefaultAccess(usr2.Access.Auth, usr2.Access.Anon) sub.SetLastSeenAndUA(usr2.LastSeen, usr2.UserAgent) join[joinOn] = sub } } if err == nil { err = rows.Err() } rows.Close() if err != nil { return nil, err } } subs = make([]t.Subscription, 0, len(join)) for _, sub := range join { subs = append(subs, sub) } return common.SelectEarliestUpdatedSubs(subs, opts, a.maxResults), nil } // UsersForTopic loads users subscribed to the given topic. // The difference between UsersForTopic vs SubsForTopic is that the former loads user.Public, // the latter does not. func (a *adapter) UsersForTopic(topic string, keepDeleted bool, opts *t.QueryOpt) ([]t.Subscription, error) { tcat := t.GetTopicCat(topic) // Fetch all subscribed users. The number of users is not large q := `SELECT s.createdat,s.updatedat,s.deletedat,s.userid,s.topic,s.delid,s.recvseqid, s.readseqid,s.modewant,s.modegiven,u.public,u.trusted,u.lastseen,u.useragent,s.private FROM subscriptions AS s JOIN users AS u ON s.userid=u.id WHERE s.topic=?` args := []any{topic} if !keepDeleted { // Filter out rows with users deleted q += " AND u.state!=?" args = append(args, t.StateDeleted) // For p2p topics we must load all subscriptions including deleted. // Otherwise it will be impossible to swipe Public values. if tcat != t.TopicCatP2P { // Filter out deleted subscriptions. q += " AND s.deletedat IS NULL" } } limit := a.maxResults var oneUser t.Uid if opts != nil { // Ignore IfModifiedSince: loading all entries because a topic cannot have too many subscribers. // Those unmodified will be stripped of Public & Private. if !opts.User.IsZero() { // For p2p topics we have to fetch both users otherwise public cannot be swapped. if tcat != t.TopicCatP2P { q += " AND s.userid=?" args = append(args, store.DecodeUid(opts.User)) } oneUser = opts.User } if opts.Limit > 0 && opts.Limit < limit { limit = opts.Limit } } q += " LIMIT ?" args = append(args, limit) q, args = expandQuery(q, args...) ctx, cancel := a.getContext() if cancel != nil { defer cancel() } rows, err := a.db.Query(ctx, q, args...) if err != nil { return nil, err } defer rows.Close() // Fetch subscriptions var sub t.Subscription var subs []t.Subscription var userId int64 var modeWant, modeGiven []byte var lastSeen *time.Time = nil var userAgent string var public, trusted any for rows.Next() { if err = rows.Scan( &sub.CreatedAt, &sub.UpdatedAt, &sub.DeletedAt, &userId, &sub.Topic, &sub.DelId, &sub.RecvSeqId, &sub.ReadSeqId, &modeWant, &modeGiven, &public, &trusted, &lastSeen, &userAgent, &sub.Private); err != nil { break } sub.User = store.EncodeUid(userId).String() sub.SetPublic(public) sub.SetTrusted(trusted) sub.SetLastSeenAndUA(lastSeen, userAgent) sub.ModeWant.Scan(modeWant) sub.ModeGiven.Scan(modeGiven) subs = append(subs, sub) } if err == nil { err = rows.Err() } if err == nil && tcat == t.TopicCatP2P && len(subs) > 0 { // Swap public & lastSeen values of P2P topics as expected. if len(subs) == 1 { // The other user is deleted, nothing we can do. subs[0].SetPublic(nil) subs[0].SetTrusted(nil) subs[0].SetLastSeenAndUA(nil, "") } else { tmp := subs[0].GetPublic() subs[0].SetPublic(subs[1].GetPublic()) subs[1].SetPublic(tmp) tmp = subs[0].GetTrusted() subs[0].SetTrusted(subs[1].GetTrusted()) subs[1].SetTrusted(tmp) lastSeen := subs[0].GetLastSeen() userAgent = subs[0].GetUserAgent() subs[0].SetLastSeenAndUA(subs[1].GetLastSeen(), subs[1].GetUserAgent()) subs[1].SetLastSeenAndUA(lastSeen, userAgent) } // Remove deleted and unneeded subscriptions if !keepDeleted || !oneUser.IsZero() { var xsubs []t.Subscription for i := range subs { if (subs[i].DeletedAt != nil && !keepDeleted) || (!oneUser.IsZero() && subs[i].Uid() != oneUser) { continue } xsubs = append(xsubs, subs[i]) } subs = xsubs } } return subs, err } // topicNamesForUser reads a slice of strings using provided query. func (a *adapter) topicNamesForUser(sqlQuery string, includeChan bool, args ...any) ([]string, error) { ctx, cancel := a.getContext() if cancel != nil { defer cancel() } rows, err := a.db.Query(ctx, sqlQuery, args...) if err != nil { return nil, err } defer rows.Close() var names []string for rows.Next() { var name string if err = rows.Scan(&name); err != nil { break } names = append(names, name) // If the name is a group topic, also add the channel name if requested. if includeChan { if channel := t.GrpToChn(name); channel != "" { names = append(names, channel) } } } if err == nil { err = rows.Err() } return names, err } // OwnTopics loads a slice of topic names where the user is the owner. func (a *adapter) OwnTopics(uid t.Uid) ([]string, error) { return a.topicNamesForUser("SELECT name FROM topics WHERE owner=$1 AND state!=$2", false, store.DecodeUid(uid), t.StateDeleted) } // ChannelsForUser loads a slice of topic names where the user is a channel reader and notifications (P) are enabled. func (a *adapter) ChannelsForUser(uid t.Uid) ([]string, error) { return a.topicNamesForUser("SELECT topic FROM subscriptions WHERE userid=$1 AND topic LIKE 'chn%' "+ "AND POSITION('P' IN modewant)>0 AND POSITION('P' IN modegiven)>0 AND deletedat IS NULL", false, store.DecodeUid(uid)) } // TopicShare creates topic subscriptions and increments the topic's subcnt. func (a *adapter) TopicShare(topic string, shares []*t.Subscription) error { ctx, cancel := a.getContextForTx() if cancel != nil { defer cancel() } tx, err := a.db.BeginTx(ctx, pgx.TxOptions{}) if err != nil { return err } defer func() { if err != nil { tx.Rollback(ctx) } }() for _, sub := range shares { err = createSubscription(ctx, tx, sub, true) if err != nil { return err } } if topic != "" { if _, err = tx.Exec(ctx, "UPDATE topics SET subcnt=subcnt+$1 WHERE name=$2", len(shares), topic); err != nil { return err } } return tx.Commit(ctx) } // TopicDelete deletes topic, subscriptions, messages. func (a *adapter) TopicDelete(topic string, isChan, hard bool) error { ctx, cancel := a.getContextForTx() if cancel != nil { defer cancel() } tx, err := a.db.BeginTx(ctx, pgx.TxOptions{}) if err != nil { return err } defer func() { if err != nil { tx.Rollback(ctx) } }() // If the topic is a channel, must try to delete subscriptions under both grpXXX and chnXXX names. args := []any{topic} if isChan { args = append(args, t.GrpToChn(topic)) } if hard { // Delete subscriptions. If this is a channel, delete both group subscriptions and channel subscriptions. q, args := expandQuery("DELETE FROM subscriptions WHERE topic IN (?)", args) if _, err = tx.Exec(ctx, q, args...); err != nil { return err } if err = messageDeleteList(ctx, tx, topic, nil); err != nil { return err } if _, err = tx.Exec(ctx, "DELETE FROM topictags WHERE topic=$1", topic); err != nil { return err } if _, err = tx.Exec(ctx, "DELETE FROM topics WHERE name=$1", topic); err != nil { return err } } else { now := t.TimeNow() q, args := expandQuery("UPDATE subscriptions SET updatedat=?,deletedat=? WHERE topic IN (?)", now, now, args) if _, err = tx.Exec(ctx, q, args...); err != nil { return err } if _, err = tx.Exec(ctx, "UPDATE topics SET updatedat=$1,touchedat=$1,state=$2,stateat=$1 WHERE name=$3", now, t.StateDeleted, topic); err != nil { return err } } return tx.Commit(ctx) } func (a *adapter) TopicUpdateOnMessage(topic string, msg *t.Message) error { ctx, cancel := a.getContext() if cancel != nil { defer cancel() } _, err := a.db.Exec(ctx, "UPDATE topics SET seqid=$1,touchedat=$2 WHERE name=$3", msg.SeqId, msg.CreatedAt, topic) return err } // TopicUpdateSubCnt updates subscriber count denormalized in topic. func (a *adapter) TopicUpdateSubCnt(topic string) error { ctx, cancel := a.getContext() if cancel != nil { defer cancel() } _, err := a.db.Exec(ctx, "UPDATE topics SET subcnt=(SELECT COUNT(*) FROM subscriptions WHERE topic IN ($1,$2) AND deletedat IS NULL) WHERE name=$1", topic, t.GrpToChn(topic)) return err } func (a *adapter) TopicUpdate(topic string, update map[string]any) error { ctx, cancel := a.getContextForTx() if cancel != nil { defer cancel() } tx, err := a.db.BeginTx(ctx, pgx.TxOptions{}) if err != nil { return err } defer func() { if err != nil { tx.Rollback(ctx) } }() if t, u := update["TouchedAt"], update["UpdatedAt"]; t == nil && u != nil { update["TouchedAt"] = u } cols, args := common.UpdateByMap(update) q, args := expandQuery("UPDATE topics SET "+strings.Join(cols, ",")+" WHERE name=?", args, topic) _, err = tx.Exec(ctx, q, args...) if err != nil { return err } // Tags are also stored in a separate table if tags := common.ExtractTags(update); tags != nil { // First delete all user tags _, err = tx.Exec(ctx, "DELETE FROM topictags WHERE topic=$1", topic) if err != nil { return err } // Now insert new tags err = addTags(ctx, tx, "topictags", "topic", topic, tags, false) if err != nil { return err } } return tx.Commit(ctx) } func (a *adapter) TopicOwnerChange(topic string, newOwner t.Uid) error { ctx, cancel := a.getContext() if cancel != nil { defer cancel() } _, err := a.db.Exec(ctx, "UPDATE topics SET owner=$1 WHERE name=$2", store.DecodeUid(newOwner), topic) return err } // Get a subscription of a user to a topic. func (a *adapter) SubscriptionGet(topic string, user t.Uid, keepDeleted bool) (*t.Subscription, error) { ctx, cancel := a.getContext() if cancel != nil { defer cancel() } query := `SELECT createdat,updatedat,deletedat,userid AS user,topic,delid,recvseqid, readseqid,modewant,modegiven,private FROM subscriptions WHERE topic=$1 AND userid=$2` if !keepDeleted { query += " AND deletedat IS NULL" } var sub t.Subscription var userId int64 var modeWant, modeGiven []byte err := a.db.QueryRow(ctx, query, topic, store.DecodeUid(user)).Scan(&sub.CreatedAt, &sub.UpdatedAt, &sub.DeletedAt, &userId, &sub.Topic, &sub.DelId, &sub.RecvSeqId, &sub.ReadSeqId, &modeWant, &modeGiven, &sub.Private) if err != nil { if err == pgx.ErrNoRows { // Nothing found - clear the error err = nil } return nil, err } sub.User = store.EncodeUid(userId).String() sub.ModeWant.Scan(modeWant) sub.ModeGiven.Scan(modeGiven) return &sub, nil } // SubsForUser loads all user's subscriptions. Does NOT load Public or Private values and does // not load deleted subscriptions. func (a *adapter) SubsForUser(forUser t.Uid) ([]t.Subscription, error) { q := `SELECT createdat,updatedat,deletedat,userid AS user,topic,delid,recvseqid, readseqid,modewant,modegiven FROM subscriptions WHERE userid=$1 AND deletedat IS NULL` args := []any{store.DecodeUid(forUser)} ctx, cancel := a.getContext() if cancel != nil { defer cancel() } rows, err := a.db.Query(ctx, q, args...) if err != nil { return nil, err } defer rows.Close() var subs []t.Subscription var sub t.Subscription var userId int64 var modeWant, modeGiven []byte for rows.Next() { if err = rows.Scan(&sub.CreatedAt, &sub.UpdatedAt, &sub.DeletedAt, &userId, &sub.Topic, &sub.DelId, &sub.RecvSeqId, &sub.ReadSeqId, &modeWant, &modeGiven); err != nil { break } sub.User = store.EncodeUid(userId).String() sub.ModeWant.Scan(modeWant) sub.ModeGiven.Scan(modeGiven) subs = append(subs, sub) } if err == nil { err = rows.Err() } return subs, err } // SubsForTopic fetches all subsciptions for a topic. Does NOT load Public value. // The difference between UsersForTopic vs SubsForTopic is that the former loads user.public+trusted, // the latter does not. func (a *adapter) SubsForTopic(topic string, keepDeleted bool, opts *t.QueryOpt) ([]t.Subscription, error) { q := `SELECT createdat,updatedat,deletedat,userid AS user,topic,delid,recvseqid, readseqid,modewant,modegiven,private FROM subscriptions WHERE topic=?` args := []any{topic} if !keepDeleted { // Filter out deleted rows. q += " AND deletedat IS NULL" } limit := a.maxResults if opts != nil { // Ignore IfModifiedSince - we must return all entries // Those unmodified will be stripped of Public & Private. if !opts.User.IsZero() { q += " AND userid=?" args = append(args, store.DecodeUid(opts.User)) } if opts.Limit > 0 && opts.Limit < limit { limit = opts.Limit } } q += " LIMIT ?" args = append(args, limit) q, args = expandQuery(q, args...) ctx, cancel := a.getContext() if cancel != nil { defer cancel() } rows, err := a.db.Query(ctx, q, args...) if err != nil { return nil, err } defer rows.Close() var subs []t.Subscription var sub t.Subscription var userId int64 var modeWant, modeGiven []byte for rows.Next() { if err = rows.Scan(&sub.CreatedAt, &sub.UpdatedAt, &sub.DeletedAt, &userId, &sub.Topic, &sub.DelId, &sub.RecvSeqId, &sub.ReadSeqId, &modeWant, &modeGiven, &sub.Private); err != nil { break } sub.User = store.EncodeUid(userId).String() sub.ModeWant.Scan(modeWant) sub.ModeGiven.Scan(modeGiven) subs = append(subs, sub) } if err == nil { err = rows.Err() } return subs, err } // SubsUpdate updates one or multiple subscriptions to a topic. func (a *adapter) SubsUpdate(topic string, user t.Uid, update map[string]any) error { ctx, cancel := a.getContextForTx() if cancel != nil { defer cancel() } tx, err := a.db.BeginTx(ctx, pgx.TxOptions{}) if err != nil { return err } defer func() { if err != nil { tx.Rollback(ctx) } }() cols, args := common.UpdateByMap(update) q := "UPDATE subscriptions SET " + strings.Join(cols, ",") + " WHERE topic=?" args = append(args, topic) if !user.IsZero() { // Update just one topic subscription q += " AND userid=?" args = append(args, store.DecodeUid(user)) } q, args = expandQuery(q, args...) if _, err = tx.Exec(ctx, q, args...); err != nil { return err } return tx.Commit(ctx) } // SubsDelete marks at most one subscription as deleted. func (a *adapter) SubsDelete(topic string, user t.Uid) error { ctx, cancel := a.getContext() if cancel != nil { defer cancel() } tx, err := a.db.Begin(ctx) if err != nil { return err } defer func() { if err != nil { tx.Rollback(ctx) } }() decoded_id := store.DecodeUid(user) now := t.TimeNow() res, err := tx.Exec(ctx, "UPDATE subscriptions SET updatedat=$1,deletedat=$2 WHERE topic=$3 AND userid=$4 AND deletedat IS NULL", now, now, topic, decoded_id) if err != nil { return err } affected := res.RowsAffected() if affected == 0 { // ensure tx.Rollback() above is ran err = t.ErrNotFound return err } // Channel readers cannot delete messages. if !t.IsChannel(topic) { // Remove records of messages soft-deleted by this user. _, err = tx.Exec(ctx, "DELETE FROM dellog WHERE topic=$1 AND deletedfor=$2", topic, decoded_id) if err != nil { return err } } if t.GetTopicCat(topic) == t.TopicCatGrp { // Decrement topic subscription count (only one subscription is deleted). _, err = tx.Exec(ctx, "UPDATE topics SET subcnt=subcnt-1 WHERE name=$1", topic) if err != nil { return err } } return tx.Commit(ctx) } // subsDelForUser marks user's subscriptions as deleted. func subsDelForUser(ctx context.Context, tx pgx.Tx, decoded_uid int64, hard bool) error { // Decrement subscription count for all topics the user is subscribed to. rows, err := tx.Query(ctx, "SELECT topic FROM subscriptions WHERE userid=$1 AND deletedat IS NULL", decoded_uid) if err != nil { return err } var topics []any for rows.Next() { var name string if err = rows.Scan(&name); err != nil { break } if t.IsChannel(name) { // Convert channel name to group name. name = t.ChnToGrp(name) } topics = append(topics, name) } if err == nil { err = rows.Err() } rows.Close() if err != nil { return err } if len(topics) > 0 { sql, args, _ := sqlx.In("UPDATE topics SET subcnt=subcnt-1 WHERE name IN (?)", topics) _, err = tx.Exec(ctx, sqlx.Rebind(sqlx.DOLLAR, sql), args...) if err != nil { return err } } if hard { // Hard delete: remove all subscriptions for the user. _, err = tx.Exec(ctx, "DELETE FROM subscriptions WHERE userid=$1", decoded_uid) } else { now := t.TimeNow() _, err = tx.Exec(ctx, "UPDATE subscriptions SET updatedat=$1,deletedat=$2 WHERE userid=$3 AND deletedat IS NULL;", now, now, decoded_uid) } return err } // SubsDelForUser marks user's subscriptions as deleted. func (a *adapter) SubsDelForUser(user t.Uid, hard bool) error { ctx, cancel := a.getContextForTx() if cancel != nil { defer cancel() } tx, err := a.db.BeginTx(ctx, pgx.TxOptions{}) if err != nil { return err } defer func() { if err != nil { tx.Rollback(ctx) } }() if err = subsDelForUser(ctx, tx, store.DecodeUid(user), hard); err != nil { return err } return tx.Commit(ctx) } // Find returns a list of users and group topics which match the given tags, such as "email:jdoe@example.com" or "tel:+18003287448". func (a *adapter) Find(caller, promoPrefix string, req [][]string, opt []string, activeOnly bool) ([]t.Subscription, error) { index := make(map[string]struct{}) var args []any constraint := "" allReq := t.FlattenDoubleSlice(req) for _, tag := range append(allReq, opt...) { args = append(args, tag) index[tag] = struct{}{} } if len(args) == 0 { // Nothing to search for. return nil, nil } constraint += "tg.tag IN (?) " constraint, args, err := sqlx.In(constraint, args) if err != nil { return nil, err } if activeOnly { args = append(args, t.StateOK) constraint += "AND state=? " } constraint = sqlx.Rebind(sqlx.DOLLAR, constraint) var matcher string if promoPrefix != "" { // The max number of tags is 16. Using 20 to make sure one prefix match is greater than all non-prefix matches. matcher = "SUM(CASE WHEN POSITION('" + promoPrefix + "' IN tg.tag)=1 THEN 20 ELSE 1 END)" } else { matcher = "COUNT(*)" } query := "SELECT CAST(u.id AS VARCHAR) AS topic,u.createdat,u.updatedat,FALSE,u.access::jsonb,0 AS subcnt,u.public::jsonb,u.trusted::jsonb,u.tags::jsonb," + matcher + " AS matches " + "FROM users AS u JOIN usertags AS tg ON tg.userid=u.id " + "WHERE " + constraint + "GROUP BY u.id,u.createdat,u.updatedat,u.access::jsonb,u.public::jsonb,u.trusted::jsonb,u.tags::jsonb " having := "" if len(allReq) > 0 { var a []any having, a = common.DisjunctionSql(req, "tg.tag") having = rebindWithStart(having, len(args)+1) query += having args = append(args, a...) } query += "UNION ALL " query += "SELECT t.name AS topic,t.createdat,t.updatedat,t.usebt,t.access::jsonb,t.subcnt,t.public::jsonb,t.trusted::jsonb,t.tags::jsonb," + matcher + " AS matches " + "FROM topics AS t JOIN topictags AS tg ON t.name=tg.topic " + "WHERE " + constraint + "GROUP BY t.name,t.createdat,t.updatedat,t.usebt,t.access::jsonb,t.subcnt,t.public::jsonb,t.trusted::jsonb,t.tags::jsonb " if having != "" { query += having } args = append(args, a.maxResults) query += "ORDER BY matches DESC, subcnt DESC LIMIT $" + strconv.Itoa(len(args)) ctx, cancel := a.getContext() if cancel != nil { defer cancel() } // Get users matched by tags, sort by number of matches from high to low. rows, err := a.db.Query(ctx, query, args...) if err != nil { return nil, err } defer rows.Close() // Fetch subscriptions var public, trusted any var access t.DefaultAccess var subcnt int var setTags t.StringSlice var ignored int var isChan bool var sub t.Subscription var subs []t.Subscription for rows.Next() { if err = rows.Scan(&sub.Topic, &sub.CreatedAt, &sub.UpdatedAt, &isChan, &access, &subcnt, &public, &trusted, &setTags, &ignored); err != nil { subs = nil break } if id, err := strconv.ParseInt(sub.Topic, 10, 64); err == nil { sub.Topic = store.EncodeUid(id).UserId() if sub.Topic == caller { // Skip the caller. continue } } if isChan { // This is a channel, convert grp to chn name. sub.Topic = t.GrpToChn(sub.Topic) } sub.SetSubCnt(subcnt) sub.SetPublic(public) sub.SetTrusted(trusted) sub.SetDefaultAccess(access.Auth, access.Anon) // Indicating that the mode is not set, not 'N'. sub.ModeGiven = t.ModeUnset sub.ModeWant = t.ModeUnset sub.Private = common.FilterFoundTags(setTags, index) subs = append(subs, sub) } if err == nil { err = rows.Err() } return subs, err } // FindOne returns topic or user which matches the given tag. func (a *adapter) FindOne(tag string) (string, error) { var args []any query := "SELECT t.name AS topic FROM topics AS t LEFT JOIN topictags AS tt ON t.name=tt.topic " + "WHERE tt.tag=?" args = append(args, tag) query += " UNION ALL " query += "SELECT CAST(u.id AS VARCHAR) AS topic FROM users AS u LEFT JOIN usertags AS ut ON ut.userid=u.id " + "WHERE ut.tag=?" args = append(args, tag) // LIMIT is applied to all resultant rows. query += " LIMIT 1" ctx, cancel := a.getContext() if cancel != nil { defer cancel() } query, args = expandQuery(query, args) rows, err := a.db.Query(ctx, query, args...) if err != nil { return "", err } defer rows.Close() var found string if rows.Next() { if err = rows.Scan(&found); err != nil { return "", err } // Check if the found value is a topic name or a user ID. // User IDs are returned as decoded decimal strings. if id, err := strconv.ParseInt(found, 10, 64); err == nil { found = store.EncodeUid(id).UserId() } } if err == nil { err = rows.Err() } return found, err } // Messages func (a *adapter) MessageSave(msg *t.Message) error { ctx, cancel := a.getContext() if cancel != nil { defer cancel() } // store assignes message ID, but we don't use it. Message IDs are not used anywhere. // Using a sequential ID provided by the database. var id int err := a.db.QueryRow(ctx, `INSERT INTO messages(createdAt,updatedAt,seqid,topic,"from",head,content) VALUES($1,$2,$3,$4,$5,$6,$7) RETURNING id`, msg.CreatedAt, msg.UpdatedAt, msg.SeqId, msg.Topic, store.DecodeUid(t.ParseUid(msg.From)), msg.Head, common.ToJSON(msg.Content)).Scan(&id) if err == nil { // Replacing ID given by store by ID given by the DB. msg.SetUid(t.Uid(id)) } return err } func (a *adapter) MessageGetAll(topic string, forUser t.Uid, opts *t.QueryOpt) ([]t.Message, error) { var limit = a.maxMessageResults args := []any{store.DecodeUid(forUser), topic} seqIdConstraint := "" if opts != nil { seqIdConstraint = "AND m.seqid " if len(opts.IdRanges) > 0 { constr, newargs := common.RangesToSql(opts.IdRanges) seqIdConstraint += constr args = append(args, newargs...) } else { seqIdConstraint += "BETWEEN ? AND ?" if opts.Since > 0 { args = append(args, opts.Since) } else { args = append(args, 0) } if opts.Before > 0 { // BETWEEN is inclusive-inclusive, Tinode API requires inclusive-exclusive, thus -1 args = append(args, opts.Before-1) } else { args = append(args, 1<<31-1) } } if opts.Limit > 0 && opts.Limit < limit { limit = opts.Limit } } args = append(args, limit) ctx, cancel := a.getContext() if cancel != nil { defer cancel() } query, args := expandQuery(`SELECT m.createdat,m.updatedat,m.deletedat,m.delid,m.seqid,m.topic,m."from",m.head,m.content`+ " FROM messages AS m LEFT JOIN dellog AS d"+ " ON d.topic=m.topic AND m.seqid BETWEEN d.low AND d.hi-1 AND d.deletedfor=?"+ " WHERE m.delid=0 AND m.topic=? "+seqIdConstraint+" AND d.deletedfor IS NULL"+ " ORDER BY m.seqid DESC LIMIT ?", args...) rows, err := a.db.Query(ctx, query, args...) if err != nil { return nil, err } defer rows.Close() msgs := make([]t.Message, 0, limit) for rows.Next() { var msg t.Message var from int64 if err = rows.Scan(&msg.CreatedAt, &msg.UpdatedAt, &msg.DeletedAt, &msg.DelId, &msg.SeqId, &msg.Topic, &from, &msg.Head, &msg.Content); err != nil { break } msg.From = store.EncodeUid(from).String() msgs = append(msgs, msg) } if err == nil { err = rows.Err() } return msgs, err } // Get ranges of deleted messages func (a *adapter) MessageGetDeleted(topic string, forUser t.Uid, opts *t.QueryOpt) ([]t.DelMessage, error) { var limit = a.maxResults var lower = 0 var upper = 1<<31 - 1 if opts != nil { if opts.Since > 0 { lower = opts.Since } if opts.Before > 1 { // DelRange is inclusive-exclusive, while BETWEEN is inclusive-inclisive. upper = opts.Before - 1 } if opts.Limit > 0 && opts.Limit < limit { limit = opts.Limit } } // Fetch log of deletions ctx, cancel := a.getContext() if cancel != nil { defer cancel() } rows, err := a.db.Query(ctx, "SELECT topic,deletedfor,delid,low,hi FROM dellog WHERE topic=$1 AND delid BETWEEN $2 AND $3"+ " AND (deletedFor=0 OR deletedFor=$4) ORDER BY delid LIMIT $5", topic, lower, upper, store.DecodeUid(forUser), limit) if err != nil { return nil, err } defer rows.Close() var dellog struct { Topic string Deletedfor int64 Delid int Low int Hi int } var dmsgs []t.DelMessage var dmsg t.DelMessage for rows.Next() { if err = rows.Scan(&dellog.Topic, &dellog.Deletedfor, &dellog.Delid, &dellog.Low, &dellog.Hi); err != nil { dmsgs = nil break } if dellog.Delid != dmsg.DelId { if dmsg.DelId > 0 { dmsgs = append(dmsgs, dmsg) } dmsg.DelId = dellog.Delid dmsg.Topic = dellog.Topic if dellog.Deletedfor > 0 { dmsg.DeletedFor = store.EncodeUid(dellog.Deletedfor).String() } else { dmsg.DeletedFor = "" } dmsg.SeqIdRanges = nil } if dellog.Hi <= dellog.Low+1 { dellog.Hi = 0 } dmsg.SeqIdRanges = append(dmsg.SeqIdRanges, t.Range{Low: dellog.Low, Hi: dellog.Hi}) } if err == nil { err = rows.Err() } if err == nil { if dmsg.DelId > 0 { dmsgs = append(dmsgs, dmsg) } } return dmsgs, err } func messageDeleteList(ctx context.Context, tx pgx.Tx, topic string, toDel *t.DelMessage) error { var err error if toDel == nil { // Whole topic is being deleted, thus also deleting all messages. _, err = tx.Exec(ctx, "DELETE FROM dellog WHERE topic=$1", topic) if err == nil { _, err = tx.Exec(ctx, "DELETE FROM messages WHERE topic=$1", topic) } // filemsglinks will be deleted because of ON DELETE CASCADE return err } // Only some messages are being deleted delRanges := toDel.SeqIdRanges if toDel.DeletedFor == "" { // Hard-deleting messages requires updates to the messages table. where := "m.topic=? " args := []any{topic} if len(delRanges) > 0 { rSql, rArgs := common.RangesToSql(delRanges) where += " AND m.seqid " + rSql args = append(args, rArgs...) } where += " AND m.deletedat IS NULL" // We are asked to delete messages no older than newerThan. if newerThan := toDel.GetNewerThan(); newerThan != nil { where += " AND m.createdat>?" args = append(args, newerThan) } // Find the actual IDs still present in the database. var seqIDs []int query, newargs := expandQuery("SELECT seqid FROM messages AS m WHERE "+where, args) rows, err := tx.Query(ctx, query, newargs...) if err != nil { return err } defer rows.Close() for rows.Next() { var seqID int if err := rows.Scan(&seqID); err != nil { return err } seqIDs = append(seqIDs, seqID) } if err = rows.Err(); err != nil { return err } if len(seqIDs) == 0 { // Nothing to delete. No need to make a log entry. All done. return nil } // Recalculate the actual ranges to delete. sort.Ints(seqIDs) delRanges = t.SliceToRanges(seqIDs) // Compose a new query with the new ranges. where = "m.topic=?" args = []any{topic} rSql, rArgs := common.RangesToSql(delRanges) where += " AND m.seqid " + rSql args = append(args, rArgs...) // No need to add anything else: deletedat etc is already accounted for. query, newargs = expandQuery("DELETE FROM filemsglinks AS fml USING messages AS m WHERE m.id=fml.msgid AND "+ where, args...) _, err = tx.Exec(ctx, query, newargs...) if err != nil { return err } query, newargs = expandQuery(`UPDATE messages AS m SET deletedat=?,delid=?,"from"=0,head=NULL,content=NULL WHERE `+ where, t.TimeNow(), toDel.DelId, args) _, err = tx.Exec(ctx, query, newargs...) if err != nil { return err } } // Now make log entries. Needed for both hard- and soft-deleting. // Prepare statement is not needed because the driver prepares the statement on first use then caches it. forUser := common.DecodeUidString(toDel.DeletedFor) for _, rng := range toDel.SeqIdRanges { if rng.Hi == 0 { // Dellog must contain valid Low and *Hi*. rng.Hi = rng.Low + 1 } if _, err = tx.Exec(ctx, "INSERT INTO dellog(topic,deletedfor,delid,low,hi) VALUES($1,$2,$3,$4,$5)", topic, forUser, toDel.DelId, rng.Low, rng.Hi); err != nil { break } } return err } // MessageDeleteList deletes messages in the given topic with seqIds from the list. func (a *adapter) MessageDeleteList(topic string, toDel *t.DelMessage) (err error) { ctx, cancel := a.getContextForTx() if cancel != nil { defer cancel() } tx, err := a.db.BeginTx(ctx, pgx.TxOptions{}) if err != nil { return err } defer func() { if err != nil { tx.Rollback(ctx) } }() if err = messageDeleteList(ctx, tx, topic, toDel); err != nil { return err } return tx.Commit(ctx) } func deviceHasher(deviceID string) string { // Generate custom key as [64-bit hash of device id] to ensure predictable // length of the key hasher := fnv.New64() hasher.Write([]byte(deviceID)) return strconv.FormatUint(uint64(hasher.Sum64()), 16) } // Device management for push notifications func (a *adapter) DeviceUpsert(uid t.Uid, def *t.DeviceDef) error { hash := deviceHasher(def.DeviceId) ctx, cancel := a.getContextForTx() if cancel != nil { defer cancel() } tx, err := a.db.BeginTx(ctx, pgx.TxOptions{}) if err != nil { return err } defer func() { if err != nil { tx.Rollback(ctx) } }() // Ensure uniqueness of the device ID: delete all records of the device ID _, err = tx.Exec(ctx, "DELETE FROM devices WHERE hash=$1", hash) if err != nil { return err } // Actually add/update DeviceId for the new user _, err = tx.Exec(ctx, "INSERT INTO devices(userid, hash, deviceId, platform, lastseen, lang) VALUES($1,$2,$3,$4,$5,$6)", store.DecodeUid(uid), hash, def.DeviceId, def.Platform, def.LastSeen, def.Lang) if err != nil { return err } return tx.Commit(ctx) } func (a *adapter) DeviceGetAll(uids ...t.Uid) (map[t.Uid][]t.DeviceDef, int, error) { var unums []any for _, uid := range uids { unums = append(unums, store.DecodeUid(uid)) } query, unums := expandQuery("SELECT userid,deviceid,platform,lastseen,lang FROM devices WHERE userid IN (?)", unums) ctx, cancel := a.getContext() if cancel != nil { defer cancel() } rows, err := a.db.Query(ctx, query, unums...) if err != nil { return nil, 0, err } defer rows.Close() var device struct { Userid int64 Deviceid string Platform string Lastseen time.Time Lang string } result := make(map[t.Uid][]t.DeviceDef) count := 0 for rows.Next() { if err = rows.Scan(&device.Userid, &device.Deviceid, &device.Platform, &device.Lastseen, &device.Lang); err != nil { break } uid := store.EncodeUid(device.Userid) udev := result[uid] udev = append(udev, t.DeviceDef{ DeviceId: device.Deviceid, Platform: device.Platform, LastSeen: device.Lastseen, Lang: device.Lang, }) result[uid] = udev count++ } if err == nil { err = rows.Err() } return result, count, err } func deviceDelete(ctx context.Context, tx pgx.Tx, uid t.Uid, deviceID string) error { var err error var res pgconn.CommandTag if deviceID == "" { res, err = tx.Exec(ctx, "DELETE FROM devices WHERE userid=$1", store.DecodeUid(uid)) } else { res, err = tx.Exec(ctx, "DELETE FROM devices WHERE userid=$1 AND hash=$2", store.DecodeUid(uid), deviceHasher(deviceID)) } if err == nil { if count := res.RowsAffected(); count == 0 { err = t.ErrNotFound } } return err } func (a *adapter) DeviceDelete(uid t.Uid, deviceID string) error { ctx, cancel := a.getContextForTx() if cancel != nil { defer cancel() } tx, err := a.db.BeginTx(ctx, pgx.TxOptions{}) if err != nil { return err } defer func() { if err != nil { tx.Rollback(ctx) } }() err = deviceDelete(ctx, tx, uid, deviceID) if err != nil { return err } return tx.Commit(ctx) } // Credential management // CredUpsert adds or updates a validation record. Returns true if inserted, false if updated. // 1. if credential is validated: // 1.1 Hard-delete unconfirmed equivalent record, if exists. // 1.2 Insert new. Report error if duplicate. // 2. if credential is not validated: // 2.1 Check if validated equivalent exist. If so, report an error. // 2.2 Soft-delete all unvalidated records of the same method. // 2.3 Undelete existing credential. Return if successful. // 2.4 Insert new credential record. func (a *adapter) CredUpsert(cred *t.Credential) (bool, error) { var err error ctx, cancel := a.getContextForTx() if cancel != nil { defer cancel() } tx, err := a.db.BeginTx(ctx, pgx.TxOptions{}) if err != nil { return false, err } defer func() { if err != nil { tx.Rollback(ctx) } }() now := t.TimeNow() userId := common.DecodeUidString(cred.User) // Enforce uniqueness: if credential is confirmed, "method:value" must be unique. // if credential is not yet confirmed, "userid:method:value" is unique. synth := cred.Method + ":" + cred.Value if !cred.Done { // Check if this credential is already validated. var done bool err = tx.QueryRow(ctx, "SELECT done FROM credentials WHERE synthetic=$1", synth).Scan(&done) if err == nil { // Assign err to ensure closing of a transaction. err = t.ErrDuplicate return false, err } if err != pgx.ErrNoRows { return false, err } // We are going to insert new record. synth = cred.User + ":" + synth // Adding new unvalidated credential. Deactivate all unvalidated records of this user and method. _, err = tx.Exec(ctx, "UPDATE credentials SET deletedat=$1 WHERE userid=$2 AND method=$3 AND done=FALSE", now, userId, cred.Method) if err != nil { return false, err } // Assume that the record exists and try to update it: undelete, update timestamp and response value. res, err := tx.Exec(ctx, "UPDATE credentials SET updatedat=$1,deletedat=NULL,resp=$2,done=FALSE WHERE synthetic=$3", cred.UpdatedAt, cred.Resp, synth) if err != nil { return false, err } // If record was updated, then all is fine. if numrows := res.RowsAffected(); numrows > 0 { return false, tx.Commit(ctx) } } else { // Hard-deleting unconformed record if it exists. _, err = tx.Exec(ctx, "DELETE FROM credentials WHERE synthetic=$1", cred.User+":"+synth) if err != nil { return false, err } } _, err = tx.Exec(ctx, "INSERT INTO credentials(createdat,updatedat,method,value,synthetic,userid,resp,done) "+ "VALUES($1,$2,$3,$4,$5,$6,$7,$8)", cred.CreatedAt, cred.UpdatedAt, cred.Method, cred.Value, synth, userId, cred.Resp, cred.Done) if err != nil { if isDupe(err) { return true, t.ErrDuplicate } return true, err } return true, tx.Commit(ctx) } // credDel deletes given validation method or all methods of the given user. // 1. If user is being deleted, hard-delete all records (method == "") // 2. If one value is being deleted: // 2.1 Delete it if it's valiated or if there were no attempts at validation // (otherwise it could be used to circumvent the limit on validation attempts). // 2.2 In that case mark it as soft-deleted. func credDel(ctx context.Context, tx pgx.Tx, uid t.Uid, method, value string) error { constraints := " WHERE userid=?" args := []any{store.DecodeUid(uid)} if method != "" { constraints += " AND method=?" args = append(args, method) if value != "" { constraints += " AND value=?" args = append(args, value) } } where, _ := expandQuery(constraints, args...) var err error var res pgconn.CommandTag if method == "" { // Case 1 res, err = tx.Exec(ctx, "DELETE FROM credentials"+where, args...) if err == nil { if count := res.RowsAffected(); count == 0 { err = t.ErrNotFound } } return err } // Case 2.1 res, err = tx.Exec(ctx, "DELETE FROM credentials"+where+" AND (done=TRUE OR retries=0)", args...) if err != nil { return err } if count := res.RowsAffected(); count > 0 { return nil } // Case 2.2 query, args := expandQuery("UPDATE credentials SET deletedat=?"+constraints, t.TimeNow(), args) res, err = tx.Exec(ctx, query, args...) if err == nil { if count := res.RowsAffected(); count >= 0 { err = t.ErrNotFound } } return err } // CredDel deletes either credentials of the given user. If method is blank all // credentials are removed. If value is blank all credentials of the given the // method are removed. func (a *adapter) CredDel(uid t.Uid, method, value string) error { ctx, cancel := a.getContextForTx() if cancel != nil { defer cancel() } tx, err := a.db.BeginTx(ctx, pgx.TxOptions{}) if err != nil { return err } defer func() { if err != nil { tx.Rollback(ctx) } }() err = credDel(ctx, tx, uid, method, value) if err != nil { return err } return tx.Commit(ctx) } // CredConfirm marks given credential method as confirmed. func (a *adapter) CredConfirm(uid t.Uid, method string) error { ctx, cancel := a.getContext() if cancel != nil { defer cancel() } res, err := a.db.Exec( ctx, "UPDATE credentials SET updatedat=$1,done=TRUE,synthetic=CONCAT(method,':',value) "+ "WHERE userid=$2 AND method=$3 AND deletedat IS NULL AND done=FALSE", t.TimeNow(), store.DecodeUid(uid), method) if err != nil { if isDupe(err) { return t.ErrDuplicate } return err } if numrows := res.RowsAffected(); numrows < 1 { return t.ErrNotFound } return nil } // CredFail increments failure count of the given validation method. func (a *adapter) CredFail(uid t.Uid, method string) error { ctx, cancel := a.getContext() if cancel != nil { defer cancel() } _, err := a.db.Exec(ctx, "UPDATE credentials SET updatedat=$1,retries=retries+1 WHERE userid=$2 AND method=$3 AND done=FALSE", t.TimeNow(), store.DecodeUid(uid), method) return err } // CredGetActive returns currently active unvalidated credential of the given user and method. func (a *adapter) CredGetActive(uid t.Uid, method string) (*t.Credential, error) { ctx, cancel := a.getContext() if cancel != nil { defer cancel() } var cred t.Credential err := a.db.QueryRow(ctx, "SELECT createdat,updatedat,method,value,resp,done,retries "+ "FROM credentials WHERE userid=$1 AND deletedat IS NULL AND method=$2 AND done=FALSE", store.DecodeUid(uid), method).Scan(&cred.CreatedAt, &cred.UpdatedAt, &cred.Method, &cred.Value, &cred.Resp, &cred.Done, &cred.Retries) if err != nil { if err == pgx.ErrNoRows { err = nil } return nil, err } cred.User = uid.String() return &cred, nil } // CredGetAll returns credential records for the given user and method, all or validated only. func (a *adapter) CredGetAll(uid t.Uid, method string, validatedOnly bool) ([]t.Credential, error) { query := "SELECT createdat,updatedat,method,value,resp,done,retries FROM credentials WHERE userid=$1 AND deletedat IS NULL" args := []any{store.DecodeUid(uid)} if method != "" { query += " AND method=$2" args = append(args, method) } if validatedOnly { query += " AND done=TRUE" } ctx, cancel := a.getContext() if cancel != nil { defer cancel() } var credentials []t.Credential rows, err := a.db.Query(ctx, query, args...) if err != nil { return nil, err } defer rows.Close() for rows.Next() { var cred t.Credential if err = rows.Scan(&cred.CreatedAt, &cred.UpdatedAt, &cred.Method, &cred.Value, &cred.Resp, &cred.Done, &cred.Retries); err != nil { credentials = nil break } credentials = append(credentials, cred) } user := uid.String() for i := range credentials { credentials[i].User = user } return credentials, err } // FileUploads // FileStartUpload initializes a file upload func (a *adapter) FileStartUpload(fd *t.FileDef) error { ctx, cancel := a.getContext() if cancel != nil { defer cancel() } var user any if fd.User != "" { user = store.DecodeUid(t.ParseUid(fd.User)) } _, err := a.db.Exec(ctx, "INSERT INTO fileuploads(id,createdat,updatedat,userid,status,mimetype,size,etag,location) "+ "VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9)", store.DecodeUid(fd.Uid()), fd.CreatedAt, fd.UpdatedAt, user, fd.Status, fd.MimeType, fd.Size, fd.ETag, fd.Location) return err } // FileFinishUpload marks file upload as completed, successfully or otherwise func (a *adapter) FileFinishUpload(fd *t.FileDef, success bool, size int64) (*t.FileDef, error) { ctx, cancel := a.getContext() if cancel != nil { defer cancel() } tx, err := a.db.BeginTx(ctx, pgx.TxOptions{}) if err != nil { return nil, err } defer func() { if err != nil { tx.Rollback(ctx) } }() now := t.TimeNow() if success { _, err = tx.Exec(ctx, "UPDATE fileuploads SET updatedat=$1,status=$2,size=$3,etag=$4,location=$5 WHERE id=$6", now, t.UploadCompleted, size, fd.ETag, fd.Location, store.DecodeUid(fd.Uid())) if err != nil { return nil, err } fd.Status = t.UploadCompleted fd.Size = size } else { // Deleting the record: there is no value in keeping it in the DB. _, err = tx.Exec(ctx, "DELETE FROM fileuploads WHERE id=$1", store.DecodeUid(fd.Uid())) if err != nil { return nil, err } fd.Status = t.UploadFailed fd.Size = 0 } fd.UpdatedAt = now return fd, tx.Commit(ctx) } // FileGet fetches a record of a specific file func (a *adapter) FileGet(fid string) (*t.FileDef, error) { id := t.ParseUid(fid) if id.IsZero() { return nil, t.ErrMalformed } ctx, cancel := a.getContext() if cancel != nil { defer cancel() } var fd t.FileDef var ID int64 var userId int64 err := a.db.QueryRow(ctx, "SELECT id,createdat,updatedat,userid AS user,status,mimetype,size,etag,location "+ "FROM fileuploads WHERE id=$1", store.DecodeUid(id)).Scan(&ID, &fd.CreatedAt, &fd.UpdatedAt, &userId, &fd.Status, &fd.MimeType, &fd.Size, &fd.ETag, &fd.Location) if err == pgx.ErrNoRows { return nil, nil } if err != nil { return nil, err } fd.Id = common.EncodeUidString(fd.Id).String() fd.User = store.EncodeUid(userId).String() return &fd, nil } // FileDeleteUnused deletes file upload records. func (a *adapter) FileDeleteUnused(olderThan time.Time, limit int) ([]string, error) { ctx, cancel := a.getContextForTx() if cancel != nil { defer cancel() } tx, err := a.db.BeginTx(ctx, pgx.TxOptions{}) if err != nil { return nil, err } defer func() { if err != nil { tx.Rollback(ctx) } }() // Garbage collecting entries which as either marked as deleted, or lack message references, or have no user assigned. query := "SELECT fu.id,fu.location FROM fileuploads AS fu LEFT JOIN filemsglinks AS fml ON fml.fileid=fu.id " + "WHERE fml.id IS NULL" var args []any if !olderThan.IsZero() { query += " AND fu.updatedat 0 { query += " LIMIT ?" args = append(args, limit) } query, _ = expandQuery(query, args...) rows, err := tx.Query(ctx, query, args...) if err != nil { return nil, err } defer rows.Close() var locations []string var ids []any for rows.Next() { var id int var loc string if err = rows.Scan(&id, &loc); err != nil { break } if loc != "" { locations = append(locations, loc) } ids = append(ids, id) } if err == nil { err = rows.Err() } if err != nil { return nil, err } if len(ids) > 0 { query, ids = expandQuery("DELETE FROM fileuploads WHERE id IN (?)", ids) _, err = tx.Exec(ctx, query, ids...) if err != nil { return nil, err } } return locations, tx.Commit(ctx) } // FileLinkAttachments connects given topic or message to the file record IDs from the list. func (a *adapter) FileLinkAttachments(topic string, userId, msgId t.Uid, fids []string) error { if len(fids) == 0 || (topic == "" && msgId.IsZero() && userId.IsZero()) { return t.ErrMalformed } now := t.TimeNow() var args []any var linkId any var linkBy string if !msgId.IsZero() { linkBy = "msgid" linkId = int64(msgId) } else if topic != "" { linkBy = "topic" linkId = topic // Only one attachment per topic is permitted at this time. fids = fids[0:1] } else { linkBy = "userid" linkId = store.DecodeUid(userId) // Only one attachment per user is permitted at this time. fids = fids[0:1] } // Decoded ids var dids []any for _, fid := range fids { id := t.ParseUid(fid) if id.IsZero() { return t.ErrMalformed } dids = append(dids, store.DecodeUid(id)) } for _, id := range dids { // createdat,fileid,[msgid|topic|userid] args = append(args, now, id, linkId) } ctx, cancel := a.getContextForTx() if cancel != nil { defer cancel() } tx, err := a.db.BeginTx(ctx, pgx.TxOptions{}) if err != nil { return err } defer func() { if err != nil { tx.Rollback(ctx) } }() // Unlink earlier uploads on the same topic or user allowing them to be garbage-collected. if msgId.IsZero() { sql := "DELETE FROM filemsglinks WHERE " + linkBy + "=$1" _, err = tx.Exec(ctx, sql, linkId) if err != nil { return err } } query, args := expandQuery("INSERT INTO filemsglinks(createdat,fileid,"+linkBy+") VALUES (?,?,?)"+ strings.Repeat(",(?,?,?)", len(dids)-1), args...) _, err = tx.Exec(ctx, query, args...) if err != nil { return err } return tx.Commit(ctx) } // PCacheGet reads a persistet cache entry. func (a *adapter) PCacheGet(key string) (string, error) { ctx, cancel := a.getContext() if cancel != nil { defer cancel() } var value string if err := a.db.QueryRow(ctx, `SELECT "value" FROM kvmeta WHERE "key"=$1 LIMIT 1`, key).Scan(&value); err != nil { if err == pgx.ErrNoRows { return "", t.ErrNotFound } return "", err } return value, nil } // PCacheUpsert creates or updates a persistent cache entry. func (a *adapter) PCacheUpsert(key string, value string, failOnDuplicate bool) error { if strings.Contains(key, "%") { // Do not allow % in keys: it interferes with LIKE query. return t.ErrMalformed } ctx, cancel := a.getContext() if cancel != nil { defer cancel() } var action string if !failOnDuplicate { action = ` ON CONFLICT ("key") DO UPDATE SET createdat=$2,"value"=$3` } _, err := a.db.Exec(ctx, `INSERT INTO kvmeta("key",createdat,"value") VALUES($1,$2,$3)`+action, key, t.TimeNow(), value) if isDupe(err) { return t.ErrDuplicate } return err } // PCacheDelete deletes one persistent cache entry. func (a *adapter) PCacheDelete(key string) error { ctx, cancel := a.getContext() if cancel != nil { defer cancel() } _, err := a.db.Exec(ctx, `DELETE FROM kvmeta WHERE "key"=$1`, key) return err } // PCacheExpire expires old entries with the given key prefix. func (a *adapter) PCacheExpire(keyPrefix string, olderThan time.Time) error { if keyPrefix == "" { return t.ErrMalformed } ctx, cancel := a.getContext() if cancel != nil { defer cancel() } _, err := a.db.Exec(ctx, `DELETE FROM kvmeta WHERE "key" LIKE $1 AND createdat<$2`, keyPrefix+"%", olderThan) return err } // GetTestDB returns a currently open database connection. func (a *adapter) GetTestDB() any { return a.db } // Helper functions // Check if MySQL error is a Error Code: 1062. Duplicate entry ... for key ... func isDupe(err error) bool { if err == nil { return false } msg := err.Error() return strings.Contains(msg, "SQLSTATE 23505") } func isMissingTable(err error) bool { if err == nil { return false } msg := err.Error() return strings.Contains(msg, "SQLSTATE 42P01") } func isMissingDb(err error) bool { if err == nil { return false } msg := err.Error() return strings.Contains(msg, "SQLSTATE 3D000") } // setConnStr converts a config structure to a DSN connection string. func setConnStr(c configType) (string, error) { // Default to disable SSL mode. sslMode := "disable" if c.SSLMode != "" { sslMode = c.SSLMode } if c.User == "" || c.Passwd == "" || c.Host == "" || c.Port == "" || c.DBName == "" { return "", errors.New("adapter postgres invalid config value") } connStr := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=%s&connect_timeout=%d", c.User, c.Passwd, c.Host, c.Port, c.DBName, sslMode, c.SqlTimeout) return connStr, nil } // expandQuery replaces the placeholders in the query with the actual values and returns // the expanded query and the arguments to be used in the query. func expandQuery(query string, args ...any) (string, []any) { var expandedArgs []any var expandedQuery string if len(args) != strings.Count(query, "?") { args = flattenSlice(args) } expandedQuery, expandedArgs, _ = sqlx.In(query, args...) return sqlx.Rebind(sqlx.DOLLAR, expandedQuery), expandedArgs } // flatMap converts a slice of mixed values/slices into a flat slice. func flattenSlice(slice []any) []any { var result []any for _, v := range slice { switch reflect.TypeOf(v).Kind() { case reflect.Slice: s := reflect.ValueOf(v) for i := 0; i < s.Len(); i++ { result = append(result, s.Index(i).Interface()) } default: result = append(result, v) } } return result } // Rebind a query from ? to the target $ with custom initial value. func rebindWithStart(query string, startAt int) string { // Add space enough for 10 params before we have to allocate rqb := make([]byte, 0, len(query)+10) var i, j = 0, startAt for i = strings.Index(query, "?"); i != -1; i = strings.Index(query, "?") { rqb = append(rqb, query[:i]...) rqb = append(rqb, '$') rqb = strconv.AppendInt(rqb, int64(j), 10) j++ query = query[i+1:] } return string(append(rqb, query...)) } // GetTestAdapter returns an adapter object. Useful for running tests. func GetTestAdapter() *adapter { return &adapter{} } func init() { store.RegisterAdapter(&adapter{}) } ================================================ FILE: server/db/postgres/blank.go ================================================ //go:build !postgres // +build !postgres // This file is needed for conditional compilation. It's used when // the build tag 'postgres' is not defined. Otherwise the adapter.go // is compiled. package postgres ================================================ FILE: server/db/postgres/schema.sql ================================================ # The MySQL and PostrgreSQL schemas are identical save for differences in SQL flavors. # SEE ../mysql/schema.sql. ================================================ FILE: server/db/postgres/tests/postgres_test.go ================================================ // To test another db backend: // 1) Create GetAdapter function inside your db backend adapter package (like one inside postgres adapter) // 2) Uncomment your db backend package ('backend' named package) // 3) Write own initConnectionToDb and 'db' variable // 4) Replace postgres specific db queries inside test to your own queries. // 5) Run. package tests import ( "context" "database/sql" "encoding/json" "flag" "fmt" "log" "os" "reflect" "strconv" "testing" "time" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/jackc/pgx/v4/pgxpool" adapter "github.com/tinode/chat/server/db" "github.com/tinode/chat/server/store" jcr "github.com/tinode/jsonco" "github.com/tinode/chat/server/db/common/test_data" backend "github.com/tinode/chat/server/db/postgres" "github.com/tinode/chat/server/logs" "github.com/tinode/chat/server/store/types" ) type configType struct { // If Reset=true test will recreate database every time it runs Reset bool `json:"reset_db_data"` // Configurations for individual adapters. Adapters map[string]json.RawMessage `json:"adapters"` } var config configType var adp adapter.Adapter var db *pgxpool.Pool var testData *test_data.TestData var ctx context.Context var dummyUid1 = types.Uid(12345) var dummyUid2 = types.Uid(54321) func TestCreateDb(t *testing.T) { if err := adp.CreateDb(config.Reset); err != nil { t.Fatal(err) } // Saved db is closed, get a fresh one. db = adp.GetTestDB().(*pgxpool.Pool) } // ================== Create tests ================================ func TestUserCreate(t *testing.T) { for _, user := range testData.Users { if err := adp.UserCreate(user); err != nil { t.Error(err) } } var count int err := db.QueryRow(ctx, "SELECT COUNT(*) FROM users").Scan(&count) if err != nil { t.Error(err) } if count == 0 { t.Error("No users created!") } } func TestCredUpsert(t *testing.T) { // Test just inserts: for i := 0; i < 2; i++ { inserted, err := adp.CredUpsert(testData.Creds[i]) if err != nil { t.Fatal(err) } if !inserted { t.Error("Should be inserted, but updated") } } // Test duplicate: _, err := adp.CredUpsert(testData.Creds[1]) if err != types.ErrDuplicate { t.Error("Should return duplicate error but got", err) } _, err = adp.CredUpsert(testData.Creds[2]) if err != types.ErrDuplicate { t.Error("Should return duplicate error but got", err) } // Test add new unvalidated credentials inserted, err := adp.CredUpsert(testData.Creds[3]) if err != nil { t.Fatal(err) } if !inserted { t.Error("Should be inserted, but updated") } inserted, err = adp.CredUpsert(testData.Creds[3]) if err != nil { t.Fatal(err) } if inserted { t.Error("Should be updated, but inserted") } // Just insert other creds (used in other tests) for _, cred := range testData.Creds[4:] { _, err = adp.CredUpsert(cred) if err != nil { t.Fatal(err) } } } func TestAuthAddRecord(t *testing.T) { for _, rec := range testData.Recs { err := adp.AuthAddRecord(types.ParseUserId("usr"+rec.UserId), rec.Scheme, rec.Unique, rec.AuthLvl, rec.Secret, rec.Expires) if err != nil { t.Fatal(err) } } //Test duplicate err := adp.AuthAddRecord(types.ParseUserId("usr"+testData.Users[0].Id), testData.Recs[0].Scheme, testData.Recs[0].Unique, testData.Recs[0].AuthLvl, testData.Recs[0].Secret, testData.Recs[0].Expires) if err != types.ErrDuplicate { t.Fatal("Should be duplicate error but got", err) } } func TestTopicCreate(t *testing.T) { err := adp.TopicCreate(testData.Topics[0]) if err != nil { t.Error(err) } // Update topic SeqId because it's not saved at creation time but used by the tests. err = adp.TopicUpdate(testData.Topics[0].Id, map[string]interface{}{ "seqid": testData.Topics[0].SeqId, }) if err != nil { t.Error(err) } for _, tpc := range testData.Topics[3:] { err = adp.TopicCreate(tpc) if err != nil { t.Error(err) } } } func decodeUid(u string) int64 { return store.DecodeUid(types.ParseUid(u)) } func encodeUid(u string) types.Uid { id, _ := strconv.ParseInt(u, 10, 64) return store.EncodeUid(int64(id)) } func TestTopicCreateP2P(t *testing.T) { err := adp.TopicCreateP2P(testData.Subs[2], testData.Subs[3]) if err != nil { t.Fatal(err) } oldModeGiven := testData.Subs[2].ModeGiven testData.Subs[2].ModeGiven = 255 err = adp.TopicCreateP2P(testData.Subs[4], testData.Subs[2]) if err != nil { t.Fatal(err) } var got types.Subscription var userId int64 var modeWant, modeGiven []byte err = db.QueryRow(ctx, "SELECT createdat,updatedat,deletedat,userid,topic,delid,recvseqid,readseqid,modewant,modegiven,private FROM subscriptions WHERE topic=$1 AND userid=$2", testData.Subs[2].Topic, decodeUid(testData.Subs[2].User)).Scan(&got.CreatedAt, &got.UpdatedAt, &got.DeletedAt, &userId, &got.Topic, &got.DelId, &got.RecvSeqId, &got.ReadSeqId, &modeWant, &modeGiven, &got.Private) if err != nil { t.Fatal(err) } got.ModeGiven.Scan(modeGiven) if got.ModeGiven == oldModeGiven { t.Error("ModeGiven update failed") } } func TestTopicShare(t *testing.T) { if err := adp.TopicShare(testData.Subs[0].Topic, testData.Subs); err != nil { t.Fatal(err) } // Must save recvseqid and readseqid separately because TopicShare // ignores them. for _, sub := range testData.Subs { adp.SubsUpdate(sub.Topic, types.ParseUid(sub.User), map[string]any{ "delid": sub.DelId, "recvseqid": sub.RecvSeqId, "readseqid": sub.ReadSeqId, }) } // Update topic SeqId because it's not saved at creation time but used by the tests. for _, tpc := range testData.Topics { err := adp.TopicUpdate(tpc.Id, map[string]any{ "seqid": tpc.SeqId, "delid": tpc.DelId, }) if err != nil { t.Error(err) } } } func TestMessageSave(t *testing.T) { for _, msg := range testData.Msgs { err := adp.MessageSave(msg) if err != nil { t.Fatal(err) } } // Some messages are soft deleted, but it's ignored by adp.MessageSave for _, msg := range testData.Msgs { if len(msg.DeletedFor) > 0 { for _, del := range msg.DeletedFor { toDel := types.DelMessage{ Topic: msg.Topic, DeletedFor: del.User, DelId: del.DelId, SeqIdRanges: []types.Range{{Low: msg.SeqId}}, } adp.MessageDeleteList(msg.Topic, &toDel) } } } } func TestFileStartUpload(t *testing.T) { for _, f := range testData.Files { err := adp.FileStartUpload(f) if err != nil { t.Fatal(err) } } } // ================== Read tests ================================== func TestUserGet(t *testing.T) { // Test not found got, err := adp.UserGet(dummyUid1) if err == nil && got != nil { t.Error("user should be nil.") } got, err = adp.UserGet(types.ParseUserId("usr" + testData.Users[0].Id)) if err != nil { t.Fatal(err) } // User agent is not stored when creating a user. Make sure it's the same. got.UserAgent = testData.Users[0].UserAgent if !reflect.DeepEqual(got, testData.Users[0]) { t.Error(mismatchErrorString("User", got, testData.Users[0])) } } func TestUserGetAll(t *testing.T) { // Test not found (dummy UIDs). got, err := adp.UserGetAll(dummyUid1, dummyUid2) if err != nil { t.Fatal(err) } if len(got) > 0 { t.Error("result users should be zero length, got", len(got)) } got, err = adp.UserGetAll(types.ParseUserId("usr"+testData.Users[0].Id), types.ParseUserId("usr"+testData.Users[1].Id)) if err != nil { t.Fatal(err) } if len(got) != 2 { t.Fatal(mismatchErrorString("resultUsers length", len(got), 2)) } for i, usr := range got { // User agent is not compared. usr.UserAgent = testData.Users[i].UserAgent if !reflect.DeepEqual(&usr, testData.Users[i]) { t.Error(mismatchErrorString("User", &usr, testData.Users[i])) } } } func TestUserGetByCred(t *testing.T) { // Test not found got, err := adp.UserGetByCred("foo", "bar") if err != nil { t.Fatal(err) } if got != types.ZeroUid { t.Error("result uid should be ZeroUid") } got, _ = adp.UserGetByCred(testData.Creds[0].Method, testData.Creds[0].Value) if got != types.ParseUserId("usr"+testData.Creds[0].User) { t.Error(mismatchErrorString("Uid", got, types.ParseUserId("usr"+testData.Creds[0].User))) } } func TestCredGetActive(t *testing.T) { got, err := adp.CredGetActive(types.ParseUserId("usr"+testData.Users[2].Id), "tel") if err != nil { t.Error(err) } if !reflect.DeepEqual(got, testData.Creds[3]) { t.Error(mismatchErrorString("Credential", got, testData.Creds[3])) } // Test not found got, err = adp.CredGetActive(dummyUid1, "") if err != nil { t.Error(err) } if got != nil { t.Error("result should be nil, but got", got) } } func TestCredGetAll(t *testing.T) { got, err := adp.CredGetAll(types.ParseUserId("usr"+testData.Users[2].Id), "", false) if err != nil { t.Fatal(err) } if len(got) != 3 { t.Error(mismatchErrorString("Credentials length", len(got), 3)) } got, _ = adp.CredGetAll(types.ParseUserId("usr"+testData.Users[2].Id), "tel", false) if len(got) != 2 { t.Error(mismatchErrorString("Credentials length", len(got), 2)) } got, _ = adp.CredGetAll(types.ParseUserId("usr"+testData.Users[2].Id), "", true) if len(got) != 1 { t.Error(mismatchErrorString("Credentials length", len(got), 1)) } got, _ = adp.CredGetAll(types.ParseUserId("usr"+testData.Users[2].Id), "tel", true) if len(got) != 1 { t.Error(mismatchErrorString("Credentials length", len(got), 1)) } } func TestAuthGetUniqueRecord(t *testing.T) { uid, authLvl, secret, expires, err := adp.AuthGetUniqueRecord("basic:alice") if err != nil { t.Fatal(err) } if uid != types.ParseUserId("usr"+testData.Recs[0].UserId) || authLvl != testData.Recs[0].AuthLvl || !reflect.DeepEqual(secret, testData.Recs[0].Secret) || expires != testData.Recs[0].Expires { got := fmt.Sprintf("%v %v %v %v", uid, authLvl, secret, expires) want := fmt.Sprintf("%v %v %v %v", testData.Recs[0].UserId, testData.Recs[0].AuthLvl, testData.Recs[0].Secret, testData.Recs[0].Expires) t.Error(mismatchErrorString("Auth record", got, want)) } // Test not found uid, _, _, _, err = adp.AuthGetUniqueRecord("qwert:asdfg") if err == nil && !uid.IsZero() { t.Error("Auth record found but shouldn't. Uid:", uid.String()) } } func TestAuthGetRecord(t *testing.T) { recId, authLvl, secret, expires, err := adp.AuthGetRecord(types.ParseUserId("usr"+testData.Recs[0].UserId), "basic") if err != nil { t.Fatal(err) } if recId != testData.Recs[0].Unique || authLvl != testData.Recs[0].AuthLvl || !reflect.DeepEqual(secret, testData.Recs[0].Secret) || expires != testData.Recs[0].Expires { got := fmt.Sprintf("%v %v %v %v", recId, authLvl, secret, expires) want := fmt.Sprintf("%v %v %v %v", testData.Recs[0].Unique, testData.Recs[0].AuthLvl, testData.Recs[0].Secret, testData.Recs[0].Expires) t.Error(mismatchErrorString("Auth record", got, want)) } // Test not found recId, _, _, _, err = adp.AuthGetRecord(types.Uid(123), "scheme") if err != types.ErrNotFound { t.Error("Auth record found but shouldn't. recId:", recId) } } func TestTopicGet(t *testing.T) { got, err := adp.TopicGet(testData.Topics[0].Id) if err != nil { t.Fatal(err) } if !reflect.DeepEqual(got, testData.Topics[0]) { t.Error(mismatchErrorString("Topic", got, testData.Topics[0])) } // Test not found got, err = adp.TopicGet("asdfasdfasdf") if err != nil { t.Fatal(err) } if got != nil { t.Error("Topic should be nil but got:", got) } } func TestTopicsForUser(t *testing.T) { qOpts := types.QueryOpt{ Topic: "p2p9AVDamaNCRbfKzGSh3mE0w", Limit: 999, } gotSubs, err := adp.TopicsForUser(types.ParseUserId("usr"+testData.Users[0].Id), false, &qOpts) if err != nil { t.Fatal(err) } if len(gotSubs) != 1 { t.Error(mismatchErrorString("Subs length", len(gotSubs), 1)) } gotSubs, err = adp.TopicsForUser(types.ParseUserId("usr"+testData.Users[1].Id), true, nil) if err != nil { t.Fatal(err) } if len(gotSubs) != 2 { t.Error(mismatchErrorString("Subs length (2)", len(gotSubs), 2)) } qOpts.Topic = "" ims := testData.Now.Add(15 * time.Minute) qOpts.IfModifiedSince = &ims gotSubs, err = adp.TopicsForUser(types.ParseUserId("usr"+testData.Users[0].Id), false, &qOpts) if err != nil { t.Fatal(err) } if len(gotSubs) != 1 { t.Error(mismatchErrorString("Subs length (IMS)", len(gotSubs), 1)) } ims = time.Now().Add(15 * time.Minute) gotSubs, err = adp.TopicsForUser(types.ParseUserId("usr"+testData.Users[0].Id), false, &qOpts) if err != nil { t.Fatal(err) } if len(gotSubs) != 0 { t.Error(mismatchErrorString("Subs length (IMS 2)", len(gotSubs), 0)) } } func TestUsersForTopic(t *testing.T) { qOpts := types.QueryOpt{ User: types.ParseUserId("usr" + testData.Users[0].Id), Limit: 999, } gotSubs, err := adp.UsersForTopic("grpgRXf0rU4uR4", false, &qOpts) if err != nil { t.Fatal(err) } if len(gotSubs) != 1 { t.Error(mismatchErrorString("Subs length", len(gotSubs), 1)) } gotSubs, err = adp.UsersForTopic("grpgRXf0rU4uR4", true, nil) if err != nil { t.Fatal(err) } if len(gotSubs) != 2 { t.Error(mismatchErrorString("Subs length", len(gotSubs), 2)) } gotSubs, err = adp.UsersForTopic("p2p9AVDamaNCRbfKzGSh3mE0w", false, nil) if err != nil { t.Fatal(err) } if len(gotSubs) != 2 { t.Error(mismatchErrorString("Subs length", len(gotSubs), 2)) } } func TestOwnTopics(t *testing.T) { gotSubs, err := adp.OwnTopics(types.ParseUserId("usr" + testData.Users[0].Id)) if err != nil { t.Fatal(err) } if len(gotSubs) != 1 { t.Fatalf("Got topic length %v instead of %v", len(gotSubs), 1) } if gotSubs[0] != testData.Topics[0].Id { t.Errorf("Got topic %v instead of %v", gotSubs[0], testData.Topics[0].Id) } } func TestChannelsForUser(t *testing.T) { // Test channels for user (PostgreSQL specific test) channels, err := adp.ChannelsForUser(types.ParseUserId("usr" + testData.Users[0].Id)) if err != nil { t.Fatal(err) } // Should return empty slice since we don't have channel subscriptions in test data if len(channels) != 0 { t.Error(mismatchErrorString("Channels length", len(channels), 0)) } } func TestSubscriptionGet(t *testing.T) { got, err := adp.SubscriptionGet(testData.Topics[0].Id, types.ParseUserId("usr"+testData.Users[0].Id), false) if err != nil { t.Error(err) } if diff := cmp.Diff(got, testData.Subs[0], cmpopts.IgnoreUnexported(types.Subscription{}, types.ObjHeader{})); diff != "" { t.Error(mismatchErrorString("Subs", diff, "")) } // Test not found got, err = adp.SubscriptionGet("dummytopic", dummyUid1, false) if err != nil { t.Error(err) } if got != nil { t.Error("result sub should be nil.") } } func TestSubsForUser(t *testing.T) { gotSubs, err := adp.SubsForUser(types.ParseUserId("usr" + testData.Users[0].Id)) if err != nil { t.Error(err) } if len(gotSubs) != 2 { t.Error(mismatchErrorString("Subs length", len(gotSubs), 2)) } // Test not found gotSubs, err = adp.SubsForUser(types.ParseUserId("usr12345678")) if err != nil { t.Error(err) } if len(gotSubs) != 0 { t.Error(mismatchErrorString("Subs length", len(gotSubs), 0)) } } func TestSubsForTopic(t *testing.T) { qOpts := types.QueryOpt{ User: types.ParseUserId("usr" + testData.Users[0].Id), Limit: 999, } gotSubs, err := adp.SubsForTopic(testData.Topics[0].Id, false, &qOpts) if err != nil { t.Error(err) } if len(gotSubs) != 1 { t.Error(mismatchErrorString("Subs length", len(gotSubs), 1)) } // Test not found gotSubs, err = adp.SubsForTopic("dummytopicid", false, nil) if err != nil { t.Error(err) } if len(gotSubs) != 0 { t.Error(mismatchErrorString("Subs length", len(gotSubs), 0)) } } func TestFind(t *testing.T) { reqTags := [][]string{{"alice", "bob", "carol", "travel", "qwer", "asdf", "zxcv"}} got, err := adp.Find("usr"+testData.Users[2].Id, "", reqTags, nil, true) if err != nil { t.Error(err) } else if len(got) != 3 { t.Error(mismatchErrorString("result length", len(got), 3)) } } func TestFindOne(t *testing.T) { // Test PostgreSQL specific FindOne method found, err := adp.FindOne("alice") if err != nil { t.Error(err) } // Should find the user with alice tag if found == "" { t.Error("Expected to find user with alice tag") } // Test not found found, err = adp.FindOne("nonexistent") if err != nil { t.Error(err) } if found != "" { t.Error("Should not find nonexistent tag") } } func TestMessageGetAll(t *testing.T) { opts := types.QueryOpt{ Since: 1, Before: 2, Limit: 999, } gotMsgs, err := adp.MessageGetAll(testData.Topics[0].Id, types.ParseUserId("usr"+testData.Users[0].Id), &opts) if err != nil { t.Fatal(err) } if len(gotMsgs) != 1 { t.Error(mismatchErrorString("Messages length opts", len(gotMsgs), 1)) } gotMsgs, _ = adp.MessageGetAll(testData.Topics[0].Id, types.ParseUserId("usr"+testData.Users[0].Id), nil) if len(gotMsgs) != 2 { t.Error(mismatchErrorString("Messages length no opts", len(gotMsgs), 2)) } gotMsgs, _ = adp.MessageGetAll(testData.Topics[0].Id, types.ZeroUid, nil) if len(gotMsgs) != 3 { t.Error(mismatchErrorString("Messages length zero uid", len(gotMsgs), 3)) } } func TestFileGet(t *testing.T) { // General test done during TestFileFinishUpload(). // Test not found got, err := adp.FileGet("dummyfileid") if err != nil && got != nil { t.Error("File found but shouldn't:", got) } } // ================== Update tests ================================ func TestUserUpdate(t *testing.T) { update := map[string]any{ "UserAgent": "Test Agent v0.11", "UpdatedAt": testData.Now.Add(30 * time.Minute), } err := adp.UserUpdate(types.ParseUserId("usr"+testData.Users[0].Id), update) if err != nil { t.Fatal(err) } var got struct { UserAgent string UpdatedAt time.Time CreatedAt time.Time } err = db.QueryRow(ctx, "SELECT useragent, updatedat, createdat FROM users WHERE id=$1", decodeUid(testData.Users[0].Id)).Scan(&got.UserAgent, &got.UpdatedAt, &got.CreatedAt) if err != nil { t.Fatal(err) } if got.UserAgent != "Test Agent v0.11" { t.Error(mismatchErrorString("UserAgent", got.UserAgent, "Test Agent v0.11")) } if got.UpdatedAt == got.CreatedAt { t.Error("UpdatedAt field not updated") } } func TestUserUpdateTags(t *testing.T) { addTags := testData.Tags[0] removeTags := testData.Tags[1] resetTags := testData.Tags[2] uid := types.ParseUserId("usr" + testData.Users[0].Id) got, err := adp.UserUpdateTags(uid, addTags, nil, nil) if err != nil { t.Fatal(err) } want := []string{"alice", "tag1"} if !reflect.DeepEqual(got, want) { t.Error(mismatchErrorString("Tags", got, want)) } got, err = adp.UserUpdateTags(uid, nil, removeTags, nil) if err != nil { t.Fatal(err) } want = nil if !reflect.DeepEqual(got, want) { t.Error(mismatchErrorString("Tags", got, want)) } got, err = adp.UserUpdateTags(uid, nil, nil, resetTags) if err != nil { t.Fatal(err) } want = []string{"alice", "tag111", "tag333"} if !reflect.DeepEqual(got, want) { t.Error(mismatchErrorString("Tags", got, want)) } got, err = adp.UserUpdateTags(uid, addTags, removeTags, nil) if err != nil { t.Fatal(err) } want = []string{"tag111", "tag333"} if !reflect.DeepEqual(got, want) { t.Error(mismatchErrorString("Tags", got, want)) } } func TestUserGetUnvalidated(t *testing.T) { // Test PostgreSQL specific method cutoff := time.Now().Add(-24 * time.Hour) uids, err := adp.UserGetUnvalidated(cutoff, 10) if err != nil { t.Error(err) } // Should return empty slice since all test users are considered validated if len(uids) > 0 { t.Error("Expected no unvalidated users in test data") } } func TestCredFail(t *testing.T) { err := adp.CredFail(types.ParseUserId("usr"+testData.Creds[3].User), "tel") if err != nil { t.Error(err) } // Check if fields updated var got struct { Retries int UpdatedAt time.Time CreatedAt time.Time } err = db.QueryRow(ctx, "SELECT retries, updatedat, createdat FROM credentials WHERE userid=$1 AND method=$2 AND value=$3", decodeUid(testData.Creds[3].User), "tel", testData.Creds[3].Value).Scan(&got.Retries, &got.UpdatedAt, &got.CreatedAt) if err != nil { t.Fatal(err) } if got.Retries != 1 { t.Error(mismatchErrorString("Retries count", got.Retries, 1)) } if got.UpdatedAt == got.CreatedAt { t.Error("UpdatedAt field not updated") } } func TestCredConfirm(t *testing.T) { err := adp.CredConfirm(types.ParseUserId("usr"+testData.Creds[3].User), "tel") if err != nil { t.Fatal(err) } // Test fields are updated var got struct { UpdatedAt time.Time CreatedAt time.Time Done bool } err = db.QueryRow(ctx, "SELECT updatedat, createdat, done FROM credentials WHERE userid=$1 AND method=$2 AND value=$3", decodeUid(testData.Creds[3].User), "tel", testData.Creds[3].Value).Scan(&got.UpdatedAt, &got.CreatedAt, &got.Done) if err != nil { t.Fatal(err) } if got.UpdatedAt == got.CreatedAt { t.Error("Credential not updated correctly") } if !got.Done { t.Error("Credential should be marked as done") } } func TestAuthUpdRecord(t *testing.T) { rec := testData.Recs[1] newSecret := []byte{'s', 'e', 'c', 'r', 'e', 't'} err := adp.AuthUpdRecord(types.ParseUserId("usr"+rec.UserId), rec.Scheme, rec.Unique, rec.AuthLvl, newSecret, rec.Expires) if err != nil { t.Fatal(err) } var got []byte err = db.QueryRow(ctx, "SELECT secret FROM auth WHERE uname=$1", rec.Unique).Scan(&got) if err != nil { t.Fatal(err) } if reflect.DeepEqual(got, rec.Secret) { t.Error(mismatchErrorString("Secret", got, rec.Secret)) } // Test with auth ID (unique) change newId := "basic:bob12345" err = adp.AuthUpdRecord(types.ParseUserId("usr"+rec.UserId), rec.Scheme, newId, rec.AuthLvl, newSecret, rec.Expires) if err != nil { t.Fatal(err) } // Test if old ID deleted var count int err = db.QueryRow(ctx, "SELECT COUNT(*) FROM auth WHERE uname=$1", rec.Unique).Scan(&count) if err != nil { t.Fatal(err) } if count != 0 { t.Error("Old auth record not deleted") } } func TestTopicUpdateOnMessage(t *testing.T) { msg := types.Message{ ObjHeader: types.ObjHeader{ CreatedAt: testData.Now.Add(33 * time.Minute), }, SeqId: 66, } err := adp.TopicUpdateOnMessage(testData.Topics[2].Id, &msg) if err != nil { t.Fatal(err) } var got struct { TouchedAt time.Time SeqId int } err = db.QueryRow(ctx, "SELECT touchedat, seqid FROM topics WHERE name=$1", testData.Topics[2].Id). Scan(&got.TouchedAt, &got.SeqId) if err != nil { t.Fatal(err) } if got.TouchedAt != msg.CreatedAt || got.SeqId != msg.SeqId { t.Error(mismatchErrorString("TouchedAt", got.TouchedAt, msg.CreatedAt)) t.Error(mismatchErrorString("SeqId", got.SeqId, msg.SeqId)) } } func TestTopicUpdate(t *testing.T) { update := map[string]any{ "UpdatedAt": testData.Now.Add(55 * time.Minute), } err := adp.TopicUpdate(testData.Topics[0].Id, update) if err != nil { t.Fatal(err) } var got time.Time err = db.QueryRow(ctx, "SELECT updatedat FROM topics WHERE name=$1", testData.Topics[0].Id).Scan(&got) if err != nil { t.Fatal(err) } if got != update["UpdatedAt"] { t.Error(mismatchErrorString("UpdatedAt", got, update["UpdatedAt"])) } } func TestTopicUpdateSubCnt(t *testing.T) { // Test PostgreSQL specific method err := adp.TopicUpdateSubCnt(testData.Topics[0].Id) if err != nil { t.Fatal(err) } // Verify the subscription count was updated correctly var subcnt int err = db.QueryRow(ctx, "SELECT subcnt FROM topics WHERE name=$1", testData.Topics[0].Id).Scan(&subcnt) if err != nil { t.Fatal(err) } // Should match the number of active subscriptions if subcnt < 0 { t.Error("Subscription count should be non-negative") } } func TestTopicOwnerChange(t *testing.T) { err := adp.TopicOwnerChange(testData.Topics[0].Id, types.ParseUserId("usr"+testData.Users[1].Id)) if err != nil { t.Fatal(err) } var got int64 err = db.QueryRow(ctx, "SELECT owner FROM topics WHERE name=$1", testData.Topics[0].Id).Scan(&got) if err != nil { t.Fatal(err) } expectedOwner := decodeUid(testData.Users[1].Id) if got != expectedOwner { t.Error(mismatchErrorString("Owner", got, expectedOwner)) } } func TestSubsUpdate(t *testing.T) { update := map[string]any{ "UpdatedAt": testData.Now.Add(22 * time.Minute), } err := adp.SubsUpdate(testData.Topics[0].Id, types.ParseUserId("usr"+testData.Users[0].Id), update) if err != nil { t.Fatal(err) } var got time.Time err = db.QueryRow(ctx, "SELECT updatedat FROM subscriptions WHERE topic=$1 AND userid=$2", testData.Topics[0].Id, decodeUid(testData.Users[0].Id)).Scan(&got) if err != nil { t.Fatal(err) } if got != update["UpdatedAt"] { t.Error(mismatchErrorString("UpdatedAt", got, update["UpdatedAt"])) } err = adp.SubsUpdate(testData.Topics[1].Id, types.ZeroUid, update) if err != nil { t.Fatal(err) } err = db.QueryRow(ctx, "SELECT updatedat FROM subscriptions WHERE topic=$1 LIMIT 1", testData.Topics[1].Id).Scan(&got) if err != nil { t.Fatal(err) } if got != update["UpdatedAt"] { t.Error(mismatchErrorString("UpdatedAt", got, update["UpdatedAt"])) } } func TestSubsDelete(t *testing.T) { err := adp.SubsDelete(testData.Topics[1].Id, types.ParseUserId("usr"+testData.Users[0].Id)) if err != nil { t.Fatal(err) } var deletedat sql.NullTime err = db.QueryRow(ctx, "SELECT deletedat FROM subscriptions WHERE topic=$1 AND userid=$2", testData.Topics[1].Id, decodeUid(testData.Users[0].Id)).Scan(&deletedat) if err != nil { t.Fatal(err) } if !deletedat.Valid { t.Error("DeletedAt should not be null") } } func TestSubsDelForUser(t *testing.T) { // Tested during TestUserDelete (both hard and soft deletions) } func TestDeviceUpsert(t *testing.T) { err := adp.DeviceUpsert(types.ParseUserId("usr"+testData.Users[0].Id), testData.Devs[0]) if err != nil { t.Fatal(err) } var got struct { DeviceId string Platform string } err = db.QueryRow(ctx, "SELECT deviceid, platform FROM devices WHERE userid=$1 LIMIT 1", decodeUid(testData.Users[0].Id)).Scan(&got.DeviceId, &got.Platform) if err != nil { t.Fatal(err) } if got.DeviceId != testData.Devs[0].DeviceId || got.Platform != testData.Devs[0].Platform { t.Error(mismatchErrorString("Device", got, testData.Devs[0])) } // Test update testData.Devs[0].Platform = "Web" err = adp.DeviceUpsert(types.ParseUserId("usr"+testData.Users[0].Id), testData.Devs[0]) if err != nil { t.Fatal(err) } err = db.QueryRow(ctx, "SELECT platform FROM devices WHERE userid=$1 AND deviceid=$2", decodeUid(testData.Users[0].Id), testData.Devs[0].DeviceId).Scan(&got.Platform) if err != nil { t.Fatal(err) } if got.Platform != "Web" { t.Error("Device not updated.", got.Platform) } // Test add same device to another user err = adp.DeviceUpsert(types.ParseUserId("usr"+testData.Users[1].Id), testData.Devs[0]) if err != nil { t.Fatal(err) } err = adp.DeviceUpsert(types.ParseUserId("usr"+testData.Users[2].Id), testData.Devs[1]) if err != nil { t.Error(err) } } func TestMessageAttachments(t *testing.T) { fids := []string{testData.Files[0].Id, testData.Files[1].Id} err := adp.FileLinkAttachments("", types.ZeroUid, types.ParseUid(testData.Msgs[1].Id), fids) if err != nil { t.Fatal(err) } // Check if attachments were linked var count int err = db.QueryRow(ctx, "SELECT COUNT(*) FROM filemsglinks WHERE msgid=$1", int64(types.ParseUid(testData.Msgs[1].Id))).Scan(&count) if err != nil { t.Fatal(err) } if count != len(fids) { t.Error(mismatchErrorString("Attachments count", count, len(fids))) } } func TestFileFinishUpload(t *testing.T) { got, err := adp.FileFinishUpload(testData.Files[0], true, 22222) if err != nil { t.Fatal(err) } if got.Status != types.UploadCompleted { t.Error(mismatchErrorString("Status", got.Status, types.UploadCompleted)) } if got.Size != 22222 { t.Error(mismatchErrorString("Size", got.Size, 22222)) } } // ================== Other tests ================================= func TestDeviceGetAll(t *testing.T) { uid0 := types.ParseUserId("usr" + testData.Users[0].Id) uid1 := types.ParseUserId("usr" + testData.Users[1].Id) uid2 := types.ParseUserId("usr" + testData.Users[2].Id) gotDevs, count, err := adp.DeviceGetAll(uid0, uid1, uid2) if err != nil { t.Fatal(err) } if count < 1 { t.Fatal(mismatchErrorString("count", count, ">=1")) } // Test that devices exist for the users if len(gotDevs) == 0 { t.Error("Expected devices for users") } } func TestDeviceDelete(t *testing.T) { err := adp.DeviceDelete(types.ParseUserId("usr"+testData.Users[1].Id), testData.Devs[0].DeviceId) if err != nil { t.Fatal(err) } var count int err = db.QueryRow(ctx, "SELECT COUNT(*) FROM devices WHERE userid=$1 AND deviceid=$2", decodeUid(testData.Users[1].Id), testData.Devs[0].DeviceId).Scan(&count) if err != nil { t.Fatal(err) } if count != 0 { t.Error("Device not deleted:", count) } err = adp.DeviceDelete(types.ParseUserId("usr"+testData.Users[2].Id), "") if err != nil { t.Fatal(err) } err = db.QueryRow(ctx, "SELECT COUNT(*) FROM devices WHERE userid=$1", decodeUid(testData.Users[2].Id)).Scan(&count) if err != nil { t.Fatal(err) } if count != 0 { t.Error("All devices not deleted:", count) } } // ================== Persistent Cache tests ====================== func TestPCacheUpsert(t *testing.T) { err := adp.PCacheUpsert("test_key", "test_value", false) if err != nil { t.Fatal(err) } // Test duplicate with failOnDuplicate = true err = adp.PCacheUpsert("test_key2", "test_value2", true) if err != nil { t.Fatal(err) } err = adp.PCacheUpsert("test_key2", "new_value", true) if err != types.ErrDuplicate { t.Error("Expected duplicate error") } } func TestPCacheGet(t *testing.T) { value, err := adp.PCacheGet("test_key") if err != nil { t.Fatal(err) } if value != "test_value" { t.Error(mismatchErrorString("Cache value", value, "test_value")) } // Test not found _, err = adp.PCacheGet("nonexistent") if err != types.ErrNotFound { t.Error("Expected not found error") } } func TestPCacheDelete(t *testing.T) { err := adp.PCacheDelete("test_key") if err != nil { t.Fatal(err) } // Verify deleted _, err = adp.PCacheGet("test_key") if err != types.ErrNotFound { t.Error("Key should be deleted") } } func TestPCacheExpire(t *testing.T) { // Insert some test keys with prefix adp.PCacheUpsert("prefix_key1", "value1", false) adp.PCacheUpsert("prefix_key2", "value2", false) // Expire keys older than now (should delete all test keys) err := adp.PCacheExpire("prefix_", time.Now().Add(1*time.Minute)) if err != nil { t.Fatal(err) } } // ================== Delete tests ================================ func TestCredDel(t *testing.T) { err := adp.CredDel(types.ParseUserId("usr"+testData.Users[0].Id), "email", "alice@test.example.com") if err != nil { t.Fatal(err) } var count int err = db.QueryRow(ctx, "SELECT COUNT(*) FROM credentials WHERE method='email' AND value='alice@test.example.com'").Scan(&count) if err != nil { t.Fatal(err) } if count != 0 { t.Error("Got result but shouldn't", count) } err = adp.CredDel(types.ParseUserId("usr"+testData.Users[1].Id), "", "") if err != nil { t.Fatal(err) } err = db.QueryRow(ctx, "SELECT COUNT(*) FROM credentials WHERE userid=$1", decodeUid(testData.Users[1].Id)).Scan(&count) if err != nil { t.Fatal(err) } if count != 0 { t.Error("Got result but shouldn't", count) } } func TestAuthDelScheme(t *testing.T) { // Test deleting auth scheme err := adp.AuthDelScheme(types.ParseUserId("usr"+testData.Recs[1].UserId), testData.Recs[1].Scheme) if err != nil { t.Fatal(err) } // Verify deleted _, _, _, _, err = adp.AuthGetRecord(types.ParseUserId("usr"+testData.Recs[1].UserId), testData.Recs[1].Scheme) if err != types.ErrNotFound { t.Error("Auth record should be deleted") } } func TestAuthDelAllRecords(t *testing.T) { delCount, err := adp.AuthDelAllRecords(types.ParseUserId("usr" + testData.Recs[0].UserId)) if err != nil { t.Fatal(err) } if delCount != 1 { t.Error(mismatchErrorString("delCount", delCount, 1)) } // With dummy user delCount, _ = adp.AuthDelAllRecords(dummyUid1) if delCount != 0 { t.Error(mismatchErrorString("delCount", delCount, 0)) } } func TestMessageDeleteList(t *testing.T) { toDel := types.DelMessage{ ObjHeader: types.ObjHeader{ Id: testData.UGen.GetStr(), CreatedAt: testData.Now, UpdatedAt: testData.Now, }, Topic: testData.Topics[1].Id, DeletedFor: testData.Users[2].Id, DelId: 1, SeqIdRanges: []types.Range{{Low: 9}, {Low: 3, Hi: 7}}, } err := adp.MessageDeleteList(toDel.Topic, &toDel) if err != nil { t.Fatal(err) } // Check messages in dellog var count int err = db.QueryRow(ctx, "SELECT COUNT(*) FROM dellog WHERE topic=$1 AND deletedfor=$2", toDel.Topic, decodeUid(toDel.DeletedFor)).Scan(&count) if err != nil { t.Fatal(err) } if count == 0 { t.Error("No dellog entries created") } // Hard delete test toDel = types.DelMessage{ ObjHeader: types.ObjHeader{ Id: testData.UGen.GetStr(), CreatedAt: testData.Now, UpdatedAt: testData.Now, }, Topic: testData.Topics[0].Id, DelId: 3, SeqIdRanges: []types.Range{{Low: 1, Hi: 3}}, } err = adp.MessageDeleteList(toDel.Topic, &toDel) if err != nil { t.Fatal(err) } // Check if messages content was cleared err = db.QueryRow(ctx, "SELECT COUNT(*) FROM messages WHERE topic=$1 AND content IS NOT NULL", toDel.Topic).Scan(&count) if err != nil { t.Fatal(err) } if count > 1 { t.Errorf("Messages not properly deleted %d, %s", count, toDel.Topic) } err = adp.MessageDeleteList(testData.Topics[0].Id, nil) if err != nil { t.Fatal(err) } err = db.QueryRow(ctx, "SELECT COUNT(*) FROM messages WHERE topic=$1", testData.Topics[0].Id).Scan(&count) if err != nil { t.Fatal(err) } if count != 0 { t.Error("Result should be empty:", count) } } func TestTopicDelete(t *testing.T) { err := adp.TopicDelete(testData.Topics[1].Id, false, false) if err != nil { t.Fatal(err) } var state int err = db.QueryRow(ctx, "SELECT state FROM topics WHERE name=$1", testData.Topics[1].Id).Scan(&state) if err != nil { t.Fatal(err) } if state != int(types.StateDeleted) { t.Error("Soft delete failed:", state) } err = adp.TopicDelete(testData.Topics[0].Id, false, true) if err != nil { t.Fatal(err) } var count int err = db.QueryRow(ctx, "SELECT COUNT(*) FROM topics WHERE name=$1", testData.Topics[0].Id).Scan(&count) if err != nil { t.Fatal(err) } if count != 0 { t.Error("Hard delete failed:", count) } } func TestFileDeleteUnused(t *testing.T) { locs, err := adp.FileDeleteUnused(time.Now().Add(1*time.Minute), 999) if err != nil { t.Fatal(err) } if len(locs) < 1 { t.Log("No unused files to delete - this is expected in test environment") } } func TestUserDelete(t *testing.T) { err := adp.UserDelete(types.ParseUserId("usr"+testData.Users[0].Id), false) if err != nil { t.Fatal(err) } var state int err = db.QueryRow(ctx, "SELECT state FROM users WHERE id=$1", decodeUid(testData.Users[0].Id)).Scan(&state) if err != nil { t.Fatal(err) } if state != int(types.StateDeleted) { t.Error("User soft delete failed", state) } err = adp.UserDelete(types.ParseUserId("usr"+testData.Users[1].Id), true) if err != nil { t.Fatal(err) } var count int err = db.QueryRow(ctx, "SELECT COUNT(*) FROM users WHERE id=$1", decodeUid(testData.Users[1].Id)).Scan(&count) if err != nil { t.Fatal(err) } if count != 0 { t.Error("User hard delete failed") } } func TestUserUnreadCount(t *testing.T) { uids := []types.Uid{ types.ParseUserId("usr" + testData.Users[1].Id), types.ParseUserId("usr" + testData.Users[2].Id), } expected := map[types.Uid]int{uids[0]: 0, uids[1]: 166} counts, err := adp.UserUnreadCount(uids...) if err != nil { t.Fatal(err) } if len(counts) != 2 { t.Error(mismatchErrorString("UnreadCount length", len(counts), 2)) } for uid, unread := range counts { if expected[uid] != unread { t.Error(mismatchErrorString("UnreadCount", unread, expected[uid])) } } // Test not found (even if the account is not found, the call must return one record). counts, err = adp.UserUnreadCount(dummyUid1) if err != nil { t.Fatal(err) } if len(counts) != 1 { t.Error(mismatchErrorString("UnreadCount length (dummy)", len(counts), 1)) } if counts[dummyUid1] != 0 { t.Error(mismatchErrorString("Non-zero UnreadCount (dummy)", counts[dummyUid1], 0)) } } func TestMessageGetDeleted(t *testing.T) { qOpts := types.QueryOpt{ Since: 1, Before: 10, Limit: 999, } got, err := adp.MessageGetDeleted(testData.Topics[1].Id, types.ParseUserId("usr"+testData.Users[2].Id), &qOpts) if err != nil { t.Fatal(err) } if len(got) != 1 { t.Error(mismatchErrorString("result length", len(got), 1)) } } // ================================================================ func mismatchErrorString(key string, got, want any) string { return fmt.Sprintf("%s mismatch:\nGot = %+v\nWant = %+v", key, got, want) } func init() { ctx = context.Background() logs.Init(os.Stderr, "stdFlags") adp = backend.GetTestAdapter() conffile := flag.String("config", "./test.conf", "config of the database connection") if file, err := os.Open(*conffile); err != nil { log.Fatal("Failed to read config file:", err) } else if err = json.NewDecoder(jcr.New(file)).Decode(&config); err != nil { log.Fatal("Failed to parse config file:", err) } if adp == nil { log.Fatal("Database adapter is missing") } if adp.IsOpen() { log.Print("Connection is already opened") } err := adp.Open(config.Adapters[adp.GetName()]) if err != nil { log.Fatal(err) } db = adp.GetTestDB().(*pgxpool.Pool) testData = test_data.InitTestData() if testData == nil { log.Fatal("Failed to initialize test data") } store.SetTestUidGenerator(*testData.UGen) } ================================================ FILE: server/db/postgres/tests/test.conf ================================================ { "reset_db_data": true, "adapters": { "postgres": { "User": "postgres", "Passwd": "postgres", "Host": "localhost", "Port": "5432", "DBName": "tinode_test" } } } ================================================ FILE: server/db/rethinkdb/adapter.go ================================================ //go:build rethinkdb // Package rethinkdb is a database adapter for RethinkDB. package rethinkdb import ( "encoding/json" "errors" "hash/fnv" "sort" "strconv" "strings" "time" "github.com/tinode/chat/server/auth" "github.com/tinode/chat/server/db/common" "github.com/tinode/chat/server/logs" "github.com/tinode/chat/server/store" t "github.com/tinode/chat/server/store/types" rdb "gopkg.in/rethinkdb/rethinkdb-go.v6" ) // adapter holds RethinkDb connection data. type adapter struct { conn *rdb.Session dbName string // Maximum number of records to return maxResults int // Maximum number of message records to return maxMessageResults int version int } const ( adpVersion = 116 adapterName = "rethinkdb" defaultHost = "localhost:28015" defaultDatabase = "tinode" defaultMaxResults = 1024 // This is capped by the Session's send queue limit (128). defaultMaxMessageResults = 100 ) // See https://godoc.org/github.com/rethinkdb/rethinkdb-go#ConnectOpts for explanations. type configType struct { Database string `json:"database,omitempty"` Addresses any `json:"addresses,omitempty"` Username string `json:"username,omitempty"` Password string `json:"password,omitempty"` AuthKey string `json:"authkey,omitempty"` Timeout int `json:"timeout,omitempty"` WriteTimeout int `json:"write_timeout,omitempty"` ReadTimeout int `json:"read_timeout,omitempty"` KeepAlivePeriod int `json:"keep_alive_timeout,omitempty"` UseJSONNumber bool `json:"use_json_number,omitempty"` NumRetries int `json:"num_retries,omitempty"` InitialCap int `json:"initial_cap,omitempty"` MaxOpen int `json:"max_open,omitempty"` DiscoverHosts bool `json:"discover_hosts,omitempty"` HostDecayDuration int `json:"host_decay_duration,omitempty"` } // Open initializes rethinkdb session func (a *adapter) Open(jsonconfig json.RawMessage) error { if a.conn != nil { return errors.New("adapter rethinkdb is already connected") } if len(jsonconfig) < 2 { return errors.New("adapter rethinkdb missing config") } var err error var config configType if err = json.Unmarshal(jsonconfig, &config); err != nil { return errors.New("adapter rethinkdb failed to parse config: " + err.Error()) } var opts rdb.ConnectOpts if config.Addresses == nil { opts.Address = defaultHost } else if host, ok := config.Addresses.(string); ok { opts.Address = host } else if ihosts, ok := config.Addresses.([]any); ok && len(ihosts) > 0 { hosts := make([]string, len(ihosts)) for i, ih := range ihosts { h, ok := ih.(string) if !ok || h == "" { return errors.New("adapter rethinkdb invalid config.Addresses value") } hosts[i] = h } opts.Addresses = hosts } else { return errors.New("adapter rethinkdb failed to parse config.Addresses") } if config.Database == "" { a.dbName = defaultDatabase } else { a.dbName = config.Database } if a.maxResults <= 0 { a.maxResults = defaultMaxResults } if a.maxMessageResults <= 0 { a.maxMessageResults = defaultMaxMessageResults } opts.Database = a.dbName opts.Username = config.Username opts.Password = config.Password opts.AuthKey = config.AuthKey opts.Timeout = time.Duration(config.Timeout) * time.Second opts.WriteTimeout = time.Duration(config.WriteTimeout) * time.Second opts.ReadTimeout = time.Duration(config.ReadTimeout) * time.Second opts.KeepAlivePeriod = time.Duration(config.KeepAlivePeriod) * time.Second opts.UseJSONNumber = config.UseJSONNumber opts.NumRetries = config.NumRetries opts.InitialCap = config.InitialCap opts.MaxOpen = config.MaxOpen opts.DiscoverHosts = config.DiscoverHosts opts.HostDecayDuration = time.Duration(config.HostDecayDuration) * time.Second a.conn, err = rdb.Connect(opts) if err != nil { return err } rdb.SetTags("json") a.version = -1 return nil } // Close closes the underlying database connection func (a *adapter) Close() error { var err error if a.conn != nil { // Close will wait for all outstanding requests to finish err = a.conn.Close() a.conn = nil a.version = -1 } return err } // IsOpen returns true if connection to database has been established. It does not check if // connection is actually live. func (a *adapter) IsOpen() bool { return a.conn != nil } // GetDbVersion returns current database version. func (a *adapter) GetDbVersion() (int, error) { if a.version > 0 { return a.version, nil } cursor, err := rdb.DB(a.dbName).Table("kvmeta").Get("version").Field("value").Run(a.conn) if err != nil { if isMissingDb(err) { err = errors.New("Database not initialized") } return -1, err } defer cursor.Close() if cursor.IsNil() { return -1, errors.New("Database not initialized") } var vers int if err = cursor.One(&vers); err != nil { return -1, err } a.version = vers return vers, nil } func (a *adapter) updateDbVersion(v int) error { a.version = -1 if _, err := rdb.DB(a.dbName).Table("kvmeta").Get("version"). Update(map[string]any{"value": v}).RunWrite(a.conn); err != nil { return err } return nil } // CheckDbVersion checks whether the actual DB version matches the expected version of this adapter. func (a *adapter) CheckDbVersion() error { version, err := a.GetDbVersion() if err != nil { return err } if version != adpVersion { return errors.New("Invalid database version " + strconv.Itoa(version) + ". Expected " + strconv.Itoa(adpVersion)) } return nil } // Version returns adapter version. func (adapter) Version() int { return adpVersion } // Stats returns DB connection stats object. func (a *adapter) Stats() any { if a.conn == nil { return nil } cursor, err := rdb.DB("rethinkdb").Table("stats").Get([]string{"cluster"}).Field("query_engine").Run(a.conn) if err != nil { return nil } defer cursor.Close() var stats []any if err = cursor.All(&stats); err != nil || len(stats) < 1 { return nil } return stats[0] } // GetName returns string that adapter uses to register itself with store. func (a *adapter) GetName() string { return adapterName } // SetMaxResults configures how many results can be returned in a single DB call. func (a *adapter) SetMaxResults(val int) error { if val <= 0 { a.maxResults = defaultMaxResults } else { a.maxResults = val } return nil } // CreateDb initializes the storage. If reset is true, the database is first deleted losing all the data. func (a *adapter) CreateDb(reset bool) error { // Drop database if exists, ignore error if it does not. if reset { rdb.DBDrop(a.dbName).RunWrite(a.conn) } if _, err := rdb.DBCreate(a.dbName).RunWrite(a.conn); err != nil { return err } // Table with metadata key-value pairs. if _, err := rdb.DB(a.dbName).TableCreate("kvmeta", rdb.TableCreateOpts{PrimaryKey: "key"}).RunWrite(a.conn); err != nil { return err } // Users if _, err := rdb.DB(a.dbName).TableCreate("users", rdb.TableCreateOpts{PrimaryKey: "Id"}).RunWrite(a.conn); err != nil { return err } // Create secondary index on State for finding suspended and soft-deleted users. if _, err := rdb.DB(a.dbName).Table("users").IndexCreate("State").RunWrite(a.conn); err != nil { return err } // Create secondary index on User.Tags array so user can be found by tags. if _, err := rdb.DB(a.dbName).Table("users").IndexCreate("Tags", rdb.IndexCreateOpts{Multi: true}).RunWrite(a.conn); err != nil { return err } // Create secondary index for User.Devices..DeviceId to ensure ID uniqueness across users if _, err := rdb.DB(a.dbName).Table("users").IndexCreateFunc("DeviceIds", func(row rdb.Term) any { devices := row.Field("Devices") return devices.Keys().Map(func(key rdb.Term) any { return devices.Field(key).Field("DeviceId") }) }, rdb.IndexCreateOpts{Multi: true}).RunWrite(a.conn); err != nil { return err } // User authentication records {unique, userid, secret} if _, err := rdb.DB(a.dbName).TableCreate("auth", rdb.TableCreateOpts{PrimaryKey: "unique"}).RunWrite(a.conn); err != nil { return err } // Should be able to access user's auth records by user id if _, err := rdb.DB(a.dbName).Table("auth").IndexCreate("userid").RunWrite(a.conn); err != nil { return err } // Subscription to a topic. The primary key is a Topic:User string if _, err := rdb.DB(a.dbName).TableCreate("subscriptions", rdb.TableCreateOpts{PrimaryKey: "Id"}).RunWrite(a.conn); err != nil { return err } if _, err := rdb.DB(a.dbName).Table("subscriptions").IndexCreate("User").RunWrite(a.conn); err != nil { return err } if _, err := rdb.DB(a.dbName).Table("subscriptions").IndexCreate("Topic").RunWrite(a.conn); err != nil { return err } // Topics stored in database if _, err := rdb.DB(a.dbName).TableCreate("topics", rdb.TableCreateOpts{PrimaryKey: "Id"}).RunWrite(a.conn); err != nil { return err } // Secondary index on Owner field for deleting users. if _, err := rdb.DB(a.dbName).Table("topics").IndexCreate("Owner").RunWrite(a.conn); err != nil { return err } // Create secondary index on State for finding suspended and soft-deleted topics. if _, err := rdb.DB(a.dbName).Table("topics").IndexCreate("State").RunWrite(a.conn); err != nil { return err } // Secondary index on Topic.Tags array so topics can be found by tags. // These tags are not unique as opposite to User.Tags. if _, err := rdb.DB(a.dbName).Table("topics").IndexCreate("Tags", rdb.IndexCreateOpts{Multi: true}).RunWrite(a.conn); err != nil { return err } // Create system topic 'sys'. if err := createSystemTopic(a); err != nil { return err } // Stored message if _, err := rdb.DB(a.dbName).TableCreate("messages", rdb.TableCreateOpts{PrimaryKey: "Id"}).RunWrite(a.conn); err != nil { return err } // Compound index of topic - seqID for selecting messages in a topic. if _, err := rdb.DB(a.dbName).Table("messages").IndexCreateFunc("Topic_SeqId", func(row rdb.Term) any { return []any{row.Field("Topic"), row.Field("SeqId")} }).RunWrite(a.conn); err != nil { return err } // Compound index of hard-deleted messages if _, err := rdb.DB(a.dbName).Table("messages").IndexCreateFunc("Topic_DelId", func(row rdb.Term) any { return []any{row.Field("Topic"), row.Field("DelId")} }).RunWrite(a.conn); err != nil { return err } // Compound multi-index of soft-deleted messages: each message gets multiple compound index entries like // [Topic, User1, DelId1], [Topic, User2, DelId2],... if _, err := rdb.DB(a.dbName).Table("messages").IndexCreateFunc("Topic_DeletedFor", func(row rdb.Term) any { return row.Field("DeletedFor").Map(func(df rdb.Term) any { return []any{row.Field("Topic"), df.Field("User"), df.Field("DelId")} }) }, rdb.IndexCreateOpts{Multi: true}).RunWrite(a.conn); err != nil { return err } // Log of deleted messages if _, err := rdb.DB(a.dbName).TableCreate("dellog", rdb.TableCreateOpts{PrimaryKey: "Id"}).RunWrite(a.conn); err != nil { return err } if _, err := rdb.DB(a.dbName).Table("dellog").IndexCreateFunc("Topic_DelId", func(row rdb.Term) any { return []any{row.Field("Topic"), row.Field("DelId")} }).RunWrite(a.conn); err != nil { return err } // User credentials - contact information such as "email:jdoe@example.com" or "tel:+18003287448": // Id: "method:credential" like "email:jdoe@example.com". See types.Credential. if _, err := rdb.DB(a.dbName).TableCreate("credentials", rdb.TableCreateOpts{PrimaryKey: "Id"}).RunWrite(a.conn); err != nil { return err } // Create secondary index on credentials.User to be able to query credentials by user id. if _, err := rdb.DB(a.dbName).Table("credentials").IndexCreate("User").RunWrite(a.conn); err != nil { return err } // Records of file uploads. See types.FileDef. if _, err := rdb.DB(a.dbName).TableCreate("fileuploads", rdb.TableCreateOpts{PrimaryKey: "Id"}).RunWrite(a.conn); err != nil { return err } // A secondary index on fileuploads.UseCount to be able to delete unused records at once. if _, err := rdb.DB(a.dbName).Table("fileuploads").IndexCreate("UseCount").RunWrite(a.conn); err != nil { return err } // Record current DB version. if _, err := rdb.DB(a.dbName).Table("kvmeta").Insert( map[string]any{"key": "version", "value": adpVersion}).RunWrite(a.conn); err != nil { return err } return nil } // UpgradeDb upgrades the database to the latest version. func (a *adapter) UpgradeDb() error { bumpVersion := func(a *adapter, x int) error { if err := a.updateDbVersion(x); err != nil { return err } _, err := a.GetDbVersion() return err } _, err := a.GetDbVersion() if err != nil { return err } if a.version == 106 || a.version == 107 { // Perform database upgrade from versions 106 or 107 to version 108. // Replace default 'Auth' access mode JRWPA with JRWPAS filter := map[string]any{"Access": map[string]any{"Auth": t.ModeCP2P}} update := map[string]any{"Access": map[string]any{"Auth": t.ModeCAuth}} if _, err := rdb.DB(a.dbName).Table("users").Filter(filter).Update(update).RunWrite(a.conn); err != nil { return err } if err := bumpVersion(a, 108); err != nil { return err } } if a.version == 108 { // Perform database upgrade from versions 108 to version 109. if err := createSystemTopic(a); err != nil { return err } if err := bumpVersion(a, 109); err != nil { return err } } if a.version == 109 { // Perform database upgrade from versions 109 to version 110. // TouchedAt is a required field now, but it's OK if it's missing. // Bumping version to keep RDB in sync with MySQL versions. if err := bumpVersion(a, 110); err != nil { return err } } if a.version == 110 { // Perform database upgrade from versions 110 to version 111. // Users // Reset previously unused field State to value StateOK. if _, err := rdb.DB(a.dbName).Table("users"). Update(map[string]any{"State": t.StateOK}). RunWrite(a.conn); err != nil { return err } // Add StatusDeleted to all deleted users as indicated by DeletedAt not being null. if _, err := rdb.DB(a.dbName).Table("users"). Between(rdb.MinVal, rdb.MaxVal, rdb.BetweenOpts{Index: "DeletedAt"}). Update(map[string]any{"State": t.StateDeleted}). RunWrite(a.conn); err != nil { return err } // Rename DeletedAt into StateAt. Update only those rows which have defined DeletedAt. if _, err := rdb.DB(a.dbName).Table("users"). Between(rdb.MinVal, rdb.MaxVal, rdb.BetweenOpts{Index: "DeletedAt"}). Replace(func(row rdb.Term) rdb.Term { return row.Without("DeletedAt"). Merge(map[string]any{"StateAt": row.Field("DeletedAt")}) }). RunWrite(a.conn); err != nil { return err } // Drop secondary index DeletedAt. if _, err := rdb.DB(a.dbName).Table("users").IndexDrop("DeletedAt").RunWrite(a.conn); err != nil { return err } // Create secondary index on State for finding suspended and soft-deleted topics. if _, err := rdb.DB(a.dbName).Table("users").IndexCreate("State").RunWrite(a.conn); err != nil { return err } // Topics // Add StateDeleted to all topics with DeletedAt not null. if _, err := rdb.DB(a.dbName).Table("topics"). Filter(rdb.Row.HasFields("DeletedAt")). Update(map[string]any{"State": t.StateDeleted}). RunWrite(a.conn); err != nil { return err } // Set StateOK for all other topics. if _, err := rdb.DB(a.dbName).Table("topics"). Filter(rdb.Row.HasFields("State").Not()). Update(map[string]any{"State": t.StateOK}). RunWrite(a.conn); err != nil { return err } // Rename DeletedAt into StateAt. Update only those rows which have defined DeletedAt. if _, err := rdb.DB(a.dbName).Table("topics"). Filter(rdb.Row.HasFields("DeletedAt")). Replace(func(row rdb.Term) rdb.Term { return row.Without("DeletedAt"). Merge(map[string]any{"StateAt": row.Field("DeletedAt")}) }). RunWrite(a.conn); err != nil { return err } // Create secondary index on State for finding suspended and soft-deleted topics. if _, err := rdb.DB(a.dbName).Table("topics").IndexCreate("State").RunWrite(a.conn); err != nil { return err } if err := bumpVersion(a, 111); err != nil { return err } } if a.version == 111 { // Just bump the version to keep up with MySQL. if err := bumpVersion(a, 112); err != nil { return err } } if a.version == 112 { // Secondary indexes cannot store NULLs, consequently no useful indexes can be created. // Just bump the version. if err := bumpVersion(a, 113); err != nil { return err } } if a.version < 116 { // Version 114: topics.aux added, fileuploads.etag added. // Version 115: SQL indexes added. // Version 116: topics.subcnt added. // Just bump the version. if err := bumpVersion(a, 116); err != nil { return err } } if a.version != adpVersion { return errors.New("Failed to perform database upgrade to version " + strconv.Itoa(adpVersion) + ". DB is still at " + strconv.Itoa(a.version)) } return nil } // Create system topic 'sys'. func createSystemTopic(a *adapter) error { now := t.TimeNow() _, err := rdb.DB(a.dbName).Table("topics").Insert(&t.Topic{ ObjHeader: t.ObjHeader{Id: "sys", CreatedAt: now, UpdatedAt: now}, TouchedAt: now, Access: t.DefaultAccess{Auth: t.ModeNone, Anon: t.ModeNone}, Public: map[string]any{"fn": "System"}, }).RunWrite(a.conn) return err } // UserCreate creates a new user. Returns error and true if error is due to duplicate user name, // false for any other error func (a *adapter) UserCreate(user *t.User) error { _, err := rdb.DB(a.dbName).Table("users").Insert(&user).RunWrite(a.conn) return err } // AuthAddRecord adds user's authentication record func (a *adapter) AuthAddRecord(uid t.Uid, scheme, unique string, authLvl auth.Level, secret []byte, expires time.Time) error { _, err := rdb.DB(a.dbName).Table("auth").Insert( &common.AuthRecord{ Unique: unique, UserId: uid.String(), Scheme: scheme, AuthLvl: authLvl, Secret: secret, Expires: expires}).RunWrite(a.conn) if err != nil { if rdb.IsConflictErr(err) { return t.ErrDuplicate } return err } return nil } // AuthDelScheme deletes an existing authentication scheme for the user. func (a *adapter) AuthDelScheme(uid t.Uid, scheme string) error { _, err := rdb.DB(a.dbName).Table("auth"). GetAllByIndex("userid", uid.String()). Filter(map[string]any{"scheme": scheme}). Delete().RunWrite(a.conn) return err } // AuthDelAllRecords deletes user's all authentication records func (a *adapter) AuthDelAllRecords(uid t.Uid) (int, error) { res, err := rdb.DB(a.dbName).Table("auth").GetAllByIndex("userid", uid.String()).Delete().RunWrite(a.conn) return res.Deleted, err } // AuthUpdRecord updates user's authentication secret. func (a *adapter) AuthUpdRecord(uid t.Uid, scheme, unique string, authLvl auth.Level, secret []byte, expires time.Time) error { // The 'unique' is used as a primary key (no other way to ensure uniqueness in RethinkDB). // The primary key is immutable. If 'unique' has changed, we have to replace the old record with a new one: // 1. Check if 'unique' has changed. // 2. If not, execute update by 'unique' // 3. If yes, first insert the new record (it may fail due to dublicate 'unique') then delete the old one. // Get the old 'unique' cursor, err := rdb.DB(a.dbName).Table("auth").GetAllByIndex("userid", uid.String()). Filter(map[string]any{"scheme": scheme}). Pluck("unique").Default(nil).Run(a.conn) if err != nil { if isNoResults(err) { return t.ErrNotFound } return err } defer cursor.Close() if cursor.IsNil() { // If the record is not found, don't update it return t.ErrNotFound } var record common.AuthRecord if err = cursor.One(&record); err != nil { return err } if record.Unique == unique { // Unique has not changed upd := map[string]any{ "authLvl": authLvl, } if len(secret) > 0 { upd["secret"] = secret } if !expires.IsZero() { upd["expires"] = expires } _, err = rdb.DB(a.dbName).Table("auth").Get(unique).Update(upd).RunWrite(a.conn) } else { // Unique has changed. Insert-Delete. // No support for transactions :( if len(secret) == 0 { secret = record.Secret } if expires.IsZero() { expires = record.Expires } err = a.AuthAddRecord(uid, scheme, unique, authLvl, secret, expires) if err == nil { // We can't do much with the error here. rdb.DB(a.dbName).Table("auth").Get(record.Unique).Delete().RunWrite(a.conn) } } return err } // AuthGetRecord retrieves user's authentication record by user ID and scheme. func (a *adapter) AuthGetRecord(uid t.Uid, scheme string) (string, auth.Level, []byte, time.Time, error) { // Default() is needed to prevent Pluck from returning an error cursor, err := rdb.DB(a.dbName).Table("auth").GetAllByIndex("userid", uid.String()). Filter(map[string]any{"scheme": scheme}). Pluck("unique", "secret", "expires", "authLvl").Default(nil).Run(a.conn) if err != nil { return "", 0, nil, time.Time{}, err } defer cursor.Close() if cursor.IsNil() { return "", 0, nil, time.Time{}, t.ErrNotFound } var record struct { Unique string `json:"unique"` AuthLvl auth.Level `json:"authLvl"` Secret []byte `json:"secret"` Expires time.Time `json:"expires"` } if err = cursor.One(&record); err != nil { return "", 0, nil, time.Time{}, err } // Convert to UTC (bug? in gorethink). record.Expires = record.Expires.UTC() return record.Unique, record.AuthLvl, record.Secret, record.Expires, nil } // AuthGetUniqueRecord retrieve user's authentication record by unique value (e.g. by login). func (a *adapter) AuthGetUniqueRecord(unique string) (t.Uid, auth.Level, []byte, time.Time, error) { // Default() is needed to prevent Pluck from returning an error cursor, err := rdb.DB(a.dbName).Table("auth").Get(unique).Pluck( "userid", "secret", "expires", "authLvl").Default(nil).Run(a.conn) if err != nil { return t.ZeroUid, 0, nil, time.Time{}, err } defer cursor.Close() if cursor.IsNil() { return t.ZeroUid, 0, nil, time.Time{}, nil } var record struct { Userid string `json:"userid"` AuthLvl auth.Level `json:"authLvl"` Secret []byte `json:"secret"` Expires time.Time `json:"expires"` } if err = cursor.One(&record); err != nil { return t.ZeroUid, 0, nil, time.Time{}, err } return t.ParseUid(record.Userid), record.AuthLvl, record.Secret, record.Expires.UTC(), nil } // UserGet fetches a single user by user id. If user is not found it returns (nil, nil) func (a *adapter) UserGet(uid t.Uid) (*t.User, error) { cursor, err := rdb.DB(a.dbName).Table("users").GetAll(uid.String()). Filter(rdb.Row.Field("State").Eq(t.StateDeleted).Not()).Run(a.conn) if err != nil { return nil, err } defer cursor.Close() if cursor.IsNil() { return nil, nil } var user t.User if err = cursor.One(&user); err != nil { return nil, err } return &user, nil } // UserGetAll fetches multiple user records by UIDs. func (a *adapter) UserGetAll(ids ...t.Uid) ([]t.User, error) { uids := make([]any, len(ids)) for i, id := range ids { uids[i] = id.String() } users := []t.User{} cursor, err := rdb.DB(a.dbName).Table("users").GetAll(uids...). Filter(rdb.Row.Field("State").Eq(t.StateDeleted).Not()).Run(a.conn) if err != nil { return nil, err } defer cursor.Close() var user t.User for cursor.Next(&user) { // Convert timestamps to UTC (gorethink returns them as +0000) user.CreatedAt = user.CreatedAt.UTC() user.UpdatedAt = user.UpdatedAt.UTC() if user.StateAt != nil { stateAt := user.StateAt.UTC() user.StateAt = &stateAt } users = append(users, user) } return users, cursor.Err() } // UserDelete deletes user record. func (a *adapter) UserDelete(uid t.Uid, hard bool) error { // Get a list of topic names owned by the user (as 'grp' and 'chn'). ownTopics, err := a.topicNamesForUser(rdb.DB(a.dbName).Table("topics"). GetAllByIndex("Owner", uid.String()).Filter(rdb.Row.Field("State").Eq(t.StateDeleted).Not()). Field("Id"), true) if err != nil { logs.Err.Println("UserDelete: cannot get user's own topics:", err) return err } if hard { // User's devices are store in user record, no separate table. // Delete user's subscriptions in all topics. if err = a.subsDelForUser(uid, true); err != nil { return err } // Delete records of messages soft-deleted for the user in all topics // and dellog entries. if err = a.clearUserDellog(uid, nil); err != nil { return err } // Can't delete user's messages in all topics because we cannot notify topics of such deletion. // Just leave the messages marked as sent by "not found" user. // Delete topics where the user is the owner: if len(ownTopics) > 0 { // 1. Delete dellog // 2. Decrement use counter of fileuploads: topic itself and messages. // 3. Delete all messages. // 4. Delete subscriptions. if _, err = rdb.DB(a.dbName).Table("topics").GetAll(ownTopics...).ForEach( func(topic rdb.Term) rdb.Term { return rdb.Expr([]any{ // Delete dellog rdb.DB(a.dbName).Table("dellog").Between( []any{topic.Field("Id"), rdb.MinVal}, []any{topic.Field("Id"), rdb.MaxVal}, rdb.BetweenOpts{Index: "Topic_DelId"}).Delete(), // Decrement topic attachment UseCounter rdb.DB(a.dbName).Table("fileuploads").GetAll(topic.Field("Attachments")). Update(func(fu rdb.Term) any { return map[string]any{"UseCount": fu.Field("UseCount").Default(1).Sub(1)} }), // Decrement message attachments UseCounter rdb.DB(a.dbName).Table("fileuploads").GetAll( rdb.Args( rdb.DB(a.dbName).Table("messages").Between( []any{topic.Field("Id"), rdb.MinVal}, []any{topic.Field("Id"), rdb.MaxVal}, rdb.BetweenOpts{Index: "Topic_SeqId"}). // Fetch messages with attachments only Filter(func(msg rdb.Term) rdb.Term { return msg.HasFields("Attachments") }). // Flatten arrays ConcatMap(func(row rdb.Term) any { return row.Field("Attachments") }). CoerceTo("array"))). Update(func(fu rdb.Term) any { return map[string]any{"UseCount": fu.Field("UseCount").Default(1).Sub(1)} }), // Delete messages rdb.DB(a.dbName).Table("messages").Between( []any{topic.Field("Id"), rdb.MinVal}, []any{topic.Field("Id"), rdb.MaxVal}, rdb.BetweenOpts{Index: "Topic_SeqId"}).Delete(), // Delete subscriptions rdb.DB(a.dbName).Table("subscriptions"). GetAllByIndex("Topic", topic.Field("Id")).Delete(), }) }).RunWrite(a.conn); err != nil { return err } // And finally delete the topics. if _, err = rdb.DB(a.dbName).Table("topics").GetAllByIndex("Owner", uid.String()). Delete().RunWrite(a.conn); err != nil { return err } } // Delete user's authentication records. if _, err = a.AuthDelAllRecords(uid); err != nil { return err } // Delete credentials. if err = a.CredDel(uid, "", ""); err != nil && err != t.ErrNotFound { return err } // Must use GetAll to produce array result expected by decFileUseCounter. q := rdb.DB(a.dbName).Table("users").GetAll(uid.String()) // Unlink user's attachment. if err = a.decFileUseCounter(q); err != nil { return err } // And finally delete the user. _, err = q.Delete().RunWrite(a.conn) } else { // Disable user's subscriptions. if err = a.subsDelForUser(uid, false); err != nil { logs.Err.Println("UserDelete: subsDelForUser:", err) return err } now := t.TimeNow() disable := map[string]any{ "UpdatedAt": now, "State": t.StateDeleted, "StateAt": now, } disableSub := map[string]any{ "UpdatedAt": now, "DeletedAt": now, } if len(ownTopics) > 0 { // Disable all subscriptions in topics where the user is the owner. if _, err = rdb.DB(a.dbName).Table("subscriptions"). GetAllByIndex("Topic", ownTopics...). Update(disableSub). RunWrite(a.conn); err != nil { return err } // Disable topics where the user is the owner. if _, err = rdb.DB(a.dbName).Table("topics"). GetAll(ownTopics...). Update(disable). RunWrite(a.conn); err != nil { return err } } // Disable p2p topics with the user. p2pTopics, err := a.p2pTopicsForUser(uid) if err != nil { logs.Err.Println("UserDelete: p2pTopics:", err) return err } if len(p2pTopics) > 0 { // Disable all subscriptions in p2p topics with the user. if _, err = rdb.DB(a.dbName).Table("subscriptions"). GetAllByIndex("Topic", p2pTopics...). Update(disableSub). RunWrite(a.conn); err != nil { return err } // Disable p2p topics with the user. if _, err = rdb.DB(a.dbName).Table("topics"). GetAll(p2pTopics...). Update(disable). RunWrite(a.conn); err != nil { return err } } // Disable the user (same fields as topic). _, err = rdb.DB(a.dbName).Table("users").Get(uid.String()). Update(disable).RunWrite(a.conn) } return err } // Delete records of messages soft-deleted for the user in all topics. func (a *adapter) clearUserDellog(uid t.Uid, topics []any) error { var err error forUser := uid.String() if topics == nil { // Get a list of all topics where the user has subscriptions. topics, err = a.topicNamesForUser(rdb.DB(a.dbName). Table("subscriptions"). GetAllByIndex("User", forUser). Field("Topic"), false) if err != nil { return err } } // No need to convert channel names to group names: // channel readers cannot delete messages. // Remove current user from the messages' soft-deletion lists // in all topics where the user has subscriptions. _, err = rdb.DB(a.dbName).Table("topics").GetAll(topics...). ForEach(func(topic rdb.Term) rdb.Term { return rdb.DB(a.dbName).Table("messages").Between( []any{topic.Field("Id"), forUser, rdb.MinVal}, []any{topic.Field("Id"), forUser, rdb.MaxVal}, rdb.BetweenOpts{Index: "Topic_DeletedFor"}). Update(map[string]any{ // Take the DeletedFor array, subtract all values which contain current user ID in 'User' field. "DeletedFor": func(msg rdb.Term) rdb.Term { return msg.Field("DeletedFor"). SetDifference(msg.Field("DeletedFor").Filter(map[string]any{"User": forUser})) }, }) }).RunWrite(a.conn) if err != nil { return err } // Delete entries in dellog for this user in all topics where the user // has subscriptions. _, err = rdb.DB(a.dbName).Table("topics").GetAll(topics...). ForEach(func(topic rdb.Term) rdb.Term { return rdb.DB(a.dbName).Table("dellog"). // Select all log entries for the given table. Between( []any{topic.Field("Id"), rdb.MinVal}, []any{topic.Field("Id"), rdb.MaxVal}, rdb.BetweenOpts{Index: "Topic_DelId"}). // Keep for deletion entries soft-deleted for the current user only. Filter(func(dle rdb.Term) rdb.Term { return dle.Field("DeletedFor").Eq(forUser) }). // Delete them. Delete() }).RunWrite(a.conn) return err } // topicNamesForUser returns a list of topic names by query. func (a *adapter) topicNamesForUser(query rdb.Term, includeChan bool) ([]any, error) { cursor, err := query.Run(a.conn) if err != nil { if isNoResults(err) { return nil, nil } return nil, err } defer cursor.Close() var result []string if err = cursor.All(&result); err != nil { return nil, err } var args []any for _, name := range result { args = append(args, name) if includeChan { // Append 'chn' topic names for each 'grp' name. if channel := t.GrpToChn(name); channel != "" { args = append(args, channel) } } } return args, nil } func (a *adapter) p2pTopicsForUser(uid t.Uid) ([]any, error) { return a.topicNamesForUser(rdb.DB(a.dbName).Table("subscriptions"). GetAllByIndex("User", uid.String()). Field("Topic"). Filter(rdb.Row.Field("Topic").Match("^p2p")), false) } // topicStateForUser is called by UserUpdate when the update contains state change. func (a *adapter) topicStateForUser(uid t.Uid, now time.Time, update any) error { state, ok := update.(t.ObjState) if !ok { return t.ErrMalformed } if now.IsZero() { now = t.TimeNow() } // Change state of all topics where the user is the owner. if _, err := rdb.DB(a.dbName).Table("topics"). GetAllByIndex("Owner", uid.String()). Filter(rdb.Row.Field("State").Eq(t.StateDeleted).Not()). Update(map[string]any{ "State": state, "StateAt": now, }).RunWrite(a.conn); err != nil { return err } // Change state of p2p topics with the user (p2p topic's owner is blank) /* r.db('tinode').table('topics').getAll( r.args( r.db("tinode").table("subscriptions").getAll('S8VFqRpXw5M', {index: 'User'})('Topic').coerceTo('array') ) ).update(...) */ if _, err := rdb.DB(a.dbName).Table("topics"). GetAll(rdb.Args( rdb.DB(a.dbName).Table("subscriptions").GetAllByIndex("User", uid.String()). Field("Topic").CoerceTo("array"))). Filter(rdb.Row.Field("Owner").Eq("").And(rdb.Row.Field("State").Eq(t.StateDeleted).Not())). Update(map[string]any{ "State": state, "StateAt": now, }).RunWrite(a.conn); err != nil { return err } // Subscriptions don't need to be updated: // subscriptions of a disabled user are not disabled and still can be manipulated. return nil } // UserUpdate updates user object. func (a *adapter) UserUpdate(uid t.Uid, update map[string]any) error { _, err := rdb.DB(a.dbName).Table("users").Get(uid.String()).Update(update).RunWrite(a.conn) if err != nil { return err } if state, ok := update["State"]; ok { now, _ := update["StateAt"].(time.Time) err = a.topicStateForUser(uid, now, state) } return err } // UserUpdateTags append or resets user's tags func (a *adapter) UserUpdateTags(uid t.Uid, add, remove, reset []string) ([]string, error) { // Compare to nil vs checking for zero length: zero length reset is valid. if reset != nil { // Replace Tags with the new value return reset, a.UserUpdate(uid, map[string]any{"Tags": reset}) } // Mutate the tag list. newTags := rdb.Row.Field("Tags") if len(add) > 0 { newTags = newTags.SetUnion(add) } if len(remove) > 0 { newTags = newTags.SetDifference(remove) } q := rdb.DB(a.dbName).Table("users").Get(uid.String()) _, err := q.Update(map[string]any{"Tags": newTags}).RunWrite(a.conn) if err != nil { return nil, err } // Get the new tags. // Using Pluck instead of Field because of https://github.com/rethinkdb/rethinkdb-go/issues/486 cursor, err := q.Pluck("Tags").Run(a.conn) if err != nil { return nil, err } defer cursor.Close() var tagsField struct{ Tags []string } err = cursor.One(&tagsField) if err != nil { return nil, err } if len(tagsField.Tags) == 0 { tagsField.Tags = nil } return tagsField.Tags, nil } // UserGetByCred returns user ID for the given validated credential. func (a *adapter) UserGetByCred(method, value string) (t.Uid, error) { cursor, err := rdb.DB(a.dbName).Table("credentials").Get(method + ":" + value).Field("User").Default(nil).Run(a.conn) if err != nil { return t.ZeroUid, err } defer cursor.Close() if cursor.IsNil() { return t.ZeroUid, nil } var userId string if err = cursor.One(&userId); err != nil { return t.ZeroUid, err } return t.ParseUid(userId), nil } // UserUnreadCount returns the total number of unread messages in all topics with // the R permission. If read fails, the counts are still returned with the original // user IDs but with the unread count undefined and non-nil error. // UserUnreadCount does not count unread messages in channels although it should. func (a *adapter) UserUnreadCount(ids ...t.Uid) (map[t.Uid]int, error) { // The call expects user IDs to be plain strings like "356zaYaumiU". uids := make([]any, len(ids)) counts := make(map[t.Uid]int, len(ids)) for i, id := range ids { uids[i] = id.String() // Ensure all original uids are always present. counts[id] = 0 } /* Query: r.db("tinode").table("subscriptions").getAll("356zaYaumiU", "k4cvfaq8zCQ", {index: "User"}) .eqJoin("Topic", r.db("tinode").table("topics"), {index: "Id"}) .filter( r.not(r.row.hasFields({"left": "DeletedAt"}).or(r.row("right")("State").eq(20))) ) .zip() .pluck("User", "ReadSeqId", "ModeWant", "ModeGiven", "SeqId") .filter(r.js('(function(row) {return row.ModeWant&row.ModeGiven&1 > 0;})')) .group("User") .sum(function(x) {return x.getField("SeqId").sub(x.getField("ReadSeqId"));}) Result: [{group: "356zaYaumiU", reduction: 1}, {group: "k4cvfaq8zCQ", reduction: 0}] */ cursor, err := rdb.DB(a.dbName).Table("subscriptions").GetAllByIndex("User", uids...). EqJoin("Topic", rdb.DB(a.dbName).Table("topics"), rdb.EqJoinOpts{Index: "Id"}). // left: subscription; right: topic. Filter( rdb.Not(rdb.Row.HasFields(map[string]any{"left": "DeletedAt"}). Or(rdb.Row.Field("right").Field("State").Eq(t.StateDeleted)))). Zip(). Pluck("User", "ReadSeqId", "ModeWant", "ModeGiven", "SeqId"). Filter(rdb.JS("(function(row) {return (row.ModeWant & row.ModeGiven & " + strconv.Itoa(int(t.ModeRead)) + ") > 0;})")). Group("User"). Sum(func(row rdb.Term) rdb.Term { return row.Field("SeqId").Sub(row.Field("ReadSeqId")) }). Run(a.conn) if err != nil { return counts, err } defer cursor.Close() var oneCount struct { Group string Reduction int } for cursor.Next(&oneCount) { counts[t.ParseUid(oneCount.Group)] = oneCount.Reduction } err = cursor.Err() return counts, err } // UserGetUnvalidated returns a list of uids which have never logged in, have no // validated credentials and haven't been updated since lastUpdatedBefore. func (a *adapter) UserGetUnvalidated(lastUpdatedBefore time.Time, limit int) ([]t.Uid, error) { /* Query: r.db('tinode').table('users') .filter(r.row('LastSeen').eq(null).and(r.row('UpdatedAt').lt('Mar 31 2022 01:03:38'))) .eqJoin('Id', r.db('tinode').table('credentials'), {index: 'User'}).zip() .pluck('User', 'Done') .group('User') .sum(function(row) {return r.branch(row('Done'), 1, 0)}) .ungroup() .filter({reduction: 0}) .pluck('group').limit(10) Result: [{"group": "3W1hPuHjobg"}, {"group": "Fh_skXNRhVg"}, {"group": "NqMZzq0ajWk"}] */ cursor, err := rdb.DB(a.dbName).Table("users"). Filter(rdb.Row.Field("LastSeen").Eq(nil).And(rdb.Row.Field("UpdatedAt").Lt(lastUpdatedBefore))). EqJoin("Id", rdb.DB(a.dbName).Table("credentials"), rdb.EqJoinOpts{Index: "User"}).Zip(). Pluck("User", "Done"). Group("User"). Sum(func(row rdb.Term) rdb.Term { return rdb.Branch(row.Field("Done"), 1, 0) }). Ungroup(). Filter(rdb.Row.Field("reduction").Eq(0)). Pluck("group"). Limit(limit). Run(a.conn) if err != nil { return nil, err } defer cursor.Close() var rec struct { Group string } var uids []t.Uid for cursor.Next(&rec) { uid := t.ParseUid(rec.Group) if !uid.IsZero() { uids = append(uids, uid) } else { return nil, errors.New("bad uid field") } } err = cursor.Err() return uids, err } // TopicCreate creates a topic from template func (a *adapter) TopicCreate(topic *t.Topic) error { _, err := rdb.DB(a.dbName).Table("topics").Insert(&topic).RunWrite(a.conn) return err } // TopicCreateP2P given two users creates a p2p topic func (a *adapter) TopicCreateP2P(initiator, invited *t.Subscription) error { initiator.Id = initiator.Topic + ":" + initiator.User // Don't care if the initiator changes own subscription _, err := rdb.DB(a.dbName).Table("subscriptions").Insert(initiator, rdb.InsertOpts{Conflict: "replace"}). RunWrite(a.conn) if err != nil { return err } // If the second subscription exists, don't overwrite it. Just make sure it's not deleted. invited.Id = invited.Topic + ":" + invited.User _, err = rdb.DB(a.dbName).Table("subscriptions").Insert(invited, rdb.InsertOpts{Conflict: "error"}). RunWrite(a.conn) if err != nil { // Is this a duplicate subscription? if !rdb.IsConflictErr(err) { // It's a genuine DB error return err } // Undelete the second subsription if it exists: remove DeletedAt, update CreatedAt and UpdatedAt, // update ModeGiven. _, err = rdb.DB(a.dbName).Table("subscriptions"). Get(invited.Id).Replace( rdb.Row.Without("DeletedAt"). Merge(map[string]any{ "CreatedAt": invited.CreatedAt, "UpdatedAt": invited.UpdatedAt, "ModeGiven": invited.ModeGiven})). RunWrite(a.conn) if err != nil { return err } } topic := &t.Topic{ObjHeader: t.ObjHeader{Id: initiator.Topic}} topic.ObjHeader.MergeTimes(&initiator.ObjHeader) topic.TouchedAt = initiator.GetTouchedAt() return a.TopicCreate(topic) } // TopicGet loads a single topic by name, if it exists. If the topic does not exist the call returns (nil, nil) func (a *adapter) TopicGet(topic string) (*t.Topic, error) { // Fetch topic by name cursor, err := rdb.DB(a.dbName).Table("topics").Get(topic).Run(a.conn) if err != nil { return nil, err } var tt = new(t.Topic) if err = cursor.One(tt); err != nil { if err == rdb.ErrEmptyResult { err = nil // No error if topic is not found. } return nil, err } // The cursor is automatically closed by executing cursor.One. if t.GetTopicCat(topic) == t.TopicCatGrp { // Topic found, get subsription count. Try both topic and channel names. if cursor, err = rdb.DB(a.dbName).Table("subscriptions"). GetAllByIndex("Topic", topic, t.GrpToChn(topic)). Filter(rdb.Row.HasFields("DeletedAt").Not()). Count().Run(a.conn); err != nil { return nil, err } subCnt := 0 if err = cursor.One(&subCnt); err != nil { return nil, err } // No need to close the cursor. if subCnt != tt.SubCnt { // Update the topic with the correct subscription count. tt.SubCnt = subCnt if _, err = rdb.DB(a.dbName).Table("topics").Get(topic). Update(map[string]any{"SubCnt": subCnt}).RunWrite(a.conn); err != nil { return nil, err } } } // RethinkDB go driver incorrectly converts UTC timezone to +0000 tt.CreatedAt = tt.CreatedAt.UTC() tt.UpdatedAt = tt.UpdatedAt.UTC() tt.TouchedAt = tt.TouchedAt.UTC() if tt.StateAt != nil { stateAt := tt.StateAt.UTC() tt.StateAt = &stateAt } return tt, nil } // TopicsForUser loads user's contact list: p2p and grp topics, except for 'me' & 'fnd' subscriptions. // Reads and denormalizes Public value. func (a *adapter) TopicsForUser(uid t.Uid, keepDeleted bool, opts *t.QueryOpt) ([]t.Subscription, error) { // Fetch ALL user's subscriptions, even those which has not been modified recently. // We are going to use these subscriptions to fetch topics and users which may have been modified recently. q := rdb.DB(a.dbName).Table("subscriptions").GetAllByIndex("User", uid.String()) if !keepDeleted { // Filter out rows with defined DeletedAt q = q.Filter(rdb.Row.HasFields("DeletedAt").Not()) } limit := 0 ims := time.Time{} if opts != nil { if opts.Topic != "" { q = q.Filter(rdb.Row.Field("Topic").Eq(opts.Topic)) } // Apply the limit only when the client does not manage the cache (or cold start). // Otherwise have to get all subscriptions and do a manual join with users/topics. if opts.IfModifiedSince == nil { if opts.Limit > 0 && opts.Limit < a.maxResults { limit = opts.Limit } else { limit = a.maxResults } } else { ims = *opts.IfModifiedSince } } else { limit = a.maxResults } if limit > 0 { q = q.Limit(limit) } cursor, err := q.Run(a.conn) if err != nil { return nil, err } // Fetch subscriptions. Two queries are needed: users table (me & p2p) and topics table (p2p and grp). // Prepare a list of Separate subscriptions to users vs topics var sub t.Subscription join := make(map[string]t.Subscription) // Keeping these to make a join with table for .private and .access topq := make([]any, 0, 16) usrq := make([]any, 0, 16) for cursor.Next(&sub) { tname := sub.Topic sub.User = uid.String() tcat := t.GetTopicCat(tname) if tcat == t.TopicCatMe || tcat == t.TopicCatFnd { // 'me' or 'fnd' subscription, skip. Don't skip 'sys'. continue } else if tcat == t.TopicCatP2P { // P2P subscription, find the other user to get user.Public uid1, uid2, _ := t.ParseP2P(sub.Topic) if uid1 == uid { usrq = append(usrq, uid2.String()) sub.SetWith(uid2.UserId()) } else { usrq = append(usrq, uid1.String()) sub.SetWith(uid1.UserId()) } } else if tcat == t.TopicCatGrp { // Maybe convert channel name to topic name. tname = t.ChnToGrp(tname) } // No special handling needed for 'slf', 'sys' subscriptions. topq = append(topq, tname) join[tname] = sub } err = cursor.Err() cursor.Close() if err != nil { return nil, err } var subs []t.Subscription if len(join) == 0 { return subs, nil } if len(topq) > 0 { // Fetch grp & p2p topics q = rdb.DB(a.dbName).Table("topics").GetAll(topq...) if !keepDeleted { q = q.Filter(rdb.Row.Field("State").Eq(t.StateDeleted).Not()) } if !ims.IsZero() { // Use cache timestamp if provided: get newer entries only. q = q.Filter(rdb.Row.Field("TouchedAt").Gt(ims)) if limit > 0 && limit < len(topq) { // No point in fetching more than the requested limit. q = q.OrderBy("TouchedAt").Limit(limit) } } cursor, err = q.Run(a.conn) if err != nil { return nil, err } var top t.Topic for cursor.Next(&top) { sub = join[top.Id] // Check if sub.UpdatedAt needs to be adjusted to earlier or later time. // top.UpdatedAt is guaranteed to be after IMS if IMS is non-zero. sub.UpdatedAt = common.SelectLatestTime(sub.UpdatedAt, top.UpdatedAt) sub.SetState(top.State) sub.SetTouchedAt(top.TouchedAt) sub.SetSeqId(top.SeqId) if t.GetTopicCat(sub.Topic) == t.TopicCatGrp { sub.SetSubCnt(top.SubCnt) sub.SetPublic(top.Public) sub.SetTrusted(top.Trusted) } // Put back the updated value of a subsription, will process further below. join[top.Id] = sub } err = cursor.Err() cursor.Close() if err != nil { return nil, err } } // Fetch p2p users and join to p2p subscriptions. if len(usrq) > 0 { q = rdb.DB(a.dbName).Table("users").GetAll(usrq...) if !keepDeleted { // Optionally skip deleted users. q = q.Filter(rdb.Row.Field("State").Eq(t.StateDeleted).Not()) } // Ignoring ims: we need all users to get LastSeen and UserAgent. cursor, err = q.Run(a.conn) if err != nil { return nil, err } var usr2 t.User for cursor.Next(&usr2) { joinOn := uid.P2PName(t.ParseUid(usr2.Id)) if sub, ok := join[joinOn]; ok { sub.UpdatedAt = common.SelectLatestTime(sub.UpdatedAt, usr2.UpdatedAt) sub.SetState(usr2.State) sub.SetPublic(usr2.Public) sub.SetTrusted(usr2.Trusted) sub.SetDefaultAccess(usr2.Access.Auth, usr2.Access.Anon) sub.SetLastSeenAndUA(usr2.LastSeen, usr2.UserAgent) join[joinOn] = sub } } err = cursor.Err() cursor.Close() if err != nil { return nil, err } } subs = make([]t.Subscription, 0, len(join)) for _, sub := range join { subs = append(subs, sub) } return common.SelectEarliestUpdatedSubs(subs, opts, a.maxResults), nil } // UsersForTopic loads users subscribed to the given topic (not channel readers). // The difference between UsersForTopic vs SubsForTopic is that the former loads user.Public, // the latter does not. func (a *adapter) UsersForTopic(topic string, keepDeleted bool, opts *t.QueryOpt) ([]t.Subscription, error) { tcat := t.GetTopicCat(topic) // Fetch topic subscribers // Fetch all subscribed users. The number of users is not large q := rdb.DB(a.dbName).Table("subscriptions").GetAllByIndex("Topic", topic) if !keepDeleted && tcat != t.TopicCatP2P { // Filter out rows with DeletedAt being not null. // P2P topics must load all subscriptions otherwise it will be impossible // to swap Public values. q = q.Filter(rdb.Row.HasFields("DeletedAt").Not()) } limit := a.maxResults var oneUser t.Uid if opts != nil { // Ignore IfModifiedSince - we must return all entries // Those unmodified will be stripped of Public & Private. if !opts.User.IsZero() { if tcat != t.TopicCatP2P { q = q.Filter(rdb.Row.Field("User").Eq(opts.User.String())) } oneUser = opts.User } if opts.Limit > 0 && opts.Limit < limit { limit = opts.Limit } } q = q.Limit(limit) cursor, err := q.Run(a.conn) if err != nil { return nil, err } // Fetch subscriptions var sub t.Subscription var subs []t.Subscription join := make(map[string]t.Subscription) usrq := make([]any, 0, 16) for cursor.Next(&sub) { join[sub.User] = sub usrq = append(usrq, sub.User) } cursor.Close() if len(usrq) > 0 { subs = make([]t.Subscription, 0, len(usrq)) // Fetch users by a list of subscriptions cursor, err = rdb.DB(a.dbName).Table("users").GetAll(usrq...). Filter(rdb.Row.Field("State").Eq(t.StateDeleted).Not()).Run(a.conn) if err != nil { return nil, err } var usr t.User for cursor.Next(&usr) { if sub, ok := join[usr.Id]; ok { sub.ObjHeader.MergeTimes(&usr.ObjHeader) sub.SetPublic(usr.Public) sub.SetTrusted(usr.Trusted) sub.SetLastSeenAndUA(usr.LastSeen, usr.UserAgent) subs = append(subs, sub) } } cursor.Close() } if t.GetTopicCat(topic) == t.TopicCatP2P && len(subs) > 0 { // Swap public values & lastSeen of P2P topics as expected. if len(subs) == 1 { // User is deleted. Nothing we can do. subs[0].SetPublic(nil) subs[0].SetTrusted(nil) subs[0].SetLastSeenAndUA(nil, "") } else { tmp := subs[0].GetPublic() subs[0].SetPublic(subs[1].GetPublic()) subs[1].SetPublic(tmp) tmp = subs[0].GetTrusted() subs[0].SetTrusted(subs[1].GetTrusted()) subs[1].SetTrusted(tmp) lastSeen := subs[0].GetLastSeen() userAgent := subs[0].GetUserAgent() subs[0].SetLastSeenAndUA(subs[1].GetLastSeen(), subs[1].GetUserAgent()) subs[1].SetLastSeenAndUA(lastSeen, userAgent) } // Remove deleted and unneeded subscriptions if !keepDeleted || !oneUser.IsZero() { var xsubs []t.Subscription for i := range subs { if (subs[i].DeletedAt != nil && !keepDeleted) || (!oneUser.IsZero() && subs[i].Uid() != oneUser) { continue } xsubs = append(xsubs, subs[i]) } subs = xsubs } } return subs, nil } // OwnTopics loads a slice of topic names where the user is the owner. func (a *adapter) OwnTopics(uid t.Uid) ([]string, error) { cursor, err := rdb.DB(a.dbName).Table("topics").GetAllByIndex("Owner", uid.String()). Filter(rdb.Row.Field("State").Eq(t.StateDeleted).Not()).Field("Id").Run(a.conn) if err != nil { return nil, err } var names []string var name string for cursor.Next(&name) { names = append(names, name) } cursor.Close() return names, nil } // ChannelsForUser loads a slice of topic names where the user is a channel reader and notifications (P) are enabled. func (a *adapter) ChannelsForUser(uid t.Uid) ([]string, error) { cursor, err := rdb.DB(a.dbName).Table("subscriptions"). GetAllByIndex("User", uid.String()). Filter(rdb.Row.HasFields("DeletedAt").Not()). Filter(rdb.Row.Field("Topic").Match("^chn")). Filter(rdb.JS("(function(row) {return (row.ModeWant & row.ModeGiven & " + strconv.Itoa(int(t.ModePres)) + ") > 0;})")). Field("Topic").Run(a.conn) if err != nil { return nil, err } var names []string var name string for cursor.Next(&name) { names = append(names, name) } cursor.Close() return names, nil } // TopicShare adds subscriptions to a topic and increments the topic's subcnt. func (a *adapter) TopicShare(topic string, shares []*t.Subscription) error { // Assign Ids. for _, sub := range shares { sub.Id = sub.Topic + ":" + sub.User } // Subscription could have been marked as deleted (DeletedAt != nil). If it's marked // as deleted, unmark by clearing the DeletedAt field of the old subscription and // updating times and ModeGiven. _, err := rdb.DB(a.dbName).Table("subscriptions"). Insert(shares, rdb.InsertOpts{Conflict: func(id, oldsub, newsub rdb.Term) any { return oldsub.Without("DeletedAt").Merge(map[string]any{ "CreatedAt": newsub.Field("CreatedAt"), "UpdatedAt": newsub.Field("UpdatedAt"), "ModeGiven": newsub.Field("ModeGiven"), "ModeWant": newsub.Field("ModeWant"), "DelId": 0, "ReadSeqId": 0, "RecvSeqId": 0}) }}).RunWrite(a.conn) if err == nil && topic != "" { _, err = rdb.DB(a.dbName).Table("topics"). Get(topic). Update(map[string]any{"SubCnt": rdb.Row.Field("SubCnt").Default(0).Add(len(shares))}). RunWrite(a.conn) } return err } // TopicDelete deletes topic, subscriptions, messages. func (a *adapter) TopicDelete(topic string, isChan, hard bool) error { var err error if err = a.subsDelForTopic(topic, isChan, hard); err != nil { return err } if hard { if err = a.MessageDeleteList(topic, nil); err != nil { return err } } // Must use GetAll to produce array result expected by decFileUseCounter. q := rdb.DB(a.dbName).Table("topics").GetAll(topic) if hard { if err = a.decFileUseCounter(q); err == nil { _, err = q.Delete().RunWrite(a.conn) } } else { now := t.TimeNow() _, err = q.Update(map[string]any{ "UpdatedAt": now, "TouchedAt": now, "State": t.StateDeleted, "StatedAt": now, }).RunWrite(a.conn) } return err } // TopicUpdateOnMessage deserializes message-related values into topic. func (a *adapter) TopicUpdateOnMessage(topic string, msg *t.Message) error { update := struct { SeqId int TouchedAt time.Time }{msg.SeqId, msg.CreatedAt} _, err := rdb.DB(a.dbName).Table("topics").Get(topic). Update(update, rdb.UpdateOpts{Durability: "soft"}).RunWrite(a.conn) return err } // TopicUpdateSubCnt updates subscriber count denormalized in topic. func (a *adapter) TopicUpdateSubCnt(topic string) error { cursor, err := rdb.DB(a.dbName).Table("subscriptions"). GetAllByIndex("Topic", topic, t.GrpToChn(topic)). Filter(rdb.Row.HasFields("DeletedAt").Not()). Count().Run(a.conn) if err != nil { return err } defer cursor.Close() subCnt := 0 if !cursor.IsNil() { if err = cursor.One(&subCnt); err != nil { return err } } _, err = rdb.DB(a.dbName).Table("topics"). Get(topic). Update(map[string]any{ "SubCnt": subCnt, }).RunWrite(a.conn) return err } // TopicUpdate performs a generic topic update. func (a *adapter) TopicUpdate(topic string, update map[string]any) error { if t, u := update["TouchedAt"], update["UpdatedAt"]; t == nil && u != nil { update["TouchedAt"] = u } _, err := rdb.DB(a.dbName).Table("topics").Get(topic).Update(update).RunWrite(a.conn) return err } // TopicOwnerChange changes topic's owner. func (a *adapter) TopicOwnerChange(topic string, newOwner t.Uid) error { _, err := rdb.DB(a.dbName).Table("topics").Get(topic). Update(map[string]any{"Owner": newOwner.String()}).RunWrite(a.conn) return err } // SubscriptionGet returns a subscription of a user to a topic func (a *adapter) SubscriptionGet(topic string, user t.Uid, keepDeleted bool) (*t.Subscription, error) { cursor, err := rdb.DB(a.dbName).Table("subscriptions").Get(topic + ":" + user.String()).Run(a.conn) if err != nil { return nil, err } defer cursor.Close() if cursor.IsNil() { return nil, nil } var sub t.Subscription if err = cursor.One(&sub); err != nil { return nil, err } if !keepDeleted && sub.DeletedAt != nil { return nil, nil } return &sub, nil } // SubsForUser loads all user's subscriptions. Does NOT load Public or Private values and does // not load deleted subscriptions. func (a *adapter) SubsForUser(forUser t.Uid) ([]t.Subscription, error) { q := rdb.DB(a.dbName). Table("subscriptions"). GetAllByIndex("User", forUser.String()). Filter(rdb.Row.HasFields("DeletedAt").Not()). Without("Private") cursor, err := q.Run(a.conn) if err != nil { return nil, err } defer cursor.Close() var subs []t.Subscription var ss t.Subscription for cursor.Next(&ss) { subs = append(subs, ss) } return subs, cursor.Err() } // SubsForTopic fetches all subsciptions for a topic. Does NOT load Public value. func (a *adapter) SubsForTopic(topic string, keepDeleted bool, opts *t.QueryOpt) ([]t.Subscription, error) { q := rdb.DB(a.dbName).Table("subscriptions").GetAllByIndex("Topic", topic) if !keepDeleted { // Filter out rows where DeletedAt is defined q = q.Filter(rdb.Row.HasFields("DeletedAt").Not()) } limit := a.maxResults if opts != nil { // Ignore IfModifiedSince - we must return all entries // Those unmodified will be stripped of Public & Private. if !opts.User.IsZero() { q = q.Filter(rdb.Row.Field("User").Eq(opts.User.String())) } if opts.Limit > 0 && opts.Limit < limit { limit = opts.Limit } } q = q.Limit(limit) cursor, err := q.Run(a.conn) if err != nil { return nil, err } defer cursor.Close() var subs []t.Subscription var ss t.Subscription for cursor.Next(&ss) { subs = append(subs, ss) } return subs, cursor.Err() } // SubsUpdate updates a single subscription. func (a *adapter) SubsUpdate(topic string, user t.Uid, update map[string]any) error { q := rdb.DB(a.dbName).Table("subscriptions") if !user.IsZero() { // Update one topic subscription q = q.Get(topic + ":" + user.String()) } else { // Update all topic subscriptions q = q.GetAllByIndex("Topic", topic) } _, err := q.Update(update).RunWrite(a.conn) return err } // SubsDelete marks at most one subscription as deleted. func (a *adapter) SubsDelete(topic string, user t.Uid) error { now := t.TimeNow() forUser := user.String() // Mark subscription as deleted. res, err := rdb.DB(a.dbName).Table("subscriptions"). Get(topic + ":" + forUser).Update(map[string]any{ "UpdatedAt": now, "DeletedAt": now, }).RunWrite(a.conn) if err != nil { return err } if res.Replaced == 0 { // Nothing was updated, nothing more to do. return t.ErrNotFound } // Decrement topic's SubCnt. _, err = rdb.DB(a.dbName).Table("topics").Get(topic). Update(map[string]any{"SubCnt": rdb.Row.Field("SubCnt").Default(1).Sub(1)}). RunWrite(a.conn) if err != nil { return err } if t.IsChannel(topic) { // Channel readers cannot delete messages, all done. return nil } // Remove records of deleted messages. // Delete dellog entries of the current user. resp, err := rdb.DB(a.dbName).Table("dellog"). // Select all log entries for the given table. Between([]any{topic, rdb.MinVal}, []any{topic, rdb.MaxVal}, rdb.BetweenOpts{Index: "Topic_DelId"}). // Keep entries soft-deleted for the current user only. Filter(rdb.Row.Field("DeletedFor").Eq(forUser)). // Delete them. Delete(). RunWrite(a.conn) if err != nil || resp.Deleted == 0 { // Either an error or nothing was deleted. Not much we can do with the error. // Returning nil even on failure. return nil } // Remove current user from the messages' soft-deletion lists. // The possible error here is ignored. rdb.DB(a.dbName).Table("messages"). // Select all messages in the given topic. Between( []any{topic, forUser, rdb.MinVal}, []any{topic, forUser, rdb.MaxVal}, rdb.BetweenOpts{Index: "Topic_DeletedFor"}). // Update the field DeletedFor: Update(map[string]any{ // Take the DeletedFor array, subtract all values which contain current user ID in 'User' field. "DeletedFor": rdb.Row.Field("DeletedFor"). SetDifference( rdb.Row.Field("DeletedFor"). Filter(map[string]any{"User": forUser}))}). RunWrite(a.conn) return nil } // subsDelForTopic marks all subscriptions to the given topic as deleted. func (a *adapter) subsDelForTopic(topic string, isChan, hard bool) error { var err error q := rdb.DB(a.dbName).Table("subscriptions") if isChan { // If the topic is a channel, must try to delete subscriptions under both grpXXX and chnXXX names. q = q.GetAllByIndex("Topic", topic, t.GrpToChn(topic)) } else { q = q.GetAllByIndex("Topic", topic) } if hard { _, err = q.Delete().RunWrite(a.conn) } else { now := t.TimeNow() _, err = q.Update(map[string]any{ "UpdatedAt": now, "DeletedAt": now, }).RunWrite(a.conn) } return err } // subsDelForUser marks all subscriptions of a given user as deleted. func (a *adapter) subsDelForUser(user t.Uid, hard bool) error { var err error forUser := user.String() // Get all topics the user is subscribed to. Channels are left as channels. topics, err := a.topicNamesForUser(rdb.DB(a.dbName).Table("subscriptions"). GetAllByIndex("User", forUser).Field("Topic"), false) if err != nil { logs.Err.Println("subsDelForUser: topicNamesForUser:", err) return err } // 1. Decrement SubCnt in topic. if _, err = rdb.DB(a.dbName).Table("topics").Get(topics...). Update(map[string]any{"SubCnt": rdb.Row.Field("SubCnt"). Default(1).Sub(1)}). RunWrite(a.conn); err != nil { return err } err = a.clearUserDellog(user, topics) if err != nil { logs.Err.Println("subsDelForUser: clearUserDellog:", err) return err } if hard { _, err = rdb.DB(a.dbName).Table("subscriptions").GetAllByIndex("User", user.String()). Delete().RunWrite(a.conn) } else { now := t.TimeNow() update := map[string]any{ "UpdatedAt": now, "DeletedAt": now, } _, err = rdb.DB(a.dbName).Table("subscriptions").GetAllByIndex("User", user.String()). Update(update).RunWrite(a.conn) } return err } // Find returns a list of users and topics who match the given tags, such as "email:jdoe@example.com" or "tel:+18003287448". func (a *adapter) Find(caller, promoPrefix string, req [][]string, opt []string, activeOnly bool) ([]t.Subscription, error) { index := make(map[string]struct{}) allReq := t.FlattenDoubleSlice(req) var allTags []any for _, tag := range append(allReq, opt...) { allTags = append(allTags, tag) index[tag] = struct{}{} } // Query for selecting matches where every group includes at least one required match (restricting search to // group members). /* r.db('tinode'). table('users'). getAll('basic:alice', 'travel', {index: "Tags"}). union(r.db('tinode').table('topics').getAll('basic:alice', 'travel', {index: "Tags"})). pluck('Id', 'Access', 'CreatedAt', 'UpdatedAt', 'UseBt', 'Public', 'Trusted', 'Tags'). group('Id'). ungroup(). map(row => row.getField('reduction').nth(0).merge( {matchedCount: row.getField('reduction'). getField('Tags'). nth(0). setIntersection(['alias:aliassa', 'basic:alice', 'travel']). map(tag => r.branch(tag.match('^alias:'), 20, 1)). sum() })). filter(row => row.getField('Tags').setIntersection(['basic:alice', 'travel']).count().ne(0)). orderBy(r.desc('matchedCount')). limit(20) */ // Get users and topics matched by tags, sort by number of matches from high to low. query := rdb.DB(a.dbName). Table("users"). GetAllByIndex("Tags", allTags...). Union(rdb.DB(a.dbName).Table("topics"). GetAllByIndex("Tags", allTags...)) if activeOnly { query = query.Filter(rdb.Row.Field("State").Eq(t.StateOK)) } query = query.Pluck("Id", "Access", "CreatedAt", "UpdatedAt", "UseBt", "SubCnt", "Public", "Trusted", "Tags"). Group("Id"). Ungroup(). Map(func(row rdb.Term) rdb.Term { return row.Field("reduction"). Nth(0). Merge(map[string]any{"MatchedTagsCount": row.Field("reduction"). Field("Tags"). Nth(0). SetIntersection(allTags). Map(func(tag rdb.Term) any { return rdb.Branch( tag.Match("^"+promoPrefix), 20, // If the tag matches the promo prefix, count it as 20. 1) // Otherwise count it as 1. }). Sum()}) }) for _, reqDisjunction := range req { if len(reqDisjunction) == 0 { continue } var reqTags []any for _, tag := range reqDisjunction { reqTags = append(reqTags, tag) } // Filter out objects which do not match at least one of the required tags. query = query.Filter(func(row rdb.Term) rdb.Term { return row.Field("Tags").SetIntersection(reqTags).Count().Ne(0) }) } cursor, err := query.OrderBy(rdb.Desc("MatchedTagsCount")).Limit(a.maxResults).Run(a.conn) if err != nil { return nil, err } defer cursor.Close() var topic t.Topic var sub t.Subscription var subs []t.Subscription for cursor.Next(&topic) { if uid := t.ParseUid(topic.Id); !uid.IsZero() { topic.Id = uid.UserId() if topic.Id == caller { // Skip the caller continue } } if topic.UseBt { sub.Topic = t.GrpToChn(topic.Id) } else { sub.Topic = topic.Id } sub.CreatedAt = topic.CreatedAt sub.UpdatedAt = topic.UpdatedAt sub.SetSubCnt(topic.SubCnt) sub.SetPublic(topic.Public) sub.SetTrusted(topic.Trusted) sub.SetDefaultAccess(topic.Access.Auth, topic.Access.Anon) // Indicating that the mode is not set, not 'N'. sub.ModeGiven = t.ModeUnset sub.ModeWant = t.ModeUnset sub.Private = common.FilterFoundTags(topic.Tags, index) subs = append(subs, sub) } return subs, cursor.Err() } // FindOne returns topic or user which matches the given tag. func (a *adapter) FindOne(tag string) (string, error) { query := rdb.DB(a.dbName). Table("users").GetAllByIndex("Tags", tag). Union(rdb.DB(a.dbName).Table("topics").GetAllByIndex("Tags", tag)). Field("Id"). Limit(1) cursor, err := query.Run(a.conn) if err != nil { return "", err } defer cursor.Close() var found string if err = cursor.One(&found); err != nil { if err == rdb.ErrEmptyResult { return "", nil } return "", err } if user := t.ParseUid(found); !user.IsZero() { found = user.UserId() } return found, nil } // Messages // MessageSave saves message to DB. func (a *adapter) MessageSave(msg *t.Message) error { _, err := rdb.DB(a.dbName).Table("messages").Insert(msg).RunWrite(a.conn) return err } // MessageGetAll retrieves all messages available to the given user. func (a *adapter) MessageGetAll(topic string, forUser t.Uid, opts *t.QueryOpt) ([]t.Message, error) { var limit = a.maxMessageResults var lower, upper any upper = rdb.MaxVal lower = rdb.MinVal if opts != nil { if opts.Since > 0 { lower = opts.Since } if opts.Before > 0 { upper = opts.Before } if opts.Limit > 0 && opts.Limit < limit { limit = opts.Limit } } lower = []any{topic, lower} upper = []any{topic, upper} requester := forUser.String() cursor, err := rdb.DB(a.dbName).Table("messages"). Between(lower, upper, rdb.BetweenOpts{Index: "Topic_SeqId"}). // Ordering by index must come before filtering OrderBy(rdb.OrderByOpts{Index: rdb.Desc("Topic_SeqId")}). // Skip hard-deleted messages Filter(rdb.Row.HasFields("DelId").Not()). // Skip messages soft-deleted for the current user Filter(func(row rdb.Term) any { return rdb.Not(row.Field("DeletedFor").Default([]any{}).Contains( func(df rdb.Term) any { return df.Field("User").Eq(requester) })) }).Limit(limit).Run(a.conn) if err != nil { return nil, err } defer cursor.Close() var msgs []t.Message if err = cursor.All(&msgs); err != nil { return nil, err } return msgs, nil } // MessageGetDeleted returns ranges of deleted messages. func (a *adapter) MessageGetDeleted(topic string, forUser t.Uid, opts *t.QueryOpt) ([]t.DelMessage, error) { /* r.db('tinode_test') .table('dellog') .between( ['p2p9AVDamaNCRbfKzGSh3mE0w', 1], ['p2p9AVDamaNCRbfKzGSh3mE0w', 10], {index: 'Topic_DelId'} ) .orderBy('Topic_DelId') .filter( row => row.getField('DeletedFor').eq('0QLrX3WPS2o').or(row.getField('DeletedFor').eq('')) ) */ var limit = a.maxResults var lower, upper any upper = rdb.MaxVal lower = rdb.MinVal if opts != nil { if opts.Since > 0 { lower = opts.Since } if opts.Before > 0 { upper = opts.Before } if opts.Limit > 0 && opts.Limit < limit { limit = opts.Limit } } // Fetch log of deletions cursor, err := rdb.DB(a.dbName).Table("dellog"). // Select log entries for the given table and DelId values between two limits. // By default, leftBound is closed and rightBound is open. Between([]any{topic, lower}, []any{topic, upper}, rdb.BetweenOpts{Index: "Topic_DelId"}). // Sort from low DelIds to high OrderBy(rdb.OrderByOpts{Index: "Topic_DelId"}). // Keep entries soft-deleted for the current user and all hard-deleted entries. Filter(func(row rdb.Term) any { return row.Field("DeletedFor").Eq(forUser.String()).Or(row.Field("DeletedFor").Eq("")) }). Limit(limit).Run(a.conn) if err != nil { return nil, err } defer cursor.Close() var dmsgs []t.DelMessage if err = cursor.All(&dmsgs); err != nil { return nil, err } return dmsgs, nil } // messagesHardDelete deletes all messages in the topic. func (a *adapter) messagesHardDelete(topic string) error { var err error // TODO: handle file uploads if _, err = rdb.DB(a.dbName).Table("dellog").Between( []any{topic, rdb.MinVal}, []any{topic, rdb.MaxVal}, rdb.BetweenOpts{Index: "Topic_DelId"}).Delete().RunWrite(a.conn); err != nil { return err } q := rdb.DB(a.dbName).Table("messages").Between( []any{topic, rdb.MinVal}, []any{topic, rdb.MaxVal}, rdb.BetweenOpts{Index: "Topic_SeqId"}) if err = a.decFileUseCounter(q); err != nil { return err } _, err = q.Delete().RunWrite(a.conn) return err } func rangeToQuery(delRanges []t.Range, topic string, query rdb.Term) rdb.Term { if len(delRanges) > 1 || delRanges[0].Hi <= delRanges[0].Low { var indexVals []any for _, rng := range delRanges { if rng.Hi == 0 { indexVals = append(indexVals, []any{topic, rng.Low}) } else { for i := rng.Low; i <= rng.Hi; i++ { indexVals = append(indexVals, []any{topic, i}) } } } query = query.GetAllByIndex("Topic_SeqId", indexVals...) } else { // Optimizing for a special case of single range low..hi query = query.Between( []any{topic, delRanges[0].Low}, []any{topic, delRanges[0].Hi}, rdb.BetweenOpts{Index: "Topic_SeqId", RightBound: "closed"}) } return query } // MessageDeleteList deletes messages in the given topic with seqIds from the list. func (a *adapter) MessageDeleteList(topic string, toDel *t.DelMessage) error { var err error if toDel == nil { // Delete all messages. return a.messagesHardDelete(topic) } // Only some messages are being deleted delRanges := toDel.SeqIdRanges query := rangeToQuery(delRanges, topic, rdb.DB(a.dbName).Table("messages")) // Skip already hard-deleted messages. query = query.Filter(rdb.Row.HasFields("DelId").Not()) if toDel.DeletedFor == "" { // Hard-deleting messages requires updates to the messages table. // We are asked to delete messages no older than newerThan. if newerThan := toDel.GetNewerThan(); newerThan != nil { query = query.Filter(rdb.Row.Field("CreatedAt").Gt(newerThan)) } query = query.Field("SeqId") // Find the actual IDs still present in the database. cursor, err := query.Run(a.conn) if err != nil { return err } defer cursor.Close() var seqIDs []int if err = cursor.All(&seqIDs); err != nil { return err } if len(seqIDs) == 0 { // Nothing to delete. No need to make a log entry. All done. return nil } // Recalculate the actual ranges to delete. sort.Ints(seqIDs) delRanges = t.SliceToRanges(seqIDs) // Compose a new query with the new ranges. query = rangeToQuery(delRanges, topic, rdb.DB(a.dbName).Table("messages")) // First decrement use counter for attachments. if err = a.decFileUseCounter(query); err != nil { return err } // Hard-delete individual messages. The messages are not deleted but all fields with personal content // are removed. if _, err = query.Replace(rdb.Row.Without("Head", "From", "Content", "Attachments").Merge( map[string]any{ "DeletedAt": t.TimeNow(), "DelId": toDel.DelId})). RunWrite(a.conn); err != nil { return err } } else { // Soft-deleting: adding DelId to DeletedFor. _, err = query. // Skip messages already soft-deleted for the current user Filter(func(row rdb.Term) any { return rdb.Not(row.Field("DeletedFor").Default([]any{}).Contains( func(df rdb.Term) any { return df.Field("User").Eq(toDel.DeletedFor) })) }). Update(map[string]any{"DeletedFor": rdb.Row.Field("DeletedFor"). Default([]any{}).Append( &t.SoftDelete{ User: toDel.DeletedFor, DelId: toDel.DelId})}).RunWrite(a.conn) if err != nil { return err } } // Make log entries. Needed for both hard- and soft-deleting. _, err = rdb.DB(a.dbName).Table("dellog").Insert(toDel).RunWrite(a.conn) return err } func deviceHasher(deviceID string) string { // Generate custom key as [64-bit hash of device id] to ensure predictable // length of the key hasher := fnv.New64() hasher.Write([]byte(deviceID)) return strconv.FormatUint(uint64(hasher.Sum64()), 16) } // Device management for push notifications // DeviceUpsert adds or updates a user's device FCM push token. func (a *adapter) DeviceUpsert(uid t.Uid, def *t.DeviceDef) error { hash := deviceHasher(def.DeviceId) user := uid.String() // Ensure uniqueness of the device ID // Find users who already use this device ID, ignore current user. cursor, err := rdb.DB(a.dbName).Table("users").GetAllByIndex("DeviceIds", def.DeviceId). // We only care about user Ids Pluck("Id"). // Make sure we filter out the current user who may legitimately use this device ID Filter(rdb.Not(rdb.Row.Field("Id").Eq(user))). // Convert slice of objects to a slice of strings ConcatMap(func(row rdb.Term) any { return []any{row.Field("Id")} }). // Execute Run(a.conn) if err != nil { return err } defer cursor.Close() var others []any if err = cursor.All(&others); err != nil { return err } if len(others) > 0 { // Delete device ID for the other users. _, err = rdb.DB(a.dbName).Table("users").GetAll(others...).Replace(rdb.Row.Without( map[string]string{"Devices": hash})).RunWrite(a.conn) if err != nil { return err } } // Actually add/update DeviceId for the new user _, err = rdb.DB(a.dbName).Table("users").Get(user). Update(map[string]any{ "Devices": map[string]*t.DeviceDef{ hash: def, }}).RunWrite(a.conn) return err } // DeviceGetAll retrives a list of user's devices (push tokens). func (a *adapter) DeviceGetAll(uids ...t.Uid) (map[t.Uid][]t.DeviceDef, int, error) { ids := make([]any, len(uids)) for i, id := range uids { ids[i] = id.String() } // {Id: "userid", Devices: {"hash1": {..def1..}, "hash2": {..def2..}} cursor, err := rdb.DB(a.dbName).Table("users").GetAll(ids...).Pluck("Id", "Devices"). Default(nil).Limit(a.maxResults).Run(a.conn) if err != nil { return nil, 0, err } defer cursor.Close() var row struct { Id string Devices map[string]*t.DeviceDef } result := make(map[t.Uid][]t.DeviceDef) count := 0 var uid t.Uid for cursor.Next(&row) { if len(row.Devices) > 0 { if err := uid.UnmarshalText([]byte(row.Id)); err != nil { continue } result[uid] = make([]t.DeviceDef, len(row.Devices)) i := 0 for _, def := range row.Devices { if def != nil { result[uid][i] = *def i++ count++ } } } } return result, count, cursor.Err() } // DeviceDelete removes user's device (push token). func (a *adapter) DeviceDelete(uid t.Uid, deviceID string) error { var err error q := rdb.DB(a.dbName).Table("users").Get(uid.String()) if deviceID == "" { q = q.Update(map[string]any{"Devices": nil}) } else { q = q.Replace(rdb.Row.Without(map[string]string{"Devices": deviceHasher(deviceID)})) } _, err = q.RunWrite(a.conn) return err } // Credential management // CredUpsert adds or updates a validation record. Returns true if inserted, false if updated. // 1. if credential is validated: // 1.1 Hard-delete unconfirmed equivalent record, if exists. // 1.2 Insert new. Report error if duplicate. // 2. if credential is not validated: // 2.1 Check if validated equivalent exist. If so, report an error. // 2.2 Soft-delete all unvalidated records of the same method. // 2.3 Undelete existing credential. Return if successful. // 2.4 Insert new credential record. func (a *adapter) CredUpsert(cred *t.Credential) (bool, error) { var err error tableCredentials := rdb.DB(a.dbName).Table("credentials") cred.Id = cred.Method + ":" + cred.Value if !cred.Done { // Check if the same credential is already validated. cursor, err := tableCredentials.Get(cred.Id).Run(a.conn) if err != nil { return false, err } defer cursor.Close() if !cursor.IsNil() { // Someone has already validated this credential. return false, t.ErrDuplicate } // Deactivate all unvalidated records of this user and method. _, err = tableCredentials.GetAllByIndex("User", cred.User). Filter(map[string]any{"Method": cred.Method, "Done": false}).Update( map[string]any{"DeletedAt": t.TimeNow()}).RunWrite(a.conn) if err != nil { return false, err } // If credential is not confirmed, it should not block others // from attempting to validate it: make index user-unique instead of global-unique. cred.Id = cred.User + ":" + cred.Id // Check if this credential has already been added by the user. cursor2, err := tableCredentials.Get(cred.Id).Run(a.conn) if err != nil { return false, err } defer cursor2.Close() if !cursor2.IsNil() { _, err = tableCredentials.Get(cred.Id). Replace(rdb.Row.Without("DeletedAt"). Merge(map[string]any{ "UpdatedAt": cred.UpdatedAt, "Resp": cred.Resp})).RunWrite(a.conn) if err != nil { return false, err } // The record was updated, all is fine. return false, nil } } else { // Hard-delete potentially present unvalidated credential. _, err = tableCredentials.Get(cred.User + ":" + cred.Id).Delete().RunWrite(a.conn) if err != nil { return false, err } } // Insert a new record. _, err = tableCredentials.Insert(cred).RunWrite(a.conn) if rdb.IsConflictErr(err) { return true, t.ErrDuplicate } return true, err } // CredDel deletes credentials for the given method. If method is empty, deletes all user's credentials. func (a *adapter) CredDel(uid t.Uid, method, value string) error { q := rdb.DB(a.dbName).Table("credentials"). GetAllByIndex("User", uid.String()) if method != "" { q = q.Filter(map[string]any{"Method": method}) if value != "" { q = q.Filter(map[string]any{"Value": value}) } } if method == "" { res, err := q.Delete().RunWrite(a.conn) if err == nil { if res.Deleted == 0 { err = t.ErrNotFound } } return err } // Hard-delete all confirmed values or values with no attempts at confirmation. res, err := q.Filter(rdb.Or(rdb.Row.Field("Done").Eq(true), rdb.Row.Field("Retries").Eq(0))).Delete().RunWrite(a.conn) if err != nil { return err } if res.Deleted > 0 { return nil } // Soft-delete all other values. res, err = q.Update(map[string]any{"DeletedAt": t.TimeNow()}).RunWrite(a.conn) if err == nil { if res.Deleted == 0 { err = t.ErrNotFound } } return err } // credGetActive reads the currently active unvalidated credential func (a *adapter) credGetActive(uid t.Uid, method string) (*t.Credential, error) { // Get the active unconfirmed credential: cursor, err := rdb.DB(a.dbName).Table("credentials").GetAllByIndex("User", uid.String()). Filter(rdb.Row.HasFields("DeletedAt").Not()). Filter(map[string]any{"Method": method, "Done": false}).Run(a.conn) if err != nil { return nil, err } defer cursor.Close() if cursor.IsNil() { return nil, nil } var cred t.Credential if err = cursor.One(&cred); err != nil { return nil, err } return &cred, nil } // CredConfirm marks given credential as validated. func (a *adapter) CredConfirm(uid t.Uid, method string) error { cred, err := a.credGetActive(uid, method) if err != nil { return err } // RethinkDb does not allow primary key to be changed (userid:method:value -> method:value) // We have to delete and re-insert with a different primary key. cred.Done = true cred.UpdatedAt = t.TimeNow() if _, err = a.CredUpsert(cred); err != nil { return err } rdb.DB(a.dbName). Table("credentials"). Get(uid.String() + ":" + cred.Method + ":" + cred.Value). Delete(rdb.DeleteOpts{Durability: "soft", ReturnChanges: false}). RunWrite(a.conn) return nil } // CredFail increments count of failed validation attepmts for the given credentials. func (a *adapter) CredFail(uid t.Uid, method string) error { _, err := rdb.DB(a.dbName).Table("credentials"). GetAllByIndex("User", uid.String()). Filter(map[string]any{"Method": method, "Done": false}). Filter(rdb.Row.HasFields("DeletedAt").Not()). Update(map[string]any{ "Retries": rdb.Row.Field("Retries").Default(0).Add(1), "UpdatedAt": t.TimeNow(), }).RunWrite(a.conn) return err } // CredGetActive returns currently active credential record for the given method. func (a *adapter) CredGetActive(uid t.Uid, method string) (*t.Credential, error) { return a.credGetActive(uid, method) } // CredGetAll returns user's credential records of the given method, validated only or all. func (a *adapter) CredGetAll(uid t.Uid, method string, validatedOnly bool) ([]t.Credential, error) { q := rdb.DB(a.dbName).Table("credentials").GetAllByIndex("User", uid.String()) if method != "" { q = q.Filter(map[string]any{"Method": method}) } if validatedOnly { q = q.Filter(map[string]any{"Done": true}) } else { q = q.Filter(rdb.Row.HasFields("DeletedAt").Not()) } cursor, err := q.Run(a.conn) if err != nil { return nil, err } defer cursor.Close() if cursor.IsNil() { return nil, nil } var credentials []t.Credential err = cursor.All(&credentials) return credentials, err } // FileUploads // FileStartUpload initializes a file upload func (a *adapter) FileStartUpload(fd *t.FileDef) error { _, err := rdb.DB(a.dbName).Table("fileuploads").Insert(fd).RunWrite(a.conn) return err } // FileFinishUpload marks file upload as completed, successfully or otherwise func (a *adapter) FileFinishUpload(fd *t.FileDef, success bool, size int64) (*t.FileDef, error) { now := t.TimeNow() if success { if _, err := rdb.DB(a.dbName).Table("fileuploads").Get(fd.Uid()). Update(map[string]any{ "UpdatedAt": now, "Status": t.UploadCompleted, "Size": size, "ETag": fd.ETag, "Location": fd.Location, }).RunWrite(a.conn); err != nil { return nil, err } fd.Status = t.UploadCompleted fd.Size = size } else { if _, err := rdb.DB(a.dbName).Table("fileuploads").Get(fd.Uid()).Delete().RunWrite(a.conn); err != nil { return nil, err } fd.Status = t.UploadFailed fd.Size = 0 } fd.UpdatedAt = now return fd, nil } // FileGet fetches a record of a specific file func (a *adapter) FileGet(fid string) (*t.FileDef, error) { cursor, err := rdb.DB(a.dbName).Table("fileuploads").Get(fid).Run(a.conn) if err != nil { return nil, err } defer cursor.Close() if cursor.IsNil() { return nil, nil } var fd t.FileDef if err = cursor.One(&fd); err != nil { return nil, err } return &fd, nil } // FileLinkAttachments connects given topic or message to the file record IDs from the list. func (a *adapter) FileLinkAttachments(topic string, userId, msgId t.Uid, fids []string) error { if len(fids) == 0 || (topic == "" && userId.IsZero() && msgId.IsZero()) { return t.ErrMalformed } now := t.TimeNow() var err error if msgId.IsZero() { // Only one link per user or topic is permitted. fids = fids[0:1] // Topics and users and mutable. Must unlink the previous attachments first. var table string var linkId string if topic != "" { table = "topics" linkId = topic } else { table = "users" linkId = userId.String() } // Find the old attachment. var cursor *rdb.Cursor cursor, err = rdb.DB(a.dbName).Table(table).Get(linkId). Field("Attachments").Default([]string{}).Run(a.conn) if err != nil { return err } defer cursor.Close() if !cursor.IsNil() { var attachments []string if err = cursor.One(&attachments); err != nil { if err != rdb.ErrEmptyResult { return err } err = nil } if len(attachments) > 0 { // Decrement the use count of old attachment. if _, err = rdb.DB(a.dbName).Table("fileuploads").Get(attachments[0]). Update(map[string]any{ "UpdatedAt": now, "UseCount": rdb.Row.Field("UseCount").Default(1).Sub(1), }).RunWrite(a.conn); err != nil { return err } } } _, err = rdb.DB(a.dbName).Table(table).Get(linkId). Update(map[string]any{ "UpdatedAt": now, "Attachments": fids, }).RunWrite(a.conn) if err != nil { return err } } else { // Messages are immutable. Just save the IDs. _, err := rdb.DB(a.dbName).Table("messages").Get(msgId.String()). Update(map[string]any{ "UpdatedAt": now, "Attachments": fids, }).RunWrite(a.conn) if err != nil { return err } } ids := make([]any, len(fids)) for i, id := range fids { ids[i] = id } _, err = rdb.DB(a.dbName).Table("fileuploads").GetAll(ids...). Update(map[string]any{ "UpdatedAt": now, "UseCount": rdb.Row.Field("UseCount").Default(0).Add(1), }).RunWrite(a.conn) return err } // FileDeleteUnused deletes orphaned file uploads. func (a *adapter) FileDeleteUnused(olderThan time.Time, limit int) ([]string, error) { q := rdb.DB(a.dbName).Table("fileuploads").GetAllByIndex("UseCount", 0) if !olderThan.IsZero() { q = q.Filter(rdb.Row.Field("UpdatedAt").Lt(olderThan)) } if limit > 0 { q = q.Limit(limit) } cursor, err := q.Field("Location").Run(a.conn) if err != nil { return nil, err } defer cursor.Close() var locations []string var loc string for cursor.Next(&loc) { locations = append(locations, loc) } if err = cursor.Err(); err != nil { return nil, err } _, err = q.Delete().RunWrite(a.conn) return locations, err } // Given a select query, decrement corresponding use counter in 'fileuploads' table. // The 'query' must return an array, i.e. GetAll, not Get. func (a *adapter) decFileUseCounter(query rdb.Term) error { /* r.db("test").table("one") .getAll( r.args(r.db("test").table("zero") .getAll( "07e2c6fe-ac91-49cb-9834-ff34bf50aad1", "0098a829-6da5-4f7b-8432-32b40de9ab3b", "0926e7dd-321a-49cb-adb1-7a705d9d9a78", "8e195450-babd-4954-a8fb-0cc414b43156") .filter(r.row.hasFields("att")) .concatMap(function(row) { return row.getField("att"); }) .coerceTo("array")) ) .update({useCount: r.row.getField("useCount").default(0).add(1)}) */ _, err := rdb.DB(a.dbName).Table("fileuploads").GetAll( rdb.Args( query. // Fetch messages with attachments only Filter(rdb.Row.HasFields("Attachments")). // Flatten arrays ConcatMap(func(row rdb.Term) any { return row.Field("Attachments") }). CoerceTo("array"))). // Decrement UseCount. Update(map[string]any{"UseCount": rdb.Row.Field("UseCount").Default(1).Sub(1)}). RunWrite(a.conn) return err } // PCacheGet reads a persistet cache entry. func (a *adapter) PCacheGet(key string) (string, error) { cursor, err := rdb.DB(a.dbName).Table("kvmeta").Get(key).Run(a.conn) if err != nil { return "", err } defer cursor.Close() if cursor.IsNil() { return "", t.ErrNotFound } var result map[string]string if err = cursor.One(&result); err != nil { return "", err } return result["value"], nil } // PCacheUpsert creates or updates a persistent cache entry. func (a *adapter) PCacheUpsert(key string, value string, failOnDuplicate bool) error { if strings.Contains(key, "^") { // Do not allow ^ in keys: it interferes with Match() query. return t.ErrMalformed } doc := map[string]any{ "key": key, "value": value, } var action string if failOnDuplicate { action = "error" doc["CreatedAt"] = t.TimeNow() } else { action = "update" } _, err := rdb.DB(a.dbName).Table("kvmeta").Insert(doc, rdb.InsertOpts{Conflict: action}).RunWrite(a.conn) if rdb.IsConflictErr(err) { return t.ErrDuplicate } return err } // PCacheDelete deletes one persistent cache entry. func (a *adapter) PCacheDelete(key string) error { _, err := rdb.DB(a.dbName).Table("kvmeta").Get(key).Delete().RunWrite(a.conn) return err } // PCacheExpire expires old entries with the given key prefix. func (a *adapter) PCacheExpire(keyPrefix string, olderThan time.Time) error { if keyPrefix == "" { return t.ErrMalformed } _, err := rdb.DB(a.dbName).Table("kvmeta"). Filter(rdb.Row.Field("CreatedAt").Lt(olderThan).And(rdb.Row.Field("key").Match("^" + keyPrefix))). Delete(). RunWrite(a.conn) return err } // GetTestDB returns a currently open database connection. func (a *adapter) GetTestDB() any { return a.conn } // Check if error is due to no results. // The case covered is calling Field('name') on a non-object value. func isNoResults(err error) bool { if err == nil { return false } return strings.Contains(err.Error(), "perform get_field on a non-object non-sequence") } // Checks if the given error is 'Database not found'. func isMissingDb(err error) bool { if err == nil { return false } msg := err.Error() // "Database `db_name` does not exist" return strings.Contains(msg, "Database `") && strings.Contains(msg, "` does not exist") } // GetTestAdapter returns an adapter object. Useful for running tests. func GetTestAdapter() *adapter { return &adapter{} } func init() { store.RegisterAdapter(&adapter{}) } ================================================ FILE: server/db/rethinkdb/blank.go ================================================ //go:build !rethinkdb // +build !rethinkdb // This file is needed for conditional compilation. It's used when // the build tag 'rethinkdb' is not defined. Otherwise the adapter.go // is compiled. package rethinkdb ================================================ FILE: server/db/rethinkdb/schema.md ================================================ # RethinkDB Database Schema ## Database `tinode` ### Table `users` Stores user accounts Fields: * `Id` user id, primary key * `CreatedAt` timestamp when the user was created * `UpdatedAt` timestamp when user metadata was updated * `State` account state: normal (ok), suspended, soft-deleted * `StateAt` timestamp when the state was last updated or NULL * `Access` user's default access level for peer-to-peer topics * `Auth`, `Anon` default permissions for authenticated and anonymous users * `Public` application-defined data * `State` state of the user: normal, disabled, deleted * `StateAt` timestamp when the state was last updated or NULL * `LastSeen` timestamp when the user was last online * `UserAgent` client User-Agent used when last online * `Tags` unique strings for user discovery * `Devices` client devices for push notifications * `DeviceId` device registration ID * `Platform` device platform string (iOS, Android, Web) * `LastSeen` last logged in * `Lang` device language, ISO code Indexes: * `Id` primary key * `Tags` multi-index (indexed array) * `DeletedAt` index * `DeviceIds` multi-index of push notification tokens Sample: ```js { "Access": { "Anon": 0 , "Auth": 47 } , "CreatedAt": Mon Jul 24 2017 11:16:38 GMT+00:00 , "State": 0, "StateAt": null , "Devices": null , "Id": "7yUCHniegrM" , "LastSeen": Mon Jan 01 1 00:00:00 GMT+00:00 , "Public": { "fn": "Alice Johnson" , "photo": { "data": , "type": "jpg" } } , "State": 1 , "Tags": [ "email:alice@example.com" , "tel:17025550001" ] , "UpdatedAt": Mon Jul 24 2017 11:16:38 GMT+00:00 , "UserAgent": "TinodeWeb/0.13 (MacIntel) tinodejs/0.13" } ``` ### Table `auth` Stores authentication secrets Fields: * `userid` ID of the user who owns the record * `unique` unique string which identifies this record, primary key; defined as "_authentication scheme_':'_some unique value per scheme_" * `secret` shared secret, for instance bcrypt of password * `authLvl` authentication level * `expires` timestamp when the records expires Indexes: * `unique` primary key * `userid` index Sample: ```js { "authLvl": 20 , "expires": Mon Jan 01 1 00:00:00 GMT+00:00 , "secret": , "unique": "basic:alice" , "userid": "7yUCHniegrM" } ``` ### Table `topics` The table stores topics. Fields: * `Id` name of the topic, primary key * `CreatedAt` topic creation time * `UpdatedAt` timestamp of the last change to topic metadata * `State` topic state: normal (ok), suspended, soft-deleted * `StateAt` timestamp when the state was last updated or NULL * `Access` stores topic's default access permissions * `Auth`, `Anon` permissions for authenticated and anonymous users respectively * `Owner` ID of the user who owns the topic * `Public` application-defined data * `State` state of the topic: normal, disabled, deleted * `SeqId` sequential ID of the last message * `DelId` topic-sequential ID of the deletion operation * `UseBt` indicator that channel functionality is enabled in the topic Indexes: * `Id` primary key * `Owner` index Sample: ```js { "Access": { "Anon": 64 , "Auth": 64 } , "DelId": 0, "CreatedAt": Thu Oct 15 2015 04:06:51 GMT+00:00 , "State": 0 , "StateAt": null , "LastMessageAt": Sat Oct 17 2015 13:51:56 GMT+00:00 , "Id": "p2pavVGHLCBbKrvJQIeeJ6Csw" , "Owner": "v2JyG4OLSoA" , "Public": { "fn": "Travel, travel, travel" , "photo": { "data": , "type": "jpg" } } , "SeqId": 14, "State": 0 , "UpdatedAt": Thu Oct 15 2015 04:06:51 GMT+00:00 , "UseBt": false } ``` ### Table `subscriptions` The table stores relationships between users and topics. Fields: * `Id` used for object retrieval * `CreatedAt` timestamp when the subscription was created * `UpdatedAt` timestamp when the subscription was updated * `DeletedAt` timestamp when the subscription was deleted * `ReadSeqId` id of the message last read by the user * `RecvSeqId` id of the message last received by any user device * `DelId` topic-sequential ID of the soft-deletion operation * `Topic` name of the topic subscribed to * `User` subscriber's user ID * `ModeWant` access mode that user wants when accessing the topic * `ModeGiven` access mode granted to user by the topic * `Private` application-defined data, accessible by the user only Indexes: * `Id` primary key composed as "_topic name_':'_user ID_" * `User` index * `Topic` index Sample: ```js { "ClearId": 0 , "CreatedAt": Tue Jul 25 2017 15:34:39 GMT+00:00 , "DeletedAt": null , "Id": "grpjajVKrHn0PU:v2JyG4OLSoA" , "ModeGiven": 47 , "ModeWant": 47 , "Private": "Kirgudu" , "ReadSeqId": 0 , "RecvSeqId": 0 , "State": 0 , "Topic": "grpjajVKrHn0PU" , "UpdatedAt": Tue Jul 25 2017 15:34:39 GMT+00:00 , "User": "v2JyG4OLSoA" } ``` ### Table `messages` The table stores `{data}` messages Fields: * `Id` currently unused, primary key * `CreatedAt` timestamp when the message was created * `UpdatedAt` initially equal to CreatedAt, for deleted messages equal to DeletedAt * `DeletedFor` array of user IDs which soft-deleted the message * `DelId` topic-sequential ID of the soft-deletion operation * `User` ID of the user who soft-deleted the message * `From` ID of the user who generated this message * `Topic` which received this message * `SeqId` messages ID - sequential number of the message in the topic * `Head` message headers * `Attachments` denormalized IDs of files attached to the message * `Content` application-defined message payload Indexes: * `Id` primary key * `Topic_SeqId` compound index `["Topic", "SeqId"]` * `Topic_DelId` compound index `["Topic", "DelId"]` * `Topic_DeletedFor` compound multi-index `["Topic", "DeletedFor"("User"), "DeletedFor"("DelId")]` Sample: ```js { "Content": { "fmt": [ { "len": 6 , "tp": "ST" } ] , "txt": "Hello!" } , "CreatedAt": Sun Dec 24 2017 05:16:23 GMT+00:00 , "From": "wTI0jO9rEqY" , "Head": { "mime": "text/x-drafty" } , "DeletedFor": [ { "DelId": 1 , "User": "wTI0jO9rEqY" } ] , "Id": "LLXKEe9W4Bs" , "SeqId": 3 , "Topic": "p2pJhbJnya8z5PBMjSM72sSpg" , "UpdatedAt": Sun Dec 24 2017 05:16:23 GMT+00:00 } ``` ### Table `dellog` The table stores records of message deletions Fields: * `Id` currently unused, primary key * `CreatedAt` timestamp when the record was created * `UpdatedAt` timestamp equal to CreatedAt * `DelId` topic-sequential ID of the deletion operation. * `DeletedFor` ID of the user for soft-deletions, blank string for hard-deletions * `Topic` affected topic * `SeqIdRanges` array of ranges of deleted message IDs (see `messages.SeqId`) Indexes: * `Id` primary key * `Topic_DelId` compound index `["Topic", "DelId"]` Sample: ```js { "Id": "9LfrjW349Rc", "CreatedAt": Tue Dec 05 2017 01:51:38 GMT+00:00, "DelId": 18, "DeletedFor": "xY-YHx09-WI" , "SeqIdRanges": [ { "Low": 20, "Hi": 25, } ] , "Topic": "grpGx7fpjQwVC0" , "UpdatedAt": Tue Dec 05 2017 01:51:38 GMT+00:00 } ``` ### Table `credentials` The tables stores user credentials used for validation. * `Id` credential, primary key * `CreatedAt` timestamp when the record was created * `UpdatedAt` timestamp when the last validation attempt was performed (successful or not). * `Method` validation method * `Done` indicator if the credential is validated * `Resp` expected validation response * `Retries` number of failed attempts at validation * `User` id of the user who owns this credential * `Value` value of the credential * `Closed` unvalidated credential is no longer being validated. Only one credential is not Closed for each user/method. Indexes: * `Id` Primary key composed either as `User`:`Method`:`Value` for unconfirmed credentials or as `Method`:`Value` for confirmed. * `User` Index Sample: ```js { "Id": "tel:+17025550001", "CreatedAt": Sun Jun 10 2018 16:37:27 GMT+00:00 , "UpdatedAt": Sun Jun 10 2018 16:37:28 GMT+00:00 , "Method": "tel" , "Done": true , "Resp": "123456" , "Retries": 0 , "User": "k3srBRk9RYw" , "Value": "+17025550001" } ``` ### Table `fileuploads` The table stores records of uploaded files. The files themselves are stored outside of the database. * `Id` unique user-visible file name, primary key * `CreatedAt` timestamp when the record was created * `UpdatedAt` timestamp of when th upload has cmpleted or failed * `User` id of the user who uploaded this file. * `Location` actual location of the file on the server. * `MimeType` file content type as a [Mime](https://en.wikipedia.org/wiki/MIME) string. * `Size` size of the file in bytes. Could be 0 if upload has not completed yet. * `UseCount` count of messages referencing this file. * `Status` upload status: 0 pending, 1 completed, -1 failed. Indexes: * `Id` primary key * `UseCount` index Sample: ```js { "CreatedAt": Sun Jun 10 2018 16:38:45 GMT+00:00 , "Id": "sFmjlQ_kA6A" , "Location": "uploads/sFmjlQ_kA6A" , "MimeType": "image/jpeg" , "Size": 54961090 , "UseCount": 3, "Status": 1, "UpdatedAt": Sun Jun 10 2018 16:38:45 GMT+00:00 , "User": "7j-RR1V7O3Y" } ``` ================================================ FILE: server/db/rethinkdb/tests/rethink_test.go ================================================ package tests // To test another db backend: // 1) Create GetAdapter function inside your db backend adapter package (like one inside rethinkdb adapter) // 2) Uncomment your db backend package ('backend' named package) // 3) Write own initConnectionToDb and 'db' variable // 4) Replace rethinkdb specific db queries inside test to your own queries. // 5) Run. import ( "encoding/json" "flag" "fmt" "log" "os" "reflect" "testing" "time" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" adapter "github.com/tinode/chat/server/db" jcr "github.com/tinode/jsonco" rdb "gopkg.in/rethinkdb/rethinkdb-go.v6" "github.com/tinode/chat/server/db/common/test_data" backend "github.com/tinode/chat/server/db/rethinkdb" "github.com/tinode/chat/server/logs" "github.com/tinode/chat/server/store" "github.com/tinode/chat/server/store/types" ) type configType struct { // If Reset=true test will recreate database every time it runs Reset bool `json:"reset_db_data"` // Configurations for individual adapters. Adapters map[string]json.RawMessage `json:"adapters"` } var config configType var adp adapter.Adapter var conn *rdb.Session var testData *test_data.TestData var dummyUid1 = types.Uid(12345) var dummyUid2 = types.Uid(54321) func TestCreateDb(t *testing.T) { if err := adp.CreateDb(config.Reset); err != nil { t.Fatal(err) } // Saved db is closed, get a fresh one. conn = adp.GetTestDB().(*rdb.Session) } // ================== Create tests ================================ func TestUserCreate(t *testing.T) { for _, user := range testData.Users { if err := adp.UserCreate(user); err != nil { t.Error(err) } } cursor, err := rdb.Table("users").Count().Run(conn) if err != nil { t.Error(err) } defer cursor.Close() var count int if err = cursor.One(&count); err != nil { t.Error(err) } if count == 0 { t.Error("No users created!") } } func TestCredUpsert(t *testing.T) { // Test just inserts: for i := 0; i < 2; i++ { inserted, err := adp.CredUpsert(testData.Creds[i]) if err != nil { t.Fatal(err) } if !inserted { t.Error("Should be inserted, but updated") } } // Test duplicate: _, err := adp.CredUpsert(testData.Creds[1]) if err != types.ErrDuplicate { t.Error("Should return duplicate error but got", err) } _, err = adp.CredUpsert(testData.Creds[2]) if err != types.ErrDuplicate { t.Error("Should return duplicate error but got", err) } // Test add new unvalidated credentials inserted, err := adp.CredUpsert(testData.Creds[3]) if err != nil { t.Fatal(err) } if !inserted { t.Error("Should be inserted, but updated") } inserted, err = adp.CredUpsert(testData.Creds[3]) if err != nil { t.Fatal(err) } if inserted { t.Error("Should be updated, but inserted") } // Just insert other creds (used in other tests) for _, cred := range testData.Creds[4:] { _, err = adp.CredUpsert(cred) if err != nil { t.Fatal(err) } } } func TestAuthAddRecord(t *testing.T) { for _, rec := range testData.Recs { err := adp.AuthAddRecord(types.ParseUserId("usr"+rec.UserId), rec.Scheme, rec.Unique, rec.AuthLvl, rec.Secret, rec.Expires) if err != nil { t.Fatal(err) } } //Test duplicate err := adp.AuthAddRecord(types.ParseUserId("usr"+testData.Users[0].Id), testData.Recs[0].Scheme, testData.Recs[0].Unique, testData.Recs[0].AuthLvl, testData.Recs[0].Secret, testData.Recs[0].Expires) if err != types.ErrDuplicate { t.Fatal("Should be duplicate error but got", err) } } func TestTopicCreate(t *testing.T) { err := adp.TopicCreate(testData.Topics[0]) if err != nil { t.Error(err) } // Update topic SeqId because it's not saved at creation time but used by the tests. err = adp.TopicUpdate(testData.Topics[0].Id, map[string]interface{}{ "SeqId": testData.Topics[0].SeqId, }) if err != nil { t.Error(err) } for _, tpc := range testData.Topics[3:] { err = adp.TopicCreate(tpc) if err != nil { t.Error(err) } } } func TestTopicCreateP2P(t *testing.T) { err := adp.TopicCreateP2P(testData.Subs[2], testData.Subs[3]) if err != nil { t.Fatal(err) } oldModeGiven := testData.Subs[2].ModeGiven testData.Subs[2].ModeGiven = 255 err = adp.TopicCreateP2P(testData.Subs[4], testData.Subs[2]) if err != nil { t.Fatal(err) } cursor, err := rdb.Table("subscriptions").Get(testData.Subs[2].Id).Run(conn) if err != nil { t.Fatal(err) } defer cursor.Close() var got types.Subscription if err = cursor.One(&got); err != nil { t.Fatal(err) } if got.ModeGiven == oldModeGiven { t.Error("ModeGiven update failed") } } func TestTopicShare(t *testing.T) { if err := adp.TopicShare(testData.Subs[0].Topic, testData.Subs); err != nil { t.Fatal(err) } // Must save recvseqid and readseqid separately because TopicShare // ignores them. for _, sub := range testData.Subs { adp.SubsUpdate(sub.Topic, types.ParseUid(sub.User), map[string]any{ "RecvSeqId": sub.RecvSeqId, "ReadSeqId": sub.ReadSeqId, }) } } func TestMessageSave(t *testing.T) { for _, msg := range testData.Msgs { err := adp.MessageSave(msg) if err != nil { t.Fatal(err) } } // Some messages are soft deleted, but it's ignored by adp.MessageSave for _, msg := range testData.Msgs { if len(msg.DeletedFor) > 0 { for _, del := range msg.DeletedFor { toDel := types.DelMessage{ Topic: msg.Topic, DeletedFor: del.User, DelId: del.DelId, SeqIdRanges: []types.Range{{Low: msg.SeqId}}, } adp.MessageDeleteList(msg.Topic, &toDel) } } } } func TestFileStartUpload(t *testing.T) { for _, f := range testData.Files { err := adp.FileStartUpload(f) if err != nil { t.Fatal(err) } } } // ================== Read tests ================================== func TestUserGet(t *testing.T) { // Test not found got, err := adp.UserGet(dummyUid1) if err == nil && got != nil { t.Error("user should be nil.") } got, err = adp.UserGet(types.ParseUserId("usr" + testData.Users[0].Id)) if err != nil { t.Fatal(err) } // User agent is not stored when creating a user. Make sure it's the same. got.UserAgent = testData.Users[0].UserAgent if !cmp.Equal(got, testData.Users[0], cmpopts.IgnoreUnexported(types.User{}, types.ObjHeader{})) { t.Error(mismatchErrorString("User", got, testData.Users[0])) } } func TestUserGetAll(t *testing.T) { // Test not found (dummy UIDs). got, err := adp.UserGetAll(dummyUid1, dummyUid2) if err != nil { t.Fatal(err) } if len(got) > 0 { t.Error("result users should be zero length, got", len(got)) } got, err = adp.UserGetAll(types.ParseUserId("usr"+testData.Users[0].Id), types.ParseUserId("usr"+testData.Users[1].Id)) if err != nil { t.Fatal(err) } if len(got) != 2 { t.Fatal(mismatchErrorString("resultUsers length", len(got), 2)) } for i, usr := range got { // User agent is not compared. usr.UserAgent = testData.Users[i].UserAgent if !reflect.DeepEqual(&usr, testData.Users[i]) { t.Error(mismatchErrorString("User", &usr, testData.Users[i])) } } } func TestUserGetByCred(t *testing.T) { // Test not found got, err := adp.UserGetByCred("foo", "bar") if err != nil { t.Fatal(err) } if got != types.ZeroUid { t.Error("result uid should be ZeroUid") } got, _ = adp.UserGetByCred(testData.Creds[0].Method, testData.Creds[0].Value) if got != types.ParseUserId("usr"+testData.Creds[0].User) { t.Error(mismatchErrorString("Uid", got, types.ParseUserId("usr"+testData.Creds[0].User))) } } func TestCredGetActive(t *testing.T) { got, err := adp.CredGetActive(types.ParseUserId("usr"+testData.Users[2].Id), "tel") if err != nil { t.Error(err) } if !cmp.Equal(got, testData.Creds[3], cmpopts.IgnoreUnexported(types.ObjHeader{}, types.Credential{})) { t.Error(mismatchErrorString("Credential", got, testData.Creds[3])) } // Test not found got, err = adp.CredGetActive(dummyUid1, "") if err != nil { t.Error(err) } if got != nil { t.Error("result should be nil, but got", got) } } func TestCredGetAll(t *testing.T) { got, err := adp.CredGetAll(types.ParseUserId("usr"+testData.Users[2].Id), "", false) if err != nil { t.Fatal(err) } if len(got) != 3 { t.Error(mismatchErrorString("Credentials length", len(got), 3)) } got, _ = adp.CredGetAll(types.ParseUserId("usr"+testData.Users[2].Id), "tel", false) if len(got) != 2 { t.Error(mismatchErrorString("Credentials length", len(got), 2)) } got, _ = adp.CredGetAll(types.ParseUserId("usr"+testData.Users[2].Id), "", true) if len(got) != 1 { t.Error(mismatchErrorString("Credentials length", len(got), 1)) } got, _ = adp.CredGetAll(types.ParseUserId("usr"+testData.Users[2].Id), "tel", true) if len(got) != 1 { t.Error(mismatchErrorString("Credentials length", len(got), 1)) } } func TestUserGetUnvalidated(t *testing.T) { // Test RethinkDB specific method cutoff := time.Now().Add(-24 * time.Hour) uids, err := adp.UserGetUnvalidated(cutoff, 10) if err != nil { t.Error(err) } // Should return empty slice since all test users are considered validated if len(uids) > 0 { t.Error("Expected no unvalidated users in test data") } } func TestAuthGetUniqueRecord(t *testing.T) { uid, authLvl, secret, expires, err := adp.AuthGetUniqueRecord("basic:alice") if err != nil { t.Fatal(err) } if uid != types.ParseUserId("usr"+testData.Recs[0].UserId) || authLvl != testData.Recs[0].AuthLvl || !reflect.DeepEqual(secret, testData.Recs[0].Secret) || expires != testData.Recs[0].Expires { got := fmt.Sprintf("%v %v %v %v", uid, authLvl, secret, expires) want := fmt.Sprintf("%v %v %v %v", testData.Recs[0].UserId, testData.Recs[0].AuthLvl, testData.Recs[0].Secret, testData.Recs[0].Expires) t.Error(mismatchErrorString("Auth record", got, want)) } // Test not found uid, _, _, _, err = adp.AuthGetUniqueRecord("qwert:asdfg") if err == nil && !uid.IsZero() { t.Error("Auth record found but shouldn't. Uid:", uid.String()) } } func TestAuthGetRecord(t *testing.T) { recId, authLvl, secret, expires, err := adp.AuthGetRecord(types.ParseUserId("usr"+testData.Recs[0].UserId), "basic") if err != nil { t.Fatal(err) } if recId != testData.Recs[0].Unique || authLvl != testData.Recs[0].AuthLvl || !reflect.DeepEqual(secret, testData.Recs[0].Secret) || expires != testData.Recs[0].Expires { got := fmt.Sprintf("%v %v %v %v", recId, authLvl, secret, expires) want := fmt.Sprintf("%v %v %v %v", testData.Recs[0].Unique, testData.Recs[0].AuthLvl, testData.Recs[0].Secret, testData.Recs[0].Expires) t.Error(mismatchErrorString("Auth record", got, want)) } // Test not found recId, _, _, _, err = adp.AuthGetRecord(types.Uid(123), "scheme") if err != types.ErrNotFound { t.Error("Auth record found but shouldn't. recId:", recId) } } func TestTopicGet(t *testing.T) { got, err := adp.TopicGet(testData.Topics[0].Id) if err != nil { t.Fatal(err) } if !reflect.DeepEqual(got, testData.Topics[0]) { t.Error(mismatchErrorString("Topic", got, testData.Topics[0])) } // Test not found got, err = adp.TopicGet("asdfasdfasdf") if err != nil { t.Fatal(err) } if got != nil { t.Error("Topic should be nil but got:", got) } } func TestTopicsForUser(t *testing.T) { qOpts := types.QueryOpt{ Topic: testData.Topics[1].Id, Limit: 999, } gotSubs, err := adp.TopicsForUser(types.ParseUserId("usr"+testData.Users[0].Id), false, &qOpts) if err != nil { t.Fatal(err) } if len(gotSubs) != 1 { t.Error(mismatchErrorString("Subs length", len(gotSubs), 1)) } gotSubs, err = adp.TopicsForUser(types.ParseUserId("usr"+testData.Users[1].Id), true, nil) if err != nil { t.Fatal(err) } if len(gotSubs) != 2 { t.Error(mismatchErrorString("Subs length (2)", len(gotSubs), 2)) } qOpts.Topic = "" ims := testData.Now.Add(15 * time.Minute) qOpts.IfModifiedSince = &ims gotSubs, err = adp.TopicsForUser(types.ParseUserId("usr"+testData.Users[0].Id), false, &qOpts) if err != nil { t.Fatal(err) } if len(gotSubs) != 1 { t.Error(mismatchErrorString("Subs length (IMS)", len(gotSubs), 1)) } ims = time.Now().Add(15 * time.Minute) gotSubs, err = adp.TopicsForUser(types.ParseUserId("usr"+testData.Users[0].Id), false, &qOpts) if err != nil { t.Fatal(err) } if len(gotSubs) != 0 { t.Error(mismatchErrorString("Subs length (IMS 2)", len(gotSubs), 0)) } } func TestUsersForTopic(t *testing.T) { qOpts := types.QueryOpt{ User: types.ParseUserId("usr" + testData.Users[0].Id), Limit: 999, } gotSubs, err := adp.UsersForTopic(testData.Topics[0].Id, false, &qOpts) if err != nil { t.Fatal(err) } if len(gotSubs) != 1 { t.Error(mismatchErrorString("Subs length", len(gotSubs), 1)) } gotSubs, err = adp.UsersForTopic(testData.Topics[0].Id, true, nil) if err != nil { t.Fatal(err) } if len(gotSubs) != 2 { t.Error(mismatchErrorString("Subs length", len(gotSubs), 2)) } gotSubs, err = adp.UsersForTopic(testData.Topics[1].Id, false, nil) if err != nil { t.Fatal(err) } if len(gotSubs) != 2 { t.Error(mismatchErrorString("Subs length", len(gotSubs), 2)) } } func TestOwnTopics(t *testing.T) { gotSubs, err := adp.OwnTopics(types.ParseUserId("usr" + testData.Users[0].Id)) if err != nil { t.Fatal(err) } if len(gotSubs) != 1 { t.Fatalf("Got topic length %v instead of %v", len(gotSubs), 1) } if gotSubs[0] != testData.Topics[0].Id { t.Errorf("Got topic %v instead of %v", gotSubs[0], testData.Topics[0].Id) } } func TestChannelsForUser(t *testing.T) { // Test RethinkDB specific method channels, err := adp.ChannelsForUser(types.ParseUserId("usr" + testData.Users[0].Id)) if err != nil { t.Fatal(err) } // Should return empty slice since we don't have channel subscriptions in test data if len(channels) != 0 { t.Error(mismatchErrorString("Channels length", len(channels), 0)) } } func TestSubscriptionGet(t *testing.T) { got, err := adp.SubscriptionGet(testData.Topics[0].Id, types.ParseUserId("usr"+testData.Users[0].Id), false) if err != nil { t.Error(err) } opts := cmpopts.IgnoreUnexported(types.Subscription{}, types.ObjHeader{}) if !cmp.Equal(got, testData.Subs[0], opts) { t.Error(mismatchErrorString("Subs", got, testData.Subs[0])) } // Test not found got, err = adp.SubscriptionGet("dummytopic", dummyUid1, false) if err != nil { t.Error(err) } if got != nil { t.Error("result sub should be nil.") } } func TestSubsForUser(t *testing.T) { gotSubs, err := adp.SubsForUser(types.ParseUserId("usr" + testData.Users[0].Id)) if err != nil { t.Error(err) } if len(gotSubs) != 2 { t.Error(mismatchErrorString("Subs length", len(gotSubs), 2)) } // Test not found gotSubs, err = adp.SubsForUser(types.ParseUserId("usr12345678")) if err != nil { t.Error(err) } if len(gotSubs) != 0 { t.Error(mismatchErrorString("Subs length", len(gotSubs), 0)) } } func TestSubsForTopic(t *testing.T) { qOpts := types.QueryOpt{ User: types.ParseUserId("usr" + testData.Users[0].Id), Limit: 999, } gotSubs, err := adp.SubsForTopic(testData.Topics[0].Id, false, &qOpts) if err != nil { t.Error(err) } if len(gotSubs) != 1 { t.Error(mismatchErrorString("Subs length", len(gotSubs), 1)) } // Test not found gotSubs, err = adp.SubsForTopic("dummytopicid", false, nil) if err != nil { t.Error(err) } if len(gotSubs) != 0 { t.Error(mismatchErrorString("Subs length", len(gotSubs), 0)) } } func TestFind(t *testing.T) { reqTags := [][]string{{"alice", "bob", "carol", "travel", "qwer", "asdf", "zxcv"}} got, err := adp.Find("usr"+testData.Users[2].Id, "", reqTags, nil, true) if err != nil { t.Error(err) } if len(got) != 3 { t.Error(mismatchErrorString("result length", len(got), 3)) } } func TestFindOne(t *testing.T) { // Test RethinkDB specific FindOne method found, err := adp.FindOne("alice") if err != nil { t.Error(err) } // Should find the user with alice tag if found == "" { t.Error("Expected to find user with alice tag") } // Test not found found, err = adp.FindOne("nonexistent") if err != nil { t.Error(err) } if found != "" { t.Error("Should not find nonexistent tag") } } func TestMessageGetAll(t *testing.T) { opts := types.QueryOpt{ Since: 1, Before: 2, Limit: 999, } gotMsgs, err := adp.MessageGetAll(testData.Topics[0].Id, types.ParseUserId("usr"+testData.Users[0].Id), &opts) if err != nil { t.Fatal(err) } if len(gotMsgs) != 1 { t.Error(mismatchErrorString("Messages length opts", len(gotMsgs), 1)) } gotMsgs, _ = adp.MessageGetAll(testData.Topics[0].Id, types.ParseUserId("usr"+testData.Users[0].Id), nil) if len(gotMsgs) != 2 { t.Error(mismatchErrorString("Messages length no opts", len(gotMsgs), 2)) } gotMsgs, _ = adp.MessageGetAll(testData.Topics[0].Id, types.ZeroUid, nil) if len(gotMsgs) != 3 { t.Error(mismatchErrorString("Messages length zero uid", len(gotMsgs), 3)) } } func TestFileGet(t *testing.T) { // General test done during TestFileFinishUpload(). // Test not found got, err := adp.FileGet("dummyfileid") if err != nil { if got != nil { t.Error("File found but shouldn't:", got) } } } // ================== Update tests ================================ func TestUserUpdate(t *testing.T) { update := map[string]any{ "UserAgent": "Test Agent v0.11", "UpdatedAt": testData.Now.Add(30 * time.Minute), } err := adp.UserUpdate(types.ParseUserId("usr"+testData.Users[0].Id), update) if err != nil { t.Fatal(err) } cursor, err := rdb.Table("users").Get(testData.Users[0].Id).Pluck("UserAgent", "UpdatedAt", "CreatedAt").Run(conn) if err != nil { t.Fatal(err) } defer cursor.Close() var got struct { UserAgent string UpdatedAt time.Time CreatedAt time.Time } if err = cursor.One(&got); err != nil { t.Fatal(err) } if got.UserAgent != "Test Agent v0.11" { t.Error(mismatchErrorString("UserAgent", got.UserAgent, "Test Agent v0.11")) } if got.UpdatedAt.Equal(got.CreatedAt) { t.Error("UpdatedAt field not updated") } } func TestUserUpdateTags(t *testing.T) { addTags := testData.Tags[0] removeTags := testData.Tags[1] resetTags := testData.Tags[2] uid := types.ParseUserId("usr" + testData.Users[0].Id) got, err := adp.UserUpdateTags(uid, addTags, nil, nil) if err != nil { t.Fatal(err) } want := []string{"alice", "tag1"} if !reflect.DeepEqual(got, want) { t.Error(mismatchErrorString("Tags", got, want)) } got, _ = adp.UserUpdateTags(uid, nil, removeTags, nil) want = nil if !reflect.DeepEqual(got, want) { t.Error(mismatchErrorString("Tags", got, want)) } got, _ = adp.UserUpdateTags(uid, nil, nil, resetTags) want = []string{"alice", "tag111", "tag333"} if !reflect.DeepEqual(got, want) { t.Error(mismatchErrorString("Tags", got, want)) } got, _ = adp.UserUpdateTags(uid, addTags, removeTags, nil) want = []string{"tag111", "tag333"} if !reflect.DeepEqual(got, want) { t.Error(mismatchErrorString("Tags", got, want)) } } func TestCredFail(t *testing.T) { err := adp.CredFail(types.ParseUserId("usr"+testData.Creds[3].User), "tel") if err != nil { t.Error(err) } // Check if fields updated cursor, err := rdb.Table("credentials").GetAllByIndex("User", testData.Creds[3].User). Filter(map[string]any{"Method": "tel", "Value": testData.Creds[3].Value}). Pluck("Retries", "UpdatedAt", "CreatedAt").Run(conn) if err != nil { t.Fatal(err) } defer cursor.Close() var got struct { Retries int UpdatedAt time.Time CreatedAt time.Time } if err = cursor.One(&got); err != nil { t.Fatal(err) } if got.Retries != 1 { t.Error(mismatchErrorString("Retries count", got.Retries, 1)) } if got.UpdatedAt.Equal(got.CreatedAt) { t.Error("UpdatedAt field not updated") } } func TestCredConfirm(t *testing.T) { err := adp.CredConfirm(types.ParseUserId("usr"+testData.Creds[3].User), "tel") if err != nil { t.Fatal(err) } // Test fields are updated - the confirmed credential should have a new ID (method:value) cursor, err := rdb.Table("credentials").Get(testData.Creds[3].Method+":"+testData.Creds[3].Value). Pluck("UpdatedAt", "CreatedAt", "Done").Run(conn) if err != nil { t.Fatal(err) } defer cursor.Close() var got struct { UpdatedAt time.Time CreatedAt time.Time Done bool } if err = cursor.One(&got); err != nil { t.Fatal(err) } if got.UpdatedAt.Equal(got.CreatedAt) { t.Error("Credential not updated correctly") } if !got.Done { t.Error("Credential should be marked as done") } // And unconfirmed credential should be deleted cursor2, err := rdb.Table("credentials").Get(testData.Creds[3].User + ":" + testData.Creds[3].Method + ":" + testData.Creds[3].Value).Run(conn) if err != nil { t.Fatal(err) } defer cursor2.Close() if !cursor2.IsNil() { t.Error("Unconfirmed credential should be deleted") } } func TestAuthUpdRecord(t *testing.T) { rec := testData.Recs[1] newSecret := []byte{'s', 'e', 'c', 'r', 'e', 't'} err := adp.AuthUpdRecord(types.ParseUserId("usr"+rec.UserId), rec.Scheme, rec.Unique, rec.AuthLvl, newSecret, rec.Expires) if err != nil { t.Fatal(err) } cursor, err := rdb.Table("auth").Get(rec.Unique).Field("secret").Run(conn) if err != nil { t.Fatal(err) } defer cursor.Close() var got []byte if err = cursor.One(&got); err != nil { t.Fatal(err) } if reflect.DeepEqual(got, rec.Secret) { t.Error(mismatchErrorString("secret", got, rec.Secret)) } // Test with auth ID (unique) change newId := "basic:bob12345" err = adp.AuthUpdRecord(types.ParseUserId("usr"+rec.UserId), rec.Scheme, newId, rec.AuthLvl, newSecret, rec.Expires) if err != nil { t.Fatal(err) } // Test if old ID deleted cursor2, err := rdb.Table("auth").Get(rec.Unique).Run(conn) if err != nil { t.Fatal(err) } defer cursor2.Close() if !cursor2.IsNil() { t.Error("Old auth record should be deleted") } } func TestTopicUpdateOnMessage(t *testing.T) { msg := types.Message{ ObjHeader: types.ObjHeader{ CreatedAt: testData.Now.Add(33 * time.Minute), }, SeqId: 66, } err := adp.TopicUpdateOnMessage(testData.Topics[2].Id, &msg) if err != nil { t.Fatal(err) } cursor, err := rdb.Table("topics").Get(testData.Topics[2].Id). Pluck("TouchedAt", "SeqId").Run(conn) if err != nil { t.Fatal(err) } defer cursor.Close() var got struct { TouchedAt time.Time SeqId int } if err = cursor.One(&got); err != nil { t.Fatal(err) } if !got.TouchedAt.Equal(msg.CreatedAt) || got.SeqId != msg.SeqId { t.Error(mismatchErrorString("TouchedAt", got.TouchedAt, msg.CreatedAt)) t.Error(mismatchErrorString("SeqId", got.SeqId, msg.SeqId)) } } func TestTopicUpdate(t *testing.T) { update := map[string]any{ "UpdatedAt": testData.Now.Add(55 * time.Minute), } err := adp.TopicUpdate(testData.Topics[0].Id, update) if err != nil { t.Fatal(err) } cursor, err := rdb.Table("topics").Get(testData.Topics[0].Id).Field("UpdatedAt").Run(conn) if err != nil { t.Fatal(err) } defer cursor.Close() var got time.Time if err = cursor.One(&got); err != nil { t.Fatal(err) } if !got.Equal(update["UpdatedAt"].(time.Time)) { t.Error(mismatchErrorString("UpdatedAt", got, update["UpdatedAt"])) } } func TestTopicUpdateSubCnt(t *testing.T) { // Test RethinkDB specific method err := adp.TopicUpdateSubCnt(testData.Topics[0].Id) if err != nil { t.Fatal(err) } // Verify the subscription count was updated correctly cursor, err := rdb.Table("topics").Get(testData.Topics[0].Id).Field("SubCnt").Run(conn) if err != nil { t.Fatal(err) } defer cursor.Close() var subcnt int if err = cursor.One(&subcnt); err != nil { t.Fatal(err) } // Should match the number of active subscriptions if subcnt < 0 { t.Error("Subscription count should be non-negative") } } func TestTopicOwnerChange(t *testing.T) { err := adp.TopicOwnerChange(testData.Topics[0].Id, types.ParseUserId("usr"+testData.Users[1].Id)) if err != nil { t.Fatal(err) } cursor, err := rdb.Table("topics").Get(testData.Topics[0].Id).Field("Owner").Run(conn) if err != nil { t.Fatal(err) } defer cursor.Close() var got string if err = cursor.One(&got); err != nil { t.Fatal(err) } if got != testData.Users[1].Id { t.Error(mismatchErrorString("Owner", got, testData.Users[1].Id)) } } func TestSubsUpdate(t *testing.T) { update := map[string]any{ "UpdatedAt": testData.Now.Add(22 * time.Minute), } err := adp.SubsUpdate(testData.Topics[0].Id, types.ParseUserId("usr"+testData.Users[0].Id), update) if err != nil { t.Fatal(err) } cursor, err := rdb.Table("subscriptions").Get(testData.Topics[0].Id + ":" + testData.Users[0].Id). Field("UpdatedAt").Run(conn) if err != nil { t.Fatal(err) } defer cursor.Close() var got time.Time if err = cursor.One(&got); err != nil { t.Fatal(err) } if !got.Equal(update["UpdatedAt"].(time.Time)) { t.Error(mismatchErrorString("UpdatedAt", got, update["UpdatedAt"])) } err = adp.SubsUpdate(testData.Topics[1].Id, types.ZeroUid, update) if err != nil { t.Fatal(err) } cursor2, err := rdb.Table("subscriptions").GetAllByIndex("Topic", testData.Topics[1].Id). Field("UpdatedAt").Limit(1).Run(conn) if err != nil { t.Fatal(err) } defer cursor2.Close() if err = cursor2.One(&got); err != nil { t.Fatal(err) } if !got.Equal(update["UpdatedAt"].(time.Time)) { t.Error(mismatchErrorString("UpdatedAt", got, update["UpdatedAt"])) } } func TestSubsDelete(t *testing.T) { err := adp.SubsDelete(testData.Topics[1].Id, types.ParseUserId("usr"+testData.Users[0].Id)) if err != nil { t.Fatal(err) } cursor, err := rdb.Table("subscriptions").Get(testData.Topics[1].Id + ":" + testData.Users[0].Id). Field("DeletedAt").Run(conn) if err != nil { t.Fatal(err) } defer cursor.Close() var deletedat time.Time if err = cursor.One(&deletedat); err != nil { t.Fatal(err) } if deletedat.IsZero() { t.Error("DeletedAt should not be null") } } func TestDeviceUpsert(t *testing.T) { err := adp.DeviceUpsert(types.ParseUserId("usr"+testData.Users[0].Id), testData.Devs[0]) if err != nil { t.Fatal(err) } cursor, err := rdb.Table("users").Get(testData.Users[0].Id).Field("Devices").Run(conn) if err != nil { t.Fatal(err) } defer cursor.Close() var got map[string]*types.DeviceDef if err = cursor.One(&got); err != nil { t.Fatal(err) } // Find the device (the key is hashed) var foundDev *types.DeviceDef for _, dev := range got { if dev != nil && dev.DeviceId == testData.Devs[0].DeviceId { foundDev = dev break } } if foundDev == nil { t.Error("Device not found after upsert") } else { foundDev.LastSeen = testData.Devs[0].LastSeen // Ignore LastSeen in comparison (workaranod for timezone issues) if !reflect.DeepEqual(*foundDev, *testData.Devs[0]) { t.Error(mismatchErrorString("Device", foundDev, testData.Devs[0])) } } // Test update testData.Devs[0].Platform = "Web" err = adp.DeviceUpsert(types.ParseUserId("usr"+testData.Users[0].Id), testData.Devs[0]) if err != nil { t.Fatal(err) } cursor2, err := rdb.Table("users").Get(testData.Users[0].Id).Field("Devices").Run(conn) if err != nil { t.Fatal(err) } defer cursor2.Close() if err = cursor2.One(&got); err != nil { t.Fatal(err) } // Find the updated device foundDev = nil for _, dev := range got { if dev != nil && dev.DeviceId == testData.Devs[0].DeviceId { foundDev = dev break } } if foundDev == nil || foundDev.Platform != "Web" { t.Error("Device not updated.", foundDev) } // Test add same device to another user err = adp.DeviceUpsert(types.ParseUserId("usr"+testData.Users[1].Id), testData.Devs[0]) if err != nil { t.Fatal(err) } err = adp.DeviceUpsert(types.ParseUserId("usr"+testData.Users[2].Id), testData.Devs[1]) if err != nil { t.Error(err) } } func TestMessageAttachments(t *testing.T) { fids := []string{testData.Files[0].Id, testData.Files[1].Id} err := adp.FileLinkAttachments("", types.ZeroUid, types.ParseUid(testData.Msgs[1].Id), fids) if err != nil { t.Fatal(err) } // Check if attachments were linked to message cursor, err := rdb.Table("messages").Get(testData.Msgs[1].Id).Field("Attachments").Run(conn) if err != nil { t.Fatal(err) } defer cursor.Close() var attachments []string if err = cursor.All(&attachments); err != nil { t.Fatal(err) } if !reflect.DeepEqual(attachments, fids) { t.Error(mismatchErrorString("Attachments", attachments, fids)) } // Check if use count was incremented in fileuploads cursor2, err := rdb.Table("fileuploads").Get(testData.Files[0].Id).Field("UseCount").Run(conn) if err != nil { t.Fatal(err) } defer cursor2.Close() var usecount int if err = cursor2.One(&usecount); err != nil { t.Fatal(err) } if usecount != 1 { t.Error(mismatchErrorString("UseCount", usecount, 1)) } } func TestFileFinishUpload(t *testing.T) { got, err := adp.FileFinishUpload(testData.Files[0], true, 22222) if err != nil { t.Fatal(err) } if got.Status != types.UploadCompleted { t.Error(mismatchErrorString("Status", got.Status, types.UploadCompleted)) } if got.Size != 22222 { t.Error(mismatchErrorString("Size", got.Size, 22222)) } } // ================== Other tests ================================= func TestDeviceGetAll(t *testing.T) { uid0 := types.ParseUserId("usr" + testData.Users[0].Id) uid1 := types.ParseUserId("usr" + testData.Users[1].Id) uid2 := types.ParseUserId("usr" + testData.Users[2].Id) gotDevs, count, err := adp.DeviceGetAll(uid0, uid1, uid2) if err != nil { t.Fatal(err) } if count < 1 { t.Fatal(mismatchErrorString("count", count, ">=1")) } // Test that devices exist for the users if len(gotDevs) == 0 { t.Error("Expected devices for users") } } func TestDeviceDelete(t *testing.T) { err := adp.DeviceDelete(types.ParseUserId("usr"+testData.Users[1].Id), testData.Devs[0].DeviceId) if err != nil { t.Fatal(err) } cursor, err := rdb.Table("users").Get(testData.Users[1].Id).Field("Devices").Run(conn) if err != nil { t.Fatal(err) } defer cursor.Close() var devices map[string]*types.DeviceDef if err = cursor.One(&devices); err != nil && err != rdb.ErrEmptyResult { t.Fatal(err) } // Check that the specific device is deleted for _, dev := range devices { if dev != nil && dev.DeviceId == testData.Devs[0].DeviceId { t.Error("Device not deleted:", dev) } } err = adp.DeviceDelete(types.ParseUserId("usr"+testData.Users[2].Id), "") if err != nil { t.Fatal(err) } cursor2, err := rdb.Table("users").Get(testData.Users[2].Id).Field("Devices").Run(conn) if err != nil { t.Fatal(err) } defer cursor2.Close() var allDevices map[string]*types.DeviceDef if err = cursor2.One(&allDevices); err != nil && err != rdb.ErrEmptyResult { t.Fatal(err) } if allDevices != nil && len(allDevices) > 0 { t.Error("All devices not deleted:", allDevices) } } // ================== Persistent Cache tests ====================== func TestPCacheUpsert(t *testing.T) { err := adp.PCacheUpsert("test_key", "test_value", false) if err != nil { t.Fatal(err) } // Test duplicate with failOnDuplicate = true err = adp.PCacheUpsert("test_key2", "test_value2", true) if err != nil { t.Fatal(err) } err = adp.PCacheUpsert("test_key2", "new_value", true) if err != types.ErrDuplicate { t.Error("Expected duplicate error") } } func TestPCacheGet(t *testing.T) { value, err := adp.PCacheGet("test_key") if err != nil { t.Fatal(err) } if value != "test_value" { t.Error(mismatchErrorString("Cache value", value, "test_value")) } // Test not found value, err = adp.PCacheGet("nonexistent") if err != types.ErrNotFound { t.Errorf("Expected not found error but got '%s', %s", value, err) } } func TestPCacheDelete(t *testing.T) { err := adp.PCacheDelete("test_key") if err != nil { t.Fatal(err) } // Verify deleted _, err = adp.PCacheGet("test_key") if err != types.ErrNotFound { t.Error("Key should be deleted") } } func TestPCacheExpire(t *testing.T) { // Insert some test keys with prefix and CreatedAt adp.PCacheUpsert("prefix_key1", "value1", true) adp.PCacheUpsert("prefix_key2", "value2", true) // Expire keys older than now (should delete all test keys) err := adp.PCacheExpire("prefix_", time.Now().Add(1*time.Minute)) if err != nil { t.Fatal(err) } } // ================== Delete tests ================================ func TestCredDel(t *testing.T) { err := adp.CredDel(types.ParseUserId("usr"+testData.Users[0].Id), "email", "alice@test.example.com") if err != nil { t.Fatal(err) } cursor, err := rdb.Table("credentials").Filter(map[string]any{"Method": "email", "Value": "alice@test.example.com"}).Run(conn) if err != nil { t.Fatal(err) } defer cursor.Close() var results []any if err = cursor.All(&results); err != nil { t.Fatal(err) } if len(results) != 0 { t.Error("Got result but shouldn't", results) } err = adp.CredDel(types.ParseUserId("usr"+testData.Users[1].Id), "", "") if err != nil { t.Fatal(err) } cursor2, err := rdb.Table("credentials").GetAllByIndex("User", testData.Users[1].Id).Run(conn) if err != nil { t.Fatal(err) } defer cursor2.Close() if err = cursor2.All(&results); err != nil { t.Fatal(err) } if len(results) != 0 { t.Error("Got result but shouldn't", results) } } func TestAuthDelScheme(t *testing.T) { // Test deleting auth scheme err := adp.AuthDelScheme(types.ParseUserId("usr"+testData.Recs[1].UserId), testData.Recs[1].Scheme) if err != nil { t.Fatal(err) } // Verify deleted _, _, _, _, err = adp.AuthGetRecord(types.ParseUserId("usr"+testData.Recs[1].UserId), testData.Recs[1].Scheme) if err != types.ErrNotFound { t.Error("Auth record should be deleted") } } func TestAuthDelAllRecords(t *testing.T) { delCount, err := adp.AuthDelAllRecords(types.ParseUserId("usr" + testData.Recs[0].UserId)) if err != nil { t.Fatal(err) } if delCount != 1 { t.Error(mismatchErrorString("delCount", delCount, 1)) } // With dummy user delCount, _ = adp.AuthDelAllRecords(dummyUid1) if delCount != 0 { t.Error(mismatchErrorString("delCount", delCount, 0)) } } func TestMessageDeleteList(t *testing.T) { toDel := types.DelMessage{ ObjHeader: types.ObjHeader{ Id: testData.UGen.GetStr(), CreatedAt: testData.Now, UpdatedAt: testData.Now, }, Topic: testData.Topics[1].Id, DeletedFor: testData.Users[2].Id, DelId: 1, SeqIdRanges: []types.Range{{Low: 9}, {Low: 3, Hi: 7}}, } err := adp.MessageDeleteList(toDel.Topic, &toDel) if err != nil { t.Fatal(err) } // Check messages in topic - some should be soft deleted cursor, err := rdb.Table("messages").Filter(map[string]any{"Topic": toDel.Topic}).Run(conn) if err != nil { t.Fatal(err) } defer cursor.Close() var messages []types.Message if err = cursor.All(&messages); err != nil { t.Fatal(err) } // Verify soft deletion worked foundSoftDeleted := false for _, msg := range messages { if msg.SeqId >= 3 && msg.SeqId <= 7 || msg.SeqId == 9 { if msg.DeletedFor != nil { for _, del := range msg.DeletedFor { if del.User == toDel.DeletedFor { foundSoftDeleted = true break } } } } } if !foundSoftDeleted { t.Error("Expected to find soft-deleted messages") } // Hard delete test toDel = types.DelMessage{ ObjHeader: types.ObjHeader{ Id: testData.UGen.GetStr(), CreatedAt: testData.Now, UpdatedAt: testData.Now, }, Topic: testData.Topics[0].Id, DelId: 3, SeqIdRanges: []types.Range{{Low: 1, Hi: 3}}, } err = adp.MessageDeleteList(toDel.Topic, &toDel) if err != nil { t.Fatal(err) } // Check if messages content was cleared (hard delete) cursor2, err := rdb.Table("messages").Filter(map[string]any{"Topic": toDel.Topic}).Run(conn) if err != nil { t.Fatal(err) } defer cursor2.Close() if err = cursor2.All(&messages); err != nil { t.Fatal(err) } for _, msg := range messages { if msg.SeqId >= 1 && msg.SeqId <= 3 { if msg.Content != nil && msg.DelId == 0 { t.Error("Message not properly hard deleted:", msg.SeqId) } } } err = adp.MessageDeleteList(testData.Topics[0].Id, nil) if err != nil { t.Fatal(err) } cursor3, err := rdb.Table("messages").Filter(map[string]any{"Topic": testData.Topics[0].Id}).Run(conn) if err != nil { t.Fatal(err) } defer cursor3.Close() if err = cursor3.All(&messages); err != nil { t.Fatal(err) } if len(messages) != 0 { t.Error("Result should be empty:", messages) } } func TestTopicDelete(t *testing.T) { err := adp.TopicDelete(testData.Topics[1].Id, false, false) if err != nil { t.Fatal(err) } cursor, err := rdb.Table("topics").Get(testData.Topics[1].Id).Field("State").Run(conn) if err != nil { t.Fatal(err) } defer cursor.Close() var state types.ObjState if err = cursor.One(&state); err != nil { t.Fatal(err) } if state != types.StateDeleted { t.Error("Soft delete failed:", state) } err = adp.TopicDelete(testData.Topics[0].Id, false, true) if err != nil { t.Fatal(err) } cursor2, err := rdb.Table("topics").Get(testData.Topics[0].Id).Run(conn) if err != nil { t.Fatal(err) } defer cursor2.Close() if !cursor2.IsNil() { t.Error("Hard delete failed - topic still exists") } } func TestFileDeleteUnused(t *testing.T) { // time.Now() is correct (as opposite to testData.Now): // the FileFinishUpload uses time.Now() as a timestamp. locs, err := adp.FileDeleteUnused(time.Now().Add(1*time.Minute), 999) if err != nil { t.Fatal(err) } if len(locs) < 1 { t.Log("No unused files to delete - this is expected in test environment") } } func TestUserDelete(t *testing.T) { err := adp.UserDelete(types.ParseUserId("usr"+testData.Users[0].Id), false) if err != nil { t.Fatal(err) } cursor, err := rdb.Table("users").Get(testData.Users[0].Id).Field("State").Run(conn) if err != nil { t.Fatal(err) } defer cursor.Close() var state types.ObjState if err = cursor.One(&state); err != nil { t.Fatal(err) } if state != types.StateDeleted { t.Error("User soft delete failed", state) } err = adp.UserDelete(types.ParseUserId("usr"+testData.Users[1].Id), true) if err != nil { t.Fatal(err) } cursor2, err := rdb.Table( "users").Get(testData.Users[1].Id).Run(conn) if err != nil { t.Fatal(err) } defer cursor2.Close() if !cursor2.IsNil() { t.Error("User hard delete failed - user still exists") } } func TestMessageGetDeleted(t *testing.T) { qOpts := types.QueryOpt{ Since: 1, Before: 10, Limit: 999, } got, err := adp.MessageGetDeleted(testData.Topics[1].Id, types.ParseUserId("usr"+testData.Users[2].Id), &qOpts) if err != nil { t.Fatal(err) } if len(got) != 1 { t.Error(mismatchErrorString("result length", len(got), 1)) } } func TestUserUnreadCount(t *testing.T) { uids := []types.Uid{ types.ParseUserId("usr" + testData.Users[1].Id), types.ParseUserId("usr" + testData.Users[2].Id), } expected := map[types.Uid]int{uids[0]: 0, uids[1]: 166} counts, err := adp.UserUnreadCount(uids...) if err != nil { t.Fatal(err) } if len(counts) != 2 { t.Error(mismatchErrorString("UnreadCount length", len(counts), 2)) } for uid, unread := range counts { if expected[uid] != unread { t.Error(mismatchErrorString("UnreadCount", unread, expected[uid])) } } // Test not found (even if the account is not found, the call must return one record). counts, err = adp.UserUnreadCount(dummyUid1) if err != nil { t.Fatal(err) } if len(counts) != 1 { t.Error(mismatchErrorString("UnreadCount length (dummy)", len(counts), 1)) } if counts[dummyUid1] != 0 { t.Error(mismatchErrorString("Non-zero UnreadCount (dummy)", counts[dummyUid1], 0)) } } // ================================================================ func mismatchErrorString(key string, got, want any) string { return fmt.Sprintf("%s mismatch:\nGot = %+v\nWant = %+v", key, got, want) } func init() { logs.Init(os.Stderr, "stdFlags") adp = backend.GetTestAdapter() conffile := flag.String("config", "./test.conf", "config of the database connection") if file, err := os.Open(*conffile); err != nil { log.Fatal("Failed to read config file:", err) } else if err = json.NewDecoder(jcr.New(file)).Decode(&config); err != nil { log.Fatal("Failed to parse config file:", err) } if adp == nil { log.Fatal("Database adapter is missing") } if adp.IsOpen() { log.Print("Connection is already opened") } err := adp.Open(config.Adapters[adp.GetName()]) if err != nil { log.Fatal(err) } conn = adp.GetTestDB().(*rdb.Session) testData = test_data.InitTestData() if testData == nil { log.Fatal("Failed to initialize test data") } store.SetTestUidGenerator(*testData.UGen) } ================================================ FILE: server/db/rethinkdb/tests/test.conf ================================================ { "reset_db_data": true, "adapters": { "rethinkdb": { // Address(es) of RethinkDB node(s): either a string or an array of strings. "addresses": "localhost:28015", // Name of the main database. "database": "tinode_test" } } } ================================================ FILE: server/drafty/drafty.go ================================================ // Package drafty contains utilities for conversion from Drafty to plain text. package drafty import ( "encoding/json" "errors" "sort" "strings" ) const ( // Maximum size of style payload in preview, in bytes. maxDataSize = 128 // Maximum count of payload fields in preview. maxDataCount = 8 ) var ( errUnrecognizedContent = errors.New("content unrecognized") errInvalidContent = errors.New("invalid format") ) type style struct { Tp string `json:"tp,omitempty"` At int `json:"at,omitempty"` Length int `json:"len,omitempty"` Key int `json:"key,omitempty"` } type entity struct { Tp string `json:"tp,omitempty"` Data map[string]any `json:"data,omitempty"` } type document struct { Txt string `json:"txt,omitempty"` Fmt []style `json:"fmt,omitempty"` Ent []entity `json:"ent,omitempty"` // Parsed out grapheme clusters. gc *graphemes } type span struct { tp string at int end int key int data map[string]any } type node struct { gc *graphemes sp *span children []*node } type previewState struct { drafty *document maxLength int keymap map[int]int } // Preview shortens Drafty to the specified length (in graphemes), removes quoted text, leading line breaks, // and large content from entities making them suitable for a one-line preview, // for example for showing in push notifications. // The return value is a Drafty document encoded as JSON string. func Preview(content any, length int) (string, error) { doc, err := decodeAsDrafty(content) if err != nil { return "", err } if doc == nil { return "", nil } tree, err := toTree(doc) if err != nil { return "", err } if tree == nil { return "", nil } state := previewState{ drafty: &document{ Fmt: make([]style, 0, len(doc.Fmt)), Ent: make([]entity, 0, len(doc.Ent)), }, maxLength: length, keymap: make(map[int]int), } if err = previewFormatter(tree, &state); err != nil { return "", err } state.drafty.Txt = state.drafty.gc.string() data, err := json.Marshal(state.drafty) return string(data), err } type plainTextState struct { txt string } // PlainText converts drafty document to plain text with some basic markdown-like formatting. // Deprecated: use Preview for new development. func PlainText(content any) (string, error) { doc, err := decodeAsDrafty(content) if err != nil { return "", err } if doc == nil { return "", nil } tree, err := toTree(doc) if err != nil { return "", err } state := plainTextState{} err = plainTextFormatter(tree, &state) if err != nil { return "", err } return strings.TrimSpace(string(state.txt)), nil } // styleToSpan converts Drafty style to internal representation. func (s *span) styleToSpan(in *style) error { s.tp = in.Tp s.at = in.At s.end = in.Length if s.end < 0 { return errInvalidContent } s.end += s.at if s.tp == "" { s.key = in.Key if s.key < 0 { return errInvalidContent } } return nil } type spanfmt struct { dec string isVoid bool } // Plain text formatting of the Drafty tags. Only non-blank tags need to be listed. var tags = map[string]spanfmt{ "BR": {"\n", true}, "CO": {"`", false}, "DL": {"~", false}, "EM": {"_", false}, "EX": {"", true}, "ST": {"*", false}, } // Type of the formatter to apply to tree nodes. type formatter func(n *node, state any) error // toTree converts a drafty document into a tree of formatted spans. // Each node of the tree is uniformly formatted. func toTree(drafty *document) (*node, error) { if len(drafty.Fmt) == 0 { return &node{gc: drafty.gc}, nil } textLen := drafty.gc.length() var spans []*span for i := range drafty.Fmt { s := span{} if err := s.styleToSpan(&drafty.Fmt[i]); err != nil { return nil, err } if s.at < -1 || s.end > textLen { return nil, errInvalidContent } // Denormalize entities into spans. if s.tp == "" && len(drafty.Ent) > 0 { if s.key < 0 || s.key >= len(drafty.Ent) { return nil, errInvalidContent } s.data = drafty.Ent[s.key].Data s.tp = drafty.Ent[s.key].Tp } if s.tp == "" && s.at == 0 && s.end == 0 && s.key == 0 { return nil, errUnrecognizedContent } spans = append(spans, &s) } // Sort spans first by start index (asc) then by length (desc). sort.Slice(spans, func(i, j int) bool { if spans[i].at == spans[j].at { // longer one comes first return spans[i].end > spans[j].end } return spans[i].at < spans[j].at }) // Drop the second format when spans overlap like '_first *second_ third*'. var filtered []*span end := -2 for _, span := range spans { if span.at < end && span.end > end { continue } filtered = append(filtered, span) if span.end > end { end = span.end } } // Iterate over an array of spans. children, err := forEach(drafty.gc, 0, textLen, filtered) if err != nil { return nil, err } return &node{children: children}, nil } // forEach recursively iterates nested spans to form a tree. func forEach(g *graphemes, start, end int, spans []*span) ([]*node, error) { var result []*node // Process ranges calling iterator for each range. for i := 0; i < len(spans); i++ { sp := spans[i] if sp.at < 0 { // Attachment result = append(result, &node{sp: sp}) continue } // Add un-styled range before the styled span starts. if start < sp.at { result = append(result, &node{gc: g.slice(start, sp.at)}) start = sp.at } // Get all spans which are within current span. var subspans []*span for si := i + 1; si < len(spans) && spans[si].at < sp.end; si++ { subspans = append(subspans, spans[si]) i = si } if tags[sp.tp].isVoid { result = append(result, &node{sp: sp}) } else { children, err := forEach(g, start, sp.end, subspans) if err != nil { return nil, err } result = append(result, &node{children: children, sp: sp}) } start = sp.end } // Add the remaining unformatted range. if start < end { result = append(result, &node{gc: g.slice(start, end)}) } return result, nil } // plainTextFormatter converts a tree of formatted spans into plan text. func plainTextFormatter(n *node, ctx any) error { if n.sp != nil && n.sp.tp == "QQ" { return nil } var text string if len(n.children) > 0 { state := &plainTextState{} for _, c := range n.children { if err := plainTextFormatter(c, state); err != nil { return err } } text = string(state.txt) } else { text = n.gc.string() } state := ctx.(*plainTextState) if n.sp == nil { state.txt += text return nil } switch n.sp.tp { case "ST", "EM", "DL", "CO": state.txt += tags[n.sp.tp].dec + text + tags[n.sp.tp].dec case "LN": if url, ok := nullableMapGet(n.sp.data, "url"); ok && url != text { state.txt += "[" + text + "](" + url + ")" } else { state.txt += text } case "MN", "HT": state.txt += text case "BR": state.txt += "\n" case "AU", "EX", "IM", "VD": name, ok := nullableMapGet(n.sp.data, "name") if !ok || name == "" { name = "?" } expand := map[string]string{"AU": "AUDIO", "EX": "FILE", "IM": "IMAGE", "VD": "VIDEO"} state.txt += "[" + expand[n.sp.tp] + " '" + name + "']" case "VC": state.txt += "[CALL]" default: state.txt += text } return nil } // previewFormatter converts a tree of formatted spans into a shortened drafty document. func previewFormatter(n *node, ctx any) error { state := ctx.(*previewState) at := state.drafty.gc.length() if at >= state.maxLength { // Maximum doc length reached. return nil } if n.sp != nil { if n.sp.tp == "QQ" { // Skip quoted text return nil } if n.sp.tp == "BR" && at == 0 { // Skip leading new lines. return nil } } if len(n.children) > 0 { for _, c := range n.children { if err := previewFormatter(c, ctx); err != nil { return err } } } else { increment := n.gc.length() if increment > 0 { if at+increment > state.maxLength { increment = state.maxLength - at } if state.drafty.gc == nil { state.drafty.gc = prepareGraphemes("") } state.drafty.gc = state.drafty.gc.append(n.gc.slice(0, increment)) } } end := state.drafty.gc.length() if n.sp != nil { fmt := style{} if n.sp.at < 0 { fmt.At = -1 } else if at < end || tags[n.sp.tp].isVoid { fmt.At = at fmt.Length = end - at } else { return nil } if n.sp.data != nil { // Check if we have already seen this payload. key, ok := state.keymap[n.sp.key] if !ok { // Payload not found, add it. ent := entity{Tp: n.sp.tp, Data: copyLight(n.sp.data)} key = len(state.drafty.Ent) state.keymap[n.sp.key] = key state.drafty.Ent = append(state.drafty.Ent, ent) } fmt.Key = key } else { fmt.Tp = n.sp.tp } state.drafty.Fmt = append(state.drafty.Fmt, fmt) } return nil } // nullableMapGet is a helper method to get a possibly missing string from a possibly nil map. func nullableMapGet(data map[string]any, key string) (string, bool) { if data == nil { return "", false } str, ok := data[key].(string) return str, ok } // decodeAsDrafty converts a string or a map to a Drafty document. func decodeAsDrafty(content any) (*document, error) { if content == nil { return nil, nil } var drafty *document switch tmp := content.(type) { case string: drafty = &document{gc: prepareGraphemes(tmp)} case map[string]any: drafty = &document{} correct := 0 if txt, ok := tmp["txt"].(string); ok { drafty.Txt = txt drafty.gc = prepareGraphemes(txt) correct++ } if ifmt, ok := tmp["fmt"].([]any); ok { for i := range ifmt { st, err := decodeAsStyle(ifmt[i]) if err != nil { return nil, err } if st != nil { drafty.Fmt = append(drafty.Fmt, *st) } correct++ } } if ient, ok := tmp["ent"].([]any); ok { for i := range ient { ent, err := decodeAsEntity(ient[i]) if err != nil { return nil, err } if ent != nil { drafty.Ent = append(drafty.Ent, *ent) } correct++ } } // At least one drafty element must be present. if correct == 0 { return nil, errUnrecognizedContent } default: return nil, errUnrecognizedContent } return drafty, nil } // decodeAsStyle converts a map to a style. func decodeAsStyle(content any) (*style, error) { if content == nil { return nil, nil } tmp, ok := content.(map[string]any) if !ok { return nil, errUnrecognizedContent } var err error st := &style{} st.Tp, _ = tmp["tp"].(string) st.At, err = intFromNumeric(tmp["at"]) if err != nil { return nil, err } st.Length, err = intFromNumeric(tmp["len"]) if err != nil { return nil, err } if st.Tp == "" { st.Key, err = intFromNumeric(tmp["key"]) if err != nil { return nil, err } if st.Key < 0 { return nil, errInvalidContent } } return st, nil } // decodeAsEntity converts a map to a entity. func decodeAsEntity(content any) (*entity, error) { if content == nil { return nil, nil } tmp, ok := content.(map[string]any) if !ok { return nil, errUnrecognizedContent } ent := &entity{} ent.Tp, _ = tmp["tp"].(string) if ent.Tp == "" { return nil, errInvalidContent } ent.Data, _ = tmp["data"].(map[string]any) return ent, nil } // A whitelist of entity fields to copy. var lightFields = []string{"mime", "name", "width", "height", "size", "url", "ref"} // copyLight makes a copy of an entity retaining keys from the white list. // It also ensures the copied values are either basic types of fixed length or a // sufficiently short string/byte slice, and the count of entries is not too great. func copyLight(in any) map[string]any { data, ok := in.(map[string]any) if !ok { return nil } result := map[string]any{} if len(data) > 0 { for _, key := range lightFields { if val, ok := data[key]; ok { if isFixedLengthType(val) { result[key] = val } else if l := getVariableTypeSize(val); l >= 0 && l < maxDataSize { result[key] = val } } if len(result) > maxDataCount { break } } if len(result) == 0 { result = nil } } return result } // intFromNumeric is a helper methjod to get an integer from a value of any numeric type. func intFromNumeric(num any) (int, error) { if num == nil { return 0, nil } switch i := num.(type) { case int: return i, nil case int16: return int(i), nil case int32: return int(i), nil case int64: return int(i), nil case float32: return int(i), nil case float64: return int(i), nil default: return 0, errInvalidContent } } // getVariableTypeSize checks that the given field is a string or a byte slice and gets its size in bytes. func getVariableTypeSize(x any) int { switch val := x.(type) { case string: return len(val) case []byte: return len(val) default: return -1 } } // isFixedLengthType checks if the given value is a type of a fixed size. func isFixedLengthType(x any) bool { switch x.(type) { case nil, bool, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, complex64, complex128: return true default: return false } } ================================================ FILE: server/drafty/drafty_test.go ================================================ package drafty import ( "encoding/json" "testing" ) var validInputs = []string{ `"This is a plain text string."`, `{ "txt":"This is a string with a line break.", "fmt":[{"at":9,"tp":"BR"}] }`, `{ "ent":[{"data":{"mime":"image/jpeg","name":"hello.jpg","val":"<38992, bytes: ...>","width":100, "height":80},"tp":"EX"}], "fmt":[{"at":-1, "key":0}] }`, `{ "ent":[{"data":{"url":"https://www.youtube.com/watch?v=dQw4w9WgXcQ"},"tp":"LN"}], "fmt":[{"len":22}], "txt":"https://api.tinode.co/" }`, `{ "ent":[{"data":{"url":"https://api.tinode.co/"},"tp":"LN"}], "fmt":[{"len":22}], "txt":"https://api.tinode.co/" }`, `{ "ent":[{"data":{"url":"http://tinode.co"},"tp":"LN"}], "fmt":[{"at":9,"len":3}, {"at":4,"len":3}], "txt":"Url one, two" }`, `{ "ent":[{"data":{"height":213,"mime":"image/jpeg","name":"roses.jpg","val":"<38992, bytes: ...>","width":638},"tp":"IM"}], "fmt":[{"len":1}], "txt":" " }`, `{ "txt":"This text has staggered formats", "fmt":[{"at":5,"len":8,"tp":"EM"},{"at":10,"len":13,"tp":"ST"}] }`, `{ "txt":"This text is formatted and deleted too", "fmt":[{"at":5,"len":4,"tp":"ST"},{"at":13,"len":9,"tp":"EM"},{"at":35,"len":3,"tp":"ST"},{"at":27,"len":11,"tp":"DL"}] }`, `{ "txt":"мультибайтовый юникод", "fmt":[{"len":14,"tp":"ST"},{"at":15,"len":6,"tp":"EM"}] }`, `{ "txt":"Alice Johnson This is a test", "fmt":[{"at":13,"len":1,"tp":"BR"},{"at":15,"len":1},{"len":13,"key":1},{"len":16,"tp":"QQ"},{"at":16,"len":1,"tp":"BR"}], "ent":[{"tp":"IM","data":{"mime":"image/jpeg","val":"<1292, bytes: /9j/4AAQSkZJ...rehH5o6D/9k=>","width":25,"height":14,"size":968}},{"tp":"MN","data":{"color":2}}] }`, `{ "txt": "Hello 😀, o😀k https://google.com", "fmt":[{"at":9,"len":3,"tp":"ST"},{"at":13,"len":18}], "ent":[{"tp":"LN","data":{"url":"https://google.com"}}] }`, `{ "txt": "Hi 🏴󠁧󠁢󠁳󠁣󠁴󠁿🏴󠁧󠁢󠁳󠁣󠁴󠁿🏴󠁧󠁢󠁳󠁣󠁴󠁿🏴󠁧󠁢󠁳󠁣󠁴󠁿 🏴󠁧󠁢󠁳󠁣󠁴󠁿🏴󠁧󠁢󠁳󠁣󠁴󠁿🏴󠁧󠁢󠁳󠁣󠁴󠁿🏴󠁧󠁢󠁳󠁣󠁴󠁿 🏴󠁧󠁢󠁳󠁣󠁴󠁿🏴󠁧󠁢󠁳󠁣󠁴󠁿🏴󠁧󠁢󠁳󠁣󠁴󠁿🏴󠁧󠁢󠁳󠁣󠁴󠁿", "fmt":[{"at":3,"len":4,"tp":"ST"},{"at":8,"len":4,"tp":"ST"}] }`, } var invalidInputs = []string{ `{ "txt":"This should fail", "fmt":[{"at":50,"len":-45,"tp":"ST"}] }`, `{ "txt":"This should fail", "fmt":[{"at":0,"len":50,"tp":"ST"}] }`, `{ "ent":[], "fmt":[{"at":0,"len":1,"tp":"ST","key":1}] }`, `{ "ent":[{"xy": true, "tp": "XY"}], "fmt":[{"len":1,"key":-2}], "txt":" " }`, `{ "ent":[{"data": true, "tp": "ST"}], "fmt":[{"len":1,"key":42, "at":"33"}], "txt":"123" }`, `{ "txt":true }`, `{ "invalid":[{"data": true, "tp": "ST"}], "content":[{"len":1, "key":42}] }`, } func TestPlainText(t *testing.T) { expect := []string{ "This is a plain text string.", "This is a\n string with a line break.", "[FILE 'hello.jpg']", "[https://api.tinode.co/](https://www.youtube.com/watch?v=dQw4w9WgXcQ)", "https://api.tinode.co/", "Url [one](http://tinode.co), [two](http://tinode.co)", "[IMAGE 'roses.jpg']", "This _text has_ staggered formats", "This *text* is _formatted_ and ~deleted *too*~", "*мультибайтовый* _юникод_", "This is a test", "Hello 😀, *o😀k* https://google.com", "Hi *🏴󠁧󠁢󠁳󠁣󠁴󠁿🏴󠁧󠁢󠁳󠁣󠁴󠁿🏴󠁧󠁢󠁳󠁣󠁴󠁿🏴󠁧󠁢󠁳󠁣󠁴󠁿* *🏴󠁧󠁢󠁳󠁣󠁴󠁿🏴󠁧󠁢󠁳󠁣󠁴󠁿🏴󠁧󠁢󠁳󠁣󠁴󠁿🏴󠁧󠁢󠁳󠁣󠁴󠁿* 🏴󠁧󠁢󠁳󠁣󠁴󠁿🏴󠁧󠁢󠁳󠁣󠁴󠁿🏴󠁧󠁢󠁳󠁣󠁴󠁿🏴󠁧󠁢󠁳󠁣󠁴󠁿", } for i := range validInputs { var val any if err := json.Unmarshal([]byte(validInputs[i]), &val); err != nil { t.Errorf("Failed to parse input %d '%s': %s", i, validInputs[i], err) } res, err := PlainText(val) if err != nil { t.Errorf("%d failed with error: %s", i, err) } else if res != expect[i] { t.Errorf("%d output '%s' does not match '%s'", i, res, expect[i]) } } for i := range invalidInputs { var val any if err := json.Unmarshal([]byte(invalidInputs[i]), &val); err != nil { // Don't make it an error: we are not testing validity of json.Unmarshal. t.Logf("Failed to parse input %d '%s': %s", i, invalidInputs[i], err) } res, err := PlainText(val) if err == nil { t.Errorf("invalid input %d '%s' did not cause an error '%s'", i, invalidInputs[i], res) } } } func TestPreview(t *testing.T) { expect := []string{ `{"txt":"This is a plain"}`, `{"txt":"This is a strin","fmt":[{"tp":"BR","at":9}]}`, `{"fmt":[{"at":-1}],"ent":[{"tp":"EX","data":{"height":80,"mime":"image/jpeg","name":"hello.jpg","width":100}}]}`, `{"txt":"https://api.tin","fmt":[{"len":15}],"ent":[{"tp":"LN","data":{"url":"https://www.youtube.com/watch?v=dQw4w9WgXcQ"}}]}`, `{"txt":"https://api.tin","fmt":[{"len":15}],"ent":[{"tp":"LN","data":{"url":"https://api.tinode.co/"}}]}`, `{"txt":"Url one, two","fmt":[{"at":4,"len":3},{"at":9,"len":3}],"ent":[{"tp":"LN","data":{"url":"http://tinode.co"}}]}`, `{"txt":" ","fmt":[{"len":1}],"ent":[{"tp":"IM","data":{"height":213,"mime":"image/jpeg","name":"roses.jpg","width":638}}]}`, `{"txt":"This text has s","fmt":[{"tp":"EM","at":5,"len":8}]}`, `{"txt":"This text is fo","fmt":[{"tp":"ST","at":5,"len":4},{"tp":"EM","at":13,"len":2}]}`, `{"txt":"мультибайтовый ","fmt":[{"tp":"ST","len":14}]}`, `{"txt":"This is a test"}`, `{"txt":"Hello 😀, o😀k ht","fmt":[{"tp":"ST","at":9,"len":3},{"at":13,"len":2}],"ent":[{"tp":"LN","data":{"url":"https://google.com"}}]}`, `{"txt":"Hi 🏴󠁧󠁢󠁳󠁣󠁴󠁿🏴󠁧󠁢󠁳󠁣󠁴󠁿🏴󠁧󠁢󠁳󠁣󠁴󠁿🏴󠁧󠁢󠁳󠁣󠁴󠁿 🏴󠁧󠁢󠁳󠁣󠁴󠁿🏴󠁧󠁢󠁳󠁣󠁴󠁿🏴󠁧󠁢󠁳󠁣󠁴󠁿🏴󠁧󠁢󠁳󠁣󠁴󠁿 🏴󠁧󠁢󠁳󠁣󠁴󠁿🏴󠁧󠁢󠁳󠁣󠁴󠁿","fmt":[{"tp":"ST","at":3,"len":4},{"tp":"ST","at":8,"len":4}]}`, } for i := range validInputs { var val any if err := json.Unmarshal([]byte(validInputs[i]), &val); err != nil { t.Errorf("Failed to parse input %d '%s': %s", i, validInputs[i], err) } res, err := Preview(val, 15) if err != nil { t.Errorf("%d failed with error: %s", i, err) } else if res != expect[i] { t.Errorf("%d output '%s' does not match '%s'", i, res, expect[i]) } } // Only some invalid input should fail these tests. testsToFail := []int{3, 4, 5, 6} for _, i := range testsToFail { var val any if err := json.Unmarshal([]byte(invalidInputs[i]), &val); err != nil { // Don't make it an error: we are not testing validity of json.Unmarshal. t.Logf("Failed to parse input %d '%s': %s", i, invalidInputs[i], err) } res, err := Preview(val, 15) if err == nil { t.Errorf("invalid input %d did not cause an error '%s'", i, res) } } } ================================================ FILE: server/drafty/grapheme.go ================================================ package drafty import ( "github.com/rivo/uniseg" ) // graphemes is a container holding lengths of grapheme clusters in a string. type graphemes struct { // The original string. original string // Sizes of grapheme clusters within the original string. sizes []byte } // prepareGraphemes returns a parsed grapheme cluster container by splitting the string into grapheme clusters // and saving their lengths. func prepareGraphemes(str string) *graphemes { // Split the string into grapheme clusters and save the size of each cluster. sizes := make([]byte, 0, len(str)) for state, remaining, cluster := -1, str, ""; len(remaining) > 0; { cluster, remaining, _, state = uniseg.StepString(remaining, state) sizes = append(sizes, byte(len(cluster))) } return &graphemes{ original: str, sizes: sizes, } } // length returns the number of grapheme clusters in the original string. func (g *graphemes) length() int { if g == nil { return 0 } return len(g.sizes) } // string returns the original string from which the grapheme cluster container was created. func (g *graphemes) string() string { if g == nil { return "" } return g.original } // slice returns a new grapheme cluster container with grapheme clusters from 'start' to 'end'. func (g *graphemes) slice(start, end int) *graphemes { // Convert grapheme offsets to string offsets. s := 0 for i := range start { s += int(g.sizes[i]) } e := s for i := start; i < end; i++ { e += int(g.sizes[i]) } return &graphemes{ original: g.original[s:e], sizes: g.sizes[start:end], } } // append appends 'other' grapheme cluster container to 'g' container and returns g. // If g is nil, the 'other' is returned. func (g *graphemes) append(other *graphemes) *graphemes { if g == nil { return other } g.original += other.original g.sizes = append(g.sizes, other.sizes...) return g } ================================================ FILE: server/hdl_files.go ================================================ /****************************************************************************** * * Description : * * Handler of large file uploads/downloads. Validates request first then calls * a handler. * *****************************************************************************/ package main import ( "encoding/base64" "encoding/json" "errors" "io" "math/rand" "mime" "net/http" "net/url" "strconv" "strings" "time" "github.com/tinode/chat/pbx" "github.com/tinode/chat/server/logs" "github.com/tinode/chat/server/store" "github.com/tinode/chat/server/store/types" "google.golang.org/grpc/peer" ) // Allowed mime types for user-provided Content-type field. Must be alphabetically sorted. // Types not in the list are converted to "application/octet-stream". // See https://www.iana.org/assignments/media-types/media-types.xhtml var allowedMimeTypes = []string{"application/", "audio/", "font/", "image/", "text/", "video/"} func largeFileServeHTTP(wrt http.ResponseWriter, req *http.Request) { now := types.TimeNow() enc := json.NewEncoder(wrt) mh := store.Store.GetMediaHandler() statsInc("FileDownloadsTotal", 1) writeHttpResponse := func(msg *ServerComMessage, err error) { // Gorilla CompressHandler requires Content-Type to be set. wrt.Header().Set("Content-Type", "application/json; charset=utf-8") wrt.WriteHeader(msg.Ctrl.Code) enc.Encode(msg) if err != nil { logs.Warn.Println("media serve:", req.URL.String(), err) } } // Preflight request: process before any security checks. if req.Method == http.MethodOptions { headers, statusCode, err := mh.Headers(req.Method, req.URL, req.Header, true) if err != nil { writeHttpResponse(decodeStoreError(err, "", now, nil), err) return } for name, values := range headers { for _, value := range values { wrt.Header().Add(name, value) } } if statusCode <= 0 { statusCode = http.StatusNoContent } wrt.WriteHeader(statusCode) logs.Info.Println("media serve: preflight completed") return } // Check if this is a GET/HEAD request. if req.Method != http.MethodGet && req.Method != http.MethodHead { writeHttpResponse(ErrOperationNotAllowed("", "", now), errors.New("method '"+req.Method+"' not allowed")) return } // Check for API key presence if isValid, _ := checkAPIKey(getAPIKey(req)); !isValid { writeHttpResponse(ErrAPIKeyRequired(now), errors.New("invalid or missing API key")) return } // Check authorization: either auth information or SID must be present authMethod, secret := getHttpAuth(req) uid, challenge, err := authFileRequest(authMethod, secret, req.FormValue("sid"), getRemoteAddr(req)) if err != nil { writeHttpResponse(decodeStoreError(err, "", now, nil), err) return } if challenge != nil { writeHttpResponse(InfoChallenge("", now, challenge), nil) return } if uid.IsZero() { // Not authenticated writeHttpResponse(ErrAuthRequired("", "", now, now), errors.New("user not authenticated")) return } // Check if media handler redirects or adds headers. headers, statusCode, err := mh.Headers(req.Method, req.URL, req.Header, true) if err != nil { writeHttpResponse(decodeStoreError(err, "", now, nil), err) return } for name, values := range headers { for _, value := range values { wrt.Header().Add(name, value) } } if statusCode != 0 { // The handler requested to terminate further processing. wrt.WriteHeader(statusCode) if req.Method == http.MethodGet { enc.Encode(&ServerComMessage{ Ctrl: &MsgServerCtrl{ Code: statusCode, Text: http.StatusText(statusCode), Timestamp: now, }, }) } logs.Info.Println("media serve: completed with status", statusCode, "uid=", uid) return } if req.Method == http.MethodHead { wrt.WriteHeader(http.StatusOK) logs.Info.Println("media serve: completed", req.Method, "uid=", uid) return } fd, rsc, err := mh.Download(req.URL.String()) if err != nil { writeHttpResponse(decodeStoreError(err, "", now, nil), err) return } defer rsc.Close() wrt.Header().Set("Content-Type", fd.MimeType) asAttachment, _ := strconv.ParseBool(req.URL.Query().Get("asatt")) // Force download for html files as a security measure. asAttachment = asAttachment || strings.Contains(fd.MimeType, "html") || strings.Contains(fd.MimeType, "xml") || strings.HasPrefix(fd.MimeType, "application/") || // The 'message', 'model', and 'multipart' cannot currently appear, but checked anyway in case // DetectContentType changes its logic. strings.HasPrefix(fd.MimeType, "message/") || strings.HasPrefix(fd.MimeType, "model/") || strings.HasPrefix(fd.MimeType, "multipart/") || strings.HasPrefix(fd.MimeType, "text/") if asAttachment { wrt.Header().Set("Content-Disposition", "attachment") } http.ServeContent(wrt, req, "", fd.UpdatedAt, rsc) logs.Info.Println("media serve: OK, uid=", uid) } // largeFileReceiveHTTP receives files from client over HTTP(S) and passes them to the configured media handler. func largeFileReceiveHTTP(wrt http.ResponseWriter, req *http.Request) { now := types.TimeNow() enc := json.NewEncoder(wrt) mh := store.Store.GetMediaHandler() statsInc("FileUploadsTotal", 1) writeHttpResponse := func(msg *ServerComMessage, err error) { // Gorilla CompressHandler requires Content-Type to be set. wrt.Header().Set("Content-Type", "application/json; charset=utf-8") wrt.WriteHeader(msg.Ctrl.Code) enc.Encode(msg) if err != nil { logs.Info.Println("media upload:", msg.Ctrl.Code, msg.Ctrl.Text, "/", err) } } // Preflight request: process before any security checks. if req.Method == http.MethodOptions { headers, statusCode, err := mh.Headers(req.Method, req.URL, req.Header, true) if err != nil { writeHttpResponse(decodeStoreError(err, "", now, nil), err) return } for name, values := range headers { for _, value := range values { wrt.Header().Add(name, value) } } if statusCode <= 0 { statusCode = http.StatusNoContent } wrt.WriteHeader(statusCode) logs.Info.Println("media upload: preflight completed") return } // Check if this is a POST/PUT/HEAD request. if req.Method != http.MethodPost && req.Method != http.MethodPut && req.Method != http.MethodHead { writeHttpResponse(ErrOperationNotAllowed("", "", now), errors.New("method '"+req.Method+"' not allowed")) return } if globals.maxFileUploadSize > 0 { // Enforce maximum upload size. req.Body = http.MaxBytesReader(wrt, req.Body, globals.maxFileUploadSize) } // Check for API key presence if isValid, _ := checkAPIKey(getAPIKey(req)); !isValid { writeHttpResponse(ErrAPIKeyRequired(now), nil) return } msgID := req.FormValue("id") // Check authorization: either auth information or SID must be present authMethod, secret := getHttpAuth(req) uid, challenge, err := authFileRequest(authMethod, secret, req.FormValue("sid"), getRemoteAddr(req)) if err != nil { writeHttpResponse(decodeStoreError(err, msgID, now, nil), err) return } if challenge != nil { writeHttpResponse(InfoChallenge(msgID, now, challenge), nil) return } if uid.IsZero() && req.FormValue("topic") != "newacc" { // Not authenticated and not signup. writeHttpResponse(ErrAuthRequired(msgID, "", now, now), nil) return } // Check if uploads are handled elsewhere. headers, statusCode, err := mh.Headers(req.Method, req.URL, req.Header, true) if err != nil { logs.Info.Println("media upload: headers check failed", err) writeHttpResponse(decodeStoreError(err, "", now, nil), err) return } for name, values := range headers { for _, value := range values { wrt.Header().Add(name, value) } } if statusCode != 0 { // The handler requested to terminate further processing. wrt.WriteHeader(statusCode) if req.Method == http.MethodPost || req.Method == http.MethodPut { enc.Encode(&ServerComMessage{ Ctrl: &MsgServerCtrl{ Code: statusCode, Text: http.StatusText(statusCode), Timestamp: now, }, }) } logs.Info.Println("media upload: completed with status", statusCode) return } if req.Method == http.MethodHead || req.Method == http.MethodOptions { wrt.WriteHeader(http.StatusOK) logs.Info.Println("media upload: completed", req.Method) return } file, header, err := req.FormFile("file") if err != nil { logs.Info.Println("media upload: invalid multipart form", err) if strings.Contains(err.Error(), "request body too large") { writeHttpResponse(ErrTooLarge(msgID, "", now), err) } else { writeHttpResponse(ErrMalformed(msgID, "", now), err) } return } buff := make([]byte, 512) if _, err = file.Read(buff); err != nil { writeHttpResponse(ErrUnknown(msgID, "", now), err) return } mimeType := http.DetectContentType(buff) // If DetectContentType fails, see if client-provided content type can be used. if mimeType == "application/octet-stream" { if userContentType, params, err := mime.ParseMediaType(header.Header.Get("Content-Type")); err == nil { // Make sure the content-type is legit. for _, allowed := range allowedMimeTypes { if strings.HasPrefix(userContentType, allowed) { if userContentType = mime.FormatMediaType(userContentType, params); userContentType != "" { mimeType = userContentType } break } } } } fdef := &types.FileDef{ ObjHeader: types.ObjHeader{ Id: store.Store.GetUidString(), }, User: uid.String(), MimeType: mimeType, } fdef.InitTimes() if _, err = file.Seek(0, io.SeekStart); err != nil { writeHttpResponse(ErrUnknown(msgID, "", now), err) return } url, size, err := mh.Upload(fdef, file) if err != nil { logs.Info.Println("media upload: failed", file, "key", fdef.Location, err) store.Files.FinishUpload(fdef, false, 0) writeHttpResponse(decodeStoreError(err, msgID, now, nil), err) return } fdef, err = store.Files.FinishUpload(fdef, true, size) if err != nil { logs.Info.Println("media upload: failed to finalize", file, "key", fdef.Location, err) // Best effort cleanup. mh.Delete([]string{fdef.Location}) writeHttpResponse(decodeStoreError(err, msgID, now, nil), err) return } params := map[string]string{"url": url} if globals.mediaGcPeriod > 0 { // How long this file is guaranteed to exist without being attached to a message or a topic. params["expires"] = now.Add(globals.mediaGcPeriod).Format(types.TimeFormatRFC3339) } writeHttpResponse(NoErrParams(msgID, "", now, params), nil) logs.Info.Println("media upload: ok", fdef.Id, fdef.Location) } // LargeFileServe is the gRPC equivalent of largeFileServeHTTP. func (*grpcNodeServer) LargeFileServe(req *pbx.FileDownReq, stream pbx.Node_LargeFileServeServer) error { now := types.TimeNow() writeResponse := func(msg *ServerComMessage, err error) { stream.Send(&pbx.FileDownResp{Id: msg.Ctrl.Id, Code: int32(msg.Ctrl.Code), Text: msg.Ctrl.Text}) if err != nil { logs.Info.Println("media serve:", msg.Ctrl.Code, msg.Ctrl.Text, "/", err) } } msgID := req.GetId() // Check authorization: auth information must be present (SID is not used for gRPC). authMethod, secret := req.Auth.Scheme, req.Auth.Secret var remoteAddr string if p, ok := peer.FromContext(stream.Context()); ok { remoteAddr = p.Addr.String() } uid, challenge, err := authFileRequest(authMethod, secret, "", remoteAddr) if err != nil { writeResponse(decodeStoreError(err, msgID, now, nil), err) return nil } if challenge != nil { writeResponse(InfoChallenge(msgID, now, challenge), nil) return nil } if uid.IsZero() { // Not authenticated writeResponse(ErrAuthRequired(msgID, "", now, now), errors.New("user not authenticated")) return nil } // Check if media handler redirects or adds headers. mh := store.Store.GetMediaHandler() url, _ := url.Parse(req.Uri) headers, statusCode, err := mh.Headers(http.MethodGet, url, http.Header{}, true) if err != nil { writeResponse(decodeStoreError(err, "", now, nil), err) return nil } resp := pbx.FileDownResp{Meta: &pbx.FileMeta{}} if statusCode != 0 { // The handler requested to terminate further processing. resp.Code = int32(statusCode) resp.Text = http.StatusText(statusCode) resp.RedirUrl = headers.Get("Location") stream.Send(&resp) logs.Info.Println("media serve: completed with status", statusCode, "uid=", uid) return nil } fd, rsc, err := mh.Download(req.GetUri()) if err != nil { writeResponse(decodeStoreError(err, msgID, now, nil), err) return nil } defer rsc.Close() resp.Code = http.StatusOK resp.Text = http.StatusText(http.StatusOK) resp.Meta.Name = fd.Location resp.Meta.MimeType = fd.MimeType resp.Meta.Size = fd.Size resp.Content = make([]byte, 1024*1024*2) var n int result := "OK" for { n, err = rsc.Read(resp.Content) if err == nil { resp.Content = resp.Content[:n] if err = stream.Send(&resp); err != nil { logs.Info.Println("media serve: failed, uid=", uid, err) break } continue } if err == io.EOF { err = nil } else { result = err.Error() } break } logs.Info.Println("media serve: ", result, ", uid=", uid) return err } // LargeFileReceive is the gRPC equivalent of LargeFileReceiveHTTP. func (*grpcNodeServer) LargeFileReceive(stream pbx.Node_LargeFileReceiveServer) error { now := types.TimeNow() mh := store.Store.GetMediaHandler() writeResponse := func(msg *ServerComMessage, err error) { stream.SendAndClose(&pbx.FileUpResp{Id: msg.Ctrl.Id, Code: int32(msg.Ctrl.Code), Text: msg.Ctrl.Text}) if err != nil { logs.Info.Println("media receive:", msg.Ctrl.Code, msg.Ctrl.Text, "/", err) } } req, err := stream.Recv() if err != nil { if errors.Is(err, io.EOF) { writeResponse(ErrDisconnected("", "", now), err) } else { writeResponse(decodeStoreError(err, "", now, nil), err) } return nil } msgID := req.GetId() // Check authorization: auth information must be present (SID is not used for gRPC). authMethod, secret := req.Auth.Scheme, req.Auth.Secret var remoteAddr string if p, ok := peer.FromContext(stream.Context()); ok { remoteAddr = p.Addr.String() } uid, challenge, err := authFileRequest(authMethod, secret, "", remoteAddr) if err != nil { writeResponse(decodeStoreError(err, msgID, now, nil), err) return nil } if challenge != nil { writeResponse(InfoChallenge(msgID, now, challenge), nil) return nil } if uid.IsZero() { // Not authenticated writeResponse(ErrAuthRequired(msgID, "", now, now), errors.New("user not authenticated")) return nil } // Check if uploads are handled elsewhere. headers, statusCode, err := mh.Headers(http.MethodPost, nil, http.Header{}, false) if err != nil { logs.Info.Println("media upload: headers check failed", err) writeResponse(decodeStoreError(err, "", now, nil), nil) return nil } if statusCode != 0 { // The handler requested to terminate further processing. err = stream.SendAndClose(&pbx.FileUpResp{ Id: msgID, Code: int32(statusCode), Text: http.StatusText(statusCode), RedirUrl: headers.Get("Location"), }) logs.Info.Println("media upload: completed with status", statusCode, "uid=", uid, err) return err } mimeType := http.DetectContentType(req.Content) // If DetectContentType fails, use client-provided content type. if mimeType == "application/octet-stream" { if contentType := req.Meta.GetMimeType(); contentType != "" { mimeType = contentType } } fdef := &types.FileDef{ ObjHeader: types.ObjHeader{ Id: store.Store.GetUidString(), }, User: uid.String(), MimeType: mimeType, } fdef.InitTimes() reader, writer := io.Pipe() // Create a non-blocking channel to collect errors from the inbound IO process. done := make(chan error, 1) go func() { defer writer.Close() for { if req, err := stream.Recv(); err == nil { chunk := req.GetContent() if _, err := writer.Write(chunk); err != nil { done <- err break } } else { if err == io.EOF { err = nil } done <- err break } } }() url, size, err := mh.Upload(fdef, reader) if err == nil { // No outbound IO error. Maybe we have an inbound one? err = <-done } if err != nil { logs.Info.Println("media upload: failed", req.Meta.Name, "key", fdef.Location, err) store.Files.FinishUpload(fdef, false, 0) writeResponse(decodeStoreError(err, msgID, now, nil), nil) return nil } err = stream.SendAndClose(&pbx.FileUpResp{ Id: msgID, Code: http.StatusOK, Text: http.StatusText(http.StatusOK), Meta: &pbx.FileMeta{ Name: url, MimeType: mimeType, Etag: fdef.ETag, Size: size, }, }) logs.Info.Println("media upload: ok", fdef.Id, fdef.Location, err) return err } // largeFileRunGarbageCollection runs every 'period' and deletes up to 'blockSize' unused files. // Returns channel which can be used to stop the process. func largeFileRunGarbageCollection(period time.Duration, blockSize int) chan<- bool { // Unbuffered stop channel. Whomever stops the gc must wait for the process to finish. stop := make(chan bool) go func() { // Add some randomness to the tick period to desynchronize runs on cluster nodes: // 0.75 * period + rand(0, 0.5) * period. period = (period >> 1) + (period >> 2) + time.Duration(rand.Intn(int(period>>1))) gcTicker := time.Tick(period) for { select { case <-gcTicker: if err := store.Files.DeleteUnused(time.Now().Add(-time.Hour), blockSize); err != nil { logs.Warn.Println("media gc:", err) } case <-stop: return } } }() return stop } // Authenticate non-websocket HTTP request func authFileRequest(authMethod, secret, sid, remoteAddr string) (types.Uid, []byte, error) { var uid types.Uid if authMethod != "" { decodedSecret := make([]byte, base64.StdEncoding.DecodedLen(len(secret))) n, err := base64.StdEncoding.Decode(decodedSecret, []byte(secret)) if err != nil { logs.Info.Println("media: invalid auth secret", authMethod, "'"+secret+"'") return uid, nil, types.ErrMalformed } if authhdl := store.Store.GetLogicalAuthHandler(authMethod); authhdl != nil { rec, challenge, err := authhdl.Authenticate(decodedSecret[:n], remoteAddr) if err != nil { return uid, nil, err } if challenge != nil { return uid, challenge, nil } uid = rec.Uid } else { logs.Info.Println("media: unknown auth method", authMethod) return uid, nil, types.ErrMalformed } } else { // Find the session, make sure it's appropriately authenticated. sess := globals.sessionStore.Get(sid) if sess != nil { uid = sess.uid } } return uid, nil, nil } ================================================ FILE: server/hdl_grpc.go ================================================ /****************************************************************************** * * Description : * * Handler of gRPC connections. See also hdl_websock.go for websockets and * hdl_longpoll.go for long polling. * *****************************************************************************/ package main import ( "crypto/tls" "io" "time" "github.com/tinode/chat/pbx" "github.com/tinode/chat/server/logs" "google.golang.org/grpc" "google.golang.org/grpc/credentials" "google.golang.org/grpc/keepalive" "google.golang.org/grpc/peer" ) type grpcNodeServer struct { pbx.UnimplementedNodeServer } func (sess *Session) closeGrpc() { if sess.proto == GRPC { sess.lock.Lock() sess.grpcnode = nil sess.lock.Unlock() } } // Equivalent of starting a new session and a read loop in one. func (*grpcNodeServer) MessageLoop(stream pbx.Node_MessageLoopServer) error { sess, count := globals.sessionStore.NewSession(stream, "") if p, ok := peer.FromContext(stream.Context()); ok { sess.remoteAddr = p.Addr.String() } logs.Info.Println("grpc: session started", sess.sid, sess.remoteAddr, count) defer func() { sess.closeGrpc() sess.cleanUp(false) }() go sess.writeGrpcLoop() for { in, err := stream.Recv() if err == io.EOF { return nil } if err != nil { logs.Err.Println("grpc: recv", sess.sid, err) return err } logs.Info.Println("grpc in:", truncateStringIfTooLong(in.String()), sess.sid) statsInc("IncomingMessagesGrpcTotal", 1) sess.dispatch(pbCliDeserialize(in)) sess.lock.Lock() if sess.grpcnode == nil { sess.lock.Unlock() break } sess.lock.Unlock() } return nil } func (sess *Session) sendMessageGrpc(msg any) bool { if len(sess.send) > sendQueueLimit { logs.Err.Println("grpc: outbound queue limit exceeded", sess.sid) return false } statsInc("OutgoingMessagesGrpcTotal", 1) if err := grpcWrite(sess, msg); err != nil { logs.Err.Println("grpc: write", sess.sid, err) return false } return true } func (sess *Session) writeGrpcLoop() { defer func() { sess.closeGrpc() // exit MessageLoop }() for { select { case msg, ok := <-sess.send: if !ok { // channel closed return } switch v := msg.(type) { case []*ServerComMessage: // batch of unserialized messages for _, msg := range v { w := sess.serializeAndUpdateStats(msg) if !sess.sendMessageGrpc(w) { return } } case *ServerComMessage: // single unserialized message w := sess.serializeAndUpdateStats(v) if !sess.sendMessageGrpc(w) { return } default: // serialized message if !sess.sendMessageGrpc(v) { return } } case <-sess.bkgTimer.C: if sess.background { sess.background = false sess.onBackgroundTimer() } case msg := <-sess.stop: // Shutdown requested, don't care if the message is delivered if msg != nil { grpcWrite(sess, msg) } return case topic := <-sess.detach: sess.delSub(topic) } } } func grpcWrite(sess *Session, msg any) error { if out := sess.grpcnode; out != nil { // Will panic if msg is not of *pbx.ServerMsg type. This is an intentional panic. return out.Send(msg.(*pbx.ServerMsg)) } return nil } func serveGrpc(addr string, kaEnabled bool, tlsConf *tls.Config) (*grpc.Server, error) { if addr == "" { return nil, nil } lis, err := netListener(addr) if err != nil { return nil, err } secure := "" var opts []grpc.ServerOption opts = append(opts, grpc.MaxRecvMsgSize(int(globals.maxMessageSize))) if tlsConf != nil { opts = append(opts, grpc.Creds(credentials.NewTLS(tlsConf))) secure = " secure" } if kaEnabled { kepConfig := keepalive.EnforcementPolicy{ MinTime: 1 * time.Second, // If a client pings more than once every second, terminate the connection PermitWithoutStream: true, // Allow pings even when there are no active streams } opts = append(opts, grpc.KeepaliveEnforcementPolicy(kepConfig)) kpConfig := keepalive.ServerParameters{ Time: 60 * time.Second, // Ping the client if it is idle for 60 seconds to ensure the connection is still active Timeout: 20 * time.Second, // Wait 20 second for the ping ack before assuming the connection is dead } opts = append(opts, grpc.KeepaliveParams(kpConfig)) } srv := grpc.NewServer(opts...) pbx.RegisterNodeServer(srv, &grpcNodeServer{}) logs.Info.Printf("gRPC/%s%s server is registered at [%s]", grpc.Version, secure, addr) go func() { if err := srv.Serve(lis); err != nil { logs.Err.Println("gRPC server failed:", err) } }() return srv, nil } ================================================ FILE: server/hdl_longpoll.go ================================================ /****************************************************************************** * * Description : * * Handler of long polling clients. See also hdl_websock.go for web sockets and * hdl_grpc.go for gRPC * *****************************************************************************/ package main import ( "encoding/json" "errors" "io" "net/http" "time" "github.com/tinode/chat/server/logs" ) func (sess *Session) sendMessageLp(wrt http.ResponseWriter, msg any) bool { if len(sess.send) > sendQueueLimit { logs.Err.Println("longPoll: outbound queue limit exceeded", sess.sid) return false } statsInc("OutgoingMessagesLongpollTotal", 1) if err := lpWrite(wrt, msg); err != nil { logs.Err.Println("longPoll: writeOnce failed", sess.sid, err) return false } return true } func (sess *Session) writeOnce(wrt http.ResponseWriter, req *http.Request) { for { select { case msg, ok := <-sess.send: if !ok { return } switch v := msg.(type) { case *ServerComMessage: // single unserialized message w := sess.serializeAndUpdateStats(v) if !sess.sendMessageLp(wrt, w) { return } default: // serialized message if !sess.sendMessageLp(wrt, v) { return } } return case <-sess.bkgTimer.C: if sess.background { sess.background = false sess.onBackgroundTimer() } case msg := <-sess.stop: // Request to close the session. Make it unavailable. globals.sessionStore.Delete(sess) // Don't care if lpWrite fails. if msg != nil { lpWrite(wrt, msg) } return case topic := <-sess.detach: // Request to detach the session from a topic. sess.delSub(topic) // No 'return' statement here: continue waiting case <-time.After(pingPeriod): // just write an empty packet on timeout if _, err := wrt.Write([]byte{}); err != nil { logs.Err.Println("longPoll: writeOnce: timout", sess.sid, err) } return case <-req.Context().Done(): // HTTP request canceled or connection lost. return } } } func lpWrite(wrt http.ResponseWriter, msg any) error { // This will panic if msg is not []byte. This is intentional. wrt.Write(msg.([]byte)) return nil } func (sess *Session) readOnce(wrt http.ResponseWriter, req *http.Request) (int, error) { if req.ContentLength > globals.maxMessageSize { return http.StatusExpectationFailed, errors.New("request too large") } req.Body = http.MaxBytesReader(wrt, req.Body, globals.maxMessageSize) raw, err := io.ReadAll(req.Body) if err == nil { // Locking-unlocking is needed because the client may issue multiple requests in parallel. // Should not affect performance sess.lock.Lock() statsInc("IncomingMessagesLongpollTotal", 1) sess.dispatchRaw(raw) sess.lock.Unlock() return 0, nil } return 0, err } // serveLongPoll handles long poll connections when WebSocket is not available // Connection could be without sid or with sid: // - if sid is empty, create session, expect a login in the same request, respond and close // - if sid is not empty and there is an initialized session, payload is optional // - if no payload, perform long poll // - if payload exists, process it and close // - if sid is not empty but there is no session, report an error func serveLongPoll(wrt http.ResponseWriter, req *http.Request) { now := time.Now().UTC().Round(time.Millisecond) // Use the lowest common denominator - this is a legacy handler after all (otherwise would use application/json) wrt.Header().Set("Content-Type", "text/plain") if globals.tlsStrictMaxAge != "" { wrt.Header().Set("Strict-Transport-Security", "max-age"+globals.tlsStrictMaxAge) } enc := json.NewEncoder(wrt) if isValid, _ := checkAPIKey(getAPIKey(req)); !isValid { wrt.WriteHeader(http.StatusForbidden) enc.Encode(ErrAPIKeyRequired(now)) return } // TODO(gene): should it be configurable? // Currently any domain is allowed to get data from the chat server wrt.Header().Set("Access-Control-Allow-Origin", "*") // Ensure the response is not cached if req.ProtoAtLeast(1, 1) { wrt.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") // HTTP 1.1 } else { wrt.Header().Set("Pragma", "no-cache") // HTTP 1.0 } wrt.Header().Set("Expires", "0") // Proxies // TODO(gene): respond differently to valious HTTP methods // Get session id sid := req.FormValue("sid") var sess *Session if sid == "" { // New session var count int sess, count = globals.sessionStore.NewSession(wrt, "") sess.remoteAddr = getRemoteAddr(req) logs.Info.Println("longPoll: session started", sess.sid, sess.remoteAddr, count) wrt.WriteHeader(http.StatusCreated) pkt := NoErrCreated(req.FormValue("id"), "", now) pkt.Ctrl.Params = map[string]string{ "sid": sess.sid, } enc.Encode(pkt) return } // Existing session sess = globals.sessionStore.Get(sid) if sess == nil { logs.Warn.Println("longPoll: invalid or expired session id", sid) wrt.WriteHeader(http.StatusForbidden) enc.Encode(ErrSessionNotFound(now)) return } if addr := getRemoteAddr(req); sess.remoteAddr != addr { sess.remoteAddr = addr logs.Warn.Println("longPoll: remote address changed", sid, addr) } if req.ContentLength != 0 { // Read payload and send it for processing. if code, err := sess.readOnce(wrt, req); err != nil { logs.Warn.Println("longPoll: readOnce failed", sess.sid, err) // Failed to read request, report an error, if possible if code != 0 { wrt.WriteHeader(code) } else { wrt.WriteHeader(http.StatusBadRequest) } enc.Encode(ErrMalformed(req.FormValue("id"), "", now)) } return } sess.writeOnce(wrt, req) } ================================================ FILE: server/hdl_websock.go ================================================ /****************************************************************************** * * Description : * * Handler of websocket connections. See also hdl_longpoll.go for long polling * and hdl_grpc.go for gRPC. * *****************************************************************************/ package main import ( "encoding/json" "net/http" "time" "github.com/gorilla/websocket" "github.com/tinode/chat/server/logs" "github.com/tinode/chat/server/store/types" ) const ( // Time allowed to write a message to the peer. writeWait = 10 * time.Second // Time allowed to read the next pong message from the peer. pongWait = idleSessionTimeout // Send pings to peer with this period. Must be less than pongWait. pingPeriod = (pongWait * 9) / 10 ) func (sess *Session) closeWS() { if sess.proto == WEBSOCK { sess.ws.Close() } } func (sess *Session) readLoop() { defer func() { sess.closeWS() sess.cleanUp(false) }() sess.ws.SetReadLimit(globals.maxMessageSize) sess.ws.SetReadDeadline(time.Now().Add(pongWait)) sess.ws.SetPongHandler(func(string) error { sess.ws.SetReadDeadline(time.Now().Add(pongWait)) return nil }) for { // Read a ClientComMessage _, raw, err := sess.ws.ReadMessage() if err != nil { if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure, websocket.CloseNormalClosure) { logs.Err.Println("ws: readLoop", sess.sid, err) } return } statsInc("IncomingMessagesWebsockTotal", 1) sess.dispatchRaw(raw) } } func (sess *Session) sendMessage(msg any) bool { if len(sess.send) > sendQueueLimit { logs.Err.Println("ws: outbound queue limit exceeded", sess.sid) return false } statsInc("OutgoingMessagesWebsockTotal", 1) if err := wsWrite(sess.ws, websocket.TextMessage, msg); err != nil { if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure, websocket.CloseNormalClosure) { logs.Err.Println("ws: writeLoop", sess.sid, err) } return false } return true } func (sess *Session) writeLoop() { ticker := time.NewTicker(pingPeriod) defer func() { ticker.Stop() // Break readLoop. sess.closeWS() }() for { select { case msg, ok := <-sess.send: if !ok { // Channel closed. return } switch v := msg.(type) { case []*ServerComMessage: // batch of unserialized messages for _, msg := range v { w := sess.serializeAndUpdateStats(msg) if !sess.sendMessage(w) { return } } case *ServerComMessage: // single unserialized message w := sess.serializeAndUpdateStats(v) if !sess.sendMessage(w) { return } default: // serialized message if !sess.sendMessage(v) { return } } case <-sess.bkgTimer.C: if sess.background { sess.background = false sess.onBackgroundTimer() } case msg := <-sess.stop: // Shutdown requested, don't care if the message is delivered if msg != nil { wsWrite(sess.ws, websocket.TextMessage, msg) } return case topic := <-sess.detach: sess.delSub(topic) case <-ticker.C: if err := wsWrite(sess.ws, websocket.PingMessage, nil); err != nil { if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure, websocket.CloseNormalClosure) { logs.Err.Println("ws: writeLoop ping", sess.sid, err) } return } } } } // Writes a message with the given message type (mt) and payload. func wsWrite(ws *websocket.Conn, mt int, msg any) error { var bits []byte if msg != nil { bits = msg.([]byte) } else { bits = []byte{} } ws.SetWriteDeadline(time.Now().Add(writeWait)) return ws.WriteMessage(mt, bits) } // Handles websocket requests from peers. var upgrader = websocket.Upgrader{ ReadBufferSize: 1024, WriteBufferSize: 1024, EnableCompression: globals.wsCompression, // Allow connections from any Origin CheckOrigin: func(r *http.Request) bool { return true }, } func serveWebSocket(wrt http.ResponseWriter, req *http.Request) { now := types.TimeNow() if isValid, _ := checkAPIKey(getAPIKey(req)); !isValid { wrt.WriteHeader(http.StatusForbidden) json.NewEncoder(wrt).Encode(ErrAPIKeyRequired(now)) logs.Err.Println("ws: Missing, invalid or expired API key") return } if req.Method != http.MethodGet { wrt.WriteHeader(http.StatusMethodNotAllowed) json.NewEncoder(wrt).Encode(ErrOperationNotAllowed("", "", now)) logs.Err.Println("ws: Invalid HTTP method", req.Method) return } ws, err := upgrader.Upgrade(wrt, req, nil) if _, ok := err.(websocket.HandshakeError); ok { logs.Err.Println("ws: Not a websocket handshake") return } else if err != nil { logs.Err.Println("ws: failed to Upgrade ", err) return } sess, count := globals.sessionStore.NewSession(ws, "") if globals.useXForwardedFor { sess.remoteAddr = req.Header.Get("X-Forwarded-For") if !isRoutableIP(sess.remoteAddr) { sess.remoteAddr = "" } } if sess.remoteAddr == "" { sess.remoteAddr = req.RemoteAddr } logs.Info.Println("ws: session started", sess.sid, sess.remoteAddr, count) // Do work in goroutines to return from serveWebSocket() to release file pointers. // Otherwise "too many open files" will happen. go sess.writeLoop() go sess.readLoop() } ================================================ FILE: server/http.go ================================================ /****************************************************************************** * * Description : * * Web server initialization and shutdown. * *****************************************************************************/ package main import ( "context" "crypto/tls" "encoding/json" "errors" "net" "net/http" "net/url" "os" "os/signal" "sort" "strconv" "strings" "syscall" "time" "github.com/tinode/chat/server/logs" "github.com/tinode/chat/server/store/types" ) func listenAndServe(addr string, mux *http.ServeMux, tlfConf *tls.Config, stop <-chan bool) error { globals.shuttingDown = false httpdone := make(chan bool) server := &http.Server{ Handler: mux, ReadHeaderTimeout: 10 * time.Second, IdleTimeout: 30 * time.Second, WriteTimeout: 90 * time.Second, MaxHeaderBytes: 1 << 14, } server.TLSConfig = tlfConf go func() { var err error if server.TLSConfig != nil { // If port is not specified, use default https port (443), // otherwise it will default to 80 if addr == "" { addr = ":https" } if globals.tlsRedirectHTTP != "" { // Serving redirects from a unix socket or to a unix socket makes no sense. if isUnixAddr(globals.tlsRedirectHTTP) || isUnixAddr(addr) { err = errors.New("HTTP to HTTPS redirect: unix sockets not supported") } else { logs.Info.Printf("Redirecting connections from HTTP at [%s] to HTTPS at [%s]", globals.tlsRedirectHTTP, addr) // This is a second HTTP server listenning on a different port. go func() { if err := http.ListenAndServe(globals.tlsRedirectHTTP, tlsRedirect(addr)); err != nil && err != http.ErrServerClosed { logs.Info.Println("HTTP redirect failed:", err) } }() } } if err == nil { logs.Info.Printf("Listening for client HTTPS connections on [%s]", addr) var lis net.Listener lis, err = netListener(addr) if err == nil { err = server.ServeTLS(lis, "", "") } } } else { logs.Info.Printf("Listening for client HTTP connections on [%s]", addr) var lis net.Listener lis, err = netListener(addr) if err == nil { err = server.Serve(lis) } } if err != nil { if globals.shuttingDown { logs.Info.Println("HTTP server: stopped") } else { logs.Err.Println("HTTP server: failed", err) } } httpdone <- true }() // Wait for either a termination signal or an error Loop: for { select { case <-stop: // Flip the flag that we are terminating and close the Accept-ing socket, so no new connections are possible. globals.shuttingDown = true // Give server 2 seconds to shut down. ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) if err := server.Shutdown(ctx); err != nil { // failure/timeout shutting down the server gracefully logs.Err.Println("HTTP server failed to terminate gracefully", err) } // While the server shuts down, termianate all sessions. globals.sessionStore.Shutdown() // Wait for http server to stop Accept()-ing connections. <-httpdone cancel() // Shutdown local cluster node, if it's a part of a cluster. globals.cluster.shutdown() // Terminate plugin connections. pluginsShutdown() // Shutdown gRPC server, if one is configured. if globals.grpcServer != nil { // GracefulStop does not terminate ServerStream. Must use Stop(). globals.grpcServer.Stop() } // Stop publishing statistics. statsShutdown() // Shutdown the hub. The hub will shutdown topics. hubdone := make(chan bool) globals.hub.shutdown <- hubdone // Wait for the hub to finish. <-hubdone // Stop updating users cache usersShutdown() break Loop case <-httpdone: break Loop } } return nil } func signalHandler() <-chan bool { stop := make(chan bool) signchan := make(chan os.Signal, 1) signal.Notify(signchan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP) go func() { // Wait for a signal. Don't care which signal it is sig := <-signchan logs.Info.Printf("Signal received: '%s', shutting down", sig) stop <- true }() return stop } // The following code is used to intercept HTTP errors so they can be wrapped into json. // Wrapper around http.ResponseWriter which detects status set to 400+ and replaces // default error message with a custom one. type errorResponseWriter struct { status int http.ResponseWriter } func (w *errorResponseWriter) WriteHeader(status int) { if status >= http.StatusBadRequest { // charset=utf-8 is the default. No need to write it explicitly // Must set all the headers before calling super.WriteHeader() w.ResponseWriter.Header().Set("Content-Type", "application/json") } w.status = status w.ResponseWriter.WriteHeader(status) } func (w *errorResponseWriter) Write(p []byte) (n int, err error) { if w.status >= http.StatusBadRequest { p, _ = json.Marshal( &ServerComMessage{ Ctrl: &MsgServerCtrl{ Timestamp: time.Now().UTC().Round(time.Millisecond), Code: w.status, Text: http.StatusText(w.status), }, }) } return w.ResponseWriter.Write(p) } // httpErrorHandler to respond with JSON_formatted error message for static content. func httpErrorHandler(h http.Handler) http.Handler { return http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { h.ServeHTTP(&errorResponseWriter{0, w}, r) }) } // Custom 404 response. func serve404(wrt http.ResponseWriter, req *http.Request) { wrt.Header().Set("Content-Type", "application/json; charset=utf-8") wrt.WriteHeader(http.StatusNotFound) json.NewEncoder(wrt).Encode( &ServerComMessage{ Ctrl: &MsgServerCtrl{ Timestamp: time.Now().UTC().Round(time.Millisecond), Code: http.StatusNotFound, Text: "not found", }, }) } // Redirect HTTP requests to HTTPS func tlsRedirect(toPort string) http.HandlerFunc { if toPort == ":443" || toPort == ":https" { toPort = "" } else if toPort != "" && toPort[:1] == ":" { // Strip leading colon. JoinHostPort will add it back. toPort = toPort[1:] } return func(wrt http.ResponseWriter, req *http.Request) { host, _, err := net.SplitHostPort(req.Host) if err != nil { // If SplitHostPort has failed assume it's because :port part is missing. host = req.Host } target, _ := url.ParseRequestURI(req.RequestURI) target.Scheme = "https" // Ensure valid redirect target. if toPort != "" { // Replace the port number. target.Host = net.JoinHostPort(host, toPort) } else { target.Host = host } if target.Path == "" { target.Path = "/" } http.Redirect(wrt, req, target.String(), http.StatusTemporaryRedirect) } } // Wrapper for adding optional HTTP headers: // - Strict-Transport-Security // - X-Frame-Options // - Referrer-Policy func optionalHttpHeaders(handler http.Handler) http.Handler { h1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Referrer-Policy", "origin") handler.ServeHTTP(w, r) }) h2 := h1 if globals.tlsStrictMaxAge != "" { h2 = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Strict-Transport-Security", "max-age="+globals.tlsStrictMaxAge) h1.ServeHTTP(w, r) }) } h3 := h2 if globals.xFrameOptions != "-" { h3 = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("X-Frame-Options", globals.xFrameOptions) h2.ServeHTTP(w, r) }) } return h3 } // Wrapper for http.Handler which optionally adds a Cache-Control header to the response func cacheControlHandler(maxAge int, handler http.Handler) http.Handler { if maxAge > 0 { strMaxAge := strconv.Itoa(maxAge) return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Cache-Control", "must-revalidate, public, max-age="+strMaxAge) handler.ServeHTTP(w, r) }) } return handler } // Get API key from an HTTP request. func getAPIKey(req *http.Request) string { // Check header. apikey := req.Header.Get("X-Tinode-APIKey") if apikey != "" { return apikey } // Check URL query parameters. apikey = req.URL.Query().Get("apikey") if apikey != "" { return apikey } // Check form values. apikey = req.FormValue("apikey") if apikey != "" { return apikey } // Check cookies. if c, err := req.Cookie("apikey"); err == nil { apikey = c.Value } return apikey } // Extracts authorization credentials from an HTTP request. // Returns authentication method and secret. func getHttpAuth(req *http.Request) (method, secret string) { // Check X-Tinode-Auth header. if parts := strings.Split(req.Header.Get("X-Tinode-Auth"), " "); len(parts) == 2 { method, secret = parts[0], parts[1] return } // Check canonical Authorization header. if parts := strings.Split(req.Header.Get("Authorization"), " "); len(parts) == 2 { method, secret = parts[0], parts[1] return } // Check URL query parameters. if method = req.URL.Query().Get("auth"); method != "" { // Get the auth secret. secret = req.URL.Query().Get("secret") // Convert base64 URL-encoding to standard encoding. secret = strings.NewReplacer("-", "+", "_", "/").Replace(secret) return } // Check form values. if method = req.FormValue("auth"); method != "" { return method, req.FormValue("secret") } // Check cookies as the last resort. if mcookie, err := req.Cookie("auth"); err == nil { if scookie, err := req.Cookie("secret"); err == nil { method, secret = mcookie.Value, scookie.Value } } return } // Obtain IP address of the client. func getRemoteAddr(req *http.Request) string { var addr string if globals.useXForwardedFor { addr = req.Header.Get("X-Forwarded-For") if !isRoutableIP(addr) { addr = "" } } if addr != "" { return addr } return req.RemoteAddr } // debugSession is session debug info. type debugSession struct { RemoteAddr string `json:"remote_addr,omitempty"` Ua string `json:"ua,omitempty"` Uid string `json:"uid,omitempty"` Sid string `json:"sid,omitempty"` Clnode string `json:"clnode,omitempty"` Subs []string `json:"subs,omitempty"` } // debugTopic is a topic debug info. type debugTopic struct { Topic string `json:"topic,omitempty"` Xorig string `json:"xorig,omitempty"` IsProxy bool `json:"is_proxy,omitempty"` PerUser []string `json:"per_user,omitempty"` PerSubs []string `json:"per_subs,omitempty"` Sessions []string `json:"sessions,omitempty"` } // debugCachedUser is a user cache entry debug info. type debugCachedUser struct { Uid string `json:"uid,omitempty"` Unread int `json:"unread,omitempty"` Topics int `json:"topics,omitempty"` } // debugDump is server internal state dump for debugging. type debugDump struct { Version string `json:"server_version,omitempty"` Build string `json:"build_id,omitempty"` Timestamp time.Time `json:"ts,omitempty"` Sessions []debugSession `json:"sessions,omitempty"` Topics []debugTopic `json:"topics,omitempty"` UserCache []debugCachedUser `json:"user_cache,omitempty"` } func serveStatus(wrt http.ResponseWriter, req *http.Request) { wrt.Header().Set("Content-Type", "application/json") result := &debugDump{ Version: currentVersion, Build: buildstamp, Timestamp: types.TimeNow(), Sessions: make([]debugSession, 0, len(globals.sessionStore.sessCache)), Topics: make([]debugTopic, 0, 10), UserCache: make([]debugCachedUser, 0, 10), } // Sessions. globals.sessionStore.Range(func(sid string, s *Session) bool { keys := make([]string, 0, len(s.subs)) for tn := range s.subs { keys = append(keys, tn) } sort.Strings(keys) var clnode string if s.clnode != nil { clnode = s.clnode.name } result.Sessions = append(result.Sessions, debugSession{ RemoteAddr: s.remoteAddr, Ua: s.userAgent, Uid: s.uid.String(), Sid: sid, Clnode: clnode, Subs: keys, }) return true }) // Topics. globals.hub.topics.Range(func(_, t any) bool { topic := t.(*Topic) psd := make([]string, 0, len(topic.sessions)) for s := range topic.sessions { psd = append(psd, s.sid) } pud := make([]string, 0, len(topic.perUser)) for uid := range topic.perUser { pud = append(pud, uid.String()) } ps := make([]string, 0, len(topic.perSubs)) for key := range topic.perSubs { ps = append(ps, key) } result.Topics = append(result.Topics, debugTopic{ Topic: topic.name, Xorig: topic.xoriginal, IsProxy: topic.isProxy, PerUser: pud, PerSubs: ps, Sessions: psd, }) return true }) for k, v := range usersCache { result.UserCache = append(result.UserCache, debugCachedUser{ Uid: k.UserId(), Unread: v.unread, Topics: v.topics, }) } json.NewEncoder(wrt).Encode(result) } ================================================ FILE: server/http_pprof.go ================================================ // Debug tooling. Dumps named profile in response to HTTP request at // http(s)://// // See godoc for the list of possible profile names: https://golang.org/pkg/runtime/pprof/#Profile package main import ( "fmt" "net/http" "path" "runtime/pprof" "strings" "github.com/tinode/chat/server/logs" ) var pprofHttpRoot string // Expose debug profiling at the given URL path. func servePprof(mux *http.ServeMux, serveAt string) { if serveAt == "" || serveAt == "-" { return } pprofHttpRoot = path.Clean("/"+serveAt) + "/" mux.HandleFunc(pprofHttpRoot, profileHandler) logs.Info.Printf("pprof: profiling info exposed at '%s'", pprofHttpRoot) } func profileHandler(wrt http.ResponseWriter, req *http.Request) { wrt.Header().Set("X-Content-Type-Options", "nosniff") wrt.Header().Set("Content-Type", "text/plain; charset=utf-8") profileName := strings.TrimPrefix(req.URL.Path, pprofHttpRoot) profile := pprof.Lookup(profileName) if profile == nil { servePprofError(wrt, http.StatusNotFound, "Unknown profile '"+profileName+"'") return } // Respond with the requested profile. profile.WriteTo(wrt, 2) } func servePprofError(wrt http.ResponseWriter, status int, txt string) { wrt.Header().Set("Content-Type", "text/plain; charset=utf-8") wrt.Header().Set("X-Go-Pprof", "1") wrt.Header().Del("Content-Disposition") wrt.WriteHeader(status) fmt.Fprintln(wrt, txt) } ================================================ FILE: server/hub.go ================================================ /****************************************************************************** * * Description : * * Main hub for processing events such as creating/tearing down topics, * routing messages between topics. * *****************************************************************************/ package main import ( "strings" "sync" "time" "github.com/tinode/chat/server/auth" "github.com/tinode/chat/server/logs" "github.com/tinode/chat/server/store" "github.com/tinode/chat/server/store/types" ) // RequestLatencyDistribution is an array of request latency distribution bounds (in milliseconds). // "var" because Go does not support array constants. var requestLatencyDistribution = []float64{1, 2, 3, 4, 5, 6, 8, 10, 13, 16, 20, 25, 30, 40, 50, 65, 80, 100, 130, 160, 200, 250, 300, 400, 500, 650, 800, 1000, 2000, 5000, 10000, 20000, 50000, 100000} // OutgoingMessageSizeDistribution is an array of outgoing message size distribution bounds (in bytes). var outgoingMessageSizeDistribution = []float64{1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 16384, 65536, 262144, 1048576, 4194304, 16777216, 67108864, 268435456, 1073741824, 4294967296} // Request to hub to remove the topic type topicUnreg struct { // Original request, could be nil. pkt *ClientComMessage // Session making the request, could be nil. sess *Session // Routable name of the topic to drop. Duplicated here because pkt could be nil. rcptTo string // UID of the user being deleted. Duplicated here because pkt could be nil. forUser types.Uid // Unregister then delete the topic. del bool // Channel for reporting operation completion when deleting topics for a user. done chan<- bool } type userStatusReq struct { // UID of the user being affected. forUser types.Uid // New topic state value. Only types.StateSuspended is supported at this time. state types.ObjState } // Hub is the core structure which holds topics. type Hub struct { // Topics must be indexed by name topics *sync.Map // Current number of loaded topics numTopics int // Channel for routing client-side messages, buffered at 4096 routeCli chan *ClientComMessage // Process get.info requests for topic not subscribed to, buffered 128. meta chan *ClientComMessage // Channel for routing server-generated messages, buffered at 4096 routeSrv chan *ServerComMessage // subscribe session to topic, possibly creating a new topic, buffered at 256 join chan *ClientComMessage // Remove topic from hub, possibly deleting it afterwards, buffered at 32 unreg chan *topicUnreg // Channel for suspending/resuming users, buffered 128. userStatus chan *userStatusReq // Cluster request to rehash topics, unbuffered rehash chan bool // Request to shutdown, unbuffered shutdown chan chan<- bool } func (h *Hub) topicGet(name string) *Topic { if t, ok := h.topics.Load(name); ok { return t.(*Topic) } return nil } func (h *Hub) topicPut(name string, t *Topic) { h.numTopics++ h.topics.Store(name, t) } func (h *Hub) topicDel(name string) { h.numTopics-- h.topics.Delete(name) } func newHub() *Hub { h := &Hub{ topics: &sync.Map{}, // TODO: verify if these channels have to be buffered. routeCli: make(chan *ClientComMessage, 4096), routeSrv: make(chan *ServerComMessage, 4096), join: make(chan *ClientComMessage, 256), unreg: make(chan *topicUnreg, 256), rehash: make(chan bool), meta: make(chan *ClientComMessage, 128), userStatus: make(chan *userStatusReq, 128), shutdown: make(chan chan<- bool), } statsRegisterInt("LiveTopics") statsRegisterInt("TotalTopics") statsRegisterInt("IncomingMessagesWebsockTotal") statsRegisterInt("OutgoingMessagesWebsockTotal") statsRegisterInt("IncomingMessagesLongpollTotal") statsRegisterInt("OutgoingMessagesLongpollTotal") statsRegisterInt("IncomingMessagesGrpcTotal") statsRegisterInt("OutgoingMessagesGrpcTotal") statsRegisterInt("FileDownloadsTotal") statsRegisterInt("FileUploadsTotal") statsRegisterInt("CtrlCodesTotal2xx") statsRegisterInt("CtrlCodesTotal3xx") statsRegisterInt("CtrlCodesTotal4xx") statsRegisterInt("CtrlCodesTotal5xx") statsRegisterHistogram("RequestLatency", requestLatencyDistribution) statsRegisterHistogram("OutgoingMessageSize", outgoingMessageSizeDistribution) go h.run() // Initialize 'sys' topic. It will be initialized either as master or proxy. h.join <- &ClientComMessage{RcptTo: "sys", Original: "sys"} return h } func (h *Hub) run() { for { select { case join := <-h.join: // Handle a subscription request: // 1. Init topic // 1.1 If a new topic is requested, create it // 1.2 If a new subscription to an existing topic is requested: // 1.2.1 check if topic is already loaded // 1.2.2 if not, load it // 1.2.3 if it cannot be loaded (not found), fail // 2. Check access rights and reject, if appropriate // 3. Attach session to the topic // Is the topic already loaded? t := h.topicGet(join.RcptTo) if t == nil { // Topic does not exist or not loaded. t = &Topic{ name: join.RcptTo, xoriginal: join.Original, // Indicates a proxy topic. isProxy: globals.cluster.isRemoteTopic(join.RcptTo), sessions: make(map[*Session]perSessionData), clientMsg: make(chan *ClientComMessage, 192), serverMsg: make(chan *ServerComMessage, 64), reg: make(chan *ClientComMessage, 256), unreg: make(chan *ClientComMessage, 256), meta: make(chan *ClientComMessage, 64), perUser: make(map[types.Uid]perUserData), exit: make(chan *shutDown, 1), } if globals.cluster != nil { if t.isProxy { t.proxy = make(chan *ClusterResp, 128) t.masterNode = globals.cluster.ring.Get(t.name) } else { // It's a master topic. Make a channel for handling // direct messages from the proxy. t.master = make(chan *ClusterSessUpdate, 8) } } // Topic is created in suspended state because it's not yet configured. t.markPaused(true) // Save topic now to prevent race condition. h.topicPut(join.RcptTo, t) // Configure the topic. go topicInit(t, join, h) } else { // Topic found. if t.isInactive() { // Topic is either not ready or being deleted. if join.sess.inflightReqs != nil { join.sess.inflightReqs.Done() } join.sess.queueOut(ErrLockedReply(join, join.Timestamp)) continue } // Topic will check access rights and send appropriate {ctrl} select { case t.reg <- join: default: if join.sess.inflightReqs != nil { join.sess.inflightReqs.Done() } join.sess.queueOut(ErrServiceUnavailableReply(join, join.Timestamp)) logs.Err.Println("hub.join loop: topic's reg queue full", join.RcptTo, join.sess.sid, " - total queue len:", len(t.reg)) } } case msg := <-h.routeCli: // This is a message from a session not subscribed to topic // Route incoming message to topic if topic permits such routing. if dst := h.topicGet(msg.RcptTo); dst != nil { // Everything is OK, sending packet to known topic if dst.clientMsg != nil { select { case dst.clientMsg <- msg: default: logs.Err.Println("hub: topic's broadcast queue is full", dst.name) } } else { logs.Warn.Println("hub: invalid topic category for broadcast", dst.name) } } else if msg.Note == nil { // Topic is unknown or offline. // Note is silently ignored, all other messages are reported as accepted to prevent // clients from guessing valid topic names. // TODO(gene): validate topic name, discarding invalid topics. logs.Info.Printf("Hub. Topic[%s] is unknown or offline", msg.RcptTo) msg.sess.queueOut(NoErrAcceptedExplicitTs(msg.Id, msg.RcptTo, types.TimeNow(), msg.Timestamp)) } case msg := <-h.routeSrv: // This is a server message from a connection not subscribed to topic // Route incoming message to topic if topic permits such routing. if dst := h.topicGet(msg.RcptTo); dst != nil { // Everything is OK, sending packet to known topic select { case dst.serverMsg <- msg: default: logs.Err.Println("hub: topic's broadcast queue is full", dst.name) } } else if (strings.HasPrefix(msg.RcptTo, "usr") || strings.HasPrefix(msg.RcptTo, "grp")) && globals.cluster.isRemoteTopic(msg.RcptTo) { // It is a remote topic. if err := globals.cluster.routeToTopicIntraCluster(msg.RcptTo, msg, msg.sess); err != nil { logs.Warn.Printf("hub: routing to '%s' failed", msg.RcptTo) } } case msg := <-h.meta: // Metadata read or update from a user who is not attached to the topic. if msg.Get != nil { if msg.MetaWhat == constMsgMetaDesc { go replyOfflineTopicGetDesc(msg.sess, msg) } else { go replyOfflineTopicGetSub(msg.sess, msg) } } else if msg.Set != nil { go replyOfflineTopicSetSub(msg.sess, msg) } case status := <-h.userStatus: // Suspend/activate user's topics. go h.topicsStateForUser(status.forUser, status.state == types.StateSuspended) case unreg := <-h.unreg: reason := StopNone if unreg.del { reason = StopDeleted } if unreg.forUser.IsZero() { // The topic is being garbage collected or deleted. if err := h.topicUnreg(unreg.sess, unreg.rcptTo, unreg.pkt, reason); err != nil { logs.Err.Println("hub.topicUnreg failed:", err) } } else { // User is being deleted. go h.stopTopicsForUser(unreg.forUser, reason, unreg.done) } case <-h.rehash: // Cluster rehashing. Some previously local topics became remote, // and the other way round. // Such topics must be shut down at this node. h.topics.Range(func(_, t any) bool { topic := t.(*Topic) // Handle two cases: // 1. Master topic has moved out to another node. // 2. Proxy topic is running on a new master node // (i.e. the master topic has moved to this node). if topic.isProxy != globals.cluster.isRemoteTopic(topic.name) { h.topicUnreg(nil, topic.name, nil, StopRehashing) } return true }) // Check if 'sys' topic has migrated to this node. if h.topicGet("sys") == nil && !globals.cluster.isRemoteTopic("sys") { // Yes, 'sys' has migrated here. Initialize it. // The h.join is unbuffered. We must call from another goroutine. Otherwise deadlock. go func() { h.join <- &ClientComMessage{RcptTo: "sys", Original: "sys"} }() } case hubdone := <-h.shutdown: // start cleanup process topicsdone := make(chan bool) topicCount := 0 h.topics.Range(func(_, topic any) bool { topic.(*Topic).exit <- &shutDown{done: topicsdone} topicCount++ return true }) for range topicCount { <-topicsdone } logs.Info.Printf("Hub shutdown completed with %d topics", topicCount) // let the main goroutine know we are done with the cleanup hubdone <- true return case <-time.After(idleSessionTimeout): } } } // Update state of all topics associated with the given user: // * all p2p topics with the given user // * group topics where the given user is the owner. // 'me' and fnd' are ignored here because they are direcly tied to the user object. func (h *Hub) topicsStateForUser(uid types.Uid, suspended bool) { h.topics.Range(func(name any, t any) bool { topic := t.(*Topic) if topic.cat == types.TopicCatMe || topic.cat == types.TopicCatFnd { return true } if _, isMember := topic.perUser[uid]; (topic.cat == types.TopicCatP2P && isMember) || topic.owner == uid { topic.markReadOnly(suspended) // Don't send "off" notification on suspension. They will be sent when the user is evicted. } return true }) } // topicUnreg deletes or unregisters the topic: // // Cases: // 1. Topic being deleted // 1.1 Topic is online // 1.1.1 If the requester is the owner or if it's the last sub in a p2p topic (p2p may be sent internally when the last user unsubscribes): // 1.1.1.1 Tell topic to stop accepting requests. // 1.1.1.2 Hub deletes the topic from database // 1.1.1.3 Hub unregisters the topic // 1.1.1.4 Hub informs the origin of success or failure // 1.1.1.5 Hub forwards request to topic // 1.1.1.6 Topic evicts all sessions // 1.1.1.7 Topic exits the run() loop // 1.1.2 If the requester is not the owner // 1.1.2.1 Send it to topic to be treated like {leave unsub=true} // // 1.2 Topic is offline // 1.2.1 If requester is the owner // 1.2.1.1 Hub deletes topic from database // 1.2.2 If not the owner // 1.2.2.1 Delete subscription from DB // 1.2.3 Hub informs the origin of success or failure // 1.2.4 Send notification to subscribers that the topic was deleted // 2. Topic is just being unregistered (topic is going offline) // 2.1 Unregister it with no further action func (h *Hub) topicUnreg(sess *Session, topic string, msg *ClientComMessage, reason int) error { now := types.TimeNow() // TODO: when channel is deleted unsubscribe all devices from channel's FCM topic. if reason == StopDeleted { var asUid types.Uid if msg != nil { asUid = types.ParseUserId(msg.AsUser) } // Case 1 (unregister and delete) if t := h.topicGet(topic); t != nil { // Case 1.1: topic is online if (!asUid.IsZero() && t.owner == asUid) || (t.cat == types.TopicCatP2P && t.subsCount() < 2) { // Case 1.1.1: requester is the owner or last sub in a p2p topic t.markPaused(true) hard := true if msg != nil && msg.Del != nil { // Soft-deleting does not make sense for p2p topics. hard = msg.Del.Hard || t.cat == types.TopicCatP2P } if err := store.Topics.Delete(topic, t.isChan, hard); err != nil { t.markPaused(false) if sess != nil { sess.queueOut(ErrUnknownReply(msg, now)) } return err } if sess != nil { sess.queueOut(NoErrReply(msg, now)) } if t.isChan { // Notify channel subscribers that the channel is deleted. sendPush(pushForChanDelete(t.name, now)) } h.topicDel(topic) t.markDeleted() t.exit <- &shutDown{reason: StopDeleted} statsInc("LiveTopics", -1) } else { // Case 1.1.2: requester is NOT the owner or not empty P2P. msg.MetaWhat = constMsgDelTopic msg.sess = sess t.meta <- msg } } else { // Case 1.2: topic is offline. // Is user a channel subscriber? Use chnABC instead of grpABC and get only this user's subscription. var opts *types.QueryOpt if types.IsChannel(msg.Original) { topic = msg.Original opts = &types.QueryOpt{User: asUid} } // Get all subscribers of non-channel topics: we need to know how many are left and notify them. // Get only one subscription for channel users. subs, err := store.Topics.GetSubs(topic, opts) if err != nil { sess.queueOut(ErrUnknownReply(msg, now)) return err } tcat := topicCat(topic) if len(subs) == 0 { if tcat == types.TopicCatP2P { // No subscribers: delete. store.Topics.Delete(topic, false, true) } sess.queueOut(InfoNoActionReply(msg, now)) return nil } // Find subscription of the current user. var sub *types.Subscription user := asUid.String() for i := range subs { if subs[i].User == user { sub = &subs[i] break } } if sub == nil { // If user has no subscription, tell him all is fine sess.queueOut(InfoNoActionReply(msg, now)) return nil } if !(sub.ModeGiven & sub.ModeWant).IsOwner() { // Case 1.2.2.1 Not the owner, but possibly last subscription in a P2P topic. if tcat == types.TopicCatP2P && len(subs) < 2 { // This is a P2P topic and fewer than 2 subscriptions, delete the entire topic if err := store.Topics.Delete(topic, false, msg.Del.Hard); err != nil { sess.queueOut(ErrUnknownReply(msg, now)) return err } // Inform plugin that the topic was deleted. pluginTopic(&Topic{name: topic}, plgActDel) } else if err := store.Subs.Delete(topic, asUid); err != nil { // Not P2P or more than 1 subscription left. // Delete user's own subscription only if err == types.ErrNotFound { sess.queueOut(InfoNoActionReply(msg, now)) err = nil } else { sess.queueOut(ErrUnknownReply(msg, now)) } return err } // Notify user's other sessions that the subscription is gone presSingleUserOfflineOffline(asUid, msg.Original, "gone", nilPresParams, sess.sid) if tcat == types.TopicCatP2P && len(subs) == 2 { uname1 := asUid.UserId() uid2 := types.ParseUserId(msg.Original) // Tell user1 to stop sending updates to user2 without passing change to user1's sessions. presSingleUserOfflineOffline(asUid, uid2.UserId(), "?none+rem", nilPresParams, "") // Don't change the online status of user1, just ask user2 to stop notification exchange. // Tell user2 that user1 is offline but let him keep sending updates in case user1 resubscribes. presSingleUserOfflineOffline(uid2, uname1, "off", nilPresParams, "") } // Inform plugin that the subscription was deleted. pluginSubscription(sub, plgActDel) } else { // Case 1.2.1.1: owner, delete the group topic from db. Only group topics have owners. // We don't know if the group topic is a channel, but cleaning it as a channel does no harm // other than a small performance penalty. if err := store.Topics.Delete(topic, true, msg.Del.Hard); err != nil { sess.queueOut(ErrUnknownReply(msg, now)) return err } // Notify subscribers that the group topic is gone. presSubsOfflineOffline(topic, tcat, subs, "gone", &presParams{}, sess.sid) // Notify channel subscribers that the channel is deleted. // The push will not be delivered to anybody if the topic is not a channel. sendPush(pushForChanDelete(topic, now)) // Inform plugin that the topic was deleted. pluginTopic(&Topic{name: topic}, plgActDel) } sess.queueOut(NoErrReply(msg, now)) } } else { // Case 2: just unregister. // If t is nil, it's not registered, no action is needed if t := h.topicGet(topic); t != nil { t.markDeleted() h.topicDel(topic) t.exit <- &shutDown{reason: reason} statsInc("LiveTopics", -1) } // sess && msg could be nil if the topic is being killed by timer or due to rehashing. if sess != nil && msg != nil { sess.queueOut(NoErrReply(msg, now)) } } return nil } // Terminate all topics associated with the given user: // * all p2p topics with the given user // * group topics where the given user is the owner. // * user's 'me', 'fnd', 'slf' topics. func (h *Hub) stopTopicsForUser(uid types.Uid, reason int, alldone chan<- bool) { var done chan bool if alldone != nil { done = make(chan bool, 128) } count := 0 h.topics.Range(func(name any, t any) bool { topic := t.(*Topic) if _, isMember := topic.perUser[uid]; (topic.cat != types.TopicCatGrp && isMember) || topic.owner == uid { topic.markDeleted() h.topics.Delete(name) // This call is non-blocking unless some other routine tries to stop it at the same time. topic.exit <- &shutDown{reason: reason, done: done} // Just send to p2p topics here. if topic.cat == types.TopicCatP2P && len(topic.perUser) == 2 { presSingleUserOfflineOffline(topic.p2pOtherUser(uid), uid.UserId(), "gone", nilPresParams, "") } count++ } return true }) statsInc("LiveTopics", -count) if alldone != nil { for range count { <-done } alldone <- true } } // replyOfflineTopicGetDesc reads a minimal topic Desc from the database. // The requester may or maynot be subscribed to the topic. func replyOfflineTopicGetDesc(sess *Session, msg *ClientComMessage) { now := types.TimeNow() desc := &MsgTopicDesc{} asUid := types.ParseUserId(msg.AsUser) topic := msg.RcptTo if strings.HasPrefix(topic, "grp") || topic == "sys" { stopic, err := store.Topics.Get(topic) if err != nil { logs.Info.Println("replyOfflineTopicGetDesc", err) sess.queueOut(decodeStoreErrorExplicitTs(err, msg.Id, msg.Original, now, msg.Timestamp, nil)) return } if stopic == nil { sess.queueOut(ErrTopicNotFoundReply(msg, now)) return } desc.CreatedAt = &stopic.CreatedAt desc.UpdatedAt = &stopic.UpdatedAt desc.Public = stopic.Public desc.Trusted = stopic.Trusted desc.IsChan = stopic.UseBt desc.SubCnt = stopic.SubCnt if stopic.Owner == msg.AsUser { desc.DefaultAcs = &MsgDefaultAcsMode{ Auth: stopic.Access.Auth.String(), Anon: stopic.Access.Anon.String(), } } // Report appropriate access level. Could be overridden below if subscription exists. desc.Acs = &MsgAccessMode{} switch sess.authLvl { case auth.LevelAuth, auth.LevelRoot: desc.Acs.Mode = stopic.Access.Auth.String() case auth.LevelAnon: desc.Acs.Mode = stopic.Access.Anon.String() } } else { // 'me' and p2p topics uid := types.ZeroUid if strings.HasPrefix(topic, "usr") { // User specified as usrXXX uid = types.ParseUserId(topic) topic = asUid.P2PName(uid) } else if strings.HasPrefix(topic, "p2p") { // User specified as p2pXXXYYY uid1, uid2, _ := types.ParseP2P(topic) if uid1 == asUid { uid = uid2 } else if uid2 == asUid { uid = uid1 } } if uid.IsZero() { logs.Warn.Println("replyOfflineTopicGetDesc: malformed p2p topic name") sess.queueOut(ErrMalformedReply(msg, now)) return } suser, err := store.Users.Get(uid) if err != nil { sess.queueOut(decodeStoreErrorExplicitTs(err, msg.Id, msg.Original, now, msg.Timestamp, nil)) return } if suser == nil { sess.queueOut(ErrUserNotFoundReply(msg, now)) return } desc.CreatedAt = &suser.CreatedAt desc.UpdatedAt = &suser.UpdatedAt desc.Public = suser.Public desc.Trusted = suser.Trusted if sess.authLvl == auth.LevelRoot { desc.State = suser.State.String() } // Report appropriate access level. Could be overridden below if subscription exists. desc.Acs = &MsgAccessMode{} switch sess.authLvl { case auth.LevelAuth, auth.LevelRoot: desc.Acs.Mode = suser.Access.Auth.String() case auth.LevelAnon: desc.Acs.Mode = suser.Access.Anon.String() } } sub, err := store.Subs.Get(topic, asUid, false) if err != nil { logs.Warn.Println("replyOfflineTopicGetDesc:", err) sess.queueOut(decodeStoreErrorExplicitTs(err, msg.Id, msg.Original, now, msg.Timestamp, nil)) return } if sub != nil { desc.Private = sub.Private // FIXME: suspended topics should get no AW access. desc.Acs = &MsgAccessMode{ Want: sub.ModeWant.String(), Given: sub.ModeGiven.String(), Mode: (sub.ModeGiven & sub.ModeWant).String(), } } sess.queueOut(&ServerComMessage{ Meta: &MsgServerMeta{ Id: msg.Id, Topic: msg.Original, Timestamp: &now, Desc: desc, }, }) } // replyOfflineTopicGetSub reads user's subscription from the database. // Only own subscription is available. // The requester must be subscribed but need not be attached. func replyOfflineTopicGetSub(sess *Session, msg *ClientComMessage) { now := types.TimeNow() if msg.Get.Sub != nil && msg.Get.Sub.User != "" && msg.Get.Sub.User != msg.AsUser { sess.queueOut(ErrPermissionDeniedReply(msg, now)) return } topicName := msg.RcptTo if types.IsChannel(msg.Original) { topicName = msg.Original } ssub, err := store.Subs.Get(topicName, types.ParseUserId(msg.AsUser), true) if err != nil { logs.Warn.Println("replyOfflineTopicGetSub:", err) sess.queueOut(decodeStoreErrorExplicitTs(err, msg.Id, msg.Original, now, msg.Timestamp, nil)) return } if ssub == nil { sess.queueOut(ErrNotFoundExplicitTs(msg.Id, msg.Original, now, msg.Timestamp)) return } sub := MsgTopicSub{} if ssub.DeletedAt == nil { sub.UpdatedAt = &ssub.UpdatedAt sub.Acs = MsgAccessMode{ Want: ssub.ModeWant.String(), Given: ssub.ModeGiven.String(), Mode: (ssub.ModeGiven & ssub.ModeWant).String(), } // Fnd is asymmetric: desc.private is a string, but sub.private is a []string. if types.GetTopicCat(msg.RcptTo) != types.TopicCatFnd { sub.Private = ssub.Private } sub.User = types.ParseUid(ssub.User).UserId() if (ssub.ModeGiven & ssub.ModeWant).IsReader() && (ssub.ModeWant & ssub.ModeGiven).IsJoiner() { sub.DelId = ssub.DelId sub.ReadSeqId = ssub.ReadSeqId sub.RecvSeqId = ssub.RecvSeqId } } else { sub.DeletedAt = ssub.DeletedAt } sess.queueOut(&ServerComMessage{ Meta: &MsgServerMeta{ Id: msg.Id, Topic: msg.Original, Timestamp: &now, Sub: []MsgTopicSub{sub}, }, }) } // replyOfflineTopicSetSub updates Desc.Private and Sub.Mode when the topic is not loaded in memory. // Only Private and Mode are updated and only for the requester. The requester must be subscribed to the // topic but does not need to be attached. func replyOfflineTopicSetSub(sess *Session, msg *ClientComMessage) { now := types.TimeNow() if (msg.Set.Desc == nil || msg.Set.Desc.Private == nil) && (msg.Set.Sub == nil || msg.Set.Sub.Mode == "") { sess.queueOut(InfoNotModifiedReply(msg, now)) return } if msg.Set.Sub != nil && msg.Set.Sub.User != "" && msg.Set.Sub.User != msg.AsUser { sess.queueOut(ErrPermissionDeniedReply(msg, now)) return } asUid := types.ParseUserId(msg.AsUser) topicName := msg.RcptTo if types.IsChannel(msg.Original) { topicName = msg.Original } sub, err := store.Subs.Get(topicName, asUid, false) if err != nil { logs.Warn.Println("replyOfflineTopicSetSub get sub:", err) sess.queueOut(decodeStoreErrorExplicitTs(err, msg.Id, msg.Original, now, msg.Timestamp, nil)) return } if sub == nil { sess.queueOut(ErrNotFoundExplicitTs(msg.Id, msg.Original, now, msg.Timestamp)) return } update := make(map[string]any) if msg.Set.Desc != nil && msg.Set.Desc.Private != nil { private, ok := msg.Set.Desc.Private.(map[string]any) if !ok { update = map[string]any{"Private": msg.Set.Desc.Private} } else if private, changed := mergeInterfaces(sub.Private, private); changed { update = map[string]any{"Private": private} } } if msg.Set.Sub != nil && msg.Set.Sub.Mode != "" { var modeWant types.AccessMode if err = modeWant.UnmarshalText([]byte(msg.Set.Sub.Mode)); err != nil { logs.Warn.Println("replyOfflineTopicSetSub mode:", err) sess.queueOut(decodeStoreErrorExplicitTs(err, msg.Id, msg.Original, now, msg.Timestamp, nil)) return } if modeWant.IsOwner() != sub.ModeWant.IsOwner() { // No ownership changes here. sess.queueOut(ErrPermissionDeniedReply(msg, now)) return } if types.GetTopicCat(msg.RcptTo) == types.TopicCatP2P { // For P2P topics ignore requests exceeding typesModeCP2P and do not allow // removal of 'A' permission. modeWant = modeWant&globals.typesModeCP2P | types.ModeApprove } if modeWant != sub.ModeWant { update["ModeWant"] = modeWant // Cache it for later use sub.ModeWant = modeWant } } if len(update) > 0 { err = store.Subs.Update(topicName, asUid, update) if err != nil { logs.Warn.Println("replyOfflineTopicSetSub update:", err) sess.queueOut(decodeStoreErrorExplicitTs(err, msg.Id, msg.Original, now, msg.Timestamp, nil)) } else { var params any if update["ModeWant"] != nil { params = map[string]any{ "acs": MsgAccessMode{ Given: sub.ModeGiven.String(), Want: sub.ModeWant.String(), Mode: (sub.ModeGiven & sub.ModeWant).String(), }, } } sess.queueOut(NoErrParamsReply(msg, now, params)) } } else { sess.queueOut(InfoNotModifiedReply(msg, now)) } } ================================================ FILE: server/init_topic.go ================================================ /****************************************************************************** * * Description : * * Topic initilization routines. * *****************************************************************************/ package main import ( "strings" "github.com/tinode/chat/server/auth" "github.com/tinode/chat/server/logs" "github.com/tinode/chat/server/store" "github.com/tinode/chat/server/store/types" ) // topicInit reads an existing topic from database or creates a new topic func topicInit(t *Topic, join *ClientComMessage, h *Hub) { var subscribeReqIssued bool defer func() { if !subscribeReqIssued && join.Sub != nil && join.sess.inflightReqs != nil { // If it was a client initiated subscribe request and we failed it. join.sess.inflightReqs.Done() } }() timestamp := types.TimeNow() var err error switch { case t.xoriginal == "me": // Request to load a 'me' topic. The topic always exists, the subscription is never new. err = initTopicMe(t, join) case t.xoriginal == "fnd": // Request to load a 'find' topic. The topic always exists, the subscription is never new. err = initTopicFnd(t, join) case strings.HasPrefix(t.xoriginal, "usr") || strings.HasPrefix(t.xoriginal, "p2p"): // Request to load an existing or create a new p2p topic, then attach to it. err = initTopicP2P(t, join) case strings.HasPrefix(t.xoriginal, "new"): // Processing request to create a new group topic. err = initTopicNewGrp(t, join, false) case strings.HasPrefix(t.xoriginal, "nch"): // Processing request to create a new channel. err = initTopicNewGrp(t, join, true) case strings.HasPrefix(t.xoriginal, "grp") || strings.HasPrefix(t.xoriginal, "chn"): // Load existing group topic (or channel). err = initTopicGrp(t) case t.xoriginal == "sys": // Initialize system topic. err = initTopicSys(t) case t.xoriginal == "slf": // Initialize self (notes and saved messages) topic. err = initTopicSlf(t, join) default: // Unrecognized topic name err = types.ErrTopicNotFound } // Failed to create or load the topic. if err != nil { // Remove topic from cache to prevent hub from forwarding more messages to it. h.topicDel(join.RcptTo) logs.Err.Println("init_topic: failed to load or create topic:", join.RcptTo, err) join.sess.queueOut(decodeStoreErrorExplicitTs(err, join.Id, t.xoriginal, timestamp, join.Timestamp, nil)) // Re-queue pending requests to join the topic. for len(t.reg) > 0 { h.join <- (<-t.reg) } // Reject all other pending requests for len(t.clientMsg) > 0 { msg := <-t.clientMsg if msg.init { msg.sess.queueOut(ErrLockedExplicitTs(msg.Id, t.xoriginal, timestamp, join.Timestamp)) } } for len(t.unreg) > 0 { msg := <-t.unreg if msg.sess != nil && msg.sess.inflightReqs != nil { msg.sess.inflightReqs.Done() } if msg.init { msg.sess.queueOut(ErrLockedReply(msg, timestamp)) } } for len(t.meta) > 0 { msg := <-t.meta if msg.init { msg.sess.queueOut(ErrLockedReply(msg, timestamp)) } } if len(t.exit) > 0 { msg := <-t.exit msg.done <- true } return } t.computePerUserAcsUnion() // prevent newly initialized topics to go live while shutdown in progress if globals.shuttingDown { h.topicDel(join.RcptTo) return } if t.isDeleted() { // Someone deleted the topic while we were trying to create it. return } statsInc("LiveTopics", 1) statsInc("TotalTopics", 1) usersRegisterTopic(t, true) // Topic will check access rights, send invite to p2p user, send {ctrl} message to the initiator session if join.Sub != nil { subscribeReqIssued = true t.reg <- join } t.markPaused(false) if t.cat == types.TopicCatFnd || t.cat == types.TopicCatSys { t.markLoaded() } go t.run(h) } // Initialize 'me' topic. func initTopicMe(t *Topic, sreg *ClientComMessage) error { t.cat = types.TopicCatMe user, err := store.Users.Get(types.ParseUserId(t.name)) if err != nil { // Log out the session sreg.sess.uid = types.ZeroUid return err } else if user == nil { // Log out the session sreg.sess.uid = types.ZeroUid return types.ErrUserNotFound } // User's default access for p2p topics t.accessAuth = user.Access.Auth t.accessAnon = user.Access.Anon // Assign tags t.tags = user.Tags if err = t.loadSubscribers(); err != nil { return err } t.public = user.Public t.trusted = user.Trusted t.created = user.CreatedAt t.updated = user.UpdatedAt // The following values are exlicitly not set for 'me'. // t.touched, t.lastId, t.delId // 'me' has no owner, t.owner = nil // Initiate User Agent with the UA of the creating session to report it later t.userAgent = sreg.sess.userAgent // Initialize channel for receiving user agent and session online updates. t.supd = make(chan *sessionUpdate, 32) if !t.isProxy { // Allocate storage for contacts. t.perSubs = make(map[string]perSubsData) } return nil } // Initialize 'fnd' topic func initTopicFnd(t *Topic, sreg *ClientComMessage) error { t.cat = types.TopicCatFnd uid := types.ParseUserId(sreg.AsUser) if uid.IsZero() { return types.ErrNotFound } user, err := store.Users.Get(uid) if err != nil { return err } else if user == nil { if !sreg.sess.isMultiplex() { sreg.sess.uid = types.ZeroUid } return types.ErrNotFound } // Make sure no one can join the topic. t.accessAuth = getDefaultAccess(t.cat, true, false) t.accessAnon = getDefaultAccess(t.cat, false, false) if err = t.loadSubscribers(); err != nil { return err } t.created = user.CreatedAt t.updated = user.UpdatedAt // 'fnd' has no owner, t.owner = nil // Publishing to fnd is not supported // t.lastId = 0, t.delId = 0, t.touched = nil return nil } // Load or create a P2P topic. // There is a reace condition when two users try to create a p2p topic at the same time. func initTopicP2P(t *Topic, sreg *ClientComMessage) error { pktsub := sreg.Sub // Handle the following cases: // 1. Neither topic nor subscriptions exist: create a new p2p topic & subscriptions. // 2. Topic exists, one of the subscriptions is missing: // 2.1 Requester's subscription is missing, recreate it. // 2.2 Other user's subscription is missing, treat like a new request for user 2. // 3. Topic exists, both subscriptions are missing: should not happen, fail. // 4. Topic and both subscriptions exist: attach to topic t.cat = types.TopicCatP2P // Check if the topic already exists stopic, err := store.Topics.Get(t.name) if err != nil { return err } // If topic exists, load subscriptions var subs []types.Subscription if stopic != nil { // Subs already have Public swapped if subs, err = store.Topics.GetUsers(t.name, nil); err != nil { return err } // Case 3, fail if len(subs) == 0 { logs.Err.Println("hub: missing both subscriptions for '" + t.name + "' (SHOULD NEVER HAPPEN!)") return types.ErrInternal } t.created = stopic.CreatedAt t.updated = stopic.UpdatedAt if !stopic.TouchedAt.IsZero() { t.touched = stopic.TouchedAt } t.aux = stopic.Aux t.lastID = stopic.SeqId t.delID = stopic.DelId } // t.owner is blank for p2p topics // Default user access to P2P topics is not set because it's unused. // Other users cannot join the topic because of how topic name is constructed. // The two participants set each other's access instead. // t.accessAuth = getDefaultAccess(t.cat, true) // t.accessAnon = getDefaultAccess(t.cat, false) // t.public and t.trusted are not used for p2p topics since each user get a different public/trusted. if stopic != nil && len(subs) == 2 { // Case 4. for i := range 2 { uid := types.ParseUid(subs[i].User) t.perUser[uid] = perUserData{ // Adapter has already swapped the state, public, defaultAccess, lastSeen values. public: subs[i].GetPublic(), lastSeen: subs[i].GetLastSeen(), lastUA: subs[i].GetUserAgent(), topicName: types.ParseUid(subs[(i+1)%2].User).UserId(), private: subs[i].Private, modeWant: subs[i].ModeWant, modeGiven: subs[i].ModeGiven, delID: subs[i].DelId, recvID: subs[i].RecvSeqId, readID: subs[i].ReadSeqId, } } } else { // Cases 1 (new topic), 2 (one of the two subscriptions is missing: either it's a new request // or the subscription was deleted) var userData perUserData // Fetching records for both users. // Requester. userID1 := types.ParseUserId(sreg.AsUser) // The other user. userID2 := types.ParseUserId(t.xoriginal) // User index: u1 - requester, u2 - responder, the other user var u1, u2 int users, err := store.Users.GetAll(userID1, userID2) if err != nil { return err } if len(users) != 2 { // Invited user does not exist return types.ErrUserNotFound } // User records are unsorted, make sure we know who is who. if users[0].Uid() == userID1 { u1, u2 = 0, 1 } else { u1, u2 = 1, 0 } // Figure out which subscriptions are missing: User1's, User2's or both. var sub1, sub2 *types.Subscription // Set to true if only requester's subscription has to be created. var user1only bool if len(subs) == 1 { if subs[0].User == userID1.String() { // User2's subscription is missing, user1's exists sub1 = &subs[0] } else { // User1's is missing, user2's exists sub2 = &subs[0] user1only = true } } // Other user's (responder's) subscription is missing if sub2 == nil { sub2 = &types.Subscription{ User: userID2.String(), Topic: t.name, Private: nil, } // Assign user2's ModeGiven based on what user1 has provided. // We don't know access mode for user2, assume it's Auth. if pktsub.Set != nil && pktsub.Set.Desc != nil && pktsub.Set.Desc.DefaultAcs != nil { // Use provided DefaultAcs as non-default modeGiven for the other user. // The other user is assumed to have auth level "Auth". sub2.ModeGiven = users[u1].Access.Auth if err := sub2.ModeGiven.UnmarshalText([]byte(pktsub.Set.Desc.DefaultAcs.Auth)); err != nil { logs.Err.Println("hub: invalid access mode", t.xoriginal, pktsub.Set.Desc.DefaultAcs.Auth) } } else { // Use user1.Auth as modeGiven for the other user sub2.ModeGiven = users[u1].Access.Auth } // Sanity check sub2.ModeGiven = sub2.ModeGiven&globals.typesModeCP2P | types.ModeApprove // Swap Public+Trusted to match swapped Public+Trusted in subs returned from store.Topics.GetSubs sub2.SetPublic(users[u1].Public) sub2.SetTrusted(users[u1].Trusted) // Mark the entire topic as new. pktsub.Created = true } // Requester's subscription is missing: // a. requester is starting a new topic // b. requester's subscription is missing: deleted or creation failed if sub1 == nil { // Set user1's ModeGiven from user2's default values userData.modeGiven = selectAccessMode(auth.Level(sreg.AuthLvl), users[u2].Access.Anon, users[u2].Access.Auth, globals.typesModeCP2P) // By default assign the same mode that user1 gave to user2 (could be changed below) userData.modeWant = sub2.ModeGiven if pktsub.Set != nil { if pktsub.Set.Sub != nil { uid := userID1 if pktsub.Set.Sub.User != "" { uid = types.ParseUserId(pktsub.Set.Sub.User) } if uid != userID1 { // Report the error and ignore the value logs.Err.Println("hub: setting mode for another user is not supported '" + t.name + "'") } else { // user1 is setting non-default modeWant if err := userData.modeWant.UnmarshalText([]byte(pktsub.Set.Sub.Mode)); err != nil { logs.Err.Println("hub: invalid access mode", t.xoriginal, pktsub.Set.Sub.Mode) } // Ensure sanity userData.modeWant = userData.modeWant&globals.typesModeCP2P | types.ModeApprove } // Since user1 issued a {sub} request, make sure the user can join userData.modeWant |= types.ModeJoin } // user1 sets non-default Private if pktsub.Set.Desc != nil { if !isNullValue(pktsub.Set.Desc.Private) { userData.private = pktsub.Set.Desc.Private } // Public, if present, is ignored } } sub1 = &types.Subscription{ User: userID1.String(), Topic: t.name, ModeWant: userData.modeWant, ModeGiven: userData.modeGiven, Private: userData.private, } // Swap Public+Trsuted to match swapped Public+Trusted in subs returned from store.Topics.GetSubs sub1.SetPublic(users[u2].Public) sub1.SetTrusted(users[u2].Trusted) // Mark this subscription as new pktsub.Newsub = true } if !user1only { // sub2 is being created, assign sub2.modeWant to what user2 gave to user1 (sub1.modeGiven) sub2.ModeWant = selectAccessMode(auth.Level(sreg.AuthLvl), users[u2].Access.Anon, users[u2].Access.Auth, globals.typesModeCP2P) // Ensure sanity sub2.ModeWant = sub2.ModeWant&globals.typesModeCP2P | types.ModeApprove } // Create everything if stopic == nil { if err = store.Topics.CreateP2P(sub1, sub2); err != nil { return err } t.created = sub1.CreatedAt t.updated = sub1.UpdatedAt t.touched = t.updated // t.lastId is not set (default 0) for new topics } else { // TODO possibly update subscription, if changed // Recreate one of the subscriptions var subToMake *types.Subscription if user1only { subToMake = sub1 } else { subToMake = sub2 } if err = store.Subs.Create(subToMake); err != nil { return err } } // Public and Trusted are already swapped. userData.public = sub1.GetPublic() userData.trusted = sub1.GetTrusted() userData.topicName = userID2.UserId() userData.modeWant = sub1.ModeWant userData.modeGiven = sub1.ModeGiven userData.delID = sub1.DelId userData.readID = sub1.ReadSeqId userData.recvID = sub1.RecvSeqId t.perUser[userID1] = userData t.perUser[userID2] = perUserData{ public: sub2.GetPublic(), trusted: sub2.GetTrusted(), topicName: userID1.UserId(), modeWant: sub2.ModeWant, modeGiven: sub2.ModeGiven, delID: sub2.DelId, readID: sub2.ReadSeqId, recvID: sub2.RecvSeqId, } } // Clear original topic name. t.xoriginal = "" return nil } // Create a new group topic func initTopicNewGrp(t *Topic, sreg *ClientComMessage, isChan bool) error { timestamp := types.TimeNow() pktsub := sreg.Sub t.cat = types.TopicCatGrp t.isChan = isChan // Generic topics have parameters stored in the topic object t.owner = types.ParseUserId(sreg.AsUser) authLevel := auth.Level(sreg.AuthLvl) t.accessAuth = getDefaultAccess(t.cat, true, isChan) t.accessAnon = getDefaultAccess(t.cat, false, isChan) // Owner/creator gets full access to the topic. Owner may change the default modeWant through 'set'. userData := perUserData{ modeGiven: types.ModeCFull, modeWant: types.ModeCFull, } if pktsub.Set != nil { // User sent initialization parameters if pktsub.Set.Desc != nil { if pktsub.Set.Desc.Trusted != nil && authLevel != auth.LevelRoot { logs.Err.Println("hub: attempt to assign Trusted by non-ROOT", t.name) return types.ErrPermissionDenied } if !isNullValue(pktsub.Set.Desc.Public) { t.public = pktsub.Set.Desc.Public } if !isNullValue(pktsub.Set.Desc.Trusted) { t.trusted = pktsub.Set.Desc.Trusted } if !isNullValue(pktsub.Set.Desc.Private) { userData.private = pktsub.Set.Desc.Private } // set default access if pktsub.Set.Desc.DefaultAcs != nil { if authMode, anonMode, err := parseTopicAccess(pktsub.Set.Desc.DefaultAcs, t.accessAuth, t.accessAnon); err != nil { // Invalid access for one or both. Make it explicitly None if authMode.IsInvalid() { t.accessAuth = types.ModeNone } else { t.accessAuth = authMode } if anonMode.IsInvalid() { t.accessAnon = types.ModeNone } else { t.accessAnon = anonMode } logs.Err.Println("hub: invalid access mode for topic '" + t.name + "': '" + err.Error() + "'") } else if authMode.IsOwner() || anonMode.IsOwner() { logs.Err.Println("hub: OWNER default access in topic", t.name) t.accessAuth, t.accessAnon = authMode & ^types.ModeOwner, anonMode & ^types.ModeOwner } else { t.accessAuth, t.accessAnon = authMode, anonMode } } } // Owner/creator may restrict own access to topic if pktsub.Set.Sub != nil && pktsub.Set.Sub.Mode != "" { userData.modeWant = types.ModeCFull if err := userData.modeWant.UnmarshalText([]byte(pktsub.Set.Sub.Mode)); err != nil { logs.Err.Println("hub: invalid access mode", t.xoriginal, pktsub.Set.Sub.Mode) } // User must not unset ModeJoin or the owner flags userData.modeWant |= types.ModeJoin | types.ModeOwner } if tags := normalizeTags(pktsub.Set.Tags, globals.maxTagCount); len(tags) > 0 { if !restrictedTagsEqual(tags, nil, globals.immutableTagNS) { return types.ErrPermissionDenied } // Assign tags t.tags = tags } } t.perUser[t.owner] = userData t.created = timestamp t.updated = timestamp t.touched = timestamp // t.lastId & t.delId are not set for new topics stopic := &types.Topic{ ObjHeader: types.ObjHeader{Id: sreg.RcptTo, CreatedAt: timestamp}, Access: types.DefaultAccess{Auth: t.accessAuth, Anon: t.accessAnon}, Tags: t.tags, UseBt: isChan, Public: t.public, Trusted: t.trusted, } // store.Topics.Create will add a subscription record for the topic creator stopic.GiveAccess(t.owner, userData.modeWant, userData.modeGiven) err := store.Topics.Create(stopic, t.owner, t.perUser[t.owner].private) if err != nil { return err } // Link uploaded avatar to topic. if sreg.Extra != nil && len(sreg.Extra.Attachments) > 0 { if err := store.Files.LinkAttachments(t.name, types.ZeroUid, sreg.Extra.Attachments); err != nil { logs.Warn.Printf("topic[%s] failed to link avatar attachment: %v", t.name, err) // This is not a critical error, continue execution. } } t.xoriginal = t.name // keeping 'new' or 'nch' as original has no value to the client t.subCnt = 1 // One subscription, the owner. pktsub.Created = true pktsub.Newsub = true return nil } // Initialize existing group topic. There is a race condition when two users attempt to load // the same topic at the same time. It's prevented at hub level. func initTopicGrp(t *Topic) error { t.cat = types.TopicCatGrp // TODO(gene): check and validate topic name stopic, err := store.Topics.Get(t.name) if err != nil { return err } else if stopic == nil { return types.ErrTopicNotFound } if err = t.loadSubscribers(); err != nil { return err } t.isChan = stopic.UseBt // t.owner is set by loadSubscriptions t.accessAuth = stopic.Access.Auth t.accessAnon = stopic.Access.Anon // Assign tags & auxiliary data. t.tags = stopic.Tags t.aux = stopic.Aux t.public = stopic.Public t.trusted = stopic.Trusted t.created = stopic.CreatedAt t.updated = stopic.UpdatedAt if !stopic.TouchedAt.IsZero() { t.touched = stopic.TouchedAt } t.lastID = stopic.SeqId t.delID = stopic.DelId t.subCnt = stopic.SubCnt // Initialize channel for receiving session online updates. t.supd = make(chan *sessionUpdate, 32) t.xoriginal = t.name // topic may have been loaded by a channel reader; make sure it's grpXXX, not chnXXX. return nil } // Initialize system topic. System topic is a singleton, always in memory. func initTopicSys(t *Topic) error { t.cat = types.TopicCatSys stopic, err := store.Topics.Get(t.name) if err != nil { return err } else if stopic == nil { return types.ErrTopicNotFound } if err = t.loadSubscribers(); err != nil { return err } // There is no t.owner // Default permissions are 'W' t.accessAuth = types.ModeWrite t.accessAnon = types.ModeWrite t.public = stopic.Public t.trusted = stopic.Trusted t.created = stopic.CreatedAt t.updated = stopic.UpdatedAt if !stopic.TouchedAt.IsZero() { t.touched = stopic.TouchedAt } t.lastID = stopic.SeqId return nil } // Initialize or load a self-topic 'slf'. func initTopicSlf(t *Topic, sreg *ClientComMessage) error { t.cat = types.TopicCatSlf stopic, err := store.Topics.Get(t.name) if err != nil { return err } // If topic exists, load subscriptions if stopic != nil { if err = t.loadSubscribers(); err != nil { return err } // t.owner is set by loadSubscriptions // Topic exists but subscription is missing. Fail. if len(t.perUser) == 0 { logs.Err.Println("hub: missing subscription for '" + t.name + "' (SHOULD NEVER HAPPEN!)") return types.ErrInternal } t.created = stopic.CreatedAt t.updated = stopic.UpdatedAt if !stopic.TouchedAt.IsZero() { t.touched = stopic.TouchedAt } t.aux = stopic.Aux t.lastID = stopic.SeqId t.delID = stopic.DelId } else { // Get topic owner. userID := types.ParseUserId(sreg.AsUser) user, err := store.Users.Get(userID) if err != nil { return err } if user == nil { // User not found. Really should not happen. return types.ErrUserNotFound } t.owner = userID t.accessAuth = getDefaultAccess(t.cat, true, false) t.accessAnon = getDefaultAccess(t.cat, false, false) // Default access for the self-owner. userData := perUserData{ modeGiven: t.accessAuth, modeWant: t.accessAuth, } // Mark the topic as new. sreg.Sub.Created = true if sreg.Sub.Set != nil { // User sets non-default Private if sreg.Sub.Set.Desc != nil { if !isNullValue(sreg.Sub.Set.Desc.Private) { userData.private = sreg.Sub.Set.Desc.Private } // Public, trusted are ignored. } if tags := normalizeTags(sreg.Sub.Set.Tags, globals.maxTagCount); len(tags) > 0 { if !restrictedTagsEqual(tags, nil, globals.immutableTagNS) { return types.ErrPermissionDenied } // Assign tags t.tags = tags } } // Mark this subscription as new sreg.Sub.Newsub = true t.perUser[t.owner] = userData timestamp := types.TimeNow() t.created = timestamp t.updated = timestamp t.touched = timestamp stopic = &types.Topic{ ObjHeader: types.ObjHeader{Id: sreg.RcptTo, CreatedAt: timestamp}, Access: types.DefaultAccess{Auth: t.accessAuth, Anon: t.accessAnon}, Tags: t.tags, } // store.Topics.Create will add a subscription record for the topic creator stopic.GiveAccess(t.owner, userData.modeWant, userData.modeGiven) err = store.Topics.Create(stopic, t.owner, t.perUser[t.owner].private) if err != nil { return err } sreg.Sub.Created = true sreg.Sub.Newsub = true } return nil } // loadSubscribers loads topic subscribers, sets topic owner. func (t *Topic) loadSubscribers() error { subs, err := store.Topics.GetSubs(t.name, nil) if err != nil { return err } if subs == nil { return nil } for i := range subs { sub := &subs[i] uid := types.ParseUid(sub.User) t.perUser[uid] = perUserData{ delID: sub.DelId, readID: sub.ReadSeqId, recvID: sub.RecvSeqId, private: sub.Private, modeWant: sub.ModeWant, modeGiven: sub.ModeGiven, } if (sub.ModeGiven & sub.ModeWant).IsOwner() { t.owner = uid } } return nil } ================================================ FILE: server/logs/logs.go ================================================ // Package logs exposes info, warning and error loggers. package logs import ( "io" "log" "strings" ) var ( // Info is a logger at the 'info' logging level. Info *log.Logger // Warn is a logger at the 'warning' logging level. Warn *log.Logger // Err is a logger at the 'error' logging level. Err *log.Logger ) func parseFlags(logFlags string) int { flags := 0 for _, v := range strings.Split(logFlags, ",") { switch { case v == "date": flags |= log.Ldate case v == "time": flags |= log.Ltime case v == "microseconds": flags |= log.Lmicroseconds case v == "longfile": flags |= log.Llongfile case v == "shortfile": flags |= log.Lshortfile case v == "UTC": flags |= log.LUTC case v == "msgprefix": flags |= log.Lmsgprefix case v == "stdFlags": flags |= log.LstdFlags default: log.Fatalln("Invalid log flags string: ", logFlags) } } if flags == 0 { flags = log.LstdFlags } return flags } // Init initializes info, warning and error loggers given the flags and the output. func Init(output io.Writer, logFlags string) { flags := parseFlags(logFlags) Info = log.New(output, "I", flags) Warn = log.New(output, "W", flags) Err = log.New(output, "E", flags) } ================================================ FILE: server/main.go ================================================ /****************************************************************************** * * Description : * * Setup & initialization. * *****************************************************************************/ package main //go:generate protoc --go_out=../pbx --go_opt=paths=source_relative --go-grpc_out=../pbx --go-grpc_opt=paths=source_relative ../pbx/model.proto import ( "encoding/json" "flag" "net/http" "os" "runtime" "runtime/pprof" "strings" "time" gh "github.com/gorilla/handlers" // For stripping comments from JSON config jcr "github.com/tinode/jsonco" // Authenticators "github.com/tinode/chat/server/auth" _ "github.com/tinode/chat/server/auth/anon" _ "github.com/tinode/chat/server/auth/basic" _ "github.com/tinode/chat/server/auth/code" _ "github.com/tinode/chat/server/auth/rest" _ "github.com/tinode/chat/server/auth/token" "github.com/tinode/chat/server/store/types" // Database backends _ "github.com/tinode/chat/server/db/mongodb" _ "github.com/tinode/chat/server/db/mysql" _ "github.com/tinode/chat/server/db/postgres" _ "github.com/tinode/chat/server/db/rethinkdb" "github.com/tinode/chat/server/logs" // Push notifications "github.com/tinode/chat/server/push" _ "github.com/tinode/chat/server/push/fcm" _ "github.com/tinode/chat/server/push/stdout" _ "github.com/tinode/chat/server/push/tnpg" "github.com/tinode/chat/server/store" // Credential validators _ "github.com/tinode/chat/server/validate/email" _ "github.com/tinode/chat/server/validate/tel" "google.golang.org/grpc" // File upload handlers _ "github.com/tinode/chat/server/media/fs" _ "github.com/tinode/chat/server/media/s3" ) const ( // currentVersion is the current API/protocol version currentVersion = "0.25" // minSupportedVersion is the minimum supported API version minSupportedVersion = "0.20" // idleSessionTimeout defines duration of being idle before terminating a session. idleSessionTimeout = time.Second * 55 // idleMasterTopicTimeout defines now long to keep master topic alive after the last session detached. idleMasterTopicTimeout = time.Second * 4 // Same as above but shut down the proxy topic sooner. Otherwise master topic would be kept alive for too long. idleProxyTopicTimeout = time.Second * 2 // defaultMaxMessageSize is the default maximum message size defaultMaxMessageSize = 1 << 19 // 512K // defaultMaxSubscriberCount is the default maximum number of group topic subscribers. // Also set in adapter. defaultMaxSubscriberCount = 256 // defaultMaxTagCount is the default maximum number of indexable tags defaultMaxTagCount = 16 // minTagLength is the shortest acceptable length of a tag in runes. Shorter tags are discarded. minTagLength = 2 // maxTagLength is the maximum length of a tag in runes. Longer tags are trimmed. maxTagLength = 96 // Delay before updating a User Agent uaTimerDelay = time.Second * 5 // maxDeleteCount is the maximum allowed number of messages to delete in one call. defaultMaxDeleteCount = 1024 // Base URL path for serving the streaming API. defaultApiPath = "/" // Mount point where static content is served, http://host-name defaultStaticMount = "/" // Local path to static content defaultStaticPath = "static" // Default country code to fall back to if the "default_country_code" field // isn't specified in the config. defaultCountryCode = "US" // Default timeout to drop an unanswered call, seconds. defaultCallEstablishmentTimeout = 30 ) // Build version number defined by the compiler: // // -ldflags "-X main.buildstamp=value_to_assign_to_buildstamp" // // Reported to clients in response to {hi} message. // For instance, to define the buildstamp as a timestamp of when the server was built add a // flag to compiler command line: // // -ldflags "-X main.buildstamp=`date -u '+%Y%m%dT%H:%M:%SZ'`" // // or to set it to git tag: // // -ldflags "-X main.buildstamp=`git describe --tags`" var buildstamp = "undef" // CredValidator holds additional config params for a credential validator. type credValidator struct { // AuthLevel(s) which require this validator. requiredAuthLvl []auth.Level addToTags bool } var globals struct { // Topics cache and processing. hub *Hub // Indicator that shutdown is in progress shuttingDown bool // Sessions cache. sessionStore *SessionStore // Cluster data. cluster *Cluster // gRPC server. grpcServer *grpc.Server // Plugins. plugins []Plugin // Runtime statistics communication channel. statsUpdate chan *varUpdate // Users cache communication channel. usersUpdate chan *UserCacheReq // Credential validators. validators map[string]credValidator // Credential validator config to pass to clients. validatorClientConfig map[string][]string // Validators required for each auth level. authValidators map[auth.Level][]string // Salt used for signing API key. apiKeySalt []byte // Tag namespaces (prefixes) which are immutable to the client. immutableTagNS map[string]bool // Tag namespaces which are immutable on User and partially mutable on Topic: // user can only mutate tags he owns. maskedTagNS map[string]bool // Na,espace used for unique user and topic aliases. aliasTagNS string // Add Strict-Transport-Security to headers, the value signifies age. // Empty string "" turns it off tlsStrictMaxAge string // Listen for connections on this address:port and redirect them to HTTPS port. tlsRedirectHTTP string // Maximum message size allowed from peer. maxMessageSize int64 // Maximum number of group topic subscribers. maxSubscriberCount int // Maximum number of indexable tags. maxTagCount int // If true, ordinary users cannot delete their accounts. permanentAccounts bool // Maximum allowed upload size. maxFileUploadSize int64 // Periodicity of a garbage collector for abandoned media uploads. mediaGcPeriod time.Duration // Prioritize X-Forwarded-For header as the source of IP address of the client. useXForwardedFor bool // Add X-Frame-Options header to HTTP response. xFrameOptions string // Country code to assign to sessions by default. defaultCountryCode string // Time before the call is dropped if not answered. callEstablishmentTimeout int // ICE servers config (video calling) iceServers []iceServer // Websocket per-message compression negotiation is enabled. wsCompression bool // URL of the main endpoint. // DEPRECTATED: use file-serving gRPC API instead. This feature will be removed. servingAt string // P2P auth access mode. With or without the D permission depending on P2PDeleteAge. typesModeCP2P types.AccessMode // Maximum age of messages which can be deleted with 'D' permission. msgDeleteAge time.Duration } // Credential validator config. type validatorConfig struct { // TRUE or FALSE to set AddToTags bool `json:"add_to_tags"` // Authentication level which triggers this validator: "auth", "anon"... or "" Required []string `json:"required"` // Validator params passed to validator unchanged. Config json.RawMessage `json:"config"` } // Stale unvalidated user account GC config. type accountGcConfig struct { Enabled bool `json:"enabled"` // How often to run GC (seconds). GcPeriod int `json:"gc_period"` // Number of accounts to delete in one pass. GcBlockSize int `json:"gc_block_size"` // Minimum hours since account was last modified. GcMinAccountAge int `json:"gc_min_account_age"` } // Large file handler config. type mediaConfig struct { // The name of the handler to use for file uploads. UseHandler string `json:"use_handler"` // Maximum allowed size of an uploaded file MaxFileUploadSize int64 `json:"max_size"` // Garbage collection timeout GcPeriod int `json:"gc_period"` // Number of entries to delete in one pass GcBlockSize int `json:"gc_block_size"` // Individual handler config params to pass to handlers unchanged. Handlers map[string]json.RawMessage `json:"handlers"` } // Contentx of the configuration file type configType struct { // HTTP(S) address:port to listen on for websocket and long polling clients. Either a // numeric or a canonical name, e.g. ":80" or ":https". Could include a host name, e.g. // "localhost:80". // Could be blank: if TLS is not configured, will use ":80", otherwise ":443". // Can be overridden from the command line, see option --listen. Listen string `json:"listen"` // Base URL path where the streaming and large file API calls are served, default is '/'. // Can be overridden from the command line, see option --api_path. ApiPath string `json:"api_path"` // Cache-Control value for static content. CacheControl int `json:"cache_control"` // If true, do not attempt to negotiate websocket per message compression (RFC 7692.4). // It should be disabled (set to true) if you are using MSFT IIS as a reverse proxy. WSCompressionDisabled bool `json:"ws_compression_disabled"` // Address:port to listen for gRPC clients. If blank gRPC support will not be initialized. // Could be overridden from the command line with --grpc_listen. GrpcListen string `json:"grpc_listen"` // Enable handling of gRPC keepalives https://github.com/grpc/grpc/blob/master/doc/keepalive.md // This sets server's GRPC_ARG_KEEPALIVE_TIME_MS to 60 seconds instead of the default 2 hours. GrpcKeepalive bool `json:"grpc_keepalive_enabled"` // URL path for mounting the directory with static files (usually TinodeWeb). StaticMount string `json:"static_mount"` // Local path to static files. All files in this path are made accessible by HTTP. StaticData string `json:"static_data"` // Salt used in signing API keys APIKeySalt []byte `json:"api_key_salt"` // Maximum message size allowed from client. Intended to prevent malicious client from sending // very large files inband (does not affect out of band uploads). MaxMessageSize int `json:"max_message_size"` // Maximum number of group topic subscribers. MaxSubscriberCount int `json:"max_subscriber_count"` // Masked tags namespaces: tags immutable on User (mask), mutable on Topic only within the mask. MaskedTagNamespaces []string `json:"masked_tags"` // Tag namespace used for unique user and topic aliases. AliasTagNamespace string `json:"alias_tag"` // Maximum number of indexable tags. MaxTagCount int `json:"max_tag_count"` // If true, ordinary users cannot delete their accounts. PermanentAccounts bool `json:"permanent_accounts"` // URL path for exposing runtime stats. Disabled if the path is blank. ExpvarPath string `json:"expvar"` // URL path for internal server status. Disabled if the path is blank. ServerStatusPath string `json:"server_status"` // Take IP address of the client from HTTP header 'X-Forwarded-For'. // Useful when tinode is behind a proxy. If missing, fallback to default RemoteAddr. UseXForwardedFor bool `json:"use_x_forwarded_for"` // Add X-Frame-Options to HTTP response headers. It should be one of "DENY", "SAMEORIGIN", // "-" (disabled). The default is SAMEORIGIN. XFrameOptions string `json:"x_frame_options"` // 2-letter country code (ISO 3166-1 alpha-2) to assign to sessions by default // when the country isn't specified by the client explicitly and // it's impossible to infer it. DefaultCountryCode string `json:"default_country_code"` // Permit hard-deleting messages in p2p topics for both participants. // If it's set to 'false' then the message is only deleted for the peer who issued the command. // If it's 'true' then the message is deleted completely by either participant. // Changing the value affects the ability to hard-delete (the added or removed the D permission) // only for new topics going forward. P2PDeleteEnabled bool `json:"p2p_delete_enabled"` // The maximum age of a message in seconds when it can be deleted by users with the 'D' permission. // E.g. 600 means messages up to 10 minutes old can be deleted, older than that cannot be deleted. // Missing or 0 means no age limit. // Does not affect topic owners: owners can delete any message. MsgDeleteAge int `json:"msg_delete_age"` // Configs for subsystems Cluster json.RawMessage `json:"cluster_config"` Plugin json.RawMessage `json:"plugins"` Store json.RawMessage `json:"store_config"` Push json.RawMessage `json:"push"` TLS json.RawMessage `json:"tls"` Auth map[string]json.RawMessage `json:"auth_config"` Validator map[string]*validatorConfig `json:"acc_validation"` AccountGC *accountGcConfig `json:"acc_gc_config"` Media *mediaConfig `json:"media"` WebRTC json.RawMessage `json:"webrtc"` } func main() { executable, _ := os.Executable() logFlags := flag.String("log_flags", "stdFlags", "Comma-separated list of log flags (as defined in https://golang.org/pkg/log/#pkg-constants without the L prefix)") configfile := flag.String("config", "tinode.conf", "Path to config file.") // Path to static content. staticPath := flag.String("static_data", defaultStaticPath, "File path to directory with static files to be served.") listenOn := flag.String("listen", "", "Override address and port to listen on for HTTP(S) clients.") apiPath := flag.String("api_path", "", "Override the base URL path where API is served.") listenGrpc := flag.String("grpc_listen", "", "Override address and port to listen on for gRPC clients.") tlsEnabled := flag.Bool("tls_enabled", false, "Override config value for enabling TLS.") clusterSelf := flag.String("cluster_self", "", "Override the name of the current cluster node.") expvarPath := flag.String("expvar", "", "Override the URL path where runtime stats are exposed. Use '-' to disable.") serverStatusPath := flag.String("server_status", "", "Override the URL path where the server's internal status is displayed. Use '-' to disable.") pprofFile := flag.String("pprof", "", "File name to save profiling info to. Disabled if not set.") pprofUrl := flag.String("pprof_url", "", "Debugging only! URL path for exposing profiling info. Disabled if not set.") flag.Parse() logs.Init(os.Stderr, *logFlags) curwd, err := os.Getwd() if err != nil { logs.Err.Fatal("Couldn't get current working directory: ", err) } logs.Info.Printf("Server v%s:%s:%s; pid %d; %d process(es)", currentVersion, executable, buildstamp, os.Getpid(), runtime.GOMAXPROCS(runtime.NumCPU())) *configfile = toAbsolutePath(curwd, *configfile) logs.Info.Printf("Using config from '%s'", *configfile) var config configType if file, err := os.Open(*configfile); err != nil { logs.Err.Fatal("Failed to read config file: ", err) } else { jr := jcr.New(file) if err = json.NewDecoder(jr).Decode(&config); err != nil { switch jerr := err.(type) { case *json.UnmarshalTypeError: lnum, cnum, _ := jr.LineAndChar(jerr.Offset) logs.Err.Fatalf("Unmarshall error in config file in %s at %d:%d (offset %d bytes): %s", jerr.Field, lnum, cnum, jerr.Offset, jerr.Error()) case *json.SyntaxError: lnum, cnum, _ := jr.LineAndChar(jerr.Offset) logs.Err.Fatalf("Syntax error in config file at %d:%d (offset %d bytes): %s", lnum, cnum, jerr.Offset, jerr.Error()) default: logs.Err.Fatal("Failed to parse config file: ", err) } } file.Close() } if *listenOn != "" { config.Listen = *listenOn } // Set up HTTP server. Must use non-default mux because of expvar. mux := http.NewServeMux() // Exposing values for statistics and monitoring. evpath := *expvarPath if evpath == "" { evpath = config.ExpvarPath } statsInit(mux, evpath) statsRegisterInt("Version") decVersion := base10Version(parseVersion(buildstamp)) if decVersion <= 0 { decVersion = base10Version(parseVersion(currentVersion)) } statsSet("Version", decVersion) // Initialize serving debug profiles (optional). servePprof(mux, *pprofUrl) // Initialize cluster and receive calculated workerId. // Cluster won't be started here yet. workerId := clusterInit(config.Cluster, clusterSelf) if *pprofFile != "" { *pprofFile = toAbsolutePath(curwd, *pprofFile) cpuf, err := os.Create(*pprofFile + ".cpu") if err != nil { logs.Err.Fatal("Failed to create CPU pprof file: ", err) } defer cpuf.Close() memf, err := os.Create(*pprofFile + ".mem") if err != nil { logs.Err.Fatal("Failed to create Mem pprof file: ", err) } defer memf.Close() pprof.StartCPUProfile(cpuf) defer pprof.StopCPUProfile() defer pprof.WriteHeapProfile(memf) logs.Info.Printf("Profiling info saved to '%s.(cpu|mem)'", *pprofFile) } err = store.Store.Open(workerId, config.Store) logs.Info.Println("DB adapter", store.Store.GetAdapterName(), store.Store.GetAdapterVersion()) if err != nil { logs.Err.Fatal("Failed to connect to DB: ", err) } defer func() { store.Store.Close() logs.Info.Println("Closed database connection(s)") logs.Info.Println("All done, good bye") }() statsRegisterDbStats() // API key signing secret globals.apiKeySalt = config.APIKeySalt err = store.InitAuthLogicalNames(config.Auth["logical_names"]) if err != nil { logs.Err.Fatal(err) } // List of tag namespaces for user discovery which cannot be changed directly // by the client, e.g. 'email' or 'tel'. globals.immutableTagNS = make(map[string]bool) authNames := store.Store.GetAuthNames() for _, name := range authNames { if authhdl := store.Store.GetLogicalAuthHandler(name); authhdl == nil { logs.Err.Fatalln("Unknown authenticator", name) } else if jsconf := config.Auth[authhdl.GetRealName()]; jsconf != nil { if err := authhdl.Init(jsconf, name); err != nil { logs.Err.Fatalln("Failed to init auth scheme", name+":", err) } tags, err := authhdl.RestrictedTags() if err != nil { logs.Err.Fatalln("Failed get restricted tag namespaces (prefixes)", name+":", err) } for _, tag := range tags { if strings.Contains(tag, ":") { logs.Err.Fatalln("tags restricted by auth handler should not contain character ':'", tag) } globals.immutableTagNS[tag] = true } } } // Process validators. for name, vconf := range config.Validator { // Check if validator is restrictive. If so, add validator name to the list of restricted tags. // The namespace can be restricted even if the validator is disabled. if vconf.AddToTags { if strings.Contains(name, ":") { logs.Err.Fatalln("acc_validation names should not contain character ':'", name) } globals.immutableTagNS[name] = true } if len(vconf.Required) == 0 { // Skip disabled validator. continue } var reqLevels []auth.Level for _, req := range vconf.Required { lvl := auth.ParseAuthLevel(req) if lvl == auth.LevelNone { logs.Err.Fatalf("Invalid required AuthLevel '%s' in validator '%s'", req, name) } reqLevels = append(reqLevels, lvl) if globals.authValidators == nil { globals.authValidators = make(map[auth.Level][]string) } globals.authValidators[lvl] = append(globals.authValidators[lvl], name) } if val := store.Store.GetValidator(name); val == nil { logs.Err.Fatal("Config provided for an unknown validator '" + name + "'") } else if err = val.Init(string(vconf.Config)); err != nil { logs.Err.Fatal("Failed to init validator '"+name+"': ", err) } if globals.validators == nil { globals.validators = make(map[string]credValidator) } globals.validators[name] = credValidator{ requiredAuthLvl: reqLevels, addToTags: vconf.AddToTags, } } // Create credential validator config for clients. if len(globals.authValidators) > 0 { globals.validatorClientConfig = make(map[string][]string) for key, val := range globals.authValidators { globals.validatorClientConfig[key.String()] = val } } // Partially restricted tag namespaces. globals.maskedTagNS = make(map[string]bool, len(config.MaskedTagNamespaces)) for _, tag := range config.MaskedTagNamespaces { if strings.Contains(tag, ":") { logs.Err.Fatal("masked_tags namespaces should not contain character ':'", tag) } globals.maskedTagNS[tag] = true } // Alias namespace. config.AliasTagNamespace = strings.TrimSpace(config.AliasTagNamespace) if config.AliasTagNamespace != "" { if prefix, _ := validateTag(config.AliasTagNamespace + ":testing"); prefix == "" { logs.Err.Fatal("alias_tag namespace should contain only alphanumeric characters and '_'", config.AliasTagNamespace) } globals.aliasTagNS = config.AliasTagNamespace } var tags []string for tag := range globals.immutableTagNS { tags = append(tags, "'"+tag+"'") } if len(tags) > 0 { logs.Info.Println("Restricted tags:", tags) } tags = nil for tag := range globals.maskedTagNS { tags = append(tags, "'"+tag+"'") } if len(tags) > 0 { logs.Info.Println("Masked tags:", tags) } if len(globals.aliasTagNS) > 0 { logs.Info.Println("Alias tag:", globals.aliasTagNS) } // Maximum message size globals.maxMessageSize = int64(config.MaxMessageSize) if globals.maxMessageSize <= 0 { globals.maxMessageSize = defaultMaxMessageSize } // Maximum number of group topic subscribers globals.maxSubscriberCount = config.MaxSubscriberCount if globals.maxSubscriberCount <= 1 { globals.maxSubscriberCount = defaultMaxSubscriberCount } // Maximum number of indexable tags per user or topics globals.maxTagCount = config.MaxTagCount if globals.maxTagCount <= 0 { globals.maxTagCount = defaultMaxTagCount } // If account deletion is disabled. globals.permanentAccounts = config.PermanentAccounts globals.useXForwardedFor = config.UseXForwardedFor globals.defaultCountryCode = config.DefaultCountryCode if globals.defaultCountryCode == "" { globals.defaultCountryCode = defaultCountryCode } // Default access mode for P2P: with/without the D permission. globals.typesModeCP2P = types.ModeCP2P if config.P2PDeleteEnabled { globals.typesModeCP2P = types.ModeCP2PD } if config.MsgDeleteAge > 0 { globals.msgDeleteAge = time.Duration(config.MsgDeleteAge) * time.Second } // Configuration of X-Frame-Options header. globals.xFrameOptions = config.XFrameOptions if globals.xFrameOptions == "" { globals.xFrameOptions = "SAMEORIGIN" } if globals.xFrameOptions != "SAMEORIGIN" && globals.xFrameOptions != "DENY" && globals.xFrameOptions != "-" { logs.Warn.Println("Ignored invalid x_frame_options", config.XFrameOptions) globals.xFrameOptions = "SAMEORIGIN" } // Websocket compression. globals.wsCompression = !config.WSCompressionDisabled if config.Media != nil { if config.Media.UseHandler == "" { config.Media = nil } else { globals.maxFileUploadSize = config.Media.MaxFileUploadSize if config.Media.Handlers != nil { var conf string if params := config.Media.Handlers[config.Media.UseHandler]; params != nil { conf = string(params) } if err = store.Store.UseMediaHandler(config.Media.UseHandler, conf); err != nil { logs.Err.Fatalf("Failed to init media handler '%s': %s", config.Media.UseHandler, err) } } if config.Media.GcPeriod > 0 && config.Media.GcBlockSize > 0 { globals.mediaGcPeriod = time.Second * time.Duration(config.Media.GcPeriod) stopFilesGc := largeFileRunGarbageCollection(globals.mediaGcPeriod, config.Media.GcBlockSize) defer func() { stopFilesGc <- true logs.Info.Println("Stopped files garbage collector") }() } } } // Stale unvalidated user account garbage collection. if config.AccountGC != nil && config.AccountGC.Enabled { if config.AccountGC.GcPeriod <= 0 || config.AccountGC.GcBlockSize <= 0 || config.AccountGC.GcMinAccountAge <= 0 { logs.Err.Fatalln("Invalid account GC config") } gcPeriod := time.Second * time.Duration(config.AccountGC.GcPeriod) stopAccountGc := garbageCollectUsers(gcPeriod, config.AccountGC.GcBlockSize, config.AccountGC.GcMinAccountAge) defer func() { stopAccountGc <- true logs.Info.Println("Stopped account garbage collector") }() } pushHandlers, err := push.Init(config.Push) if err != nil { logs.Err.Fatal("Failed to initialize push notifications:", err) } defer func() { push.Stop() logs.Info.Println("Stopped push notifications") }() logs.Info.Println("Push handlers configured:", pushHandlers) if err = initVideoCalls(config.WebRTC); err != nil { logs.Err.Fatal("Failed to init video calls: %w", err) } // Keep inactive LP sessions for 15 seconds globals.sessionStore = NewSessionStore(idleSessionTimeout + 15*time.Second) // The hub (the main message router) globals.hub = newHub() // Start accepting cluster traffic. if globals.cluster != nil { globals.cluster.start() } tlsConfig, err := parseTLSConfig(*tlsEnabled, config.TLS) if err != nil { logs.Err.Fatalln(err) } // Initialize plugins. pluginsInit(config.Plugin) // Initialize users cache usersInit() // Set up gRPC server, if one is configured if *listenGrpc == "" { *listenGrpc = config.GrpcListen } if globals.grpcServer, err = serveGrpc(*listenGrpc, config.GrpcKeepalive, tlsConfig); err != nil { logs.Err.Fatal(err) } // Serve static content from the directory in -static_data flag if that's // available, otherwise assume '/static'. The content is served at // the path pointed by 'static_mount' in the config. If that is missing then it's // served at root '/'. var staticMountPoint string if *staticPath != "" && *staticPath != "-" { // Resolve path to static content. *staticPath = toAbsolutePath(curwd, *staticPath) if _, err = os.Stat(*staticPath); os.IsNotExist(err) { logs.Err.Fatal("Static content directory is not found", *staticPath) } staticMountPoint = config.StaticMount if staticMountPoint == "" { staticMountPoint = defaultStaticMount } else { if !strings.HasPrefix(staticMountPoint, "/") { staticMountPoint = "/" + staticMountPoint } if !strings.HasSuffix(staticMountPoint, "/") { staticMountPoint += "/" } } mux.Handle(staticMountPoint, // Add optional Cache-Control header. cacheControlHandler(config.CacheControl, // Optionally add Strict-Transport-Security and X-Frame-Options to the response. optionalHttpHeaders( // Add gzip compression. gh.CompressHandler( // And add custom formatter of errors. httpErrorHandler( // Remove mount point prefix. http.StripPrefix(staticMountPoint, http.FileServer(http.Dir(*staticPath)))))))) logs.Info.Printf("Serving static content from '%s' at '%s'", *staticPath, staticMountPoint) } else { logs.Info.Println("Static content is disabled") } // Configure root path for serving API calls. if *apiPath != "" { config.ApiPath = *apiPath } if config.ApiPath == "" { config.ApiPath = defaultApiPath } else { if !strings.HasPrefix(config.ApiPath, "/") { config.ApiPath = "/" + config.ApiPath } if !strings.HasSuffix(config.ApiPath, "/") { config.ApiPath += "/" } } logs.Info.Printf("API served from root URL path '%s'", config.ApiPath) // Best guess location of the main endpoint. // TODO: provide fix for the case when the serving is over unix sockets. // TODO: implement serving large files over gRPC, then remove globals.servingAt. globals.servingAt = config.Listen + config.ApiPath if tlsConfig != nil { globals.servingAt = "https://" + globals.servingAt } else { globals.servingAt = "http://" + globals.servingAt } sspath := *serverStatusPath if sspath == "" { sspath = config.ServerStatusPath } if sspath != "" && sspath != "-" { logs.Info.Printf("Server status is available at '%s'", sspath) mux.HandleFunc(sspath, serveStatus) } // Handle websocket clients. mux.HandleFunc(config.ApiPath+"v0/channels", serveWebSocket) // Handle long polling clients. Enable compression. mux.Handle(config.ApiPath+"v0/channels/lp", gh.CompressHandler(http.HandlerFunc(serveLongPoll))) if config.Media != nil { // Handle uploads of large files. mux.Handle(config.ApiPath+"v0/file/u/", gh.CompressHandler(http.HandlerFunc(largeFileReceiveHTTP))) // Serve large files. mux.Handle(config.ApiPath+"v0/file/s/", gh.CompressHandler(http.HandlerFunc(largeFileServeHTTP))) logs.Info.Println("Large media handling enabled", config.Media.UseHandler) } if staticMountPoint != "/" { // Serve json-formatted 404 for all other URLs mux.HandleFunc("/", serve404) } if err = listenAndServe(config.Listen, mux, tlsConfig, signalHandler()); err != nil { logs.Err.Fatal(err) } } ================================================ FILE: server/media/fs/filesys.go ================================================ // Package fs implements github.com/tinode/chat/server/media interface by storing media objects in a single // directory in the file system. // This module won't perform well with tens of thousand of files because it stores all files in a single directory. package fs import ( "encoding/base32" "encoding/json" "errors" "hash/fnv" "io" "mime" "net/http" "net/url" "os" "path/filepath" "strings" "github.com/tinode/chat/server/logs" "github.com/tinode/chat/server/media" "github.com/tinode/chat/server/store" "github.com/tinode/chat/server/store/types" ) const ( defaultServeURL = "/v0/file/s/" defaultCacheControl = "max-age=86400" handlerName = "fs" ) type fileConfig struct { // FileUploadDirectory: In case of a cluster fileUploadLocation must be accessible to all cluster members. FileUploadDirectory string `json:"upload_dir"` ServeURL string `json:"serve_url"` CorsOrigins []string `json:"cors_origins"` CacheControl string `json:"cache_control"` } type fshandler struct { fileConfig // corsOrigins parsed allowed origins. corsOrigins []media.AllowedOrigin } func (fh *fshandler) Init(jsconf string) error { var err error if err = json.Unmarshal([]byte(jsconf), &fh.fileConfig); err != nil { return errors.New("failed to parse config: " + err.Error()) } if fh.FileUploadDirectory == "" { return errors.New("missing upload location") } if fh.ServeURL == "" { fh.ServeURL = defaultServeURL } if fh.CacheControl == "" { fh.CacheControl = defaultCacheControl } fh.corsOrigins, err = media.ParseCORSAllow(fh.CorsOrigins) if err != nil { return errors.New("failed to parse CORS allowed origins: " + err.Error()) } // Make sure the upload directory exists. return os.MkdirAll(fh.FileUploadDirectory, 0777) } // Headers is used for cache management and serving CORS headers. func (fh *fshandler) Headers(method string, url *url.URL, headers http.Header, serve bool) (http.Header, int, error) { if method == http.MethodGet { fid := fh.GetIdFromUrl(url.String()) if fid.IsZero() { return nil, 0, types.ErrNotFound } fdef, err := fh.getFileRecord(fid) if err != nil { return nil, 0, err } if etag := strings.Trim(headers.Get("If-None-Match"), "\""); etag != "" && etag == fdef.ETag { return http.Header{ "Last-Modified": {fdef.UpdatedAt.Format(http.TimeFormat)}, "ETag": {`"` + fdef.ETag + `"`}, "Cache-Control": {fh.CacheControl}, }, http.StatusNotModified, nil } return http.Header{ "Content-Type": {fdef.MimeType}, "Cache-Control": {fh.CacheControl}, "ETag": {`"` + fdef.ETag + `"`}, }, 0, nil } if method != http.MethodOptions { // Not an OPTIONS request. No special handling for all other requests. return nil, 0, nil } header, status := media.CORSHandler(method, headers, fh.corsOrigins, serve) return header, status, nil } // Upload processes request for file upload. The file is given as io.Reader. func (fh *fshandler) Upload(fdef *types.FileDef, file io.Reader) (string, int64, error) { // FIXME: create two-three levels of nested directories. Serving from a single directory // with tens of thousands of files in it will not perform well. // Generate a unique file name and attach it to path. Using base32 instead of base64 to avoid possible // file name collisions on Windows due to case-insensitive file names there. location := filepath.Join(fh.FileUploadDirectory, fdef.Uid().String32()) outfile, err := os.Create(location) if err != nil { logs.Warn.Println("Upload: failed to create file", fdef.Location, err) return "", 0, err } if err = store.Files.StartUpload(fdef); err != nil { outfile.Close() os.Remove(location) logs.Warn.Println("failed to create file record", fdef.Id, err) return "", 0, err } size, err := io.Copy(outfile, file) outfile.Close() if err != nil { os.Remove(location) return "", 0, err } fname := fdef.Id ext, _ := mime.ExtensionsByType(fdef.MimeType) if len(ext) > 0 { fname += ext[0] } fdef.Location = location // Use file path to create ETag. File paths are unique so will be the ETag. fdef.ETag = etagFromPath(fdef.Location) return fh.ServeURL + fname, size, nil } // Download processes request for file download. // The returned ReadSeekCloser must be closed after use. func (fh *fshandler) Download(url string) (*types.FileDef, media.ReadSeekCloser, error) { fid := fh.GetIdFromUrl(url) if fid.IsZero() { return nil, nil, types.ErrNotFound } fd, err := fh.getFileRecord(fid) if err != nil { logs.Warn.Println("Download: file not found", fid) return nil, nil, err } file, err := os.Open(fd.Location) if err != nil { if os.IsNotExist(err) { // If the file is not found, send 404 instead of the default 500 err = types.ErrNotFound } return nil, nil, err } return fd, file, nil } // Delete deletes files from storage by provided slice of locations. func (fh *fshandler) Delete(locations []string) error { for _, loc := range locations { if err, _ := os.Remove(loc).(*os.PathError); err != nil { if err != os.ErrNotExist { logs.Warn.Println("fs: error deleting file", loc, err) } } } return nil } // GetIdFromUrl converts an attahment URL to a file UID. func (fh *fshandler) GetIdFromUrl(url string) types.Uid { return media.GetIdFromUrl(url, fh.ServeURL) } // getFileRecord given file ID reads file record from the database. func (fh *fshandler) getFileRecord(fid types.Uid) (*types.FileDef, error) { fd, err := store.Files.Get(fid.String()) if err != nil { return nil, err } if fd == nil { return nil, types.ErrNotFound } return fd, nil } func etagFromPath(path string) string { hasher := fnv.New128() hasher.Write([]byte(path)) return strings.ToLower(base32.StdEncoding.WithPadding(base32.NoPadding). EncodeToString(hasher.Sum(make([]byte, 0, hasher.Size())))) } func init() { store.RegisterMediaHandler(handlerName, &fshandler{}) } ================================================ FILE: server/media/media.go ================================================ // Package media defines an interface which must be implemented by media upload/download handlers. package media import ( "errors" "io" "net/http" "net/url" "path" "regexp" "strings" "slices" "github.com/tinode/chat/server/store/types" ) // ReadSeekCloser must be implemented by the media being downloaded. type ReadSeekCloser interface { io.Reader io.Seeker io.Closer } // Handler is an interface which must be implemented by media handlers (uploaders-downloaders). type Handler interface { // Init initializes the media upload handler. Init(jsconf string) error // Headers checks if the handler wants to provide additional HTTP headers for the request. // It could be CORS headers, redirect to serve files from another URL, cache-control headers. // It returns headers as a map, HTTP status code to stop processing or 0 to continue, error. Headers(method string, url *url.URL, headers http.Header, serve bool) (http.Header, int, error) // Upload processes request for file upload. Returns file URL, file size, error. Upload(fdef *types.FileDef, file io.Reader) (string, int64, error) // Download processes request for file download. Download(url string) (*types.FileDef, ReadSeekCloser, error) // Delete deletes file from storage. Delete(locations []string) error // GetIdFromUrl extracts file ID from download URL. GetIdFromUrl(url string) types.Uid } type AllowedOrigin struct { Origin string URL url.URL HostParts []string HasWildcard bool } var fileNamePattern = regexp.MustCompile(`^[-_A-Za-z0-9]+`) // GetIdFromUrl is a helper method for extracting file ID from a URL. func GetIdFromUrl(url, serveUrl string) types.Uid { dir, fname := path.Split(path.Clean(url)) if dir != "" && dir != serveUrl { return types.ZeroUid } return types.ParseUid(fileNamePattern.FindString(fname)) } // ParseCORSAllow pre-parses allowed origins from the configuration. func ParseCORSAllow(allowed []string) ([]AllowedOrigin, error) { if len(allowed) == 0 { return nil, nil } result := make([]AllowedOrigin, 0, len(allowed)) for _, val := range allowed { parsed := AllowedOrigin{Origin: val} switch val { case "*": if len(allowed) > 1 { return nil, errors.New("wildcard origin '*' must be the only entry") } parsed.HasWildcard = true case "": if len(allowed) > 1 { return nil, errors.New("empty allowed origin '' must be the only entry") } // Empty string means no origin allowed - no URL parsing needed parsed.HasWildcard = false default: u, err := url.ParseRequestURI(val) if err != nil { return nil, err } parsed.HostParts = strings.Split(u.Hostname(), ".") parsed.URL = *u parsed.HasWildcard = strings.Contains(u.Hostname(), "*") } result = append(result, parsed) } return result, nil } // matchCORSOrigin compares origin from the HTTP request to a list of allowed origins. func matchCORSOrigin(allowed []AllowedOrigin, origin string) string { if len(allowed) == 0 { // Not configured return "" } if origin == "" && allowed[0].Origin != "*" { // Request has no Origin header and "*" (any origin) not allowed. return "" } if allowed[0].Origin == "*" { if origin == "" { return "*" } return origin } // Check for empty string in allowed origins - this means no origin is allowed. if allowed[0].Origin == "" { return "" } originUrl, err := url.ParseRequestURI(origin) if err != nil { return "" } originParts := strings.Split(originUrl.Hostname(), ".") for _, val := range allowed { if val.Origin == origin { return origin } if !val.HasWildcard || originUrl.Scheme != val.URL.Scheme || originUrl.Port() != val.URL.Port() || len(originParts) != len(val.HostParts) { continue } matched := true for i, part := range val.HostParts { if part == "*" { continue } if part != originParts[i] { matched = false break } } if matched { return origin } } return "" } // allowMethods must be in UPPERCASE. func matchCORSMethod(allowMethods []string, method string) bool { if method == "" { // Request has no Method header. return false } return slices.Contains(allowMethods, strings.ToUpper(method)) } // CORSHandler is the default CORS processor for use by media handlers. It adds CORS headers to // preflight OPTIONS requests, Vary & Access-Control-Allow-Origin headers to all responses. func CORSHandler(method string, reqHeader http.Header, allowedOrigins []AllowedOrigin, serve bool) (http.Header, int) { respHeader := map[string][]string{ // Always add Vary because of possible intermediate caches. "Vary": {"Origin", "Access-Control-Request-Method, Access-Control-Request-Headers"}, } origin := reqHeader.Get("Origin") allowedOrigin := matchCORSOrigin(allowedOrigins, origin) if acMethod := reqHeader.Get("Access-Control-Request-Method"); method == http.MethodOptions && acMethod != "" { // Preflight request. if allowedOrigin == "" { return respHeader, http.StatusNoContent } var allowMethods []string if serve { allowMethods = []string{http.MethodGet, http.MethodHead, http.MethodOptions} } else { allowMethods = []string{http.MethodPost, http.MethodPut, http.MethodHead, http.MethodOptions} } if !matchCORSMethod(allowMethods, acMethod) { // CORS policy does not allow this method. return respHeader, http.StatusNoContent } respHeader["Access-Control-Allow-Headers"] = []string{"*"} respHeader["Access-Control-Allow-Credentials"] = []string{"true"} respHeader["Access-Control-Allow-Methods"] = []string{strings.Join(allowMethods, ", ")} respHeader["Access-Control-Max-Age"] = []string{"86400"} respHeader["Access-Control-Allow-Origin"] = []string{allowedOrigin} return respHeader, http.StatusNoContent } // Regular request, not a preflight. if allowedOrigin != "" { // Returning Origin from the actual request instead of '*', otherwise there could be an issue with Credentials. respHeader["Access-Control-Allow-Origin"] = []string{origin} } return respHeader, 0 } ================================================ FILE: server/media/media_test.go ================================================ package media import ( "strings" "testing" ) func TestMatchCORSOrigin(t *testing.T) { cases := []struct { allowed []string origin string expected string expectError bool errorMessage string }{ { allowed: []string{"https://example.com"}, origin: "https://example.com", expected: "https://example.com", }, { allowed: []string{"https://example2.com", "https://example.com"}, origin: "https://example.com", expected: "https://example.com", }, { allowed: []string{"*"}, origin: "https://example.com", expected: "https://example.com", }, { allowed: []string{""}, origin: "https://example.com", expected: "", }, { allowed: []string{}, origin: "", expected: "", }, { allowed: []string{"https://example.com"}, origin: "", expected: "", }, { allowed: []string{}, origin: "https://example.com", expected: "", }, { allowed: []string{"http://example.com"}, origin: "https://example.com", expected: "", }, { allowed: []string{"https://example.com"}, origin: "http://example.com", expected: "", }, { allowed: []string{"http://example.com:8000"}, origin: "http://example.com:8000", expected: "http://example.com:8000", }, { allowed: []string{"http://localhost:8090"}, origin: "http://localhost:8090", expected: "http://localhost:8090", }, { allowed: []string{"http://localhost:8090"}, origin: "http://localhost:8080", expected: "", }, { allowed: []string{"https://example.com"}, origin: "https://sub.example.com", expected: "", }, { allowed: []string{"https://*.example.com"}, origin: "https://sub.example.com", expected: "https://sub.example.com", }, { allowed: []string{"https://*.example.com"}, origin: "https://pre.sub.example.com", expected: "", }, { allowed: []string{"https://*.example.com", "https://*.sub.example.com"}, origin: "https://pre.sub.example.com", expected: "https://pre.sub.example.com", }, { allowed: []string{"https://*.*.example.com"}, origin: "https://pre.sub.example.com", expected: "https://pre.sub.example.com", }, { allowed: []string{"https://*.sub.example.com"}, origin: "https://pre.asd.example.com", expected: "", }, { allowed: []string{"https://pre.*.example.com"}, origin: "https://pre.sub.example.com", expected: "https://pre.sub.example.com", }, { allowed: []string{"https://*.*.*.example.com"}, origin: "https://www.pre.sub.example.com", expected: "https://www.pre.sub.example.com", }, { allowed: []string{"*"}, origin: "", expected: "*", // Should allow any origin, including empty }, // Error cases - these should fail during ParseCORSAllow { allowed: []string{"*", "https://example.com"}, origin: "https://example.com", expectError: true, errorMessage: "wildcard origin '*' must be the only entry", }, { allowed: []string{"not-a-valid-url"}, origin: "https://example.com", expectError: true, errorMessage: "invalid URI for request", }, { allowed: []string{"://invalid-url"}, origin: "https://example.com", expectError: true, errorMessage: "missing protocol scheme", }, { allowed: []string{"https://", "example.com"}, origin: "https://example.com", expectError: true, errorMessage: "invalid URI for request", }, // Valid cases that should not error { allowed: []string{"", "https://example.com"}, origin: "https://example.com", expectError: true, errorMessage: "empty allowed origin '' must be the only entry", // Empty string should make all origins disallowed }, { allowed: []string{"https://Example.com"}, origin: "https://example.com", expected: "", // Should not match due to case sensitivity }, { allowed: []string{"https://example.com/"}, origin: "https://example.com", expected: "", // Should not match due to trailing slash }, { allowed: []string{"https://example.com"}, origin: "not-a-valid-url", expected: "", // Should handle malformed origin gracefully }, { allowed: []string{"https://example.*.com"}, origin: "https://example.sub.com", expected: "https://example.sub.com", }, { allowed: []string{"https://*.com"}, origin: "https://example.com", expected: "https://example.com", }, { allowed: []string{"http://*.example.com"}, origin: "https://sub.example.com", expected: "", // Should not match due to protocol difference }, { allowed: []string{"https://example.com", "https://example.com"}, origin: "https://example.com", expected: "https://example.com", // Should still work with duplicates }, } for i, tc := range cases { allowedOrigins, err := ParseCORSAllow(tc.allowed) if tc.expectError { if err == nil { t.Errorf("Test case %d: Expected error but got none. Allowed: %v", i, tc.allowed) continue } if tc.errorMessage != "" && !containsSubstring(err.Error(), tc.errorMessage) { t.Errorf("Test case %d: Expected error containing '%s', got '%s'", i, tc.errorMessage, err.Error()) } // For error cases, we don't test the matching logic continue } if err != nil { t.Errorf("Test case %d: Unexpected error parsing allowed origins: %v. Allowed: %v", i, err, tc.allowed) continue } result := matchCORSOrigin(allowedOrigins, tc.origin) if result != tc.expected { t.Errorf("Test case %d: Match CORS origin got wrong result. Expected '%s', got '%s'. Allowed: %v, Origin: '%s'", i, tc.expected, result, tc.allowed, tc.origin) } } } // Helper function to check if a string contains a substring (case-insensitive) func containsSubstring(str, substr string) bool { return strings.Contains(strings.ToLower(str), strings.ToLower(substr)) } ================================================ FILE: server/media/s3/s3.go ================================================ // Package s3 implements media interface by storing media objects in Amazon S3 bucket. package s3 import ( "encoding/json" "errors" "io" "mime" "net/http" "net/url" "strconv" "strings" "sync/atomic" "time" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/aws/credentials" "github.com/aws/aws-sdk-go/aws/request" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/s3" "github.com/aws/aws-sdk-go/service/s3/s3manager" "github.com/tinode/chat/server/logs" "github.com/tinode/chat/server/media" "github.com/tinode/chat/server/store" "github.com/tinode/chat/server/store/types" ) const ( defaultServeURL = "/v0/file/s/" defaultCacheControl = "no-cache, must-revalidate" handlerName = "s3" // Presign GET URLs for this number of seconds. defaultPresignDuration = 120 ) type awsconfig struct { AccessKeyId string `json:"access_key_id"` SecretAccessKey string `json:"secret_access_key"` Region string `json:"region"` DisableSSL bool `json:"disable_ssl"` ForcePathStyle bool `json:"force_path_style"` Endpoint string `json:"endpoint"` BucketName string `json:"bucket"` CorsOrigins []string `json:"cors_origins"` ServeURL string `json:"serve_url"` PresignTTL int `json:"presign_ttl"` CacheControl string `json:"cache_control"` } type awshandler struct { svc *s3.S3 conf awsconfig corsOrigins []media.AllowedOrigin } // readerCounter is a byte counter for bytes read through the io.Reader type readerCounter struct { io.Reader count int64 reader io.Reader } // Read reads the bytes and records the number of read bytes. func (rc *readerCounter) Read(buf []byte) (int, error) { n, err := rc.reader.Read(buf) atomic.AddInt64(&rc.count, int64(n)) return n, err } // Init initializes the media handler. func (ah *awshandler) Init(jsconf string) error { var err error if err = json.Unmarshal([]byte(jsconf), &ah.conf); err != nil { return errors.New("failed to parse config: " + err.Error()) } if ah.conf.AccessKeyId == "" { return errors.New("missing Access Key ID") } if ah.conf.SecretAccessKey == "" { return errors.New("missing Secret Access Key") } if ah.conf.Region == "" { return errors.New("missing Region") } if ah.conf.BucketName == "" { return errors.New("missing Bucket") } if ah.conf.PresignTTL <= 0 { ah.conf.PresignTTL = defaultPresignDuration } if ah.conf.CacheControl == "" { ah.conf.CacheControl = defaultCacheControl } if ah.conf.ServeURL == "" { ah.conf.ServeURL = defaultServeURL } ah.corsOrigins, err = media.ParseCORSAllow(ah.conf.CorsOrigins) if err != nil { return errors.New("failed to parse CORS allowed origins: " + err.Error()) } var sess *session.Session if sess, err = session.NewSession(&aws.Config{ Region: aws.String(ah.conf.Region), DisableSSL: aws.Bool(ah.conf.DisableSSL), S3ForcePathStyle: aws.Bool(ah.conf.ForcePathStyle), Endpoint: aws.String(ah.conf.Endpoint), Credentials: credentials.NewStaticCredentials(ah.conf.AccessKeyId, ah.conf.SecretAccessKey, ""), }); err != nil { return err } // Create S3 service client ah.svc = s3.New(sess) // Check if bucket already exists. _, err = ah.svc.HeadBucket(&s3.HeadBucketInput{Bucket: aws.String(ah.conf.BucketName)}) if err == nil { // Bucket exists return nil } if aerr, ok := err.(awserr.Error); !ok || aerr.Code() != s3.ErrCodeNoSuchBucket { // Hard error. return err } // Bucket does not exist. Create one. _, err = ah.svc.CreateBucket(&s3.CreateBucketInput{Bucket: aws.String(ah.conf.BucketName)}) if err != nil { // Check if someone has already created a bucket (possible in a cluster). if aerr, ok := err.(awserr.Error); ok { if aerr.Code() == s3.ErrCodeBucketAlreadyExists || aerr.Code() == s3.ErrCodeBucketAlreadyOwnedByYou || // Someone is already creating this bucket: // OperationAborted: A conflicting conditional operation is currently in progress against this resource. aerr.Code() == "OperationAborted" { // Clear benign error err = nil } } } else { // This is a new bucket. // The following serves two purposes: // 1. Setup CORS policy to be able to serve media directly from S3. // 2. Verify that the bucket is accessible to the current user. origins := ah.conf.CorsOrigins if len(origins) == 0 { origins = append(origins, "*") } _, err = ah.svc.PutBucketCors(&s3.PutBucketCorsInput{ Bucket: aws.String(ah.conf.BucketName), CORSConfiguration: &s3.CORSConfiguration{ CORSRules: []*s3.CORSRule{{ AllowedMethods: aws.StringSlice([]string{http.MethodGet, http.MethodHead}), AllowedOrigins: aws.StringSlice(origins), AllowedHeaders: aws.StringSlice([]string{"*"}), }}, }, }) } return err } // Headers adds CORS headers and redirects GET and HEAD requests to the AWS server. func (ah *awshandler) Headers(method string, url *url.URL, headers http.Header, serve bool) (http.Header, int, error) { // Add CORS headers, if necessary. headers, status := media.CORSHandler(method, headers, ah.corsOrigins, serve) if status != 0 || method == http.MethodPost || method == http.MethodPut { return headers, status, nil } fid := ah.GetIdFromUrl(url.String()) if fid.IsZero() { return nil, 0, types.ErrNotFound } fdef, err := ah.getFileRecord(fid) if err != nil { return nil, 0, err } if fdef.ETag != "" && headers.Get("If-None-Match") == `"`+fdef.ETag+`"` { return http.Header{ "ETag": {`"` + fdef.ETag + `"`}, "Cache-Control": {ah.conf.CacheControl}, }, http.StatusNotModified, nil } var awsReq *request.Request switch method { case http.MethodGet: // If the query parameter "asatt" is set to a true, set Content-Disposition to attachment. // This will cause browsers to download the file rather than attempt to display it. // This closes an XSS vulnerability when users upload HTML files. var contentDisposition *string if isAttachment, _ := strconv.ParseBool(url.Query().Get("asatt")); isAttachment { contentDisposition = aws.String("attachment") } awsReq, _ = ah.svc.GetObjectRequest(&s3.GetObjectInput{ Bucket: aws.String(ah.conf.BucketName), Key: aws.String(fid.String32()), ResponseCacheControl: aws.String(ah.conf.CacheControl), ResponseContentType: aws.String(fdef.MimeType), ResponseContentDisposition: contentDisposition, }) case http.MethodHead: awsReq, _ = ah.svc.HeadObjectRequest(&s3.HeadObjectInput{ Bucket: aws.String(ah.conf.BucketName), Key: aws.String(fid.String32()), }) } if awsReq != nil { // Return presigned URL with 308 Permanent redirect. Let the client cache the response. // The original URL will stop working after a short period of time to prevent use of Tinode // as a free file server. url, err := awsReq.Presign(time.Second * time.Duration(ah.conf.PresignTTL)) return http.Header{ "Location": {url}, "ETag": {`"` + fdef.ETag + `"`}, "Content-Type": {"application/json; charset=utf-8"}, "Cache-Control": {ah.conf.CacheControl}, }, http.StatusPermanentRedirect, err } return nil, 0, nil } // Upload processes request for a file upload. The file is given as io.Reader. func (ah *awshandler) Upload(fdef *types.FileDef, file io.Reader) (string, int64, error) { var err error // Using String32 just for consistency with the file handler. key := fdef.Uid().String32() uploader := s3manager.NewUploaderWithClient(ah.svc) if err = store.Files.StartUpload(fdef); err != nil { logs.Warn.Println("failed to create file record", fdef.Id, err) return "", 0, err } rc := readerCounter{reader: file} result, err := uploader.Upload(&s3manager.UploadInput{ CacheControl: aws.String(ah.conf.CacheControl), Bucket: aws.String(ah.conf.BucketName), Key: aws.String(key), Body: &rc, }) if err != nil { return "", 0, err } fname := fdef.Id ext, _ := mime.ExtensionsByType(fdef.MimeType) if len(ext) > 0 { fname += ext[0] } fdef.Location = key if result.ETag != nil { fdef.ETag = strings.Trim(*result.ETag, "\"") } return ah.conf.ServeURL + fname, rc.count, nil } // Download processes request for file download. // The returned ReadSeekCloser must be closed after use. func (ah *awshandler) Download(url string) (*types.FileDef, media.ReadSeekCloser, error) { return nil, nil, types.ErrUnsupported } // Delete deletes files from aws by provided slice of locations. func (ah *awshandler) Delete(locations []string) error { toDelete := make([]s3manager.BatchDeleteObject, len(locations)) for i, key := range locations { toDelete[i] = s3manager.BatchDeleteObject{ Object: &s3.DeleteObjectInput{ Key: aws.String(key), Bucket: aws.String(ah.conf.BucketName), }} } batcher := s3manager.NewBatchDeleteWithClient(ah.svc) return batcher.Delete(aws.BackgroundContext(), &s3manager.DeleteObjectsIterator{ Objects: toDelete, }) } // GetIdFromUrl converts an attahment URL to a file UID. func (ah *awshandler) GetIdFromUrl(url string) types.Uid { return media.GetIdFromUrl(url, ah.conf.ServeURL) } // getFileRecord given file ID reads file record from the database. func (ah *awshandler) getFileRecord(fid types.Uid) (*types.FileDef, error) { fd, err := store.Files.Get(fid.String()) if err != nil { return nil, err } if fd == nil { return nil, types.ErrNotFound } return fd, nil } func init() { store.RegisterMediaHandler(handlerName, &awshandler{}) } ================================================ FILE: server/pbconverter.go ================================================ // Converts between protobuf structs and Go representation of packets package main import ( "encoding/json" "time" "github.com/tinode/chat/pbx" "github.com/tinode/chat/server/logs" "github.com/tinode/chat/server/store/types" ) func pbServCtrlSerializeBasic(ctrl *MsgServerCtrl) *pbx.ServerCtrl { var params map[string][]byte if ctrl.Params != nil { if in, ok := ctrl.Params.(map[string]any); ok { params = interfaceMapToByteMap(in) } } return &pbx.ServerCtrl{ Id: ctrl.Id, Topic: ctrl.Topic, Code: int32(ctrl.Code), Text: ctrl.Text, Params: params, } } func pbServCtrlSerialize(ctrl *MsgServerCtrl) *pbx.ServerMsg_Ctrl { return &pbx.ServerMsg_Ctrl{ Ctrl: pbServCtrlSerializeBasic(ctrl), } } func pbServDataSerialize(data *MsgServerData) *pbx.ServerMsg_Data { return &pbx.ServerMsg_Data{ Data: &pbx.ServerData{ Topic: data.Topic, FromUserId: data.From, Timestamp: timeToInt64(&data.Timestamp), DeletedAt: timeToInt64(data.DeletedAt), SeqId: int32(data.SeqId), Head: interfaceMapToByteMap(data.Head), Content: interfaceToBytes(data.Content), }, } } func pbServPresSerialize(pres *MsgServerPres) *pbx.ServerMsg_Pres { var what pbx.ServerPres_What switch pres.What { case "on": what = pbx.ServerPres_ON case "off": what = pbx.ServerPres_OFF case "ua": what = pbx.ServerPres_UA case "upd": what = pbx.ServerPres_UPD case "gone": what = pbx.ServerPres_GONE case "acs": what = pbx.ServerPres_ACS case "term": what = pbx.ServerPres_TERM case "msg": what = pbx.ServerPres_MSG case "read": what = pbx.ServerPres_READ case "recv": what = pbx.ServerPres_RECV case "del": what = pbx.ServerPres_DEL case "tags": what = pbx.ServerPres_TAGS case "aux": what = pbx.ServerPres_AUX default: logs.Info.Println("Unknown pres.what value", pres.What) } return &pbx.ServerMsg_Pres{ Pres: &pbx.ServerPres{ Topic: pres.Topic, Src: pres.Src, What: what, UserAgent: pres.UserAgent, SeqId: int32(pres.SeqId), DelId: int32(pres.DelId), DelSeq: pbDelQuerySerialize(pres.DelSeq), TargetUserId: pres.AcsTarget, ActorUserId: pres.AcsActor, Acs: pbAccessModeSerialize(pres.Acs), }, } } func pbServInfoSerialize(info *MsgServerInfo) *pbx.ServerMsg_Info { return &pbx.ServerMsg_Info{ Info: &pbx.ServerInfo{ Topic: info.Topic, FromUserId: info.From, Src: info.Src, What: pbInfoNoteWhatSerialize(info.What), SeqId: int32(info.SeqId), Event: pbCallEventSerialize(info.Event), Payload: info.Payload, }, } } func pbServMetaSerialize(meta *MsgServerMeta) *pbx.ServerMsg_Meta { return &pbx.ServerMsg_Meta{ Meta: &pbx.ServerMeta{ Id: meta.Id, Topic: meta.Topic, Desc: pbTopicDescSerialize(meta.Desc), Sub: pbTopicSubSliceSerialize(meta.Sub), Del: pbDelValuesSerialize(meta.Del), Tags: meta.Tags, Cred: pbServerCredsSerialize(meta.Cred), Aux: interfaceMapToByteMap(meta.Aux), }, } } // Convert ServerComMessage to pbx.ServerMsg func pbServSerialize(msg *ServerComMessage) *pbx.ServerMsg { var pkt pbx.ServerMsg switch { case msg.Ctrl != nil: pkt.Message = pbServCtrlSerialize(msg.Ctrl) case msg.Data != nil: pkt.Message = pbServDataSerialize(msg.Data) case msg.Pres != nil: pkt.Message = pbServPresSerialize(msg.Pres) case msg.Info != nil: pkt.Message = pbServInfoSerialize(msg.Info) case msg.Meta != nil: pkt.Message = pbServMetaSerialize(msg.Meta) } return &pkt } func pbServDeserialize(pkt *pbx.ServerMsg) *ServerComMessage { var msg ServerComMessage if ctrl := pkt.GetCtrl(); ctrl != nil { msg.Ctrl = &MsgServerCtrl{ Id: ctrl.GetId(), Topic: ctrl.GetTopic(), Code: int(ctrl.GetCode()), Text: ctrl.GetText(), Params: byteMapToInterfaceMap(ctrl.GetParams()), } } else if data := pkt.GetData(); data != nil { tsptr := int64ToTime(data.GetTimestamp()) if tsptr == nil { tsptr = &time.Time{} } msg.Data = &MsgServerData{ Topic: data.GetTopic(), From: data.GetFromUserId(), Timestamp: *tsptr, DeletedAt: int64ToTime(data.GetDeletedAt()), SeqId: int(data.GetSeqId()), Head: byteMapToInterfaceMap(data.GetHead()), Content: data.GetContent(), } } else if pres := pkt.GetPres(); pres != nil { var what string switch pres.GetWhat() { case pbx.ServerPres_ON: what = "on" case pbx.ServerPres_OFF: what = "off" case pbx.ServerPres_UA: what = "ua" case pbx.ServerPres_UPD: what = "upd" case pbx.ServerPres_GONE: what = "gone" case pbx.ServerPres_ACS: what = "acs" case pbx.ServerPres_TERM: what = "term" case pbx.ServerPres_MSG: what = "msg" case pbx.ServerPres_READ: what = "read" case pbx.ServerPres_RECV: what = "recv" case pbx.ServerPres_DEL: what = "del" case pbx.ServerPres_TAGS: what = "tags" case pbx.ServerPres_AUX: what = "aux" } msg.Pres = &MsgServerPres{ Topic: pres.GetTopic(), Src: pres.GetSrc(), What: what, UserAgent: pres.GetUserAgent(), SeqId: int(pres.GetSeqId()), DelId: int(pres.GetDelId()), DelSeq: pbDelQueryDeserialize(pres.GetDelSeq()), AcsTarget: pres.GetTargetUserId(), AcsActor: pres.GetActorUserId(), Acs: pbAccessModeDeserialize(pres.GetAcs()), } } else if info := pkt.GetInfo(); info != nil { msg.Info = &MsgServerInfo{ Topic: info.GetTopic(), Src: info.GetSrc(), From: info.GetFromUserId(), What: pbInfoNoteWhatDeserialize(info.GetWhat()), SeqId: int(info.GetSeqId()), Event: pbCallEventDeserialize(info.GetEvent()), Payload: info.GetPayload(), } } else if meta := pkt.GetMeta(); meta != nil { msg.Meta = &MsgServerMeta{ Id: meta.GetId(), Topic: meta.GetTopic(), Desc: pbTopicDescDeserialize(meta.GetDesc()), Sub: pbTopicSubSliceDeserialize(meta.GetSub()), Del: pbDelValuesDeserialize(meta.GetDel()), Tags: meta.GetTags(), Cred: pbServerCredsDeserialize(meta.GetCred()), Aux: byteMapToInterfaceMap(meta.GetAux()), } } return &msg } // Convert ClientComMessage to pbx.ClientMsg func pbCliSerialize(msg *ClientComMessage) *pbx.ClientMsg { var pkt pbx.ClientMsg switch { case msg.Hi != nil: pkt.Message = &pbx.ClientMsg_Hi{ Hi: &pbx.ClientHi{ Id: msg.Hi.Id, UserAgent: msg.Hi.UserAgent, Ver: msg.Hi.Version, DeviceId: msg.Hi.DeviceID, Platform: msg.Hi.Platform, Lang: msg.Hi.Lang, Background: msg.Hi.Background, }, } case msg.Acc != nil: var authLevel pbx.AuthLevel switch msg.Acc.AuthLevel { case "NONE", "none", "": authLevel = pbx.AuthLevel_NONE case "ANON", "anon": authLevel = pbx.AuthLevel_ANON case "AUTH", "auth": authLevel = pbx.AuthLevel_AUTH case "ROOT", "root": // No support for ROOT here. authLevel = pbx.AuthLevel_NONE } pkt.Message = &pbx.ClientMsg_Acc{ Acc: &pbx.ClientAcc{ Id: msg.Acc.Id, UserId: msg.Acc.User, State: msg.Acc.State, TmpScheme: msg.Acc.TmpScheme, TmpSecret: msg.Acc.TmpSecret, AuthLevel: authLevel, Scheme: msg.Acc.Scheme, Secret: msg.Acc.Secret, Login: msg.Acc.Login, Tags: msg.Acc.Tags, Cred: pbClientCredsSerialize(msg.Acc.Cred), Desc: pbSetDescSerialize(msg.Acc.Desc), }, } case msg.Login != nil: pkt.Message = &pbx.ClientMsg_Login{ Login: &pbx.ClientLogin{ Id: msg.Login.Id, Scheme: msg.Login.Scheme, Secret: msg.Login.Secret, Cred: pbClientCredsSerialize(msg.Login.Cred), }, } case msg.Sub != nil: pkt.Message = &pbx.ClientMsg_Sub{ Sub: &pbx.ClientSub{ Id: msg.Sub.Id, Topic: msg.Sub.Topic, SetQuery: pbSetQuerySerialize(msg.Sub.Set), GetQuery: pbGetQuerySerialize(msg.Sub.Get), }, } case msg.Leave != nil: pkt.Message = &pbx.ClientMsg_Leave{ Leave: &pbx.ClientLeave{ Id: msg.Leave.Id, Topic: msg.Leave.Topic, Unsub: msg.Leave.Unsub, }, } case msg.Pub != nil: pkt.Message = &pbx.ClientMsg_Pub{ Pub: &pbx.ClientPub{ Id: msg.Pub.Id, Topic: msg.Pub.Topic, NoEcho: msg.Pub.NoEcho, Head: interfaceMapToByteMap(msg.Pub.Head), Content: interfaceToBytes(msg.Pub.Content), }, } case msg.Get != nil: pkt.Message = &pbx.ClientMsg_Get{ Get: &pbx.ClientGet{ Id: msg.Get.Id, Topic: msg.Get.Topic, Query: pbGetQuerySerialize(&msg.Get.MsgGetQuery), }, } case msg.Set != nil: pkt.Message = &pbx.ClientMsg_Set{ Set: &pbx.ClientSet{ Id: msg.Set.Id, Topic: msg.Set.Topic, Query: pbSetQuerySerialize(&msg.Set.MsgSetQuery), }, } case msg.Del != nil: var what pbx.ClientDel_What switch msg.Del.What { case "msg": what = pbx.ClientDel_MSG case "topic": what = pbx.ClientDel_TOPIC case "sub": what = pbx.ClientDel_SUB case "user": what = pbx.ClientDel_USER case "cred": what = pbx.ClientDel_CRED } pkt.Message = &pbx.ClientMsg_Del{ Del: &pbx.ClientDel{ Id: msg.Del.Id, Topic: msg.Del.Topic, What: what, DelSeq: pbDelQuerySerialize(msg.Del.DelSeq), UserId: msg.Del.User, Cred: pbClientCredSerialize(msg.Del.Cred), Hard: msg.Del.Hard, }, } case msg.Note != nil: pkt.Message = &pbx.ClientMsg_Note{ Note: &pbx.ClientNote{ Topic: msg.Note.Topic, What: pbInfoNoteWhatSerialize(msg.Note.What), SeqId: int32(msg.Note.SeqId), Unread: int32(msg.Note.Unread), Event: pbCallEventSerialize(msg.Note.Event), Payload: msg.Note.Payload, }, } } if pkt.Message == nil { return nil } if msg.Extra != nil { pkt.Extra = &pbx.ClientExtra{ Attachments: msg.Extra.Attachments, OnBehalfOf: msg.Extra.AsUser, AuthLevel: pbx.AuthLevel(msg.AuthLvl), } } return &pkt } // Convert pbx.ClientMsg to ClientComMessage func pbCliDeserialize(pkt *pbx.ClientMsg) *ClientComMessage { var msg ClientComMessage if hi := pkt.GetHi(); hi != nil { msg.Hi = &MsgClientHi{ Id: hi.GetId(), UserAgent: hi.GetUserAgent(), Version: hi.GetVer(), DeviceID: hi.GetDeviceId(), Platform: hi.GetPlatform(), Lang: hi.GetLang(), Background: hi.GetBackground(), } } else if acc := pkt.GetAcc(); acc != nil { msg.Acc = &MsgClientAcc{ Id: acc.GetId(), User: acc.GetUserId(), State: acc.GetState(), TmpScheme: acc.GetTmpScheme(), TmpSecret: acc.GetTmpSecret(), AuthLevel: acc.GetAuthLevel().String(), Scheme: acc.GetScheme(), Secret: acc.GetSecret(), Login: acc.GetLogin(), Tags: acc.GetTags(), Desc: pbSetDescDeserialize(acc.GetDesc()), Cred: pbClientCredsDeserialize(acc.GetCred()), } } else if login := pkt.GetLogin(); login != nil { msg.Login = &MsgClientLogin{ Id: login.GetId(), Scheme: login.GetScheme(), Secret: login.GetSecret(), Cred: pbClientCredsDeserialize(login.GetCred()), } } else if sub := pkt.GetSub(); sub != nil { msg.Sub = &MsgClientSub{ Id: sub.GetId(), Topic: sub.GetTopic(), Get: pbGetQueryDeserialize(sub.GetGetQuery()), Set: pbSetQueryDeserialize(sub.GetSetQuery()), } } else if leave := pkt.GetLeave(); leave != nil { msg.Leave = &MsgClientLeave{ Id: leave.GetId(), Topic: leave.GetTopic(), Unsub: leave.GetUnsub(), } } else if pub := pkt.GetPub(); pub != nil { msg.Pub = &MsgClientPub{ Id: pub.GetId(), Topic: pub.GetTopic(), NoEcho: pub.GetNoEcho(), Head: byteMapToInterfaceMap(pub.GetHead()), Content: bytesToInterface(pub.GetContent()), } } else if get := pkt.GetGet(); get != nil { msg.Get = &MsgClientGet{ Id: get.GetId(), Topic: get.GetTopic(), } if gq := get.GetQuery(); gq != nil { msg.Get.MsgGetQuery = *pbGetQueryDeserialize(gq) } } else if set := pkt.GetSet(); set != nil { msg.Set = &MsgClientSet{ Id: set.GetId(), Topic: set.GetTopic(), } if sq := set.GetQuery(); sq != nil { msg.Set.MsgSetQuery = *pbSetQueryDeserialize(sq) } } else if del := pkt.GetDel(); del != nil { msg.Del = &MsgClientDel{ Id: del.GetId(), Topic: del.GetTopic(), DelSeq: pbDelQueryDeserialize(del.GetDelSeq()), User: del.GetUserId(), Cred: pbClientCredDeserialize(del.GetCred()), Hard: del.GetHard(), } switch del.GetWhat() { case pbx.ClientDel_MSG: msg.Del.What = "msg" case pbx.ClientDel_TOPIC: msg.Del.What = "topic" case pbx.ClientDel_SUB: msg.Del.What = "sub" case pbx.ClientDel_USER: msg.Del.What = "user" case pbx.ClientDel_CRED: msg.Del.What = "cred" } } else if note := pkt.GetNote(); note != nil { msg.Note = &MsgClientNote{ Topic: note.GetTopic(), SeqId: int(note.GetSeqId()), What: pbInfoNoteWhatDeserialize(note.GetWhat()), Unread: int(note.GetUnread()), Event: pbCallEventDeserialize(note.GetEvent()), Payload: note.GetPayload(), } } if extra := pkt.GetExtra(); extra != nil { msg.Extra = &MsgClientExtra{ Attachments: extra.GetAttachments(), AsUser: extra.GetOnBehalfOf(), AuthLevel: extra.GetAuthLevel().String(), } } return &msg } func interfaceMapToByteMap(in map[string]any) map[string][]byte { out := make(map[string][]byte, len(in)) for key, val := range in { if val != nil { out[key], _ = json.Marshal(val) } } return out } func byteMapToInterfaceMap(in map[string][]byte) map[string]any { out := make(map[string]any, len(in)) for key, raw := range in { if val := bytesToInterface(raw); val != nil { out[key] = val } } return out } func interfaceToBytes(in any) []byte { if in != nil { out, _ := json.Marshal(in) return out } return nil } func bytesToInterface(in []byte) any { var out any if len(in) > 0 { err := json.Unmarshal(in, &out) if err != nil { logs.Warn.Println("pbx: failed to parse bytes", string(in), err) } } return out } func timeToInt64(ts *time.Time) int64 { if ts != nil { return ts.UnixNano() / int64(time.Millisecond) } return 0 } func int64ToTime(ts int64) *time.Time { if ts > 0 { res := time.Unix(ts/1000, ts%1000).UTC() return &res } return nil } func pbGetQuerySerialize(in *MsgGetQuery) *pbx.GetQuery { if in == nil { return nil } out := &pbx.GetQuery{ What: in.What, } if in.Desc != nil { out.Desc = &pbx.GetOpts{ IfModifiedSince: timeToInt64(in.Desc.IfModifiedSince), User: in.Desc.User, Topic: in.Desc.Topic, Limit: int32(in.Desc.Limit), } } if in.Sub != nil { out.Sub = &pbx.GetOpts{ IfModifiedSince: timeToInt64(in.Sub.IfModifiedSince), User: in.Sub.User, Topic: in.Sub.Topic, Limit: int32(in.Sub.Limit), } } if in.Data != nil { out.Data = &pbx.GetOpts{ BeforeId: int32(in.Data.BeforeId), SinceId: int32(in.Data.SinceId), Limit: int32(in.Data.Limit), } if len(in.Data.IdRanges) > 0 { out.Data.Ranges = make([]*pbx.SeqRange, len(in.Data.IdRanges)) for i, dq := range in.Data.IdRanges { out.Data.Ranges[i] = &pbx.SeqRange{Low: int32(dq.LowId), Hi: int32(dq.HiId)} } } } return out } func pbGetQueryDeserialize(in *pbx.GetQuery) *MsgGetQuery { if in == nil { return nil } msg := MsgGetQuery{ What: in.GetWhat(), } if desc := in.GetDesc(); desc != nil { msg.Desc = &MsgGetOpts{ IfModifiedSince: int64ToTime(desc.GetIfModifiedSince()), Limit: int(desc.GetLimit()), } } if sub := in.GetSub(); sub != nil { msg.Sub = &MsgGetOpts{ IfModifiedSince: int64ToTime(sub.GetIfModifiedSince()), Limit: int(sub.GetLimit()), } } if data := in.GetData(); data != nil { msg.Data = &MsgGetOpts{ BeforeId: int(data.GetBeforeId()), SinceId: int(data.GetSinceId()), Limit: int(data.GetLimit()), } if ranges := data.GetRanges(); len(ranges) > 0 { msg.Data.IdRanges = make([]MsgRange, len(ranges)) for i, sr := range ranges { msg.Data.IdRanges[i].LowId = int(sr.GetLow()) msg.Data.IdRanges[i].HiId = int(sr.GetHi()) } } } return &msg } func pbSetDescSerialize(in *MsgSetDesc) *pbx.SetDesc { if in == nil { return nil } if in.DefaultAcs != nil || in.Public != nil || in.Trusted != nil || in.Private != nil { return &pbx.SetDesc{ DefaultAcs: pbDefaultAcsSerialize(in.DefaultAcs), Public: interfaceToBytes(in.Public), Trusted: interfaceToBytes(in.Trusted), Private: interfaceToBytes(in.Private), } } return nil } func pbSetDescDeserialize(in *pbx.SetDesc) *MsgSetDesc { if in == nil { return nil } defacs := pbDefaultAcsDeserialize(in.GetDefaultAcs()) public := in.GetPublic() trusted := in.GetTrusted() private := in.GetPrivate() if defacs != nil || public != nil || private != nil || trusted != nil { return &MsgSetDesc{ DefaultAcs: defacs, Public: bytesToInterface(public), Trusted: bytesToInterface(trusted), Private: bytesToInterface(private), } } return nil } func pbSetQuerySerialize(in *MsgSetQuery) *pbx.SetQuery { if in == nil { return nil } out := &pbx.SetQuery{ Desc: pbSetDescSerialize(in.Desc), } if in.Sub != nil { out.Sub = &pbx.SetSub{ UserId: in.Sub.User, Mode: in.Sub.Mode, } } out.Tags = in.Tags out.Cred = pbClientCredSerialize(in.Cred) return out } func pbSetQueryDeserialize(in *pbx.SetQuery) *MsgSetQuery { if in == nil { return nil } var msg *MsgSetQuery if desc := in.GetDesc(); desc != nil { msg = &MsgSetQuery{} msg.Desc = pbSetDescDeserialize(desc) } if sub := in.GetSub(); sub != nil { user := sub.GetUserId() mode := sub.GetMode() if user != "" || mode != "" { if msg == nil { msg = &MsgSetQuery{} } msg.Sub = &MsgSetSub{ User: sub.GetUserId(), Mode: sub.GetMode(), } } } if tags := in.GetTags(); tags != nil { if msg == nil { msg = &MsgSetQuery{} } msg.Tags = tags } if cred := in.GetCred(); cred != nil { if msg == nil { msg = &MsgSetQuery{} } msg.Cred = pbClientCredDeserialize(cred) } return msg } func pbInfoNoteWhatSerialize(what string) pbx.InfoNote { var out pbx.InfoNote switch what { case "kp": out = pbx.InfoNote_KP case "read": out = pbx.InfoNote_READ case "recv": out = pbx.InfoNote_RECV case "call": out = pbx.InfoNote_CALL default: logs.Info.Println("unknown info-note.what", what) } return out } func pbInfoNoteWhatDeserialize(what pbx.InfoNote) string { var out string switch what { case pbx.InfoNote_KP: out = "kp" case pbx.InfoNote_READ: out = "read" case pbx.InfoNote_RECV: out = "recv" case pbx.InfoNote_CALL: out = "call" default: } return out } func pbCallEventSerialize(event string) pbx.CallEvent { var out pbx.CallEvent switch event { case "accept": out = pbx.CallEvent_ACCEPT case "answer": out = pbx.CallEvent_ANSWER case "hang-up": out = pbx.CallEvent_HANG_UP case "ice-candidate": out = pbx.CallEvent_ICE_CANDIDATE case "invite": out = pbx.CallEvent_INVITE case "offer": out = pbx.CallEvent_OFFER case "ringing": out = pbx.CallEvent_RINGING case "": out = pbx.CallEvent_X2 default: logs.Info.Println("unknown call event", event) } return out } func pbCallEventDeserialize(event pbx.CallEvent) string { var out string switch event { case pbx.CallEvent_ACCEPT: out = "accept" case pbx.CallEvent_ANSWER: out = "answer" case pbx.CallEvent_HANG_UP: out = "hang-up" case pbx.CallEvent_ICE_CANDIDATE: out = "ice-candidate" case pbx.CallEvent_INVITE: out = "invite" case pbx.CallEvent_OFFER: out = "offer" case pbx.CallEvent_RINGING: out = "ringing" default: } return out } func pbAccessModeSerialize(acs *MsgAccessMode) *pbx.AccessMode { if acs == nil { return nil } return &pbx.AccessMode{ Want: acs.Want, Given: acs.Given, } } func pbAccessModeDeserialize(acs *pbx.AccessMode) *MsgAccessMode { if acs == nil { return nil } return &MsgAccessMode{ Want: acs.Want, Given: acs.Given, } } func pbDefaultAcsSerialize(defacs *MsgDefaultAcsMode) *pbx.DefaultAcsMode { if defacs == nil { return nil } return &pbx.DefaultAcsMode{ Auth: defacs.Auth, Anon: defacs.Anon, } } func pbDefaultAcsDeserialize(defacs *pbx.DefaultAcsMode) *MsgDefaultAcsMode { if defacs == nil { return nil } auth := defacs.GetAuth() anon := defacs.GetAnon() if auth != "" || anon != "" { return &MsgDefaultAcsMode{ Auth: auth, Anon: anon, } } return nil } func pbTopicDescSerialize(desc *MsgTopicDesc) *pbx.TopicDesc { if desc == nil { return nil } out := &pbx.TopicDesc{ CreatedAt: timeToInt64(desc.CreatedAt), UpdatedAt: timeToInt64(desc.UpdatedAt), TouchedAt: timeToInt64(desc.TouchedAt), State: desc.State, Online: desc.Online, IsChan: desc.IsChan, Defacs: pbDefaultAcsSerialize(desc.DefaultAcs), Acs: pbAccessModeSerialize(desc.Acs), SeqId: int32(desc.SeqId), ReadId: int32(desc.ReadSeqId), RecvId: int32(desc.RecvSeqId), DelId: int32(desc.DelId), Public: interfaceToBytes(desc.Public), Trusted: interfaceToBytes(desc.Trusted), Private: interfaceToBytes(desc.Private), } if desc.LastSeen != nil { out.LastSeenTime = timeToInt64(desc.LastSeen.When) out.LastSeenUserAgent = desc.LastSeen.UserAgent } return out } func pbTopicDescDeserialize(desc *pbx.TopicDesc) *MsgTopicDesc { if desc == nil { return nil } out := &MsgTopicDesc{ CreatedAt: int64ToTime(desc.GetCreatedAt()), UpdatedAt: int64ToTime(desc.GetUpdatedAt()), TouchedAt: int64ToTime(desc.GetTouchedAt()), State: desc.GetState(), Online: desc.GetOnline(), IsChan: desc.GetIsChan(), DefaultAcs: pbDefaultAcsDeserialize(desc.GetDefacs()), Acs: pbAccessModeDeserialize(desc.GetAcs()), SeqId: int(desc.SeqId), ReadSeqId: int(desc.ReadId), RecvSeqId: int(desc.RecvId), DelId: int(desc.DelId), Public: bytesToInterface(desc.Public), Trusted: bytesToInterface(desc.Trusted), Private: bytesToInterface(desc.Private), } if desc.GetLastSeenTime() > 0 { out.LastSeen = &MsgLastSeenInfo{ When: int64ToTime(desc.GetLastSeenTime()), UserAgent: desc.GetLastSeenUserAgent(), } } return out } func pbTopicSerializeToDesc(topic *Topic) *pbx.TopicDesc { if topic == nil { return nil } return &pbx.TopicDesc{ CreatedAt: timeToInt64(&topic.created), UpdatedAt: timeToInt64(&topic.updated), Defacs: &pbx.DefaultAcsMode{ Auth: topic.accessAuth.String(), Anon: topic.accessAnon.String(), }, SeqId: int32(topic.lastID), DelId: int32(topic.delID), Public: interfaceToBytes(topic.public), Trusted: interfaceToBytes(topic.trusted), } } func pbTopicSubSliceSerialize(subs []MsgTopicSub) []*pbx.TopicSub { if len(subs) == 0 { return nil } out := make([]*pbx.TopicSub, len(subs)) for i := range subs { out[i] = pbTopicSubSerialize(&subs[i]) } return out } func pbTopicSubSerialize(sub *MsgTopicSub) *pbx.TopicSub { out := &pbx.TopicSub{ UpdatedAt: timeToInt64(sub.UpdatedAt), DeletedAt: timeToInt64(sub.DeletedAt), Online: sub.Online, Acs: pbAccessModeSerialize(&sub.Acs), ReadId: int32(sub.ReadSeqId), RecvId: int32(sub.RecvSeqId), Public: interfaceToBytes(sub.Public), Trusted: interfaceToBytes(sub.Trusted), Private: interfaceToBytes(sub.Private), UserId: sub.User, Topic: sub.Topic, TouchedAt: timeToInt64(sub.TouchedAt), SeqId: int32(sub.SeqId), DelId: int32(sub.DelId), } if sub.LastSeen != nil { out.LastSeenTime = timeToInt64(sub.LastSeen.When) out.LastSeenUserAgent = sub.LastSeen.UserAgent } return out } func pbTopicSubSliceDeserialize(subs []*pbx.TopicSub) []MsgTopicSub { if len(subs) == 0 { return nil } out := make([]MsgTopicSub, len(subs)) for i := range subs { out[i] = MsgTopicSub{ UpdatedAt: int64ToTime(subs[i].GetUpdatedAt()), DeletedAt: int64ToTime(subs[i].GetDeletedAt()), Online: subs[i].GetOnline(), ReadSeqId: int(subs[i].GetReadId()), RecvSeqId: int(subs[i].GetRecvId()), Public: bytesToInterface(subs[i].GetPublic()), Trusted: bytesToInterface(subs[i].GetTrusted()), Private: bytesToInterface(subs[i].GetPrivate()), User: subs[i].GetUserId(), Topic: subs[i].GetTopic(), TouchedAt: int64ToTime(subs[i].GetTouchedAt()), SeqId: int(subs[i].GetSeqId()), DelId: int(subs[i].GetDelId()), } if acs := subs[i].GetAcs(); acs != nil { out[i].Acs = *pbAccessModeDeserialize(acs) } if subs[i].GetLastSeenTime() > 0 { out[i].LastSeen = &MsgLastSeenInfo{ When: int64ToTime(subs[i].GetLastSeenTime()), UserAgent: subs[i].GetLastSeenUserAgent(), } } } return out } func pbSubSliceDeserialize(subs []*pbx.TopicSub) []types.Subscription { if len(subs) == 0 { return nil } out := make([]types.Subscription, len(subs)) for i := range subs { out[i] = types.Subscription{ ObjHeader: types.ObjHeader{ UpdatedAt: *int64ToTime(subs[i].GetUpdatedAt()), }, DeletedAt: int64ToTime(subs[i].GetDeletedAt()), User: subs[i].GetUserId(), Topic: subs[i].GetTopic(), DelId: int(subs[i].GetDelId()), Private: bytesToInterface(subs[i].GetPrivate()), } out[i].SetPublic(bytesToInterface(subs[i].GetPublic())) out[i].SetTrusted(bytesToInterface(subs[i].GetTrusted())) if acs := subs[i].GetAcs(); acs != nil { out[i].ModeGiven.UnmarshalText([]byte(acs.GetGiven())) out[i].ModeWant.UnmarshalText([]byte(acs.GetWant())) } if subs[i].GetLastSeenTime() > 0 { out[i].SetLastSeenAndUA(int64ToTime(subs[i].GetLastSeenTime()), subs[i].GetLastSeenUserAgent()) } } return out } func pbDelQuerySerialize(in []MsgRange) []*pbx.SeqRange { if in == nil { return nil } out := make([]*pbx.SeqRange, len(in)) for i, dq := range in { out[i] = &pbx.SeqRange{Low: int32(dq.LowId), Hi: int32(dq.HiId)} } return out } func pbDelQueryDeserialize(in []*pbx.SeqRange) []MsgRange { if in == nil { return nil } out := make([]MsgRange, len(in)) for i, sr := range in { out[i].LowId = int(sr.GetLow()) out[i].HiId = int(sr.GetHi()) } return out } func pbDelValuesSerialize(in *MsgDelValues) *pbx.DelValues { if in == nil { return nil } return &pbx.DelValues{ DelId: int32(in.DelId), DelSeq: pbDelQuerySerialize(in.DelSeq), } } func pbDelValuesDeserialize(in *pbx.DelValues) *MsgDelValues { if in == nil { return nil } return &MsgDelValues{ DelId: int(in.GetDelId()), DelSeq: pbDelQueryDeserialize(in.GetDelSeq()), } } func pbClientCredSerialize(in *MsgCredClient) *pbx.ClientCred { if in == nil { return nil } return &pbx.ClientCred{ Method: in.Method, Value: in.Value, Response: in.Response, Params: interfaceMapToByteMap(in.Params), } } func pbClientCredsSerialize(in []MsgCredClient) []*pbx.ClientCred { if in == nil { return nil } out := make([]*pbx.ClientCred, len(in)) for i := range in { out[i] = pbClientCredSerialize(&in[i]) } return out } func pbClientCredDeserialize(in *pbx.ClientCred) *MsgCredClient { if in == nil { return nil } return &MsgCredClient{ Method: in.GetMethod(), Value: in.GetValue(), Response: in.GetResponse(), Params: byteMapToInterfaceMap(in.GetParams()), } } func pbClientCredsDeserialize(in []*pbx.ClientCred) []MsgCredClient { if in == nil { return nil } out := make([]MsgCredClient, len(in)) for i, cr := range in { out[i] = *pbClientCredDeserialize(cr) } return out } func pbServerCredsSerialize(in []*MsgCredServer) []*pbx.ServerCred { if in == nil { return nil } out := make([]*pbx.ServerCred, len(in)) for i, cr := range in { out[i] = &pbx.ServerCred{ Method: cr.Method, Value: cr.Value, } } return out } func pbServerCredsDeserialize(in []*pbx.ServerCred) []*MsgCredServer { if in == nil { return nil } out := make([]*MsgCredServer, len(in)) for i, cr := range in { out[i] = &MsgCredServer{ Method: cr.GetMethod(), Value: cr.GetValue(), Done: cr.GetDone(), } } return out } ================================================ FILE: server/plugins.go ================================================ // External services contacted through RPC package main import ( "context" "encoding/json" "errors" "strings" "time" "github.com/tinode/chat/pbx" "github.com/tinode/chat/server/logs" "github.com/tinode/chat/server/store/types" "google.golang.org/grpc" ) const ( plgHi = 1 << iota plgAcc plgLogin plgSub plgLeave plgPub plgGet plgSet plgDel plgNote plgData plgMeta plgPres plgInfo plgClientMask = plgHi | plgAcc | plgLogin | plgSub | plgLeave | plgPub | plgGet | plgSet | plgDel | plgNote plgServerMask = plgData | plgMeta | plgPres | plgInfo ) const ( plgActCreate = 1 << iota plgActUpd plgActDel plgActMask = plgActCreate | plgActUpd | plgActDel ) const ( plgTopicMe = 1 << iota plgTopicFnd plgTopicP2P plgTopicGrp plgTopicSys plgTopicSlf plgTopicNew plgTopicNch plgTopicCatMask = plgTopicMe | plgTopicFnd | plgTopicP2P | plgTopicGrp | plgTopicSys | plgTopicSlf ) const ( plgFilterByTopicType = 1 << iota plgFilterByPacket plgFilterByAction ) var ( plgPacketNames = []string{ "hi", "acc", "login", "sub", "leave", "pub", "get", "set", "del", "note", "data", "meta", "pres", "info", } plgTopicCatNames = []string{"me", "fnd", "p2p", "grp", "sys", "slf", "new", "nch"} ) // PluginFilter is a enum which defines filtering types. type PluginFilter struct { byPacket int byTopicType int byAction int } // ParsePluginFilter parses filter config string. func ParsePluginFilter(s *string, filterBy int) (*PluginFilter, error) { if s == nil { return nil, nil } parseByName := func(parts []string, options []string, def int) (int, error) { var result int // Iterate over filter parts for _, inp := range parts { if inp != "" { inp = strings.ToLower(inp) // Split string like "hi,login,pres" or "me,p2p,fnd" values := strings.Split(inp, ",") // For each value in the input string, try to find it in the options set for _, val := range values { i := 0 // Iterate over the options, i.e find "hi" in the slice of packet names for i = range options { if options[i] == val { result |= 1 << uint(i) break } } if result != 0 && i == len(options) { // Mix of known and unknown options in the input return 0, errors.New("plugin: unknown value in filter " + val) } } if result != 0 { // Found and parsed the right part break } } } // If the filter value is not defined, use default. if result == 0 { result = def } return result, nil } parseAction := func(parts []string) int { var result int for _, inp := range parts { Loop: for _, char := range inp { switch char { case 'c', 'C': result |= plgActCreate case 'u', 'U': result |= plgActUpd case 'd', 'D': result |= plgActDel default: // Unknown symbol means this is not an action string. result = 0 break Loop } } if result != 0 { // Found and parsed actions. break } } if result == 0 { result = plgActMask } return result } filter := PluginFilter{} parts := strings.Split(*s, ";") var err error if filterBy&plgFilterByPacket != 0 { if filter.byPacket, err = parseByName(parts, plgPacketNames, plgClientMask); err != nil { return nil, err } } if filterBy&plgFilterByTopicType != 0 { if filter.byTopicType, err = parseByName(parts, plgTopicCatNames, plgTopicCatMask); err != nil { return nil, err } } if filterBy&plgFilterByAction != 0 { filter.byAction = parseAction(parts) } return &filter, nil } // PluginRPCFilterConfig filters for an individual RPC call. Filter strings are formatted as follows: // ; ; // For instance: // "acc,login;;CU" - grab packets {acc} or {login}; no filtering by topic, Create or Update action // "pub,pres;me,p2p;" type pluginRPCFilterConfig struct { // Filter by packet name, topic type [or exact name - not supported yet]. 2D: "pub,pres;p2p,me" FireHose *string `json:"fire_hose"` // Filter by CUD, [exact user name - not supported yet]. 1D: "C" Account *string `json:"account"` // Filter by CUD, topic type[, exact name]: "p2p;CU" Topic *string `json:"topic"` // Filter by CUD, topic type[, exact topic name, exact user name]: "CU" Subscription *string `json:"subscription"` // Filter by C.D, topic type[, exact topic name, exact user name]: "grp;CD" Message *string `json:"message"` // Call Find service, true or false Find bool } type pluginConfig struct { Enabled bool `json:"enabled"` // Unique service name Name string `json:"name"` // Microseconds to wait before timeout Timeout int64 `json:"timeout"` // Filters for RPC calls: when to call vs when to skip the call Filters pluginRPCFilterConfig `json:"filters"` // What should the server do if plugin failed: HTTP error code FailureCode int `json:"failure_code"` // HTTP Error message to go with the code FailureMessage string `json:"failure_text"` // Address of plugin server of the form "tcp://localhost:123" or "unix://path_to_socket_file" ServiceAddr string `json:"service_addr"` } // Plugin defines client-side parameters of a gRPC plugin. type Plugin struct { name string timeout time.Duration // Filters for individual methods filterFireHose *PluginFilter filterAccount *PluginFilter filterTopic *PluginFilter filterSubscription *PluginFilter filterMessage *PluginFilter filterFind bool failureCode int failureText string network string addr string conn *grpc.ClientConn client pbx.PluginClient } func pluginsInit(configString json.RawMessage) { // Check if any plugins are defined if len(configString) == 0 { return } var config []pluginConfig if err := json.Unmarshal(configString, &config); err != nil { logs.Err.Fatal(err) } nameIndex := make(map[string]bool) globals.plugins = make([]Plugin, len(config)) count := 0 for i := range config { conf := &config[i] if !conf.Enabled { continue } if nameIndex[conf.Name] { logs.Err.Fatalf("plugins: duplicate name '%s'", conf.Name) } globals.plugins[count] = Plugin{ name: conf.Name, timeout: time.Duration(conf.Timeout) * time.Microsecond, failureCode: conf.FailureCode, failureText: conf.FailureMessage, } var err error if globals.plugins[count].filterFireHose, err = ParsePluginFilter(conf.Filters.FireHose, plgFilterByTopicType|plgFilterByPacket); err != nil { logs.Err.Fatal("plugins: bad FireHose filter", err) } if globals.plugins[count].filterAccount, err = ParsePluginFilter(conf.Filters.Account, plgFilterByAction); err != nil { logs.Err.Fatal("plugins: bad Account filter", err) } if globals.plugins[count].filterTopic, err = ParsePluginFilter(conf.Filters.Topic, plgFilterByTopicType|plgFilterByAction); err != nil { logs.Err.Fatal("plugins: bad Topic filter", err) } if globals.plugins[count].filterSubscription, err = ParsePluginFilter(conf.Filters.Subscription, plgFilterByTopicType|plgFilterByAction); err != nil { logs.Err.Fatal("plugins: bad Subscription filter", err) } if globals.plugins[count].filterMessage, err = ParsePluginFilter(conf.Filters.Message, plgFilterByTopicType|plgFilterByAction); err != nil { logs.Err.Fatal("plugins: bad Message filter", err) } globals.plugins[count].filterFind = conf.Filters.Find if parts := strings.SplitN(conf.ServiceAddr, "://", 2); len(parts) < 2 { logs.Err.Fatal("plugins: invalid server address format", conf.ServiceAddr) } else { globals.plugins[count].network = parts[0] globals.plugins[count].addr = parts[1] } globals.plugins[count].conn, err = grpc.Dial(globals.plugins[count].addr, grpc.WithInsecure()) if err != nil { logs.Err.Fatalf("plugins: connection failure %v", err) } globals.plugins[count].client = pbx.NewPluginClient(globals.plugins[count].conn) nameIndex[conf.Name] = true count++ } globals.plugins = globals.plugins[:count] if len(globals.plugins) == 0 { logs.Info.Println("plugins: no active plugins found") globals.plugins = nil } else { var names []string for i := range globals.plugins { names = append(names, globals.plugins[i].name+"("+globals.plugins[i].addr+")") } logs.Info.Println("plugins: active", "'"+strings.Join(names, "', '")+"'") } } func pluginsShutdown() { if globals.plugins == nil { return } for i := range globals.plugins { globals.plugins[i].conn.Close() } } func pluginGenerateClientReq(sess *Session, msg *ClientComMessage) *pbx.ClientReq { cmsg := pbCliSerialize(msg) if cmsg == nil { return nil } return &pbx.ClientReq{ Msg: cmsg, Sess: &pbx.Session{ SessionId: sess.sid, UserId: sess.uid.UserId(), AuthLevel: pbx.AuthLevel(sess.authLvl), UserAgent: sess.userAgent, RemoteAddr: sess.remoteAddr, DeviceId: sess.deviceID, Language: sess.lang, }, } } func pluginFireHose(sess *Session, msg *ClientComMessage) (*ClientComMessage, *ServerComMessage) { if globals.plugins == nil { // Return the original message to continue processing without changes return msg, nil } var req *pbx.ClientReq id, topic := pluginIDAndTopic(msg) ts := time.Now().UTC().Round(time.Millisecond) for i := range globals.plugins { p := &globals.plugins[i] if !pluginDoFiltering(p.filterFireHose, msg) { // Plugin is not interested in FireHose continue } if req == nil { // Generate request only if needed req = pluginGenerateClientReq(sess, msg) if req == nil { // Failed to serialize message. Most likely the message is invalid. break } } var ctx context.Context var cancel context.CancelFunc if p.timeout > 0 { ctx, cancel = context.WithTimeout(context.Background(), p.timeout) defer cancel() } else { ctx = context.Background() } if resp, err := p.client.FireHose(ctx, req); err == nil { respStatus := resp.GetStatus() // CONTINUE means default processing if respStatus == pbx.RespCode_CONTINUE { continue } // DROP means stop processing of the message if respStatus == pbx.RespCode_DROP { return nil, nil } // REPLACE: ClientMsg was updated by the plugin. Use the new one for further processing. if respStatus == pbx.RespCode_REPLACE { return pbCliDeserialize(resp.GetClmsg()), nil } // RESPOND: Plugin provided an alternative response message. Use it return nil, pbServDeserialize(resp.GetSrvmsg()) } else if p.failureCode != 0 { // Plugin failed and it's configured to stop further processing. logs.Err.Println("plugin: failed,", p.name, err) return nil, &ServerComMessage{ Ctrl: &MsgServerCtrl{ Id: id, Code: p.failureCode, Text: p.failureText, Topic: topic, Timestamp: ts, }, } } else { // Plugin failed but configured to ignore failure. logs.Warn.Println("plugin: failure ignored,", p.name, err) } } return msg, nil } // Ask plugin to perform search. func pluginFind(user types.Uid, query string) (string, []types.Subscription, error) { if globals.plugins == nil { return query, nil, nil } find := &pbx.SearchQuery{ UserId: user.UserId(), Query: query, } for i := range globals.plugins { p := &globals.plugins[i] if !p.filterFind { // Plugin cannot service Find requests continue } var ctx context.Context var cancel context.CancelFunc if p.timeout > 0 { ctx, cancel = context.WithTimeout(context.Background(), p.timeout) defer cancel() } else { ctx = context.Background() } resp, err := p.client.Find(ctx, find) if err != nil { logs.Warn.Println("plugins: Find call failed", p.name, err) return "", nil, err } respStatus := resp.GetStatus() // CONTINUE means default processing if respStatus == pbx.RespCode_CONTINUE { continue } // DROP means stop processing the request if respStatus == pbx.RespCode_DROP { return "", nil, nil } // REPLACE: query string was changed. Use the new one for further processing. if respStatus == pbx.RespCode_REPLACE { return resp.GetQuery(), nil, nil } // RESPOND: Plugin provided a specific response. Use it return "", pbSubSliceDeserialize(resp.GetResult()), nil } return query, nil, nil } func pluginAccount(user *types.User, action int) { if globals.plugins == nil { return } var event *pbx.AccountEvent for i := range globals.plugins { p := &globals.plugins[i] if p.filterAccount == nil || p.filterAccount.byAction&action == 0 { // Plugin is not interested in Account actions continue } if event == nil { event = &pbx.AccountEvent{ Action: pluginActionToCrud(action), UserId: user.Uid().UserId(), DefaultAcs: pbDefaultAcsSerialize(&MsgDefaultAcsMode{ Auth: user.Access.Auth.String(), Anon: user.Access.Anon.String(), }), Public: interfaceToBytes(user.Public), Tags: user.Tags, } } var ctx context.Context var cancel context.CancelFunc if p.timeout > 0 { ctx, cancel = context.WithTimeout(context.Background(), p.timeout) defer cancel() } else { ctx = context.Background() } if _, err := p.client.Account(ctx, event); err != nil { logs.Warn.Println("plugins: Account call failed", p.name, err) } } } func pluginTopic(topic *Topic, action int) { if globals.plugins == nil { return } var event *pbx.TopicEvent for i := range globals.plugins { p := &globals.plugins[i] if p.filterTopic == nil || p.filterTopic.byAction&action == 0 { // Plugin is not interested in Message actions continue } if event == nil { event = &pbx.TopicEvent{ Action: pluginActionToCrud(action), Name: topic.name, Desc: pbTopicSerializeToDesc(topic), } } var ctx context.Context var cancel context.CancelFunc if p.timeout > 0 { ctx, cancel = context.WithTimeout(context.Background(), p.timeout) defer cancel() } else { ctx = context.Background() } if _, err := p.client.Topic(ctx, event); err != nil { logs.Warn.Println("plugins: Topic call failed", p.name, err) } } } func pluginSubscription(sub *types.Subscription, action int) { if globals.plugins == nil { return } var event *pbx.SubscriptionEvent for i := range globals.plugins { p := &globals.plugins[i] if p.filterSubscription == nil || p.filterSubscription.byAction&action == 0 { // Plugin is not interested in Message actions continue } if event == nil { event = &pbx.SubscriptionEvent{ Action: pluginActionToCrud(action), Topic: sub.Topic, UserId: sub.User, DelId: int32(sub.DelId), ReadId: int32(sub.ReadSeqId), RecvId: int32(sub.RecvSeqId), Mode: &pbx.AccessMode{ Want: sub.ModeWant.String(), Given: sub.ModeGiven.String(), }, Private: interfaceToBytes(sub.Private), } } var ctx context.Context var cancel context.CancelFunc if p.timeout > 0 { ctx, cancel = context.WithTimeout(context.Background(), p.timeout) defer cancel() } else { ctx = context.Background() } if _, err := p.client.Subscription(ctx, event); err != nil { logs.Warn.Println("plugins: Subscription call failed", p.name, err) } } } // Message accepted for delivery func pluginMessage(data *MsgServerData, action int) { if globals.plugins == nil || action != plgActCreate { return } var event *pbx.MessageEvent for i := range globals.plugins { p := &globals.plugins[i] if p.filterMessage == nil || p.filterMessage.byAction&action == 0 { // Plugin is not interested in Message actions continue } if event == nil { event = &pbx.MessageEvent{ Action: pluginActionToCrud(action), Msg: pbServDataSerialize(data).Data, } } var ctx context.Context var cancel context.CancelFunc if p.timeout > 0 { ctx, cancel = context.WithTimeout(context.Background(), p.timeout) defer cancel() } else { ctx = context.Background() } if _, err := p.client.Message(ctx, event); err != nil { logs.Warn.Println("plugins: Message call failed", p.name, err) } } } // Returns false to skip, true to process func pluginDoFiltering(filter *PluginFilter, msg *ClientComMessage) bool { filterByTopic := func(topic string, flt int) bool { if topic == "" || flt == plgTopicCatMask { return true } tt := topic if len(tt) > 3 { tt = topic[:3] } switch tt { case "me": return flt&plgTopicMe != 0 case "fnd": return flt&plgTopicFnd != 0 case "usr": return flt&plgTopicP2P != 0 case "grp": return flt&plgTopicGrp != 0 case "sys": return flt&plgTopicSys != 0 case "slf": return flt&plgTopicSlf != 0 case "new": return flt&plgTopicNew != 0 case "nch": return flt&plgTopicNch != 0 } return false } // Check if plugin has any filters for this call if filter == nil || filter.byPacket == 0 { return false } // Check if plugin wants all the messages if filter.byPacket == plgClientMask && filter.byTopicType == plgTopicCatMask { return true } // Check individual bits if msg.Hi != nil { return filter.byPacket&plgHi != 0 } if msg.Acc != nil { return filter.byPacket&plgAcc != 0 } if msg.Login != nil { return filter.byPacket&plgLogin != 0 } if msg.Sub != nil { return filter.byPacket&plgSub != 0 && filterByTopic(msg.Sub.Topic, filter.byTopicType) } if msg.Leave != nil { return filter.byPacket&plgLeave != 0 && filterByTopic(msg.Leave.Topic, filter.byTopicType) } if msg.Pub != nil { return filter.byPacket&plgPub != 0 && filterByTopic(msg.Pub.Topic, filter.byTopicType) } if msg.Get != nil { return filter.byPacket&plgGet != 0 && filterByTopic(msg.Get.Topic, filter.byTopicType) } if msg.Set != nil { return filter.byPacket&plgSet != 0 && filterByTopic(msg.Set.Topic, filter.byTopicType) } if msg.Del != nil { return filter.byPacket&plgDel != 0 && filterByTopic(msg.Del.Topic, filter.byTopicType) } if msg.Note != nil { return filter.byPacket&plgNote != 0 && filterByTopic(msg.Note.Topic, filter.byTopicType) } return false } func pluginActionToCrud(action int) pbx.Crud { switch action { case plgActCreate: return pbx.Crud_CREATE case plgActUpd: return pbx.Crud_UPDATE case plgActDel: return pbx.Crud_DELETE } panic("plugin: unknown action") } // pluginIDAndTopic extracts message ID and topic name. func pluginIDAndTopic(msg *ClientComMessage) (string, string) { if msg.Hi != nil { return msg.Hi.Id, "" } if msg.Acc != nil { return msg.Acc.Id, "" } if msg.Login != nil { return msg.Login.Id, "" } if msg.Sub != nil { return msg.Sub.Id, msg.Sub.Topic } if msg.Leave != nil { return msg.Leave.Id, msg.Leave.Topic } if msg.Pub != nil { return msg.Pub.Id, msg.Pub.Topic } if msg.Get != nil { return msg.Get.Id, msg.Get.Topic } if msg.Set != nil { return msg.Set.Id, msg.Set.Topic } if msg.Del != nil { return msg.Del.Id, msg.Del.Topic } if msg.Note != nil { return "", msg.Note.Topic } return "", "" } ================================================ FILE: server/pres.go ================================================ package main import ( "encoding/json" "strings" "github.com/tinode/chat/server/logs" "github.com/tinode/chat/server/store" "github.com/tinode/chat/server/store/types" ) // presParams defines parameters for creating a presence notification. type presParams struct { userAgent string seqID int delID int delSeq []MsgRange // Uid who performed the action actor string // Subject of the action target string dWant string dGiven string } type presFilters struct { // Send messages only to users with this access mode being non-zero. filterIn types.AccessMode // Exclude users with this access mode being non-zero. filterOut types.AccessMode // Send messages to the sessions of this single user defined by ID as a string 'usrABC'. singleUser string // Do not send messages to sessions of this user defined by ID as a string 'usrABC'. excludeUser string } func (p *presParams) packAcs() *MsgAccessMode { if p.dWant != "" || p.dGiven != "" { return &MsgAccessMode{Want: p.dWant, Given: p.dGiven} } return nil } // Presence: Add another user to the list of contacts to notify of presence and other changes func (t *Topic) addToPerSubs(topic string, online, enabled bool) { if topic == t.name { // No need to push updates to self return } // TODO: maybe skip loading channel subscriptions. They can's send or receive these notifications anyway. if uid1, uid2, err := types.ParseP2P(topic); err == nil { // If this is a P2P topic, index it by second user's ID if uid1.UserId() == t.name { topic = uid2.UserId() } else { topic = uid1.UserId() } } t.perSubs[topic] = perSubsData{online: online, enabled: enabled} } // loadContacts loads topic.perSubs to support presence notifications. // perSubs contains (a) topics that the user wants to notify of his presence and // (b) those which want to receive notifications from this user. func (t *Topic) loadContacts(uid types.Uid) error { subs, err := store.Users.GetSubs(uid) if err != nil { return err } for i := range subs { t.addToPerSubs(subs[i].Topic, false, (subs[i].ModeGiven & subs[i].ModeWant).IsPresencer()) } return nil } // This topic got a request from a 'me' topic to start/stop sending presence updates. // The originating topic reports its own status in 'what' as "on", "off", "gone" or "?unkn". // // "on" - requester came online // "off" - requester is offline now // "?none" - anchor for "+" command: requester status is unknown, won't generate a response // and isn't forwarded to clients. // "gone" - topic deleted or otherwise gone - equivalent of "off+remove" // "?unkn" - requester wants to initiate online status exchange but it's own status is unknown yet. This // notifications is not forwarded to users. // // "+" commands: // "+en": enable subscription, i.e. start accepting incoming notifications from the user2; // "+rem": terminate and remove the subscription (subscription deleted) // "+dis" disable subscription withot removing it, the opposite of "en". // The "+en/rem/dis" command itself is stripped from the notification. func (t *Topic) procPresReq(fromUserID, what string, wantReply bool) string { if t.isInactive() { return "" } if t.isProxy { // Passthrough on proxy: there is no point in maintaining peer status // at the proxy, it's an exact replica of the master. return what } var reqReply, onlineUpdate bool online := &onlineUpdate replyAs := "on" parts := strings.Split(what, "+") what = parts[0] cmd := "" if len(parts) > 1 { cmd = parts[1] } switch what { case "on": // online *online = true case "off": // offline case "?none": // no change to online status online = nil what = "" case "gone": // offline: off+rem cmd = "rem" case "?unkn": // no change in online status online = nil reqReply = true what = "" default: // All other notifications are not processed here return what } if t.cat == types.TopicCatMe { // Find if the contact is listed. if psd, ok := t.perSubs[fromUserID]; ok { if cmd == "rem" { replyAs = "off+rem" if !psd.enabled && what == "off" { // If it was disabled before, don't send a redundant update. what = "" } delete(t.perSubs, fromUserID) } else { switch cmd { case "": // No change in being enabled or disabled and not being added or removed. if !psd.enabled || online == nil || psd.online == *online { // Not enabled or no change in online status - remove unnecessary notification. what = "" } case "en": if !psd.enabled { psd.enabled = true } else if online == nil || psd.online == *online { // Was active and no change or online before: skip unnecessary update. what = "" } case "dis": if psd.enabled { psd.enabled = false if !psd.online { what = "" } } else { // Was disabled and consequently offline before, still offline - skip the update. what = "" } default: panic("presProcReq: unknown command '" + cmd + "'") } if !psd.enabled { // If we don't care about updates, keep the other user off psd.online = false } else if online != nil { psd.online = *online } t.perSubs[fromUserID] = psd } } else if cmd != "rem" { // Got request from a new topic. This must be a new subscription. Record it. // If it's unknown, recording it as offline. t.addToPerSubs(fromUserID, onlineUpdate, cmd == "en") if cmd != "en" { // If the connection is not enabled, ignore the update. what = "" } } else { // Not in list and asked to be removed from the list - ignore what = "" } } // If requester's online status has not changed, do not reply, otherwise an endless loop will happen. // wantReply is needed to ensure unnecessary {pres} is not sent: // A[online, B:off] to B[online, A:off]: {pres A on} // B[online, A:on] to A[online, B:off]: {pres B on} // A[online, B:on] to B[online, A:on]: {pres A on} <<-- unnecessary, that's why wantReply is needed if (onlineUpdate || reqReply) && wantReply { globals.hub.routeSrv <- &ServerComMessage{ // Topic is 'me' even for group topics; group topics will use 'me' as a signal to drop the message // without forwarding to sessions Pres: &MsgServerPres{ Topic: "me", What: replyAs, Src: t.name, WantReply: reqReply, }, RcptTo: fromUserID, } } return what } // Get user-specific topic name for notifying users of interest, or skip the notification. func notifyOnOrSkip(topic, what string, online bool) string { // Don't send notifications on channels if types.IsChannel(topic) { return "" } // P2P contacts are notified on 'me', group topics are notified on proper topic name. notifyOn := "me" if what == "upd" || what == "ua" { if !online { // Skip "upd" and "ua" notifications if the contact is offline. return "" } if types.GetTopicCat(topic) == types.TopicCatGrp { notifyOn = topic } } return notifyOn } // Publish user's update to his/her subscriptions: p2p on their 'me' topic, group topics on the topic. // Case A: user came online, "on", ua // Case B: user went offline, "off", ua // Case C: user agent change, "ua", ua // Case D: User updated 'public', "upd" func (t *Topic) presUsersOfInterest(what, ua string) { parts := strings.Split(what, "+") wantReply := parts[0] == "on" goOffline := len(parts) > 1 && parts[1] == "dis" // Push update to subscriptions for topic, psd := range t.perSubs { notifyOn := notifyOnOrSkip(topic, what, psd.online) if notifyOn == "" { continue } globals.hub.routeSrv <- &ServerComMessage{ Pres: &MsgServerPres{ Topic: notifyOn, What: what, Src: t.name, UserAgent: ua, WantReply: wantReply, }, RcptTo: topic, } if psd.online && goOffline { psd.online = false t.perSubs[topic] = psd } } } // Publish user's update to his/her users of interest on their 'me' topic while user's 'me' topic is offline // Case A: user is being deleted, "gone". func presUsersOfInterestOffline(uid types.Uid, subs []types.Subscription, what string) { // Push update to subscriptions for i := range subs { notifyOn := notifyOnOrSkip(subs[i].Topic, what, true) if notifyOn == "" { continue } globals.hub.routeSrv <- &ServerComMessage{ Pres: &MsgServerPres{ Topic: notifyOn, What: what, Src: uid.UserId(), WantReply: false, }, RcptTo: subs[i].Topic, } } } // Report change to topic subscribers online, group or p2p // // Case I: User joined the topic, "on" // Case J: User left topic, "off" // Case K.2: User altered WANT (and maybe got default Given), "acs" // Case L.1: Admin altered GIVEN, "acs" to affected user // Case L.3: Admin altered GIVEN (and maybe got assigned default WANT), "acs" to admins // Case M: Topic unaccessible (cluster failure), "left" to everyone currently online // Case V.2: Messages soft deleted, "del" to one user only // Case W.2: Messages hard-deleted, "del" func (t *Topic) presSubsOnline(what, src string, params *presParams, filter *presFilters, skipSid string) { // If affected user is the same as the user making the change, clear 'who' actor := params.actor target := params.target if actor == src { actor = "" } if target == src { target = "" } globals.hub.routeSrv <- &ServerComMessage{ Pres: &MsgServerPres{ Topic: t.xoriginal, What: what, Src: src, Acs: params.packAcs(), AcsActor: actor, AcsTarget: target, SeqId: params.seqID, DelId: params.delID, DelSeq: params.delSeq, FilterIn: int(filter.filterIn), FilterOut: int(filter.filterOut), SingleUser: filter.singleUser, ExcludeUser: filter.excludeUser, }, RcptTo: t.name, SkipSid: skipSid, } } // userIsPresencer returns true if the user (specified by `uid`) may receive presence notifications. func (t *Topic) userIsPresencer(uid types.Uid) bool { var want, given types.AccessMode if uid.IsZero() { // For zero uids (typically for proxy sessions), return the union of all permissions. want = t.modeWantUnion given = t.modeGivenUnion } else { pud := t.perUser[uid] if pud.deleted { return false } want = pud.modeWant given = pud.modeGiven } return (want & given).IsPresencer() } // Send notification to attached sessions directly, without routing though topic. // This is needed because the session(s) may be already disconnected by the time it's routed through topic. func (t *Topic) presSubsOnlineDirect(what string, params *presParams, filter *presFilters, skipSid string) { msg := &ServerComMessage{ Pres: &MsgServerPres{ Topic: t.xoriginal, What: what, Acs: params.packAcs(), SeqId: params.seqID, DelId: params.delID, DelSeq: params.delSeq, }, } for s, pssd := range t.sessions { if !s.isMultiplex() { if skipSid == s.sid { continue } pud := t.perUser[pssd.uid] // Check presence filters if pud.deleted || !presOfflineFilter(pud.modeGiven&pud.modeWant, what, filter) { continue } if filter != nil { if filter.singleUser != "" && filter.singleUser != pssd.uid.UserId() { continue } if filter.excludeUser != "" && filter.excludeUser == pssd.uid.UserId() { continue } } // For p2p topics topic name is dependent on receiver. // It's OK to change the pointer here because the message will be serialized in queueOut // before being placed into the channel. t.prepareBroadcastableMessage(msg, pssd.uid, pssd.isChanSub) } s.queueOut(msg) } } // Communicates "topic unaccessible (cluster rehashing or node connection lost)" event // to a list of topics promting the client to resubscribe to the topics. func (s *Session) presTermDirect(subs []string) { msg := &ServerComMessage{ Pres: &MsgServerPres{Topic: "me", What: "term"}, } for _, topic := range subs { msg.Pres.Src = topic s.queueOut(msg) } } // Publish to topic subscribers's sessions currently offline in the topic, on their 'me' // Group and P2P. // Case E: topic came online, "on" // Case F: topic went offline, "off" // Case G: topic updated 'public', "upd", who // Case H: topic deleted, "gone" // Case K.3: user altered WANT, "acs" to admins // Case L.4: Admin altered GIVEN, "acs" to admins // Case T: message sent, "msg" to all with 'R' // Case W.1: messages hard-deleted, "del" to all with 'R' func (t *Topic) presSubsOffline(what string, params *presParams, filterSource *presFilters, filterTarget *presFilters, skipSid string, offlineOnly bool) { var skipTopic string if offlineOnly { skipTopic = t.name } for uid, pud := range t.perUser { if pud.deleted || !presOfflineFilter(pud.modeGiven&pud.modeWant, what, filterSource) { continue } user := uid.UserId() actor := params.actor target := params.target if actor == user { actor = "" } if target == user { target = "" } globals.hub.routeSrv <- &ServerComMessage{ Pres: &MsgServerPres{ Topic: "me", What: what, Src: t.original(uid), Acs: params.packAcs(), AcsActor: actor, AcsTarget: target, SeqId: params.seqID, DelId: params.delID, FilterIn: int(filterTarget.filterIn), FilterOut: int(filterTarget.filterOut), SingleUser: filterTarget.singleUser, ExcludeUser: filterTarget.excludeUser, SkipTopic: skipTopic, }, RcptTo: user, SkipSid: skipSid, } } } // Publish {info what=read|recv|kp} to topic subscribers's sessions currently offline in the topic, // on subscriber's 'me'. Group and P2P. func (t *Topic) infoSubsOffline(from types.Uid, what string, seq int, skipSid string) { user := from.UserId() for uid, pud := range t.perUser { mode := pud.modeGiven & pud.modeWant if pud.deleted || !mode.IsPresencer() || !mode.IsReader() { continue } globals.hub.routeSrv <- &ServerComMessage{ Info: &MsgServerInfo{ Topic: "me", Src: t.original(uid), From: user, What: what, SeqId: seq, SkipTopic: t.name, }, RcptTo: uid.UserId(), SkipSid: skipSid, } } } // Publish {info what=call} to topic subscribers's sessions on subscriber's 'me'. func (t *Topic) infoCallSubsOffline(from string, target types.Uid, event string, seq int, sdp json.RawMessage, skipSid string, offlineOnly bool) { if target.IsZero() { logs.Err.Printf("callSubs could not find target: topic %s - from %s", t.name, from) return } pud := t.perUser[target] mode := pud.modeGiven & pud.modeWant if pud.deleted || !mode.IsPresencer() || !mode.IsReader() { return } msg := &ServerComMessage{ Info: &MsgServerInfo{ Topic: "me", Src: t.original(target), From: from, What: "call", Event: event, SeqId: seq, Payload: sdp, }, RcptTo: target.UserId(), SkipSid: skipSid, } if offlineOnly { msg.Info.SkipTopic = t.name } globals.hub.routeSrv <- msg } // Same as presSubsOffline, but the topic has not been loaded/initialized first: offline topic, offline subscribers func presSubsOfflineOffline(topic string, cat types.TopicCat, subs []types.Subscription, what string, params *presParams, skipSid string) { count := 0 original := topic for i := range subs { sub := &subs[i] // Let "acs" and "gone" through regardless of 'P'. Don't check for deleted subscriptions: // they are not passed here. if !presOfflineFilter(sub.ModeWant&sub.ModeGiven, what, nil) { continue } if cat == types.TopicCatP2P { original = types.ParseUid(subs[(count+1)%2].User).UserId() count++ } user := types.ParseUid(sub.User).UserId() actor := params.actor target := params.target if actor == user { actor = "" } if target == user { target = "" } globals.hub.routeSrv <- &ServerComMessage{ Pres: &MsgServerPres{ Topic: "me", What: what, Src: original, Acs: params.packAcs(), AcsActor: actor, AcsTarget: target, SeqId: params.seqID, DelId: params.delID, }, RcptTo: user, SkipSid: skipSid, } } } // Announce to a single user on 'me' topic // // Case K.1: User altered WANT (includes new subscription, deleted subscription) // Case L.2: Sharer altered GIVEN (inludes invite, eviction) // Case U: read/recv notification // Case V.1: messages soft-deleted func (t *Topic) presSingleUserOffline(uid types.Uid, mode types.AccessMode, what string, params *presParams, skipSid string, offlineOnly bool) { var skipTopic string if offlineOnly { skipTopic = t.name } // ModeInvalid means the user is deleted (pud.deleted == true) if mode != types.ModeInvalid && presOfflineFilter(mode, what, nil) { user := uid.UserId() actor := params.actor target := params.target if actor == user { actor = "" } if target == user { target = "" } globals.hub.routeSrv <- &ServerComMessage{ Pres: &MsgServerPres{ Topic: "me", What: what, Src: t.original(uid), SeqId: params.seqID, DelId: params.delID, Acs: params.packAcs(), AcsActor: actor, AcsTarget: target, UserAgent: params.userAgent, WantReply: strings.HasPrefix(what, "?unkn"), SkipTopic: skipTopic, }, RcptTo: user, SkipSid: skipSid, } } } // Announce to a single user on 'me' topic. The originating topic is not used (not loaded or user // already unsubscribed). func presSingleUserOfflineOffline(uid types.Uid, original, what string, params *presParams, skipSid string) { user := uid.UserId() actor := params.actor target := params.target if actor == user { actor = "" } if target == user { target = "" } globals.hub.routeSrv <- &ServerComMessage{ Pres: &MsgServerPres{ Topic: "me", What: what, Src: original, SeqId: params.seqID, DelId: params.delID, Acs: params.packAcs(), AcsActor: actor, AcsTarget: target, }, RcptTo: uid.UserId(), SkipSid: skipSid, } } // Let other sessions of a given user know what messages are now received/read. // If both 'read' and 'recv' != 0 then 'read' takes precedence over 'recv'. // Cases U func (t *Topic) presPubMessageCount(uid types.Uid, mode types.AccessMode, read, recv int, skip string) { var what string var seq int if read > 0 { what = "read" seq = read } else if recv > 0 { what = "recv" seq = recv } if what != "" { // Announce to user's other sessions on 'me' only if they are not attached to this topic. // Attached topics will receive an {info} t.presSingleUserOffline(uid, mode, what, &presParams{seqID: seq}, skip, true) } } // Let other sessions of a given user know that messages are now deleted // Cases V.1, V.2 func (t *Topic) presPubMessageDelete(uid types.Uid, mode types.AccessMode, delID int, list []MsgRange, skip string) { if len(list) == 0 && delID <= 0 { logs.Warn.Printf("Case V.1, V.2: topic[%s] invalid request - missing payload", t.name) return } // This check is only needed for V.1, but it does not hurt V.2. Let's do it here for both. if !t.userIsPresencer(uid) { return } params := &presParams{delID: delID, delSeq: list} // Case V.2 user := uid.UserId() t.presSubsOnline("del", user, params, &presFilters{singleUser: user}, skip) // Case V.1 t.presSingleUserOffline(uid, mode, "del", params, skip, true) } // Filter by permissions and notification type: check for exceptions, // then check if mode.IsPresencer() AND mode has at least some // bits specified in 'filter' (or filter is ModeNone). func presOfflineFilter(mode types.AccessMode, what string, pf *presFilters) bool { if what == "acs" || what == "gone" { return true } if what == "upd" && mode.IsJoiner() { return true } return mode.IsPresencer() && (pf == nil || ((pf.filterIn == types.ModeNone || mode&pf.filterIn != 0) && (pf.filterOut == types.ModeNone || mode&pf.filterOut == 0))) } ================================================ FILE: server/push/common/typedef.go ================================================ package common import ( "fmt" "net/http" "reflect" "strings" "github.com/tinode/chat/server/push" "google.golang.org/api/googleapi" ) // Payload to be sent for a specific notification type. type Payload struct { // Common for APNS and Android Body string `json:"body,omitempty"` Title string `json:"title,omitempty"` TitleLocKey string `json:"title_loc_key,omitempty"` TitleLocArgs []string `json:"title_loc_args,omitempty"` // Android BodyLocKey string `json:"body_loc_key,omitempty"` BodyLocArgs []string `json:"body_loc_args,omitempty"` Icon string `json:"icon,omitempty"` Color string `json:"color,omitempty"` ClickAction string `json:"click_action,omitempty"` Sound string `json:"sound,omitempty"` Image string `json:"image,omitempty"` // APNS Action string `json:"action,omitempty"` ActionLocKey string `json:"action_loc_key,omitempty"` LaunchImage string `json:"launch_image,omitempty"` LocArgs []string `json:"loc_args,omitempty"` LocKey string `json:"loc_key,omitempty"` Subtitle string `json:"subtitle,omitempty"` SummaryArg string `json:"summary_arg,omitempty"` SummaryArgCount int `json:"summary_arg_count,omitempty"` } // Config is the configuration of a Notification payload. type Config struct { Enabled bool `json:"enabled,omitempty"` // Common defaults for all push types. Payload // Configs for specific push types. Msg Payload `json:"msg,omitempty"` Sub Payload `json:"sub,omitempty"` } func (cp Payload) getStringAttr(field string) string { val := reflect.ValueOf(cp).FieldByName(field) if !val.IsValid() { return "" } if val.Kind() == reflect.String { return val.String() } return "" } func (cp Payload) getIntAttr(field string) int { val := reflect.ValueOf(cp).FieldByName(field) if !val.IsValid() { return 0 } if val.Kind() == reflect.Int { return int(val.Int()) } return 0 } func (cc *Config) GetStringField(what, field string) string { var val string if what == push.ActMsg { val = cc.Msg.getStringAttr(field) } else if what == push.ActSub { val = cc.Sub.getStringAttr(field) } if val == "" { val = cc.Payload.getStringAttr(field) } return val } func (cc *Config) GetIntField(what, field string) int { var val int if what == push.ActMsg { val = cc.Msg.getIntAttr(field) } else if what == push.ActSub { val = cc.Sub.getIntAttr(field) } if val == 0 { val = cc.Payload.getIntAttr(field) } return val } // AndroidVisibilityType defines notification visibility constants // https://developer.android.com/reference/android/app/Notification.html#visibility type AndroidVisibilityType string const ( // AndroidVisibilityUnspecified if unspecified, default to `Visibility.PRIVATE`. AndroidVisibilityUnspecified AndroidVisibilityType = "VISIBILITY_UNSPECIFIED" // AndroidVisibilityPrivate show this notification on all lockscreens, but conceals // sensitive or private information on secure lockscreens. AndroidVisibilityPrivate AndroidVisibilityType = "PRIVATE" // AndroidVisibilityPublic show this notification in its entirety on all lockscreens. AndroidVisibilityPublic AndroidVisibilityType = "PUBLIC" // AndroidVisibilitySecret do not reveal any part of this notification on a secure lockscreen. AndroidVisibilitySecret AndroidVisibilityType = "SECRET" ) // AndroidNotificationPriorityType defines notification priority consumeed by the client // after it receives the notification. Does not affect FCM sending. type AndroidNotificationPriorityType string const ( // If priority is unspecified, notification priority is set to `PRIORITY_DEFAULT`. AndroidNotificationPriorityUnspecified AndroidNotificationPriorityType = "PRIORITY_UNSPECIFIED" // Lowest notification priority. Notifications with this `PRIORITY_MIN` might not be // shown to the user except under special circumstances, such as detailed notification logs. AndroidNotificationPriorityMin AndroidNotificationPriorityType = "PRIORITY_MIN" // Lower notification priority. The UI may choose to show the notifications smaller, // or at a different position in the list, compared with notifications with `PRIORITY_DEFAULT`. AndroidNotificationPriorityLow AndroidNotificationPriorityType = "PRIORITY_LOW" // Default notification priority. If the application does not prioritize its own notifications, // use this value for all notifications. AndroidNotificationPriorityDefault AndroidNotificationPriorityType = "PRIORITY_DEFAULT" // Higher notification priority. Use this for more important notifications or alerts. // The UI may choose to show these notifications larger, or at a different position in the notification // lists, compared with notifications with `PRIORITY_DEFAULT`. AndroidNotificationPriorityHigh AndroidNotificationPriorityType = "PRIORITY_HIGH" // Highest notification priority. Use this for the application's most important items that // require the user's prompt attention or input. AndroidNotificationPriorityMax AndroidNotificationPriorityType = "PRIORITY_MAX" ) // AndroidPriorityType defines the server-side priorities https://goo.gl/GjONJv. It affects how soon // FCM sends the push. type AndroidPriorityType string const ( // Default priority for data messages. Normal priority messages won't open network // connections on a sleeping device, and their delivery may be delayed to conserve // the battery. For less time-sensitive messages, such as notifications of new email // or other data to sync, choose normal delivery priority. AndroidPriorityNormal AndroidPriorityType = "NORMAL" // Default priority for notification messages. FCM attempts to deliver high priority // messages immediately, allowing the FCM service to wake a sleeping device when possible // and open a network connection to your app server. Apps with instant messaging, chat, // or voice call alerts, for example, generally need to open a network connection and make // sure FCM delivers the message to the device without delay. Set high priority if the message // is time-critical and requires the user's immediate interaction, but beware that setting // your messages to high priority contributes more to battery drain compared with normal priority messages. AndroidPriorityHigh AndroidPriorityType = "HIGH" ) // InterruptionLevelType defines the values for the APNS payload.aps.InterruptionLevel. type InterruptionLevelType string const ( // InterruptionLevelPassive is used to indicate that notification be delivered in a passive manner. InterruptionLevelPassive InterruptionLevelType = "passive" // InterruptionLevelActive is used to indicate the importance and delivery timing of a notification. InterruptionLevelActive InterruptionLevelType = "active" // InterruptionLevelTimeSensitive is used to indicate the importance and delivery timing of a notification. InterruptionLevelTimeSensitive InterruptionLevelType = "time-sensitive" // InterruptionLevelCritical is used to indicate the importance and delivery timing of a notification. // This interruption level requires an approved entitlement from Apple. // See: https://developer.apple.com/documentation/usernotifications/unnotificationinterruptionlevel/ InterruptionLevelCritical InterruptionLevelType = "critical" ) const ( // A canonical UUID that identifies the notification. If there is an error sending the notification, // APNs uses this value to identify the notification to your server. // The canonical form is 32 lowercase hexadecimal digits, displayed in five groups separated by hyphens // in the form 8-4-4-4-12. An example UUID is as follows: 123e4567-e89b-12d3-a456-42665544000 // If you omit this header, a new UUID is created by APNs and returned in the response. HeaderApnsID = "apns-id" // A UNIX epoch date expressed in seconds (UTC). This header identifies the date when the notification // is no longer valid and can be discarded. // If this value is nonzero, APNs stores the notification and tries to deliver it at least once, repeating // the attempt as needed if it is unable to deliver the notification the first time. If the value is 0, // APNs treats the notification as if it expires immediately and does not store the notification or attempt // to redeliver it. HeaderApnsExpiration = "apns-expiration" // The priority of the notification. Specify one of the following values: // 10–Send the push message immediately. Notifications with this priority must trigger an alert, sound, // or badge on the target device. It is an error to use this priority for a push notification that // contains only the content-available key. // 5—Send the push message at a time that takes into account power considerations for the device. // Notifications with this priority might be grouped and delivered in bursts. They are throttled, // and in some cases are not delivered. // If you omit this header, the APNs server sets the priority to 10. HeaderApnsPriority = "apns-priority" // The topic of the remote notification, which is typically the bundle ID for your app. // The certificate you create in your developer account must include the capability for this topic. // If your certificate includes multiple topics, you must specify a value for this header. // If you omit this request header and your APNs certificate does not specify multiple topics, // the APNs server uses the certificate’s Subject as the default topic. // If you are using a provider token instead of a certificate, you must specify a value for this // request header. The topic you provide should be provisioned for the your team named in your developer account. HeaderApnsTopic = "apns-topic" // Multiple notifications with the same collapse identifier are displayed to the user as a single notification. // The value of this key must not exceed 64 bytes. For more information, see Quality of Service, // Store-and-Forward, and Coalesced Notifications. HeaderApnsCollapseID = "apns-collapse-id" // The value of this header must accurately reflect the contents of your notification’s payload. // If there’s a mismatch, or if the header is missing on required systems, APNs may return an error, // delay the delivery of the notification, or drop it altogether. HeaderApnsPushType = "apns-push-type" ) type ApnsPushTypeType string const ( // Use the alert push type for notifications that trigger a user interaction—for example, an alert, badge, or sound. // If you set this push type, the apns-topic header field must use your app’s bundle ID as the topic. // For more information, see Generating a remote notification. // If the notification requires immediate action from the user, set notification priority to 10; otherwise use 5. ApnsPushTypeAlert ApnsPushTypeType = "alert" // Use the background push type for notifications that deliver content in the background, and don’t trigger any user interactions. // If you set this push type, the apns-topic header field must use your app’s bundle ID as the topic. Always use priority 5. // Using priority 10 is an error. For more information, see Pushing Background Updates to Your App. ApnsPushTypeBackground ApnsPushTypeType = "background" // Use the location push type for notifications that request a user’s location. If you set this push type, // the apns-topic header field must use your app’s bundle ID with .location-query appended to the end. // If the location query requires an immediate response from the Location Push Service Extension, set notification // apns-priority to 10; otherwise, use 5. The location push type supports only token-based authentication. ApnsPushTypeLocation ApnsPushTypeType = "location" // Use the voip push type for notifications that provide information about an incoming Voice-over-IP (VoIP) call. // For more information, see Responding to VoIP Notifications from PushKit. // If you set this push type, the apns-topic header field must use your app’s bundle ID with .voip appended to the end. // If you’re using certificate-based authentication, you must also register the certificate for VoIP services. // The topic is then part of the 1.2.840.113635.100.6.3.4 or 1.2.840.113635.100.6.3.6 extension. ApnsPushTypeVoip ApnsPushTypeType = "voip" // Use the fileprovider push type to signal changes to a File Provider extension. If you set this push type, // the apns-topic header field must use your app’s bundle ID with .pushkit.fileprovider appended to the end. // For more information, see Using Push Notifications to Signal Changes. ApnsPushTypeFileprovider ApnsPushTypeType = "fileprovider" ) // Aps is the APNS payload. See explanation here: // https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/generating_a_remote_notification#2943363 type Aps struct { Alert *ApsAlert `json:"alert,omitempty"` Badge int `json:"badge,omitempty"` Category string `json:"category,omitempty"` ContentAvailable int `json:"content-available,omitempty"` InterruptionLevel InterruptionLevelType `json:"interruption-level,omitempty"` MutableContent int `json:"mutable-content,omitempty"` RelevanceScore any `json:"relevance-score,omitempty"` Sound any `json:"sound,omitempty"` ThreadID string `json:"thread-id,omitempty"` URLArgs []string `json:"url-args,omitempty"` } // ApsAlert is the content of the aps.Alert field. type ApsAlert struct { Action string `json:"action,omitempty"` ActionLocKey string `json:"action-loc-key,omitempty"` Body string `json:"body,omitempty"` LaunchImage string `json:"launch-image,omitempty"` LocArgs []string `json:"loc-args,omitempty"` LocKey string `json:"loc-key,omitempty"` Title string `json:"title,omitempty"` Subtitle string `json:"subtitle,omitempty"` TitleLocArgs []string `json:"title-loc-args,omitempty"` TitleLocKey string `json:"title-loc-key,omitempty"` SummaryArg string `json:"summary-arg,omitempty"` SummaryArgCount int `json:"summary-arg-count,omitempty"` } // FCM error codes const ( // No more information is available about this error. ErrorUnspecified = "UNSPECIFIED_ERROR" // Request parameters were invalid (HTTP error code = 400). An extension of type google.rpc.BadRequest is returned // to specify which field was invalid. // Potential causes: // - Invalid registration: Check the format of the registration token you pass to the server. Make sure it matches // the registration token the client app receives from registering with Firebase Notifications. // Do not truncate or add additional characters. // - Invalid package name: Make sure the message was addressed to a registration token whose package name matches // the value passed in the request. // - Message too big: Check that the total size of the payload data included in a message does not exceed FCM limits: // 4096 bytes for most messages, or 2048 bytes in the case of messages to topics. This includes both the keys and the values. // - Invalid data key: Check that the payload data does not contain a key (such as from, or gcm, or any value prefixed // by google) that is used internally by FCM. Note that some words (such as collapse_key) are also used by FCM but are // allowed in the payload, in which case the payload value will be overridden by the FCM value. // - Invalid TTL: Check that the value used in ttl is an integer representing a duration in seconds between 0 and // 2,419,200 (4 weeks). // - Invalid parameters: Check that the provided parameters have the right name and type. ErrorInvalidArgument = "INVALID_ARGUMENT" // App instance was unregistered from FCM (HTTP error code = 404). This usually means that the token used is no // longer valid and a new one must be used. // This error can be caused by missing registration tokens, or unregistered tokens. // - Missing Registration: If the message's target is a token value, check that the request contains a registration token. // - Not registered: An existing registration token may cease to be valid in a number of scenarios, including: // - If the client app unregisters with FCM. // - If the client app is automatically unregistered, which can happen if the user uninstalls the application. // For example, on iOS, if the APNS Feedback Service reported the APNS token as invalid. // - If the registration token expires (for example, Google might decide to refresh registration tokens, // or the APNS token has expired for iOS devices). // - If the client app is updated but the new version is not configured to receive messages. // For all these cases, remove this registration token from the app server and stop using it to send messages. ErrorUnregistered = "UNREGISTERED" // The authenticated sender ID is different from the sender ID for the registration token (HTTP error code = 403). // A registration token is tied to a certain group of senders. When a client app registers for FCM, it must specify // which senders are allowed to send messages. You should use one of those sender IDs when sending messages to // the client app. If you switch to a different sender, the existing registration tokens won't work. ErrorSenderIDMismatch = "SENDER_ID_MISMATCH" // Sending limit exceeded for the message target (HTTP error code = 429). An extension of type google.rpc.QuotaFailure // is returned to specify which quota got exceeded. This error can be caused by exceeded message rate quota, // exceeded device message rate quota, or exceeded topic message rate quota. // - Message rate exceeded: The sending rate of messages is too high. Reduce the number of messages sent and use // exponential backoff to retry sending. // - Device message rate exceeded: The rate of messages to a particular device is too high. If an iOS app sends // messages at a rate exceeding APNs limits, it may receive this error message. Reduce the number of messages // sent to this device and use exponential backoff to retry sending. // - Topic message rate exceeded: The rate of messages to subscribers to a particular topic is too high. // Reduce the number of messages sent for this topic and use exponential backoff to retry sending. ErrorQuotaExceeded = "QUOTA_EXCEEDED" // The server is overloaded (HTTP error code = 503). The server couldn't process the request in time. Retry the // same request, but you must: // - Honor the Retry-After header if it is included in the response from the FCM Connection Server. // - Implement exponential back-off in your retry mechanism. (e.g. if you waited one second before the first retry, // wait at least two second before the next one, then 4 seconds and so on). If you're sending multiple messages, // delay each one independently by an additional random amount to avoid issuing a new request for all messages // at the same time. Senders that cause problems risk being denylisted. ErrorUnavailable = "UNAVAILABLE" // An unknown internal error occurred (HTTP error code = 500). The server encountered an error while trying to process // the request. You could retry the same request following the requirements listed in "Timeout" (see row above). // If the error persists, please contact Firebase support. ErrorInternal = "INTERNAL" // APNs certificate or web push auth key was invalid or missing (HTTP error code = 401). A message targeted to an // iOS device or a web push registration could not be sent. Check the validity of your development and production // credentials. ErrorThirdPartyAuth = "THIRD_PARTY_AUTH_ERROR" ) // APNS error messages const ( // The collapse identifier exceeds the maximum allowed size (HTTP error code = 400). ErrorApnsBadCollapseId = "BadCollapseId" // The specified device token was bad. Verify that the request contains a valid token and that the // token matches the environment (HTTP error code = 400). ErrorApnsBadDeviceToken = "BadDeviceToken" // The apns-expiration value is bad (HTTP error code = 400). ErrorApnsBadExpirationDate = "BadExpirationDate" // The apns-id value is bad (HTTP error code = 400). ErrorApnsBadMessageId = "BadMessageId" // The apns-priority value is bad (HTTP error code = 400). ErrorApnsBadPriority = "BadPriority" // The apns-topic was invalid (HTTP error code = 400). ErrorApnsBadTopic = "BadTopic" // The device token does not match the specified topic (HTTP error code = 400). ErrorApnsDeviceTokenNotForTopic = "DeviceTokenNotForTopic" // One or more headers were repeated (HTTP error code = 400). ErrorApnsDuplicateHeaders = "DuplicateHeaders" // Idle time out (HTTP error code = 400). ErrorApnsIdleTimeout = "IdleTimeout" // The device token is not specified in the request :path. Verify that the :path header // contains the device token (HTTP error code = 400). ErrorApnsMissingDeviceToken = "MissingDeviceToken" // The apns-topic header of the request was not specified and was required. // The apns-topic header is mandatory when the client is connected using a certificate // that supports multiple topics (HTTP error code = 400). ErrorApnsMissingTopic = "MissingTopic" // The message payload was empty (HTTP error code = 400). ErrorApnsPayloadEmpty = "PayloadEmpty" // Pushing to this topic is not allowed (HTTP error code = 400). ErrorApnsTopicDisallowed = "TopicDisallowed" // The certificate was bad (HTTP error code = 403). ErrorApnsBadCertificate = "BadCertificate" // The client certificate was for the wrong environment (HTTP error code = 403). ErrorApnsBadCertificateEnvironment = "BadCertificateEnvironment" // The provider token is stale and a new token should be generated (HTTP error code = 403). ErrorApnsExpiredProviderToken = "ExpiredProviderToken" // The specified action is not allowed (HTTP error code = 403). ErrorApnsForbidden = "Forbidden" // The provider token is not valid or the token signature could not be verified (HTTP error code = 403). ErrorApnsInvalidProviderToken = "InvalidProviderToken" // No provider certificate was used to connect to APNs and Authorization header was missing // or no provider token was specified (HTTP error code = 403). ErrorApnsMissingProviderToken = "MissingProviderToken" // The request contained a bad :path value (HTTP error code = 404). ErrorApnsBadPath = "BadPath" // The specified :method was not POST (HTTP error code = 405). ErrorApnsMethodNotAllowed = "MethodNotAllowed" // The device token is inactive for the specified topic (HTTP error code = 410). ErrorApnsUnregistered = "Unregistered" // The message payload was too large. See Creating the Remote Notification Payload // for details on maximum payload size (HTTP error code = 413). ErrorApnsPayloadTooLarge = "PayloadTooLarge" // The provider token is being updated too often (HTTP error code = 429). ErrorApnsTooManyProviderTokenUpdates = "TooManyProviderTokenUpdates" // Too many requests were made consecutively to the same device token (HTTP error code = 429). ErrorApnsTooManyRequests = "TooManyRequests" // An internal server error occurred (HTTP error code = 500). ErrorApnsInternalServerError = "InternalServerError" // The service is unavailable (HTTP error code = 503). ErrorApnsServiceUnavailable = "ServiceUnavailable" // The server is shutting down (HTTP error code = 503). ErrorApnsShutdown = "Shutdown" ) // GApiError stores a simplified representation of an error returned by a call to Google API. type GApiError struct { HttpCode int // This one is not informative, but can be logged for user consideration. ErrMessage string // FCM error code, informative but may be missing. FcmErrCode string // Extended error info dependent on the fcmErrCode. ExtendedInfo string } // DecodeGoogleApiError converts very complex googleapi.Error to a bit more manageable structure. func DecodeGoogleApiError(err error) (decoded *GApiError, errs []error) { decoded = &GApiError{} if gerr, ok := err.(*googleapi.Error); ok { // HTTP status code. decoded.HttpCode = gerr.Code decoded.ErrMessage = gerr.Message for _, errInfo := range gerr.Errors { decoded.ErrMessage += "; " + errInfo.Reason + "/" + errInfo.Message } // Decode the FCM error. for _, iface := range gerr.Details { details, ok := iface.(map[string]any) if !ok { errs = append(errs, fmt.Errorf("error.Details unrecognized format %T", iface)) continue } switch details["@type"] { case "type.googleapis.com/google.firebase.fcm.v1.FcmError": if errCode, ok := details["errorCode"].(string); ok { if decoded.FcmErrCode != "" { // This has not been observed but FCM is uncler if it can happen. errs = append(errs, fmt.Errorf("multiple FcmError codes '%s', '%s'", errCode, decoded.FcmErrCode)) } else { decoded.FcmErrCode = errCode } } else { errs = append(errs, fmt.Errorf("error.Details errorCode is not a string: %T", details["errorCode"])) } case "type.googleapis.com/google.rpc.BadRequest": // dst.fcmErrCode == INVALID_ARGUMENT if fieldViolations, ok := details["fieldViolations"].([]any); !ok { errs = append(errs, fmt.Errorf("wrong type of error.Details 'fieldViolations': %T", details["fieldViolations"])) } else { var fields []string for _, violationIface := range fieldViolations { if violation, ok := violationIface.(map[string]any); !ok { errs = append(errs, fmt.Errorf("wrong type of error.Details.fieldViolations item: %T", iface)) } else if field, ok := violation["field"].(string); ok && field != "" { fields = append(fields, field) } else { errs = append(errs, fmt.Errorf("error.Details 'fieldViolation' has no 'field': %T, %s", violation["field"], violation["description"])) } } decoded.ExtendedInfo = strings.Join(fields, ",") } case "type.googleapis.com/google.rpc.QuotaFailure": // dst.fcmErrCode == QUOTA_EXCEEDED // TODO: this error has not been observed, don't know how to handle it. errs = append(errs, fmt.Errorf("quota exceeded %v", details)) default: errs = append(errs, fmt.Errorf("unknown error '@type': %v", details)) } } } else { decoded.HttpCode = http.StatusBadRequest decoded.ErrMessage = err.Error() errs = append(errs, fmt.Errorf("not googleapi.Error %w", err)) } if decoded.FcmErrCode == "" { decoded.FcmErrCode = string(ErrorUnspecified) } return } ================================================ FILE: server/push/fcm/README.md ================================================ # FCM push adapter This adapter sends push notifications to mobile clients and web browsers using [Google FCM](https://firebase.google.com/docs/cloud-messaging/). As of the time of this writing it supports Android with [Play Services](https://developers.google.com/android/guides/overview), iOS devices, and all major web browsers [excluding Safari](https://caniuse.com/#feat=push-api). This adapter requires you to obtain your own credentials from Goole Firebase. If you want to use iOS and Android mobile apps with your service, they must be recompiled with your credentials obtained from Google. If you do not want to recompile mobile clients, consider using TNPG adapter instead. ## Configuring FCM adapter ### Server and TinodeWeb 1. Create a project at https://firebase.google.com/ if you have not done so already. 2. Follow instructions at https://cloud.google.com/iam/docs/creating-managing-service-account-keys to download the credentials file. 3. Update the server config [`tinode.conf`](../server/tinode.conf#L255), section `"push"` -> `"name": "fcm"`. Do _ONE_ of the following: * _Either_ enter the path to the downloaded credentials file into `"credentials_file"`. * _OR_ copy the file contents to `"credentials"`.

Remove the other entry. I.e. if you have updated `"credentials_file"`, remove `"credentials"` and vice versa. 4. Update [TinodeWeb](/tinode/webapp/) config [`firebase-init.js`](https://github.com/tinode/webapp/blob/master/firebase-init.js): update `apiKey`, `messagingSenderId`, `projectId`, `appId`, `messagingVapidKey`. See more info at https://github.com/tinode/webapp/#push_notifications ### iOS and Android 1. If you are using an Android client, add `google-services.json` to [Tindroid](/tinode/tindroid/) by following instructions at https://developers.google.com/android/guides/google-services-plugin and recompile the client. You may also optionally submit it to Google Play Store. See more info at https://github.com/tinode/tindroid/#push_notifications 2. If you are using an iOS client, add `GoogleService-Info.plist` to [Tinodios](/tinode/ios/) by following instructions at https://firebase.google.com/docs/cloud-messaging/ios/client) and recompile the client. You may optionally submit the app to Apple AppStore. See more info at https://github.com/tinode/ios/#push_notifications ================================================ FILE: server/push/fcm/payload.go ================================================ package fcm import ( "encoding/json" "errors" "strconv" "time" fcmv1 "google.golang.org/api/fcm/v1" "github.com/tinode/chat/server/drafty" "github.com/tinode/chat/server/logs" "github.com/tinode/chat/server/push" "github.com/tinode/chat/server/push/common" "github.com/tinode/chat/server/store" t "github.com/tinode/chat/server/store/types" "maps" ) const ( // TTL of a VOIP push notification in seconds. voipTimeToLive = 10 // TTL of a regular push notification in seconds. defaultTimeToLive = 3600 ) func payloadToData(pl *push.Payload) (map[string]string, error) { if pl == nil { return nil, errors.New("empty push payload") } data := make(map[string]string) var err error data["what"] = pl.What if pl.Silent { data["silent"] = "true" } data["topic"] = pl.Topic data["ts"] = pl.Timestamp.Format(time.RFC3339Nano) // Must use "xfrom" because "from" is a reserved word. Google did not bother to document it anywhere. data["xfrom"] = pl.From if pl.What == push.ActMsg { data["seq"] = strconv.Itoa(pl.SeqId) if pl.ContentType != "" { data["mime"] = pl.ContentType } // Convert Drafty content to plain text (clients 0.16 and below). data["content"], err = drafty.PlainText(pl.Content) if err != nil { return nil, err } // Trim long strings to 128 runes. // Check byte length first and don't waste time converting short strings. if len(data["content"]) > push.MaxPayloadLength { runes := []rune(data["content"]) if len(runes) > push.MaxPayloadLength { data["content"] = string(runes[:push.MaxPayloadLength]) + "…" } } // Rich content for clients version 0.17 and above. data["rc"], err = drafty.Preview(pl.Content, push.MaxPayloadLength) if pl.Webrtc != "" { data["webrtc"] = pl.Webrtc if pl.AudioOnly { data["aonly"] = "true" } // Video call push notifications are silent. data["silent"] = "true" } if pl.Replace != "" { // Notification of a message edit should be silent too. data["silent"] = "true" data["replace"] = pl.Replace } if err != nil { return nil, err } } else if pl.What == push.ActSub { data["modeWant"] = pl.ModeWant.String() data["modeGiven"] = pl.ModeGiven.String() } else if pl.What == push.ActRead { data["seq"] = strconv.Itoa(pl.SeqId) data["silent"] = "true" } else { return nil, errors.New("unknown push type") } return data, nil } func clonePayload(src map[string]string) map[string]string { dst := make(map[string]string, len(src)) maps.Copy(dst, src) return dst } // PrepareV1Notifications creates notification payloads ready to be posted // to push notification server for the provided receipt. func PrepareV1Notifications(rcpt *push.Receipt, config *configType) ([]*fcmv1.Message, []t.Uid) { data, err := payloadToData(&rcpt.Payload) if err != nil { logs.Warn.Println("fcm push: could not parse payload:", err) return nil, nil } // Device IDs to send pushes to. var devices map[t.Uid][]t.DeviceDef // Count of device IDs to push to. var count int // Devices which were online in the topic when the message was sent. skipDevices := make(map[string]struct{}) if len(rcpt.To) > 0 { // List of UIDs for querying the database uids := make([]t.Uid, len(rcpt.To)) i := 0 for uid, to := range rcpt.To { uids[i] = uid i++ // Some devices were online and received the message. Skip them. for _, deviceID := range to.Devices { skipDevices[deviceID] = struct{}{} } } devices, count, err = store.Devices.GetAll(uids...) if err != nil { logs.Warn.Println("fcm push: db error", err) return nil, nil } } if count == 0 && rcpt.Channel == "" { return nil, nil } if config == nil { // config is nil when called from tnpg adapter; provide a blank one for simplicity. config = &configType{} } var messages []*fcmv1.Message var uids []t.Uid for uid, devList := range devices { topic := rcpt.Payload.Topic userData := data tcat := t.GetTopicCat(topic) if rcpt.To[uid].Delivered > 0 || tcat == t.TopicCatP2P { userData = clonePayload(data) // Fix topic name for P2P pushes. if tcat == t.TopicCatP2P { topic, _ = t.P2PNameForUser(uid, topic) userData["topic"] = topic } // Silence the push for user who have received the data interactively. if rcpt.To[uid].Delivered > 0 { userData["silent"] = "true" } } for i := range devList { d := &devList[i] if _, ok := skipDevices[d.DeviceId]; !ok && d.DeviceId != "" { msg := fcmv1.Message{ Token: d.DeviceId, Data: userData, } switch d.Platform { case "android": msg.Android = androidNotificationConfig(rcpt.Payload.What, topic, userData, config) case "ios": msg.Apns = apnsNotificationConfig(rcpt.Payload.What, topic, userData, rcpt.To[uid].Unread, config) case "web": if config != nil && config.Webpush != nil && config.Webpush.Enabled { msg.Webpush = &fcmv1.WebpushConfig{} } case "": // ignore default: logs.Warn.Println("fcm: unknown device platform", d.Platform) } uids = append(uids, uid) messages = append(messages, &msg) } } } if rcpt.Channel != "" { topic := rcpt.Channel userData := clonePayload(data) userData["topic"] = topic // Channel receiver should not know the ID of the message sender. delete(userData, "xfrom") msg := fcmv1.Message{ Topic: topic, Data: userData, } // We don't know the platform of the receiver, must provide payload for all platforms. msg.Android = androidNotificationConfig(rcpt.Payload.What, topic, userData, config) msg.Apns = apnsNotificationConfig(rcpt.Payload.What, topic, userData, 0, config) // TODO: add webpush payload. messages = append(messages, &msg) // UID is not used in handling Topic pushes, but should keep the same count as messages. uids = append(uids, t.ZeroUid) } return messages, uids } // DevicesForUser loads device IDs of the given user. func DevicesForUser(uid t.Uid) []string { ddef, count, err := store.Devices.GetAll(uid) if err != nil { logs.Warn.Println("fcm devices for user: db error", err) return nil } if count == 0 { return nil } devices := make([]string, count) for i, dd := range ddef[uid] { devices[i] = dd.DeviceId } return devices } // ChannelsForUser loads user's channel subscriptions with P permission. func ChannelsForUser(uid t.Uid) []string { channels, err := store.Users.GetChannels(uid) if err != nil { logs.Warn.Println("fcm channels for user: db error", err) return nil } return channels } func androidNotificationConfig(what, topic string, data map[string]string, config *configType) *fcmv1.AndroidConfig { timeToLive := strconv.Itoa(defaultTimeToLive) + "s" if config != nil && config.TimeToLive > 0 { timeToLive = strconv.Itoa(config.TimeToLive) + "s" } if what == push.ActRead { return &fcmv1.AndroidConfig{ Priority: string(common.AndroidPriorityNormal), Notification: nil, Ttl: timeToLive, } } _, videoCall := data["webrtc"] if videoCall { timeToLive = "0s" } // Sending priority. priority := string(common.AndroidPriorityHigh) ac := &fcmv1.AndroidConfig{ Priority: priority, Ttl: timeToLive, } // When this notification type is included and the app is not in the foreground // Android won't wake up the app and won't call FirebaseMessagingService:onMessageReceived. // See dicussion: https://github.com/firebase/quickstart-js/issues/71 if config.Android == nil || !config.Android.Enabled { return ac } body := config.Android.GetStringField(what, "Body") if body == "$content" { body = data["content"] } // Client-side display priority. priority = string(common.AndroidNotificationPriorityHigh) if videoCall { priority = string(common.AndroidNotificationPriorityMax) } ac.Notification = &fcmv1.AndroidNotification{ // Android uses Tag value to group notifications together: // show just one notification per topic. Tag: topic, NotificationPriority: priority, Visibility: string(common.AndroidVisibilityPrivate), TitleLocKey: config.Android.GetStringField(what, "TitleLocKey"), Title: config.Android.GetStringField(what, "Title"), BodyLocKey: config.Android.GetStringField(what, "BodyLocKey"), Body: body, Icon: config.Android.GetStringField(what, "Icon"), Color: config.Android.GetStringField(what, "Color"), ClickAction: config.Android.GetStringField(what, "ClickAction"), } return ac } func apnsShouldPresentAlert(what, callStatus, isSilent string, config *configType) bool { return config.Apns != nil && config.Apns.Enabled && what != push.ActRead && callStatus == "" && isSilent == "" } func apnsNotificationConfig(what, topic string, data map[string]string, unread int, config *configType) *fcmv1.ApnsConfig { callStatus := data["webrtc"] expires := time.Now().UTC().Add(time.Duration(defaultTimeToLive) * time.Second) if config.TimeToLive > 0 { expires = time.Now().UTC().Add(time.Duration(config.TimeToLive) * time.Second) } bundleId := config.ApnsBundleID pushType := common.ApnsPushTypeAlert priority := 10 interruptionLevel := common.InterruptionLevelTimeSensitive if callStatus == "started" { // Send VOIP push only when a new call is started, otherwise send normal alert. interruptionLevel = common.InterruptionLevelCritical // FIXME: PushKit notifications do not work with the current FCM adapter. // Using normal pushes as a poor-man's replacement for VOIP pushes. // Uncomment the following two lines when FCM fixes its problem or when we switch to // a different adapter. // pushType = common.ApnsPushTypeVoip // bundleId += ".voip" expires = time.Now().UTC().Add(time.Duration(voipTimeToLive) * time.Second) } else if what == push.ActRead { priority = 5 interruptionLevel = common.InterruptionLevelPassive pushType = common.ApnsPushTypeBackground } apsPayload := common.Aps{ Badge: unread, ContentAvailable: 1, MutableContent: 1, InterruptionLevel: interruptionLevel, Sound: "default", ThreadID: topic, } // Do not present alert for read notifications and video calls. if apnsShouldPresentAlert(what, callStatus, data["silent"], config) { body := config.Apns.GetStringField(what, "Body") if body == "$content" { body = data["content"] } apsPayload.Alert = &common.ApsAlert{ Action: config.Apns.GetStringField(what, "Action"), ActionLocKey: config.Apns.GetStringField(what, "ActionLocKey"), Body: body, LaunchImage: config.Apns.GetStringField(what, "LaunchImage"), LocKey: config.Apns.GetStringField(what, "LocKey"), Title: config.Apns.GetStringField(what, "Title"), Subtitle: config.Apns.GetStringField(what, "Subtitle"), TitleLocKey: config.Apns.GetStringField(what, "TitleLocKey"), SummaryArg: config.Apns.GetStringField(what, "SummaryArg"), SummaryArgCount: config.Apns.GetIntField(what, "SummaryArgCount"), } } payload, err := json.Marshal(map[string]any{"aps": apsPayload}) if err != nil { return nil } headers := map[string]string{ common.HeaderApnsExpiration: strconv.FormatInt(expires.Unix(), 10), common.HeaderApnsPriority: strconv.Itoa(priority), common.HeaderApnsTopic: bundleId, common.HeaderApnsCollapseID: topic, common.HeaderApnsPushType: string(pushType), } ac := &fcmv1.ApnsConfig{ Headers: headers, Payload: payload, } return ac } ================================================ FILE: server/push/fcm/push_fcm.go ================================================ // Package fcm implements push notification plugin for Google FCM backend. // Push notifications for Android, iOS and web clients are sent through Google's Firebase Cloud Messaging service. // Package fcm is push notification plugin using Google FCM. // https://firebase.google.com/docs/cloud-messaging package fcm import ( "context" "encoding/json" "errors" "os" "strings" fbase "firebase.google.com/go" legacy "firebase.google.com/go/messaging" fcmv1 "google.golang.org/api/fcm/v1" "github.com/tinode/chat/server/logs" "github.com/tinode/chat/server/push" "github.com/tinode/chat/server/push/common" "github.com/tinode/chat/server/store" "github.com/tinode/chat/server/store/types" "golang.org/x/oauth2/google" "google.golang.org/api/option" ) var handler Handler const ( // Size of the input channel buffer. bufferSize = 1024 // The number of push messages sent in one batch. FCM constant. pushBatchSize = 100 // The number of sub/unsub requests sent in one batch. FCM constant. subBatchSize = 1000 ) // Handler represents the push handler; implements push.PushHandler interface. type Handler struct { input chan *push.Receipt channel chan *push.ChannelReq stop chan bool projectID string client *legacy.Client v1 *fcmv1.Service } type configType struct { Enabled bool `json:"enabled"` DryRun bool `json:"dry_run"` Credentials json.RawMessage `json:"credentials"` CredentialsFile string `json:"credentials_file"` TimeToLive int `json:"time_to_live,omitempty"` ApnsBundleID string `json:"apns_bundle_id,omitempty"` Android *common.Config `json:"android,omitempty"` Apns *common.Config `json:"apns,omitempty"` Webpush *common.Config `json:"webpush,omitempty"` } // Init initializes the push handler func (Handler) Init(jsonconf json.RawMessage) (bool, error) { var config configType err := json.Unmarshal([]byte(jsonconf), &config) if err != nil { return false, errors.New("failed to parse config: " + err.Error()) } if !config.Enabled { return false, nil } if config.Credentials == nil && config.CredentialsFile != "" { config.Credentials, err = os.ReadFile(config.CredentialsFile) if err != nil { return false, err } } if config.Credentials == nil { return false, errors.New("missing credentials") } ctx := context.Background() credentials, err := google.CredentialsFromJSON(ctx, config.Credentials, "https://www.googleapis.com/auth/firebase.messaging") if err != nil { return false, err } if credentials.ProjectID == "" { return false, errors.New("missing project ID") } app, err := fbase.NewApp(ctx, &fbase.Config{}, option.WithCredentials(credentials)) if err != nil { return false, err } handler.client, err = app.Messaging(ctx) if err != nil { return false, err } handler.v1, err = fcmv1.NewService(ctx, option.WithCredentials(credentials), option.WithScopes(fcmv1.FirebaseMessagingScope)) if err != nil { return false, err } handler.input = make(chan *push.Receipt, bufferSize) handler.channel = make(chan *push.ChannelReq, bufferSize) handler.stop = make(chan bool, 1) handler.projectID = credentials.ProjectID go func() { for { select { case rcpt := <-handler.input: go sendFcmV1(rcpt, &config) case sub := <-handler.channel: go processSubscription(sub) case <-handler.stop: return } } }() return true, nil } func sendFcmV1(rcpt *push.Receipt, config *configType) { messages, uids := PrepareV1Notifications(rcpt, config) for i := range messages { req := &fcmv1.SendMessageRequest{ Message: messages[i], ValidateOnly: config.DryRun, } _, err := handler.v1.Projects.Messages.Send("projects/"+handler.projectID, req).Do() if err != nil { gerr, decodingErrs := common.DecodeGoogleApiError(err) for _, err := range decodingErrs { logs.Info.Println("fcm googleapi.Error decoding:", err) } switch strings.ToUpper(gerr.FcmErrCode) { case "": // no error case common.ErrorQuotaExceeded, common.ErrorUnavailable, common.ErrorInternal, common.ErrorUnspecified: // Transient errors. Stop sending this batch. logs.Warn.Println("fcm transient failure:", gerr.FcmErrCode, gerr.ErrMessage) return case common.ErrorSenderIDMismatch, common.ErrorInvalidArgument, common.ErrorThirdPartyAuth: // Config errors. Stop. logs.Warn.Println("fcm invalid config:", gerr.FcmErrCode, gerr.ErrMessage) return case common.ErrorUnregistered: // Token is no longer valid. Delete token from DB and continue sending. logs.Warn.Println("fcm invalid token:", gerr.FcmErrCode, gerr.ErrMessage) if err := store.Devices.Delete(uids[i], messages[i].Token); err != nil { logs.Warn.Println("tnpg failed to delete invalid token:", err) } default: // Unknown error. Stop sending just in case. logs.Warn.Println("tnpg unrecognized error:", gerr.FcmErrCode, gerr.ErrMessage) return } } } } func processSubscription(req *push.ChannelReq) { var channel string var devices []string var device string var channels []string if req.Channel != "" { devices = DevicesForUser(req.Uid) channel = req.Channel } else if req.DeviceID != "" { channels = ChannelsForUser(req.Uid) device = req.DeviceID } if (len(devices) == 0 && device == "") || (len(channels) == 0 && channel == "") { // No channels or devces to subscribe or unsubscribe. return } if len(devices) > subBatchSize { // It's extremely unlikely for a single user to have this many devices. devices = devices[0:subBatchSize] logs.Warn.Println("fcm: user", req.Uid.UserId(), "has more than", subBatchSize, "devices") } var err error var resp *legacy.TopicManagementResponse if channel != "" && len(devices) > 0 { if req.Unsub { resp, err = handler.client.UnsubscribeFromTopic(context.Background(), devices, channel) } else { resp, err = handler.client.SubscribeToTopic(context.Background(), devices, channel) } if err != nil { // Complete failure. logs.Warn.Println("fcm: sub or upsub failed", req.Unsub, err) } else { // Check for partial failure. handleSubErrors(resp, req.Uid, devices) } return } if device != "" && len(channels) > 0 { devices := []string{device} for _, channel := range channels { if req.Unsub { resp, err = handler.client.UnsubscribeFromTopic(context.Background(), devices, channel) } else { resp, err = handler.client.SubscribeToTopic(context.Background(), devices, channel) } if err != nil { // Complete failure. logs.Warn.Println("fcm: sub or upsub failed", req.Unsub, err) break } // Check for partial failure. handleSubErrors(resp, req.Uid, devices) } return } // Invalid request: either multiple channels & multiple devices (not supported) or no channels and no devices. logs.Err.Println("fcm: user", req.Uid.UserId(), "invalid combination of sub/unsub channels/devices", len(devices), len(channels)) } func handleSubErrors(response *legacy.TopicManagementResponse, uid types.Uid, devices []string) { if response.FailureCount <= 0 { return } for _, errinfo := range response.Errors { // FCM documentation sucks. There is no list of possible errors so no action can be taken but logging. logs.Warn.Println("fcm sub/unsub error", errinfo.Reason, uid, devices[errinfo.Index]) } } // IsReady checks if the push handler has been initialized. func (Handler) IsReady() bool { return handler.input != nil } // Push returns a channel that the server will use to send messages to. // If the adapter blocks, the message will be dropped. func (Handler) Push() chan<- *push.Receipt { return handler.input } // Channel returns a channel for subscribing/unsubscribing devices to FCM topics. func (Handler) Channel() chan<- *push.ChannelReq { return handler.channel } // Stop shuts down the handler func (Handler) Stop() { handler.stop <- true } func init() { push.Register("fcm", &handler) } ================================================ FILE: server/push/push.go ================================================ // Package push contains interfaces to be implemented by push notification plugins. package push import ( "encoding/json" "errors" "time" t "github.com/tinode/chat/server/store/types" ) // Push actions const ( // New message. ActMsg = "msg" // New subscription. ActSub = "sub" // Messages read: clear unread count. ActRead = "read" ) // MaxPayloadLength is the maximum length of push payload in multibyte characters. const MaxPayloadLength = 128 // Recipient is a user targeted by the push. type Recipient struct { // Count of user's connections that were live when the packet was dispatched from the server Delivered int `json:"delivered"` // List of user's devices that the packet was delivered to (if known). Len(Devices) >= Delivered Devices []string `json:"devices,omitempty"` // Unread count to include in the push Unread int `json:"unread"` // Indicates whether unread counter in the cache should be incremented before sending the push. ShouldIncrementUnreadCountInCache bool `json:"-"` } // Receipt is the push payload with a list of recipients. type Receipt struct { // List of individual recipients, including those who did not receive the message. To map[t.Uid]Recipient `json:"to"` // Push topic for group notifications. Channel string `json:"channel"` // Actual content to be delivered to the client. Payload Payload `json:"payload"` } // ChannelReq is a request to subscribe/unsubscribe device ID(s) to channel(s) (FCM topic). // - If DeviceID is provided, it's subscribed/unsubscribed to all user's channels. // - If Channel is provided, then all user's devices are subscribed/unsubscribed from the channel. type ChannelReq struct { // Uid is the ID of the user making request. Uid t.Uid // DeviceID is the device-provided token in case a single device is being subscribed to all channels. DeviceID string // Channel to subscribe to or unsubscribe from. Channel string // Unsub is set to true to unsubscribe devices, otherwise subscribe them. Unsub bool } // Payload is content of the push. type Payload struct { // Action type of the push: new message (msg), new subscription (sub), etc. What string `json:"what"` // If this is a silent push: perform action but do not show a notification to the user. Silent bool `json:"silent"` // Topic which was affected by the action. Topic string `json:"topic"` // Timestamp of the action. Timestamp time.Time `json:"ts"` // {data} notification. // Message sender 'usrXXX' From string `json:"from"` // Sequential ID of the message. SeqId int `json:"seq"` // MIME-Type of the message content, text/x-drafty or text/plain ContentType string `json:"mime"` // Actual Data.Content of the message, if requested Content any `json:"content,omitempty"` // State of the video call (available in video call messages only). Webrtc string `json:"webrtc,omitempty"` // If call is audio-only (available only if Webrtc is present). AudioOnly bool `json:"aonly,omitempty"` // Seq id the message is supposed to replace. Replace string `json:"replace,omitempty"` // Subscription change notification. // New access mode when notifying of a subscription change. // ModeNone for both means the subscription is removed. ModeWant t.AccessMode `json:"want,omitempty"` ModeGiven t.AccessMode `json:"given,omitempty"` } // Handler is an interface which must be implemented by handlers. type Handler interface { // Init initializes the handler. Init(jsonconf json.RawMessage) (bool, error) // IsReady сhecks if the handler is initialized. IsReady() bool // Push returns a channel that the server will use to send messages to. // The message will be dropped if the channel blocks. Push() chan<- *Receipt // Subscribe/unsubscribe device from FCM topic (channel). Channel() chan<- *ChannelReq // Stop terminates the handler's worker and stops sending pushes. Stop() } type configType struct { Name string `json:"name"` Config json.RawMessage `json:"config"` } var handlers map[string]Handler // Register a push handler func Register(name string, hnd Handler) { if handlers == nil { handlers = make(map[string]Handler) } if hnd == nil { panic("Register: push handler is nil") } if _, dup := handlers[name]; dup { panic("Register: called twice for handler " + name) } handlers[name] = hnd } // Init initializes registered handlers. func Init(jsconfig json.RawMessage) ([]string, error) { var config []configType if err := json.Unmarshal(jsconfig, &config); err != nil { return nil, errors.New("failed to parse config: " + err.Error()) } var enabled []string for _, cc := range config { if hnd := handlers[cc.Name]; hnd != nil { if ok, err := hnd.Init(cc.Config); err != nil { return nil, err } else if ok { enabled = append(enabled, cc.Name) } } } return enabled, nil } // Push a single message to devices. func Push(msg *Receipt) { if handlers == nil { return } for _, hnd := range handlers { if !hnd.IsReady() { continue } // Push without delay or skip select { case hnd.Push() <- msg: default: } } } // ChannelSub handles a channel (FCM topic) subscription/unsubscription request. func ChannelSub(msg *ChannelReq) { if handlers == nil { return } for _, hnd := range handlers { if !hnd.IsReady() { continue } // Send without delay or skip. select { case hnd.Channel() <- msg: default: } } } // Stop all pushes func Stop() { if handlers == nil { return } for _, hnd := range handlers { if hnd.IsReady() { // Will potentially block hnd.Stop() } } } ================================================ FILE: server/push/stdout/README.md ================================================ # `stdout` push adapter This is an adapter which logs push notifications to `STDOUT` where they can be redirected to file or processed by some other service. This adapter is primarily intended for debugging and logging. ================================================ FILE: server/push/stdout/push_stdout.go ================================================ // Package stdout is a sample implementation of a push plugin. // If enabled, it writes every notification to stdout. package stdout import ( "encoding/json" "errors" "fmt" "os" "github.com/tinode/chat/server/push" ) var handler stdoutPush // How much to buffer the input channel. const defaultBuffer = 32 type stdoutPush struct { initialized bool input chan *push.Receipt channel chan *push.ChannelReq stop chan bool } type configType struct { Enabled bool `json:"enabled"` Buffer int `json:"buffer"` } // Init initializes the handler func (stdoutPush) Init(jsonconf json.RawMessage) (bool, error) { // Check if the handler is already initialized if handler.initialized { return false, errors.New("already initialized") } var config configType if err := json.Unmarshal([]byte(jsonconf), &config); err != nil { return false, errors.New("failed to parse config: " + err.Error()) } handler.initialized = true if !config.Enabled { return false, nil } if config.Buffer <= 0 { config.Buffer = defaultBuffer } handler.input = make(chan *push.Receipt, config.Buffer) handler.channel = make(chan *push.ChannelReq, config.Buffer) handler.stop = make(chan bool, 1) go func() { for { select { case msg := <-handler.input: fmt.Fprintln(os.Stdout, msg) case msg := <-handler.channel: fmt.Fprintln(os.Stdout, msg) case <-handler.stop: return } } }() return true, nil } // IsReady checks if the handler is initialized. func (stdoutPush) IsReady() bool { return handler.input != nil } // Push returns a channel that the server will use to send messages to. // If the adapter blocks, the message will be dropped. func (stdoutPush) Push() chan<- *push.Receipt { return handler.input } // Channel returns a channel that caller can use to subscribe/unsubscribe devices to channels (FCM topics). // If the adapter blocks, the message will be dropped. func (stdoutPush) Channel() chan<- *push.ChannelReq { return handler.channel } // Stop terminates the handler's worker and stops sending pushes. func (stdoutPush) Stop() { handler.stop <- true } func init() { push.Register("stdout", &handler) } ================================================ FILE: server/push/tnpg/README.md ================================================ # TNPG: Push Gateway This is a push notifications adapter which communicates with Tinode Push Gateway (TNPG). TNPG is a proprietary service intended to simplify deployment of on-premise installations. Deploying a Tinode server without TNPG requires [configuring Google FCM](../fcm/) with your own credentials, recompiling Android and iOS clients, releasing them to PlayStore and AppStore under your own accounts. It's usually time consuming and relatively complex. TNPG solves this problem by letting Tinode LLC (the company behind Tinode) to send push notifications on your behalf: you hand a notification over to TNPG, TNPG sends it to the clients using its own credentials and certificates. Internally it uses [Google FCM](https://firebase.google.com/docs/cloud-messaging/) and as such supports the same platforms as FCM. The main advantage of using TNPG over FCM is simplicity of configuration: you can use stock mobile clients with your custom Tinode server, all is needed is a configuration update on the server. ## Configuring TNPG adapter ### Obtain TNPG token 1. Register at https://console.tinode.co and create an organization. 2. Get the TPNG token from the _Self hosting_ → _Push Gateway_ section by following the instructions there. ### Configure the server Update the server config [`tinode.conf`](../../tinode.conf#L413), section `"push"` -> `"name": "tnpg"`: ```js { "enabled": true, "org": "myorg", // Short name (URL) of the organization you registered at console.tinode.co "token": "SoMe_LonG.RaNDoM-StRiNg.12345" // authentication token obtained from console.tinode.co } ``` Make sure the `fcm` section is disabled `"enabled": false` or removed altogether. ================================================ FILE: server/push/tnpg/push_tnpg.go ================================================ // Package tnpg implements push notification plugin for Tinode Push Gateway. package tnpg import ( "bytes" "compress/gzip" "encoding/json" "errors" "io" "net/http" "net/url" "strings" "sync" "github.com/tinode/chat/server/logs" "github.com/tinode/chat/server/push" "github.com/tinode/chat/server/push/common" "github.com/tinode/chat/server/push/fcm" "github.com/tinode/chat/server/store" "github.com/tinode/chat/server/store/types" fcmv1 "google.golang.org/api/fcm/v1" ) const ( baseTargetAddress = "https://pushgw.tinode.co/" pushPath = "pushv1" subsPath = "sub" pushBatchSize = 100 subBatchSize = 1000 bufferSize = 1024 ) var handler Handler const maxPooledPostBodyCap = 1 << 16 var postBodyPool = sync.Pool{ New: func() any { return new(bytes.Buffer) }, } var gzipWriterPool = sync.Pool{ New: func() any { return gzip.NewWriter(nil) }, } // Handler represents state of TNPG push client. type Handler struct { input chan *push.Receipt channel chan *push.ChannelReq stop chan bool pushUrl string subUrl string } type configType struct { Enabled bool `json:"enabled"` OrgID string `json:"org"` AuthToken string `json:"token"` DebugPushGWHost string `json:"debug_server"` } // subUnsubReq is a request to subscribe/unsubscribe device ID(s) to channel(s) (FCM topic). // One device to multiple channels or multiple devices to one channel. type subUnsubReq struct { Channel string `json:"channel,omitempty"` Channels []string `json:"channels,omitempty"` Device string `json:"device,omitempty"` Devices []string `json:"devices,omitempty"` Unsub bool `json:"unsub"` } type tnpgResponse struct { // Push message response only. MessageID string `json:"msg_id,omitempty"` // Server response HTTP code. Code int `json:"code,omitempty"` // FCM response code. Both push and sub/unsub response. ErrorCode string `json:"errcode,omitempty"` ExtendedError string `json:"exerr,omitempty"` ErrorMessage string `json:"errmsg,omitempty"` // Channel sub/unsub response only. Index int `json:"index,omitempty"` } type batchResponse struct { // Number of successfully sent messages. SuccessCount int `json:"sent_count"` // Number of failures. FailureCount int `json:"fail_count"` // Error code and message if the entire batch failed. FatalCode string `json:"errcode,omitempty"` FatalMessage string `json:"errmsg,omitempty"` // Individual reponses in the same order as messages. Could be nil if the entire batch failed. Responses []*tnpgResponse `json:"resp,omitempty"` // Local values httpCode int httpStatus string } // Error codes copied from https://github.com/firebase/firebase-admin-go/blob/master/messaging/messaging.go const ( internalError = "internal-error" invalidAPNSCredentials = "invalid-apns-credentials" invalidArgument = "invalid-argument" messageRateExceeded = "message-rate-exceeded" mismatchedCredential = "mismatched-credential" registrationTokenNotRegistered = "registration-token-not-registered" serverUnavailable = "server-unavailable" tooManyTopics = "too-many-topics" unknownError = "unknown-error" ) // Init initializes the handler func (Handler) Init(jsonconf json.RawMessage) (bool, error) { var config configType if err := json.Unmarshal(jsonconf, &config); err != nil { return false, errors.New("failed to parse config: " + err.Error()) } if !config.Enabled { return false, nil } config.OrgID = strings.TrimSpace(config.OrgID) if config.OrgID == "" { return false, errors.New("organization name is missing") } // Convert to lower case to avoid confusion. config.OrgID = strings.ToLower(config.OrgID) // Construct server URLs. serverAddr := baseTargetAddress if config.DebugPushGWHost != "" { serverAddr = config.DebugPushGWHost } serverUrl, err := url.Parse(serverAddr) if err != nil { return false, err } serverUrl.Path += pushPath + "/" + config.OrgID handler.pushUrl = serverUrl.String() serverUrl, _ = url.Parse(serverAddr) serverUrl.Path += subsPath + "/" + config.OrgID handler.subUrl = serverUrl.String() handler.input = make(chan *push.Receipt, bufferSize) handler.channel = make(chan *push.ChannelReq, bufferSize) handler.stop = make(chan bool, 1) go func() { for { select { case rcpt := <-handler.input: go sendPushes(rcpt, &config) case sub := <-handler.channel: go processSubscription(sub, &config) case <-handler.stop: return } } }() return true, nil } func postMessage(endpoint string, body any, config *configType) (*batchResponse, error) { buf := postBodyPool.Get().(*bytes.Buffer) defer func() { buf.Reset() if cap(buf.Bytes()) > maxPooledPostBodyCap { return } postBodyPool.Put(buf) }() gzw := gzipWriterPool.Get().(*gzip.Writer) defer func() { gzw.Reset(nil) gzipWriterPool.Put(gzw) }() gzw.Reset(buf) err := json.NewEncoder(gzw).Encode(body) if closeErr := gzw.Close(); err == nil { err = closeErr } if err != nil { return nil, err } req, err := http.NewRequest(http.MethodPost, endpoint, buf) if err != nil { return nil, err } req.Header.Add("Authorization", "Bearer "+config.AuthToken) req.Header.Set("Content-Type", "application/json; charset=utf-8") req.Header.Add("Content-Encoding", "gzip") req.Header.Add("Accept-Encoding", "gzip") resp, err := http.DefaultClient.Do(req) if err != nil { return nil, err } var batch batchResponse var reader io.ReadCloser if strings.Contains(resp.Header.Get("Content-Encoding"), "gzip") { reader, err = gzip.NewReader(resp.Body) if err == nil { defer reader.Close() } } else { reader = resp.Body } if err == nil { err = json.NewDecoder(reader).Decode(&batch) } resp.Body.Close() if err != nil { // Just log the error, but don't report it to caller. The push succeeded. logs.Warn.Println("tnpg failed to decode response:", err) } batch.httpCode = resp.StatusCode batch.httpStatus = resp.Status return &batch, nil } func sendPushes(rcpt *push.Receipt, config *configType) { messages, uids := fcm.PrepareV1Notifications(rcpt, nil) n := len(messages) for i := 0; i < n; i += pushBatchSize { upper := min(i+pushBatchSize, n) var payloads []any for j := i; j < upper; j++ { payloads = append(payloads, messages[j]) } resp, err := postMessage(handler.pushUrl, payloads, config) if err != nil { logs.Warn.Println("tnpg push request failed:", err) break } if resp.httpCode >= 300 { logs.Warn.Println("tnpg push rejected:", resp.httpStatus) break } if resp.FatalCode != "" { logs.Err.Println("tnpg push failed:", resp.FatalMessage) break } // Check for expired tokens and other errors. handlePushResponse(resp, messages[i:upper], uids[i:upper]) } } func processSubscription(req *push.ChannelReq, config *configType) { su := subUnsubReq{ Unsub: req.Unsub, } if req.Channel != "" { su.Devices = fcm.DevicesForUser(req.Uid) su.Channel = req.Channel } else if req.DeviceID != "" { su.Channels = fcm.ChannelsForUser(req.Uid) su.Device = req.DeviceID } if (len(su.Devices) == 0 && su.Device == "") || (len(su.Channels) == 0 && su.Channel == "") { return } if len(su.Devices) > subBatchSize { // It's extremely unlikely for a single user to have this many devices. su.Devices = su.Devices[0:subBatchSize] logs.Warn.Println("tnpg: user", req.Uid.UserId(), "has more than", subBatchSize, "devices") } resp, err := postMessage(handler.subUrl, &su, config) if err != nil { logs.Warn.Println("tnpg channel sub request failed:", err) return } if resp.httpCode >= 300 { logs.Warn.Println("tnpg channel sub rejected:", resp.httpStatus) return } if resp.FatalCode != "" { logs.Err.Println("tnpg channel sub failed:", resp.FatalMessage) return } // Check for expired tokens and other errors. handleSubResponse(resp, req, su.Devices, su.Channels) } func handlePushResponse(batch *batchResponse, messages []*fcmv1.Message, uids []types.Uid) { if batch.FailureCount <= 0 { return } for i, resp := range batch.Responses { switch resp.ErrorCode { case "": // no error case common.ErrorQuotaExceeded, common.ErrorUnavailable, common.ErrorInternal, common.ErrorUnspecified: // Transient errors. Stop sending this batch. logs.Warn.Println("tnpg transient failure:", resp.ErrorMessage) return case common.ErrorInvalidArgument: // Usually an invalid token. logs.Warn.Println("tnpg invalid argument:", resp.ExtendedError, resp.ErrorMessage) if strings.Contains(resp.ExtendedError, "message.token") { if err := store.Devices.Delete(uids[i], messages[i].Token); err != nil { logs.Warn.Println("tnpg failed to delete invalid token:", err) } } case common.ErrorSenderIDMismatch, common.ErrorThirdPartyAuth: // Config errors logs.Warn.Println("tnpg invalid config:", resp.ExtendedError, resp.ErrorMessage) return case common.ErrorUnregistered: // Token is no longer valid. logs.Info.Println("tnpg invalid token:", resp.ErrorMessage, resp.ExtendedError, resp.MessageID) if err := store.Devices.Delete(uids[i], messages[i].Token); err != nil { logs.Warn.Println("tnpg failed to delete invalid token:", err) } default: logs.Warn.Println("tnpg unrecognized error:", resp.ErrorCode, resp.ErrorMessage, resp.ExtendedError, resp.Code) } } } func handleSubResponse(batch *batchResponse, req *push.ChannelReq, devices, channels []string) { if batch.FailureCount <= 0 { return } var src string for _, resp := range batch.Responses { if len(devices) > 0 { src = devices[resp.Index] } else { src = channels[resp.Index] } // FCM documentation sucks. There is no list of possible errors so no action can be taken but logging. logs.Warn.Println("fcm sub/unsub error", resp.ErrorCode, req.Uid, src) } } // IsReady checks if the handler is initialized. func (Handler) IsReady() bool { return handler.input != nil } // Push returns a channel that the server will use to send messages to. // If the adapter blocks, the message will be dropped. func (Handler) Push() chan<- *push.Receipt { return handler.input } // Channel returns a channel that the server will use to send group requests to. // If the adapter blocks, the message will be dropped. func (Handler) Channel() chan<- *push.ChannelReq { return handler.channel } // Stop terminates the handler's worker and stops sending pushes. func (Handler) Stop() { handler.stop <- true } func init() { push.Register("tnpg", &handler) } ================================================ FILE: server/push.go ================================================ /****************************************************************************** * * Description: * Push notifications handling. * *****************************************************************************/ package main import ( "time" "github.com/tinode/chat/server/push" "github.com/tinode/chat/server/store/types" ) // Subscribe or unsubscribe user to/from FCM topic (channel). func (t *Topic) channelSubUnsub(uid types.Uid, sub bool) { push.ChannelSub(&push.ChannelReq{ Uid: uid, Channel: types.GrpToChn(t.name), Unsub: !sub, }) } // Prepares a payload to be delivered to a mobile device as a push notification in response to a {data} message. func (t *Topic) pushForData(fromUid types.Uid, data *MsgServerData, msgMarkedAsReadBySender bool) *push.Receipt { // Passing `Topic` as `t.name` for group topics and P2P topics. The p2p topic name is later rewritten for // each recipient then the payload is created: p2p recipient sees the topic as the ID of the other user. // Initialize the push receipt. contentType, _ := data.Head["mime"].(string) receipt := push.Receipt{ To: make(map[types.Uid]push.Recipient, t.subsCount()), Payload: push.Payload{ What: push.ActMsg, Silent: false, Topic: t.name, From: data.From, Timestamp: data.Timestamp, SeqId: data.SeqId, ContentType: contentType, Content: data.Content, }, } if webrtc, found := data.Head["webrtc"].(string); found { receipt.Payload.Webrtc = webrtc audioOnly, _ := data.Head["aonly"].(bool) receipt.Payload.AudioOnly = audioOnly } if replace, found := data.Head["replace"].(string); found { receipt.Payload.Replace = replace } if t.isChan { // Channel readers should get a push on a channel name (as an FCM topic push). receipt.Channel = types.GrpToChn(t.name) } for uid, pud := range t.perUser { online := pud.online if uid == fromUid && online == 0 { // Make sure the sender's devices receive a silent push. online = 1 } // Send only to those who have notifications enabled. mode := pud.modeWant & pud.modeGiven if mode.IsPresencer() && mode.IsReader() && !pud.deleted && !pud.isChan { receipt.To[uid] = push.Recipient{ // Number of attached sessions the data message will be delivered to. // Push notifications sent to users with non-zero online sessions will be marked silent. Delivered: online, // Unread counts are incremented for all recipients, // and for sender only if the message wasnt't marked 'read' by the sender ShouldIncrementUnreadCountInCache: uid != fromUid || !msgMarkedAsReadBySender, } } } if len(receipt.To) > 0 || receipt.Channel != "" { return &receipt } // If there are no recipient there is no need to send the push notification. return nil } func (t *Topic) preparePushForSubReceipt(fromUid types.Uid, now time.Time) *push.Receipt { // The `Topic` in the push receipt is `t.xoriginal` for group topics, `fromUid` for p2p topics, // not the t.original(fromUid) because it's the topic name as seen by the recipient, not by the sender. topic := t.xoriginal if t.cat == types.TopicCatP2P { topic = fromUid.UserId() } // Initialize the push receipt. receipt := &push.Receipt{ To: make(map[types.Uid]push.Recipient, t.subsCount()), Payload: push.Payload{ What: push.ActSub, Silent: false, Topic: topic, From: fromUid.UserId(), Timestamp: now, SeqId: t.lastID, }, } return receipt } // Prepares payload to be delivered to a mobile device as a push notification in response to a new subscription in a p2p topic. func (t *Topic) pushForP2PSub(fromUid, toUid types.Uid, want, given types.AccessMode, now time.Time) *push.Receipt { receipt := t.preparePushForSubReceipt(fromUid, now) receipt.Payload.ModeWant = want receipt.Payload.ModeGiven = given receipt.To[toUid] = push.Recipient{} return receipt } // Prepares payload to be delivered to a mobile device as a push notification in response to a new subscription in a group topic. func (t *Topic) pushForGroupSub(fromUid types.Uid, now time.Time) *push.Receipt { receipt := t.preparePushForSubReceipt(fromUid, now) if pud, ok := t.perUser[fromUid]; ok { receipt.Payload.ModeWant = pud.modeWant receipt.Payload.ModeGiven = pud.modeGiven } else { // Sender is not a subscriber (BUG?) return nil } for uid, pud := range t.perUser { // Send only to those who have notifications enabled. mode := pud.modeWant & pud.modeGiven if mode.IsPresencer() && mode.IsReader() && !pud.deleted && !pud.isChan { receipt.To[uid] = push.Recipient{} } } if len(receipt.To) > 0 || receipt.Channel != "" { return receipt } return nil } // Prepares payload to be delivered to a mobile device as a push notification in response to owner deleting a channel. func pushForChanDelete(topicName string, now time.Time) *push.Receipt { topicName = types.GrpToChn(topicName) // Initialize the push receipt. return &push.Receipt{ Payload: push.Payload{ What: push.ActSub, Silent: true, Topic: topicName, Timestamp: now, ModeWant: types.ModeNone, ModeGiven: types.ModeNone, }, Channel: topicName, } } // Prepares payload to be delivered to a mobile device as a push notification in response to receiving "read" notification. func (t *Topic) pushForReadRcpt(uid types.Uid, seq int, now time.Time) *push.Receipt { // The `Topic` in the push receipt is `t.xoriginal` for group topics, `fromUid` for p2p topics, // not the t.original(fromUid) because it's the topic name as seen by the recipient, not by the sender. topic := t.xoriginal if t.cat == types.TopicCatP2P { topic = uid.UserId() } // Initialize the push receipt. receipt := &push.Receipt{ To: make(map[types.Uid]push.Recipient, 1), Payload: push.Payload{ What: push.ActRead, Silent: true, Topic: topic, From: uid.UserId(), Timestamp: now, SeqId: seq, }, } receipt.To[uid] = push.Recipient{} return receipt } // Process push notification. func sendPush(rcpt *push.Receipt) { if rcpt == nil || globals.usersUpdate == nil { return } var local *UserCacheReq // In case of a cluster pushes will be initiated at the nodes which own the users. // Sort users into local and remote. if globals.cluster != nil { local = &UserCacheReq{PushRcpt: &push.Receipt{ Payload: rcpt.Payload, Channel: rcpt.Channel, To: make(map[types.Uid]push.Recipient), }} remote := &UserCacheReq{PushRcpt: &push.Receipt{ Payload: rcpt.Payload, Channel: rcpt.Channel, To: make(map[types.Uid]push.Recipient), }} for uid, recipient := range rcpt.To { if globals.cluster.isRemoteTopic(uid.UserId()) { remote.PushRcpt.To[uid] = recipient } else { local.PushRcpt.To[uid] = recipient } } if len(remote.PushRcpt.To) > 0 || remote.PushRcpt.Channel != "" { globals.cluster.routeUserReq(remote) } } else { local = &UserCacheReq{PushRcpt: rcpt} } if len(local.PushRcpt.To) > 0 || local.PushRcpt.Channel != "" { select { case globals.usersUpdate <- local: default: } } } ================================================ FILE: server/ringhash/ringhash.go ================================================ // Package ringhash implementats a consistent ring hash: // https://en.wikipedia.org/wiki/Consistent_hashing package ringhash import ( "encoding/ascii85" "hash/crc32" "hash/fnv" "sort" "strconv" "github.com/tinode/chat/server/logs" ) // Hash is a signature of a hash function used by the package. type Hash func(data []byte) uint32 type elem struct { key string hash uint32 } type sortable []elem func (k sortable) Len() int { return len(k) } func (k sortable) Swap(i, j int) { k[i], k[j] = k[j], k[i] } func (k sortable) Less(i, j int) bool { // Weak hash function may cause collisions. if k[i].hash < k[j].hash { return true } if k[i].hash == k[j].hash { return k[i].key < k[j].key } return false } // Ring is the definition of the ringhash. type Ring struct { keys []elem // Sorted list of keys. signature string replicas int hashfunc Hash } // New initializes an empty ringhash with the given number of replicas and a hash function. // If the hash function is nil, crc32.NewIEEE() is used. func New(replicas int, fn Hash) *Ring { ring := &Ring{ replicas: replicas, hashfunc: fn, } if ring.hashfunc == nil { ring.hashfunc = func(data []byte) uint32 { hash := crc32.NewIEEE() hash.Write(data) return hash.Sum32() } } return ring } // Len returns the number of keys in the ring. func (ring *Ring) Len() int { return len(ring.keys) } // Add adds keys to the ring. func (ring *Ring) Add(keys ...string) { for _, key := range keys { for i := range ring.replicas { ring.keys = append(ring.keys, elem{ hash: ring.hashfunc([]byte(strconv.Itoa(i) + key)), key: key}) } } sort.Sort(sortable(ring.keys)) // Calculate signature hash := fnv.New128a() b := make([]byte, 4) for _, key := range ring.keys { b[0] = byte(key.hash) b[1] = byte(key.hash >> 8) b[2] = byte(key.hash >> 16) b[3] = byte(key.hash >> 24) hash.Write(b) hash.Write([]byte(key.key)) } b = []byte{} b = hash.Sum(b) dst := make([]byte, ascii85.MaxEncodedLen(len(b))) ascii85.Encode(dst, b) ring.signature = string(dst) } // Get returns the closest item in the ring to the provided key. func (ring *Ring) Get(key string) string { if ring.Len() == 0 { return "" } hash := ring.hashfunc([]byte(key)) // Binary search for appropriate replica. idx := sort.Search(len(ring.keys), func(i int) bool { el := ring.keys[i] return (el.hash > hash) || (el.hash == hash && el.key >= key) }) // Means we have cycled back to the first replica. if idx == len(ring.keys) { idx = 0 } return ring.keys[idx].key } // Signature returns the ring's hash signature. Two identical ringhashes // will have the same signature. Two hashes with different // number of keys or replicas or hash functions will have different // signatures. func (ring *Ring) Signature() string { return ring.signature } func (ring *Ring) dump() { for _, e := range ring.keys { logs.Info.Printf("key: '%s', hash=%d", e.key, e.hash) } } ================================================ FILE: server/ringhash/ringhash_test.go ================================================ package ringhash_test import ( "fmt" "hash/crc32" "hash/fnv" "testing" "github.com/tinode/chat/server/ringhash" ) func TestHashing(t *testing.T) { // Ring with 3 elements hashed by crc32.ChecksumIEEE ring := ringhash.New(3, crc32.ChecksumIEEE) ring.Add("A", "B", "C") // The ring contains: // B0 = 105710768 -> B // B1 = 525743601 -> B // B2 = 880502322 -> B // C2 = 1132222116 -> C // C1 = 1750140263 -> C // C0 = 1900688422 -> C // A1 = 2254398539 -> A // A0 = 2672055562 -> A // A2 = 2909943688 -> A // Key=A, Hash=3554254475 -> B0 // Key=B, Hash=1255198513 -> C1 // Key=C, Hash=1037565863 -> C2 // Key=D, Hash=2746444292 -> A2 // Key=E, Hash=3568589458 -> B0 // Key=F, Hash=1304234792 -> C1 testHashes := map[string]string{ "A": "B", "B": "C", "C": "C", "D": "A", "E": "B", "F": "C", } for k, v := range testHashes { n := ring.Get(k) if n != v { t.Errorf("Key '%s', expecting '%s', got '%s'", k, v, n) } } ring.Add("X") // Adding // X0 = 4214226378 // X1 = 3795111051 // X2 = 3373899592 // Changes to mapping: testHashes["A"] = "X" testHashes["E"] = "X" for k, v := range testHashes { n := ring.Get(k) if n != v { t.Errorf("Key '%s, expecting %s, got %s", k, v, n) } } } func TestConsistency(t *testing.T) { ring1 := ringhash.New(3, nil) ring2 := ringhash.New(3, nil) ring1.Add("owl", "crow", "sparrow") ring2.Add("sparrow", "owl", "crow") if ring1.Get("duck") != ring2.Get("duck") { t.Error("'duck' should map to 'sparrow' in both cases") } // Collision test: these strings generate CRC32 collisions // Google's implementation fails this test. // 0VXGD 0BGABAA // 0VXGG 0BGABAB // 0VXGF 0BGABAC // 0VXGA 0BGABAD // 0VXGC 0BGABAF // 0VXGB 0BGABAG // 0VXGM 0BGABAH // 0VXGL 0BGABAI // 0VXGO 0BGABAJ // 0VXGN 0BGABAK // 0VXGI 0BGABAL ring1 = ringhash.New(1, crc32.ChecksumIEEE) ring2 = ringhash.New(1, crc32.ChecksumIEEE) ring1.Add("VXGD", "BGABAA", "VXGG", "BGABAB", "VXGF", "BGABAC") ring2.Add("BGABAA", "VXGD", "BGABAB", "VXGG", "BGABAC", "VXGF") str := []string{ "datsam", "kGmVht", "dSPmEr", "RloWQr", "WFkAkG", "gLBNPX", "twEwll", "RnRdaf", "ruEMuJ", "ZvXJsJ", "xjQzKD", "CKfSFg", "BMKMvM", "PSzYdC", "CsxqTR", "IbzdXz", "xdnZGj", "VdHcVp", "iVgIvH", "bZsTIX", "CyRBUO", "ylgEGS", "vOTwJD", "JZbyFU", "Hayrly", "jQQkOV", "NEVjlJ", "SkJfie", "HrdJuL", "ASwkXH", "UwJOmo", "nfbrxA", } for _, key := range str { if ring1.Get(key) != ring2.Get(key) { t.Errorf("'%s' should map to the same bin in both cases", key) } } } func TestSignature(t *testing.T) { ring1 := ringhash.New(4, nil) ring2 := ringhash.New(4, nil) ring1.Add("owl", "crow", "sparrow") ring2.Add("sparrow", "owl", "crow") if ring1.Signature() != ring2.Signature() { t.Error("Signatures must be identical") } ring1 = ringhash.New(4, nil) ring2 = ringhash.New(5, nil) ring1.Add("owl", "crow", "sparrow") ring2.Add("owl", "crow", "sparrow") if ring1.Signature() == ring2.Signature() { t.Error("Signatures must be different - different count of replicas") } ring1 = ringhash.New(4, nil) ring2 = ringhash.New(4, nil) ring1.Add("owl", "crow", "sparrow") ring2.Add("owl", "crow", "sparrow", "crane") if ring1.Signature() == ring2.Signature() { t.Error("Signatures must be different - different keys") } fnvHashfunc := func(data []byte) uint32 { hash := fnv.New32a() hash.Write(data) return hash.Sum32() } ring1 = ringhash.New(4, nil) ring2 = ringhash.New(4, fnvHashfunc) ring1.Add("owl", "crow", "sparrow") ring2.Add("owl", "crow", "sparrow") if ring1.Signature() == ring2.Signature() { t.Error("Signatures must be different - different hash functions") } } func BenchmarkGet8(b *testing.B) { benchmarkGet(b, 8) } func BenchmarkGet32(b *testing.B) { benchmarkGet(b, 32) } func BenchmarkGet128(b *testing.B) { benchmarkGet(b, 128) } func BenchmarkGet512(b *testing.B) { benchmarkGet(b, 512) } func benchmarkGet(b *testing.B, keycount int) { ring := ringhash.New(53, nil) var ids []string for i := range keycount { ids = append(ids, fmt.Sprintf("id=%d", i)) } ring.Add(ids...) b.ResetTimer() for i := range b.N { ring.Get(ids[i&(keycount-1)]) } } ================================================ FILE: server/run-cluster.sh ================================================ #!/bin/bash # Start/stop test cluster on localhost. This is NOT a production script. Use it for reference only. # Names of cluster nodes ALL_NODE_NAMES=( one two three ) # Port where the first node will listen for client connections over http HTTP_BASE_PORT=6060 # Port where the first node will listen for gRPC intra-cluster connections. GRPC_BASE_PORT=16060 USAGE="Usage: $0 [ --config ] {start|stop}" # Your server binary may have a different name and location. SERVER='./server' if [ "$#" -lt "1" ]; then echo $USAGE exit 1 fi while [[ $# -gt 0 ]]; do key="$1" shift echo "$key" case "$key" in -c|--config) config=$1 shift # value ;; -s|--static_data) static_data=$1 shift # value ;; start) if [ ! -z "$config" ] ; then TINODE_CONF=$config else TINODE_CONF="tinode.conf" fi if [ ! -z "${static_data+x}" ] ; then STATIC_DATA_DIR=$static_data else STATIC_DATA_DIR="static" fi echo "HTTP ports 6060-6062, gRPC ports 16060-16062, config ${config}" HTTP_PORT=$HTTP_BASE_PORT GRPC_PORT=$GRPC_BASE_PORT for NODE_NAME in "${ALL_NODE_NAMES[@]}" do # Start the node $SERVER -config=${TINODE_CONF} -cluster_self=${NODE_NAME} -listen=:${HTTP_PORT} -grpc_listen=:${GRPC_PORT} -static_data=${STATIC_DATA_DIR} -log_flags=stdFlags,shortfile & # Save PID of the node to a temp file. # /var/tmp/ does not requre root access. echo $!> "/var/tmp/tinode-${NODE_NAME}.pid" # Increment ports for the next node. HTTP_PORT=$((HTTP_PORT+1)) GRPC_PORT=$((GRPC_PORT+1)) done exit 0 ;; stop) echo 'Stopping cluster' for NODE_NAME in "${ALL_NODE_NAMES[@]}" do # Read PIDs of running nodes from temp files and kill them. kill `cat /var/tmp/tinode-${NODE_NAME}.pid` # Clean up: delete temp files. rm "/var/tmp/tinode-${NODE_NAME}.pid" done exit 0 ;; *) echo $USAGE exit 1 esac done ================================================ FILE: server/sanity-test.sh ================================================ #!/bin/bash BINARY_PATH=$GOPATH/bin TINODE_BINARY=$BINARY_PATH/server # Kills and removes any running containers. cleanup() { ./run-cluster.sh stop if [ -f "./server" ]; then rm ./server fi docker stop mysql && docker rm mysql } # Reports failure. fail() { cleanup echo "**************************************************" printf "Tests Failed: ${@}\n" echo "**************************************************" exit 1 } # Reports success. pass() { cleanup echo "**************************************************" echo "* OK *" echo "**************************************************" exit 0 } # Brings up a mysql docker container. setup() { docker info 1>/dev/null 2>&1 || (echo "docker not running" && return 1) docker run -p 3306:3306 --name mysql --env MYSQL_ALLOW_EMPTY_PASSWORD=yes -d mysql:5.7 || return 1 # This fails to detect when the mysql is actually ready. # TODO: figure out why. #until nc -z -v -w30 localhost 3306; do # echo "Waiting for database connection..." # sleep 1 #done echo -n "Waiting for mysql to come up." while ! mysqladmin ping -u root -h 127.0.0.1 --silent; do echo -n "." sleep 1 done # Make sure there's no Tinode server binary in the current directory. if [ -f "./server" ]; then rm ./server fi } # Compiles Tinode binaries. build() { go install -tags mysql -ldflags "-X main.buildstamp=`date -u '+%Y%m%dT%H:%M:%SZ'`" \ github.com/tinode/chat/tinode-db \ github.com/tinode/chat/server && \ ln -s $TINODE_BINARY } # Initializes Tinode database. init-db() { $GOPATH/bin/tinode-db -config=./tinode.conf -data=../tinode-db/data.json } wait-for() { local port=$1 while ! nc -z localhost $port; do sleep 1 done } # Brings up a three-node Tinode cluster. run-server() { ./run-cluster.sh -s "" start && wait-for 16060 } send-requests() { local expect=12 local port=$1 local id=$2 local outfile=$(mktemp /tmp/tinode-${id}.txt) pushd . cd ../tn-cli python3 tn-cli.py --host=localhost:${port} --no-login < sample-script.txt > $outfile || fail "Test script failed (instance port ${port})" popd num_positive_responses=`grep -c '<= 20[0-9]' $outfile` if [ $num_positive_responses -ne expect ] then fail "Instance ${port}: unexpected number of 20X responses: ${num_positive_responses} (expected ${expected}). Log file ${outfile}" fi rm $outfile } # Catch unexpected failures, do cleanup and output an error message trap 'cleanup ; fail "For Unexpected Reasons"'\ HUP INT QUIT PIPE TERM # Normal script termination. #trap 'cleanup'\ # EXIT run_id=`date +%s` echo "+----------------------------------------------------+" echo "| Tinode sanity test. |" echo "+----------------------------------------------------+" echo "Timestamp = ${run_id}" setup || fail "Test setup failed." build || fail "Could not build Tinode binaries" init-db || fail "Could not initialize Tinode database" run-server || fail "Could not start tinode" # Test requests. send-requests 16060 $run_id send-requests 16061 $run_id send-requests 16062 $run_id pass ================================================ FILE: server/session.go ================================================ /****************************************************************************** * * Description : * * Handling of user sessions/connections. One user may have multiple sesions. * Each session may handle multiple topics * *****************************************************************************/ package main import ( "container/list" "encoding/json" "fmt" "net/http" "strings" "sync" "sync/atomic" "time" "github.com/gorilla/websocket" "github.com/tinode/chat/pbx" "github.com/tinode/chat/server/auth" "github.com/tinode/chat/server/logs" "github.com/tinode/chat/server/store" "github.com/tinode/chat/server/store/types" "golang.org/x/text/language" ) // Maximum number of queued messages before session is considered stale and dropped. const sendQueueLimit = 128 // Time given to a background session to terminate to avoid tiggering presence notifications. // If session terminates (or unsubscribes from topic) in this time frame notifications are not sent at all. const deferredNotificationsTimeout = time.Second * 5 var minSupportedVersionValue = parseVersion(minSupportedVersion) // SessionProto is the type of the wire transport. type SessionProto int // Constants defining individual types of wire transports. const ( // NONE is undefined/not set. NONE SessionProto = iota // WEBSOCK represents websocket connection. WEBSOCK // LPOLL represents a long polling connection. LPOLL // GRPC is a gRPC connection GRPC // PROXY is temporary session used as a proxy at master node. PROXY // MULTIPLEX is a multiplexing session reprsenting a connection from proxy topic to master. MULTIPLEX ) // Session represents a single WS connection or a long polling session. A user may have multiple // sessions. type Session struct { // protocol - NONE (unset), WEBSOCK, LPOLL, GRPC, PROXY, MULTIPLEX proto SessionProto // Session ID sid string // Websocket. Set only for websocket sessions. ws *websocket.Conn // Pointer to session's record in sessionStore. Set only for Long Poll sessions. lpTracker *list.Element // gRPC handle. Set only for gRPC clients. grpcnode pbx.Node_MessageLoopServer // Reference to the cluster node where the session has originated. Set only for cluster RPC sessions. clnode *ClusterNode // Reference to multiplexing session. Set only for proxy sessions. multi *Session proxiedTopic string // IP address of the client. For long polling this is the IP of the last poll. remoteAddr string // User agent, a string provived by an authenticated client in {login} packet. userAgent string // Protocol version of the client: ((major & 0xff) << 8) | (minor & 0xff). ver int // Device ID of the client deviceID string // Platform: web, ios, android platf string // Human language of the client lang string // Country code of the client countryCode string // ID of the current user. Could be zero if session is not authenticated // or for multiplexing sessions. uid types.Uid // Authentication level - NONE (unset), ANON, AUTH, ROOT. authLvl auth.Level // Time when the long polling session was last refreshed lastTouched time.Time // Time when the session received any packer from client lastAction int64 // Timer which triggers after some seconds to mark background session as foreground. bkgTimer *time.Timer // Number of subscribe/unsubscribe requests in flight. inflightReqs *boundedWaitGroup // Synchronizes access to session store in cluster mode: // subscribe/unsubscribe replies are asynchronous. sessionStoreLock sync.Mutex // Indicates that the session is terminating. // After this flag's been flipped to true, there must not be any more writes // into the session's send channel. // Read/written atomically. // 0 = false // 1 = true terminating int32 // Background session: subscription presence notifications and online status are delayed. background bool // Outbound mesages, buffered. // The content must be serialized in format suitable for the session. send chan any // Channel for shutting down the session, buffer 1. // Content in the same format as for 'send' stop chan any // detach - channel for detaching session from topic, buffered. // Content is topic name to detach from. detach chan string // Map of topic subscriptions, indexed by topic name. // Don't access directly. Use getters/setters. subs map[string]*Subscription // Mutex for subs access: both topic go routines and network go routines access // subs concurrently. subsLock sync.RWMutex // Needed for long polling and grpc. lock sync.Mutex // Field used only in cluster mode by topic master node. // Type of proxy to master request being handled. proxyReq ProxyReqType } // Subscription is a mapper of sessions to topics. type Subscription struct { // Channel to communicate with the topic, copy of Topic.clientMsg broadcast chan<- *ClientComMessage // Session sends a signal to Topic when this session is unsubscribed // This is a copy of Topic.unreg done chan<- *ClientComMessage // Channel to send {meta} requests, copy of Topic.meta meta chan<- *ClientComMessage // Channel to ping topic with session's updates, copy of Topic.supd supd chan<- *sessionUpdate } func (s *Session) addSub(topic string, sub *Subscription) { if s.multi != nil { s.multi.addSub(topic, sub) return } s.subsLock.Lock() // Sessions that serve as an interface between proxy topics and their masters (proxy sessions) // may have only one subscription, that is, to its master topic. // Normal sessions may be subscribed to multiple topics. if !s.isMultiplex() || s.countSub() == 0 { s.subs[topic] = sub } s.subsLock.Unlock() } func (s *Session) getSub(topic string) *Subscription { // Don't check s.multi here. Let it panic if called for proxy session. s.subsLock.RLock() defer s.subsLock.RUnlock() return s.subs[topic] } func (s *Session) delSub(topic string) { if s.multi != nil { s.multi.delSub(topic) return } s.subsLock.Lock() delete(s.subs, topic) s.subsLock.Unlock() } func (s *Session) countSub() int { if s.multi != nil { return s.multi.countSub() } return len(s.subs) } // Inform topics that the session is being terminated. // No need to check for s.multi because it's not called for PROXY sessions. func (s *Session) unsubAll() { s.subsLock.RLock() defer s.subsLock.RUnlock() for _, sub := range s.subs { // sub.done is the same as topic.unreg // The whole session is being dropped; ClientComMessage is a wrapper for session, ClientComMessage.init is false. // keep redundant init: false so it can be searched for. sub.done <- &ClientComMessage{sess: s, init: false} } } // Indicates whether this session is a local interface for a remote proxy topic. // It multiplexes multiple sessions. func (s *Session) isMultiplex() bool { return s.proto == MULTIPLEX } // Indicates whether this session is a short-lived proxy for a remote session. func (s *Session) isProxy() bool { return s.proto == PROXY } // Cluster session: either a proxy or a multiplexing session. func (s *Session) isCluster() bool { return s.isProxy() || s.isMultiplex() } func (s *Session) scheduleClusterWriteLoop() { if globals.cluster != nil && globals.cluster.proxyEventQueue != nil { globals.cluster.proxyEventQueue.Schedule( func() { s.clusterWriteLoop(s.proxiedTopic) }) } } func (s *Session) supportsMessageBatching() bool { switch s.proto { case WEBSOCK: return true case GRPC: return true default: return false } } // queueOut attempts to send a list of ServerComMessages to a session write loop; // it fails if the send buffer is full. func (s *Session) queueOutBatch(msgs []*ServerComMessage) bool { if s == nil { return true } if atomic.LoadInt32(&s.terminating) > 0 { return true } if s.multi != nil { // In case of a cluster we need to pass a copy of the actual session. for i := range msgs { msgs[i].sess = s } if s.multi.queueOutBatch(msgs) { s.multi.scheduleClusterWriteLoop() return true } return false } if s.supportsMessageBatching() { select { case s.send <- msgs: default: // Never block here since it may also block the topic's run() goroutine. logs.Err.Println("s.queueOut: session's send queue2 full", s.sid) return false } if s.isMultiplex() { s.scheduleClusterWriteLoop() } } else { for _, msg := range msgs { s.queueOut(msg) } } return true } // queueOut attempts to send a ServerComMessage to a session write loop; // it fails, if the send buffer is full. func (s *Session) queueOut(msg *ServerComMessage) bool { if s == nil { return true } if atomic.LoadInt32(&s.terminating) > 0 { return true } if s.multi != nil { // In case of a cluster we need to pass a copy of the actual session. msg.sess = s if s.multi.queueOut(msg) { s.multi.scheduleClusterWriteLoop() return true } return false } // Record latency only on {ctrl} messages and end-user sessions. if msg.Ctrl != nil && msg.Id != "" { if !msg.Ctrl.Timestamp.IsZero() && !s.isCluster() { duration := time.Since(msg.Ctrl.Timestamp).Milliseconds() statsAddHistSample("RequestLatency", float64(duration)) } if 200 <= msg.Ctrl.Code && msg.Ctrl.Code < 600 { statsInc(fmt.Sprintf("CtrlCodesTotal%dxx", msg.Ctrl.Code/100), 1) } else { logs.Warn.Println("Invalid response code: ", msg.Ctrl.Code) } } select { case s.send <- msg: default: // Never block here since it may also block the topic's run() goroutine. logs.Err.Println("s.queueOut: session's send queue full", s.sid) return false } if s.isMultiplex() { s.scheduleClusterWriteLoop() } return true } // queueOutBytes attempts to send a ServerComMessage already serialized to []byte. // If the send buffer is full, it fails. func (s *Session) queueOutBytes(data []byte) bool { if s == nil || atomic.LoadInt32(&s.terminating) > 0 { return true } select { case s.send <- data: default: logs.Err.Println("s.queueOutBytes: session's send queue full", s.sid) return false } if s.isMultiplex() { s.scheduleClusterWriteLoop() } return true } func (s *Session) maybeScheduleClusterWriteLoop() { if s.multi != nil { s.multi.scheduleClusterWriteLoop() return } if s.isMultiplex() { s.scheduleClusterWriteLoop() } } func (s *Session) detachSession(fromTopic string) { if atomic.LoadInt32(&s.terminating) == 0 { s.detach <- fromTopic s.maybeScheduleClusterWriteLoop() } } func (s *Session) stopSession(data any) { s.stop <- data s.maybeScheduleClusterWriteLoop() } func (s *Session) purgeChannels() { for len(s.send) > 0 { <-s.send } for len(s.stop) > 0 { <-s.stop } for len(s.detach) > 0 { <-s.detach } } // cleanUp is called when the session is terminated to perform resource cleanup. func (s *Session) cleanUp(expired bool) { atomic.StoreInt32(&s.terminating, 1) s.purgeChannels() s.inflightReqs.Wait() s.inflightReqs = nil if !expired { s.sessionStoreLock.Lock() globals.sessionStore.Delete(s) s.sessionStoreLock.Unlock() } s.background = false s.bkgTimer.Stop() s.unsubAll() // Stop the write loop. s.stopSession(nil) } // Message received, convert bytes to ClientComMessage and dispatch func (s *Session) dispatchRaw(raw []byte) { now := types.TimeNow() var msg ClientComMessage if atomic.LoadInt32(&s.terminating) > 0 { logs.Warn.Println("s.dispatch: message received on a terminating session", s.sid) s.queueOut(ErrLocked("", "", now)) return } if len(raw) == 1 && raw[0] == 0x31 { // 0x31 == '1'. This is a network probe message. Respond with a '0': s.queueOutBytes([]byte{0x30}) return } toLog := raw truncated := "" if len(raw) > 512 { toLog = raw[:512] truncated = "<...>" } logs.Info.Printf("in: '%s%s' sid='%s' uid='%s'", toLog, truncated, s.sid, s.uid) if err := json.Unmarshal(raw, &msg); err != nil { // Malformed message logs.Warn.Println("s.dispatch", err, s.sid) s.queueOut(ErrMalformed("", "", now)) return } s.dispatch(&msg) } func (s *Session) dispatch(msg *ClientComMessage) { now := types.TimeNow() atomic.StoreInt64(&s.lastAction, now.UnixNano()) // This should be the first block here, before any other checks. var resp *ServerComMessage if msg, resp = pluginFireHose(s, msg); resp != nil { // Plugin provided a response. No further processing is needed. s.queueOut(resp) return } else if msg == nil { // Plugin requested to silently drop the request. return } if msg.Extra == nil || (msg.Extra.AsUser == "" && msg.Extra.AuthLevel == "") { // Use current user's ID and auth level. msg.AsUser = s.uid.UserId() msg.AuthLvl = int(s.authLvl) } else if s.authLvl != auth.LevelRoot { // Only root user can set alternative user ID and auth level values. s.queueOut(ErrPermissionDenied("", "", now)) logs.Warn.Println("s.dispatch: non-root assigned asUser", s.sid) return } else if fromUid := types.ParseUserId(msg.Extra.AsUser); fromUid.IsZero() { // Invalid msg.Extra.AsUser. s.queueOut(ErrMalformed("", "", now)) logs.Warn.Println("s.dispatch: malformed asUser: ", msg.Extra.AsUser, s.sid) return } else { // Use provided msg.Extra.AsUser msg.AsUser = msg.Extra.AsUser // Assign auth level, if one is provided. Ignore invalid strings. if authLvl := auth.ParseAuthLevel(msg.Extra.AuthLevel); authLvl == auth.LevelNone { // AuthLvl is not set by the caller, assign default LevelAuth. msg.AuthLvl = int(auth.LevelAuth) } else { msg.AuthLvl = int(authLvl) } } msg.Timestamp = now var handler func(*ClientComMessage) var uaRefresh bool // Check if s.ver is defined checkVers := func(handler func(*ClientComMessage)) func(*ClientComMessage) { return func(m *ClientComMessage) { if s.ver == 0 { logs.Warn.Println("s.dispatch: {hi} is missing", s.sid) s.queueOut(ErrCommandOutOfSequence(m.Id, m.Original, msg.Timestamp)) return } handler(m) } } // Check if user is logged in checkUser := func(handler func(*ClientComMessage)) func(*ClientComMessage) { return func(m *ClientComMessage) { if msg.AsUser == "" { logs.Warn.Println("s.dispatch: authentication required", s.sid) s.queueOut(ErrAuthRequiredReply(m, m.Timestamp)) return } handler(m) } } switch { case msg.Pub != nil: handler = checkVers(checkUser(s.publish)) msg.Id = msg.Pub.Id msg.Original = msg.Pub.Topic uaRefresh = true case msg.Sub != nil: handler = checkVers(checkUser(s.subscribe)) msg.Id = msg.Sub.Id msg.Original = msg.Sub.Topic uaRefresh = true case msg.Leave != nil: handler = checkVers(checkUser(s.leave)) msg.Id = msg.Leave.Id msg.Original = msg.Leave.Topic case msg.Hi != nil: handler = s.hello msg.Id = msg.Hi.Id case msg.Login != nil: handler = checkVers(s.login) msg.Id = msg.Login.Id case msg.Get != nil: handler = checkVers(checkUser(s.get)) msg.Id = msg.Get.Id msg.Original = msg.Get.Topic uaRefresh = true case msg.Set != nil: handler = checkVers(checkUser(s.set)) msg.Id = msg.Set.Id msg.Original = msg.Set.Topic uaRefresh = true case msg.Del != nil: handler = checkVers(checkUser(s.del)) msg.Id = msg.Del.Id msg.Original = msg.Del.Topic case msg.Acc != nil: handler = checkVers(s.acc) msg.Id = msg.Acc.Id case msg.Note != nil: // If user is not authenticated or version not set the {note} is silently ignored. handler = s.note msg.Original = msg.Note.Topic uaRefresh = true default: // Unknown message s.queueOut(ErrMalformed("", "", msg.Timestamp)) logs.Warn.Println("s.dispatch: unknown message", s.sid) return } if globals.cluster.isPartitioned() { // The cluster is partitioned due to network or other failure and this node is a part of the smaller partition. // In order to avoid data inconsistency across the cluster we must reject all requests. s.queueOut(ErrClusterUnreachableReply(msg, msg.Timestamp)) return } msg.sess = s msg.init = true handler(msg) // Notify 'me' topic that this session is currently active. if uaRefresh && msg.AsUser != "" && s.userAgent != "" { if sub := s.getSub(msg.AsUser); sub != nil { // The chan is buffered. If the buffer is exhaused, the session will wait for 'me' to become available sub.supd <- &sessionUpdate{userAgent: s.userAgent} } } } // Request to subscribe to a topic. func (s *Session) subscribe(msg *ClientComMessage) { if strings.HasPrefix(msg.Original, "new") || strings.HasPrefix(msg.Original, "nch") { // Request to create a new group/channel topic. // If we are in a cluster, make sure the new topic belongs to the current node. msg.RcptTo = globals.cluster.genLocalTopicName() } else { var resp *ServerComMessage msg.RcptTo, resp = s.expandTopicName(msg) if resp != nil { s.queueOut(resp) return } } s.inflightReqs.Add(1) // Session can subscribe to topic on behalf of a single user at a time. if sub := s.getSub(msg.RcptTo); sub != nil { s.queueOut(InfoAlreadySubscribed(msg.Id, msg.Original, msg.Timestamp)) s.inflightReqs.Done() } else { select { case globals.hub.join <- msg: default: // Reply with a 503 to the user. s.queueOut(ErrServiceUnavailableReply(msg, msg.Timestamp)) s.inflightReqs.Done() logs.Err.Println("s.subscribe: hub.join queue full, topic ", msg.RcptTo, s.sid) } // Hub will send Ctrl success/failure packets back to session } } // Leave/Unsubscribe a topic func (s *Session) leave(msg *ClientComMessage) { // Expand topic name var resp *ServerComMessage msg.RcptTo, resp = s.expandTopicName(msg) if resp != nil { s.queueOut(resp) return } s.inflightReqs.Add(1) if sub := s.getSub(msg.RcptTo); sub != nil { // Session is attached to the topic. if (msg.Original == "me" || msg.Original == "fnd") && msg.Leave.Unsub { // User should not unsubscribe from 'me' or 'find'. Just leaving is fine. s.queueOut(ErrPermissionDeniedReply(msg, msg.Timestamp)) s.inflightReqs.Done() } else { // Unlink from topic, topic will send a reply. sub.done <- msg } return } s.inflightReqs.Done() if !msg.Leave.Unsub { // Session is not attached to the topic, wants to leave - fine, no change s.queueOut(InfoNotJoined(msg.Id, msg.Original, msg.Timestamp)) } else { // Session wants to unsubscribe from the topic it did not join // TODO(gene): allow topic to unsubscribe without joining first; send to hub to unsub logs.Warn.Println("s.leave:", "must attach first", s.sid) s.queueOut(ErrAttachFirst(msg, msg.Timestamp)) } } // Broadcast a message to all topic subscribers func (s *Session) publish(msg *ClientComMessage) { // TODO(gene): Check for repeated messages with the same ID var resp *ServerComMessage msg.RcptTo, resp = s.expandTopicName(msg) if resp != nil { s.queueOut(resp) return } // Add "sender" header if the message is sent on behalf of another user. if msg.AsUser != s.uid.UserId() { if msg.Pub.Head == nil { msg.Pub.Head = make(map[string]any) } msg.Pub.Head["sender"] = s.uid.UserId() } else if msg.Pub.Head != nil { // Clear potentially false "sender" field. delete(msg.Pub.Head, "sender") if len(msg.Pub.Head) == 0 { msg.Pub.Head = nil } } if sub := s.getSub(msg.RcptTo); sub != nil { // This is a post to a subscribed topic. The message is sent to the topic only select { case sub.broadcast <- msg: default: // Reply with a 503 to the user. s.queueOut(ErrServiceUnavailableReply(msg, msg.Timestamp)) logs.Err.Println("s.publish: sub.broadcast channel full, topic ", msg.RcptTo, s.sid) } } else if msg.RcptTo == "sys" { // Publishing to "sys" topic requires no subscription. select { case globals.hub.routeCli <- msg: default: // Reply with a 503 to the user. s.queueOut(ErrServiceUnavailableReply(msg, msg.Timestamp)) logs.Err.Println("s.publish: hub.route channel full", s.sid) } } else { // Publish request received without attaching to topic first. s.queueOut(ErrAttachFirst(msg, msg.Timestamp)) logs.Warn.Printf("s.publish[%s]: must attach first %s", msg.RcptTo, s.sid) } } // Client metadata func (s *Session) hello(msg *ClientComMessage) { var params map[string]any var deviceIDUpdate bool if s.ver == 0 { s.ver = parseVersion(msg.Hi.Version) if s.ver == 0 { logs.Warn.Println("s.hello:", "failed to parse version", s.sid) s.queueOut(ErrMalformed(msg.Id, "", msg.Timestamp)) return } // Check version compatibility if versionCompare(s.ver, minSupportedVersionValue) < 0 { s.ver = 0 s.queueOut(ErrVersionNotSupported(msg.Id, msg.Timestamp)) logs.Warn.Println("s.hello:", "unsupported version", s.sid) return } params = map[string]any{ "ver": currentVersion, "build": store.Store.GetAdapterName() + ":" + buildstamp, "maxMessageSize": globals.maxMessageSize, "maxSubscriberCount": globals.maxSubscriberCount, "minTagLength": minTagLength, "maxTagLength": maxTagLength, "maxTagCount": globals.maxTagCount, "maxFileUploadSize": globals.maxFileUploadSize, "reqCred": globals.validatorClientConfig, "msgDelAge": globals.msgDeleteAge.Seconds(), } if len(globals.iceServers) > 0 { params["iceServers"] = globals.iceServers } if globals.callEstablishmentTimeout > 0 { params["callTimeout"] = globals.callEstablishmentTimeout } if s.proto == GRPC { // gRPC client may need server address to be able to fetch large files over http(s). // TODO: add support for fetching files over gRPC, then remove this parameter. params["servingAt"] = globals.servingAt // Report cluster size. if globals.cluster != nil { params["clusterSize"] = len(globals.cluster.nodes) + 1 } else { params["clusterSize"] = 1 } } // Set ua & platform in the beginning of the session. // Don't change them later. s.userAgent = msg.Hi.UserAgent s.platf = msg.Hi.Platform if s.platf == "" { s.platf = platformFromUA(msg.Hi.UserAgent) } // This is a background session. Start a timer. if msg.Hi.Background { s.bkgTimer.Reset(deferredNotificationsTimeout) } } else if msg.Hi.Version == "" || parseVersion(msg.Hi.Version) == s.ver { // Save changed device ID+Lang or delete earlier specified device ID. // Platform cannot be changed. if !s.uid.IsZero() { var err error if msg.Hi.DeviceID == types.NullValue { // User wants to delete device ID. deviceIDUpdate = true if s.deviceID != "" { err = store.Devices.Delete(s.uid, s.deviceID) } } else if msg.Hi.DeviceID != "" && s.deviceID != msg.Hi.DeviceID { deviceIDUpdate = true err = store.Devices.Update(s.uid, s.deviceID, &types.DeviceDef{ DeviceId: msg.Hi.DeviceID, Platform: s.platf, LastSeen: msg.Timestamp, Lang: msg.Hi.Lang, }) userChannelsSubUnsub(s.uid, msg.Hi.DeviceID, true) } if err != nil { s.queueOut(decodeStoreError(err, msg.Id, msg.Timestamp, nil)) logs.Warn.Println("s.hello:", "device ID", err, s.sid) return } } else { // Session is not authenticated, report an error. Otherwise, // the client may think that the device ID was updated successfully, // but it will not be saved in the database. s.queueOut(ErrAuthRequiredReply(msg, msg.Timestamp)) logs.Warn.Println("s.hello:", "device ID update requires authentication", s.sid) return } } else { // Version cannot be changed mid-session. s.queueOut(ErrCommandOutOfSequence(msg.Id, "", msg.Timestamp)) logs.Warn.Println("s.hello:", "version cannot be changed", s.sid) return } if msg.Hi.DeviceID == types.NullValue { msg.Hi.DeviceID = "" } s.deviceID = msg.Hi.DeviceID s.lang = msg.Hi.Lang // Try to deduce the country from the locale. // Tag may be well-defined even if err != nil. For example, for 'zh_CN_#Hans' // the tag is 'zh-CN' exact but the err is 'tag is not well-formed'. if tag, _ := language.Parse(s.lang); tag != language.Und { if region, conf := tag.Region(); region.IsCountry() && conf >= language.High { s.countryCode = region.String() } } if s.countryCode == "" { if len(s.lang) > 2 { // Logging strings longer than 2 b/c language.Parse(XX) always succeeds // returning confidence Low. logs.Warn.Println("s.hello:", "could not parse locale ", s.lang) } s.countryCode = globals.defaultCountryCode } var httpStatus int var httpStatusText string if s.proto == LPOLL || deviceIDUpdate { // In case of long polling StatusCreated was reported earlier. // In case of deviceID update just report success. httpStatus = http.StatusOK httpStatusText = "ok" } else { httpStatus = http.StatusCreated httpStatusText = "created" } ctrl := &MsgServerCtrl{Id: msg.Id, Code: httpStatus, Text: httpStatusText, Timestamp: msg.Timestamp} if len(params) > 0 { ctrl.Params = params } s.queueOut(&ServerComMessage{Ctrl: ctrl}) } // Account creation func (s *Session) acc(msg *ClientComMessage) { newAcc := strings.HasPrefix(msg.Acc.User, "new") // If temporary auth parameters are provided, get the user ID from them. var rec *auth.Rec if !newAcc && msg.Acc.TmpScheme != "" { if !s.uid.IsZero() { s.queueOut(ErrAlreadyAuthenticated(msg.Acc.Id, "", msg.Timestamp)) logs.Warn.Println("s.acc: got temp auth while already authenticated", s.sid) return } authHdl := store.Store.GetLogicalAuthHandler(msg.Acc.TmpScheme) if authHdl == nil { logs.Warn.Println("s.acc: unknown authentication scheme", msg.Acc.TmpScheme, s.sid) s.queueOut(ErrAuthUnknownScheme(msg.Id, "", msg.Timestamp)) } var err error rec, _, err = authHdl.Authenticate(msg.Acc.TmpSecret, s.remoteAddr) if err != nil { s.queueOut(decodeStoreError(err, msg.Acc.Id, msg.Timestamp, map[string]any{"what": "auth"})) logs.Warn.Println("s.acc: invalid temp auth", err, s.sid) return } } if newAcc { // New account replyCreateUser(s, msg, rec) } else { // Existing account. replyUpdateUser(s, msg, rec) } } // Authenticate func (s *Session) login(msg *ClientComMessage) { // msg.from is ignored here if msg.Login.Scheme == "reset" { if err := s.authSecretReset(msg.Login.Secret); err != nil { s.queueOut(decodeStoreError(err, msg.Id, msg.Timestamp, nil)) } else { s.queueOut(InfoAuthReset(msg.Id, msg.Timestamp)) } return } if !s.uid.IsZero() { // TODO: change error to notice InfoNoChange and return current user ID & auth level // params := map[string]interface{}{"user": s.uid.UserId(), "authlvl": s.authLevel.String()} s.queueOut(ErrAlreadyAuthenticated(msg.Id, "", msg.Timestamp)) return } handler := store.Store.GetLogicalAuthHandler(msg.Login.Scheme) if handler == nil { logs.Warn.Println("s.login: unknown authentication scheme", msg.Login.Scheme, s.sid) s.queueOut(ErrAuthUnknownScheme(msg.Id, "", msg.Timestamp)) return } rec, challenge, err := handler.Authenticate(msg.Login.Secret, s.remoteAddr) if err != nil { resp := decodeStoreError(err, msg.Id, msg.Timestamp, nil) if resp.Ctrl.Code >= 500 { // Log internal errors logs.Warn.Println("s.login: internal", err, s.sid) } s.queueOut(resp) return } // If authenticator did not check user state, it returns state "undef". If so, check user state here. if rec.State == types.StateUndefined { rec.State, err = userGetState(rec.Uid) } if err == nil && rec.State != types.StateOK { err = types.ErrPermissionDenied } if err != nil { logs.Warn.Println("s.login: user state check failed", rec.Uid, err, s.sid) s.queueOut(decodeStoreError(err, msg.Id, msg.Timestamp, nil)) return } if challenge != nil { // Multi-stage authentication. Issue challenge to the client. s.queueOut(InfoChallenge(msg.Id, msg.Timestamp, challenge)) return } var missing []string if rec.Features&auth.FeatureValidated == 0 && len(globals.authValidators[rec.AuthLevel]) > 0 { var validated []string // Check responses. Ignore invalid responses, just keep cred unvalidated. if validated, _, err = validatedCreds(rec.Uid, rec.AuthLevel, msg.Login.Cred, false); err == nil { // Get a list of credentials which have not been validated. _, missing, _ = stringSliceDelta(globals.authValidators[rec.AuthLevel], validated) } } if err != nil { logs.Warn.Println("s.login: failed to validate credentials:", err, s.sid) s.queueOut(decodeStoreError(err, msg.Id, msg.Timestamp, nil)) } else { s.queueOut(s.onLogin(msg.Id, msg.Timestamp, rec, missing)) } } // authSecretReset resets an authentication secret; // params: "auth-method-to-reset:credential-method:credential-value", // for example: "basic:email:alice@example.com". func (s *Session) authSecretReset(params []byte) error { var authScheme, credMethod, credValue string if parts := strings.Split(string(params), ":"); len(parts) >= 3 { authScheme, credMethod, credValue = parts[0], parts[1], parts[2] } else { return types.ErrMalformed } // Technically we don't need to check it here, but we are going to mail the 'authScheme' string to the user. // We have to make sure it does not contain any exploits. This is the simplest check. auther := store.Store.GetLogicalAuthHandler(authScheme) if auther == nil { return types.ErrUnsupported } validator := store.Store.GetValidator(credMethod) if validator == nil { return types.ErrUnsupported } uid, err := store.Users.GetByCred(credMethod, credValue) if err != nil { return err } if uid.IsZero() { // Prevent discovery of existing contacts: report "no error" if contact is not found. return nil } resetParams, err := auther.GetResetParams(uid) if err != nil { return err } tempScheme, err := validator.TempAuthScheme() if err != nil { return err } tempAuth := store.Store.GetLogicalAuthHandler(tempScheme) if tempAuth == nil || !tempAuth.IsInitialized() { logs.Err.Println("s.authSecretReset: validator with missing temp auth", credMethod, tempScheme, s.sid) return types.ErrInternal } code, _, err := tempAuth.GenSecret(&auth.Rec{ Uid: uid, AuthLevel: auth.LevelAuth, Features: auth.FeatureNoLogin, Credential: credMethod + ":" + credValue, }) if err != nil { return err } return validator.ResetSecret(credValue, authScheme, s.lang, code, resetParams) } // onLogin performs steps after successful authentication. func (s *Session) onLogin(msgID string, timestamp time.Time, rec *auth.Rec, missing []string) *ServerComMessage { var reply *ServerComMessage var params map[string]any features := rec.Features params = map[string]any{ "user": rec.Uid.UserId(), "authlvl": rec.AuthLevel.String(), } if len(missing) > 0 { // Some credentials are not validated yet. Respond with request for validation. reply = InfoValidateCredentials(msgID, timestamp) params["cred"] = missing } else { // Everything is fine, authenticate the session. reply = NoErr(msgID, "", timestamp) // Check if the token is suitable for session authentication. if features&auth.FeatureNoLogin == 0 { // Authenticate the session. s.uid = rec.Uid s.authLvl = rec.AuthLevel // Reset expiration time. rec.Lifetime = 0 } features |= auth.FeatureValidated // Record deviceId used in this session if s.deviceID != "" { if err := store.Devices.Update(rec.Uid, "", &types.DeviceDef{ DeviceId: s.deviceID, Platform: s.platf, LastSeen: timestamp, Lang: s.lang, }); err != nil { logs.Warn.Println("failed to update device record", err) } } } // GenSecret fails only if tokenLifetime is < 0. It can't be < 0 here, // otherwise login would have failed earlier. rec.Features = features params["token"], params["expires"], _ = store.Store.GetLogicalAuthHandler("token").GenSecret(rec) reply.Ctrl.Params = params return reply } func (s *Session) get(msg *ClientComMessage) { // Expand topic name. var resp *ServerComMessage msg.RcptTo, resp = s.expandTopicName(msg) if resp != nil { s.queueOut(resp) return } msg.MetaWhat = parseMsgClientMeta(msg.Get.What) sub := s.getSub(msg.RcptTo) if msg.MetaWhat == 0 { s.queueOut(ErrMalformedReply(msg, msg.Timestamp)) logs.Warn.Println("s.get: invalid Get message action", msg.Get.What) } else if sub != nil { select { case sub.meta <- msg: default: // Reply with a 503 to the user. s.queueOut(ErrServiceUnavailableReply(msg, msg.Timestamp)) logs.Err.Println("s.get: sub.meta channel full, topic ", msg.RcptTo, s.sid) } } else if msg.MetaWhat&(constMsgMetaDesc|constMsgMetaSub) != 0 { // Request some minimal info from a topic not currently attached to. select { case globals.hub.meta <- msg: default: // Reply with a 503 to the user. s.queueOut(ErrServiceUnavailableReply(msg, msg.Timestamp)) logs.Err.Println("s.get: hub.meta channel full", s.sid) } } else { logs.Warn.Println("s.get: subscribe first to get=", msg.Get.What) s.queueOut(ErrPermissionDeniedReply(msg, msg.Timestamp)) } } func (s *Session) set(msg *ClientComMessage) { // Expand topic name. var resp *ServerComMessage msg.RcptTo, resp = s.expandTopicName(msg) if resp != nil { s.queueOut(resp) return } if msg.Set.Desc != nil { msg.MetaWhat = constMsgMetaDesc } if msg.Set.Sub != nil { msg.MetaWhat |= constMsgMetaSub } if msg.Set.Tags != nil { msg.MetaWhat |= constMsgMetaTags } if msg.Set.Cred != nil { msg.MetaWhat |= constMsgMetaCred } if msg.Set.Aux != nil { msg.MetaWhat |= constMsgMetaAux } if msg.MetaWhat == 0 { s.queueOut(ErrMalformedReply(msg, msg.Timestamp)) logs.Warn.Println("s.set: nil Set action") } else if sub := s.getSub(msg.RcptTo); sub != nil { select { case sub.meta <- msg: default: // Reply with a 503 to the user. s.queueOut(ErrServiceUnavailableReply(msg, msg.Timestamp)) logs.Err.Println("s.set: sub.meta channel full, topic ", msg.RcptTo, s.sid) } } else if msg.MetaWhat&(constMsgMetaTags|constMsgMetaCred|constMsgMetaAux) != 0 { logs.Warn.Println("s.set: setting tags/creds/aux is allowed for subscribed topics only", msg.MetaWhat) s.queueOut(ErrPermissionDeniedReply(msg, msg.Timestamp)) } else { // Desc.Private and Sub updates are possible without the subscription. select { case globals.hub.meta <- msg: default: // Reply with a 503 to the user. s.queueOut(ErrServiceUnavailableReply(msg, msg.Timestamp)) logs.Err.Println("s.set: hub.meta channel full", s.sid) } } } func (s *Session) del(msg *ClientComMessage) { msg.MetaWhat = parseMsgClientDel(msg.Del.What) // Delete user if msg.MetaWhat == constMsgDelUser { replyDelUser(s, msg) return } // Delete something other than user: topic, subscription, message(s) // Expand topic name and validate request. var resp *ServerComMessage msg.RcptTo, resp = s.expandTopicName(msg) if resp != nil { s.queueOut(resp) return } if msg.MetaWhat == 0 { s.queueOut(ErrMalformedReply(msg, msg.Timestamp)) logs.Warn.Println("s.del: invalid Del action", msg.Del.What, s.sid) return } if msg.MetaWhat == constMsgDelTopic { // Deleting topic: for sessions attached or not attached, send request to hub first. // Hub will forward to topic, if appropriate. select { case globals.hub.unreg <- &topicUnreg{ rcptTo: msg.RcptTo, pkt: msg, sess: s, del: true, }: default: // Reply with a 503 to the user. s.queueOut(ErrServiceUnavailableReply(msg, msg.Timestamp)) logs.Err.Println("s.del: hub.unreg channel full", s.sid) } } else if sub := s.getSub(msg.RcptTo); sub != nil { // Session is attached, deleting subscription or messages. Send to topic. select { case sub.meta <- msg: default: // Reply with a 503 to the user. s.queueOut(ErrServiceUnavailableReply(msg, msg.Timestamp)) logs.Err.Println("s.del: sub.meta channel full, topic ", msg.RcptTo, s.sid) } } else { // Must join the topic to delete messages or subscriptions. s.queueOut(ErrAttachFirst(msg, msg.Timestamp)) logs.Warn.Println("s.del: invalid Del action while unsubbed", msg.Del.What, s.sid) } } // Broadcast a transient message to active topic subscribers. // Not reporting any errors. func (s *Session) note(msg *ClientComMessage) { if s.ver == 0 || msg.AsUser == "" { // Silently ignore the message: have not received {hi} or don't know who sent the message. return } // Expand topic name and validate request. var resp *ServerComMessage msg.RcptTo, resp = s.expandTopicName(msg) if resp != nil { // Silently ignoring the message return } switch msg.Note.What { case "data": if msg.Note.Payload == nil { // Payload must be present in 'data' notifications. return } case "kp", "kpa", "kpv": if msg.Note.SeqId != 0 { return } case "call": if types.GetTopicCat(msg.RcptTo) != types.TopicCatP2P { // Calls are only available in P2P topics. return } fallthrough case "read", "recv": if msg.Note.SeqId <= 0 { return } default: return } if sub := s.getSub(msg.RcptTo); sub != nil { // Pings can be sent to subscribed topics only select { case sub.broadcast <- msg: default: // Reply with a 503 to the user. s.queueOut(ErrServiceUnavailableReply(msg, msg.Timestamp)) logs.Err.Println("s.note: sub.broacast channel full, topic ", msg.RcptTo, s.sid) } } else if msg.Note.What == "recv" || (msg.Note.What == "call" && (msg.Note.Event == "ringing" || msg.Note.Event == "hang-up" || msg.Note.Event == "accept")) { // One of the following events happened: // 1. Client received a pres notification about a new message, initiated a fetch // from the server (and detached from the topic) and acknowledges receipt. // 2. Client is either accepting or terminating the current video call or // letting the initiator of the call know that it is ringing/notifying // the user about the call. // // Hub will forward to topic, if appropriate. select { case globals.hub.routeCli <- msg: default: // Reply with a 503 to the user. s.queueOut(ErrServiceUnavailableReply(msg, msg.Timestamp)) logs.Err.Println("s.note: hub.route channel full", s.sid) } } else { s.queueOut(ErrAttachFirst(msg, msg.Timestamp)) logs.Warn.Println("s.note: note to invalid topic - must subscribe first", msg.Note.What, s.sid) } } // expandTopicName expands session specific topic name to global name // Returns // // topic: session-specific topic name the message recipient should see // routeTo: routable global topic name // err: *ServerComMessage with an error to return to the sender func (s *Session) expandTopicName(msg *ClientComMessage) (string, *ServerComMessage) { if msg.Original == "" { logs.Warn.Println("s.etn: empty topic name", s.sid) return "", ErrMalformed(msg.Id, "", msg.Timestamp) } // Expanded name of the topic to route to i.e. rcptto: or s.subs[routeTo] var routeTo string if msg.Original == "me" { routeTo = msg.AsUser } else if msg.Original == "fnd" { routeTo = types.ParseUserId(msg.AsUser).FndName() } else if msg.Original == "slf" { routeTo = types.ParseUserId(msg.AsUser).SlfName() } else if strings.HasPrefix(msg.Original, "usr") { // p2p topic uid1 := types.ParseUserId(msg.AsUser) uid2 := types.ParseUserId(msg.Original) if uid2.IsZero() { // Ensure the user id is valid. logs.Warn.Println("s.etn: failed to parse p2p topic name", s.sid) return "", ErrMalformed(msg.Id, msg.Original, msg.Timestamp) } else if uid2 == uid1 { // Use 'me' to access self-topic. logs.Warn.Println("s.etn: invalid p2p self-subscription", s.sid) return "", ErrPermissionDeniedReply(msg, msg.Timestamp) } routeTo = uid1.P2PName(uid2) } else if tmp := types.ChnToGrp(msg.Original); tmp != "" { routeTo = tmp } else { routeTo = msg.Original } return routeTo, nil } func (s *Session) serializeAndUpdateStats(msg *ServerComMessage) any { dataSize, data := s.serialize(msg) if dataSize >= 0 { statsAddHistSample("OutgoingMessageSize", float64(dataSize)) } return data } func (s *Session) serialize(msg *ServerComMessage) (int, any) { if s.proto == GRPC { msg := pbServSerialize(msg) // TODO: calculate and return the size of `msg`. return -1, msg } if s.isMultiplex() { // No need to serialize the message to bytes within the cluster. return -1, msg } out, _ := json.Marshal(msg) return len(out), out } // onBackgroundTimer marks background session as foreground and informs topics it's subscribed to. func (s *Session) onBackgroundTimer() { s.subsLock.RLock() defer s.subsLock.RUnlock() update := &sessionUpdate{sess: s} for _, sub := range s.subs { if sub.supd != nil { sub.supd <- update } } } ================================================ FILE: server/session_test.go ================================================ package main import ( "net/http" "sync" "testing" "time" "github.com/golang/mock/gomock" "github.com/tinode/chat/server/auth" "github.com/tinode/chat/server/auth/mock_auth" "github.com/tinode/chat/server/store" "github.com/tinode/chat/server/store/mock_store" "github.com/tinode/chat/server/store/types" ) func test_makeSession(uid types.Uid) *Session { return &Session{ send: make(chan any, 10), uid: uid, authLvl: auth.LevelAuth, inflightReqs: newBoundedWaitGroup(1), ver: 22, } } func TestDispatchHello(t *testing.T) { s := &Session{ send: make(chan any, 10), uid: types.Uid(1), authLvl: auth.LevelAuth, } wg := sync.WaitGroup{} r := responses{} wg.Add(1) go s.testWriteLoop(&r, &wg) msg := &ClientComMessage{ Hi: &MsgClientHi{ Id: "123", Version: "1", UserAgent: "test-ua", Lang: "en-GB", }, } s.dispatch(msg) close(s.send) wg.Wait() if len(r.messages) != 1 { t.Errorf("responses: expected 1, received %d.", len(r.messages)) } resp := r.messages[0].(*ServerComMessage) if resp == nil { t.Fatal("Response must be ServerComMessage") } if resp.Ctrl != nil { if resp.Ctrl.Code != 201 { t.Errorf("Response code: expected 201, got %d", resp.Ctrl.Code) } if resp.Ctrl.Params == nil { t.Error("Response is expected to contain params dict.") } } else { t.Error("Response must contain a ctrl message.") } if s.lang != "en-GB" { t.Errorf("Session language expected to be 'en-GB' vs '%s'", s.lang) } if s.userAgent != "test-ua" { t.Errorf("Session UA expected to be 'test-ua' vs '%s'", s.userAgent) } if s.countryCode != "GB" { t.Errorf("Country code expected to be 'GB' vs '%s'", s.countryCode) } if s.ver == 0 { t.Errorf("s.ver expected 0 vs found %d", s.ver) } } func verifyResponseCodes(r *responses, codes []int, t *testing.T) { if len(r.messages) != len(codes) { t.Errorf("responses: expected %d, received %d.", len(codes), len(r.messages)) } for i := range codes { resp := r.messages[i].(*ServerComMessage) if resp == nil { t.Fatalf("Response %d must be ServerComMessage", i) } if resp.Ctrl == nil { t.Fatalf("Response %d must contain a ctrl message.", i) } if resp.Ctrl.Code != codes[i] { t.Errorf("Response code: expected %d, got %d", codes[i], resp.Ctrl.Code) } } } func TestDispatchInvalidVersion(t *testing.T) { s := &Session{ send: make(chan any, 10), uid: types.Uid(1), authLvl: auth.LevelAuth, } wg := sync.WaitGroup{} r := responses{} wg.Add(1) go s.testWriteLoop(&r, &wg) msg := &ClientComMessage{ Hi: &MsgClientHi{ Id: "123", // Invalid version string. Version: "INVALID VERSION STRING", }, } s.dispatch(msg) close(s.send) wg.Wait() verifyResponseCodes(&r, []int{http.StatusBadRequest}, t) } func TestDispatchUnsupportedVersion(t *testing.T) { s := &Session{ send: make(chan any, 10), uid: types.Uid(1), authLvl: auth.LevelAuth, } wg := sync.WaitGroup{} r := responses{} wg.Add(1) go s.testWriteLoop(&r, &wg) msg := &ClientComMessage{ Hi: &MsgClientHi{ Id: "123", // Invalid version string. Version: "0.1", }, } s.dispatch(msg) close(s.send) wg.Wait() verifyResponseCodes(&r, []int{http.StatusHTTPVersionNotSupported}, t) } func TestDispatchLogin(t *testing.T) { ctrl := gomock.NewController(t) ss := mock_store.NewMockPersistentStorageInterface(ctrl) aa := mock_auth.NewMockAuthHandler(ctrl) uid := types.Uid(1) store.Store = ss defer func() { store.Store = nil ctrl.Finish() }() secret := "<==auth-secret==>" authRec := &auth.Rec{ Uid: uid, AuthLevel: auth.LevelAuth, Tags: []string{"tag1", "tag2"}, State: types.StateOK, } ss.EXPECT().GetLogicalAuthHandler("basic").Return(aa) aa.EXPECT().Authenticate([]byte(secret), gomock.Any()).Return(authRec, nil, nil) // Token generation. ss.EXPECT().GetLogicalAuthHandler("token").Return(aa) token := "<==auth-token==>" expires, _ := time.Parse(time.RFC822, "01 Jan 50 00:00 UTC") aa.EXPECT().GenSecret(authRec).Return([]byte(token), expires, nil) s := &Session{ send: make(chan any, 10), authLvl: auth.LevelAuth, ver: 16, } wg := sync.WaitGroup{} r := responses{} wg.Add(1) go s.testWriteLoop(&r, &wg) msg := &ClientComMessage{ Login: &MsgClientLogin{ Id: "123", Scheme: "basic", Secret: []byte(secret), }, } s.dispatch(msg) close(s.send) wg.Wait() if len(r.messages) != 1 { t.Errorf("responses: expected 1, received %d.", len(r.messages)) } resp := r.messages[0].(*ServerComMessage) if resp == nil { t.Fatal("Response must be ServerComMessage") } if resp.Ctrl != nil { if resp.Ctrl.Id != "123" { t.Errorf("Response id: expected '123', found '%s'", resp.Ctrl.Id) } if resp.Ctrl.Code != 200 { t.Errorf("Response code: expected 200, got %d", resp.Ctrl.Code) } if resp.Ctrl.Params == nil { t.Error("Response is expected to contain params dict.") } p := resp.Ctrl.Params.(map[string]any) if authToken := string(p["token"].([]byte)); authToken != token { t.Errorf("Auth token: expected '%s', found '%s'.", token, authToken) } if exp := p["expires"].(time.Time); exp != expires { t.Errorf("Token expiration: expected '%s', found '%s'.", expires, exp) } } else { t.Error("Response must contain a ctrl message.") } } func TestDispatchSubscribe(t *testing.T) { uid := types.Uid(1) s := test_makeSession(uid) wg := sync.WaitGroup{} r := responses{} wg.Add(1) go s.testWriteLoop(&r, &wg) hub := &Hub{ join: make(chan *ClientComMessage, 10), } globals.hub = hub defer func() { globals.hub = nil }() msg := &ClientComMessage{ Sub: &MsgClientSub{ Id: "123", Topic: "me", Get: &MsgGetQuery{ What: "sub desc tags cred", }, }, } s.dispatch(msg) close(s.send) wg.Wait() // Check we've routed the join request via the hub. if len(r.messages) != 0 { t.Errorf("responses: expected 0, received %d.", len(r.messages)) } if len(hub.join) == 1 { join := <-hub.join if join.sess != s { t.Error("Hub.join request: sess field expected to be the session under test.") } if join != msg { t.Error("Hub.join request: subscribe message expected to be the original subscribe message.") } } else { t.Errorf("Hub join messages: expected 1, received %d.", len(hub.join)) } s.inflightReqs.Done() } func TestDispatchAlreadySubscribed(t *testing.T) { uid := types.Uid(1) s := test_makeSession(uid) wg := sync.WaitGroup{} r := responses{} wg.Add(1) go s.testWriteLoop(&r, &wg) msg := &ClientComMessage{ Sub: &MsgClientSub{ Id: "123", Topic: "me", Get: &MsgGetQuery{ What: "sub desc tags cred", }, }, } // Pretend the session's already subscribed to topic 'me'. s.subs = make(map[string]*Subscription) s.subs[uid.UserId()] = &Subscription{} s.dispatch(msg) close(s.send) wg.Wait() verifyResponseCodes(&r, []int{http.StatusNotModified}, t) } func TestDispatchSubscribeJoinChannelFull(t *testing.T) { uid := types.Uid(1) s := test_makeSession(uid) wg := sync.WaitGroup{} r := responses{} wg.Add(1) go s.testWriteLoop(&r, &wg) hub := &Hub{ // Make it unbuffered with no readers - so emit operation fails immediately. join: make(chan *ClientComMessage), } globals.hub = hub defer func() { globals.hub = nil }() msg := &ClientComMessage{ Sub: &MsgClientSub{ Id: "123", Topic: "me", Get: &MsgGetQuery{ What: "sub desc tags cred", }, }, } s.dispatch(msg) close(s.send) wg.Wait() verifyResponseCodes(&r, []int{http.StatusServiceUnavailable}, t) } func TestDispatchLeave(t *testing.T) { uid := types.Uid(1) s := test_makeSession(uid) wg := sync.WaitGroup{} r := responses{} wg.Add(1) go s.testWriteLoop(&r, &wg) destUid := types.Uid(2) topicName := uid.P2PName(destUid) leave := make(chan *ClientComMessage, 1) s.subs = make(map[string]*Subscription) s.subs[topicName] = &Subscription{ done: leave, } msg := &ClientComMessage{ Leave: &MsgClientLeave{ Id: "123", Topic: destUid.UserId(), }, } s.dispatch(msg) close(s.send) wg.Wait() // Check we've routed the join request via the leave channel. if len(r.messages) != 0 { t.Errorf("responses: expected 0, received %d.", len(r.messages)) } if len(leave) == 1 { req := <-leave if req.sess != s { t.Error("Leave request: sess field expected to be the session under test.") } if req != msg { t.Error("Leave request: leave message expected to be the original leave message.") } // leave request handler is expected to clean up subs. s.delSub(topicName) } else { t.Errorf("Unsub messages: expected 1, received %d.", len(leave)) } if len(s.subs) != 0 { t.Errorf("Session subs: expected to be empty, actual size: %d", len(s.subs)) } s.inflightReqs.Done() } func TestDispatchLeaveUnsubMe(t *testing.T) { uid := types.Uid(1) s := test_makeSession(uid) wg := sync.WaitGroup{} r := responses{} wg.Add(1) go s.testWriteLoop(&r, &wg) s.subs = make(map[string]*Subscription) s.subs[uid.UserId()] = &Subscription{} msg := &ClientComMessage{ Leave: &MsgClientLeave{ Id: "123", // Cannot unsubscribe from 'me'. Topic: "me", Unsub: true, }, } s.dispatch(msg) close(s.send) wg.Wait() verifyResponseCodes(&r, []int{http.StatusForbidden}, t) } func TestDispatchLeaveUnknownTopic(t *testing.T) { uid := types.Uid(1) s := test_makeSession(uid) wg := sync.WaitGroup{} r := responses{} wg.Add(1) go s.testWriteLoop(&r, &wg) // Session isn't subscribed to topic 'me'. // And wants to leave it => no change. s.subs = make(map[string]*Subscription) msg := &ClientComMessage{ Leave: &MsgClientLeave{ Id: "123", Topic: "me", }, } s.dispatch(msg) close(s.send) wg.Wait() verifyResponseCodes(&r, []int{http.StatusNotModified}, t) } func TestDispatchLeaveUnsubFromUnknownTopic(t *testing.T) { uid := types.Uid(1) s := test_makeSession(uid) wg := sync.WaitGroup{} r := responses{} wg.Add(1) go s.testWriteLoop(&r, &wg) // Session isn't subscribed to topic 'me'. // And wants to leave & unsubscribe from it. s.subs = make(map[string]*Subscription) msg := &ClientComMessage{ Leave: &MsgClientLeave{ Id: "123", Topic: "me", Unsub: true, }, } s.dispatch(msg) close(s.send) wg.Wait() verifyResponseCodes(&r, []int{http.StatusConflict}, t) } func TestDispatchPublish(t *testing.T) { uid := types.Uid(1) s := test_makeSession(uid) wg := sync.WaitGroup{} r := responses{} wg.Add(1) go s.testWriteLoop(&r, &wg) destUid := types.Uid(2) topicName := uid.P2PName(destUid) brdcst := make(chan *ClientComMessage, 1) s.subs = make(map[string]*Subscription) s.subs[topicName] = &Subscription{ broadcast: brdcst, } testMessage := "test content" msg := &ClientComMessage{ Pub: &MsgClientPub{ Id: "123", Topic: destUid.UserId(), Content: testMessage, }, } s.dispatch(msg) close(s.send) wg.Wait() // Check we've routed the join request via the broadcast channel. if len(r.messages) != 0 { t.Errorf("responses: expected 0, received %d.", len(r.messages)) } if len(brdcst) == 1 { req := <-brdcst if req.sess != s { t.Error("Pub request: sess field expected to be the session under test.") } if req.Pub.Content != testMessage { t.Errorf("Pub request content: expected '%s' vs '%s'.", testMessage, req.Pub.Content) } if req.Pub.Topic != destUid.UserId() { t.Errorf("Pub request topic: expected '%s' vs '%s'.", destUid.UserId(), req.Pub.Topic) } } else { t.Errorf("Pub messages: expected 1, received %d.", len(brdcst)) } } func TestDispatchPublishBroadcastChannelFull(t *testing.T) { uid := types.Uid(1) s := test_makeSession(uid) wg := sync.WaitGroup{} r := responses{} wg.Add(1) go s.testWriteLoop(&r, &wg) destUid := types.Uid(2) topicName := uid.P2PName(destUid) // Make broadcast channel unbuffered with no reader - // emit op will fail. brdcst := make(chan *ClientComMessage) s.subs = make(map[string]*Subscription) s.subs[topicName] = &Subscription{ broadcast: brdcst, } testMessage := "test content" msg := &ClientComMessage{ Pub: &MsgClientPub{ Id: "123", Topic: destUid.UserId(), Content: testMessage, }, } s.dispatch(msg) close(s.send) wg.Wait() verifyResponseCodes(&r, []int{http.StatusServiceUnavailable}, t) } func TestDispatchPublishMissingSubcription(t *testing.T) { uid := types.Uid(1) s := test_makeSession(uid) wg := sync.WaitGroup{} r := responses{} wg.Add(1) go s.testWriteLoop(&r, &wg) destUid := types.Uid(2) // Subscription to topic missing. s.subs = make(map[string]*Subscription) testMessage := "test content" msg := &ClientComMessage{ Pub: &MsgClientPub{ Id: "123", Topic: destUid.UserId(), Content: testMessage, }, } s.dispatch(msg) close(s.send) wg.Wait() verifyResponseCodes(&r, []int{http.StatusConflict}, t) } func TestDispatchGet(t *testing.T) { uid := types.Uid(1) s := test_makeSession(uid) wg := sync.WaitGroup{} r := responses{} wg.Add(1) go s.testWriteLoop(&r, &wg) destUid := types.Uid(2) topicName := uid.P2PName(destUid) meta := make(chan *ClientComMessage, 1) s.subs = make(map[string]*Subscription) s.subs[topicName] = &Subscription{ meta: meta, } msg := &ClientComMessage{ Get: &MsgClientGet{ Id: "123", Topic: destUid.UserId(), MsgGetQuery: MsgGetQuery{ What: "desc sub del cred", }, }, } s.dispatch(msg) close(s.send) wg.Wait() // Check we've routed the join request via the meta channel. if len(r.messages) != 0 { t.Errorf("responses: expected 0, received %d.", len(r.messages)) } if len(meta) == 1 { req := <-meta if req.sess != s { t.Error("Get request: sess field expected to be the session under test.") } } else { t.Errorf("Get messages: expected 1, received %d.", len(meta)) } } func TestDispatchGetMalformedWhat(t *testing.T) { uid := types.Uid(1) s := test_makeSession(uid) wg := sync.WaitGroup{} r := responses{} wg.Add(1) go s.testWriteLoop(&r, &wg) destUid := types.Uid(2) msg := &ClientComMessage{ Get: &MsgClientGet{ Id: "123", Topic: destUid.UserId(), MsgGetQuery: MsgGetQuery{ // Empty 'what'. This will produce an error. What: "", }, }, } s.dispatch(msg) close(s.send) wg.Wait() verifyResponseCodes(&r, []int{http.StatusBadRequest}, t) } func TestDispatchGetMetaChannelFull(t *testing.T) { uid := types.Uid(1) s := test_makeSession(uid) wg := sync.WaitGroup{} r := responses{} wg.Add(1) go s.testWriteLoop(&r, &wg) destUid := types.Uid(2) topicName := uid.P2PName(destUid) // Unbuffered chan with no readers - emit will fail. meta := make(chan *ClientComMessage) s.subs = make(map[string]*Subscription) s.subs[topicName] = &Subscription{ meta: meta, } msg := &ClientComMessage{ Get: &MsgClientGet{ Id: "123", Topic: destUid.UserId(), MsgGetQuery: MsgGetQuery{ What: "desc sub", }, }, } s.dispatch(msg) close(s.send) wg.Wait() verifyResponseCodes(&r, []int{http.StatusServiceUnavailable}, t) } func TestDispatchSet(t *testing.T) { uid := types.Uid(1) s := test_makeSession(uid) wg := sync.WaitGroup{} r := responses{} wg.Add(1) go s.testWriteLoop(&r, &wg) destUid := types.Uid(2) topicName := uid.P2PName(destUid) meta := make(chan *ClientComMessage, 1) s.subs = make(map[string]*Subscription) s.subs[topicName] = &Subscription{ meta: meta, } msg := &ClientComMessage{ Set: &MsgClientSet{ Id: "123", Topic: destUid.UserId(), MsgSetQuery: MsgSetQuery{ Desc: &MsgSetDesc{}, Sub: &MsgSetSub{}, Tags: []string{"abc"}, Cred: &MsgCredClient{}, }, }, } s.dispatch(msg) close(s.send) wg.Wait() // Check we've routed the join request via the meta channel. if len(r.messages) != 0 { t.Errorf("responses: expected 0, received %d.", len(r.messages)) } if len(meta) == 1 { req := <-meta if req.sess != s { t.Error("Set request: sess field expected to be the session under test.") } expectedWhat := constMsgMetaDesc | constMsgMetaSub | constMsgMetaTags | constMsgMetaCred if msg.MetaWhat != expectedWhat { t.Errorf("Set request what: expected %d vs %d", expectedWhat, msg.MetaWhat) } } else { t.Errorf("Set messages: expected 1, received %d.", len(meta)) } } func TestDispatchSetMalformedWhat(t *testing.T) { uid := types.Uid(1) s := test_makeSession(uid) wg := sync.WaitGroup{} r := responses{} wg.Add(1) go s.testWriteLoop(&r, &wg) destUid := types.Uid(2) msg := &ClientComMessage{ Set: &MsgClientSet{ Id: "123", Topic: destUid.UserId(), MsgSetQuery: MsgSetQuery{ // No meta requests. }, }, } s.dispatch(msg) close(s.send) wg.Wait() verifyResponseCodes(&r, []int{http.StatusBadRequest}, t) } func TestDispatchSetMetaChannelFull(t *testing.T) { uid := types.Uid(1) s := test_makeSession(uid) wg := sync.WaitGroup{} r := responses{} wg.Add(1) go s.testWriteLoop(&r, &wg) destUid := types.Uid(2) topicName := uid.P2PName(destUid) // Unbuffered meta channel w/ no readers - emit will fail. meta := make(chan *ClientComMessage) s.subs = make(map[string]*Subscription) s.subs[topicName] = &Subscription{ meta: meta, } msg := &ClientComMessage{ Set: &MsgClientSet{ Id: "123", Topic: destUid.UserId(), MsgSetQuery: MsgSetQuery{ // No meta requests. Desc: &MsgSetDesc{}, Sub: &MsgSetSub{}, Tags: []string{"abc"}, Cred: &MsgCredClient{}, }, }, } s.dispatch(msg) close(s.send) wg.Wait() verifyResponseCodes(&r, []int{http.StatusServiceUnavailable}, t) } func TestDispatchDelMsg(t *testing.T) { uid := types.Uid(1) s := test_makeSession(uid) wg := sync.WaitGroup{} r := responses{} wg.Add(1) go s.testWriteLoop(&r, &wg) destUid := types.Uid(2) topicName := uid.P2PName(destUid) meta := make(chan *ClientComMessage, 1) s.subs = make(map[string]*Subscription) s.subs[topicName] = &Subscription{ meta: meta, } msg := &ClientComMessage{ Del: &MsgClientDel{ Id: "123", Topic: destUid.UserId(), What: "msg", DelSeq: []MsgRange{{LowId: 3, HiId: 4}}, Hard: true, }, } s.dispatch(msg) close(s.send) wg.Wait() // Check we've routed the join request via the meta channel. if len(r.messages) != 0 { t.Errorf("responses: expected 0, received %d.", len(r.messages)) } if len(meta) == 1 { req := <-meta if req.sess != s { t.Error("Del request: sess field expected to be the session under test.") } } else { t.Errorf("Del messages: expected 1, received %d.", len(meta)) } } func TestDispatchDelMalformedWhat(t *testing.T) { uid := types.Uid(1) s := test_makeSession(uid) wg := sync.WaitGroup{} r := responses{} wg.Add(1) go s.testWriteLoop(&r, &wg) destUid := types.Uid(2) msg := &ClientComMessage{ Del: &MsgClientDel{ Id: "123", Topic: destUid.UserId(), // Invalid 'what' - this will produce an error. What: "INVALID", }, } s.dispatch(msg) close(s.send) wg.Wait() verifyResponseCodes(&r, []int{http.StatusBadRequest}, t) } func TestDispatchDelMetaChanFull(t *testing.T) { uid := types.Uid(1) s := test_makeSession(uid) wg := sync.WaitGroup{} r := responses{} wg.Add(1) go s.testWriteLoop(&r, &wg) destUid := types.Uid(2) topicName := uid.P2PName(destUid) // Unbuffered chan - to simulate a full buffered chan. meta := make(chan *ClientComMessage) s.subs = make(map[string]*Subscription) s.subs[topicName] = &Subscription{ meta: meta, } msg := &ClientComMessage{ Del: &MsgClientDel{ Id: "123", Topic: destUid.UserId(), What: "msg", DelSeq: []MsgRange{{LowId: 3, HiId: 4}}, Hard: true, }, } s.dispatch(msg) close(s.send) wg.Wait() verifyResponseCodes(&r, []int{http.StatusServiceUnavailable}, t) } func TestDispatchDelUnsubscribedSession(t *testing.T) { uid := types.Uid(1) s := test_makeSession(uid) wg := sync.WaitGroup{} r := responses{} wg.Add(1) go s.testWriteLoop(&r, &wg) destUid := types.Uid(2) // Session isn't subscribed. s.subs = make(map[string]*Subscription) msg := &ClientComMessage{ Del: &MsgClientDel{ Id: "123", Topic: destUid.UserId(), What: "msg", DelSeq: []MsgRange{{LowId: 3, HiId: 4}}, Hard: true, }, } s.dispatch(msg) close(s.send) wg.Wait() verifyResponseCodes(&r, []int{http.StatusConflict}, t) } func TestDispatchNote(t *testing.T) { uid := types.Uid(1) s := test_makeSession(uid) wg := sync.WaitGroup{} r := responses{} wg.Add(1) go s.testWriteLoop(&r, &wg) destUid := types.Uid(2) topicName := uid.P2PName(destUid) brdcst := make(chan *ClientComMessage, 1) s.subs = make(map[string]*Subscription) s.subs[topicName] = &Subscription{ broadcast: brdcst, } msg := &ClientComMessage{ Note: &MsgClientNote{ Topic: destUid.UserId(), What: "recv", SeqId: 5, }, } s.dispatch(msg) close(s.send) wg.Wait() // Check we've routed the join request via the broadcast channel. if len(r.messages) != 0 { t.Errorf("responses: expected 0, received %d.", len(r.messages)) } if len(brdcst) == 1 { req := <-brdcst if req.sess != s { t.Error("Pub request: sess field expected to be the session under test.") } if req.Note.What != msg.Note.What { t.Errorf("Note request what: expected '%s' vs '%s'.", msg.Note.What, req.Note.What) } if req.Note.SeqId != msg.Note.SeqId { t.Errorf("Note request seqId: expected %d vs %d.", msg.Note.SeqId, req.Note.SeqId) } if req.Note.Topic != destUid.UserId() { t.Errorf("Note request topic: expected '%s' vs '%s'.", destUid.UserId(), req.Note.Topic) } } else { t.Errorf("Note messages: expected 1, received %d.", len(brdcst)) } } func TestDispatchNoteBroadcastChanFull(t *testing.T) { uid := types.Uid(1) s := test_makeSession(uid) wg := sync.WaitGroup{} r := responses{} wg.Add(1) go s.testWriteLoop(&r, &wg) destUid := types.Uid(2) topicName := uid.P2PName(destUid) // Unbuffered chan - to simulate a full buffered chan. brdcst := make(chan *ClientComMessage) s.subs = make(map[string]*Subscription) s.subs[topicName] = &Subscription{ broadcast: brdcst, } msg := &ClientComMessage{ Note: &MsgClientNote{ Topic: destUid.UserId(), What: "recv", SeqId: 5, }, } s.dispatch(msg) close(s.send) wg.Wait() verifyResponseCodes(&r, []int{http.StatusServiceUnavailable}, t) } func TestDispatchNoteOnNonSubscribedTopic(t *testing.T) { uid := types.Uid(1) s := test_makeSession(uid) wg := sync.WaitGroup{} r := responses{} wg.Add(1) go s.testWriteLoop(&r, &wg) destUid := types.Uid(2) s.subs = make(map[string]*Subscription) msg := &ClientComMessage{ Note: &MsgClientNote{ Topic: destUid.UserId(), What: "read", SeqId: 5, }, } s.dispatch(msg) close(s.send) wg.Wait() verifyResponseCodes(&r, []int{http.StatusConflict}, t) } func TestDispatchAccNew(t *testing.T) { ctrl := gomock.NewController(t) ss := mock_store.NewMockPersistentStorageInterface(ctrl) uu := mock_store.NewMockUsersPersistenceInterface(ctrl) aa := mock_auth.NewMockAuthHandler(ctrl) uid := types.Uid(1) store.Store = ss store.Users = uu defer func() { store.Store = nil store.Users = nil ctrl.Finish() }() remoteAddr := "192.168.0.1" secret := "<==auth-secret==>" tags := []string{"tag1", "tag2"} authRec := &auth.Rec{ Uid: uid, AuthLevel: auth.LevelAuth, Tags: tags, State: types.StateOK, } ss.EXPECT().GetLogicalAuthHandler("basic").Return(aa) // This login is available. aa.EXPECT().IsUnique([]byte(secret), remoteAddr).Return(true, nil) uu.EXPECT().Create(gomock.Any(), gomock.Any()).DoAndReturn( func(user *types.User, private any) (*types.User, error) { user.SetUid(uid) return user, nil }) aa.EXPECT().AddRecord(gomock.Any(), []byte(secret), remoteAddr).Return(authRec, nil) // Token generation. ss.EXPECT().GetLogicalAuthHandler("token").Return(aa) token := "<==auth-token==>" aa.EXPECT().GenSecret(gomock.Any()).Return([]byte(token), time.Now(), nil) uu.EXPECT().UpdateTags(uid, tags, nil, nil).Return(tags, nil) s := &Session{ send: make(chan any, 10), authLvl: auth.LevelAuth, ver: 16, remoteAddr: remoteAddr, } wg := sync.WaitGroup{} r := responses{} wg.Add(1) go s.testWriteLoop(&r, &wg) public := "public name" msg := &ClientComMessage{ Acc: &MsgClientAcc{ Id: "123", User: "newXYZ", Scheme: "basic", Secret: []byte(secret), Tags: []string{"abc", "123"}, Desc: &MsgSetDesc{Public: public}, }, } s.dispatch(msg) close(s.send) wg.Wait() if len(r.messages) != 1 { t.Errorf("responses: expected 1, received %d.", len(r.messages)) } resp := r.messages[0].(*ServerComMessage) if resp == nil { t.Fatal("Response must be ServerComMessage") } if resp.Ctrl != nil { if resp.Ctrl.Id != "123" { t.Errorf("Response id: expected '123', found '%s'", resp.Ctrl.Id) } if resp.Ctrl.Code != 201 { t.Errorf("Response code: expected 201, got %d", resp.Ctrl.Code) } if resp.Ctrl.Params == nil { t.Error("Response is expected to contain params dict.") } p := resp.Ctrl.Params.(map[string]any) if respUid := string(p["user"].(string)); respUid != uid.UserId() { t.Errorf("Response uid: expected '%s', found '%s'.", uid.UserId(), respUid) } if lvl := p["authlvl"].(string); lvl != auth.LevelAuth.String() { t.Errorf("Auth level: expected '%s', found '%s'.", auth.LevelAuth.String(), lvl) } if desc := p["desc"].(*MsgTopicDesc); desc.Public.(string) != public { t.Errorf("Public: expected '%s', found '%s'.", public, desc.Public.(string)) } } else { t.Error("Response must contain a ctrl message.") } } func TestDispatchNoMessage(t *testing.T) { remoteAddr := "192.168.0.1" s := &Session{ send: make(chan any, 10), authLvl: auth.LevelAuth, ver: 16, remoteAddr: remoteAddr, } wg := sync.WaitGroup{} r := responses{} wg.Add(1) go s.testWriteLoop(&r, &wg) msg := &ClientComMessage{} s.dispatch(msg) close(s.send) wg.Wait() if len(r.messages) != 1 { t.Errorf("responses: expected 1, received %d.", len(r.messages)) } resp := r.messages[0].(*ServerComMessage) if resp == nil { t.Fatal("Response must be ServerComMessage") } if resp.Ctrl == nil { t.Fatal("Response must contain a ctrl message.") } if resp.Ctrl.Code != 400 { t.Errorf("Response code: expected 400, got %d", resp.Ctrl.Code) } } ================================================ FILE: server/sessionstore.go ================================================ /****************************************************************************** * * Description: * * Session management. * *****************************************************************************/ package main import ( "container/list" "net/http" "sync" "time" "github.com/gorilla/websocket" "github.com/tinode/chat/pbx" "github.com/tinode/chat/server/logs" "github.com/tinode/chat/server/store" "github.com/tinode/chat/server/store/types" ) // WaitGroup with a semaphore functionality // (limiting number of threads/goroutines accessing the guarded resource simultaneously). type boundedWaitGroup struct { wg sync.WaitGroup sem chan struct{} } func newBoundedWaitGroup(capacity int) *boundedWaitGroup { return &boundedWaitGroup{sem: make(chan struct{}, capacity)} } func (w *boundedWaitGroup) Add(delta int) { if delta <= 0 { return } for range delta { w.sem <- struct{}{} } w.wg.Add(delta) } func (w *boundedWaitGroup) Done() { select { case _, ok := <-w.sem: if !ok { logs.Err.Panicln("boundedWaitGroup.sem closed.") } default: logs.Err.Panicln("boundedWaitGroup.Done() called before Add().") } w.wg.Done() } func (w *boundedWaitGroup) Wait() { w.wg.Wait() } // SessionStore holds live sessions. Long polling sessions are stored in a linked list with // most recent sessions on top. In addition all sessions are stored in a map indexed by session ID. type SessionStore struct { lock sync.Mutex // Support for long polling sessions: a list of sessions sorted by last access time. // Needed for cleaning abandoned sessions. lru *list.List lifeTime time.Duration // All sessions indexed by session ID sessCache map[string]*Session } // NewSession creates a new session and saves it to the session store. func (ss *SessionStore) NewSession(conn any, sid string) (*Session, int) { var s Session if sid == "" { s.sid = store.Store.GetUidString() } else { s.sid = sid } ss.lock.Lock() if _, found := ss.sessCache[s.sid]; found { logs.Err.Fatalln("ERROR! duplicate session ID", s.sid) } ss.lock.Unlock() switch c := conn.(type) { case *websocket.Conn: s.proto = WEBSOCK s.ws = c case http.ResponseWriter: s.proto = LPOLL // no need to store c for long polling, it changes with every request case *ClusterNode: s.proto = MULTIPLEX s.clnode = c case pbx.Node_MessageLoopServer: s.proto = GRPC s.grpcnode = c default: logs.Err.Panicln("session: unknown connection type", conn) } s.subs = make(map[string]*Subscription) s.send = make(chan any, sendQueueLimit+32) // buffered s.stop = make(chan any, 1) // Buffered by 1 just to make it non-blocking s.detach = make(chan string, 64) // buffered s.bkgTimer = time.NewTimer(time.Hour) s.bkgTimer.Stop() // Make sure at most 1 request is modifying session/topic state at any time. // TODO: use Mutex & CondVar? s.inflightReqs = newBoundedWaitGroup(1) s.lastTouched = time.Now() ss.lock.Lock() if s.proto == LPOLL { // Only LP sessions need to be sorted by last active s.lpTracker = ss.lru.PushFront(&s) } ss.sessCache[s.sid] = &s // Expire stale long polling sessions: ss.lru contains only long polling sessions. // If ss.lru is empty this is a noop. var expired []*Session expire := s.lastTouched.Add(-ss.lifeTime) for elem := ss.lru.Back(); elem != nil; elem = ss.lru.Back() { sess := elem.Value.(*Session) if sess.lastTouched.Before(expire) { ss.lru.Remove(elem) delete(ss.sessCache, sess.sid) expired = append(expired, sess) } else { break // don't need to traverse further } } numSessions := len(ss.sessCache) statsSet("LiveSessions", int64(numSessions)) statsInc("TotalSessions", 1) ss.lock.Unlock() // Deleting long polling sessions. for _, sess := range expired { // This locks the session. Thus cleaning up outside of the // sessionStore lock. Otherwise deadlock. sess.cleanUp(true) } return &s, numSessions } // Get fetches a session from store by session ID. func (ss *SessionStore) Get(sid string) *Session { ss.lock.Lock() defer ss.lock.Unlock() if sess := ss.sessCache[sid]; sess != nil { if sess.proto == LPOLL { ss.lru.MoveToFront(sess.lpTracker) sess.lastTouched = time.Now() } return sess } return nil } // Delete removes session from store. func (ss *SessionStore) Delete(s *Session) { ss.lock.Lock() defer ss.lock.Unlock() delete(ss.sessCache, s.sid) if s.proto == LPOLL { ss.lru.Remove(s.lpTracker) } statsSet("LiveSessions", int64(len(ss.sessCache))) } // Range calls given function for all sessions. It stops if the function returns false. func (ss *SessionStore) Range(f func(sid string, s *Session) bool) { ss.lock.Lock() for sid, s := range ss.sessCache { if !f(sid, s) { break } } ss.lock.Unlock() } // Shutdown terminates sessionStore. No need to clean up. // Don't send to clustered sessions, their servers are not being shut down. func (ss *SessionStore) Shutdown() { ss.lock.Lock() defer ss.lock.Unlock() shutdown := NoErrShutdown(types.TimeNow()) for _, s := range ss.sessCache { if !s.isMultiplex() { _, data := s.serialize(shutdown) s.stopSession(data) } } // TODO: Consider broadcasting shutdown to other cluster nodes. logs.Info.Println("SessionStore shut down, sessions terminated:", len(ss.sessCache)) } // EvictUser terminates all sessions of a given user. func (ss *SessionStore) EvictUser(uid types.Uid, skipSid string) { ss.lock.Lock() defer ss.lock.Unlock() // FIXME: this probably needs to be optimized. This may take very long time if the node hosts 100000 sessions. evicted := NoErrEvicted("", "", types.TimeNow()) evicted.AsUser = uid.UserId() for _, s := range ss.sessCache { if s.uid == uid && !s.isMultiplex() && s.sid != skipSid { _, data := s.serialize(evicted) s.stopSession(data) delete(ss.sessCache, s.sid) if s.proto == LPOLL { ss.lru.Remove(s.lpTracker) } } } statsSet("LiveSessions", int64(len(ss.sessCache))) } // NodeRestarted removes stale sessions from a restarted cluster node. // - nodeName is the name of affected node // - fingerprint is the new fingerprint of the node. func (ss *SessionStore) NodeRestarted(nodeName string, fingerprint int64) { ss.lock.Lock() defer ss.lock.Unlock() for _, s := range ss.sessCache { if !s.isMultiplex() || s.clnode.name != nodeName { continue } if s.clnode.fingerprint != fingerprint { s.stopSession(nil) delete(ss.sessCache, s.sid) } } statsSet("LiveSessions", int64(len(ss.sessCache))) } // NewSessionStore initializes a session store. func NewSessionStore(lifetime time.Duration) *SessionStore { ss := &SessionStore{ lru: list.New(), lifeTime: lifetime, sessCache: make(map[string]*Session), } statsRegisterInt("LiveSessions") statsRegisterInt("TotalSessions") return ss } ================================================ FILE: server/stats.go ================================================ // Logic related to expvar handling: reporting live stats such as // session and topic counts, memory usage etc. // The stats updates happen in a separate go routine to avoid // locking on main logic routines. package main import ( "encoding/json" "expvar" "net/http" "runtime" "sort" "time" "github.com/tinode/chat/server/logs" "github.com/tinode/chat/server/store" ) // A simple implementation of histogram expvar.Var. // `Bounds` specifies the histogram buckets as follows (length = len(bounds)): // // (-inf, Bounds[i]) for i = 0 // [Bounds[i-1], Bounds[i]) for 0 < i < length // [Bounds[i-1], +inf) for i = length type histogram struct { Count int64 `json:"count"` Sum float64 `json:"sum"` CountPerBucket []int64 `json:"count_per_bucket"` Bounds []float64 `json:"bounds"` } func (h *histogram) addSample(v float64) { h.Count++ h.Sum += v idx := sort.SearchFloat64s(h.Bounds, v) h.CountPerBucket[idx]++ } func (h *histogram) String() string { if r, err := json.Marshal(h); err == nil { return string(r) } return "" } type varUpdate struct { // Name of the variable to update varname string // Value to publish (int, float, etc.) value any // Treat the count as an increment as opposite to the final value. inc bool } // Initialize stats reporting through expvar. func statsInit(mux *http.ServeMux, path string) { if path == "" || path == "-" { return } mux.Handle(path, expvar.Handler()) globals.statsUpdate = make(chan *varUpdate, 1024) start := time.Now() expvar.Publish("Uptime", expvar.Func(func() any { return time.Since(start).Seconds() })) expvar.Publish("NumGoroutines", expvar.Func(func() any { return runtime.NumGoroutine() })) go statsUpdater() logs.Info.Printf("stats: variables exposed at '%s'", path) } func statsRegisterDbStats() { if f := store.Store.DbStats(); f != nil { expvar.Publish("DbStats", expvar.Func(f)) } } // Register integer variable. Don't check for initialization. func statsRegisterInt(name string) { expvar.Publish(name, new(expvar.Int)) } // Register histogram variable. `bounds` specifies histogram buckets/bins // (see comment next to the `histogram` struct definition). func statsRegisterHistogram(name string, bounds []float64) { numBuckets := len(bounds) + 1 expvar.Publish(name, &histogram{ CountPerBucket: make([]int64, numBuckets), Bounds: bounds, }) } // Async publish int variable. func statsSet(name string, val int64) { if globals.statsUpdate != nil { select { case globals.statsUpdate <- &varUpdate{name, val, false}: default: } } } // Async publish an increment (decrement) to int variable. func statsInc(name string, val int) { if globals.statsUpdate != nil { select { case globals.statsUpdate <- &varUpdate{name, int64(val), true}: default: } } } // Async publish a value (add a sample) to a histogram variable. func statsAddHistSample(name string, val float64) { if globals.statsUpdate != nil { select { case globals.statsUpdate <- &varUpdate{varname: name, value: val}: default: } } } // Stop publishing stats. func statsShutdown() { if globals.statsUpdate != nil { globals.statsUpdate <- nil } } // The go routine which actually publishes stats updates. func statsUpdater() { for upd := range globals.statsUpdate { if upd == nil { globals.statsUpdate = nil // Dont' care to close the channel. break } // Handle var update if ev := expvar.Get(upd.varname); ev != nil { switch v := ev.(type) { case *expvar.Int: count := upd.value.(int64) if upd.inc { v.Add(count) } else { v.Set(count) } case *histogram: val := upd.value.(float64) v.addSample(val) default: logs.Err.Panicf("stats: unsupported expvar type %T", ev) } } else { panic("stats: update to unknown variable " + upd.varname) } } logs.Info.Println("stats: shutdown") } ================================================ FILE: server/store/mock_store/mock_store.go ================================================ // Code generated by MockGen. DO NOT EDIT. // Source: store/store.go // Package mock_store is a generated GoMock package. package mock_store import ( json "encoding/json" reflect "reflect" time "time" gomock "github.com/golang/mock/gomock" auth "github.com/tinode/chat/server/auth" adapter "github.com/tinode/chat/server/db" media "github.com/tinode/chat/server/media" types "github.com/tinode/chat/server/store/types" validate "github.com/tinode/chat/server/validate" ) // MockPersistentStorageInterface is a mock of PersistentStorageInterface interface. type MockPersistentStorageInterface struct { ctrl *gomock.Controller recorder *MockPersistentStorageInterfaceMockRecorder } // MockPersistentStorageInterfaceMockRecorder is the mock recorder for MockPersistentStorageInterface. type MockPersistentStorageInterfaceMockRecorder struct { mock *MockPersistentStorageInterface } // NewMockPersistentStorageInterface creates a new mock instance. func NewMockPersistentStorageInterface(ctrl *gomock.Controller) *MockPersistentStorageInterface { mock := &MockPersistentStorageInterface{ctrl: ctrl} mock.recorder = &MockPersistentStorageInterfaceMockRecorder{mock} return mock } // EXPECT returns an object that allows the caller to indicate expected use. func (m *MockPersistentStorageInterface) EXPECT() *MockPersistentStorageInterfaceMockRecorder { return m.recorder } // Close mocks base method. func (m *MockPersistentStorageInterface) Close() error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Close") ret0, _ := ret[0].(error) return ret0 } // Close indicates an expected call of Close. func (mr *MockPersistentStorageInterfaceMockRecorder) Close() *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Close", reflect.TypeOf((*MockPersistentStorageInterface)(nil).Close)) } // DbStats mocks base method. func (m *MockPersistentStorageInterface) DbStats() func() any { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "DbStats") ret0, _ := ret[0].(func() any) return ret0 } // DbStats indicates an expected call of DbStats. func (mr *MockPersistentStorageInterfaceMockRecorder) DbStats() *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DbStats", reflect.TypeOf((*MockPersistentStorageInterface)(nil).DbStats)) } // GetAdapter mocks base method. func (m *MockPersistentStorageInterface) GetAdapter() adapter.Adapter { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetAdapter") ret0, _ := ret[0].(adapter.Adapter) return ret0 } // GetAdapter indicates an expected call of GetAdapter. func (mr *MockPersistentStorageInterfaceMockRecorder) GetAdapter() *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAdapter", reflect.TypeOf((*MockPersistentStorageInterface)(nil).GetAdapter)) } // GetAdapterName mocks base method. func (m *MockPersistentStorageInterface) GetAdapterName() string { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetAdapterName") ret0, _ := ret[0].(string) return ret0 } // GetAdapterName indicates an expected call of GetAdapterName. func (mr *MockPersistentStorageInterfaceMockRecorder) GetAdapterName() *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAdapterName", reflect.TypeOf((*MockPersistentStorageInterface)(nil).GetAdapterName)) } // GetAdapterVersion mocks base method. func (m *MockPersistentStorageInterface) GetAdapterVersion() int { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetAdapterVersion") ret0, _ := ret[0].(int) return ret0 } // GetAdapterVersion indicates an expected call of GetAdapterVersion. func (mr *MockPersistentStorageInterfaceMockRecorder) GetAdapterVersion() *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAdapterVersion", reflect.TypeOf((*MockPersistentStorageInterface)(nil).GetAdapterVersion)) } // GetAuthHandler mocks base method. func (m *MockPersistentStorageInterface) GetAuthHandler(name string) auth.AuthHandler { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetAuthHandler", name) ret0, _ := ret[0].(auth.AuthHandler) return ret0 } // GetAuthHandler indicates an expected call of GetAuthHandler. func (mr *MockPersistentStorageInterfaceMockRecorder) GetAuthHandler(name interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAuthHandler", reflect.TypeOf((*MockPersistentStorageInterface)(nil).GetAuthHandler), name) } // GetAuthNames mocks base method. func (m *MockPersistentStorageInterface) GetAuthNames() []string { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetAuthNames") ret0, _ := ret[0].([]string) return ret0 } // GetAuthNames indicates an expected call of GetAuthNames. func (mr *MockPersistentStorageInterfaceMockRecorder) GetAuthNames() *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAuthNames", reflect.TypeOf((*MockPersistentStorageInterface)(nil).GetAuthNames)) } // GetDbVersion mocks base method. func (m *MockPersistentStorageInterface) GetDbVersion() int { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetDbVersion") ret0, _ := ret[0].(int) return ret0 } // GetDbVersion indicates an expected call of GetDbVersion. func (mr *MockPersistentStorageInterfaceMockRecorder) GetDbVersion() *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetDbVersion", reflect.TypeOf((*MockPersistentStorageInterface)(nil).GetDbVersion)) } // GetLogicalAuthHandler mocks base method. func (m *MockPersistentStorageInterface) GetLogicalAuthHandler(name string) auth.AuthHandler { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetLogicalAuthHandler", name) ret0, _ := ret[0].(auth.AuthHandler) return ret0 } // GetLogicalAuthHandler indicates an expected call of GetLogicalAuthHandler. func (mr *MockPersistentStorageInterfaceMockRecorder) GetLogicalAuthHandler(name interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetLogicalAuthHandler", reflect.TypeOf((*MockPersistentStorageInterface)(nil).GetLogicalAuthHandler), name) } // GetMediaHandler mocks base method. func (m *MockPersistentStorageInterface) GetMediaHandler() media.Handler { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetMediaHandler") ret0, _ := ret[0].(media.Handler) return ret0 } // GetMediaHandler indicates an expected call of GetMediaHandler. func (mr *MockPersistentStorageInterfaceMockRecorder) GetMediaHandler() *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetMediaHandler", reflect.TypeOf((*MockPersistentStorageInterface)(nil).GetMediaHandler)) } // GetUid mocks base method. func (m *MockPersistentStorageInterface) GetUid() types.Uid { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetUid") ret0, _ := ret[0].(types.Uid) return ret0 } // GetUid indicates an expected call of GetUid. func (mr *MockPersistentStorageInterfaceMockRecorder) GetUid() *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetUid", reflect.TypeOf((*MockPersistentStorageInterface)(nil).GetUid)) } // GetUidString mocks base method. func (m *MockPersistentStorageInterface) GetUidString() string { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetUidString") ret0, _ := ret[0].(string) return ret0 } // GetUidString indicates an expected call of GetUidString. func (mr *MockPersistentStorageInterfaceMockRecorder) GetUidString() *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetUidString", reflect.TypeOf((*MockPersistentStorageInterface)(nil).GetUidString)) } // GetValidator mocks base method. func (m *MockPersistentStorageInterface) GetValidator(name string) validate.Validator { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetValidator", name) ret0, _ := ret[0].(validate.Validator) return ret0 } // GetValidator indicates an expected call of GetValidator. func (mr *MockPersistentStorageInterfaceMockRecorder) GetValidator(name interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetValidator", reflect.TypeOf((*MockPersistentStorageInterface)(nil).GetValidator), name) } // InitDb mocks base method. func (m *MockPersistentStorageInterface) InitDb(jsonconf json.RawMessage, reset bool) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "InitDb", jsonconf, reset) ret0, _ := ret[0].(error) return ret0 } // InitDb indicates an expected call of InitDb. func (mr *MockPersistentStorageInterfaceMockRecorder) InitDb(jsonconf, reset interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "InitDb", reflect.TypeOf((*MockPersistentStorageInterface)(nil).InitDb), jsonconf, reset) } // IsOpen mocks base method. func (m *MockPersistentStorageInterface) IsOpen() bool { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "IsOpen") ret0, _ := ret[0].(bool) return ret0 } // IsOpen indicates an expected call of IsOpen. func (mr *MockPersistentStorageInterfaceMockRecorder) IsOpen() *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "IsOpen", reflect.TypeOf((*MockPersistentStorageInterface)(nil).IsOpen)) } // Open mocks base method. func (m *MockPersistentStorageInterface) Open(workerId int, jsonconf json.RawMessage) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Open", workerId, jsonconf) ret0, _ := ret[0].(error) return ret0 } // Open indicates an expected call of Open. func (mr *MockPersistentStorageInterfaceMockRecorder) Open(workerId, jsonconf interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Open", reflect.TypeOf((*MockPersistentStorageInterface)(nil).Open), workerId, jsonconf) } // UpgradeDb mocks base method. func (m *MockPersistentStorageInterface) UpgradeDb(jsonconf json.RawMessage) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "UpgradeDb", jsonconf) ret0, _ := ret[0].(error) return ret0 } // UpgradeDb indicates an expected call of UpgradeDb. func (mr *MockPersistentStorageInterfaceMockRecorder) UpgradeDb(jsonconf interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpgradeDb", reflect.TypeOf((*MockPersistentStorageInterface)(nil).UpgradeDb), jsonconf) } // UseMediaHandler mocks base method. func (m *MockPersistentStorageInterface) UseMediaHandler(name, config string) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "UseMediaHandler", name, config) ret0, _ := ret[0].(error) return ret0 } // UseMediaHandler indicates an expected call of UseMediaHandler. func (mr *MockPersistentStorageInterfaceMockRecorder) UseMediaHandler(name, config interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UseMediaHandler", reflect.TypeOf((*MockPersistentStorageInterface)(nil).UseMediaHandler), name, config) } // MockUsersPersistenceInterface is a mock of UsersPersistenceInterface interface. type MockUsersPersistenceInterface struct { ctrl *gomock.Controller recorder *MockUsersPersistenceInterfaceMockRecorder } // MockUsersPersistenceInterfaceMockRecorder is the mock recorder for MockUsersPersistenceInterface. type MockUsersPersistenceInterfaceMockRecorder struct { mock *MockUsersPersistenceInterface } // NewMockUsersPersistenceInterface creates a new mock instance. func NewMockUsersPersistenceInterface(ctrl *gomock.Controller) *MockUsersPersistenceInterface { mock := &MockUsersPersistenceInterface{ctrl: ctrl} mock.recorder = &MockUsersPersistenceInterfaceMockRecorder{mock} return mock } // EXPECT returns an object that allows the caller to indicate expected use. func (m *MockUsersPersistenceInterface) EXPECT() *MockUsersPersistenceInterfaceMockRecorder { return m.recorder } // AddAuthRecord mocks base method. func (m *MockUsersPersistenceInterface) AddAuthRecord(uid types.Uid, authLvl auth.Level, scheme, unique string, secret []byte, expires time.Time) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "AddAuthRecord", uid, authLvl, scheme, unique, secret, expires) ret0, _ := ret[0].(error) return ret0 } // AddAuthRecord indicates an expected call of AddAuthRecord. func (mr *MockUsersPersistenceInterfaceMockRecorder) AddAuthRecord(uid, authLvl, scheme, unique, secret, expires interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AddAuthRecord", reflect.TypeOf((*MockUsersPersistenceInterface)(nil).AddAuthRecord), uid, authLvl, scheme, unique, secret, expires) } // ConfirmCred mocks base method. func (m *MockUsersPersistenceInterface) ConfirmCred(id types.Uid, method string) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ConfirmCred", id, method) ret0, _ := ret[0].(error) return ret0 } // ConfirmCred indicates an expected call of ConfirmCred. func (mr *MockUsersPersistenceInterfaceMockRecorder) ConfirmCred(id, method interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ConfirmCred", reflect.TypeOf((*MockUsersPersistenceInterface)(nil).ConfirmCred), id, method) } // Create mocks base method. func (m *MockUsersPersistenceInterface) Create(user *types.User, private any) (*types.User, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Create", user, private) ret0, _ := ret[0].(*types.User) ret1, _ := ret[1].(error) return ret0, ret1 } // Create indicates an expected call of Create. func (mr *MockUsersPersistenceInterfaceMockRecorder) Create(user, private interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Create", reflect.TypeOf((*MockUsersPersistenceInterface)(nil).Create), user, private) } // DelAuthRecords mocks base method. func (m *MockUsersPersistenceInterface) DelAuthRecords(uid types.Uid, scheme string) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "DelAuthRecords", uid, scheme) ret0, _ := ret[0].(error) return ret0 } // DelAuthRecords indicates an expected call of DelAuthRecords. func (mr *MockUsersPersistenceInterfaceMockRecorder) DelAuthRecords(uid, scheme interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DelAuthRecords", reflect.TypeOf((*MockUsersPersistenceInterface)(nil).DelAuthRecords), uid, scheme) } // DelCred mocks base method. func (m *MockUsersPersistenceInterface) DelCred(id types.Uid, method, value string) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "DelCred", id, method, value) ret0, _ := ret[0].(error) return ret0 } // DelCred indicates an expected call of DelCred. func (mr *MockUsersPersistenceInterfaceMockRecorder) DelCred(id, method, value interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DelCred", reflect.TypeOf((*MockUsersPersistenceInterface)(nil).DelCred), id, method, value) } // Delete mocks base method. func (m *MockUsersPersistenceInterface) Delete(id types.Uid, hard bool) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Delete", id, hard) ret0, _ := ret[0].(error) return ret0 } // Delete indicates an expected call of Delete. func (mr *MockUsersPersistenceInterfaceMockRecorder) Delete(id, hard interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockUsersPersistenceInterface)(nil).Delete), id, hard) } // FailCred mocks base method. func (m *MockUsersPersistenceInterface) FailCred(id types.Uid, method string) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "FailCred", id, method) ret0, _ := ret[0].(error) return ret0 } // FailCred indicates an expected call of FailCred. func (mr *MockUsersPersistenceInterfaceMockRecorder) FailCred(id, method interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FailCred", reflect.TypeOf((*MockUsersPersistenceInterface)(nil).FailCred), id, method) } // FindOne mocks base method. func (m *MockUsersPersistenceInterface) FindOne(tag string) (string, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "FindOne", tag) ret0, _ := ret[0].(string) ret1, _ := ret[1].(error) return ret0, ret1 } // FindOne indicates an expected call of FindOne. func (mr *MockUsersPersistenceInterfaceMockRecorder) FindOne(tag interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindOne", reflect.TypeOf((*MockUsersPersistenceInterface)(nil).FindOne), tag) } // FindSubs mocks base method. func (m *MockUsersPersistenceInterface) FindSubs(caller types.Uid, prefPrefix string, required [][]string, optional []string, activeOnly bool) ([]types.Subscription, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "FindSubs", caller, prefPrefix, required, optional, activeOnly) ret0, _ := ret[0].([]types.Subscription) ret1, _ := ret[1].(error) return ret0, ret1 } // FindSubs indicates an expected call of FindSubs. func (mr *MockUsersPersistenceInterfaceMockRecorder) FindSubs(caller, prefPrefix, required, optional, activeOnly interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindSubs", reflect.TypeOf((*MockUsersPersistenceInterface)(nil).FindSubs), caller, prefPrefix, required, optional, activeOnly) } // Get mocks base method. func (m *MockUsersPersistenceInterface) Get(uid types.Uid) (*types.User, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Get", uid) ret0, _ := ret[0].(*types.User) ret1, _ := ret[1].(error) return ret0, ret1 } // Get indicates an expected call of Get. func (mr *MockUsersPersistenceInterfaceMockRecorder) Get(uid interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Get", reflect.TypeOf((*MockUsersPersistenceInterface)(nil).Get), uid) } // GetActiveCred mocks base method. func (m *MockUsersPersistenceInterface) GetActiveCred(id types.Uid, method string) (*types.Credential, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetActiveCred", id, method) ret0, _ := ret[0].(*types.Credential) ret1, _ := ret[1].(error) return ret0, ret1 } // GetActiveCred indicates an expected call of GetActiveCred. func (mr *MockUsersPersistenceInterfaceMockRecorder) GetActiveCred(id, method interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetActiveCred", reflect.TypeOf((*MockUsersPersistenceInterface)(nil).GetActiveCred), id, method) } // GetAll mocks base method. func (m *MockUsersPersistenceInterface) GetAll(uid ...types.Uid) ([]types.User, error) { m.ctrl.T.Helper() varargs := []interface{}{} for _, a := range uid { varargs = append(varargs, a) } ret := m.ctrl.Call(m, "GetAll", varargs...) ret0, _ := ret[0].([]types.User) ret1, _ := ret[1].(error) return ret0, ret1 } // GetAll indicates an expected call of GetAll. func (mr *MockUsersPersistenceInterfaceMockRecorder) GetAll(uid ...interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAll", reflect.TypeOf((*MockUsersPersistenceInterface)(nil).GetAll), uid...) } // GetAllCreds mocks base method. func (m *MockUsersPersistenceInterface) GetAllCreds(id types.Uid, method string, validatedOnly bool) ([]types.Credential, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetAllCreds", id, method, validatedOnly) ret0, _ := ret[0].([]types.Credential) ret1, _ := ret[1].(error) return ret0, ret1 } // GetAllCreds indicates an expected call of GetAllCreds. func (mr *MockUsersPersistenceInterfaceMockRecorder) GetAllCreds(id, method, validatedOnly interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAllCreds", reflect.TypeOf((*MockUsersPersistenceInterface)(nil).GetAllCreds), id, method, validatedOnly) } // GetAuthRecord mocks base method. func (m *MockUsersPersistenceInterface) GetAuthRecord(user types.Uid, scheme string) (string, auth.Level, []byte, time.Time, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetAuthRecord", user, scheme) ret0, _ := ret[0].(string) ret1, _ := ret[1].(auth.Level) ret2, _ := ret[2].([]byte) ret3, _ := ret[3].(time.Time) ret4, _ := ret[4].(error) return ret0, ret1, ret2, ret3, ret4 } // GetAuthRecord indicates an expected call of GetAuthRecord. func (mr *MockUsersPersistenceInterfaceMockRecorder) GetAuthRecord(user, scheme interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAuthRecord", reflect.TypeOf((*MockUsersPersistenceInterface)(nil).GetAuthRecord), user, scheme) } // GetAuthUniqueRecord mocks base method. func (m *MockUsersPersistenceInterface) GetAuthUniqueRecord(scheme, unique string) (types.Uid, auth.Level, []byte, time.Time, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetAuthUniqueRecord", scheme, unique) ret0, _ := ret[0].(types.Uid) ret1, _ := ret[1].(auth.Level) ret2, _ := ret[2].([]byte) ret3, _ := ret[3].(time.Time) ret4, _ := ret[4].(error) return ret0, ret1, ret2, ret3, ret4 } // GetAuthUniqueRecord indicates an expected call of GetAuthUniqueRecord. func (mr *MockUsersPersistenceInterfaceMockRecorder) GetAuthUniqueRecord(scheme, unique interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAuthUniqueRecord", reflect.TypeOf((*MockUsersPersistenceInterface)(nil).GetAuthUniqueRecord), scheme, unique) } // GetByCred mocks base method. func (m *MockUsersPersistenceInterface) GetByCred(method, value string) (types.Uid, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetByCred", method, value) ret0, _ := ret[0].(types.Uid) ret1, _ := ret[1].(error) return ret0, ret1 } // GetByCred indicates an expected call of GetByCred. func (mr *MockUsersPersistenceInterfaceMockRecorder) GetByCred(method, value interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetByCred", reflect.TypeOf((*MockUsersPersistenceInterface)(nil).GetByCred), method, value) } // GetChannels mocks base method. func (m *MockUsersPersistenceInterface) GetChannels(id types.Uid) ([]string, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetChannels", id) ret0, _ := ret[0].([]string) ret1, _ := ret[1].(error) return ret0, ret1 } // GetChannels indicates an expected call of GetChannels. func (mr *MockUsersPersistenceInterfaceMockRecorder) GetChannels(id interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetChannels", reflect.TypeOf((*MockUsersPersistenceInterface)(nil).GetChannels), id) } // GetOwnTopics mocks base method. func (m *MockUsersPersistenceInterface) GetOwnTopics(id types.Uid) ([]string, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetOwnTopics", id) ret0, _ := ret[0].([]string) ret1, _ := ret[1].(error) return ret0, ret1 } // GetOwnTopics indicates an expected call of GetOwnTopics. func (mr *MockUsersPersistenceInterfaceMockRecorder) GetOwnTopics(id interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetOwnTopics", reflect.TypeOf((*MockUsersPersistenceInterface)(nil).GetOwnTopics), id) } // GetSubs mocks base method. func (m *MockUsersPersistenceInterface) GetSubs(id types.Uid) ([]types.Subscription, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetSubs", id) ret0, _ := ret[0].([]types.Subscription) ret1, _ := ret[1].(error) return ret0, ret1 } // GetSubs indicates an expected call of GetSubs. func (mr *MockUsersPersistenceInterfaceMockRecorder) GetSubs(id interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetSubs", reflect.TypeOf((*MockUsersPersistenceInterface)(nil).GetSubs), id) } // GetTopics mocks base method. func (m *MockUsersPersistenceInterface) GetTopics(id types.Uid, opts *types.QueryOpt) ([]types.Subscription, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetTopics", id, opts) ret0, _ := ret[0].([]types.Subscription) ret1, _ := ret[1].(error) return ret0, ret1 } // GetTopics indicates an expected call of GetTopics. func (mr *MockUsersPersistenceInterfaceMockRecorder) GetTopics(id, opts interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetTopics", reflect.TypeOf((*MockUsersPersistenceInterface)(nil).GetTopics), id, opts) } // GetTopicsAny mocks base method. func (m *MockUsersPersistenceInterface) GetTopicsAny(id types.Uid, opts *types.QueryOpt) ([]types.Subscription, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetTopicsAny", id, opts) ret0, _ := ret[0].([]types.Subscription) ret1, _ := ret[1].(error) return ret0, ret1 } // GetTopicsAny indicates an expected call of GetTopicsAny. func (mr *MockUsersPersistenceInterfaceMockRecorder) GetTopicsAny(id, opts interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetTopicsAny", reflect.TypeOf((*MockUsersPersistenceInterface)(nil).GetTopicsAny), id, opts) } // GetUnreadCount mocks base method. func (m *MockUsersPersistenceInterface) GetUnreadCount(ids ...types.Uid) (map[types.Uid]int, error) { m.ctrl.T.Helper() varargs := []interface{}{} for _, a := range ids { varargs = append(varargs, a) } ret := m.ctrl.Call(m, "GetUnreadCount", varargs...) ret0, _ := ret[0].(map[types.Uid]int) ret1, _ := ret[1].(error) return ret0, ret1 } // GetUnreadCount indicates an expected call of GetUnreadCount. func (mr *MockUsersPersistenceInterfaceMockRecorder) GetUnreadCount(ids ...interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetUnreadCount", reflect.TypeOf((*MockUsersPersistenceInterface)(nil).GetUnreadCount), ids...) } // GetUnvalidated mocks base method. func (m *MockUsersPersistenceInterface) GetUnvalidated(lastUpdatedBefore time.Time, limit int) ([]types.Uid, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetUnvalidated", lastUpdatedBefore, limit) ret0, _ := ret[0].([]types.Uid) ret1, _ := ret[1].(error) return ret0, ret1 } // GetUnvalidated indicates an expected call of GetUnvalidated. func (mr *MockUsersPersistenceInterfaceMockRecorder) GetUnvalidated(lastUpdatedBefore, limit interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetUnvalidated", reflect.TypeOf((*MockUsersPersistenceInterface)(nil).GetUnvalidated), lastUpdatedBefore, limit) } // Update mocks base method. func (m *MockUsersPersistenceInterface) Update(uid types.Uid, update map[string]any) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Update", uid, update) ret0, _ := ret[0].(error) return ret0 } // Update indicates an expected call of Update. func (mr *MockUsersPersistenceInterfaceMockRecorder) Update(uid, update interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Update", reflect.TypeOf((*MockUsersPersistenceInterface)(nil).Update), uid, update) } // UpdateAuthRecord mocks base method. func (m *MockUsersPersistenceInterface) UpdateAuthRecord(uid types.Uid, authLvl auth.Level, scheme, unique string, secret []byte, expires time.Time) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "UpdateAuthRecord", uid, authLvl, scheme, unique, secret, expires) ret0, _ := ret[0].(error) return ret0 } // UpdateAuthRecord indicates an expected call of UpdateAuthRecord. func (mr *MockUsersPersistenceInterfaceMockRecorder) UpdateAuthRecord(uid, authLvl, scheme, unique, secret, expires interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateAuthRecord", reflect.TypeOf((*MockUsersPersistenceInterface)(nil).UpdateAuthRecord), uid, authLvl, scheme, unique, secret, expires) } // UpdateLastSeen mocks base method. func (m *MockUsersPersistenceInterface) UpdateLastSeen(uid types.Uid, userAgent string, when time.Time) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "UpdateLastSeen", uid, userAgent, when) ret0, _ := ret[0].(error) return ret0 } // UpdateLastSeen indicates an expected call of UpdateLastSeen. func (mr *MockUsersPersistenceInterfaceMockRecorder) UpdateLastSeen(uid, userAgent, when interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateLastSeen", reflect.TypeOf((*MockUsersPersistenceInterface)(nil).UpdateLastSeen), uid, userAgent, when) } // UpdateState mocks base method. func (m *MockUsersPersistenceInterface) UpdateState(uid types.Uid, state types.ObjState) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "UpdateState", uid, state) ret0, _ := ret[0].(error) return ret0 } // UpdateState indicates an expected call of UpdateState. func (mr *MockUsersPersistenceInterfaceMockRecorder) UpdateState(uid, state interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateState", reflect.TypeOf((*MockUsersPersistenceInterface)(nil).UpdateState), uid, state) } // UpdateTags mocks base method. func (m *MockUsersPersistenceInterface) UpdateTags(uid types.Uid, add, remove, reset []string) ([]string, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "UpdateTags", uid, add, remove, reset) ret0, _ := ret[0].([]string) ret1, _ := ret[1].(error) return ret0, ret1 } // UpdateTags indicates an expected call of UpdateTags. func (mr *MockUsersPersistenceInterfaceMockRecorder) UpdateTags(uid, add, remove, reset interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateTags", reflect.TypeOf((*MockUsersPersistenceInterface)(nil).UpdateTags), uid, add, remove, reset) } // UpsertCred mocks base method. func (m *MockUsersPersistenceInterface) UpsertCred(cred *types.Credential) (bool, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "UpsertCred", cred) ret0, _ := ret[0].(bool) ret1, _ := ret[1].(error) return ret0, ret1 } // UpsertCred indicates an expected call of UpsertCred. func (mr *MockUsersPersistenceInterfaceMockRecorder) UpsertCred(cred interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpsertCred", reflect.TypeOf((*MockUsersPersistenceInterface)(nil).UpsertCred), cred) } // MockTopicsPersistenceInterface is a mock of TopicsPersistenceInterface interface. type MockTopicsPersistenceInterface struct { ctrl *gomock.Controller recorder *MockTopicsPersistenceInterfaceMockRecorder } // MockTopicsPersistenceInterfaceMockRecorder is the mock recorder for MockTopicsPersistenceInterface. type MockTopicsPersistenceInterfaceMockRecorder struct { mock *MockTopicsPersistenceInterface } // NewMockTopicsPersistenceInterface creates a new mock instance. func NewMockTopicsPersistenceInterface(ctrl *gomock.Controller) *MockTopicsPersistenceInterface { mock := &MockTopicsPersistenceInterface{ctrl: ctrl} mock.recorder = &MockTopicsPersistenceInterfaceMockRecorder{mock} return mock } // EXPECT returns an object that allows the caller to indicate expected use. func (m *MockTopicsPersistenceInterface) EXPECT() *MockTopicsPersistenceInterfaceMockRecorder { return m.recorder } // Create mocks base method. func (m *MockTopicsPersistenceInterface) Create(topic *types.Topic, owner types.Uid, private any) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Create", topic, owner, private) ret0, _ := ret[0].(error) return ret0 } // Create indicates an expected call of Create. func (mr *MockTopicsPersistenceInterfaceMockRecorder) Create(topic, owner, private interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Create", reflect.TypeOf((*MockTopicsPersistenceInterface)(nil).Create), topic, owner, private) } // CreateP2P mocks base method. func (m *MockTopicsPersistenceInterface) CreateP2P(initiator, invited *types.Subscription) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "CreateP2P", initiator, invited) ret0, _ := ret[0].(error) return ret0 } // CreateP2P indicates an expected call of CreateP2P. func (mr *MockTopicsPersistenceInterfaceMockRecorder) CreateP2P(initiator, invited interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateP2P", reflect.TypeOf((*MockTopicsPersistenceInterface)(nil).CreateP2P), initiator, invited) } // Delete mocks base method. func (m *MockTopicsPersistenceInterface) Delete(topic string, isChan, hard bool) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Delete", topic, isChan, hard) ret0, _ := ret[0].(error) return ret0 } // Delete indicates an expected call of Delete. func (mr *MockTopicsPersistenceInterfaceMockRecorder) Delete(topic, isChan, hard interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockTopicsPersistenceInterface)(nil).Delete), topic, isChan, hard) } // Get mocks base method. func (m *MockTopicsPersistenceInterface) Get(topic string) (*types.Topic, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Get", topic) ret0, _ := ret[0].(*types.Topic) ret1, _ := ret[1].(error) return ret0, ret1 } // Get indicates an expected call of Get. func (mr *MockTopicsPersistenceInterfaceMockRecorder) Get(topic interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Get", reflect.TypeOf((*MockTopicsPersistenceInterface)(nil).Get), topic) } // GetSubs mocks base method. func (m *MockTopicsPersistenceInterface) GetSubs(topic string, opts *types.QueryOpt) ([]types.Subscription, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetSubs", topic, opts) ret0, _ := ret[0].([]types.Subscription) ret1, _ := ret[1].(error) return ret0, ret1 } // GetSubs indicates an expected call of GetSubs. func (mr *MockTopicsPersistenceInterfaceMockRecorder) GetSubs(topic, opts interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetSubs", reflect.TypeOf((*MockTopicsPersistenceInterface)(nil).GetSubs), topic, opts) } // GetSubsAny mocks base method. func (m *MockTopicsPersistenceInterface) GetSubsAny(topic string, opts *types.QueryOpt) ([]types.Subscription, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetSubsAny", topic, opts) ret0, _ := ret[0].([]types.Subscription) ret1, _ := ret[1].(error) return ret0, ret1 } // GetSubsAny indicates an expected call of GetSubsAny. func (mr *MockTopicsPersistenceInterfaceMockRecorder) GetSubsAny(topic, opts interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetSubsAny", reflect.TypeOf((*MockTopicsPersistenceInterface)(nil).GetSubsAny), topic, opts) } // GetUsers mocks base method. func (m *MockTopicsPersistenceInterface) GetUsers(topic string, opts *types.QueryOpt) ([]types.Subscription, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetUsers", topic, opts) ret0, _ := ret[0].([]types.Subscription) ret1, _ := ret[1].(error) return ret0, ret1 } // GetUsers indicates an expected call of GetUsers. func (mr *MockTopicsPersistenceInterfaceMockRecorder) GetUsers(topic, opts interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetUsers", reflect.TypeOf((*MockTopicsPersistenceInterface)(nil).GetUsers), topic, opts) } // GetUsersAny mocks base method. func (m *MockTopicsPersistenceInterface) GetUsersAny(topic string, opts *types.QueryOpt) ([]types.Subscription, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetUsersAny", topic, opts) ret0, _ := ret[0].([]types.Subscription) ret1, _ := ret[1].(error) return ret0, ret1 } // GetUsersAny indicates an expected call of GetUsersAny. func (mr *MockTopicsPersistenceInterfaceMockRecorder) GetUsersAny(topic, opts interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetUsersAny", reflect.TypeOf((*MockTopicsPersistenceInterface)(nil).GetUsersAny), topic, opts) } // OwnerChange mocks base method. func (m *MockTopicsPersistenceInterface) OwnerChange(topic string, newOwner types.Uid) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "OwnerChange", topic, newOwner) ret0, _ := ret[0].(error) return ret0 } // OwnerChange indicates an expected call of OwnerChange. func (mr *MockTopicsPersistenceInterfaceMockRecorder) OwnerChange(topic, newOwner interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "OwnerChange", reflect.TypeOf((*MockTopicsPersistenceInterface)(nil).OwnerChange), topic, newOwner) } // Update mocks base method. func (m *MockTopicsPersistenceInterface) Update(topic string, update map[string]any) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Update", topic, update) ret0, _ := ret[0].(error) return ret0 } // Update indicates an expected call of Update. func (mr *MockTopicsPersistenceInterfaceMockRecorder) Update(topic, update interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Update", reflect.TypeOf((*MockTopicsPersistenceInterface)(nil).Update), topic, update) } // UpdateSubCnt mocks base method. func (m *MockTopicsPersistenceInterface) UpdateSubCnt(topic string) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "UpdateSubCnt", topic) ret0, _ := ret[0].(error) return ret0 } // UpdateSubCnt indicates an expected call of UpdateSubCnt. func (mr *MockTopicsPersistenceInterfaceMockRecorder) UpdateSubCnt(topic interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateSubCnt", reflect.TypeOf((*MockTopicsPersistenceInterface)(nil).UpdateSubCnt), topic) } // MockSubsPersistenceInterface is a mock of SubsPersistenceInterface interface. type MockSubsPersistenceInterface struct { ctrl *gomock.Controller recorder *MockSubsPersistenceInterfaceMockRecorder } // MockSubsPersistenceInterfaceMockRecorder is the mock recorder for MockSubsPersistenceInterface. type MockSubsPersistenceInterfaceMockRecorder struct { mock *MockSubsPersistenceInterface } // NewMockSubsPersistenceInterface creates a new mock instance. func NewMockSubsPersistenceInterface(ctrl *gomock.Controller) *MockSubsPersistenceInterface { mock := &MockSubsPersistenceInterface{ctrl: ctrl} mock.recorder = &MockSubsPersistenceInterfaceMockRecorder{mock} return mock } // EXPECT returns an object that allows the caller to indicate expected use. func (m *MockSubsPersistenceInterface) EXPECT() *MockSubsPersistenceInterfaceMockRecorder { return m.recorder } // Create mocks base method. func (m *MockSubsPersistenceInterface) Create(subs ...*types.Subscription) error { m.ctrl.T.Helper() varargs := []interface{}{} for _, a := range subs { varargs = append(varargs, a) } ret := m.ctrl.Call(m, "Create", varargs...) ret0, _ := ret[0].(error) return ret0 } // Create indicates an expected call of Create. func (mr *MockSubsPersistenceInterfaceMockRecorder) Create(subs ...interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Create", reflect.TypeOf((*MockSubsPersistenceInterface)(nil).Create), subs...) } // Delete mocks base method. func (m *MockSubsPersistenceInterface) Delete(topic string, user types.Uid) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Delete", topic, user) ret0, _ := ret[0].(error) return ret0 } // Delete indicates an expected call of Delete. func (mr *MockSubsPersistenceInterfaceMockRecorder) Delete(topic, user interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockSubsPersistenceInterface)(nil).Delete), topic, user) } // Get mocks base method. func (m *MockSubsPersistenceInterface) Get(topic string, user types.Uid, keepDeleted bool) (*types.Subscription, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Get", topic, user, keepDeleted) ret0, _ := ret[0].(*types.Subscription) ret1, _ := ret[1].(error) return ret0, ret1 } // Get indicates an expected call of Get. func (mr *MockSubsPersistenceInterfaceMockRecorder) Get(topic, user, keepDeleted interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Get", reflect.TypeOf((*MockSubsPersistenceInterface)(nil).Get), topic, user, keepDeleted) } // Update mocks base method. func (m *MockSubsPersistenceInterface) Update(topic string, user types.Uid, update map[string]any) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Update", topic, user, update) ret0, _ := ret[0].(error) return ret0 } // Update indicates an expected call of Update. func (mr *MockSubsPersistenceInterfaceMockRecorder) Update(topic, user, update interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Update", reflect.TypeOf((*MockSubsPersistenceInterface)(nil).Update), topic, user, update) } // MockMessagesPersistenceInterface is a mock of MessagesPersistenceInterface interface. type MockMessagesPersistenceInterface struct { ctrl *gomock.Controller recorder *MockMessagesPersistenceInterfaceMockRecorder } // MockMessagesPersistenceInterfaceMockRecorder is the mock recorder for MockMessagesPersistenceInterface. type MockMessagesPersistenceInterfaceMockRecorder struct { mock *MockMessagesPersistenceInterface } // NewMockMessagesPersistenceInterface creates a new mock instance. func NewMockMessagesPersistenceInterface(ctrl *gomock.Controller) *MockMessagesPersistenceInterface { mock := &MockMessagesPersistenceInterface{ctrl: ctrl} mock.recorder = &MockMessagesPersistenceInterfaceMockRecorder{mock} return mock } // EXPECT returns an object that allows the caller to indicate expected use. func (m *MockMessagesPersistenceInterface) EXPECT() *MockMessagesPersistenceInterfaceMockRecorder { return m.recorder } // DeleteList mocks base method. func (m *MockMessagesPersistenceInterface) DeleteList(topic string, delID int, forUser types.Uid, msgDelAge time.Duration, ranges []types.Range) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "DeleteList", topic, delID, forUser, msgDelAge, ranges) ret0, _ := ret[0].(error) return ret0 } // DeleteList indicates an expected call of DeleteList. func (mr *MockMessagesPersistenceInterfaceMockRecorder) DeleteList(topic, delID, forUser, msgDelAge, ranges interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteList", reflect.TypeOf((*MockMessagesPersistenceInterface)(nil).DeleteList), topic, delID, forUser, msgDelAge, ranges) } // GetAll mocks base method. func (m *MockMessagesPersistenceInterface) GetAll(topic string, forUser types.Uid, opt *types.QueryOpt) ([]types.Message, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetAll", topic, forUser, opt) ret0, _ := ret[0].([]types.Message) ret1, _ := ret[1].(error) return ret0, ret1 } // GetAll indicates an expected call of GetAll. func (mr *MockMessagesPersistenceInterfaceMockRecorder) GetAll(topic, forUser, opt interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAll", reflect.TypeOf((*MockMessagesPersistenceInterface)(nil).GetAll), topic, forUser, opt) } // GetDeleted mocks base method. func (m *MockMessagesPersistenceInterface) GetDeleted(topic string, forUser types.Uid, opt *types.QueryOpt) ([]types.Range, int, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetDeleted", topic, forUser, opt) ret0, _ := ret[0].([]types.Range) ret1, _ := ret[1].(int) ret2, _ := ret[2].(error) return ret0, ret1, ret2 } // GetDeleted indicates an expected call of GetDeleted. func (mr *MockMessagesPersistenceInterfaceMockRecorder) GetDeleted(topic, forUser, opt interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetDeleted", reflect.TypeOf((*MockMessagesPersistenceInterface)(nil).GetDeleted), topic, forUser, opt) } // Save mocks base method. func (m *MockMessagesPersistenceInterface) Save(msg *types.Message, attachmentURLs []string, readBySender bool) (error, bool) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Save", msg, attachmentURLs, readBySender) ret0, _ := ret[0].(error) ret1, _ := ret[1].(bool) return ret0, ret1 } // Save indicates an expected call of Save. func (mr *MockMessagesPersistenceInterfaceMockRecorder) Save(msg, attachmentURLs, readBySender interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Save", reflect.TypeOf((*MockMessagesPersistenceInterface)(nil).Save), msg, attachmentURLs, readBySender) } // MockDevicePersistenceInterface is a mock of DevicePersistenceInterface interface. type MockDevicePersistenceInterface struct { ctrl *gomock.Controller recorder *MockDevicePersistenceInterfaceMockRecorder } // MockDevicePersistenceInterfaceMockRecorder is the mock recorder for MockDevicePersistenceInterface. type MockDevicePersistenceInterfaceMockRecorder struct { mock *MockDevicePersistenceInterface } // NewMockDevicePersistenceInterface creates a new mock instance. func NewMockDevicePersistenceInterface(ctrl *gomock.Controller) *MockDevicePersistenceInterface { mock := &MockDevicePersistenceInterface{ctrl: ctrl} mock.recorder = &MockDevicePersistenceInterfaceMockRecorder{mock} return mock } // EXPECT returns an object that allows the caller to indicate expected use. func (m *MockDevicePersistenceInterface) EXPECT() *MockDevicePersistenceInterfaceMockRecorder { return m.recorder } // Delete mocks base method. func (m *MockDevicePersistenceInterface) Delete(uid types.Uid, deviceID string) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Delete", uid, deviceID) ret0, _ := ret[0].(error) return ret0 } // Delete indicates an expected call of Delete. func (mr *MockDevicePersistenceInterfaceMockRecorder) Delete(uid, deviceID interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockDevicePersistenceInterface)(nil).Delete), uid, deviceID) } // GetAll mocks base method. func (m *MockDevicePersistenceInterface) GetAll(uid ...types.Uid) (map[types.Uid][]types.DeviceDef, int, error) { m.ctrl.T.Helper() varargs := []interface{}{} for _, a := range uid { varargs = append(varargs, a) } ret := m.ctrl.Call(m, "GetAll", varargs...) ret0, _ := ret[0].(map[types.Uid][]types.DeviceDef) ret1, _ := ret[1].(int) ret2, _ := ret[2].(error) return ret0, ret1, ret2 } // GetAll indicates an expected call of GetAll. func (mr *MockDevicePersistenceInterfaceMockRecorder) GetAll(uid ...interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAll", reflect.TypeOf((*MockDevicePersistenceInterface)(nil).GetAll), uid...) } // Update mocks base method. func (m *MockDevicePersistenceInterface) Update(uid types.Uid, oldDeviceID string, dev *types.DeviceDef) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Update", uid, oldDeviceID, dev) ret0, _ := ret[0].(error) return ret0 } // Update indicates an expected call of Update. func (mr *MockDevicePersistenceInterfaceMockRecorder) Update(uid, oldDeviceID, dev interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Update", reflect.TypeOf((*MockDevicePersistenceInterface)(nil).Update), uid, oldDeviceID, dev) } // MockFilePersistenceInterface is a mock of FilePersistenceInterface interface. type MockFilePersistenceInterface struct { ctrl *gomock.Controller recorder *MockFilePersistenceInterfaceMockRecorder } // MockFilePersistenceInterfaceMockRecorder is the mock recorder for MockFilePersistenceInterface. type MockFilePersistenceInterfaceMockRecorder struct { mock *MockFilePersistenceInterface } // NewMockFilePersistenceInterface creates a new mock instance. func NewMockFilePersistenceInterface(ctrl *gomock.Controller) *MockFilePersistenceInterface { mock := &MockFilePersistenceInterface{ctrl: ctrl} mock.recorder = &MockFilePersistenceInterfaceMockRecorder{mock} return mock } // EXPECT returns an object that allows the caller to indicate expected use. func (m *MockFilePersistenceInterface) EXPECT() *MockFilePersistenceInterfaceMockRecorder { return m.recorder } // DeleteUnused mocks base method. func (m *MockFilePersistenceInterface) DeleteUnused(olderThan time.Time, limit int) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "DeleteUnused", olderThan, limit) ret0, _ := ret[0].(error) return ret0 } // DeleteUnused indicates an expected call of DeleteUnused. func (mr *MockFilePersistenceInterfaceMockRecorder) DeleteUnused(olderThan, limit interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteUnused", reflect.TypeOf((*MockFilePersistenceInterface)(nil).DeleteUnused), olderThan, limit) } // FinishUpload mocks base method. func (m *MockFilePersistenceInterface) FinishUpload(fd *types.FileDef, success bool, size int64) (*types.FileDef, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "FinishUpload", fd, success, size) ret0, _ := ret[0].(*types.FileDef) ret1, _ := ret[1].(error) return ret0, ret1 } // FinishUpload indicates an expected call of FinishUpload. func (mr *MockFilePersistenceInterfaceMockRecorder) FinishUpload(fd, success, size interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FinishUpload", reflect.TypeOf((*MockFilePersistenceInterface)(nil).FinishUpload), fd, success, size) } // Get mocks base method. func (m *MockFilePersistenceInterface) Get(fid string) (*types.FileDef, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Get", fid) ret0, _ := ret[0].(*types.FileDef) ret1, _ := ret[1].(error) return ret0, ret1 } // Get indicates an expected call of Get. func (mr *MockFilePersistenceInterfaceMockRecorder) Get(fid interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Get", reflect.TypeOf((*MockFilePersistenceInterface)(nil).Get), fid) } // LinkAttachments mocks base method. func (m *MockFilePersistenceInterface) LinkAttachments(topic string, msgId types.Uid, attachments []string) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "LinkAttachments", topic, msgId, attachments) ret0, _ := ret[0].(error) return ret0 } // LinkAttachments indicates an expected call of LinkAttachments. func (mr *MockFilePersistenceInterfaceMockRecorder) LinkAttachments(topic, msgId, attachments interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "LinkAttachments", reflect.TypeOf((*MockFilePersistenceInterface)(nil).LinkAttachments), topic, msgId, attachments) } // StartUpload mocks base method. func (m *MockFilePersistenceInterface) StartUpload(fd *types.FileDef) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "StartUpload", fd) ret0, _ := ret[0].(error) return ret0 } // StartUpload indicates an expected call of StartUpload. func (mr *MockFilePersistenceInterfaceMockRecorder) StartUpload(fd interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "StartUpload", reflect.TypeOf((*MockFilePersistenceInterface)(nil).StartUpload), fd) } // MockPersistentCacheInterface is a mock of PersistentCacheInterface interface. type MockPersistentCacheInterface struct { ctrl *gomock.Controller recorder *MockPersistentCacheInterfaceMockRecorder } // MockPersistentCacheInterfaceMockRecorder is the mock recorder for MockPersistentCacheInterface. type MockPersistentCacheInterfaceMockRecorder struct { mock *MockPersistentCacheInterface } // NewMockPersistentCacheInterface creates a new mock instance. func NewMockPersistentCacheInterface(ctrl *gomock.Controller) *MockPersistentCacheInterface { mock := &MockPersistentCacheInterface{ctrl: ctrl} mock.recorder = &MockPersistentCacheInterfaceMockRecorder{mock} return mock } // EXPECT returns an object that allows the caller to indicate expected use. func (m *MockPersistentCacheInterface) EXPECT() *MockPersistentCacheInterfaceMockRecorder { return m.recorder } // Delete mocks base method. func (m *MockPersistentCacheInterface) Delete(key string) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Delete", key) ret0, _ := ret[0].(error) return ret0 } // Delete indicates an expected call of Delete. func (mr *MockPersistentCacheInterfaceMockRecorder) Delete(key interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockPersistentCacheInterface)(nil).Delete), key) } // Expire mocks base method. func (m *MockPersistentCacheInterface) Expire(keyPrefix string, olderThan time.Time) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Expire", keyPrefix, olderThan) ret0, _ := ret[0].(error) return ret0 } // Expire indicates an expected call of Expire. func (mr *MockPersistentCacheInterfaceMockRecorder) Expire(keyPrefix, olderThan interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Expire", reflect.TypeOf((*MockPersistentCacheInterface)(nil).Expire), keyPrefix, olderThan) } // Get mocks base method. func (m *MockPersistentCacheInterface) Get(key string) (string, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Get", key) ret0, _ := ret[0].(string) ret1, _ := ret[1].(error) return ret0, ret1 } // Get indicates an expected call of Get. func (mr *MockPersistentCacheInterfaceMockRecorder) Get(key interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Get", reflect.TypeOf((*MockPersistentCacheInterface)(nil).Get), key) } // Upsert mocks base method. func (m *MockPersistentCacheInterface) Upsert(key, value string, failOnDuplicate bool) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Upsert", key, value, failOnDuplicate) ret0, _ := ret[0].(error) return ret0 } // Upsert indicates an expected call of Upsert. func (mr *MockPersistentCacheInterfaceMockRecorder) Upsert(key, value, failOnDuplicate interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Upsert", reflect.TypeOf((*MockPersistentCacheInterface)(nil).Upsert), key, value, failOnDuplicate) } ================================================ FILE: server/store/store.go ================================================ // Package store provides methods for registering and accessing database adapters. package store import ( "encoding/json" "errors" "fmt" "sort" "strings" "time" "github.com/tinode/chat/server/auth" adapter "github.com/tinode/chat/server/db" "github.com/tinode/chat/server/logs" "github.com/tinode/chat/server/media" "github.com/tinode/chat/server/store/types" "github.com/tinode/chat/server/validate" ) var adp adapter.Adapter var availableAdapters = make(map[string]adapter.Adapter) var mediaHandler media.Handler // Unique ID generator var uGen types.UidGenerator type configType struct { // 16-byte key for XTEA. Used to initialize types.UidGenerator. UidKey []byte `json:"uid_key"` // Maximum number of results to return from adapter. MaxResults int `json:"max_results"` // DB adapter name to use. Should be one of those specified in `Adapters`. UseAdapter string `json:"use_adapter"` // Configurations for individual adapters. Adapters map[string]json.RawMessage `json:"adapters"` } func openAdapter(workerId int, jsonconf json.RawMessage) error { var config configType if err := json.Unmarshal(jsonconf, &config); err != nil { return errors.New("store: failed to parse config: " + err.Error() + "(" + string(jsonconf) + ")") } if adp == nil { if len(config.UseAdapter) > 0 { // Adapter name specified explicitly. if ad, ok := availableAdapters[config.UseAdapter]; ok { adp = ad } else { return errors.New("store: " + config.UseAdapter + " adapter is not available in this binary") } } else if len(availableAdapters) == 1 { // Default to the only entry in availableAdapters. for _, v := range availableAdapters { adp = v } } else { return errors.New("store: db adapter is not specified. Please set `store_config.use_adapter` in `tinode.conf`") } } if adp.IsOpen() { return errors.New("store: connection is already opened") } // Initialize snowflake. if workerId < 0 || workerId > 1023 { return errors.New("store: invalid worker ID") } if err := uGen.Init(uint(workerId), config.UidKey); err != nil { return errors.New("store: failed to init snowflake: " + err.Error()) } if err := adp.SetMaxResults(config.MaxResults); err != nil { return err } var adapterConfig json.RawMessage if config.Adapters != nil { adapterConfig = config.Adapters[adp.GetName()] } return adp.Open(adapterConfig) } // PersistentStorageInterface defines methods used for interation with persistent storage. type PersistentStorageInterface interface { Open(workerId int, jsonconf json.RawMessage) error Close() error IsOpen() bool GetAdapter() adapter.Adapter GetAdapterName() string GetAdapterVersion() int GetDbVersion() int InitDb(jsonconf json.RawMessage, reset bool) error UpgradeDb(jsonconf json.RawMessage) error GetUid() types.Uid GetUidString() string DbStats() func() any GetAuthNames() []string GetAuthHandler(name string) auth.AuthHandler GetLogicalAuthHandler(name string) auth.AuthHandler GetValidator(name string) validate.Validator GetMediaHandler() media.Handler UseMediaHandler(name, config string) error } // Store is the main object for interacting with persistent storage. var Store PersistentStorageInterface type storeObj struct{} // Open initializes the persistence system. Adapter holds a connection pool for a database instance. // // name - name of the adapter rquested in the config file // jsonconf - configuration string func (storeObj) Open(workerId int, jsonconf json.RawMessage) error { if err := openAdapter(workerId, jsonconf); err != nil { return err } return adp.CheckDbVersion() } // Close terminates connection to persistent storage. func (storeObj) Close() error { if adp.IsOpen() { return adp.Close() } return nil } // IsOpen checks if persistent storage connection has been initialized. func (storeObj) IsOpen() bool { if adp != nil { return adp.IsOpen() } return false } // GetAdapter returns the currently configured adapter. func (storeObj) GetAdapter() adapter.Adapter { return adp } // GetAdapterName returns the name of the current adater. func (storeObj) GetAdapterName() string { if adp != nil { return adp.GetName() } return "" } // GetAdapterVersion returns version of the current adater. func (storeObj) GetAdapterVersion() int { if adp != nil { return adp.Version() } return -1 } // GetDbVersion returns version of the underlying database. func (storeObj) GetDbVersion() int { if adp != nil { vers, _ := adp.GetDbVersion() return vers } return -1 } // InitDb creates and configures a new database instance. If 'reset' is true it will first // attempt to drop an existing database. If jsconf is nil it will assume that the adapter is // already open. If it's non-nil and the adapter is not open, it will use the config string // to open the adapter first. func (s storeObj) InitDb(jsonconf json.RawMessage, reset bool) error { if !s.IsOpen() { if err := openAdapter(1, jsonconf); err != nil { return err } } return adp.CreateDb(reset) } // UpgradeDb performes an upgrade of the database to the current adapter version. // If jsconf is nil it will assume that the adapter is already open. If it's non-nil and the // adapter is not open, it will use the config string to open the adapter first. func (s storeObj) UpgradeDb(jsonconf json.RawMessage) error { if !s.IsOpen() { if err := openAdapter(1, jsonconf); err != nil { return err } } return adp.UpgradeDb() } // RegisterAdapter makes a persistence adapter available. // If Register is called twice or if the adapter is nil, it panics. func RegisterAdapter(a adapter.Adapter) { if a == nil { panic("store: Register adapter is nil") } adapterName := a.GetName() if _, ok := availableAdapters[adapterName]; ok { panic("store: adapter '" + adapterName + "' is already registered") } availableAdapters[adapterName] = a } // GetUid generates a unique ID suitable for use as a primary key. func (storeObj) GetUid() types.Uid { return uGen.Get() } // GetUidString generate unique ID as a string. func (storeObj) GetUidString() string { return uGen.GetStr() } // DecodeUid takes an XTEA encrypted Uid and decrypts it into an int64. // This is needed for sql compatibility. Tte original int64 values // are generated by snowflake which ensures that the top bit is unset. func DecodeUid(uid types.Uid) int64 { if uid.IsZero() { return 0 } return uGen.DecodeUid(uid) } // EncodeUid applies XTEA encryption to an int64 value. It's the inverse of DecodeUid. func EncodeUid(id int64) types.Uid { if id == 0 { return types.ZeroUid } return uGen.EncodeInt64(id) } // DbStats returns a callback returning db connection stats object. func (s storeObj) DbStats() func() any { if !s.IsOpen() { return nil } return adp.Stats } // UsersPersistenceInterface is an interface which defines methods for persistent storage of user records. type UsersPersistenceInterface interface { Create(user *types.User, private any) (*types.User, error) GetAuthRecord(user types.Uid, scheme string) (string, auth.Level, []byte, time.Time, error) GetAuthUniqueRecord(scheme, unique string) (types.Uid, auth.Level, []byte, time.Time, error) AddAuthRecord(uid types.Uid, authLvl auth.Level, scheme, unique string, secret []byte, expires time.Time) error UpdateAuthRecord(uid types.Uid, authLvl auth.Level, scheme, unique string, secret []byte, expires time.Time) error DelAuthRecords(uid types.Uid, scheme string) error Get(uid types.Uid) (*types.User, error) GetAll(uid ...types.Uid) ([]types.User, error) GetByCred(method, value string) (types.Uid, error) Delete(id types.Uid, hard bool) error UpdateLastSeen(uid types.Uid, userAgent string, when time.Time) error Update(uid types.Uid, update map[string]any) error UpdateTags(uid types.Uid, add, remove, reset []string) ([]string, error) UpdateState(uid types.Uid, state types.ObjState) error GetSubs(id types.Uid) ([]types.Subscription, error) FindSubs(caller types.Uid, prefPrefix string, required [][]string, optional []string, activeOnly bool) ([]types.Subscription, error) FindOne(tag string) (string, error) GetTopics(id types.Uid, opts *types.QueryOpt) ([]types.Subscription, error) GetTopicsAny(id types.Uid, opts *types.QueryOpt) ([]types.Subscription, error) GetOwnTopics(id types.Uid) ([]string, error) GetChannels(id types.Uid) ([]string, error) UpsertCred(cred *types.Credential) (bool, error) ConfirmCred(id types.Uid, method string) error FailCred(id types.Uid, method string) error GetActiveCred(id types.Uid, method string) (*types.Credential, error) GetAllCreds(id types.Uid, method string, validatedOnly bool) ([]types.Credential, error) DelCred(id types.Uid, method, value string) error GetUnreadCount(ids ...types.Uid) (map[types.Uid]int, error) GetUnvalidated(lastUpdatedBefore time.Time, limit int) ([]types.Uid, error) } // usersMapper is a concrete type which implements UsersPersistenceInterface. type usersMapper struct{} // Users is a singleton ancor object exporting UsersPersistenceInterface methods. var Users UsersPersistenceInterface // Create inserts User object into a database, updates creation time and assigns UID func (usersMapper) Create(user *types.User, private any) (*types.User, error) { user.SetUid(Store.GetUid()) user.InitTimes() err := adp.UserCreate(user) if err != nil { return nil, err } // Create user's subscription to 'me' && 'fnd'. These topics are ephemeral, the topic object need not to be // inserted. err = Subs.Create( &types.Subscription{ ObjHeader: types.ObjHeader{CreatedAt: user.CreatedAt}, User: user.Id, Topic: user.Uid().UserId(), ModeWant: types.ModeCMeFnd, ModeGiven: types.ModeCMeFnd, Private: private, }, &types.Subscription{ ObjHeader: types.ObjHeader{CreatedAt: user.CreatedAt}, User: user.Id, Topic: user.Uid().FndName(), ModeWant: types.ModeCMeFnd, ModeGiven: types.ModeCMeFnd, Private: nil, }) if err != nil { // Best effort to delete incomplete user record. Orphaned user records are not a problem. // They just take up space. adp.UserDelete(user.Uid(), true) return nil, err } return user, nil } // GetAuthRecord takes a user ID and a authentication scheme name, fetches unique scheme-dependent identifier and // authentication secret. func (usersMapper) GetAuthRecord(user types.Uid, scheme string) (string, auth.Level, []byte, time.Time, error) { unique, authLvl, secret, expires, err := adp.AuthGetRecord(user, scheme) if err == nil { parts := strings.Split(unique, ":") if len(parts) > 1 { unique = parts[1] } else { err = types.ErrInternal } } return unique, authLvl, secret, expires, err } // GetAuthUniqueRecord takes a unique identifier and a authentication scheme name, fetches user ID and // authentication secret. func (usersMapper) GetAuthUniqueRecord(scheme, unique string) (types.Uid, auth.Level, []byte, time.Time, error) { return adp.AuthGetUniqueRecord(scheme + ":" + unique) } // AddAuthRecord creates a new authentication record for the given user. func (usersMapper) AddAuthRecord(uid types.Uid, authLvl auth.Level, scheme, unique string, secret []byte, expires time.Time) error { return adp.AuthAddRecord(uid, scheme, scheme+":"+unique, authLvl, secret, expires) } // UpdateAuthRecord updates authentication record with a new secret and expiration time. func (usersMapper) UpdateAuthRecord(uid types.Uid, authLvl auth.Level, scheme, unique string, secret []byte, expires time.Time) error { return adp.AuthUpdRecord(uid, scheme, scheme+":"+unique, authLvl, secret, expires) } // DelAuthRecords deletes user's auth records of the given scheme. func (usersMapper) DelAuthRecords(uid types.Uid, scheme string) error { return adp.AuthDelScheme(uid, scheme) } // Get returns a user object for the given user ID or nil if the user is not found. func (usersMapper) Get(uid types.Uid) (*types.User, error) { return adp.UserGet(uid) } // GetAll returns a slice of user objects for the given user IDs. func (usersMapper) GetAll(uid ...types.Uid) ([]types.User, error) { return adp.UserGetAll(uid...) } // GetByCred returns user ID for the given validated credential. func (usersMapper) GetByCred(method, value string) (types.Uid, error) { return adp.UserGetByCred(method, value) } // Delete deletes user records. func (usersMapper) Delete(id types.Uid, hard bool) error { return adp.UserDelete(id, hard) } // UpdateLastSeen updates LastSeen and UserAgent. func (usersMapper) UpdateLastSeen(uid types.Uid, userAgent string, when time.Time) error { return adp.UserUpdate(uid, map[string]any{"LastSeen": when, "UserAgent": userAgent}) } // Update is a general-purpose update of user data. func (usersMapper) Update(uid types.Uid, update map[string]any) error { if _, ok := update["UpdatedAt"]; !ok { update["UpdatedAt"] = types.TimeNow() } return adp.UserUpdate(uid, update) } // UpdateTags either adds, removes, or resets tags to the given slices. func (usersMapper) UpdateTags(uid types.Uid, add, remove, reset []string) ([]string, error) { return adp.UserUpdateTags(uid, add, remove, reset) } // UpdateState changes user's state and state of some topics associated with the user. func (usersMapper) UpdateState(uid types.Uid, state types.ObjState) error { update := map[string]any{ "State": state, "StateAt": types.TimeNow()} return adp.UserUpdate(uid, update) } // GetSubs loads *all* subscriptions for the given user. // Does not load Public/Trusted or Private, does not load deleted subscriptions. func (usersMapper) GetSubs(id types.Uid) ([]types.Subscription, error) { return adp.SubsForUser(id) } // FindSubs find a list of users and topics for the given tags. Results are formatted as subscriptions. // `required` specifies an AND of ORs for required terms: // at least one element of every sublist in `required` must be present in the object's tags list. // `optional` specifies a list of optional terms. func (usersMapper) FindSubs(caller types.Uid, prefPrefix string, required [][]string, optional []string, activeOnly bool) ([]types.Subscription, error) { if len(required) == 0 && len(optional) == 0 { // No tags specified, return empty list. return nil, nil } return adp.Find(caller.UserId(), prefPrefix, required, optional, activeOnly) } // Find returns topics and/or users which match the given tag, with optional partial matching. func (usersMapper) FindOne(tag string) (string, error) { return adp.FindOne(tag) } // GetTopics load a list of user's subscriptions with Public+Trusted fields copied to subscription func (usersMapper) GetTopics(id types.Uid, opts *types.QueryOpt) ([]types.Subscription, error) { return adp.TopicsForUser(id, false, opts) } // GetTopicsAny load a list of user's subscriptions with Public+Trusted fields copied to subscription. // Deleted topics are returned too. func (usersMapper) GetTopicsAny(id types.Uid, opts *types.QueryOpt) ([]types.Subscription, error) { return adp.TopicsForUser(id, true, opts) } // GetOwnTopics returns a slice of group topic names where the user is the owner. func (usersMapper) GetOwnTopics(id types.Uid) ([]string, error) { return adp.OwnTopics(id) } // GetChannels returns a slice of group topic names where the user is a channel reader. func (usersMapper) GetChannels(id types.Uid) ([]string, error) { return adp.ChannelsForUser(id) } // UpsertCred adds or updates a credential validation request. Return true if the record was inserted, false if updated. func (usersMapper) UpsertCred(cred *types.Credential) (bool, error) { cred.InitTimes() return adp.CredUpsert(cred) } // ConfirmCred marks credential method as confirmed. func (usersMapper) ConfirmCred(id types.Uid, method string) error { return adp.CredConfirm(id, method) } // FailCred increments fail count for the given credential method. func (usersMapper) FailCred(id types.Uid, method string) error { return adp.CredFail(id, method) } // GetActiveCred gets a the currently active credential for the given user and method. func (usersMapper) GetActiveCred(id types.Uid, method string) (*types.Credential, error) { return adp.CredGetActive(id, method) } // GetAllCreds returns credentials of the given user, all or validated only. func (usersMapper) GetAllCreds(id types.Uid, method string, validatedOnly bool) ([]types.Credential, error) { return adp.CredGetAll(id, method, validatedOnly) } // DelCred deletes user's credentials. If method is "", all credentials are deleted. func (usersMapper) DelCred(id types.Uid, method, value string) error { return adp.CredDel(id, method, value) } // GetUnreadCount returs users' total count of unread messages in all topics with the R permissions. func (usersMapper) GetUnreadCount(ids ...types.Uid) (map[types.Uid]int, error) { return adp.UserUnreadCount(ids...) } // GetUnvalidated returns a list of stale user ids which have unvalidated credentials, // their auth levels and a comma-separated list of these credential names. func (usersMapper) GetUnvalidated(lastUpdatedBefore time.Time, limit int) ([]types.Uid, error) { return adp.UserGetUnvalidated(lastUpdatedBefore, limit) } // TopicsPersistenceInterface is an interface which defines methods for persistent storage of topics. type TopicsPersistenceInterface interface { Create(topic *types.Topic, owner types.Uid, private any) error CreateP2P(initiator, invited *types.Subscription) error Get(topic string) (*types.Topic, error) GetUsers(topic string, opts *types.QueryOpt) ([]types.Subscription, error) GetUsersAny(topic string, opts *types.QueryOpt) ([]types.Subscription, error) GetSubs(topic string, opts *types.QueryOpt) ([]types.Subscription, error) GetSubsAny(topic string, opts *types.QueryOpt) ([]types.Subscription, error) Update(topic string, update map[string]any) error UpdateSubCnt(topic string) error OwnerChange(topic string, newOwner types.Uid) error Delete(topic string, isChan, hard bool) error } // topicsMapper is a concrete type implementing TopicsPersistenceInterface. type topicsMapper struct{} // Topics is a singleton ancor object exporting TopicsPersistenceInterface methods. var Topics TopicsPersistenceInterface // Create creates a topic and owner's subscription to it. func (topicsMapper) Create(topic *types.Topic, owner types.Uid, private any) error { topic.InitTimes() topic.TouchedAt = topic.CreatedAt topic.Owner = owner.String() err := adp.TopicCreate(topic) if err != nil { return err } if !owner.IsZero() { err = Subs.Create(&types.Subscription{ ObjHeader: types.ObjHeader{CreatedAt: topic.CreatedAt}, User: owner.String(), Topic: topic.Id, ModeGiven: types.ModeCFull, ModeWant: topic.GetAccess(owner), Private: private}) } return err } // CreateP2P creates a P2P topic by generating two user's subsciptions to each other. func (topicsMapper) CreateP2P(initiator, invited *types.Subscription) error { initiator.InitTimes() initiator.SetTouchedAt(initiator.CreatedAt) invited.InitTimes() invited.SetTouchedAt(invited.CreatedAt) return adp.TopicCreateP2P(initiator, invited) } // Get a single topic with a list of relevant users de-normalized into it func (topicsMapper) Get(topic string) (*types.Topic, error) { return adp.TopicGet(topic) } // GetUsers loads subscriptions for topic plus loads user.Public+Trusted. // Deleted subscriptions are not loaded. func (topicsMapper) GetUsers(topic string, opts *types.QueryOpt) ([]types.Subscription, error) { return adp.UsersForTopic(topic, false, opts) } // GetUsersAny loads subscriptions for topic plus loads user.Public+Trusted. It's the same as GetUsers, // except it loads deleted subscriptions too. func (topicsMapper) GetUsersAny(topic string, opts *types.QueryOpt) ([]types.Subscription, error) { return adp.UsersForTopic(topic, true, opts) } // GetSubs loads a list of subscriptions to the given topic, user.Public+Trusted and deleted // subscriptions are not loaded. Suspended subscriptions are loaded. func (topicsMapper) GetSubs(topic string, opts *types.QueryOpt) ([]types.Subscription, error) { return adp.SubsForTopic(topic, false, opts) } // GetSubsAny loads a list of subscriptions to the given topic including deleted subscription. // user.Public/Trusted are not loaded func (topicsMapper) GetSubsAny(topic string, opts *types.QueryOpt) ([]types.Subscription, error) { return adp.SubsForTopic(topic, true, opts) } // UpdateSubCnt refreshes subscriber count value denormalized in topic. func (topicsMapper) UpdateSubCnt(topic string) error { return adp.TopicUpdateSubCnt(topic) } // Update is a generic topic update. func (topicsMapper) Update(topic string, update map[string]any) error { if _, ok := update["UpdatedAt"]; !ok { update["UpdatedAt"] = types.TimeNow() } return adp.TopicUpdate(topic, update) } // OwnerChange replaces the old topic owner with the new owner. func (topicsMapper) OwnerChange(topic string, newOwner types.Uid) error { return adp.TopicOwnerChange(topic, newOwner) } // Delete deletes topic, messages, attachments, and subscriptions. func (topicsMapper) Delete(topic string, isChan, hard bool) error { return adp.TopicDelete(topic, isChan, hard) } // SubsPersistenceInterface is an interface which defines methods for persistent storage of subscriptions. type SubsPersistenceInterface interface { Create(subs ...*types.Subscription) error Get(topic string, user types.Uid, keepDeleted bool) (*types.Subscription, error) Update(topic string, user types.Uid, update map[string]any) error Delete(topic string, user types.Uid) error } // subsMapper is a concrete type implementing SubsPersistenceInterface. type subsMapper struct{} // Subs is a singleton ancor object exporting SubsPersistenceInterface. var Subs SubsPersistenceInterface // Create creates multiple subscriptions. func (subsMapper) Create(subs ...*types.Subscription) error { if len(subs) == 0 { // Nothing to do. return nil } topic := subs[0].Topic if types.IsEphemeralTopic(topic) { // Ephemeral topics are not persisted in 'topics' table, don't try to update them. // Mixing ephemeral and real topics is not permitted. topic = "" } for _, sub := range subs { sub.InitTimes() if topic != "" && sub.Topic != topic { return fmt.Errorf("all subscriptions must be for the same topic, got %s vs %s", sub.Topic, topic) } } return adp.TopicShare(topic, subs) } // Get subscription given topic and user ID. func (subsMapper) Get(topic string, user types.Uid, keepDeleted bool) (*types.Subscription, error) { return adp.SubscriptionGet(topic, user, keepDeleted) } // Update values of topic's subscriptions. func (subsMapper) Update(topic string, user types.Uid, update map[string]any) error { update["UpdatedAt"] = types.TimeNow() return adp.SubsUpdate(topic, user, update) } // Delete deletes a subscription. // To delete channel subscription the channel name must be explicitly specified. func (subsMapper) Delete(topic string, user types.Uid) error { return adp.SubsDelete(topic, user) } // MessagesPersistenceInterface is an interface which defines methods for persistent storage of messages. type MessagesPersistenceInterface interface { Save(msg *types.Message, attachmentURLs []string, readBySender bool) (error, bool) DeleteList(topic string, delID int, forUser types.Uid, msgDelAge time.Duration, ranges []types.Range) error GetAll(topic string, forUser types.Uid, opt *types.QueryOpt) ([]types.Message, error) GetDeleted(topic string, forUser types.Uid, opt *types.QueryOpt) ([]types.Range, int, error) } // messagesMapper is a concrete type implementing MessagesPersistenceInterface. type messagesMapper struct{} // Messages is a singleton ancor object for exporting MessagesPersistenceInterface. var Messages MessagesPersistenceInterface // Save message func (messagesMapper) Save(msg *types.Message, attachmentURLs []string, readBySender bool) (error, bool) { msg.InitTimes() msg.SetUid(Store.GetUid()) // Increment topic's or user's SeqId err := adp.TopicUpdateOnMessage(msg.Topic, msg) if err != nil { return err, false } err = adp.MessageSave(msg) if err != nil { return err, false } markedReadBySender := false // Mark message as read by the sender. if readBySender { // Make sure From is valid, otherwise we will reset values for all subscribers. fromUid := types.ParseUid(msg.From) if !fromUid.IsZero() { // Ignore the error here. It's not a big deal if it fails. if subErr := adp.SubsUpdate(msg.Topic, fromUid, map[string]any{ "RecvSeqId": msg.SeqId, "ReadSeqId": msg.SeqId}); subErr != nil { logs.Warn.Printf("topic[%s]: failed to mark message (seq: %d) read by sender - err: %+v", msg.Topic, msg.SeqId, subErr) } else { markedReadBySender = true } } } if len(attachmentURLs) > 0 { var attachments []string for _, url := range attachmentURLs { // Convert attachment URLs to file IDs. if fid := mediaHandler.GetIdFromUrl(url); !fid.IsZero() { attachments = append(attachments, fid.String()) } } if len(attachments) > 0 { return adp.FileLinkAttachments("", types.ZeroUid, msg.Uid(), attachments), markedReadBySender } } return nil, markedReadBySender } // DeleteList deletes multiple messages defined by a list of ranges. func (messagesMapper) DeleteList(topic string, delID int, forUser types.Uid, msgDelAge time.Duration, ranges []types.Range) error { var toDel *types.DelMessage if delID > 0 { toDel = &types.DelMessage{ Topic: topic, DelId: delID, DeletedFor: forUser.String(), SeqIdRanges: ranges} toDel.SetUid(Store.GetUid()) toDel.InitTimes() if msgDelAge > 0 { toDel.SetNewerThan(toDel.CreatedAt.Add(-msgDelAge)) } } err := adp.MessageDeleteList(topic, toDel) if err != nil { return err } // TODO: move to adapter. if delID > 0 { // Record ID of the delete transaction err = adp.TopicUpdate(topic, map[string]any{"DelId": delID}) if err != nil { return err } // Soft-deleting will update one subscription, hard-deleting will ipdate all. // Soft- or hard- is defined by the forUser being defined. err = adp.SubsUpdate(topic, forUser, map[string]any{"DelId": delID}) if err != nil { return err } } return err } // GetAll returns multiple messages. func (messagesMapper) GetAll(topic string, forUser types.Uid, opt *types.QueryOpt) ([]types.Message, error) { return adp.MessageGetAll(topic, forUser, opt) } // GetDeleted returns the ranges of deleted messages and the largest DelId reported in the list. func (messagesMapper) GetDeleted(topic string, forUser types.Uid, opt *types.QueryOpt) ([]types.Range, int, error) { dmsgs, err := adp.MessageGetDeleted(topic, forUser, opt) if err != nil { return nil, 0, err } var ranges []types.Range var maxID int // Flatten out the ranges for i := range dmsgs { dm := &dmsgs[i] if dm.DelId > maxID { maxID = dm.DelId } ranges = append(ranges, dm.SeqIdRanges...) } sort.Sort(types.RangeSorter(ranges)) ranges = types.RangeSorter(ranges).Normalize() return ranges, maxID, nil } // Registered authentication handlers. var authHandlers map[string]auth.AuthHandler // Logical auth handler names var authHandlerNames map[string]string // RegisterAuthScheme registers an authentication scheme handler. // The 'name' must be the hardcoded name, NOT the logical name. func RegisterAuthScheme(name string, handler auth.AuthHandler) { if name == "" { panic("RegisterAuthScheme: empty auth scheme name") } if handler == nil { panic("RegisterAuthScheme: scheme handler is nil") } name = strings.ToLower(name) if authHandlers == nil { authHandlers = make(map[string]auth.AuthHandler) } if _, dup := authHandlers[name]; dup { panic("RegisterAuthScheme: called twice for scheme " + name) } authHandlers[name] = handler } // GetAuthNames returns all addressable auth handler names, logical and hardcoded // excluding those which are disabled like "basic:". func (s storeObj) GetAuthNames() []string { if len(authHandlers) == 0 { return nil } allNames := make(map[string]struct{}) for name := range authHandlers { allNames[name] = struct{}{} } for name := range authHandlerNames { allNames[name] = struct{}{} } var names []string for name := range allNames { if s.GetLogicalAuthHandler(name) != nil { names = append(names, name) } } return names } // GetAuthHandler returns an auth handler by actual hardcoded name irrspectful of logical naming. func (storeObj) GetAuthHandler(name string) auth.AuthHandler { return authHandlers[strings.ToLower(name)] } // GetLogicalAuthHandler returns an auth handler by logical name. If there is no handler by that // logical name it tries to find one by the hardcoded name. func (storeObj) GetLogicalAuthHandler(name string) auth.AuthHandler { name = strings.ToLower(name) if len(authHandlerNames) != 0 { if lname, ok := authHandlerNames[name]; ok { return authHandlers[lname] } } return authHandlers[name] } // InitAuthLogicalNames initializes authentication mapping "logical handler name":"actual handler name". // Logical name must not be empty, actual name could be an empty string. func InitAuthLogicalNames(config json.RawMessage) error { if config == nil || string(config) == "null" { return nil } var mapping []string if err := json.Unmarshal(config, &mapping); err != nil { return errors.New("store: failed to parse logical auth names: " + err.Error() + "(" + string(config) + ")") } if len(mapping) == 0 { return nil } if authHandlerNames == nil { authHandlerNames = make(map[string]string) } for _, pair := range mapping { if parts := strings.Split(pair, ":"); len(parts) == 2 { if parts[0] == "" { return errors.New("store: empty logical auth name '" + pair + "'") } parts[0] = strings.ToLower(parts[0]) if _, ok := authHandlerNames[parts[0]]; ok { return errors.New("store: duplicate mapping for logical auth name '" + pair + "'") } parts[1] = strings.ToLower(parts[1]) if parts[1] != "" { if _, ok := authHandlers[parts[1]]; !ok { return errors.New("store: unknown handler for logical auth name '" + pair + "'") } } if parts[0] == parts[1] { // Skip useless identity mapping. continue } authHandlerNames[parts[0]] = parts[1] } else { return errors.New("store: invalid logical auth mapping '" + pair + "'") } } return nil } // Registered authentication handlers. var validators map[string]validate.Validator // RegisterValidator registers validation scheme. func RegisterValidator(name string, v validate.Validator) { name = strings.ToLower(name) if validators == nil { validators = make(map[string]validate.Validator) } if v == nil { panic("RegisterValidator: validator is nil") } if _, dup := validators[name]; dup { panic("RegisterValidator: called twice for validator " + name) } validators[name] = v } // GetValidator returns registered validator by name. func (storeObj) GetValidator(name string) validate.Validator { return validators[strings.ToLower(name)] } // DevicePersistenceInterface is an interface which defines methods used for handling device IDs. // Mostly used to generate push notifications. type DevicePersistenceInterface interface { Update(uid types.Uid, oldDeviceID string, dev *types.DeviceDef) error GetAll(uid ...types.Uid) (map[types.Uid][]types.DeviceDef, int, error) Delete(uid types.Uid, deviceID string) error } // deviceMapper is a concrete type implementing DevicePersistenceInterface. type deviceMapper struct{} // Devices is a singleton instance of DevicePersistenceInterface to map methods to. var Devices DevicePersistenceInterface // Update updates a device record. func (deviceMapper) Update(uid types.Uid, oldDeviceID string, dev *types.DeviceDef) error { // If the old device Id is specified and it's different from the new ID, delete the old id if oldDeviceID != "" && (dev == nil || dev.DeviceId != oldDeviceID) { if err := adp.DeviceDelete(uid, oldDeviceID); err != nil { return err } } // Insert or update the new DeviceId if one is given. if dev != nil && dev.DeviceId != "" { return adp.DeviceUpsert(uid, dev) } return nil } // GetAll returns all known device IDs for a given list of user IDs. // The second return parameter is the count of found device IDs. func (deviceMapper) GetAll(uid ...types.Uid) (map[types.Uid][]types.DeviceDef, int, error) { return adp.DeviceGetAll(uid...) } // Delete deletes device record for a given user. func (deviceMapper) Delete(uid types.Uid, deviceID string) error { return adp.DeviceDelete(uid, deviceID) } // Registered media/file handlers. var fileHandlers map[string]media.Handler // RegisterMediaHandler saves reference to a media handler (file upload-download handler). func RegisterMediaHandler(name string, mh media.Handler) { if fileHandlers == nil { fileHandlers = make(map[string]media.Handler) } if mh == nil { panic("RegisterMediaHandler: handler is nil") } if _, dup := fileHandlers[name]; dup { panic("RegisterMediaHandler: called twice for handler " + name) } fileHandlers[name] = mh } // GetMediaHandler returns default media handler. func (storeObj) GetMediaHandler() media.Handler { return mediaHandler } // UseMediaHandler sets specified media handler as default. func (storeObj) UseMediaHandler(name, config string) error { mediaHandler = fileHandlers[name] if mediaHandler == nil { panic("UseMediaHandler: unknown handler '" + name + "'") } return mediaHandler.Init(config) } // FilePersistenceInterface is an interface wchich defines methods used for file handling (records or uploaded files). type FilePersistenceInterface interface { // StartUpload records that the given user initiated a file upload StartUpload(fd *types.FileDef) error // FinishUpload marks started upload as successfully finished. FinishUpload(fd *types.FileDef, success bool, size int64) (*types.FileDef, error) // Get fetches a file record for a unique file id. Get(fid string) (*types.FileDef, error) // DeleteUnused removes unused attachments. DeleteUnused(olderThan time.Time, limit int) error // LinkAttachments connects earlier uploaded attachments to a message or topic to prevent it // from being garbage collected. LinkAttachments(topic string, msgId types.Uid, attachments []string) error } // fileMapper is concrete type which implements FilePersistenceInterface. type fileMapper struct{} // Files is a sigleton instance of FilePersistenceInterface to be used for handling file uploads. var Files FilePersistenceInterface // StartUpload records that the given user initiated a file upload func (fileMapper) StartUpload(fd *types.FileDef) error { fd.Status = types.UploadStarted return adp.FileStartUpload(fd) } // FinishUpload marks started upload as successfully finished or failed. func (fileMapper) FinishUpload(fd *types.FileDef, success bool, size int64) (*types.FileDef, error) { return adp.FileFinishUpload(fd, success, size) } // Get fetches a file record for a unique file id. func (fileMapper) Get(fid string) (*types.FileDef, error) { return adp.FileGet(fid) } // DeleteUnused removes unused attachments and avatars. func (fileMapper) DeleteUnused(olderThan time.Time, limit int) error { toDel, err := adp.FileDeleteUnused(olderThan, limit) if err != nil { return err } if len(toDel) > 0 { logs.Warn.Println("deleting media", toDel) return Store.GetMediaHandler().Delete(toDel) } return nil } // LinkAttachments connects earlier uploaded attachments to a message or topic to prevent it // from being garbage collected. func (fileMapper) LinkAttachments(topic string, msgId types.Uid, attachments []string) error { // Convert attachment URLs to file IDs. var fids []string for _, url := range attachments { if fid := mediaHandler.GetIdFromUrl(url); !fid.IsZero() { fids = append(fids, fid.String()) } } if len(fids) > 0 { userId := types.ZeroUid if types.GetTopicCat(topic) == types.TopicCatMe { userId = types.ParseUserId(topic) topic = "" } return adp.FileLinkAttachments(topic, userId, msgId, fids) } return nil } // PersistentCacheInterface is an interface which defines methods used for accessing persistent key-value cache. type PersistentCacheInterface interface { // Get reads a persistent cache entry. Get(key string) (string, error) // Upsert creates or updates a persistent cache entry. Upsert(key string, value string, failOnDuplicate bool) error // Delete deletes a single persistent cache entry. Delete(key string) error // Expire expires older entries with the specified key prefix. Expire(keyPrefix string, olderThan time.Time) error } // pcacheMapper is concrete type which implements PersistentCacheInterface. type pcacheMapper struct{} var PCache PersistentCacheInterface // Get reads a persistent cache entry. func (pcacheMapper) Get(key string) (string, error) { return adp.PCacheGet(key) } // Upsert creates or updates a persistent cache entry. func (pcacheMapper) Upsert(key string, value string, failOnDuplicate bool) error { return adp.PCacheUpsert(key, value, failOnDuplicate) } // Delete deletes a single persistent cache entry. func (pcacheMapper) Delete(key string) error { return adp.PCacheDelete(key) } // Expire expires older entries with the specified key prefix. func (pcacheMapper) Expire(keyPrefix string, olderThan time.Time) error { return adp.PCacheExpire(keyPrefix, olderThan) } func SetTestUidGenerator(g types.UidGenerator) { uGen = g } func init() { Store = storeObj{} Users = usersMapper{} Topics = topicsMapper{} Subs = subsMapper{} Messages = messagesMapper{} Devices = deviceMapper{} Files = fileMapper{} PCache = pcacheMapper{} } ================================================ FILE: server/store/types/types.go ================================================ // Package types provides data types for persisting objects in the databases. package types import ( "database/sql/driver" "encoding/base32" "encoding/base64" "encoding/binary" "encoding/json" "errors" "slices" "sort" "strings" "time" ) // StoreError satisfies Error interface but allows constant values for // direct comparison. type StoreError string // Error is required by error interface. func (s StoreError) Error() string { return string(s) } const ( // ErrInternal means DB or other internal failure. ErrInternal = StoreError("internal") // ErrMalformed means the secret cannot be parsed or otherwise wrong. ErrMalformed = StoreError("malformed") // ErrFailed means authentication failed (wrong login or password, etc). ErrFailed = StoreError("failed") // ErrDuplicate means duplicate credential, i.e. non-unique login. ErrDuplicate = StoreError("duplicate value") // ErrUnsupported means an operation is not supported. ErrUnsupported = StoreError("unsupported") // ErrExpired means the secret has expired. ErrExpired = StoreError("expired") // ErrPolicy means policy violation, e.g. password too weak. ErrPolicy = StoreError("policy") // ErrCredentials means credentials like email or captcha must be validated. ErrCredentials = StoreError("credentials") // ErrUserNotFound means the user was not found. ErrUserNotFound = StoreError("user not found") // ErrTopicNotFound means the topic was not found. ErrTopicNotFound = StoreError("topic not found") // ErrNotFound means the object other then user or topic was not found. ErrNotFound = StoreError("not found") // ErrPermissionDenied means the operation is not permitted. ErrPermissionDenied = StoreError("denied") // ErrInvalidResponse means the client's response does not match server's expectation. ErrInvalidResponse = StoreError("invalid response") // ErrRedirected means the subscription request was redirected to another topic. ErrRedirected = StoreError("redirected") ) // Uid is a database-specific record id, suitable to be used as a primary key. type Uid uint64 // ZeroUid is a constant representing uninitialized Uid. const ZeroUid Uid = 0 // NullValue is a Unicode DEL character which indicated that the value is being deleted. const NullValue = "\u2421" // Lengths of various Uid representations. const ( uidBase64Unpadded = 11 p2pBase64Unpadded = 22 ) // IsZero checks if Uid is uninitialized. func (uid Uid) IsZero() bool { return uid == ZeroUid } // Compare returns 0 if uid is equal to u2, 1 if u2 is greater than uid, -1 if u2 is smaller. func (uid Uid) Compare(u2 Uid) int { if uid < u2 { return -1 } else if uid > u2 { return 1 } return 0 } // MarshalBinary converts Uid to byte slice. func (uid Uid) MarshalBinary() ([]byte, error) { dst := make([]byte, 8) binary.LittleEndian.PutUint64(dst, uint64(uid)) return dst, nil } // UnmarshalBinary reads Uid from byte slice. func (uid *Uid) UnmarshalBinary(b []byte) error { if len(b) < 8 { return errors.New("Uid.UnmarshalBinary: invalid length") } *uid = Uid(binary.LittleEndian.Uint64(b)) return nil } // UnmarshalText reads Uid from string represented as byte slice. func (uid *Uid) UnmarshalText(src []byte) error { if len(src) != uidBase64Unpadded { return errors.New("Uid.UnmarshalText: invalid length") } dec := make([]byte, base64.URLEncoding.WithPadding(base64.NoPadding).DecodedLen(uidBase64Unpadded)) count, err := base64.URLEncoding.WithPadding(base64.NoPadding).Decode(dec, src) if count < 8 { if err != nil { return errors.New("Uid.UnmarshalText: failed to decode " + err.Error()) } return errors.New("Uid.UnmarshalText: failed to decode") } *uid = Uid(binary.LittleEndian.Uint64(dec)) return nil } // MarshalText converts Uid to string represented as byte slice. func (uid *Uid) MarshalText() ([]byte, error) { if *uid == ZeroUid { return []byte{}, nil } src := make([]byte, 8) dst := make([]byte, base64.URLEncoding.WithPadding(base64.NoPadding).EncodedLen(8)) binary.LittleEndian.PutUint64(src, uint64(*uid)) base64.URLEncoding.WithPadding(base64.NoPadding).Encode(dst, src) return dst, nil } // MarshalJSON converts Uid to double quoted ("ajjj") string. func (uid *Uid) MarshalJSON() ([]byte, error) { dst, _ := uid.MarshalText() return append(append([]byte{'"'}, dst...), '"'), nil } // UnmarshalJSON reads Uid from a double quoted string. func (uid *Uid) UnmarshalJSON(b []byte) error { size := len(b) if size != (uidBase64Unpadded + 2) { return errors.New("Uid.UnmarshalJSON: invalid length") } else if b[0] != '"' || b[size-1] != '"' { return errors.New("Uid.UnmarshalJSON: unrecognized") } return uid.UnmarshalText(b[1 : size-1]) } // String converts Uid to base64 string. func (uid Uid) String() string { buf, _ := uid.MarshalText() return string(buf) } // String32 converts Uid to lowercase base32 string (suitable for file names on Windows). func (uid Uid) String32() string { data, _ := uid.MarshalBinary() return strings.ToLower(base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(data)) } // ParseUid parses string NOT prefixed with anything. func ParseUid(s string) Uid { var uid Uid uid.UnmarshalText([]byte(s)) return uid } // ParseUid32 parses base32-encoded string into Uid. func ParseUid32(s string) Uid { var uid Uid if data, err := base32.StdEncoding.WithPadding(base32.NoPadding).DecodeString(s); err == nil { uid.UnmarshalBinary(data) } return uid } // UserId converts Uid to string prefixed with 'usr', like usrXXXXX. func (uid Uid) UserId() string { return uid.PrefixId("usr") } // FndName generates 'fnd' topic name for the given Uid. func (uid Uid) FndName() string { return uid.PrefixId("fnd") } // SlfName generates 'slf' topic name for the given Uid. func (uid Uid) SlfName() string { return uid.PrefixId("slf") } // PrefixId converts Uid to string prefixed with the given prefix. func (uid Uid) PrefixId(prefix string) string { if uid.IsZero() { return "" } return prefix + uid.String() } // ParseUserId parses user ID of the form "usrXXXXXX". func ParseUserId(s string) Uid { var uid Uid if strings.HasPrefix(s, "usr") { (&uid).UnmarshalText([]byte(s)[3:]) } return uid } // GrpToChn converts group topic name to corresponding channel name. // If it's a non-group channel topic, the name is returned unchanged. // If it's neither, an empty string is returned. func GrpToChn(grp string) string { if strings.HasPrefix(grp, "grp") { return strings.Replace(grp, "grp", "chn", 1) } // Return unchanged if it's a channel already. if strings.HasPrefix(grp, "chn") { return grp } return "" } // IsChannel checks if the given topic name is a reference to a channel. // The "nch" should not be considered a channel reference because it can only be used by the topic owner at the time of // group topic creation. func IsChannel(name string) bool { return strings.HasPrefix(name, "chn") } // ChnToGrp gets group topic name from channel name. // If it's a non-channel group topic, the name is returned unchanged. // If it's neither, an empty string is returned. func ChnToGrp(chn string) string { if strings.HasPrefix(chn, "chn") { return strings.Replace(chn, "chn", "grp", 1) } // Return unchanged if it's a group already. if strings.HasPrefix(chn, "grp") { return chn } return "" } // UidSlice is a slice of Uids sorted in ascending order. type UidSlice []Uid func (us UidSlice) find(uid Uid) (int, bool) { l := len(us) if l == 0 || us[0] > uid { return 0, false } if uid > us[l-1] { return l, false } idx := sort.Search(l, func(i int) bool { return uid <= us[i] }) return idx, idx < l && us[idx] == uid } // Add uid to UidSlice keeping it sorted. Duplicates are ignored. func (us *UidSlice) Add(uid Uid) bool { idx, found := us.find(uid) if found { return false } // Inserting without creating a temporary slice. *us = append(*us, ZeroUid) copy((*us)[idx+1:], (*us)[idx:]) (*us)[idx] = uid return true } // Rem removes uid from UidSlice. func (us *UidSlice) Rem(uid Uid) bool { idx, found := us.find(uid) if !found { return false } if idx == len(*us)-1 { *us = (*us)[:idx] } else { *us = slices.Delete((*us), idx, idx+1) } return true } // Contains checks if the UidSlice contains the given UID. func (us UidSlice) Contains(uid Uid) bool { _, contains := us.find(uid) return contains } // P2PName takes two Uids and generates a P2P topic name. func (uid Uid) P2PName(u2 Uid) string { if !uid.IsZero() && !u2.IsZero() { b1, _ := uid.MarshalBinary() b2, _ := u2.MarshalBinary() if uid < u2 { b1 = append(b1, b2...) } else if uid > u2 { b1 = append(b2, b1...) } else { // Explicitly disable P2P with self return "" } return "p2p" + base64.URLEncoding.WithPadding(base64.NoPadding).EncodeToString(b1) } return "" } // ParseP2P extracts uids from the name of a p2p topic. func ParseP2P(p2p string) (uid1, uid2 Uid, err error) { if strings.HasPrefix(p2p, "p2p") { src := []byte(p2p)[3:] if len(src) != p2pBase64Unpadded { err = errors.New("ParseP2P: invalid length") return } dec := make([]byte, base64.URLEncoding.WithPadding(base64.NoPadding).DecodedLen(p2pBase64Unpadded)) var count int count, err = base64.URLEncoding.WithPadding(base64.NoPadding).Decode(dec, src) if count < 16 { if err != nil { err = errors.New("ParseP2P: failed to decode " + err.Error()) } else { err = errors.New("ParseP2P: invalid decoded length") } return } uid1 = Uid(binary.LittleEndian.Uint64(dec)) uid2 = Uid(binary.LittleEndian.Uint64(dec[8:])) } else { err = errors.New("ParseP2P: missing or invalid prefix") } return } // P2PNameForUser takes a user ID and a full name of a P2P topic and generates the name of the // P2P topic as it should be seen by the given user. func P2PNameForUser(uid Uid, p2p string) (string, error) { uid1, uid2, err := ParseP2P(p2p) if err != nil { return "", err } if uid.Compare(uid1) == 0 { return uid2.UserId(), nil } return uid1.UserId(), nil } // ObjHeader is the header shared by all stored objects. type ObjHeader struct { // using string to get around rethinkdb's problems with uint64; // `bson:"_id"` tag is for mongodb to use as primary key '_id'. Id string `bson:"_id"` id Uid CreatedAt time.Time UpdatedAt time.Time } // Uid assigns Uid header field. func (h *ObjHeader) Uid() Uid { if h.id.IsZero() && h.Id != "" { h.id.UnmarshalText([]byte(h.Id)) } return h.id } // SetUid assigns given Uid to appropriate header fields. func (h *ObjHeader) SetUid(uid Uid) { h.id = uid h.Id = uid.String() } // TimeNow returns current wall time in UTC rounded to milliseconds. func TimeNow() time.Time { return time.Now().UTC().Round(time.Millisecond) } // TimeFormatRFC3339 is a format string for writing timestamps as RFC3339. const TimeFormatRFC3339 = "2006-01-02T15:04:05.999" // InitTimes initializes time.Time variables in the header to current time. func (h *ObjHeader) InitTimes() { if h.CreatedAt.IsZero() { h.CreatedAt = TimeNow() } h.UpdatedAt = h.CreatedAt } // MergeTimes intelligently copies time.Time variables from h2 to h. func (h *ObjHeader) MergeTimes(h2 *ObjHeader) { // Set the creation time to the earliest value if h.CreatedAt.IsZero() || (!h2.CreatedAt.IsZero() && h2.CreatedAt.Before(h.CreatedAt)) { h.CreatedAt = h2.CreatedAt } // Set the update time to the latest value if h.UpdatedAt.Before(h2.UpdatedAt) { h.UpdatedAt = h2.UpdatedAt } } // StringSlice is defined so Scanner and Valuer can be attached to it. type StringSlice []string // Scan implements sql.Scanner interface. func (ss *StringSlice) Scan(val any) error { if val == nil { return nil } return json.Unmarshal(val.([]byte), ss) } // Value implements sql/driver.Valuer interface. func (ss StringSlice) Value() (driver.Value, error) { return json.Marshal(ss) } // ObjState represents information on objects state, // such as an indication that User or Topic is suspended/soft-deleted. type ObjState int const ( // StateOK indicates normal user or topic. StateOK ObjState = 0 // StateSuspended indicates suspended user or topic. StateSuspended ObjState = 10 // StateDeleted indicates soft-deleted user or topic. StateDeleted ObjState = 20 // StateUndefined indicates state which has not been set explicitly. StateUndefined ObjState = 30 ) // String returns string representation of ObjState. func (os ObjState) String() string { switch os { case StateOK: return "ok" case StateSuspended: return "susp" case StateDeleted: return "del" case StateUndefined: return "undef" } return "" } // NewObjState parses string into an ObjState. func NewObjState(in string) (ObjState, error) { in = strings.ToLower(in) switch in { case "", "ok": return StateOK, nil case "susp": return StateSuspended, nil case "del": return StateDeleted, nil case "undef": return StateUndefined, nil } // This is the default. return StateOK, errors.New("failed to parse object state") } // MarshalJSON converts ObjState to a quoted string. func (os ObjState) MarshalJSON() ([]byte, error) { return append(append([]byte{'"'}, []byte(os.String())...), '"'), nil } // UnmarshalJSON reads ObjState from a quoted string. func (os *ObjState) UnmarshalJSON(b []byte) error { if b[0] != '"' || b[len(b)-1] != '"' { return errors.New("syntax error") } state, err := NewObjState(string(b[1 : len(b)-1])) if err == nil { *os = state } return err } // Scan is an implementation of sql.Scanner interface. It expects the // value to be a byte slice representation of an ASCII string. func (os *ObjState) Scan(val any) error { switch intval := val.(type) { case int64: *os = ObjState(intval) return nil } return errors.New("data is not an int64") } // Value is an implementation of sql.driver.Valuer interface. func (os ObjState) Value() (driver.Value, error) { return int64(os), nil } // User is a representation of a DB-stored user record. type User struct { ObjHeader `bson:",inline"` State ObjState StateAt *time.Time `json:"StateAt,omitempty" bson:",omitempty"` // Default access to user for P2P topics (used as default modeGiven) Access DefaultAccess // Values for 'me' topic: // Last time when the user joined 'me' topic, by User Agent LastSeen *time.Time // User agent provided when accessing the topic last time UserAgent string Public any Trusted any // Unique indexed tags (email, phone) for finding this user. Stored on the // 'users' as well as indexed in 'tagunique' Tags StringSlice // Info on known devices, used for push notifications Devices map[string]*DeviceDef `bson:"__devices,skip,omitempty"` // Same for mongodb scheme. Ignore in other db backends if its not suitable. DeviceArray []*DeviceDef `json:"-" bson:"devices"` } // AccessMode is a definition of access mode bits. type AccessMode uint // Various access mode constants. const ( ModeJoin AccessMode = 1 << iota // user can join, i.e. {sub} (J:1) ModeRead // user can receive broadcasts ({data}, {info}) (R:2) ModeWrite // user can Write, i.e. {pub} (W:4) ModePres // user can receive presence updates (P:8) ModeApprove // user can approve new members or evict existing members (A:0x10, 16) ModeShare // user can invite new members (S:0x20, 32) ModeDelete // user can hard-delete messages (D:0x40, 64) ModeOwner // user is the owner (O:0x80, 128) - full access ModeUnset // Non-zero value to indicate unknown or undefined mode (:0x100, 256), to make it different from ModeNone. ModeNone AccessMode = 0 // No access, requests to gain access are processed normally (N:0) // Normal user's access to a topic ("JRWPS", 47, 0x2F). ModeCPublic AccessMode = ModeJoin | ModeRead | ModeWrite | ModePres | ModeShare // User's subscription to 'me' and 'fnd' ("JPS", 41, 0x29). ModeCMeFnd AccessMode = ModeJoin | ModePres | ModeShare // User's subscription to 'slf' topic ("JRWDO", 199, 0xC7). ModeCSelf = ModeJoin | ModeRead | ModeWrite | ModeDelete | ModeOwner // Owner's subscription to a generic topic ("JRWPASDO", 255, 0xFF). ModeCFull AccessMode = ModeJoin | ModeRead | ModeWrite | ModePres | ModeApprove | ModeShare | ModeDelete | ModeOwner // Default P2P access mode ("JRWPA", 31, 0x1F). ModeCP2P AccessMode = ModeJoin | ModeRead | ModeWrite | ModePres | ModeApprove // P2P acess mode when hard-deleting messages is enabled ("JRWPAD", 95, 0x5F) ModeCP2PD AccessMode = ModeJoin | ModeRead | ModeWrite | ModePres | ModeApprove | ModeDelete // Default Auth access mode for a user ("JRWPAS", 63, 0x3F). ModeCAuth AccessMode = ModeCP2P | ModeCPublic // Read-only access to topic ("JR", 3). ModeCReadOnly = ModeJoin | ModeRead // Access to 'sys' topic by a root user ("JRWPD", 79, 0x4F). ModeCSys = ModeJoin | ModeRead | ModeWrite | ModePres | ModeDelete // Channel publisher: person authorized to publish content; no J: by invitation only ("RWPD", 78, 0x4E). ModeCChnWriter = ModeRead | ModeWrite | ModePres | ModeShare // Reader's access mode to a channel (JRP, 11, 0xB). ModeCChnReader = ModeJoin | ModeRead | ModePres // Admin: user who can modify access mode ("OA", dec: 144, hex: 0x90). ModeCAdmin = ModeOwner | ModeApprove // Sharer: flags which define user who can be notified of access mode changes ("OAS", dec: 176, hex: 0xB0). ModeCSharer = ModeCAdmin | ModeShare // Invalid mode to indicate an error. ModeInvalid AccessMode = 0x100000 // All possible valid bits (excluding ModeInvalid and ModeUnset) = 0xFF, 255. ModeBitmask AccessMode = ModeJoin | ModeRead | ModeWrite | ModePres | ModeApprove | ModeShare | ModeDelete | ModeOwner ) // MarshalText converts AccessMode to ASCII byte slice. func (m AccessMode) MarshalText() ([]byte, error) { if m == ModeNone { return []byte{'N'}, nil } if m == ModeInvalid { return nil, errors.New("AccessMode invalid") } res := []byte{} modes := []byte{'J', 'R', 'W', 'P', 'A', 'S', 'D', 'O'} for i, chr := range modes { if (m & (1 << uint(i))) != 0 { res = append(res, chr) } } return res, nil } // ParseAcs parses AccessMode from a byte array. func ParseAcs(b []byte) (AccessMode, error) { m0 := ModeUnset Loop: for i := range b { switch b[i] { case 'J', 'j': m0 |= ModeJoin case 'R', 'r': m0 |= ModeRead case 'W', 'w': m0 |= ModeWrite case 'A', 'a': m0 |= ModeApprove case 'S', 's': m0 |= ModeShare case 'D', 'd': m0 |= ModeDelete case 'P', 'p': m0 |= ModePres case 'O', 'o': m0 |= ModeOwner case 'N', 'n': if m0 != ModeUnset { return ModeUnset, errors.New("AccessMode: access N cannot be combined with any other") } m0 = ModeNone // N means explicitly no access, all bits cleared break Loop default: return ModeUnset, errors.New("AccessMode: invalid character '" + string(b[i]) + "'") } } return m0, nil } // UnmarshalText parses access mode string as byte slice. // Does not change the mode if the string is empty or invalid. func (m *AccessMode) UnmarshalText(b []byte) error { m0, err := ParseAcs(b) if err != nil { return err } if m0 != ModeUnset { *m = (m0 & ModeBitmask) } return nil } // String returns string representation of AccessMode. func (m AccessMode) String() string { res, err := m.MarshalText() if err != nil { return "" } return string(res) } // MarshalJSON converts AccessMode to a quoted string. func (m AccessMode) MarshalJSON() ([]byte, error) { res, err := m.MarshalText() if err != nil { return nil, err } return append(append([]byte{'"'}, res...), '"'), nil } // UnmarshalJSON reads AccessMode from a quoted string. func (m *AccessMode) UnmarshalJSON(b []byte) error { if b[0] != '"' || b[len(b)-1] != '"' { return errors.New("syntax error") } return m.UnmarshalText(b[1 : len(b)-1]) } // Scan is an implementation of sql.Scanner interface. It expects the // value to be a byte slice representation of an ASCII string. func (m *AccessMode) Scan(val any) error { if bb, ok := val.([]byte); ok { return m.UnmarshalText(bb) } return errors.New("scan failed: data is not a byte slice") } // Value is an implementation of sql.driver.Valuer interface. func (m AccessMode) Value() (driver.Value, error) { res, err := m.MarshalText() if err != nil { return "", err } return string(res), nil } // BetterThan checks if grant mode allows more permissions than requested in want mode. func (grant AccessMode) BetterThan(want AccessMode) bool { return ModeBitmask&grant&^want != 0 } // BetterEqual checks if grant mode allows all permissions requested in want mode. func (grant AccessMode) BetterEqual(want AccessMode) bool { return ModeBitmask&grant&want == want } // Delta between two modes as a string old.Delta(new). JRPAS -> JRWS: "+W-PA" // Zero delta is an empty string "" func (o AccessMode) Delta(n AccessMode) string { // Removed bits, bits present in 'old' but missing in 'new' -> '-' var removed string if o2n := ModeBitmask & o &^ n; o2n > 0 { removed = o2n.String() if removed != "" { removed = "-" + removed } } // Added bits, bits present in 'n' but missing in 'o' -> '+' var added string if n2o := ModeBitmask & n &^ o; n2o > 0 { added = n2o.String() if added != "" { added = "+" + added } } return added + removed } // ApplyMutation sets of modifies access mode: // * if `mutation` contains either '+' or '-', attempts to apply a delta change on `m`. // * otherwise, treats it as an assignment. func (m *AccessMode) ApplyMutation(mutation string) error { if mutation == "" { return nil } if strings.ContainsAny(mutation, "+-") { return m.ApplyDelta(mutation) } return m.UnmarshalText([]byte(mutation)) } // ApplyDelta applies the acs delta to AccessMode. // Delta is in the same format as generated by AccessMode.Delta. // E.g. JPRA.ApplyDelta(-PR+W) -> JWA. func (m *AccessMode) ApplyDelta(delta string) error { if delta == "" || delta == "N" { // No updates. return nil } m0 := *m for next := 0; next+1 < len(delta) && next >= 0; { ch := delta[next] end := strings.IndexAny(delta[next+1:], "+-") var chunk string if end >= 0 { end += next + 1 chunk = delta[next+1 : end] } else { chunk = delta[next+1:] } next = end upd, err := ParseAcs([]byte(chunk)) if err != nil { return err } switch ch { case '+': if upd != ModeUnset { m0 |= upd & ModeBitmask } case '-': if upd != ModeUnset { m0 &^= upd & ModeBitmask } default: return errors.New("Invalid acs delta string: '" + delta + "'") } } *m = m0 return nil } // IsJoiner checks if joiner flag J is set. func (m AccessMode) IsJoiner() bool { return m&ModeJoin != 0 } // IsOwner checks if owner bit O is set. func (m AccessMode) IsOwner() bool { return m&ModeOwner != 0 } // IsApprover checks if approver A bit is set. func (m AccessMode) IsApprover() bool { return m&ModeApprove != 0 } // IsAdmin check if owner O or approver A flag is set. func (m AccessMode) IsAdmin() bool { return m.IsOwner() || m.IsApprover() } // IsSharer checks if approver A or sharer S or owner O flag is set. func (m AccessMode) IsSharer() bool { return m.IsAdmin() || (m&ModeShare != 0) } // IsWriter checks if allowed to publish (writer flag W is set). func (m AccessMode) IsWriter() bool { return m&ModeWrite != 0 } // IsReader checks if reader flag R is set. func (m AccessMode) IsReader() bool { return m&ModeRead != 0 } // IsPresencer checks if user receives presence updates (P flag set). func (m AccessMode) IsPresencer() bool { return m&ModePres != 0 } // IsDeleter checks if user can hard-delete messages (D flag is set). func (m AccessMode) IsDeleter() bool { return m&ModeDelete != 0 } // IsZero checks if no flags are set. func (m AccessMode) IsZero() bool { return m == ModeNone } // IsInvalid checks if mode is invalid. func (m AccessMode) IsInvalid() bool { return m == ModeInvalid } // IsDefined checks if the mode is defined: not invalid and not unset. // ModeNone is considered to be defined. func (m AccessMode) IsDefined() bool { return m != ModeInvalid && m != ModeUnset } // DefaultAccess is a per-topic default access modes type DefaultAccess struct { Auth AccessMode Anon AccessMode } // Scan is an implementation of Scanner interface so the value can be read from SQL DBs // It assumes the value is serialized and stored as JSON func (da *DefaultAccess) Scan(val any) error { return json.Unmarshal(val.([]byte), da) } // Value implements sql's driver.Valuer interface. func (da DefaultAccess) Value() (driver.Value, error) { return json.Marshal(da) } // Credential hold data needed to validate and check validity of a credential like email or phone. type Credential struct { ObjHeader `bson:",inline"` // Credential owner User string // Verification method (email, tel, captcha, etc) Method string // Credential value - `jdoe@example.com` or `+12345678901` Value string // Expected response Resp string // If credential was successfully confirmed Done bool // Retry count Retries int } // LastSeenUA is a timestamp and a user agent of when the user was last seen. type LastSeenUA struct { // When is the timestamp when the user was last online. When time.Time // UserAgent is the client UA of the last online access. UserAgent string } // Subscription to a topic type Subscription struct { ObjHeader `bson:",inline"` // User who has relationship with the topic User string // Topic subscribed to Topic string DeletedAt *time.Time `bson:",omitempty"` // Values persisted through subscription soft-deletion // ID of the latest Soft-delete operation DelId int // Last SeqId reported by user as received by at least one of his sessions RecvSeqId int // Last SeqID reported read by the user ReadSeqId int // Access mode requested by this user ModeWant AccessMode // Access mode granted to this user ModeGiven AccessMode // User's private data associated with the subscription to topic Private any // Deserialized ephemeral values // Deserialized public value from topic or user (depends on context) // In case of P2P topics this is the Public value of the other user. public any // In case of P2P topics this is the Trusted value of the other user. trusted any // deserialized SeqID from user or topic seqId int // Deserialized TouchedAt from topic touchedAt time.Time // Timestamp & user agent of when the user was last online. lastSeenUA *LastSeenUA // Count of subscribers. subCnt int // P2P only. ID of the other user with string // P2P only. Default access: this is the mode given by the other user to this user modeDefault *DefaultAccess // Topic's or user's state. state ObjState // This is not a fully initialized subscription object dummy bool } // SetPublic assigns a value to `public`, otherwise not accessible from outside the package. func (s *Subscription) SetPublic(pub any) { s.public = pub } // GetPublic reads value of `public`. func (s *Subscription) GetPublic() any { return s.public } // SetTrusted assigns a value to `trusted`, otherwise not accessible from outside the package. func (s *Subscription) SetTrusted(tstd any) { s.trusted = tstd } // GetTrusted reads value of `trusted`. func (s *Subscription) GetTrusted() any { return s.trusted } // SetWith sets other user for P2P subscriptions. func (s *Subscription) SetWith(with string) { s.with = with } // GetWith returns the other user for P2P subscriptions. func (s *Subscription) GetWith() string { return s.with } // GetTouchedAt returns touchedAt. func (s *Subscription) GetTouchedAt() time.Time { return s.touchedAt } // SetTouchedAt sets the value of touchedAt. func (s *Subscription) SetTouchedAt(touchedAt time.Time) { if touchedAt.After(s.touchedAt) { s.touchedAt = touchedAt } } // LastModified returns the greater of either TouchedAt or UpdatedAt. func (s *Subscription) LastModified() time.Time { if s.UpdatedAt.Before(s.touchedAt) { return s.touchedAt } return s.UpdatedAt } // GetSeqId returns seqId. func (s *Subscription) GetSeqId() int { return s.seqId } // SetSeqId sets seqId field. func (s *Subscription) SetSeqId(id int) { s.seqId = id } // GetSubCnt returns subCnt (subscriber count). func (s *Subscription) GetSubCnt() int { return s.subCnt } // SetSubCnt sets subCnt (subscriber count). func (s *Subscription) SetSubCnt(cnt int) { s.subCnt = cnt } // GetLastSeen returns lastSeen. func (s *Subscription) GetLastSeen() *time.Time { if s.lastSeenUA != nil { return &s.lastSeenUA.When } return nil } // GetUserAgent returns userAgent. func (s *Subscription) GetUserAgent() string { if s.lastSeenUA != nil { return s.lastSeenUA.UserAgent } return "" } // SetLastSeenAndUA updates lastSeen time and userAgent. func (s *Subscription) SetLastSeenAndUA(when *time.Time, ua string) { if when != nil && !when.IsZero() { s.lastSeenUA = &LastSeenUA{ When: *when, UserAgent: ua, } } else { s.lastSeenUA = nil } } // SetDefaultAccess updates default access values. func (s *Subscription) SetDefaultAccess(auth, anon AccessMode) { s.modeDefault = &DefaultAccess{auth, anon} } // GetDefaultAccess returns default access. func (s *Subscription) GetDefaultAccess() *DefaultAccess { return s.modeDefault } // GetState returns topic's or user's state. func (s *Subscription) GetState() ObjState { return s.state } // SetState assigns topic's or user's state. func (s *Subscription) SetState(state ObjState) { s.state = state } // SetDummy marks this subscription object as only partially intialized. func (s *Subscription) SetDummy(dummy bool) { s.dummy = dummy } // IsDummy is true if this subscription object as only partially intialized. func (s *Subscription) IsDummy() bool { return s.dummy } // Contact is a result of a search for connections type Contact struct { Id string MatchOn []string Access DefaultAccess LastSeen time.Time Public any } type perUserData struct { private any want AccessMode given AccessMode } // MessageHeaders is needed to attach Scan() to. type KVMap map[string]any // Scan implements sql.Scanner interface. func (kvm *KVMap) Scan(val any) error { if val == nil { kvm = nil return nil } return json.Unmarshal(val.([]byte), kvm) } // Value implements sql's driver.Valuer interface. func (kvm KVMap) Value() (driver.Value, error) { return json.Marshal(kvm) } // Topic stored in database. Topic's name is Id type Topic struct { ObjHeader `bson:",inline"` // State of the topic: normal (ok), suspended, deleted State ObjState StateAt *time.Time `json:"StateAt,omitempty" bson:",omitempty"` // Timestamp when the last message has passed through the topic TouchedAt time.Time // Indicates that the topic is a channel. UseBt bool // Topic owner. Could be zero Owner string // Default access to topic Access DefaultAccess // Server-issued sequential ID SeqId int // If messages were deleted, sequential id of the last operation to delete them DelId int // Count of topic subscribers. SubCnt int Public any Trusted any // Indexed tags for finding this topic. Tags StringSlice // Auxiliary set of key-value pairs. Aux KVMap `json:"Aux,omitempty" bson:",omitempty"` // Deserialized ephemeral params perUser map[Uid]*perUserData // deserialized from Subscription } // GiveAccess updates access mode for the given user. func (t *Topic) GiveAccess(uid Uid, want, given AccessMode) { if t.perUser == nil { t.perUser = make(map[Uid]*perUserData, 1) } pud := t.perUser[uid] if pud == nil { pud = &perUserData{} } pud.want = want pud.given = given t.perUser[uid] = pud if want&given&ModeOwner != 0 && t.Owner == "" { t.Owner = uid.String() } } // SetPrivate updates private value for the given user. func (t *Topic) SetPrivate(uid Uid, private any) { if t.perUser == nil { t.perUser = make(map[Uid]*perUserData, 1) } pud := t.perUser[uid] if pud == nil { pud = &perUserData{} } pud.private = private t.perUser[uid] = pud } // GetPrivate returns given user's private value. func (t *Topic) GetPrivate(uid Uid) (private any) { if t.perUser == nil { return } pud := t.perUser[uid] if pud == nil { return } private = pud.private return } // GetAccess returns given user's access mode. func (t *Topic) GetAccess(uid Uid) (mode AccessMode) { if t.perUser == nil { return } pud := t.perUser[uid] if pud == nil { return } mode = pud.given & pud.want return } // SoftDelete is a single DB record of soft-deletetion. type SoftDelete struct { User string DelId int } // Message is a stored {data} message type Message struct { ObjHeader `bson:",inline"` DeletedAt *time.Time `json:"DeletedAt,omitempty" bson:",omitempty"` // ID of the hard-delete operation DelId int `json:"DelId,omitempty" bson:",omitempty"` // List of users who have marked this message as soft-deleted DeletedFor []SoftDelete `json:"DeletedFor,omitempty" bson:",omitempty"` SeqId int Topic string // Sender's user ID as string (without 'usr' prefix), could be empty. From string Head KVMap `json:"Head,omitempty" bson:",omitempty"` Content any } // Range is a range of message SeqIDs. Low end is inclusive (closed), high end is exclusive (open): [Low, Hi). // If the range contains just one ID, Hi is set to 0 type Range struct { Low int Hi int `json:"Hi,omitempty" bson:",omitempty"` } // RangeSorter is a helper type required by 'sort' package. type RangeSorter []Range // Len is the length of the range. func (rs RangeSorter) Len() int { return len(rs) } // Swap swaps two items in a slice. func (rs RangeSorter) Swap(i, j int) { rs[i], rs[j] = rs[j], rs[i] } // Less is a comparator. Sort by Low ascending, then sort by Hi descending func (rs RangeSorter) Less(i, j int) bool { if rs[i].Low < rs[j].Low { return true } if rs[i].Low == rs[j].Low { return rs[i].Hi >= rs[j].Hi } return false } // Normalize ranges - remove overlaps: [1..4],[2..4],[5..7] -> [1..7]. // The ranges are expected to be sorted. // Ranges are inclusive-inclusive, i.e. [1..3] -> 1, 2, 3. func (rs RangeSorter) Normalize() RangeSorter { if ll := rs.Len(); ll > 1 { prev := 0 for i := 1; i < ll; i++ { if rs[prev].Low == rs[i].Low { // Earlier range is guaranteed to be wider or equal to the later range, // collapse two ranges into one (by doing nothing) continue } // Check for full or partial overlap if rs[prev].Hi > 0 && rs[prev].Hi+1 >= rs[i].Low { // Partial overlap if rs[prev].Hi < rs[i].Hi { rs[prev].Hi = rs[i].Hi } // Otherwise the next range is fully within the previous range, consume it by doing nothing. continue } // No overlap prev++ } rs = rs[:prev+1] } return rs } // Convert a slice of int values to a slice of ranges. // The int slice must be sorted low -> high. func SliceToRanges(in []int) []Range { if len(in) == 0 { return nil } var out []Range for _, id := range in { size := len(out) if size == 0 { out = append(out, Range{Low: id}) continue } prev := &out[size-1] if (prev.Hi == 0 && (id != prev.Low+1)) || (id > prev.Hi) { // New range. out = append(out, Range{Low: id}) } else { // Expand existing range. prev.Hi = id + 1 } } return out } // DelMessage is a log entry of a deleted message range. type DelMessage struct { ObjHeader `bson:",inline"` Topic string DeletedFor string DelId int SeqIdRanges []Range // Delete messages newer than this value. Not serialized. newerThan *time.Time } // GetNewerThan returns a newerThan delete query parameter. func (dm *DelMessage) GetNewerThan() *time.Time { return dm.newerThan } // SetNewerThan sets a newerThan delete query parameter. func (dm *DelMessage) SetNewerThan(t time.Time) { dm.newerThan = &t } // QueryOpt is options of a query, [since, before] - both ends inclusive (closed) type QueryOpt struct { // Subscription query User Uid Topic string IfModifiedSince *time.Time // ID-based query parameters: Messages Since int Before int // Common parameter Limit int // Ranges of IDs. IdRanges []Range } // TopicCat is an enum of topic categories. type TopicCat int const ( // TopicCatMe is a value denoting 'me' topic. TopicCatMe TopicCat = iota // TopicCatFnd is a value denoting 'fnd' topic. TopicCatFnd // TopicCatP2P is a value denoting 'p2p topic. TopicCatP2P // TopicCatGrp is a value denoting group topic. TopicCatGrp // TopicCatSys is a constant indicating a system topic. TopicCatSys // TopicCatSlf si a constant indicating a 'self' topic, i.e. topic for saved messages and notes. TopicCatSlf ) // GetTopicCat given topic name returns topic category. func GetTopicCat(name string) TopicCat { switch name[:3] { case "usr": return TopicCatMe case "p2p": return TopicCatP2P case "grp", "chn": return TopicCatGrp case "fnd": return TopicCatFnd case "sys": return TopicCatSys case "slf": return TopicCatSlf default: panic("invalid topic type for name '" + name + "'") } } // IsEphemeralTopic checks if the topic is ephemeral, i.e. it's a reference to the user, // it's not stored in the 'topics' table like 'me' or 'fnd' topics. func IsEphemeralTopic(topic string) bool { cat := GetTopicCat(topic) return cat == TopicCatMe || cat == TopicCatFnd } // DeviceDef is the data provided by connected device. Used primarily for // push notifications. type DeviceDef struct { // Device registration ID DeviceId string // Device platform (iOS, Android, Web) Platform string // Last logged in LastSeen time.Time // Device language, ISO code Lang string } // Media handling constants const ( // UploadStarted indicates that the upload has started but not finished yet. UploadStarted = iota // UploadCompleted indicates that the upload has completed successfully. UploadCompleted // UploadFailed indicates that the upload has failed. UploadFailed // UploadDeleted indicates that the upload is no longer needed and can be deleted. UploadDeleted ) // FileDef is a stored record of a file upload type FileDef struct { ObjHeader `bson:",inline"` // Status of upload Status int // User who created the file User string // Type of the file. MimeType string // Size of the file in bytes. Size int64 // Internal file location, i.e. path on disk or an S3 blob address. Location string // ETag generated by the file server. ETag string } // FlattenDoubleSlice turns 2d slice into a 1d slice. func FlattenDoubleSlice(data [][]string) []string { var result []string for _, el := range data { result = append(result, el...) } return result } ================================================ FILE: server/store/types/uidgen.go ================================================ package types import ( "encoding/base64" "encoding/binary" "errors" sf "github.com/tinode/snowflake" "golang.org/x/crypto/xtea" ) // UidGenerator holds snowflake and encryption paramenets. // RethinkDB generates UUIDs as primary keys. Using snowflake-generated uint64 instead. type UidGenerator struct { seq *sf.SnowFlake cipher *xtea.Cipher } var ErrUninitialized = errors.New("uninitialized") // Init initialises the Uid generator func (ug *UidGenerator) Init(workerID uint, key []byte) error { var err error if ug.seq == nil { ug.seq, err = sf.NewSnowFlake(uint32(workerID)) } if err == nil && ug.cipher == nil { ug.cipher, err = xtea.NewCipher(key) } return err } // Get generates a unique weakly-encryped random-looking ID. // The Uid is a unit64 with the highest bit possibly set which makes it // incompatible with go's pre-1.9 sql package. func (ug *UidGenerator) Get() Uid { buf, err := getIDBuffer(ug) if err != nil { return ZeroUid } return Uid(binary.LittleEndian.Uint64(buf)) } // GetStr generates the same unique ID as Get then returns it as // base64-encoded string. Slightly more efficient than calling Get() // then base64-encoding the result. func (ug *UidGenerator) GetStr() string { buf, err := getIDBuffer(ug) if err != nil { return "" } return base64.URLEncoding.WithPadding(base64.NoPadding).EncodeToString(buf) } // getIdBuffer returns a byte array holding the Uid bytes func getIDBuffer(ug *UidGenerator) ([]byte, error) { if ug == nil || ug.seq == nil { return nil, ErrUninitialized } var id uint64 var err error if id, err = ug.seq.Next(); err != nil { return nil, err } src := make([]byte, 8) dst := make([]byte, 8) binary.LittleEndian.PutUint64(src, id) ug.cipher.Encrypt(dst, src) return dst, nil } // DecodeUid takes an encrypted Uid and decrypts it into a non-negative int64. // This is needed for go/sql compatibility where uint64 with high bit // set is unsupported and possibly for other uses such as MySQL's recommendation // for sequential primary keys. func (ug *UidGenerator) DecodeUid(uid Uid) int64 { if ug == nil || ug.seq == nil { return 0 } src := make([]byte, 8) dst := make([]byte, 8) binary.LittleEndian.PutUint64(src, uint64(uid)) ug.cipher.Decrypt(dst, src) return int64(binary.LittleEndian.Uint64(dst)) } // EncodeInt64 takes a positive int64 and encrypts it into a Uid. // This is needed for go/sql compatibility where uint64 with high bit // set is unsupported and possibly for other uses such as MySQL's recommendation // for sequential primary keys. func (ug *UidGenerator) EncodeInt64(val int64) Uid { if ug == nil || ug.seq == nil { return ZeroUid } src := make([]byte, 8) dst := make([]byte, 8) binary.LittleEndian.PutUint64(src, uint64(val)) ug.cipher.Encrypt(dst, src) return Uid(binary.LittleEndian.Uint64(dst)) } ================================================ FILE: server/store/types/uidgen_test.go ================================================ package types import ( "encoding/base64" "testing" "time" ) func TestUidGeneratorInit(t *testing.T) { ug := &UidGenerator{} key := []byte("testkey1testkey2") // 16 bytes for XTEA // Test successful initialization err := ug.Init(1, key) if err != nil { t.Fatalf("Expected no error, got %v", err) } if ug.seq == nil { t.Error("Snowflake generator should be initialized") } if ug.cipher == nil { t.Error("Cipher should be initialized") } // Test initialization with different worker ID ug2 := &UidGenerator{} err = ug2.Init(2, key) if err != nil { t.Fatalf("Expected no error, got %v", err) } // Test that already initialized generator doesn't reinitialize oldSeq := ug.seq oldCipher := ug.cipher err = ug.Init(3, key) if err != nil { t.Fatalf("Expected no error, got %v", err) } if ug.seq != oldSeq { t.Error("Snowflake generator should not be reinitialized") } if ug.cipher != oldCipher { t.Error("Cipher should not be reinitialized") } } func TestUidGeneratorInitWithInvalidKey(t *testing.T) { ug := &UidGenerator{} // Test with key that's too short (XTEA requires 16 bytes) shortKey := []byte("short") err := ug.Init(1, shortKey) if err == nil { t.Error("Expected error with short key") } // Test with nil key err = ug.Init(1, nil) if err == nil { t.Error("Expected error with nil key") } } func TestUidGeneratorGet(t *testing.T) { ug := &UidGenerator{} key := []byte("testkey1testkey2") err := ug.Init(1, key) if err != nil { t.Fatalf("Failed to initialize generator: %v", err) } // Test basic UID generation uid1 := ug.Get() if uid1 == ZeroUid { t.Error("Generated UID should not be zero") } uid2 := ug.Get() if uid2 == ZeroUid { t.Error("Generated UID should not be zero") } // UIDs should be unique if uid1 == uid2 { t.Error("Generated UIDs should be unique") } // Test multiple UIDs are unique uids := make(map[Uid]bool) for i := 0; i < 1000; i++ { uid := ug.Get() if uid == ZeroUid { t.Errorf("UID %d should not be zero", i) } if uids[uid] { t.Errorf("Duplicate UID generated: %v", uid) } uids[uid] = true } } func TestUidGeneratorGetWithUninitializedGenerator(t *testing.T) { ug := &UidGenerator{} // Test Get() without initialization uid := ug.Get() if uid != ZeroUid { t.Error("Expected ZeroUid from uninitialized generator") } } func TestUidGeneratorGetStr(t *testing.T) { ug := &UidGenerator{} key := []byte("testkey1testkey2") err := ug.Init(1, key) if err != nil { t.Fatalf("Failed to initialize generator: %v", err) } // Test basic string UID generation uidStr1 := ug.GetStr() if uidStr1 == "" { t.Error("Generated UID string should not be empty") } uidStr2 := ug.GetStr() if uidStr2 == "" { t.Error("Generated UID string should not be empty") } // UID strings should be unique if uidStr1 == uidStr2 { t.Error("Generated UID strings should be unique") } // Test that string is valid base64 _, err = base64.URLEncoding.WithPadding(base64.NoPadding).DecodeString(uidStr1) if err != nil { t.Errorf("Generated UID string should be valid base64: %v", err) } // Test consistency between Get() and GetStr() uidStr := ug.GetStr() // Decode the string and compare with binary UID decoded, err := base64.URLEncoding.WithPadding(base64.NoPadding).DecodeString(uidStr) if err != nil { t.Fatalf("Failed to decode UID string: %v", err) } if len(decoded) != 8 { t.Errorf("Decoded UID should be 8 bytes, got %d", len(decoded)) } } func TestUidGeneratorGetStrWithUninitializedGenerator(t *testing.T) { ug := &UidGenerator{} // Test GetStr() without initialization uidStr := ug.GetStr() if uidStr != "" { t.Error("Expected empty string from uninitialized generator") } } func TestUidGeneratorDecodeUid(t *testing.T) { ug := &UidGenerator{} key := []byte("testkey1testkey2") err := ug.Init(1, key) if err != nil { t.Fatalf("Failed to initialize generator: %v", err) } // Test decoding generated UIDs uid := ug.Get() decoded := ug.DecodeUid(uid) // Decoded value should be non-negative (for SQL compatibility) if decoded < 0 { t.Errorf("Decoded UID should be non-negative, got %d", decoded) } // Test multiple UIDs decode to different values uid1 := ug.Get() uid2 := ug.Get() decoded1 := ug.DecodeUid(uid1) decoded2 := ug.DecodeUid(uid2) if decoded1 == decoded2 { t.Error("Different UIDs should decode to different values") } } func TestUidGeneratorEncodeInt64(t *testing.T) { ug := &UidGenerator{} key := []byte("testkey1testkey2") err := ug.Init(1, key) if err != nil { t.Fatalf("Failed to initialize generator: %v", err) } // Test encoding positive int64 values val1 := int64(12345) uid1 := ug.EncodeInt64(val1) if uid1 == ZeroUid { t.Error("Encoded UID should not be zero for positive value") } val2 := int64(67890) uid2 := ug.EncodeInt64(val2) if uid2 == ZeroUid { t.Error("Encoded UID should not be zero for positive value") } // Different values should encode to different UIDs if uid1 == uid2 { t.Error("Different values should encode to different UIDs") } // Test encoding zero uid0 := ug.EncodeInt64(0) if uid0 == ZeroUid { t.Error("Encoded UID for 0 should not be ZeroUid (due to encryption)") } // Test encoding large values maxVal := int64(9223372036854775807) // max int64 uidMax := ug.EncodeInt64(maxVal) if uidMax == ZeroUid { t.Error("Should be able to encode max int64 value") } } func TestUidGeneratorEncodeDecodeRoundtrip(t *testing.T) { ug := &UidGenerator{} key := []byte("testkey1testkey2") err := ug.Init(1, key) if err != nil { t.Fatalf("Failed to initialize generator: %v", err) } // Test encode/decode roundtrip for various values testValues := []int64{0, 1, 42, 12345, 1000000, 9223372036854775807} for _, val := range testValues { encoded := ug.EncodeInt64(val) decoded := ug.DecodeUid(encoded) if decoded != val { t.Errorf("Roundtrip failed for %d: got %d", val, decoded) } } // Test that generated UIDs can be decoded back to sequential values uid := ug.Get() decoded := ug.DecodeUid(uid) reencoded := ug.EncodeInt64(decoded) if reencoded != uid { t.Error("Generated UID roundtrip failed") } } func TestUidGeneratorConcurrency(t *testing.T) { ug := &UidGenerator{} key := []byte("testkey1testkey2") err := ug.Init(1, key) if err != nil { t.Fatalf("Failed to initialize generator: %v", err) } // Test concurrent UID generation const numGoroutines = 10 const uidsPerGoroutine = 100 uidChan := make(chan Uid, numGoroutines*uidsPerGoroutine) for i := 0; i < numGoroutines; i++ { go func() { for j := 0; j < uidsPerGoroutine; j++ { uid := ug.Get() uidChan <- uid } }() } // Collect all UIDs uids := make(map[Uid]bool) for i := 0; i < numGoroutines*uidsPerGoroutine; i++ { uid := <-uidChan if uid == ZeroUid { t.Error("Generated UID should not be zero") } if uids[uid] { t.Errorf("Duplicate UID generated in concurrent test: %v", uid) } uids[uid] = true } if len(uids) != numGoroutines*uidsPerGoroutine { t.Errorf("Expected %d unique UIDs, got %d", numGoroutines*uidsPerGoroutine, len(uids)) } } func TestUidGeneratorPerformance(t *testing.T) { if testing.Short() { t.Skip("Skipping performance test in short mode") } ug := &UidGenerator{} key := []byte("testkey1testkey2") err := ug.Init(1, key) if err != nil { t.Fatalf("Failed to initialize generator: %v", err) } // Performance test for Get() start := time.Now() for i := 0; i < 100000; i++ { uid := ug.Get() if uid == ZeroUid { t.Error("Generated UID should not be zero") } } duration := time.Since(start) t.Logf("Generated 100,000 UIDs in %v (%.0f UIDs/sec)", duration, 100000/duration.Seconds()) // Performance test for GetStr() start = time.Now() for i := 0; i < 100000; i++ { uidStr := ug.GetStr() if uidStr == "" { t.Error("Generated UID string should not be empty") } } duration = time.Since(start) t.Logf("Generated 100,000 UID strings in %v (%.0f UIDs/sec)", duration, 100000/duration.Seconds()) } func TestUidGeneratorDifferentWorkerIds(t *testing.T) { key := []byte("testkey1testkey2") // Test that different worker IDs produce different sequences ug1 := &UidGenerator{} ug2 := &UidGenerator{} err1 := ug1.Init(1, key) err2 := ug2.Init(2, key) if err1 != nil || err2 != nil { t.Fatalf("Failed to initialize generators: %v, %v", err1, err2) } // Generate UIDs from both generators uids1 := make([]Uid, 100) uids2 := make([]Uid, 100) for i := 0; i < 100; i++ { uids1[i] = ug1.Get() uids2[i] = ug2.Get() } // Check for uniqueness across generators allUids := make(map[Uid]bool) for _, uid := range uids1 { if allUids[uid] { t.Error("Duplicate UID found across generators") } allUids[uid] = true } for _, uid := range uids2 { if allUids[uid] { t.Error("Duplicate UID found across generators") } allUids[uid] = true } } func TestUidGeneratorInitErrorConditions(t *testing.T) { // Test with invalid worker ID (snowflake has limits) ug := &UidGenerator{} key := []byte("testkey1testkey2") // Test with worker ID that might cause snowflake to fail // Snowflake typically supports worker IDs up to 1023 (10 bits) err := ug.Init(1024, key) // This should potentially fail if err != nil { t.Logf("Expected behavior: worker ID 1024 failed with: %v", err) } // Test with extremely large worker ID ug2 := &UidGenerator{} err = ug2.Init(4294967295, key) // max uint32 if err != nil { t.Logf("Expected behavior: max uint32 worker ID failed with: %v", err) } } func TestUidGeneratorInitKeyValidation(t *testing.T) { // Test with various invalid key lengths testCases := []struct { name string key []byte }{ {"nil key", nil}, {"empty key", []byte{}}, {"too short key", []byte("short")}, {"15 byte key", []byte("testkey1testkey")}, // 15 bytes {"17 byte key", []byte("testkey1testkey22")}, // 17 bytes } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { ug := &UidGenerator{} err := ug.Init(1, tc.key) if err == nil { t.Errorf("Expected error for %s, but got none", tc.name) } }) } } func TestUidGeneratorInitValidKeys(t *testing.T) { // Test with exactly 16 byte key (XTEA requirement) ug := &UidGenerator{} key16 := []byte("testkey1testkey2") // exactly 16 bytes err := ug.Init(1, key16) if err != nil { t.Errorf("16-byte key should work: %v", err) } // Test with different valid 16-byte keys validKeys := [][]byte{ []byte("1234567890123456"), []byte("abcdefghijklmnop"), []byte("\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"), } for i, key := range validKeys { ug := &UidGenerator{} err := ug.Init(uint(i), key) if err != nil { t.Errorf("Valid key %d should work: %v", i, err) } } } func TestUidGeneratorInitPartialFailure(t *testing.T) { // Test scenario where snowflake init succeeds but cipher init fails ug := &UidGenerator{} // First, initialize with valid parameters validKey := []byte("testkey1testkey2") err := ug.Init(1, validKey) if err != nil { t.Fatalf("Initial setup should work: %v", err) } // Now try to init again with invalid key - should not affect existing snowflake oldSeq := ug.seq err = ug.Init(1, []byte("short")) // The snowflake should remain the same (not re-initialized) if ug.seq != oldSeq { t.Error("Snowflake should not be re-initialized on partial failure") } // But cipher might be affected depending on implementation if err != nil { t.Logf("Expected behavior: partial init failed with: %v", err) } } func TestUidGeneratorInitMultipleWorkers(t *testing.T) { key := []byte("testkey1testkey2") generators := make([]*UidGenerator, 10) // Initialize multiple generators with different worker IDs for i := 0; i < 10; i++ { generators[i] = &UidGenerator{} err := generators[i].Init(uint(i), key) if err != nil { t.Errorf("Worker %d initialization failed: %v", i, err) } } // Verify they all generate unique UIDs uids := make(map[Uid]int) // UID -> worker ID for i, gen := range generators { for j := 0; j < 10; j++ { uid := gen.Get() if uid == ZeroUid { t.Errorf("Worker %d generated ZeroUid", i) continue } if existingWorker, exists := uids[uid]; exists { t.Errorf("Duplicate UID %v between workers %d and %d", uid, existingWorker, i) } uids[uid] = i } } } func TestUidGeneratorInitIdempotency(t *testing.T) { ug := &UidGenerator{} key := []byte("testkey1testkey2") // First initialization err1 := ug.Init(1, key) if err1 != nil { t.Fatalf("First init failed: %v", err1) } seq1 := ug.seq cipher1 := ug.cipher // Second initialization with same parameters err2 := ug.Init(1, key) if err2 != nil { t.Errorf("Second init should not fail: %v", err2) } // Should not re-initialize if ug.seq != seq1 { t.Error("Snowflake should not be re-initialized") } if ug.cipher != cipher1 { t.Error("Cipher should not be re-initialized") } // Third initialization with different parameters err3 := ug.Init(2, key) if err3 != nil { t.Errorf("Third init should not fail: %v", err3) } // Still should not re-initialize if ug.seq != seq1 { t.Error("Snowflake should not be re-initialized even with different worker ID") } if ug.cipher != cipher1 { t.Error("Cipher should not be re-initialized even with different worker ID") } } func TestUidGeneratorInitConcurrent(t *testing.T) { const numGoroutines = 20 key := []byte("testkey1testkey2") ug := &UidGenerator{} errChan := make(chan error, numGoroutines) // Concurrent initialization attempts for i := 0; i < numGoroutines; i++ { go func(workerID uint) { err := ug.Init(workerID, key) errChan <- err }(uint(i)) } // Collect results var errors []error for i := 0; i < numGoroutines; i++ { if err := <-errChan; err != nil { errors = append(errors, err) } } // At least one should succeed, others might fail due to race conditions // but the generator should be in a valid state if ug.seq == nil { t.Error("Snowflake should be initialized after concurrent attempts") } if ug.cipher == nil { t.Error("Cipher should be initialized after concurrent attempts") } // Should be able to generate UIDs uid := ug.Get() if uid == ZeroUid { t.Error("Should be able to generate UID after concurrent initialization") } if len(errors) > 0 { t.Logf("Some concurrent initializations failed (may be expected): %v", errors) } } func TestUidGeneratorInitBoundaryWorkerIDs(t *testing.T) { key := []byte("testkey1testkey2") // Test boundary values for worker ID testCases := []struct { name string workerID uint expect bool // true if should succeed }{ {"zero worker ID", 0, true}, {"worker ID 1", 1, true}, {"worker ID 1023", 1023, true}, // Common snowflake limit {"worker ID 1024", 1024, false}, // Might exceed limit } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { ug := &UidGenerator{} err := ug.Init(tc.workerID, key) if tc.expect && err != nil { t.Errorf("Expected success for worker ID %d, got error: %v", tc.workerID, err) } else if !tc.expect && err == nil { t.Errorf("Expected error for worker ID %d, but succeeded", tc.workerID) } // If initialization succeeded, verify it works if err == nil { uid := ug.Get() if uid == ZeroUid { t.Errorf("Generator with worker ID %d should produce valid UIDs", tc.workerID) } } }) } } func BenchmarkUidGeneratorGet(b *testing.B) { ug := &UidGenerator{} key := []byte("testkey1testkey2") err := ug.Init(1, key) if err != nil { b.Fatalf("Failed to initialize generator: %v", err) } b.ResetTimer() for i := 0; i < b.N; i++ { _ = ug.Get() } } func BenchmarkUidGeneratorGetStr(b *testing.B) { ug := &UidGenerator{} key := []byte("testkey1testkey2") err := ug.Init(1, key) if err != nil { b.Fatalf("Failed to initialize generator: %v", err) } b.ResetTimer() for i := 0; i < b.N; i++ { _ = ug.GetStr() } } func BenchmarkUidGeneratorDecodeUid(b *testing.B) { ug := &UidGenerator{} key := []byte("testkey1testkey2") err := ug.Init(1, key) if err != nil { b.Fatalf("Failed to initialize generator: %v", err) } // Pre-generate a UID to decode uid := ug.Get() b.ResetTimer() for i := 0; i < b.N; i++ { _ = ug.DecodeUid(uid) } } func BenchmarkUidGeneratorEncodeInt64(b *testing.B) { ug := &UidGenerator{} key := []byte("testkey1testkey2") err := ug.Init(1, key) if err != nil { b.Fatalf("Failed to initialize generator: %v", err) } val := int64(12345) b.ResetTimer() for i := 0; i < b.N; i++ { _ = ug.EncodeInt64(val) } } ================================================ FILE: server/templ/email-password-reset-en.templ ================================================ {{/* ENGLISH This template defines contents of the password reset email. See explanation in ./email-validation-en.templ */}} {{define "subject" -}} Reset Tinode password {{- end}} {{define "body_html" -}}

Hello.

You recently requested to reset the password for your Tinode account. Use the link or code below to reset it. The link and code are valid for the next 24 hours only.

Click to reset your password.

If you’re having trouble with the link above, copy and paste the URL below into your web browser:

{{.HostUrl}}#reset?scheme={{.Scheme}}&code={{.Code}}&cred={{.Cred}}

Please enter the following code if prompted:

{{.Code}}
{{with .Login}}

In case you have forgotten, here is your login: {{.}}.

{{end}}

If you did not request a password reset, please ignore this message.

Tinode Team

{{- end}} {{define "body_plain" -}} Hello. You recently requested to reset the password for your Tinode account ({{.HostUrl}}). Use the link or code below to reset it. The link and code are valid for the next 24 hours only. {{.HostUrl}}#reset?scheme={{.Scheme}}&code={{.Code}}&cred={{.Cred}} If you’re having trouble with clicking the link above, copy and paste it into your web browser. Please enter the following code if prompted: {{.Code}} {{- with .Login}} In case you have forgotten, here is your login: {{.}}. {{end -}} If you did not request a password reset, please ignore this message. Tinode Team https://tinode.co/ {{- end}} ================================================ FILE: server/templ/email-password-reset-es.templ ================================================ {{/* SPANISH This template defines contents of the password reset email in spanish. See explanation in ./email-validation-en.templ */}} {{define "subject" -}} Reestablecer contraseña de Tinode {{- end}} {{define "body_html" -}}

Hola.

Recientemente solicitaste reestablecer la contraseña para tu cuenta Tinode. Usa el enlace de abajo para reestablecerla. El enlace es válido solamente por las siguientes 24 horas.

Clic para reestablecer contraseña.

Si tienes problemas con el enlace superior, copia y pega le siguiente URL en tu navegador.

{{.HostUrl}}#reset?scheme={{.Scheme}}&token={{.Token}}

Ingrese el siguiente código si se le solicita:

{{.Code}}
{{with .Login}}

En caso de que lo hayas olvidado, tu usuario es: {{.}}.

{{end}}

Si no solicitaste el reestablecimiento de tu contrseña, por favor ignora este mensaje.

Equipo de Tinode

{{- end}} {{define "body_plain" -}} Hola. Recientemente solicitaste reestablecer la contraseña para tu cuenta Tinode ({{.HostUrl}}). Usa el enlace de abajo para reestablecerla. El enlace es válido solamente por las siguientes 24 horas. {{.HostUrl}}#reset?scheme={{.Scheme}}&token={{.Token}} Si tienes problemas con el enlace superior, copia y pega le siguiente URL en tu navegador. Ingrese el siguiente código si se le solicita: {{.Code}} {{- with .Login}} En caso de que lo hayas olvidado, tu usuario es: {{.}}. {{end -}} Si no solicitaste el reestablecimiento de tu contrseña, por favor ignora este mensaje. Equipo de Tinode https://tinode.co/ {{- end}} ================================================ FILE: server/templ/email-password-reset-fr.templ ================================================ {{/* FRENCH This template defines contents of the password reset email. See explanation in ./email-validation-en.templ */}} {{define "subject" -}} Réinitialiser votre mot de passe Tinode {{- end}} {{define "body_html" -}}

Bonjour.

Vous avez récemment demandé à réinitialiser le mot de passe de votre compte Tinode. Utilisez le lien ou le code ci-dessous pour le réinitialiser. Le lien et le code sont valides pour les prochaines 24 heures seulement.

Cliquer ici pour réinitialiser votre mot de passe.

Si vous avez des problèmes avec le lien ci-dessus, copiez et collez l'URL ci-dessous dans votre navigateur web :

{{.HostUrl}}#reset?scheme={{.Scheme}}&token={{.Token}}

Veuillez saisir le code suivant si vous y êtes invité :

{{.Code}}
{{with .Login}}

Au cas où vous l'auriez oublié, voici votre login : {{.}}.

{{end}}

Si vous n'avez pas demandé la réinitialisation de votre mot de passe, veuillez ignorer ce message.

L'équipe Tinode

{{- end}} {{define "body_plain" -}} Bonjour. Vous avez récemment demandé à réinitialiser le mot de passe de votre compte ({{.HostUrl}}). Utilisez le lien ou le code ci-dessous pour le réinitialiser. Le lien et le code sont valides pour les prochaines 24 heures seulement. {{.HostUrl}}#reset?scheme={{.Scheme}}&token={{.Token}} Si vous avez des difficultés à cliquer sur le lien ci-dessus, copiez et collez-le dans votre navigateur web. Veuillez saisir le code suivant si vous y êtes invité: {{.Code}} {{- with .Login}} Au cas où vous l'auriez oublié, voici votre login : {{.}}. {{end -}} Si vous n'avez pas demandé la réinitialisation de votre mot de passe, veuillez ignorer ce message. L'équipe Tinode https://tinode.co/ {{- end}} ================================================ FILE: server/templ/email-password-reset-pt.templ ================================================ {{/* PORTUGUESE This template defines contents of the password reset e-mail in portuguese. See explanation in ./email-validation-en.templ */}} {{define "subject" -}} Redefinir senha Tinode {{- end}} {{define "body_html" -}}

Olá.

Você solicitou recentemente a redefinição da sua senha Tinode. Use o link ou código abaixo para redefini-lo. O link e o código são válidos apenas pelas próximas 24 horas.

Clic para redefinir sua senha.

Se você tiver problema com o link acima, copie e cole a URL abaixo no seu navegador:

{{.HostUrl}}#reset?scheme={{.Scheme}}&token={{.Token}}

Digite o seguinte código se solicitado:

{{.Code}}
{{with .Login}}

Em caso de esquecimento, aqui seu login: {{.}}.

{{end}}

Se não solicitaste a redefinição da senha, por favor ignorar essa mensagem.

Equipe Tinode

{{- end}} {{define "body_plain" -}} Olá. Você solicitou recentemente a redefinição da sua senha Tinode ({{.HostUrl}}). Use o link ou código abaixo para redefini-lo. O link e o código são válidos apenas pelas próximas 24 horas. {{.HostUrl}}#reset?scheme={{.Scheme}}&token={{.Token}} Si tienes problemas con el enlace superior, copia y pega le siguiente URL en tu navegador. Digite o seguinte código se solicitado: {{.Code}} {{- with .Login}} Em caso de esquecimento, aqui seu login: {{.}}. {{end -}} Se não solicitaste a redefinição da senha, por favor ignorar essa mensagem.. Equipe Tinode https://tinode.co/ {{- end}} ================================================ FILE: server/templ/email-password-reset-ru.templ ================================================ {{/* RUSSIAN Password reset email. See explanation in ./email-validation-en.templ */}} {{define "subject" -}} Изменить пароль Tinode {{- end}} {{define "body_html" -}}

Здравствуйте.

Вы прислали запрос на изменение пароля для вашего аккаунта Tinode. Используйте ссылку или код ниже, чтобы сбросить его. Ссылка и код действительны только в течение следующих 24 часов.

Кликните для изменения пароля.

Если ссылка по какой-то причине не работает, скопируйте следующий URL и вставьте его в адресную строку браузера:

{{.HostUrl}}#reset?scheme={{.Scheme}}&token={{.Token}}&hl=RU

Пожалуйста, введите следующий код, если потребуется:

{{.Code}}
{{with .Login}}

В случае, если вы забыли, ваш логин: {{.}}.

{{end}}

Если вы не отправляли запрос на изменение пароля, просто игнорируйте это сообщение.

Команда Tinode

{{- end}} {{define "body_plain" -}} Здравствуйте. Вы прислали запрос на изменение пароля для вашего аккаунта Tinode ({{.HostUrl}}). Используйте ссылку или код ниже, чтобы сбросить его. Ссылка и код действительны только в течение следующих 24 часов. {{.HostUrl}}#reset?scheme={{.Scheme}}&token={{.Token}}&hl=RU Если ссылка по какой-то вы не можете кликнуть по ссылке выше, скопируйте ее и вставьте его в адресную строку браузера. Пожалуйста, введите следующий код, если потребуется: {{.Code}} {{- with .Login}} В случае, если вы забыли, ваш логин: {{.}}. {{end -}} Если вы не отправляли запрос на изменение пароля, просто игнорируйте это сообщение. Команда Tinode https://tinode.co/ {{- end}} ================================================ FILE: server/templ/email-password-reset-uk.templ ================================================ {{/* UKRAINIAN Password reset email. See explanation in ./email-validation-en.templ */}} {{define "subject" -}} Змінити пароль Tinode {{- end}} {{define "body_html" -}}

Доброго дня.

Ви надіслали запит на зміну пароля для вашого акаунта Tinode. Щоб скинути його, скористайтеся посиланням або кодом нижче. Посилання та код дійсні лише протягом наступних 24 годин.

Натисніть для зміни пароля.

Якщо посилання з якоїсь причини не працює, скопіюйте наступну URL-адресу та вставте її в адресний рядок браузера:

{{.HostUrl}}#reset?scheme={{.Scheme}}&token={{.Token}}&hl=UK

Будь ласка, введіть наступний код, якщо потрібно:

{{.Code}}
{{with .Login}}

У випадку, якщо ви забули ваш логін: {{.}}.

{{end}}

Якщо ви не надсилали запит на зміну пароля, просто ігноруйте це повідомлення.

Команда Tinode

{{- end}} {{define "body_plain" -}} Доброго дня. Ви надіслали запит на зміну пароля для вашого акаунта Tinode ({{.HostUrl}}). Щоб скинути його, скористайтеся посиланням або кодом нижче. Посилання та код дійсні лише протягом наступних 24 годин. {{.HostUrl}}#reset?scheme={{.Scheme}}&token={{.Token}}&hl=UK Якщо посилання з якоїсь причини не працює, скопіюйте наступну URL-адресу та вставте її в адресний рядок браузера. Будь ласка, введіть наступний код, якщо потрібно: {{.Code}} {{- with .Login}} У випадку, якщо ви забули ваш логін: {{.}}. {{end -}} Якщо ви не надсилали запит на зміну пароля, просто ігноруйте це повідомлення. Команда Tinode https://tinode.co/ {{- end}} ================================================ FILE: server/templ/email-password-reset-vi.templ ================================================ {{/* VIETNAMESE This template defines contents of the password reset email. See explanation in ./email-validation-en.templ */}} {{define "subject" -}} Đặt lại mật khẩu Tinode {{- end}} {{define "body_html" -}}

Xin chào.

Bạn vừa gửi yêu cầu đặt lại mật khẩu cho tài khoản Tinode của bạn. Sử dụng liên kết hoặc mã dưới đây để thiết lập lại nó. Liên kết và mã chỉ có hiệu lực trong 24 giờ tới.

Bấm để đặt lại mật khẩu.

Nếu bạn gặp vấn đề khi bấm vào liên kết trên, vui lòng sao chép liên kết dưới đây và dán vào trình duyệt của bạn:

{{.HostUrl}}#reset?scheme={{.Scheme}}&token={{.Token}}

Vui lòng nhập mã sau nếu được nhắc:

{{.Code}}
{{with .Login}}

Trong trường hợp bạn quên, thì đây là tên đăng nhập của bạn: {{.}}.

{{end}}

Nếu bạn không yêu cầu cấp lại mật khẩu, Vui lòng bỏ qua tin nhắn này.

Tinode Team

{{- end}} {{define "body_plain" -}} Xin chào. Bạn vừa gửi yêu cầu đặt lại mật khẩu cho tài khoản Tinode ({{.HostUrl}}). Sử dụng liên kết hoặc mã dưới đây để thiết lập lại nó. Liên kết và mã chỉ có hiệu lực trong 24 giờ tới. {{.HostUrl}}#reset?scheme={{.Scheme}}&token={{.Token}} Nếu bạn gặp vấn đề khi bấm vào liên kết trên, vui lòng sao chép và dán vào trình duyệt của bạn Vui lòng nhập mã sau nếu được nhắc: {{.Code}} {{- with .Login}} Trong trường hợp bạn quên, thì đây là tên đăng nhập của bạn: {{.}}. {{end -}} Nếu bạn không yêu cầu cấp lại mật khẩu, Vui lòng bỏ qua tin nhắn này. Tinode Team https://tinode.co/ {{- end}} ================================================ FILE: server/templ/email-password-reset-zh-TW.templ ================================================ {{/* 繁體中文 此模板定義密碼重設電子郵件的內容。 說明請參見 ./email-validation-en.templ */}} {{define "subject" -}} 重設 Tinode 密碼 {{- end}} {{define "body_html" -}}

您好。

您最近請求重設您的 Tinode 帳戶密碼。 請使用以下連結或驗證碼進行重設。此連結和驗證碼僅在接下來的 24 小時內有效。

點擊重設您的密碼。

如果您無法使用上方的連結,請複製以下網址並貼到您的網頁瀏覽器中:

{{.HostUrl}}#reset?scheme={{.Scheme}}&code={{.Code}}&cred={{.Cred}}

如有提示,請輸入以下驗證碼:

{{.Code}}
{{with .Login}}

如果您忘記了,您的登入名稱是:{{.}}。

{{end}}

如果您沒有請求密碼重設,請忽略此訊息。

Tinode 團隊

{{- end}} {{define "body_plain" -}} 您好。 您最近請求重設您的 Tinode 帳戶密碼 ({{.HostUrl}})。 請使用以下連結或驗證碼進行重設。此連結和驗證碼僅在接下來的 24 小時內有效。 {{.HostUrl}}#reset?scheme={{.Scheme}}&code={{.Code}}&cred={{.Cred}} 如果您無法點擊上方的連結,請複製並貼到您的網頁瀏覽器中。 如有提示,請輸入以下驗證碼: {{.Code}} {{- with .Login}} 如果您忘記了,您的登入名稱是:{{.}}。 {{end -}} 如果您沒有請求密碼重設,請忽略此訊息。 Tinode 團隊 https://tinode.co/ {{- end}} ================================================ FILE: server/templ/email-password-reset-zh.templ ================================================ {{/* CHINESE 定义重置密码文案的模版。 参阅 ./email-validation-zh.templ */}} {{define "subject" -}} 重置 Tinode 密码 {{- end}} {{define "body_html" -}}

您好!

您正在申请重置 Tinode 使用下面的链接或代码重置它。 链接和代码仅在接下来的 24 小时内有效。

重置密码

如果无法点击上面的链接,您可以复制该地址,并粘帖在浏览器的地址栏中访问:

{{.HostUrl}}#reset?scheme={{.Scheme}}&token={{.Token}}

如果出现提示,请输入以下代码:

{{.Code}}
{{with .Login}}

如果您忘记了,您的登录名是: {{.}}.

{{end}}

如非您没有申请重置密码,请忽略这条消息。

Tinode 团队

{{- end}} {{define "body_plain" -}} 您好! 您正在申请重置 Tinode ({{.HostUrl}}) 账号密码。 使用下面的链接或代码重置它。 链接和代码仅在接下来的 24 小时内有效。 {{.HostUrl}}#reset?scheme={{.Scheme}}&token={{.Token}} 如果无法点击上面的链接,您可以复制该地址,并粘帖在浏览器的地址栏中访问: 如果出现提示,请输入以下代码: {{.Code}} {{- with .Login}} 如果您忘记了,您的登录名是: {{.}}. {{end -}} 如非您没有申请重置密码,请忽略这条消息。 Tinode 团队 https://tinode.co/ {{- end}} ================================================ FILE: server/templ/email-validation-en.templ ================================================ {{/* ENGLISH This template defines content of the email sent to users as a request to confirm registration email address. See https://golang.org/pkg/text/template/ for syntax. The template must contain the following parts parts: - 'subject': Subject line of an email message - One or both of the following: - 'body_html': HTML content of the message. A header "Content-type: text/html" will be added. - 'body_plain': plain text content of the message. A header "Content-type: text/plain" will be added. If both body_html and body_plain are included, both are sent as parts of 'multipart/alternative' message. */}} {{define "subject" -}} Tinode registration: confirm email {{- end}} {{define "body_html" -}}

Hello.

You're receiving this message because someone used your email to register at Tinode.

Click to confirm or go to {{.HostUrl}}#cred?method=email and enter the following code:

{{.Code}}

You may need to enter login and password.

If you did not register at Tinode just ignore this message.

Tinode Team

{{- end}} {{define "body_plain" -}} Hello. You're receiving this message because someone used your email to register at Tinode ({{.HostUrl}}). Click the link {{.HostUrl}}#cred?method=email&code={{.Code}}&token={{.Token}} to confirm or go to {{.HostUrl}}#cred?what=email and enter the following code: {{.Code}} You may need to enter login and password. If you did not register at Tinode just ignore this message. Tinode Team https://tinode.co/ {{- end}} ================================================ FILE: server/templ/email-validation-es.templ ================================================ {{/* SPANISH See explanation in ./email-validation-en.templ */}} {{define "subject" -}} Registro Tinode: Correo de confirmación {{- end}} {{define "body_html" -}}

Hola.

Estás recibiendo este correo porque alguien uso tu correo para registrarse en Tinode.

Clic para confirmar o ve a {{.HostUrl}}#cred?method=email e ingresa el siguiente código:

{{.Code}}

Necesitas ingresar tu usuario y contraseña.

Si tú no te registraste en Tinode solo ignora este mensaje.

Equipo de Tinode

{{- end}} {{define "body_plain" -}} Hola. Estás recibiendo este correo porque alguien uso tu correo para registrarse en Tinode ({{.HostUrl}}). Da clic en el enlace {{.HostUrl}}#cred?method=email&code={{.Code}}&token={{.Token}} para confirmar o ve a {{.HostUrl}}#cred?what=email e ingresa el siguiente código: {{.Code}} Necesitas ingresar tu usuario y contraseña. Si tú no te registraste en Tinode solo ignora este mensaje. Equipo de Tinode https://tinode.co/ {{- end}} ================================================ FILE: server/templ/email-validation-fr.templ ================================================ {{/* FRENCH See explanation in ./email-validation-en.templ */}} {{define "subject" -}} Tinode enregistrement : confirmer l'adresse e-mail {{- end}} {{define "body_html" -}}

Bonjour.

Vous recevez ce message car quelqu'un a utilisé votre adresse électronique pour s'inscrire sur le site Tinode.

Cliquer ici pour confimer ou rendez-vous à l'adresse {{.HostUrl}}#cred?method=email et entrez le code suivant :

{{.Code}}

Vous devrez peut-être entrer un login et un mot de passe.

Si vous ne vous êtes pas inscrit à Tinode, ignorez ce message.

L'équipe Tinode

{{- end}} {{define "body_plain" -}} Bonjour. Vous recevez ce message car quelqu'un a utilisé votre adresse électronique pour s'inscrire sur le site Tinode ({{.HostUrl}}). Cliquer sur le lien {{.HostUrl}}#cred?method=email&code={{.Code}}&token={{.Token}} pour confimer ou rendez-vous à l'adresse {{.HostUrl}}#cred?what=email et entrez le code suivant : {{.Code}} Vous devrez peut-être entrer un login et un mot de passe. Si vous ne vous êtes pas inscrit à Tinode, ignorez ce message. L'équipe Tinode https://tinode.co/ {{- end}} ================================================ FILE: server/templ/email-validation-pt.templ ================================================ {{/* PORTUGUESE See explanation in ./email-validation-en.templ */}} {{define "subject" -}} Registro Tinode: E-mail de confirmação {{- end}} {{define "body_html" -}}

Olá.

Você está recebendo esse e-mail porque alguém usou seu e-mail para registrar-se em Tinode.

Clique para confirmar ou acesse {{.HostUrl}}#cred?method=email e entre com o seguinte código:

{{.Code}}

Necessário entrar com login e senha.

Se você não se registrou em Tinode apenas ignore essa mensagem.

Equipe Tinode

{{- end}} {{define "body_plain" -}} Olá. Você está recebendo este e-mail porque alguém usou seu e-mail para registrar-se em Tinode ({{.HostUrl}}). Clique no link {{.HostUrl}}#cred?method=email&code={{.Code}}&token={{.Token}} para confirmar {{.HostUrl}}#cred?what=email e entre com o seguinte código: {{.Code}} Necessário entrar com login e senha. Se você não se registrou em Tinode apenas ignore essa mensagem.. Equipe Tinode https://tinode.co/ {{- end}} ================================================ FILE: server/templ/email-validation-ru.templ ================================================ {{/* RUSSIAN See explanation in ./email-validation-en.templ */}} {{define "subject" -}} Регистрация Tinode: подтвердите емейл {{- end}} {{define "body_html" -}}

Здравствуйте.

Вы получили это сообщение потому, что зарегистрировались в Tinode.

Кликните здесь чтобы подтвердить регистрацию или перейдите по сслыке {{.HostUrl}}#cred?method=email&hl=RU и введите следующий код:

{{.Code}}

Возможно, вам потребуется ввести логин и пароль.

Если вы не регистрировались в Tinode, просто игнорируйте это сообщение.

Команда Tinode

{{- end}} {{define "body_plain" -}} Здравствуйте. Вы получили это сообщение потому, что зарегистрировались в Tinode ({{.HostUrl}}). Кликните на {{.HostUrl}}#cred?method=email&code={{.Code}}&token={{.Token}}&hl=RU чтобы подтвердить регистрацию или перейдите по сслыке {{.HostUrl}}#cred?what=email">{{.HostUrl}}#cred?method=email&hl=RU и введите следующий код: {{.Code}} Возможно, вам также потребуется ввести логин и пароль. Если вы не регистрировались в Tinode, просто игнорируйте это сообщение. Команда Tinode https://tinode.co/ {{- end}} ================================================ FILE: server/templ/email-validation-uk.templ ================================================ {{/* UKRAINIAN See explanation in ./email-validation-en.templ */}} {{define "subject" -}} Реєстрація Tinode: підтвердіть емейл {{- end}} {{define "body_html" -}}

Доброго дня.

Ви отримали це повідомлення тому, що зареєструвалися в Tinode.

Натисніть тут, щоб підтвердити реєстрацію або перейдіть за посиланням {{.HostUrl}}#cred?method=email&hl=UK та введіть наступний код:

{{.Code}}

Можливо, вам потрібно буде ввести логін та пароль.

Якщо ви не реєструвалися в Tinode, просто ігноруйте це повідомлення.

Команда Tinode

{{- end}} {{define "body_plain" -}} Доброго дня. Ви отримали це повідомлення тому, що зареєструвалися в Tinode ({{.HostUrl}}). Натисніть на {{.HostUrl}}#cred?method=email&code={{.Code}}&token={{.Token}}&hl=UK щоб підтвердити реєстрацію або перейдіть за посиланням {{.HostUrl}}#cred?what=email">{{.HostUrl}}#cred?method=email&hl=UK та введіть наступний код: {{.Code}} Можливо, вам потрібно буде ввести логін та пароль. Якщо ви не реєструвалися в Tinode, просто ігноруйте це повідомлення. Команда Tinode https://tinode.co/ {{- end}} ================================================ FILE: server/templ/email-validation-vi.templ ================================================ {{/* VIETNAMESE See explanation in ./email-validation-en.templ */}} {{define "subject" -}} Xác thực đăng ký tài khoản Tinode {{- end}} {{define "body_html" -}}

Xin chào.

Bạn nhận được tin nhắn này bởi vì có ai đó đã dùng email này để đăng ký tài khoản tại Tinode.

Bấm để xác nhận hoặc đi tới liên kết {{.HostUrl}}#cred?method=email và nhập mã xác thực:

{{.Code}}

Có thể bạn cần nhập tên đăng nhập và mật khẩu.

Nếu bạn không đăng ký tài khoản tại Tinode vui lòng bỏ qua tin nhắn này.

Tinode Team

{{- end}} {{define "body_plain" -}} Xin chào. Bạn nhận được tin nhắn này bởi vì có ai đó đã dùng email này để đăng ký tài khoản tại Tinode ({{.HostUrl}}). Bấm vào liên kết {{.HostUrl}}#cred?method=email&code={{.Code}}&token={{.Token}} để xác nhận hoặc đi tới {{.HostUrl}}#cred?what=email và nhập mã xác thực {{.Code}} Có thể bạn sẽ cần nhập tên đăng nhập và mật khẩu. Nếu bạn không đăng ký tài khoản tại Tinode vui lòng bỏ qua tin nhắn này. Tinode Team https://tinode.co/ {{- end}} ================================================ FILE: server/templ/email-validation-zh-TW.templ ================================================ {{/* CHINESE Traditional (Taiwan) 繁體中文 此模板定義發送給用戶的電子郵件內容,用於請求確認註冊電子郵件地址。 語法請參見 https://golang.org/pkg/text/template/ 模板必須包含以下部分: - 'subject': 電子郵件的主題行 - 以下一個或兩個: - 'body_html': 訊息的 HTML 內容。將添加標頭 "Content-type: text/html"。 - 'body_plain': 訊息的純文字內容。將添加標頭 "Content-type: text/plain"。 如果同時包含 body_html 和 body_plain,兩者都將作為 'multipart/alternative' 訊息的部分發送。 */}} {{define "subject" -}} Tinode 註冊:確認電子郵件 {{- end}} {{define "body_html" -}}

您好。

您收到此訊息是因為有人使用您的電子郵件在 Tinode 註冊。

點擊確認 或前往 {{.HostUrl}}#cred?method=email 並輸入以下驗證碼:

{{.Code}}

您可能需要輸入登入名稱和密碼。

如果您沒有在 Tinode 註冊,請忽略此訊息。

Tinode 團隊

{{- end}} {{define "body_plain" -}} 您好。 您收到此訊息是因為有人使用您的電子郵件在 Tinode ({{.HostUrl}}) 註冊。 點擊連結 {{.HostUrl}}#cred?method=email&code={{.Code}}&token={{.Token}} 進行確認,或前往 {{.HostUrl}}#cred?what=email 並輸入以下驗證碼: {{.Code}} 您可能需要輸入登入名稱和密碼。 如果您沒有在 Tinode 註冊,請忽略此訊息。 Tinode 團隊 https://tinode.co/ {{- end}} ================================================ FILE: server/templ/email-validation-zh.templ ================================================ {{/* CHINESE 定义用户注册邮件确认文案的模版。 语法参阅 https://golang.org/pkg/text/template/ 。 模版必须包含以下内容: - 'subject':邮件主题 - 以下一项或两项: - 'body_html': 包含请求头"Content-type: text/html"的HTML格式消息内容。 - 'body_plain': 包含请求头"Content-type: text/plain"的文本格式消息内容。 如果同时包含 body_html 和 body_plain,则都作为 'multipart/alternative' 消息的一部分发送。 */}} {{define "subject" -}} Tinode 注册: 确认邮件 {{- end}} {{define "body_html" -}}

您好!

您收到此消息是因为您注册了Tinode

确认注册 或者跳转至链接 {{.HostUrl}}#cred?method=email 并输入验证码:

{{.Code}}

您可能需要输入登录名和密码。

如果您没有注册Tinode,请忽略这条消息。

Tinode 团队

{{- end}} {{define "body_plain" -}} 您好! 您收到此消息是因为您注册了 Tinode ({{.HostUrl}})。 点击链接 {{.HostUrl}}#cred?method=email&code={{.Code}}&token={{.Token}} 确认注册或者跳转至链接 {{.HostUrl}}#cred?what=email 并输入验证码: {{.Code}} 您可能需要输入登录名和密码。 如果您没有注册Tinode,请忽略这条消息。 Tinode 团队 https://tinode.co/ {{- end}} ================================================ FILE: server/templ/sms-universal-en.templ ================================================ {{/* ENGLISH Universal confirmation and password reset template for SMS. */}} Tinode confirmation code: {{.Code}} {{.HostUrl}} ================================================ FILE: server/templ/sms-universal-es.templ ================================================ {{/* SPANISH Universal confirmation and password reset template for SMS. */}} Código de confirmación de Tinode: {{.Code}} {{.HostUrl}} ================================================ FILE: server/templ/sms-universal-fr.templ ================================================ {{/* FRENCH Modèle universel de confirmation et de réinitialisation du mot de passe pour SMS. */}} Code de confirmation Tinode : {{.Code}} {{.HostUrl}} ================================================ FILE: server/templ/sms-universal-pt.templ ================================================ {{/* PORTUGESE Modelo universal de confirmação e redefinição de senha para SMS. */}} Código de confirmação Tinode: {{.Code}} {{.HostUrl}} ================================================ FILE: server/templ/sms-universal-ru.templ ================================================ {{/* RUSSIAN Универсальный шаблон подтверждения и сброса пароля для СМС. */}} Код подтверждения Tinode: {{.Code}} {{.HostUrl}} ================================================ FILE: server/templ/sms-universal-uk.templ ================================================ {{/* UKRAINIAN Універсальний шаблон підтвердження та скидання пароля для СМС. */}} Код підтвердження Tinode: {{.Code}} {{.HostUrl}} ================================================ FILE: server/templ/sms-universal-vi.templ ================================================ {{/* VIETNAMESE Universal confirmation and password reset template for SMS. */}} Mã xác thực từ Tinode: {{.Code}} {{.HostUrl}} ================================================ FILE: server/templ/sms-universal-zh-TW.templ ================================================ {{/* 繁體中文 通用確認和密碼重設簡訊模板。 */}} Tinode 確認驗證碼:{{.Code}} {{.HostUrl}} ================================================ FILE: server/templ/sms-universal-zh.templ ================================================ {{/* CHINESE Universal confirmation and password reset template for SMS. */}} 【Tinode】验证码: {{.Code}} {{.HostUrl}} ================================================ FILE: server/tinode.conf ================================================ // The JSON comments are somewhat brittle. Don't try anything too fancy. { // HTTP(S) address to listen on for websocket and long polling clients. Either a TCP host:port pair // or a path to Unix socket as "unix:/path/to/socket.sock". // The TCP port is either a numerical value or a canonical name, e.g. ":80" or ":https". May include // the host name, e.g. "localhost:80" or "hostname.example.com:https". // It could be blank: if TLS is not configured it will default to ":80", otherwise to ":443". // Can be overridden from the command line, see option --listen. "listen": ":6060", // Base URL path for serving streaming and large file API calls. // Can be overridden from the command line, see option --api_path. "api_path": "/", // Cache-Control header for static content in seconds. 39600 is 11 hours. "cache_control": 39600, // If true, do not attempt to negotiate websocket per message compression (RFC 7692.4). // It should be disabled (set to true) if you are using MSFT IIS as a reverse proxy. "ws_compression_disabled": false, // URL path for mounting the directory with static files. "static_mount": "/", // TCP host:port or unix:/path/to/socket to listen for gRPC clients. // Leave blank to disable gRPC support. // Could be overridden from the command line with --grpc_listen. "grpc_listen": ":16060", // Enable handling of gRPC keepalives https://github.com/grpc/grpc/blob/master/doc/keepalive.md // This sets server's GRPC_ARG_KEEPALIVE_TIME_MS to 60 seconds instead of the default 2 hours. "grpc_keepalive_enabled": true, // Salt for signing API key. 32 random bytes base64-encoded. Use 'keygen' tool (included in this // distro) to generate the API key and the salt. "api_key_salt": "T713/rYYgW7g4m3vG6zGRh7+FM1t0T8j13koXScOAj4=", // Maximum message size allowed from the clients in bytes (131072 = 128KB). // Media files with sizes greater than this limit are sent out of band. // Don't change this limit to a much higher value because it would likely cause failures: // * on Android & iOS due to a limit on the SQLite cursor window size; // * on the server-side with MySQL adapter due to the limit on the sort buffer size. "max_message_size": 131072, // Maximum number of subscribers per group topic. // This is the max number of subscribers to a group topic who may be configured to have specific access // permissions, like writing (posting), admin permissions, etc. // This setting is unrelated to channels readers (those have immutable read-only access). Channels have no // limit on readers. "max_subscriber_count": 128, // Maximum number of indexable tags per topic or user. "max_tag_count": 16, // If true, ordinary users cannot delete their accounts. "permanent_accounts": false, // URL path for exposing runtime stats. Disabled if the path is blank or "-". // Could be overriden from the command line with --expvar. "expvar": "/debug/vars", // URL path for server's internal status, useful when debugging. // Do not use this URL for docker status checks and some such. It's not a health check, // it is a debug endpoint. Disabled if the path is blank or "-". Could be overriden // from the command line with --server_status. // "server_status": "/debug/status", // Read IP address of the client from the HTTP header 'X-Forwarded-For'. // Useful when Tinode is behind a proxy. If missing, fallback to default RemoteAddr. "use_x_forwarded_for": true, // Add X-Frame-Options to HTTP response headers. It should be one of "DENY", "SAMEORIGIN", // "-" (disabled). If the option is missing then it's treated as SAMEORIGIN. "x_frame_options": "SAMEORIGIN", // 2-letter country code to assign to sessions by default when the country isn't specified // by the client explicitly and it's impossible to infer it. // If missing, the server will default to "US". "default_country_code": "", // Permit hard-deleting messages in p2p topics for both participants. // If it's set to 'false' then the message is only deleted for the peer who issued the command. // If it's 'true' then the message is deleted completely by either participant. // Changing the value affects the ability to hard-delete (the added or removed the D permission) // only for new topics going forward. "p2p_delete_enabled": true, // The maximum age of a message in seconds when it can be deleted by users with the 'D' permission. // E.g. 600 means messages up to 10 minutes old can be deleted, older than that cannot be deleted. // Missing or 0 means no age limit. // Does not affect topic owners: owners can delete any message. "msg_delete_age": 600, // Globally unique namespace. This is a special tag namespace which is used to store // aliases of the user. The alias is a tag which is not a valid Tinode user ID. "alias_tag": "alias", // Large media/blob handlers: large files/images included in messages. "media": { // The name of the media handler to use. "use_handler": "fs", // Maximum size of uploaded file (8MB here for testing, maybe increase to 100MB = 104857600 in prod) "max_size": 8388608, // Garbage collection periodicity in seconds: unused or abandoned uploads are deleted. "gc_period": 60, // The number of unused/abandoned entries to delete in one pass. "gc_block_size": 100, // Configurations of individual handlers. "handlers": { // File system storage. "fs": { // File system location to store uploaded files. In case of a cluster it // must be accessible by all cluster members, i.e. a network drive like https://www.samba.org/ "upload_dir": "uploads", // Cache-Control header to use for uploaded files. 86400 seconds = 24 hours. "cache_control": "max-age=86400", // Origin URLs allowed to download/upload files, e.g. ["https://www.example.com", "http://example.com", "https://*.example.com", "http://*.*.example.com"]. // Not necessary in most cases. // "cors_origins": ["*"] }, // Amazon AWS S3 storage. // See detailed explanation at https://pkg.go.dev/github.com/aws/aws-sdk-go/aws#Config "s3":{ // Use AWS console to get Access Key ID and Secret Access Key. // https://aws.amazon.com/blogs/security/wheres-my-secret-access-key/ "access_key_id": "your_s3_access_key_id", "secret_access_key": "your_s3_secret_access_key", // Region where the bucket is hosted. "region": "s3 region, like us-east-2", // Name of the S3 bucket. "bucket": "your_s3_bucket_name", // Set this to `true` to disable SSL when sending requests. Defaults to `false`. "disable_ssl": false, // Set this to `true` to force the request to use path-style addressing, // i.e., `http://s3.amazonaws.com/BUCKET/KEY`. By default, the S3 client // will use virtual hosted bucket addressing when possible // (`http://BUCKET.s3.amazonaws.com/KEY`). "force_path_style": false, // An optional endpoint URL (hostname only or fully qualified URI) // to override the default generated endpoint, or `""` to use the default generated endpoint. // The endpoint can be of any S3-compatible service, such as "minio-api.x.io". "endpoint": "", // Expiration time for presigned URLs in seconds. "presign_ttl": 3600, // Cache-Control header to use for uploaded files. 86400 seconds = 24 hours. "cache_control": "max-age=86400", // Origin URLs allowed to download files, e.g. ["https://www.example.com", "http://example.com"]. // See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin "cors_origins": ["*"] } } }, // TLS (httpS) configuration. Applies to both web and gRPC interfaces. "tls": { // Enable TLS. "enabled": false, // Listen for connections on this port and redirect them to HTTPS port. // Cannot be a Unix socket. "http_redirect": ":80", // Add Strict-Transport-Security to headers, the value signifies age. // Zero or negative value turns it off. "strict_max_age": 604800, // Letsencrypt configuration. "autocert": { // Location of certificates. "cache": "/etc/letsencrypt/live/your.domain.here", // Contact address for this installation. LetsEncrypt will send // messages to this address in case of problems. Replace with your // own address or remove this line. "email": "noreply@example.com", // Domains served. Replace with your own domain name. "domains": ["whatever.example.com"] }, // If "autocert" config is not defined, read static certificates from // these locations. Ignored if "autocert" is defined. "cert_file": "/etc/httpd/conf/your.domain.crt", "key_file": "/etc/httpd/conf/your.domain.key" }, // Authentication configuration. "auth_config": { // Optional mapping of externally-visible authenticator names to internal names. // For example use ["my-auth:basic", "basic:"] to rename "basic" authenticator to // "my-auth" and make "basic" unaccessible by the old name. If you want to use REST-auth, then // the config is ["basic:rest", "rest:"]. // Default is identity mapping. "logical_names": [], // Basic (login + password) authentication. "basic": { // Add 'auth-name:username' to tags making user discoverable by username. "add_to_tags": true, // The minimum length of a login in unicode runes, i.e. "登录" is length 2, not 6. // The maximum length is 32 and it cannot be changed. "min_login_length": 4, // The minimum length of a password in unicode runes, "пароль" is length 6, not 12. // There is no limit on maximum length, but MySQL & PgSQL adapters have a limit of 32 bytes. "min_password_length": 6 }, // Token authentication "token": { // Lifetime of a security token in seconds. 1209600 = 2 weeks. "expire_in": 1209600, // Serial number of the token. Can be used to invalidate all issued tokens at once. "serial_num": 1, // Secret key (HMAC salt) for signing the tokens. Generate your own then keep it secret. // Any 32 random bytes base64 encoded. // // === IMPORTANT === // // CHANGE IT IN PRODUCTION!!! Otherwise anyone will be able to log in // to your server without the password. It's just random base64-encoded bytes, use any suitable // means to get it. For example: // Linux/Mac: // echo $(head -c 32 /dev/urandom | base64 | tr -d '\n') // Windows: // powershell -command "[Convert]::ToBase64String((1..32|%{[byte](Get-Random -Max 256)}))" "key": "wfaY2RgF2S1OQI/ZlK+LSrp1KB2jwAdGAIHQ7JZn+Kc=" }, // Short code authenticator for resetting passwords. "code": { // Lifetime of a security code in seconds. 900 seconds = 15 minutes. "expire_in": 900, // Number of times a user can try to enter the code. "max_retries": 3, // Length of the secret code. "code_length": 6 } }, // Database configuration "store_config": { // XTEA encryption key for user IDs and topic names. 16 random bytes base64-encoded. // Generate your own and keep it secret. Otherwise your user IDs will be predictable // and it will be easy to spam your users. "uid_key": "la6YsO+bNX/+XIkOqc5Svw==", // Maximum number of results fetched in one DB call. "max_results": 1024, // DB adapter name to communicate with the DB backend. // Must be one of the adapters from the list below. "use_adapter": "", // Configurations of individual adapters. "adapters": { // PostgreSQL configuration. See https://godoc.org/github.com/jackc/pgx#Config // for other possible options. "postgres": { // PostgreSQL connection settings. // Don't change the username before reading the FAQ! "User": "postgres", "Passwd": "postgres", "Host": "localhost", "Port": "5432", "DBName": "tinode", "SSLMode": "disable", // DSN: alternative way of specifying database configuration, passed unchanged // to the driver. See https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING // "dsn": "postgresql://postgres:postgres@localhost:5432/tinode?sslmode=disable&connect_timeout=10", // PostgreSQL connection pool settings. // Maximum number of open connections to the database. Zero means unlimited. "max_open_conns": 50, // Maximum number of connections in the idle connection pool. Zero means no idle connections are retained. "max_idle_conns": 50, // Maximum amount of time a connection may be reused. Zero means unlimited. "conn_max_lifetime": 60, // Maximum amount of time waiting for a connection from the pool. Zero means no timeout. "sql_timeout": 10 }, // MySQL configuration. See https://godoc.org/github.com/go-sql-driver/mysql#Config // for other possible options. "mysql": { // MySQL connection settings. // See https://pkg.go.dev/github.com/go-sql-driver/mysql#Config for more info // and available fields and options. "User": "root", "Net": "tcp", "Addr": "localhost", "DBName": "tinode", // The 'collation=utf8mb4_0900_ai_ci' is default in MySQL 8.0 and above. It is optional but highly // recommended for emoji and certain CJK characters in earlier versions of MySQL. "Collation": "utf8mb4_0900_ai_ci", // Parse time values to time.Time. Required. "ParseTime": true, // DSN: alternative way of specifying database configuration, passed unchanged // to MySQL driver. See https://github.com/go-sql-driver/mysql#dsn-data-source-name for syntax. // DSN may optionally start with mysql:// // "dsn": "root@tcp(localhost)/tinode?parseTime=true&collation=utf8mb4_0900_ai_ci", // MySQL connection pool settings. // Maximum number of open connections to the database. Default: 0 (unlimited). "max_open_conns": 64, // Maximum number of connections in the idle connection pool. If negative or zero, // no idle connections are retained. "max_idle_conns": 64, // Maximum amount of time a connection may be reused (in seconds). "conn_max_lifetime": 60, // DB request timeout (in seconds). // If not set (or <= 0), DB queries and transactions will run without a timeout. "sql_timeout": 10 }, // RethinkDB configuration. See // https://godoc.org/github.com/rethinkdb/rethinkdb-go#ConnectOpts for other possible // options. "rethinkdb": { // Address(es) of RethinkDB node(s): either a string or an array of strings. "addresses": "localhost:28015", // Name of the main database. "database": "tinode" }, // MongoDB configuration. "mongodb": { // Connection string https://www.mongodb.com/docs/manual/reference/connection-string/ // Options configured with the 'uri' connection string override all other options // (only 'uri' is sent to the server, all other options are ignored). // If you are using Atlas, then you MUST use 'uri' to connect. See here: // https://www.mongodb.com/docs/manual/reference/connection-string/#std-label-connections-dns-seedlist // Something like // "uri": "mongodb+srv://CREDENTIALS@PROJECT.gmuaq.mongodb.net/DATABASE?retryWrites=true&w=majority", "uri": "", // The only supported server API version is "1". May or maynot be needed depending on server version. "api_version": "", // Address(es) of MongoDB node(s): either a string or an array of strings. "addresses": "localhost:27017", // Name of the main database. "database": "tinode", // Name of replica set of mongodb instance. Remove this line to use a standalone instance. // If replica_set is disabled, transactions will be disabled as well. "replica_set": "rs0", // Authentication options. Uncomment if auth is configured on your MongoDB. // Authentication mechanism. See https://www.mongodb.com/docs/manual/core/authentication/ // Default "SCRAM-SHA-256" // "auth_mechanism": "SCRAM-SHA-256", // The name of database that has the collection with the user credentials. Default "admin". // "auth_source": "admin", // Username: // "username": "tinode", // Password: // "password": "tinode", // Driver's TLS configuration. Uncomment to enable TLS. // "tls": true, // Path to the client certificate. Optional. // "tls_cert_file": "/path/to/cert_file", // Path to private key. Optional. // "tls_private_key": "/path/to/private_key", // Specifies whether or not certificates and hostnames received from the server should be validated. // Not recommended to enable in production. Default is false. // "tls_skip_verify": false } } }, // Account validators (email or SMS or captcha). "acc_validation": { // Email validator config. "email": { // Restrict use of "email" namespace: make users searchable by their emails, // disable manual creation of email: tags. "add_to_tags": true, // List of authentication levels which require this validation method. // Remove this line to disable email validation. "required": ["auth"], // Configuration passed to the validator unchanged. "config": { // Address of the host where the Tinode server is running. This will be used // in URLs in the email. "host_url": "http://localhost:6060/", // Address of the SMPT server to use. "smtp_server": "smtp.example.com", // SMTP port to use. "25" for basic email RFC 5321 (2821, 821), "587" for RFC 3207 (TLS). "smtp_port": "25", // RFC 5322 email address to show in the From: field. "sender": "\"Tinode\" ", // Optional login to use for authentication; if missing, the connection is not authenticated. "login": "john.doe@example.com", // Password to use when authenticating the sender; used only if "login" is provided. "sender_password": "your-password-here", // Authentication mechanism to use, optional. One of "login", "cram-md5", "plain" (default). "auth_mechanism": "login", // FQDN to use in SMTP HELO/EHLO command; if missing, the hostname from "host_url" is used. "smtp_helo_host": "example.com", // Skip verification of the server's certificate chain and host name. // In this mode, TLS is susceptible to man-in-the-middle attacks. "insecure_skip_verify": false, // Optional list of human languages to try to load templates for. If you don't care about i18n, // leave it blank or remove. The first language in the list is the default language. "languages": ["en", "es", "fr", "pt", "ru", "uk", "vi", "zh", "zh-TW"], // Message template for credential validation. // The file path itself is treated as a template. It's resolved by using the // "languages" field above. One template per language. // See the template file for the explanation of the expected structure. "validation_templ": "./templ/email-validation-{{.Language}}.templ", // Message template for resetting authentication secret. // One template per language. See email-validation-en template for the explanation // of the expected structure. "reset_secret_templ": "./templ/email-password-reset-{{.Language}}.templ", // Allow this many confirmation attempts before blocking the credential. "max_retries": 3, // List of email domains allowed to be used for registration. // Missing or empty list means any email domain is accepted. "domains": [], // Dummy response to accept. // // === IMPORTANT === // // REMOVE IN PRODUCTION!!! Otherwise anyone will be able to register // with fake emails. "debug_response": "123456" } }, // Placeholder validator for SMS and voice validation. Disabled by default. // Use something like twilio.com or sinch.com in production. "tel": { "add_to_tags": true, "config": { // Address of the host where the Tinode server is running. This will be used // in URLs in the SMS. "host_url": "http://localhost:6060/", // Optional list of locales to try to load templates for. If you don't care about i18n, // leave it blank or remove. The first language in the list is the default language. "languages": ["en", "es", "fr", "pt", "ru", "uk", "vi", "zh", "zh-TW"], // String to use in the From field of the SMS. "sender": "Tinode", // Message template for credential validation and password reset. The file path itself is // treated as a template. It's resolved by using the "languages" field above. One template // per language. "universal_templ": "./templ/sms-universal-{{.Language}}.templ", // Allow this many confirmation attempts before blocking the credential. "max_retries": 3, // Twilio configuration (optional). //"twilio_conf": { // "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", // "auth_token": "f2xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" //}, // Dummy response to accept. // // === IMPORTANT === // // REMOVE IN PRODUCTION!!! Otherwise anyone will be able to register // with fake phone numbers. "debug_response": "123456" } } }, // Configuration for stale account garbage collector: remove // stale unvalidated user accounts which have been last updated at least // 'gc_min_account_age' hours ago. "acc_gc_config": { "enabled": true, // How often to run GC (seconds). "gc_period": 3600, // Number of accounts to delete in one pass. "gc_block_size": 10, // Minimum hours since account was last modified. "gc_min_account_age": 30 }, // Configuration of push notifications. "push": [ { // Notificator which writes to STDOUT. Useful for debugging. "name":"stdout", "config": { // Disabled. "enabled": false } }, { // Google FCM notificator. "name":"fcm", "config": { // Disabled. Won't work without the server key anyway. See below. "enabled": false, // Firebase project ID. "project_id": "your-project-id", // Service account credentials as json. // See instructions how to download the service account credentials file: // https://cloud.google.com/iam/docs/creating-managing-service-account-keys // Then insert the file contents here. Yes, this is convoluted, but that's Google's fault. "credentials": { "type": "service_account", "project_id": "your-project-id", "private_key_id": "some-random-looking-hex-number", "private_key": "-----BEGIN PRIVATE KEY----- base64-encoded bits of your private key \n-----END PRIVATE KEY-----\n", "client_email": "firebase-adminsdk-abc123@your-project-id.iam.gserviceaccount.com", "client_id": "1234567890123456789", "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://oauth2.googleapis.com/token", "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-abc123%40your-project-id.iam.gserviceaccount.com" }, // An alternative way to provide Firebase service account credentials. "credentials_file": "/path/to/service-account-file-with-credentials.json", // Time in seconds before notification is discarded (by Google) if undelivered. "time_to_live": 3600, // Payload of AndroidNotification. If enabled, this will take precedence over data payload. "android": { // Set to false to push a data-only message. "enabled": false, // Android drawable resource ID to use as a notification icon. "icon": "ic_logo_push", // Notification color. "color": "#3949AB", // Name of intent filter which will catch this notification. "click_action": ".MessageActivity", // Notification of a new message. You can include custom "icon", "color", "click_action" // into this section and it will override the value above. "msg": { // Literal title string. Not recommended because it's not localized. "title": "", // Literal message body. Not recommended because it's not localized. "body": "", // Android string resource ID to use as a notification title. Localized. // Takes precedence over "title". "new_message" is "New message" in Tindroid. "title_loc_key": "new_message", // Android string resource ID to use as a notification body. Localized. // Takes precedence over "body". "body_loc_key": "" }, // Notification of a new subscription. Same rules as section "msg" above. "sub": { // Android resource string ID to use as notification title. Localized. // "new_chat" is "New chat" in Tindroid. "title_loc_key": "new_chat", // Android resource string ID to use as notification body. Localized. "body_loc_key": "" } } } }, { // Tinode Push Gateway, see https://github.com/tinode/chat/tree/master/server/push/tnpg. "name":"tnpg", "config": { // Disabled. Configure first then enable. "enabled": false, // Short name (URL) of the organization you registered at console.tinode.co. "org": "test", // Authentication token obtained from console.tinode.co "token": "jwt-security-token-obtained-from-console.tinode.co", } } ], // Configuration for voice and video calls. "webrtc": { // Disabled. Won't work without functioning ice_servers (see below). "enabled": false, // Timeout in seconds before a video/voice call is dropped if not answered. "call_establishment_timeout": 30, // Interactive Communication Establishment (ICE) STUN and TURN server configuration for video calls. // You need to configure your own servers or consider https://www.metered.ca/tools/openrelay/. // Video calls will not work if both parties are behind NAT and no ICE servers are configured. "ice_servers": [ { "urls": [ "stun:stun.example.com" ] }, { "username": "user-name-to-use-for-authentication-with-the-server", "credential": "your-password", "urls": [ "turn:turn.example.com:80?transport=udp", "turn:turn.example.com:3478?transport=udp", "turn:turn.example.com:80?transport=tcp", "turn:turn.example.com:3478?transport=tcp", "turns:turn.example.com:443?transport=tcp", "turns:turn.example.com:5349?transport=tcp" ] } ], // An alternative way to provide STUN/TURN configuration. "ice_servers_file": "/path/to/ice-servers-config.json" }, // Cluster-mode configuration. "cluster_config": { // Name of this node. Can be assigned from the command line as --cluster_self. // Empty string disables clustering. "self": "", // List of available nodes. "nodes": [ // Name and TCP address of every node in the cluster. The ports 12001..12003 // are cluster communication ports. They don't need to be exposed to end-users. {"name": "one", "addr":"localhost:12001"}, {"name": "two", "addr":"localhost:12002"}, {"name": "three", "addr":"localhost:12003"} ], // Failover config. No need to change unless you are doing something unusual. "failover": { // Failover is enabled. "enabled": true, // Time in milliseconds between heartbeats. "heartbeat": 100, // Initiate leader election when the leader is not available for this many heartbeats. "vote_after": 8, // Consider node failed when it missed this many heartbeats. "node_fail_after": 16 } }, // Configuration of plugins. "plugins": [ { // Enable or disable this plugin. "enabled": false, // Name of the plugin, must be unique. "name": "python_chat_bot", // Timeout in microseconds. "timeout": 20000, // Events to send to the plugin. "filters": { // Account creation events. "account": "C" }, // Error code to use in case plugin has failed; 0 means to ignore the failures. "failure_code": 0, // Text of an error message to report in case of plugin failure. "failure_text": null, // Address of the plugin. "service_addr": "tcp://localhost:40051" } ] } ================================================ FILE: server/topic.go ================================================ /****************************************************************************** * * Description : * An isolated communication channel (chat room, 1:1 conversation) for * usually multiple users. There is no communication across topics. * *****************************************************************************/ package main import ( "errors" "sort" "strings" "sync/atomic" "time" "github.com/tinode/chat/server/auth" "github.com/tinode/chat/server/logs" "github.com/tinode/chat/server/store" "github.com/tinode/chat/server/store/types" ) // Topic is an isolated communication channel type Topic struct { // Еxpanded/unique name of the topic. name string // For single-user topics session-specific topic name, such as 'me', // otherwise the same as 'name'. xoriginal string // Topic category cat types.TopicCat // Name of the master node for this topic if isProxy is true. masterNode string // Time when the topic was first created. created time.Time // Time when the topic was last updated. updated time.Time // Time of the last outgoing message. touched time.Time // Server-side ID of the last data message lastID int // ID of the deletion operation. Not an ID of the message. delID int // Total count of subscribers (excluding deleted). // This is different from subsCount() for channels. subCnt int // Last published userAgent ('me' topic only) userAgent string // User ID of the topic owner/creator. Could be zero. owner types.Uid // Default access mode accessAuth types.AccessMode accessAnon types.AccessMode // Topic discovery tags tags []string // Auxiliary set of key-value pairs aux map[string]any // Topic's public data public any // Topic's trusted data trusted any // Topic's per-subscriber data perUser map[types.Uid]perUserData // Union of permissions across all users (used by proxy sessions with uid = 0). // These are used by master topics only (in the proxy-master topic context) // as a coarse-grained attempt to perform acs checks since proxy sessions "impersonate" // multiple normal sessions (uids) which may have different uids. modeWantUnion types.AccessMode modeGivenUnion types.AccessMode // User's contact list (not nil for 'me' topic only). // The map keys are UserIds for P2P topics and grpXXX for group topics. perSubs map[string]perSubsData // Sessions attached to this topic. The UID kept here may not match Session.uid if session is // subscribed on behalf of another user. sessions map[*Session]perSessionData // Present video call data. Null when there's no call in progress or being established. // Only available for p2p topics. currentCall *videoCall // Channel for receiving client messages from sessions or other topics, buffered = 256. clientMsg chan *ClientComMessage // Channel for receiving server messages generated on the server or received from other cluster nodes, buffered = 64. serverMsg chan *ServerComMessage // Channel for receiving {get}/{set}/{del} requests, buffered = 64 meta chan *ClientComMessage // Subscribe requests from sessions, buffered = 256 reg chan *ClientComMessage // Unsubscribe requests from sessions, buffered = 256 unreg chan *ClientComMessage // Session updates: background sessions coming online, User Agent changes. Buffered = 32 supd chan *sessionUpdate // Channel to terminate topic -- either the topic is deleted or system is being shut down. Buffered = 1. exit chan *shutDown // Channel to receive topic master responses (used only by proxy topics). proxy chan *ClusterResp // Channel to receive topic proxy service requests, e.g. sending deferred notifications. master chan *ClusterSessUpdate // Flag which tells topic lifecycle status: new, ready, paused, marked for deletion. status int32 // Channel functionality is enabled for the group topic. isChan bool // If isProxy == true, the actual topic is hosted by another cluster member. // The topic should: // 1. forward all messages to master // 2. route replies from the master to sessions. // 3. disconnect sessions at master's request. // 4. shut down the topic at master's request. // 5. aggregate access permissions on behalf of attached sessions. isProxy bool // Countdown timer for destroying the topic when there are no more attached sessions to it. killTimer *time.Timer // Countdown timer for terminating iniatated (but not established) calls. callEstablishmentTimer *time.Timer } // perUserData holds topic's cache of per-subscriber data type perUserData struct { // Count of subscription online and announced (presence not deferred). online int // Last t.lastId reported by user through {pres} as received or read recvID int readID int // ID of the latest Delete operation delID int private any modeWant types.AccessMode modeGiven types.AccessMode // P2P only: public any trusted any lastSeen *time.Time lastUA string topicName string deleted bool // The user is a channel subscriber. isChan bool } // perSubsData holds user's (on 'me' topic) cache of subscription data type perSubsData struct { // The other user's/topic's online status as seen by this user. online bool // True if we care about the updates from the other user/topic: (want&given).IsPresencer(). // Does not affect sending notifications from this user to other users. enabled bool } // Data related to a subscription of a session to a topic. type perSessionData struct { // ID of the subscribed user (asUid); not necessarily the session owner. // Could be zero for multiplexed sessions in cluster. uid types.Uid // This is a channel subscription isChanSub bool // IDs of subscribed users in a multiplexing session. muids []types.Uid } // Reasons why topic is being shut down. const ( // StopNone no reason given/default. StopNone = iota // StopShutdown terminated due to system shutdown. StopShutdown // StopDeleted terminated due to being deleted. StopDeleted // StopRehashing terminated due to cluster rehashing (moved to a different node). StopRehashing ) // Topic shutdown type shutDown struct { // Channel to report back completion of topic shutdown. Could be nil done chan<- bool // Topic is being deleted as opposite to total system shutdown reason int } // Session update: user agent change or background session becoming normal. // If sess is nil then user agent change, otherwise bg to fg update. type sessionUpdate struct { sess *Session userAgent string } var ( nilPresParams = &presParams{} nilPresFilters = &presFilters{} ) func (t *Topic) run(hub *Hub) { if !t.isProxy { t.runLocal(hub) } else { t.runProxy(hub) } } // getPerUserAcs returns `want` and `given` permissions for the given user id. func (t *Topic) getPerUserAcs(uid types.Uid) (types.AccessMode, types.AccessMode) { if uid.IsZero() { // For zero uids (typically for proxy sessions), return the union of all permissions. return t.modeWantUnion, t.modeGivenUnion } pud := t.perUser[uid] return pud.modeWant, pud.modeGiven } // passesPresenceFilters applies presence filters to `msg` // depending on per-user want and given acls for the provided `uid`. func (t *Topic) passesPresenceFilters(pres *MsgServerPres, uid types.Uid) bool { modeWant, modeGiven := t.getPerUserAcs(uid) // "gone" and "acs" notifications are sent even if the topic is muted. return ((modeGiven & modeWant).IsPresencer() || pres.What == "gone" || pres.What == "acs") && (pres.FilterIn == 0 || int(modeGiven&modeWant)&pres.FilterIn != 0) && (pres.FilterOut == 0 || int(modeGiven&modeWant)&pres.FilterOut == 0) } // userIsReader returns true if the user (specified by `uid`) may read the given topic. func (t *Topic) userIsReader(uid types.Uid) bool { modeWant, modeGiven := t.getPerUserAcs(uid) return (modeGiven & modeWant).IsReader() } // prepareBroadcastableMessage sets the topic field in `msg` depending on the uid and subscription type. func (t *Topic) prepareBroadcastableMessage(msg *ServerComMessage, uid types.Uid, isChanSub bool) { // We are only interested in broadcastable messages. if msg.Data == nil && msg.Pres == nil && msg.Info == nil { return } if (t.cat == types.TopicCatP2P && !uid.IsZero()) || (t.cat == types.TopicCatGrp && t.isChan) { // For p2p topics topic name is dependent on receiver. // Channel topics may be presented as grpXXX or chnXXX. var topicName string if isChanSub { topicName = types.GrpToChn(t.xoriginal) } else { topicName = t.original(uid) } switch { case msg.Data != nil: msg.Data.Topic = topicName case msg.Pres != nil: msg.Pres.Topic = topicName case msg.Info != nil: msg.Info.Topic = topicName } } // Send channel messages anonymously. if isChanSub && msg.Data != nil { msg.Data.From = "" } } // computePerUserAcsUnion computes want and given permissions unions over all topic's subscribers. func (t *Topic) computePerUserAcsUnion() { wantUnion := types.ModeNone givenUnion := types.ModeNone for _, pud := range t.perUser { if pud.isChan { continue } wantUnion |= pud.modeWant givenUnion |= pud.modeGiven } if t.isChan { // Apply standard channel permissions to channel topics. wantUnion |= types.ModeCChnReader givenUnion |= types.ModeCChnReader } t.modeWantUnion = wantUnion t.modeGivenUnion = givenUnion } // unregisterSession implements all logic following receipt of a leave // request via the Topic.unreg channel. func (t *Topic) unregisterSession(msg *ClientComMessage) { if t.currentCall != nil { shouldTerminateCall := false if msg.sess.isMultiplex() { // Check if any of the call party sessions is multiplexed over msg.sess. for _, p := range t.currentCall.parties { if p.sess.isProxy() && p.sess.multi == msg.sess { shouldTerminateCall = true break } } } else if _, found := t.currentCall.parties[msg.sess.sid]; found { // Normal session disconnecting from topic. Just terminate the call. shouldTerminateCall = true } if shouldTerminateCall { t.terminateCallInProgress(false) } } t.handleLeaveRequest(msg, msg.sess) if msg.init && msg.sess.inflightReqs != nil { // If it's a client initiated request. msg.sess.inflightReqs.Done() } // If there are no more subscriptions to this topic, start a kill timer if len(t.sessions) == 0 && t.cat != types.TopicCatSys { t.killTimer.Reset(idleMasterTopicTimeout) } } // registerSession handles a session join (registration) request // received via the Topic.reg channel. func (t *Topic) registerSession(msg *ClientComMessage) { // Request to add a connection to this topic if t.isInactive() { msg.sess.queueOut(ErrLockedReply(msg, types.TimeNow())) } else if msg.sess.getSub(t.name) != nil { // Session is already subscribed to topic. Subscription is checked in session.go, // but there is a gap between topic creation/un-pausing and processing the // first subscription request, before the topic is linked to session: a client // may send several subscription requests in that gap. msg.sess.queueOut(InfoAlreadySubscribed(msg.Id, msg.Original, msg.Timestamp)) } else { // The topic is alive, so stop the kill timer, if it's ticking. We don't want the topic to die // while processing the call. t.killTimer.Stop() if err := t.handleSubscription(msg); err == nil { if msg.Sub.Created { // Call plugins with the new topic pluginTopic(t, plgActCreate) } } else { if len(t.sessions) == 0 && t.cat != types.TopicCatSys { // Failed to subscribe, the topic is still inactive t.killTimer.Reset(idleMasterTopicTimeout) } logs.Warn.Printf("topic[%s] subscription failed %v, sid=%s", t.name, err, msg.sess.sid) } } if msg.sess.inflightReqs != nil { msg.sess.inflightReqs.Done() } } func (t *Topic) handleMetaGet(msg *ClientComMessage, asUid types.Uid, asChan bool, authLevel auth.Level) { if msg.MetaWhat&constMsgMetaDesc != 0 { if err := t.replyGetDesc(msg.sess, asUid, asChan, msg.Get.Desc, msg); err != nil { logs.Warn.Printf("topic[%s] meta.Get.Desc failed: %s", t.name, err) } } if msg.MetaWhat&constMsgMetaSub != 0 { if err := t.replyGetSub(msg.sess, asUid, authLevel, asChan, msg); err != nil { logs.Warn.Printf("topic[%s] meta.Get.Sub failed: %s", t.name, err) } } if msg.MetaWhat&constMsgMetaData != 0 { if err := t.replyGetData(msg.sess, asUid, asChan, msg.Get.Data, msg); err != nil { logs.Warn.Printf("topic[%s] meta.Get.Data failed: %s", t.name, err) } } if msg.MetaWhat&constMsgMetaDel != 0 { if err := t.replyGetDel(msg.sess, asUid, msg.Get.Del, msg); err != nil { logs.Warn.Printf("topic[%s] meta.Get.Del failed: %s", t.name, err) } } if msg.MetaWhat&constMsgMetaTags != 0 { if err := t.replyGetTags(msg.sess, asUid, msg); err != nil { logs.Warn.Printf("topic[%s] meta.Get.Tags failed: %s", t.name, err) } } if msg.MetaWhat&constMsgMetaCred != 0 { if err := t.replyGetCreds(msg.sess, asUid, msg); err != nil { logs.Warn.Printf("topic[%s] meta.Get.Creds failed: %s", t.name, err) } } if msg.MetaWhat&constMsgMetaAux != 0 { logs.Warn.Printf("topic[%s] handle getAux", t.name) if err := t.replyGetAux(msg.sess, asUid, msg); err != nil { logs.Warn.Printf("topic[%s] meta.Get.Aux failed: %s", t.name, err) } } } func (t *Topic) handleMetaSet(msg *ClientComMessage, asUid types.Uid, asChan bool, authLevel auth.Level) { if msg.MetaWhat&constMsgMetaDesc != 0 { if err := t.replySetDesc(msg.sess, asUid, asChan, authLevel, msg); err == nil { // Notify plugins of the update pluginTopic(t, plgActUpd) } else { logs.Warn.Printf("topic[%s] meta.Set.Desc failed: %v", t.name, err) } } if msg.MetaWhat&constMsgMetaSub != 0 { if err := t.replySetSub(msg.sess, msg, asChan); err != nil { logs.Warn.Printf("topic[%s] meta.Set.Sub failed: %v", t.name, err) } } if msg.MetaWhat&constMsgMetaTags != 0 { if err := t.replySetTags(msg.sess, asUid, msg); err != nil { logs.Warn.Printf("topic[%s] meta.Set.Tags failed: %v", t.name, err) } } if msg.MetaWhat&constMsgMetaCred != 0 { if err := t.replySetCred(msg.sess, asUid, authLevel, msg); err != nil { logs.Warn.Printf("topic[%s] meta.Set.Cred failed: %v", t.name, err) } } if msg.MetaWhat&constMsgMetaAux != 0 { if err := t.replySetAux(msg.sess, asUid, msg); err != nil { logs.Warn.Printf("topic[%s] meta.Set.Aux failed: %v", t.name, err) } } } func (t *Topic) handleMetaDel(msg *ClientComMessage, asUid types.Uid, asChan bool, authLevel auth.Level) { var err error switch msg.MetaWhat { case constMsgDelMsg: err = t.replyDelMsg(msg.sess, asUid, asChan, msg) case constMsgDelSub: err = t.replyDelSub(msg.sess, asUid, msg) case constMsgDelTopic: err = t.replyDelTopic(msg.sess, asUid, msg) case constMsgDelCred: err = t.replyDelCred(msg.sess, asUid, authLevel, msg) } if err != nil { logs.Warn.Printf("topic[%s] meta.Del failed: %v", t.name, err) } } // handleMeta implements logic handling meta requests // received via the Topic.meta channel. func (t *Topic) handleMeta(msg *ClientComMessage) { // Request to get/set topic metadata asUid := types.ParseUserId(msg.AsUser) authLevel := auth.Level(msg.AuthLvl) asChan, err := t.verifyChannelAccess(msg.Original) if err != nil { // User should not be able to address non-channel topic as channel. msg.sess.queueOut(ErrNotFoundReply(msg, types.TimeNow())) return } switch { case msg.Get != nil: // Get request t.handleMetaGet(msg, asUid, asChan, authLevel) case msg.Set != nil: // Set request t.handleMetaSet(msg, asUid, asChan, authLevel) case msg.Del != nil: // Del request t.handleMetaDel(msg, asUid, asChan, authLevel) } } func (t *Topic) handleSessionUpdate(upd *sessionUpdate, currentUA *string, uaTimer *time.Timer) { if upd.sess != nil { // 'me' & 'grp' only. Background session timed out and came online. t.sessToForeground(upd.sess) } else if *currentUA != upd.userAgent { if t.cat != types.TopicCatMe { logs.Warn.Panicln("invalid topic category in UA update", t.name) } // 'me' only. Process an update to user agent from one of the sessions. *currentUA = upd.userAgent uaTimer.Reset(uaTimerDelay) } } func (t *Topic) handleUATimerEvent(currentUA string) { // Publish user agent changes after a delay if currentUA == "" || currentUA == t.userAgent { return } t.userAgent = currentUA t.presUsersOfInterest("ua", t.userAgent) } func (t *Topic) handleTopicTimeout(hub *Hub, currentUA string, uaTimer, defrNotifTimer *time.Timer) { // Topic timeout hub.unreg <- &topicUnreg{rcptTo: t.name} defrNotifTimer.Stop() switch t.cat { case types.TopicCatMe: uaTimer.Stop() t.presUsersOfInterest("off", currentUA) case types.TopicCatGrp: t.presSubsOffline("off", nilPresParams, nilPresFilters, nilPresFilters, "", false) } } func (t *Topic) handleTopicTermination(sd *shutDown) { // Handle four cases: // 1. Topic is shutting down by timer due to inactivity (reason == StopNone) // 2. Topic is being deleted (reason == StopDeleted) // 3. System shutdown (reason == StopShutdown, done != nil). // 4. Cluster rehashing (reason == StopRehashing) switch sd.reason { case StopDeleted: if t.cat == types.TopicCatGrp { t.presSubsOffline("gone", nilPresParams, nilPresFilters, nilPresFilters, "", false) } // P2P users get "off+remove" earlier in the process // Inform plugins that the topic is deleted pluginTopic(t, plgActDel) case StopRehashing: // Must send individual messages to sessions because normal sending through the topic's // broadcast channel won't work - it will be shut down too soon. t.presSubsOnlineDirect("term", nilPresParams, nilPresFilters, "") } // In case of a system shutdown don't bother with notifications. They won't be delivered anyway. // Tell sessions to remove the topic for s := range t.sessions { s.detachSession(t.name) } if t.cat == types.TopicCatGrp { // Update topic subscriber count. if err := store.Topics.UpdateSubCnt(t.name); err != nil { logs.Warn.Println("topic update sub cnt:", err) } } usersRegisterTopic(t, false) // Report completion back to sender, if 'done' is not nil. if sd.done != nil { sd.done <- true } } func (t *Topic) runLocal(hub *Hub) { // Kills topic after a period of inactivity. t.killTimer = time.NewTimer(time.Hour) t.killTimer.Stop() // Notifies about user agent change. 'me' only uaTimer := time.NewTimer(time.Minute) var currentUA string uaTimer.Stop() // Ticker for deferred presence notifications. defrNotifTimer := time.NewTimer(time.Millisecond * 500) t.callEstablishmentTimer = time.NewTimer(time.Second) t.callEstablishmentTimer.Stop() for { select { case msg := <-t.reg: t.registerSession(msg) case msg := <-t.unreg: t.unregisterSession(msg) case msg := <-t.clientMsg: t.handleClientMsg(msg) case msg := <-t.serverMsg: t.handleServerMsg(msg) case meta := <-t.meta: t.handleMeta(meta) case upd := <-t.supd: t.handleSessionUpdate(upd, ¤tUA, uaTimer) case <-uaTimer.C: t.handleUATimerEvent(currentUA) case <-t.killTimer.C: t.handleTopicTimeout(hub, currentUA, uaTimer, defrNotifTimer) case <-t.callEstablishmentTimer.C: t.terminateCallInProgress(true) case sd := <-t.exit: t.handleTopicTermination(sd) return } } } // handleClientMsg is the top-level handler of messages received by the topic from sessions. func (t *Topic) handleClientMsg(msg *ClientComMessage) { if msg.Pub != nil { t.handlePubBroadcast(msg) } else if msg.Note != nil { t.handleNoteBroadcast(msg) } else { // TODO(gene): maybe remove this panic. logs.Err.Panic("topic: wrong client message type for broadcasting", t.name) } } // handleServerMsg is the top-level handler of messages generated at the server. func (t *Topic) handleServerMsg(msg *ServerComMessage) { // Server-generated message: {info} or {pres}. if t.isInactive() { // Ignore message - the topic is paused or being deleted. return } if msg.Pres != nil { t.handlePresence(msg) } else if msg.Info != nil { t.broadcastToSessions(msg) } else { // TODO(gene): maybe remove this panic. logs.Err.Panic("topic: wrong server message type for broadcasting", t.name) } } // Session subscribed to a topic, created == true if topic was just created and {pres} needs to be announced func (t *Topic) handleSubscription(msg *ClientComMessage) error { asUid := types.ParseUserId(msg.AsUser) authLevel := auth.Level(msg.AuthLvl) asChan, err := t.verifyChannelAccess(msg.Original) if err != nil { // User should not be able to address non-channel topic as channel. msg.sess.queueOut(ErrNotFoundReply(msg, types.TimeNow())) return err } if err := t.subscriptionReply(asChan, msg); err != nil { return err } msgsub := msg.Sub getWhat := 0 if msgsub.Get != nil { getWhat = parseMsgClientMeta(msgsub.Get.What) } if getWhat&constMsgMetaDesc != 0 { // Send get.desc as a {meta} packet. if err := t.replyGetDesc(msg.sess, asUid, asChan, msgsub.Get.Desc, msg); err != nil { logs.Warn.Printf("topic[%s] handleSubscription Get.Desc failed: %v sid=%s", t.name, err, msg.sess.sid) } } if getWhat&constMsgMetaSub != 0 { // Send get.sub response as a separate {meta} packet if err := t.replyGetSub(msg.sess, asUid, authLevel, asChan, msg); err != nil { logs.Warn.Printf("topic[%s] handleSubscription Get.Sub failed: %v sid=%s", t.name, err, msg.sess.sid) } } if getWhat&constMsgMetaTags != 0 { // Send get.tags response as a separate {meta} packet if err := t.replyGetTags(msg.sess, asUid, msg); err != nil { logs.Warn.Printf("topic[%s] handleSubscription Get.Tags failed: %v sid=%s", t.name, err, msg.sess.sid) } } if getWhat&constMsgMetaCred != 0 { // Send get.tags response as a separate {meta} packet if err := t.replyGetCreds(msg.sess, asUid, msg); err != nil { logs.Warn.Printf("topic[%s] handleSubscription Get.Cred failed: %v sid=%s", t.name, err, msg.sess.sid) } } if getWhat&constMsgMetaAux != 0 { // Send get.aux response as a separate {meta} packet if err := t.replyGetAux(msg.sess, asUid, msg); err != nil { logs.Warn.Printf("topic[%s] handleSubscription Get.Aux failed: %v sid=%s", t.name, err, msg.sess.sid) } } if getWhat&constMsgMetaData != 0 { // Send get.data response as {data} packets if err := t.replyGetData(msg.sess, asUid, asChan, msgsub.Get.Data, msg); err != nil { logs.Warn.Printf("topic[%s] handleSubscription Get.Data failed: %v sid=%s", t.name, err, msg.sess.sid) } } if getWhat&constMsgMetaDel != 0 { // Send get.del response as a separate {meta} packet if err := t.replyGetDel(msg.sess, asUid, msgsub.Get.Del, msg); err != nil { logs.Warn.Printf("topic[%s] handleSubscription Get.Del failed: %v sid=%s", t.name, err, msg.sess.sid) } } return nil } // handleLeaveRequest processes a session leave request. func (t *Topic) handleLeaveRequest(msg *ClientComMessage, sess *Session) { // Remove connection from topic; session may continue to function now := types.TimeNow() var asUid types.Uid var asChan bool if msg.init { asUid = types.ParseUserId(msg.AsUser) var err error asChan, err = t.verifyChannelAccess(msg.Original) if err != nil { // Group topic cannot be addressed as channel unless channel functionality is enabled. sess.queueOut(ErrNotFoundReply(msg, now)) } } if t.isInactive() { if !asUid.IsZero() && msg.init { sess.queueOut(ErrLockedReply(msg, now)) } return } // User wants to leave and unsubscribe. if msg.init && msg.Leave.Unsub { // asUid must not be Zero. if err := t.replyLeaveUnsub(sess, msg, asUid); err != nil { logs.Err.Println("failed to unsub", err, sess.sid) } return } // User wants to leave without unsubscribing. if pssd, _ := t.remSession(sess, asUid); pssd != nil { if !sess.isProxy() { sess.delSub(t.name) } if pssd.isChanSub != asChan { // Cannot address non-channel subscription as channel and vice versa. if msg.init { // Group topic cannot be addressed as channel unless channel functionality is enabled. sess.queueOut(ErrNotFoundReply(msg, now)) } return } var uid types.Uid if sess.isProxy() { // Multiplexing session, multiple UIDs. uid = asUid } else { // Simple session, single UID. uid = pssd.uid } var pud perUserData // uid may be zero when a proxy session is trying to terminate (it called unsubAll). if !uid.IsZero() { // UID not zero: one user removed. pud = t.perUser[uid] if !sess.background { pud.online-- t.perUser[uid] = pud } } else if len(pssd.muids) > 0 { // UID is zero: multiplexing session is dropped altogether. // Using new 'uid' and 'pud' variables. for _, uid := range pssd.muids { pud := t.perUser[uid] pud.online-- t.perUser[uid] = pud } } else if !sess.isCluster() { logs.Warn.Panic("cannot determine uid: leave req", msg, sess) } switch t.cat { case types.TopicCatMe: mrs := t.mostRecentSession() if mrs == nil { // Last session mrs = sess } else { // Change UA to the most recent live session and announce it. Don't block. select { case t.supd <- &sessionUpdate{userAgent: mrs.userAgent}: default: } } meUid := uid if meUid.IsZero() && len(pssd.muids) > 0 { // The entire multiplexing session is being dropped. Need to find owner's UID. // len(pssd.muids) could be zero if the session was a background session. meUid = pssd.muids[0] } if !meUid.IsZero() { // Update user's last online timestamp & user agent. Only one user can be subscribed to 'me' topic. if err := store.Users.UpdateLastSeen(meUid, mrs.userAgent, now); err != nil { logs.Warn.Println("user update last seen:", err) } } case types.TopicCatFnd: // FIXME: this does not work correctly in case of a multiplexing session. // Remove ephemeral query. t.fndRemovePublic(sess) case types.TopicCatGrp: // Subscriber is going offline in the topic: notify other subscribers who are currently online. readFilter := &presFilters{filterIn: types.ModeRead} if !uid.IsZero() { if pud.online == 0 { if asChan { // Simply delete record from perUserData delete(t.perUser, uid) } else { t.presSubsOnline("off", uid.UserId(), nilPresParams, readFilter, "") } } } else if len(pssd.muids) > 0 { for _, uid := range pssd.muids { if t.perUser[uid].online == 0 { if asChan { // delete record from perUserData delete(t.perUser, uid) } else { t.presSubsOnline("off", uid.UserId(), nilPresParams, readFilter, "") } } } } } if !uid.IsZero() { // Respond if contains an id. if msg.init { sess.queueOut(NoErrReply(msg, now)) } } } } // sessToForeground updates perUser online status accounting and fires due // deferred notifications for the provided session. func (t *Topic) sessToForeground(sess *Session) { s := sess if s.multi != nil { s = s.multi } if pssd, ok := t.sessions[s]; ok && !pssd.isChanSub { uid := pssd.uid if s.isMultiplex() { // If 's' is a multiplexing session, then sess is a proxy and it contains correct UID. // Add UID to the list of online users. uid = sess.uid pssd.muids = append(pssd.muids, uid) } // Mark user as online pud := t.perUser[uid] pud.online++ t.perUser[uid] = pud t.sendSubNotifications(uid, sess.sid, sess.userAgent) } } // Send immediate presence notification in response to a subscription. // Send push notification to the P2P counterpart. // In case of a new channel subscription subscribe user to an FCM topic. // These notifications are always sent immediately even if background is requested. func (t *Topic) sendImmediateSubNotifications(asUid types.Uid, acs *MsgAccessMode, sreg *ClientComMessage, now time.Time) { modeWant, _ := types.ParseAcs([]byte(acs.Want)) modeGiven, _ := types.ParseAcs([]byte(acs.Given)) mode := modeWant & modeGiven asChan := t.isChan && types.IsChannel(sreg.Original) if t.cat == types.TopicCatP2P { uid2 := t.p2pOtherUser(asUid) pud2 := t.perUser[uid2] mode2 := pud2.modeGiven & pud2.modeWant if pud2.deleted { mode2 = types.ModeInvalid } // Inform the other user that the topic was just created. if sreg.Sub.Created { t.presSingleUserOffline(uid2, mode2, "acs", &presParams{ dWant: pud2.modeWant.String(), dGiven: pud2.modeGiven.String(), actor: asUid.UserId(), }, "", false) } if sreg.Sub.Newsub { // Notify current user's 'me' topic to accept notifications from user2 t.presSingleUserOffline(asUid, mode, "?none+en", nilPresParams, "", false) // Initiate exchange of 'online' status with the other user. // We don't know if the current user is online in the 'me' topic, // so sending an '?unkn' status to user2. His 'me' topic // will reply with user2's status and request an actual status from user1. status := "?unkn" if mode2.IsPresencer() { // If user2 should receive notifications, enable it. status += "+en" } t.presSingleUserOffline(uid2, mode2, status, nilPresParams, "", false) // Also send a push notification to the other user. sendPush(t.pushForP2PSub(asUid, uid2, pud2.modeWant, pud2.modeGiven, now)) } } else if t.cat == types.TopicCatGrp && !asChan && sreg.Sub.Newsub { // For new group subscriptions, notify other group members. sendPush(t.pushForGroupSub(asUid, now)) } // newsub could be true only for p2p and group topics, no need to check topic category explicitly. if sreg.Sub.Newsub { // Notify creator's other sessions that the subscription (or the entire topic) was created. t.presSingleUserOffline(asUid, mode, "acs", &presParams{ dWant: acs.Want, dGiven: acs.Given, actor: asUid.UserId(), }, sreg.sess.sid, false) if asChan { t.channelSubUnsub(asUid, true) } } } // Send immediate or deferred presence notification in response to a subscription. // Not used by channels. func (t *Topic) sendSubNotifications(asUid types.Uid, sid, userAgent string) { switch t.cat { case types.TopicCatMe: // Notify user's contact that the given user is online now. if !t.isLoaded() { t.markLoaded() if err := t.loadContacts(asUid); err != nil { logs.Err.Println("topic: failed to load contacts", t.name, err.Error()) } // User online: notify users of interest without forcing response (no +en here). t.presUsersOfInterest("on", userAgent) } case types.TopicCatGrp: pud := t.perUser[asUid] if pud.isChan { // Not sendng notifications for channel readers. return } // Enable notifications for a new group topic, if appropriate. if !t.isLoaded() { t.markLoaded() status := "on" if (pud.modeGiven & pud.modeWant).IsPresencer() { status += "+en" } // Notify topic subscribers that the topic is online now. t.presSubsOffline(status, nilPresParams, nilPresFilters, nilPresFilters, "", false) } else if pud.online == 1 { // If this is the first session of the user in the topic. // Notify other online group members that the user is online now. t.presSubsOnline("on", asUid.UserId(), nilPresParams, &presFilters{filterIn: types.ModeRead}, sid) } } } // Saves a new message (defined by head, content and attachments) in the topic // in response to a client request (msg, asUid) and broadcasts it to the attached sessions. func (t *Topic) saveAndBroadcastMessage(msg *ClientComMessage, asUid types.Uid, noEcho bool, attachments []string, head map[string]any, content any) error { pud, userFound := t.perUser[asUid] // Anyone is allowed to post to 'sys' topic. if t.cat != types.TopicCatSys { // If it's not 'sys' check write permission. if !(pud.modeWant & pud.modeGiven).IsWriter() { msg.sess.queueOut(ErrPermissionDenied(msg.Id, t.original(asUid), msg.Timestamp)) return types.ErrPermissionDenied } } if msg.sess != nil && msg.sess.uid != asUid { // The "sender" header contains ID of the user who sent the message on behalf of asUid. if head == nil { head = map[string]any{} } head["sender"] = msg.sess.uid.UserId() } else if head != nil { // Make sure the received Head does not include a fake "sender" header. delete(head, "sender") } markedReadBySender := false if err, unreadUpdated := store.Messages.Save( &types.Message{ ObjHeader: types.ObjHeader{CreatedAt: msg.Timestamp}, SeqId: t.lastID + 1, Topic: t.name, From: asUid.String(), Head: head, Content: content, }, attachments, (pud.modeGiven & pud.modeWant).IsReader()); err != nil { logs.Warn.Printf("topic[%s]: failed to save message: %v", t.name, err) msg.sess.queueOut(ErrUnknown(msg.Id, t.original(asUid), msg.Timestamp)) return err } else { markedReadBySender = unreadUpdated } t.lastID++ t.touched = msg.Timestamp if userFound { pud.readID = t.lastID pud.recvID = t.lastID t.perUser[asUid] = pud } if msg.Id != "" && msg.sess != nil { reply := NoErrAccepted(msg.Id, t.original(asUid), msg.Timestamp) reply.Ctrl.Params = map[string]any{"seq": t.lastID} msg.sess.queueOut(reply) } data := &ServerComMessage{ Data: &MsgServerData{ Topic: msg.Original, From: msg.AsUser, Timestamp: msg.Timestamp, SeqId: t.lastID, Head: head, Content: content, }, // Internal-only values. Id: msg.Id, RcptTo: msg.RcptTo, AsUser: msg.AsUser, Timestamp: msg.Timestamp, sess: msg.sess, } if noEcho { data.SkipSid = msg.sess.sid } // Message sent: notify offline 'R' subscrbers on 'me'. t.presSubsOffline("msg", &presParams{seqID: t.lastID, actor: msg.AsUser}, &presFilters{filterIn: types.ModeRead}, nilPresFilters, "", true) // Tell the plugins that a message was accepted for delivery pluginMessage(data.Data, plgActCreate) t.broadcastToSessions(data) // sendPush will update unread message count and send push notification. if pushRcpt := t.pushForData(asUid, data.Data, markedReadBySender); pushRcpt != nil { sendPush(pushRcpt) } return nil } // handlePubBroadcast fans out {pub} -> {data} messages to recipients in a master topic. // This is a NON-proxy broadcast. func (t *Topic) handlePubBroadcast(msg *ClientComMessage) { asUid := types.ParseUserId(msg.AsUser) if t.isInactive() { // Ignore broadcast - topic is paused or being deleted. msg.sess.queueOut(ErrLocked(msg.Id, t.original(asUid), msg.Timestamp)) return } if t.isReadOnly() { msg.sess.queueOut(ErrPermissionDenied(msg.Id, t.original(asUid), msg.Timestamp)) return } isCall := msg.Pub.Head != nil && msg.Pub.Head["webrtc"] != nil if isCall { if len(globals.iceServers) == 0 { msg.sess.queueOut(ErrNotImplementedReply(msg, types.TimeNow())) return } if t.cat != types.TopicCatP2P { msg.sess.queueOut(ErrPermissionDeniedReply(msg, types.TimeNow())) return } if t.currentCall != nil { msg.sess.queueOut(ErrCallBusyReply(msg, types.TimeNow())) return } } // Save to DB at master topic. var attachments []string if msg.Extra != nil && len(msg.Extra.Attachments) > 0 { attachments = msg.Extra.Attachments } if err := t.saveAndBroadcastMessage(msg, asUid, msg.Pub.NoEcho, attachments, msg.Pub.Head, msg.Pub.Content); err != nil { logs.Err.Printf("topic[%s]: failed to save messagge - %s", t.name, err) return } if isCall { t.handleCallInvite(msg, asUid) } } // handleNoteBroadcast fans out {note} -> {info} messages to recipients in a master topic. // This is a NON-proxy broadcast (at master topic). func (t *Topic) handleNoteBroadcast(msg *ClientComMessage) { if t.isInactive() { // Ignore broadcast - topic is paused or being deleted. return } if msg.Note.SeqId > t.lastID { // Drop bogus read notification return } asChan, err := t.verifyChannelAccess(msg.Original) if err != nil { // Silently drop invalid notification. return } asUid := types.ParseUserId(msg.AsUser) pud := t.perUser[asUid] mode := pud.modeGiven & pud.modeWant if pud.deleted { mode = types.ModeInvalid } switch msg.Note.What { case "kp", "kpa", "kpv": // Filter out "kp*" from users with no 'W' permission (or people without a subscription). if !mode.IsWriter() || t.isReadOnly() { return } case "read", "recv": // Filter out "read/recv" from users with no 'R' permission (or people without a subscription). if !mode.IsReader() { return } case "call": // Handle calls separately. t.handleCallEvent(msg) return } var read, recv, unread, seq int switch msg.Note.What { case "read": if msg.Note.SeqId <= pud.readID { // No need to report stale or bogus read status. return } // The number of unread messages has decreased, negative value. unread = pud.readID - msg.Note.SeqId pud.readID = msg.Note.SeqId if pud.readID > pud.recvID { pud.recvID = pud.readID } read = pud.readID seq = read case "recv": if msg.Note.SeqId <= pud.recvID { // Stale or bogus recv status. return } pud.recvID = msg.Note.SeqId if pud.readID > pud.recvID { pud.recvID = pud.readID } recv = pud.recvID seq = recv } if seq > 0 { topicName := t.name if asChan { topicName = msg.Note.Topic } upd := map[string]any{} if recv > 0 { upd["RecvSeqId"] = recv } if read > 0 { upd["ReadSeqId"] = read } if err := store.Subs.Update(topicName, asUid, upd); err != nil { logs.Warn.Printf("topic[%s]: failed to update SeqRead/Recv counter: %v", t.name, err) return } // Read/recv updated: notify user's other sessions of the change t.presPubMessageCount(asUid, mode, read, recv, msg.sess.sid) if read > 0 { // Send push notification to other user devices. sendPush(t.pushForReadRcpt(asUid, read, msg.Timestamp)) } // Update cached count of unread messages (not tracking unread messages fror channels). if !asChan { usersUpdateUnread(asUid, unread, true) } } if asChan { // No need to forward {note} to other subscribers in channels return } if seq > 0 { t.perUser[asUid] = pud } // Read/recv/kp: notify users offline in the topic on their 'me'. t.infoSubsOffline(asUid, msg.Note.What, seq, msg.sess.sid) info := &ServerComMessage{ Info: &MsgServerInfo{ Topic: msg.Original, From: msg.AsUser, What: msg.Note.What, SeqId: msg.Note.SeqId, }, RcptTo: msg.RcptTo, AsUser: msg.AsUser, Timestamp: msg.Timestamp, SkipSid: msg.sess.sid, sess: msg.sess, } t.broadcastToSessions(info) } // handlePresence fans out {pres} messages to recipients in topic. func (t *Topic) handlePresence(msg *ServerComMessage) { what := t.procPresReq(msg.Pres.Src, msg.Pres.What, msg.Pres.WantReply) if t.xoriginal != msg.Pres.Topic || what == "" { // This is just a request for status, don't forward it to sessions return } // "what" may have changed, i.e. unset or "+command" removed ("on+en" -> "on") msg.Pres.What = what t.broadcastToSessions(msg) } // broadcastToSessions writes message to attached sessions. func (t *Topic) broadcastToSessions(msg *ServerComMessage) { // List of sessions to be dropped. var dropSessions []*Session // Broadcast the message. Only {data}, {pres}, {info} are broadcastable. // {meta} and {ctrl} are sent to the session only for sess, pssd := range t.sessions { // Send all messages to multiplexing session. if !sess.isMultiplex() { if sess.sid == msg.SkipSid { continue } if msg.Pres != nil { // Skip notifying - already notified on topic. if msg.Pres.SkipTopic != "" && sess.getSub(msg.Pres.SkipTopic) != nil { continue } // Notification addressed to a single user only. if msg.Pres.SingleUser != "" && pssd.uid.UserId() != msg.Pres.SingleUser { continue } // Notification should skip a single user. if msg.Pres.ExcludeUser != "" && pssd.uid.UserId() == msg.Pres.ExcludeUser { continue } // Check presence filters if !t.passesPresenceFilters(msg.Pres, pssd.uid) { continue } } else { if msg.Info != nil { // Don't forward read receipts and key presses to channel readers and those without the R permission. // OK to forward with Src != "" because it's sent from another topic to 'me', permissions already // checked there. if msg.Info.Src == "" && (pssd.isChanSub || !t.userIsReader(pssd.uid)) { continue } // Skip notifying - already notified on topic. if msg.Info.SkipTopic != "" && sess.getSub(msg.Info.SkipTopic) != nil { continue } // Don't send key presses from one user's session to the other sessions of the same user. if msg.Info.What == "kp" && msg.Info.From == pssd.uid.UserId() { continue } } else if !t.userIsReader(pssd.uid) && !pssd.isChanSub { // Skip {data} if the user has no Read permission and not a channel reader. continue } } } else if pssd.isChanSub && types.IsChannel(sess.sid) { // If it's a chnX multiplexing session, check if there's a corresponding // grpX multiplexing session as we don't want to send the message to both. grpSid := types.ChnToGrp(sess.sid) if grpSess := globals.sessionStore.Get(grpSid); grpSess != nil && grpSess.isMultiplex() { // If grpX multiplexing session's attached to topic, skip this chnX session // (message will be routed to the topic proxy via the grpX session). if _, attached := t.sessions[grpSess]; attached { continue } } } // Make a copy of msg since messages sent to sessions differ. msgCopy := msg.copy() // Topic name may be different depending on the user to which the `sess` belongs. t.prepareBroadcastableMessage(msgCopy, pssd.uid, pssd.isChanSub) // Send message to session. if !sess.queueOut(msgCopy) { logs.Warn.Printf("topic[%s]: connection stuck, detaching - %s", t.name, sess.sid) dropSessions = append(dropSessions, sess) } } // Drop "bad" sessions. for _, sess := range dropSessions { // The whole session is being dropped, so ClientComMessage.init is false. // keep redundant init: false so it can be searched for. t.unregisterSession(&ClientComMessage{sess: sess, init: false}) } } // subscriptionReply generates a response to a subscription request func (t *Topic) subscriptionReply(asChan bool, msg *ClientComMessage) error { // The topic is already initialized by the Hub msgsub := msg.Sub // For newly created topics report topic creation time. var now time.Time if msgsub.Created { now = t.updated } else { now = types.TimeNow() } asUid := types.ParseUserId(msg.AsUser) if !msgsub.Newsub && (t.cat == types.TopicCatP2P || t.cat == types.TopicCatGrp) { // Check if this is a new subscription (P2P & GRP only. SLF, SYS are excluded here). pud, found := t.perUser[asUid] msgsub.Newsub = !found || pud.deleted } var private any var mode string if msgsub.Set != nil { if msgsub.Set.Sub != nil { if msgsub.Set.Sub.User != "" { msg.sess.queueOut(ErrMalformedReply(msg, now)) return errors.New("user id must not be specified") } mode = msgsub.Set.Sub.Mode } if msgsub.Set.Desc != nil { private = msgsub.Set.Desc.Private } } var err error var modeChanged *MsgAccessMode // Create new subscription or modify an existing one. if modeChanged, err = t.thisUserSub(msg.sess, msg, asUid, asChan, mode, private); err != nil { return err } hasJoined := true if modeChanged != nil { if acs, err := types.ParseAcs([]byte(modeChanged.Mode)); err == nil { hasJoined = acs.IsJoiner() } } if hasJoined { // Subscription successfully created. Link topic to session. msg.sess.addSub(t.name, &Subscription{ broadcast: t.clientMsg, done: t.unreg, meta: t.meta, supd: t.supd, }) t.addSession(msg.sess, asUid, asChan) // The user is online in the topic. Increment the counter if notifications are not deferred. if !msg.sess.background { userData := t.perUser[asUid] userData.online++ t.perUser[asUid] = userData } if t.cat == types.TopicCatGrp && msgsub.Newsub { // Increment subscriber count for new group subscriptions only. t.subCnt++ } } params := map[string]any{} // Report back the assigned access mode. if modeChanged != nil { params["acs"] = modeChanged } toriginal := t.original(asUid) // When a group topic is created, it's given a temporary name by the client. // Then this name changes. Report back the original name here. if msgsub.Created && msg.Original != toriginal { params["tmpname"] = msg.Original // The new123ABC name is no longer useful after this. msg.Original = toriginal } if len(params) == 0 { // Don't send empty params '{}' msg.sess.queueOut(NoErr(msg.Id, toriginal, now)) } else { msg.sess.queueOut(NoErrParams(msg.Id, toriginal, now, params)) } // Some notifications are always sent immediately. if modeChanged != nil { t.sendImmediateSubNotifications(asUid, modeChanged, msg, now) } if !msg.sess.background && hasJoined { // Other notifications are also sent immediately for foreground sessions. t.sendSubNotifications(asUid, msg.sess.sid, msg.sess.userAgent) } return nil } // User requests or updates a self-subscription to a topic. Called as a // result of {sub} or {meta set=sub}. // Returns new access mode as *MsgAccessMode if user's access mode has changed, nil otherwise. // // sess - originating session // pkt - client message which triggered this request; {sub} or {set} // asUid - id of the user making the request // asChan - true if the user is subscribing to a channel topic // want - requested access mode // private - private value to assign to the subscription // background - presence notifications are deferred // // Handle these cases: // A. User is trying to subscribe for the first time (no subscription). // A.1 Normal user is subscribing to the topic. // A.2 Reader is joining the channel. // B. User is already subscribed, just joining without changing anything. // C. User is responding to an earlier invite (modeWant was "N" in subscription). // D. User is already subscribed, changing modeWant. // E. User is accepting ownership transfer (requesting ownership transfer is not permitted). // In case of a group topic the user may be a reader or a full subscriber. func (t *Topic) thisUserSub(sess *Session, pkt *ClientComMessage, asUid types.Uid, asChan bool, want string, private any) (*MsgAccessMode, error) { now := types.TimeNow() asLvl := auth.Level(pkt.AuthLvl) // Access mode values as they were before this request was processed. oldWant := types.ModeNone oldGiven := types.ModeNone // Parse access mode requested by the user modeWant := types.ModeUnset if want != "" { if err := modeWant.UnmarshalText([]byte(want)); err != nil { sess.queueOut(ErrMalformedReply(pkt, now)) return nil, err } } var err error // Check if it's an attempt at a new subscription to the topic / a first connection of a channel reader // (channel readers are not permanently cached). // It could be an actual subscription (IsJoiner() == true) or a ban (IsJoiner() == false). userData, existingSub := t.perUser[asUid] if !existingSub || userData.deleted { // New subscription or a not yet cached channel reader, either new or existing. // Check if the max number of subscriptions is already reached. if t.cat == types.TopicCatGrp && !asChan && t.subsCount() >= globals.maxSubscriberCount { sess.queueOut(ErrPolicyReply(pkt, now)) return nil, errors.New("max subscription count exceeded") } var sub *types.Subscription tname := t.name if t.cat == types.TopicCatP2P { // P2P could be here only if it was previously deleted. I.e. existingSub is always true for P2P. if modeWant != types.ModeUnset { userData.modeWant = modeWant } // If no modeWant is provided, leave existing one unchanged. // Make sure the user is not asking for unreasonable permissions userData.modeWant = (userData.modeWant & globals.typesModeCP2P) | types.ModeApprove } else if t.cat == types.TopicCatSys { if asLvl != auth.LevelRoot { sess.queueOut(ErrPermissionDeniedReply(pkt, now)) return nil, errors.New("subscription to 'sys' topic requires root access level") } // Assign default access levels userData.modeWant = types.ModeCSys userData.modeGiven = types.ModeCSys if modeWant != types.ModeUnset { userData.modeWant = (modeWant & types.ModeCSys) | types.ModeWrite | types.ModeJoin } } else if asChan { userData.isChan = true // Check if user is already subscribed. sub, err = store.Subs.Get(pkt.Original, asUid, false) if err != nil { sess.queueOut(ErrUnknownReply(pkt, now)) return nil, err } // Given mode is immutable. oldGiven = types.ModeCChnReader userData.modeGiven = types.ModeCChnReader if sub != nil { // Subscription exists, read old access mode. oldWant = sub.ModeWant } else { // Subscription not found, use default. oldWant = types.ModeCChnReader } if modeWant != types.ModeUnset { // New access mode is explicitly assigned. userData.modeWant = (modeWant & types.ModeCChnReader) | types.ModeRead | types.ModeJoin } else { // Default: unchanged. userData.modeWant = oldWant } // User is subscribed to chnXXX, not grpXXX. tname = pkt.Original } else { // All other topic types. if !existingSub { // Check if the user has been subscribed previously and if so, use previous modeGiven. // Otherwise the user may delete subscription and resubscribe to avoid being blocked. sub, err = store.Subs.Get(t.name, asUid, true) if err != nil { sess.queueOut(ErrUnknownReply(pkt, now)) return nil, err } if sub != nil { userData.modeGiven = sub.ModeGiven } else { // If no mode was previously given, give default access. userData.modeGiven = types.ModeUnset } } if userData.modeGiven == types.ModeUnset { // New user: default access. userData.modeGiven = t.accessFor(asLvl) } if modeWant == types.ModeUnset { // User wants default access mode. userData.modeWant = t.accessFor(asLvl) } else { userData.modeWant = modeWant } } // Reject new subscription: 'given' permissions have no 'J'. if !userData.modeGiven.IsJoiner() { sess.queueOut(ErrPermissionDeniedReply(pkt, now)) return nil, errors.New("subscription rejected due to permissions") } // Undelete. if userData.deleted { userData.deleted = false userData.delID, userData.readID, userData.recvID = 0, 0, 0 } if isNullValue(private) { private = nil } userData.private = private // Add subscription to database, if missing. if sub == nil || sub.DeletedAt != nil { sub = &types.Subscription{ User: asUid.String(), Topic: tname, ModeWant: userData.modeWant, ModeGiven: userData.modeGiven, Private: userData.private, } if err := store.Subs.Create(sub); err != nil { sess.queueOut(ErrUnknownReply(pkt, now)) return nil, err } } else if asChan && userData.modeWant != oldWant { // Channel reader changed access mode, save changed mode to db. if err := store.Subs.Update(tname, asUid, map[string]any{"ModeWant": userData.modeWant}); err != nil { sess.queueOut(ErrUnknownReply(pkt, now)) return nil, err } // Enable or disable fcm push notifications for the subsciption. t.channelSubUnsub(asUid, userData.modeWant.IsPresencer()) } if asChan { if userData.modeWant != oldWant { pluginSubscription(sub, plgActCreate) } else { pluginSubscription(sub, plgActUpd) } } else { // Add subscribed user to cache. usersRegisterUser(asUid, true) // Notify plugins of a new subscription pluginSubscription(sub, plgActCreate) } } else { // Process update to existing subscription. It could be an incomplete subscription for a new topic. if !userData.isChan && asChan { // A normal subscriber is trying to access topic as a channel. // Direct the subscriber to use non-channel topic name. sess.queueOut(InfoUseOtherReply(pkt, t.name, now)) return nil, types.ErrNotFound } var ownerChange bool // Save old access values oldWant = userData.modeWant oldGiven = userData.modeGiven if modeWant != types.ModeUnset { // Explicit modeWant is provided // Make sure the current owner cannot unset the owner flag or ban himself. if t.owner == asUid && (!modeWant.IsOwner() || !modeWant.IsJoiner()) { sess.queueOut(ErrPermissionDeniedReply(pkt, now)) return nil, errors.New("cannot unset ownership or self-ban the owner") } // Perform sanity checks if userData.modeGiven.IsOwner() { // Check for possible ownership transfer. Handle the following cases: // 1. Acceptance or rejection of the ownership transfer // 2. Owner changing own settings // Ownership transfer ownerChange = modeWant.IsOwner() && !userData.modeWant.IsOwner() // The owner should be able to grant himself any access permissions. if modeWant.IsOwner() && !userData.modeGiven.BetterEqual(modeWant) { userData.modeGiven |= modeWant } } else if modeWant.IsOwner() { // Ownership transfer can only be initiated by the owner. sess.queueOut(ErrPermissionDeniedReply(pkt, now)) return nil, errors.New("non-owner cannot request ownership transfer") } else if t.cat == types.TopicCatGrp && userData.modeGiven.IsAdmin() && modeWant.IsAdmin() { // A group topic Admin should be able to grant himself any permissions except // ownership (checked previously) & hard-deleting messages. if !userData.modeGiven.BetterEqual(modeWant & ^types.ModeDelete) { userData.modeGiven |= (modeWant & ^types.ModeDelete) } } switch t.cat { case types.TopicCatP2P: // For P2P topics ignore requests exceeding the maximum allowed. Otherwise it will generate // a useless announcement. modeWant = (modeWant & globals.typesModeCP2P) | types.ModeApprove case types.TopicCatSys: // Anyone can always write to Sys topic. modeWant &= (modeWant & types.ModeCSys) | types.ModeWrite } } // If user has not requested a new access mode, provide one by default. if modeWant == types.ModeUnset { // If the user has self-banned before, un-self-ban. Otherwise do not make a change. if !oldWant.IsJoiner() { // Set permissions NO WORSE than default, but possibly better (admin or owner banned himself). userData.modeWant = userData.modeGiven | t.accessFor(asLvl) } } else if userData.modeWant != modeWant { // The user has provided a new modeWant and it' different from the one before userData.modeWant = modeWant } // Create a subscription object to notify plugins. sub := types.Subscription{ User: asUid.String(), Topic: t.name, } // Save changes to DB update := map[string]any{} if isNullValue(private) { update["Private"] = nil userData.private = nil sub.Private = private } else if private != nil { update["Private"] = private userData.private = private sub.Private = private } if userData.modeWant != oldWant { update["ModeWant"] = userData.modeWant sub.ModeWant = userData.modeWant } if userData.modeGiven != oldGiven { update["ModeGiven"] = userData.modeGiven sub.ModeGiven = userData.modeGiven } if len(update) > 0 { if err := store.Subs.Update(t.name, asUid, update); err != nil { sess.queueOut(ErrUnknownReply(pkt, now)) return nil, err } pluginSubscription(&sub, plgActUpd) } // No transactions in RethinkDB, but two owners are better than none if ownerChange { oldOwnerData := t.perUser[t.owner] oldOwnerOldWant, oldOwnerOldGiven := oldOwnerData.modeWant, oldOwnerData.modeGiven oldOwnerData.modeGiven = (oldOwnerData.modeGiven & ^types.ModeOwner) oldOwnerData.modeWant = (oldOwnerData.modeWant & ^types.ModeOwner) if err := store.Subs.Update(t.name, t.owner, map[string]any{ "ModeWant": oldOwnerData.modeWant, "ModeGiven": oldOwnerData.modeGiven, }); err != nil { return nil, err } if err := store.Topics.OwnerChange(t.name, asUid); err != nil { return nil, err } t.perUser[t.owner] = oldOwnerData // Send presence notifications. t.notifySubChange(t.owner, asUid, false, oldOwnerOldWant, oldOwnerOldGiven, oldOwnerData.modeWant, oldOwnerData.modeGiven, "") t.owner = asUid } } if !asChan { // If topic is being muted, send "off" notification and disable updates. // Do it before applying the new permissions. if (oldWant & oldGiven).IsPresencer() && !(userData.modeWant & userData.modeGiven).IsPresencer() { if t.cat == types.TopicCatMe { t.presUsersOfInterest("off+dis", t.userAgent) } else { t.presSingleUserOffline(asUid, userData.modeWant&userData.modeGiven, "off+dis", nilPresParams, "", false) } } } // Apply changes. t.perUser[asUid] = userData var modeChanged *MsgAccessMode // Send presence notifications and update cached unread count. if oldWant != userData.modeWant || oldGiven != userData.modeGiven { if !asChan { oldReader := (oldWant & oldGiven).IsReader() newReader := (userData.modeWant & userData.modeGiven).IsReader() if oldReader && !newReader { // Decrement unread count usersUpdateUnread(asUid, userData.readID-t.lastID, true) } else if !oldReader && newReader { // Increment unread count usersUpdateUnread(asUid, t.lastID-userData.readID, true) } } // Notify actor of the changes in access mode. t.notifySubChange(asUid, asUid, asChan, oldWant, oldGiven, userData.modeWant, userData.modeGiven, sess.sid) } if (pkt.Sub != nil && pkt.Sub.Newsub) || oldWant != userData.modeWant || oldGiven != userData.modeGiven { modeChanged = &MsgAccessMode{ Want: userData.modeWant.String(), Given: userData.modeGiven.String(), Mode: (userData.modeGiven & userData.modeWant).String(), } } if !userData.modeWant.IsJoiner() { // The user is self-banning from the topic. Re-subscription will unban. t.evictUser(asUid, false, "") // The callee will send NoErrOK return modeChanged, nil } if !userData.modeGiven.IsJoiner() { // User was banned sess.queueOut(ErrPermissionDeniedReply(pkt, now)) return nil, errors.New("topic access denied; user is banned") } return modeChanged, nil } // anotherUserSub processes a request to initiate an invite or approve a subscription request from another user. // Returns changed == true if user's access mode has changed. // Handle these cases: // A. Sharer or Approver is inviting another user for the first time (no prior subscription) // B. Sharer or Approver is re-inviting another user (adjusting modeGiven, modeWant is still Unset) // C. Approver is changing modeGiven for another user, modeWant != Unset func (t *Topic) anotherUserSub(sess *Session, asUid, target types.Uid, asChan bool, pkt *ClientComMessage) (*MsgAccessMode, error) { now := types.TimeNow() set := pkt.Set // Check if approver actually has permission to manage sharing hostData, ok := t.perUser[asUid] // Access mode of the person who is executing this approval process hostMode := hostData.modeGiven & hostData.modeWant if !ok || !hostMode.IsSharer() { sess.queueOut(ErrPermissionDeniedReply(pkt, now)) return nil, errors.New("topic access denied; approver has no permission") } if asChan { // TODO: need to implement promoting reader to subscriber. Rejecting for now. sess.queueOut(ErrPermissionDeniedReply(pkt, now)) return nil, errors.New("topic access denied: cannot subscribe reader to channel") } // Check if topic is suspended. if t.isReadOnly() { sess.queueOut(ErrPermissionDeniedReply(pkt, now)) return nil, errors.New("topic is suspended") } // Parse the access mode granted modeGiven := types.ModeUnset if set.Sub.Mode != "" { if err := modeGiven.UnmarshalText([]byte(set.Sub.Mode)); err != nil { sess.queueOut(ErrMalformedReply(pkt, now)) return nil, err } // Make sure the new permissions are reasonable in P2P topics: permissions no greater than allowed, // approver permission cannot be removed. if t.cat == types.TopicCatP2P { modeGiven = (modeGiven & globals.typesModeCP2P) | types.ModeApprove } } // Make sure only the owner & approvers can set non-default access mode if modeGiven != types.ModeUnset && !hostMode.IsAdmin() { sess.queueOut(ErrPermissionDeniedReply(pkt, now)) return nil, errors.New("sharer cannot set explicit modeGiven") } // Make sure no one but the owner can do an ownership transfer if modeGiven.IsOwner() && t.owner != asUid { sess.queueOut(ErrPermissionDeniedReply(pkt, now)) return nil, errors.New("attempt to transfer ownership by non-owner") } // Access mode values as they were before this request was processed. oldWant := types.ModeUnset oldGiven := types.ModeUnset // Check if it's a new invite. If so, save it to database as a subscription. // Saved subscription does not mean the user is allowed to post/read userData, existingSub := t.perUser[target] if !existingSub || userData.deleted { // Check if the max number of subscriptions is already reached. if t.cat == types.TopicCatGrp && t.subsCount() >= globals.maxSubscriberCount { sess.queueOut(ErrPolicyReply(pkt, now)) return nil, errors.New("max subscription count exceeded") } if modeGiven == types.ModeUnset { // Request to use default access mode for the new subscriptions. // Assuming LevelAuth. Approver should use non-default access if that is not suitable. modeGiven = t.accessFor(auth.LevelAuth) // Enable new subscription even if default is no joiner. modeGiven |= types.ModeJoin } var modeWant types.AccessMode // Check if the invitee has been subscribed previously and if so, use previous modeWant. // Otherwise the inviter may delete blocked subscription and reinvite to spam the user. sub, err := store.Subs.Get(t.name, target, true) if err != nil { sess.queueOut(ErrUnknownReply(pkt, now)) return nil, err } if sub != nil { // Existing deleted subscription. modeWant = sub.ModeWant } else { // Get user's default access mode to be used as modeWant if user, err := store.Users.Get(target); err != nil { sess.queueOut(ErrUnknownReply(pkt, now)) return nil, err } else if user == nil { sess.queueOut(ErrUserNotFoundReply(pkt, now)) return nil, errors.New("user not found") } else if user.State != types.StateOK { sess.queueOut(ErrPermissionDeniedReply(pkt, now)) return nil, errors.New("user is suspended") } else { // Don't ask by default for more permissions than the granted ones. modeWant = user.Access.Auth & modeGiven } } // Reject invitation: 'want' permissions have no 'J'. if !modeWant.IsJoiner() { sess.queueOut(ErrPermissionDeniedReply(pkt, now)) return nil, errors.New("invitation rejected due to permissions") } // Add subscription to database sub = &types.Subscription{ User: target.String(), Topic: t.name, ModeWant: modeWant, ModeGiven: modeGiven, } if err := store.Subs.Create(sub); err != nil { sess.queueOut(ErrUnknownReply(pkt, now)) return nil, err } userData = perUserData{ modeGiven: sub.ModeGiven, modeWant: sub.ModeWant, private: nil, } t.perUser[target] = userData t.computePerUserAcsUnion() // Cache user's record usersRegisterUser(target, true) // Notify plugins of a new subscription. pluginSubscription(sub, plgActCreate) // Send push notification for the new subscription. // TODO: maybe skip user's devices which were online when this event has happened. sendPush(t.pushForP2PSub(asUid, target, userData.modeWant, userData.modeGiven, now)) } else { // Action on an existing subscription: re-invite, change existing permission, confirm/decline request. oldGiven = userData.modeGiven oldWant = userData.modeWant if modeGiven == types.ModeUnset { // Request to re-send invite without changing the access mode modeGiven = userData.modeGiven } else if modeGiven != userData.modeGiven { // Changing the previously assigned value. // Cannot strip owner of ownership or ban the owner. if t.owner == target && (!modeGiven.IsOwner() || !modeGiven.IsJoiner()) { sess.queueOut(ErrPermissionDeniedReply(pkt, now)) return nil, errors.New("cannot stip ownership or ban the owner") } // Save changed value to database if err := store.Subs.Update(t.name, target, map[string]any{"ModeGiven": modeGiven}); err != nil { return nil, err } userData.modeGiven = modeGiven t.perUser[target] = userData } } var modeChanged *MsgAccessMode // Access mode has changed. if oldGiven != userData.modeGiven { oldReader := (oldWant & oldGiven).IsReader() newReader := (userData.modeWant & userData.modeGiven).IsReader() if oldReader && !newReader { // Decrement unread count usersUpdateUnread(target, userData.readID-t.lastID, true) } else if !oldReader && newReader { // Increment unread count usersUpdateUnread(target, t.lastID-userData.readID, true) } t.notifySubChange(target, asUid, false, oldWant, oldGiven, userData.modeWant, userData.modeGiven, sess.sid) modeChanged = &MsgAccessMode{ Given: userData.modeGiven.String(), Want: userData.modeWant.String(), Mode: (userData.modeGiven & userData.modeWant).String(), } } if !userData.modeGiven.IsJoiner() { // The user is banned from the topic. t.evictUser(target, false, "") } return modeChanged, nil } // replyGetDesc is a response to a get.desc request on a topic, sent to just the session as a {meta} packet func (t *Topic) replyGetDesc(sess *Session, asUid types.Uid, _ bool, opts *MsgGetOpts, msg *ClientComMessage) error { now := types.TimeNow() id := msg.Id if opts != nil && (opts.User != "" || opts.Limit != 0) { sess.queueOut(ErrMalformedReply(msg, now)) return errors.New("invalid GetDesc query") } // Check if user requested modified data ifUpdated := opts == nil || opts.IfModifiedSince == nil || opts.IfModifiedSince.Before(t.updated) desc := &MsgTopicDesc{} if opts == nil || opts.IfModifiedSince == nil { // Send CreatedAt only when the user requests full information (nothing is cached at the client). desc.CreatedAt = &t.created } if !t.updated.IsZero() { desc.UpdatedAt = &t.updated } pud, full := t.perUser[asUid] full = full || t.cat == types.TopicCatMe if t.cat == types.TopicCatGrp { desc.IsChan = t.isChan desc.SubCnt = t.subCnt logs.Info.Println("replyGetDesc: grp topic", t.name, "subs", t.subCnt) } if ifUpdated { if t.public != nil || t.trusted != nil { // Not a p2p topic. desc.Public = t.public desc.Trusted = t.trusted } else if full && t.cat == types.TopicCatP2P { // FIXME: when a P2P participant updates desc at 'me', these cached values are not updated. desc.Public = pud.public desc.Trusted = pud.trusted } } // Request may come from a subscriber (full == true) or a stranger. // Give subscriber a fuller description than to a stranger/channel reader. if full { if t.cat == types.TopicCatP2P { // For p2p topics default access mode makes no sense: only participants have access to topic. // Don't report it. } else if t.cat == types.TopicCatMe || (pud.modeGiven & pud.modeWant).IsSharer() { desc.DefaultAcs = &MsgDefaultAcsMode{ Auth: t.accessAuth.String(), Anon: t.accessAnon.String(), } } desc.Acs = &MsgAccessMode{ Want: pud.modeWant.String(), Given: pud.modeGiven.String(), Mode: (pud.modeGiven & pud.modeWant).String(), } if t.cat == types.TopicCatMe && sess.authLvl == auth.LevelRoot { // If 'me' is in memory then user account is invariably not suspended. desc.State = types.StateOK.String() } if (pud.modeGiven & pud.modeWant).IsPresencer() { switch t.cat { case types.TopicCatGrp: desc.Online = t.isOnline() case types.TopicCatP2P: // This is the timestamp when the other user logged off last time. // It does not change while the topic is loaded into memory and that's OK most of the time // because to stay in memory at least one of the users must be connected to topic. // FIXME(gene): it breaks when user A stays active in one session and connects-disconnects // from another session. The second session will not see correct LastSeen time and UserAgent. if pud.lastSeen != nil { desc.LastSeen = &MsgLastSeenInfo{ When: pud.lastSeen, UserAgent: pud.lastUA, } } } } if ifUpdated { desc.Private = pud.private } // Don't report message IDs to users without Read access. if (pud.modeGiven & pud.modeWant).IsReader() { desc.SeqId = t.lastID if !t.touched.IsZero() { desc.TouchedAt = &t.touched } // Make sure reported values are sane: // t.delID <= pud.delID; t.readID <= t.recvID <= t.lastID desc.DelId = max(pud.delID, t.delID) desc.ReadSeqId = pud.readID desc.RecvSeqId = max(pud.recvID, pud.readID) } else { // Send some sane value of touched. desc.TouchedAt = &t.updated } } sess.queueOut(&ServerComMessage{ Meta: &MsgServerMeta{ Id: id, Topic: msg.Original, Desc: desc, Timestamp: &now, }, }) return nil } // replySetDesc updates topic metadata, saves it to DB, replies to the caller as {ctrl} message, // generates {pres} update if necessary. func (t *Topic) replySetDesc(sess *Session, asUid types.Uid, asChan bool, authLevel auth.Level, msg *ClientComMessage) error { now := types.TimeNow() assignAccess := func(upd map[string]any, mode *MsgDefaultAcsMode) error { if mode == nil { return nil } if auth, anon, err := parseTopicAccess(mode, types.ModeUnset, types.ModeUnset); err != nil { return err } else if auth.IsOwner() || anon.IsOwner() { return errors.New("default 'owner' access is not permitted") } else { access := types.DefaultAccess{Auth: t.accessAuth, Anon: t.accessAnon} if auth != types.ModeUnset { if t.cat == types.TopicCatMe { auth &= types.ModeCAuth if auth != types.ModeNone { // This is the default access mode for P2P topics. // It must be either an N or must include an A permission. auth |= types.ModeApprove } } access.Auth = auth } if anon != types.ModeUnset { if t.cat == types.TopicCatMe { anon &= globals.typesModeCP2P if anon != types.ModeNone { anon |= types.ModeApprove } } access.Anon = anon } if access.Auth != t.accessAuth || access.Anon != t.accessAnon { upd["Access"] = access } } return nil } assignGenericValues := func(upd map[string]any, what string, dst, src any) (changed bool) { if dst, changed = mergeInterfaces(dst, src); changed { upd[what] = dst } return } // DefaultAccess and/or Public have chanegd var sendCommon bool // Private has changed var sendPriv bool var err error // Change to the main object (user or topic). core := make(map[string]any) // Change to subscription. sub := make(map[string]any) if set := msg.Set; set.Desc != nil { if set.Desc.Trusted != nil && authLevel != auth.LevelRoot { // Only ROOT can change Trusted. sess.queueOut(ErrPermissionDeniedReply(msg, now)) return errors.New("attempt to change Trusted by non-root") } switch t.cat { case types.TopicCatMe: // Update current user err = assignAccess(core, set.Desc.DefaultAcs) sendCommon = assignGenericValues(core, "Public", t.public, set.Desc.Public) sendCommon = assignGenericValues(core, "Trusted", t.trusted, set.Desc.Trusted) || sendCommon case types.TopicCatFnd: // set.Desc.DefaultAcs is ignored. if set.Desc.Trusted != nil { // 'fnd' does not support Trusted. sess.queueOut(ErrPermissionDeniedReply(msg, now)) return errors.New("attempt to assign Trusted in fnd topic") } // Do not send presence if fnd.Public has changed. assignGenericValues(core, "Public", t.fndGetPublic(sess), set.Desc.Public) case types.TopicCatP2P: // Reject direct changes to P2P topics. if set.Desc.Public != nil || set.Desc.Trusted != nil || set.Desc.DefaultAcs != nil { sess.queueOut(ErrPermissionDeniedReply(msg, now)) return errors.New("incorrect attempt to change metadata of a p2p topic") } case types.TopicCatGrp: // Update group topic if t.owner == asUid { err = assignAccess(core, set.Desc.DefaultAcs) sendCommon = assignGenericValues(core, "Public", t.public, set.Desc.Public) sendCommon = assignGenericValues(core, "Trusted", t.trusted, set.Desc.Trusted) || sendCommon } else if set.Desc.DefaultAcs != nil || set.Desc.Public != nil || set.Desc.Trusted != nil { // This is a request from non-owner sess.queueOut(ErrPermissionDeniedReply(msg, now)) return errors.New("attempt to change public or permissions by non-owner") } } if err != nil { sess.queueOut(ErrMalformedReply(msg, now)) return err } sendPriv = assignGenericValues(sub, "Private", t.perUser[asUid].private, set.Desc.Private) } if len(core)+len(sub) == 0 { sess.queueOut(InfoNotModifiedReply(msg, now)) return errors.New("{set} generated no update to DB") } if len(core) > 0 { core["UpdatedAt"] = now switch t.cat { case types.TopicCatMe: err = store.Users.Update(asUid, core) case types.TopicCatFnd: // The only value to be stored in topic is Public, and Public for fnd is not saved according to specs. default: err = store.Topics.Update(t.name, core) } } if err == nil && len(sub) > 0 { tname := t.name if asChan { tname = types.GrpToChn(tname) } err = store.Subs.Update(tname, asUid, sub) } if err != nil { sess.queueOut(ErrUnknownReply(msg, now)) return err } if len(core) > 0 && msg.Extra != nil && len(msg.Extra.Attachments) > 0 { if err := store.Files.LinkAttachments(t.name, types.ZeroUid, msg.Extra.Attachments); err != nil { logs.Warn.Printf("topic[%s] failed to link avatar attachment: %v", t.name, err) // This is not a critical error, continue execution. } } // Update values cached in the topic object switch t.cat { case types.TopicCatMe, types.TopicCatGrp: if tmp, ok := core["Access"]; ok { access := tmp.(types.DefaultAccess) t.accessAuth = access.Auth t.accessAnon = access.Anon } if public, ok := core["Public"]; ok { t.public = public } if trusted, ok := core["Trusted"]; ok { t.trusted = trusted } case types.TopicCatFnd: // Assign per-session fnd.Public. t.fndSetPublic(sess, core["Public"]) } pud := t.perUser[asUid] mode := pud.modeGiven & pud.modeWant if private, ok := sub["Private"]; ok { pud.private = private t.perUser[asUid] = pud } if sendCommon || sendPriv { // t.public/t.trusted, t.accessAuth/Anon have changed, make an announcement if sendCommon { if t.cat == types.TopicCatMe { t.presUsersOfInterest("upd", "") } else { // Notify all subscribers on 'me' except the user who made the change and blocked users. // The user who made the change will be notified separately (see below). filter := &presFilters{excludeUser: asUid.UserId(), filterIn: types.ModeJoin} t.presSubsOffline("upd", nilPresParams, filter, filter, sess.sid, false) } t.updated = now } // Notify user's other sessions. t.presSingleUserOffline(asUid, mode, "upd", nilPresParams, sess.sid, false) } sess.queueOut(NoErrReply(msg, now)) return nil } // replyGetSub is a response to a get.sub request on a topic - load a list of subscriptions/subscribers, // send it just to the session as a {meta} packet func (t *Topic) replyGetSub(sess *Session, asUid types.Uid, authLevel auth.Level, asChan bool, msg *ClientComMessage) error { now := types.TimeNow() id := msg.Id incomingReqTs := msg.Timestamp var req *MsgGetOpts if msg.Sub != nil { req = msg.Sub.Get.Sub } else { req = msg.Get.Sub } if req != nil && (req.SinceId != 0 || req.BeforeId != 0) { sess.queueOut(ErrMalformedReply(msg, now)) return errors.New("invalid MsgGetOpts query") } var err error var ifModified time.Time if req != nil && req.IfModifiedSince != nil { ifModified = *req.IfModifiedSince } userData := t.perUser[asUid] var subs []types.Subscription switch t.cat { case types.TopicCatMe: if req != nil { // If topic is provided, it could be in the form of user ID 'usrAbCd'. // Convert it to P2P topic name. Likewise for Self topic 'slf' -> 'slfAbcD'. if uid2 := types.ParseUserId(req.Topic); !uid2.IsZero() { req.Topic = uid2.P2PName(asUid) } if req.Topic == "slf" { req.Topic = asUid.SlfName() } } // Fetch user's subscriptions, with Topic.Public+Topic.Trusted denormalized into subscription. if ifModified.IsZero() { // No cache management. Skip deleted subscriptions. subs, err = store.Users.GetTopics(asUid, msgOpts2storeOpts(req)) } else { // User manages cache. Include deleted subscriptions too. subs, err = store.Users.GetTopicsAny(asUid, msgOpts2storeOpts(req)) // Returned subscriptions do not contain topics which are online now but otherwise unchanged. // We need to add these topic to the list otherwise the user would see them as offline. selected := map[string]struct{}{} for i := range subs { sub := &subs[i] with := sub.GetWith() if with != "" { selected[with] = struct{}{} } else { selected[sub.Topic] = struct{}{} } } // Add dummy subscriptions for missing online topics. for topic, psd := range t.perSubs { _, present := selected[topic] if !present && psd.online { sub := types.Subscription{Topic: topic} sub.SetWith(topic) sub.SetDummy(true) subs = append(subs, sub) } } } case types.TopicCatFnd: // Select public or private query. Public is set interactively and has priority. query := t.fndGetPublic(sess) if query == "" { query, _ = userData.private.(string) } // Empty queries are ignored with "NoContent". if query != "" { query, subs, err = pluginFind(asUid, query) if err == nil && subs == nil && query != "" { if and, opt, err := parseSearchQuery(query); err == nil { var req [][]string for _, tag := range and { rewritten := rewriteTag(tag, sess.countryCode) if len(rewritten) > 0 { req = append(req, rewritten) } } opt = rewriteTagSlice(opt, sess.countryCode) // Check if the query contains terms that the user is not allowed to use. if restr, _, _ := stringSliceDelta(t.tags, filterTags(append(types.FlattenDoubleSlice(req), opt...), globals.maskedTagNS)); len(restr) > 0 { sess.queueOut(ErrPermissionDeniedReply(msg, now)) return errors.New("attempt to search by restricted tags") } // Ordinary users: find only active topics and accounts. // Root users: find all topics and accounts, including suspended and soft-deleted. subs, err = store.Users.FindSubs(asUid, globals.aliasTagNS, req, opt, sess.authLvl != auth.LevelRoot) if err != nil { sess.queueOut(decodeStoreErrorExplicitTs(err, id, msg.Original, now, incomingReqTs, nil)) return err } } } } case types.TopicCatP2P: // TODO(gene): don't load subs from DB, use perUserData - it already contains subscriptions. // No need to load Public for p2p topics. if ifModified.IsZero() { // No cache management. Skip deleted subscriptions. subs, err = store.Topics.GetSubs(t.name, msgOpts2storeOpts(req)) } else { // User manages cache. Include deleted subscriptions too. subs, err = store.Topics.GetSubsAny(t.name, msgOpts2storeOpts(req)) } case types.TopicCatGrp: topicName := t.name if asChan { // In case of a channel allow fetching the subscription of the current user only. if req == nil { req = &MsgGetOpts{} } req.User = asUid.UserId() // Channel subscribers are using chnXXX topic name rather than grpXXX. topicName = msg.Original } // Include sub.Public. if ifModified.IsZero() { // No cache management. Skip deleted subscriptions. subs, err = store.Topics.GetUsers(topicName, msgOpts2storeOpts(req)) } else { // User manages cache. Include deleted subscriptions too. subs, err = store.Topics.GetUsersAny(topicName, msgOpts2storeOpts(req)) } // Do nothing for all other topic types, like 'sys', 'slf'. } if err != nil { sess.queueOut(decodeStoreErrorExplicitTs(err, id, msg.Original, now, incomingReqTs, nil)) return err } if len(subs) == 0 { // Inform the client that there are no subscriptions. sess.queueOut(NoContentParamsReply(msg, now, map[string]any{"what": "sub"})) return nil } meta := &MsgServerMeta{ Id: id, Topic: msg.Original, Sub: make([]MsgTopicSub, 0, len(subs)), Timestamp: &now} presencer := (userData.modeGiven & userData.modeWant).IsPresencer() sharer := (userData.modeGiven & userData.modeWant).IsSharer() for i := range subs { sub := &subs[i] // Indicator if the requester has provided a cut off date for ts of pub & priv updates. var sendPubPriv bool var banned bool var mts MsgTopicSub deleted := sub.DeletedAt != nil if ifModified.IsZero() { sendPubPriv = true } else { // Skip sending deleted subscriptions if they were deleted before the cut off date. // If they are freshly deleted send minimum info if deleted { if !sub.DeletedAt.After(ifModified) { continue } mts.DeletedAt = sub.DeletedAt } sendPubPriv = !deleted && sub.UpdatedAt.After(ifModified) } uid := types.ParseUid(sub.User) subMode := sub.ModeGiven & sub.ModeWant isReader := subMode.IsReader() if t.cat == types.TopicCatMe { // Mark subscriptions that the user does not care about. if !subMode.IsJoiner() { banned = true } // Reporting user's subscriptions to other topics. P2P topic name is the // UID of the other user. with := sub.GetWith() if with != "" { mts.Topic = with mts.Online = t.perSubs[with].online && !deleted && presencer } else if strings.HasPrefix(sub.Topic, "slf") { mts.Topic = "slf" // Not reporting Online as it makes no sense for slf. } else { mts.Topic = sub.Topic mts.Online = t.perSubs[sub.Topic].online && !deleted && presencer } if !deleted && !banned { if isReader { touchedAt := sub.GetTouchedAt() if touchedAt.IsZero() { mts.TouchedAt = nil } else { mts.TouchedAt = &touchedAt } mts.SeqId = sub.GetSeqId() mts.DelId = sub.DelId } else if !sub.UpdatedAt.IsZero() { mts.TouchedAt = &sub.UpdatedAt } lastSeen := sub.GetLastSeen() if lastSeen != nil && !mts.Online { mts.LastSeen = &MsgLastSeenInfo{ When: lastSeen, UserAgent: sub.GetUserAgent(), } } mts.SubCnt = sub.GetSubCnt() } } else { // Mark subscriptions that the user does not care about. if t.cat == types.TopicCatGrp && !subMode.IsJoiner() { banned = true } // Reporting subscribers to fnd, a group or a p2p topic mts.User = uid.UserId() if t.cat == types.TopicCatFnd { mts.Topic = sub.Topic } if !deleted { if uid == asUid && isReader && !banned { // Report deleted ID for own subscriptions only mts.DelId = sub.DelId } if t.cat == types.TopicCatGrp { pud := t.perUser[uid] mts.Online = pud.online > 0 && presencer } } } if !deleted { if !sub.UpdatedAt.IsZero() { mts.UpdatedAt = &sub.UpdatedAt } if isReader && !banned { mts.ReadSeqId = sub.ReadSeqId mts.RecvSeqId = sub.RecvSeqId } if t.cat != types.TopicCatFnd { // p2p and grp if !sub.IsDummy() && (sharer || uid == asUid || subMode.IsAdmin()) { // If user is not a sharer, the access mode of other ordinary users if not accessible. // Own and admin permissions only are visible to non-sharers. mts.Acs.Mode = subMode.String() mts.Acs.Want = sub.ModeWant.String() mts.Acs.Given = sub.ModeGiven.String() } } else { // Topic 'fnd' // sub.ModeXXX may be defined by the plugin. if sub.ModeGiven.IsDefined() && sub.ModeWant.IsDefined() { mts.Acs.Mode = subMode.String() mts.Acs.Want = sub.ModeWant.String() mts.Acs.Given = sub.ModeGiven.String() } else if types.IsChannel(sub.Topic) { mts.Acs.Mode = types.ModeCChnReader.String() } else if defacs := sub.GetDefaultAccess(); defacs != nil { switch authLevel { case auth.LevelAnon: mts.Acs.Mode = defacs.Anon.String() case auth.LevelAuth, auth.LevelRoot: mts.Acs.Mode = defacs.Auth.String() } } mts.SubCnt = sub.GetSubCnt() } // Returning public and private only if they have changed since ifModified if sendPubPriv { // 'sub' has nil 'public'/'trusted' in P2P topics which is OK. mts.Public = sub.GetPublic() mts.Trusted = sub.GetTrusted() // Reporting 'private' only if it's user's own subscription. if uid == asUid { mts.Private = sub.Private } } // Always reporting 'private' for fnd topic. if t.cat == types.TopicCatFnd { mts.Private = sub.Private } } meta.Sub = append(meta.Sub, mts) } sess.queueOut(&ServerComMessage{Meta: meta}) return nil } // replySetSub is a response to new subscription request or an update to a subscription {set.sub}: // update topic metadata cache, save/update subs, reply to the caller as {ctrl} message, // generate a presence notification, if appropriate. func (t *Topic) replySetSub(sess *Session, pkt *ClientComMessage, asChan bool) error { now := types.TimeNow() asUid := types.ParseUserId(pkt.AsUser) set := pkt.Set var target types.Uid if target = types.ParseUserId(set.Sub.User); target.IsZero() && set.Sub.User != "" { // Invalid user ID sess.queueOut(ErrMalformedReply(pkt, now)) return errors.New("invalid user id") } // if set.User is not set, request is for the current user if target.IsZero() { target = asUid } var err error var modeChanged *MsgAccessMode if target == asUid { // Request new subscription or modify own subscription modeChanged, err = t.thisUserSub(sess, pkt, asUid, asChan, set.Sub.Mode, nil) } else { // Request to approve/change someone's subscription modeChanged, err = t.anotherUserSub(sess, asUid, target, asChan, pkt) } if err != nil { return err } var resp *ServerComMessage if modeChanged != nil { // Report resulting access mode. params := map[string]any{"acs": modeChanged} if target != asUid { params["user"] = target.UserId() } resp = NoErrParamsReply(pkt, now, params) } else { resp = InfoNotModifiedReply(pkt, now) } sess.queueOut(resp) return nil } // replyGetData is a response to a get.data request - load a list of stored messages, send them to session as {data} // response goes to a single session rather than all sessions in a topic func (t *Topic) replyGetData(sess *Session, asUid types.Uid, asChan bool, req *MsgGetOpts, msg *ClientComMessage) error { now := types.TimeNow() toriginal := t.original(asUid) if req != nil && (req.IfModifiedSince != nil || req.User != "" || req.Topic != "") { sess.queueOut(ErrMalformedReply(msg, now)) return errors.New("invalid MsgGetOpts query") } // Check if the user has permission to read the topic data count := 0 if userData := t.perUser[asUid]; (userData.modeGiven & userData.modeWant).IsReader() { // Read messages from DB messages, err := store.Messages.GetAll(t.name, asUid, msgOpts2storeOpts(req)) if err != nil { sess.queueOut(ErrUnknownReply(msg, now)) return err } // Push the list of messages to the client as {data}. if messages != nil { count = len(messages) if count > 0 { outgoingMessages := make([]*ServerComMessage, count) for i := range messages { mm := &messages[i] from := "" if !asChan { // Don't show sender for channel readers from = types.ParseUid(mm.From).UserId() } outgoingMessages[i] = &ServerComMessage{ Data: &MsgServerData{ Topic: toriginal, Head: mm.Head, SeqId: mm.SeqId, From: from, Timestamp: mm.CreatedAt, Content: mm.Content, }, } } sess.queueOutBatch(outgoingMessages) } } } else { sess.queueOut(ErrPermissionDeniedReply(msg, now)) return errors.New("attempt to get messages by non-reader") } // Inform the requester that all the data has been served. if count == 0 { sess.queueOut(NoContentParamsReply(msg, now, map[string]any{"what": "data"})) } else { sess.queueOut(NoErrDeliveredParams(msg.Id, msg.Original, now, map[string]any{"what": "data", "count": count})) } return nil } // replyGetTags returns topics' tags - tokens used for discovery. func (t *Topic) replyGetTags(sess *Session, asUid types.Uid, msg *ClientComMessage) error { now := types.TimeNow() if t.cat == types.TopicCatFnd { // Fnd: checking for alias availability. // Checking public (session) data only. if tag := t.fndGetPublic(sess); tag != "" { var found string tag, subs, err := pluginFind(asUid, tag) if err == nil { if subs == nil { if prefix, _ := validateTag(tag); prefix != "" { // Check only if a fully-qualified tag was sent. Otherwise ignore the request. found, err = store.Users.FindOne(tag) } } else { // The plugin returned a list of topics. Send the first one. found = subs[0].Topic } } if err != nil { sess.queueOut(decodeStoreErrorExplicitTs(err, msg.Id, msg.Original, now, msg.Timestamp, nil)) return err } if found != "" { sess.queueOut(&ServerComMessage{ Meta: &MsgServerMeta{ Id: msg.Id, Topic: msg.Original, Timestamp: &now, Tags: []string{found}, }, }) return nil } } // Inform the requester that there are no tags. sess.queueOut(NoContentParamsReply(msg, now, map[string]string{"what": "tags"})) return nil } if t.cat != types.TopicCatMe && t.cat != types.TopicCatGrp { sess.queueOut(ErrOperationNotAllowedReply(msg, now)) return errors.New("invalid topic category for getting tags") } if t.cat == types.TopicCatGrp && t.owner != asUid { sess.queueOut(ErrPermissionDeniedReply(msg, now)) return errors.New("request for tags from non-owner") } if len(t.tags) > 0 { sess.queueOut(&ServerComMessage{ Meta: &MsgServerMeta{ Id: msg.Id, Topic: t.original(asUid), Timestamp: &now, Tags: t.tags, }, }) return nil } // Inform the requester that there are no tags. sess.queueOut(NoContentParamsReply(msg, now, map[string]string{"what": "tags"})) return nil } // replySetTags updates topic's tags - tokens used for discovery. func (t *Topic) replySetTags(sess *Session, asUid types.Uid, msg *ClientComMessage) error { now := types.TimeNow() if t.cat != types.TopicCatMe && t.cat != types.TopicCatGrp { sess.queueOut(ErrOperationNotAllowedReply(msg, now)) return errors.New("invalid topic category to assign tags") } if t.cat == types.TopicCatGrp && t.owner != asUid { sess.queueOut(ErrPermissionDeniedReply(msg, now)) return errors.New("tags update by non-owner") } tags := normalizeTags(msg.Set.Tags, globals.maxTagCount) if len(tags) == 0 { sess.queueOut(InfoNotModifiedReply(msg, now)) return nil } if !restrictedTagsEqual(t.tags, tags, globals.immutableTagNS) { sess.queueOut(ErrPermissionDeniedReply(msg, now)) return errors.New("attempt to mutate restricted tags") } if hasDuplicateNamespaceTags(tags, globals.aliasTagNS) { sess.queueOut(ErrMalformedReply(msg, now)) return errors.New("duplicate unique tags") } added, removed, _ := stringSliceDelta(t.tags, tags) if t.cat == types.TopicCatMe && len(added) > 0 { // User tags must all be prefixed. Users are not rearchable by generic tags. var prefixed []string for _, tag := range added { if prefix, _ := validateTag(tag); prefix != "" { prefixed = append(prefixed, prefix) } } added = prefixed } if len(added) == 0 && len(removed) == 0 { sess.queueOut(InfoNotModifiedReply(msg, now)) return nil } // Remove unprefixed tags if unique := filterTags(added, map[string]bool{globals.aliasTagNS: true}); len(unique) > 0 { // Check for global uniqueness. // It's not inside a transaction, so a race may happen. for _, tag := range unique { result, err := store.Users.FindOne(tag) if err != nil { sess.queueOut(ErrUnknownReply(msg, now)) return err } if result != "" { sess.queueOut(ErrMalformedReply(msg, now)) return errors.New("globally duplicate unique tags") } } } update := map[string]any{"Tags": tags, "UpdatedAt": now} var err error switch t.cat { case types.TopicCatMe: err = store.Users.Update(asUid, update) case types.TopicCatGrp: err = store.Topics.Update(t.name, update) } if err != nil { sess.queueOut(ErrUnknownReply(msg, now)) return err } t.tags = tags t.presSubsOnline("tags", "", nilPresParams, &presFilters{singleUser: asUid.UserId()}, sess.sid) params := make(map[string]any) if len(added) > 0 { params["added"] = len(added) } if len(removed) > 0 { params["removed"] = len(removed) } sess.queueOut(NoErrParamsReply(msg, now, params)) return nil } // replyGetCreds returns user's credentials such as email and phone numbers. func (t *Topic) replyGetCreds(sess *Session, asUid types.Uid, msg *ClientComMessage) error { now := types.TimeNow() id := msg.Id if t.cat != types.TopicCatMe { sess.queueOut(ErrOperationNotAllowedReply(msg, now)) return errors.New("invalid topic category for getting credentials") } screds, err := store.Users.GetAllCreds(asUid, "", false) if err != nil { sess.queueOut(decodeStoreErrorExplicitTs(err, id, msg.Original, now, msg.Timestamp, nil)) return err } if len(screds) > 0 { creds := make([]*MsgCredServer, len(screds)) for i, sc := range screds { creds[i] = &MsgCredServer{Method: sc.Method, Value: sc.Value, Done: sc.Done} } sess.queueOut(&ServerComMessage{ Meta: &MsgServerMeta{ Id: id, Topic: t.original(asUid), Timestamp: &now, Cred: creds, }, }) return nil } // Inform the requester that there are no credentials. sess.queueOut(NoContentParamsReply(msg, now, map[string]string{"what": "creds"})) return nil } // replySetCred adds or validates user credentials such as email and phone numbers. func (t *Topic) replySetCred(sess *Session, asUid types.Uid, authLevel auth.Level, msg *ClientComMessage) error { now := types.TimeNow() set := msg.Set if t.cat != types.TopicCatMe { sess.queueOut(ErrOperationNotAllowedReply(msg, now)) return errors.New("invalid topic category for updating credentials") } var err error var tags []string creds := []MsgCredClient{*set.Cred} if set.Cred.Response != "" { // Credential is being validated. Return an arror if response is invalid. _, tags, err = validatedCreds(asUid, authLevel, creds, true) } else { // Credential is being added or updated. tmpToken, _, _ := store.Store.GetLogicalAuthHandler("token").GenSecret(&auth.Rec{ Uid: asUid, AuthLevel: auth.LevelNone, Lifetime: auth.Duration(time.Hour * 24), Features: auth.FeatureNoLogin, }) _, tags, err = addCreds(asUid, creds, nil, sess.lang, tmpToken) } if tags != nil { t.tags = tags t.presSubsOnline("tags", "", nilPresParams, nilPresFilters, "") } sess.queueOut(decodeStoreErrorExplicitTs(err, set.Id, t.original(asUid), now, msg.Timestamp, nil)) return err } // replyGetAux returns topic's auxiliary set of key-value pairs. func (t *Topic) replyGetAux(sess *Session, asUid types.Uid, msg *ClientComMessage) error { now := types.TimeNow() if t.cat != types.TopicCatP2P && t.cat != types.TopicCatGrp && t.cat != types.TopicCatSlf { sess.queueOut(ErrOperationNotAllowedReply(msg, now)) return errors.New("invalid topic category to query aux") } if len(t.aux) > 0 { sess.queueOut(&ServerComMessage{ Meta: &MsgServerMeta{ Id: msg.Id, Topic: t.original(asUid), Timestamp: &now, Aux: t.aux, }, }) return nil } // Inform the requester that there are no tags. sess.queueOut(NoContentParamsReply(msg, now, map[string]string{"what": "aux"})) return nil } // replyGetAux returns topic's auxiliary set of key-value pairs. func (t *Topic) replySetAux(sess *Session, asUid types.Uid, msg *ClientComMessage) error { now := types.TimeNow() if t.cat != types.TopicCatP2P && t.cat != types.TopicCatGrp && t.cat != types.TopicCatSlf { sess.queueOut(ErrOperationNotAllowedReply(msg, now)) return errors.New("invalid topic category to assign aux") } if userData := t.perUser[asUid]; !(userData.modeGiven & userData.modeWant).IsAdmin() { sess.queueOut(ErrPermissionDeniedReply(msg, now)) return errors.New("aux update by non-admin") } if aux, changed := mergeMaps(copyMap(t.aux), msg.Set.Aux); changed { err := store.Topics.Update(t.name, map[string]any{"Aux": aux, "UpdatedAt": now}) if err == nil { t.aux = aux t.presSubsOnline("aux", "", nilPresParams, nilPresFilters, sess.sid) } sess.queueOut(decodeStoreErrorExplicitTs(err, msg.Set.Id, t.original(asUid), now, msg.Timestamp, nil)) return err } sess.queueOut(InfoNotModifiedReply(msg, now)) return nil } // replyGetDel is a response to a get[what=del] request: load a list of deleted message ids, send them to // a session as {meta} // response goes to a single session rather than all sessions in a topic func (t *Topic) replyGetDel(sess *Session, asUid types.Uid, req *MsgGetOpts, msg *ClientComMessage) error { now := types.TimeNow() toriginal := t.original(asUid) id := msg.Id incomingReqTs := msg.Timestamp if req != nil && (req.IfModifiedSince != nil || req.User != "" || req.Topic != "") { sess.queueOut(ErrMalformedReply(msg, now)) return errors.New("invalid MsgGetOpts query") } // Check if the user has permission to read the topic data and the request is valid. if userData := t.perUser[asUid]; (userData.modeGiven & userData.modeWant).IsReader() { ranges, delID, err := store.Messages.GetDeleted(t.name, asUid, msgOpts2storeOpts(req)) if err != nil { sess.queueOut(ErrUnknownReply(msg, now)) return err } if len(ranges) > 0 { sess.queueOut(&ServerComMessage{ Meta: &MsgServerMeta{ Id: id, Topic: toriginal, Del: &MsgDelValues{ DelId: delID, DelSeq: rangeDeserialize(ranges), }, Timestamp: &now, }, }) return nil } } sess.queueOut(NoContentParams(id, toriginal, now, incomingReqTs, map[string]string{"what": "del"})) return nil } // replyDelMsg deletes (soft or hard) messages in response to del.msg packet. func (t *Topic) replyDelMsg(sess *Session, asUid types.Uid, asChan bool, msg *ClientComMessage) error { now := types.TimeNow() if asChan { // Do not allow channel readers delete messages. sess.queueOut(ErrOperationNotAllowedReply(msg, now)) return errors.New("channel readers cannot delete messages") } del := msg.Del pud := t.perUser[asUid] if !(pud.modeGiven & pud.modeWant).IsDeleter() { // User must have an R permission: if the user cannot read messages, he has // no business of deleting them. if !(pud.modeGiven & pud.modeWant).IsReader() { sess.queueOut(ErrPermissionDeniedReply(msg, now)) return errors.New("del.msg: permission denied") } // User has just the R permission, cannot hard-delete messages, silently // switching to soft-deleting del.Hard = false } var err error var ranges []types.Range if len(del.DelSeq) == 0 { err = errors.New("del.msg: no IDs to delete") } else { count := 0 for _, dq := range del.DelSeq { if dq.LowId > t.lastID || dq.LowId < 0 || dq.HiId < 0 || (dq.HiId > 0 && dq.LowId > dq.HiId) || (dq.LowId == 0 && dq.HiId == 0) { err = errors.New("del.msg: invalid entry in list") break } if dq.HiId > t.lastID { // Range is inclusive - exclusive [low, hi), // to delete all messages hi must be lastId + 1 dq.HiId = t.lastID + 1 } else if dq.LowId == dq.HiId || dq.LowId+1 == dq.HiId { dq.HiId = 0 } if dq.HiId == 0 { count++ } else { count += dq.HiId - dq.LowId } ranges = append(ranges, types.Range{Low: dq.LowId, Hi: dq.HiId}) } if err == nil { // Sort by Low ascending then by Hi descending. sort.Sort(types.RangeSorter(ranges)) // Collapse overlapping ranges ranges = types.RangeSorter(ranges).Normalize() } if count > defaultMaxDeleteCount && len(ranges) > 1 { err = errors.New("del.msg: too many messages to delete") } } if err != nil { sess.queueOut(ErrMalformedReply(msg, now)) return err } forUser := asUid var age time.Duration if del.Hard { forUser = types.ZeroUid age = globals.msgDeleteAge } if err = store.Messages.DeleteList(t.name, t.delID+1, forUser, age, ranges); err != nil { sess.queueOut(ErrUnknownReply(msg, now)) return err } // Increment Delete transaction ID t.delID++ dr := rangeDeserialize(ranges) if del.Hard { for uid, pud := range t.perUser { pud.delID = t.delID t.perUser[uid] = pud // Update unread counters for all users who may have had these messages as unread if (pud.modeGiven & pud.modeWant).IsReader() { // Calculate how many unread messages were deleted for this user unreadDeleted := calculateUnreadInRanges(pud.readID, t.lastID, ranges) if unreadDeleted > 0 { // Decrease unread count (negative value) usersUpdateUnread(uid, -unreadDeleted, true) } } } // Broadcast the change to all, online and offline, exclude the session making the change. params := &presParams{delID: t.delID, delSeq: dr, actor: asUid.UserId()} filters := &presFilters{filterIn: types.ModeRead} t.presSubsOnline("del", params.actor, params, filters, sess.sid) t.presSubsOffline("del", params, filters, nilPresFilters, sess.sid, true) } else { pud := t.perUser[asUid] pud.delID = t.delID t.perUser[asUid] = pud // Notify user's other sessions t.presPubMessageDelete(asUid, pud.modeGiven&pud.modeWant, t.delID, dr, sess.sid) } sess.queueOut(NoErrParamsReply(msg, now, map[string]int{"del": t.delID})) return nil } // Handle request to delete the topic {del what="topic"}. // 1. If requester is the owner then it should have been handled at the hub, log an error. // 2. If requester is not the owner, treat it like {leave unsub=true}. func (t *Topic) replyDelTopic(sess *Session, asUid types.Uid, msg *ClientComMessage) error { if t.owner != asUid { return t.replyLeaveUnsub(sess, msg, asUid) } // This is an indication of a bug. logs.Err.Println("replyDelTopic called by owner (SHOULD NOT HAPPEN!)") return nil } // Delete credential func (t *Topic) replyDelCred(sess *Session, asUid types.Uid, authLvl auth.Level, msg *ClientComMessage) error { now := types.TimeNow() incomingReqTs := msg.Timestamp del := msg.Del if t.cat != types.TopicCatMe { sess.queueOut(ErrPermissionDeniedReply(msg, now)) return errors.New("del.cred: invalid topic category") } if del.Cred == nil || del.Cred.Method == "" { sess.queueOut(ErrMalformedReply(msg, now)) return errors.New("del.cred: missing method") } tags, err := deleteCred(asUid, authLvl, del.Cred) if tags != nil { // Check if anything has been actually removed. _, removed, _ := stringSliceDelta(t.tags, tags) if len(removed) > 0 { t.tags = tags t.presSubsOnline("tags", "", nilPresParams, nilPresFilters, "") } } else if err == nil { sess.queueOut(InfoNoActionReply(msg, now)) return nil } sess.queueOut(decodeStoreErrorExplicitTs(err, del.Id, del.Topic, now, incomingReqTs, nil)) return err } // Delete subscription. func (t *Topic) replyDelSub(sess *Session, asUid types.Uid, msg *ClientComMessage) error { now := types.TimeNow() del := msg.Del asChan, err := t.verifyChannelAccess(msg.Original) if err != nil { // User should not be able to address non-channel topic as channel. sess.queueOut(ErrNotFoundReply(msg, now)) return types.ErrNotFound } if asChan { // Don't allow channel readers to delete self-subscription. Use leave-unsub or del-topic. sess.queueOut(ErrPermissionDeniedReply(msg, now)) return errors.New("channel access denied: cannot delete subscription") } // Get ID of the affected user uid := types.ParseUserId(del.User) pud := t.perUser[asUid] if !(pud.modeGiven & pud.modeWant).IsAdmin() { err = errors.New("del.sub: permission denied") } else if uid.IsZero() || uid == asUid { // Cannot delete self-subscription. User [leave unsub] or [delete topic] err = errors.New("del.sub: cannot delete self-subscription") } else if t.cat == types.TopicCatP2P { // Don't try to delete the other P2P user err = errors.New("del.sub: cannot apply to a P2P topic") } if err != nil { sess.queueOut(ErrPermissionDeniedReply(msg, now)) return err } pud, ok := t.perUser[uid] if !ok { sess.queueOut(InfoNoActionReply(msg, now)) return errors.New("del.sub: user not found") } // Check if the user being ejected is the owner. if (pud.modeGiven & pud.modeWant).IsOwner() { err = errors.New("del.sub: cannot evict topic owner") } else if !pud.modeWant.IsJoiner() { // If the user has banned the topic, subscription should not be deleted. Otherwise user may be re-invited // which defeats the purpose of banning. err = errors.New("del.sub: cannot delete banned subscription") } if err != nil { sess.queueOut(ErrPermissionDeniedReply(msg, now)) return err } // Delete user's subscription from the database if err := store.Subs.Delete(t.name, uid); err != nil { if err == types.ErrNotFound { sess.queueOut(InfoNoActionReply(msg, now)) } else { sess.queueOut(ErrUnknownReply(msg, now)) return err } } else { sess.queueOut(NoErrReply(msg, now)) } // Update cached unread count: negative value if (pud.modeWant & pud.modeGiven).IsReader() { usersUpdateUnread(uid, pud.readID-t.lastID, true) } // ModeUnset signifies deleted subscription as opposite to ModeNone - no access. t.notifySubChange(uid, asUid, false, pud.modeWant, pud.modeGiven, types.ModeUnset, types.ModeUnset, sess.sid) t.evictUser(uid, true, "") // Notify plugins. pluginSubscription(&types.Subscription{Topic: t.name, User: uid.String()}, plgActDel) // If all P2P users were deleted, suspend the topic to let it shut down. if t.cat == types.TopicCatP2P && t.subsCount() == 0 { t.markPaused(true) globals.hub.unreg <- &topicUnreg{del: true, sess: nil, rcptTo: t.name, pkt: nil} } return nil } // replyLeaveUnsub is a request to unsubscribe user and detach all user's sessions from topic. func (t *Topic) replyLeaveUnsub(sess *Session, msg *ClientComMessage, asUid types.Uid) error { now := types.TimeNow() if asUid.IsZero() { panic("replyLeaveUnsub: zero asUid") } if t.owner == asUid { if msg.init { sess.queueOut(ErrPermissionDeniedReply(msg, now)) } return errors.New("replyLeaveUnsub: owner cannot unsubscribe") } var err error var asChan bool if msg.init { asChan, err = t.verifyChannelAccess(msg.Original) if err != nil { sess.queueOut(ErrNotFoundReply(msg, now)) return errors.New("replyLeaveUnsub: incorrect addressing of channel") } } pud := t.perUser[asUid] // Delete user's subscription from the database; msg could be nil, so cannot use msg.Original. if pud.isChan { // Handle channel reader. err = store.Subs.Delete(types.GrpToChn(t.name), asUid) } else { // Handle subscriber. err = store.Subs.Delete(t.name, asUid) } if err != nil { if msg.init { if err == types.ErrNotFound { sess.queueOut(InfoNoActionReply(msg, now)) err = nil } else { sess.queueOut(ErrUnknownReply(msg, now)) } } return err } if msg.init { sess.queueOut(NoErrReply(msg, now)) } var oldWant types.AccessMode var oldGiven types.AccessMode if !asChan { // Update cached unread count: negative value if (pud.modeWant & pud.modeGiven).IsReader() { usersUpdateUnread(asUid, pud.readID-t.lastID, true) } oldWant, oldGiven = pud.modeWant, pud.modeGiven } else { oldWant, oldGiven = types.ModeCChnReader, types.ModeCChnReader // Unsubscribe user's devices from the channel (FCM topic). t.channelSubUnsub(asUid, false) } // Send prsence notifictions to admins, other users, and user's other sessions. t.notifySubChange(asUid, asUid, asChan, oldWant, oldGiven, types.ModeUnset, types.ModeUnset, sess.sid) // Evict all user's sessions, clear cached data, send notifications. t.evictUser(asUid, true, sess.sid) // Notify plugins. pluginSubscription(&types.Subscription{Topic: t.name, User: asUid.String()}, plgActDel) if t.cat == types.TopicCatGrp { // Decrement group's cached member count. t.subCnt-- } // If all P2P users were deleted, suspend the topic to let it shut down. if t.cat == types.TopicCatP2P && t.subsCount() == 0 { t.markPaused(true) globals.hub.unreg <- &topicUnreg{del: true, sess: nil, rcptTo: t.name, pkt: nil} } return nil } // evictUser evicts all given user's sessions from the topic and clears user's cached data, if appropriate. func (t *Topic) evictUser(uid types.Uid, unsub bool, skip string) { now := types.TimeNow() pud, ok := t.perUser[uid] // Detach user from topic if unsub { if t.cat == types.TopicCatP2P { // P2P: mark user as deleted pud.online = 0 pud.deleted = true t.perUser[uid] = pud } else if ok { // Grp: delete per-user data delete(t.perUser, uid) t.computePerUserAcsUnion() if !pud.isChan { usersRegisterUser(uid, false) } } } else if ok { if pud.isChan { delete(t.perUser, uid) // No need to call computePerUserAcsUnion because removal of a channel reader does not change union permissions. // No need to unregister user as we ignore unread channel messages. } else { // Clear online status pud.online = 0 t.perUser[uid] = pud } } // Detach all user's sessions msg := NoErrEvicted("", t.original(uid), now) msg.Ctrl.Params = map[string]any{"unsub": unsub} msg.SkipSid = skip msg.uid = uid msg.AsUser = uid.UserId() for s := range t.sessions { if pssd, removed := t.remSession(s, uid); pssd != nil { if removed { s.detachSession(t.name) } if s.sid != skip { s.queueOut(msg) } } } } // User's subscription to a topic has changed, send presence notifications. // 1. New subscription // 2. Deleted subscription // 3. Permissions changed // Sending to // (a) Topic admins online on topic itself. // (b) Topic admins offline on 'me' if approval is needed. // (c) If subscription is deleted, 'gone' to target. // (d) 'off' to topic members online if deleted or muted. // (e) To target user. func (t *Topic) notifySubChange(uid, actor types.Uid, isChan bool, oldWant, oldGiven, newWant, newGiven types.AccessMode, skip string) { unsub := newWant == types.ModeUnset || newGiven == types.ModeUnset target := uid.UserId() dWant := types.ModeNone.String() if newWant.IsDefined() { if oldWant.IsDefined() && !oldWant.IsZero() { dWant = oldWant.Delta(newWant) } else { dWant = newWant.String() } } dGiven := types.ModeNone.String() if newGiven.IsDefined() { if oldGiven.IsDefined() && !oldGiven.IsZero() { dGiven = oldGiven.Delta(newGiven) } else { dGiven = newGiven.String() } } params := &presParams{ target: target, actor: actor.UserId(), dWant: dWant, dGiven: dGiven, } filterSharers := &presFilters{ filterIn: types.ModeCSharer, excludeUser: target, } // Announce the change in permissions to the admins who are online in the topic, exclude the target // and exclude the actor's session. t.presSubsOnline("acs", target, params, filterSharers, skip) // If it's a new subscription or if the user asked for permissions in excess of what was granted, // announce the request to topic admins on 'me' so they can approve the request. The notification // is not sent to the target user or the actor's session. if newWant.BetterThan(newGiven) || oldWant == types.ModeNone { t.presSubsOffline("acs", params, filterSharers, filterSharers, skip, true) } // Handling of muting/unmuting. // Case A: subscription deleted. // Case B: subscription muted only. if unsub { // Subscription deleted. // In case of a P2P topic subscribe/unsubscribe users from each other's notifications. if t.cat == types.TopicCatP2P { uid2 := t.p2pOtherUser(uid) // Remove user1's subscription to user2 and notify user1's other sessions that he is gone. t.presSingleUserOffline(uid, newWant&newGiven, "gone", nilPresParams, skip, false) // Tell user2 that user1 is offline but let him keep sending updates in case user1 resubscribes. presSingleUserOfflineOffline(uid2, target, "off", nilPresParams, "") } else if t.cat == types.TopicCatGrp && !isChan { // Notify all sharers that the user is offline now. t.presSubsOnline("off", uid.UserId(), nilPresParams, filterSharers, skip) // Notify target that the subscription is gone. presSingleUserOfflineOffline(uid, t.name, "gone", nilPresParams, skip) } } else { // Subscription altered. if !(newWant & newGiven).IsPresencer() && (oldWant & oldGiven).IsPresencer() { // Subscription just muted. var source string if t.cat == types.TopicCatP2P { source = t.p2pOtherUser(uid).UserId() } else if t.cat == types.TopicCatGrp && !isChan { source = t.name } if source != "" { // Tell user1 to start discarding updates from muted topic/user. presSingleUserOfflineOffline(uid, source, "off+dis", nilPresParams, "") } } else if (newWant & newGiven).IsPresencer() && !(oldWant & oldGiven).IsPresencer() { // Subscription un-muted. // Notify subscriber of topic's online status. if t.cat == types.TopicCatGrp && !isChan { t.presSingleUserOffline(uid, newWant&newGiven, "?unkn+en", nilPresParams, "", false) } else if t.cat == types.TopicCatMe { // User is visible online now, notify subscribers. t.presUsersOfInterest("on+en", t.userAgent) } } // Notify target that permissions have changed. // Notify sessions online in the topic. t.presSubsOnlineDirect("acs", params, &presFilters{singleUser: target}, skip) // Notify target's other sessions on 'me'. t.presSingleUserOffline(uid, newWant&newGiven, "acs", params, skip, true) } } // FIXME: this won't work correctly with multiplexing sessions. func (t *Topic) mostRecentSession() *Session { var sess *Session var latest int64 for s := range t.sessions { sessionLastAction := atomic.LoadInt64(&s.lastAction) if sessionLastAction > latest { sess = s latest = sessionLastAction } } return sess } const ( // Topic is fully initialized. topicStatusLoaded = 0x1 // Topic is paused: all packets are rejected. topicStatusPaused = 0x2 // Topic is in the process of being deleted. This is irrecoverable. topicStatusMarkedDeleted = 0x10 // Topic is suspended: read-only mode. topicStatusReadOnly = 0x20 ) // statusChangeBits sets or removes given bits from t.status func (t *Topic) statusChangeBits(bits int32, set bool) { for { oldStatus := atomic.LoadInt32(&t.status) newStatus := oldStatus if set { newStatus |= bits } else { newStatus &= ^bits } if newStatus == oldStatus { break } if atomic.CompareAndSwapInt32(&t.status, oldStatus, newStatus) { break } } } // markLoaded indicates that topic subscribers have been loaded into memory. func (t *Topic) markLoaded() { t.statusChangeBits(topicStatusLoaded, true) } // markPaused pauses or unpauses the topic. When the topic is paused all // messages are rejected. func (t *Topic) markPaused(pause bool) { t.statusChangeBits(topicStatusPaused, pause) } // markDeleted marks topic as being deleted. func (t *Topic) markDeleted() { t.statusChangeBits(topicStatusMarkedDeleted, true) } // markReadOnly suspends/un-suspends the topic: adds or removes the 'read-only' flag. func (t *Topic) markReadOnly(readOnly bool) { t.statusChangeBits(topicStatusReadOnly, readOnly) } // isInactive checks if topic is paused or being deleted. func (t *Topic) isInactive() bool { return (atomic.LoadInt32(&t.status) & (topicStatusPaused | topicStatusMarkedDeleted)) != 0 } func (t *Topic) isReadOnly() bool { return (atomic.LoadInt32(&t.status) & topicStatusReadOnly) != 0 } func (t *Topic) isLoaded() bool { return (atomic.LoadInt32(&t.status) & topicStatusLoaded) != 0 } func (t *Topic) isDeleted() bool { return (atomic.LoadInt32(&t.status) & topicStatusMarkedDeleted) != 0 } // Get topic name suitable for the given client func (t *Topic) original(uid types.Uid) string { if t.cat == types.TopicCatP2P { if pud, ok := t.perUser[uid]; ok { return pud.topicName } panic("Invalid P2P topic") } if t.cat == types.TopicCatGrp && t.isChan { if t.perUser[uid].isChan { // This is a channel reader. return types.GrpToChn(t.xoriginal) } } return t.xoriginal } // Get ID of the other user in a P2P topic func (t *Topic) p2pOtherUser(uid types.Uid) types.Uid { if t.cat == types.TopicCatP2P { // Try to find user in subscribers. for u2 := range t.perUser { if u2.Compare(uid) != 0 { return u2 } } } // Even when one user is deleted, the subscription must be restored // before p2pOtherUser is called. panic("Not a valid P2P topic") } // Get per-session value of fnd.Public func (t *Topic) fndGetPublic(sess *Session) string { if t.cat == types.TopicCatFnd { if t.public == nil { return "" } if pubmap, ok := t.public.(map[string]any); ok { if public, ok := pubmap[sess.sid].(string); ok { return public } return "" } panic("Invalid Fnd.Public type") } panic("Not Fnd topic") } // Assign per-session fnd.Public. Returns true if value has been changed. func (t *Topic) fndSetPublic(sess *Session, public any) bool { if t.cat != types.TopicCatFnd { panic("Not Fnd topic") } var pubmap map[string]any var ok bool if t.public != nil { if pubmap, ok = t.public.(map[string]any); !ok { // This could only happen if fnd.public is assigned outside of this function. panic("Invalid Fnd.Public type") } } if pubmap == nil { pubmap = make(map[string]any) } if public != nil { pubmap[sess.sid] = public } else { ok = (pubmap[sess.sid] != nil) delete(pubmap, sess.sid) if len(pubmap) == 0 { pubmap = nil } } t.public = pubmap return ok } // Remove per-session value of fnd.Public. func (t *Topic) fndRemovePublic(sess *Session) { if t.public == nil { return } // FIXME: case of a multiplexing session won't work correctly. // Maybe handle it at the proxy topic. if pubmap, ok := t.public.(map[string]any); ok { delete(pubmap, sess.sid) return } panic("Invalid Fnd.Public type") } func (t *Topic) accessFor(authLvl auth.Level) types.AccessMode { return selectAccessMode(authLvl, t.accessAnon, t.accessAuth, getDefaultAccess(t.cat, true, false)) } // subsCount returns the number of topic subscribers. This method is different from subCnt with respect to channels: // * subsCount counts subscribers + attached channel users. // * subCnt counts all subscribers (including all channel users). func (t *Topic) subsCount() int { if t.cat == types.TopicCatP2P { count := 0 for uid := range t.perUser { if !t.perUser[uid].deleted { count++ } } return count } return len(t.perUser) } // Add session record. 'user' may be different from sess.uid. func (t *Topic) addSession(sess *Session, asUid types.Uid, isChanSub bool) { s := sess if sess.multi != nil { s = s.multi } if pssd, ok := t.sessions[s]; ok { // Subscription already exists. if s.isMultiplex() && !sess.background { // This slice is expected to be relatively short. // Not doing anything fancy here like maps or sorting. pssd.muids = append(pssd.muids, asUid) t.sessions[s] = pssd } // Maybe panic here. return } if s.isMultiplex() { if sess.background { t.sessions[s] = perSessionData{} } else { t.sessions[s] = perSessionData{muids: []types.Uid{asUid}, isChanSub: isChanSub} } } else { t.sessions[s] = perSessionData{uid: asUid, isChanSub: isChanSub} } } // Disconnects session from topic if either one of the following is true: // * 's' is an ordinary session AND ('asUid' is zero OR 'asUid' matches subscribed user). // * 's' is a multiplexing session and it's being dropped all together ('asUid' is zero ). // If 's' is a multiplexing session and asUid is not zero, it's removed from the list of session // users 'muids'. // Returns perSessionData if it was found and true if session was actually detached from topic. func (t *Topic) remSession(sess *Session, asUid types.Uid) (*perSessionData, bool) { s := sess if sess.multi != nil { s = s.multi } pssd, ok := t.sessions[s] if !ok { // Session not found at all. return nil, false } if pssd.uid == asUid || asUid.IsZero() { delete(t.sessions, s) return &pssd, true } for i := range pssd.muids { if pssd.muids[i] == asUid { pssd.muids[i] = pssd.muids[len(pssd.muids)-1] pssd.muids = pssd.muids[:len(pssd.muids)-1] t.sessions[s] = pssd if len(pssd.muids) == 0 { delete(t.sessions, s) return &pssd, true } return &pssd, false } } return nil, false } // Check if topic has any online (non-background) users. func (t *Topic) isOnline() bool { // Find at least one non-background session. for s, pssd := range t.sessions { if s.isMultiplex() && len(pssd.muids) > 0 { return true } if !s.background { return true } } return false } // Verifies if topic can be access by the provided name: access any topic as non-channel, access channel as channel. // Returns true if access is for channel, false if not and error if access is invalid. func (t *Topic) verifyChannelAccess(asTopic string) (bool, error) { if !types.IsChannel(asTopic) { return false, nil } if t.isChan { return true, nil } return false, types.ErrNotFound } // Infer topic category from name. func topicCat(name string) types.TopicCat { return types.GetTopicCat(name) } // Generate the name of the group topic as a "grp" followed by random-looking // unique string. func genTopicName() string { return "grp" + store.Store.GetUidString() } // Convert expanded (routable) topic name into name suitable for sending to the user. // For example p2pAbCDef123 -> usrAbCDef func topicNameForUser(name string, uid types.Uid, isChan bool) string { switch topicCat(name) { case types.TopicCatMe: return "me" case types.TopicCatFnd: return "fnd" case types.TopicCatP2P: topic, _ := types.P2PNameForUser(uid, name) return topic case types.TopicCatGrp: if isChan { return types.GrpToChn(name) } } return name } // calculateUnreadInRanges calculates how many unread messages are within the given ranges. // unreadStart is the first unread message SeqId (readID + 1), unreadEnd is the last possible message SeqId. // Assumes ranges are sorted by Low ascending. func calculateUnreadInRanges(readID, lastID int, ranges []types.Range) int { if readID >= lastID { // No unread messages return 0 } unreadStart := readID + 1 unreadEnd := lastID // Sum up unread messages. count := 0 for i := 0; i < len(ranges); i++ { rangeStart := ranges[i].Low rangeEnd := ranges[i].Hi if rangeEnd == 0 { rangeEnd = rangeStart + 1 } // Find the first range where rangeEnd > readID if rangeEnd <= readID { continue } // Find intersection of [unreadStart, unreadEnd] and [rangeStart, rangeEnd) intersectionStart := max(unreadStart, rangeStart) intersectionEnd := min(unreadEnd+1, rangeEnd) // +1 because unreadEnd is inclusive if intersectionStart < intersectionEnd { count += intersectionEnd - intersectionStart } } return count } ================================================ FILE: server/topic_proxy.go ================================================ /****************************************************************************** * Description : * Topic in a cluster which serves as a local representation of the master * topic hosted at another node. *****************************************************************************/ package main import ( "net/http" "time" "github.com/tinode/chat/server/logs" "github.com/tinode/chat/server/store/types" ) func (t *Topic) runProxy(hub *Hub) { killTimer := time.NewTimer(time.Hour) killTimer.Stop() for { select { case msg := <-t.reg: // Request to add a connection to this topic if t.isInactive() { msg.sess.queueOut(ErrLockedReply(msg, types.TimeNow())) } else if err := globals.cluster.routeToTopicMaster(ProxyReqJoin, msg, t.name, msg.sess); err != nil { // Response (ctrl message) will be handled when it's received via the proxy channel. logs.Warn.Printf("proxy topic[%s]: route join request from proxy to master failed - %s", t.name, err) msg.sess.queueOut(ErrClusterUnreachableReply(msg, types.TimeNow())) } if msg.sess.inflightReqs != nil { msg.sess.inflightReqs.Done() } case msg := <-t.unreg: if !t.handleProxyLeaveRequest(msg, killTimer) { sid := "nil" if msg.sess != nil { sid = msg.sess.sid } logs.Warn.Printf("proxy topic[%s]: failed to update proxy topic state for leave request - sid %s", t.name, sid) msg.sess.queueOut(ErrClusterUnreachableReply(msg, types.TimeNow())) } if msg.init && msg.sess.inflightReqs != nil { // If it's a client initiated request. msg.sess.inflightReqs.Done() } case msg := <-t.clientMsg: // Content message intended for broadcasting to recipients if err := globals.cluster.routeToTopicMaster(ProxyReqBroadcast, msg, t.name, msg.sess); err != nil { logs.Warn.Printf("topic proxy[%s]: route broadcast request from proxy to master failed - %s", t.name, err) msg.sess.queueOut(ErrClusterUnreachableReply(msg, types.TimeNow())) } case msg := <-t.serverMsg: if msg.Info != nil || msg.Pres != nil { globals.cluster.routeToTopicIntraCluster(t.name, msg, msg.sess) } else { // FIXME: should something be done here? logs.Err.Printf("ERROR!!! topic proxy[%s]: unexpected server-side message in proxy topic %s", t.name, msg.describe()) } case msg := <-t.meta: // Request to get/set topic metadata if err := globals.cluster.routeToTopicMaster(ProxyReqMeta, msg, t.name, msg.sess); err != nil { logs.Warn.Printf("proxy topic[%s]: route meta request from proxy to master failed - %s", t.name, err) msg.sess.queueOut(ErrClusterUnreachableReply(msg, types.TimeNow())) } case upd := <-t.supd: // Either an update to 'me' user agent from one of the sessions or // background session comes to foreground. req := ProxyReqMeUserAgent tmpSess := &Session{userAgent: upd.userAgent} if upd.sess != nil { // Subscribed user may not match session user. Find out who is subscribed pssd, ok := t.sessions[upd.sess] if !ok { logs.Warn.Printf("proxy topic[%s]: sess update request from detached session - sid %s", t.name, upd.sess.sid) continue } req = ProxyReqBgSession tmpSess.uid = pssd.uid tmpSess.sid = upd.sess.sid tmpSess.userAgent = upd.sess.userAgent } if err := globals.cluster.routeToTopicMaster(req, nil, t.name, tmpSess); err != nil { logs.Warn.Printf("proxy topic[%s]: route sess update request from proxy to master failed - %s", t.name, err) } case msg := <-t.proxy: t.proxyMasterResponse(msg, killTimer) case sd := <-t.exit: // Tell sessions to remove the topic for s := range t.sessions { s.detachSession(t.name) } if err := globals.cluster.topicProxyGone(t.name); err != nil { logs.Warn.Printf("proxy topic[%s] shutdown: failed to notify master - %s", t.name, err) } // Report completion back to sender, if 'done' is not nil. if sd.done != nil { sd.done <- true } return case <-killTimer.C: // Topic timeout hub.unreg <- &topicUnreg{rcptTo: t.name} } } } // Takes a session leave request, forwards it to the topic master and // modifies the local state accordingly. // Returns whether the operation was successful. func (t *Topic) handleProxyLeaveRequest(msg *ClientComMessage, killTimer *time.Timer) bool { // Detach session from topic; session may continue to function. var asUid types.Uid if msg.init { asUid = types.ParseUserId(msg.AsUser) } if asUid.IsZero() { if pssd, ok := t.sessions[msg.sess]; ok { asUid = pssd.uid } else { logs.Warn.Printf("proxy topic[%s]: leave request sent for unknown session", t.name) return false } } // Remove the session from the topic without waiting for a response from the master node // because by the time the response arrives this session may be already gone from the session store // and we won't be able to find and remove it by its sid. pssd, result := t.remSession(msg.sess, asUid) if result { msg.sess.delSub(t.name) } if !msg.init { // Explicitly specify the uid because the master multiplex session needs to know which // of its multiple hosted sessions to delete. msg.AsUser = asUid.UserId() msg.Leave = &MsgClientLeave{} msg.init = true } // Make sure we set the Original field if it's empty (e.g. when session is terminating altogether). if msg.Original == "" { if t.cat == types.TopicCatGrp && t.isChan { // It's a channel topic. Original topic name depends the subscription type. if result && pssd.isChanSub { msg.Original = types.GrpToChn(t.xoriginal) } else { msg.Original = t.xoriginal } } else { msg.Original = t.original(asUid) } } if err := globals.cluster.routeToTopicMaster(ProxyReqLeave, msg, t.name, msg.sess); err != nil { logs.Warn.Printf("proxy topic[%s]: route leave request from proxy to master failed - %s", t.name, err) } if len(t.sessions) == 0 { // No more sessions attached. Start the countdown. killTimer.Reset(idleProxyTopicTimeout) } return result } // proxyMasterResponse at proxy topic processes a master topic response to an earlier request. func (t *Topic) proxyMasterResponse(msg *ClusterResp, killTimer *time.Timer) { // Kills topic after a period of inactivity. keepAlive := idleProxyTopicTimeout if msg.SrvMsg.Pres != nil && msg.SrvMsg.Pres.What == "acs" && msg.SrvMsg.Pres.Acs != nil { // If the server changed acs on this topic, update the internal state. t.updateAcsFromPresMsg(msg.SrvMsg.Pres) } if msg.OrigSid == "*" { // It is a broadcast. switch { case msg.SrvMsg.Pres != nil || msg.SrvMsg.Data != nil || msg.SrvMsg.Info != nil: // Regular broadcast. t.handleProxyBroadcast(msg.SrvMsg) case msg.SrvMsg.Ctrl != nil: // Ctrl broadcast. E.g. for user eviction. t.proxyCtrlBroadcast(msg.SrvMsg) default: } } else { sess := globals.sessionStore.Get(msg.OrigSid) if sess == nil { logs.Warn.Printf("proxy topic[%s]: session %s not found; already terminated?", t.name, msg.OrigSid) } switch msg.OrigReqType { case ProxyReqJoin: if sess != nil && msg.SrvMsg.Ctrl != nil { // TODO: do we need to let the master topic know that the subscription is not longer valid // or is it already informed by the session when it terminated? // Subscription result. if msg.SrvMsg.Ctrl.Code < 300 { sess.sessionStoreLock.Lock() // Make sure the session isn't gone yet. if session := globals.sessionStore.Get(msg.OrigSid); session != nil { // Successful subscriptions. t.addSession(session, msg.SrvMsg.uid, types.IsChannel(msg.SrvMsg.Ctrl.Topic)) session.addSub(t.name, &Subscription{ broadcast: t.clientMsg, done: t.unreg, meta: t.meta, supd: t.supd, }) } sess.sessionStoreLock.Unlock() killTimer.Stop() } else if len(t.sessions) == 0 { killTimer.Reset(keepAlive) } } case ProxyReqBroadcast, ProxyReqMeta, ProxyReqCall: // no processing case ProxyReqLeave: if msg.SrvMsg != nil && msg.SrvMsg.Ctrl != nil { if msg.SrvMsg.Ctrl.Code < 300 { if sess != nil { t.remSession(sess, sess.uid) } } // All sessions are gone. Start the kill timer. if len(t.sessions) == 0 { killTimer.Reset(keepAlive) } } default: logs.Err.Printf("proxy topic[%s] received response referencing unexpected request type %d", t.name, msg.OrigReqType) } if sess != nil && !sess.queueOut(msg.SrvMsg) { logs.Err.Printf("proxy topic[%s]: timeout in sending response - sid %s", t.name, sess.sid) } } } // handleProxyBroadcast broadcasts a Data, Info or Pres message to sessions attached to this proxy topic. func (t *Topic) handleProxyBroadcast(msg *ServerComMessage) { if t.isInactive() { // Ignore broadcast - topic is paused or being deleted. return } if msg.Data != nil { t.lastID = msg.Data.SeqId } t.broadcastToSessions(msg) } // proxyCtrlBroadcast broadcasts a ctrl command to certain sessions attached to this proxy topic. func (t *Topic) proxyCtrlBroadcast(msg *ServerComMessage) { if msg.Ctrl.Code == http.StatusResetContent && msg.Ctrl.Text == "evicted" { // We received a ctrl command for evicting a user. if msg.uid.IsZero() { logs.Err.Panicf("proxy topic[%s]: proxy received evict message with empty uid", t.name) } for sess := range t.sessions { // Proxy topic may only have ordinary sessions. No multiplexing or proxy sessions here. if _, removed := t.remSession(sess, msg.uid); removed { sess.detachSession(t.name) if sess.sid != msg.SkipSid { sess.queueOut(msg) } } } } } // updateAcsFromPresMsg modifies user acs in Topic's perUser struct based on the data in `pres`. func (t *Topic) updateAcsFromPresMsg(pres *MsgServerPres) { uid := types.ParseUserId(pres.Src) if uid.IsZero() { if t.cat != types.TopicCatMe { logs.Warn.Printf("proxy topic[%s]: received acs change for invalid user id '%s'", t.name, pres.Src) } return } // If t.perUser[uid] does not exist, pud is initialized with blanks, otherwise it gets existing values. pud := t.perUser[uid] dacs := pres.Acs if err := pud.modeWant.ApplyMutation(dacs.Want); err != nil { logs.Warn.Printf("proxy topic[%s]: could not process acs change - want: %s", t.name, err) return } if err := pud.modeGiven.ApplyMutation(dacs.Given); err != nil { logs.Warn.Printf("proxy topic[%s]: could not process acs change - given: %s", t.name, err) return } // Update existing or add new. t.perUser[uid] = pud } ================================================ FILE: server/topic_test.go ================================================ package main import ( "fmt" "net/http" "os" "sync" "testing" "time" "github.com/golang/mock/gomock" "github.com/tinode/chat/server/auth" "github.com/tinode/chat/server/logs" "github.com/tinode/chat/server/store" "github.com/tinode/chat/server/store/mock_store" "github.com/tinode/chat/server/store/types" ) type responses struct { messages []any } // Test fixture. type TopicTestHelper struct { numUsers int uids []types.Uid // Gomock controller. ctrl *gomock.Controller // Sessions. sessions []*Session sessWg *sync.WaitGroup // Per-session responses (i.e. what gets dumped into sessions' write loops). results []*responses // Hub. hub *Hub // Messages captured from Hub.route channel on the per-user (RcptTo) basis. hubMessages map[string][]*ServerComMessage // For stopping hub loop. hubDone chan bool // Topic. topic *Topic // Mock objects. mm *mock_store.MockMessagesPersistenceInterface uu *mock_store.MockUsersPersistenceInterface tt *mock_store.MockTopicsPersistenceInterface ss *mock_store.MockSubsPersistenceInterface } func (b *TopicTestHelper) finish() { b.topic.killTimer.Stop() b.topic.callEstablishmentTimer.Stop() // Stop session write loops. for _, s := range b.sessions { close(s.send) } b.sessWg.Wait() // Hub loop. close(b.hub.routeSrv) close(b.hub.routeCli) <-b.hubDone } func (b *TopicTestHelper) newSession(sid string, uid types.Uid) (*Session, *responses) { s := &Session{ sid: sid, uid: uid, subs: make(map[string]*Subscription), send: make(chan any, 10), detach: make(chan string, 10), } r := &responses{} b.sessWg.Add(1) go s.testWriteLoop(r, b.sessWg) return s, r } func (b *TopicTestHelper) setUp(t *testing.T, numUsers int, cat types.TopicCat, topicName string, attachSessions bool) { t.Helper() b.numUsers = numUsers b.uids = make([]types.Uid, numUsers) for i := range numUsers { // Can't use 0 as a valid uid. b.uids[i] = types.Uid(i + 1) } // Mocks. b.ctrl = gomock.NewController(t) b.mm = mock_store.NewMockMessagesPersistenceInterface(b.ctrl) b.uu = mock_store.NewMockUsersPersistenceInterface(b.ctrl) b.tt = mock_store.NewMockTopicsPersistenceInterface(b.ctrl) b.ss = mock_store.NewMockSubsPersistenceInterface(b.ctrl) store.Messages = b.mm store.Users = b.uu store.Topics = b.tt store.Subs = b.ss // Sessions. b.sessions = make([]*Session, b.numUsers) b.results = make([]*responses, b.numUsers) b.sessWg = &sync.WaitGroup{} for i := range b.sessions { s, r := b.newSession(fmt.Sprintf("sid%d", i), b.uids[i]) b.results[i] = r b.sessions[i] = s } // Hub. b.hub = &Hub{ routeCli: make(chan *ClientComMessage, 10), routeSrv: make(chan *ServerComMessage, 10), } globals.hub = b.hub b.hubMessages = make(map[string][]*ServerComMessage) b.hubDone = make(chan bool) go b.hub.testHubLoop(t, b.hubMessages, b.hubDone) // Topic. pu := make(map[types.Uid]perUserData) ps := make(map[*Session]perSessionData) for i, uid := range b.uids { puData := perUserData{ modeWant: types.ModeCFull, modeGiven: types.ModeCFull, } if cat == types.TopicCatP2P { puData.topicName = b.uids[i^1].UserId() } if attachSessions { ps[b.sessions[i]] = perSessionData{uid: uid} puData.online = 1 } pu[uid] = puData } b.topic = &Topic{ name: topicName, cat: cat, status: topicStatusLoaded, perUser: pu, isProxy: false, sessions: ps, killTimer: time.NewTimer(time.Hour), callEstablishmentTimer: time.NewTimer(time.Second), } if cat != types.TopicCatSys { b.topic.accessAuth = getDefaultAccess(cat, true, false) b.topic.accessAnon = getDefaultAccess(cat, true, false) } if cat == types.TopicCatMe { b.topic.xoriginal = "me" } if cat == types.TopicCatGrp { b.topic.xoriginal = topicName b.topic.owner = b.uids[0] } } func (b *TopicTestHelper) tearDown() { globals.hub = nil store.Messages = nil store.Users = nil store.Topics = nil store.Subs = nil b.ctrl.Finish() } func (s *Session) testWriteLoop(results *responses, wg *sync.WaitGroup) { for msg := range s.send { results.messages = append(results.messages, msg) } wg.Done() } func (h *Hub) testHubLoop(t *testing.T, results map[string][]*ServerComMessage, done chan bool) { t.Helper() for msg := range h.routeSrv { if msg.RcptTo == "" { // Don't call t.Fatal from goroutine - instead send error info back results["__ERROR__"] = []*ServerComMessage{{ Ctrl: &MsgServerCtrl{ Code: 500, Text: "Hub.route received a message without addressee.", }, }} done <- true return } results[msg.RcptTo] = append(results[msg.RcptTo], msg) } done <- true } func TestHandleBroadcastDataP2P(t *testing.T) { numUsers := 2 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatP2P, "p2p-test" /*attach=*/, true) defer helper.tearDown() helper.mm.EXPECT().Save(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, true) from := helper.uids[0].UserId() msg := &ClientComMessage{ AsUser: from, Original: from, Pub: &MsgClientPub{ Topic: "p2p", Content: "test", NoEcho: true, }, sess: helper.sessions[0], } helper.topic.handleClientMsg(msg) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } // Message uid1 -> uid2. for i, m := range helper.results { if i == 0 { if len(m.messages) != 0 { t.Fatalf("Uid1: expected 0 messages, got %d", len(m.messages)) } } else { if len(m.messages) != 1 { t.Fatalf("Uid2: expected 1 messages, got %d", len(m.messages)) } r := m.messages[0].(*ServerComMessage) if r.Data == nil { t.Fatalf("Response[0] must have a ctrl message") } if r.Data.Topic != from { t.Errorf("Response[0] topic: expected '%s', got '%s'", from, r.Data.Topic) } if r.Data.Content.(string) != "test" { t.Errorf("Response[0] content: expected 'test', got '%s'", r.Data.Content.(string)) } if r.Data.From != from { t.Errorf("Response[0] from: expected '%s', got '%s'", from, r.Data.From) } } } // Checking presence messages routed through the helper. if len(helper.hubMessages) != 2 { t.Fatal("Huhelper.route expected exactly two recipients routed via huhelper.") } for i, uid := range helper.uids { if mm, ok := helper.hubMessages[uid.UserId()]; ok { if len(mm) == 1 { s := mm[0] if s.Pres != nil { p := s.Pres if p.Topic != "me" { t.Errorf("Uid %s: pres notify on topic is expected to be 'me', got %s", uid.UserId(), p.Topic) } if p.SkipTopic != "p2p-test" { t.Errorf("Uid %s: pres skip topic is expected to be 'p2p-test', got %s", uid.UserId(), p.SkipTopic) } expectedSrc := helper.uids[i^1].UserId() if p.Src != expectedSrc { t.Errorf("Uid %s: pres.src expected: %s, found: %s", uid.UserId(), expectedSrc, p.Src) } } else { t.Errorf("Uid %s: hub message expected to be {pres}.", uid.UserId()) } } else { t.Errorf("Uid %s: expected 1 hub message, got %d.", uid.UserId(), len(mm)) } } else { t.Errorf("Uid %s: no hub results found.", uid.UserId()) } } } func TestHandleBroadcastCall(t *testing.T) { numUsers := 2 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatP2P, "p2p-test" /*attach=*/, true) globals.iceServers = []iceServer{{Username: "dummy"}} helper.topic.lastID = 5 defer helper.tearDown() helper.mm.EXPECT().Save(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, true) from := helper.uids[0].UserId() msg := &ClientComMessage{ AsUser: from, Original: from, Pub: &MsgClientPub{ Topic: "p2p", Head: map[string]any{"webrtc": "started"}, Content: "test", NoEcho: true, }, sess: helper.sessions[0], } helper.topic.handleClientMsg(msg) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } globals.iceServers = nil // Message uid1 -> uid2. for i, m := range helper.results { if i == 0 { if len(m.messages) != 0 { t.Fatalf("Uid1: expected 0 messages, got %d", len(m.messages)) } } else { if len(m.messages) != 1 { t.Fatalf("Uid2: expected 1 messages, got %d", len(m.messages)) } r := m.messages[0].(*ServerComMessage) if r.Data == nil { t.Fatalf("Response[0] must have a ctrl message") } if r.Data.Topic != from { t.Errorf("Response[0] topic: expected '%s', got '%s'", from, r.Data.Topic) } if r.Data.Content.(string) != "test" { t.Errorf("Response[0] content: expected 'test', got '%s'", r.Data.Content.(string)) } if r.Data.Head == nil || r.Data.Head["webrtc"].(string) != "started" { t.Errorf("Response[0] head: expected {'webrtc': 'started'}', got '%s'", r.Data.Content.(string)) } if r.Data.From != from { t.Errorf("Response[0] from: expected '%s', got '%s'", from, r.Data.From) } } } // Checking presence messages routed through the helper. if len(helper.hubMessages) != 2 { t.Fatal("Huhelper.route expected exactly two recipients routed via huhelper.") } for i, uid := range helper.uids { if mm, ok := helper.hubMessages[uid.UserId()]; ok { if len(mm) == 1 { s := mm[0] if s.Pres != nil { p := s.Pres if p.Topic != "me" { t.Errorf("Uid %s: pres notify on topic is expected to be 'me', got %s", uid.UserId(), p.Topic) } if p.SkipTopic != "p2p-test" { t.Errorf("Uid %s: pres skip topic is expected to be 'p2p-test', got %s", uid.UserId(), p.SkipTopic) } expectedSrc := helper.uids[i^1].UserId() if p.Src != expectedSrc { t.Errorf("Uid %s: pres.src expected: %s, found: %s", uid.UserId(), expectedSrc, p.Src) } } else { t.Errorf("Uid %s: hub message expected to be {pres}.", uid.UserId()) } } else { t.Errorf("Uid %s: expected 1 hub message, got %d.", uid.UserId(), len(mm)) } } else { t.Errorf("Uid %s: no hub results found.", uid.UserId()) } } if helper.topic.currentCall == nil { t.Fatal("No call in progress") } if helper.topic.currentCall.seq != 6 { t.Errorf("Call seq: expected 6, found %d.", helper.topic.currentCall.seq) } if len(helper.topic.currentCall.parties) != 1 { t.Fatalf("Call parties: expected 1, found %d.", len(helper.topic.currentCall.parties)) } if p, ok := helper.topic.currentCall.parties[helper.sessions[0].sid]; ok { if !p.isOriginator { t.Error("Call party is not a call originator.") } if p.uid != helper.uids[0] { t.Errorf("Call party wrong uid: expected %s, found %s.", helper.uids[0].UserId(), p.uid.UserId()) } } else { t.Errorf("Call party for session %s not found.", helper.sessions[0].sid) } } func TestHandleBroadcastDataGroup(t *testing.T) { topicName := "grp-test" numUsers := 4 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatGrp, topicName, true) defer func() { store.Messages = nil helper.tearDown() }() helper.mm.EXPECT().Save(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, true) // User 3 isn't allowed to read. pu3 := helper.topic.perUser[helper.uids[3]] pu3.modeWant = types.ModeJoin | types.ModeWrite | types.ModePres pu3.modeGiven = pu3.modeWant helper.topic.perUser[helper.uids[3]] = pu3 from := helper.uids[0].UserId() msg := &ClientComMessage{ AsUser: from, Original: topicName, Pub: &MsgClientPub{ Topic: topicName, Content: "test", NoEcho: true, }, sess: helper.sessions[0], } if helper.topic.lastID != 0 { t.Errorf("Topic.lastID: expected 0, found %d", helper.topic.lastID) } helper.topic.handleClientMsg(msg) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } if helper.topic.lastID != 1 { t.Errorf("Topic.lastID: expected 1, found %d", helper.topic.lastID) } // Message uid0 -> uid1, uid2, uid3. // Uid0 is the sender. if len(helper.results[0].messages) != 0 { t.Fatalf("Uid0 is the sender: expected 0 messages, got %d", len(helper.results[0].messages)) } // Uid3 is not a topic reader. if len(helper.results[3].messages) != 0 { t.Fatalf("Uid3 isn't allowed to read messages: expected 0 messages, got %d", len(helper.results[3].messages)) } for i := 1; i < 3; i++ { m := helper.results[i] if len(m.messages) != 1 { t.Fatalf("Uid%d: expected 1 messages, got %d", i, len(m.messages)) } r := m.messages[0].(*ServerComMessage) if r.Data == nil { t.Fatalf("Response[0] must have a ctrl message") } if r.Data.Topic != topicName { t.Errorf("Response[0] topic: expected '%s', got '%s'", topicName, r.Data.Topic) } if r.Data.From != from { t.Errorf("Response[0] from: expected '%s', got '%s'", from, r.Data.From) } if r.Data.Content.(string) != "test" { t.Errorf("Response[0] content: expected 'test', got '%s'", r.Data.Content.(string)) } } // Presence messages. if len(helper.hubMessages) != 3 { t.Fatal("Hubhelper.route expected exactly three recipients routed via huhelper.") } for i, uid := range helper.uids { if i == 3 { if _, ok := helper.hubMessages[uid.UserId()]; ok { t.Errorf("Uid %s: not expected to receive pres notifications.", uid.UserId()) } continue } if mm, ok := helper.hubMessages[uid.UserId()]; ok { if len(mm) == 1 { s := mm[0] if s.Pres != nil { p := s.Pres if p.Topic != "me" { t.Errorf("Uid %s: pres notify on topic is expected to be 'me', got %s", uid.UserId(), p.Topic) } if p.SkipTopic != topicName { t.Errorf("Uid %s: pres skip topic is expected to be 'p2p-test', got %s", uid.UserId(), p.SkipTopic) } if p.Src != topicName { t.Errorf("Uid %s: pres.src expected: %s, found: %s", uid.UserId(), topicName, p.Src) } } else { t.Errorf("Uid %s: hub message expected to be {pres}.", uid.UserId()) } } else { t.Errorf("Uid %s: expected 1 hub message, got %d.", uid.UserId(), len(mm)) } } else { t.Errorf("Uid %s: no hub results found.", uid.UserId()) } } } func TestHandleBroadcastDataMissingWritePermission(t *testing.T) { topicName := "p2p-test" numUsers := 2 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatP2P, topicName, true) defer helper.tearDown() // Remove W permission for uid1. uid1 := helper.uids[0] pud := helper.topic.perUser[uid1] pud.modeGiven = types.ModeRead | types.ModeJoin helper.topic.perUser[uid1] = pud // Make test message. from := helper.uids[0].UserId() msg := &ClientComMessage{ AsUser: from, Original: from, Pub: &MsgClientPub{ Topic: "p2p", Content: "test", }, sess: helper.sessions[0], } helper.topic.handleClientMsg(msg) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } // Message uid1 -> uid2. if len(helper.results[0].messages) == 1 { em := helper.results[0].messages[0].(*ServerComMessage) if em.Ctrl == nil { t.Fatal("User 1 is expected to receive a ctrl message") } if em.Ctrl.Code < 400 || em.Ctrl.Code >= 500 { t.Errorf("User1: expected ctrl.code 4xx, received %d", em.Ctrl.Code) } } else { t.Errorf("User 1 is expected to receive one message vs %d received.", len(helper.results[0].messages)) } if len(helper.results[1].messages) != 0 { t.Errorf("User 2 is not expected to receive any messages, %d received.", len(helper.results[1].messages)) } // Checking presence messages routed through hubhelper. if len(helper.hubMessages) != 0 { t.Errorf("Hubhelper.route did not expect any messages, however %d received.", len(helper.hubMessages)) } } func TestHandleBroadcastDataDbError(t *testing.T) { numUsers := 2 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatP2P, "p2p-test", true) defer helper.tearDown() // DB returns an error. helper.mm.EXPECT().Save(gomock.Any(), gomock.Any(), gomock.Any()).Return(types.ErrInternal, false) // Make test message. from := helper.uids[0].UserId() msg := &ClientComMessage{ AsUser: from, Pub: &MsgClientPub{ Topic: "p2p", Content: "test", }, sess: helper.sessions[0], } if helper.topic.lastID != 0 { t.Errorf("Topic.lastID: expected 0, found %d", helper.topic.lastID) } helper.topic.handleClientMsg(msg) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } if helper.topic.lastID != 0 { t.Errorf("Topic.lastID: expected to remain 0, found %d", helper.topic.lastID) } // Message uid1 -> uid2. if len(helper.results[0].messages) == 1 { em := helper.results[0].messages[0].(*ServerComMessage) if em.Ctrl == nil { t.Fatal("User 1 is expected to receive a ctrl message") } if em.Ctrl.Code < 500 || em.Ctrl.Code >= 600 { t.Errorf("User1: expected ctrl.code 5xx, received %d", em.Ctrl.Code) } } else { t.Errorf("User 1 is expected to receive one message vs %d received.", len(helper.results[0].messages)) } if len(helper.results[1].messages) != 0 { t.Errorf("User 2 is not expected to receive any messages, %d received.", len(helper.results[1].messages)) } // Checking presence messages routed through hubhelper. if len(helper.hubMessages) != 0 { t.Errorf("Hubhelper.route did not expect any messages, however %d received.", len(helper.hubMessages)) } } func TestHandleBroadcastDataInactiveTopic(t *testing.T) { numUsers := 2 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatP2P, "p2p-test", true) defer helper.tearDown() // Make test message. from := helper.uids[0].UserId() msg := &ClientComMessage{ AsUser: from, Pub: &MsgClientPub{ Topic: "p2p", Content: "test", }, sess: helper.sessions[0], } // Deactivate topic. helper.topic.markDeleted() helper.topic.handleClientMsg(msg) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } // Message uid1 -> uid2. if len(helper.results[0].messages) == 1 { em := helper.results[0].messages[0].(*ServerComMessage) if em.Ctrl == nil { t.Fatal("User 1 is expected to receive a ctrl message") } if em.Ctrl.Code < 500 || em.Ctrl.Code >= 600 { t.Errorf("User1: expected ctrl.code 5xx, received %d", em.Ctrl.Code) } } else { t.Errorf("User 1 is expected to receive one message vs %d received.", len(helper.results[0].messages)) } if len(helper.results[1].messages) != 0 { t.Errorf("User 2 is not expected to receive any messages, %d received.", len(helper.results[1].messages)) } // Checking presence messages routed through hubhelper. if len(helper.hubMessages) != 0 { t.Errorf("Hubhelper.route did not expect any messages, however %d received.", len(helper.hubMessages)) } } func TestHandleBroadcastInfoP2P(t *testing.T) { topicName := "usrP2P" numUsers := 2 readId := 8 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatP2P, topicName, true) defer helper.tearDown() // Pretend we have 10 messages. helper.topic.lastID = 10 // uid1 notifies uid2 that uid1 has read messages up to seqid 8. from := helper.uids[0] to := helper.uids[1] helper.ss.EXPECT().Update(topicName, from, map[string]any{"ReadSeqId": readId}).Return(nil) msg := &ClientComMessage{ AsUser: from.UserId(), Note: &MsgClientNote{ Topic: to.UserId(), What: "read", SeqId: readId, }, sess: helper.sessions[0], } helper.topic.handleClientMsg(msg) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } // Topic metadata. if actualReadId := helper.topic.perUser[from].readID; actualReadId != readId { t.Errorf("perUser[%s].readID: expected %d, found %d.", from.UserId(), readId, actualReadId) } // Server messages. if len(helper.results[0].messages) != 0 { t.Errorf("Session 0 isn't expected to receive any messages. Received %d", len(helper.results[0].messages)) } if len(helper.results[1].messages) != 1 { t.Fatalf("Session 1 is expected to receive exactly 1 message. Received %d", len(helper.results[1].messages)) } res := helper.results[1].messages[0].(*ServerComMessage) if res.Info != nil { info := res.Info // Topic name will be fixed (to -> from). if info.Topic != from.UserId() { t.Errorf("Info.Topic: expected '%s', found '%s'", to.UserId(), info.Topic) } if info.From != from.UserId() { t.Errorf("Info.From: expected '%s', found '%s'", from.UserId(), info.From) } if info.What != "read" { t.Errorf("Info.What: expected 'read', found '%s'", info.What) } if info.SeqId != readId { t.Errorf("Info.SeqId: expected %d, found %d", readId, info.SeqId) } } else { t.Error("Session message is expected to contain `info` section.") } // Checking presence messages routed through hub helper. These are intended for offline sessions. if len(helper.hubMessages) != 2 { t.Fatalf("Hubhelper.route expected exactly two recipients routed via hubhelper. Found %d", len(helper.hubMessages)) } for i, uid := range helper.uids { if routedMsgs, ok := helper.hubMessages[uid.UserId()]; ok { expectedSrc := helper.uids[i^1].UserId() for _, s := range routedMsgs { if s.Info != nil { // Info messages for offline sessions. info := s.Info if info.Topic != "me" { t.Errorf("Uid %s: info.topic is expected to be 'me', got %s", uid.UserId(), info.Topic) } if info.Src != expectedSrc { t.Errorf("Uid %s: info.src expected: %s, found: %s", uid.UserId(), expectedSrc, info.Src) } if info.What != "read" { t.Error("info.what expected to be 'read'") } if info.SeqId != readId { t.Errorf("info.seq: expected %d, found %d", readId, info.SeqId) } } else if s.Pres != nil { // Pres messages for offline sessions. pres := s.Pres if pres.Topic != "me" { t.Errorf("Uid %s: pres.topic is expected to be 'me', got %s", uid.UserId(), pres.Topic) } if pres.What != "read" { t.Error("pres.what expected to be 'read'") } if pres.Src != expectedSrc { t.Errorf("Uid %s: pres.src expected: %s, found: %s", uid.UserId(), expectedSrc, pres.Src) } if pres.SeqId != readId { t.Errorf("pres.seq: expected %d, found %d", readId, pres.SeqId) } } else { t.Error("Hub messages must be either `info` or `pres`.") } } } else { t.Errorf("Uid %s: no hub results found.", uid.UserId()) } } } func TestHandleBroadcastInfoBogusNotification(t *testing.T) { topicName := "usrP2P" numUsers := 2 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatP2P, topicName, true) defer helper.tearDown() // Pretend we have 10 messages. helper.topic.lastID = 10 // uid1 notifies uid2 that uid1 has read messages up to seqid 11. readId := 11 from := helper.uids[0] to := helper.uids[1] msg := &ClientComMessage{ AsUser: from.UserId(), Original: to.UserId(), Note: &MsgClientNote{ Topic: to.UserId(), What: "read", SeqId: readId, }, sess: helper.sessions[0], } helper.topic.handleClientMsg(msg) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } // Read id should not be updated. if actualReadId := helper.topic.perUser[from].readID; actualReadId != 0 { t.Errorf("perUser[%s].readID: expected 0, found %d.", from.UserId(), actualReadId) } // Server messages. for i, r := range helper.results { if numMessages := len(r.messages); numMessages != 0 { t.Errorf("User %d is not expected to receive any messages, %d received.", i, numMessages) } } // Nothing should be routed through the hub. if len(helper.hubMessages) != 0 { t.Errorf("Hubhelper.route did not expect any messages, however %d received.", len(helper.hubMessages)) } } func TestHandleBroadcastInfoFilterOutRecvWithoutRPermission(t *testing.T) { topicName := "usrP2P" numUsers := 2 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatP2P, topicName, true) defer helper.tearDown() // Pretend we have 10 messages. helper.topic.lastID = 10 // uid1 notifies uid2 that uid1 has read messages up to seqid 11. readId := 8 from := helper.uids[0] to := helper.uids[1] // Revoke R permission from the sender. pud := helper.topic.perUser[from] pud.modeGiven = types.ModeWrite | types.ModeJoin helper.topic.perUser[from] = pud msg := &ClientComMessage{ AsUser: from.UserId(), Original: to.UserId(), Note: &MsgClientNote{ Topic: to.UserId(), What: "recv", SeqId: readId, }, sess: helper.sessions[0], } helper.topic.handleClientMsg(msg) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } // Read id should not be updated. if actualReadId := helper.topic.perUser[from].readID; actualReadId != 0 { t.Errorf("perUser[%s].readID: expected 0, found %d.", from.UserId(), actualReadId) } // Server messages. for i, r := range helper.results { if numMessages := len(r.messages); numMessages != 0 { t.Errorf("User %d is not expected to receive any messages, %d received.", i, numMessages) } } // Nothing should be routed through the hub. if len(helper.hubMessages) != 0 { t.Errorf("Hubhelper.route did not expect any messages, however %d received.", len(helper.hubMessages)) } } func TestHandleBroadcastInfoFilterOutKpWithoutWPermission(t *testing.T) { topicName := "usrP2P" numUsers := 2 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatP2P, topicName, true) defer helper.tearDown() // Pretend we have 10 messages. helper.topic.lastID = 10 // uid1 notifies uid2 that uid1 has read messages up to seqid 11. readId := 8 from := helper.uids[0] to := helper.uids[1] // Revoke W permission from the sender. pud := helper.topic.perUser[from] pud.modeGiven = types.ModeRead | types.ModeJoin helper.topic.perUser[from] = pud msg := &ClientComMessage{ AsUser: from.UserId(), Original: to.UserId(), Note: &MsgClientNote{ Topic: to.UserId(), What: "kp", SeqId: readId, }, sess: helper.sessions[0], } helper.topic.handleClientMsg(msg) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } // Read id should not be updated. if actualReadId := helper.topic.perUser[from].readID; actualReadId != 0 { t.Errorf("perUser[%s].readID: expected 0, found %d.", from.UserId(), actualReadId) } // Server messages. for i, r := range helper.results { if numMessages := len(r.messages); numMessages != 0 { t.Errorf("User %d is not expected to receive any messages, %d received.", i, numMessages) } } // Nothing should be routed through the hub. if len(helper.hubMessages) != 0 { t.Errorf("Hubhelper.route did not expect any messages, however %d received.", len(helper.hubMessages)) } } func TestHandleBroadcastInfoDuplicatedRead(t *testing.T) { topicName := "usrP2P" numUsers := 2 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatP2P, topicName /*attach=*/, true) defer helper.tearDown() // Pretend we have 10 messages. helper.topic.lastID = 10 // uid1 notifies uid2 that uid1 has read messages up to seqid 11. readId := 8 from := helper.uids[0] to := helper.uids[1] // Revoke R permission from the sender. pud := helper.topic.perUser[from] pud.readID = 8 helper.topic.perUser[from] = pud msg := &ClientComMessage{ AsUser: from.UserId(), Original: to.UserId(), Note: &MsgClientNote{ Topic: to.UserId(), What: "read", SeqId: readId, }, sess: helper.sessions[0], } helper.topic.handleClientMsg(msg) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } // Read id should not be updated. if actualReadId := helper.topic.perUser[from].readID; actualReadId != 8 { t.Errorf("perUser[%s].readID: expected 8, found %d.", from.UserId(), actualReadId) } // Server messages. for i, r := range helper.results { if numMessages := len(r.messages); numMessages != 0 { t.Errorf("User %d is not expected to receive any messages, %d received.", i, numMessages) } } // Nothing should be routed through the hub. if len(helper.hubMessages) != 0 { t.Errorf("Hubhelper.route did not expect any messages, however %d received.", len(helper.hubMessages)) } } func TestHandleBroadcastInfoDbError(t *testing.T) { topicName := "usrP2P" numUsers := 2 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatP2P, topicName, true) defer helper.tearDown() // Pretend we have 10 messages. helper.topic.lastID = 10 // uid1 notifies uid2 that uid1 has read messages up to seqid 11. readId := 8 from := helper.uids[0] to := helper.uids[1] helper.ss.EXPECT().Update(topicName, from, map[string]any{"ReadSeqId": readId}).Return(types.ErrInternal) msg := &ClientComMessage{ AsUser: from.UserId(), Original: to.UserId(), Note: &MsgClientNote{ Topic: to.UserId(), What: "read", SeqId: readId, }, sess: helper.sessions[0], } helper.topic.handleClientMsg(msg) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } // Read id should not be updated. if actualReadId := helper.topic.perUser[from].readID; actualReadId != 0 { t.Errorf("perUser[%s].readID: expected 0, found %d.", from.UserId(), actualReadId) } // Server messages. for i, r := range helper.results { if numMessages := len(r.messages); numMessages != 0 { t.Errorf("User %d is not expected to receive any messages, %d received.", i, numMessages) } } // Nothing should be routed through the hub. if len(helper.hubMessages) != 0 { t.Errorf("Hubhelper.route did not expect any messages, however %d received.", len(helper.hubMessages)) } } func TestHandleBroadcastInfoInvalidChannelAccess(t *testing.T) { topicName := "grpTest" chanName := "chnTest" numUsers := 3 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatGrp, topicName, true) // This is not a channel. However, we will try to handle an info message where // the topic is referenced as "chn". helper.topic.isChan = false defer helper.tearDown() // Pretend we have 10 messages. helper.topic.lastID = 10 // uid1 notifies uid2 that uid1 has read messages up to seqid 11. readId := 8 from := helper.uids[0] for i := 1; i < numUsers; i++ { uid := helper.uids[i] pud := helper.topic.perUser[uid] pud.modeGiven = types.ModeCChnReader helper.topic.perUser[uid] = pud } msg := &ClientComMessage{ Original: chanName, AsUser: from.UserId(), Note: &MsgClientNote{ Topic: chanName, What: "read", SeqId: readId, }, sess: helper.sessions[0], } helper.topic.handleClientMsg(msg) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } // Read id should not be updated. if actualReadId := helper.topic.perUser[from].readID; actualReadId != 0 { t.Errorf("perUser[%s].readID: expected 0, found %d.", from.UserId(), actualReadId) } // Server messages. for i, r := range helper.results { if numMessages := len(r.messages); numMessages != 0 { t.Errorf("User %d is not expected to receive any messages, %d received.", i, numMessages) } } // Nothing should be routed through the hub. if len(helper.hubMessages) != 0 { t.Errorf("Hubhelper.route did not expect any messages, however %d received.", len(helper.hubMessages)) } } func TestHandleBroadcastInfoChannelProcessing(t *testing.T) { topicName := "grpTest" chanName := "chnTest" numUsers := 3 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatGrp, topicName, true) helper.topic.isChan = true defer helper.tearDown() // Pretend we have 10 messages. helper.topic.lastID = 10 // uid1 notifies uid2 that uid1 has read messages up to seqid 11. readId := 8 from := helper.uids[0] for i := 1; i < numUsers; i++ { uid := helper.uids[i] pud := helper.topic.perUser[uid] pud.modeGiven = types.ModeCChnReader pud.isChan = true helper.topic.perUser[uid] = pud } helper.ss.EXPECT().Update(chanName, from, map[string]any{"ReadSeqId": readId}).Return(nil) msg := &ClientComMessage{ AsUser: from.UserId(), Original: chanName, Note: &MsgClientNote{ Topic: chanName, What: "read", SeqId: readId, }, sess: helper.sessions[0], } helper.topic.handleClientMsg(msg) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } // Topic metadata. // We do not update read ids for channel topics. if actualReadId := helper.topic.perUser[from].readID; actualReadId != 0 { t.Errorf("perUser[%s].readID: expected 0, found %d.", from.UserId(), actualReadId) } // Server messages. Note messages aren't forwarded by channel topics. for i, r := range helper.results { if numMessages := len(r.messages); numMessages != 0 { t.Errorf("User %d is not expected to receive any messages, %d received.", i, numMessages) } } // Send a pres back to the sender. if len(helper.hubMessages) != 1 { t.Fatalf("Hubhelper.route did not expect any messages, however %d received.", len(helper.hubMessages)) } if mm, ok := helper.hubMessages[from.UserId()]; ok || len(mm) != 1 { s := mm[0] if s.Pres != nil { p := s.Pres if p.Topic != "me" { t.Errorf("Uid %s: pres notify on topic is expected to be 'me', got %s", from.UserId(), p.Topic) } if p.SkipTopic != topicName { t.Errorf("Uid %s: pres skip topic is expected to be '%s', got %s", from.UserId(), topicName, p.SkipTopic) } if p.Src != topicName { t.Errorf("Uid %s: pres.src expected: %s, found: %s", from.UserId(), topicName, p.Src) } if p.What != "read" { t.Errorf("Uid %s: pres.what expected: 'read', found: %s", from.UserId(), p.What) } } else { t.Errorf("Uid %s: hub message expected to be {pres}.", from.UserId()) } } else { t.Errorf("Uid %s: expected 1 hub message, got %d.", from.UserId(), len(mm)) } } func TestHandleBroadcastPresMe(t *testing.T) { topicName := "usrMe" numUsers := 1 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatMe, topicName, true) defer helper.tearDown() uid := helper.uids[0] srcUid := types.Uid(10) helper.topic.perSubs = make(map[string]perSubsData) helper.topic.perSubs[srcUid.UserId()] = perSubsData{enabled: true, online: false} msg := &ServerComMessage{ AsUser: uid.UserId(), RcptTo: uid.UserId(), Pres: &MsgServerPres{ Topic: "me", Src: srcUid.UserId(), What: "on", }, } helper.topic.handleServerMsg(msg) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } // Topic metadata. if online := helper.topic.perSubs[srcUid.UserId()].online; !online { t.Errorf("User %s is expected to be online.", srcUid.UserId()) } // Server messages. if len(helper.results[0].messages) != 1 { t.Fatalf("Session 0 is expected to receive one message. Received %d.", len(helper.results[0].messages)) } s := helper.results[0].messages[0].(*ServerComMessage) if s.RcptTo != uid.UserId() { t.Errorf("Message.RcptTo: expected '%s', found '%s'", uid.UserId(), s.RcptTo) } if s.Pres != nil { pres := s.Pres if pres.Topic != "me" { t.Errorf("Expected to notify user on 'me' topic. Found: '%s'", pres.Topic) } if pres.Src != srcUid.UserId() { t.Errorf("Expected notification from '%s'. Found: '%s'", srcUid.UserId(), pres.Topic) } if pres.What != "on" { t.Errorf("Expected an online notification. Found: '%s'", pres.What) } } else { t.Error("Message is expected to be pres.") } if len(helper.hubMessages) != 0 { t.Errorf("Hubhelper.route isn't expected to receive messages. Received %d", len(helper.hubMessages)) } } func TestHandleBroadcastPresInactiveTopic(t *testing.T) { topicName := "usrMe" numUsers := 1 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatMe, topicName, true) defer helper.tearDown() uid := helper.uids[0] srcUid := types.Uid(10) helper.topic.perSubs = make(map[string]perSubsData) helper.topic.perSubs[srcUid.UserId()] = perSubsData{enabled: true, online: false} msg := &ServerComMessage{ AsUser: uid.UserId(), RcptTo: uid.UserId(), Pres: &MsgServerPres{ Topic: "me", Src: srcUid.UserId(), What: "on", }, } // Deactivate topic. helper.topic.markDeleted() helper.topic.handleServerMsg(msg) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } // Topic metadata. if online := helper.topic.perSubs[srcUid.UserId()].online; online { t.Errorf("User %s is expected to be offline.", srcUid.UserId()) } // Server messages. if len(helper.results[0].messages) != 0 { t.Fatalf("Session 0 is not expected to receive messages. Received %d.", len(helper.results[0].messages)) } if len(helper.hubMessages) != 0 { t.Errorf("Hubhelper.route isn't expected to receive messages. Received %d", len(helper.hubMessages)) } } const ( NoSub = 0 ExistingSubEnabled = 1 ExistingSubDisabled = 2 ) func NoChangeInStatusTest(t *testing.T, subscriptionStatus int, what string) *TopicTestHelper { t.Helper() topicName := "usrMe" numUsers := 1 helper := &TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatMe, topicName, true) uid := helper.uids[0] srcUid := types.Uid(10) helper.topic.perSubs = make(map[string]perSubsData) enabled := false switch subscriptionStatus { case NoSub: case ExistingSubEnabled: enabled = true fallthrough case ExistingSubDisabled: helper.topic.perSubs[srcUid.UserId()] = perSubsData{enabled: enabled, online: false} } msg := &ServerComMessage{ AsUser: uid.UserId(), RcptTo: uid.UserId(), Pres: &MsgServerPres{ Topic: "me", Src: srcUid.UserId(), // No change in online status. What: what, }, } helper.topic.handleServerMsg(msg) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } // Topic metadata. if online := helper.topic.perSubs[srcUid.UserId()].online; online { t.Errorf("User %s is expected to be offline.", srcUid.UserId()) } // Server messages. if len(helper.results[0].messages) != 0 { t.Fatalf("Session 0 is not expected to receive messages. Received %d.", len(helper.results[0].messages)) } if len(helper.hubMessages) != 0 { t.Errorf("Hubhelper.route isn't expected to receive messages. Received %d", len(helper.hubMessages)) } return helper } func TestHandleBroadcastPresUnkn(t *testing.T) { NoChangeInStatusTest(t, ExistingSubEnabled, "?unkn").tearDown() } func TestHandleBroadcastPresNone(t *testing.T) { NoChangeInStatusTest(t, ExistingSubEnabled, "?none").tearDown() } func TestHandleBroadcastPresRedundantUpdate(t *testing.T) { h := NoChangeInStatusTest(t, ExistingSubDisabled, "off+rem") uid := h.uids[0] if _, ok := h.topic.perSubs[uid.UserId()]; ok { t.Errorf("Subscription for user %s expected to be deleted.", uid.UserId()) } h.tearDown() } func TestHandleBroadcastPresNewSub(t *testing.T) { NoChangeInStatusTest(t, NoSub, "off+wrong").tearDown() } func TestHandleBroadcastPresUnknownSub(t *testing.T) { NoChangeInStatusTest(t, NoSub, "on+rem").tearDown() } func TestReplyGetDescInvalidOpts(t *testing.T) { numUsers := 1 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatMe, "" /*attach=*/, true) defer helper.tearDown() msg := ClientComMessage{ Original: "dummy", } // Can't specify User in opts. if err := helper.topic.replyGetDesc(helper.sessions[0], 123, false, &MsgGetOpts{User: "abcdef"}, &msg); err == nil { t.Error("replyGetDesc expected to error out.") } else if err.Error() != "invalid GetDesc query" { t.Errorf("Unexpected error: expected 'invalid GetDesc query', got '%s'", err.Error()) } helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } if len(helper.results[0].messages) != 1 { t.Fatalf("`responses` expected to contain 1 element, found %d", len(helper.results[0].messages)) } resp := helper.results[0].messages[0].(*ServerComMessage) if resp.Ctrl == nil { t.Fatalf("response expected to contain a Ctrl message") } if resp.Ctrl.Code != 400 { t.Errorf("response code: expected 400, found: %d", resp.Ctrl.Code) } // Presence notifications. if len(helper.hubMessages) != 0 { t.Errorf("Hub isn't expected to receive any messages, received %d", len(helper.hubMessages)) } } // Verifies ctrl codes in session outputs. func registerSessionVerifyOutputs(t *testing.T, sessionOutput *responses, expectedCtrlCodes []int) { t.Helper() // Session output. if len(sessionOutput.messages) == len(expectedCtrlCodes) { n := len(expectedCtrlCodes) for i := range n { resp := sessionOutput.messages[i].(*ServerComMessage) code := expectedCtrlCodes[i] if resp.Ctrl != nil { if resp.Ctrl.Code != code { t.Errorf("response code: expected %d, found: %d", code, resp.Ctrl.Code) } } else { t.Errorf("response %d: expected to contain a Ctrl message", i) } } } else { t.Errorf("Session output: expected %d responses, received %d", len(expectedCtrlCodes), len(sessionOutput.messages)) } } func TestRegisterSessionMe(t *testing.T) { topicName := "usrMe" numUsers := 1 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatMe, topicName, false) defer helper.tearDown() if len(helper.topic.sessions) != 0 { helper.finish() t.Fatalf("Initially attached sessions: expected 0 vs found %d", len(helper.topic.sessions)) } uid := helper.uids[0] // Add a couple more sessions. for i := 1; i < 3; i++ { s, r := helper.newSession(fmt.Sprintf("sid%d", i), uid) helper.sessions = append(helper.sessions, s) helper.results = append(helper.results, r) } for i, s := range helper.sessions { join := &ClientComMessage{ Sub: &MsgClientSub{ Id: fmt.Sprintf("id456-%d", i), Topic: "me", }, AsUser: uid.UserId(), sess: s, } helper.topic.registerSession(join) } helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } if len(helper.topic.sessions) != 3 { t.Errorf("Attached sessions: expected 3, found %d", len(helper.topic.sessions)) } for _, s := range helper.sessions { if len(s.subs) != 1 { t.Errorf("Session subscriptions: expected 3, found %d", len(s.subs)) } } online := helper.topic.perUser[uid].online if online != 3 { t.Errorf("Number of online sessions: expected 3, found %d", online) } // Session output. for _, r := range helper.results { registerSessionVerifyOutputs(t, r, []int{http.StatusOK}) } // Presence notifications. if len(helper.hubMessages) != 0 { t.Errorf("Hub isn't expected to receive any messages, received %d", len(helper.hubMessages)) } } func TestRegisterSessionInactiveTopic(t *testing.T) { topicName := "usrMe" numUsers := 1 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatMe, topicName, false) defer helper.tearDown() if len(helper.topic.sessions) != 0 { helper.finish() t.Fatalf("Initially attached sessions: expected 0 vs found %d", len(helper.topic.sessions)) } uid := helper.uids[0] s := helper.sessions[0] join := &ClientComMessage{ Sub: &MsgClientSub{ Id: "id456", Topic: "me", }, AsUser: uid.UserId(), sess: s, } // Deactivate topic. helper.topic.markDeleted() helper.topic.registerSession(join) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } if len(s.subs) != 0 { t.Errorf("Session subscriptions: expected 0, found %d", len(s.subs)) } online := helper.topic.perUser[uid].online if online != 0 { t.Errorf("Number of online sessions: expected 0, found %d", online) } // Session output. registerSessionVerifyOutputs(t, helper.results[0], []int{http.StatusServiceUnavailable}) // Presence notifications. if len(helper.hubMessages) != 0 { t.Errorf("Hub isn't expected to receive any messages, received %d", len(helper.hubMessages)) } } func TestRegisterSessionUserSpecifiedInSetMessage(t *testing.T) { topicName := "grpTest" numUsers := 1 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatGrp, topicName, false) defer helper.tearDown() if len(helper.topic.sessions) != 0 { helper.finish() t.Fatalf("Initially attached sessions: expected 0 vs found %d", len(helper.topic.sessions)) } uid := helper.uids[0] s := helper.sessions[0] join := &ClientComMessage{ Original: topicName, Sub: &MsgClientSub{ Id: "id456", Topic: topicName, Set: &MsgSetQuery{ Sub: &MsgSetSub{ // Specify the user. This should result in an error. User: "foo", }, }, }, AsUser: uid.UserId(), sess: s, } helper.topic.registerSession(join) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } if len(s.subs) != 0 { t.Errorf("Session subscriptions: expected 0, found %d", len(s.subs)) } online := helper.topic.perUser[uid].online if online != 0 { t.Errorf("Number of online sessions: expected 0, found %d", online) } // Session output. registerSessionVerifyOutputs(t, helper.results[0], []int{http.StatusBadRequest}) // Presence notifications. if len(helper.hubMessages) != 0 { t.Errorf("Hub isn't expected to receive any messages, received %d", len(helper.hubMessages)) } } func TestRegisterSessionInvalidWantStrInSetMessage(t *testing.T) { topicName := "grpTest" numUsers := 1 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatGrp, topicName, false) defer helper.tearDown() if len(helper.topic.sessions) != 0 { helper.finish() t.Fatalf("Initially attached sessions: expected 0 vs found %d", len(helper.topic.sessions)) } uid := helper.uids[0] s := helper.sessions[0] join := &ClientComMessage{ Original: topicName, Sub: &MsgClientSub{ Id: "id456", Topic: topicName, Set: &MsgSetQuery{ Sub: &MsgSetSub{ // Specify the user. This should result in an error. Mode: "Invalid mode string", }, }, }, AsUser: uid.UserId(), sess: s, } helper.topic.registerSession(join) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } if len(s.subs) != 0 { t.Errorf("Session subscriptions: expected 0, found %d", len(s.subs)) } online := helper.topic.perUser[uid].online if online != 0 { t.Errorf("Number of online sessions: expected 0, found %d", online) } // Session output. registerSessionVerifyOutputs(t, helper.results[0], []int{http.StatusBadRequest}) // Presence notifications. if len(helper.hubMessages) != 0 { t.Errorf("Hub isn't expected to receive any messages, received %d", len(helper.hubMessages)) } } func TestRegisterSessionMaxSubscriberCountExceeded(t *testing.T) { topicName := "grpTest" // Pretend we already exceeded the maximum user count. This should produce an error. numUsers := 10 oldMaxSubscribers := globals.maxSubscriberCount globals.maxSubscriberCount = 10 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatGrp, topicName, false) defer func() { helper.tearDown() globals.maxSubscriberCount = oldMaxSubscribers }() if len(helper.topic.sessions) != 0 { helper.finish() t.Fatalf("Initially attached sessions: expected 0 vs found %d", len(helper.topic.sessions)) } // New uid. This should attempt to add a new subscription. uid := types.Uid(10001) s, r := helper.newSession("test-sid", uid) helper.sessions = append(helper.sessions, s) helper.results = append(helper.results, r) join := &ClientComMessage{ Original: topicName, Sub: &MsgClientSub{ Id: "id456", Topic: topicName, }, AsUser: uid.UserId(), sess: s, } helper.topic.registerSession(join) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } if len(s.subs) != 0 { t.Errorf("Session subscriptions: expected 0, found %d", len(s.subs)) } online := helper.topic.perUser[uid].online if online != 0 { t.Errorf("Number of online sessions: expected 0, found %d", online) } // Session output. registerSessionVerifyOutputs(t, r, []int{http.StatusUnprocessableEntity}) // Presence notifications. if len(helper.hubMessages) != 0 { t.Errorf("Hub isn't expected to receive any messages, received %d", len(helper.hubMessages)) } } func TestRegisterSessionLowAuthLevelWithSysTopic(t *testing.T) { topicName := "sys" // No one is subscribed to sys. numUsers := 0 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatSys, topicName, false) defer helper.tearDown() if len(helper.topic.sessions) != 0 { helper.finish() t.Fatalf("Initially attached sessions: expected 0 vs found %d", len(helper.topic.sessions)) } // New uid. This should attempt to add a new subscription // which produces an error b/c authLevel isn't root. uid := types.Uid(10001) s, r := helper.newSession("test-sid", uid) helper.sessions = append(helper.sessions, s) helper.results = append(helper.results, r) join := &ClientComMessage{ Original: topicName, Sub: &MsgClientSub{ Id: "id456", Topic: topicName, }, AsUser: uid.UserId(), sess: s, } helper.topic.registerSession(join) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } if len(s.subs) != 0 { t.Errorf("Session subscriptions: expected 0, found %d", len(s.subs)) } online := helper.topic.perUser[uid].online if online != 0 { t.Errorf("Number of online sessions: expected 0, found %d", online) } // Session output. registerSessionVerifyOutputs(t, r, []int{http.StatusForbidden}) // Presence notifications. if len(helper.hubMessages) != 0 { t.Errorf("Hub isn't expected to receive any messages, received %d", len(helper.hubMessages)) } } func TestRegisterSessionNewChannelGetSubDbError(t *testing.T) { topicName := "grpTest" chanName := "chnTest" numUsers := 1 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatGrp, topicName, false) // It is a channel. helper.topic.isChan = true defer helper.tearDown() if len(helper.topic.sessions) != 0 { helper.finish() t.Fatalf("Initially attached sessions: expected 0 vs found %d", len(helper.topic.sessions)) } // New uid. This should attempt to add a new subscription // which produces an error b/c authLevel isn't root. uid := types.Uid(10001) s, r := helper.newSession("test-sid", uid) helper.sessions = append(helper.sessions, s) helper.results = append(helper.results, r) join := &ClientComMessage{ Original: chanName, Sub: &MsgClientSub{ Id: "id456", Topic: chanName, }, AsUser: uid.UserId(), sess: s, } helper.ss.EXPECT().Get(chanName, uid, false).Return(nil, types.ErrInternal) helper.topic.registerSession(join) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } if len(s.subs) != 0 { t.Errorf("Session subscriptions: expected 0, found %d", len(s.subs)) } online := helper.topic.perUser[uid].online if online != 0 { t.Errorf("Number of online sessions: expected 0, found %d", online) } // Session output. registerSessionVerifyOutputs(t, r, []int{http.StatusInternalServerError}) // Presence notifications. if len(helper.hubMessages) != 0 { t.Errorf("Hub isn't expected to receive any messages, received %d", len(helper.hubMessages)) } } func TestRegisterSessionCreateSubFailed(t *testing.T) { topicName := "grpTest" numUsers := 1 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatGrp, topicName, false) defer helper.tearDown() if len(helper.topic.sessions) != 0 { helper.finish() t.Fatalf("Initially attached sessions: expected 0 vs found %d", len(helper.topic.sessions)) } // New uid. This should attempt to add a new subscription // which produces an error b/c authLevel isn't root. uid := types.Uid(10001) s, r := helper.newSession("test-sid", uid) helper.sessions = append(helper.sessions, s) helper.results = append(helper.results, r) join := &ClientComMessage{ Original: topicName, Sub: &MsgClientSub{ Id: "id456", Topic: topicName, }, AsUser: uid.UserId(), AuthLvl: int(auth.LevelAuth), sess: s, } helper.ss.EXPECT().Get(topicName, uid, true).Return(nil, types.ErrInternal) helper.topic.registerSession(join) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } if len(s.subs) != 0 { t.Errorf("Session subscriptions: expected 0, found %d", len(s.subs)) } online := helper.topic.perUser[uid].online if online != 0 { t.Errorf("Number of online sessions: expected 0, found %d", online) } // Session output. registerSessionVerifyOutputs(t, r, []int{http.StatusInternalServerError}) // Presence notifications. if len(helper.hubMessages) != 0 { t.Errorf("Hub isn't expected to receive any messages, received %d", len(helper.hubMessages)) } } func TestRegisterSessionAsChanUserNotChanSubcriber(t *testing.T) { topicName := "grpTest" chanName := "chnTest" numUsers := 1 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatGrp, topicName, false) // The topic is a channel. helper.topic.isChan = true defer helper.tearDown() if len(helper.topic.sessions) != 0 { helper.finish() t.Fatalf("Initially attached sessions: expected 0 vs found %d", len(helper.topic.sessions)) } s := helper.sessions[0] uid := helper.uids[0] r := helper.results[0] // User is not a channel subscriber (userData.isChan is false). join := &ClientComMessage{ Original: chanName, Sub: &MsgClientSub{ Id: "id456", Topic: chanName, }, AsUser: uid.UserId(), AuthLvl: int(auth.LevelAuth), sess: s, } helper.topic.registerSession(join) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } if len(s.subs) != 0 { t.Errorf("Session subscriptions: expected 0, found %d", len(s.subs)) } online := helper.topic.perUser[uid].online if online != 0 { t.Errorf("Number of online sessions: expected 0, found %d", online) } // Session output. Tell the subscriber to use non-channel name. registerSessionVerifyOutputs(t, r, []int{http.StatusSeeOther}) // Presence notifications. if len(helper.hubMessages) != 0 { t.Errorf("Hub isn't expected to receive any messages, received %d", len(helper.hubMessages)) } } func TestRegisterSessionOwnerBansHimself(t *testing.T) { topicName := "grpTest" numUsers := 1 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatGrp, topicName, false) defer helper.tearDown() if len(helper.topic.sessions) != 0 { helper.finish() t.Fatalf("Initially attached sessions: expected 0 vs found %d", len(helper.topic.sessions)) } s := helper.sessions[0] uid := helper.uids[0] r := helper.results[0] // User is the topic owner. helper.topic.owner = uid pud := helper.topic.perUser[uid] pud.modeGiven |= types.ModeOwner helper.topic.perUser[uid] = pud join := &ClientComMessage{ Original: topicName, Sub: &MsgClientSub{ Id: "id456", Topic: topicName, Set: &MsgSetQuery{ Sub: &MsgSetSub{ // No O permission. Mode: "JPRW", }, }, }, AsUser: uid.UserId(), AuthLvl: int(auth.LevelAuth), sess: s, } helper.topic.registerSession(join) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } if len(s.subs) != 0 { t.Errorf("Session subscriptions: expected 0, found %d", len(s.subs)) } online := helper.topic.perUser[uid].online if online != 0 { t.Errorf("Number of online sessions: expected 0, found %d", online) } // Session output. registerSessionVerifyOutputs(t, r, []int{http.StatusForbidden}) // Presence notifications. if len(helper.hubMessages) != 0 { t.Errorf("Hub isn't expected to receive any messages, received %d", len(helper.hubMessages)) } } func TestRegisterSessionInvalidOwnershipTransfer(t *testing.T) { topicName := "grpTest" numUsers := 2 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatGrp, topicName, false) defer helper.tearDown() if len(helper.topic.sessions) != 0 { helper.finish() t.Fatalf("Initially attached sessions: expected 0 vs found %d", len(helper.topic.sessions)) } s := helper.sessions[1] uid := helper.uids[1] r := helper.results[1] // User is the topic owner. pud := helper.topic.perUser[uid] pud.modeWant = types.ModeCPublic pud.modeGiven = types.ModeCPublic helper.topic.perUser[uid] = pud join := &ClientComMessage{ Original: topicName, Sub: &MsgClientSub{ Id: "id456", Topic: topicName, Set: &MsgSetQuery{ Sub: &MsgSetSub{ // Want ownership. Mode: "JPRWSO", }, }, }, AsUser: uid.UserId(), AuthLvl: int(auth.LevelAuth), sess: s, } helper.topic.registerSession(join) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } if len(s.subs) != 0 { t.Errorf("Session subscriptions: expected 0, found %d", len(s.subs)) } online := helper.topic.perUser[uid].online if online != 0 { t.Errorf("Number of online sessions: expected 0, found %d", online) } // Session output. registerSessionVerifyOutputs(t, r, []int{http.StatusForbidden}) // Presence notifications. if len(helper.hubMessages) != 0 { t.Errorf("Hub isn't expected to receive any messages, received %d", len(helper.hubMessages)) } } func TestRegisterSessionMetadataUpdateFails(t *testing.T) { topicName := "grpTest" numUsers := 2 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatGrp, topicName, false) defer helper.tearDown() if len(helper.topic.sessions) != 0 { helper.finish() t.Fatalf("Initially attached sessions: expected 0 vs found %d", len(helper.topic.sessions)) } s := helper.sessions[1] uid := helper.uids[1] r := helper.results[1] pud := helper.topic.perUser[uid] pud.modeWant = types.ModeCPublic pud.modeGiven = types.ModeCPublic helper.topic.perUser[uid] = pud // Want ownership. newWant := "JRWP" join := &ClientComMessage{ Original: topicName, Sub: &MsgClientSub{ Id: "id456", Topic: topicName, Set: &MsgSetQuery{ Sub: &MsgSetSub{ Mode: newWant, }, }, }, AsUser: uid.UserId(), AuthLvl: int(auth.LevelAuth), sess: s, } // DB call fails. helper.ss.EXPECT().Update(topicName, uid, gomock.Any()).Return(types.ErrInternal) helper.topic.registerSession(join) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } if len(s.subs) != 0 { t.Errorf("Session subscriptions: expected 0, found %d", len(s.subs)) } online := helper.topic.perUser[uid].online if online != 0 { t.Errorf("Number of online sessions: expected 0, found %d", online) } // Session output. registerSessionVerifyOutputs(t, r, []int{http.StatusInternalServerError}) // Presence notifications. if len(helper.hubMessages) != 0 { t.Errorf("Hub isn't expected to receive any messages, received %d", len(helper.hubMessages)) } } func TestRegisterSessionOwnerChangeDbCallFails(t *testing.T) { topicName := "grpTest" numUsers := 1 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatGrp, topicName, false) defer helper.tearDown() if len(helper.topic.sessions) != 0 { helper.finish() t.Fatalf("Initially attached sessions: expected 0 vs found %d", len(helper.topic.sessions)) } s := helper.sessions[0] uid := helper.uids[0] r := helper.results[0] // User is the topic owner. pud := helper.topic.perUser[uid] pud.modeWant = types.ModeCPublic helper.topic.perUser[uid] = pud // Want ownership. newWant := "JRWPASO" join := &ClientComMessage{ Original: topicName, Sub: &MsgClientSub{ Id: "id456", Topic: topicName, Set: &MsgSetQuery{ Sub: &MsgSetSub{ Mode: newWant, }, }, }, AsUser: uid.UserId(), AuthLvl: int(auth.LevelAuth), sess: s, } helper.ss.EXPECT().Update(topicName, uid, gomock.Any()).Return(nil).Times(2) // OwnerChange call fails. helper.tt.EXPECT().OwnerChange(topicName, uid).Return(types.ErrInternal) helper.topic.registerSession(join) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } if len(s.subs) != 0 { t.Errorf("Session subscriptions: expected 0, found %d", len(s.subs)) } online := helper.topic.perUser[uid].online if online != 0 { t.Errorf("Number of online sessions: expected 0, found %d", online) } registerSessionVerifyOutputs(t, r, []int{}) // Presence notifications. if len(helper.hubMessages) != 0 { t.Errorf("Hub isn't expected to receive any messages, received %d", len(helper.hubMessages)) } } func TestUnregisterSessionSimple(t *testing.T) { topicName := "usrMe" numUsers := 1 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatMe, topicName /*attach=*/, true) defer helper.tearDown() uid := helper.uids[0] helper.uu.EXPECT().UpdateLastSeen(uid, gomock.Any(), gomock.Any()).Return(nil) // Add a couple more sessions. for i := 1; i < 3; i++ { s, r := helper.newSession(fmt.Sprintf("sid%d", i), uid) helper.sessions = append(helper.sessions, s) helper.results = append(helper.results, r) helper.topic.sessions[s] = perSessionData{uid: uid} pu := helper.topic.perUser[uid] pu.online++ helper.topic.perUser[uid] = pu } // Initial online and attach session counts. if len(helper.topic.sessions) != 3 { helper.finish() t.Fatalf("Initially attached sessions: expected 3 vs found %d", len(helper.topic.sessions)) } if online := helper.topic.perUser[uid].online; online != 3 { t.Errorf("Number of online sessions: expected 3 vs found %d", online) } s := helper.sessions[0] r := helper.results[0] leave := &ClientComMessage{ Leave: &MsgClientLeave{ Id: "id456", Topic: topicName, }, AsUser: uid.UserId(), sess: s, init: true, } helper.topic.unregisterSession(leave) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } if len(helper.topic.sessions) != 2 { t.Errorf("Attached sessions: expected 2, found %d", len(helper.topic.sessions)) } if len(s.subs) != 0 { t.Errorf("Session subscriptions: expected 0, found %d", len(helper.sessions[0].subs)) } if online := helper.topic.perUser[uid].online; online != 2 { t.Errorf("Number of online sessions after unregistering: expected 2, found %d", online) } // Session output. registerSessionVerifyOutputs(t, r, []int{http.StatusOK}) // Presence notifications. if len(helper.hubMessages) != 0 { t.Errorf("Hub isn't expected to receive any messages, received %d", len(helper.hubMessages)) } } func TestUnregisterSessionInactiveTopic(t *testing.T) { topicName := "usrMe" numUsers := 1 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatMe, topicName, true) defer helper.tearDown() uid := helper.uids[0] // Initial online and attach session counts. if len(helper.topic.sessions) != 1 { helper.finish() t.Fatalf("Initially attached sessions: expected 1 vs found %d", len(helper.topic.sessions)) } if online := helper.topic.perUser[uid].online; online != 1 { t.Errorf("Number of online sessions: expected 1 vs found %d", online) } s := helper.sessions[0] r := helper.results[0] leave := &ClientComMessage{ Leave: &MsgClientLeave{ Id: "id456", Topic: topicName, }, AsUser: uid.UserId(), sess: s, init: true, } // Deactivate topic. helper.topic.markDeleted() helper.topic.unregisterSession(leave) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } if len(helper.topic.sessions) != 1 { t.Errorf("Attached sessions: expected 1, found %d", len(helper.topic.sessions)) } if len(s.subs) != 0 { t.Errorf("Session subscriptions: expected 0, found %d", len(s.subs)) } if online := helper.topic.perUser[uid].online; online != 1 { t.Errorf("Number of online sessions after unregistering: expected 1, found %d", online) } // Session output. registerSessionVerifyOutputs(t, r, []int{http.StatusServiceUnavailable}) // Presence notifications. if len(helper.hubMessages) != 0 { t.Errorf("Hub isn't expected to receive any messages, received %d", len(helper.hubMessages)) } } func TestUnregisterSessionUnsubscribe(t *testing.T) { topicName := "grpTest" numUsers := 3 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatGrp, topicName, true) defer helper.tearDown() uid := helper.uids[2] helper.ss.EXPECT().Delete(topicName, uid).Return(nil) // Add a couple more sessions. for i := range 2 { s, r := helper.newSession(fmt.Sprintf("sid-uid-%d-%d", uid, i), uid) helper.sessions = append(helper.sessions, s) helper.results = append(helper.results, r) helper.topic.sessions[s] = perSessionData{uid: uid} pu := helper.topic.perUser[uid] pu.online++ helper.topic.perUser[uid] = pu } // Initial online and attach session counts. if len(helper.topic.sessions) != 5 { helper.finish() t.Fatalf("Initially attached sessions: expected 5 vs found %d", len(helper.topic.sessions)) } if online := helper.topic.perUser[uid].online; online != 3 { t.Errorf("Number of online sessions: expected 3 vs found %d", online) } leave := &ClientComMessage{ Leave: &MsgClientLeave{ Id: "id456", Topic: topicName, Unsub: true, }, AsUser: uid.UserId(), sess: helper.sessions[0], init: true, } helper.topic.unregisterSession(leave) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } if len(helper.topic.sessions) != 2 { t.Errorf("Attached sessions: expected 2, found %d", len(helper.topic.sessions)) } if len(helper.sessions[0].subs) != 0 { t.Errorf("Session subscriptions: expected 0, found %d", len(helper.sessions[0].subs)) } if pu, ok := helper.topic.perUser[uid]; pu.online != 0 || ok { t.Errorf("Number of online sessions after unsubscribing: expected 2, found %d; perUser entry found: %t", pu.online, ok) } // Session output. Sessions 2, 3, 4 are the evicted/unsubscribed uid. for i := 2; i < 5; i++ { r := helper.results[i] registerSessionVerifyOutputs(t, r, []int{http.StatusResetContent}) } // Presence notifications. if len(helper.hubMessages) != 2 { t.Errorf("Hub messages recipients: expected 2, received %d", len(helper.hubMessages)) } // Group presSubs. if grpPres, ok := helper.hubMessages[topicName]; ok { if len(grpPres) != 2 { t.Fatalf("Group presence messages: expected 2, got %d", len(grpPres)) } for _, msg := range grpPres { // pres := msg.Pres if pres == nil { t.Fatal("Presence message expected in hub output, but not found.") } if pres.Topic != topicName { t.Errorf("Presence message topic: expected %s, found %s", topicName, pres.Topic) } if pres.Src != uid.UserId() { t.Errorf("Presence message src: expected %s, found %s", uid.UserId(), pres.Src) } if pres.What != "acs" && pres.What != "off" { t.Errorf("Presence message what: expected 'acs' or 'off', found %s", pres.What) } } } else { t.Errorf("Hub expected to pres recipient %s", topicName) } // User notification. if userPres, ok := helper.hubMessages[uid.UserId()]; ok { if len(userPres) != 1 { t.Fatalf("User presence messages: expected 1, got %d", len(userPres)) } pres := userPres[0].Pres if pres == nil { t.Fatal("Presence message expected in hub output, but not found.") } if pres.Topic != "me" { t.Errorf("Presence message topic: expected 'me', found %s", pres.Topic) } if pres.Src != topicName { t.Errorf("Presence message src: expected %s, found %s", topicName, pres.Src) } if pres.What != "gone" { t.Errorf("Presence message what: expected 'gone', found %s", pres.What) } } else { t.Errorf("Hub expected to pres recipient %s", uid.UserId()) } } func TestUnregisterSessionOwnerCannotUnsubscribe(t *testing.T) { topicName := "grpTest" numUsers := 3 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatGrp, topicName, true) defer helper.tearDown() uid := helper.uids[0] s := helper.sessions[0] r := helper.results[0] leave := &ClientComMessage{ Leave: &MsgClientLeave{ Id: "id456", Topic: topicName, Unsub: true, }, AsUser: uid.UserId(), sess: s, init: true, } helper.topic.unregisterSession(leave) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } if len(helper.topic.sessions) != 3 { t.Errorf("Attached sessions: expected 3, found %d", len(helper.topic.sessions)) } if len(s.subs) != 0 { t.Errorf("Session subscriptions: expected 0, found %d", len(helper.sessions[0].subs)) } if online := helper.topic.perUser[uid].online; online != 1 { t.Errorf("Number of online sessions after failed unsubscribing: expected 1, found %d.", online) } // Session output. registerSessionVerifyOutputs(t, r, []int{http.StatusForbidden}) // Presence notifications. if len(helper.hubMessages) != 0 { t.Errorf("Hub messages recipients: expected 0, received %d", len(helper.hubMessages)) } } func TestUnregisterSessionUnsubDeleteCallFails(t *testing.T) { topicName := "grpTest" numUsers := 3 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatGrp, topicName, true) defer helper.tearDown() // Unsubscribe user 1 (cannot unsub user 0, the owner). uid := helper.uids[1] s := helper.sessions[1] r := helper.results[1] leave := &ClientComMessage{ Leave: &MsgClientLeave{ Id: "id456", Topic: topicName, Unsub: true, }, AsUser: uid.UserId(), sess: s, init: true, } // DB call fails. helper.ss.EXPECT().Delete(topicName, uid).Return(types.ErrInternal) helper.topic.unregisterSession(leave) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } if len(helper.topic.sessions) != 3 { t.Errorf("Attached sessions: expected 3, found %d", len(helper.topic.sessions)) } if len(s.subs) != 0 { t.Errorf("Session subscriptions: expected 0, found %d", len(helper.sessions[0].subs)) } if online := helper.topic.perUser[uid].online; online != 1 { t.Errorf("Number of online sessions after failed unsubscribing: expected 1, found %d.", online) } // Session output. registerSessionVerifyOutputs(t, r, []int{http.StatusInternalServerError}) // Presence notifications. if len(helper.hubMessages) != 0 { t.Errorf("Hub messages recipients: expected 0, received %d", len(helper.hubMessages)) } } func TestHandleMetaChanErr(t *testing.T) { topicName := "grpTest" chanName := "chnTest" numUsers := 3 helper := TopicTestHelper{} defer helper.tearDown() helper.setUp(t, numUsers, types.TopicCatGrp, topicName, false) // This is not a channel. However, we will try to handle an info message where // the topic is referenced as "chn". helper.topic.isChan = false // Empty message since this request should trigger an error anyway. meta := &ClientComMessage{ AsUser: helper.uids[0].UserId(), Original: chanName, MetaWhat: constMsgMetaDesc | constMsgMetaSub | constMsgMetaData | constMsgMetaDel, sess: helper.sessions[0], } helper.topic.handleMeta(meta) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } // Session output. registerSessionVerifyOutputs(t, helper.results[0], []int{http.StatusNotFound}) // Presence notifications. if len(helper.hubMessages) != 0 { t.Errorf("Hub messages recipients: expected 0, received %d", len(helper.hubMessages)) } } func TestHandleMetaGet(t *testing.T) { topicName := "usrMe" numUsers := 1 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatMe, topicName, true) defer helper.tearDown() uid := helper.uids[0] helper.mm.EXPECT().GetAll(topicName, uid, gomock.Any()).Return([]types.Message{}, nil) helper.mm.EXPECT().GetDeleted(topicName, uid, gomock.Any()).Return([]types.Range{}, 0, nil) helper.uu.EXPECT().GetTopics(uid, gomock.Any()).Return([]types.Subscription{}, nil) meta := &ClientComMessage{ Get: &MsgClientGet{ Id: "id456", Topic: topicName, MsgGetQuery: MsgGetQuery{ What: "desc sub data del", Desc: &MsgGetOpts{}, Sub: &MsgGetOpts{}, Data: &MsgGetOpts{}, Del: &MsgGetOpts{}, }, }, AsUser: uid.UserId(), MetaWhat: constMsgMetaDesc | constMsgMetaSub | constMsgMetaData | constMsgMetaDel, sess: helper.sessions[0], } helper.topic.handleMeta(meta) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } r := helper.results[0] if len(r.messages) != 4 { t.Errorf("responses received: expected 4, received %d", len(r.messages)) } for _, msg := range r.messages { m := msg.(*ServerComMessage) if m.Meta != nil { if m.Meta.Desc == nil { t.Error("Meta.Desc expected to be specified.") } } else if m.Ctrl == nil { t.Error("Expected only meta or ctrl messages.") } } // Presence notifications. if len(helper.hubMessages) != 0 { t.Errorf("Hub messages recipients: expected 0, received %d", len(helper.hubMessages)) } } // Matches a subset in a superset. type supersetOf struct{ subset map[string]string } func SupersetOf(subset map[string]string) gomock.Matcher { return &supersetOf{subset} } func (s *supersetOf) Matches(x any) bool { super := x.(map[string]any) if super == nil { return false } for k, v := range s.subset { if x, ok := super[k]; ok { val := x.(string) if val != v { return false } } else { return false } } return true } func (s *supersetOf) String() string { return fmt.Sprintf("%+v is subset", s.subset) } func TestHandleMetaSetDescMePublicPrivate(t *testing.T) { topicName := "usrMe" numUsers := 1 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatMe, topicName /*attach=*/, true) defer helper.tearDown() uid := helper.uids[0] gomock.InOrder( helper.uu.EXPECT().Update(uid, SupersetOf(map[string]string{"Public": "new public"})).Return(nil), helper.ss.EXPECT().Update(topicName, uid, map[string]any{"Private": "new private"}).Return(nil), ) meta := &ClientComMessage{ Set: &MsgClientSet{ Id: "id456", Topic: topicName, MsgSetQuery: MsgSetQuery{ Desc: &MsgSetDesc{ Public: "new public", Private: "new private", }, }, }, AsUser: uid.UserId(), MetaWhat: constMsgMetaDesc, sess: helper.sessions[0], } helper.topic.handleMeta(meta) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } r := helper.results[0] if len(r.messages) != 1 { t.Fatalf("responses received: expected 1, received %d", len(r.messages)) } msg := r.messages[0].(*ServerComMessage) if msg == nil || msg.Ctrl == nil { t.Fatalf("Server message expected to have a ctrl submessage: %+v", msg) } if msg.Ctrl.Code != 200 { t.Errorf("Response code: expected 200, found %d", msg.Ctrl.Code) } // Presence notifications. if len(helper.hubMessages) != 1 { t.Fatalf("Hub messages recipients: expected 1, received %d", len(helper.hubMessages)) } // Make sure uid's sessions are notified. if userPres, ok := helper.hubMessages[uid.UserId()]; ok { if len(userPres) != 1 { t.Fatalf("User presence messages: expected 1, got %d", len(userPres)) } if userPres[0].SkipSid != helper.sessions[0].sid { t.Errorf("Pres notification SkipSid: %s expected vs %s found", helper.sessions[0].sid, userPres[0].SkipSid) } pres := userPres[0].Pres if pres == nil { t.Fatal("Presence message expected in hub output, but not found.") } if pres.Topic != "me" { t.Errorf("Presence message topic: expected 'me', found %s", pres.Topic) } if pres.What != "upd" { t.Errorf("Presence message what: expected 'upd', found %s", pres.What) } } else { t.Errorf("Hub expected to pres recipient %s", uid.UserId()) } } func TestHandleSessionUpdateSessToForeground(t *testing.T) { topicName := "usrMe" numUsers := 1 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatMe, topicName /*attach=*/, true) defer helper.tearDown() uid := helper.uids[0] supd := &sessionUpdate{ sess: helper.sessions[0], } var uaAgent string helper.topic.handleSessionUpdate(supd, &uaAgent, nil) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } // Expect online count bumped up to 2. if online := helper.topic.perUser[uid].online; online != 2 { t.Errorf("online count for %s: expected 2, found %d", uid.UserId(), online) } } func TestHandleSessionUpdateUserAgent(t *testing.T) { topicName := "usrMe" numUsers := 1 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatMe, topicName /*attach=*/, true) defer helper.tearDown() uid := helper.uids[0] supd := &sessionUpdate{ userAgent: "newUA", } uaAgent := "oldUA" timer := time.NewTimer(time.Hour) helper.topic.handleSessionUpdate(supd, &uaAgent, timer) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } // online count stays 1. if online := helper.topic.perUser[uid].online; online != 1 { t.Errorf("online count for %s: expected 1, found %d", uid.UserId(), online) } if uaAgent != "newUA" { t.Errorf("User agent: expected 'newUA', found '%s'", uaAgent) } timer.Stop() } func TestHandleUATimerEvent(t *testing.T) { topicName := "usrMe" numUsers := 1 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatMe, topicName /*attach=*/, true) defer helper.tearDown() uid := helper.uids[0] helper.topic.perSubs = make(map[string]perSubsData) helper.topic.perSubs[uid.UserId()] = perSubsData{online: true} helper.topic.handleUATimerEvent("newUA") helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } if helper.topic.userAgent != "newUA" { t.Errorf("Topic's user agent: expected 'newUA', found '%s'", helper.topic.userAgent) } // Presence notifications. if len(helper.hubMessages) != 1 { t.Fatalf("Hub messages recipients: expected 1, received %d", len(helper.hubMessages)) } // Make sure uid's sessions are notified. if userPres, ok := helper.hubMessages[uid.UserId()]; ok { if len(userPres) != 1 { t.Fatalf("User presence messages: expected 1, got %d", len(userPres)) } pres := userPres[0].Pres if pres == nil { t.Fatal("Presence message expected in hub output, but not found.") } if pres.Topic != "me" { t.Errorf("Presence message topic: expected 'me', found '%s'", pres.Topic) } if pres.What != "ua" { t.Errorf("Presence message what: expected 'ua', found '%s'", pres.What) } if pres.Src != topicName { t.Errorf("Presence message src: expected '%s', found '%s'", topicName, pres.Src) } } else { t.Errorf("Hub expected to pres recipient %s", uid.UserId()) } } func TestHandleTopicTimeout(t *testing.T) { topicName := "usrMe" numUsers := 1 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatMe, topicName /*attach=*/, true) defer helper.tearDown() uid := helper.uids[0] helper.topic.perSubs = make(map[string]perSubsData) helper.topic.perSubs[uid.UserId()] = perSubsData{online: true} helper.hub.unreg = make(chan *topicUnreg, 10) uaTimer := time.NewTimer(time.Hour) notifTimer := time.NewTimer(time.Hour) helper.topic.handleTopicTimeout(helper.hub, "newUA", uaTimer, notifTimer) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } if len(helper.hub.unreg) != 1 { t.Fatalf("Hub.unreg chan must contain exactly 1 message. Found %d.", len(helper.hub.unreg)) } if unreg := <-helper.hub.unreg; unreg.rcptTo != topicName { t.Errorf("unreg.rcptTo: expected '%s', found '%s'", topicName, unreg.rcptTo) } uaTimer.Stop() notifTimer.Stop() // Presence notifications. if len(helper.hubMessages) != 1 { t.Fatalf("Hub messages recipients: expected 1, received %d", len(helper.hubMessages)) } // Make sure uid's sessions are notified. if userPres, ok := helper.hubMessages[uid.UserId()]; ok { if len(userPres) != 1 { t.Fatalf("User presence messages: expected 1, got %d", len(userPres)) } pres := userPres[0].Pres if pres == nil { t.Fatal("Presence message expected in hub output, but not found.") } if pres.Topic != "me" { t.Errorf("Presence message topic: expected 'me', found '%s'", pres.Topic) } if pres.What != "off" { t.Errorf("Presence message what: expected 'off', found '%s'", pres.What) } if pres.Src != topicName { t.Errorf("Presence message src: expected '%s', found '%s'", topicName, pres.Src) } } else { t.Errorf("Hub expected to pres recipient %s", uid.UserId()) } } func TestHandleTopicTermination(t *testing.T) { topicName := "usrMe" numUsers := 1 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatMe, topicName /*attach=*/, true) defer helper.tearDown() done := make(chan bool, 1) exit := &shutDown{ reason: StopDeleted, done: done, } helper.topic.handleTopicTermination(exit) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } if len(done) != 1 { t.Fatal("done callback isn't invoked.") } <-done for i, s := range helper.sessions { if len(s.detach) != 1 { t.Fatalf("Session %d: detach channel is empty.", i) } val := <-s.detach if val != topicName { t.Errorf("Session %d is expected to detach from topic '%s', found '%s'.", i, topicName, val) } } // Presence notifications. if len(helper.hubMessages) != 0 { t.Fatalf("Hub messages recipients: expected 0, received %d", len(helper.hubMessages)) } } func TestHandleBroadcastDataWithAttachments(t *testing.T) { numUsers := 2 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatP2P, "p2p-test", true) defer helper.tearDown() helper.mm.EXPECT().Save(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, true) from := helper.uids[0].UserId() msg := &ClientComMessage{ AsUser: from, Original: from, Pub: &MsgClientPub{ Topic: "p2p", Content: "Check out this image!", Head: map[string]any{ "attachments": []map[string]any{ {"mime": "image/jpeg", "name": "photo.jpg", "size": 1024000}, }, }, NoEcho: true, }, sess: helper.sessions[0], } helper.topic.handleClientMsg(msg) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } // Verify message with attachments was delivered if len(helper.results[1].messages) != 1 { t.Fatalf("Uid2: expected 1 message, got %d", len(helper.results[1].messages)) } r := helper.results[1].messages[0].(*ServerComMessage) if r.Data == nil { t.Fatal("Response must have a data message") } if r.Data.Head == nil { t.Fatal("Response must have attachments in head") } attachments := r.Data.Head["attachments"] if attachments == nil { t.Fatal("Expected attachments in message head") } } func TestHandleBroadcastInfoChannelWithMultipleReaders(t *testing.T) { topicName := "grpTest" chanName := "chnTest" numUsers := 5 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatGrp, topicName, true) helper.topic.isChan = true defer helper.tearDown() helper.topic.lastID = 15 readId := 12 from := helper.uids[0] // Set up multiple channel readers for i := 1; i < numUsers; i++ { uid := helper.uids[i] pud := helper.topic.perUser[uid] pud.modeGiven = types.ModeCChnReader pud.isChan = true helper.topic.perUser[uid] = pud } helper.ss.EXPECT().Update(chanName, from, map[string]any{"ReadSeqId": readId}).Return(nil) msg := &ClientComMessage{ AsUser: from.UserId(), Original: chanName, Note: &MsgClientNote{ Topic: chanName, What: "read", SeqId: readId, }, sess: helper.sessions[0], } helper.topic.handleClientMsg(msg) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } // Channel topics don't forward note messages to other users for i, r := range helper.results { if numMessages := len(r.messages); numMessages != 0 { t.Errorf("User %d is not expected to receive any messages, %d received", i, numMessages) } } // Only sender gets presence notification if len(helper.hubMessages) != 1 { t.Fatalf("Hub expected exactly 1 recipient, got %d", len(helper.hubMessages)) } if _, ok := helper.hubMessages[from.UserId()]; !ok { t.Fatal("Expected presence notification for sender") } } func TestRegisterSessionWithComplexModeString(t *testing.T) { topicName := "grpTest" numUsers := 2 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatGrp, topicName, false) defer helper.tearDown() uid := helper.uids[1] s := helper.sessions[1] r := helper.results[1] // User with existing subscription wants to change mode pud := helper.topic.perUser[uid] pud.modeWant = types.ModeCPublic pud.modeGiven = types.ModeCPublic helper.topic.perUser[uid] = pud join := &ClientComMessage{ Original: topicName, Sub: &MsgClientSub{ Id: "id456", Topic: topicName, Set: &MsgSetQuery{ Sub: &MsgSetSub{ Mode: "JRWPAS", // Complex mode string with multiple permissions }, }, }, AsUser: uid.UserId(), AuthLvl: int(auth.LevelAuth), sess: s, } helper.ss.EXPECT().Update(topicName, uid, gomock.Any()).Return(nil) helper.topic.registerSession(join) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } if len(helper.topic.sessions) != 1 { t.Fatalf("Attached sessions: expected 1, found %d", len(helper.topic.sessions)) } if len(s.subs) != 1 { t.Fatalf("Session subscriptions: expected 1, found %d", len(s.subs)) } online := helper.topic.perUser[uid].online if online != 1 { t.Fatalf("Number of online sessions: expected 1, found %d", online) } registerSessionVerifyOutputs(t, r, []int{http.StatusOK}) } func TestHandleBroadcastDataGroupWithMutedUser(t *testing.T) { topicName := "grp-test" numUsers := 4 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatGrp, topicName, true) defer helper.tearDown() helper.mm.EXPECT().Save(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, true) // User 2 has muted the topic (no Pres permission) pu2 := helper.topic.perUser[helper.uids[2]] pu2.modeWant = types.ModeJoin | types.ModeRead | types.ModeWrite pu2.modeGiven = pu2.modeWant helper.topic.perUser[helper.uids[2]] = pu2 from := helper.uids[0].UserId() msg := &ClientComMessage{ AsUser: from, Original: topicName, Pub: &MsgClientPub{ Topic: topicName, Content: "test message", NoEcho: true, }, sess: helper.sessions[0], } helper.topic.handleClientMsg(msg) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } // User 2 should still receive the message (has Read permission) if len(helper.results[2].messages) != 1 { t.Fatalf("Uid2: expected 1 message, got %d", len(helper.results[2].messages)) } // Check presence notifications - muted user should not receive presence if len(helper.hubMessages) != 3 { // Users 0, 1, 3 but not 2 t.Fatalf("Hub expected 3 recipients, got %d", len(helper.hubMessages)) } // Verify user 2 is not in presence notifications if _, ok := helper.hubMessages[helper.uids[2].UserId()]; ok { t.Fatal("Muted user should not receive presence notifications") } } func TestUnregisterSessionWithPendingCall(t *testing.T) { numUsers := 2 helper := TopicTestHelper{} helper.setUp(t, numUsers, types.TopicCatP2P, "p2p-test", true) defer helper.tearDown() uid := helper.uids[0] s := helper.sessions[0] r := helper.results[0] // Set up a pending call matching the actual videoCall structure helper.topic.currentCall = &videoCall{ seq: 123, parties: make(map[string]callPartyData), } helper.topic.currentCall.parties[s.sid] = callPartyData{ uid: uid, isOriginator: true, sess: s, } helper.mm.EXPECT().Save(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, true) leave := &ClientComMessage{ Leave: &MsgClientLeave{ Id: "id456", Topic: "p2p-test", }, AsUser: uid.UserId(), sess: s, init: true, } helper.topic.unregisterSession(leave) helper.finish() // Check for errors from testHubLoop if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { t.Fatal(errorMsgs[0].Ctrl.Text) } // Verify session was unregistered if len(helper.topic.sessions) != 1 { t.Errorf("Attached sessions: expected 1, found %d", len(helper.topic.sessions)) } if len(s.subs) != 0 { t.Errorf("Session subscriptions: expected 0, found %d", len(s.subs)) } // Verify call party was removed (if the implementation handles this) if helper.topic.currentCall != nil && helper.topic.currentCall.parties != nil { if _, exists := helper.topic.currentCall.parties[s.sid]; exists { t.Error("Call party should have been removed when session unregistered") } } if len(r.messages) != 3 { t.Fatalf("`responses` expected to contain 3 elements, found %d", len(r.messages)) } // Expected one of each: {data}, {info}, {ctrl}. var found = 0 for _, msg := range r.messages { m := msg.(*ServerComMessage) if m.Data != nil { found++ if m.Data.Head == nil || m.Data.Head["webrtc"] != "disconnected" || m.Data.Head["replace"] != ":123" { t.Fatalf("Unexpected Data.Head: %+v", m.Data.Head) } } else if m.Info != nil { found++ if m.Info.SeqId != 123 { t.Fatalf("Unexpected Info.SeqId: %d", m.Info.SeqId) } if m.Info.What != "call" { t.Fatalf("Unexpected Info.What: %s", m.Info.What) } if m.Info.Event != "hang-up" { t.Fatalf("Unexpected Info.Event: %s", m.Info.Event) } } else if m.Ctrl != nil { found++ if m.Ctrl.Code != http.StatusOK { t.Fatalf("Unexpected Ctrl.Code: %d", m.Ctrl.Code) } } else { t.Error("Expected only {data}, {info}, {ctrl} messages.") } } if found != 3 { t.Fatal("Expected only {data}, {info}, {ctrl} messages, but some are missing") } } func TestReplyDelMsgHardDelete(t *testing.T) { // Test hard delete scenario - hard deletes affect all users equally // and don't update individual unread counters the same way as soft deletes topicName := "p2pTest" helper := TopicTestHelper{} helper.setUp(t, 2, types.TopicCatP2P, topicName, true) defer helper.tearDown() user1 := helper.uids[0] // User with delete permission user2 := helper.uids[1] // Other user // Set up initial state: user2 has read up to message 5, topic has messages up to 10 helper.topic.lastID = 10 pud1 := helper.topic.perUser[user1] pud1.readID = 10 pud1.modeGiven = types.ModeCFull // Full permissions including delete pud1.modeWant = types.ModeCFull helper.topic.perUser[user1] = pud1 pud2 := helper.topic.perUser[user2] pud2.readID = 5 pud2.modeGiven = types.ModeCFull pud2.modeWant = types.ModeCFull helper.topic.perUser[user2] = pud2 // Simulate user1 doing a hard delete of messages 7 and 8 msg := &ClientComMessage{ Del: &MsgClientDel{ Id: "del123", What: "msg", DelSeq: []MsgRange{ {LowId: 7, HiId: 9}, // Deletes messages 7 and 8 [7, 9) }, Hard: true, // Hard delete }, AsUser: user1.UserId(), sess: helper.sessions[0], init: true, } // Mock the message deletion for hard delete (forUser = types.ZeroUid) helper.mm.EXPECT().DeleteList(topicName, 1, types.ZeroUid, gomock.Any(), []types.Range{{Low: 7, Hi: 9}}).Return(nil) // Call the function under test err := helper.topic.replyDelMsg(helper.sessions[0], user1, false, msg) // Verify if err != nil { t.Fatalf("replyDelMsg failed: %v", err) } // Verify session got success response helper.finish() registerSessionVerifyOutputs(t, helper.results[0], []int{http.StatusOK}) // For hard deletes, all users' delID should be updated if helper.topic.perUser[user1].delID != 1 { t.Errorf("Expected user1.delID to be 1, got %d", helper.topic.perUser[user1].delID) } if helper.topic.perUser[user2].delID != 1 { t.Errorf("Expected user2.delID to be 1, got %d", helper.topic.perUser[user2].delID) } } func TestReplyDelMsgUpdatesUnreadCounters(t *testing.T) { // This test simulates the scenario from issue #898: // 1. User1 sends messages to User2 // 2. User1 deletes some messages (soft delete) // 3. Verify that the unread calculation logic works correctly topicName := "p2pTest" helper := TopicTestHelper{} helper.setUp(t, 2, types.TopicCatP2P, topicName, true) defer helper.tearDown() user1 := helper.uids[0] // Sender/deleter user2 := helper.uids[1] // Recipient // Set up initial state: user2 has read up to message 5, topic has messages up to 10 // So user2 has 5 unread messages (6, 7, 8, 9, 10) helper.topic.lastID = 10 pud1 := helper.topic.perUser[user1] pud1.readID = 10 // user1 has read all helper.topic.perUser[user1] = pud1 pud2 := helper.topic.perUser[user2] pud2.readID = 5 // user2 has 5 unread messages helper.topic.perUser[user2] = pud2 // Simulate user1 deleting messages 7 and 8 (2 of user2's unread messages) msg := &ClientComMessage{ Del: &MsgClientDel{ Id: "del123", What: "msg", DelSeq: []MsgRange{ {LowId: 7, HiId: 9}, // Deletes messages 7 and 8 [7, 9) }, Hard: false, // Soft delete }, AsUser: user1.UserId(), sess: helper.sessions[0], init: true, } // Mock the message deletion helper.mm.EXPECT().DeleteList(topicName, 1, user1, time.Duration(0), []types.Range{{Low: 7, Hi: 9}}).Return(nil) // Call the function under test err := helper.topic.replyDelMsg(helper.sessions[0], user1, false, msg) // Verify if err != nil { t.Fatalf("replyDelMsg failed: %v", err) } // Verify session got success response helper.finish() registerSessionVerifyOutputs(t, helper.results[0], []int{http.StatusOK}) // The key verification is that calculateUnreadInRanges should have been called // with the correct parameters. We can test this indirectly by testing the function: ranges := []types.Range{{Low: 7, Hi: 9}} unreadDeleted := calculateUnreadInRanges(5, 10, ranges) // user2's readID=5, lastID=10 if unreadDeleted != 2 { t.Errorf("Expected 2 unread messages to be deleted for user2, got %d", unreadDeleted) } } func TestCalculateUnreadInRanges(t *testing.T) { tests := []struct { name string readID int lastID int ranges []types.Range expected int }{ { name: "no unread messages", readID: 10, lastID: 10, ranges: []types.Range{{Low: 5, Hi: 15}}, expected: 0, }, { name: "no deleted messages in unread range", readID: 5, lastID: 10, ranges: []types.Range{{Low: 1, Hi: 5}}, expected: 0, }, { name: "all unread messages deleted", readID: 5, lastID: 10, ranges: []types.Range{{Low: 6, Hi: 11}}, expected: 5, }, { name: "partial unread messages deleted", readID: 5, lastID: 10, ranges: []types.Range{{Low: 7, Hi: 9}}, expected: 2, }, { name: "single message deleted", readID: 5, lastID: 10, ranges: []types.Range{{Low: 7, Hi: 0}}, // Hi: 0 means single message expected: 1, }, { name: "multiple ranges", readID: 5, lastID: 15, ranges: []types.Range{{Low: 7, Hi: 9}, {Low: 12, Hi: 14}}, expected: 4, // 2 messages in range [7,9) + 2 messages in range [12,14) }, { name: "overlapping with unread boundaries", readID: 5, lastID: 10, ranges: []types.Range{{Low: 4, Hi: 8}, {Low: 9, Hi: 12}}, expected: 4, // [6,8) + [9,11) = 2 + 2 = 4 unread messages deleted }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { result := calculateUnreadInRanges(tt.readID, tt.lastID, tt.ranges) if result != tt.expected { t.Errorf("calculateUnreadInRanges(%d, %d, %v) = %d; want %d", tt.readID, tt.lastID, tt.ranges, result, tt.expected) } }) } } func TestMain(m *testing.M) { logs.Init(os.Stderr, "stdFlags") // Set max subscriber count to effective infinity. globals.maxSubscriberCount = 1_000_000_000 os.Exit(m.Run()) } ================================================ FILE: server/user.go ================================================ package main import ( "container/heap" "math/rand" "time" "slices" "github.com/tinode/chat/server/auth" "github.com/tinode/chat/server/logs" "github.com/tinode/chat/server/push" "github.com/tinode/chat/server/store" "github.com/tinode/chat/server/store/types" ) const ( // Unread counter update return codes. // Counter not initialized, IO pending. unreadUpdateIOPending = -1 // Counter initialization error. unreadUpdateError = -2 ) // Process request for a new account. func replyCreateUser(s *Session, msg *ClientComMessage, rec *auth.Rec) { // The session cannot authenticate with the new account because it's already authenticated. if msg.Acc.Login && (!s.uid.IsZero() || rec != nil) { s.queueOut(ErrAlreadyAuthenticated(msg.Id, "", msg.Timestamp)) logs.Warn.Println("create user: login requested while authenticated, sid=", s.sid) return } // Find authenticator for the requested scheme. authhdl := store.Store.GetLogicalAuthHandler(msg.Acc.Scheme) if authhdl == nil { // New accounts must have an authentication scheme s.queueOut(ErrMalformed(msg.Id, "", msg.Timestamp)) logs.Warn.Println("create user: unknown auth handler, sid=", s.sid) return } // Check if login is unique and compliance with the policy (not too long or too short). if ok, err := authhdl.IsUnique(msg.Acc.Secret, s.remoteAddr); !ok { logs.Warn.Println("create user: auth secret is not compliant", err, "sid=", s.sid) s.queueOut(decodeStoreError(err, msg.Id, msg.Timestamp, map[string]any{"what": "auth"})) return } var user types.User var private any // If account state is being assigned, make sure the sender is a root user. if msg.Acc.State != "" { if auth.Level(msg.AuthLvl) != auth.LevelRoot { logs.Warn.Println("create user: attempt to set account state by non-root, sid=", s.sid) msg := ErrPermissionDenied(msg.Id, "", msg.Timestamp) msg.Ctrl.Params = map[string]any{"what": "state"} s.queueOut(msg) return } state, err := types.NewObjState(msg.Acc.State) if err != nil || state == types.StateUndefined || state == types.StateDeleted { logs.Warn.Println("create user: invalid account state", err, "sid=", s.sid) s.queueOut(ErrMalformed(msg.Id, "", msg.Timestamp)) return } user.State = state } // Ensure tags are unique and not restricted. if tags := normalizeTags(msg.Acc.Tags, globals.maxTagCount); tags != nil { if !restrictedTagsEqual(tags, nil, globals.immutableTagNS) { logs.Warn.Println("create user: attempt to directly assign restricted tags, sid=", s.sid) msg := ErrPermissionDenied(msg.Id, "", msg.Timestamp) msg.Ctrl.Params = map[string]any{"what": "tags"} s.queueOut(msg) return } user.Tags = tags } // Pre-check credentials for validity. We don't know user's access level // consequently cannot check presence of required credentials. Must do that later. creds := normalizeCredentials(msg.Acc.Cred, true) for i := range creds { cr := &creds[i] vld := store.Store.GetValidator(cr.Method) if _, err := vld.PreCheck(cr.Value, cr.Params); err != nil { logs.Warn.Println("create user: failed credential pre-check", cr, err, "sid=", s.sid) s.queueOut(decodeStoreError(err, msg.Id, msg.Timestamp, map[string]any{"what": cr.Method})) return } } // Assign default access values in case the acc creator has not provided them user.Access.Auth = getDefaultAccess(types.TopicCatP2P, true, false) | getDefaultAccess(types.TopicCatGrp, true, false) user.Access.Anon = getDefaultAccess(types.TopicCatP2P, false, false) | getDefaultAccess(types.TopicCatGrp, false, false) // Assign actual access values, public and private. if msg.Acc.Desc != nil { if msg.Acc.Desc.DefaultAcs != nil { if msg.Acc.Desc.DefaultAcs.Auth != "" { user.Access.Auth.UnmarshalText([]byte(msg.Acc.Desc.DefaultAcs.Auth)) user.Access.Auth &= globals.typesModeCP2P if user.Access.Auth != types.ModeNone { user.Access.Auth |= types.ModeApprove } } if msg.Acc.Desc.DefaultAcs.Anon != "" { user.Access.Anon.UnmarshalText([]byte(msg.Acc.Desc.DefaultAcs.Anon)) user.Access.Anon &= globals.typesModeCP2P if user.Access.Anon != types.ModeNone { user.Access.Anon |= types.ModeApprove } } } if !isNullValue(msg.Acc.Desc.Public) { user.Public = msg.Acc.Desc.Public } if !isNullValue(msg.Acc.Desc.Private) { private = msg.Acc.Desc.Private } } // Create user record in the database. if _, err := store.Users.Create(&user, private); err != nil { logs.Warn.Println("create user: failed to create user", err, "sid=", s.sid) s.queueOut(ErrUnknown(msg.Id, "", msg.Timestamp)) return } // Add authentication record. The authhdl.AddRecord may change tags. rec, err := authhdl.AddRecord(&auth.Rec{Uid: user.Uid(), Tags: user.Tags}, msg.Acc.Secret, s.remoteAddr) if err != nil { logs.Warn.Println("create user: add auth record failed", err, "sid=", s.sid) s.queueOut(decodeStoreError(err, msg.Id, msg.Timestamp, nil)) // Attempt to delete incomplete user record if err = store.Users.Delete(user.Uid(), true); err != nil { logs.Warn.Println("create user: failed to delete incomplete user record", err, "sid=", s.sid) } return } // When creating an account, the user must provide all required credentials. // If any are missing, reject the request. if len(creds) < len(globals.authValidators[rec.AuthLevel]) { logs.Warn.Println("create user: missing credentials; have:", creds, "want:", globals.authValidators[rec.AuthLevel], s.sid) _, missing, _ := stringSliceDelta(globals.authValidators[rec.AuthLevel], credentialMethods(creds)) s.queueOut(decodeStoreError(types.ErrPolicy, msg.Id, msg.Timestamp, map[string]any{"creds": missing})) // Attempt to delete incomplete user record if err = store.Users.Delete(user.Uid(), true); err != nil { logs.Warn.Println("create user: failed to delete incomplete user record", err, "sid=", s.sid) } return } // Save credentials, update tags if necessary. tmpToken, _, _ := store.Store.GetLogicalAuthHandler("token").GenSecret(&auth.Rec{ Uid: user.Uid(), AuthLevel: auth.LevelAuth, Lifetime: auth.Duration(time.Hour * 24), }) validated, _, err := addCreds(user.Uid(), creds, rec.Tags, s.lang, tmpToken) if err != nil { logs.Warn.Println("create user: failed to save or validate credential", err, "sid=", s.sid) s.queueOut(decodeStoreError(err, msg.Id, msg.Timestamp, nil)) // Delete incomplete user record. if err = store.Users.Delete(user.Uid(), true); err != nil { logs.Warn.Println("create user: failed to delete incomplete user record", err, "sid=", s.sid) } return } if msg.Extra != nil && len(msg.Extra.Attachments) > 0 { if err := store.Files.LinkAttachments(user.Uid().UserId(), types.ZeroUid, msg.Extra.Attachments); err != nil { logs.Warn.Println("create user: failed to link avatar attachment", err, "sid=", s.sid) // This is not a critical error, continue execution. } } var reply *ServerComMessage if msg.Acc.Login { // Process user's login request. _, missing, _ := stringSliceDelta(globals.authValidators[rec.AuthLevel], validated) reply = s.onLogin(msg.Id, msg.Timestamp, rec, missing) } else { // Not using the new account for logging in. reply = NoErrCreated(msg.Id, "", msg.Timestamp) reply.Ctrl.Params = map[string]any{ "user": user.Uid().UserId(), "authlvl": rec.AuthLevel.String(), } } params := reply.Ctrl.Params.(map[string]any) params["desc"] = &MsgTopicDesc{ CreatedAt: &user.CreatedAt, UpdatedAt: &user.UpdatedAt, DefaultAcs: &MsgDefaultAcsMode{ Auth: user.Access.Auth.String(), Anon: user.Access.Anon.String(), }, Public: user.Public, Private: private, } s.queueOut(reply) pluginAccount(&user, plgActCreate) } // Process update to an account: // * Authentication update, i.e. login/password change // * Credentials update func replyUpdateUser(s *Session, msg *ClientComMessage, rec *auth.Rec) { if s.uid.IsZero() && rec == nil { // Session is not authenticated and no temporary auth is provided. logs.Warn.Println("replyUpdateUser: not a new account and not authenticated", s.sid) s.queueOut(ErrPermissionDenied(msg.Id, "", msg.Timestamp)) return } else if msg.AsUser != "" && rec != nil { // Two UIDs: one from msg.from, one from temporary auth. Ambigous, reject. logs.Warn.Println("replyUpdateUser: got both authenticated session and token", s.sid) s.queueOut(ErrMalformed(msg.Id, "", msg.Timestamp)) return } userId := msg.AsUser authLvl := auth.Level(msg.AuthLvl) if rec != nil { userId = rec.Uid.UserId() authLvl = rec.AuthLevel } if msg.Acc.User != "" && msg.Acc.User != userId { if s.authLvl != auth.LevelRoot { logs.Warn.Println("replyUpdateUser: attempt to change another's account by non-root", s.sid) s.queueOut(ErrPermissionDenied(msg.Id, "", msg.Timestamp)) return } // Root is editing someone else's account. userId = msg.Acc.User authLvl = auth.ParseAuthLevel(msg.Acc.AuthLevel) } uid := types.ParseUserId(userId) if uid.IsZero() { // msg.Acc.User contains invalid data. s.queueOut(ErrMalformed(msg.Id, "", msg.Timestamp)) logs.Warn.Println("replyUpdateUser: user id is invalid or missing", s.sid) return } // Only root can suspend accounts, including own account. if msg.Acc.State != "" && s.authLvl != auth.LevelRoot { s.queueOut(ErrPermissionDenied(msg.Id, "", msg.Timestamp)) logs.Warn.Println("replyUpdateUser: attempt to change account state by non-root", s.sid) return } user, err := store.Users.Get(uid) if user == nil && err == nil { err = types.ErrNotFound } if err != nil { logs.Warn.Println("replyUpdateUser: failed to fetch user from DB", err, s.sid) s.queueOut(decodeStoreError(err, msg.Id, msg.Timestamp, nil)) return } var params map[string]any if msg.Acc.Scheme != "" { err = updateUserAuth(msg, user, rec, s.remoteAddr) } else if len(msg.Acc.Cred) > 0 { if authLvl == auth.LevelNone { // msg.Acc.AuthLevel contains invalid data. s.queueOut(ErrMalformed(msg.Id, "", msg.Timestamp)) logs.Warn.Println("replyUpdateUser: auth level is missing", s.sid) return } // Handle request to update credentials. tmpToken, _, _ := store.Store.GetLogicalAuthHandler("token").GenSecret(&auth.Rec{ Uid: uid, AuthLevel: auth.LevelNone, Lifetime: auth.Duration(time.Hour * 24), Features: auth.FeatureNoLogin, }) _, _, err := addCreds(uid, msg.Acc.Cred, nil, s.lang, tmpToken) if err == nil { if allCreds, err := store.Users.GetAllCreds(uid, "", true); err != nil { var validated []string for i := range allCreds { validated = append(validated, allCreds[i].Method) } _, missing, _ := stringSliceDelta(globals.authValidators[authLvl], validated) if len(missing) > 0 { params = map[string]any{"cred": missing} } } } } else if msg.Acc.State != "" { var changed bool changed, err = changeUserState(s, uid, user, msg) if !changed && err == nil { s.queueOut(InfoNotModified(msg.Id, "", msg.Timestamp)) return } } else { err = types.ErrMalformed } if err != nil { logs.Warn.Println("replyUpdateUser: failed to update user", err, s.sid) s.queueOut(decodeStoreError(err, msg.Id, msg.Timestamp, nil)) return } s.queueOut(NoErrParams(msg.Id, "", msg.Timestamp, params)) // Call plugin with the account update pluginAccount(user, plgActUpd) } // Authentication update func updateUserAuth(msg *ClientComMessage, user *types.User, _ *auth.Rec, remoteAddr string) error { authhdl := store.Store.GetLogicalAuthHandler(msg.Acc.Scheme) if authhdl != nil { // Request to update auth of an existing account. Only basic & rest auth are currently supported // TODO(gene): support adding new auth schemes rec, err := authhdl.UpdateRecord(&auth.Rec{Uid: user.Uid(), Tags: user.Tags}, msg.Acc.Secret, remoteAddr) if err != nil { return err } // Tags may have been changed by authhdl.UpdateRecord, reset them. // Can't do much with the error here, logging it but not returning. if _, err = store.Users.UpdateTags(user.Uid(), nil, nil, rec.Tags); err != nil { logs.Warn.Println("updateUserAuth tags update failed:", err) } return nil } // Invalid or unknown auth scheme return types.ErrMalformed } // addCreds adds new credentials and re-send validation request for existing ones. // It also adds credential-defined tags if necessary. // Returns methods validated in this call only. Returns either a full set of tags // or nil for tags when tags are unchanged. func addCreds(uid types.Uid, creds []MsgCredClient, extraTags []string, lang string, tmpToken []byte) ([]string, []string, error) { var validated []string for i := range creds { cr := &creds[i] vld := store.Store.GetValidator(cr.Method) if vld == nil || !vld.IsInitialized() { // Ignore unknown or un-initialized validator. continue } isNew, err := vld.Request(uid, cr.Value, lang, cr.Response, tmpToken) if err != nil { return nil, nil, err } if isNew && cr.Response != "" { // If response is provided and vld.Request did not return an error, the new request was // successfully validated. validated = append(validated, cr.Method) // Generate tags for these confirmed credentials. if globals.validators[cr.Method].addToTags { extraTags = append(extraTags, cr.Method+":"+cr.Value) } } } // Save tags potentially changed by the validator. if len(extraTags) > 0 { if utags, err := store.Users.UpdateTags(uid, extraTags, nil, nil); err == nil { extraTags = utags } else { logs.Warn.Println("add cred tags update failed:", err) } } else { extraTags = nil } return validated, extraTags, nil } // validatedCreds returns the list of validated credentials including those validated in this call. // Returns all validated methods including those validated earlier and now. // Returns either a full set of tags or nil for tags if tags are unchanged. func validatedCreds(uid types.Uid, authLvl auth.Level, creds []MsgCredClient, errorOnFail bool) ([]string, []string, error) { // Check if credential validation is required. if len(globals.authValidators[authLvl]) == 0 { return nil, nil, nil } // Get all validated methods allCreds, err := store.Users.GetAllCreds(uid, "", true) if err != nil { return nil, nil, err } methods := make(map[string]struct{}) for i := range allCreds { methods[allCreds[i].Method] = struct{}{} } // Add credentials which are validated in this call. // Unknown validators are removed. creds = normalizeCredentials(creds, false) var tagsToAdd []string for i := range creds { cr := &creds[i] if cr.Response == "" { // Ignore empty response. continue } vld := store.Store.GetValidator(cr.Method) // No need to check for nil, unknown methods are removed earlier. value, err := vld.Check(uid, cr.Response) if err != nil { // Check failed. if storeErr, ok := err.(types.StoreError); ok && storeErr == types.ErrCredentials { if errorOnFail { // Report invalid response. return nil, nil, types.ErrInvalidResponse } // Skip invalid response. Keep credential unvalidated. continue } // Actual error. Report back. return nil, nil, err } // Check did not return an error: the request was successfully validated. methods[cr.Method] = struct{}{} // Add validated credential to user's tags. if globals.validators[cr.Method].addToTags { tagsToAdd = append(tagsToAdd, cr.Method+":"+value) } } var tags []string if len(tagsToAdd) > 0 { // Save update to tags if utags, err := store.Users.UpdateTags(uid, tagsToAdd, nil, nil); err == nil { tags = utags } else { logs.Warn.Println("validated creds tags update failed:", err) tags = nil } } else { tags = nil } validated := make([]string, 0, len(methods)) for method := range methods { validated = append(validated, method) } return validated, tags, nil } // deleteCred deletes user's credential. // Returns full set of remaining tags or nil if tags are unchanged. func deleteCred(uid types.Uid, authLvl auth.Level, cred *MsgCredClient) ([]string, error) { vld := store.Store.GetValidator(cred.Method) if vld == nil || cred.Value == "" { // Reject invalid request: unknown validation method or missing credential value. return nil, types.ErrMalformed } // Is this a required credential for this validation level? isRequired := slices.Contains(globals.authValidators[authLvl], cred.Method) // If credential is required, make sure the method remains validated even after this credential is deleted. if isRequired { // There could be multiple validated credentials for the same method thus we are getting a map with count // for each method. // Get all credentials of the given method. allCreds, err := store.Users.GetAllCreds(uid, cred.Method, false) if err != nil { return nil, err } // Check if it's OK to delete: there is another validated value // or this value is not validated in the first place. var okTodelete bool for _, cr := range allCreds { if (cr.Done && cr.Value != cred.Value) || (!cr.Done && cr.Value == cred.Value) { okTodelete = true break } } if !okTodelete { // Reject: this is the only validated credential and it must be provided. return nil, types.ErrPolicy } } // The credential is either not required or more than one credential is validated for the given method. err := vld.Remove(uid, cred.Value) if err != nil { if err == types.ErrNotFound { // Credential is not deleted because it's not found err = nil } return nil, err } // Remove generated tags for the deleted credential. var tags []string if globals.validators[cred.Method].addToTags { // This error should not be returned to user. if utags, err := store.Users.UpdateTags(uid, nil, []string{cred.Method + ":" + cred.Value}, nil); err == nil { tags = utags } else { logs.Warn.Println("delete cred: failed to update tags:", err) tags = nil } } else { tags = nil } return tags, nil } // Change user state: suspended/normal (ok). // 1. Not needed -- Disable/enable logins (state checked after login). // 2. If suspending, evict user's sessions. Skip this step if resuming. // 3. Suspend/activate p2p with the user. // 4. Suspend/activate grp topics where the user is the owner. // 5. Update user's DB record. func changeUserState(s *Session, uid types.Uid, user *types.User, msg *ClientComMessage) (bool, error) { state, err := types.NewObjState(msg.Acc.State) if err != nil || state == types.StateUndefined { logs.Warn.Println("replyUpdateUser: invalid account state", s.sid) return false, types.ErrMalformed } // State unchanged. if user.State == state { return false, nil } if state != types.StateOK { // Terminate all sessions. globals.sessionStore.EvictUser(uid, "") } err = store.Users.UpdateState(uid, state) if err != nil { return false, err } // Update state of all loaded in memory user's p2p & grp-owner topics. globals.hub.userStatus <- &userStatusReq{forUser: uid, state: state} user.State = state return true, err } // Request to delete a user: // 1. Disable user's login // 2. Terminate all user's sessions except the current session. // 3. Stop all active topics // 4. Notify other subscribers that topics are being deleted. // 5. Delete user from the database. // 6. Report success or failure. // 7. Terminate user's last session. func replyDelUser(s *Session, msg *ClientComMessage) { var uid types.Uid if msg.Del.User == "" || msg.Del.User == s.uid.UserId() { // Check if account deletion is disabled. if globals.permanentAccounts && s.authLvl != auth.LevelRoot { logs.Warn.Println("replyDelUser: account deletion disabled", s.sid) s.queueOut(ErrPolicy(msg.Id, "", msg.Timestamp)) return } // Delete current user. uid = s.uid } else if s.authLvl == auth.LevelRoot { // Delete another user. uid = types.ParseUserId(msg.Del.User) if uid.IsZero() { logs.Warn.Println("replyDelUser: invalid user ID", msg.Del.User, s.sid) s.queueOut(ErrMalformed(msg.Id, "", msg.Timestamp)) return } } else { logs.Warn.Println("replyDelUser: illegal attempt to delete another user", msg.Del.User, s.sid) s.queueOut(ErrPermissionDenied(msg.Id, "", msg.Timestamp)) return } // Disable all authenticators authnames := store.Store.GetAuthNames() for _, name := range authnames { hdl := store.Store.GetLogicalAuthHandler(name) if !hdl.IsInitialized() { continue } if err := hdl.DelRecords(uid); err != nil { // This could be completely benign, i.e. authenticator exists but not used. logs.Warn.Println("replyDelUser: failed to delete auth record", uid.UserId(), name, err, s.sid) if storeErr, ok := err.(types.StoreError); ok && storeErr == types.ErrUnsupported { // Authenticator refused to delete record: user account cannot be deleted. s.queueOut(ErrOperationNotAllowed(msg.Id, "", msg.Timestamp)) return } } } // Terminate all sessions. Skip the current session so the requester gets a response. globals.sessionStore.EvictUser(uid, s.sid) // Remove user from cache and announce to cluster that the user is deleted. usersRemoveUser(uid) // Stop topics where the user is the owner and p2p topics. done := make(chan bool) globals.hub.unreg <- &topicUnreg{forUser: uid, del: msg.Del.Hard, done: done} <-done // Notify users of interest that the user is gone. if uoi, err := store.Users.GetSubs(uid); err == nil { presUsersOfInterestOffline(uid, uoi, "gone") } else { logs.Warn.Println("replyDelUser: failed to send notifications to users", err, s.sid) } // Notify subscribers of the group topics where the user was the owner that the topics were deleted. if ownTopics, err := store.Users.GetOwnTopics(uid); err == nil { for _, topicName := range ownTopics { if subs, err := store.Topics.GetSubs(topicName, nil); err == nil { presSubsOfflineOffline(topicName, types.TopicCatGrp, subs, "gone", &presParams{}, s.sid) } else { logs.Warn.Println("replyDelUser: failed to notify topic subscribers", err, topicName, s.sid) } } } else { logs.Warn.Println("replyDelUser: failed to send notifications to owned topics", err, s.sid) } // TODO: suspend all P2P topics with the user. // Delete user's records from the database. if err := store.Users.Delete(uid, msg.Del.Hard); err != nil { logs.Warn.Println("replyDelUser: failed to delete user", err, s.sid) s.queueOut(decodeStoreError(err, msg.Id, msg.Timestamp, nil)) return } s.queueOut(NoErr(msg.Id, "", msg.Timestamp)) if s.uid == uid && s.multi == nil { // Evict the current session if it belongs to the deleted user. // No need to send it to multiplexing session: remote node will be notified separately. _, data := s.serialize(NoErrEvicted("", "", msg.Timestamp)) s.stopSession(data) } } // Read user's state from DB. func userGetState(uid types.Uid) (types.ObjState, error) { user, err := store.Users.Get(uid) if err != nil { return types.StateUndefined, err } if user == nil { return types.StateUndefined, types.ErrUserNotFound } return user.State, nil } // Subscribe or unsubscribe a single user's device to/from all FCM topics (channels). func userChannelsSubUnsub(uid types.Uid, deviceID string, sub bool) { push.ChannelSub(&push.ChannelReq{ Uid: uid, DeviceID: deviceID, Unsub: !sub, }) } // UserCacheReq contains data which mutates one or more user cache entries. type UserCacheReq struct { // Name of the node sending this request in case of cluster. Not set otherwise. Node string // UserId is set when count of unread messages is updated for a single user or // when the user is being deleted. UserId types.Uid // UserIdList is set when subscription count is updated for users of a topic. UserIdList []types.Uid // Unread count (UserId is set) Unread int // In case of set UserId: treat Unread count as an increment as opposite to the final value. // In case of set UserIdList: intement (Inc == true) or decrement subscription count by one. Inc bool // User is being deleted, remove user from cache. Gone bool // Optional push notification PushRcpt *push.Receipt } type userCacheEntry struct { unread int topics int } // Preserved update entry kept while we read the unread counter from the DB. type bufferedUpdate struct { val int inc bool } type ioResult struct { counts map[types.Uid]int err error } // Represents pending push notification receipt. type pendingReceipt struct { // Number of unread counters currently being read from the DB. pendingIOs int // The index is needed by update and is maintained by the heap.Interface methods. index int // Underlying receipt. rcpt *push.Receipt } // Pending pushes organized as a priority queue (priority = number of pending IOs). // It allows to quickly discover receipts ready for sending (num pending IOs is 0). type pendingReceiptsQueue []*pendingReceipt // Heap interface methods. func (pq pendingReceiptsQueue) Len() int { return len(pq) } func (pq pendingReceiptsQueue) Less(i, j int) bool { // We want Pop to give us the highest, not lowest, priority so we use greater than here. return pq[i].pendingIOs < pq[j].pendingIOs } func (pq pendingReceiptsQueue) Swap(i, j int) { pq[i], pq[j] = pq[j], pq[i] pq[i].index = i pq[j].index = j } func (pq *pendingReceiptsQueue) Push(x any) { n := len(*pq) item := x.(*pendingReceipt) item.index = n *pq = append(*pq, item) } func (pq *pendingReceiptsQueue) Pop() any { old := *pq n := len(old) item := old[n-1] old[n-1] = nil // avoid memory leak item.index = -1 // for safety *pq = old[0 : n-1] return item } func (pq *pendingReceiptsQueue) fix(index int) { heap.Fix(pq, index) } // Initialize users cache. func usersInit() { globals.usersUpdate = make(chan *UserCacheReq, 1024) go userUpdater() } // Shutdown users cache. func usersShutdown() { if globals.usersUpdate != nil { globals.usersUpdate <- nil } } func usersUpdateUnread(uid types.Uid, val int, inc bool) { if globals.usersUpdate == nil || (val == 0 && inc) { return } upd := &UserCacheReq{UserId: uid, Unread: val, Inc: inc} if globals.cluster.isRemoteTopic(uid.UserId()) { // Send request to remote node which owns the user. globals.cluster.routeUserReq(upd) } else { select { case globals.usersUpdate <- upd: default: } } } // Start tracking a single user. Used for cache management. // 'add' increments/decrements user's count of subscribed topics. func usersRegisterUser(uid types.Uid, add bool) { if globals.usersUpdate == nil { return } upd := &UserCacheReq{UserIdList: make([]types.Uid, 1), Inc: add} upd.UserIdList[0] = uid if globals.cluster.isRemoteTopic(uid.UserId()) { // Send request to remote node which owns the user. globals.cluster.routeUserReq(upd) } else { select { case globals.usersUpdate <- upd: default: } } } // Stop tracking user and remove him from cache. func usersRemoveUser(uid types.Uid) { if globals.usersUpdate == nil { return } upd := &UserCacheReq{UserId: uid, Gone: true} if !globals.cluster.isRemoteTopic(uid.UserId()) { select { case globals.usersUpdate <- upd: default: } } if globals.cluster != nil { // Announce to cluster even if the user is local. globals.cluster.routeUserReq(upd) } } // Account users as members of an active topic. Used for cache management. // In case of a cluster this method is called only when the topic is local: // globals.cluster.isRemoteTopic(t.name) == false func usersRegisterTopic(t *Topic, add bool) { if globals.usersUpdate == nil { return } if t.cat == types.TopicCatFnd || t.cat == types.TopicCatMe { // Ignoring me and fnd topics. return } local := &UserCacheReq{Inc: add} // In case of a cluster UIDs could be local and remote. Process local UIDs locally, // send remote UIDs to other cluster nodes for processing. The UIDs may have to be // sent to multiple nodes. remote := &UserCacheReq{Inc: add} for uid, pud := range t.perUser { if pud.isChan { // Skip channel subscribers. continue } if globals.cluster.isRemoteTopic(uid.UserId()) { remote.UserIdList = append(remote.UserIdList, uid) } else { local.UserIdList = append(local.UserIdList, uid) } } if len(remote.UserIdList) > 0 { globals.cluster.routeUserReq(remote) } if len(local.UserIdList) > 0 { select { case globals.usersUpdate <- local: default: logs.Err.Println("User cache: globals.usersUpdate queue full: ", len(globals.usersUpdate)) } } } // usersRequestFromCluster handles requests which came from other cluser nodes. func usersRequestFromCluster(req *UserCacheReq) { if globals.usersUpdate == nil { return } select { case globals.usersUpdate <- req: default: } } var usersCache map[types.Uid]userCacheEntry // The go routine for processing updates to users cache. func userUpdater() { // Caches unread counters and numbers of topics the user's subscribed to. usersCache = make(map[types.Uid]userCacheEntry) // Unread counter updates blocked by IO on per user basis. We flush them when the IO completes. perUserBuffers := make(map[types.Uid][]bufferedUpdate) // Push notification recipients blocked by IO (unread counters for some of the recipients // are being read from the database) on the per user basis. perUserPendingReceipts := make(map[types.Uid][]*pendingReceipt) // All pending push receipts organized as a priority queue by the number of pending IOs. receiptQueue := pendingReceiptsQueue{} // IO callback queue. ioDone := make(chan *ioResult, 1024) unreadUpdater := func(uids []types.Uid, vals []int, inc bool) map[types.Uid]int { var dbPending []types.Uid counts := make(map[types.Uid]int, len(uids)) for i, uid := range uids { counts[uid] = 0 uce, ok := usersCache[uid] if !ok { logs.Err.Println("ERROR: attempt to update unread count for user who has not been loaded", uid) counts[uid] = unreadUpdateError continue } val := vals[i] if uce.unread < 0 { // Unread counter not initialized yet. Maybe start a DB read? if updateBuf, ioInProgress := perUserBuffers[uid]; ioInProgress { // Buffer this update. updateBuf = append(updateBuf, bufferedUpdate{val: val, inc: inc}) perUserBuffers[uid] = updateBuf } else { // Schedule reading the counter from DB. updateBuf = []bufferedUpdate{} perUserBuffers[uid] = updateBuf dbPending = append(dbPending, uid) } counts[uid] = unreadUpdateIOPending continue } else if inc { uce.unread += val } else { uce.unread = val } usersCache[uid] = uce counts[uid] = uce.unread } if len(dbPending) > 0 { go func() { dbUnread, err := store.Users.GetUnreadCount(dbPending...) if err != nil { logs.Warn.Println("users: failed to load unread count: ", err) } ioDone <- &ioResult{counts: dbUnread, err: err} }() } return counts } for { select { case io := <-ioDone: // Unread counter read has completed. for uid, count := range io.counts { updateBuf, ok := perUserBuffers[uid] // Stop buffering updates. New updates will be handled normally. delete(perUserBuffers, uid) if io.err != nil { continue } // Update counter. if ok { for _, upd := range updateBuf { if upd.inc { count += upd.val } else { count = upd.val } } } else { logs.Warn.Println("ERROR: io didn't have an update buffer, uid", uid) } if uce, ok := usersCache[uid]; ok { if uce.unread >= 0 { logs.Warn.Println("users: unread count double initialization, uid", uid) } uce.unread = count usersCache[uid] = uce } else { logs.Warn.Println("users: missing users cache entry after IO completion, uid", uid) } // Now that the unread counter is initialized, handle pending push notification receipts. // Decrease pending IO counts in pending push receipts for this user. if pendingReceipts, ok := perUserPendingReceipts[uid]; ok { for _, pp := range pendingReceipts { pp.pendingIOs-- receiptQueue.fix(pp.index) } delete(perUserPendingReceipts, uid) } } if io.err != nil { logs.Err.Println("users: failed to read unread count:", io.err) continue } // Send ready receipts. for receiptQueue.Len() > 0 && receiptQueue[0].pendingIOs == 0 { rcpt := heap.Pop(&receiptQueue).(*pendingReceipt).rcpt for uid, rcptTo := range rcpt.To { if uce, ok := usersCache[uid]; ok && uce.unread >= 0 { rcptTo.Unread = uce.unread rcpt.To[uid] = rcptTo } } push.Push(rcpt) } case upd := <-globals.usersUpdate: if globals.shuttingDown { // If shutdown is in progress we don't care to process anything. // ignore all calls. continue } // Shutdown requested. if upd == nil { globals.usersUpdate = nil // Dont' care to close the channel. goto Exit } // Request to send push notifications. if upd.PushRcpt != nil { // List of uids for which the unread count is being read from the DB. pendingUsers := []types.Uid{} allUids := make([]types.Uid, 0, len(upd.PushRcpt.To)) allDeltas := make([]int, 0, len(upd.PushRcpt.To)) for uid, r := range upd.PushRcpt.To { allUids = append(allUids, uid) delta := 0 if r.ShouldIncrementUnreadCountInCache { delta = 1 } allDeltas = append(allDeltas, delta) } allUnread := unreadUpdater(allUids, allDeltas, true) for uid, unread := range allUnread { rcptTo := upd.PushRcpt.To[uid] // Handle update if unread >= 0 { rcptTo.Unread = unread upd.PushRcpt.To[uid] = rcptTo } else if unread == unreadUpdateIOPending { pendingUsers = append(pendingUsers, uid) } } if len(pendingUsers) == 0 { // All data present in memory. Just send the push. push.Push(upd.PushRcpt) } else { // We are waiting for IO. Add this receipt to the queues. pp := &pendingReceipt{ pendingIOs: len(pendingUsers), rcpt: upd.PushRcpt, } for _, uid := range pendingUsers { var queue []*pendingReceipt var ok bool if queue, ok = perUserPendingReceipts[uid]; !ok { queue = []*pendingReceipt{} } perUserPendingReceipts[uid] = append(queue, pp) } heap.Push(&receiptQueue, pp) } continue } // Request to add/remove user from cache. if len(upd.UserIdList) > 0 { for _, uid := range upd.UserIdList { uce, ok := usersCache[uid] if upd.Inc { if !ok { // This is a registration of a new user. // We are not loading unread count here, so set it to -1. uce.unread = -1 } uce.topics++ usersCache[uid] = uce } else if ok { if uce.topics > 1 { uce.topics-- usersCache[uid] = uce } else { // Remove user from cache delete(usersCache, uid) } } else { // BUG! logs.Err.Println("ERROR: request to unregister user which has not been registered", uid) } } continue } if upd.Gone { // User is being deleted. Don't care if there is a record. delete(usersCache, upd.UserId) continue } // Request to update unread count for one user. unreadUpdater([]types.Uid{upd.UserId}, []int{upd.Unread}, upd.Inc) } } Exit: logs.Info.Println("users: shutdown") } // garbageCollectUsers runs every 'period' and deletes up to 'blockSize' // stale unvalidated user accounts which have been last updated at least // 'minAccountAgeHours' hours. // Returns channel which can be used to stop the process. func garbageCollectUsers(period time.Duration, blockSize, minAccountAgeHours int) chan<- bool { // Unbuffered stop channel. Whomever stops the gc must wait for the process to finish. stop := make(chan bool) go func() { // Add some randomness to the tick period to desynchronize runs on cluster nodes: // 0.75 * period + rand(0, 0.5) * period. period = period - (period >> 2) + time.Duration(rand.Intn(int(period>>1))) gcTicker := time.Tick(period) logs.Info.Printf("Stale account GC started with period %s, block size %d, min account age %d hours", period.Round(time.Second), blockSize, minAccountAgeHours) staleAge := time.Hour * time.Duration(minAccountAgeHours) for { select { case <-gcTicker: if uids, err := store.Users.GetUnvalidated(time.Now().Add(-staleAge), blockSize); err == nil { if len(uids) > 0 { logs.Info.Println("Stale account GC will delete uids:", uids) for _, uid := range uids { if err = store.Users.Delete(uid, true); err != nil { logs.Warn.Printf("Stale account GC failed to delete %s: %+v", uid.UserId(), err) } } } } else { logs.Warn.Println("Stale account GC error:", err) } case <-stop: return } } }() return stop } ================================================ FILE: server/utils.go ================================================ // Generic data manipulation utilities. package main import ( "crypto/tls" "encoding/json" "errors" "fmt" "net" "path/filepath" "reflect" "regexp" "sort" "strconv" "strings" "time" "unicode" "unicode/utf8" "github.com/tinode/chat/server/auth" "github.com/tinode/chat/server/store" "github.com/tinode/chat/server/store/types" "maps" "golang.org/x/crypto/acme/autocert" ) // Tag with prefix: // * prefix starts with an ASCII letter, contains ASCII letters, numbers, from 2 to 16 chars // * tag body may contain Unicode letters and numbers, as well as the following symbols: +-.!?#@_ // Tag body can be up to maxTagLength (96) chars long. var prefixedTagRegexp = regexp.MustCompile(`^([a-z]\w{1,15}):([-_+.!?#@\pL\pN]{1,96})$`) // Generic tag: the same restrictions as tag body. var tagRegexp = regexp.MustCompile(`^[-_+.!?#@\pL\pN]{1,96}$`) const nullValue = "\u2421" // Convert database ranges into wire protocol ranges. func rangeDeserialize(in []types.Range) []MsgRange { if len(in) == 0 { return nil } out := make([]MsgRange, 0, len(in)) for _, r := range in { out = append(out, MsgRange{LowId: r.Low, HiId: r.Hi}) } return out } // Convert wire protocol ranges into database ranges. func rangeSerialize(in []MsgRange) []types.Range { if len(in) == 0 { return nil } out := make([]types.Range, 0, len(in)) for _, r := range in { out = append(out, types.Range{Low: r.LowId, Hi: r.HiId}) } return out } // stringSliceDelta extracts the slices of added and removed strings from two slices: // // added := newSlice - (oldSlice & newSlice) -- present in new but missing in old // removed := oldSlice - (oldSlice & newSlice) -- present in old but missing in new // intersection := oldSlice & newSlice -- present in both old and new func stringSliceDelta(rold, rnew []string) (added, removed, intersection []string) { if len(rold) == 0 && len(rnew) == 0 { return nil, nil, nil } if len(rold) == 0 { return rnew, nil, nil } if len(rnew) == 0 { return nil, rold, nil } sort.Strings(rold) sort.Strings(rnew) // Match old slice against the new slice and separate removed strings from added. o, n := 0, 0 lold, lnew := len(rold), len(rnew) for o < lold || n < lnew { if o == lold || (n < lnew && rold[o] > rnew[n]) { // Present in new, missing in old: added added = append(added, rnew[n]) n++ } else if n == lnew || rold[o] < rnew[n] { // Present in old, missing in new: removed removed = append(removed, rold[o]) o++ } else { // present in both intersection = append(intersection, rold[o]) if o < lold { o++ } if n < lnew { n++ } } } return added, removed, intersection } // Process credentials for correctness: remove duplicate and unknown methods. // In case of duplicate methods only the first one satisfying valueRequired is kept. // If valueRequired is true, keep only those where Value is non-empty. func normalizeCredentials(creds []MsgCredClient, valueRequired bool) []MsgCredClient { if len(creds) == 0 { return nil } index := make(map[string]*MsgCredClient) for i := range creds { c := &creds[i] if _, ok := globals.validators[c.Method]; ok && (!valueRequired || c.Value != "") { index[c.Method] = c } } creds = make([]MsgCredClient, 0, len(index)) for _, c := range index { creds = append(creds, *index[c.Method]) } return creds } // Get a string slice with methods of credentials. func credentialMethods(creds []MsgCredClient) []string { out := make([]string, len(creds)) for i := range creds { out[i] = creds[i].Method } return out } // Takes MsgClientGet query parameters, returns database query parameters func msgOpts2storeOpts(req *MsgGetOpts) *types.QueryOpt { var opts *types.QueryOpt if req != nil { opts = &types.QueryOpt{ User: types.ParseUserId(req.User), Topic: req.Topic, IfModifiedSince: req.IfModifiedSince, Limit: req.Limit, Since: req.SinceId, Before: req.BeforeId, IdRanges: rangeSerialize(req.IdRanges), } } return opts } // Check if the interface contains a string with a single Unicode Del control character. func isNullValue(i any) bool { if str, ok := i.(string); ok { return str == nullValue } return false } func decodeStoreError(err error, id string, ts time.Time, params map[string]any) *ServerComMessage { return decodeStoreErrorExplicitTs(err, id, "", ts, ts, params) } func decodeStoreErrorExplicitTs(err error, id, topic string, serverTs, incomingReqTs time.Time, params map[string]any) *ServerComMessage { var errmsg *ServerComMessage if err == nil { errmsg = NoErrExplicitTs(id, topic, serverTs, incomingReqTs) } else if storeErr, ok := err.(types.StoreError); !ok { errmsg = ErrUnknownExplicitTs(id, topic, serverTs, incomingReqTs) } else { switch storeErr { case types.ErrInternal: errmsg = ErrUnknownExplicitTs(id, topic, serverTs, incomingReqTs) case types.ErrMalformed: errmsg = ErrMalformedExplicitTs(id, topic, serverTs, incomingReqTs) case types.ErrFailed: errmsg = ErrAuthFailed(id, topic, serverTs, incomingReqTs) case types.ErrPermissionDenied: errmsg = ErrPermissionDeniedExplicitTs(id, topic, serverTs, incomingReqTs) case types.ErrDuplicate: errmsg = ErrDuplicateCredential(id, topic, serverTs, incomingReqTs) case types.ErrUnsupported: errmsg = ErrNotImplemented(id, topic, serverTs, incomingReqTs) case types.ErrExpired: errmsg = ErrAuthFailed(id, topic, serverTs, incomingReqTs) case types.ErrPolicy: errmsg = ErrPolicyExplicitTs(id, topic, serverTs, incomingReqTs) case types.ErrCredentials: errmsg = InfoValidateCredentialsExplicitTs(id, serverTs, incomingReqTs) case types.ErrUserNotFound: errmsg = ErrUserNotFound(id, topic, serverTs, incomingReqTs) case types.ErrTopicNotFound: errmsg = ErrTopicNotFound(id, topic, serverTs, incomingReqTs) case types.ErrNotFound: errmsg = ErrNotFoundExplicitTs(id, topic, serverTs, incomingReqTs) case types.ErrInvalidResponse: errmsg = ErrInvalidResponse(id, topic, serverTs, incomingReqTs) case types.ErrRedirected: errmsg = InfoUseOther(id, topic, params["topic"].(string), serverTs, incomingReqTs) default: errmsg = ErrUnknownExplicitTs(id, topic, serverTs, incomingReqTs) } } if params != nil { errmsg.Ctrl.Params = params } return errmsg } // Helper function to select access mode for the given auth level func selectAccessMode(authLvl auth.Level, anonMode, authMode, rootMode types.AccessMode) types.AccessMode { switch authLvl { case auth.LevelNone: return types.ModeNone case auth.LevelAnon: return anonMode case auth.LevelAuth: return authMode case auth.LevelRoot: return rootMode default: return types.ModeNone } } // Get default modeWant for the given topic category func getDefaultAccess(cat types.TopicCat, authUser, isChan bool) types.AccessMode { if !authUser { return types.ModeNone } switch cat { case types.TopicCatP2P: return globals.typesModeCP2P case types.TopicCatFnd: return types.ModeNone case types.TopicCatGrp: if isChan { return types.ModeCChnWriter } return types.ModeCPublic case types.TopicCatMe: return types.ModeCMeFnd case types.TopicCatSlf: return types.ModeCSelf default: panic("Unknown topic category") } } // Parse topic access parameters func parseTopicAccess(acs *MsgDefaultAcsMode, defAuth, defAnon types.AccessMode) (authMode, anonMode types.AccessMode, err error) { authMode, anonMode = defAuth, defAnon if acs.Auth != "" { err = authMode.UnmarshalText([]byte(acs.Auth)) } if acs.Anon != "" { err = anonMode.UnmarshalText([]byte(acs.Anon)) } return } // Parse one component of a semantic version string. func parseVersionPart(vers string) int { end := strings.IndexFunc(vers, func(r rune) bool { return !unicode.IsDigit(r) }) t := 0 var err error if end > 0 { t, err = strconv.Atoi(vers[:end]) } else if len(vers) > 0 { t, err = strconv.Atoi(vers) } if err != nil || t > 0x1fff || t <= 0 { return 0 } return t } // Parses semantic version string in the following formats: // // 1.2, 1.2abc, 1.2.3, 1.2.3-abc, v0.12.34-rc5 // // Unparceable values are replaced with zeros. func parseVersion(vers string) int { var major, minor, patch int // Maybe remove the optional "v" prefix. vers = strings.TrimPrefix(vers, "v") // We can handle 3 parts only. parts := strings.SplitN(vers, ".", 3) count := len(parts) if count > 0 { major = parseVersionPart(parts[0]) if count > 1 { minor = parseVersionPart(parts[1]) if count > 2 { patch = parseVersionPart(parts[2]) } } } return (major << 16) | (minor << 8) | patch } // Version as a base-10 number. Used by monitoring. func base10Version(hex int) int64 { major := hex >> 16 & 0xFF minor := hex >> 8 & 0xFF trailer := hex & 0xFF return int64(major*10000 + minor*100 + trailer) } func versionToString(vers int) string { str := strconv.Itoa(vers>>16) + "." + strconv.Itoa((vers>>8)&0xff) if vers&0xff != 0 { str += "-" + strconv.Itoa(vers&0xff) } return str } // Tag handling // filterTags takes a slice of tags and a map of namespaces, return a slice of namespace tags // contained in the input. // params: Tags to filter, namespaces to use as the filter. func filterTags(tags []string, namespaces map[string]bool) []string { var out []string if len(namespaces) == 0 { return out } for _, s := range tags { parts := prefixedTagRegexp.FindStringSubmatch(s) if len(parts) < 2 { continue } // [1] is the prefix. [0] is the whole tag. if namespaces[parts[1]] { out = append(out, s) } } return out } // rewriteTag attempts to match the original token against the email and telephone number. // The tag is expected to be in lowercase. // On success, it returns a slice with the original tag and the tag with the corresponding prefix. It returns an // empty slice if the tag is invalid. // TODO: consider inferring country code from user location. func rewriteTag(orig, countryCode string) []string { // Check if the tag already has a prefix e.g. basic:alice. if prefixedTagRegexp.MatchString(orig) { return []string{orig} } // Check if token can be rewritten by any of the validators param := map[string]any{"countryCode": countryCode} for name, conf := range globals.validators { if conf.addToTags { val := store.Store.GetValidator(name) if tag, _ := val.PreCheck(orig, param); tag != "" { return []string{orig, tag} } } } if tagRegexp.MatchString(orig) { return []string{orig} } // invalid generic tag return nil } // rewriteTagSlice calls rewriteTag for each slice member and return a new slice with original and converted values. func rewriteTagSlice(tags []string, countryCode string) []string { var result []string for _, tag := range tags { rewritten := rewriteTag(tag, countryCode) if len(rewritten) != 0 { result = append(result, rewritten...) } } return result } // restrictedTagsEqual checks if two sets of tags contain the same set of restricted tags: // true - same, false - different. func restrictedTagsEqual(oldTags, newTags []string, namespaces map[string]bool) bool { rold := filterTags(oldTags, namespaces) rnew := filterTags(newTags, namespaces) if len(rold) != len(rnew) { return false } sort.Strings(rold) sort.Strings(rnew) // Match old tags against the new tags. for i := range rnew { if rold[i] != rnew[i] { return false } } return true } // Trim whitespace, remove short/empty tags and duplicates, convert to lowercase, ensure // the number of tags does not exceed the maximum. func normalizeTags(src []string, maxTags int) types.StringSlice { if src == nil { return nil } // Make sure the number of tags does not exceed the maximum. // Technically it may result in fewer tags than the maximum due to empty tags and // duplicates, but that's user's fault. if len(src) > maxTags { src = src[:maxTags] } // Trim whitespace and force to lowercase. for i := range src { src[i] = strings.ToLower(strings.TrimSpace(src[i])) } // Sort tags sort.Strings(src) // Remove short, invalid tags and de-dupe keeping the order. It may result in fewer tags than could have // been if length were enforced later, but that's client's fault. var prev string var dst []string for _, curr := range src { if isNullValue(curr) { // Return non-nil empty array return make([]string, 0, 1) } // Unicode handling ucurr := []rune(curr) // Enforce length in characters, not in bytes. if len(ucurr) < minTagLength || len(ucurr) > maxTagLength || curr == prev { continue } // Make sure the tag starts with a letter or a number. if unicode.IsLetter(ucurr[0]) || unicode.IsDigit(ucurr[0]) { dst = append(dst, curr) prev = curr } } return types.StringSlice(dst) } func validateTag(tag string) (string, string) { // Check if the tag already has a prefix e.g. basic:alice. if parts := prefixedTagRegexp.FindStringSubmatch(tag); len(parts) == 3 { // Valid prefixed tag. return parts[1], parts[2] } if tagRegexp.MatchString(tag) { // Valid unprefixed tag (tag value only). return "", tag } return "", "" } // hasDuplicateNamespaceTags checks for duplication of unique NS tags. // Each namespace can have only one tag. This does not prevent tags from // being duplicate across requests, just saves an extra DB call. func hasDuplicateNamespaceTags(src []string, uniqueNS string) bool { found := map[string]bool{} for _, tag := range src { parts := prefixedTagRegexp.FindStringSubmatch(tag) if len(parts) != 3 { // Invalid tag, ignored. continue } if uniqueNS == parts[1] && found[parts[1]] { return true } found[parts[1]] = true } return false } // Parser for search queries. The query may contain non-ASCII characters, // i.e. length of string in bytes != length of string in runes. // Returns // * required tags: AND tags (at least one must be present in every result), // * optional tags // * error. func parseSearchQuery(query string) ([]string, []string, error) { const ( NONE = iota QUO // 1 AND // 2 OR // 3 END // 4 ORD // 5 ) type token struct { op int val string } type context struct { // Pre-token operand. preOp int // Post-token operand. postOp int // Inside quoted string. quo bool // Start of the current token. start int // End of the current token. end int } ctx := context{preOp: AND} var out []token var prev int query = strings.TrimSpace(query) // Split query into tokens. // i - character index into the string. // pos - rune index into the string. // w - width of the current rune in characters. for i, w, pos := 0, 0, 0; prev != END; i, pos = i+w, pos+1 { // var emit bool // Lexer: get next rune. var r rune // Ordinary character by default. curr := ORD r, w = utf8.DecodeRuneInString(query[i:]) switch { case w == 0: // Width zero: end of the string. curr = END case r == '"': // Quote opening or closing. curr = QUO case !ctx.quo: // Not inside quoted string, test for control characters. if r == ' ' || r == '\t' { // Tab or space. curr = AND } else if r == ',' { curr = OR } } if curr == QUO { if ctx.quo { // End of the quoted string. Close the quote. ctx.quo = false } else { if prev == ORD { // Reject strings like a"b return nil, nil, fmt.Errorf("missing operator at or near %d", pos) } // Start of the quoted string. Open the quote. ctx.quo = true } // Treat quoted string as ordinary. curr = ORD } // Parser: process the current lexem in context. switch curr { case OR: if ctx.postOp == OR { // More than one comma: ' , ,,' return nil, nil, fmt.Errorf("invalid operator sequence at or near %d", pos) } // Ensure context is not "and", i.e. the case like ' ,' -> ',' ctx.postOp = OR if prev == ORD { // Close the current token. ctx.end = i } case AND: if prev == ORD { // Close the current token. ctx.end = i ctx.postOp = AND } else if ctx.postOp != OR { // "and" does not change the "or" context. ctx.postOp = AND } case ORD: if prev == OR || prev == AND { // Ordinary character after a comma or a space: ' a' or ',a'. // Emit without changing the operation. emit = true } case END: if prev == ORD { // Close the current token. ctx.end = i } emit = true } if emit { if ctx.quo && curr == END { return nil, nil, fmt.Errorf("unterminated quoted string at or near %d %#v", pos, ctx) } // Emit the new token. op := ctx.preOp if ctx.postOp == OR { op = OR } start, end := ctx.start, ctx.end if query[start] == '"' && query[end-1] == '"' { start++ end-- } // Add token if non-empty. if start < end { out = append(out, token{val: strings.ToLower(query[start:end]), op: op}) } ctx.start = i ctx.preOp, ctx.postOp = ctx.postOp, NONE } prev = curr } if len(out) == 0 { return nil, nil, nil } // Convert tokens to two string slices. var and []string var or []string for _, t := range out { switch t.op { case AND: and = append(and, t.val) case OR: or = append(or, t.val) } } return and, or, nil } // Returns > 0 if v1 > v2; zero if equal; < 0 if v1 < v2 // Only Major and Minor parts are compared, the trailer is ignored. func versionCompare(v1, v2 int) int { return (v1 >> 8) - (v2 >> 8) } func max(a, b int) int { if a > b { return a } return b } // Truncate string if it's too long. Used in logging. func truncateStringIfTooLong(s string) string { if len(s) <= 1024 { return s } return s[:1024] + "..." } // Convert relative filepath to absolute. func toAbsolutePath(base, path string) string { if filepath.IsAbs(path) { return path } return filepath.Clean(filepath.Join(base, path)) } // Detect platform from the UserAgent string. func platformFromUA(ua string) string { ua = strings.ToLower(ua) switch { case strings.Contains(ua, "reactnative"): switch { case strings.Contains(ua, "iphone"), strings.Contains(ua, "ipad"): return "ios" case strings.Contains(ua, "android"): return "android" } return "" case strings.Contains(ua, "tinodejs"): return "web" case strings.Contains(ua, "tindroid"): return "android" case strings.Contains(ua, "tinodios"): return "ios" } return "" } func parseTLSConfig(tlsEnabled bool, jsconfig json.RawMessage) (*tls.Config, error) { type tlsAutocertConfig struct { // Domains to support by autocert Domains []string `json:"domains"` // Name of directory where auto-certificates are cached, e.g. /etc/letsencrypt/live/your-domain-here CertCache string `json:"cache"` // Contact email for letsencrypt Email string `json:"email"` } type tlsConfig struct { // Flag enabling TLS Enabled bool `json:"enabled"` // Listen for connections on this address:port and redirect them to HTTPS port. RedirectHTTP string `json:"http_redirect"` // Enable Strict-Transport-Security by setting max_age > 0 StrictMaxAge int `json:"strict_max_age"` // ACME autocert config, e.g. letsencrypt.org Autocert *tlsAutocertConfig `json:"autocert"` // If Autocert is not defined, provide file names of static certificate and key CertFile string `json:"cert_file"` KeyFile string `json:"key_file"` } var config tlsConfig if jsconfig != nil { if err := json.Unmarshal(jsconfig, &config); err != nil { return nil, errors.New("http: failed to parse tls_config: " + err.Error() + "(" + string(jsconfig) + ")") } } if !tlsEnabled && !config.Enabled { return nil, nil } if config.StrictMaxAge > 0 { globals.tlsStrictMaxAge = strconv.Itoa(config.StrictMaxAge) } globals.tlsRedirectHTTP = config.RedirectHTTP // If autocert is provided, use it. if config.Autocert != nil { certManager := autocert.Manager{ Prompt: autocert.AcceptTOS, HostPolicy: autocert.HostWhitelist(config.Autocert.Domains...), Cache: autocert.DirCache(config.Autocert.CertCache), Email: config.Autocert.Email, } return certManager.TLSConfig(), nil } // Otherwise try to use static keys. cert, err := tls.LoadX509KeyPair(config.CertFile, config.KeyFile) if err != nil { return nil, err } return &tls.Config{Certificates: []tls.Certificate{cert}}, nil } // Merge source interface{} into destination interface. // If values are maps,deep-merge them. Otherwise shallow-copy. // Returns dst, true if the dst value was changed. func mergeInterfaces(dst, src any) (any, bool) { var changed bool if src == nil { return dst, changed } vsrc := reflect.ValueOf(src) switch vsrc.Kind() { case reflect.Map: if xsrc, ok := src.(map[string]any); ok { xdst, _ := dst.(map[string]any) dst, changed = mergeMaps(xdst, xsrc) } else { changed = true dst = src } case reflect.String: if vsrc.String() == nullValue { changed = dst != nil dst = nil } else { changed = true dst = src } default: changed = true dst = src } return dst, changed } // Deep copy maps. func mergeMaps(dst, src map[string]any) (map[string]any, bool) { var changed bool if len(src) == 0 { return dst, changed } if dst == nil { dst = make(map[string]any) } for key, val := range src { xval := reflect.ValueOf(val) switch xval.Kind() { case reflect.Map: if xsrc, _ := val.(map[string]any); xsrc != nil { // Deep-copy map[string]any xdst, _ := dst[key].(map[string]any) var lchange bool dst[key], lchange = mergeMaps(xdst, xsrc) changed = changed || lchange } else if val != nil { // The map is shallow-copied if it's not of the type map[string]any dst[key] = val changed = true } case reflect.String: changed = true if xval.String() == nullValue { delete(dst, key) } else if val != nil { dst[key] = val } default: if val != nil { dst[key] = val changed = true } } } return dst, changed } // Shallow copy of a map func copyMap(src map[string]any) map[string]any { dst := make(map[string]any, len(src)) maps.Copy(dst, src) return dst } // netListener creates net.Listener for tcp and unix domains: // if addr is in the form "unix:/run/tinode.sock" it's a unix socket, otherwise TCP host:port. func netListener(addr string) (net.Listener, error) { addrParts := strings.SplitN(addr, ":", 2) if len(addrParts) == 2 && addrParts[0] == "unix" { return net.Listen("unix", addrParts[1]) } return net.Listen("tcp", addr) } // Check if specified address is a unix socket like "unix:/run/tinode.sock". func isUnixAddr(addr string) bool { addrParts := strings.SplitN(addr, ":", 2) return len(addrParts) == 2 && addrParts[0] == "unix" } var privateIPBlocks []*net.IPNet func isRoutableIP(ipStr string) bool { ip := net.ParseIP(ipStr) if ip == nil { return false } if ip.IsLoopback() || ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() { return false } if privateIPBlocks == nil { for _, cidr := range []string{ "10.0.0.0/8", // RFC1918 "172.16.0.0/12", // RFC1918 "192.168.0.0/16", // RFC1918 "fc00::/7", // RFC4193, IPv6 unique local addr } { _, block, _ := net.ParseCIDR(cidr) privateIPBlocks = append(privateIPBlocks, block) } } for _, block := range privateIPBlocks { if block.Contains(ip) { return false } } return true } ================================================ FILE: server/utils_test.go ================================================ package main import ( "reflect" "strings" "testing" "github.com/tinode/chat/server/store/types" ) func slicesEqual(expected, gotten []string) bool { if len(expected) != len(gotten) { return false } for i, v := range expected { if v != gotten[i] { return false } } return true } func expectSlicesEqual(t *testing.T, name string, expected, gotten []string) { if !slicesEqual(expected, gotten) { e := "'" + strings.Join(expected, "','") + "'" g := "'" + strings.Join(gotten, "','") + "'" t.Errorf("%s: expected %+v, got %+v", name, e, g) } } func TestStringSliceDelta(t *testing.T) { // Case format: // - inputs: old, new // - expected outputs: added, removed, intersection cases := [][5][]string{ { {"abc", "def", "fff"}, {}, {}, {"abc", "def", "fff"}, {}, }, { {}, {}, {}, {}, {}, }, { {"aa", "xx", "bb", "aa", "bb"}, {"yy", "aa"}, {"yy"}, {"aa", "bb", "bb", "xx"}, {"aa"}, }, { {"bb", "aa", "bb"}, {"yy", "aa", "bb", "zzz", "zzz", "cc"}, {"cc", "yy", "zzz", "zzz"}, {"bb"}, {"aa", "bb"}, }, { {"aa", "aa", "aa"}, {"aa", "aa", "aa"}, {}, {}, {"aa", "aa", "aa"}, }, } for _, tc := range cases { added, removed, both := stringSliceDelta( tc[0], tc[1], ) expectSlicesEqual(t, "added", tc[2], added) expectSlicesEqual(t, "removed", tc[3], removed) expectSlicesEqual(t, "both", tc[4], both) } } func TestParseSearchQuery(t *testing.T) { cases := []struct { query string expectedAnd []string expectedOr []string expectError bool }{ { query: `tag1 tag2 tag3`, expectedAnd: []string{"tag1", "tag2", "tag3"}, expectedOr: []string{}, expectError: false, }, { query: `tag1,tag2,tag3`, expectedAnd: []string{}, expectedOr: []string{"tag1", "tag2", "tag3"}, expectError: false, }, { query: `tag1 tag2,tag3`, expectedAnd: []string{"tag1"}, expectedOr: []string{"tag2", "tag3"}, expectError: false, }, { query: `tag1 tag2,tag3 "tag4 tag5"`, expectedAnd: []string{"tag1", "tag4 tag5"}, expectedOr: []string{"tag2", "tag3"}, expectError: false, }, { query: `tag1,tag2 tag3`, expectedAnd: []string{"tag3"}, expectedOr: []string{"tag1", "tag2"}, expectError: false, }, { query: `"tag1 tag2" tag3,tag4`, expectedAnd: []string{"tag1 tag2"}, expectedOr: []string{"tag3", "tag4"}, expectError: false, }, { query: `tag1 "tag2 tag3"`, expectedAnd: []string{"tag1", "tag2 tag3"}, expectedOr: []string{}, expectError: false, }, { query: `tag1, tag2, tag3`, expectedAnd: []string{}, expectedOr: []string{"tag1", "tag2", "tag3"}, expectError: false, }, { query: `tag1 , tag2 ,tag3`, expectedAnd: []string{}, expectedOr: []string{"tag1", "tag2", "tag3"}, expectError: false, }, { query: `tag1 , tag2 tag3`, expectedAnd: []string{"tag3"}, expectedOr: []string{"tag1", "tag2"}, expectError: false, }, { query: `tag1 "unterminated quote`, expectedAnd: nil, expectedOr: nil, expectError: true, }, { query: `tag1,,tag2`, expectedAnd: nil, expectedOr: nil, expectError: true, }, { query: `tag1 "tag2" tag3`, expectedAnd: []string{"tag1", "tag2", "tag3"}, expectedOr: []string{}, expectError: false, }, { query: `tag1"tag2" quote in the middle`, expectedAnd: nil, expectedOr: nil, expectError: true, }, } for _, tc := range cases { and, or, err := parseSearchQuery(tc.query) if tc.expectError { if err == nil { t.Errorf("expected error for query '%s', got none", tc.query) } } else { if err != nil { t.Errorf("unexpected error for query '%s': %v", tc.query, err) } else { expectSlicesEqual(t, tc.query+" AND", tc.expectedAnd, and) expectSlicesEqual(t, tc.query+" OR", tc.expectedOr, or) } } } } func TestNormalizeTags(t *testing.T) { cases := []struct { input []string expected types.StringSlice }{ { input: []string{" Tag1 ", "tag2", "TAG3", "tag1"}, expected: types.StringSlice{"tag1", "tag2", "tag3"}, }, { input: []string{" ", "tag2", "TAG3", "tag1"}, expected: types.StringSlice{"tag1", "tag2", "tag3"}, }, { input: []string{"tag1"}, expected: types.StringSlice{"tag1"}, }, { input: []string{"tag1", nullValue}, expected: []string{}, }, { input: []string{}, expected: nil, }, } for _, tc := range cases { got := normalizeTags(tc.input, 16) expectSlicesEqual(t, "normalizeTags", tc.expected, got) } } func TestRestrictedTagsEqual(t *testing.T) { cases := []struct { oldTags []string newTags []string namespaces map[string]bool expected bool }{ { oldTags: []string{"ns1:tag1", "ns2:tag2"}, newTags: []string{"ns1:tag1", "ns2:tag2"}, namespaces: map[string]bool{"ns1": true, "ns2": true}, expected: true, }, { oldTags: []string{"ns1:tag1", "ns2:tag2"}, newTags: []string{"ns1:tag1", "ns2:tag3"}, namespaces: map[string]bool{"ns1": true, "ns2": true}, expected: false, }, { oldTags: []string{"ns1:tag1", "ns2:tag2"}, newTags: []string{"ns1:tag1"}, namespaces: map[string]bool{"ns1": true, "ns2": true}, expected: false, }, } for _, tc := range cases { got := restrictedTagsEqual(tc.oldTags, tc.newTags, tc.namespaces) if got != tc.expected { t.Errorf("restrictedTagsEqual: expected %v, got %v", tc.expected, got) } } } func TestIsNullValue(t *testing.T) { cases := []struct { input any expected bool }{ { input: nullValue, expected: true, }, { input: "some string", expected: false, }, { input: 123, expected: false, }, { input: nil, expected: false, }, } for _, tc := range cases { got := isNullValue(tc.input) if got != tc.expected { t.Errorf("isNullValue: expected %v, got %v", tc.expected, got) } } } func TestParseVersion(t *testing.T) { cases := []struct { input string expected int }{ { input: "1.2.3", expected: (1 << 16) | (2 << 8) | 3, }, { input: "1.2", expected: (1 << 16) | (2 << 8), }, { input: "1", expected: (1 << 16), }, { input: "v1.2.3", expected: (1 << 16) | (2 << 8) | 3, }, { input: "v1.2", expected: (1 << 16) | (2 << 8), }, { input: "v1", expected: (1 << 16), }, { input: "1.2.3-rc1", expected: (1 << 16) | (2 << 8) | 3, }, { input: "v1.2.3-rc1", expected: (1 << 16) | (2 << 8) | 3, }, { input: "0.0.0", expected: 0, }, { input: "v0.0.0", expected: 0, }, { input: "1.2.8192", // 8192 is greater than 0x1fff, should be ignored expected: (1 << 16) | (2 << 8), }, { input: "1.8192.3", // 8192 is greater than 0x1fff, should be ignored expected: (1 << 16) | 3, }, { input: "8192.2.3", // 8192 is greater than 0x1fff, should be ignored expected: (2 << 8) | 3, }, { input: "", expected: 0, }, } for _, tc := range cases { got := parseVersion(tc.input) if got != tc.expected { t.Errorf("parseVersion(%q): expected %d, got %d", tc.input, tc.expected, got) } } } func TestMergeMaps(t *testing.T) { cases := []struct { dst map[string]any src map[string]any expected map[string]any changed bool }{ { dst: map[string]any{"a": 1, "b": 2}, src: map[string]any{"b": 3, "c": 4}, expected: map[string]any{"a": 1, "b": 3, "c": 4}, changed: true, }, { dst: map[string]any{"a": 1, "b": map[string]any{"x": 1}}, src: map[string]any{"b": map[string]any{"y": 2}}, expected: map[string]any{"a": 1, "b": map[string]any{"x": 1, "y": 2}}, changed: true, }, { dst: map[string]any{"a": 1, "b": map[string]any{"x": 1}}, src: map[string]any{"b": map[string]any{"x": nullValue}}, expected: map[string]any{"a": 1, "b": map[string]any{}}, changed: true, }, { dst: map[string]any{"a": 1, "b": 2}, src: map[string]any{}, expected: map[string]any{"a": 1, "b": 2}, changed: false, }, { dst: nil, src: map[string]any{"a": 1}, expected: map[string]any{"a": 1}, changed: true, }, { dst: map[string]any{"a": 1}, src: map[string]any{"a": nullValue}, expected: map[string]any{}, changed: true, }, } for _, tc := range cases { got, changed := mergeMaps(tc.dst, tc.src) if !reflect.DeepEqual(got, tc.expected) || changed != tc.changed { t.Errorf("mergeMaps(%v, %v): expected (%v, %v), got (%v, %v)", tc.dst, tc.src, tc.expected, tc.changed, got, changed) } } } func TestMergeInterfaces(t *testing.T) { cases := []struct { dst any src any expected any changed bool }{ { dst: map[string]any{"a": 1, "b": map[string]any{"x": 1}}, src: map[string]any{"b": map[string]any{"y": 2}}, expected: map[string]any{"a": 1, "b": map[string]any{"x": 1, "y": 2}}, changed: true, }, { dst: map[string]any{"a": 1, "b": map[string]any{"x": 1}}, src: map[string]any{"b": map[string]any{"x": nullValue}}, expected: map[string]any{"a": 1, "b": map[string]any{}}, changed: true, }, { dst: map[string]any{"a": 1}, src: nullValue, expected: nil, changed: true, }, { dst: "old string", src: "new string", expected: "new string", changed: true, }, { dst: "old string", src: 12345, expected: 12345, changed: true, }, { dst: "old string", src: nullValue, expected: nil, changed: true, }, { dst: 123, src: 456, expected: 456, changed: true, }, { dst: 123, src: nil, expected: 123, changed: false, }, { dst: 123, src: true, expected: true, changed: true, }, { dst: []string{"a", "b", "c"}, src: []string{"d", "e", "f"}, expected: []string{"d", "e", "f"}, changed: true, }, } for _, tc := range cases { got, changed := mergeInterfaces(tc.dst, tc.src) if !reflect.DeepEqual(got, tc.expected) || changed != tc.changed { t.Errorf("mergeInterfaces(%v, %v): expected (%v, %v), got (%v, %v)", tc.dst, tc.src, tc.expected, tc.changed, got, changed) } } } func TestFilterTags(t *testing.T) { cases := []struct { tags []string ns map[string]bool expected []string }{ { tags: []string{"ns1:tag1", "ns2:tag3", "nons", "inval::tag", ":tag3", "tag4:", "tag5: "}, ns: map[string]bool{"ns1": true, "ns2": false, "xtra": true}, expected: []string{"ns1:tag1"}, }, { tags: []string{"ns1:tag1", "ns2:tag3", "nons", "inval::tag", ":tag3", "tag4:", "tag5: "}, ns: map[string]bool{}, expected: nil, }, } for _, tc := range cases { got := filterTags(tc.tags, tc.ns) if !reflect.DeepEqual(got, tc.expected) { t.Errorf("filterTags(%v, %v): expected (%v), got (%v)", tc.tags, tc.ns, tc.expected, got) } } } func TestHasDuplicateNamespaceTags(t *testing.T) { cases := []struct { tags []string ns string expected bool }{ { tags: []string{"ns1:tag1", "ns2:tag3", "nons", "inval::tag", ":tag3", "tag4:", "tag5: "}, ns: "ns1", expected: false, }, { tags: []string{"ns1:tag1", "ns2:tag3", "nons", "inval::tag", ":tag3", "tag4:", "tag5: ", "ns1:tag2"}, ns: "ns1", expected: true, }, { tags: []string{"ns1:tag1", "ns2:tag3", "nons", "inval::tag", ":tag3", "tag4:", "tag5: "}, ns: "", expected: false, }, } for _, tc := range cases { got := hasDuplicateNamespaceTags(tc.tags, tc.ns) if !reflect.DeepEqual(got, tc.expected) { t.Errorf("filterTags(%v, %v): expected (%v), got (%v)", tc.tags, tc.ns, tc.expected, got) } } } ================================================ FILE: server/validate/email/validate.go ================================================ // Package email is a credential validator which uses an external SMTP server. package email import ( "bytes" crand "crypto/rand" "crypto/tls" "encoding/base64" "encoding/json" "errors" "fmt" "math/big" "math/rand" "mime" qp "mime/quotedprintable" "net/mail" "net/smtp" "net/url" "strconv" "strings" textt "text/template" "slices" "github.com/tinode/chat/server/logs" "github.com/tinode/chat/server/store" t "github.com/tinode/chat/server/store/types" "github.com/tinode/chat/server/validate" i18n "golang.org/x/text/language" ) // Validator configuration. type validator struct { // Base URL of the web client. HostUrl string `json:"host_url"` // List of languages supported by templates. Languages []string `json:"languages"` // Path to email validation templates, either a template itself or a literal string. ValidationTemplFile string `json:"validation_templ"` // Path to templates for resetting the authentication secret. ResetTemplFile string `json:"reset_secret_templ"` // Sender RFC 5322 email address. SendFrom string `json:"sender"` // Login to use for SMTP authentication. Login string `json:"login"` // Password to use for SMTP authentication. SenderPassword string `json:"sender_password"` // Authentication mechanism to use, optional. One of "login", "md5", "plain" (default). AuthMechanism string `json:"auth_mechanism"` // Optional response which bypasses the validation. DebugResponse string `json:"debug_response"` // Number of validation attempts before email is locked. MaxRetries int `json:"max_retries"` // Address of the SMTP server. SMTPAddr string `json:"smtp_server"` // Port of the SMTP server. SMTPPort string `json:"smtp_port"` // ServerName used in SMTP HELO/EHLO command. SMTPHeloHost string `json:"smtp_helo_host"` // Skip verification of the server's certificate chain and host name. // In this mode, TLS is susceptible to machine-in-the-middle attacks. TLSInsecureSkipVerify bool `json:"insecure_skip_verify"` // Optional whitelist of email domains accepted for registration. Domains []string `json:"domains"` // Length of secret numeric code to sent for validation. CodeLength int `json:"code_length"` // Must use index into language array instead of language tags because language.Matcher is brain damaged: // https://github.com/golang/go/issues/24211 validationTempl []*textt.Template resetTempl []*textt.Template auth smtp.Auth senderEmail string langMatcher i18n.Matcher maxCodeValue *big.Int } const ( validatorName = "email" defaultMaxRetries = 3 defaultPort = "25" // Technically email could be up to 255 bytes long but practically 128 is enough. maxEmailLength = 128 // Default code length when one is not provided in the config defaultCodeLength = 6 ) // Email template parts var templateParts = []string{"subject", "body_plain", "body_html"} // Init: initialize validator. func (v *validator) Init(jsonconf string) error { if err := json.Unmarshal([]byte(jsonconf), v); err != nil { return err } sender, err := mail.ParseAddress(v.SendFrom) if err != nil { return err } v.senderEmail = sender.Address // Enable auth if login is provided. if v.Login != "" { mechanism := strings.ToLower(v.AuthMechanism) switch mechanism { case "cram-md5": v.auth = smtp.CRAMMD5Auth(v.Login, v.SenderPassword) case "login": v.auth = &loginAuth{[]byte(v.Login), []byte(v.SenderPassword)} case "", "plain": v.auth = smtp.PlainAuth("", v.Login, v.SenderPassword, v.SMTPAddr) default: return errors.New("unknown auth_mechanism") } } // Optionally resolve paths. v.ValidationTemplFile, err = validate.ResolveTemplatePath(v.ValidationTemplFile) if err != nil { return err } v.ResetTemplFile, err = validate.ResolveTemplatePath(v.ResetTemplFile) if err != nil { return err } // Paths to templates could be templates themselves: they may be language-dependent. var validationPathTempl, resetPathTempl *textt.Template validationPathTempl, err = textt.New("validation").Parse(v.ValidationTemplFile) if err != nil { return err } resetPathTempl, err = textt.New("reset").Parse(v.ResetTemplFile) if err != nil { return err } var path string if len(v.Languages) > 0 { v.validationTempl = make([]*textt.Template, len(v.Languages)) v.resetTempl = make([]*textt.Template, len(v.Languages)) var langTags []i18n.Tag // Find actual content templates for each defined language. for idx, lang := range v.Languages { tag, err := i18n.Parse(lang) if err != nil { return err } langTags = append(langTags, tag) if v.validationTempl[idx], path, err = validate.ReadTemplateFile(validationPathTempl, lang); err != nil { return err } if err = isTemplateValid(v.validationTempl[idx]); err != nil { return fmt.Errorf("parsing %s: %w", path, err) } if v.resetTempl[idx], path, err = validate.ReadTemplateFile(resetPathTempl, lang); err != nil { return err } if err = isTemplateValid(v.resetTempl[idx]); err != nil { return fmt.Errorf("parsing %s: %w", path, err) } } v.langMatcher = i18n.NewMatcher(langTags) } else { v.validationTempl = make([]*textt.Template, 1) v.resetTempl = make([]*textt.Template, 1) // No i18n support. Use defaults. v.validationTempl[0], path, err = validate.ReadTemplateFile(validationPathTempl, "") if err != nil { return err } if err = isTemplateValid(v.validationTempl[0]); err != nil { return fmt.Errorf("parsing %s: %w", path, err) } v.resetTempl[0], path, err = validate.ReadTemplateFile(resetPathTempl, "") if err != nil { return err } if err = isTemplateValid(v.resetTempl[0]); err != nil { return fmt.Errorf("parsing %s: %w", path, err) } } if v.HostUrl, err = validate.ValidateHostURL(v.HostUrl); err != nil { return err } if v.SMTPHeloHost == "" { hostUrl, _ := url.Parse(v.HostUrl) v.SMTPHeloHost = hostUrl.Hostname() } if v.SMTPHeloHost == "" { return errors.New("missing SMTP host") } if v.MaxRetries == 0 { v.MaxRetries = defaultMaxRetries } if v.CodeLength == 0 { v.CodeLength = defaultCodeLength } v.maxCodeValue = big.NewInt(0).Exp(big.NewInt(10), big.NewInt(int64(v.CodeLength)), nil) if v.SMTPPort == "" { v.SMTPPort = defaultPort } return nil } // IsInitialized returns true if the validator is initialized. func (v *validator) IsInitialized() bool { return v.SMTPHeloHost != "" } // PreCheck validates the credential and parameters without sending an email. // If the credential is valid, it's returned with an appropriate prefix. func (v *validator) PreCheck(cred string, _ map[string]any) (string, error) { if len(cred) > maxEmailLength { return "", t.ErrMalformed } // The email must be plain user@domain. addr, err := mail.ParseAddress(cred) if err != nil || addr.Address != cred { return "", t.ErrMalformed } // Normalize email to make sure Unicode case collisions don't lead to security problems. addr.Address = strings.ToLower(addr.Address) // If a whitelist of domains is provided, make sure the email belongs to the list. if len(v.Domains) > 0 { // Parse email into user and domain parts. parts := strings.Split(addr.Address, "@") if len(parts) != 2 { return "", t.ErrMalformed } if !slices.Contains(v.Domains, parts[1]) { return "", t.ErrPolicy } } return validatorName + ":" + addr.Address, nil } // Send a request for confirmation to the user: makes a record in DB and nothing else. func (v *validator) Request(user t.Uid, email, lang, resp string, tmpToken []byte) (bool, error) { // Email validator cannot accept an immediate response. if resp != "" { return false, t.ErrFailed } // Normalize email to make sure Unicode case collisions don't lead to security problems. email = strings.ToLower(email) token := make([]byte, base64.StdEncoding.EncodedLen(len(tmpToken))) base64.StdEncoding.Encode(token, tmpToken) // Generate expected response as a random numeric string between 0 and 999999. code, err := crand.Int(crand.Reader, v.maxCodeValue) if err != nil { return false, err } resp = strconv.FormatInt(code.Int64(), 10) resp = strings.Repeat("0", v.CodeLength-len(resp)) + resp var template *textt.Template if v.langMatcher != nil { // Find the template for the requested language. // Make sure the language tag is standardized. Matcher is a bit dumber than Parse(). normalized, _ := i18n.Parse(lang) // The matched tag is usually not in the list of available languages (e.g. es_ES -> es-u-rg-eszzzz). // Use index to find the template instead of tag. _, idx := i18n.MatchStrings(v.langMatcher, normalized.String()) template = v.validationTempl[idx] } else { template = v.validationTempl[0] } content, err := validate.ExecuteTemplate(template, templateParts, map[string]any{ "Token": url.QueryEscape(string(token)), "Code": resp, "HostUrl": v.HostUrl}) if err != nil { return false, err } // Create or update validation record in DB. isNew, err := store.Users.UpsertCred(&t.Credential{ User: user.String(), Method: validatorName, Value: email, Resp: resp}) if err != nil { return false, err } // Send email without blocking. Email sending may take long time. go v.send(email, content) return isNew, nil } // ResetSecret sends a message with instructions for resetting an authentication secret. func (v *validator) ResetSecret(email, scheme, lang string, code []byte, params map[string]any) error { // Normalize email to make sure Unicode case collisions don't lead to security problems. email = strings.ToLower(email) var template *textt.Template if v.langMatcher != nil { _, idx := i18n.MatchStrings(v.langMatcher, lang) template = v.resetTempl[idx] } else { template = v.resetTempl[0] } var login string if params != nil { // Invariant: params["login"] is a string. Will panic if the invariant doesn't hold. login = params["login"].(string) } content, err := validate.ExecuteTemplate(template, templateParts, map[string]any{ "Login": login, "Code": string(code), "Cred": email, "Scheme": scheme, "HostUrl": v.HostUrl}) if err != nil { return err } // Send email without blocking. Email sending may take long time. go v.send(email, content) return nil } // Check checks if the provided validation response matches the expected response. // Returns the value of validated credential on success. func (v *validator) Check(user t.Uid, resp string) (string, error) { cred, err := store.Users.GetActiveCred(user, validatorName) if err != nil { return "", err } if cred == nil { // Request to validate non-existent credential. return "", t.ErrNotFound } if cred.Retries > v.MaxRetries { return "", t.ErrPolicy } if resp == "" { return "", t.ErrCredentials } // Comparing with dummy response too. if cred.Resp == resp || v.DebugResponse == resp { // Valid response, save confirmation. return cred.Value, store.Users.ConfirmCred(user, validatorName) } // Invalid response, increment fail counter, ignore possible error. store.Users.FailCred(user, validatorName) return "", t.ErrCredentials } // Delete deletes user's records. func (v *validator) Delete(user t.Uid) error { return store.Users.DelCred(user, validatorName, "") } // Remove deactivates or removes user's credential. func (v *validator) Remove(user t.Uid, value string) error { return store.Users.DelCred(user, validatorName, value) } // TempAuthScheme returns a temporary authentication method used by this validator. func (v *validator) TempAuthScheme() (string, error) { return "code", nil } // SendMail replacement func (v *validator) sendMail(rcpt []string, msg []byte) error { client, err := smtp.Dial(v.SMTPAddr + ":" + v.SMTPPort) if err != nil { return err } defer client.Close() if err = client.Hello(v.SMTPHeloHost); err != nil { return err } if istls, _ := client.Extension("STARTTLS"); istls { tlsConfig := &tls.Config{ InsecureSkipVerify: v.TLSInsecureSkipVerify, ServerName: v.SMTPAddr, } if err = client.StartTLS(tlsConfig); err != nil { return err } } if v.auth != nil { if isauth, _ := client.Extension("AUTH"); isauth { err = client.Auth(v.auth) if err != nil { return err } } } if err = client.Mail(strings.ReplaceAll(strings.ReplaceAll(v.senderEmail, "\r", " "), "\n", " ")); err != nil { return err } for _, to := range rcpt { if err = client.Rcpt(strings.ReplaceAll(strings.ReplaceAll(to, "\r", " "), "\n", " ")); err != nil { return err } } w, err := client.Data() if err != nil { return err } if _, err = w.Write(msg); err != nil { return err } if err = w.Close(); err != nil { return err } return client.Quit() } // This is a basic SMTP sender which connects to a server using login/password. // - // See here how to send email from Amazon SES: // https://docs.aws.amazon.com/sdk-for-go/api/service/ses/#example_SES_SendEmail_shared00 // - // Mailjet and SendGrid have some free email limits. func (v *validator) send(to string, content map[string]string) error { message := &bytes.Buffer{} // Common headers. fmt.Fprintf(message, "From: %s\r\n", v.SendFrom) fmt.Fprintf(message, "To: %s\r\n", to) message.WriteString("Subject: ") // Old email clients may barf on UTF-8 strings. // Encode as quoted printable with 75-char strings separated by spaces, split by spaces, reassemble. message.WriteString(strings.Join(strings.Split(mime.QEncoding.Encode("utf-8", content["subject"]), " "), "\r\n ")) message.WriteString("\r\n") message.WriteString("MIME-version: 1.0;\r\n") if content["body_html"] == "" { // Plain text message message.WriteString("Content-Type: text/plain; charset=\"UTF-8\"; format=flowed; delsp=yes\r\n") message.WriteString("Content-Transfer-Encoding: base64\r\n\r\n") b64w := base64.NewEncoder(base64.StdEncoding, message) b64w.Write([]byte(content["body_plain"])) b64w.Close() } else if content["body_plain"] == "" { // HTML-formatted message message.WriteString("Content-Type: text/html; charset=\"UTF-8\"\r\n") message.WriteString("Content-Transfer-Encoding: quoted-printable\r\n\r\n") qpw := qp.NewWriter(message) qpw.Write([]byte(content["body_html"])) qpw.Close() } else { // Multipart-alternative message includes both HTML and plain text components. boundary := randomBoundary() message.WriteString("Content-Type: multipart/alternative; boundary=\"" + boundary + "\"\r\n\r\n") message.WriteString("--" + boundary + "\r\n") message.WriteString("Content-Type: text/plain; charset=\"UTF-8\"; format=flowed; delsp=yes\r\n") message.WriteString("Content-Transfer-Encoding: base64\r\n\r\n") b64w := base64.NewEncoder(base64.StdEncoding, message) b64w.Write([]byte(content["body_plain"])) b64w.Close() message.WriteString("\r\n") message.WriteString("--" + boundary + "\r\n") message.WriteString("Content-Type: text/html; charset=\"UTF-8\"\r\n") message.WriteString("Content-Transfer-Encoding: quoted-printable\r\n\r\n") qpw := qp.NewWriter(message) qpw.Write([]byte(content["body_html"])) qpw.Close() message.WriteString("\r\n--" + boundary + "--") } message.WriteString("\r\n") err := v.sendMail([]string{to}, message.Bytes()) if err != nil { logs.Warn.Println("SMTP error", to, err) } return err } // Check if the template contains all required parts. func isTemplateValid(templ *textt.Template) error { if templ.Lookup("subject") == nil { return fmt.Errorf("template invalid: '%s' not found", "subject") } if templ.Lookup("body_plain") == nil && templ.Lookup("body_html") == nil { return fmt.Errorf("template invalid: neither of '%s', '%s' is found", "body_plain", "body_html") } return nil } type loginAuth struct { username, password []byte } // Start begins an authentication with a server. Exported only to satisfy the interface definition. func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) { return "LOGIN", []byte(a.username), nil } // Next continues the authentication. Exported only to satisfy the interface definition. func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) { if more { switch strings.ToLower(string(fromServer)) { case "username:": return a.username, nil case "password:": return a.password, nil default: return nil, fmt.Errorf("LOGIN AUTH unknown server response '%s'", string(fromServer)) } } return nil, nil } func randomBoundary() string { var buf [24]byte rand.Read(buf[:]) return fmt.Sprintf("tinode--%x", buf[:]) } func init() { store.RegisterValidator(validatorName, &validator{}) } ================================================ FILE: server/validate/tel/twilio.go ================================================ package tel import ( "encoding/json" "github.com/twilio/twilio-go" twapi "github.com/twilio/twilio-go/rest/api/v2010" ) type twilioConfig struct { AccountSid string `json:"account_sid"` AuthToken string `json:"auth_token"` } var twilioClient *twilio.RestClient func twilioInit(jsonconf json.RawMessage) error { var conf twilioConfig if err := json.Unmarshal(jsonconf, &conf); err != nil { return err } twilioClient = twilio.NewRestClientWithParams(twilio.ClientParams{ Username: conf.AccountSid, Password: conf.AuthToken, }) return nil } func twilioSend(from, to, body string) error { _, err := twilioClient.Api.CreateMessage(&twapi.CreateMessageParams{ From: &from, To: &to, Body: &body, }) return err } ================================================ FILE: server/validate/tel/validate.go ================================================ // Package tel is an incomplete implementation of SMS or voice credential validator. package tel import ( "crypto/rand" "encoding/json" "math/big" "strconv" "strings" textt "text/template" "github.com/nyaruka/phonenumbers" "github.com/tinode/chat/server/logs" "github.com/tinode/chat/server/store" t "github.com/tinode/chat/server/store/types" "github.com/tinode/chat/server/validate" i18n "golang.org/x/text/language" ) // Empty placeholder struct. type validator struct { // Base URL of the web client to tell clients. HostUrl string `json:"host_url"` // List of languages supported by templates. Languages []string `json:"languages"` // Path to email validation and password reset templates, either a template itself or a literal string. UniversalTemplFile string `json:"universal_templ"` // Sender address (phone number). Sender string `json:"sender"` // Debug response to accept during testing. DebugResponse string `json:"debug_response"` // Maximum number of validation retires. MaxRetries int `json:"max_retries"` // Length of secret numeric code to sent for validation. CodeLength int `json:"code_length"` // Twilio-specific config. Twilio json.RawMessage `json:"twilio_conf"` // Must use index into language array instead of language tags because language.Matcher is brain damaged: // https://github.com/golang/go/issues/24211 universalTempl []*textt.Template langMatcher i18n.Matcher maxCodeValue *big.Int } const ( validatorName = "tel" defaultMaxRetries = 3 // Default code length when one is not provided in the config defaultCodeLength = 6 defaultSender = "Tinode" ) func (v *validator) Init(jsonconf string) error { var err error if err = json.Unmarshal([]byte(jsonconf), v); err != nil { return err } if v.HostUrl, err = validate.ValidateHostURL(v.HostUrl); err != nil { return err } var universalPathTempl *textt.Template universalPathTempl, err = textt.New("universal").Parse(v.UniversalTemplFile) if err != nil { return err } if len(v.Languages) > 0 { v.universalTempl = make([]*textt.Template, len(v.Languages)) var langTags []i18n.Tag // Find actual content templates for each defined language. for idx, lang := range v.Languages { tag, err := i18n.Parse(lang) if err != nil { return err } langTags = append(langTags, tag) if v.universalTempl[idx], _, err = validate.ReadTemplateFile(universalPathTempl, lang); err != nil { return err } } v.langMatcher = i18n.NewMatcher(langTags) } else { v.universalTempl = make([]*textt.Template, 1) // No i18n support. Use defaults. v.universalTempl[0], _, err = validate.ReadTemplateFile(universalPathTempl, "") if err != nil { return err } } if v.Twilio != nil { if err = twilioInit(v.Twilio); err != nil { return err } } if v.Sender == "" { v.Sender = defaultSender } if v.MaxRetries == 0 { v.MaxRetries = defaultMaxRetries } if v.CodeLength == 0 { v.CodeLength = defaultCodeLength } v.maxCodeValue = big.NewInt(0).Exp(big.NewInt(10), big.NewInt(int64(v.CodeLength)), nil) return nil } // IsInitialized returns true if the validator is initialized. func (v *validator) IsInitialized() bool { return v.CodeLength > 0 } // PreCheck validates the credential and parameters without sending an SMS or making the call. // If credential is valid, it's formatted and prefixed with a tag namespace. func (*validator) PreCheck(cred string, params map[string]any) (string, error) { // Parse will try to extract the number from any text, make sure it's just the number. if !phonenumbers.VALID_PHONE_NUMBER_PATTERN.MatchString(cred) { return "", t.ErrMalformed } countryCode, ok := params["countryCode"].(string) if !ok { countryCode = "US" } number, err := phonenumbers.Parse(cred, countryCode) if err != nil { return "", t.ErrMalformed } if !phonenumbers.IsValidNumber(number) { return "", t.ErrMalformed } if numType := phonenumbers.GetNumberType(number); numType != phonenumbers.FIXED_LINE_OR_MOBILE && numType != phonenumbers.MOBILE { return "", t.ErrMalformed } return validatorName + ":" + phonenumbers.Format(number, phonenumbers.E164), nil } // Request sends a request for confirmation to the user: makes a record in DB and nothing else. func (v *validator) Request(user t.Uid, phone, lang, resp string, tmpToken []byte) (bool, error) { // Phone validator cannot accept an immediate response. if resp != "" { return false, t.ErrFailed } // Generate expected response as a random numeric string between 0 and 999999. code, err := rand.Int(rand.Reader, v.maxCodeValue) if err != nil { return false, err } resp = strconv.FormatInt(code.Int64(), 10) resp = strings.Repeat("0", v.CodeLength-len(resp)) + resp var template *textt.Template if v.langMatcher != nil { _, idx := i18n.MatchStrings(v.langMatcher, lang) template = v.universalTempl[idx] } else { template = v.universalTempl[0] } content, err := validate.ExecuteTemplate(template, nil, map[string]any{ "Code": resp, "HostUrl": v.HostUrl}) if err != nil { return false, err } // Create or update validation record in DB. isNew, err := store.Users.UpsertCred(&t.Credential{ User: user.String(), Method: validatorName, Value: phone, Resp: resp}) if err != nil { return false, err } // Send SMS without blocking. It sending may take long time. go v.send(phone, content[""]) return isNew, nil } // ResetSecret sends a message with instructions for resetting an authentication secret. func (v *validator) ResetSecret(phone, scheme, lang string, code []byte, params map[string]any) error { var template *textt.Template if v.langMatcher != nil { _, idx := i18n.MatchStrings(v.langMatcher, lang) template = v.universalTempl[idx] } else { template = v.universalTempl[0] } content, err := validate.ExecuteTemplate(template, nil, map[string]any{ "Code": string(code), "HostUrl": v.HostUrl}) if err != nil { return err } // Send SMS without blocking. Sending may take long time. go v.send(phone, content[""]) return nil } // Check checks validity of user's response. func (v *validator) Check(user t.Uid, resp string) (string, error) { cred, err := store.Users.GetActiveCred(user, validatorName) if err != nil { return "", err } if cred == nil { // Blank credential. return "", t.ErrNotFound } if cred.Retries > v.MaxRetries { return "", t.ErrPolicy } if resp == "" { return "", t.ErrCredentials } // Comparing with dummy response too. if cred.Resp == resp || v.DebugResponse == resp { // Valid response, save confirmation. return cred.Value, store.Users.ConfirmCred(user, validatorName) } // Invalid response, increment fail counter, ignore possible error. store.Users.FailCred(user, validatorName) return "", t.ErrCredentials } // Delete deletes user's records. Returns deleted credentials. func (*validator) Delete(user t.Uid) error { return store.Users.DelCred(user, validatorName, "") } // Remove or disable the given record. func (*validator) Remove(user t.Uid, value string) error { return store.Users.DelCred(user, validatorName, value) } // TempAuthScheme returns a temporary authentication method used by this validator. func (v *validator) TempAuthScheme() (string, error) { return "code", nil } // Implement sending the SMS. func (v *validator) send(to, body string) error { if v.Twilio != nil { if err := twilioSend(v.Sender, to, body); err != nil { logs.Warn.Println("Twilio SMS error", to, err) } } else { logs.Info.Println("Send SMS, To:", to, "\nText:", body) } return nil } func init() { store.RegisterValidator(validatorName, &validator{}) } ================================================ FILE: server/validate/validator.go ================================================ // Package validate defines an interface which must be implmented by credential validators. package validate import ( "bytes" "errors" "fmt" "net/url" "os" "path/filepath" "text/template" t "github.com/tinode/chat/server/store/types" ) // Validator handles validation of user's credentials, like email or phone. type Validator interface { // Init initializes the validator. Init(jsonconf string) error // IsInitialized returns true if the validator is initialized. IsInitialized() bool // PreCheck pre-validates the credential without sending an actual request for validation: // check uniqueness (if appropriate), format, etc // Returns normalized credential prefixed with an appropriate namespace prefix. PreCheck(cred string, params map[string]any) (string, error) // Request sends a request for validation to the user. Returns true if it's a new credential, // false if it re-sent request for an existing unconfirmed credential. // user: UID of the user making the request. // cred: credential being validated, such as email or phone. // lang: user's human language as repored in the session. // resp: optional response if user already has it (i.e. captcha/recaptcha). // tmpToken: temporary authentication token to include in the request. Request(user t.Uid, cred, lang, resp string, tmpToken []byte) (bool, error) // ResetSecret sends a message with instructions for resetting an authentication secret. // cred: address to use for the message. // scheme: authentication scheme being reset. // lang: human language as reported in the session. // tmpToken: temporary authentication token // params: authentication params. ResetSecret(cred, scheme, lang string, tmpToken []byte, params map[string]any) error // Check checks validity of user's response. // Returns the value of validated credential on success. Check(user t.Uid, resp string) (string, error) // Remove deletes or deactivates user's given value. Remove(user t.Uid, value string) error // Delete deletes user's record. Delete(user t.Uid) error // TempAuthScheme returns a temporary authentication method used by this validator. // It should be either "code" or "token". TempAuthScheme() (string, error) } func ValidateHostURL(origUrl string) (string, error) { hostUrl, err := url.Parse(origUrl) if err != nil { return "", err } if !hostUrl.IsAbs() { return "", errors.New("host_url must be absolute") } if hostUrl.Hostname() == "" { return "", errors.New("invalid host_url") } if hostUrl.Fragment != "" { return "", errors.New("fragment is not allowed in host_url") } if hostUrl.Path == "" { hostUrl.Path = "/" } return hostUrl.String(), nil } func ExecuteTemplate(template *template.Template, parts []string, params map[string]any) (map[string]string, error) { content := map[string]string{} buffer := new(bytes.Buffer) if parts == nil { if err := template.Execute(buffer, params); err != nil { return nil, err } content[""] = buffer.String() } else { for _, part := range parts { buffer.Reset() if templBody := template.Lookup(part); templBody != nil { if err := templBody.Execute(buffer, params); err != nil { return nil, err } } content[part] = buffer.String() } } return content, nil } func ResolveTemplatePath(path string) (string, error) { if filepath.IsAbs(path) { return path, nil } curwd, err := os.Getwd() if err != nil { return "", err } return filepath.Clean(filepath.Join(curwd, path)), nil } func ReadTemplateFile(pathTempl *template.Template, lang string) (*template.Template, string, error) { buffer := bytes.Buffer{} err := pathTempl.Execute(&buffer, map[string]any{"Language": lang}) path := buffer.String() if err != nil { return nil, path, fmt.Errorf("reading %s: %w", path, err) } templ, err := template.ParseFiles(path) return templ, path, err } ================================================ FILE: tinode-db/README.md ================================================ # Utility to initialize or upgrade `tinode` DB This utility initializes the `tinode` database (or upgrades an existing DB from an earlier version) and optionally loads it with data. To force database reset use command line option `--reset=true`. ## Build the package: - **RethinkDB** `go build -tags rethinkdb` or `go build -i -tags rethinkdb` to automatically install missing dependencies. - **MySQL** `go build -tags mysql` or `go build -i -tags mysql` to automatically install missing dependencies. - **MongoDB** `go build -tags mongodb` or `go build -i -tags mongodb` to automatically install missing dependencies. - **PostgreSQL** `go build -tags postgres` or `go build -i -tags postgres` to automatically install missing dependencies. ## Run Run from the command line. `tinode-db [parameters]` Command line parameters: - `--reset`: delete the database then re-create it in a blank state; it has no effect if the database does not exist. - `--upgrade`: upgrade database from an earlier version retaining all the data; make sure to backup the DB before upgrading. - `--no_init`: check that database exists but don't create it if missing. - `--data=FILENAME`: fill `tinode` database with data from the provided file. See [data.json](data.json). - `--config=FILENAME`: load configuration from FILENAME. Example config is included as [tinode.conf](tinode.conf). - `--make_root=USER_ID`: promote an existing user to root user, `USER_ID` of the form `usrAbCDef123`. - `--add_root=USERNAME[:PASSWORD]`: create a new user account and make it root; if password is missing, a strong password will be generated. Configuration file options: - `uid_key` is a base64-encoded 16 byte XTEA encryption key to (weakly) encrypt object IDs so they don't appear sequential. You probably want to use your own key in production. - `store_config.adapters.mysql` and `store_config.adapters.rethinkdb` are database-specific sections: - `database` is the name of the database to generate. - `addresses` is RethinkDB/MongoDB's host and port number to connect to. An array of hosts can be provided as well `["host1", "host2"]`. - `dsn` is MySQL's Data Source Name. - `replica_set` is MongoDB's Replicaset name. The `uid_key` is only used if the sample data is being loaded. It should match the key of a production server and should be kept private. The default `data.json` file creates six users with user names `alice`, `bob`, `carol`, `dave`, `frank`, and `tino` (chat bot user). Passwords are the same as the user names with 123 appended, e.g. user `alice` gets password `alice123`; `tino` gets a randomly generated password. It also creates three group topics, and multiple peer to peer topics. Users are subscribed to topics and to each other. All topics are randomly filled with messages. Avatar photos curtesy of https://www.pexels.com/ under [CC0 license](https://www.pexels.com/photo-license/). ## Links: * [RethinkDB schema](https://github.com/tinode/chat/tree/master/server/db/rethinkdb/schema.md) * [MySQL schema](https://github.com/tinode/chat/tree/master/server/db/mysql/schema.sql) * [MongoDB schema](https://github.com/tinode/chat/tree/master/server/db/mongodb/schema.md) * [PostgreSQL schema](https://github.com/tinode/chat/tree/master/server/db/postgres/schema.sql) ================================================ FILE: tinode-db/credentials.sh ================================================ #!/bin/bash # Credential extractor. Tino the Chatbot is created with a random password. The password is written # to stdout by tinode-db. The script converts it to chatbot's authentication cookie. # The script takes a string like 'usr;tino;usrImlot_X9vAc;cOuTvzVa' (ignored;login;user_id;password) # and formats it into a json chatbot's authentication cookie like # '{"schema": "basic", "secret": "username:password", "user": "user_id"}'. COOKIE_FILE=$@ while read line; do IFS=';' read -r -a parts <<< "$line" if [ ${#parts[@]} -eq 0 ] ; then continue fi # If the name of the cookie file is given, write to file # Otherwise write to stdout if [ "$COOKIE_FILE" ]; then exec 3>"$COOKIE_FILE" else exec 3>&1 fi echo "{\"schema\": \"basic\", \"secret\": \"${parts[1]}:${parts[3]}\", \"user\": \"${parts[2]}\"}" 1>&3 break done < /dev/stdin ================================================ FILE: tinode-db/data.json ================================================ { "users": [ { "createdAt": "-140h", "email": "alice@example.com", "tel": "+17025550001", "passhash": "alice123", "private": {"comment": "some comment 123"}, "public": {"fn": "Alice Hatter", "photo": "alice-128.jpg", "type": "jpg"}, "trusted": {"verified": true}, "tags": ["Alice"], "state": "ok", "status": { "text": "DND" }, "username": "alice", "addressBook": ["email:bob@example.com", "email:carol@example.com", "email:dave@example.com", "email:eve@example.com","email:frank@example.com","email:george@example.com","email:tino@example.com", "tel:+17025550001", "tel:+17025550002", "tel:+17025550003", "tel:+17025550004", "tel:+17025550005", "tel:+17025550006", "tel:+17025550007", "tel:+17025550008", "tel:+17025550009"] }, { "createdAt": "-139h", "email": "bob@example.com", "tel": "+17025550002", "passhash": "bob123", "private": {"comment": "no comments :)"}, "public": {"fn": "Bob Smith", "photo": "bob-128.jpg", "type": "jpg"}, "state": "ok", "status": "stuff", "username": "bob", "addressBook": ["email:alice@example.com", "email:carol@example.com", "email:dave@example.com", "email:eve@example.com", "email:frank@example.com", "email:george@example.com", "email:tino@example.com", "tel:+17025550001", "tel:+17025550002", "tel:+17025550003", "tel:+17025550004", "tel:+17025550005", "tel:+17025550006", "tel:+17025550007", "tel:+17025550008", "tel:+17025550009"] }, { "createdAt": "-138h", "email": "carol@example.com", "tel": "+17025550003", "passhash": "carol123", "private": {"comment": "more stuff"}, "public": {"fn": "Carol Xmas", "photo": "carol-128.jpg", "type": "jpg"}, "state": "", "status": "ho ho ho", "username": "carol", "addressBook": ["email:alice@example.com", "email:bob@example.com", "email:dave@example.com", "email:eve@example.com", "email:frank@example.com", "email:george@example.com", "email:tino@example.com", "tel:+17025550001", "tel:+17025550002", "tel:+17025550003", "tel:+17025550004", "tel:+17025550005", "tel:+17025550006", "tel:+17025550007", "tel:+17025550008", "tel:+17025550009"] }, { "createdAt": "-137h", "email": "dave@example.com", "tel": "+17025550004", "passhash": "dave123", "private": {"comment": "stuff 123"}, "public": {"fn": "Dave Goliathsson", "photo": "dave-128.jpg", "type": "jpg"}, "state": "ok", "status": "hiding!", "username": "dave", "addressBook": ["email:alice@example.com", "email:bob@example.com", "email:carol@example.com", "email:eve@example.com", "email:frank@example.com", "email:george@example.com", "email:tino@example.com", "tel:+17025550001", "tel:+17025550002", "tel:+17025550003", "tel:+17025550004", "tel:+17025550005", "tel:+17025550006", "tel:+17025550007", "tel:+17025550008", "tel:+17025550009"] }, { "createdAt": "-136h", "email": "eve@example.com", "tel": "+17025550005", "passhash": "eve123", "private": {"comment": "apples?"}, "public": {"fn": "Eve Adams", "photo": "eve-128.jpg", "type": "jpg"}, "state": "ok", "username": "eve", "addressBook": ["email:alice@example.com", "email:bob@example.com", "email:carol@example.com", "email:dave@example.com", "email:george@example.com", "email:tino@example.com", "tel:+17025550001", "tel:+17025550002", "tel:+17025550003", "tel:+17025550004", "tel:+17025550005", "tel:+17025550006", "tel:+17025550007", "tel:+17025550008", "tel:+17025550009"] }, { "createdAt": "-135h", "email": "frank@example.com", "tel": "+17025550006", "passhash": "frank123", "private": {"comment": "things, not stuff"}, "public": {"fn": "Frank Singer"}, "state": "ok", "status": "singing!", "username": "frank", "addressBook": ["email:bob@example.com", "email:carol@example.com", "email:dave@example.com", "email:eve@example.com", "email:george@example.com", "email:tino@example.com", "tel:+17025550001", "tel:+17025550002", "tel:+17025550003", "tel:+17025550004", "tel:+17025550005", "tel:+17025550007", "tel:+17025550008", "tel:+17025550009"] }, { "createdAt": "-134h", "email": "tino@example.com", "tel": "+17025550010", "passhash": "(random)", "private": {"comment": "I'm a chatbot short and stout"}, "trusted": {"staff": true}, "public": {"fn": "Tino the Chatbot", "photo": "tino-128.jpg", "type": "jpg"}, "state": "", "status": "Chatting nonsensically", "username": "tino", "addressBook": [] }, { "createdAt": "-133h", "email": "xena@example.com", "tel": "+17025550099", "passhash": "xena123", "authLevel": "root", "private": {"comment": "No one knowns that Xena exists"}, "public": {"fn": "Xena Pacifist Peasant", "photo": "xena-128.jpg", "type": "jpg"}, "trusted": {"staff": true}, "state": "", "status": "Just a simple peaceful agriculture specialist", "username": "xena", "addressBook": [] } ], "grouptopics": [ { "createdAt": "-128h", "name": "*ABC", "owner": "carol", "tags": ["flower", "flowers", "flora"], "public": {"fn": "Let's talk about flowers", "photo": "abc-128.jpg", "type": "jpg"} }, { "createdAt": "-126h", "name": "*ABCDEF", "owner": "alice", "tags": ["travel"], "public": {"fn": "Travel, travel, travel", "photo": "abcdef-128.jpg", "type": "jpg"} }, { "createdAt": "-124h", "name": "*BF", "owner": "frank", "tags": ["sikrit", "secret"], "public": {"fn": "Sikrit group!", "photo": "bf-128.jpg", "type": "jpg"} }, { "createdAt": "-123h", "name": "*X", "owner": "xena", "tags": ["support","public"], "public": {"fn": "Support", "photo": "support-128.jpg", "type": "jpg"}, "trusted": {"staff": true}, "access": {"auth": "JRWP", "anon": "JW"} }, { "createdAt": "-122h", "name": "*BACDF", "owner": "bob", "channel": true, "tags": ["coffee","channel"], "public": {"fn": "Coffee Channel", "photo": "chan-128.jpg", "type": "jpg"}, "trusted": {"verified": true}, "access": {"auth": "RWPD", "anon": "N"} } ], "p2psubs": [ { "createdAt": "-120h", "users": [{"name": "alice"}, {"name": "bob", "private": {"comment": "Alice Jo"}}] }, { "createdAt": "-119h", "users": [{"name": "alice"}, {"name": "carol", "private": {"comment": "Alice Joha"}}] }, { "createdAt": "-118h", "users": [{"name": "alice"}, {"name": "dave", "private": {"comment": "Alice not in Wunderland"}}] }, { "createdAt": "-117h", "private": {"comment": "apples to oranges"}, "users": [{"name": "alice", "private": {"comment": "Apples"}}, {"name": "eve", "private": {"comment": "Alice is not what she seems"}}] }, { "createdAt": "-116h", "users": [{"name": "alice", "private": {"comment": "Frank Frank Frank a-\u003ef"}}, {"name": "frank", "private": {"comment": "Johnson f-\u003ea"}}] }, { "createdAt": "-115.5h", "users": [{"name": "alice", "private": {"comment": "Python chatbot, short and stout"}}, {"name": "tino"}] }, { "createdAt": "-114h", "users": [{"name": "bob", "private": {"comment": "I'm banned by Dave!"}, "have": "A"}, {"name": "dave", "private": {"comment": "I banned Bob."}, "want": "N"}] }, { "createdAt": "-113.5h", "users": [{"name": "bob", "private": {"comment": "Eve nee Ng"}}, {"name": "eve"}] }, { "createdAt": "-113.45h", "users": [{"name": "bob", "private": {"comment": "Python chatbot, short and stout"}}, {"name": "tino"}] }, { "createdAt": "-113.3h", "users": [{"name": "carol", "private": {"comment": "Python chatbot, short and stout"}}, {"name": "tino"}] }, { "createdAt": "-112.7h", "private": {"comment": "Python chatbot short and stout"}, "users": [{"name": "dave", "private": {"comment": "Python chatbot, short and stout"}}, {"name": "tino"}] }, { "createdAt": "-112.3h", "users": [{"name": "eve", "private": {"comment": "Python chatbot, short and stout"}}, {"name": "tino"}] }, { "createdAt": "-112.1h", "users": [{"name": "frank", "private": {"comment": "Python chatbot, short and stout"}}, {"name": "tino"}] } ], "groupsubs": [{ "createdAt": "-112h", "private": {"comment": "My super cool group topic"}, "topic": "*ABC", "user": "alice" }, { "createdAt": "-111.9h", "private": {"comment": "Wow"}, "topic": "*ABC", "user": "bob" }, { "createdAt": "-111.8h", "private": {"comment": "Custom group description by Bob"}, "topic": "*ABCDEF", "user": "bob" }, { "createdAt": "-111.7h", "private": {"comment": "Kirgudu"}, "topic": "*ABCDEF", "user": "carol" }, { "createdAt": "-111.6h", "topic": "*ABCDEF", "user": "dave" }, { "createdAt": "-111.5h", "topic": "*ABCDEF", "user": "eve" }, { "createdAt": "-111.4h", "topic": "*ABCDEF", "user": "frank" }, { "createdAt": "-111.3h", "private": {"comment": "I'm not the owner, Frank is"}, "topic": "*BF", "user": "bob" }, { "createdAt": "-111.2h", "topic": "*BACDF", "user": "alice" }, { "createdAt": "-111.1h", "topic": "*BACDF", "asChan": true, "user": "carol" }, { "createdAt": "-111.0h", "topic": "*BACDF", "asChan": true, "user": "dave" }, { "createdAt": "-110.8h", "topic": "*BACDF", "asChan": true, "user": "frank" } ], "messages": [ "Caution: Do not view laser light with remaining eye.", "Caution: breathing may be hazardous to your health.", "Celebrate Hannibal Day this year. Take an elephant to lunch.", "Celibacy is not hereditary.", "Center 1127 -- It's not just a job, it's an adventure!", "Center meeting at 4pm in 2C-543", "Centran manuals are available in 2B-515.", "Charlie don't surf.", "Children are hereditary: if your parents didn't have any, neither will you.", "Clothes make the man. Naked people have little or no influence on society.", "Club sandwiches, not baby seals.", "Cocaine is nature's way of saying you make too much money.", "Cogito Ergo Spud.", "Cogito cogito ergo cogito sum.", "Colorless green ideas sleep furiously.", "Communication is only possible between equals.", "Computers are not intelligent. They only think they are.", "Consistency is always easier to defend than correctness.", "Constants aren't. Variables don't. LISP does. Functions won't. Bytes do.", "Contains no kung fu, car chases or decapitations.", "Continental Life. Why do you ask?", "Convictions cause convicts -- what you believe imprisons you.", "Core Error - Bus Dumped", "Could not open 2147478952 framebuffers.", "Courage is something you can never totally own or totally lose.", "Cowards die many times before their deaths;/The valiant never taste of death but once.", "Crazee Edeee, his prices are INSANE!!!", "Creativity is no substitute for knowing what you are doing.", "Creditors have much better memories than debtors.", "Critics are like eunuchs in a harem: they know how it's done, they've seen it done", "every day, but they're unable to do it themselves. -Brendan Behan", "Cthulhu Saves! ... in case He's hungry later.", "Dames is like streetcars -- The oceans is full of 'em. -Archie Bunker", "Dames lie about anything - just for practice. -Raymond Chandler", "Damn it, i gotta get outta here!", "Dangerous knowledge is a little thing.", "It is certain", "It is decidedly so", "Without a doubt", "Yes definitely", "You may rely on it", "As I see it yes", "Most likely", "Outlook good", "Yes", "No", "No! No, no, no! No!!", "Signs point to yes", "Reply hazy try again", "Ask again later", "Better not tell you now", "Cannot predict now", "Concentrate and ask again", "Don't count on it", "My reply is no", "My sources say no.", "Outlook not so good", "Very doubtful...", "All your base are belong to us." ], "forms": [ { "txt": "Post responseYesДа", "fmt": [ {"at": 0, "len": 18, "tp": "FM"}, {"at": 0, "len": 13, "tp": "ST"}, {"at": 13, "len": 3, "key": 0}, {"at": 16, "len": 2, "key": 1} ], "ent": [ {"tp": "BN", "data": {"name": "yes", "act": "pub"}}, {"tp": "BN", "data": {"name": "duh", "val": "42", "act": "pub"}} ] }, { "txt": "Go to URL:Yes OK", "fmt": [ {"at": 0, "len": 16, "tp": "FM"}, {"at": 0, "len": 10, "tp": "ST"}, {"at": 10, "len": 6, "tp": "RW"}, {"at": 10, "len": 3, "key": 0}, {"at": 14, "len": 2, "key": 1} ], "ent": [ {"tp": "BN", "data": {"name": "ok", "act": "url", "ref": "https://github.com/tinode/chat/?key=val"}}, {"tp": "BN", "data": {"name": "oops", "val": "test", "act": "url", "ref": "https://github.com/tinode/chat"}} ] } ] } ================================================ FILE: tinode-db/gendb.go ================================================ package main import ( "fmt" "log" "math/rand" "os" "path/filepath" "strings" "time" "github.com/tinode/chat/server/auth" _ "github.com/tinode/chat/server/auth/basic" "github.com/tinode/chat/server/store" "github.com/tinode/chat/server/store/types" ) func genDb(data *Data, p2pDel bool) { var err error var botAccount string if len(data.Users) == 0 { log.Println("No data provided, stopping") return } // Add authentication record authHandler := store.Store.GetAuthHandler("basic") authHandler.Init([]byte(`{"add_to_tags": true}`), "basic") nameIndex := make(map[string]string, len(data.Users)) log.Println("Generating users...") for _, uu := range data.Users { state, err := types.NewObjState(uu.State) if err != nil { log.Fatal(err) } user := types.User{ State: state, Access: types.DefaultAccess{ Auth: types.ModeCAuth, Anon: types.ModeNone, }, Tags: uu.Tags, Public: parsePublic(&uu.Public, data.datapath), } if !uu.Trusted.IsZero() { user.Trusted = uu.Trusted } user.CreatedAt = getCreatedTime(uu.CreatedAt) user.Tags = append(user.Tags, "basic:"+uu.Username) if uu.Email != "" { user.Tags = append(user.Tags, "email:"+uu.Email) } if uu.Tel != "" { user.Tags = append(user.Tags, "tel:"+uu.Tel) } // store.Users.Create will subscribe user to !me topic but won't create a !me topic if _, err := store.Users.Create(&user, uu.Private); err != nil { log.Fatal(err) } // Save credentials: email and phone number as if they were confirmed. if uu.Email != "" { if _, err := store.Users.UpsertCred(&types.Credential{ User: user.Id, Method: "email", Value: uu.Email, Done: true, }); err != nil { log.Fatal(err) } } if uu.Tel != "" { if _, err := store.Users.UpsertCred(&types.Credential{ User: user.Id, Method: "tel", Value: uu.Tel, Done: true, }); err != nil { log.Fatal(err) } } authLevel := auth.LevelAuth if uu.AuthLevel != "" { authLevel = auth.ParseAuthLevel(uu.AuthLevel) if authLevel == auth.LevelNone { log.Fatal("Unknown authLevel", uu.AuthLevel) } } // Add authentication record authHandler := store.Store.GetAuthHandler("basic") passwd := uu.Password if passwd == "(random)" { // Generate random password passwd = getPassword(8) botAccount = uu.Username } if _, err := authHandler.AddRecord(&auth.Rec{Uid: user.Uid(), AuthLevel: authLevel}, []byte(uu.Username+":"+passwd), ""); err != nil { log.Fatal(err) } nameIndex[uu.Username] = user.Id // Add address book as fnd.private if len(uu.AddressBook) > 0 { if err := store.Subs.Update(user.Uid().FndName(), user.Uid(), map[string]any{"Private": strings.Join(uu.AddressBook, ",")}); err != nil { log.Fatal(err) } } fmt.Println("usr;" + uu.Username + ";" + user.Uid().UserId() + ";" + passwd) } if botAccount == "" && len(data.Users) > 0 { botAccount = data.Users[0].Username } log.Println("Generating group topics...") for _, gt := range data.Grouptopics { name := genTopicName() nameIndex[gt.Name] = name accessAuth := types.ModeCPublic if gt.Access.Auth != "" { if err := accessAuth.UnmarshalText([]byte(gt.Access.Auth)); err != nil { log.Fatal("Invalid Auth access mode", gt.Access.Auth, err) } } accessAnon := types.ModeCReadOnly if gt.Access.Anon != "" { if err := accessAnon.UnmarshalText([]byte(gt.Access.Anon)); err != nil { log.Fatal("Invalid Anon access mode", gt.Access.Anon, err) } } topic := &types.Topic{ ObjHeader: types.ObjHeader{Id: name}, Access: types.DefaultAccess{ Auth: accessAuth, Anon: accessAnon, }, UseBt: gt.Channel, Tags: gt.Tags, Public: parsePublic(>.Public, data.datapath), } if !gt.Trusted.IsZero() { topic.Trusted = gt.Trusted } var owner types.Uid if gt.Owner != "" { owner = types.ParseUid(nameIndex[gt.Owner]) if owner.IsZero() { log.Fatal("Invalid owner", gt.Owner, "for topic", gt.Name) } topic.GiveAccess(owner, types.ModeCFull, types.ModeCFull) } topic.CreatedAt = getCreatedTime(gt.CreatedAt) if err = store.Topics.Create(topic, owner, gt.OwnerPrivate); err != nil { log.Fatal(err) } fmt.Println("grp;" + gt.Name + ";" + name) } log.Println("Generating P2P subscriptions...") for i, ss := range data.P2psubs { if ss.Users[0].Name < ss.Users[1].Name { ss.pair = ss.Users[0].Name + ":" + ss.Users[1].Name } else { ss.pair = ss.Users[1].Name + ":" + ss.Users[0].Name } uid1 := types.ParseUid(nameIndex[ss.Users[0].Name]) uid2 := types.ParseUid(nameIndex[ss.Users[1].Name]) topic := uid1.P2PName(uid2) created := getCreatedTime(ss.CreatedAt) // Assign default access mode defaultMode := types.ModeCP2P if p2pDel { defaultMode = types.ModeCP2PD } s0want := defaultMode s0given := defaultMode s1want := defaultMode s1given := defaultMode // Check of non-default access mode was provided if ss.Users[0].Want != "" { if err := s0want.UnmarshalText([]byte(ss.Users[0].Want)); err != nil { log.Fatal(err) } } if ss.Users[0].Have != "" { if err := s0given.UnmarshalText([]byte(ss.Users[0].Have)); err != nil { log.Fatal(err) } } if ss.Users[1].Want != "" { if err := s1want.UnmarshalText([]byte(ss.Users[1].Want)); err != nil { log.Fatal(err) } } if ss.Users[1].Have != "" { if err := s1given.UnmarshalText([]byte(ss.Users[1].Have)); err != nil { log.Fatal(err) } } err := store.Topics.CreateP2P( &types.Subscription{ ObjHeader: types.ObjHeader{CreatedAt: created}, User: uid1.String(), Topic: topic, ModeWant: s0want, ModeGiven: s0given, Private: ss.Users[0].Private}, &types.Subscription{ ObjHeader: types.ObjHeader{CreatedAt: created}, User: uid2.String(), Topic: topic, ModeWant: s1want, ModeGiven: s1given, Private: ss.Users[1].Private}) if err != nil { log.Fatal(err) } data.P2psubs[i].pair = ss.pair nameIndex[ss.pair] = topic fmt.Println("p2p;" + ss.pair + ";" + topic) } log.Println("Generating group subscriptions...") for _, ss := range data.Groupsubs { var want, given types.AccessMode if ss.AsChan { want = types.ModeCChnReader given = types.ModeCChnReader } else { want = types.ModeCPublic given = types.ModeCPublic } if ss.Want != "" { if err := want.UnmarshalText([]byte(ss.Want)); err != nil { log.Fatal(err) } } if ss.Have != "" { if err := given.UnmarshalText([]byte(ss.Have)); err != nil { log.Fatal(err) } } tname := nameIndex[ss.Topic] if ss.AsChan { tname = types.GrpToChn(tname) } if err = store.Subs.Create(&types.Subscription{ ObjHeader: types.ObjHeader{CreatedAt: getCreatedTime(ss.CreatedAt)}, User: nameIndex[ss.User], Topic: tname, ModeWant: want, ModeGiven: given, Private: ss.Private}); err != nil { log.Fatal(err) } } seqIds := map[string]int{} now := types.TimeNow().Add(-time.Minute * 10) messageCount := len(data.Messages) if messageCount > 0 { log.Println("Inserting messages...") if messageCount > 1 { // Shuffle messages rand.Shuffle(len(data.Messages), func(i, j int) { data.Messages[i], data.Messages[j] = data.Messages[j], data.Messages[i] }) // Starting 4 days ago. timestamp := now.Add(time.Hour * time.Duration(-24*4)) toInsert := 96 // 96 is the maximum, otherwise messages may appear in the future // Initial maximum increment of the message sent time in milliseconds increment := 3600 * 1000 subIdx := rand.Intn(len(data.Groupsubs) + len(data.P2psubs)*2) for i := range toInsert { // At least 20% of subsequent messages should come from the same user in the same topic. if rand.Intn(5) > 0 { subIdx = rand.Intn(len(data.Groupsubs) + len(data.P2psubs)*2) } var topic string var from types.Uid if subIdx < len(data.Groupsubs) { if data.Groupsubs[subIdx].AsChan { // Channel readers should not have any published messages. continue } topic = nameIndex[data.Groupsubs[subIdx].Topic] from = types.ParseUid(nameIndex[data.Groupsubs[subIdx].User]) } else { idx := (subIdx - len(data.Groupsubs)) / 2 usr := (subIdx - len(data.Groupsubs)) % 2 sub := data.P2psubs[idx] topic = nameIndex[sub.pair] from = types.ParseUid(nameIndex[sub.Users[usr].Name]) } seqIds[topic]++ seqId := seqIds[topic] str := data.Messages[i%len(data.Messages)] // Max time between messages is 2 hours, averate - 1 hour, time is increasing as seqId increases timestamp = timestamp.Add(time.Microsecond * time.Duration(rand.Intn(increment))) if timestamp.After(now) { now = timestamp } if err, _ = store.Messages.Save(&types.Message{ ObjHeader: types.ObjHeader{CreatedAt: timestamp}, SeqId: seqId, Topic: topic, From: from.String(), Content: str, }, nil, true); err != nil { log.Fatal("Failed to insert message: ", err) } // New increment: remaining time until 'now' divided by the number of messages to be inserted, // then converted to milliseconds. increment = int(now.Sub(timestamp).Nanoseconds() / int64(toInsert-i) / 1000000) // log.Printf("Msg.seq=%d at %v, topic='%s' from='%s'", msg.SeqId, msg.CreatedAt, topic, from.UserId()) } } else { // Only one message is provided. Just insert it into every topic. now := time.Now().UTC().Add(-time.Minute).Round(time.Millisecond) for _, gt := range data.Grouptopics { seqIds[nameIndex[gt.Name]] = 1 if err, _ = store.Messages.Save(&types.Message{ ObjHeader: types.ObjHeader{CreatedAt: now}, SeqId: 1, Topic: nameIndex[gt.Name], From: nameIndex[gt.Owner], Content: data.Messages[0], }, nil, true); err != nil { log.Fatal("Failed to insert message: ", err) } } usedp2p := make(map[string]bool) for i := 0; len(usedp2p) < len(data.P2psubs)/2; i++ { sub := data.P2psubs[i] if usedp2p[nameIndex[sub.pair]] { continue } usedp2p[nameIndex[sub.pair]] = true seqIds[nameIndex[sub.pair]] = 1 if err, _ = store.Messages.Save(&types.Message{ ObjHeader: types.ObjHeader{CreatedAt: now}, SeqId: 1, Topic: nameIndex[sub.pair], From: nameIndex[sub.Users[0].Name], Content: data.Messages[0], }, nil, true); err != nil { log.Fatal("Failed to insert message: ", err) } } } } if len(data.Forms) != 0 { from := nameIndex[botAccount] log.Println("Inserting forms as ", botAccount, from) ts := now for _, form := range data.Forms { for _, sub := range data.P2psubs { ts = ts.Add(time.Second) seqIds[nameIndex[sub.pair]]++ seqId := seqIds[nameIndex[sub.pair]] if sub.Users[0].Name == botAccount || sub.Users[1].Name == botAccount { if err, _ = store.Messages.Save(&types.Message{ ObjHeader: types.ObjHeader{CreatedAt: ts}, SeqId: seqId, Topic: nameIndex[sub.pair], Head: types.KVMap{"mime": "text/x-drafty"}, From: from, Content: form, }, nil, true); err != nil { log.Fatal("Failed to insert form: ", err) } } } } } log.Println("Sample data processing completed.") } // Go json cannot unmarshal Duration from a string, thus this hack. func getCreatedTime(delta string) time.Time { dd, err := time.ParseDuration(delta) if err != nil && delta != "" { log.Fatal("Invalid duration string", delta) } return time.Now().UTC().Round(time.Millisecond).Add(dd) } type photoStruct struct { Type string `json:"type" db:"type"` Data []byte `json:"data" db:"data"` } type card struct { Fn string `json:"fn" db:"fn"` Photo *photoStruct `json:"photo,omitempty" db:"photo"` } // {"fn": "Alice Johnson", "photo": "alice-128.jpg"} func parsePublic(public *theCard, path string) *card { var photo *photoStruct var err error if public.Fn == "" && public.Photo == "" { return nil } if fname := public.Photo; fname != "" { photo = &photoStruct{Type: public.Type} dir, _ := filepath.Split(fname) if dir == "" { dir = path } photo.Data, err = os.ReadFile(filepath.Join(dir, fname)) if err != nil { log.Fatal(err) } } return &card{Fn: public.Fn, Photo: photo} } ================================================ FILE: tinode-db/generate_dataset.py ================================================ # Script that generates a synthetic Tinode dataset in the same format as data.json. # Run as: # $ python generate_dataset.py --num_users X # # User ids and passwords are: userN and userN123 where N is in [0..X). # The output will be dumped to stdout. import argparse as ap import json import numpy as np import random import sys parser = ap.ArgumentParser(description='Simple python script to generate synthetic Tinode datasets.') parser.add_argument('--num_users', type=int, default=50, help='Number of user accounts (default: 50).') args = parser.parse_args() random.seed() np.random.seed() data = dict() # Users. num_users = args.num_users users = list() ul = ['user%d' % i for i in range(num_users)] for i in range(num_users): userid = ul[i] u = { 'createdAt': '-%dh' % random.randint(1, 300), 'email': '%s@example.com' % userid, 'passhash': '%s123' % userid, 'private': {'comment': 'some comment 123'}, 'public': {'fn': userid}, 'tags': [userid], 'state': 'ok', 'status': { 'text': 'my status %s' % userid }, 'username': userid, } users.append(u) data['users'] = users # Groups. group_topics = list() # TODO: make it configurable. num_groups = random.randint(1, num_users >> 1) for i in range(num_groups): owner = random.choice(ul) g = { 'createdAt': '-%dh' % random.randint(1, 300), 'name': '*ABCgroup%d' % i, 'owner': owner, 'tags': ['group%d' % i], 'public': {'fn': 'My group %d' % i} } group_topics.append(g) data['grouptopics'] = group_topics # P2P subs. p2p_subs = list() ids = set() # TODO: Poisson mean should be configurable. num_contacts = np.random.poisson(40, num_users).tolist() num_contacts = [min(max(2, int(s)), num_users - 1) for s in num_contacts] all_ids = [i for i in range(num_users)] for i in range(num_users): contacts = [ul[x] for x in random.sample(all_ids, num_contacts[i]) if x > i] for c in contacts: p2p = { 'createdAt': '-%dh' % random.randint(1, 300), 'users': [{'name': ul[i]}, {'name': c}] } p2p_subs.append(p2p) data['p2psubs'] = p2p_subs # Group subs. # TODO: Poisson mean should be configurable as well. sizes = np.random.poisson(4, num_groups).tolist() sizes = [min(max(2, int(s)), num_users) for s in sizes] group_subs = list() for i in range(num_groups): k = sizes[i] gs = random.sample(ul, k) for uid in gs: g = { 'createdAt': '-%dh' % random.randint(1, 300), 'topic': '*ABCgroup%d' % i, 'user': uid } group_subs.append(g) data['groupsubs'] = group_subs json.dump(data, sys.stdout, ensure_ascii=False, indent=4) ================================================ FILE: tinode-db/main.go ================================================ package main import ( crand "crypto/rand" "encoding/json" "flag" "log" "math/rand" "os" "path/filepath" "strconv" "strings" "time" "github.com/tinode/chat/server/auth" _ "github.com/tinode/chat/server/db/mongodb" _ "github.com/tinode/chat/server/db/mysql" _ "github.com/tinode/chat/server/db/postgres" _ "github.com/tinode/chat/server/db/rethinkdb" "github.com/tinode/chat/server/store" "github.com/tinode/chat/server/store/types" jcr "github.com/tinode/jsonco" ) type configType struct { P2PDeleteEnabled bool `json:"p2p_delete_enabled"` StoreConfig json.RawMessage `json:"store_config"` } type theCard struct { Fn string `json:"fn"` Photo string `json:"photo"` Type string `json:"type"` } type tPrivate struct { Comment string `json:"comment"` } type tTrusted struct { Verified bool `json:"verified,omitempty"` Staff bool `json:"staff,omitempty"` } func (t tTrusted) IsZero() bool { return !t.Verified && !t.Staff } // DefAccess is default access mode. type DefAccess struct { Auth string `json:"auth"` Anon string `json:"anon"` } /* User object in data.json "createdAt": "-140h", "email": "alice@example.com", "tel": "17025550001", "passhash": "alice123", "private": {"comment": "some comment 123"}, "public": {"fn": "Alice Johnson", "photo": "alice-64.jpg", "type": "jpg"}, "state": "ok", "authLevel": "auth", "status": { "text": "DND" }, "username": "alice", "tags": ["tag1"], "addressBook": ["email:bob@example.com", "email:carol@example.com", "email:dave@example.com", "email:eve@example.com","email:frank@example.com","email:george@example.com","email:tob@example.com", "tel:17025550001", "tel:17025550002", "tel:17025550003", "tel:17025550004", "tel:17025550005", "tel:17025550006", "tel:17025550007", "tel:17025550008", "tel:17025550009"] } */ type User struct { CreatedAt string `json:"createdAt"` Email string `json:"email"` Tel string `json:"tel"` AuthLevel string `json:"authLevel"` Username string `json:"username"` Password string `json:"passhash"` Private tPrivate `json:"private"` Public theCard `json:"public"` Trusted tTrusted `json:"trusted"` State string `json:"state"` Status any `json:"status"` AddressBook []string `json:"addressBook"` Tags []string `json:"tags"` } /* GroupTopic object in data.json "createdAt": "-128h", "name": "*ABC", "owner": "carol", "channel": true, "public": {"fn": "Let's talk about flowers", "photo": "abc-64.jpg", "type": "jpg"} */ type GroupTopic struct { CreatedAt string `json:"createdAt"` Name string `json:"name"` Owner string `json:"owner"` Channel bool `json:"channel"` Public theCard `json:"public"` Trusted tTrusted `json:"trusted"` Access DefAccess `json:"access"` Tags []string `json:"tags"` OwnerPrivate tPrivate `json:"ownerPrivate"` } /* GroupSub object in data.json "createdAt": "-112h", "private": "My super cool group topic", "topic": "*ABC", "user": "alice", "asChan: false, "want": "JRWPSA", "have": "JRWP" */ type GroupSub struct { CreatedAt string `json:"createdAt"` Private tPrivate `json:"private"` Topic string `json:"topic"` User string `json:"user"` AsChan bool `json:"asChan"` Want string `json:"want"` Have string `json:"have"` } /* P2PUser topic in data.json "createdAt": "-117h", "users": [ {"name": "eve", "private": {"comment":"ho ho"}, "want": "JRWP", "have": "N"}, {"name": "alice", "private": {"comment": "ha ha"}} ] */ type P2PUser struct { Name string `json:"name"` Private tPrivate `json:"private"` Want string `json:"want"` Have string `json:"have"` } // P2PSub is a p2p subscription in data.json type P2PSub struct { CreatedAt string `json:"createdAt"` Users []P2PUser `json:"users"` // Cached value 'user1:user2' as a surrogare topic name pair string } // Data is a message in data.json. type Data struct { Users []User `json:"users"` Grouptopics []GroupTopic `json:"grouptopics"` Groupsubs []GroupSub `json:"groupsubs"` P2psubs []P2PSub `json:"p2psubs"` Messages []string `json:"messages"` Forms []map[string]any `json:"forms"` datapath string } // Generate random string as a name of the group topic func genTopicName() string { return "grp" + store.Store.GetUidString() } // Generates password of length n func getPassword(n int) string { const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-/.+?=&" rbuf := make([]byte, n) if _, err := crand.Read(rbuf); err != nil { log.Fatalln("Unable to generate password", err) } passwd := make([]byte, n) for i, r := range rbuf { passwd[i] = letters[int(r)%len(letters)] } return string(passwd) } func main() { reset := flag.Bool("reset", false, "force database reset") upgrade := flag.Bool("upgrade", false, "perform database version upgrade") noInit := flag.Bool("no_init", false, "check that database exists but don't create if missing") addRoot := flag.String("add_root", "", "create ROOT user, auth scheme 'basic'") makeRoot := flag.String("make_root", "", "promote ordinary user to ROOT, auth scheme 'basic'") datafile := flag.String("data", "", "name of file with sample data to load") conffile := flag.String("config", "./tinode.conf", "config of the database connection") flag.Parse() var data Data if *datafile != "" && *datafile != "-" { raw, err := os.ReadFile(*datafile) if err != nil { log.Fatalln("Failed to read sample data file:", err) } err = json.Unmarshal(raw, &data) if err != nil { log.Fatalln("Failed to parse sample data:", err) } } rand.Seed(time.Now().UnixNano()) data.datapath, _ = filepath.Split(*datafile) var config configType if file, err := os.Open(*conffile); err != nil { log.Fatalln("Failed to read config file:", err) } else { jr := jcr.New(file) if err = json.NewDecoder(jr).Decode(&config); err != nil { switch jerr := err.(type) { case *json.UnmarshalTypeError: lnum, cnum, _ := jr.LineAndChar(jerr.Offset) log.Fatalf("Unmarshall error in config file in %s at %d:%d (offset %d bytes): %s", jerr.Field, lnum, cnum, jerr.Offset, jerr.Error()) case *json.SyntaxError: lnum, cnum, _ := jr.LineAndChar(jerr.Offset) log.Fatalf("Syntax error in config file at %d:%d (offset %d bytes): %s", lnum, cnum, jerr.Offset, jerr.Error()) default: log.Fatal("Failed to parse config file: ", err) } } } err := store.Store.Open(1, config.StoreConfig) defer store.Store.Close() adapterVersion := store.Store.GetAdapterVersion() databaseVersion := 0 if store.Store.IsOpen() { databaseVersion = store.Store.GetDbVersion() } log.Printf("Database adapter: '%s'; version: %d", store.Store.GetAdapterName(), adapterVersion) var created bool if err != nil { if strings.Contains(err.Error(), "Database not initialized") { if *noInit { log.Fatalln("Database not found.") } log.Println("Database not found. Creating.") err = store.Store.InitDb(config.StoreConfig, false) if err == nil { log.Println("Database successfully created.") created = true } } else if strings.Contains(err.Error(), "Invalid database version") { msg := "Wrong DB version: expected " + strconv.Itoa(adapterVersion) + ", got " + strconv.Itoa(databaseVersion) + "." if *reset { log.Println(msg, "Reset Requested. Dropping and recreating the database.") err = store.Store.InitDb(config.StoreConfig, true) if err == nil { log.Println("Database successfully reset.") } } else if *upgrade { if databaseVersion > adapterVersion { log.Fatalln(msg, "Unable to upgrade: database has greater version than the adapter.") } log.Println(msg, "Upgrading the database.") err = store.Store.UpgradeDb(config.StoreConfig) if err == nil { log.Println("Database successfully upgraded.") } } else { log.Fatalln(msg, "Use --reset to reset, --upgrade to upgrade.") } } else { log.Fatalln("Failed to init DB adapter:", err) } } else if *reset { log.Println("Reset requested. Dropping and recreating the database.") err = store.Store.InitDb(config.StoreConfig, true) if err == nil { log.Println("Database successfully reset.") } } else { log.Println("Database exists, version is correct.") } if err != nil { log.Fatalln("Failure:", err) } if *reset || created { genDb(&data, config.P2PDeleteEnabled) } else if len(data.Users) > 0 { log.Println("Sample data ignored.") } // Promote existing user account to root if *makeRoot != "" { adapter := store.Store.GetAdapter() userId := types.ParseUserId(*makeRoot) if userId.IsZero() { log.Fatalf("Must specify a valid user ID '%s' to promote to ROOT", *makeRoot) } if err := adapter.AuthUpdRecord(userId, "basic", "", auth.LevelRoot, nil, time.Time{}); err != nil { log.Fatalln("Failed to promote user to ROOT", err) } log.Printf("User '%s' promoted to ROOT", *makeRoot) } // Create root user account. if *addRoot != "" { var password string parts := strings.Split(*addRoot, ":") uname := parts[0] if len(uname) < 3 { log.Fatalf("Failed to create a ROOT user: username '%s' is too short", uname) } if len(parts) == 1 || parts[1] == "" { password = getPassword(10) } else { password = parts[1] } var user types.User user.Public = &card{ Fn: "ROOT " + uname, } store.Users.Create(&user, nil) if _, err := store.Users.Create(&user, nil); err != nil { log.Fatalln("Failed to create ROOT user:", err) } authHandler := store.Store.GetAuthHandler("basic") if _, err := authHandler.AddRecord(&auth.Rec{Uid: user.Uid(), AuthLevel: auth.LevelRoot}, []byte(uname+":"+password), ""); err != nil { store.Users.Delete(user.Uid(), true) log.Fatalln("Failed to add ROOT auth record:", err) } log.Printf("ROOT user created: '%s:%s'", uname, password) } log.Println("All done.") os.Exit(0) } ================================================ FILE: tinode-db/tinode.conf ================================================ { "p2p_delete_enabled": true, "store_config": { "uid_key": "la6YsO+bNX/+XIkOqc5Svw==", "use_adapter": "", "adapters": { "postgres": { "database": "tinode", "dsn": "postgresql://postgres:postgres@localhost:5432/tinode?sslmode=disable" }, "mysql": { "database": "tinode", "dsn": "root@tcp(localhost)/tinode?parseTime=true&collation=utf8mb4_unicode_ci" }, "rethinkdb": { "database": "tinode", "addresses": "localhost:28015" }, "mongodb": { "database": "tinode", "addresses": "localhost:27017", //"replica_set": "rs0", //"auth_source": "admin", //"username": "tinode", //"password": "tinode", } } } } ================================================ FILE: tn-cli/CODE-STRUCTURE.md ================================================ # tn-cli Refactoring Summary The code is organized into following focused modules: ## New Structure ### 1. **tn-cli.py** (Entry Point - ~120 lines) - Command-line argument parsing - Application initialization - Version handling - Authentication setup (token, basic, cookie) - Macro loading - Entry point that calls `run()` from client module **Key functions:** - `exception_hook()` - Crash handler - `if __name__ == '__main__'` - Main entry point --- ### 2. **utils.py** (Utility Functions - ~200 lines) - Helper functions and data structures - File/image processing utilities - Encoding and parsing functions **Key functions:** - `dotdict` - Dictionary with dot notation access - `makeTheCard()` - Pack user profile data - `inline_image()` - Create drafty image messages - `attachment()` - Create drafty attachment messages - `encode_to_bytes()` - Convert objects to bytes - `parse_cred()` - Parse credentials - `parse_trusted()` - Parse trusted values **Constants:** - `MAX_INBAND_ATTACHMENT_SIZE` - `MAX_EXTERN_ATTACHMENT_SIZE` - `MAX_IMAGE_DIM` - `DELETE_MARKER` - `TINODE_DEL` --- ### 3. **commands.py** (Command Parsing & Message Building - ~850 lines) - Command-line parsing for all commands - Protobuf message construction - Variable dereferencing - Command serialization **Key functions:** - `parse_input()` - Parse command line input - `parse_cmd()` - Create argument parsers - `serialize_cmd()` - Convert commands to protobuf - `derefVals()` / `getVar()` - Variable dereferencing - Message builders: `hiMsg()`, `accMsg()`, `loginMsg()`, `subMsg()`, `leaveMsg()`, `pubMsg()`, `getMsg()`, `setMsg()`, `delMsg()`, `noteMsg()` - File operations: `upload()`, `fileUpload()`, `fileDownload()` - `print_server_params()` - Log server info --- ### 4. **client.py** (gRPC Client & Communication - ~260 lines) - gRPC connection management - Message generation and streaming - Server response handling - Login/authentication handling - Cookie management **Key functions:** - `run()` - Main client loop - `gen_message()` - Generate outgoing messages - `handle_ctrl()` - Handle server control responses - `handle_login()` - Process login response - `save_cookie()` / `read_cookie()` - Cookie persistence - `pop_from_output_queue()` - Output queue management --- ### 5. **input_handler.py** (User Input - ~70 lines) - Terminal input reading - Multi-line input support - Interactive and non-interactive modes **Key functions:** - `stdin()` - Main input loop - `readLinesFromStdin()` - Read with prompt support --- ### 6. **tn_globals.py** (Shared Global State - ~104 lines) - Global variables shared across all modules - Asynchronous I/O queue management - Utility functions for logging and output - Protobuf to JSON conversion **Key variables:** - `OnCompletion` - Dictionary of callbacks for server responses - `WaitingFor` - Outstanding synchronous command request - `AuthToken` - Current authentication token - `InputQueue` / `OutputQueue` - Async I/O queues - `InputThread` - Background input thread - `IsInteractive` - Detect if running in interactive mode - `Prompt` - PromptSession for interactive input - `DefaultUser` / `DefaultTopic` - Default context values - `Variables` - Store command execution results - `Connection` - gRPC connection to server - `Verbose` - Extended logging flag **Key functions:** - `printout()` - Print in interactive mode only - `printerr()` - Write to stderr - `stdout()` / `stdoutln()` - Async output to stdout - `clip_long_string()` - Shorten long strings for logging - `to_json()` - Convert protobuf messages to JSON --- ### 7. **macros.py** (Command Macros - ~341 lines) - High-level command macros that expand into basic commands - Simplifies complex multi-step operations - Requires root privileges for most operations **Macro base class:** - `Macro` - Base class for all macros with parsing and execution **Available macros:** - `usermod` - Modify user account (suspend/unsuspend, update theCard, trusted values) - `resolve` - Resolve login name to user ID - `passwd` - Set user's password - `useradd` - Create new user account with credentials - `chacs` - Change default permissions/acs for a user - `userdel` - Delete user account (soft or hard delete) - `chcred` - Add/delete/validate user credentials - `thecard` - Print user's public/private data or credentials **Key functions:** - `parse_macro()` - Find parser for macro command - `Macro.expand()` - Expand macro to list of basic commands - `Macro.run()` - Execute macro or explain expansion **Macro dictionary:** - `Macros` - Dictionary mapping macro names to instances --- ## Module Dependencies ``` tn-cli.py ├── tn_globals ├── client (run, read_cookie) └── commands (set_macros_module) client.py ├── tn_globals ├── tinode_grpc (pb, pbx) ├── utils (dotdict) ├── input_handler (stdin) └── commands (hiMsg, loginMsg, serialize_cmd) commands.py ├── tn_globals ├── tinode_grpc (pb, pbx) ├── utils (makeTheCard, inline_image, attachment, etc.) └── client (handle_ctrl, handle_login, save_cookie) [for specific commands] utils.py ├── tn_globals └── tinode_grpc (pb) input_handler.py └── tn_globals macros.py └── tn_globals tn_globals.py └── (no dependencies - provides shared state) ``` ## Why 1. **Separation of Concerns**: Each module has a clear, focused responsibility 2. **Maintainability**: Easier to find and modify specific functionality 3. **Testability**: Individual modules can be tested independently 4. **Readability**: Smaller files are easier to understand 5. **Reusability**: Utilities and client code can be reused 6. **Extensibility**: Easy to add new macros or commands without modifying core logic 7. **Shared State Management**: `tn_globals.py` provides centralized state accessible to all modules ## Usage Run the application using: ```bash python3 tn-cli.py [arguments] ``` Or make it executable: ```bash chmod +x tn-cli.py ./tn-cli.py [arguments] ``` ================================================ FILE: tn-cli/LICENSE ================================================ Code in this folder is licensed under Apache 2.0 http://www.apache.org/licenses/LICENSE-2.0 ================================================ FILE: tn-cli/README.md ================================================ # Command Line Client for Tinode This is a scriptable command line chat client. It's written in Python and can be used to extend Tinode using [gRPC](https://grpc.io) [API](../pbx/). Python 2.7 or 3.4+ is required. PIP 9.0.1 or newer is required. Install dependencies: ``` $ python -m pip install -r requirements.txt ``` Run the client from the command line: ``` python tn-cli.py --login-basic=alice:alice123 ``` If you are updating an existent installation, make sure the `tinode_grpc` version matches the [server](../server/) version. Upgrade `tinode_grpc` if needed: ``` python -m pip install --upgrade tinode_grpc==X.XX.XX ``` where `X.XX.XX` is the version number which must match the server version number. The client takes optional parameters: * `--host` is the address of the gRPC server to connect to; default `localhost:16060`. * `--web-host` is the address of Tinode web server, used for file uploads only; default `localhost:6060`. * `--ssl` the server requires a secure connection (SSL) * `--ssl-host` the domain name to use for SNI if different from the `--host` domain name. * `--login-basic` is the `login:password` to be authenticated with. * `--login-token` is the token to be authenticated with. * `--login-cookie` direct the client to read the token from the cookie file `.tn-cli-cookie` generated during an earlier login. * `--no-login` do not login even if cookie file is present; this is the default in non-interactive (scripted) mode. * `--no-cookie` do not save cookie on successful login; this is the default in non-interactive (scripted) mode. * `--api-key` web API key for file uploads; default `AQEAAAABAAD_rAp4DJh05a1HAwFT3A6K` * `--load-macros` path to a macro file. * `--verbose` log incoming and outgoing messages as JSON. * `--background` start interactive session in background; non-interactive sessions are always started in background. If multiple `login-XYZ` are provided, `login-cookie` is considered first, then `login-token` then `login-basic`. Authentication with token (and cookie) is much faster than with the username-password pair. ## Commands Type ` -h` for help See some of these commands in use in the [sample-script.txt](sample-script.txt). Try it as ``` python tn-cli.py < sample-script.txt ``` ### Local (non-networking) * `.await` - issue a gRPC call and wait for completion, optionally assign result to a variable. * `.delmark` - use custom delete marker instead of default `DEL!`; needed when some value is to be removed rather than set to blank. * `.exit` - terminate execution and exit the CLI; also `.quit`. * `.log` - write a value of a variable to `stdout`. * `.must` - issue a gRPC call and wait for completion, optionally assign result to a variable; raise an exception if result is not a success. * `.quit` - terminate execution and exit the CLI; also `.exit`. * `.sleep` - suspend the process for a number of milliseconds. * `.use` - set default user (on_behalf_of user) or topic. * `.verbose` - toggle logging verbosity. ### gRPC calls * `acc` - create or modify an account * `login` - authenticate current session * `sub` - subscribe to topic * `leave` - detach or unsubscribe from topic * `pub` - post message to topic * `get` - query topic for metadata or messages * `set` - update topic metadata * `del` - delete message(s), topic, subscription, or user * `note` - send notification * `file` - upload or download large file out of band ### HTTP requests * `upload` - (deprecated, use `file`) upload file out of band ### Macros Macros are high-level wrappers for series of gRPC calls. Currently, the following macros are [available](macros.py): * `chacs` - change default permissions/acs for a user (requires root privileges) * `chcred` - add or delete a credential for a user (requires root privileges) * `passwd` - set user's password (requires root privileges) * `resolve` - resolve login and print the corresponding user id * `useradd` - create a new user account * `userdel` - delete user account (requires root privileges) * `usermod` - modify user account (requires root privileges) * `thecard` - print user's public and private info (requires root privileges) You can define your own macros in [macros.py](macros.py) or create a separate python module (you can load it via `--load-macros`). Refer to [macros.py](macros.py) for examples. ## Connecting to secure (HTTPS) server If the server is configured to use TLS, i.e. running as `httpS://my-server.example.com/`, the gRPC endpoint also uses the same SSL certificate. In that case add the `--ssl` option. If you want to connect to the secure gRPC endpoint over a local network or under a different name i.e. as `localhost` instead of `my-server.example.com` in this example, you must specify the SSL domain name to use, otherwise the server will not be able to find the right SSL certificate: ``` python tn-cli.py --host=localhost:6001 --ssl --ssl-host=my-server.example.com ``` The `--ssl-host` option makes the connection susceptible to the [Man-in-the-middle attack](https://en.wikipedia.org/wiki/Man-in-the-middle_attack), so don't do it over public networks. ## Crash on shutdown Python 3.6 sometimes crashes on shutdown with a message `Fatal Python error: PyImport_GetModuleDict: no module dictionary!`. That happens because Python is buggy: https://bugs.python.org/issue26153 ================================================ FILE: tn-cli/client.py ================================================ """Tinode gRPC client operations and message handling.""" from __future__ import print_function import grpc import json import sys import time from tinode_grpc import pb from tinode_grpc import pbx import tn_globals from tn_globals import printerr, stdoutln, to_json from utils import dotdict # 5 seconds timeout for .await/.must commands. AWAIT_TIMEOUT = 5 # Handle {ctrl} server response def handle_ctrl(ctrl): # Run code on command completion func = tn_globals.OnCompletion.get(ctrl.id) if func: del tn_globals.OnCompletion[ctrl.id] if ctrl.code >= 200 and ctrl.code < 400: func(ctrl.params) if tn_globals.WaitingFor and tn_globals.WaitingFor.await_id == ctrl.id: if 'varname' in tn_globals.WaitingFor: tn_globals.Variables[tn_globals.WaitingFor.varname] = ctrl if tn_globals.WaitingFor.failOnError and ctrl.code >= 400: raise Exception(str(ctrl.code) + " " + ctrl.text) tn_globals.WaitingFor = None topic = " (" + str(ctrl.topic) + ")" if ctrl.topic else "" stdoutln("\r<= " + str(ctrl.code) + " " + ctrl.text + topic) # Lambda for handling login def handle_login(params): if params == None: return None # Protobuf map 'params' is a map which is not a python object or a dictionary. Convert it. nice = {} for p in params: nice[p] = json.loads(params[p]) stdoutln("Authenticated as", nice.get('user')) tn_globals.AuthToken = nice.get('token') return nice # Save cookie to file after successful login. def save_cookie(params): if params == None: return try: cookie = open('.tn-cli-cookie', 'w') json.dump(handle_login(params), cookie) cookie.close() except Exception as err: stdoutln("Failed to save authentication cookie", err) # Read cookie file for logging in with the cookie. def read_cookie(): try: cookie = open('.tn-cli-cookie', 'r') params = json.load(cookie) cookie.close() return params.get("token") except Exception as err: printerr("Missing or invalid cookie file '.tn-cli-cookie'", err) return None def pop_from_output_queue(): if tn_globals.OutputQueue.empty(): return False sys.stdout.write("\r<= "+tn_globals.OutputQueue.get()) sys.stdout.flush() return True # Generator of protobuf messages. def gen_message(scheme, secret, args): """Client message generator: reads user input as string, converts to pb.ClientMsg, and yields""" import random import threading from input_handler import stdin from commands import hiMsg, loginMsg, serialize_cmd random.seed() id = random.randint(10000,60000) # Asynchronous input-output tn_globals.InputThread = threading.Thread(target=stdin, args=(tn_globals.InputQueue,)) tn_globals.InputThread.daemon = True tn_globals.InputThread.start() try: from importlib.metadata import version except ImportError: from importlib_metadata import version import platform APP_NAME = "tn-cli" APP_VERSION = "3.0.1" LIB_VERSION = version("tinode_grpc") GRPC_VERSION = version("grpcio") user_agent = APP_NAME + "/" + APP_VERSION + " (" + \ platform.system() + "/" + platform.release() + "); gRPC-python/" + LIB_VERSION + "+" + GRPC_VERSION msg = hiMsg(id, args.background, user_agent, LIB_VERSION) if tn_globals.Verbose: stdoutln("\r=> " + to_json(msg)) yield msg if scheme != None: id += 1 login = lambda:None setattr(login, 'scheme', scheme) setattr(login, 'secret', secret) setattr(login, 'cred', None) msg = loginMsg(id, login, args) if tn_globals.Verbose: stdoutln("\r=> " + to_json(msg)) yield msg print_prompt = True while True: try: if not tn_globals.WaitingFor and tn_globals.InputQueue: id += 1 inp = tn_globals.InputQueue.popleft() if inp == 'exit' or inp == 'quit' or inp == '.exit' or inp == '.quit': # Drain the output queue. while pop_from_output_queue(): pass return pbMsg, cmd = serialize_cmd(inp, id, args) print_prompt = tn_globals.IsInteractive if isinstance(cmd, list): # Push the expanded macro back on the command queue. tn_globals.InputQueue.extendleft(reversed(cmd)) continue if pbMsg != None: if not tn_globals.IsInteractive: sys.stdout.write("=> " + inp + "\n") sys.stdout.flush() if cmd.synchronous: cmd.await_ts = time.time() cmd.await_id = str(id) tn_globals.WaitingFor = cmd if not hasattr(cmd, 'no_yield'): if tn_globals.Verbose: stdoutln("\r=> " + to_json(pbMsg)) yield pbMsg elif not tn_globals.OutputQueue.empty(): pop_from_output_queue() print_prompt = tn_globals.IsInteractive else: if print_prompt: sys.stdout.write("tn> ") sys.stdout.flush() print_prompt = False if tn_globals.WaitingFor: if time.time() - tn_globals.WaitingFor.await_ts > AWAIT_TIMEOUT: stdoutln("Timeout while waiting for '{0}' response".format(tn_globals.WaitingFor.cmd)) tn_globals.WaitingFor = None if tn_globals.IsInteractive: time.sleep(0.1) else: time.sleep(0.01) except Exception as err: stdoutln("Exception in generator: {0}".format(err)) # The main processing loop: send messages to server, receive responses. def run(args, schema, secret): failed = False try: from prompt_toolkit import PromptSession if tn_globals.IsInteractive: tn_globals.Prompt = PromptSession() # Create channel with default credentials. tn_globals.Connection = None if args.ssl: opts = (('grpc.ssl_target_name_override', args.ssl_host),) if args.ssl_host else None tn_globals.Connection = grpc.secure_channel(args.host, grpc.ssl_channel_credentials(), opts) else: tn_globals.Connection = grpc.insecure_channel(args.host) # Call the server stream = pbx.NodeStub(tn_globals.Connection).MessageLoop(gen_message(schema, secret, args)) # Read server responses for msg in stream: if tn_globals.Verbose: stdoutln("\r<= " + to_json(msg)) if msg.HasField("ctrl"): handle_ctrl(msg.ctrl) elif msg.HasField("meta"): what = [] if len(msg.meta.sub) > 0: what.append("sub") if msg.meta.HasField("desc"): what.append("desc") if msg.meta.HasField("del"): what.append("del") if len(msg.meta.tags) > 0: what.append("tags") stdoutln("\r<= meta " + ",".join(what) + " " + msg.meta.topic) if tn_globals.WaitingFor and tn_globals.WaitingFor.await_id == msg.meta.id: if 'varname' in tn_globals.WaitingFor: tn_globals.Variables[tn_globals.WaitingFor.varname] = msg.meta tn_globals.WaitingFor = None elif msg.HasField("data"): stdoutln("\n\rFrom: " + msg.data.from_user_id) stdoutln("Topic: " + msg.data.topic) stdoutln("Seq: " + str(msg.data.seq_id)) if msg.data.head: stdoutln("Headers:") for key in msg.data.head: stdoutln("\t" + key + ": "+str(msg.data.head[key])) stdoutln(json.loads(msg.data.content)) elif msg.HasField("pres"): # 'ON', 'OFF', 'UA', 'UPD', 'GONE', 'ACS', 'TERM', 'MSG', 'READ', 'RECV', 'DEL', 'TAGS', 'AUX' what = pb.ServerPres.What.Name(msg.pres.what) stdoutln("\r<= pres " + what + " " + msg.pres.topic) elif msg.HasField("info"): switcher = { pb.READ: 'READ', pb.RECV: 'RECV', pb.KP: 'KP', pb.CALL: 'CALL' } stdoutln("\rMessage #" + str(msg.info.seq_id) + " " + switcher.get(msg.info.what, "unknown") + " by " + msg.info.from_user_id + "; topic=" + msg.info.topic + " (" + msg.topic + ")") else: stdoutln("\rMessage type not handled" + str(msg)) except grpc.RpcError as err: # print(err) printerr("gRPC failed with {0}: {1}".format(err.code(), err.details())) failed = True except Exception as ex: printerr("Request failed: {0}".format(ex)) failed = True finally: from tn_globals import printout printout('Shutting down...') tn_globals.Connection.close() if tn_globals.InputThread != None: tn_globals.InputThread.join(0.3) return 1 if failed else 0 ================================================ FILE: tn-cli/commands.py ================================================ """Command parsing and message construction for tn-cli.""" from __future__ import print_function import argparse import base64 import json import mimetypes import os import re import requests import shlex import threading import time from tinode_grpc import pb from tinode_grpc import pbx import tn_globals from tn_globals import printout, stdoutln from utils import ( makeTheCard, inline_image, attachment, encode_to_bytes, parse_cred, parse_trusted, dotdict, DELETE_MARKER, TINODE_DEL ) APP_NAME = "tn-cli" APP_VERSION = "3.0.1" PROTOCOL_VERSION = "0" # Regex to match and parse subscripted entries in variable paths. RE_INDEX = re.compile(r"(\w+)\[(\w+)\]") # Macros module (may be None). macros = None def set_macros_module(m): """Set the macros module for use in command parsing.""" global macros macros = m # Create proto for ClientExtra def pack_extra(cmd): return pb.ClientExtra(on_behalf_of=tn_globals.DefaultUser, auth_level=pb.ROOT if cmd.as_root else pb.NONE) # Read a value in the server response using dot notation, i.e. # $user.params.token or $meta.sub[1].user def getVar(path): if not path.startswith("$"): return path parts = path.split('.') if parts[0] not in tn_globals.Variables: return None var = tn_globals.Variables[parts[0]] if len(parts) > 1: parts = parts[1:] for p in parts: x = None m = RE_INDEX.match(p) if m: p = m.group(1) if m.group(2).isdigit(): x = int(m.group(2)) else: x = m.group(2) var = getattr(var, p) if x or x == 0: var = var[x] if isinstance(var, bytes): var = var.decode('utf-8') return var # Dereference values, i.e. cmd.val == $usr => cmd.val == def derefVals(cmd): for key in dir(cmd): if not key.startswith("__") and key != 'varname': val = getattr(cmd, key) if type(val) is str and val.startswith("$"): setattr(cmd, key, getVar(val)) return cmd # Constructing individual messages # {hi} def hiMsg(id, background, user_agent, lib_version): tn_globals.OnCompletion[str(id)] = lambda params: print_server_params(params) return pb.ClientMsg(hi=pb.ClientHi(id=str(id), user_agent=user_agent, ver=lib_version, lang="EN", background=background)) # {acc} def accMsg(id, cmd, ignored): if cmd.uname: cmd.scheme = 'basic' if cmd.password == None: cmd.password = '' cmd.secret = str(cmd.uname) + ":" + str(cmd.password) if cmd.secret: if cmd.scheme == None: cmd.scheme = 'basic' cmd.secret = cmd.secret.encode('utf-8') else: cmd.secret = b'' state = None if cmd.suspend == 'true': state = 'susp' elif cmd.suspend == 'false': state = 'ok' cmd.public = encode_to_bytes(makeTheCard(cmd.fn, cmd.note, cmd.photo)) cmd.private = encode_to_bytes(cmd.private) return pb.ClientMsg(acc=pb.ClientAcc(id=str(id), user_id=cmd.user, state=state, scheme=cmd.scheme, secret=cmd.secret, login=cmd.do_login, tags=cmd.tags.split(",") if cmd.tags else None, desc=pb.SetDesc(default_acs=pb.DefaultAcsMode(auth=cmd.auth, anon=cmd.anon), public=cmd.public, private=cmd.private, trusted=encode_to_bytes(parse_trusted(cmd.trusted))), cred=parse_cred(cmd.cred)), extra=pack_extra(cmd)) # {login} def loginMsg(id, cmd, args): if cmd.secret == None: if cmd.uname == None: cmd.uname = '' if cmd.password == None: cmd.password = '' cmd.secret = str(cmd.uname) + ":" + str(cmd.password) cmd.secret = cmd.secret.encode('utf-8') elif cmd.scheme == "basic": # Assuming secret is a uname:password string. cmd.secret = str(cmd.secret).encode('utf-8') else: # All other schemes: assume secret is a base64-encoded string cmd.secret = base64.b64decode(cmd.secret) from client import handle_login, save_cookie msg = pb.ClientMsg(login=pb.ClientLogin(id=str(id), scheme=cmd.scheme, secret=cmd.secret, cred=parse_cred(cmd.cred))) if args.no_cookie or not tn_globals.IsInteractive: tn_globals.OnCompletion[str(id)] = lambda params: handle_login(params) else: tn_globals.OnCompletion[str(id)] = lambda params: save_cookie(params) return msg # {sub} def subMsg(id, cmd, ignored): if not cmd.topic: cmd.topic = tn_globals.DefaultTopic if cmd.get_query: cmd.get_query = pb.GetQuery(what=" ".join(cmd.get_query.split(","))) cmd.public = encode_to_bytes(makeTheCard(cmd.fn, cmd.note, cmd.photo)) cmd.private = TINODE_DEL if cmd.private == DELETE_MARKER else encode_to_bytes(cmd.private) return pb.ClientMsg(sub=pb.ClientSub(id=str(id), topic=cmd.topic, set_query=pb.SetQuery( desc=pb.SetDesc(public=cmd.public, private=cmd.private, trusted=encode_to_bytes(parse_trusted(cmd.trusted)), default_acs=pb.DefaultAcsMode(auth=cmd.auth, anon=cmd.anon)), sub=pb.SetSub(mode=cmd.mode), tags=cmd.tags.split(",") if cmd.tags else None), get_query=cmd.get_query), extra=pack_extra(cmd)) # {leave} def leaveMsg(id, cmd, ignored): if not cmd.topic: cmd.topic = tn_globals.DefaultTopic return pb.ClientMsg(leave=pb.ClientLeave(id=str(id), topic=cmd.topic, unsub=cmd.unsub), extra=pack_extra(cmd)) # {pub} def pubMsg(id, cmd, ignored): if not cmd.topic: cmd.topic = tn_globals.DefaultTopic head = {} if cmd.drafty or cmd.image or cmd.attachment: head['mime'] = encode_to_bytes('text/x-drafty') # Excplicitly provided 'mime' will override the one assigned above. if cmd.head: for h in cmd.head.split(","): key, val = h.split(":") head[key] = encode_to_bytes(val) content = json.loads(cmd.drafty) if cmd.drafty \ else inline_image(cmd.image) if cmd.image \ else attachment(cmd.attachment) if cmd.attachment \ else cmd.content if not content: return None return pb.ClientMsg(pub=pb.ClientPub(id=str(id), topic=cmd.topic, no_echo=True, head=head, content=encode_to_bytes(content)), extra=pack_extra(cmd)) # {get} def getMsg(id, cmd, ignored): if not cmd.topic: cmd.topic = tn_globals.DefaultTopic what = [] if cmd.desc: what.append("desc") if cmd.sub: what.append("sub") if cmd.tags: what.append("tags") if cmd.data: what.append("data") if cmd.cred: what.append("cred") return pb.ClientMsg(get=pb.ClientGet(id=str(id), topic=cmd.topic, query=pb.GetQuery(what=" ".join(what))), extra=pack_extra(cmd)) # {set} def setMsg(id, cmd, ignored): if not cmd.topic: cmd.topic = tn_globals.DefaultTopic if cmd.public == None: cmd.public = encode_to_bytes(makeTheCard(cmd.fn, cmd.note, cmd.photo)) else: cmd.public = TINODE_DEL if cmd.public == DELETE_MARKER else encode_to_bytes(cmd.public) cmd.private = TINODE_DEL if cmd.private == DELETE_MARKER else encode_to_bytes(cmd.private) cred = parse_cred(cmd.cred) if cred: if len(cred) > 1: stdoutln('Warning: multiple credentials specified. Will use only the first one.') cred = cred[0] return pb.ClientMsg(set=pb.ClientSet(id=str(id), topic=cmd.topic, query=pb.SetQuery( desc=pb.SetDesc(default_acs=pb.DefaultAcsMode(auth=cmd.auth, anon=cmd.anon), public=cmd.public, private=cmd.private, trusted=encode_to_bytes(parse_trusted(cmd.trusted))), sub=pb.SetSub(user_id=cmd.user, mode=cmd.mode), tags=cmd.tags.split(",") if cmd.tags else None, cred=cred)), extra=pack_extra(cmd)) # {del} def delMsg(id, cmd, ignored): if not cmd.what: stdoutln("Must specify what to delete") return None enum_what = None before = None seq_list = None cred = None if cmd.what == 'msg': enum_what = pb.ClientDel.MSG cmd.topic = cmd.topic if cmd.topic else tn_globals.DefaultTopic if not cmd.topic: stdoutln("Must specify topic to delete messages") return None if cmd.user: stdoutln("Unexpected '--user' parameter") return None if not cmd.seq: stdoutln("Must specify message IDs to delete") return None if cmd.seq == 'all': seq_list = [pb.SeqRange(low=1, hi=0x8FFFFFF)] else: # Split a list like '1,2,3,10-22' into ranges. try: seq_list = [] for item in cmd.seq.split(','): if '-' in item: low, hi = [int(x.strip()) for x in item.split('-')] if low>=hi or low<=0: stdoutln("Invalid message ID range {0}-{1}".format(low, hi)) return None seq_list.append(pb.SeqRange(low=low, hi=hi)) else: seq_list.append(pb.SeqRange(low=int(item.strip()))) except ValueError as err: stdoutln("Invalid message IDs: {0}".format(err)) return None elif cmd.what == 'sub': cmd.topic = cmd.topic if cmd.topic else tn_globals.DefaultTopic cmd.user = cmd.user if cmd.user else tn_globals.DefaultUser if not cmd.user or not cmd.topic: stdoutln("Must specify topic and user to delete subscription") return None enum_what = pb.ClientDel.SUB elif cmd.what == 'topic': cmd.topic = cmd.topic if cmd.topic else tn_globals.DefaultTopic if cmd.user: stdoutln("Unexpected '--user' parameter") return None if not cmd.topic: stdoutln("Must specify topic to delete") return None enum_what = pb.ClientDel.TOPIC elif cmd.what == 'user': cmd.user = cmd.user if cmd.user else tn_globals.DefaultUser if cmd.topic: stdoutln("Unexpected '--topic' parameter") return None enum_what = pb.ClientDel.USER elif cmd.what == 'cred': if cmd.user: stdoutln("Unexpected '--user' parameter") return None if cmd.topic != 'me': stdoutln("Topic must be 'me'") return None cred = parse_cred(cmd.cred) if cred is None: stdoutln("Failed to parse credential '{0}'".format(cmd.cred)) return None cred = cred[0] enum_what = pb.ClientDel.CRED else: stdoutln("Unrecognized delete option '", cmd.what, "'") return None msg = pb.ClientMsg(extra=pack_extra(cmd)) # Field named 'del' conflicts with the keyword 'del. This is a work around. xdel = getattr(msg, 'del') """ setattr(msg, 'del', pb.ClientDel(id=str(id), topic=topic, what=enum_what, hard=hard, del_seq=seq_list, user_id=user)) """ xdel.id = str(id) xdel.what = enum_what if cmd.hard != None: xdel.hard = cmd.hard if seq_list != None: xdel.del_seq.extend(seq_list) if cmd.user != None: xdel.user_id = cmd.user if cmd.topic != None: xdel.topic = cmd.topic if cred != None: xdel.cred.MergeFrom(cred) return msg # {note} def noteMsg(id, cmd, ignored): if not cmd.topic: cmd.topic = tn_globals.DefaultTopic enum_what = None cmd.seq = int(cmd.seq) if cmd.what == 'kp': enum_what = pb.KP cmd.seq = None elif cmd.what == 'read': enum_what = pb.READ elif cmd.what == 'recv': enum_what = pb.RECV elif cmd.what == 'call': enum_what = pb.CALL enum_event = None if enum_what == pb.CALL: if cmd.what == 'accept': enum_event = pb.ACCEPT elif cmd.what == 'answer': enum_event = pb.ANSWER elif cmd.what == 'ice-candidate': enum_event = pb.ICE_CANDIDATE elif cmd.what == 'hang-up': enum_event = pb.HANG_UP elif cmd.what == 'offer': enum_event = pb.OFFER elif cmd.what == 'ringing': enum_event = pb.RINGING else: cmd.payload = None return pb.ClientMsg(note=pb.ClientNote(topic=cmd.topic, what=enum_what, seq_id=cmd.seq, event=enum_event, payload=cmd.payload), extra=pack_extra(cmd)) # Upload file out of band over HTTP(S) (not gRPC). def upload(id, cmd, args): try: from client import handle_ctrl scheme = 'https' if args.ssl else 'http' try: from importlib.metadata import version except ImportError: from importlib_metadata import version LIB_VERSION = version("tinode_grpc") result = requests.post( scheme + '://' + args.web_host + '/v' + PROTOCOL_VERSION + '/file/u/', headers = { 'X-Tinode-APIKey': args.api_key, 'X-Tinode-Auth': 'Token ' + tn_globals.AuthToken, 'User-Agent': APP_NAME + " " + APP_VERSION + "/" + LIB_VERSION }, data = {'id': id}, files = {'file': (cmd.filename, open(cmd.filename, 'rb'))}) handle_ctrl(dotdict(json.loads(result.text)['ctrl'])) except Exception as ex: stdoutln("Failed to upload '{0}'".format(cmd.filename), ex) return None def fileUpload(id, cmd, args): def iter_file(filepath, size=1024*1024): _, name = os.path.split(filepath) mimeType = mimetypes.guess_type(filepath)[0] with open(filepath, mode='rb') as fd: try: yield pb.FileUpReq(id=str(id), auth=pb.Auth(scheme='token', secret=tn_globals.AuthToken), topic="", meta=pb.FileMeta(name=name, mime_type=mimeType, size=0)) while True: chunk = fd.read(size) if chunk: yield pb.FileUpReq(content=chunk) else: # Finished. break except Exception as ex: stdoutln("Failed to read '{0}':".format(cmd.filename), ex) try: response = pbx.NodeStub(tn_globals.Connection).LargeFileReceive(iter_file(cmd.filename)) if response.code == 200: stdoutln("Upload OK: '{0}' ({1}), size={2}" .format(response.meta.name, response.meta.mime_type, response.meta.size)) else: stdoutln("Upload failed: {0} {1}".format(response.code, response.text)) except Exception as ex: stdoutln("Failed to upload '{0}':".format(cmd.filename), ex) def fileDownload(id, cmd, args): req = pb.FileDownReq(id=str(id), auth=pb.Auth(scheme='token', secret=tn_globals.AuthToken), uri=cmd.filename, if_modified="") # Call the server stream = pbx.NodeStub(tn_globals.Connection).LargeFileServe(req) # Read file chunks fd = None for chunk in stream: if chunk: if chunk.code >= 400: stdoutln("Failed to download '{0}': {1} {2}".format(cmd.filename, chunk.code, chunk.text)) break if chunk.code >= 300: stdoutln("Use HTTP {0} to download from {1}".format(chunk.code, chunk.redir_url)) break if not fd: fd = open(chunk.meta.name, mode='wb') fd.write(chunk.content) continue if fd: fd.close() # Given an array of parts, parse commands and arguments def parse_cmd(parts): parser = None if parts[0] == "acc": parser = argparse.ArgumentParser(prog=parts[0], description='Create or alter an account') parser.add_argument('--user', default='new', help='ID of the account to update') parser.add_argument('--scheme', default=None, help='authentication scheme, default=basic') parser.add_argument('--secret', default=None, help='secret for authentication') parser.add_argument('--uname', default=None, help='user name for basic authentication') parser.add_argument('--password', default=None, help='password for basic authentication') parser.add_argument('--do-login', action='store_true', help='login with the newly created account') parser.add_argument('--tags', action=None, help='tags for user discovery, comma separated list without spaces') parser.add_argument('--fn', default=None, help='user\'s human name') parser.add_argument('--photo', default=None, help='avatar file name') parser.add_argument('--private', default=None, help='user\'s private info') parser.add_argument('--note', default=None, help='user\'s description') parser.add_argument('--trusted', default=None, help='trusted markers: verified, staff, danger, prepend with rm- to remove, e.g. rm-verified') parser.add_argument('--auth', default=None, help='default access mode for authenticated users') parser.add_argument('--anon', default=None, help='default access mode for anonymous users') parser.add_argument('--cred', default=None, help='credentials, comma separated list in method:value format, e.g. email:test@example.com,tel:12345') parser.add_argument('--suspend', default=None, help='true to suspend the account, false to un-suspend') elif parts[0] == "del": parser = argparse.ArgumentParser(prog=parts[0], description='Delete message(s), subscription, topic, user') parser.add_argument('what', default=None, help='what to delete') parser.add_argument('--topic', default=None, help='topic being affected') parser.add_argument('--user', default=None, help='either delete this user or a subscription with this user') parser.add_argument('--seq', default=None, help='"all" or a list of comma- and dash-separated message IDs to delete, e.g. "1,2,9-12"') parser.add_argument('--hard', action='store_true', help='request to hard-delete') parser.add_argument('--cred', help='credential to delete in method:value format, e.g. email:test@example.com, tel:12345') elif parts[0] == "file": parser = argparse.ArgumentParser(prog=parts[0], description='Download or upload a large file') parser.add_argument('--what', default='down', choices=['down', 'up'], help='download \'down\' or upload \'up\'') parser.add_argument('filename', help='name of the file to upload') elif parts[0] == "get": parser = argparse.ArgumentParser(prog=parts[0], description='Query topic for messages or metadata') parser.add_argument('topic', nargs='?', default=argparse.SUPPRESS, help='topic to query') parser.add_argument('--topic', dest='topic', default=None, help='topic to query') parser.add_argument('--desc', action='store_true', help='query topic description') parser.add_argument('--sub', action='store_true', help='query topic subscriptions') parser.add_argument('--tags', action='store_true', help='query topic tags') parser.add_argument('--data', action='store_true', help='query topic messages') parser.add_argument('--cred', action='store_true', help='query account credentials') elif parts[0] == "leave": parser = argparse.ArgumentParser(prog=parts[0], description='Detach or unsubscribe from topic') parser.add_argument('topic', nargs='?', default=argparse.SUPPRESS, help='topic to detach from') parser.add_argument('--topic', dest='topic', default=None, help='topic to detach from') parser.add_argument('--unsub', action='store_true', help='detach and unsubscribe from topic') elif parts[0] == "login": parser = argparse.ArgumentParser(prog=parts[0], description='Authenticate current session') parser.add_argument('secret', nargs='?', default=argparse.SUPPRESS, help='secret for authentication') parser.add_argument('--scheme', default='basic', help='authentication schema, default=basic') parser.add_argument('--secret', dest='secret', default=None, help='secret for authentication') parser.add_argument('--uname', default=None, help='user name in basic authentication scheme') parser.add_argument('--password', default=None, help='password in basic authentication scheme') parser.add_argument('--cred', default=None, help='credentials, comma separated list in method:value:response format, e.g. email:test@example.com,tel:12345') elif parts[0] == "note": parser = argparse.ArgumentParser(prog=parts[0], description='Send notification to topic, ex "note kp"') parser.add_argument('topic', help='topic to notify') parser.add_argument('what', nargs='?', default='kp', const='kp', choices=['call', 'kp', 'read', 'recv'], help='notification type: kp (key press), recv, read - message received or read receipt') parser.add_argument('--seq', help='message ID being reported') parser.add_argument('--event', help='video call event', choices=['accept', 'answer', 'ice-candidate', 'hang-up', 'offer', 'ringing']) parser.add_argument('--payload', help='video call payload') elif parts[0] == "pub": parser = argparse.ArgumentParser(prog=parts[0], description='Send message to topic') parser.add_argument('topic', nargs='?', default=argparse.SUPPRESS, help='topic to publish to') parser.add_argument('--topic', dest='topic', default=None, help='topic to publish to') parser.add_argument('content', nargs='?', default=argparse.SUPPRESS, help='message to send') parser.add_argument('--head', help='message headers') parser.add_argument('--content', dest='content', help='message to send') parser.add_argument('--drafty', help='structured message to send, e.g. drafty content') parser.add_argument('--image', help='image file to insert into message (not implemented yet)') parser.add_argument('--attachment', help='file to send as an attachment (not implemented yet)') elif parts[0] == "set": parser = argparse.ArgumentParser(prog=parts[0], description='Update topic metadata') parser.add_argument('topic', help='topic to update') parser.add_argument('--fn', help='topic\'s title') parser.add_argument('--photo', help='avatar file name') parser.add_argument('--public', help='topic\'s public info, alternative to fn+photo+note') parser.add_argument('--private', help='topic\'s private info') parser.add_argument('--note', default=None, help='topic\'s description') parser.add_argument('--trusted', default=None, help='trusted markers: verified, staff, danger') parser.add_argument('--auth', help='default access mode for authenticated users') parser.add_argument('--anon', help='default access mode for anonymous users') parser.add_argument('--user', help='ID of the account to update') parser.add_argument('--mode', help='new value of access mode') parser.add_argument('--tags', help='tags for topic discovery, comma separated list without spaces') parser.add_argument('--cred', help='credential to add in method:value format, e.g. email:test@example.com, tel:12345') elif parts[0] == "sub": parser = argparse.ArgumentParser(prog=parts[0], description='Subscribe to topic') parser.add_argument('topic', nargs='?', default=argparse.SUPPRESS, help='topic to subscribe to') parser.add_argument('--topic', dest='topic', default=None, help='topic to subscribe to') parser.add_argument('--fn', default=None, help='topic\'s user-visible name') parser.add_argument('--photo', default=None, help='avatar file name') parser.add_argument('--private', default=None, help='topic\'s private info') parser.add_argument('--note', default=None, help='topic\'s description') parser.add_argument('--trusted', default=None, help='trusted markers: verified, staff, danger') parser.add_argument('--auth', default=None, help='default access mode for authenticated users') parser.add_argument('--anon', default=None, help='default access mode for anonymous users') parser.add_argument('--mode', default=None, help='new value of access mode') parser.add_argument('--tags', default=None, help='tags for topic discovery, comma separated list without spaces') parser.add_argument('--get-query', default=None, help='query for topic metadata or messages, comma separated list without spaces') elif parts[0] == "upload": parser = argparse.ArgumentParser(prog=parts[0], description='Upload file out of band over HTTP(S)') parser.add_argument('filename', help='name of the file to upload') elif macros: parser = macros.parse_macro(parts) if parser: try: parser.add_argument('--as_root', action='store_true', help='execute command at ROOT auth level') except Exception: # Ignore exception here: --as_root has been added already, macro parser is persistent. pass return parser # Parses command line into command and parameters. def parse_input(cmd): # Split line into parts using shell-like syntax. try: parts = shlex.split(cmd, comments=True) except Exception as err: printout('Error parsing command: ', err) return None if len(parts) == 0: return None parser = None varname = None synchronous = False failOnError = False if parts[0] == ".use": parser = argparse.ArgumentParser(prog=parts[0], description='Set default user or topic') parser.add_argument('--user', default="unchanged", help='ID of default (on_behalf_of) user') parser.add_argument('--topic', default="unchanged", help='Name of default topic') elif parts[0] == ".await" or parts[0] == ".must": # .await|.must [<$variable_name>] if len(parts) > 1: synchronous = True failOnError = parts[0] == ".must" if len(parts) > 2 and parts[1][0] == '$': # Varname is given varname = parts[1] parts = parts[2:] parser = parse_cmd(parts) else: # No varname parts = parts[1:] parser = parse_cmd(parts) elif parts[0] == ".log": parser = argparse.ArgumentParser(prog=parts[0], description='Write value of a variable to stdout') parser.add_argument('varname', help='name of the variable to print') elif parts[0] == ".sleep": parser = argparse.ArgumentParser(prog=parts[0], description='Pause execution') parser.add_argument('millis', type=int, help='milliseconds to wait') elif parts[0] == ".verbose": parser = argparse.ArgumentParser(prog=parts[0], description='Toggle logging verbosity') elif parts[0] == ".delmark": parser = argparse.ArgumentParser(prog=parts[0], description='Use custom delete maker instead of default DEL!') parser.add_argument('delmark', help='marker to use') else: parser = parse_cmd(parts) if not parser: printout("Unrecognized:", parts[0]) printout("Possible commands:") printout("\t.await\t\t- wait for completion of an operation") printout("\t.delmark\t- custom delete marker to use instead of default DEL!") printout("\t.exit\t\t- exit the program (also .quit)") printout("\t.log\t\t- write value of a variable to stdout") printout("\t.must\t\t- wait for completion of an operation, terminate on failure") printout("\t.sleep\t\t- pause execution") printout("\t.use\t\t- set default user (on_behalf_of) or topic") printout("\t.verbose\t- toggle logging verbosity on/off") printout("\tacc\t\t- create or alter an account") printout("\tdel\t\t- delete message(s), topic, subscription, or user") printout("\tfile\t\t- download or upload a large file") printout("\tget\t\t- query topic for metadata or messages") printout("\tleave\t\t- detach or unsubscribe from topic") printout("\tlogin\t\t- authenticate current session") printout("\tnote\t\t- send a notification") printout("\tpub\t\t- post message to topic") printout("\tset\t\t- update topic metadata") printout("\tsub\t\t- subscribe to topic") printout("\tupload\t\t- upload file out of band over HTTP(S)") printout("\tusermod\t\t- modify user account") printout("\n\tType -h for help") if macros: printout("\nMacro commands:") for key in sorted(macros.Macros): macro = macros.Macros[key] printout("\t%s\t\t- %s" % (macro.name(), macro.description())) return None try: args = parser.parse_args(parts[1:]) args.cmd = parts[0] args.synchronous = synchronous args.failOnError = failOnError if varname: args.varname = varname return args except SystemExit: return None # Process command-line input string: execute local commands, generate # protobuf messages for remote commands. def serialize_cmd(string, id, args): """Take string read from the command line, convert in into a protobuf message""" global DELETE_MARKER messages = { "acc": accMsg, "login": loginMsg, "sub": subMsg, "leave": leaveMsg, "pub": pubMsg, "get": getMsg, "set": setMsg, "del": delMsg, "note": noteMsg, } try: # Convert string into a dictionary cmd = parse_input(string) if cmd == None: return None, None elif cmd.cmd == "file": # Start async upload target = fileUpload if cmd.what == 'up' else fileDownload upload_thread = threading.Thread(target=target, args=(id, derefVals(cmd), args), name="file_"+cmd.filename) upload_thread.start() cmd.no_yield = True return True, cmd # Process dictionary elif cmd.cmd == ".log": stdoutln(getVar(cmd.varname)) return None, None elif cmd.cmd == ".use": if cmd.user != "unchanged": if cmd.user: if len(cmd.user) > 3 and cmd.user.startswith("usr"): tn_globals.DefaultUser = cmd.user else: stdoutln("Error: user ID '{}' is invalid".format(cmd.user)) else: tn_globals.DefaultUser = None stdoutln("Default user='{}'".format(tn_globals.DefaultUser)) if cmd.topic != "unchanged": if cmd.topic: if cmd.topic[:3] in ['me', 'fnd', 'sys', 'usr', 'grp', 'chn']: tn_globals.DefaultTopic = cmd.topic else: stdoutln("Error: topic '{}' is invalid".format(cmd.topic)) else: tn_globals.DefaultTopic = None stdoutln("Default topic='{}'".format(tn_globals.DefaultTopic)) return None, None elif cmd.cmd == ".sleep": stdoutln("Pausing for {}ms...".format(cmd.millis)) time.sleep(cmd.millis/1000.) return None, None elif cmd.cmd == ".verbose": tn_globals.Verbose = not tn_globals.Verbose stdoutln("Logging is {}".format("verbose" if tn_globals.Verbose else "normal")) return None, None elif cmd.cmd == ".delmark": DELETE_MARKER = cmd.delmark stdoutln("Using {} as delete marker".format(DELETE_MARKER)) return None, None elif cmd.cmd == "upload": # Start async upload upload_thread = threading.Thread(target=upload, args=(id, derefVals(cmd), args), name="Uploader_"+cmd.filename) upload_thread.start() cmd.no_yield = True return True, cmd elif cmd.cmd in messages: return messages[cmd.cmd](id, derefVals(cmd), args), cmd elif macros and cmd.cmd in macros.Macros: return True, macros.Macros[cmd.cmd].run(id, derefVals(cmd), args) else: stdoutln("Error: unrecognized: '{}'".format(cmd.cmd)) return None, None except Exception as err: stdoutln("Error in '{0}': {1}".format(string, err)) return None, None # Log server info. def print_server_params(params): servParams = [] for p in params: servParams.append(p + ": " + str(json.loads(params[p]))) stdoutln("\r<= Connected to server: " + "; ".join(servParams)) ================================================ FILE: tn-cli/input_handler.py ================================================ """User input handling for tn-cli.""" from __future__ import print_function import sys import tn_globals from tn_globals import printerr # Prints prompt and reads lines from stdin. def readLinesFromStdin(): if tn_globals.IsInteractive: while True: try: line = tn_globals.Prompt.prompt() yield line except EOFError as e: # Ctrl+D. break else: # iter(...) is a workaround for a python2 bug https://bugs.python.org/issue3907 for cmd in iter(sys.stdin.readline, ''): yield cmd # Stdin reads a possibly multiline input from stdin and queues it for asynchronous processing. def stdin(InputQueue): partial_input = "" try: for cmd in readLinesFromStdin(): cmd = cmd.strip() # Check for continuation symbol \ in the end of the line. if len(cmd) > 0 and cmd[-1] == "\\": cmd = cmd[:-1].rstrip() if cmd: if partial_input: partial_input += " " + cmd else: partial_input = cmd if tn_globals.IsInteractive: sys.stdout.write("... ") sys.stdout.flush() continue # Check if we have cached input from a previous multiline command. if partial_input: if cmd: partial_input += " " + cmd InputQueue.append(partial_input) partial_input = "" continue InputQueue.append(cmd) # Stop processing input if cmd == 'exit' or cmd == 'quit' or cmd == '.exit' or cmd == '.quit': return except Exception as ex: printerr("Exception in stdin", ex) InputQueue.append('exit') ================================================ FILE: tn-cli/macros.py ================================================ """Tinode command line macro definitions.""" import argparse import tn_globals from tn_globals import stdoutln class Macro: """Macro base class. The external callers are expected to access * self.parser - an instance of argparse.Argument parser which attempts to turn a list of tokens into the corresponding argparse command. * self.run() - executes the macro as instructed by the user.""" def __init__(self): self.parser = argparse.ArgumentParser(prog=self.name(), description=self.description()) self.add_parser_args() # Explain argument. self.parser.add_argument('--explain', action='store_true', help='Only print out expanded macro') def name(self): """Macro name.""" pass def description(self): """Macro description.""" pass def add_parser_args(self): """Method which adds custom command line arguments.""" pass def expand(self, id, cmd, args): """Expands the macro to a list of basic Tinode CLI commands.""" pass def run(self, id, cmd, args): """Expands the macro and returns the list of commands to actually execute to the caller depending on the presence of the --explain argument. """ cmds = self.expand(id, cmd, args) if cmd.explain: if cmds is None: return None for item in cmds: stdoutln(item) return [] return cmds class Usermod(Macro): """Modifies user account. The following modes are available: * suspend/unsuspend account. * change user's theCard (public name, description, avatar), private comment, trusted values. This macro requires root privileges.""" def name(self): return "usermod" def description(self): return 'Modify user account (requires root privileges)' def add_parser_args(self): self.parser.add_argument('userid', help='user to update') self.parser.add_argument('-L', '--suspend', action='store_true', help='Suspend account') self.parser.add_argument('-U', '--unsuspend', action='store_true', help='Unsuspend account') self.parser.add_argument('--name', help='Public name') self.parser.add_argument('--avatar', help='Avatar file name') self.parser.add_argument('--comment', help='Private comment on account') self.parser.add_argument('--note', help='Account description') self.parser.add_argument('--trusted', help='Add/remove trusted marker: verified, staff, danger') def expand(self, id, cmd, args): if not cmd.userid: return None # Suspend/unsuspend user. if cmd.suspend or cmd.unsuspend: if cmd.suspend and cmd.unsuspend: stdoutln("Cannot both suspend and unsuspend account") return None new_cmd = 'acc --user %s --as_root' % cmd.userid if cmd.suspend: new_cmd += ' --suspend true' if cmd.unsuspend: new_cmd += ' --suspend false' return [new_cmd] # Change theCard. varname = cmd.varname if hasattr(cmd, 'varname') and cmd.varname else '$temp' set_cmd = '.must ' + varname + ' set me' if cmd.name is not None: set_cmd += ' --fn="%s"' % cmd.name if cmd.avatar is not None: set_cmd += ' --photo="%s"' % cmd.avatar if cmd.comment is not None: set_cmd += ' --private="%s"' % cmd.comment if cmd.note is not None: set_cmd += ' --note="%s"' % cmd.note if cmd.trusted is not None: set_cmd += ' --trusted="%s" --as_root' % cmd.trusted old_user = tn_globals.DefaultUser if tn_globals.DefaultUser else '' return ['.use --user %s' % cmd.userid, '.must sub me', set_cmd, '.must leave me', '.use --user "%s"' % old_user] class Resolve(Macro): """Looks up user id by login name and prints it.""" def name(self): return "resolve" def description(self): return "Resolve login and print the corresponding user id" def add_parser_args(self): self.parser.add_argument('login', help='login to resolve') def expand(self, id, cmd, args): if not cmd.login: return None varname = cmd.varname if hasattr(cmd, 'varname') and cmd.varname else '$temp' return ['.must sub fnd', '.must set fnd --public=basic:%s' % cmd.login, '.must %s get fnd --sub' % varname, '.must leave fnd', '.log %s.sub[0].topic' % varname] class Passwd(Macro): """Sets user's password (requires root privileges).""" def name(self): return "passwd" def description(self): return "Set user's password (requires root privileges)" def add_parser_args(self): self.parser.add_argument('userid', help='Id of the user') self.parser.add_argument('-P', '--password', help='New password') def expand(self, id, cmd, args): if not cmd.userid: return None if not cmd.password: stdoutln("Password (-P) not specified") return None return ['acc --user %s --scheme basic --secret :%s' % (cmd.userid, cmd.password)] class Useradd(Macro): """Creates a new user account.""" def name(self): return "useradd" def description(self): return "Create a new user account" def add_parser_args(self): self.parser.add_argument('login', help='User login') self.parser.add_argument('-P', '--password', help='Password') self.parser.add_argument('--cred', help='List of comma-separated credentials in format "(email|tel):value1,(email|tel):value2,..."') self.parser.add_argument('--name', help='Public name of the user') self.parser.add_argument('--comment', help='Private comment') self.parser.add_argument('--note', help='Public description') self.parser.add_argument('--trusted', help='Add/remove trusted marker: verified, staff, danger') self.parser.add_argument('--tags', help='Comma-separated list of tags') self.parser.add_argument('--avatar', help='Path to avatar file') self.parser.add_argument('--auth', help='Default auth acs') self.parser.add_argument('--anon', help='Default anon acs') def expand(self, id, cmd, args): if not cmd.login: return None if not cmd.password: stdoutln("Password --password must be specified") return None if not cmd.cred: stdoutln("Must specify at least one credential: --cred.") return None varname = cmd.varname if hasattr(cmd, 'varname') and cmd.varname else '$temp' new_cmd = '.must ' + varname + ' acc --scheme basic --secret="%s:%s" --cred="%s"' % (cmd.login, cmd.password, cmd.cred) if cmd.name: new_cmd += ' --fn="%s"' % cmd.name if cmd.comment: new_cmd += ' --private="%s"' % cmd.comment if cmd.note is not None: set_cmd += ' --note="%s"' % cmd.note if cmd.trusted is not None: set_cmd += ' --trusted="%s" --as_root' % cmd.trusted if cmd.tags: new_cmd += ' --tags="%s"' % cmd.tags if cmd.avatar: new_cmd += ' --photo="%s"' % cmd.avatar if cmd.auth: new_cmd += ' --auth="%s"' % cmd.auth if cmd.anon: new_cmd += ' --anon="%s"' % cmd.anon return [new_cmd] class Chacs(Macro): """Modifies default acs (permissions) on a user account.""" def name(self): return "chacs" def description(self): return "Change default permissions/acs for a user (requires root privileges)" def add_parser_args(self): self.parser.add_argument('userid', help='User id') self.parser.add_argument('--auth', help='New auth acs value') self.parser.add_argument('--anon', help='New anon acs value') def expand(self, id, cmd, args): if not cmd.userid: return None if not cmd.auth and not cmd.anon: stdoutln('Must specify at least either of --auth, --anon') return None set_cmd = '.must set me' if cmd.auth: set_cmd += ' --auth=%s' % cmd.auth if cmd.anon: set_cmd += ' --anon=%s' % cmd.anon old_user = tn_globals.DefaultUser if tn_globals.DefaultUser else '' return ['.use --user %s' % cmd.userid, '.must sub me', set_cmd, '.must leave me', '.use --user "%s"' % old_user] class Userdel(Macro): """Deletes a user account.""" def name(self): return "userdel" def description(self): return "Delete user account (requires root privileges)" def add_parser_args(self): self.parser.add_argument('userid', help='User id') self.parser.add_argument('--hard', action='store_true', help='Hard delete') def expand(self, id, cmd, args): if not cmd.userid: return None del_cmd = 'del user --user %s --as_root' % cmd.userid if cmd.hard: del_cmd += ' --hard' return [del_cmd] class Chcred(Macro): """Adds, deletes or validates credentials for a user account.""" def name(self): return "chcred" def description(self): return "Add/delete/validate credentials (requires root privileges)" def add_parser_args(self): self.parser.add_argument('userid', help='User id') self.parser.add_argument('cred', help='Affected credential in formt method:value, e.g. email: abc@example.com, tel:17771112233') self.parser.add_argument('--add', action='store_true', help='Add credential') self.parser.add_argument('--rm', action='store_true', help='Delete credential') self.parser.add_argument('--validate', action='store_true', help='Validate credential') def expand(self, id, cmd, args): if not cmd.userid: return None if not cmd.cred: stdoutln('Must specify cred') return None num_actions = (1 if cmd.add else 0) + (1 if cmd.rm else 0) + (1 if cmd.validate else 0) if num_actions == 0 or num_actions > 1: stdoutln('Must specify exactly one action: --add, --rm, --validate') return None if cmd.add: cred_cmd = '.must set me --cred %s' % cmd.cred if cmd.rm: cred_cmd = '.must del --topic me --cred %s cred' % cmd.cred old_user = tn_globals.DefaultUser if tn_globals.DefaultUser else '' return ['.use --user %s' % cmd.userid, '.must sub me', cred_cmd, '.must leave me', '.use --user "%s"' % old_user] class Thecard(Macro): """Prints user's theCard.""" def name(self): return "thecard" def description(self): return "Print theCard for a user (requires root privileges)" def add_parser_args(self): self.parser.add_argument('userid', help='User id') self.parser.add_argument('--what', choices=['desc', 'cred'], required=True, help='Type of data to print (desc - public/private data, cred - list of credentials.') def expand(self, id, cmd, args): if not cmd.userid: return None varname = cmd.varname if hasattr(cmd, 'varname') and cmd.varname else '$temp' old_user = tn_globals.DefaultUser if tn_globals.DefaultUser else '' return ['.use --user %s' % cmd.userid, '.must sub me', '.must %s get me --%s' % (varname, cmd.what), '.must leave me', '.use --user "%s"' % old_user, '.log %s' % varname] def parse_macro(parts): """Attempts to find a parser for the provided sequence of tokens.""" global Macros # parts[0] is the command. macro = Macros.get(parts[0]) if not macro: return None return macro.parser Macros = {x.name(): x for x in [Usermod(), Resolve(), Passwd(), Useradd(), Chacs(), Userdel(), Chcred(), Thecard()]} ================================================ FILE: tn-cli/requirements.txt ================================================ futures>=3.2.0; python_version<'3' grpcio>=1.40.0 Pillow>=5.4.1 requests>=2.21.0 tinode-grpc>=0.22.0 prompt_toolkit>=2.0.10 importlib-metadata>=1.0; python_version<'3.8' ================================================ FILE: tn-cli/sample-macro-script.txt ================================================ # This script shows what can be done with tn-cli macros. Run it as # # python tn-cli.py --no-login < sample-macro-script.txt # # The script performs the following: # # - Creates a new user Test User with useradd # - Logs in as Xena (user has root privileges) # - Changes Test User's name to 'Test User 1' # - Modifies Test User's default anon acs to JR with chacs # - Sets Test User's password to test456 # - Deletes Test User with userdel # - Uses resolve to locate user Alice # - Subscribes to Alice # - Sends a message to Alice # Create user 'Test User' # .must directive ensures the command succeeds (in case of failure, the script execution stops. # $user is the variable that will hold the result of the command execution. .must $user useradd --name 'Test User' --password test123 --comment test0 \ --cred email:test@example.com --avatar ./test-128.jpg --tags test,test-user \ --auth=JRWPA --anon=JW test # Login as xena. .must $xena login --scheme=basic --secret=xena:xena123 # Change test's public name. .must usermod $user.params[user] --name 'Test User 1' # Change test's anon acs. .must chacs $user.params[user] --anon=JR # Set test's password to test456. passwd $user.params[user] -P test456 # Delete test user. .must userdel $user.params[user] --hard # Find Alice. .must $alice resolve alice # Subscribe to Alice .must sub $alice.sub[0].topic # Send a plain text message to Alice (async) pub $alice.sub[0].topic 'Hello, Alice' .sleep 2000 ================================================ FILE: tn-cli/sample-script.txt ================================================ # This script shows what can be done with tn-cli. Run it as # # python tn-cli.py --no-login < sample-script.txt # # The script performs the following: # # - Creates a new user Test User # - Uses 'fnd' topic to locate user Alice # - Subscribes Test User to Alice # - Sends typing notification to Alice # - Sends a message to Alice # - Creates a group topic Test Group # - Adds Alice to the Test Group # - Sends a message with a drafty-formatted form to the Test Group # - Deletes Test Group # - Deletes Test User # Create user 'Test User' .must $user acc --scheme=basic --secret=test:test123 \ --fn='Test User' --photo=./test-128.jpg --tags=test,test-user --do-login \ --auth=JRWPA --anon=JW \ --cred=email:test@example.com # Print out user's auth token. # Params is a map, thus must be addressed as params[x] instead of params.x .log $user.params[token] # Login and confirm credentials with a dummy response .must login --scheme=token --secret=$user.params[token] --cred=email::123456 # Alternatively just login with an existing user. # .must $user login bob:bob123 # Find Alice .must sub fnd .must set fnd --public=email:alice@example.com .must $meta get fnd --sub # Print out Alice's UID. .log $meta.sub[0].topic # Subscribe to Alice .must sub $meta.sub[0].topic # Send typing notification to Alice (async) note $meta.sub[0].topic # Send a plain text message to Alice (async) pub $meta.sub[0].topic 'Hello, Alice' # Create a new group topic Test Group .must $group sub new --fn='Test Group' --tags=test,test-group \ --auth=JRWPA --anon=JR # Add Alice to the new topic. .must set $group.topic --user=$meta.sub[0].topic # Publish a drafty-formatted form to the new topic. pub $group.topic --drafty='{"txt": "What’s your favorite color?red green none",\ "fmt": [ {"at": 0, "len": 42, "tp": "FM"}, {"at": 0, "len": 27, "tp": "ST"},\ {"at": 27, "len": 14, "tp": "RW"}, {"at": 27, "len": 3, "key": 0}, {"at": 31, "len": 5, "key": 1},\ {"at": 37, "len": 4, "key": 2} ], "ent": [ {"tp": "BN", "data": {"name": "red", "act": "pub", "val": 3840}},\ {"tp": "BN", "data": {"name": "green", "act": "pub", "val": 240}},\ {"tp": "BN", "data": {"name": "none", "act": "pub"}}]}' # Wait 2 seconds before cleaning up. .sleep 2000 # Delete Test Group. .await del topic --topic=$group.topic --hard # Delete Test User .await del user --hard # Wait for more messages before exiting. .sleep 1000 ================================================ FILE: tn-cli/tn-cli.py ================================================ #!/usr/bin/env python # coding=utf-8 """Python implementation of Tinode command line client using gRPC.""" from __future__ import print_function import argparse import os import platform import sys try: from importlib.metadata import version except ImportError: # Fallback for Python < 3.8 from importlib_metadata import version import tn_globals from tn_globals import printout from client import run, read_cookie from commands import set_macros_module APP_NAME = "tn-cli" APP_VERSION = "3.1.0" # format: 1.9.0b1 LIB_VERSION = version("tinode_grpc") GRPC_VERSION = version("grpcio") # This is needed for gRPC SSL to work correctly. os.environ["GRPC_SSL_CIPHER_SUITES"] = "HIGH+ECDSA" # Setup crash handler: close input reader otherwise a crash # makes terminal session unusable. def exception_hook(type, value, traceBack): if tn_globals.InputThread != None: tn_globals.InputThread.join(0.3) sys.excepthook = exception_hook # Enable the following variables for debugging. # os.environ["GRPC_TRACE"] = "all" # os.environ["GRPC_VERBOSITY"] = "INFO" if __name__ == '__main__': """Parse command-line arguments. Extract host name and authentication scheme, if one is provided""" version_str = APP_VERSION + "/" + LIB_VERSION + "; gRPC/" + GRPC_VERSION + "; Python " + platform.python_version() purpose = "Tinode command line client. Version " + version_str + "." parser = argparse.ArgumentParser(description=purpose) parser.add_argument('--host', default='localhost:16060', help='address of Tinode gRPC server') parser.add_argument('--web-host', default='localhost:6060', help='address of Tinode web server (for file uploads)') parser.add_argument('--ssl', action='store_true', help='connect to server over secure connection') parser.add_argument('--ssl-host', help='SSL host name to use instead of default (useful for connecting to localhost)') parser.add_argument('--login-basic', help='login using basic authentication username:password') parser.add_argument('--login-token', help='login using token authentication') parser.add_argument('--login-cookie', action='store_true', help='read token from cookie file and use it for authentication') parser.add_argument('--no-login', action='store_true', help='do not login even if cookie file is present; default in non-interactive (scripted) mode') parser.add_argument('--no-cookie', action='store_true', help='do not save login cookie; default in non-interactive (scripted) mode') parser.add_argument('--api-key', default='AQEAAAABAAD_rAp4DJh05a1HAwFT3A6K', help='API key for file uploads') parser.add_argument('--load-macros', default='./macros.py', help='path to macro module to load') parser.add_argument('--version', action='store_true', help='print version') parser.add_argument('--verbose', action='store_true', help='log full JSON representation of all messages') parser.add_argument('--background', action='store_const', const=True, help='start interactive sessionin background (non-intractive is always in background)') args = parser.parse_args() if args.version: printout(version_str) exit() if args.verbose: tn_globals.Verbose = True printout(purpose) printout("Secure server" if args.ssl else "Server", "at '"+args.host+"'", "SNI="+args.ssl_host if args.ssl_host else "") schema = None secret = None if not args.no_login: if args.login_token: """Use token to login""" schema = 'token' secret = args.login_token.encode('ascii') printout("Logging in with token", args.login_token) elif args.login_basic: """Use username:password""" schema = 'basic' secret = args.login_basic printout("Logging in with login:password", args.login_basic) elif tn_globals.IsInteractive: """Interactive mode only: try reading the cookie file""" printout("Logging in with cookie file") schema = 'token' secret = read_cookie() if not secret: schema = None # Attempt to load the macro file if available. macros = None if args.load_macros: import importlib macros = importlib.import_module('macros', args.load_macros) if args.load_macros else None set_macros_module(macros) # Check if background session is specified explicitly. If not set it to # True for non-interactive sessions. if args.background is None and not tn_globals.IsInteractive: args.background = True sys.exit(run(args, schema, secret)) ================================================ FILE: tn-cli/tn_globals.py ================================================ """Global objects for Tinode command line client.""" # To make print() compatible between p2 and p3 from __future__ import print_function import json import sys from collections import deque from google.protobuf.json_format import MessageToDict try: import Queue as queue except ImportError: import queue if sys.version_info[0] >= 3: # for compatibility with python2 unicode = str # Dictionary wich contains lambdas to be executed when server {ctrl} response is received. OnCompletion = {} # Outstanding request for a synchronous message. WaitingFor = None # Last obtained authentication token AuthToken = '' # IO queues and a thread for asynchronous input/output #InputQueue = queue.Queue() InputQueue = deque() OutputQueue = queue.Queue() InputThread = None # Detect if the tn-cli is running interactively or being piped. IsInteractive = sys.stdin.isatty() Prompt = None # Default values for user and topic DefaultUser = None DefaultTopic = None # Variables: results of command execution Variables = {} # Connection to the server Connection = None # Flag to enable extended logging. Useful for debugging. Verbose = False # Print prompts in interactive mode only. def printout(*args): if IsInteractive: print(*args) def printerr(*args): text = "" for a in args: text = text + str(a) + " " # Strip just the spaces here, don't strip the newline or tabs. text = text.strip(" ") if text: sys.stderr.write(text + "\n") # Support for asynchronous input-output to/from stdin/stdout # Stdout asynchronously writes to sys.stdout def stdout(*args): text = "" for a in args: text = text + str(a) + " " # Strip just the spaces here, don't strip the newline or tabs. text = text.strip(" ") if text: OutputQueue.put(text) # Stdoutln asynchronously writes to sys.stdout and adds a new line to input. def stdoutln(*args): args = args + ("\n",) stdout(*args) # Shorten long strings for logging. def clip_long_string(obj): if isinstance(obj, str) or isinstance(obj, unicode): if len(obj) > 64: return '<' + str(len(obj)) + ' bytes: ' + obj[:12] + '...' + obj[-12:] + '>' return obj elif isinstance(obj, (list, tuple)): return [clip_long_string(item) for item in obj] elif isinstance(obj, dict): return dict((key, clip_long_string(val)) for key, val in obj.items()) else: return obj # Convert protobuff message to json. Shorten very long strings. def to_json(msg): if not msg: return 'null' try: return json.dumps(clip_long_string(MessageToDict(msg))) except Exception as err: stdoutln("Exception: {}".format(err)) return 'exception' ================================================ FILE: tn-cli/utils.py ================================================ """Utility functions for tn-cli.""" from __future__ import print_function import base64 import json from PIL import Image try: from io import BytesIO as memory_io except ImportError: from cStringIO import StringIO as memory_io import mimetypes import os from tn_globals import stdoutln # Maximum in-band (included directly into the message) attachment size which fits into # a message of 256K in size, assuming base64 encoding and 1024 bytes of overhead. # This is size of an object *before* base64 encoding is applied. MAX_INBAND_ATTACHMENT_SIZE = 195840 # Absolute maximum attachment size to be used with the server = 8MB. MAX_EXTERN_ATTACHMENT_SIZE = 1 << 23 # Maximum allowed linear dimension of an inline image in pixels. MAX_IMAGE_DIM = 768 # String used as a delete marker. I.e. when a value needs to be deleted, use this string DELETE_MARKER = 'DEL!' # Unicode DEL character used internally by Tinode when a value needs to be deleted. TINODE_DEL = '␡' # Python is retarded. class dotdict(dict): """dot.notation access to dictionary attributes""" __getattr__ = dict.get __setattr__ = dict.__setitem__ __delattr__ = dict.__delitem__ # Pack name, description, and avatar into a theCard. def makeTheCard(fn, note, photofile): card = None if (fn != None and fn.strip() != "") or photofile != None or note != None: card = {} if fn != None: fn = fn.strip() card['fn'] = TINODE_DEL if fn == DELETE_MARKER or fn == '' else fn if note != None: note = note.strip() card['note'] = TINODE_DEL if note == DELETE_MARKER or note == '' else note if photofile != None: if photofile == '' or photofile == DELETE_MARKER: # Delete the avatar. card['photo'] = { 'data': TINODE_DEL } else: try: f = open(photofile, 'rb') # File extension is used as a file type mimetype = mimetypes.guess_type(photofile) if mimetype[0]: mimetype = mimetype[0].split("/")[1] else: mimetype = 'jpeg' data = base64.b64encode(f.read()) # python3 fix. if type(data) is not str: data = data.decode() card['photo'] = { 'data': data, 'type': mimetype } f.close() except IOError as err: stdoutln("Error opening '" + photofile + "':", err) return card # Create drafty representation of a message with an inline image. def inline_image(filename): try: im = Image.open(filename, 'r') width = im.width height = im.height format = im.format if im.format else "JPEG" if width > MAX_IMAGE_DIM or height > MAX_IMAGE_DIM: # Scale the image scale = min(min(width, MAX_IMAGE_DIM) / width, min(height, MAX_IMAGE_DIM) / height) width = int(width * scale) height = int(height * scale) resized = im.resize((width, height)) im.close() im = resized mimetype = 'image/' + format.lower() bitbuffer = memory_io() im.save(bitbuffer, format=format) data = base64.b64encode(bitbuffer.getvalue()) # python3 fix. if type(data) is not str: data = data.decode() result = { 'txt': ' ', 'fmt': [{'len': 1}], 'ent': [{'tp': 'IM', 'data': {'val': data, 'mime': mimetype, 'width': width, 'height': height, 'name': os.path.basename(filename)}}] } im.close() return result except IOError as err: stdoutln("Failed processing image '" + filename + "':", err) return None # Create a drafty message with an *in-band* attachment. def attachment(filename): try: f = open(filename, 'rb') # Try to guess the mime type. mimetype = mimetypes.guess_type(filename)[0] data = base64.b64encode(f.read()) # python3 fix. if type(data) is not str: data = data.decode() result = { 'fmt': [{'at': -1}], 'ent': [{'tp': 'EX', 'data':{ 'val': data, 'mime': mimetype, 'name':os.path.basename(filename) }}] } f.close() return result except IOError as err: stdoutln("Error processing attachment '" + filename + "':", err) return None # encode_to_bytes converts the 'src' to a byte array. # An object/dictionary is first converted to json string then it's converted to bytes. # A string is directly converted to bytes. def encode_to_bytes(src): if src == None: return None if isinstance(src, str): return ('"' + src + '"').encode() return json.dumps(src).encode('utf-8') # Parse credentials def parse_cred(cred): result = None if cred != None: result = [] for c in cred.split(","): parts = c.split(":") from tinode_grpc import pb result.append(pb.ClientCred(method=parts[0] if len(parts) > 0 else None, value=parts[1] if len(parts) > 1 else None, response=parts[2] if len(parts) > 2 else None)) return result # Parse trusted values: [staff,rm-verified]. def parse_trusted(trusted): result = None if trusted != None: result = {} for t in trusted.split(","): t = t.strip() if t.startswith("rm-"): result[t[3:]] = False else: result[t] = True return result