[MOD/ADD] Separate AS code, XMPP reactions removal

This commit is contained in:
LDA 2024-08-21 13:51:52 +02:00
commit fb511b4df0
16 changed files with 1183 additions and 1029 deletions

138
src/AS/Profile.c Normal file
View file

@ -0,0 +1,138 @@
#include <AS.h>
#include <Cytoplasm/Memory.h>
#include <Cytoplasm/Str.h>
#include <Cytoplasm/Log.h>
#include <Cytoplasm/Uri.h>
#include <string.h>
#include <stdlib.h>
#include <Matrix.h>
void
ASSetAvatar(const ParseeConfig *conf, char *user, char *mxc)
{
HttpClientContext *ctx = NULL;
HashMap *json;
char *path;
if (!conf || !user || !mxc)
{
return;
}
user = HttpUrlEncode(user);
path = StrConcat(6,
"/_matrix/client/v3/profile/",
user, "/avatar_url", "?",
"user_id=", user
);
json = HashMapCreate();
HashMapSet(json, "avatar_url", JsonValueString(mxc));
ctx = ParseeCreateRequest(conf, HTTP_PUT, path);
Free(path);
ASAuthenticateRequest(conf, ctx);
ParseeSetRequestJSON(ctx, json);
HttpClientContextFree(ctx);
JsonFree(json);
Free(user);
}
void
ASSetName(const ParseeConfig *conf, char *user, char *name)
{
HttpClientContext *ctx = NULL;
HashMap *json;
char *path;
if (!conf || !user || !name)
{
return;
}
user = HttpUrlEncode(user);
path = StrConcat(6,
"/_matrix/client/v3/profile/",
user, "/displayname", "?",
"user_id=", user
);
json = HashMapCreate();
HashMapSet(json, "displayname", JsonValueString(name));
ctx = ParseeCreateRequest(conf, HTTP_PUT, path);
Free(path);
ASAuthenticateRequest(conf, ctx);
ParseeSetRequestJSON(ctx, json);
HttpClientContextFree(ctx);
JsonFree(json);
Free(user);
}
char *
ASGetName(const ParseeConfig *c, char *room, char *user)
{
HttpClientContext *ctx;
HashMap *reply;
char *path, *ret;
char *u2 = user;
if (!c || !user)
{
return NULL;
}
if (!room)
{
user = HttpUrlEncode(user);
path = StrConcat(3,
"/_matrix/client/v3/profile/", user, "/displayname"
);
ctx = ParseeCreateRequest(c, HTTP_GET, path);
Free(user);
ASAuthenticateRequest(c, ctx);
HttpRequestSendHeaders(ctx);
HttpRequestSend(ctx);
reply = JsonDecode(HttpClientStream(ctx));
ret = StrDuplicate(
JsonValueAsString(HashMapGet(reply, "displayname"))
);
HttpClientContextFree(ctx);
JsonFree(reply);
Free(path);
if (!ret)
{
ret = StrDuplicate(u2);
}
return ret;
}
user = HttpUrlEncode(user);
room = HttpUrlEncode(room);
path = StrConcat(4,
"/_matrix/client/v3/rooms/", room,
"/state/m.room.member/", user
);
ctx = ParseeCreateRequest(c, HTTP_GET, path);
Free(user);
Free(room);
ASAuthenticateRequest(c, ctx);
HttpRequestSendHeaders(ctx);
HttpRequestSend(ctx);
reply = JsonDecode(HttpClientStream(ctx));
ret = StrDuplicate(
JsonValueAsString(HashMapGet(reply, "displayname"))
);
HttpClientContextFree(ctx);
JsonFree(reply);
Free(path);
if (!ret)
{
ret = StrDuplicate(u2);
}
return ret;
}