Skip to content

Commit

Permalink
Add ResourceName GraphQL type
Browse files Browse the repository at this point in the history
  • Loading branch information
chrzaszcz committed Nov 18, 2022
1 parent 83bc2ce commit f9ab0e9
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion priv/graphql/schemas/admin/session.gql
Original file line number Diff line number Diff line change
Expand Up @@ -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!]
Expand Down
2 changes: 2 additions & 0 deletions priv/graphql/schemas/global/scalar_types.gql
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 4 additions & 4 deletions priv/graphql/schemas/user/muc.gql
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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")
}

"""
Expand Down
2 changes: 1 addition & 1 deletion priv/graphql/schemas/user/session.gql
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 12 additions & 0 deletions src/graphql/mongoose_graphql_scalar.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) ->
Expand Down Expand Up @@ -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 = <<>>}} ->
Expand Down

0 comments on commit f9ab0e9

Please sign in to comment.