MCPcopy Index your code
hub / github.com/AnswerOverflow/AnswerOverflow / applyPublicFlagsToMessages

Function applyPublicFlagsToMessages

packages/core/src/message.ts:56–149  ·  view source on GitHub ↗
(messages: T[])

Source from the content-addressed store, hash-verified

54};
55
56export function applyPublicFlagsToMessages<
57 T extends MessageWithAuthorServerSettings & {
58 solutions: MessageWithAuthorServerSettings[];
59 reference: MessageWithAuthorServerSettings | null;
60 server: Pick<Server, 'bitfield'>;
61 },
62>(messages: T[]) {
63 if (messages.length === 0) {
64 return [];
65 }
66
67 const getLookupKey = (m: { userId: string; serverId: string }) =>
68 `${m.userId}-${m.serverId}`;
69
70 // TODO: Ew 🤮
71 const authorServerSettingsLookup = new Map(
72 messages
73 .filter((msg) => msg.author && msg.author.userServerSettings)
74 .flatMap((msg) => [
75 ...msg.author.userServerSettings,
76 ...msg.solutions.flatMap((s) => s.author.userServerSettings),
77 ...(msg?.reference?.author.userServerSettings ?? []),
78 ])
79 .map((uss) => [getLookupKey(uss), addFlagsToUserServerSettings(uss)]),
80 );
81 const seedLookup = new Map(messages.map((a) => [a.authorId, getRandomId()]));
82
83 const makeMessageWithAuthor = (
84 msg: MessageWithAuthorServerSettings,
85 serverWithFlags: Pick<ServerWithFlags, 'flags'>,
86 ) => {
87 if (!msg.author || !msg.author.userServerSettings) {
88 return null;
89 }
90 const author = msg.author;
91 const seed = seedLookup.get(author.id) ?? getRandomId();
92 const authorServerSettings = authorServerSettingsLookup.get(
93 getLookupKey({
94 serverId: msg.serverId,
95 userId: author.id,
96 }),
97 );
98
99 const areAllServerMessagesPublic =
100 serverWithFlags.flags.considerAllMessagesPublic;
101 const hasUserGrantedConsent =
102 authorServerSettings?.flags.canPubliclyDisplayMessages ?? false;
103 const isMessagePublic = areAllServerMessagesPublic || hasUserGrantedConsent;
104 const publicAccount = zDiscordAccountPublic.parse(author);
105 const isAnonymous =
106 serverWithFlags.flags.anonymizeMessages &&
107 !authorServerSettings?.flags.canPubliclyDisplayMessages;
108 return {
109 ...pick(
110 msg,
111 'content',
112 'id',
113 'channelId',

Callers 4

findMessageResultPageFunction · 0.90

Calls 5

getRandomIdFunction · 0.90
addFlagsToServerFunction · 0.90
getLookupKeyFunction · 0.85
makeMessageWithAuthorFunction · 0.85

Tested by

no test coverage detected