bb-fca 2.0.10 → 2.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,4 @@
1
+ import * as fs from 'fs';
1
2
  import utils = require('../../../utils');
2
3
 
3
4
  /**
@@ -632,6 +633,485 @@ export default function (defaultFuncs: any, api: any, ctx: any) {
632
633
  hasNextPage: Boolean(pageInfo.has_next_page),
633
634
  };
634
635
  },
636
+
637
+ /**
638
+ * Resolves a Facebook share URL to the actual group ID.
639
+ *
640
+ * Accepts various formats:
641
+ * - Full URL: `https://www.facebook.com/share/g/14bKqsywAfu/`
642
+ * - Path only: `/share/g/14bKqsywAfu/`
643
+ * - Short key: `14bKqsywAfu`
644
+ *
645
+ * @param {string} shareUrl The share URL, path, or short key to resolve.
646
+ * @returns {Promise<{ groupID: string; name: string | null; url: string | null }>}
647
+ * The resolved group ID, name (if available), and canonical URL.
648
+ * @throws {Error} If the shareUrl is missing or the group ID cannot be extracted.
649
+ *
650
+ * @example
651
+ * const result = await api.group.resolveShareUrl('https://www.facebook.com/share/g/14bKqsywAfu/');
652
+ * console.log(result.groupID); // "1482314963016056"
653
+ */
654
+ resolveShareUrl: async function (
655
+ shareUrl: string,
656
+ ): Promise<{ groupID: string; name: string | null; url: string | null }> {
657
+ if (!shareUrl || typeof shareUrl !== 'string') {
658
+ throw new Error(
659
+ 'resolveShareUrl: shareUrl must be a non-empty string.',
660
+ );
661
+ }
662
+
663
+ // Normalize input to a full URL
664
+ let fullUrl: string;
665
+ if (shareUrl.startsWith('http://') || shareUrl.startsWith('https://')) {
666
+ fullUrl = shareUrl;
667
+ } else if (shareUrl.startsWith('/')) {
668
+ fullUrl = 'https://www.facebook.com' + shareUrl;
669
+ } else {
670
+ // Assume it's just the short key
671
+ fullUrl = `https://www.facebook.com/share/g/${shareUrl}/`;
672
+ }
673
+
674
+ // Fetch the share page - Facebook will embed the group data in the HTML
675
+ const allJsonData = await utils.json(
676
+ fullUrl,
677
+ ctx.jar,
678
+ null,
679
+ ctx.globalOptions,
680
+ ctx,
681
+ );
682
+
683
+ if (!allJsonData || allJsonData.length === 0) {
684
+ throw new Error(
685
+ 'resolveShareUrl: Could not fetch data from the share URL.',
686
+ );
687
+ }
688
+
689
+ // Strategy 1: Look for group object with __typename "Group" in the JSON data
690
+ let groupID: string | null = null;
691
+ let groupName: string | null = null;
692
+ let groupUrl: string | null = null;
693
+
694
+ const groupObj = deepFind(
695
+ allJsonData,
696
+ (val, _key) =>
697
+ val &&
698
+ typeof val === 'object' &&
699
+ val.__typename === 'Group' &&
700
+ typeof val.id === 'string',
701
+ );
702
+
703
+ if (groupObj) {
704
+ groupID = groupObj.id;
705
+ groupName = groupObj.name || null;
706
+ groupUrl = groupObj.url || null;
707
+ }
708
+
709
+ // Strategy 2: Look for groupID in various data patterns
710
+ if (!groupID) {
711
+ const groupIdVal = deepFind(
712
+ allJsonData,
713
+ (val, key) =>
714
+ (key === 'groupID' || key === 'group_id') &&
715
+ typeof val === 'string' &&
716
+ /^\d+$/.test(val),
717
+ );
718
+ if (groupIdVal) {
719
+ groupID = groupIdVal;
720
+ }
721
+ }
722
+
723
+ // Strategy 3: Look for meta property og:url or al:android:url containing group ID
724
+ if (!groupID) {
725
+ // Try to find the raw HTML response from the GET request
726
+ const res = await utils.get(
727
+ fullUrl,
728
+ ctx.jar,
729
+ null,
730
+ ctx.globalOptions,
731
+ ctx,
732
+ );
733
+ const html = typeof res.body === 'string' ? res.body : String(res.body);
734
+
735
+ // Try og:url meta tag: <meta property="og:url" content="https://www.facebook.com/groups/XXXXXXX/" />
736
+ const ogUrlMatch = html.match(
737
+ /property="og:url"\s+content="[^"]*\/groups\/(\d+)/,
738
+ );
739
+ if (ogUrlMatch) {
740
+ groupID = ogUrlMatch[1];
741
+ }
742
+
743
+ // Try al:android:url: fb://group/XXXXXXX
744
+ if (!groupID) {
745
+ const androidUrlMatch = html.match(
746
+ /property="al:android:url"\s+content="fb:\/\/group\/(\d+)"/,
747
+ );
748
+ if (androidUrlMatch) {
749
+ groupID = androidUrlMatch[1];
750
+ }
751
+ }
752
+
753
+ // Try entity_id pattern in the HTML
754
+ if (!groupID) {
755
+ const entityIdMatch = html.match(/"entity_id":"(\d+)"/);
756
+ if (entityIdMatch) {
757
+ groupID = entityIdMatch[1];
758
+ }
759
+ }
760
+
761
+ // Try groupID in the URL of the final redirect
762
+ if (!groupID) {
763
+ const groupUrlMatch = html.match(
764
+ /facebook\.com\/groups\/(\d+)/,
765
+ );
766
+ if (groupUrlMatch) {
767
+ groupID = groupUrlMatch[1];
768
+ }
769
+ }
770
+
771
+ // Extract group name from og:title if available
772
+ if (!groupName) {
773
+ const ogTitleMatch = html.match(
774
+ /property="og:title"\s+content="([^"]*)"/,
775
+ );
776
+ if (ogTitleMatch) {
777
+ groupName = ogTitleMatch[1];
778
+ }
779
+ }
780
+ }
781
+
782
+ if (!groupID) {
783
+ throw new Error(
784
+ 'resolveShareUrl: Could not extract group ID from the share URL. ' +
785
+ 'The link may be invalid, expired, or require authentication.',
786
+ );
787
+ }
788
+
789
+ if (!groupUrl) {
790
+ groupUrl = `https://www.facebook.com/groups/${groupID}/`;
791
+ }
792
+
793
+ return {
794
+ groupID,
795
+ name: groupName,
796
+ url: groupUrl,
797
+ };
798
+ },
799
+
800
+ /**
801
+ * Uploads a photo to Facebook for use in group posts.
802
+ *
803
+ * The photo is uploaded to `upload.facebook.com` with the group discussion
804
+ * route context (`CometGroupDiscussionRoute`), making it ready to be
805
+ * attached to a group post via {@link createGroupPost}.
806
+ *
807
+ * @param {string | string[]} photoPaths - A single file path or an array of file paths to upload.
808
+ * @returns {Promise<{ photoID: string; uploadID: string; data: any }[]>}
809
+ * Array of upload results, one per photo, each containing:
810
+ * - `photoID` – The Facebook-assigned photo ID (use in `createGroupPost`).
811
+ * - `uploadID` – The client-generated upload ID.
812
+ * - `data` – The raw server response payload.
813
+ * @throws {Error} If any path is missing, not a string, or doesn't exist on disk.
814
+ *
815
+ * @example
816
+ * const [photo] = await api.group.uploadPhoto('/path/to/image.jpg');
817
+ * const post = await api.group.createGroupPost('123456789', {
818
+ * message: 'Check this out!',
819
+ * photos: [photo.photoID],
820
+ * });
821
+ */
822
+ uploadPhoto: async function (
823
+ photoPaths: string | string[],
824
+ ): Promise<{ photoID: string; uploadID: string; data: any }[]> {
825
+ const paths = Array.isArray(photoPaths) ? photoPaths : [photoPaths];
826
+
827
+ if (paths.length === 0) {
828
+ throw new Error('uploadPhoto: at least one photo path is required.');
829
+ }
830
+
831
+ const results: { photoID: string; uploadID: string; data: any }[] = [];
832
+
833
+ for (const photoPath of paths) {
834
+ if (!photoPath || typeof photoPath !== 'string') {
835
+ throw new Error('uploadPhoto: each photo path must be a non-empty string.');
836
+ }
837
+ if (!fs.existsSync(photoPath)) {
838
+ throw new Error(`uploadPhoto: file not found: ${photoPath}`);
839
+ }
840
+
841
+ const photoStream = fs.createReadStream(photoPath);
842
+
843
+ const uploadId = `jsc_c_${Math.random().toString(36).substring(2, 11)}`;
844
+
845
+ // Build URL with query parameters matching the captured request
846
+ const url = new URL(
847
+ 'https://upload.facebook.com/ajax/react_composer/attachments/photo/upload',
848
+ );
849
+ url.searchParams.append('av', ctx.userID);
850
+ url.searchParams.append('__aaid', '0');
851
+ url.searchParams.append('__user', ctx.userID);
852
+ url.searchParams.append('__a', '1');
853
+ url.searchParams.append('__req', '1');
854
+ url.searchParams.append('__hs', '20558.HCSV2:comet_pkg.2.1...0');
855
+ url.searchParams.append('dpr', '1');
856
+ url.searchParams.append('__ccg', 'EXCELLENT');
857
+ url.searchParams.append('__comet_req', '15');
858
+ url.searchParams.append('fb_dtsg', ctx.fb_dtsg);
859
+ url.searchParams.append('jazoest', ctx.jazoest);
860
+ url.searchParams.append('lsd', ctx.lsd || ctx.fb_dtsg);
861
+ url.searchParams.append('__spin_r', '1037381017');
862
+ url.searchParams.append('__spin_b', 'trunk');
863
+ url.searchParams.append('__spin_t', Math.floor(Date.now() / 1000).toString());
864
+ url.searchParams.append('__crn', 'comet.fbweb.CometGroupDiscussionRoute');
865
+
866
+ // Multipart form fields
867
+ const form = {
868
+ source: '8',
869
+ profile_id: ctx.userID,
870
+ waterfallxapp: 'comet',
871
+ farr: photoStream,
872
+ upload_id: uploadId,
873
+ };
874
+
875
+ const uploadResponse = await utils.postFormData(
876
+ url.toString(),
877
+ ctx.jar,
878
+ form,
879
+ ctx.globalOptions,
880
+ ctx,
881
+ );
882
+
883
+ const uploadResult = JSON.parse(
884
+ uploadResponse.body.toString().replace(/^for \(;;\);/, ''),
885
+ );
886
+
887
+ if (uploadResult.error || uploadResult.errors) {
888
+ throw new Error(
889
+ JSON.stringify(uploadResult.error || uploadResult.errors),
890
+ );
891
+ }
892
+
893
+ const photoId =
894
+ uploadResult.payload?.fbid ||
895
+ uploadResult.payload?.photoID ||
896
+ null;
897
+
898
+ results.push({
899
+ photoID: photoId,
900
+ uploadID: uploadId,
901
+ data: uploadResult,
902
+ });
903
+ }
904
+
905
+ return results;
906
+ },
907
+
908
+ /**
909
+ * Creates a post in a Facebook group, optionally with photo attachments and a title.
910
+ *
911
+ * @param {string} groupID - The ID of the group to post in.
912
+ * @param {object} options - Post options.
913
+ * @param {string} [options.message=''] - The text content of the post.
914
+ * @param {string} [options.title] - Optional post title (for groups that support titled posts).
915
+ * @param {string[]} [options.photos=[]] - Array of photo IDs from {@link uploadPhoto}.
916
+ * @returns {Promise<{ success: boolean; postID: string | null; url: string | null; data: any }>}
917
+ * @throws {Error} If groupID is missing or the API request fails.
918
+ *
919
+ * @example
920
+ * // Text-only post
921
+ * await api.group.createGroupPost('123456789', { message: 'Hello group!' });
922
+ *
923
+ * // Post with photos
924
+ * const photos = await api.group.uploadPhoto(['/path/a.jpg', '/path/b.jpg']);
925
+ * await api.group.createGroupPost('123456789', {
926
+ * message: 'Check these out!',
927
+ * photos: photos.map(p => p.photoID),
928
+ * });
929
+ *
930
+ * // Post with title and photos
931
+ * await api.group.createGroupPost('123456789', {
932
+ * message: 'Post body here',
933
+ * title: 'My Post Title',
934
+ * photos: photos.map(p => p.photoID),
935
+ * });
936
+ */
937
+ createGroupPost: async function (
938
+ groupID: string,
939
+ options: { message?: string; title?: string; photos?: string[] } = {},
940
+ ): Promise<{
941
+ success: boolean;
942
+ postID: string | null;
943
+ url: string | null;
944
+ data: any;
945
+ }> {
946
+ if (!groupID) throw new Error('createGroupPost: groupID is required.');
947
+
948
+ const postMessage = options.message || '';
949
+ const postTitle = options.title || null;
950
+ const photos = options.photos || [];
951
+
952
+ const composerSessionId = createBsid();
953
+
954
+ const variables: any = {
955
+ input: {
956
+ composer_entry_point: 'inline_composer',
957
+ composer_source_surface: 'group',
958
+ composer_type: 'group',
959
+ logging: {
960
+ composer_session_id: composerSessionId,
961
+ },
962
+ source: 'WWW',
963
+ message: {
964
+ ranges: [],
965
+ text: postMessage,
966
+ },
967
+ with_tags_ids: null,
968
+ inline_activities: [],
969
+ text_format_preset_id: '0',
970
+ group_flair: {
971
+ flair_id: null,
972
+ },
973
+ composed_text: {
974
+ block_data: ['{}'],
975
+ block_depths: [0],
976
+ block_types: [0],
977
+ blocks: [postMessage],
978
+ entities: ['[]'],
979
+ entity_map: '{}',
980
+ inline_styles: ['[]'],
981
+ },
982
+ navigation_data: {
983
+ attribution_id_v2: `CometGroupDiscussionRoot.react,comet.group,tap_bookmark,${Date.now()},352021,${groupID},,`,
984
+ },
985
+ tracking: [null],
986
+ event_share_metadata: {
987
+ surface: 'newsfeed',
988
+ },
989
+ audience: {
990
+ to_id: groupID,
991
+ },
992
+ actor_id: ctx.userID,
993
+ client_mutation_id: Math.floor(Math.random() * 10 + 1).toString(),
994
+ },
995
+ feedLocation: 'GROUP',
996
+ feedbackSource: 0,
997
+ focusCommentID: null,
998
+ gridMediaWidth: null,
999
+ groupID: null,
1000
+ scale: 1,
1001
+ privacySelectorRenderLocation: 'COMET_STREAM',
1002
+ checkPhotosToReelsUpsellEligibility: false,
1003
+ referringStoryRenderLocation: null,
1004
+ renderLocation: 'group',
1005
+ useDefaultActor: false,
1006
+ inviteShortLinkKey: null,
1007
+ isFeed: false,
1008
+ isFundraiser: false,
1009
+ isFunFactPost: false,
1010
+ isGroup: true,
1011
+ isEvent: false,
1012
+ isTimeline: false,
1013
+ isSocialLearning: false,
1014
+ isPageNewsFeed: false,
1015
+ isProfileReviews: false,
1016
+ isWorkSharedDraft: false,
1017
+ canUserManageOffers: false,
1018
+ __relay_internal__pv__CometUFIShareActionMigrationrelayprovider: true,
1019
+ __relay_internal__pv__GHLShouldChangeSponsoredDataFieldNamerelayprovider: true,
1020
+ __relay_internal__pv__GHLShouldChangeAdIdFieldNamerelayprovider: true,
1021
+ __relay_internal__pv__CometUFI_dedicated_comment_routable_dialog_gkrelayprovider: true,
1022
+ __relay_internal__pv__CometUFICommentAutoTranslationTyperelayprovider: 'ORIGINAL',
1023
+ __relay_internal__pv__CometUFICommentAvatarStickerAnimatedImagerelayprovider: false,
1024
+ __relay_internal__pv__CometUFICommentActionLinksRewriteEnabledrelayprovider: false,
1025
+ __relay_internal__pv__IsWorkUserrelayprovider: false,
1026
+ __relay_internal__pv__CometUFIReactionsEnableShortNamerelayprovider: false,
1027
+ __relay_internal__pv__CometUFISingleLineUFIrelayprovider: false,
1028
+ __relay_internal__pv__CometFeedStory_enable_post_permalink_white_space_clickrelayprovider: false,
1029
+ __relay_internal__pv__TestPilotShouldIncludeDemoAdUseCaserelayprovider: false,
1030
+ __relay_internal__pv__FBReels_deprecate_short_form_video_context_gkrelayprovider: true,
1031
+ __relay_internal__pv__FBReels_enable_view_dubbed_audio_type_gkrelayprovider: true,
1032
+ __relay_internal__pv__CometImmersivePhotoCanUserDisable3DMotionrelayprovider: false,
1033
+ __relay_internal__pv__WorkCometIsEmployeeGKProviderrelayprovider: false,
1034
+ __relay_internal__pv__IsMergQAPollsrelayprovider: false,
1035
+ __relay_internal__pv__FBReelsMediaFooter_comet_enable_reels_ads_gkrelayprovider: true,
1036
+ __relay_internal__pv__FBReelsIFUTileContent_reelsIFUPlayOnHoverrelayprovider: true,
1037
+ __relay_internal__pv__GroupsCometGYSJFeedItemHeightrelayprovider: 150,
1038
+ __relay_internal__pv__ShouldEnableBakedInTextStoriesrelayprovider: false,
1039
+ __relay_internal__pv__StoriesShouldIncludeFbNotesrelayprovider: false,
1040
+ __relay_internal__pv__groups_comet_use_glvrelayprovider: false,
1041
+ __relay_internal__pv__GHLShouldChangeSponsoredAuctionDistanceFieldNamerelayprovider: false,
1042
+ __relay_internal__pv__GHLShouldUseSponsoredAuctionLabelFieldNameV1relayprovider: false,
1043
+ __relay_internal__pv__GHLShouldUseSponsoredAuctionLabelFieldNameV2relayprovider: false,
1044
+ };
1045
+
1046
+ // Attach post title if provided
1047
+ if (postTitle) {
1048
+ variables.input.post_message_title = { text: postTitle };
1049
+ }
1050
+
1051
+ // Attach photos if provided
1052
+ if (photos.length > 0) {
1053
+ variables.input.attachments = photos
1054
+ .filter(Boolean)
1055
+ .map((photoID) => ({
1056
+ photo: { id: String(photoID) },
1057
+ }));
1058
+ }
1059
+
1060
+ const form = {
1061
+ av: ctx.userID,
1062
+ __aaid: '0',
1063
+ __user: ctx.userID,
1064
+ __a: '1',
1065
+ __req: '1z',
1066
+ __hs: '20558.HCSV2:comet_pkg.2.1...0',
1067
+ dpr: '1',
1068
+ __ccg: 'EXCELLENT',
1069
+ __comet_req: '15',
1070
+ locale: 'vi_VN',
1071
+ fb_dtsg: ctx.fb_dtsg,
1072
+ jazoest: ctx.jazoest,
1073
+ lsd: ctx.lsd || ctx.fb_dtsg,
1074
+ __spin_r: '1037381017',
1075
+ __spin_b: 'trunk',
1076
+ __spin_t: Math.floor(Date.now() / 1000).toString(),
1077
+ __crn: 'comet.fbweb.CometGroupDiscussionRoute',
1078
+ fb_api_caller_class: 'RelayModern',
1079
+ fb_api_req_friendly_name: 'ComposerStoryCreateMutation',
1080
+ variables: JSON.stringify(variables),
1081
+ server_timestamps: 'true',
1082
+ doc_id: '34999618373018994',
1083
+ };
1084
+
1085
+ const customHeader = {
1086
+ 'x-fb-friendly-name': 'ComposerStoryCreateMutation',
1087
+ 'x-fb-lsd': ctx.lsd || '',
1088
+ 'x-asbd-id': '359341',
1089
+ origin: 'https://www.facebook.com',
1090
+ referer: `https://www.facebook.com/groups/${groupID}?locale=vi_VN`,
1091
+ };
1092
+
1093
+ const res = await utils.post(
1094
+ 'https://www.facebook.com/api/graphql/',
1095
+ ctx.jar,
1096
+ form,
1097
+ ctx.globalOptions,
1098
+ ctx,
1099
+ customHeader,
1100
+ );
1101
+
1102
+ const data = parseResponseBody(res.body);
1103
+
1104
+ if (data?.errors) {
1105
+ throw new Error(JSON.stringify(data.errors));
1106
+ }
1107
+
1108
+ return {
1109
+ success: true,
1110
+ postID: data?.data?.story_create?.story?.id || null,
1111
+ url: data?.data?.story_create?.story?.url || null,
1112
+ data,
1113
+ };
1114
+ },
635
1115
  };
636
1116
 
637
1117
  return groupModule;