Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
E
emowojibot
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
polygon
emowojibot
Commits
b09b9eb1
Commit
b09b9eb1
authored
1 year ago
by
dasPolygon
Browse files
Options
Downloads
Patches
Plain Diff
cargo fmt
parent
2db4c884
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main.rs
+54
-27
54 additions, 27 deletions
src/main.rs
with
54 additions
and
27 deletions
src/main.rs
+
54
−
27
View file @
b09b9eb1
use
image
::{
io
::
Reader
as
ImageReader
,
GenericImageView
};
use
matrix_sdk
::{
self
,
config
::
SyncSettings
,
ruma
::{
events
::{
room
::{
member
::
StrippedRoomMemberEvent
,
message
::{
MessageType
,
OriginalSyncRoomMessageEvent
},
},
events
::
room
::{
member
::
StrippedRoomMemberEvent
,
message
::{
MessageType
,
OriginalSyncRoomMessageEvent
},
},
RoomId
,
},
Client
,
Room
,
RoomState
,
};
use
serde_json
::{
Value
as
JsonValue
,
json
};
use
std
::{
collections
::
HashMap
,
env
,
fs
,
process
::
exit
,
fs
::
File
,
path
::
Path
,
io
::
Result
};
use
image
::{
io
::
Reader
as
ImageReader
,
GenericImageView
};
use
mime_guess
;
use
mime_guess
::
from_path
;
use
serde_json
::{
json
,
Value
as
JsonValue
};
use
std
::{
collections
::
HashMap
,
env
,
fs
,
fs
::
File
,
io
::
Result
,
path
::
Path
,
process
::
exit
};
use
tokio
::
time
::{
sleep
,
Duration
};
use
mime_guess
;
use
tracing_subscriber
;
use
uuid
::
Uuid
;
...
...
@@ -25,19 +23,30 @@ use uuid::Uuid;
async
fn
main
()
->
anyhow
::
Result
<
()
>
{
tracing_subscriber
::
fmt
::
init
();
let
(
homeserver_url
,
username
,
password
)
=
match
(
env
::
args
()
.nth
(
1
),
env
::
args
()
.nth
(
2
),
env
::
args
()
.nth
(
3
))
{
(
Some
(
a
),
Some
(
b
),
Some
(
c
))
=>
(
a
,
b
,
c
),
_
=>
{
eprintln!
(
"Usage: {} <homeserver_url> <username> <password>"
,
env
::
args
()
.next
()
.unwrap
());
exit
(
1
);
}
};
let
(
homeserver_url
,
username
,
password
)
=
match
(
env
::
args
()
.nth
(
1
),
env
::
args
()
.nth
(
2
),
env
::
args
()
.nth
(
3
))
{
(
Some
(
a
),
Some
(
b
),
Some
(
c
))
=>
(
a
,
b
,
c
),
_
=>
{
eprintln!
(
"Usage: {} <homeserver_url> <username> <password>"
,
env
::
args
()
.next
()
.unwrap
()
);
exit
(
1
);
}
};
login_and_sync
(
homeserver_url
,
&
username
,
&
password
)
.await
?
;
Ok
(())
}
async
fn
login_and_sync
(
homeserver_url
:
String
,
username
:
&
str
,
password
:
&
str
)
->
anyhow
::
Result
<
()
>
{
let
client
=
Client
::
builder
()
.homeserver_url
(
homeserver_url
)
.build
()
.await
?
;
async
fn
login_and_sync
(
homeserver_url
:
String
,
username
:
&
str
,
password
:
&
str
,
)
->
anyhow
::
Result
<
()
>
{
let
client
=
Client
::
builder
()
.homeserver_url
(
homeserver_url
)
.build
()
.await
?
;
client
.matrix_auth
()
...
...
@@ -56,7 +65,11 @@ async fn login_and_sync(homeserver_url: String, username: &str, password: &str)
Ok
(())
}
async
fn
on_stripped_state_member
(
room_member
:
StrippedRoomMemberEvent
,
client
:
Client
,
room
:
Room
){
async
fn
on_stripped_state_member
(
room_member
:
StrippedRoomMemberEvent
,
client
:
Client
,
room
:
Room
,
)
{
if
room_member
.state_key
!=
client
.user_id
()
.unwrap
()
{
return
;
}
...
...
@@ -66,7 +79,12 @@ async fn on_stripped_state_member(room_member: StrippedRoomMemberEvent, client:
let
mut
delay
=
2
;
while
let
Err
(
err
)
=
room
.join
()
.await
{
eprintln!
(
"Failed to join room {} ({:?}), retrying in {}s"
,
room
.room_id
(),
err
,
delay
);
eprintln!
(
"Failed to join room {} ({:?}), retrying in {}s"
,
room
.room_id
(),
err
,
delay
);
sleep
(
Duration
::
from_secs
(
delay
))
.await
;
delay
*=
2
;
...
...
@@ -83,7 +101,9 @@ async fn on_room_message(event: OriginalSyncRoomMessageEvent, room: Room, client
if
room
.state
()
!=
RoomState
::
Joined
{
return
;
}
let
MessageType
::
Text
(
text_content
)
=
event
.content.msgtype
else
{
return
;
};
let
MessageType
::
Text
(
text_content
)
=
event
.content.msgtype
else
{
return
;
};
let
mut
parts
=
text_content
.body
.trim
()
.splitn
(
2
,
' '
);
let
command
=
parts
.next
()
.unwrap_or_default
();
...
...
@@ -117,9 +137,9 @@ fn create_image_object(mxc_url: String, path_string: &str) -> anyhow::Result<Jso
let
metadata
=
path
.metadata
()
?
;
let
size
=
metadata
.len
();
let
mime_type
=
from_path
(
&
path
)
.first_or_octet_stream
()
.essence_str
()
.to_string
();
.first_or_octet_stream
()
.essence_str
()
.to_string
();
Ok
(
serde_json
::
json!
({
"url"
:
mxc_url
,
...
...
@@ -142,11 +162,17 @@ fn create_pack_event(images: &HashMap<String, JsonValue>, pack_name: &str) -> Js
})
}
async
fn
send_pack_event
(
client
:
&
Client
,
room_id
:
&
RoomId
,
event
:
JsonValue
)
->
anyhow
::
Result
<
()
>
{
async
fn
send_pack_event
(
client
:
&
Client
,
room_id
:
&
RoomId
,
event
:
JsonValue
,
)
->
anyhow
::
Result
<
()
>
{
println!
(
"Sending im.ponies.room_emotes event to the room"
);
if
let
Some
(
room
)
=
client
.get_room
(
&
room_id
)
{
let
uuid
=
Uuid
::
new_v4
();
let
response
=
room
.send_state_event_raw
(
"im.ponies.room_emotes"
,
&
uuid
.to_string
(),
json!
(
event
))
.await
?
;
let
response
=
room
.send_state_event_raw
(
"im.ponies.room_emotes"
,
&
uuid
.to_string
(),
json!
(
event
))
.await
?
;
println!
(
"Got event id {}"
,
response
.event_id
);
}
Ok
(())
...
...
@@ -164,7 +190,8 @@ async fn process_image_pack(client: &Client, room: &Room, pack_name: &str) -> an
match
create_image_object
(
mxc_url
,
path
.to_str
()
.unwrap
())
{
Ok
(
image_object
)
=>
{
images
.insert
(
shortcode
,
image_object
);
}
Err
(
e
)
=>
{
}
Err
(
e
)
=>
{
eprintln!
(
"Error processing image {}: {}"
,
shortcode
,
e
);
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment