Skip to content

Commit

Permalink
Handle ChatMessage deprecations (foundryvtt#14553)
Browse files Browse the repository at this point in the history
  • Loading branch information
In3luki authored and CarlosFdez committed May 29, 2024
1 parent 49c6024 commit efd2601
Show file tree
Hide file tree
Showing 23 changed files with 69 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/module/actor/army/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ class ArmyPF2e<TParent extends TokenDocumentPF2e | null = TokenDocumentPF2e | nu
content: createHTMLElement("p", {
children: [game.i18n.localize("PF2E.Kingmaker.Army.Potions.UsedPotionContent")],
}).outerHTML,
type: CONST.CHAT_MESSAGE_TYPES.EMOTE,
style: CONST.CHAT_MESSAGE_STYLES.EMOTE,
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/module/actor/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1291,7 +1291,7 @@ class ActorPF2e<TParent extends TokenDocumentPF2e | null = TokenDocumentPF2e | n
},
flavor,
content,
type: CONST.CHAT_MESSAGE_TYPES.OTHER,
style: CONST.CHAT_MESSAGE_STYLES.OTHER,
whisper:
game.settings.get("pf2e", "metagame_secretDamage") && !token.actor?.hasPlayerOwner
? ChatMessagePF2e.getWhisperRecipients("GM").map((u) => u.id)
Expand Down
2 changes: 1 addition & 1 deletion src/module/actor/character/crafting/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export async function craftItem(
}

await ChatMessagePF2e.create({
user: game.user.id,
author: game.user.id,
content: game.i18n.format("PF2E.Actions.Craft.Information.ReceiveItem", {
actorName: actor.name,
quantity: itemQuantity,
Expand Down
2 changes: 1 addition & 1 deletion src/module/actor/character/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ class WeaponAuxiliaryAction {
content,
speaker: ChatMessagePF2e.getSpeaker({ actor, token }),
flavor,
type: CONST.CHAT_MESSAGE_TYPES.EMOTE,
style: CONST.CHAT_MESSAGE_STYLES.EMOTE,
});
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/module/actor/item-transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ export class ItemTransfer implements ItemTransferData {
const flavor = await this.#messageFlavor(sourceActor, targetActor, localize("BuySubtitle"));

await ChatMessage.create({
user: requester.id,
author: requester.id,
speaker: { alias: ItemTransfer.#tokenName(targetActor) },
type: CONST.CHAT_MESSAGE_TYPES.EMOTE,
style: CONST.CHAT_MESSAGE_STYLES.EMOTE,
flavor,
content,
});
Expand Down Expand Up @@ -309,9 +309,9 @@ export class ItemTransfer implements ItemTransferData {
const flavor = await this.#messageFlavor(sourceActor, targetActor, subtitle);

await ChatMessage.create({
user: requester.id,
author: requester.id,
speaker: { alias: speaker },
type: CONST.CHAT_MESSAGE_TYPES.EMOTE,
style: CONST.CHAT_MESSAGE_STYLES.EMOTE,
flavor,
content,
});
Expand Down
4 changes: 2 additions & 2 deletions src/module/actor/sheet/popups/distribute-coins-popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ export class DistributeCoinsPopup extends FormApplication<ActorPF2e, DistributeC
else message += ` and ${actor.name}.`;
}
ChatMessagePF2e.create({
user: game.user.id,
type: CONST.CHAT_MESSAGE_TYPES.OTHER,
author: game.user.id,
style: CONST.CHAT_MESSAGE_STYLES.OTHER,
content: message,
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/module/actor/sheet/popups/identify-popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class IdentifyItemPopup extends FormApplication<PhysicalItemPF2e> {
uuid: item.uuid,
});

await ChatMessagePF2e.create({ user: game.user.id, content });
await ChatMessagePF2e.create({ author: game.user.id, content });
});
}

Expand Down
1 change: 0 additions & 1 deletion src/module/apps/sidebar/chat-log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ class ChatLogPF2e extends ChatLog<ChatMessagePF2e> {
})();
rolls.push(await roll.evaluate());
}
chatData.type = CONST.CHAT_MESSAGE_TYPES.ROLL;
chatData.rolls = rolls.map((r) => r.toJSON());
chatData.sound = CONFIG.sounds.dice;
chatData.content = rolls.reduce((t, r) => t + r.total, 0).toString();
Expand Down
4 changes: 2 additions & 2 deletions src/module/canvas/status-effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,10 @@ export class StatusEffects {

const content = await renderTemplate("systems/pf2e/templates/chat/participant-conditions.hbs", { conditions });
const messageSource: Partial<foundry.documents.ChatMessageSource> = {
user: game.user.id,
author: game.user.id,
speaker: ChatMessagePF2e.getSpeaker({ token }),
content,
type: CONST.CHAT_MESSAGE_TYPES.OTHER,
style: CONST.CHAT_MESSAGE_STYLES.OTHER,
};
const isNPCEvent = !token.actor?.hasPlayerOwner;
const whisperMessage = whisper || (isNPCEvent && game.settings.get("pf2e", "metagame_secretCondition"));
Expand Down
6 changes: 3 additions & 3 deletions src/module/chat-message/listeners/cards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ class ChatCards {
if (isSpellConsumable(physicalItem.id) && physicalItem.isOfType("consumable")) {
craftSpellConsumable(physicalItem, quantity, actor);
ChatMessagePF2e.create({
user: game.user.id,
author: game.user.id,
content: game.i18n.format("PF2E.Actions.Craft.Information.PayAndReceive", {
actorName: actor.name,
cost: coinsToRemove.toString(),
Expand All @@ -310,7 +310,7 @@ class ChatCards {
}

ChatMessagePF2e.create({
user: game.user.id,
author: game.user.id,
content: game.i18n.format("PF2E.Actions.Craft.Information.LoseMaterials", {
actorName: actor.name,
cost: coinsToRemove.toString(),
Expand All @@ -327,7 +327,7 @@ class ChatCards {
ui.notifications.warn(game.i18n.localize("PF2E.Actions.Craft.Warning.InsufficientCoins"));
} else {
ChatMessagePF2e.create({
user: game.user.id,
author: game.user.id,
content: game.i18n.format("PF2E.Actions.Craft.Information.PayAndReceive", {
actorName: actor.name,
cost: coinsToRemove.toString(),
Expand Down
2 changes: 1 addition & 1 deletion src/module/item/base/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ class ItemPF2e<TParent extends ActorPF2e | null = ActorPF2e | null> extends Item
const rollMode = options.rollMode ?? eventToRollMode(originalEvent);
const chatData = ChatMessagePF2e.applyRollMode(
{
type: CONST.CHAT_MESSAGE_TYPES.OTHER,
style: CONST.CHAT_MESSAGE_STYLES.OTHER,
speaker: ChatMessagePF2e.getSpeaker({
actor: this.actor,
token: this.actor.getActiveTokens(false, true).at(0),
Expand Down
2 changes: 1 addition & 1 deletion src/module/rules/rule-element/fast-healing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class FastHealingRuleElement extends RuleElementPF2e<FastHealingRuleSchema> {
const roll = (await new DamageRoll(`{(${value})[healing]}`).evaluate()).toJSON();
const rollMode = this.actor.hasPlayerOwner ? "publicroll" : "gmroll";
const speaker = ChatMessagePF2e.getSpeaker({ actor: this.actor, token: this.token });
ChatMessagePF2e.create({ flavor, speaker, type: CONST.CHAT_MESSAGE_TYPES.ROLL, rolls: [roll] }, { rollMode });
ChatMessagePF2e.create({ flavor, speaker, rolls: [roll] }, { rollMode });
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/scripts/macros/check-prompt/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class CheckPromptDialog extends Application<CheckPromptDialogOptions> {
const dc = this.#getDC(html);
const content = types.map((type) => this.#constructCheck(type, dc, traits, extras)).join("");

ChatMessagePF2e.create({ user: game.user.id, flavor, content });
ChatMessagePF2e.create({ author: game.user.id, flavor, content });
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/scripts/macros/earn-income/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function chatTemplate(skillName: string, earnIncomeResult: EarnIncomeResult): st
function postToChat(skillName: string, earnIncomeResult: EarnIncomeResult): Promise<ChatMessagePF2e | undefined> {
const content = chatTemplate(skillName, earnIncomeResult);
return ChatMessagePF2e.create({
user: game.user.id,
author: game.user.id,
content,
speaker: ChatMessage.getSpeaker(),
});
Expand Down
1 change: 0 additions & 1 deletion src/scripts/macros/encouraging-words.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export function encouragingWords(options: ActionDefaultOptions): void {

ChatMessagePF2e.create({
speaker: ChatMessagePF2e.getSpeaker({ actor, token }),
type: CONST.CHAT_MESSAGE_TYPES.ROLL,
flavor: `<strong>${rollType} ${localize("Title")}</strong> (${successLabel})`,
rolls: [healRoll.toJSON()],
});
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/macros/hotbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export async function rollActionMacro({
const chatData: PreCreate<foundry.documents.ChatMessageSource> = {
speaker: ChatMessagePF2e.getSpeaker({ actor, token }),
content,
type: CONST.CHAT_MESSAGE_TYPES.OTHER,
style: CONST.CHAT_MESSAGE_STYLES.OTHER,
};

const rollMode = game.settings.get("core", "rollMode");
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/macros/raise-a-shield.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export async function raiseAShield(options: ActionDefaultOptions): Promise<void>
});

await ChatMessagePF2e.create({
type: CONST.CHAT_MESSAGE_TYPES.EMOTE,
style: CONST.CHAT_MESSAGE_STYLES.EMOTE,
speaker,
flavor,
content,
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/macros/rest-for-the-night.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export async function restForTheNight(options: RestForTheNightOptions): Promise<

// Call a hook for modules to do anything extra
Hooks.callAll("pf2e.restForTheNight", actor);
messages.push({ user: game.user.id, content, speaker: ChatMessagePF2e.getSpeaker({ actor }) });
messages.push({ author: game.user.id, content, speaker: ChatMessagePF2e.getSpeaker({ actor }) });
}

return ChatMessagePF2e.createDocuments(messages, { restForTheNight: true });
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/macros/steel-your-resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function steelYourResolve(options: ActionDefaultOptions): void {

const toChat = (alias: string, content: string) => {
ChatMessage.create({
user: game.user.id,
author: game.user.id,
content,
speaker: { alias },
});
Expand Down
2 changes: 0 additions & 2 deletions src/scripts/macros/treat-wounds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ async function treat(
if (riskySurgery) {
ChatMessagePF2e.create({
flags: message.toObject().flags,
type: CONST.CHAT_MESSAGE_TYPES.ROLL,
flavor: `<strong>${game.i18n.localize("PF2E.Actions.TreatWounds.Rolls.RiskySurgery")}</strong>`,
rolls: [(await new DamageRoll("{1d8[slashing]}").roll()).toJSON()],
speaker,
Expand All @@ -179,7 +178,6 @@ async function treat(
: game.i18n.localize("PF2E.Actions.TreatWounds.Rolls.TreatWoundsCriticalFailure");
ChatMessagePF2e.create({
flags: message.toObject().flags,
type: CONST.CHAT_MESSAGE_TYPES.ROLL,
flavor: `<strong>${rollType}</strong> (${successLabel})`,
rolls: [healRoll.toJSON()],
speaker,
Expand Down
18 changes: 13 additions & 5 deletions types/foundry/common/constants.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,22 @@ export const CANVAS_PERFORMANCE_MODES: {
MAX: 3;
};

/** Valid Chat Message types */
export const CHAT_MESSAGE_TYPES: {
/** Valid Chat Message styles which affect how the message is presented in the chat log. */
export const CHAT_MESSAGE_STYLES: {
/** An uncategorized chat message */
OTHER: 0;
/**
* The message is spoken out of character (OOC).
* OOC messages will be outlined by the player's color to make them more easily recognizable.
*/
OOC: 1;
/** The message is spoken by an associated character. */
IC: 2;
/**
* The message is an emote performed by the selected character.
* Entering "/emote waves his hand." while controlling a character named Simon will send the message, "Simon waves his hand."
*/
EMOTE: 3;
WHISPER: 4;
ROLL: 5;
};

/** Define the set of languages which have built-in support in the core software */
Expand Down Expand Up @@ -767,7 +775,7 @@ declare global {
type ActiveEffectChangeMode = (typeof CONST.ACTIVE_EFFECT_MODES)[keyof typeof CONST.ACTIVE_EFFECT_MODES];
type AudioFileExtension = keyof typeof AUDIO_FILE_EXTENSIONS;
type CanvasPerformanceMode = (typeof CANVAS_PERFORMANCE_MODES)[keyof typeof CANVAS_PERFORMANCE_MODES];
type ChatMessageType = (typeof CONST.CHAT_MESSAGE_TYPES)[keyof typeof CONST.CHAT_MESSAGE_TYPES];
type ChatMessageStyle = (typeof CONST.CHAT_MESSAGE_STYLES)[keyof typeof CONST.CHAT_MESSAGE_STYLES];
type CompatibilityMode = (typeof CONST.COMPATIBILITY_MODES)[keyof typeof CONST.COMPATIBILITY_MODES];
type DirectorySearchMode = (typeof DIRECTORY_SEARCH_MODES)[keyof typeof DIRECTORY_SEARCH_MODES];
type DocumentOwnershipLevel = (typeof DOCUMENT_OWNERSHIP_LEVELS)[DocumentOwnershipString];
Expand Down
23 changes: 23 additions & 0 deletions types/foundry/common/data/fields.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,29 @@ type DocumentStatsSchema = {
lastModifiedBy: ForeignDocumentField<string>;
};

/**
* A subclass of [StringField]{@link StringField} that is used specifically for the Document "type" field.
*/
export class DocumentTypeField<
TSourceProp extends string = string,
TModelProp extends string = TSourceProp,
TRequired extends boolean = true,
TNullable extends boolean = false,
THasInitial extends boolean = true,
TDocument extends abstract.Document = abstract.Document,
> extends StringField<TSourceProp, TModelProp, TRequired, TNullable, THasInitial> {
/**
* @param documentClass The base document class which belongs in this field
* @param [options] Options which configure the behavior of the field
* @param [context] Additional context which describes the field
*/
constructor(
documentClass: ConstructorOf<TDocument>,
options?: StringFieldOptions<TSourceProp, TRequired, TNullable, THasInitial>,
context?: { name?: string; parent?: abstract.Document },
);
}

/** A subclass of `ObjectField` which supports a system-level data object. */
export class TypeDataField<
TSourceProp extends object = object,
Expand Down
12 changes: 9 additions & 3 deletions types/foundry/common/documents/chat-message.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ export default interface BaseChatMessage
export type ChatMessageSchema = {
/** The _id which uniquely identifies this ChatMessage document */
_id: fields.DocumentIdField;
/** The message type from CONST.CHAT_MESSAGE_TYPES */
type: fields.NumberField<ChatMessageType, ChatMessageType, true, true, true>;
/** An ChatMessage subtype which configures the system data model applied */
type: fields.DocumentTypeField<string, string, true, false, true, BaseChatMessage>;
/** The system data object */
system: fields.TypeDataField;
/** The message style from CONST.CHAT_MESSAGE_STYLES */
style: fields.NumberField<ChatMessageStyle, ChatMessageStyle, true, true, true>;
/** The _id of the User document who generated this message */
user: fields.ForeignDocumentField<BaseUser, true, false, true>;
author: fields.ForeignDocumentField<BaseUser, true, false, true>;
/** The timestamp at which point this message was generated */
timestamp: fields.NumberField<number, number, true, false, true>;
/** An optional flavor text message which summarizes this message */
Expand All @@ -50,6 +54,8 @@ export type ChatMessageSchema = {
emote: fields.BooleanField;
/** An object of optional key/value flags */
flags: fields.ObjectField<ChatMessageFlags>;
/** An object of creation and access information. */
_stats: fields.DocumentStatsField;
};

export type ChatMessageSource = SourceFromSchema<ChatMessageSchema>;
Expand Down

0 comments on commit efd2601

Please sign in to comment.