Skip to content

Add audio support #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,712 changes: 1,856 additions & 1,856 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getContact } from "./api/get-contact";
import { getConversation } from "./api/get-conversation";
import { getConversations } from "./api/get-conversations";
import { getJoinUrl } from "./api/get-join-url";
import { sendAudio } from "./api/send-audio";
import { sendImage } from "./api/send-image";
import { sendMessage } from "./api/send-message";
import { setConversationTopic } from "./api/set-conversation-topic";
Expand Down Expand Up @@ -95,7 +96,11 @@ export class Api extends events.EventEmitter implements ApiEvents {
return createConversation(this.io, this.context, allUsers);
}

async sendImage(message: api.NewImage, conversationId: string): Promise<api.SendMessageResult> {
async sendAudio(message: api.NewMediaMessage, conversationId: string): Promise<api.SendMessageResult> {
return sendAudio(this.io, this.context, message, conversationId);
}

async sendImage(message: api.NewMediaMessage, conversationId: string): Promise<api.SendMessageResult> {
return sendImage(this.io, this.context, message, conversationId);
}

Expand Down
107 changes: 107 additions & 0 deletions src/lib/api/send-audio.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import * as fs from "async-file";
import { Incident } from "incident";
import * as api from "../interfaces/api/api";
import { Context } from "../interfaces/api/context";
import * as io from "../interfaces/http-io";
import * as messagesUri from "../messages-uri";
import { getCurrentTime } from "../utils";

interface SendMessageQuery {
clientmessageid: string;
content: string;
messagetype: string;
contenttype: string;
amsreferences: string[];
}

interface SendMessageResponse {
OriginalArrivalTime: number;
}

export async function sendAudio(
io: io.HttpIo,
apiContext: Context,
document: api.NewMediaMessage,
conversationId: string,
): Promise<api.SendMessageResult> {
const bodyNewObject: any = {
filename: document.name,
type: "sharing/audio",
permissions: {[conversationId]: ["read"]},
};

const bodyNewObjectStr: string = JSON.stringify(bodyNewObject);
const requestOptionsNewObject: io.PostOptions = {
uri: messagesUri.objects("api.asm.skype.com"),
cookies: apiContext.cookies,
body: bodyNewObjectStr,
headers: {
"Authorization": `skype_token ${apiContext.skypeToken.value}`,
"Content-Type": "application/json",
"Content-Length": bodyNewObjectStr.length.toString(10),
"X-Client-Version": "0/0.0.0.0",
},
};

const resNewObject: io.Response = await io.post(requestOptionsNewObject);

if (resNewObject.statusCode !== 201) {
return Promise.reject(new Incident("send-audio", "Received wrong return code"));
}
const objectId: string = JSON.parse(resNewObject.body).id;
const file: Buffer = await fs.readFile(document.file);
const requestOptionsPutObject: io.PutOptions = {
uri: messagesUri.objectContent("api.asm.skype.com", objectId, "audio"),
cookies: apiContext.cookies,
body: file,
headers: {
"Authorization": `skype_token ${apiContext.skypeToken.value}`,
"Content-Type": "application",
"Content-Length": file.length.toString(10),
},
};

const resObject: io.Response = await io.put(requestOptionsPutObject);
if (resObject.statusCode !== 201) {
return Promise.reject(new Incident("send-audio", "Received wrong return code in upload"));
}

const objectContent: string = messagesUri.object("api.asm.skype.com", objectId);
const thumbnail: string = messagesUri.objectView("api.asm.skype.com", objectId, "audio");
const query: SendMessageQuery = {
clientmessageid: String(getCurrentTime() + Math.floor(10000 * Math.random())),
content: `
<URIObject uri="${objectContent}" url_thumbnail="${thumbnail}" type="Audio.1" doc_id="${objectId}">
loading...
<OriginalName v="${document.name}"></OriginalName>
<FileSize v="${file.length}"></FileSize>
</URIObject>
`,
messagetype: "RichText/Media_GenericFile",
contenttype: "text",
amsreferences: [objectId],
};

const requestOptions: io.PostOptions = {
uri: messagesUri.messages(apiContext.registrationToken.host, messagesUri.DEFAULT_USER, conversationId),
cookies: apiContext.cookies,
body: JSON.stringify(query),
headers: {
RegistrationToken: apiContext.registrationToken.raw,
},
};
const res: io.Response = await io.post(requestOptions);

if (res.statusCode !== 201) {
return Promise.reject(new Incident("send-audio", "Received wrong return code in send document"));
}

const parsed: messagesUri.MessageUri = messagesUri.parseMessage(res.headers["location"]);
const body: SendMessageResponse = JSON.parse(res.body);
return {
clientMessageId: query.clientmessageid,
arrivalTime: body.OriginalArrivalTime,
textContent: query.content,
MessageId: parsed.message,
};
}
3 changes: 2 additions & 1 deletion src/lib/api/send-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface SendMessageQuery {

export async function sendImage(
io: io.HttpIo, apiContext: Context,
img: api.NewImage,
img: api.NewMediaMessage,
conversationId: string,
): Promise<api.SendMessageResult> {
const bodyNewObject: any = {
Expand All @@ -35,6 +35,7 @@ export async function sendImage(
"Authorization": `skype_token ${apiContext.skypeToken.value}`,
"Content-Type": "application/json",
"Content-Length": bodyNewObjectStr.length.toString(10),
"X-Client-Version": "0/0.0.0.0",
},
};
const resNewObject: io.Response = await io.post(requestOptionsNewObject);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/interfaces/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface NewMessage {
textContent: string;
}

export interface NewImage {
export interface NewMediaMessage {
file: string;
name: string;
}
Expand Down
6 changes: 5 additions & 1 deletion src/lib/interfaces/api/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface CallParticipant {

export declare type ResourceType = "Text" | "RichText" | "Control/ClearTyping" | "Control/Typing" | "RichText/UriObject"
| "RichText/Media_GenericFile" | "Signal/Flamingo" | "Event/Call" | "RichText/Location" | "ConversationUpdate"
| "RichText/Media_Video";
| "RichText/Media_Video" | "RichText/Media_AudioMsg";

export interface Resource {
type: ResourceType;
Expand Down Expand Up @@ -54,6 +54,10 @@ export interface RichTextMediaVideoResource extends FileResource {
type: "RichText/Media_Video";
}

export interface RichTextMediaAudioResource extends FileResource {
type: "RichText/Media_AudioMsg";
}

export interface RichTextUriObjectResource extends FileResource {
type: "RichText/UriObject";
}
Expand Down
6 changes: 6 additions & 0 deletions src/lib/interfaces/native-api/message-resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,9 @@ export interface MediaVideo extends MessageResource {
clientmessageid: string; // A large integer (~20 digits)
content: string; // XML, root is <URIObject>
}

export interface MediaAudio extends MessageResource {
messagetype: "RichText/Media_AudioMsg";
clientmessageid: string;
content: string;
}
14 changes: 13 additions & 1 deletion src/lib/polling/messages-poller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ function formatMessageResource(nativeResource: nativeResources.MessageResource):
case "RichText/Media_Video":
// tslint:disable-next-line:max-line-length
return formatMediaVideoResource(formatFileResource(formatGenericMessageResource(nativeResource, nativeResource.messagetype), <nativeMessageResources.MediaVideo> nativeResource), <nativeMessageResources.MediaVideo> nativeResource);
case "RichText/Media_AudioMsg":
// tslint:disable-next-line:max-line-length
return formatMediaAudioResource(formatFileResource(formatGenericMessageResource(nativeResource, nativeResource.messagetype), <nativeMessageResources.MediaAudio> nativeResource), <nativeMessageResources.MediaAudio> nativeResource);
case "RichText/Media_GenericFile":
// tslint:disable-next-line:max-line-length
return formatMediaGenericFileResource(formatFileResource(formatGenericMessageResource(nativeResource, nativeResource.messagetype), <nativeMessageResources.MediaGenericFile> nativeResource), <nativeMessageResources.MediaGenericFile> nativeResource);
Expand Down Expand Up @@ -158,7 +161,8 @@ function formatMessageResource(nativeResource: nativeResources.MessageResource):
type NativeFileResouce =
nativeMessageResources.MediaGenericFile
| nativeMessageResources.UriObject
| nativeMessageResources.MediaVideo;
| nativeMessageResources.MediaVideo
| nativeMessageResources.MediaAudio;

function formatFileResource(retObj: resources.Resource, native: NativeFileResouce): resources.FileResource {
const ret: resources.FileResource = retObj as resources.FileResource;
Expand Down Expand Up @@ -194,6 +198,14 @@ function formatMediaVideoResource(
return ret;
}

function formatMediaAudioResource(
retObj: resources.FileResource,
_native: nativeMessageResources.MediaAudio,
): resources.RichTextMediaGenericFileResource {
const ret: resources.RichTextMediaGenericFileResource = retObj as resources.RichTextMediaGenericFileResource;
return ret;
}

// tslint:disable-next-line:max-line-length
function formatUriObjectResource(
retObj: resources.FileResource,
Expand Down