2023-05-12 05:35:37 +00:00
const { test } = require ( "supertape" )
const { messageToEvent } = require ( "./message-to-event" )
const data = require ( "../../test/data" )
2023-07-11 04:51:30 +00:00
const Ty = require ( "../../types" )
/ * *
* @ param { string } roomID
* @ param { string } eventID
* @ returns { ( roomID : string , eventID : string ) => Promise < Ty . Event . Outer < Ty . Event . M _Room _Message >> }
* /
function mockGetEvent ( t , roomID _in , eventID _in , outer ) {
return async function ( roomID , eventID ) {
t . equal ( roomID , roomID _in )
t . equal ( eventID , eventID _in )
return new Promise ( resolve => {
setTimeout ( ( ) => {
resolve ( {
event _id : eventID _in ,
room _id : roomID _in ,
origin _server _ts : 1680000000000 ,
unsigned : {
age : 2245 ,
transaction _id : "$local.whatever"
} ,
... outer
} )
} )
} )
}
}
2023-05-12 05:35:37 +00:00
2023-07-07 05:31:39 +00:00
test ( "message2event: simple plaintext" , async t => {
2023-08-16 08:44:38 +00:00
const events = await messageToEvent ( data . message . simple _plaintext , data . guild . general , { } )
2023-07-07 07:28:23 +00:00
t . deepEqual ( events , [ {
$type : "m.room.message" ,
2023-07-11 04:51:30 +00:00
"m.mentions" : { } ,
2023-07-07 07:28:23 +00:00
msgtype : "m.text" ,
body : "ayy lmao"
} ] )
2023-07-07 05:31:39 +00:00
} )
2023-08-17 13:22:33 +00:00
test ( "message2event: simple plaintext with quotes" , async t => {
const events = await messageToEvent ( data . message . simple _plaintext _with _quotes , data . guild . general , { } )
t . deepEqual ( events , [ {
$type : "m.room.message" ,
"m.mentions" : { } ,
msgtype : "m.text" ,
body : ` then he said, "you and her aren't allowed in here!" `
} ] )
} )
2023-07-07 05:31:39 +00:00
test ( "message2event: simple user mention" , async t => {
2023-08-16 08:44:38 +00:00
const events = await messageToEvent ( data . message . simple _user _mention , data . guild . general , { } )
2023-07-07 07:28:23 +00:00
t . deepEqual ( events , [ {
$type : "m.room.message" ,
2023-07-11 04:51:30 +00:00
"m.mentions" : { } ,
2023-07-07 07:28:23 +00:00
msgtype : "m.text" ,
body : "@crunch god: Tell me about Phil, renowned martial arts master and creator of the Chin Trick" ,
format : "org.matrix.custom.html" ,
formatted _body : '<a href="https://matrix.to/#/@_ooye_crunch_god:cadence.moe">@crunch god</a> Tell me about Phil, renowned martial arts master and creator of the Chin Trick'
} ] )
2023-07-07 05:31:39 +00:00
} )
2023-07-10 20:01:11 +00:00
test ( "message2event: simple room mention" , async t => {
2023-08-16 08:44:38 +00:00
const events = await messageToEvent ( data . message . simple _room _mention , data . guild . general , { } )
2023-07-10 20:01:11 +00:00
t . deepEqual ( events , [ {
$type : "m.room.message" ,
2023-07-11 04:51:30 +00:00
"m.mentions" : { } ,
2023-07-10 20:01:11 +00:00
msgtype : "m.text" ,
2023-07-10 22:10:47 +00:00
body : "#main" ,
2023-07-10 20:01:11 +00:00
format : "org.matrix.custom.html" ,
2023-07-10 22:10:47 +00:00
formatted _body : '<a href="https://matrix.to/#/!kLRqKKUQXcibIMtOpl:cadence.moe">#main</a>'
2023-07-10 20:01:11 +00:00
} ] )
} )
test ( "message2event: simple message link" , async t => {
2023-08-16 08:44:38 +00:00
const events = await messageToEvent ( data . message . simple _message _link , data . guild . general , { } )
2023-07-10 20:01:11 +00:00
t . deepEqual ( events , [ {
$type : "m.room.message" ,
2023-07-11 04:51:30 +00:00
"m.mentions" : { } ,
2023-07-10 20:01:11 +00:00
msgtype : "m.text" ,
body : "https://matrix.to/#/!kLRqKKUQXcibIMtOpl:cadence.moe/$X16nfVks1wsrhq4E9SSLiqrf2N8KD0erD0scZG7U5xg" ,
format : "org.matrix.custom.html" ,
formatted _body : '<a href="https://matrix.to/#/!kLRqKKUQXcibIMtOpl:cadence.moe/$X16nfVks1wsrhq4E9SSLiqrf2N8KD0erD0scZG7U5xg">https://matrix.to/#/!kLRqKKUQXcibIMtOpl:cadence.moe/$X16nfVks1wsrhq4E9SSLiqrf2N8KD0erD0scZG7U5xg</a>'
} ] )
} )
2023-07-01 13:41:31 +00:00
test ( "message2event: attachment with no content" , async t => {
2023-08-16 08:44:38 +00:00
const events = await messageToEvent ( data . message . attachment _no _content , data . guild . general , { } )
2023-07-07 07:28:23 +00:00
t . deepEqual ( events , [ {
$type : "m.room.message" ,
2023-07-11 04:51:30 +00:00
"m.mentions" : { } ,
2023-07-07 07:28:23 +00:00
msgtype : "m.image" ,
url : "mxc://cadence.moe/qXoZktDqNtEGuOCZEADAMvhM" ,
body : "image.png" ,
external _url : "https://cdn.discordapp.com/attachments/497161332244742154/1124628646431297546/image.png" ,
2023-08-30 03:20:39 +00:00
filename : "image.png" ,
2023-07-07 07:28:23 +00:00
info : {
mimetype : "image/png" ,
w : 466 ,
h : 85 ,
size : 12919 ,
} ,
} ] )
2023-07-01 13:41:31 +00:00
} )
2023-09-02 09:44:54 +00:00
test ( "message2event: spoiler attachment" , async t => {
const events = await messageToEvent ( data . message . spoiler _attachment , data . guild . general , { } )
t . deepEqual ( events , [ {
$type : "m.room.message" ,
"m.mentions" : { } ,
msgtype : "m.text" ,
body : "📄 Uploaded SPOILER file: https://cdn.discordapp.com/attachments/1100319550446252084/1147465564307079258/SPOILER_69-GNDP-CADENCE.nfs.gci (74 KB)" ,
format : "org.matrix.custom.html" ,
formatted _body : "<blockquote>📄 Uploaded SPOILER file: <span data-mx-spoiler><a href=\"https://cdn.discordapp.com/attachments/1100319550446252084/1147465564307079258/SPOILER_69-GNDP-CADENCE.nfs.gci\">View</a></span> (74 KB)</blockquote>"
} ] )
} )
2023-05-12 05:35:37 +00:00
test ( "message2event: stickers" , async t => {
2023-08-16 08:44:38 +00:00
const events = await messageToEvent ( data . message . sticker , data . guild . general , { } )
2023-07-07 07:28:23 +00:00
t . deepEqual ( events , [ {
$type : "m.room.message" ,
2023-07-11 04:51:30 +00:00
"m.mentions" : { } ,
2023-07-07 07:28:23 +00:00
msgtype : "m.text" ,
body : "can have attachments too"
} , {
$type : "m.room.message" ,
2023-07-11 04:51:30 +00:00
"m.mentions" : { } ,
2023-07-07 07:28:23 +00:00
msgtype : "m.image" ,
url : "mxc://cadence.moe/ZDCNYnkPszxGKgObUIFmvjus" ,
body : "image.png" ,
external _url : "https://cdn.discordapp.com/attachments/122155380120748034/1106366167486038016/image.png" ,
2023-08-30 03:20:39 +00:00
filename : "image.png" ,
2023-07-07 07:28:23 +00:00
info : {
mimetype : "image/png" ,
w : 333 ,
h : 287 ,
size : 127373 ,
} ,
} , {
$type : "m.sticker" ,
2023-07-11 04:51:30 +00:00
"m.mentions" : { } ,
2023-07-07 07:28:23 +00:00
body : "pomu puff - damn that tiny lil bitch really chuffing. puffing that fat ass dart" ,
info : {
mimetype : "image/png"
// thumbnail_url
// thumbnail_info
} ,
url : "mxc://cadence.moe/UuUaLwXhkxFRwwWCXipDlBHn"
} ] )
2023-05-12 05:35:37 +00:00
} )
2023-07-11 04:51:30 +00:00
test ( "message2event: skull webp attachment with content" , async t => {
2023-08-16 08:44:38 +00:00
const events = await messageToEvent ( data . message . skull _webp _attachment _with _content , data . guild . general , { } )
2023-07-11 04:51:30 +00:00
t . deepEqual ( events , [ {
$type : "m.room.message" ,
"m.mentions" : { } ,
msgtype : "m.text" ,
body : "Image"
} , {
$type : "m.room.message" ,
"m.mentions" : { } ,
msgtype : "m.image" ,
body : "skull.webp" ,
info : {
w : 1200 ,
h : 628 ,
mimetype : "image/webp" ,
size : 74290
} ,
external _url : "https://cdn.discordapp.com/attachments/112760669178241024/1128084747910918195/skull.webp" ,
2023-08-30 03:20:39 +00:00
filename : "skull.webp" ,
2023-07-11 04:51:30 +00:00
url : "mxc://cadence.moe/sDxWmDErBhYBxtDcJQgBETes"
} ] )
} )
test ( "message2event: reply to skull webp attachment with content" , async t => {
2023-08-16 08:44:38 +00:00
const events = await messageToEvent ( data . message . reply _to _skull _webp _attachment _with _content , data . guild . general , { } )
2023-07-11 04:51:30 +00:00
t . deepEqual ( events , [ {
$type : "m.room.message" ,
"m.relates_to" : {
"m.in_reply_to" : {
event _id : "$oLyUTyZ_7e_SUzGNWZKz880ll9amLZvXGbArJCKai2Q"
}
} ,
"m.mentions" : { } ,
msgtype : "m.text" ,
body : "> Extremity: Image\n\nReply" ,
format : "org.matrix.custom.html" ,
formatted _body :
'<mx-reply><blockquote><a href="https://matrix.to/#/!kLRqKKUQXcibIMtOpl:cadence.moe/$oLyUTyZ_7e_SUzGNWZKz880ll9amLZvXGbArJCKai2Q">In reply to</a> Extremity'
+ '<br>Image</blockquote></mx-reply>'
+ 'Reply'
} , {
$type : "m.room.message" ,
"m.mentions" : { } ,
msgtype : "m.image" ,
body : "RDT_20230704_0936184915846675925224905.jpg" ,
info : {
w : 2048 ,
h : 1536 ,
mimetype : "image/jpeg" ,
size : 85906
} ,
external _url : "https://cdn.discordapp.com/attachments/112760669178241024/1128084851023675515/RDT_20230704_0936184915846675925224905.jpg" ,
2023-08-30 03:20:39 +00:00
filename : "RDT_20230704_0936184915846675925224905.jpg" ,
2023-07-11 04:51:30 +00:00
url : "mxc://cadence.moe/WlAbFSiNRIHPDEwKdyPeGywa"
} ] )
} )
test ( "message2event: simple reply to matrix user" , async t => {
2023-08-16 08:44:38 +00:00
const events = await messageToEvent ( data . message . simple _reply _to _matrix _user , data . guild . general , { } , {
api : {
getEvent : mockGetEvent ( t , "!kLRqKKUQXcibIMtOpl:cadence.moe" , "$Ij3qo7NxMA4VPexlAiIx2CB9JbsiGhJeyt-2OvkAUe4" , {
type : "m.room.message" ,
content : {
msgtype : "m.text" ,
body : "so can you reply to my webhook uwu"
} ,
sender : "@cadence:cadence.moe"
} )
}
2023-07-11 04:51:30 +00:00
} )
t . deepEqual ( events , [ {
$type : "m.room.message" ,
"m.relates_to" : {
"m.in_reply_to" : {
event _id : "$Ij3qo7NxMA4VPexlAiIx2CB9JbsiGhJeyt-2OvkAUe4"
}
} ,
"m.mentions" : {
user _ids : [
"@cadence:cadence.moe"
]
} ,
msgtype : "m.text" ,
body : "> cadence: so can you reply to my webhook uwu\n\nReply" ,
format : "org.matrix.custom.html" ,
formatted _body :
'<mx-reply><blockquote><a href="https://matrix.to/#/!kLRqKKUQXcibIMtOpl:cadence.moe/$Ij3qo7NxMA4VPexlAiIx2CB9JbsiGhJeyt-2OvkAUe4">In reply to</a> <a href="https://matrix.to/#/@cadence:cadence.moe">cadence</a>'
+ '<br>so can you reply to my webhook uwu</blockquote></mx-reply>'
+ 'Reply'
} ] )
} )
2023-08-16 08:44:38 +00:00
test ( "message2event: simple reply to matrix user, reply fallbacks disabled" , async t => {
const events = await messageToEvent ( data . message . simple _reply _to _matrix _user , data . guild . general , { includeReplyFallback : false } , {
api : {
getEvent : mockGetEvent ( t , "!kLRqKKUQXcibIMtOpl:cadence.moe" , "$Ij3qo7NxMA4VPexlAiIx2CB9JbsiGhJeyt-2OvkAUe4" , {
type : "m.room.message" ,
content : {
msgtype : "m.text" ,
body : "so can you reply to my webhook uwu"
} ,
sender : "@cadence:cadence.moe"
} )
}
} )
t . deepEqual ( events , [ {
$type : "m.room.message" ,
"m.relates_to" : {
"m.in_reply_to" : {
event _id : "$Ij3qo7NxMA4VPexlAiIx2CB9JbsiGhJeyt-2OvkAUe4"
}
} ,
"m.mentions" : {
user _ids : [
"@cadence:cadence.moe"
]
} ,
msgtype : "m.text" ,
body : "Reply"
} ] )
} )
2023-08-17 06:17:53 +00:00
test ( "message2event: simple written @mentions for matrix users" , async t => {
let called = 0
2023-08-16 08:44:38 +00:00
const events = await messageToEvent ( data . message . simple _written _at _mention _for _matrix , data . guild . general , { } , {
api : {
async getJoinedMembers ( roomID ) {
2023-08-17 06:17:53 +00:00
called ++
2023-08-16 08:44:38 +00:00
t . equal ( roomID , "!kLRqKKUQXcibIMtOpl:cadence.moe" )
return new Promise ( resolve => {
setTimeout ( ( ) => {
resolve ( {
joined : {
"@cadence:cadence.moe" : {
2023-09-03 13:43:12 +00:00
displayname : "cadence [they]" ,
2023-08-16 08:44:38 +00:00
avatar _url : "whatever"
} ,
"@huckleton:cadence.moe" : {
2023-09-03 13:43:12 +00:00
displayname : "huck" ,
2023-08-16 08:44:38 +00:00
avatar _url : "whatever"
} ,
"@_ooye_botrac4r:cadence.moe" : {
2023-09-03 13:43:12 +00:00
displayname : "botrac4r" ,
2023-08-16 08:44:38 +00:00
avatar _url : "whatever"
} ,
"@_ooye_bot:cadence.moe" : {
2023-09-03 13:43:12 +00:00
displayname : "Out Of Your Element" ,
2023-08-16 08:44:38 +00:00
avatar _url : "whatever"
}
2023-07-11 05:27:40 +00:00
}
2023-08-16 08:44:38 +00:00
} )
2023-07-11 05:27:40 +00:00
} )
} )
2023-08-16 08:44:38 +00:00
}
2023-07-11 05:27:40 +00:00
}
} )
t . deepEqual ( events , [ {
$type : "m.room.message" ,
"m.mentions" : {
user _ids : [
"@cadence:cadence.moe" ,
"@huckleton:cadence.moe"
]
} ,
msgtype : "m.text" ,
body : "@Cadence, tell me about @Phil, the creator of the Chin Trick, who has become ever more powerful under the mentorship of @botrac4r and @huck"
} ] )
2023-08-17 06:17:53 +00:00
t . equal ( called , 1 , "should only look up the member list once" )
2023-07-11 05:27:40 +00:00
} )
2023-07-13 05:36:20 +00:00
test ( "message2event: very large attachment is linked instead of being uploaded" , async t => {
const events = await messageToEvent ( {
content : "hey" ,
attachments : [ {
filename : "hey.jpg" ,
url : "https://discord.com/404/hey.jpg" ,
content _type : "application/i-made-it-up" ,
size : 100e6
} ]
} )
t . deepEqual ( events , [ {
$type : "m.room.message" ,
"m.mentions" : { } ,
msgtype : "m.text" ,
body : "hey"
} , {
$type : "m.room.message" ,
"m.mentions" : { } ,
msgtype : "m.text" ,
body : "📄 Uploaded file: https://discord.com/404/hey.jpg (100 MB)" ,
format : "org.matrix.custom.html" ,
formatted _body : '📄 Uploaded file: <a href="https://discord.com/404/hey.jpg">hey.jpg</a> (100 MB)'
} ] )
} )
2023-08-19 10:54:23 +00:00
test ( "message2event: type 4 channel name change" , async t => {
const events = await messageToEvent ( data . special _message . thread _name _change , data . guild . general )
t . deepEqual ( events , [ {
$type : "m.room.message" ,
"m.mentions" : { } ,
msgtype : "m.emote" ,
body : "changed the channel name to **worming**" ,
format : "org.matrix.custom.html" ,
formatted _body : "changed the channel name to <strong>worming</strong>"
} ] )
} )
2023-08-21 11:31:40 +00:00
test ( "message2event: thread start message reference" , async t => {
const events = await messageToEvent ( data . special _message . thread _start _context , data . guild . general , { } , {
api : {
getEvent : mockGetEvent ( t , "!PnyBKvUBOhjuCucEfk:cadence.moe" , "$FchUVylsOfmmbj-VwEs5Z9kY49_dt2zd0vWfylzy5Yo" , {
"type" : "m.room.message" ,
2023-09-07 12:13:25 +00:00
"sender" : "@_ooye_kyuugryphon:cadence.moe" ,
2023-08-21 11:31:40 +00:00
"content" : {
"m.mentions" : { } ,
"msgtype" : "m.text" ,
"body" : "layer 4"
}
} )
}
} )
t . deepEqual ( events , [ {
$type : "m.room.message" ,
msgtype : "m.text" ,
body : "layer 4" ,
"m.mentions" : { }
} ] )
} )