0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2025-03-22 01:10:27 +01:00

ircd:Ⓜ️ Add convenience suite to extract creator from event tuple.

This commit is contained in:
Jason Volk 2023-02-20 10:31:42 -08:00
parent 884c2e68b7
commit 72dce4364c
3 changed files with 82 additions and 4 deletions
include/ircd/m
matrix

79
include/ircd/m/creator.h Normal file
View file

@ -0,0 +1,79 @@
// Matrix Construct
//
// Copyright (C) Matrix Construct Developers, Authors & Contributors
// Copyright (C) 2016-2023 Jason Volk <jason@zemos.net>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice is present in all copies. The
// full license for this software is available in the LICENSE file.
#pragma once
#define HAVE_IRCD_M_CREATOR_H
namespace ircd::m
{
id::user creator(const event &);
id::user creation(const event &);
bool creator(const event &, const id::user &);
bool creation(const event &, const id::user &);
}
/// Events that are not type=m.room.create will return false; the sender field
/// will be tried if available, otherwise content.creator will be tried.
inline bool
ircd::m::creation(const event &event,
const id::user &user)
{
if(json::get<"type"_>(event) != "m.room.create")
return false;
return creator(event, user);
}
/// The sender field will be tried if available, otherwise content.creator will
/// be tried.
inline bool
ircd::m::creator(const event &event,
const id::user &user)
{
assert(defined(user));
if(likely(defined(json::get<"sender"_>(event))))
if(json::get<"sender"_>(event) == user)
return true;
const json::string creator
{
json::get<"content"_>(event).get("creator")
};
return creator == user;
}
/// Events that are not type=m.room.create will return empty; the sender
/// will be tried if available, otherwise content.creator will be tried.
inline ircd::m::id::user
ircd::m::creation(const event &event)
{
if(json::get<"type"_>(event) != "m.room.create")
return id::user{};
return creator(event);
}
/// The sender will be tried if available, otherwise content.creator will be
/// tried.
inline ircd::m::id::user
ircd::m::creator(const event &event)
{
if(likely(defined(json::get<"sender"_>(event))))
return json::get<"sender"_>(event);
const json::string creator
{
json::get<"content"_>(event).get("creator")
};
return creator;
}

View file

@ -63,6 +63,7 @@ namespace ircd::m
#include "device.h"
#include "push.h"
#include "createroom.h"
#include "creator.h"
#include "txn.h"
#include "relates.h"
#include "room/room.h"

View file

@ -775,6 +775,7 @@ ircd::m::creator(const id::room &room_id)
{
// Query the sender field of the event to get the creator. This is for
// future compatibility if the content.creator field gets eliminated.
// If the sender gets eliminated instead, include "content" in the keys.
static const event::fetch::opts fopts
{
event::keys::include {"sender"}
@ -789,10 +790,7 @@ ircd::m::creator(const id::room &room_id)
state.get("m.room.create", "", [&ret]
(const m::event &event)
{
ret = user::id
{
json::get<"sender"_>(event)
};
ret = m::creator(event);
});
return ret;