Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F7890103
matrix-room.cpp
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
7 KB
Referenced Files
None
Subscribers
None
matrix-room.cpp
View Options
/*
* This file is part of kazv.
* SPDX-FileCopyrightText: 2020-2023 tusooa <tusooa@kazv.moe>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include
<libkazv-config.hpp>
#include
<immer/config.hpp>
// https://github.com/arximboldi/immer/issues/168
#include
<lager/setter.hpp>
#include
<nlohmann/json.hpp>
#include
<event.hpp>
#include
<QFile>
#include
<QMimeDatabase>
#include
"kazv-log.hpp"
#include
"matrix-room.hpp"
#include
"matrix-room-timeline.hpp"
#include
"matrix-room-member.hpp"
#include
"matrix-room-member-list-model.hpp"
#include
"matrix-promise.hpp"
#include
"matrix-event.hpp"
#include
"qfunctionutils.hpp"
#include
"helper.hpp"
using
namespace
Kazv
;
static
const
int
typingDebounceMs
=
500
;
static
const
int
typingTimeoutMs
=
1000
;
MatrixRoom
::
MatrixRoom
(
Kazv
::
Room
room
,
lager
::
reader
<
std
::
string
>
selfUserId
,
QObject
*
parent
)
:
QObject
(
parent
)
,
m_room
(
room
)
,
m_selfUserId
(
selfUserId
)
,
m_memberNames
(
m_room
.
members
())
,
LAGER_QT
(
roomId
)(
m_room
.
roomId
().
xform
(
strToQt
))
,
LAGER_QT
(
name
)(
m_room
.
nameOpt
()[
lager
::
lenses
::
or_default
].
xform
(
strToQt
))
,
LAGER_QT
(
heroNames
)(
m_room
.
heroDisplayNames
().
xform
(
strListToQt
))
,
LAGER_QT
(
avatarMxcUri
)(
m_room
.
avatarMxcUri
().
xform
(
strToQt
))
,
LAGER_QT
(
roomOrHeroAvatarMxcUri
)(
lager
::
with
(
m_room
.
heroMemberEvents
(),
m_room
.
avatarMxcUri
())
.
map
([](
const
auto
&
heroes
,
const
auto
&
roomAvatar
)
{
if
(
!
roomAvatar
.
empty
())
{
return
roomAvatar
;
}
if
(
heroes
.
size
()
==
1
)
{
return
zug
::
reduce
(
roomMemberToAvatar
(
zug
::
last
),
std
::
string
(),
heroes
);
}
return
std
::
string
();
})
.
xform
(
strToQt
))
,
LAGER_QT
(
localDraft
)(
lager
::
with_setter
(
m_room
.
localDraft
().
xform
(
strToQt
).
make
(),
[
this
](
QString
localDraft
)
{
m_room
.
setLocalDraft
(
localDraft
.
toStdString
());
},
lager
::
automatic_tag
{}))
,
LAGER_QT
(
memberNames
)(
m_memberNames
.
xform
(
strListToQt
))
,
LAGER_QT
(
tagIds
)(
m_room
.
tags
().
map
([](
const
auto
&
tagsMap
)
{
return
zug
::
into
(
QStringList
(),
zug
::
map
([](
const
auto
pair
)
{
return
QString
::
fromStdString
(
pair
.
first
);
}),
tagsMap
);
}))
,
LAGER_QT
(
membership
)(
m_room
.
membership
().
map
([](
const
auto
&
membership
)
{
return
static_cast
<
Membership
>
(
membership
);
}))
,
m_setTypingThrottled
(
QFunctionUtils
::
Throttle
([
self
=
QPointer
<
MatrixRoom
>
(
this
)]()
{
if
(
self
)
{
self
->
setTypingImpl
();
}
},
typingDebounceMs
))
{
}
MatrixRoom
::~
MatrixRoom
()
=
default
;
MatrixRoomTimeline
*
MatrixRoom::timeline
()
const
{
return
new
MatrixRoomTimeline
(
m_room
);
}
void
MatrixRoom::sendTextMessage
(
QString
text
)
const
{
m_room
.
sendTextMessage
(
text
.
toStdString
());
}
void
MatrixRoom::sendMediaFileMessage
(
QString
fileName
,
QString
mimeType
,
qint64
fileSize
,
QString
mxcUri
)
const
{
auto
j
=
makeMediaFileMessageJson
(
fileName
,
mimeType
,
fileSize
,
mxcUri
);
Kazv
::
Event
e
{
j
};
m_room
.
sendMessage
(
e
);
}
void
MatrixRoom::sendEncryptedFileMessage
(
const
QString
&
fileName
,
const
QString
&
mimeType
,
const
qint64
fileSize
,
const
QString
&
mxcUri
,
const
QString
&
key
,
const
QString
&
iv
,
const
QByteArray
&
hash
)
const
{
auto
j
=
makeEncryptedFileMessageJson
(
fileName
,
mimeType
,
fileSize
,
mxcUri
,
key
,
iv
,
hash
);
Kazv
::
Event
e
{
j
};
m_room
.
sendMessage
(
e
);
}
void
MatrixRoom::resendMessage
(
QString
txnId
)
const
{
m_room
.
resendMessage
(
txnId
.
toStdString
());
}
nlohmann
::
json
MatrixRoom::makeMediaFileMessageJson
(
QString
fileName
,
QString
mimeType
,
qint64
fileSize
,
QString
mxcUri
)
const
{
static
auto
available_msgtype
=
std
::
array
<
std
::
string
,
3
>
{
"m.audio"
,
"m.video"
,
"m.image"
};
auto
try_msgtype
=
std
::
find
(
available_msgtype
.
begin
(),
available_msgtype
.
end
(),
QStringLiteral
(
"m."
).
append
(
mimeType
.
split
(
QChar
(
'/'
))[
0
]).
toStdString
());
std
::
string
msgtype
;
if
(
try_msgtype
==
available_msgtype
.
end
())
{
msgtype
=
"m.file"
;
}
else
{
msgtype
=
*
try_msgtype
;
}
return
nlohmann
::
json
{
{
"type"
,
"m.room.message"
},
{
"content"
,
{
{
"msgtype"
,
msgtype
},
{
"body"
,
fileName
.
toStdString
()},
{
"url"
,
mxcUri
.
toStdString
()},
{
"info"
,
{
{
"size"
,
fileSize
},
{
"mimetype"
,
mimeType
.
toStdString
()}
}}
}}
};
}
nlohmann
::
json
MatrixRoom::makeEncryptedFileMessageJson
(
const
QString
&
fileName
,
const
QString
&
mimeType
,
const
qint64
fileSize
,
const
QString
&
mxcUri
,
const
QString
&
key
,
const
QString
&
iv
,
const
QByteArray
&
hash
)
const
{
static
auto
available_msgtype
=
std
::
array
<
std
::
string
,
3
>
{
"m.audio"
,
"m.video"
,
"m.image"
};
auto
try_msgtype
=
std
::
find
(
available_msgtype
.
begin
(),
available_msgtype
.
end
(),
QStringLiteral
(
"m."
).
append
(
mimeType
.
split
(
QChar
(
'/'
))[
0
]).
toStdString
());
std
::
string
msgtype
;
if
(
try_msgtype
==
available_msgtype
.
end
())
{
msgtype
=
"m.file"
;
}
else
{
msgtype
=
*
try_msgtype
;
}
return
nlohmann
::
json
{
{
"type"
,
"m.room.message"
},
{
"content"
,
{
{
"msgtype"
,
msgtype
},
{
"body"
,
fileName
.
toStdString
()},
{
"file"
,
{
{
"url"
,
mxcUri
.
toStdString
()},
{
"key"
,
{
{
"kty"
,
"oct"
},
{
"key_ops"
,
nlohmann
::
json
::
array
({
"encrypt"
,
"decrypt"
})},
{
"alg"
,
"A256CTR"
},
{
"k"
,
key
.
toStdString
()},
{
"ext"
,
true
}
}},
{
"iv"
,
iv
.
toStdString
()},
{
"hashes"
,
{
{
"sha256"
,
hash
.
toStdString
()}
}},
{
"v"
,
"v2"
}
}},
{
"info"
,
{
{
"size"
,
fileSize
},
{
"mimetype"
,
mimeType
.
toStdString
()}
}}
}}
};
}
MatrixPromise
*
MatrixRoom::redactEvent
(
QString
eventId
,
QString
reason
)
const
{
qCInfo
(
kazvLog
)
<<
"redactEvent("
<<
eventId
<<
", "
<<
reason
<<
")"
;
return
new
MatrixPromise
(
m_room
.
redactEvent
(
eventId
.
toStdString
(),
reason
.
isEmpty
()
?
std
::
nullopt
:
std
::
optional
<
std
::
string
>
(
reason
.
toStdString
())
));
}
MatrixPromise
*
MatrixRoom::addOrSetTag
(
QString
tagId
)
const
{
return
new
MatrixPromise
(
m_room
.
addOrSetTag
(
tagId
.
toStdString
()));
}
MatrixPromise
*
MatrixRoom::removeTag
(
QString
tagId
)
const
{
return
new
MatrixPromise
(
m_room
.
removeTag
(
tagId
.
toStdString
()));
}
MatrixPromise
*
MatrixRoom::paginateBackFrom
(
QString
eventId
)
const
{
return
new
MatrixPromise
(
m_room
.
paginateBackFromEvent
(
eventId
.
toStdString
()));
}
MatrixPromise
*
MatrixRoom::leaveRoom
()
const
{
return
new
MatrixPromise
(
m_room
.
leave
());
}
MatrixRoomMember
*
MatrixRoom::memberAt
(
int
index
)
const
{
return
new
MatrixRoomMember
(
m_room
.
memberEventByCursor
(
m_memberNames
[
index
][
lager
::
lenses
::
or_default
]));
}
MatrixRoomMember
*
MatrixRoom::member
(
QString
userId
)
const
{
return
new
MatrixRoomMember
(
m_room
.
memberEventFor
(
userId
.
toStdString
()));
}
MatrixEvent
*
MatrixRoom::messageById
(
QString
eventId
)
const
{
return
new
MatrixEvent
(
m_room
.
message
(
lager
::
make_constant
(
eventId
.
toStdString
()))
.
map
([](
const
auto
&
e
)
->
std
::
variant
<
Kazv
::
Event
,
Kazv
::
LocalEchoDesc
>
{
return
e
;
})
);
}
MatrixRoomMemberListModel
*
MatrixRoom::typingUsers
()
const
{
return
new
MatrixRoomMemberListModel
(
m_room
.
typingMemberEvents
()
.
xform
(
containerMap
(
EventList
{},
zug
::
filter
([
self
=
m_selfUserId
.
make
().
get
()](
const
auto
&
e
)
{
return
e
.
stateKey
()
!=
self
;
})))
);
}
void
MatrixRoom::setTyping
(
bool
typing
)
{
if
(
typing
)
{
m_setTypingThrottled
();
}
else
{
m_room
.
setTyping
(
false
,
std
::
nullopt
);
}
}
void
MatrixRoom::setTypingImpl
()
{
m_room
.
setTyping
(
true
,
typingTimeoutMs
);
}
File Metadata
Details
Attached
Mime Type
text/x-c
Expires
Thu, Oct 2, 2:57 AM (16 h, 24 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
472470
Default Alt Text
matrix-room.cpp (7 KB)
Attached To
Mode
rK kazv
Attached
Detach File
Event Timeline
Log In to Comment