mirror of
https://github.com/mempool/mempool.git
synced 2024-11-20 02:11:49 +01:00
Backend API to load sponsor profile photos.
This commit is contained in:
parent
9d4659c3ba
commit
fff8120daa
@ -13,7 +13,18 @@ class Donations {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
sponsorsCache: any[] = [];
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
|
this.$updateCache();
|
||||||
|
}
|
||||||
|
|
||||||
|
async $updateCache() {
|
||||||
|
try {
|
||||||
|
this.sponsorsCache = await this.$getDonationsFromDatabase('handle, image');
|
||||||
|
} catch (e) {
|
||||||
|
logger.warn('Setting sponsorsCache failed ' + e.message || e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setNotfyDonationStatusCallback(fn: any): void {
|
setNotfyDonationStatusCallback(fn: any): void {
|
||||||
@ -73,17 +84,25 @@ class Donations {
|
|||||||
const imageBlob = await this.$downloadProfileImageBlob(imageUrl);
|
const imageBlob = await this.$downloadProfileImageBlob(imageUrl);
|
||||||
|
|
||||||
logger.debug('Creating database entry for donation with invoice id: ' + response.id);
|
logger.debug('Creating database entry for donation with invoice id: ' + response.id);
|
||||||
this.$addDonationToDatabase(response.btcPaid, userData.screen_name, userData.id, response.id, imageUrl, imageBlob);
|
await this.$addDonationToDatabase(response.btcPaid, userData.screen_name, userData.id, response.id, imageUrl, imageBlob);
|
||||||
|
this.$updateCache();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.err(`Error fetching twitter data for handle ${response.orderId}: ${e.message}`);
|
logger.err(`Error fetching twitter data for handle ${response.orderId}: ${e.message}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async $getDonationsFromDatabase(): Promise<any[]> {
|
getSponsorImage(id: string): any | undefined {
|
||||||
|
const sponsor = this.sponsorsCache.find((s) => s.handle === id);
|
||||||
|
if (sponsor) {
|
||||||
|
return sponsor.image;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async $getDonationsFromDatabase(fields: string): Promise<any[]> {
|
||||||
try {
|
try {
|
||||||
const connection = await DB.pool.getConnection();
|
const connection = await DB.pool.getConnection();
|
||||||
const query = `SELECT handle, imageUrl, TO_BASE64(image) AS image_64 FROM donations WHERE handle != '' ORDER BY id DESC`;
|
const query = `SELECT ${fields} FROM donations ORDER BY id DESC`;
|
||||||
const [rows] = await connection.query<any>(query);
|
const [rows] = await connection.query<any>(query);
|
||||||
connection.release();
|
connection.release();
|
||||||
return rows;
|
return rows;
|
||||||
|
@ -174,6 +174,7 @@ class Server {
|
|||||||
if (config.BTCPAY_URL) {
|
if (config.BTCPAY_URL) {
|
||||||
this.app
|
this.app
|
||||||
.get(config.API_ENDPOINT + 'donations', routes.getDonations.bind(routes))
|
.get(config.API_ENDPOINT + 'donations', routes.getDonations.bind(routes))
|
||||||
|
.get(config.API_ENDPOINT + 'donations/images/:id', routes.getSponsorImage.bind(routes))
|
||||||
.post(config.API_ENDPOINT + 'donations', routes.createDonationRequest.bind(routes))
|
.post(config.API_ENDPOINT + 'donations', routes.createDonationRequest.bind(routes))
|
||||||
.post(config.API_ENDPOINT + 'donations-webhook', routes.donationWebhook.bind(routes))
|
.post(config.API_ENDPOINT + 'donations-webhook', routes.donationWebhook.bind(routes))
|
||||||
;
|
;
|
||||||
|
@ -143,13 +143,27 @@ class Routes {
|
|||||||
|
|
||||||
public async getDonations(req: Request, res: Response) {
|
public async getDonations(req: Request, res: Response) {
|
||||||
try {
|
try {
|
||||||
const result = await donations.$getDonationsFromDatabase();
|
const result = await donations.$getDonationsFromDatabase('handle, imageUrl');
|
||||||
res.json(result);
|
res.json(result);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
res.status(500).send(e.message);
|
res.status(500).send(e.message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async getSponsorImage(req: Request, res: Response) {
|
||||||
|
try {
|
||||||
|
const result = await donations.getSponsorImage(req.params.id);
|
||||||
|
if (result) {
|
||||||
|
res.set('Content-Type', 'image/jpeg');
|
||||||
|
res.send(result);
|
||||||
|
} else {
|
||||||
|
res.status(404).end();
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
res.status(500).send(e.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public async donationWebhook(req: Request, res: Response) {
|
public async donationWebhook(req: Request, res: Response) {
|
||||||
try {
|
try {
|
||||||
donations.$handleWebhookRequest(req.body);
|
donations.$handleWebhookRequest(req.body);
|
||||||
|
@ -54,7 +54,7 @@
|
|||||||
<ng-template ngFor let-sponsor [ngForOf]="sponsors">
|
<ng-template ngFor let-sponsor [ngForOf]="sponsors">
|
||||||
<a [href]="'https://twitter.com/' + sponsor.handle" target="_blank" rel="sponsored">
|
<a [href]="'https://twitter.com/' + sponsor.handle" target="_blank" rel="sponsored">
|
||||||
<div class="profile_photo d-inline-block" [title]="sponsor.handle">
|
<div class="profile_photo d-inline-block" [title]="sponsor.handle">
|
||||||
<img class="profile_img" [src]="bypassSecurityTrustUrl('data:image/jpeg;base64,' + sponsor.image_64)" />
|
<img class="profile_img" [src]="'/api/v1/donations/images/' + sponsor.handle" />
|
||||||
</div>
|
</div>
|
||||||
</a>
|
</a>
|
||||||
</ng-template>
|
</ng-template>
|
||||||
|
Loading…
Reference in New Issue
Block a user