0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-06-09 13:38:55 +02:00

ircd:Ⓜ️🆔 Add class representations for event::v3 and event::v4.

This commit is contained in:
Jason Volk 2019-06-26 04:15:56 -07:00
parent ed5e7ef10a
commit ed5af1fdbd
2 changed files with 94 additions and 0 deletions

View file

@ -157,6 +157,9 @@ struct ircd::m::id::user
struct ircd::m::id::event
:ircd::m::id
{
struct v3;
struct v4;
using buf = m::id::buf<event>;
using closure = std::function<void (const id::event &)>;
using closure_bool = std::function<bool (const id::event &)>;
@ -171,6 +174,24 @@ struct ircd::m::id::event
event() = default;
};
/// Version 3 event_id
struct ircd::m::id::event::v3
:ircd::m::id::event
{
v3(const mutable_buffer &out, const json::object &);
v3(const string_view &id);
v3() = default;
};
/// Version 4 event_id
struct ircd::m::id::event::v4
:ircd::m::id::event
{
v4(const mutable_buffer &out, const json::object &);
v4(const string_view &id);
v4() = default;
};
/// (Appendix 4.2.2) Room IDs and Event IDs
///
/// A room has exactly one room ID. A room ID has the format:

View file

@ -675,6 +675,79 @@ const
"1";
}
//
// id::event::v3
//
ircd::m::id::event::v3::v3(const string_view &id)
:id::event{id}
{
if(unlikely(version() != "3"))
throw m::INVALID_MXID
{
"Not a version 3 event mxid"
};
}
ircd::m::id::event::v3::v3(const mutable_buffer &out,
const json::object &event)
:v3{[&out, event]
{
if(unlikely(buffer::size(out) < 44))
throw std::out_of_range
{
"Output buffer insufficient for v3 event_id"
};
const sha256::buf hash
{
sha256{event}
};
out[0] = '$';
return b64encode_unpadded(out + 1, hash);
}()}
{
}
//
// id::event::v4
//
ircd::m::id::event::v4::v4(const string_view &id)
:id::event{id}
{
if(unlikely(version() != "4"))
throw m::INVALID_MXID
{
"Not a version 4 event mxid"
};
}
ircd::m::id::event::v4::v4(const mutable_buffer &out,
const json::object &event)
:v4{[&out, event]
{
if(unlikely(buffer::size(out) < 44))
throw std::out_of_range
{
"Output buffer insufficient for v4 event_id"
};
const sha256::buf hash
{
sha256{event}
};
out[0] = '$';
string_view ret;
ret = b64encode_unpadded(out + 1, hash);
ret = b64tob64url(out + 1, ret);
return ret;
}()}
{
}
//
// util
//