BasePlatformService.js 4.1 KB

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