Repository: matrix-org/synapse

Issue:
3PID invites should include the configured web client instance / homeserver url in the email
Related https://github.com/vector-im/element-meta/issues/1281

Some context in https://matrix.to/#/!oBWlVutBAdQYcYUsAc:sw1v.org/$pnwt-VMwwf3pC7_QET_e2jk_ojfRAPTNZ9fj2ag-Uio?via=jki.re&via=matrix.org&via=vector.modular.im

(This also applies to Sydent, but the work is 50/50 across both projects)

Synapse currently uses the identity server to send 3pid invites out to users (so it sends an email when an email address is invited to a room). However, [the templates](https://github.com/matrix-org/sydent/blob/master/res/invite_template.eml) do not include and do not have a way to include the target web client instance. Instead, all invites include app.element.io.

This is a problem if you are running your own custom Element / Synapse deployment and wish to send invited users to your specific instance, especially so if your instance is not federated. 

There are several ways to resolve this:
- Invites could be sent by Synapse (and thus, the template could be configured by the owner).
- Synapse could send the config value of `web_client_location` to sydent, and the template could use that.
- Synapse could send the homeserver URL to sydent, and sydent could include a parameter to target the right homeserver. This would require https://github.com/vector-im/element-web/issues/5469


Discussion:

Resolving patch:
diff --git a/synapse/config/emailconfig.py b/synapse/config/emailconfig.py
--- a/synapse/config/emailconfig.py
+++ b/synapse/config/emailconfig.py
@@ -322,6 +322,22 @@ def read_config(self, config, **kwargs):
 
         self.email_subjects = EmailSubjectConfig(**subjects)
 
+        # The invite client location should be a HTTP(S) URL or None.
+        self.invite_client_location = email_config.get("invite_client_location") or None
+        if self.invite_client_location:
+            if not isinstance(self.invite_client_location, str):
+                raise ConfigError(
+                    "Config option email.invite_client_location must be type str"
+                )
+            if not (
+                self.invite_client_location.startswith("http://")
+                or self.invite_client_location.startswith("https://")
+            ):
+                raise ConfigError(
+                    "Config option email.invite_client_location must be a http or https URL",
+                    path=("email", "invite_client_location"),
+                )
+
     def generate_config_section(self, config_dir_path, server_name, **kwargs):
         return (
             """\
@@ -389,6 +405,12 @@ def generate_config_section(self, config_dir_path, server_name, **kwargs):
           #
           #validation_token_lifetime: 15m
 
+          # The web client location to direct users to during an invite. This is passed
+          # to the identity server as the org.matrix.web_client_location key. Defaults
+          # to unset, giving no guidance to the identity server.
+          #
+          #invite_client_location: https://app.element.io
+
           # Directory in which Synapse will try to find the template files below.
           # If not set, or the files named below are not found within the template
           # directory, default templates from within the Synapse package will be used.
diff --git a/synapse/handlers/identity.py b/synapse/handlers/identity.py
--- a/synapse/handlers/identity.py
+++ b/synapse/handlers/identity.py
@@ -55,6 +55,8 @@ def __init__(self, hs):
         self.federation_http_client = hs.get_federation_http_client()
         self.hs = hs
 
+        self._web_client_location = hs.config.invite_client_location
+
     async def threepid_from_creds(
         self, id_server: str, creds: Dict[str, str]
     ) -> Optional[JsonDict]:
@@ -803,6 +805,9 @@ async def ask_id_server_for_third_party_invite(
             "sender_display_name": inviter_display_name,
             "sender_avatar_url": inviter_avatar_url,
         }
+        # If a custom web client location is available, include it in the request.
+        if self._web_client_location:
+            invite_config["org.matrix.web_client_location"] = self._web_client_location
 
         # Add the identity service access token to the JSON body and use the v2
         # Identity Service endpoints if id_access_token is present


Test patch: