Handle d2m events with empty guild/user avatars #106
10 changed files with 41 additions and 35 deletions
fixup! Handle d2m events with empty guild/user avatars
commit
6118c7fb74
|
|
@ -181,7 +181,7 @@ async function channelToKState(channel, guild, di) {
|
|||
network: {
|
||||
id: guild.id,
|
||||
displayname: guild.name,
|
||||
...(guild.icon ? {avatar_url: {$url: file.guildIcon(guild)}} : {})
|
||||
avatar_url: {$url: file.guildIcon(guild)}
|
||||
},
|
||||
channel: {
|
||||
id: channel.id,
|
||||
|
|
|
|||
|
|
@ -77,7 +77,7 @@ async function guildToKState(guild, privacyLevel) {
|
|||
"m.room.name/": {name: guild.name},
|
||||
"m.room.avatar/": {
|
||||
$if: guild.icon,
|
||||
url: guild.icon ? {$url: file.guildIcon(guild)} : null
|
||||
url: {$url: file.guildIcon(guild)}
|
||||
},
|
||||
"m.room.guest_access/": {guest_access: createRoom.PRIVACY_ENUMS.GUEST_ACCESS[privacyLevel]},
|
||||
"m.room.history_visibility/": {history_visibility: createRoom.PRIVACY_ENUMS.SPACE_HISTORY_VISIBILITY[privacyLevel]},
|
||||
|
|
|
|||
|
|
@ -110,10 +110,7 @@ async function ensureSimJoined(pkMessage, roomID) {
|
|||
async function memberToStateContent(pkMessage, author) {
|
||||
// We prefer to use the member's avatar URL data since the image upload can be cached across channels,
|
||||
// unlike the userAvatar URL which is unique per channel, due to the webhook ID being in the URL.
|
||||
const avatar = pkMessage.member.avatar_url
|
||||
|| pkMessage.member.webhook_avatar_url
|
||||
|| pkMessage.system.avatar_url
|
||||
|| (author.avatar ? file.userAvatar(author) : null)
|
||||
const avatar = pkMessage.member.avatar_url || pkMessage.member.webhook_avatar_url || pkMessage.system.avatar_url || file.userAvatar(author)
|
||||
const color = pkMessage.member.color ?? pkMessage.system.color
|
||||
|
||||
const content = {
|
||||
|
|
|
|||
|
|
@ -115,7 +115,7 @@ async function ensureSimJoined(pluRalMessage, roomID) {
|
|||
async function memberToStateContent(pluRalMessage, author) {
|
||||
// We prefer to use the member's avatar URL data since the image upload can be cached across channels,
|
||||
// unlike the userAvatar URL which is unique per channel, due to the webhook ID being in the URL.
|
||||
const avatar = pluRalMessage.member.avatar_url || (author.avatar ? file.userAvatar(author) : null)
|
||||
const avatar = pluRalMessage.member.avatar_url || file.userAvatar(author)
|
||||
|
||||
const content = {
|
||||
displayname: author.username,
|
||||
|
|
|
|||
|
|
@ -102,8 +102,8 @@ async function userToGlobalProfile(user) {
|
|||
globalProfile.displayname = user.username
|
||||
if (user.global_name) globalProfile.displayname = user.global_name
|
||||
|
||||
if (user.avatar) {
|
||||
const avatarPath = file.userAvatar(user) // the user avatar only
|
||||
const avatarPath = file.userAvatar(user) // the user avatar only
|
||||
if (avatarPath) {
|
||||
globalProfile.avatar_url = await file.uploadDiscordFileToMxc(avatarPath)
|
||||
}
|
||||
|
||||
|
|
@ -132,9 +132,9 @@ async function memberToStateContent(user, member, guildID) {
|
|||
}
|
||||
}
|
||||
|
||||
if (member?.avatar || user.avatar) {
|
||||
// const avatarPath = file.userAvatar(user) // the user avatar only
|
||||
const avatarPath = file.memberAvatar(guildID, user, member) // the member avatar or the user avatar
|
||||
// const avatarPath = file.userAvatar(user) // the user avatar only
|
||||
const avatarPath = file.memberAvatar(guildID, user, member) // the member avatar or the user avatar
|
||||
if (avatarPath) {
|
||||
content["moe.cadence.ooye.member"].avatar = avatarPath
|
||||
content.avatar_url = await file.uploadDiscordFileToMxc(avatarPath)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -89,7 +89,7 @@ async function ensureSimJoined(fakeUserID, author, roomID) {
|
|||
* @param {Ty.WebhookAuthor} author
|
||||
*/
|
||||
async function authorToStateContent(author) {
|
||||
const avatar = author.avatar ? file.userAvatar(author) : null
|
||||
const avatar = file.userAvatar(author)
|
||||
|
||||
const content = {
|
||||
displayname: author.username,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
// @ts-check
|
||||
|
||||
const assert = require("assert").strict
|
||||
const DiscordTypes = require("discord-api-types/v10")
|
||||
const passthrough = require("../passthrough")
|
||||
const {reg, writeRegistration} = require("./read-registration.js")
|
||||
|
|
@ -142,19 +141,19 @@ async function _actuallyUploadDiscordFileToMxc(url) {
|
|||
|
||||
/**
|
||||
* @param {DiscordTypes.APIGuild} guild
|
||||
* @returns {string}
|
||||
* @returns {string | null}
|
||||
*/
|
||||
function guildIcon(guild) {
|
||||
assert(guild.icon, `guild ${guild.id} has no icon`)
|
||||
if (!guild.icon) return null
|
||||
return `/icons/${guild.id}/${guild.icon}.png?size=${IMAGE_SIZE}`
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {DiscordTypes.APIUser | Ty.WebhookAuthor} user
|
||||
* @returns {string}
|
||||
* @returns {string | null}
|
||||
*/
|
||||
function userAvatar(user) {
|
||||
assert(user.avatar, `user ${user.id} has no avatar`)
|
||||
if (!user.avatar) return null
|
||||
return `/avatars/${user.id}/${user.avatar}.png?size=${IMAGE_SIZE}`
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -35,15 +35,12 @@ test("guildIcon: returns icon path", t => {
|
|||
)
|
||||
})
|
||||
|
||||
test("guildIcon: throws if guild has no icon", t => {
|
||||
let err
|
||||
try {
|
||||
test("guildIcon: returns null if guild has no icon", t => {
|
||||
t.equal(
|
||||
// @ts-ignore
|
||||
file.guildIcon({id: "112760669178241024", icon: null})
|
||||
} catch (e) {
|
||||
err = e
|
||||
}
|
||||
t.match(err?.message, /has no icon/)
|
||||
file.guildIcon({id: "112760669178241024", icon: null}),
|
||||
null
|
||||
)
|
||||
})
|
||||
|
||||
test("userAvatar: returns avatar path", t => {
|
||||
|
|
@ -53,12 +50,9 @@ test("userAvatar: returns avatar path", t => {
|
|||
)
|
||||
})
|
||||
|
||||
test("userAvatar: throws if user has no avatar", t => {
|
||||
let err
|
||||
try {
|
||||
file.userAvatar({id: "113340068197859328", username: "kumaccino", avatar: null})
|
||||
} catch (e) {
|
||||
err = e
|
||||
}
|
||||
t.match(err?.message, /has no avatar/)
|
||||
test("userAvatar: returns null if user has no avatar", t => {
|
||||
t.equal(
|
||||
file.userAvatar({id: "113340068197859328", username: "kumaccino", avatar: null}),
|
||||
null
|
||||
)
|
||||
})
|
||||
|
|
|
|||
|
|
@ -32,7 +32,11 @@ async function kstateUploadMxc(obj) {
|
|||
for (const [k, v] of Object.entries(obj)) {
|
||||
if (v == null || typeof v !== "object") continue
|
||||
|
||||
if (v.$url) {
|
||||
if ("$url" in v) {
|
||||
if (!v.$url) {
|
||||
delete obj[k]
|
||||
continue
|
||||
}
|
||||
promises.push(
|
||||
file.uploadDiscordFileToMxc(v.$url)
|
||||
.then(mxc => obj[k] = mxc)
|
||||
|
|
|
|||
|
|
@ -21,6 +21,18 @@ test("kstate strip: keeps true conditions while removing $if", t => {
|
|||
})
|
||||
})
|
||||
|
||||
test("kstateUploadMxc: removes keys where $url is null", async t => {
|
||||
const input = {
|
||||
"m.room.avatar/": {
|
||||
url: {$url: null}
|
||||
}
|
||||
}
|
||||
await kstateUploadMxc(input)
|
||||
t.deepEqual(input, {
|
||||
"m.room.avatar/": {}
|
||||
})
|
||||
})
|
||||
|
||||
test("kstateUploadMxc: sets the mxc", async t => {
|
||||
const input = {
|
||||
"m.room.avatar/": {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue