Skip to content

Commit

Permalink
Merge pull request #384 from mvdicarlo/develop
Browse files Browse the repository at this point in the history
v3.1.51
  • Loading branch information
mvdicarlo authored Nov 11, 2024
2 parents 837c0d8 + eb47d44 commit d104112
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 206 deletions.
2 changes: 1 addition & 1 deletion electron-app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postybirb-plus",
"version": "3.1.50",
"version": "3.1.51",
"description": "(ClientServer) PostyBirb is an application that helps artists post art and other multimedia to multiple websites more quickly.",
"main": "dist/main.js",
"author": "Michael DiCarlo",
Expand Down
16 changes: 9 additions & 7 deletions electron-app/src/server/websites/bluesky/bluesky.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import FormData from 'form-data';
// HACK: The atproto library contains some kind of invalid typescript
// declaration in @atproto/api, so we can't include directly from it. Rummaging
// around in the dist files directly works though.
// This hack is also present in BlueskyLogin.tsx.
import { AtUri } from '@atproto/syntax';
import { BlobRef } from '@atproto/lexicon';
import { BskyAgent } from '@atproto/api/dist/bsky-agent';
Expand Down Expand Up @@ -71,7 +70,6 @@ export class Bluesky extends Website {
// by letting you specify a fetch handler, but then uses Headers and
// FormData unconditionally anyway, with no way to change that behavior.
// Patching them into the global namespace is ugly, but it works.
// This hack is also present in BlueskyLogin.tsx.
globalThis.FormData = FormData as any;
globalThis.Headers = fetch.Headers;
globalThis.Request = fetch.Request;
Expand All @@ -80,18 +78,22 @@ export class Bluesky extends Website {
}

async checkLoginStatus(data: UserAccountEntity): Promise<LoginResponse> {
const status: LoginResponse = { loggedIn: false, username: null };
const agent = this.makeAgent();
const username = data?.data?.username;
const password = data?.data?.password;
if (!username || !password) {
return { loggedIn: false, username };
}

const status: LoginResponse = { loggedIn: false, username };
const agent = this.makeAgent();
await agent
.login({
identifier: data.data.username,
password: data.data.password,
identifier: username,
password,
})
.then(res => {
if (res.success) {
status.loggedIn = true;
status.username = data.data.username;
} else {
status.loggedIn = false;
}
Expand Down
129 changes: 72 additions & 57 deletions electron-app/src/server/websites/telegram/telegram.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,25 +246,47 @@ export class Telegram extends Website {
}

private async loadChannels(profileId: string, appId: string) {
const { chats } = await this.callApi<Dialogs>(appId, 'messages.getDialogs', {
offset_peer: { _: 'inputPeerEmpty' },
limit: 400,
});
// Used code from https://github.com/alik0211/mtproto-core/issues/279#issuecomment-1540604519

const channels: Folder[] = [];
for (const chat of chats) {
if (!this.canSendMediaInChat(chat)) continue;

const id = chat.id.toString();
const value = chat.access_hash ? `${id}-${chat.access_hash}` : id;
if (!channels.some(e => e.value !== value)) {
channels.push({
label: chat.title,
value: value,
});
const offsetId = 0;
const offsetPeer = {
_: 'inputPeerEmpty',
};

let requestLimit = 30;
let offsetDate = 0;

while (requestLimit >= 0) {
requestLimit--;

const { chats, messages } = await this.callApi<Dialogs>(appId, 'messages.getDialogs', {
offset_id: offsetId,
offset_peer: offsetPeer,
offset_date: offsetDate,
limit: 100,
});

for (const chat of chats) {
if (!this.canSendMediaInChat(chat)) continue;

const id = chat.id.toString();
const value = chat.access_hash ? `${id}-${chat.access_hash}` : id;
if (!channels.find(e => e.value === value)) {
channels.push({
label: chat.title,
value: value,
});
}
}

if (messages.length > 0) {
offsetDate = messages[messages.length - 1].date;
} else break;
}

this.logger.debug(`Loaded ${channels.length} channels and chats.`);

this.storeAccountInformation(profileId, GenericAccountProp.FOLDERS, channels);
}

Expand All @@ -275,7 +297,7 @@ export class Telegram extends Website {
if (
chat.creator ||
chat.admin_rights?.post_messages ||
// False means that user can send media
// Right is not banned -> user can send media
chat.default_banned_rights?.send_media === false
)
return true;
Expand All @@ -292,7 +314,7 @@ export class Telegram extends Website {
}

getScalingOptions(file: FileRecord): ScalingOptions {
return { maxSize: FileSize.MBtoBytes(this.MAX_MB) };
return { maxSize: FileSize.MBtoBytes(this.MAX_MB), maxWidth: 2560, maxHeight: 2560 };
}

private async upload(appId: string, file: PostFileRecord, spoiler: boolean) {
Expand Down Expand Up @@ -508,28 +530,8 @@ export class Telegram extends Website {
const warnings: string[] = [];
const isAutoscaling: boolean = submissionPart.data.autoScale;

if (submissionPart.data.channels?.length) {
const folders: Folder[] = _.get(
this.accountInformation.get(submissionPart.accountId),
GenericAccountProp.FOLDERS,
[],
);
submissionPart.data.channels.forEach(f => {
if (!WebsiteValidator.folderIdExists(f, folders)) {
problems.push(this.channelNotFound(f));
}
});
} else {
problems.push('No channel(s) selected.');
}

const { description } = TelegramDescription.fromHTML(
FormContent.getDescription(defaultPart.data.description, submissionPart.data.description),
);

if (description.length > 4096) {
warnings.push('Max description length allowed is 4,096 characters.');
}
this.validateChannels(submissionPart, warnings, problems);
this.validateDescriptionLength(defaultPart, submissionPart, warnings);

const files = [
submission.primary,
Expand Down Expand Up @@ -570,34 +572,45 @@ export class Telegram extends Website {
const problems: string[] = [];
const warnings: string[] = [];

if (submissionPart.data.channels?.length) {
const folders: Folder[] = _.get(
this.accountInformation.get(submissionPart.accountId),
GenericAccountProp.FOLDERS,
[],
);
submissionPart.data.channels.forEach(f => {
if (!WebsiteValidator.folderIdExists(f, folders)) {
problems.push(this.channelNotFound(f));
}
});
} else {
problems.push('No channel(s) selected.');
}
this.validateChannels(submissionPart, warnings, problems);
this.validateDescriptionLength(defaultPart, submissionPart, warnings);

return { problems, warnings };
}

private validateDescriptionLength(
defaultPart: SubmissionPart<DefaultOptions>,
submissionPart: SubmissionPart<TelegramNotificationOptions>,
warnings: string[],
) {
const { description } = TelegramDescription.fromHTML(
FormContent.getDescription(defaultPart.data.description, submissionPart.data.description),
);

if (description.length > 4096) {
warnings.push('Max description length allowed is 4,096 characters.');
}

return { problems, warnings };
}

private channelNotFound(f: string) {
return `Channel (${f}) not found. To fix this, simply post something in the channel. PostyBirb requests latest 400 chats and then filters them to include only those where you can send media. If you have a lot of active chats, PostyBirb will be not able to view inactive channels.`;
private validateChannels(
submissionPart: SubmissionPart<TelegramNotificationOptions>,
warnings: string[],
problems: string[],
) {
if (!submissionPart.data.channels?.length) {
problems.push('No channel(s) selected.');
} else {
const folders: Folder[] = _.get(
this.accountInformation.get(submissionPart.accountId),
GenericAccountProp.FOLDERS,
[],
);
submissionPart.data.channels.forEach(f => {
if (!WebsiteValidator.folderIdExists(f, folders)) {
warnings.push(`Channel (${f}) not found.`);
}
});
}
}
}

Expand Down Expand Up @@ -645,11 +658,13 @@ interface Chat {
*/
send_media: boolean;
};
admin_rights: {
admin_rights?: {
post_messages: boolean;
};
}

interface Dialogs {
chats: Chat[];
dialogs: object[];
messages: { date: number }[];
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postybirb-plus",
"version": "3.1.50",
"version": "3.1.51",
"description": "PostyBirb is an application that helps artists post art and other multimedia to multiple websites more quickly..",
"main": "index.js",
"scripts": {
Expand Down
Loading

0 comments on commit d104112

Please sign in to comment.