BasePlatformService.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. const Fastify = require('fastify');
  2. const RabbitMQConnector = require('./RabbitMQConnector');
  3. const { createLogger } = require('./logger');
  4. /**
  5. * BasePlatformService — tüm platform servisleri bu sınıftan extend eder.
  6. *
  7. * Her platform servisi şu metodları override etmeli:
  8. * - fetchFeed() → platform API'sinden feed çeker
  9. * - publishPost(post) → platforma içerik gönderir
  10. * - getStatus() → bağlantı durumu döner
  11. * - authenticate(code) → OAuth callback işler
  12. */
  13. class BasePlatformService extends RabbitMQConnector {
  14. constructor(platformName) {
  15. super();
  16. this.platformName = platformName;
  17. this.log = createLogger(platformName);
  18. this.app = Fastify({ logger: this.log });
  19. this._setupRoutes();
  20. }
  21. /** HTTP route'ları kaydet — platform servisleri override edebilir */
  22. _setupRoutes() {
  23. this.app.get('/status', async () => {
  24. return this.getStatus();
  25. });
  26. this.app.get('/feed', async (request, reply) => {
  27. try {
  28. const items = await this.fetchFeed(request.query);
  29. return { success: true, platform: this.platformName, count: items.length, items };
  30. } catch (err) {
  31. reply.code(500).send({ success: false, error: err.message });
  32. }
  33. });
  34. this.app.post('/post', async (request, reply) => {
  35. try {
  36. const result = await this.publishPost(request.body);
  37. return { success: true, platform: this.platformName, result };
  38. } catch (err) {
  39. reply.code(500).send({ success: false, error: err.message });
  40. }
  41. });
  42. this.app.get('/auth/callback', async (request, reply) => {
  43. try {
  44. const result = await this.authenticate(request.query);
  45. return { success: true, platform: this.platformName, result };
  46. } catch (err) {
  47. reply.code(500).send({ success: false, error: err.message });
  48. }
  49. });
  50. }
  51. /** HTTP sunucusunu başlat */
  52. async start(port = 3000) {
  53. await this.connect();
  54. await this.app.listen({ port, host: '0.0.0.0' });
  55. this.app.log.info({ action: 'service_start', port, outcome: 'success' }, `${this.platformName} service started`);
  56. }
  57. // ─── Alt sınıfların override edeceği metodlar ───────────────────────────────
  58. /** @returns {{ connected: boolean, platform: string, username?: string }} */
  59. async getStatus() {
  60. return { connected: false, platform: this.platformName };
  61. }
  62. /** @returns {Array<FeedItem>} normalize edilmiş feed öğeleri */
  63. async fetchFeed() {
  64. throw new Error(`[${this.platformName}] fetchFeed() implement edilmedi`);
  65. }
  66. /** @param {{ content: string, media?: Array, tags?: Array }} post */
  67. async publishPost() {
  68. throw new Error(`[${this.platformName}] publishPost() implement edilmedi`);
  69. }
  70. /** @param {{ code: string, state?: string }} query — OAuth callback params */
  71. async authenticate() {
  72. throw new Error(`[${this.platformName}] authenticate() implement edilmedi`);
  73. }
  74. // ─── Yardımcı metodlar ───────────────────────────────────────────────────────
  75. /**
  76. * Normalize edilmiş feed öğesi oluştur.
  77. * Platform servisleri bu şablonu kullanarak veriyi standartlaştırır.
  78. */
  79. normalizeFeedItem({
  80. originalId,
  81. author,
  82. content,
  83. contentHtml = null,
  84. media = [],
  85. links = [],
  86. platformTags = [],
  87. metrics = {},
  88. url = null,
  89. createdAt = new Date(),
  90. }) {
  91. return {
  92. platform: this.platformName,
  93. originalId: String(originalId),
  94. author: {
  95. name: author.name || '',
  96. username: author.username || '',
  97. avatar: author.avatar || null,
  98. profileUrl: author.profileUrl || null,
  99. },
  100. content,
  101. contentHtml,
  102. media,
  103. links,
  104. tags: [],
  105. platformTags,
  106. metrics: {
  107. likes: metrics.likes || 0,
  108. comments: metrics.comments || 0,
  109. shares: metrics.shares || 0,
  110. views: metrics.views || 0,
  111. bookmarks: metrics.bookmarks || 0,
  112. },
  113. url,
  114. createdAt: new Date(createdAt),
  115. fetchedAt: new Date(),
  116. };
  117. }
  118. }
  119. module.exports = BasePlatformService;