Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
mtxclient
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Nheko Reborn
mtxclient
Commits
1f0b16b1
Commit
1f0b16b1
authored
7 years ago
by
Konstantinos Sideris
Browse files
Options
Downloads
Patches
Plain Diff
Add support for sending state events
fixes #9 fixes #10
parent
3e426004
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/client.hpp
+38
-1
38 additions, 1 deletion
src/client.hpp
tests/client_api.cpp
+83
-2
83 additions, 2 deletions
tests/client_api.cpp
with
121 additions
and
3 deletions
src/client.hpp
+
38
−
1
View file @
1f0b16b1
...
...
@@ -127,6 +127,19 @@ public:
const
std
::
string
&
txn_id
,
Payload
payload
,
std
::
function
<
void
(
const
mtx
::
responses
::
EventId
&
,
RequestErr
)
>
callback
);
//! Send a state event by providing the state key.
template
<
class
Payload
,
mtx
::
events
::
EventType
Event
>
void
send_state_event
(
const
mtx
::
identifiers
::
Room
&
room_id
,
const
std
::
string
&
state_key
,
Payload
payload
,
std
::
function
<
void
(
const
mtx
::
responses
::
EventId
&
,
RequestErr
)
>
callback
);
//! Send a state event with an empty state key.
template
<
class
Payload
,
mtx
::
events
::
EventType
Event
>
void
send_state_event
(
const
mtx
::
identifiers
::
Room
&
room_id
,
Payload
payload
,
std
::
function
<
void
(
const
mtx
::
responses
::
EventId
&
,
RequestErr
)
>
callback
);
/* void download_room_avatar(); */
/* void download_media(); */
...
...
@@ -421,5 +434,29 @@ mtx::client::Client::send_room_message(
const
auto
api_path
=
"/client/r0/rooms/"
+
room_id
.
toString
()
+
"/send/"
+
mtx
::
events
::
to_string
(
Event
)
+
"/"
+
txn_id
;
put
<
nlohmann
::
json
>
(
api_path
,
payload
,
callback
);
put
<
Payload
,
mtx
::
responses
::
EventId
>
(
api_path
,
payload
,
callback
);
}
template
<
class
Payload
,
mtx
::
events
::
EventType
Event
>
void
mtx
::
client
::
Client
::
send_state_event
(
const
mtx
::
identifiers
::
Room
&
room_id
,
const
std
::
string
&
state_key
,
Payload
payload
,
std
::
function
<
void
(
const
mtx
::
responses
::
EventId
&
,
RequestErr
)
>
callback
)
{
const
auto
api_path
=
"/client/r0/rooms/"
+
room_id
.
toString
()
+
"/state/"
+
mtx
::
events
::
to_string
(
Event
)
+
"/"
+
state_key
;
put
<
Payload
,
mtx
::
responses
::
EventId
>
(
api_path
,
payload
,
callback
);
}
template
<
class
Payload
,
mtx
::
events
::
EventType
Event
>
void
mtx
::
client
::
Client
::
send_state_event
(
const
mtx
::
identifiers
::
Room
&
room_id
,
Payload
payload
,
std
::
function
<
void
(
const
mtx
::
responses
::
EventId
&
,
RequestErr
)
>
callback
)
{
send_state_event
<
Payload
,
Event
>
(
room_id
,
""
,
payload
,
callback
);
}
This diff is collapsed.
Click to expand it.
tests/client_api.cpp
+
83
−
2
View file @
1f0b16b1
...
...
@@ -12,6 +12,7 @@
using
namespace
mtx
::
client
;
using
namespace
mtx
::
identifiers
;
using
namespace
mtx
::
events
::
collections
;
using
namespace
std
;
...
...
@@ -40,8 +41,9 @@ validate_login(const std::string &user, const mtx::responses::Login &res)
ASSERT_TRUE
(
res
.
device_id
.
size
()
>
5
);
}
template
<
class
Collection
>
vector
<
string
>
get_event_ids
(
const
std
::
vector
<
mtx
::
events
::
collections
::
TimelineEvents
>
&
events
)
get_event_ids
(
const
std
::
vector
<
Collection
>
&
events
)
{
vector
<
string
>
ids
;
...
...
@@ -713,7 +715,7 @@ TEST(ClientAPI, SendMessages)
[
room_id
,
event_ids
](
const
mtx
::
responses
::
Sync
&
res
,
ErrType
err
)
{
check_error
(
err
);
auto
ids
=
get_event_ids
(
auto
ids
=
get_event_ids
<
TimelineEvents
>
(
res
.
rooms
.
join
.
at
(
room_id
.
toString
()).
timeline
.
events
);
// The sent event ids should be visible in the timeline.
...
...
@@ -728,3 +730,82 @@ TEST(ClientAPI, SendMessages)
alice
->
close
();
bob
->
close
();
}
TEST
(
ClientAPI
,
SendStateEvents
)
{
auto
alice
=
std
::
make_shared
<
Client
>
(
"localhost"
);
auto
bob
=
std
::
make_shared
<
Client
>
(
"localhost"
);
alice
->
login
(
"alice"
,
"secret"
,
[
alice
](
const
mtx
::
responses
::
Login
&
,
ErrType
err
)
{
check_error
(
err
);
});
bob
->
login
(
"bob"
,
"secret"
,
[
bob
](
const
mtx
::
responses
::
Login
&
,
ErrType
err
)
{
check_error
(
err
);
});
while
(
alice
->
access_token
().
empty
()
||
bob
->
access_token
().
empty
())
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
milliseconds
(
100
));
mtx
::
requests
::
CreateRoom
req
;
req
.
invite
=
{
"@bob:localhost"
};
alice
->
create_room
(
req
,
[
alice
,
bob
](
const
mtx
::
responses
::
CreateRoom
&
res
,
ErrType
err
)
{
check_error
(
err
);
auto
room_id
=
res
.
room_id
;
// Flag to indicate when those messages would be ready to be read by
// alice.
vector
<
string
>
event_ids
;
mtx
::
events
::
state
::
Name
event
;
event
.
name
=
"Bob's room"
;
bob
->
send_state_event
<
mtx
::
events
::
state
::
Name
,
mtx
::
events
::
EventType
::
RoomName
>
(
room_id
,
event
,
[](
const
mtx
::
responses
::
EventId
&
,
ErrType
err
)
{
ASSERT_TRUE
(
err
);
ASSERT_EQ
(
"M_FORBIDDEN"
,
mtx
::
errors
::
to_string
(
err
->
matrix_error
.
errcode
));
});
mtx
::
events
::
state
::
Name
name_event
;
name_event
.
name
=
"Alice's room"
;
alice
->
send_state_event
<
mtx
::
events
::
state
::
Name
,
mtx
::
events
::
EventType
::
RoomName
>
(
room_id
,
name_event
,
[
&
event_ids
](
const
mtx
::
responses
::
EventId
&
res
,
ErrType
err
)
{
check_error
(
err
);
event_ids
.
push_back
(
res
.
event_id
.
toString
());
});
mtx
::
events
::
state
::
Avatar
avatar
;
avatar
.
url
=
"mxc://localhost/random"
;
alice
->
send_state_event
<
mtx
::
events
::
state
::
Avatar
,
mtx
::
events
::
EventType
::
RoomAvatar
>
(
room_id
,
avatar
,
[
&
event_ids
](
const
mtx
::
responses
::
EventId
&
res
,
ErrType
err
)
{
check_error
(
err
);
event_ids
.
push_back
(
res
.
event_id
.
toString
());
});
while
(
event_ids
.
size
()
!=
2
)
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
milliseconds
(
500
));
alice
->
sync
(
""
,
""
,
false
,
0
,
[
room_id
,
event_ids
](
const
mtx
::
responses
::
Sync
&
res
,
ErrType
err
)
{
check_error
(
err
);
auto
ids
=
get_event_ids
<
TimelineEvents
>
(
res
.
rooms
.
join
.
at
(
room_id
.
toString
()).
timeline
.
events
);
// The sent event ids should be visible in the timeline.
for
(
const
auto
&
event_id
:
event_ids
)
ASSERT_TRUE
(
std
::
find
(
ids
.
begin
(),
ids
.
end
(),
event_id
)
!=
std
::
end
(
ids
));
});
});
alice
->
close
();
bob
->
close
();
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment