mirror of
				https://gitlab.com/comunic/comunicapiv2
				synced 2025-11-04 03:24:04 +00:00 
			
		
		
		
	Can update following state of a conversation
This commit is contained in:
		@@ -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
 | 
			
		||||
	 * 
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user