mirror of
https://gitlab.com/comunic/comunicapiv2
synced 2024-11-22 13:29:22 +00:00
Can update following state of a conversation
This commit is contained in:
parent
bd19411a48
commit
4406b8c882
@ -72,6 +72,28 @@ export class ConversationsController {
|
||||
handler.send(this.ConversationToAPI(conv));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update conversation settings
|
||||
*
|
||||
* @param handler Request handler
|
||||
*/
|
||||
public static async UpdateSettings(handler: RequestHandler) : Promise<void> {
|
||||
const convID = await this.GetPostConversationId("conversationID", handler);
|
||||
|
||||
// Update following state, if required
|
||||
if(handler.hasPostParameter("following")) {
|
||||
await ConversationsHelper.SetFollowing(
|
||||
handler.getUserId(),
|
||||
convID,
|
||||
handler.postBool("following")
|
||||
);
|
||||
}
|
||||
|
||||
// TODO : update moderation settings
|
||||
|
||||
handler.success("Conversation information successfully updated!");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get and return safely a conversation ID specified in a $_POST Request
|
||||
*
|
||||
|
@ -55,6 +55,7 @@ export const Routes : Route[] = [
|
||||
{path: "/conversations/getInfoOne", cb: (h) => ConversationsController.GetInfoSingle(h)},
|
||||
{path: "/conversations/getInfosOne", cb: (h) => ConversationsController.GetInfoSingle(h)}, // Legacy
|
||||
|
||||
{path: "/conversations/updateSettings", cb: (h) => ConversationsController.UpdateSettings(h)},
|
||||
|
||||
// Search controller
|
||||
{path: "/search/user", cb: (h) => SearchController.SearchUser(h)},
|
||||
|
@ -52,6 +52,15 @@ export class RequestHandler {
|
||||
return param;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check out whether a post parameter is present into the request or not
|
||||
*
|
||||
* @param name The name of the post field to check
|
||||
*/
|
||||
public hasPostParameter(name: string) : boolean {
|
||||
return this.getPostParam(name) != undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an email address included in a post request
|
||||
*
|
||||
@ -233,7 +242,7 @@ export class RequestHandler {
|
||||
this.responseSent = true;
|
||||
|
||||
if(should_throw)
|
||||
throw Error("Could not complete request! ("+ message +")");
|
||||
throw Error("Could not complete request! ("+ message +")");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -146,6 +146,26 @@ export class ConversationsHelper {
|
||||
}) == 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update following state of the conversation
|
||||
*
|
||||
* @param userID User to update
|
||||
* @param convID Target conversation ID
|
||||
* @param following New status
|
||||
*/
|
||||
public static async SetFollowing(userID: number, convID: number, following: boolean) {
|
||||
await DatabaseHelper.UpdateRows({
|
||||
table: USERS_TABLE,
|
||||
set: {
|
||||
"following": following ? 1 : 0
|
||||
},
|
||||
where: {
|
||||
"conv_id": convID,
|
||||
"user_id": userID
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of members of a conversation
|
||||
*
|
||||
|
@ -23,6 +23,12 @@ export interface QueryInformation {
|
||||
limit ?: number,
|
||||
}
|
||||
|
||||
export interface UpdateInformation {
|
||||
table: string,
|
||||
set: Object,
|
||||
where ?: Object,
|
||||
}
|
||||
|
||||
export interface CountQueryInformation {
|
||||
table: string,
|
||||
where ?: Object
|
||||
@ -177,6 +183,66 @@ export class DatabaseHelper {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform update on the database
|
||||
*
|
||||
* @param info Information about the request
|
||||
* @returns The number of affected rows
|
||||
*/
|
||||
static async UpdateRows(info : UpdateInformation) : Promise<number> {
|
||||
let sql = "UPDATE " + info.table + " SET ";
|
||||
let args = [];
|
||||
|
||||
// Process updates
|
||||
let isFirst = true;
|
||||
for (const key in info.set) {
|
||||
if (info.set.hasOwnProperty(key)) {
|
||||
const value = info.set[key];
|
||||
|
||||
if(!isFirst)
|
||||
sql += ", ";
|
||||
else
|
||||
isFirst = false;
|
||||
|
||||
sql += key + " = ? "
|
||||
args.push(value);
|
||||
}
|
||||
}
|
||||
|
||||
// Process conditions
|
||||
isFirst = true;
|
||||
if(info.where) {
|
||||
sql += " WHERE ";
|
||||
for (const key in info.where) {
|
||||
if (info.where.hasOwnProperty(key)) {
|
||||
const value = info.where[key];
|
||||
|
||||
if(!isFirst)
|
||||
sql += " AND ";
|
||||
else
|
||||
isFirst = false;
|
||||
|
||||
sql += key + " = ? "
|
||||
args.push(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
throw Error("Error : Updates without conditions are blocked for security!");
|
||||
|
||||
// Execute request
|
||||
return await new Promise((resolve, reject) => {
|
||||
this.connection.query(sql, args, (err, results, f) => {
|
||||
if(err){
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
|
||||
resolve(results.affectedRows);
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete entries from a table
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user