From f9ab0e9ca44a226b1a8723c4769a9504a626c64c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chrz=C4=85szcz?= Date: Fri, 18 Nov 2022 18:28:27 +0100 Subject: [PATCH] Add ResourceName GraphQL type --- priv/graphql/schemas/admin/session.gql | 2 +- priv/graphql/schemas/global/scalar_types.gql | 2 ++ priv/graphql/schemas/user/muc.gql | 8 ++++---- priv/graphql/schemas/user/session.gql | 2 +- src/graphql/mongoose_graphql_scalar.erl | 12 ++++++++++++ 5 files changed, 20 insertions(+), 6 deletions(-) diff --git a/priv/graphql/schemas/admin/session.gql b/priv/graphql/schemas/admin/session.gql index 1a528a94554..5782e4bd07e 100644 --- a/priv/graphql/schemas/admin/session.gql +++ b/priv/graphql/schemas/admin/session.gql @@ -15,7 +15,7 @@ type SessionAdminQuery @protected{ countUserResources(user: JID!): Int @protected(type: DOMAIN, args: ["user"]) "Get the resource string of the n-th session of a user" - getUserResource(user: JID!, number: Int): String + getUserResource(user: JID!, number: Int): ResourceName @protected(type: DOMAIN, args: ["user"]) "Get the list of logged users with this status for a specified domain or globally" listUsersWithStatus(domain: DomainName, status: String!): [UserStatus!] diff --git a/priv/graphql/schemas/global/scalar_types.gql b/priv/graphql/schemas/global/scalar_types.gql index c5a93b4e0a3..a9490e3929a 100644 --- a/priv/graphql/schemas/global/scalar_types.gql +++ b/priv/graphql/schemas/global/scalar_types.gql @@ -12,6 +12,8 @@ scalar UserName @spectaql(options: [{ key: "example", value: "alice" }]) scalar RoomName @spectaql(options: [{ key: "example", value: "my-chat-room" }]) "XMPP domain name (domain part of a JID)" scalar DomainName @spectaql(options: [{ key: "example", value: "localhost" }]) +"XMPP resource name (resource part of a JID)" +scalar ResourceName @spectaql(options: [{ key: "example", value: "res1" }]) "String that contains at least one character" scalar NonEmptyString @spectaql(options: [{ key: "example", value: "xyz789" }]) "Integer that has a value above zero" diff --git a/priv/graphql/schemas/user/muc.gql b/priv/graphql/schemas/user/muc.gql index 18511bb0aee..0435ce42b0b 100644 --- a/priv/graphql/schemas/user/muc.gql +++ b/priv/graphql/schemas/user/muc.gql @@ -10,9 +10,9 @@ type MUCUserMutation @protected @use(modules: ["mod_muc"]){ "Kick a user from a MUC room" kickUser(room: JID!, nick: String!, reason: String): String @use(arg: "room") "Send a message to a MUC room" - sendMessageToRoom(room: JID!, body: String!, resource: String): String @use(arg: "room") + sendMessageToRoom(room: JID!, body: String!, resource: ResourceName): String @use(arg: "room") "Send a private message to a MUC room user from the given resource" - sendPrivateMessage(room: JID!, toNick: String!, body: String!, resource: String): String @use(arg: "room") + sendPrivateMessage(room: JID!, toNick: String!, body: String!, resource: ResourceName): String @use(arg: "room") "Remove a MUC room" deleteRoom(room: JID!, reason: String): String @use(arg: "room") "Change configuration of a MUC room" @@ -22,9 +22,9 @@ type MUCUserMutation @protected @use(modules: ["mod_muc"]){ "Change a user affiliation" setUserAffiliation(room: JID!, user: JID!, affiliation: MUCAffiliation!): String @use(arg: "room") "Enter the room with given resource and nick" - enterRoom(room: JID!, nick: String!, resource: String!, password: String): String @use(arg: "room") + enterRoom(room: JID!, nick: String!, resource: ResourceName!, password: String): String @use(arg: "room") "Exit the room with given resource and nick" - exitRoom(room: JID!, nick: String!, resource: String!): String @use(arg: "room") + exitRoom(room: JID!, nick: String!, resource: ResourceName!): String @use(arg: "room") } """ diff --git a/priv/graphql/schemas/user/session.gql b/priv/graphql/schemas/user/session.gql index d351a83ac6a..a958a1029a7 100644 --- a/priv/graphql/schemas/user/session.gql +++ b/priv/graphql/schemas/user/session.gql @@ -3,7 +3,7 @@ Allow user to get information about sessions. """ type SessionUserQuery @protected{ "List connected resources" - listResources: [String!] + listResources: [ResourceName!] "Count connected resources" countResources: Int "Get information about all sessions" diff --git a/src/graphql/mongoose_graphql_scalar.erl b/src/graphql/mongoose_graphql_scalar.erl index faacc599eab..175e0cfcb61 100644 --- a/src/graphql/mongoose_graphql_scalar.erl +++ b/src/graphql/mongoose_graphql_scalar.erl @@ -16,6 +16,7 @@ input(<<"JID">>, Jid) -> jid_from_binary(Jid); input(<<"UserName">>, User) -> user_from_binary(User); input(<<"RoomName">>, Room) -> room_from_binary(Room); input(<<"DomainName">>, Domain) -> domain_from_binary(Domain); +input(<<"ResourceName">>, Res) -> resource_from_binary(Res); input(<<"FullJID">>, Jid) -> full_jid_from_binary(Jid); input(<<"NonEmptyString">>, Value) -> non_empty_string_to_binary(Value); input(<<"PosInt">>, Value) -> validate_pos_integer(Value); @@ -35,6 +36,7 @@ output(<<"JID">>, Jid) -> {ok, jid:to_binary(Jid)}; output(<<"UserName">>, User) -> {ok, User}; output(<<"RoomName">>, Room) -> {ok, Room}; output(<<"DomainName">>, Domain) -> {ok, Domain}; +output(<<"ResourceName">>, Res) -> {ok, Res}; output(<<"NonEmptyString">>, Value) -> binary_to_non_empty_string(Value); output(<<"PosInt">>, Value) -> validate_pos_integer(Value); output(Ty, V) -> @@ -79,6 +81,16 @@ domain_from_binary(Value) -> {ok, Domain} end. +resource_from_binary(<<>>) -> + {error, empty_resource_name}; +resource_from_binary(Value) -> + case jid:resourceprep(Value) of + error -> + {error, failed_to_parse_resource_name}; + Res -> + {ok, Res} + end. + full_jid_from_binary(Value) -> case jid_from_binary(Value) of {ok, #jid{lresource = <<>>}} ->