Feed Comments and Interactions
Manage social interactions with feed items including submitting comments, loading comments, liking/unliking posts, and deleting content.
Available Methods
Submit Comment
submitComment(string $appID, string $feedID, string $text, string $parentID = '', array $taggedPlayers = []): int
Load Comments
loadComments(string $appID, string $feedID, int $loadedCount = 0, int $maxResults = 3, string $parentID = ''): array
Like/Unlike Feed
updateLike(string $appID, string $feedID, bool $like = true): int
Delete Comment
deleteComment(string $appID, string $feedID, string $commentID): int
Delete Feed
deleteFeed(string $appID, string $feedID): int
Submit Text Feed
submitTextFeed(string $appID, string $text): int
Method Details
Submit Comment
Submit a comment on a feed item with optional player tagging and reply threading.
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
$appID | string | Yes | The app ID that the feed belongs to |
$feedID | string | Yes | The ID of the feed item to comment on |
$text | string | Yes | The comment text content |
$parentID | string | No | Parent comment ID for threaded replies |
$taggedPlayers | array | No | Array of player IDs tagged in the comment |
Return Value
Returns HTTP status code (200 for success).
Example Usage
use Veniso\Moitribe\Sdk\MoitribeApi;
$moitribe = new MoitribeApi(['apiKey' => 'your-api-key']);
// Basic comment
$statusCode = $moitribe->feeds()->submitComment(
'your-app-id',
'feed-item-123',
'Great achievement! Congratulations!'
);
if ($statusCode === 200) {
echo "Comment submitted successfully\n";
}
// Comment with player tagging
$statusCode = $moitribe->feeds()->submitComment(
'your-app-id',
'feed-item-123',
'Nice work @player-456 and @player-789!',
'', // No parent ID (top-level comment)
['player-456', 'player-789'] // Tagged players
);
// Reply to existing comment
$statusCode = $moitribe->feeds()->submitComment(
'your-app-id',
'feed-item-123',
'I agree with your point!',
'comment-456' // Parent comment ID
);
Load Comments
Load comments for a specific feed item with pagination and threading support.
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
$appID | string | Yes | The app ID that the feed item belongs to |
$feedID | string | Yes | The feed ID of the item |
$loadedCount | int | No | Number of comments already loaded (default: 0) |
$maxResults | int | No | Maximum comments to fetch (default: 3) |
$parentID | string | No | Parent comment ID for sub-comments |
Return Value
Returns an array with structure:
[
'players' => Player[], // Player objects referenced in comments
'data' => FeedComment[] // Comment objects
]
Example Usage
// Load top-level comments
$comments = $moitribe->feeds()->loadComments(
'your-app-id',
'feed-item-123',
0, // No comments loaded yet
10 // Load up to 10 comments
);
foreach ($comments['data'] as $comment) {
echo "Comment: " . $comment->getText() . "\n";
echo "Author: " . $comment->getPlayerId() . "\n";
echo "Likes: " . $comment->getLikes() . "\n";
echo "---\n";
}
// Load replies to a specific comment
$replies = $moitribe->feeds()->loadComments(
'your-app-id',
'feed-item-123',
0, // Start from beginning
5, // Load 5 replies
'comment-456' // Parent comment ID
);
// Paginated comment loading
function loadAllComments($moitribe, $appId, $feedId) {
$allComments = [];
$loadedCount = 0;
$batchSize = 20;
do {
$batch = $moitribe->feeds()->loadComments(
$appId,
$feedId,
$loadedCount,
$batchSize
);
if (empty($batch['data'])) {
break;
}
$allComments = array_merge($allComments, $batch['data']);
$loadedCount += count($batch['data']);
} while (count($batch['data']) >= $batchSize);
return $allComments;
}
Like/Unlike Feed
Like or unlike a feed item.
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
$appID | string | Yes | The app ID that the feed item belongs to |
$feedID | string | Yes | The feed ID of the item |
$like | bool | No | True to like, false to unlike (default: true) |
Return Value
Returns HTTP status code (200 for success).
Example Usage
// Like a feed item
$statusCode = $moitribe->feeds()->updateLike(
'your-app-id',
'feed-item-123',
true // Like
);
// Unlike a feed item
$statusCode = $moitribe->feeds()->updateLike(
'your-app-id',
'feed-item-123',
false // Unlike
);
// Toggle like status
function toggleLike($moitribe, $appId, $feedId, $currentLikeStatus) {
$newStatus = !$currentLikeStatus;
$statusCode = $moitribe->feeds()->updateLike($appId, $feedId, $newStatus);
return [
'success' => $statusCode === 200,
'liked' => $newStatus
];
}
Delete Comment
Delete a comment from a feed item.
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
$appID | string | Yes | The app ID that the feed item belongs to |
$feedID | string | Yes | The feed ID of the item |
$commentID | string | Yes | The comment ID to delete |
Return Value
Returns HTTP status code (200 for success).
Example Usage
$statusCode = $moitribe->feeds()->deleteComment(
'your-app-id',
'feed-item-123',
'comment-456'
);
if ($statusCode === 200) {
echo "Comment deleted successfully\n";
}
Delete Feed
Delete an entire feed item (only available to the item owner or moderators).
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
$appID | string | Yes | The app ID that the feed belongs to |
$feedID | string | Yes | The feed ID to delete |
Return Value
Returns HTTP status code (200 for success).
Example Usage
$statusCode = $moitribe->feeds()->deleteFeed(
'your-app-id',
'feed-item-123'
);
if ($statusCode === 200) {
echo "Feed item deleted successfully\n";
}
Submit Text Feed
Create a new text-only feed post.
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
$appID | string | Yes | The app ID for which the post is made |
$text | string | Yes | The text content of the post |
Return Value
Returns HTTP status code (200 for success).
Example Usage
$statusCode = $moitribe->feeds()->submitTextFeed(
'your-app-id',
'Just achieved a new high score! '
);
if ($statusCode === 200) {
echo "Feed post created successfully\n";
}
Integration Examples
Complete Comment System
class CommentManager {
private $moitribe;
private $appId;
public function __construct($moitribe, $appId) {
$this->moitribe = $moitribe;
$this->appId = $appId;
}
public function addComment($feedId, $text, $parentCommentId = null, $taggedPlayers = []) {
try {
$statusCode = $this->moitribe->feeds()->submitComment(
$this->appId,
$feedId,
$text,
$parentCommentId ?? '',
$taggedPlayers
);
return [
'success' => $statusCode === 200,
'message' => $statusCode === 200 ? 'Comment added' : 'Failed to add comment'
];
} catch (Exception $e) {
return [
'success' => false,
'message' => $e->getMessage()
];
}
}
public function getComments($feedId, $page = 0, $pageSize = 10) {
try {
$loadedCount = $page * $pageSize;
$comments = $this->moitribe->feeds()->loadComments(
$this->appId,
$feedId,
$loadedCount,
$pageSize
);
return [
'success' => true,
'comments' => $comments['data'],
'players' => $comments['players'],
'has_more' => count($comments['data']) >= $pageSize
];
} catch (Exception $e) {
return [
'success' => false,
'message' => $e->getMessage()
];
}
}
public function toggleLike($feedId, $like) {
try {
$statusCode = $this->moitribe->feeds()->updateLike(
$this->appId,
$feedId,
$like
);
return [
'success' => $statusCode === 200,
'liked' => $like
];
} catch (Exception $e) {
return [
'success' => false,
'message' => $e->getMessage()
];
}
}
}
Web API Endpoints
// Laravel Routes
Route::post('/api/feed/{feedId}/comment', function (Request $request, $feedId) {
$request->validate([
'text' => 'required|string|max:500',
'parent_id' => 'nullable|string',
'tagged_players' => 'array'
]);
$commentManager = new CommentManager($moitribe, config('moitribe.app_id'));
$result = $commentManager->addComment(
$feedId,
$request->text,
$request->parent_id,
$request->tagged_players ?? []
);
return response()->json($result);
});
Route::get('/api/feed/{feedId}/comments', function (Request $request, $feedId) {
$page = $request->input('page', 0);
$pageSize = $request->input('page_size', 10);
$commentManager = new CommentManager($moitribe, config('moitribe.app_id'));
$result = $commentManager->getComments($feedId, $page, $pageSize);
return response()->json($result);
});
Route::post('/api/feed/{feedId}/like', function (Request $request, $feedId) {
$request->validate([
'like' => 'required|boolean'
]);
$commentManager = new CommentManager($moitribe, config('moitribe.app_id'));
$result = $commentManager->toggleLike($feedId, $request->like);
return response()->json($result);
});
Real-time Comment Updates
class RealtimeFeedService {
private $moitribe;
private $websocket;
public function __construct($moitribe, $websocket) {
$this->moitribe = $moitribe;
$this->websocket = $websocket;
}
public function handleNewComment($appId, $feedId, $comment) {
// Broadcast new comment to connected clients
$this->websocket->broadcast("feed:{$feedId}:new_comment", [
'comment' => $comment,
'timestamp' => time()
]);
}
public function handleLikeUpdate($appId, $feedId, $playerId, $liked) {
// Broadcast like status update
$this->websocket->broadcast("feed:{$feedId}:like_update", [
'player_id' => $playerId,
'liked' => $liked,
'timestamp' => time()
]);
}
}
Error Handling
try {
$statusCode = $moitribe->feeds()->submitComment(
'your-app-id',
'feed-item-123',
'This is a comment!'
);
if ($statusCode === 200) {
echo "Comment submitted successfully\n";
} else {
echo "Failed to submit comment. Status: {$statusCode}\n";
}
} catch (ErrorException $e) {
echo "Authentication error: " . $e->getMessage() . "\n";
} catch (InvalidArgumentException $e) {
echo "Invalid argument: " . $e->getMessage() . "\n";
} catch (Exception $e) {
echo "General error: " . $e->getMessage() . "\n";
}
Best Practices
- Validation: Always validate comment text length and content before submission
- Rate Limiting: Implement rate limiting for comment submissions to prevent spam
- Pagination: Use pagination for comments to avoid loading large datasets at once
- Caching: Cache comment data for short periods to improve performance
- Permissions: Verify user permissions before allowing comment deletion
- Sanitization: Sanitize user input to prevent XSS attacks
- Notifications: Implement notification systems for comment replies and mentions
Related Methods
- getFeedData - Retrieve feed data with items
- submitTextFeed - Create new feed posts
Data Structures
FeedComment Object
Each comment contains:
id- Unique comment identifierfeedId- Parent feed item IDplayerId- Author's player IDtext- Comment text contenttimestamp- Creation timestamplikes- Number of likesparentId- Parent comment ID for threaded repliestaggedPlayers- Array of tagged player IDs
Player Object
Player data included in responses:
id- Player IDusername- Display usernameavatar- Profile image URLlevel- Player level (if applicable)