whatsapp-web.js 1.22.2-alpha.0 → 1.22.2-alpha.2

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.
@@ -14,9 +14,9 @@ exports.ExposeStore = (moduleRaidStr) => {
14
14
  window.Store.CryptoLib = window.mR.findModule('decryptE2EMedia')[0];
15
15
  window.Store.DownloadManager = window.mR.findModule('downloadManager')[0].downloadManager;
16
16
  window.Store.GroupMetadata = window.mR.findModule('GroupMetadata')[0].default.GroupMetadata;
17
- window.Store.Invite = window.mR.findModule('resetGroupInviteCode')[0];
18
- window.Store.InviteInfo = window.mR.findModule('queryGroupInvite')[0];
17
+ window.Store.GroupMetadata.queryAndUpdate = window.mR.findModule('queryAndUpdateGroupMetadataById')[0].queryAndUpdateGroupMetadataById;
19
18
  window.Store.Label = window.mR.findModule('LabelCollection')[0].LabelCollection;
19
+ window.Store.ContactCollection = window.mR.findModule('ContactCollection')[0].ContactCollection;
20
20
  window.Store.MediaPrep = window.mR.findModule('prepRawMedia')[0];
21
21
  window.Store.MediaObject = window.mR.findModule('getOrCreateMediaObject')[0];
22
22
  window.Store.NumberInfo = window.mR.findModule('formattedPhoneNumber')[0];
@@ -25,7 +25,6 @@ exports.ExposeStore = (moduleRaidStr) => {
25
25
  window.Store.MsgKey = window.mR.findModule((module) => module.default && module.default.fromString)[0].default;
26
26
  window.Store.MessageInfo = window.mR.findModule('sendQueryMsgInfo')[0];
27
27
  window.Store.OpaqueData = window.mR.findModule(module => module.default && module.default.createFromData)[0].default;
28
- window.Store.QueryExist = window.mR.findModule('queryExists')[0] ? window.mR.findModule('queryExists')[0].queryExists : window.mR.findModule('queryExist')[0].queryWidExists;
29
28
  window.Store.QueryProduct = window.mR.findModule('queryProduct')[0];
30
29
  window.Store.QueryOrder = window.mR.findModule('queryOrder')[0];
31
30
  window.Store.SendClear = window.mR.findModule('sendClear')[0];
@@ -44,32 +43,54 @@ exports.ExposeStore = (moduleRaidStr) => {
44
43
  window.Store.ProfilePic = window.mR.findModule('profilePicResync')[0];
45
44
  window.Store.PresenceUtils = window.mR.findModule('sendPresenceAvailable')[0];
46
45
  window.Store.ChatState = window.mR.findModule('sendChatStateComposing')[0];
47
- window.Store.GroupParticipants = window.mR.findModule('promoteParticipants')[0];
48
- window.Store.JoinInviteV4 = window.mR.findModule('queryGroupInviteV4')[0];
49
46
  window.Store.findCommonGroups = window.mR.findModule('findCommonGroups')[0].findCommonGroups;
50
47
  window.Store.StatusUtils = window.mR.findModule('setMyStatus')[0];
51
48
  window.Store.ConversationMsgs = window.mR.findModule('loadEarlierMsgs')[0];
52
49
  window.Store.sendReactionToMsg = window.mR.findModule('sendReactionToMsg')[0].sendReactionToMsg;
53
50
  window.Store.createOrUpdateReactionsModule = window.mR.findModule('createOrUpdateReactions')[0];
54
51
  window.Store.EphemeralFields = window.mR.findModule('getEphemeralFields')[0];
55
- window.Store.ReplyUtils = window.mR.findModule('canReplyMsg').length > 0 && window.mR.findModule('canReplyMsg')[0];
56
52
  window.Store.MsgActionChecks = window.mR.findModule('canSenderRevokeMsg')[0];
57
53
  window.Store.QuotedMsg = window.mR.findModule('getQuotedMsgObj')[0];
58
54
  window.Store.Socket = window.mR.findModule('deprecatedSendIq')[0];
59
55
  window.Store.SocketWap = window.mR.findModule('wap')[0];
60
56
  window.Store.SearchContext = window.mR.findModule('getSearchContext')[0].getSearchContext;
61
57
  window.Store.DrawerManager = window.mR.findModule('DrawerManager')[0].DrawerManager;
58
+ window.Store.LidUtils = window.mR.findModule('getCurrentLid')[0];
59
+ window.Store.WidToJid = window.mR.findModule('widToUserJid')[0];
60
+ window.Store.JidToWid = window.mR.findModule('userJidToUserWid')[0];
61
+
62
+ /* eslint-disable no-undef, no-cond-assign */
63
+ window.Store.QueryExist = ((m = window.mR.findModule('queryExists')[0]) ? m.queryExists : window.mR.findModule('queryExist')[0].queryWidExists);
64
+ window.Store.ReplyUtils = (m = window.mR.findModule('canReplyMsg')).length > 0 && m[0];
65
+ if ((m = window.mR.findModule('ChatCollection')[0]) && m.ChatCollection && typeof m.ChatCollection.findImpl === 'undefined' && typeof m.ChatCollection._find !== 'undefined') m.ChatCollection.findImpl = m.ChatCollection._find;
66
+ /* eslint-enable no-undef, no-cond-assign */
67
+
62
68
  window.Store.StickerTools = {
63
69
  ...window.mR.findModule('toWebpSticker')[0],
64
70
  ...window.mR.findModule('addWebpMetadata')[0]
65
71
  };
66
-
67
72
  window.Store.GroupUtils = {
68
73
  ...window.mR.findModule('createGroup')[0],
69
74
  ...window.mR.findModule('setGroupDescription')[0],
70
75
  ...window.mR.findModule('sendExitGroup')[0],
71
76
  ...window.mR.findModule('sendSetPicture')[0]
72
77
  };
78
+ window.Store.GroupParticipants = {
79
+ ...window.mR.findModule('promoteParticipants')[0],
80
+ ...window.mR.findModule('sendAddParticipantsRPC')[0]
81
+ };
82
+ window.Store.GroupInvite = {
83
+ ...window.mR.findModule('resetGroupInviteCode')[0],
84
+ ...window.mR.findModule('queryGroupInvite')[0]
85
+ };
86
+ window.Store.GroupInviteV4 = {
87
+ ...window.mR.findModule('queryGroupInviteV4')[0],
88
+ ...window.mR.findModule('sendGroupInviteMessage')[0]
89
+ };
90
+ window.Store.MembershipRequestUtils = {
91
+ ...window.mR.findModule('getMembershipApprovalRequests')[0],
92
+ ...window.mR.findModule('sendMembershipRequestsActionRPC')[0]
93
+ };
73
94
 
74
95
  if (!window.Store.Chat._find) {
75
96
  window.Store.Chat._find = e => {
@@ -80,12 +101,6 @@ exports.ExposeStore = (moduleRaidStr) => {
80
101
  };
81
102
  }
82
103
 
83
- if (window.mR.findModule('ChatCollection')[0] && window.mR.findModule('ChatCollection')[0].ChatCollection) {
84
- if (typeof window.mR.findModule('ChatCollection')[0].ChatCollection.findImpl === 'undefined' && typeof window.mR.findModule('ChatCollection')[0].ChatCollection._find != 'undefined') {
85
- window.mR.findModule('ChatCollection')[0].ChatCollection.findImpl = window.mR.findModule('ChatCollection')[0].ChatCollection._find;
86
- }
87
- }
88
-
89
104
  // TODO remove these once everybody has been updated to WWebJS with legacy sessions removed
90
105
  const _linkPreview = window.mR.findModule('queryLinkPreview');
91
106
  if (_linkPreview && _linkPreview[0] && _linkPreview[0].default) {
@@ -103,6 +118,32 @@ exports.ExposeStore = (moduleRaidStr) => {
103
118
  if(_features) {
104
119
  window.Store.Features = _features.LegacyPhoneFeatures;
105
120
  }
121
+
122
+ /**
123
+ * Target options object description
124
+ * @typedef {Object} TargetOptions
125
+ * @property {string|number} moduleId The name or a key of the target module to search
126
+ * @property {number} index The index value of the target module
127
+ * @property {string} property The function name to get from a module
128
+ */
129
+
130
+ /**
131
+ * Function to modify functions
132
+ * @param {TargetOptions} target Options specifying the target function to search for modifying
133
+ * @param {Function} callback Modified function
134
+ */
135
+ window.injectToFunction = (target, callback) => {
136
+ const module = typeof target.moduleId === 'string'
137
+ ? window.mR.findModule(target.moduleId)
138
+ : window.mR.modules[target.moduleId];
139
+ const originalFunction = module[target.index][target.property];
140
+ const modifiedFunction = (...args) => callback(originalFunction, ...args);
141
+ module[target.index][target.property] = modifiedFunction;
142
+ };
143
+
144
+ window.injectToFunction({ moduleId: 'mediaTypeFromProtobuf', index: 0, property: 'mediaTypeFromProtobuf' }, (func, ...args) => { const [proto] = args; return proto.locationMessage ? null : func(...args); });
145
+
146
+ window.injectToFunction({ moduleId: 'typeAttributeFromProtobuf', index: 0, property: 'typeAttributeFromProtobuf' }, (func, ...args) => { const [proto] = args; return proto.locationMessage || proto.groupInviteMessage ? 'text' : func(...args); });
106
147
  };
107
148
 
108
149
  exports.LoadUtils = () => {
@@ -159,11 +200,15 @@ exports.LoadUtils = () => {
159
200
 
160
201
  let locationOptions = {};
161
202
  if (options.location) {
203
+ let { latitude, longitude, description, url } = options.location;
204
+ url = window.Store.Validators.findLink(url)?.href;
205
+ url && !description && (description = url);
162
206
  locationOptions = {
163
207
  type: 'location',
164
- loc: options.location.description,
165
- lat: options.location.latitude,
166
- lng: options.location.longitude
208
+ loc: description,
209
+ lat: latitude,
210
+ lng: longitude,
211
+ clientUrl: url
167
212
  };
168
213
  delete options.location;
169
214
  }
@@ -832,4 +877,198 @@ exports.LoadUtils = () => {
832
877
  throw err;
833
878
  }
834
879
  };
880
+
881
+ window.WWebJS.getProfilePicThumbToBase64 = async (chatWid) => {
882
+ const profilePicCollection = await window.Store.ProfilePicThumb.find(chatWid);
883
+
884
+ const _readImageAsBase64 = (imageBlob) => {
885
+ return new Promise((resolve) => {
886
+ const reader = new FileReader();
887
+ reader.onloadend = function () {
888
+ const base64Image = reader.result;
889
+ if (base64Image == null) {
890
+ resolve(undefined);
891
+ } else {
892
+ const base64Data = base64Image.toString().split(',')[1];
893
+ resolve(base64Data);
894
+ }
895
+ };
896
+ reader.readAsDataURL(imageBlob);
897
+ });
898
+ };
899
+
900
+ if (profilePicCollection?.img) {
901
+ try {
902
+ const response = await fetch(profilePicCollection.img);
903
+ if (response.ok) {
904
+ const imageBlob = await response.blob();
905
+ if (imageBlob) {
906
+ const base64Image = await _readImageAsBase64(imageBlob);
907
+ return base64Image;
908
+ }
909
+ }
910
+ } catch (error) { /* empty */ }
911
+ }
912
+ return undefined;
913
+ };
914
+
915
+ window.WWebJS.getAddParticipantsRpcResult = async (groupMetadata, groupWid, participantWid) => {
916
+ const participantLidArgs = groupMetadata?.isLidAddressingMode
917
+ ? {
918
+ phoneNumber: participantWid,
919
+ lid: window.Store.LidUtils.getCurrentLid(participantWid)
920
+ }
921
+ : { phoneNumber: participantWid };
922
+
923
+ const iqTo = window.Store.WidToJid.widToGroupJid(groupWid);
924
+
925
+ const participantArgs =
926
+ participantLidArgs.lid
927
+ ? [{
928
+ participantJid: window.Store.WidToJid.widToUserJid(participantLidArgs.lid),
929
+ phoneNumberMixinArgs: {
930
+ anyPhoneNumber: window.Store.WidToJid.widToUserJid(participantLidArgs.phoneNumber)
931
+ }
932
+ }]
933
+ : [{
934
+ participantJid: window.Store.WidToJid.widToUserJid(participantLidArgs.phoneNumber)
935
+ }];
936
+
937
+ let rpcResult, resultArgs;
938
+ const isOldImpl = window.compareWwebVersions(window.Debug.VERSION, '<=', '2.2335.9');
939
+ const data = {
940
+ name: undefined,
941
+ code: undefined,
942
+ inviteV4Code: undefined,
943
+ inviteV4CodeExp: undefined
944
+ };
945
+
946
+ try {
947
+ rpcResult = await window.Store.GroupParticipants.sendAddParticipantsRPC({ participantArgs, iqTo });
948
+ resultArgs = isOldImpl
949
+ ? rpcResult.value.addParticipant[0].addParticipantsParticipantMixins
950
+ : rpcResult.value.addParticipant[0]
951
+ .addParticipantsParticipantAddedOrNonRegisteredWaUserParticipantErrorLidResponseMixinGroup
952
+ .value
953
+ .addParticipantsParticipantMixins;
954
+ } catch (err) {
955
+ data.code = 400;
956
+ return data;
957
+ }
958
+
959
+ if (rpcResult.name === 'AddParticipantsResponseSuccess') {
960
+ const code = resultArgs?.value.error ?? '200';
961
+ data.name = resultArgs?.name;
962
+ data.code = +code;
963
+ data.inviteV4Code = resultArgs?.value.addRequestCode;
964
+ data.inviteV4CodeExp = resultArgs?.value.addRequestExpiration?.toString();
965
+ }
966
+
967
+ else if (rpcResult.name === 'AddParticipantsResponseClientError') {
968
+ const { code: code } = rpcResult.value.errorAddParticipantsClientErrors.value;
969
+ data.code = +code;
970
+ }
971
+
972
+ else if (rpcResult.name === 'AddParticipantsResponseServerError') {
973
+ const { code: code } = rpcResult.value.errorServerErrors.value;
974
+ data.code = +code;
975
+ }
976
+
977
+ return data;
978
+ };
979
+
980
+ window.WWebJS.membershipRequestAction = async (groupId, action, requesterIds, sleep) => {
981
+ const groupWid = window.Store.WidFactory.createWid(groupId);
982
+ const group = await window.Store.Chat.find(groupWid);
983
+ const toApprove = action === 'Approve';
984
+ let membershipRequests;
985
+ let response;
986
+ let result = [];
987
+
988
+ await window.Store.GroupMetadata.queryAndUpdate(groupWid);
989
+
990
+ if (!requesterIds?.length) {
991
+ membershipRequests = group.groupMetadata.membershipApprovalRequests._models.map(({ id }) => id);
992
+ } else {
993
+ !Array.isArray(requesterIds) && (requesterIds = [requesterIds]);
994
+ membershipRequests = requesterIds.map(r => window.Store.WidFactory.createWid(r));
995
+ }
996
+
997
+ if (!membershipRequests.length) return [];
998
+
999
+ const participantArgs = membershipRequests.map(m => ({
1000
+ participantArgs: [
1001
+ {
1002
+ participantJid: window.Store.WidToJid.widToUserJid(m)
1003
+ }
1004
+ ]
1005
+ }));
1006
+
1007
+ const groupJid = window.Store.WidToJid.widToGroupJid(groupWid);
1008
+
1009
+ const _getSleepTime = (sleep) => {
1010
+ if (!Array.isArray(sleep) || (sleep.length === 2 && sleep[0] === sleep[1])) {
1011
+ return sleep;
1012
+ }
1013
+ if (sleep.length === 1) {
1014
+ return sleep[0];
1015
+ }
1016
+ sleep[1] - sleep[0] < 100 && (sleep[0] = sleep[1]) && (sleep[1] += 100);
1017
+ return Math.floor(Math.random() * (sleep[1] - sleep[0] + 1)) + sleep[0];
1018
+ };
1019
+
1020
+ const membReqResCodes = {
1021
+ default: `An unknown error occupied while ${toApprove ? 'approving' : 'rejecting'} the participant membership request`,
1022
+ 400: 'ParticipantNotFoundError',
1023
+ 401: 'ParticipantNotAuthorizedError',
1024
+ 403: 'ParticipantForbiddenError',
1025
+ 404: 'ParticipantRequestNotFoundError',
1026
+ 408: 'ParticipantTemporarilyBlockedError',
1027
+ 409: 'ParticipantConflictError',
1028
+ 412: 'ParticipantParentLinkedGroupsResourceConstraintError',
1029
+ 500: 'ParticipantResourceConstraintError'
1030
+ };
1031
+
1032
+ try {
1033
+ for (const participant of participantArgs) {
1034
+ response = await window.Store.MembershipRequestUtils.sendMembershipRequestsActionRPC({
1035
+ iqTo: groupJid,
1036
+ [toApprove ? 'approveArgs' : 'rejectArgs']: participant
1037
+ });
1038
+
1039
+ if (response.name === 'MembershipRequestsActionResponseSuccess') {
1040
+ const value = toApprove
1041
+ ? response.value.membershipRequestsActionApprove
1042
+ : response.value.membershipRequestsActionReject;
1043
+ if (value?.participant) {
1044
+ const [_] = value.participant.map(p => {
1045
+ const error = toApprove
1046
+ ? value.participant[0].membershipRequestsActionAcceptParticipantMixins?.value.error
1047
+ : value.participant[0].membershipRequestsActionRejectParticipantMixins?.value.error;
1048
+ return {
1049
+ requesterId: window.Store.WidFactory.createWid(p.jid)._serialized,
1050
+ ...(error
1051
+ ? { error: +error, message: membReqResCodes[error] || membReqResCodes.default }
1052
+ : { message: `${toApprove ? 'Approved' : 'Rejected'} successfully` })
1053
+ };
1054
+ });
1055
+ _ && result.push(_);
1056
+ }
1057
+ } else {
1058
+ result.push({
1059
+ requesterId: window.Store.JidToWid.userJidToUserWid(participant.participantArgs[0].participantJid)._serialized,
1060
+ message: 'ServerStatusCodeError'
1061
+ });
1062
+ }
1063
+
1064
+ sleep &&
1065
+ participantArgs.length > 1 &&
1066
+ participantArgs.indexOf(participant) !== participantArgs.length - 1 &&
1067
+ (await new Promise((resolve) => setTimeout(resolve, _getSleepTime(sleep))));
1068
+ }
1069
+ return result;
1070
+ } catch (err) {
1071
+ return [];
1072
+ }
1073
+ };
835
1074
  };