Skip to content

Commit 3909f10

Browse files
committed
chore: remove attachment ids as int, instead use the url
1 parent 7e16b65 commit 3909f10

File tree

9 files changed

+44
-30
lines changed

9 files changed

+44
-30
lines changed

ts/models/message.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { ClosedGroupV2VisibleMessage } from '../session/messages/outgoing/visibl
99
import { PubKey } from '../session/types';
1010
import {
1111
UserUtils,
12+
attachmentIdAsStrFromUrl,
1213
uploadAttachmentsToFileServer,
1314
uploadLinkPreviewToFileServer,
1415
uploadQuoteThumbnailsToFileServer,
@@ -788,7 +789,7 @@ export class MessageModel extends Model<MessageAttributes> {
788789
let attachmentPromise;
789790
let linkPreviewPromise;
790791
let quotePromise;
791-
const fileIdsToLink: Array<number> = [];
792+
const fileIdsToLink: Array<string> = [];
792793

793794
// we can only send a single preview
794795
const firstPreviewWithData = previewWithData?.[0] || null;
@@ -811,9 +812,9 @@ export class MessageModel extends Model<MessageAttributes> {
811812
linkPreviewPromise,
812813
quotePromise,
813814
]);
814-
fileIdsToLink.push(...attachments.map(m => m.id));
815-
if (preview) {
816-
fileIdsToLink.push(preview.id);
815+
fileIdsToLink.push(...attachments.map(m => attachmentIdAsStrFromUrl(m.url)));
816+
if (preview && preview.image?.url) {
817+
fileIdsToLink.push(attachmentIdAsStrFromUrl(preview.image.url));
817818
}
818819

819820
if (quote && quote.attachments?.length) {

ts/session/apis/file_server_api/FileServerApi.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ const POST_GET_FILE_ENDPOINT = '/file';
2323
/**
2424
* Upload a file to the file server v2 using the onion v4 encoding
2525
* @param fileContent the data to send
26-
* @returns null or the fileID and complete URL to share this file
26+
* @returns null or the complete URL to share this file
2727
*/
2828
export const uploadFileToFsWithOnionV4 = async (
2929
fileContent: ArrayBuffer
30-
): Promise<{ fileId: number; fileUrl: string } | null> => {
30+
): Promise<{ fileUrl: string } | null> => {
3131
if (!fileContent || !fileContent.byteLength) {
3232
return null;
3333
}
@@ -51,14 +51,13 @@ export const uploadFileToFsWithOnionV4 = async (
5151
}
5252
const fileUrl = `${fileServerURL}${POST_GET_FILE_ENDPOINT}/${fileId}`;
5353
return {
54-
fileId,
5554
fileUrl,
5655
};
5756
};
5857

5958
/**
6059
* Download a file given the fileId from the fileserver
61-
* @param fileIdOrCompleteUrl the fileId to download or the completeUrl to the fileitself
60+
* @param fileIdOrCompleteUrl the fileId to download or the completeUrl to the file itself
6261
* @returns the data as an Uint8Array or null
6362
*/
6463
export const downloadFileFromFileServer = async (

ts/session/apis/open_group_api/opengroupV2/OpenGroupMessageV2.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ export class OpenGroupMessageV2 {
1212
public sentTimestamp: number;
1313
public base64EncodedData: string;
1414
public base64EncodedSignature?: string;
15-
public filesToLink?: Array<number>;
15+
public filesToLink?: Array<string>;
1616

1717
constructor(messageData: {
1818
serverId?: number;
1919
sender?: string;
2020
sentTimestamp: number;
2121
base64EncodedData: string;
2222
base64EncodedSignature?: string;
23-
filesToLink?: Array<number>;
23+
filesToLink?: Array<string>;
2424
}) {
2525
const {
2626
base64EncodedData,

ts/session/messages/outgoing/visibleMessage/VisibleMessage.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import ByteBuffer from 'bytebuffer';
22
import { isEmpty } from 'lodash';
3+
import Long from 'long';
34
import { SignalService } from '../../../../protobuf';
45
import { Reaction } from '../../../../types/Reaction';
56
import { DataMessage } from '../DataMessage';
67
import { LokiProfile } from '../../../../types/message';
78
import { ExpirableMessageParams } from '../ExpirableMessage';
9+
import { attachmentIdAsLongFromUrl } from '../../../utils';
810

911
interface AttachmentPointerCommon {
1012
contentType?: string;
@@ -21,12 +23,10 @@ interface AttachmentPointerCommon {
2123

2224
export interface AttachmentPointer extends AttachmentPointerCommon {
2325
url?: string;
24-
id?: number;
2526
}
2627

2728
export interface AttachmentPointerWithUrl extends AttachmentPointerCommon {
2829
url: string;
29-
id: number;
3030
}
3131

3232
export interface Preview {
@@ -37,7 +37,6 @@ export interface Preview {
3737

3838
export interface PreviewWithAttachmentUrl {
3939
url: string;
40-
id: number;
4140
title?: string;
4241
image?: AttachmentPointerWithUrl;
4342
}
@@ -75,12 +74,12 @@ export interface VisibleMessageParams extends ExpirableMessageParams {
7574
export class VisibleMessage extends DataMessage {
7675
public readonly reaction?: Reaction;
7776

78-
private readonly attachments?: Array<AttachmentPointerWithUrl>;
77+
private readonly attachments?: Array<AttachmentPointerWithUrl & { id: Long }>;
7978
private readonly body?: string;
8079
private readonly quote?: Quote;
8180
private readonly profileKey?: Uint8Array;
8281
private readonly profile?: SignalService.DataMessage.ILokiProfile;
83-
private readonly preview?: Array<PreviewWithAttachmentUrl>;
82+
private readonly preview?: Array<PreviewWithAttachmentUrl & { id?: Long }>;
8483

8584
/// In the case of a sync message, the public key of the person the message was targeted at.
8685
/// - Note: `null or undefined` if this isn't a sync message.
@@ -93,7 +92,10 @@ export class VisibleMessage extends DataMessage {
9392
expirationType: params.expirationType,
9493
expireTimer: params.expireTimer,
9594
});
96-
this.attachments = params.attachments;
95+
this.attachments = params.attachments?.map(attachment => ({
96+
...attachment,
97+
id: attachmentIdAsLongFromUrl(attachment.url),
98+
}));
9799
this.body = params.body;
98100
this.quote = params.quote;
99101

@@ -102,7 +104,10 @@ export class VisibleMessage extends DataMessage {
102104
this.profile = profile.lokiProfile;
103105
this.profileKey = profile.profileKey;
104106

105-
this.preview = params.preview;
107+
this.preview = params.preview?.map(attachment => ({
108+
...attachment,
109+
id: attachment.image?.url ? attachmentIdAsLongFromUrl(attachment.image.url) : undefined,
110+
}));
106111
this.reaction = params.reaction;
107112
this.syncTarget = params.syncTarget;
108113
}
@@ -122,9 +127,6 @@ export class VisibleMessage extends DataMessage {
122127

123128
dataMessage.attachments = this.attachments || [];
124129

125-
if (this.preview) {
126-
dataMessage.preview = this.preview;
127-
}
128130
if (this.reaction) {
129131
dataMessage.reaction = this.reaction;
130132
}
@@ -173,7 +175,9 @@ export class VisibleMessage extends DataMessage {
173175
if (preview.url) {
174176
item.url = preview.url;
175177
}
176-
item.image = preview.image || null;
178+
item.image = preview.image
179+
? { ...preview.image, id: attachmentIdAsLongFromUrl(preview.image.url) }
180+
: null;
177181

178182
return item;
179183
});

ts/session/sending/MessageQueue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export class MessageQueueCl {
6565
message: OpenGroupVisibleMessage;
6666
roomInfos: OpenGroupRequestCommonType;
6767
blinded: boolean;
68-
filesToLink: Array<number>;
68+
filesToLink: Array<string>;
6969
}) {
7070
// Skipping the MessageQueue for Open Groups v2; the message is sent directly
7171

ts/session/sending/MessageSender.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ async function sendToOpenGroupV2(
584584
rawMessage: OpenGroupVisibleMessage,
585585
roomInfos: OpenGroupRequestCommonType,
586586
blinded: boolean,
587-
filesToLink: Array<number>
587+
filesToLink: Array<string>
588588
): Promise<OpenGroupMessageV2 | boolean> {
589589
// we agreed to pad messages for opengroup v2
590590
const paddedBody = addMessagePadding(rawMessage.plainTextBuffer());

ts/session/utils/Attachments.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as crypto from 'crypto';
22
import _ from 'lodash';
3+
import Long from 'long';
34

45
import { Attachment } from '../../types/Attachment';
56

@@ -80,7 +81,6 @@ async function uploadToFileServer(params: UploadParams): Promise<AttachmentPoint
8081
if (uploadToV2Result) {
8182
const pointerWithUrl: AttachmentPointerWithUrl = {
8283
...pointer,
83-
id: uploadToV2Result.fileId,
8484
url: uploadToV2Result.fileUrl,
8585
};
8686
return pointerWithUrl;
@@ -118,7 +118,6 @@ export async function uploadLinkPreviewToFileServer(
118118
return {
119119
...preview,
120120
image,
121-
id: image.id,
122121
};
123122
}
124123

@@ -143,7 +142,6 @@ export async function uploadQuoteThumbnailsToFileServer(
143142
...attachment,
144143
thumbnail,
145144
url: thumbnail.url,
146-
id: thumbnail.id,
147145
} as QuotedAttachmentWithUrl;
148146
});
149147

@@ -154,3 +152,18 @@ export async function uploadQuoteThumbnailsToFileServer(
154152
attachments,
155153
};
156154
}
155+
156+
export function attachmentIdAsStrFromUrl(url: string) {
157+
return attachmentIdAsLongFromUrl(url).toString();
158+
}
159+
160+
export function attachmentIdAsLongFromUrl(url: string) {
161+
const urlObj = new URL(url);
162+
163+
const lastSegment = urlObj.pathname.split('/').pop();
164+
if (!lastSegment) {
165+
throw new Error('attachmentIdAsLongFromUrl last is not valid');
166+
}
167+
// this throws if not a valid long
168+
return Long.fromString(lastSegment);
169+
}

ts/session/utils/AttachmentsV2.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ async function uploadV3(params: UploadParamsV2): Promise<AttachmentPointerWithUr
5252

5353
return {
5454
...pointer,
55-
id: fileDetails.fileId,
5655
url: fileDetails.fileUrl,
5756
};
5857
}
@@ -89,7 +88,6 @@ export async function uploadLinkPreviewsV3(
8988
...preview,
9089
image,
9190
url: preview.url || image.url,
92-
id: image.id,
9391
};
9492
}
9593

ts/test/session/unit/messages/ChatMessage_test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ describe('VisibleMessage', () => {
121121
});
122122

123123
it('can create message with a preview', () => {
124-
const preview: PreviewWithAttachmentUrl = { url: 'url', title: 'title', id: 1 };
124+
const preview: PreviewWithAttachmentUrl = { url: 'url', title: 'title' };
125125
const previews = new Array<PreviewWithAttachmentUrl>();
126126
previews.push(preview);
127127

@@ -143,7 +143,6 @@ describe('VisibleMessage', () => {
143143
const attachment: AttachmentPointerWithUrl = {
144144
url: 'url',
145145
contentType: 'contentType',
146-
id: 1234,
147146
};
148147
const attachments = new Array<AttachmentPointerWithUrl>();
149148
attachments.push(attachment);

0 commit comments

Comments
 (0)