Skip to content

Commit

Permalink
allow receiving messages via channel inbox.
Browse files Browse the repository at this point in the history
  • Loading branch information
MaddyUnderStars committed Aug 14, 2023
1 parent 268cfbe commit d4f2859
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 24 deletions.
46 changes: 25 additions & 21 deletions src/util/entities/Member.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,28 +365,30 @@ export class Member extends BaseClassWithoutId {
bio: "",
};

const ret = Member.create({
...member,
roles: [Role.create({ id: guild_id })],
// read_state: {},
settings: {
guild_id: null,
mute_config: null,
mute_scheduled_events: false,
flags: 0,
hide_muted_channels: false,
notify_highlights: 0,
channel_overrides: {},
message_notifications: 0,
mobile_push: true,
muted: false,
suppress_everyone: false,
suppress_roles: false,
version: 0,
},
// Member.save is needed because else the roles relations wouldn't be updated
});

await Promise.all([
Member.create({
...member,
roles: [Role.create({ id: guild_id })],
// read_state: {},
settings: {
guild_id: null,
mute_config: null,
mute_scheduled_events: false,
flags: 0,
hide_muted_channels: false,
notify_highlights: 0,
channel_overrides: {},
message_notifications: 0,
mobile_push: true,
muted: false,
suppress_everyone: false,
suppress_roles: false,
version: 0,
},
// Member.save is needed because else the roles relations wouldn't be updated
}).save(),
ret.save(),
Guild.increment({ id: guild_id }, "member_count", 1),
emitEvent({
event: "GUILD_MEMBER_ADD",
Expand Down Expand Up @@ -443,6 +445,8 @@ export class Member extends BaseClassWithoutId {
} as MessageCreateEvent),
]);
}

return ret;
}

toPublicMember() {
Expand Down
18 changes: 16 additions & 2 deletions src/util/entities/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,19 +310,33 @@ export class Message extends BaseClass {
relations: { guild: true },
});

const user = await User.fromAP(attrib as APPerson);
let member;
if (
(await Member.count({
where: { id: user.id, guild_id: channel.guild_id },
})) == 0
)
member = await Member.addToGuild(user.id, channel.guild.id);

return Message.create({
id: Snowflake.generate(),
author: await User.fromAP(attrib as APPerson),
author: user,
member,
content: data.content, // convert html to markdown
timestamp: data.published,
channel_id,
channel,
guild: channel.guild,

sticker_items: [],
guild_id: channel.guild_id,
attachments: [],
embeds: [],
reactions: [],
type: 0,
mentions: [],
mention_roles: [],
mention_channels: [],
});
}
}
Expand Down
31 changes: 30 additions & 1 deletion src/util/entities/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,40 @@ export class User extends BaseClass {
}).then((x) => x.json())) as APPerson;
}

const cache = await User.findOne({
where: {
email: `${data.preferredUsername}@${
new URL(data.id!).hostname

Check warning on line 336 in src/util/entities/User.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Forbidden non-null assertion
}`,
},
});
if (cache) return cache;

return User.create({
id: Snowflake.generate(), // hm
username: data.preferredUsername,
discriminator: new URL(data.id!).hostname,

Check warning on line 345 in src/util/entities/User.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Forbidden non-null assertion
premium: false,
bio: data.summary, // TODO: convert to markdown
});

email: `${data.preferredUsername}@${new URL(data.id!).hostname}`,

Check warning on line 349 in src/util/entities/User.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Forbidden non-null assertion
data: {
hash: "#",
valid_tokens_since: new Date(),
},
extended_settings: "{}",
settings: UserSettings.create(),
publicKey: "",
privateKey: "",

premium_since: Config.get().defaults.user.premium
? new Date()
: undefined,
rights: Config.get().register.defaultRights,
premium_type: Config.get().defaults.user.premiumType ?? 0,
verified: Config.get().defaults.user.verified ?? true,
created_at: new Date(),
}).save();
}

static async getPublicUser(user_id: string, opts?: FindOneOptions<User>) {
Expand Down

0 comments on commit d4f2859

Please sign in to comment.