-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.ts
116 lines (101 loc) · 4.31 KB
/
server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Including dev-kit interfaces. It is not necessary, only helps development with types.
// You need to prefix them with ./node_modules
import {IExtensionObject} from './node_modules/pigallery2-extension-kit';
import {PhotoMetadata} from './node_modules/pigallery2-extension-kit/lib/common/entities/PhotoDTO';
import {UserRoles} from './node_modules/pigallery2-extension-kit/lib/common/entities/UserDTO';
// Including prod extension packages. You need to prefix them with ./node_modules
// lodash does not have types
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import * as _ from './node_modules/lodash';
// Importing packages that are available in the main app (listed in the packages.json in pigallery2)
import {Column, Entity, Index, PrimaryGeneratedColumn} from 'typeorm';
import {TestConfig} from './config';
// Using typeorm for ORM
@Entity()
class TestLoggerEntity {
@Index()
@PrimaryGeneratedColumn({unsigned: true})
id: number;
@Column()
text: string;
}
export const init = async (extension: IExtensionObject<TestConfig>): Promise<void> => {
extension.Logger.debug(`My extension is setting up. name: ${extension.extensionName}, id: ${extension.extensionId}`);
/**
* (Optional) Adding custom SQL table
*/
await extension.db.setExtensionTables([TestLoggerEntity]);
/**
* (Optional) Using prod package
*/
extension.Logger.silly('lodash prod package works: ', _.defaults({'a': 1}, {'a': 3, 'b': 2}));
/**
* (Optional) Implementing lifecycles events with MetadataLoader example
* */
extension.events.gallery.MetadataLoader
.loadPhotoMetadata.before(async (input, event) => {
extension.Logger.silly('onBefore: processing: ', JSON.stringify(input));
// The return value of this function will be piped to the next before handler
// or if no other handler then returned to the app
return input;
/*
* (Optional) It is possible to prevent default run and return with the expected out output of the MetadataLoader.loadPhotoMetadata
NOTE: if event.stopPropagation = true, MetadataLoader.loadPhotoMetadata.after won't be called.
event.stopPropagation = true;
return {
size: {width: 1, height: 1},
fileSize: 1,
creationDate: 0
} as PhotoMetadata;
*/
});
extension.events.gallery.MetadataLoader
.loadPhotoMetadata.after(async (data: { input: [string], output: PhotoMetadata }) => {
// Overrides the caption on all photos
// NOTE: this needs db reset as MetadataLoader only runs during indexing time
data.output.caption = extension.config.getConfig().myFavouriteNumber + '|PG2 sample extension:' + data.output.caption;
// The return value of this function will be piped to the next after handler
// or if no other handler then returned to the app
return data.output;
});
/**
* (Optional) Adding a REST api endpoint for logged-in users
*/
extension.RESTApi.get.jsonResponse(['/sample'], UserRoles.User, async () => {
// Inserting into our extension table and returning with the result
const conn = await extension.db.getSQLConnection();
conn.getRepository(TestLoggerEntity).save({text: 'called /sample at: ' + Date.now()});
return await conn.getRepository(TestLoggerEntity).find();
});
/**
* (Optional) Creating a messenger. You can use it with TopPickJob to send photos
*/
extension.messengers.addMessenger<{
text: string // same as the Ids below in the config array
}>('SampleMessenger',
/**
* (Optional) Creating messenger config (these values will be requested in the TopPickJob)
* Note: Jobs cant use typeconfig yet, so it uses a different way for configuration
*/
[{
id: 'text', // same as the keys in the function template above
type: 'string',
name: 'just a text',
description: 'nothing to mention here',
defaultValue: 'I hand picked these photos just for you:',
}],
{
sendMedia: async (c, m) => {
console.log('config got:', c.text);
// we are not sending the photos anywhere, just logging them on the console.
console.log(m);
}
});
};
export const cleanUp = async (extension: IExtensionObject<TestConfig>): Promise<void> => {
extension.Logger.debug('Cleaning up');
/*
* No need to clean up changed through extension.db, extension.RESTApi or extension.events
* */
};