Port MatrixRoom to C

This commit is contained in:
Gergely Polonkai 2017-11-20 16:29:02 +01:00
parent ebcb781290
commit 57bbfa43a1
6 changed files with 1778 additions and 349 deletions

1
.gitignore vendored
View File

@ -69,4 +69,3 @@ Makefile.in
/src/matrix-event-call-hangup.c
/src/matrix-glib-0.0.pc
/src/matrix-message-notice.c
/src/matrix-room.c

View File

@ -36,7 +36,6 @@ libmatrix_glib_0_0_la_VALA_SOURCES = \
matrix-event-call-answer.vala \
matrix-event-call-hangup.vala \
matrix-message-notice.vala \
matrix-room.vala \
$(NULL)
AM_CPPFLAGS += \
@ -115,6 +114,7 @@ INST_H_SRC_FILES = \
matrix-event-receipt.h \
utils.h \
matrix-profile.h \
matrix-room.h \
$(NULL)
INST_H_BUILT_FILES = \
@ -157,6 +157,7 @@ libmatrix_glib_0_0_la_SOURCES = \
matrix-event-room-avatar.c \
matrix-event-room-name.c \
matrix-profile.c \
matrix-room.c \
utils.c \
matrix-enumtypes.c \
$(INST_H_SRC_FILES) \

1629
src/matrix-room.c Normal file

File diff suppressed because it is too large Load Diff

92
src/matrix-room.h Normal file
View File

@ -0,0 +1,92 @@
/*
* This file is part of matrix-glib-sdk
*
* matrix-glib-sdk is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* matrix-glib-sdk is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with matrix-glib-sdk. If not, see
* <http://www.gnu.org/licenses/>.
*/
#ifndef __MATRIX_GLIB_SDK_ROOM_H__
# define __MATRIX_GLIB_SDK_ROOM_H__
# include <glib-object.h>
# include "matrix-profile.h"
# include "matrix-types.h"
G_BEGIN_DECLS
# define MATRIX_TYPE_ROOM matrix_room_get_type()
G_DECLARE_DERIVABLE_TYPE(MatrixRoom, matrix_room, MATRIX, ROOM, GObject)
struct _MatrixRoomClass {
GObjectClass parent_class;
};
MatrixRoom *matrix_room_new(const gchar *room_id);
void matrix_room_add_member(MatrixRoom *room, const gchar *user_id, MatrixProfile *profile, gboolean third_party, GError **error);
MatrixProfile *matrix_room_get_or_add_member(MatrixRoom *room, const gchar *user_id, gboolean third_party, GError **error);
MatrixProfile *matrix_room_get_member(MatrixRoom *room, const gchar *user_id, gboolean *third_party, GError **error);
void matrix_room_remove_member(MatrixRoom *room, const gchar *user_id, GError **error);
void matrix_room_clear_user_levels(MatrixRoom *room);
void matrix_room_set_user_level(MatrixRoom *room, const gchar *user_id, gint level);
gint matrix_room_get_user_level(MatrixRoom *room, const gchar *user_id);
void matrix_room_clear_event_levels(MatrixRoom *room);
void matrix_room_set_event_level(MatrixRoom *room, const gchar *event_type, gint level);
gint matrix_room_get_event_level(MatrixRoom *room, const gchar *event_type, GError **error);
const gchar *matrix_room_get_room_id(MatrixRoom *room);
gchar **matrix_room_get_aliases(MatrixRoom *room, int *n_aliases);
void matrix_room_set_aliases(MatrixRoom *room, const gchar **aliases, int n_aliases);
const gchar *matrix_room_get_avatar_url(MatrixRoom *room);
void matrix_room_set_avatar_url(MatrixRoom *room, const gchar *avatar_url);
MatrixImageInfo *matrix_room_get_avatar_info(MatrixRoom *room);
void matrix_room_set_avatar_info(MatrixRoom *room, MatrixImageInfo *avatar_info);
const gchar *matrix_room_get_avatar_thumbnail_url(MatrixRoom *room);
void matrix_room_set_avatar_thumbnail_url(MatrixRoom *room, const gchar *avatar_thumbnail_url);
MatrixImageInfo *matrix_room_get_avatar_thumbnail_info(MatrixRoom *room);
void matrix_room_set_avatar_thumbnail_info(MatrixRoom *room, MatrixImageInfo *avatar_thumbnail_info);
const gchar *matrix_room_get_canonical_alias(MatrixRoom *room);
void matrix_room_set_canonical_alias(MatrixRoom *room, const gchar *canonical_alias);
const gchar *matrix_room_get_creator(MatrixRoom *room);
void matrix_room_set_creator(MatrixRoom *room, const gchar *creator);
gboolean matrix_room_get_federate(MatrixRoom *room);
void matrix_room_set_federate(MatrixRoom *room, gboolean federate);
MatrixGuestAccess matrix_room_get_guest_access(MatrixRoom *room);
void matrix_room_set_guest_access(MatrixRoom *room, MatrixGuestAccess guest_access);
MatrixHistoryVisibility matrix_room_get_history_visibility(MatrixRoom *room);
void matrix_room_set_history_visibility(MatrixRoom *room, MatrixHistoryVisibility history_visibility);
MatrixJoinRules matrix_room_get_join_rules(MatrixRoom *room);
void matrix_room_set_join_rules(MatrixRoom *room, MatrixJoinRules join_rules);
const gchar *matrix_room_get_name(MatrixRoom *room);
void matrix_room_set_name(MatrixRoom *room, const gchar *name);
gint matrix_room_get_default_power_level(MatrixRoom *room);
void matrix_room_set_default_power_level(MatrixRoom *room, gint default_power_level);
gint matrix_room_get_default_event_level(MatrixRoom *room);
void matrix_room_set_default_event_level(MatrixRoom *room, gint default_event_level);
gint matrix_room_get_default_state_level(MatrixRoom *room);
void matrix_room_set_default_state_level(MatrixRoom *room, gint default_state_level);
gint matrix_room_get_ban_level(MatrixRoom *room);
void matrix_room_set_ban_level(MatrixRoom *room, gint ban_level);
gint matrix_room_get_kick_level(MatrixRoom *room);
void matrix_room_set_kick_level(MatrixRoom *room, gint kick_level);
gint matrix_room_get_redact_level(MatrixRoom *room);
void matrix_room_set_redact_level(MatrixRoom *room, gint redact_level);
gint matrix_room_get_invite_level(MatrixRoom *room);
void matrix_room_set_invite_level(MatrixRoom *room, gint invite_level);
const gchar *matrix_room_get_topic(MatrixRoom *room);
void matrix_room_set_topic(MatrixRoom *room, const gchar *topic);
gchar **matrix_room_get_typing_users(MatrixRoom *room, int *n_typing_users);
void matrix_room_set_typing_users(MatrixRoom *room, gchar **typing_users, int n_typing_users);
G_END_DECLS
#endif /* __MATRIX_GLIB_SDK_ROOM_H__ */

View File

@ -1,347 +0,0 @@
/*
* This file is part of matrix-glib-sdk
*
* matrix-glib-sdk is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* matrix-glib-sdk is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with matrix-glib-sdk. If not, see
* <http://www.gnu.org/licenses/>.
*/
/**
* Class to hold data about rooms
*
* The Room class is there to hold different parameters of a room like
* its known aliases, its members, etc.
*/
public class Matrix.Room : GLib.Object {
/**
* The ID of the room this object belongs to.
*/
public string room_id { get; construct; }
/**
* All the known room aliases.
*/
public string[] aliases { get; set; }
/**
* The URL of the rooms avatar.
*/
public string? avatar_url { get; set; default = null; }
/**
* ImageInfo relevant to the room avatar.
*/
public ImageInfo? avatar_info { get; set; default = null; }
/**
* The URL of the room avatars thumbnail.
*/
public string? avatar_thumbnail_url { get; set; default = null; }
/**
* ImageInfo relevant to the room avatars thumbnail.
*/
public ImageInfo? avatar_thumbnail_info { get; set; default = null; }
/**
* The canonical alias of the room.
*/
public string? canonical_alias { get; set; default = null; }
/**
* The Matrix ID of the rooms creator.
*/
public string? creator { get; set; default = null; }
/**
* If false, the room is not federated.
*/
public bool federate { get; set; default = false; }
/**
* Wether guests are allowed to join the room.
*/
public GuestAccess guest_access { get; set; default = GuestAccess.UNKNOWN; }
/**
* This value controls the visibility of the rooms history.
*/
public HistoryVisibility history_visibility {
get; set;
default = HistoryVisibility.UNKNOWN;
}
/**
* Controls who can join the room.
*/
public JoinRules join_rules { get; set; default = JoinRules.UNKNOWN; }
/**
* The rooms name.
*/
public string? name { get; set; default = null; }
/**
* The default power level users get upon joining the room.
*/
public int default_power_level { get; set; default = 0; }
/**
* The power level required to send non-state events to the
* room. This gets applied if the event type doesnt have an
* explicit level requirement (see set_event_level() and
* get_event_level()).
*/
public int default_event_level { get; set; default = 0; }
/**
* The power level required to send state events to the room. This
* get applied if the event type doesnt have an explicit level
* requirement (see set_event_level() and get_event_level()).
*/
public int default_state_level { get; set; default = 10; }
/**
* The power level required to ban other users from the room.
*/
public int ban_level { get; set; default = 5; }
/**
* The power level required to kick other users from the room.
*/
public int kick_level { get; set; default = 5; }
/**
* The power level required to redact events in the room.
*/
public int redact_level { get; set; default = 20; }
/**
* The power level required to invite users to the room.
*/
public int invite_level { get; set; default = 0; }
/**
* The rooms topic.
*/
public string? topic { get; set; default = null; }
/**
* The users currently typing in the room.
*/
public string[] typing_users { get; set; }
private HashTable<string, int?> _event_levels =
new HashTable<string, int?>(str_hash, str_equal);
private HashTable<string, int?> _user_levels =
new HashTable<string, int?>(str_hash, str_equal);
private struct MemberData {
Profile profile;
bool thirdparty;
}
private HashTable<string, MemberData?> _members =
new HashTable<string, MemberData?>(str_hash, str_equal);
/**
* Create a new Room object.
*/
public
Room(string room_id)
{
Object(room_id : room_id);
}
/**
* Add a member to the room member list. If a member with the
* same @user_id exists, {@link Matrix.Error.ALREADY_EXISTS} is
* thrown.
*
* @param user_id the Matrix ID of the user to add
* @param third_party if true, the member is marked as a pending
* 3rd party invitation
*/
public void
add_member(string user_id, Profile? profile, bool third_party)
throws Matrix.Error
{
MemberData? data = null;
if ((data = _members[user_id]) != null) {
throw new Matrix.Error.ALREADY_EXISTS(
"User already exists in that room");
}
data = MemberData();
if (profile == null) {
data.profile = new Profile();
} else {
data.profile = profile;
}
data.thirdparty = third_party;
_members[user_id] = data;
}
/**
* Gets the profile of the room member specified in @user_id. If
* that user is not added to the room yet, it gets added with an
* empty profile and that profile is returned.
*
* @param user_id the Matrix ID of the user to add
* @param third_party if true, the member is marked as a pending
* 3rd party invitation
* @return the {@link Matrix.Profile} of the user
*/
public Profile
get_or_add_member(string user_id, bool third_party = false)
throws Matrix.Error
{
try {
return get_member(user_id, null);
} catch (Matrix.Error e) {
add_member(user_id, null, third_party);
return get_member(user_id, null);
}
}
/**
* Gets the profile of the room member specified in @user_id. If
* that user is not added to the room yet,
* {@link Matrix.Error.NOT_FOUND} is thrown.
*
* @param user_id the Matrix ID of the user to find
* @param third_party gets a true value if the member is actually
* a pending 3rd party invitation
* @return the profile of the user
*/
public Profile
get_member(string user_id, out bool third_party)
throws Matrix.Error
{
MemberData? data = null;
if ((data = _members[user_id]) == null) {
throw new Matrix.Error.NOT_FOUND(
"No such room member");
}
third_party = data.thirdparty;
return data.profile;
}
/**
* Removes a member from the member list.
*/
public void
remove_member(string user_id)
throws Matrix.Error
{
MemberData? data = null;
if ((data = _members[user_id]) == null) {
throw new Matrix.Error.NOT_FOUND(
"No such room member");
}
_members.remove(user_id);
}
/**
* Clear the stored individual user levels. This should be called
* e.g. when receiving a new m.room.power_levels event.
*/
public void
clear_user_levels()
{
_user_levels.remove_all();
}
/**
* Set an individual power level for a user.
*
* @param user_id a fully qualified Matrix ID
* @param level the new power level
*/
public void
set_user_level(string user_id, int level)
{
_user_levels[user_id] = level;
}
/**
* Get the power level of a user.
*
* @param user_id a fully qualified Matrix ID
* @return the level of the user. If the user doesnt have an
* individually set power level, the default value is
* returned
*/
public int
get_user_level(string user_id)
{
int? level = _user_levels[user_id];
if (level == null) {
level = _default_power_level;
}
return level;
}
/**
* Clear the stored event level requirements. This should be
* called e.g. when receiving a new m.room.power_levels event.
*/
public void
clear_event_levels()
{
_event_levels.remove_all();
}
/**
* Set the required level to send an event of
* type @param event_type.
*
* @param event_type the event type to restrict
* @param level the desired level for the event type
*/
public void
set_event_level(string event_type, int level)
{
_event_levels[event_type] = level;
}
/**
* Get the required level to send an event of
* type @param event_type.
*
* @param event_type the event type to query
* @return the level required to send a specific event. If there
* is no level requirement is set for this event type,
* this function returns null as there is no way to decide
* if {@link Matrix.Room.default_state_level} or
* {@link Matrix.Room.default_event_level} should be used
*/
public int?
get_event_level(string event_type)
{
return _event_levels[event_type];
}
}

View File

@ -405,6 +405,61 @@ namespace Matrix {
throws Matrix.Error;
}
[CCode (cheader_filename = "matrix-room.h")]
public class Room : GLib.Object {
public string room_id { get; construct; }
public string[] aliases { get; set; }
public string? avatar_url { get; set; default = null; }
public Matrix.ImageInfo? avatar_info { get; set; default = null; }
public string? avatar_thumbnail_url { get; set; default = null; }
public Matrix.ImageInfo? avatar_thumbnail_info { get; set; default = null; }
public string? canonical_alias { get; set; default = null; }
public string? creator { get; set; default = null; }
public bool federate { get; set; default = false; }
public Matrix.GuestAccess guest_access { get; set; default = Matrix.GuestAccess.UNKNOWN; }
public Matrix.HistoryVisibility history_visibility { get; set; default = Matrix.HistoryVisibility.UNKNOWN; }
public Matrix.JoinRules join_rules { get; set; default = Matrix.JoinRules.UNKNOWN; }
public string? name { get; set; default = null; }
public int default_power_level { get; set; default = 0; }
public int default_event_level { get; set; default = 0; }
public int default_state_level { get; set; default = 10; }
public int ban_level { get; set; default = 5; }
public int kick_level { get; set; default = 5; }
public int redact_level { get; set; default = 20; }
public int invite_level { get; set; default = 0; }
public string? topic { get; set; default = null; }
public string[] typing_users { get; set; }
public Room(string room_id);
public void add_member(string user_id, Profile? profile, bool third_party)
throws Matrix.Error;
public Profile
get_or_add_member(string user_id, bool third_party = false)
throws Matrix.Error;
public Profile
get_member(string user_id, out bool third_party)
throws Matrix.Error;
public void
remove_member(string user_id)
throws Matrix.Error;
public void clear_user_levels();
public void set_user_level(string user_id, int level);
public int get_user_level(string user_id)
throws Matrix.Error;
public void clear_event_levels();
public void set_event_level(string event_type, int level);
public int? get_event_level(string event_type);
}
/* Utilities */
[CCode (cheader_filename = "utils.h", cname = "_matrix_g_enum_to_string")]
public string? _g_enum_value_to_nick(GLib.Type enum_type, int value, char convert_dashes = '_');